org.el 571 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.22b
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum)
  71. (require 'calendar))
  72. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.22b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C registry: A registry for Org links" org-registry)
  175. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  176. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  177. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  178. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  179. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  180. (defcustom org-support-shift-select nil
  181. "Non-nil means, make shift-cursor commands select text when possible.
  182. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  183. selecting a region, or enlarge thusly regions started in this way.
  184. In Org-mode, in special contexts, these same keys are used for other
  185. purposes, important enough to compete with shift selection. Org tries
  186. to balance these needs by supporting `shift-select-mode' outside these
  187. special contexts, under control of this variable.
  188. The default of this variable is nil, to avoid confusing behavior. Shifted
  189. cursor keys will then execute Org commands in the following contexts:
  190. - on a headline, changing TODO state (left/right) and priority (up/down)
  191. - on a time stamp, changing the time
  192. - in a plain list item, changing the bullet type
  193. - in a property definition line, switching between allowed values
  194. - in the BEGIN line of a clock table (changing the time block).
  195. Outside these contexts, the commands will throw an error.
  196. When this variable is t and the cursor is not in a special context,
  197. Org-mode will support shift-selection for making and enlarging regions.
  198. To make this more effective, the bullet cycling will no longer happen
  199. anywhere in an item line, but only if the cursor is exactly on the bullet.
  200. If you set this variable to the symbol `always', then the keys
  201. will not be special in headlines, property lines, and item lines, to make
  202. shift selection work there as well. If this is what you want, you can
  203. use the following alternative commands: `C-c C-t' and `C-c ,' to
  204. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  205. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  206. edited by hand or in column view.
  207. However, when the cursor is on a timestamp, shift-cursor commands
  208. will still edit the time stamp - this is just too good to give up.
  209. XEmacs user should have this variable set to nil, because shift-select-mode
  210. is Emacs 23 only."
  211. :group 'org
  212. :type '(choice
  213. (const :tag "Never" nil)
  214. (const :tag "When outside special context" t)
  215. (const :tag "Everywhere except timestamps" always)))
  216. (defgroup org-startup nil
  217. "Options concerning startup of Org-mode."
  218. :tag "Org Startup"
  219. :group 'org)
  220. (defcustom org-startup-folded t
  221. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  222. This can also be configured on a per-file basis by adding one of
  223. the following lines anywhere in the buffer:
  224. #+STARTUP: fold
  225. #+STARTUP: nofold
  226. #+STARTUP: content"
  227. :group 'org-startup
  228. :type '(choice
  229. (const :tag "nofold: show all" nil)
  230. (const :tag "fold: overview" t)
  231. (const :tag "content: all headlines" content)))
  232. (defcustom org-startup-truncated t
  233. "Non-nil means, entering Org-mode will set `truncate-lines'.
  234. This is useful since some lines containing links can be very long and
  235. uninteresting. Also tables look terrible when wrapped."
  236. :group 'org-startup
  237. :type 'boolean)
  238. (defcustom org-startup-align-all-tables nil
  239. "Non-nil means, align all tables when visiting a file.
  240. This is useful when the column width in tables is forced with <N> cookies
  241. in table fields. Such tables will look correct only after the first re-align.
  242. This can also be configured on a per-file basis by adding one of
  243. the following lines anywhere in the buffer:
  244. #+STARTUP: align
  245. #+STARTUP: noalign"
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defcustom org-insert-mode-line-in-empty-file nil
  249. "Non-nil means insert the first line setting Org-mode in empty files.
  250. When the function `org-mode' is called interactively in an empty file, this
  251. normally means that the file name does not automatically trigger Org-mode.
  252. To ensure that the file will always be in Org-mode in the future, a
  253. line enforcing Org-mode will be inserted into the buffer, if this option
  254. has been set."
  255. :group 'org-startup
  256. :type 'boolean)
  257. (defcustom org-replace-disputed-keys nil
  258. "Non-nil means use alternative key bindings for some keys.
  259. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  260. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  261. If you want to use Org-mode together with one of these other modes,
  262. or more generally if you would like to move some Org-mode commands to
  263. other keys, set this variable and configure the keys with the variable
  264. `org-disputed-keys'.
  265. This option is only relevant at load-time of Org-mode, and must be set
  266. *before* org.el is loaded. Changing it requires a restart of Emacs to
  267. become effective."
  268. :group 'org-startup
  269. :type 'boolean)
  270. (defcustom org-use-extra-keys nil
  271. "Non-nil means use extra key sequence definitions for certain
  272. commands. This happens automatically if you run XEmacs or if
  273. window-system is nil. This variable lets you do the same
  274. manually. You must set it before loading org.
  275. Example: on Carbon Emacs 22 running graphically, with an external
  276. keyboard on a Powerbook, the default way of setting M-left might
  277. not work for either Alt or ESC. Setting this variable will make
  278. it work for ESC."
  279. :group 'org-startup
  280. :type 'boolean)
  281. (if (fboundp 'defvaralias)
  282. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  283. (defcustom org-disputed-keys
  284. '(([(shift up)] . [(meta p)])
  285. ([(shift down)] . [(meta n)])
  286. ([(shift left)] . [(meta -)])
  287. ([(shift right)] . [(meta +)])
  288. ([(control shift right)] . [(meta shift +)])
  289. ([(control shift left)] . [(meta shift -)]))
  290. "Keys for which Org-mode and other modes compete.
  291. This is an alist, cars are the default keys, second element specifies
  292. the alternative to use when `org-replace-disputed-keys' is t.
  293. Keys can be specified in any syntax supported by `define-key'.
  294. The value of this option takes effect only at Org-mode's startup,
  295. therefore you'll have to restart Emacs to apply it after changing."
  296. :group 'org-startup
  297. :type 'alist)
  298. (defun org-key (key)
  299. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  300. Or return the original if not disputed."
  301. (if org-replace-disputed-keys
  302. (let* ((nkey (key-description key))
  303. (x (org-find-if (lambda (x)
  304. (equal (key-description (car x)) nkey))
  305. org-disputed-keys)))
  306. (if x (cdr x) key))
  307. key))
  308. (defun org-find-if (predicate seq)
  309. (catch 'exit
  310. (while seq
  311. (if (funcall predicate (car seq))
  312. (throw 'exit (car seq))
  313. (pop seq)))))
  314. (defun org-defkey (keymap key def)
  315. "Define a key, possibly translated, as returned by `org-key'."
  316. (define-key keymap (org-key key) def))
  317. (defcustom org-ellipsis nil
  318. "The ellipsis to use in the Org-mode outline.
  319. When nil, just use the standard three dots. When a string, use that instead,
  320. When a face, use the standard 3 dots, but with the specified face.
  321. The change affects only Org-mode (which will then use its own display table).
  322. Changing this requires executing `M-x org-mode' in a buffer to become
  323. effective."
  324. :group 'org-startup
  325. :type '(choice (const :tag "Default" nil)
  326. (face :tag "Face" :value org-warning)
  327. (string :tag "String" :value "...#")))
  328. (defvar org-display-table nil
  329. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  330. (defgroup org-keywords nil
  331. "Keywords in Org-mode."
  332. :tag "Org Keywords"
  333. :group 'org)
  334. (defcustom org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].
  339. Changes become only effective after restarting Emacs."
  340. :group 'org-keywords
  341. :type 'string)
  342. (defcustom org-scheduled-string "SCHEDULED:"
  343. "String to mark scheduled TODO entries.
  344. A schedule is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-schedule].
  347. Changes become only effective after restarting Emacs."
  348. :group 'org-keywords
  349. :type 'string)
  350. (defcustom org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry."
  352. :group 'org-keywords
  353. :type 'string)
  354. (defcustom org-clock-string "CLOCK:"
  355. "String used as prefix for timestamps clocking work hours on an item."
  356. :group 'org-keywords
  357. :type 'string)
  358. (defcustom org-comment-string "COMMENT"
  359. "Entries starting with this keyword will never be exported.
  360. An entry can be toggled between COMMENT and normal with
  361. \\[org-toggle-comment].
  362. Changes become only effective after restarting Emacs."
  363. :group 'org-keywords
  364. :type 'string)
  365. (defcustom org-quote-string "QUOTE"
  366. "Entries starting with this keyword will be exported in fixed-width font.
  367. Quoting applies only to the text in the entry following the headline, and does
  368. not extend beyond the next headline, even if that is lower level.
  369. An entry can be toggled between QUOTE and normal with
  370. \\[org-toggle-fixed-width-section]."
  371. :group 'org-keywords
  372. :type 'string)
  373. (defconst org-repeat-re
  374. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  375. "Regular expression for specifying repeated events.
  376. After a match, group 1 contains the repeat expression.")
  377. (defgroup org-structure nil
  378. "Options concerning the general structure of Org-mode files."
  379. :tag "Org Structure"
  380. :group 'org)
  381. (defgroup org-reveal-location nil
  382. "Options about how to make context of a location visible."
  383. :tag "Org Reveal Location"
  384. :group 'org-structure)
  385. (defconst org-context-choice
  386. '(choice
  387. (const :tag "Always" t)
  388. (const :tag "Never" nil)
  389. (repeat :greedy t :tag "Individual contexts"
  390. (cons
  391. (choice :tag "Context"
  392. (const agenda)
  393. (const org-goto)
  394. (const occur-tree)
  395. (const tags-tree)
  396. (const link-search)
  397. (const mark-goto)
  398. (const bookmark-jump)
  399. (const isearch)
  400. (const default))
  401. (boolean))))
  402. "Contexts for the reveal options.")
  403. (defcustom org-show-hierarchy-above '((default . t))
  404. "Non-nil means, show full hierarchy when revealing a location.
  405. Org-mode often shows locations in an org-mode file which might have
  406. been invisible before. When this is set, the hierarchy of headings
  407. above the exposed location is shown.
  408. Turning this off for example for sparse trees makes them very compact.
  409. Instead of t, this can also be an alist specifying this option for different
  410. contexts. Valid contexts are
  411. agenda when exposing an entry from the agenda
  412. org-goto when using the command `org-goto' on key C-c C-j
  413. occur-tree when using the command `org-occur' on key C-c /
  414. tags-tree when constructing a sparse tree based on tags matches
  415. link-search when exposing search matches associated with a link
  416. mark-goto when exposing the jump goal of a mark
  417. bookmark-jump when exposing a bookmark location
  418. isearch when exiting from an incremental search
  419. default default for all contexts not set explicitly"
  420. :group 'org-reveal-location
  421. :type org-context-choice)
  422. (defcustom org-show-following-heading '((default . nil))
  423. "Non-nil means, show following heading when revealing a location.
  424. Org-mode often shows locations in an org-mode file which might have
  425. been invisible before. When this is set, the heading following the
  426. match is shown.
  427. Turning this off for example for sparse trees makes them very compact,
  428. but makes it harder to edit the location of the match. In such a case,
  429. use the command \\[org-reveal] to show more context.
  430. Instead of t, this can also be an alist specifying this option for different
  431. contexts. See `org-show-hierarchy-above' for valid contexts."
  432. :group 'org-reveal-location
  433. :type org-context-choice)
  434. (defcustom org-show-siblings '((default . nil) (isearch t))
  435. "Non-nil means, show all sibling heading when revealing a location.
  436. Org-mode often shows locations in an org-mode file which might have
  437. been invisible before. When this is set, the sibling of the current entry
  438. heading are all made visible. If `org-show-hierarchy-above' is t,
  439. the same happens on each level of the hierarchy above the current entry.
  440. By default this is on for the isearch context, off for all other contexts.
  441. Turning this off for example for sparse trees makes them very compact,
  442. but makes it harder to edit the location of the match. In such a case,
  443. use the command \\[org-reveal] to show more context.
  444. Instead of t, this can also be an alist specifying this option for different
  445. contexts. See `org-show-hierarchy-above' for valid contexts."
  446. :group 'org-reveal-location
  447. :type org-context-choice)
  448. (defcustom org-show-entry-below '((default . nil))
  449. "Non-nil means, show the entry below a headline when revealing a location.
  450. Org-mode often shows locations in an org-mode file which might have
  451. been invisible before. When this is set, the text below the headline that is
  452. exposed is also shown.
  453. By default this is off for all contexts.
  454. Instead of t, this can also be an alist specifying this option for different
  455. contexts. See `org-show-hierarchy-above' for valid contexts."
  456. :group 'org-reveal-location
  457. :type org-context-choice)
  458. (defcustom org-indirect-buffer-display 'other-window
  459. "How should indirect tree buffers be displayed?
  460. This applies to indirect buffers created with the commands
  461. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  462. Valid values are:
  463. current-window Display in the current window
  464. other-window Just display in another window.
  465. dedicated-frame Create one new frame, and re-use it each time.
  466. new-frame Make a new frame each time. Note that in this case
  467. previously-made indirect buffers are kept, and you need to
  468. kill these buffers yourself."
  469. :group 'org-structure
  470. :group 'org-agenda-windows
  471. :type '(choice
  472. (const :tag "In current window" current-window)
  473. (const :tag "In current frame, other window" other-window)
  474. (const :tag "Each time a new frame" new-frame)
  475. (const :tag "One dedicated frame" dedicated-frame)))
  476. (defgroup org-cycle nil
  477. "Options concerning visibility cycling in Org-mode."
  478. :tag "Org Cycle"
  479. :group 'org-structure)
  480. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  481. "Names of drawers. Drawers are not opened by cycling on the headline above.
  482. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  483. this:
  484. :DRAWERNAME:
  485. .....
  486. :END:
  487. The drawer \"PROPERTIES\" is special for capturing properties through
  488. the property API.
  489. Drawers can be defined on the per-file basis with a line like:
  490. #+DRAWERS: HIDDEN STATE PROPERTIES"
  491. :group 'org-structure
  492. :type '(repeat (string :tag "Drawer Name")))
  493. (defcustom org-cycle-global-at-bob nil
  494. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  495. This makes it possible to do global cycling without having to use S-TAB or
  496. C-u TAB. For this special case to work, the first line of the buffer
  497. must not be a headline - it may be empty or some other text. When used in
  498. this way, `org-cycle-hook' is disables temporarily, to make sure the
  499. cursor stays at the beginning of the buffer.
  500. When this option is nil, don't do anything special at the beginning
  501. of the buffer."
  502. :group 'org-cycle
  503. :type 'boolean)
  504. (defcustom org-cycle-emulate-tab t
  505. "Where should `org-cycle' emulate TAB.
  506. nil Never
  507. white Only in completely white lines
  508. whitestart Only at the beginning of lines, before the first non-white char
  509. t Everywhere except in headlines
  510. exc-hl-bol Everywhere except at the start of a headline
  511. If TAB is used in a place where it does not emulate TAB, the current subtree
  512. visibility is cycled."
  513. :group 'org-cycle
  514. :type '(choice (const :tag "Never" nil)
  515. (const :tag "Only in completely white lines" white)
  516. (const :tag "Before first char in a line" whitestart)
  517. (const :tag "Everywhere except in headlines" t)
  518. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  519. ))
  520. (defcustom org-cycle-separator-lines 2
  521. "Number of empty lines needed to keep an empty line between collapsed trees.
  522. If you leave an empty line between the end of a subtree and the following
  523. headline, this empty line is hidden when the subtree is folded.
  524. Org-mode will leave (exactly) one empty line visible if the number of
  525. empty lines is equal or larger to the number given in this variable.
  526. So the default 2 means, at least 2 empty lines after the end of a subtree
  527. are needed to produce free space between a collapsed subtree and the
  528. following headline.
  529. Special case: when 0, never leave empty lines in collapsed view."
  530. :group 'org-cycle
  531. :type 'integer)
  532. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  533. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  534. org-cycle-hide-drawers
  535. org-cycle-show-empty-lines
  536. org-optimize-window-after-visibility-change)
  537. "Hook that is run after `org-cycle' has changed the buffer visibility.
  538. The function(s) in this hook must accept a single argument which indicates
  539. the new state that was set by the most recent `org-cycle' command. The
  540. argument is a symbol. After a global state change, it can have the values
  541. `overview', `content', or `all'. After a local state change, it can have
  542. the values `folded', `children', or `subtree'."
  543. :group 'org-cycle
  544. :type 'hook)
  545. (defgroup org-edit-structure nil
  546. "Options concerning structure editing in Org-mode."
  547. :tag "Org Edit Structure"
  548. :group 'org-structure)
  549. (defcustom org-odd-levels-only nil
  550. "Non-nil means, skip even levels and only use odd levels for the outline.
  551. This has the effect that two stars are being added/taken away in
  552. promotion/demotion commands. It also influences how levels are
  553. handled by the exporters.
  554. Changing it requires restart of `font-lock-mode' to become effective
  555. for fontification also in regions already fontified.
  556. You may also set this on a per-file basis by adding one of the following
  557. lines to the buffer:
  558. #+STARTUP: odd
  559. #+STARTUP: oddeven"
  560. :group 'org-edit-structure
  561. :group 'org-font-lock
  562. :type 'boolean)
  563. (defcustom org-adapt-indentation t
  564. "Non-nil means, adapt indentation when promoting and demoting.
  565. When this is set and the *entire* text in an entry is indented, the
  566. indentation is increased by one space in a demotion command, and
  567. decreased by one in a promotion command. If any line in the entry
  568. body starts at column 0, indentation is not changed at all."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-special-ctrl-a/e nil
  572. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  573. When t, `C-a' will bring back the cursor to the beginning of the
  574. headline text, i.e. after the stars and after a possible TODO keyword.
  575. In an item, this will be the position after the bullet.
  576. When the cursor is already at that position, another `C-a' will bring
  577. it to the beginning of the line.
  578. `C-e' will jump to the end of the headline, ignoring the presence of tags
  579. in the headline. A second `C-e' will then jump to the true end of the
  580. line, after any tags.
  581. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  582. and only a directly following, identical keypress will bring the cursor
  583. to the special positions."
  584. :group 'org-edit-structure
  585. :type '(choice
  586. (const :tag "off" nil)
  587. (const :tag "after bullet first" t)
  588. (const :tag "border first" reversed)))
  589. (if (fboundp 'defvaralias)
  590. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  591. (defcustom org-special-ctrl-k nil
  592. "Non-nil means `C-k' will behave specially in headlines.
  593. When nil, `C-k' will call the default `kill-line' command.
  594. When t, the following will happen while the cursor is in the headline:
  595. - When the cursor is at the beginning of a headline, kill the entire
  596. line and possible the folded subtree below the line.
  597. - When in the middle of the headline text, kill the headline up to the tags.
  598. - When after the headline text, kill the tags."
  599. :group 'org-edit-structure
  600. :type 'boolean)
  601. (defcustom org-yank-folded-subtrees t
  602. "Non-nil means, when yanking subtrees, fold them.
  603. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  604. it starts with a heading and all other headings in it are either children
  605. or siblings, then fold all the subtrees. However, do this only if no
  606. text after the yank would be swallowed into a folded tree by this action."
  607. :group 'org-edit-structure
  608. :type 'boolean)
  609. (defcustom org-yank-adjusted-subtrees nil
  610. "Non-nil means, when yanking subtrees, adjust the level.
  611. With this setting, `org-paste-subtree' is used to insert the subtree, see
  612. this function for details."
  613. :group 'org-edit-structure
  614. :type 'boolean)
  615. (defcustom org-M-RET-may-split-line '((default . t))
  616. "Non-nil means, M-RET will split the line at the cursor position.
  617. When nil, it will go to the end of the line before making a
  618. new line.
  619. You may also set this option in a different way for different
  620. contexts. Valid contexts are:
  621. headline when creating a new headline
  622. item when creating a new item
  623. table in a table field
  624. default the value to be used for all contexts not explicitly
  625. customized"
  626. :group 'org-structure
  627. :group 'org-table
  628. :type '(choice
  629. (const :tag "Always" t)
  630. (const :tag "Never" nil)
  631. (repeat :greedy t :tag "Individual contexts"
  632. (cons
  633. (choice :tag "Context"
  634. (const headline)
  635. (const item)
  636. (const table)
  637. (const default))
  638. (boolean)))))
  639. (defcustom org-insert-heading-respect-content nil
  640. "Non-nil means, insert new headings after the current subtree.
  641. When nil, the new heading is created directly after the current line.
  642. The commands \\[org-insert-heading-respect-content] and
  643. \\[org-insert-todo-heading-respect-content] turn this variable on
  644. for the duration of the command."
  645. :group 'org-structure
  646. :type 'boolean)
  647. (defcustom org-blank-before-new-entry '((heading . auto)
  648. (plain-list-item . auto))
  649. "Should `org-insert-heading' leave a blank line before new heading/item?
  650. The value is an alist, with `heading' and `plain-list-item' as car,
  651. and a boolean flag as cdr."
  652. :group 'org-edit-structure
  653. :type '(list
  654. (cons (const heading)
  655. (choice (const :tag "Never" nil)
  656. (const :tag "Always" t)
  657. (const :tag "Auto" auto)))
  658. (cons (const plain-list-item)
  659. (choice (const :tag "Never" nil)
  660. (const :tag "Always" t)
  661. (const :tag "Auto" auto)))))
  662. (defcustom org-insert-heading-hook nil
  663. "Hook being run after inserting a new heading."
  664. :group 'org-edit-structure
  665. :type 'hook)
  666. (defcustom org-enable-fixed-width-editor t
  667. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  668. This currently only means, they are never auto-wrapped.
  669. When nil, such lines will be treated like ordinary lines.
  670. See also the QUOTE keyword."
  671. :group 'org-edit-structure
  672. :type 'boolean)
  673. (defcustom org-edit-src-region-extra nil
  674. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  675. For examples see the function `org-edit-src-find-region-and-lang'.
  676. The regular expression identifying the begin marker should end with a newline,
  677. and the regexp marking the end line should start with a newline, to make sure
  678. there are kept outside the narrowed region."
  679. :group 'org-edit-structure
  680. :type '(repeat
  681. (list
  682. (regexp :tag "begin regexp")
  683. (regexp :tag "end regexp")
  684. (choice :tag "language"
  685. (string :tag "specify")
  686. (integer :tag "from match group")
  687. (const :tag "from `lang' element")
  688. (const :tag "from `style' element")))))
  689. (defcustom org-coderef-label-format "(ref:%s)"
  690. "The default coderef format.
  691. This format string will be used to search for coderef labels in literal
  692. examples (EXAMPLE and SRC blocks). The format can be overwritten
  693. an individual literal example with the -f option, like
  694. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  695. ...
  696. #+END_SRC
  697. If you want to use this for HTML export, make sure that the format does
  698. not introduce special font-locking, and avoid the HTML special
  699. characters `<', `>', and `&'. The reason for this restriction is that
  700. the labels are searched for only after htmlize has done its job."
  701. :group 'org-edit-structure ; FIXME this is not in the right group
  702. :type 'string)
  703. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  704. "The mode that should be used to edit fixed-width regions.
  705. These are the regions where each line starts with a colon."
  706. :group 'org-edit-structure
  707. :type '(choice
  708. (const artist-mode)
  709. (const picture-mode)
  710. (const fundamental-mode)
  711. (function :tag "Other (specify)")))
  712. (defcustom org-goto-auto-isearch t
  713. "Non-nil means, typing characters in org-goto starts incremental search."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defgroup org-sparse-trees nil
  717. "Options concerning sparse trees in Org-mode."
  718. :tag "Org Sparse Trees"
  719. :group 'org-structure)
  720. (defcustom org-highlight-sparse-tree-matches t
  721. "Non-nil means, highlight all matches that define a sparse tree.
  722. The highlights will automatically disappear the next time the buffer is
  723. changed by an edit command."
  724. :group 'org-sparse-trees
  725. :type 'boolean)
  726. (defcustom org-remove-highlights-with-change t
  727. "Non-nil means, any change to the buffer will remove temporary highlights.
  728. Such highlights are created by `org-occur' and `org-clock-display'.
  729. When nil, `C-c C-c needs to be used to get rid of the highlights.
  730. The highlights created by `org-preview-latex-fragment' always need
  731. `C-c C-c' to be removed."
  732. :group 'org-sparse-trees
  733. :group 'org-time
  734. :type 'boolean)
  735. (defcustom org-occur-hook '(org-first-headline-recenter)
  736. "Hook that is run after `org-occur' has constructed a sparse tree.
  737. This can be used to recenter the window to show as much of the structure
  738. as possible."
  739. :group 'org-sparse-trees
  740. :type 'hook)
  741. (defgroup org-imenu-and-speedbar nil
  742. "Options concerning imenu and speedbar in Org-mode."
  743. :tag "Org Imenu and Speedbar"
  744. :group 'org-structure)
  745. (defcustom org-imenu-depth 2
  746. "The maximum level for Imenu access to Org-mode headlines.
  747. This also applied for speedbar access."
  748. :group 'org-imenu-and-speedbar
  749. :type 'number)
  750. (defgroup org-table nil
  751. "Options concerning tables in Org-mode."
  752. :tag "Org Table"
  753. :group 'org)
  754. (defcustom org-enable-table-editor 'optimized
  755. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  756. When nil, such lines will be treated like ordinary lines.
  757. When equal to the symbol `optimized', the table editor will be optimized to
  758. do the following:
  759. - Automatic overwrite mode in front of whitespace in table fields.
  760. This makes the structure of the table stay in tact as long as the edited
  761. field does not exceed the column width.
  762. - Minimize the number of realigns. Normally, the table is aligned each time
  763. TAB or RET are pressed to move to another field. With optimization this
  764. happens only if changes to a field might have changed the column width.
  765. Optimization requires replacing the functions `self-insert-command',
  766. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  767. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  768. very good at guessing when a re-align will be necessary, but you can always
  769. force one with \\[org-ctrl-c-ctrl-c].
  770. If you would like to use the optimized version in Org-mode, but the
  771. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  772. This variable can be used to turn on and off the table editor during a session,
  773. but in order to toggle optimization, a restart is required.
  774. See also the variable `org-table-auto-blank-field'."
  775. :group 'org-table
  776. :type '(choice
  777. (const :tag "off" nil)
  778. (const :tag "on" t)
  779. (const :tag "on, optimized" optimized)))
  780. (defcustom org-table-tab-recognizes-table.el t
  781. "Non-nil means, TAB will automatically notice a table.el table.
  782. When it sees such a table, it moves point into it and - if necessary -
  783. calls `table-recognize-table'."
  784. :group 'org-table-editing
  785. :type 'boolean)
  786. (defgroup org-link nil
  787. "Options concerning links in Org-mode."
  788. :tag "Org Link"
  789. :group 'org)
  790. (defvar org-link-abbrev-alist-local nil
  791. "Buffer-local version of `org-link-abbrev-alist', which see.
  792. The value of this is taken from the #+LINK lines.")
  793. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  794. (defcustom org-link-abbrev-alist nil
  795. "Alist of link abbreviations.
  796. The car of each element is a string, to be replaced at the start of a link.
  797. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  798. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  799. [[linkkey:tag][description]]
  800. If REPLACE is a string, the tag will simply be appended to create the link.
  801. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  802. the placeholder \"%h\" will cause a url-encoded version of the tag to
  803. be inserted at that point (see the function `url-hexify-string').
  804. REPLACE may also be a function that will be called with the tag as the
  805. only argument to create the link, which should be returned as a string.
  806. See the manual for examples."
  807. :group 'org-link
  808. :type '(repeat
  809. (cons
  810. (string :tag "Protocol")
  811. (choice
  812. (string :tag "Format")
  813. (function)))))
  814. (defcustom org-descriptive-links t
  815. "Non-nil means, hide link part and only show description of bracket links.
  816. Bracket links are like [[link][description]]. This variable sets the initial
  817. state in new org-mode buffers. The setting can then be toggled on a
  818. per-buffer basis from the Org->Hyperlinks menu."
  819. :group 'org-link
  820. :type 'boolean)
  821. (defcustom org-link-file-path-type 'adaptive
  822. "How the path name in file links should be stored.
  823. Valid values are:
  824. relative Relative to the current directory, i.e. the directory of the file
  825. into which the link is being inserted.
  826. absolute Absolute path, if possible with ~ for home directory.
  827. noabbrev Absolute path, no abbreviation of home directory.
  828. adaptive Use relative path for files in the current directory and sub-
  829. directories of it. For other files, use an absolute path."
  830. :group 'org-link
  831. :type '(choice
  832. (const relative)
  833. (const absolute)
  834. (const noabbrev)
  835. (const adaptive)))
  836. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  837. "Types of links that should be activated in Org-mode files.
  838. This is a list of symbols, each leading to the activation of a certain link
  839. type. In principle, it does not hurt to turn on most link types - there may
  840. be a small gain when turning off unused link types. The types are:
  841. bracket The recommended [[link][description]] or [[link]] links with hiding.
  842. angular Links in angular brackets that may contain whitespace like
  843. <bbdb:Carsten Dominik>.
  844. plain Plain links in normal text, no whitespace, like http://google.com.
  845. radio Text that is matched by a radio target, see manual for details.
  846. tag Tag settings in a headline (link to tag search).
  847. date Time stamps (link to calendar).
  848. footnote Footnote labels.
  849. Changing this variable requires a restart of Emacs to become effective."
  850. :group 'org-link
  851. :type '(set :greedy t
  852. (const :tag "Double bracket links (new style)" bracket)
  853. (const :tag "Angular bracket links (old style)" angular)
  854. (const :tag "Plain text links" plain)
  855. (const :tag "Radio target matches" radio)
  856. (const :tag "Tags" tag)
  857. (const :tag "Timestamps" date)
  858. (const :tag "Footnotes" footnote)))
  859. (defcustom org-make-link-description-function nil
  860. "Function to use to generate link descriptions from links. If
  861. nil the link location will be used. This function must take two
  862. parameters; the first is the link and the second the description
  863. org-insert-link has generated, and should return the description
  864. to use."
  865. :group 'org-link
  866. :type 'function)
  867. (defgroup org-link-store nil
  868. "Options concerning storing links in Org-mode."
  869. :tag "Org Store Link"
  870. :group 'org-link)
  871. (defcustom org-email-link-description-format "Email %c: %.30s"
  872. "Format of the description part of a link to an email or usenet message.
  873. The following %-escapes will be replaced by corresponding information:
  874. %F full \"From\" field
  875. %f name, taken from \"From\" field, address if no name
  876. %T full \"To\" field
  877. %t first name in \"To\" field, address if no name
  878. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  879. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  880. %s subject
  881. %m message-id.
  882. You may use normal field width specification between the % and the letter.
  883. This is for example useful to limit the length of the subject.
  884. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  885. :group 'org-link-store
  886. :type 'string)
  887. (defcustom org-from-is-user-regexp
  888. (let (r1 r2)
  889. (when (and user-mail-address (not (string= user-mail-address "")))
  890. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  891. (when (and user-full-name (not (string= user-full-name "")))
  892. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  893. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  894. "Regexp matched against the \"From:\" header of an email or usenet message.
  895. It should match if the message is from the user him/herself."
  896. :group 'org-link-store
  897. :type 'regexp)
  898. (defcustom org-link-to-org-use-id 'create-if-interactive
  899. "Non-nil means, storing a link to an Org file will use entry IDs.
  900. Note that before this variable is even considered, org-id must be loaded,
  901. to please customize `org-modules' and turn it on.
  902. The variable can have the following values:
  903. t Create an ID if needed to make a link to the current entry.
  904. create-if-interactive
  905. If `org-store-link' is called directly (interactively, as a user
  906. command), do create an ID to support the link. But when doing the
  907. job for remember, only use the ID if it already exists. The
  908. purpose of this setting is to avoid proliferation of unwanted
  909. IDs, just because you happen to be in an Org file when you
  910. call `org-remember' that automatically and preemptively
  911. creates a link. If you do want to get an ID link in a remember
  912. template to an entry not having an ID, create it first by
  913. explicitly creating a link to it, using `C-c C-l' first.
  914. use-existing
  915. Use existing ID, do not create one.
  916. nil Never use an ID to make a link, instead link using a text search for
  917. the headline text."
  918. :group 'org-link-store
  919. :type '(choice
  920. (const :tag "Create ID to make link" t)
  921. (const :tag "Create if string link interactively"
  922. 'create-if-interactive)
  923. (const :tag "Only use existing" 'use-existing)
  924. (const :tag "Do not use ID to create link" nil)))
  925. (defcustom org-context-in-file-links t
  926. "Non-nil means, file links from `org-store-link' contain context.
  927. A search string will be added to the file name with :: as separator and
  928. used to find the context when the link is activated by the command
  929. `org-open-at-point'.
  930. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  931. negates this setting for the duration of the command."
  932. :group 'org-link-store
  933. :type 'boolean)
  934. (defcustom org-keep-stored-link-after-insertion nil
  935. "Non-nil means, keep link in list for entire session.
  936. The command `org-store-link' adds a link pointing to the current
  937. location to an internal list. These links accumulate during a session.
  938. The command `org-insert-link' can be used to insert links into any
  939. Org-mode file (offering completion for all stored links). When this
  940. option is nil, every link which has been inserted once using \\[org-insert-link]
  941. will be removed from the list, to make completing the unused links
  942. more efficient."
  943. :group 'org-link-store
  944. :type 'boolean)
  945. (defgroup org-link-follow nil
  946. "Options concerning following links in Org-mode."
  947. :tag "Org Follow Link"
  948. :group 'org-link)
  949. (defcustom org-link-translation-function nil
  950. "Function to translate links with different syntax to Org syntax.
  951. This can be used to translate links created for example by the Planner
  952. or emacs-wiki packages to Org syntax.
  953. The function must accept two parameters, a TYPE containing the link
  954. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  955. which is everything after the link protocol. It should return a cons
  956. with possibly modified values of type and path.
  957. Org contains a function for this, so if you set this variable to
  958. `org-translate-link-from-planner', you should be able follow many
  959. links created by planner."
  960. :group 'org-link-follow
  961. :type 'function)
  962. (defcustom org-follow-link-hook nil
  963. "Hook that is run after a link has been followed."
  964. :group 'org-link-follow
  965. :type 'hook)
  966. (defcustom org-tab-follows-link nil
  967. "Non-nil means, on links TAB will follow the link.
  968. Needs to be set before org.el is loaded.
  969. This really should not be used, it does not make sense, and the
  970. implementation is bad."
  971. :group 'org-link-follow
  972. :type 'boolean)
  973. (defcustom org-return-follows-link nil
  974. "Non-nil means, on links RET will follow the link.
  975. Needs to be set before org.el is loaded."
  976. :group 'org-link-follow
  977. :type 'boolean)
  978. (defcustom org-mouse-1-follows-link
  979. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  980. "Non-nil means, mouse-1 on a link will follow the link.
  981. A longer mouse click will still set point. Does not work on XEmacs.
  982. Needs to be set before org.el is loaded."
  983. :group 'org-link-follow
  984. :type 'boolean)
  985. (defcustom org-mark-ring-length 4
  986. "Number of different positions to be recorded in the ring
  987. Changing this requires a restart of Emacs to work correctly."
  988. :group 'org-link-follow
  989. :type 'integer)
  990. (defcustom org-link-frame-setup
  991. '((vm . vm-visit-folder-other-frame)
  992. (gnus . gnus-other-frame)
  993. (file . find-file-other-window))
  994. "Setup the frame configuration for following links.
  995. When following a link with Emacs, it may often be useful to display
  996. this link in another window or frame. This variable can be used to
  997. set this up for the different types of links.
  998. For VM, use any of
  999. `vm-visit-folder'
  1000. `vm-visit-folder-other-frame'
  1001. For Gnus, use any of
  1002. `gnus'
  1003. `gnus-other-frame'
  1004. `org-gnus-no-new-news'
  1005. For FILE, use any of
  1006. `find-file'
  1007. `find-file-other-window'
  1008. `find-file-other-frame'
  1009. For the calendar, use the variable `calendar-setup'.
  1010. For BBDB, it is currently only possible to display the matches in
  1011. another window."
  1012. :group 'org-link-follow
  1013. :type '(list
  1014. (cons (const vm)
  1015. (choice
  1016. (const vm-visit-folder)
  1017. (const vm-visit-folder-other-window)
  1018. (const vm-visit-folder-other-frame)))
  1019. (cons (const gnus)
  1020. (choice
  1021. (const gnus)
  1022. (const gnus-other-frame)
  1023. (const org-gnus-no-new-news)))
  1024. (cons (const file)
  1025. (choice
  1026. (const find-file)
  1027. (const find-file-other-window)
  1028. (const find-file-other-frame)))))
  1029. (defcustom org-display-internal-link-with-indirect-buffer nil
  1030. "Non-nil means, use indirect buffer to display infile links.
  1031. Activating internal links (from one location in a file to another location
  1032. in the same file) normally just jumps to the location. When the link is
  1033. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1034. another window. When this option is set, the other window actually displays
  1035. an indirect buffer clone of the current buffer, to avoid any visibility
  1036. changes to the current buffer."
  1037. :group 'org-link-follow
  1038. :type 'boolean)
  1039. (defcustom org-open-non-existing-files nil
  1040. "Non-nil means, `org-open-file' will open non-existing files.
  1041. When nil, an error will be generated."
  1042. :group 'org-link-follow
  1043. :type 'boolean)
  1044. (defcustom org-open-directory-means-index-dot-org nil
  1045. "Non-nil means, a link to a directory really means to index.org.
  1046. When nil, following a directory link will run dired or open a finder/explorer
  1047. window on that directory."
  1048. :group 'org-link-follow
  1049. :type 'boolean)
  1050. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1051. "Function and arguments to call for following mailto links.
  1052. This is a list with the first element being a lisp function, and the
  1053. remaining elements being arguments to the function. In string arguments,
  1054. %a will be replaced by the address, and %s will be replaced by the subject
  1055. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1056. :group 'org-link-follow
  1057. :type '(choice
  1058. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1059. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1060. (const :tag "message-mail" (message-mail "%a" "%s"))
  1061. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1062. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1063. "Non-nil means, ask for confirmation before executing shell links.
  1064. Shell links can be dangerous: just think about a link
  1065. [[shell:rm -rf ~/*][Google Search]]
  1066. This link would show up in your Org-mode document as \"Google Search\",
  1067. but really it would remove your entire home directory.
  1068. Therefore we advise against setting this variable to nil.
  1069. Just change it to `y-or-n-p' of you want to confirm with a
  1070. single keystroke rather than having to type \"yes\"."
  1071. :group 'org-link-follow
  1072. :type '(choice
  1073. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1074. (const :tag "with y-or-n (faster)" y-or-n-p)
  1075. (const :tag "no confirmation (dangerous)" nil)))
  1076. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1078. Elisp links can be dangerous: just think about a link
  1079. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defconst org-file-apps-defaults-gnu
  1091. '((remote . emacs)
  1092. (system . mailcap)
  1093. (t . mailcap))
  1094. "Default file applications on a UNIX or GNU/Linux system.
  1095. See `org-file-apps'.")
  1096. (defconst org-file-apps-defaults-macosx
  1097. '((remote . emacs)
  1098. (t . "open %s")
  1099. (system . "open %s")
  1100. ("ps.gz" . "gv %s")
  1101. ("eps.gz" . "gv %s")
  1102. ("dvi" . "xdvi %s")
  1103. ("fig" . "xfig %s"))
  1104. "Default file applications on a MacOS X system.
  1105. The system \"open\" is known as a default, but we use X11 applications
  1106. for some files for which the OS does not have a good default.
  1107. See `org-file-apps'.")
  1108. (defconst org-file-apps-defaults-windowsnt
  1109. (list
  1110. '(remote . emacs)
  1111. (cons t
  1112. (list (if (featurep 'xemacs)
  1113. 'mswindows-shell-execute
  1114. 'w32-shell-execute)
  1115. "open" 'file))
  1116. (cons 'system
  1117. (list (if (featurep 'xemacs)
  1118. 'mswindows-shell-execute
  1119. 'w32-shell-execute)
  1120. "open" 'file)))
  1121. "Default file applications on a Windows NT system.
  1122. The system \"open\" is used for most files.
  1123. See `org-file-apps'.")
  1124. (defcustom org-file-apps
  1125. '(
  1126. (auto-mode . emacs)
  1127. ("\\.x?html?\\'" . default)
  1128. ("\\.pdf\\'" . default)
  1129. )
  1130. "External applications for opening `file:path' items in a document.
  1131. Org-mode uses system defaults for different file types, but
  1132. you can use this variable to set the application for a given file
  1133. extension. The entries in this list are cons cells where the car identifies
  1134. files and the cdr the corresponding command. Possible values for the
  1135. file identifier are
  1136. \"regex\" Regular expression matched against the file name. For backward
  1137. compatibility, this can also be a string with only alphanumeric
  1138. characters, which is then interpreted as an extension.
  1139. `directory' Matches a directory
  1140. `remote' Matches a remote file, accessible through tramp or efs.
  1141. Remote files most likely should be visited through Emacs
  1142. because external applications cannot handle such paths.
  1143. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1144. so all files Emacs knows how to handle. Using this with
  1145. command `emacs' will open most files in Emacs. Beware that this
  1146. will also open html files inside Emacs, unless you add
  1147. (\"html\" . default) to the list as well.
  1148. t Default for files not matched by any of the other options.
  1149. `system' The system command to open files, like `open' on Windows
  1150. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1151. that will be selected if you call `C-c C-o' with a double
  1152. `C-u C-u' prefix.
  1153. Possible values for the command are:
  1154. `emacs' The file will be visited by the current Emacs process.
  1155. `default' Use the default application for this file type, which is the
  1156. association for t in the list, most likely in the system-specific
  1157. part.
  1158. This can be used to overrule an unwanted setting in the
  1159. system-specific variable.
  1160. `system' Use the system command for opening files, like \"open\".
  1161. This command is specified by the entry whose car is `system'.
  1162. Most likely, the system-specific version of this variable
  1163. does define this command, but you can overrule/replace it
  1164. here.
  1165. string A command to be executed by a shell; %s will be replaced
  1166. by the path to the file.
  1167. sexp A Lisp form which will be evaluated. The file path will
  1168. be available in the Lisp variable `file'.
  1169. For more examples, see the system specific constants
  1170. `org-file-apps-defaults-macosx'
  1171. `org-file-apps-defaults-windowsnt'
  1172. `org-file-apps-defaults-gnu'."
  1173. :group 'org-link-follow
  1174. :type '(repeat
  1175. (cons (choice :value ""
  1176. (string :tag "Extension")
  1177. (const :tag "System command to open files" system)
  1178. (const :tag "Default for unrecognized files" t)
  1179. (const :tag "Remote file" remote)
  1180. (const :tag "Links to a directory" directory)
  1181. (const :tag "Any files that have Emacs modes"
  1182. auto-mode))
  1183. (choice :value ""
  1184. (const :tag "Visit with Emacs" emacs)
  1185. (const :tag "Use default" default)
  1186. (const :tag "Use the system command" system)
  1187. (string :tag "Command")
  1188. (sexp :tag "Lisp form")))))
  1189. (defgroup org-refile nil
  1190. "Options concerning refiling entries in Org-mode."
  1191. :tag "Org Refile"
  1192. :group 'org)
  1193. (defcustom org-directory "~/org"
  1194. "Directory with org files.
  1195. This is just a default location to look for Org files. There is no need
  1196. at all to put your files into this directory. It is only used in the
  1197. following situations:
  1198. 1. When a remember template specifies a target file that is not an
  1199. absolute path. The path will then be interpreted relative to
  1200. `org-directory'
  1201. 2. When a remember note is filed away in an interactive way (when exiting the
  1202. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1203. with `org-directory' as the default path."
  1204. :group 'org-refile
  1205. :group 'org-remember
  1206. :type 'directory)
  1207. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1208. "Default target for storing notes.
  1209. Used by the hooks for remember.el. This can be a string, or nil to mean
  1210. the value of `remember-data-file'.
  1211. You can set this on a per-template basis with the variable
  1212. `org-remember-templates'."
  1213. :group 'org-refile
  1214. :group 'org-remember
  1215. :type '(choice
  1216. (const :tag "Default from remember-data-file" nil)
  1217. file))
  1218. (defcustom org-goto-interface 'outline
  1219. "The default interface to be used for `org-goto'.
  1220. Allowed values are:
  1221. outline The interface shows an outline of the relevant file
  1222. and the correct heading is found by moving through
  1223. the outline or by searching with incremental search.
  1224. outline-path-completion Headlines in the current buffer are offered via
  1225. completion. This is the interface also used by
  1226. the refile command."
  1227. :group 'org-refile
  1228. :type '(choice
  1229. (const :tag "Outline" outline)
  1230. (const :tag "Outline-path-completion" outline-path-completion)))
  1231. (defcustom org-goto-max-level 5
  1232. "Maximum level to be considered when running org-goto with refile interface."
  1233. :group 'org-refile
  1234. :type 'number)
  1235. (defcustom org-reverse-note-order nil
  1236. "Non-nil means, store new notes at the beginning of a file or entry.
  1237. When nil, new notes will be filed to the end of a file or entry.
  1238. This can also be a list with cons cells of regular expressions that
  1239. are matched against file names, and values."
  1240. :group 'org-remember
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Reverse always" t)
  1244. (const :tag "Reverse never" nil)
  1245. (repeat :tag "By file name regexp"
  1246. (cons regexp boolean))))
  1247. (defcustom org-refile-targets nil
  1248. "Targets for refiling entries with \\[org-refile].
  1249. This is list of cons cells. Each cell contains:
  1250. - a specification of the files to be considered, either a list of files,
  1251. or a symbol whose function or variable value will be used to retrieve
  1252. a file name or a list of file names. If you use `org-agenda-files' for
  1253. that, all agenda files will be scanned for targets. Nil means, consider
  1254. headings in the current buffer.
  1255. - A specification of how to find candidate refile targets. This may be
  1256. any of:
  1257. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1258. This tag has to be present in all target headlines, inheritance will
  1259. not be considered.
  1260. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1261. todo keyword.
  1262. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1263. headlines that are refiling targets.
  1264. - a cons cell (:level . N). Any headline of level N is considered a target.
  1265. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1266. When this variable is nil, all top-level headlines in the current buffer
  1267. are used, equivalent to the value `((nil . (:level . 1))'."
  1268. :group 'org-refile
  1269. :type '(repeat
  1270. (cons
  1271. (choice :value org-agenda-files
  1272. (const :tag "All agenda files" org-agenda-files)
  1273. (const :tag "Current buffer" nil)
  1274. (function) (variable) (file))
  1275. (choice :tag "Identify target headline by"
  1276. (cons :tag "Specific tag" (const :value :tag) (string))
  1277. (cons :tag "TODO keyword" (const :value :todo) (string))
  1278. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1279. (cons :tag "Level number" (const :value :level) (integer))
  1280. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1281. (defcustom org-refile-use-outline-path nil
  1282. "Non-nil means, provide refile targets as paths.
  1283. So a level 3 headline will be available as level1/level2/level3.
  1284. When the value is `file', also include the file name (without directory)
  1285. into the path. When `full-file-path', include the full file path."
  1286. :group 'org-refile
  1287. :type '(choice
  1288. (const :tag "Not" nil)
  1289. (const :tag "Yes" t)
  1290. (const :tag "Start with file name" file)
  1291. (const :tag "Start with full file path" full-file-path)))
  1292. (defcustom org-outline-path-complete-in-steps t
  1293. "Non-nil means, complete the outline path in hierarchical steps.
  1294. When Org-mode uses the refile interface to select an outline path
  1295. \(see variable `org-refile-use-outline-path'), the completion of
  1296. the path can be done is a single go, or if can be done in steps down
  1297. the headline hierarchy. Going in steps is probably the best if you
  1298. do not use a special completion package like `ido' or `icicles'.
  1299. However, when using these packages, going in one step can be very
  1300. fast, while still showing the whole path to the entry."
  1301. :group 'org-refile
  1302. :type 'boolean)
  1303. (defgroup org-todo nil
  1304. "Options concerning TODO items in Org-mode."
  1305. :tag "Org TODO"
  1306. :group 'org)
  1307. (defgroup org-progress nil
  1308. "Options concerning Progress logging in Org-mode."
  1309. :tag "Org Progress"
  1310. :group 'org-time)
  1311. (defvar org-todo-interpretation-widgets
  1312. '(
  1313. (:tag "Sequence (cycling hits every state)" sequence)
  1314. (:tag "Type (cycling directly to DONE)" type))
  1315. "The available interpretation symbols for customizing
  1316. `org-todo-keywords'.
  1317. Interested libraries should add to this list.")
  1318. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1319. "List of TODO entry keyword sequences and their interpretation.
  1320. \\<org-mode-map>This is a list of sequences.
  1321. Each sequence starts with a symbol, either `sequence' or `type',
  1322. indicating if the keywords should be interpreted as a sequence of
  1323. action steps, or as different types of TODO items. The first
  1324. keywords are states requiring action - these states will select a headline
  1325. for inclusion into the global TODO list Org-mode produces. If one of
  1326. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1327. signify that no further action is necessary. If \"|\" is not found,
  1328. the last keyword is treated as the only DONE state of the sequence.
  1329. The command \\[org-todo] cycles an entry through these states, and one
  1330. additional state where no keyword is present. For details about this
  1331. cycling, see the manual.
  1332. TODO keywords and interpretation can also be set on a per-file basis with
  1333. the special #+SEQ_TODO and #+TYP_TODO lines.
  1334. Each keyword can optionally specify a character for fast state selection
  1335. \(in combination with the variable `org-use-fast-todo-selection')
  1336. and specifiers for state change logging, using the same syntax
  1337. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1338. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1339. indicates to record a time stamp each time this state is selected.
  1340. Each keyword may also specify if a timestamp or a note should be
  1341. recorded when entering or leaving the state, by adding additional
  1342. characters in the parenthesis after the keyword. This looks like this:
  1343. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1344. record only the time of the state change. With X and Y being either
  1345. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1346. Y when leaving the state if and only if the *target* state does not
  1347. define X. You may omit any of the fast-selection key or X or /Y,
  1348. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1349. For backward compatibility, this variable may also be just a list
  1350. of keywords - in this case the interpretation (sequence or type) will be
  1351. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1352. :group 'org-todo
  1353. :group 'org-keywords
  1354. :type '(choice
  1355. (repeat :tag "Old syntax, just keywords"
  1356. (string :tag "Keyword"))
  1357. (repeat :tag "New syntax"
  1358. (cons
  1359. (choice
  1360. :tag "Interpretation"
  1361. ;;Quick and dirty way to see
  1362. ;;`org-todo-interpretations'. This takes the
  1363. ;;place of item arguments
  1364. :convert-widget
  1365. (lambda (widget)
  1366. (widget-put widget
  1367. :args (mapcar
  1368. #'(lambda (x)
  1369. (widget-convert
  1370. (cons 'const x)))
  1371. org-todo-interpretation-widgets))
  1372. widget))
  1373. (repeat
  1374. (string :tag "Keyword"))))))
  1375. (defvar org-todo-keywords-1 nil
  1376. "All TODO and DONE keywords active in a buffer.")
  1377. (make-variable-buffer-local 'org-todo-keywords-1)
  1378. (defvar org-todo-keywords-for-agenda nil)
  1379. (defvar org-done-keywords-for-agenda nil)
  1380. (defvar org-todo-keyword-alist-for-agenda nil)
  1381. (defvar org-tag-alist-for-agenda nil)
  1382. (defvar org-agenda-contributing-files nil)
  1383. (defvar org-not-done-keywords nil)
  1384. (make-variable-buffer-local 'org-not-done-keywords)
  1385. (defvar org-done-keywords nil)
  1386. (make-variable-buffer-local 'org-done-keywords)
  1387. (defvar org-todo-heads nil)
  1388. (make-variable-buffer-local 'org-todo-heads)
  1389. (defvar org-todo-sets nil)
  1390. (make-variable-buffer-local 'org-todo-sets)
  1391. (defvar org-todo-log-states nil)
  1392. (make-variable-buffer-local 'org-todo-log-states)
  1393. (defvar org-todo-kwd-alist nil)
  1394. (make-variable-buffer-local 'org-todo-kwd-alist)
  1395. (defvar org-todo-key-alist nil)
  1396. (make-variable-buffer-local 'org-todo-key-alist)
  1397. (defvar org-todo-key-trigger nil)
  1398. (make-variable-buffer-local 'org-todo-key-trigger)
  1399. (defcustom org-todo-interpretation 'sequence
  1400. "Controls how TODO keywords are interpreted.
  1401. This variable is in principle obsolete and is only used for
  1402. backward compatibility, if the interpretation of todo keywords is
  1403. not given already in `org-todo-keywords'. See that variable for
  1404. more information."
  1405. :group 'org-todo
  1406. :group 'org-keywords
  1407. :type '(choice (const sequence)
  1408. (const type)))
  1409. (defcustom org-use-fast-todo-selection t
  1410. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1411. This variable describes if and under what circumstances the cycling
  1412. mechanism for TODO keywords will be replaced by a single-key, direct
  1413. selection scheme.
  1414. When nil, fast selection is never used.
  1415. When the symbol `prefix', it will be used when `org-todo' is called with
  1416. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1417. in an agenda buffer.
  1418. When t, fast selection is used by default. In this case, the prefix
  1419. argument forces cycling instead.
  1420. In all cases, the special interface is only used if access keys have actually
  1421. been assigned by the user, i.e. if keywords in the configuration are followed
  1422. by a letter in parenthesis, like TODO(t)."
  1423. :group 'org-todo
  1424. :type '(choice
  1425. (const :tag "Never" nil)
  1426. (const :tag "By default" t)
  1427. (const :tag "Only with C-u C-c C-t" prefix)))
  1428. (defcustom org-provide-todo-statistics t
  1429. "Non-nil means, update todo statistics after insert and toggle.
  1430. When this is set, todo statistics is updated in the parent of the current
  1431. entry each time a todo state is changed."
  1432. :group 'org-todo
  1433. :type 'boolean)
  1434. (defcustom org-after-todo-state-change-hook nil
  1435. "Hook which is run after the state of a TODO item was changed.
  1436. The new state (a string with a TODO keyword, or nil) is available in the
  1437. Lisp variable `state'."
  1438. :group 'org-todo
  1439. :type 'hook)
  1440. (defvar org-blocker-hook nil
  1441. "Hook for functions that are allowed to block a state change.
  1442. Each function gets as its single argument a property list, see
  1443. `org-trigger-hook' for more information about this list.
  1444. If any of the functions in this hook returns nil, the state change
  1445. is blocked.")
  1446. (defvar org-trigger-hook nil
  1447. "Hook for functions that are triggered by a state change.
  1448. Each function gets as its single argument a property list with at least
  1449. the following elements:
  1450. (:type type-of-change :position pos-at-entry-start
  1451. :from old-state :to new-state)
  1452. Depending on the type, more properties may be present.
  1453. This mechanism is currently implemented for:
  1454. TODO state changes
  1455. ------------------
  1456. :type todo-state-change
  1457. :from previous state (keyword as a string), or nil, or a symbol
  1458. 'todo' or 'done', to indicate the general type of state.
  1459. :to new state, like in :from")
  1460. (defcustom org-enforce-todo-dependencies nil
  1461. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1462. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1463. be blocked if any prior sibling is not yet done.
  1464. This variable needs to be set before org.el is loaded, and you need to
  1465. restart Emacs after a change to make the change effective. The only way
  1466. to change is while Emacs is running is through the customize interface."
  1467. :set (lambda (var val)
  1468. (set var val)
  1469. (if val
  1470. (add-hook 'org-blocker-hook
  1471. 'org-block-todo-from-children-or-siblings)
  1472. (remove-hook 'org-blocker-hook
  1473. 'org-block-todo-from-children-or-siblings)))
  1474. :group 'org-todo
  1475. :type 'boolean)
  1476. (defcustom org-enforce-todo-checkbox-dependencies nil
  1477. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1478. When this is nil, checkboxes have no influence on switching TODO states.
  1479. When non-nil, you first need to check off all check boxes before the TODO
  1480. entry can be switched to DONE.
  1481. This variable needs to be set before org.el is loaded, and you need to
  1482. restart Emacs after a change to make the change effective. The only way
  1483. to change is while Emacs is running is through the customize interface."
  1484. :set (lambda (var val)
  1485. (set var val)
  1486. (if val
  1487. (add-hook 'org-blocker-hook
  1488. 'org-block-todo-from-checkboxes)
  1489. (remove-hook 'org-blocker-hook
  1490. 'org-block-todo-from-checkboxes)))
  1491. :group 'org-todo
  1492. :type 'boolean)
  1493. (defcustom org-todo-state-tags-triggers nil
  1494. "Tag changes that should be triggered by TODO state changes.
  1495. This is a list. Each entry is
  1496. (state-change (tag . flag) .......)
  1497. State-change can be a string with a state, and empty string to indicate the
  1498. state that has no TODO keyword, or it can be one of the symbols `todo'
  1499. or `done', meaning any not-done or done state, respectively."
  1500. :group 'org-todo
  1501. :group 'org-tags
  1502. :type '(repeat
  1503. (cons (choice :tag "When changing to"
  1504. (const :tag "Not-done state" todo)
  1505. (const :tag "Done state" done)
  1506. (string :tag "State"))
  1507. (repeat
  1508. (cons :tag "Tag action"
  1509. (string :tag "Tag")
  1510. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1511. (defcustom org-log-done nil
  1512. "Information to record when a task moves to the DONE state.
  1513. Possible values are:
  1514. nil Don't add anything, just change the keyword
  1515. time Add a time stamp to the task
  1516. note Prompt a closing note and add it with template `org-log-note-headings'
  1517. This option can also be set with on a per-file-basis with
  1518. #+STARTUP: nologdone
  1519. #+STARTUP: logdone
  1520. #+STARTUP: lognotedone
  1521. You can have local logging settings for a subtree by setting the LOGGING
  1522. property to one or more of these keywords."
  1523. :group 'org-todo
  1524. :group 'org-progress
  1525. :type '(choice
  1526. (const :tag "No logging" nil)
  1527. (const :tag "Record CLOSED timestamp" time)
  1528. (const :tag "Record CLOSED timestamp with closing note." note)))
  1529. ;; Normalize old uses of org-log-done.
  1530. (cond
  1531. ((eq org-log-done t) (setq org-log-done 'time))
  1532. ((and (listp org-log-done) (memq 'done org-log-done))
  1533. (setq org-log-done 'note)))
  1534. (defcustom org-log-note-clock-out nil
  1535. "Non-nil means, record a note when clocking out of an item.
  1536. This can also be configured on a per-file basis by adding one of
  1537. the following lines anywhere in the buffer:
  1538. #+STARTUP: lognoteclock-out
  1539. #+STARTUP: nolognoteclock-out"
  1540. :group 'org-todo
  1541. :group 'org-progress
  1542. :type 'boolean)
  1543. (defcustom org-log-done-with-time t
  1544. "Non-nil means, the CLOSED time stamp will contain date and time.
  1545. When nil, only the date will be recorded."
  1546. :group 'org-progress
  1547. :type 'boolean)
  1548. (defcustom org-log-note-headings
  1549. '((done . "CLOSING NOTE %t")
  1550. (state . "State %-12s %t")
  1551. (note . "Note taken on %t")
  1552. (clock-out . ""))
  1553. "Headings for notes added to entries.
  1554. The value is an alist, with the car being a symbol indicating the note
  1555. context, and the cdr is the heading to be used. The heading may also be the
  1556. empty string.
  1557. %t in the heading will be replaced by a time stamp.
  1558. %s will be replaced by the new TODO state, in double quotes.
  1559. %u will be replaced by the user name.
  1560. %U will be replaced by the full user name."
  1561. :group 'org-todo
  1562. :group 'org-progress
  1563. :type '(list :greedy t
  1564. (cons (const :tag "Heading when closing an item" done) string)
  1565. (cons (const :tag
  1566. "Heading when changing todo state (todo sequence only)"
  1567. state) string)
  1568. (cons (const :tag "Heading when just taking a note" note) string)
  1569. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1570. (unless (assq 'note org-log-note-headings)
  1571. (push '(note . "%t") org-log-note-headings))
  1572. (defcustom org-log-state-notes-insert-after-drawers nil
  1573. "Non-nil means, insert state change notes after any drawers in entry.
  1574. Only the drawers that *immediately* follow the headline and the
  1575. deadline/scheduled line are skipped.
  1576. When nil, insert notes right after the heading and perhaps the line
  1577. with deadline/scheduling if present."
  1578. :group 'org-todo
  1579. :group 'org-progress
  1580. :type 'boolean)
  1581. (defcustom org-log-states-order-reversed t
  1582. "Non-nil means, the latest state change note will be directly after heading.
  1583. When nil, the notes will be orderer according to time."
  1584. :group 'org-todo
  1585. :group 'org-progress
  1586. :type 'boolean)
  1587. (defcustom org-log-repeat 'time
  1588. "Non-nil means, record moving through the DONE state when triggering repeat.
  1589. An auto-repeating tasks is immediately switched back to TODO when marked
  1590. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1591. the TODO keyword definition, or recording a closing note by setting
  1592. `org-log-done', there will be no record of the task moving through DONE.
  1593. This variable forces taking a note anyway. Possible values are:
  1594. nil Don't force a record
  1595. time Record a time stamp
  1596. note Record a note
  1597. This option can also be set with on a per-file-basis with
  1598. #+STARTUP: logrepeat
  1599. #+STARTUP: lognoterepeat
  1600. #+STARTUP: nologrepeat
  1601. You can have local logging settings for a subtree by setting the LOGGING
  1602. property to one or more of these keywords."
  1603. :group 'org-todo
  1604. :group 'org-progress
  1605. :type '(choice
  1606. (const :tag "Don't force a record" nil)
  1607. (const :tag "Force recording the DONE state" time)
  1608. (const :tag "Force recording a note with the DONE state" note)))
  1609. (defgroup org-priorities nil
  1610. "Priorities in Org-mode."
  1611. :tag "Org Priorities"
  1612. :group 'org-todo)
  1613. (defcustom org-highest-priority ?A
  1614. "The highest priority of TODO items. A character like ?A, ?B etc.
  1615. Must have a smaller ASCII number than `org-lowest-priority'."
  1616. :group 'org-priorities
  1617. :type 'character)
  1618. (defcustom org-lowest-priority ?C
  1619. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1620. Must have a larger ASCII number than `org-highest-priority'."
  1621. :group 'org-priorities
  1622. :type 'character)
  1623. (defcustom org-default-priority ?B
  1624. "The default priority of TODO items.
  1625. This is the priority an item get if no explicit priority is given."
  1626. :group 'org-priorities
  1627. :type 'character)
  1628. (defcustom org-priority-start-cycle-with-default t
  1629. "Non-nil means, start with default priority when starting to cycle.
  1630. When this is nil, the first step in the cycle will be (depending on the
  1631. command used) one higher or lower that the default priority."
  1632. :group 'org-priorities
  1633. :type 'boolean)
  1634. (defgroup org-time nil
  1635. "Options concerning time stamps and deadlines in Org-mode."
  1636. :tag "Org Time"
  1637. :group 'org)
  1638. (defcustom org-insert-labeled-timestamps-at-point nil
  1639. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1640. When nil, these labeled time stamps are forces into the second line of an
  1641. entry, just after the headline. When scheduling from the global TODO list,
  1642. the time stamp will always be forced into the second line."
  1643. :group 'org-time
  1644. :type 'boolean)
  1645. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1646. "Formats for `format-time-string' which are used for time stamps.
  1647. It is not recommended to change this constant.")
  1648. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1649. "Number of minutes to round time stamps to.
  1650. These are two values, the first applies when first creating a time stamp.
  1651. The second applies when changing it with the commands `S-up' and `S-down'.
  1652. When changing the time stamp, this means that it will change in steps
  1653. of N minutes, as given by the second value.
  1654. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1655. numbers should be factors of 60, so for example 5, 10, 15.
  1656. When this is larger than 1, you can still force an exact time-stamp by using
  1657. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1658. and by using a prefix arg to `S-up/down' to specify the exact number
  1659. of minutes to shift."
  1660. :group 'org-time
  1661. :get '(lambda (var) ; Make sure all entries have 5 elements
  1662. (if (integerp (default-value var))
  1663. (list (default-value var) 5)
  1664. (default-value var)))
  1665. :type '(list
  1666. (integer :tag "when inserting times")
  1667. (integer :tag "when modifying times")))
  1668. ;; Normalize old customizations of this variable.
  1669. (when (integerp org-time-stamp-rounding-minutes)
  1670. (setq org-time-stamp-rounding-minutes
  1671. (list org-time-stamp-rounding-minutes
  1672. org-time-stamp-rounding-minutes)))
  1673. (defcustom org-display-custom-times nil
  1674. "Non-nil means, overlay custom formats over all time stamps.
  1675. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1676. To turn this on on a per-file basis, insert anywhere in the file:
  1677. #+STARTUP: customtime"
  1678. :group 'org-time
  1679. :set 'set-default
  1680. :type 'sexp)
  1681. (make-variable-buffer-local 'org-display-custom-times)
  1682. (defcustom org-time-stamp-custom-formats
  1683. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1684. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1685. These are overlayed over the default ISO format if the variable
  1686. `org-display-custom-times' is set. Time like %H:%M should be at the
  1687. end of the second format."
  1688. :group 'org-time
  1689. :type 'sexp)
  1690. (defun org-time-stamp-format (&optional long inactive)
  1691. "Get the right format for a time string."
  1692. (let ((f (if long (cdr org-time-stamp-formats)
  1693. (car org-time-stamp-formats))))
  1694. (if inactive
  1695. (concat "[" (substring f 1 -1) "]")
  1696. f)))
  1697. (defcustom org-time-clocksum-format "%d:%02d"
  1698. "The format string used when creating CLOCKSUM lines, or when
  1699. org-mode generates a time duration."
  1700. :group 'org-time
  1701. :type 'string)
  1702. (defcustom org-deadline-warning-days 14
  1703. "No. of days before expiration during which a deadline becomes active.
  1704. This variable governs the display in sparse trees and in the agenda.
  1705. When 0 or negative, it means use this number (the absolute value of it)
  1706. even if a deadline has a different individual lead time specified."
  1707. :group 'org-time
  1708. :group 'org-agenda-daily/weekly
  1709. :type 'number)
  1710. (defcustom org-read-date-prefer-future t
  1711. "Non-nil means, assume future for incomplete date input from user.
  1712. This affects the following situations:
  1713. 1. The user gives a day, but no month.
  1714. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1715. read this as the third of *next* month. However, if you enter \"17\",
  1716. it will be considered as *this* month.
  1717. 2. The user gives a month but not a year.
  1718. For example, if it is april and you enter \"feb 2\", this will be read
  1719. as feb 2, *next* year. \"May 5\", however, will be this year.
  1720. Currently this does not work for ISO week specifications.
  1721. When this option is nil, the current month and year will always be used
  1722. as defaults."
  1723. :group 'org-time
  1724. :type 'boolean)
  1725. (defcustom org-read-date-display-live t
  1726. "Non-nil means, display current interpretation of date prompt live.
  1727. This display will be in an overlay, in the minibuffer."
  1728. :group 'org-time
  1729. :type 'boolean)
  1730. (defcustom org-read-date-popup-calendar t
  1731. "Non-nil means, pop up a calendar when prompting for a date.
  1732. In the calendar, the date can be selected with mouse-1. However, the
  1733. minibuffer will also be active, and you can simply enter the date as well.
  1734. When nil, only the minibuffer will be available."
  1735. :group 'org-time
  1736. :type 'boolean)
  1737. (if (fboundp 'defvaralias)
  1738. (defvaralias 'org-popup-calendar-for-date-prompt
  1739. 'org-read-date-popup-calendar))
  1740. (defcustom org-extend-today-until 0
  1741. "The hour when your day really ends. Must be an integer.
  1742. This has influence for the following applications:
  1743. - When switching the agenda to \"today\". It it is still earlier than
  1744. the time given here, the day recognized as TODAY is actually yesterday.
  1745. - When a date is read from the user and it is still before the time given
  1746. here, the current date and time will be assumed to be yesterday, 23:59.
  1747. Also, timestamps inserted in remember templates follow this rule.
  1748. IMPORTANT: This is a feature whose implementation is and likely will
  1749. remain incomplete. Really, it is only here because past midnight seems to
  1750. be the favorite working time of John Wiegley :-)"
  1751. :group 'org-time
  1752. :type 'number)
  1753. (defcustom org-edit-timestamp-down-means-later nil
  1754. "Non-nil means, S-down will increase the time in a time stamp.
  1755. When nil, S-up will increase."
  1756. :group 'org-time
  1757. :type 'boolean)
  1758. (defcustom org-calendar-follow-timestamp-change t
  1759. "Non-nil means, make the calendar window follow timestamp changes.
  1760. When a timestamp is modified and the calendar window is visible, it will be
  1761. moved to the new date."
  1762. :group 'org-time
  1763. :type 'boolean)
  1764. (defgroup org-tags nil
  1765. "Options concerning tags in Org-mode."
  1766. :tag "Org Tags"
  1767. :group 'org)
  1768. (defcustom org-tag-alist nil
  1769. "List of tags allowed in Org-mode files.
  1770. When this list is nil, Org-mode will base TAG input on what is already in the
  1771. buffer.
  1772. The value of this variable is an alist, the car of each entry must be a
  1773. keyword as a string, the cdr may be a character that is used to select
  1774. that tag through the fast-tag-selection interface.
  1775. See the manual for details."
  1776. :group 'org-tags
  1777. :type '(repeat
  1778. (choice
  1779. (cons (string :tag "Tag name")
  1780. (character :tag "Access char"))
  1781. (const :tag "Start radio group" (:startgroup))
  1782. (const :tag "End radio group" (:endgroup)))))
  1783. (defvar org-file-tags nil
  1784. "List of tags that can be inherited by all entries in the file.
  1785. The tags will be inherited if the variable `org-use-tag-inheritance'
  1786. says they should be.
  1787. This variable is populated from #+TAG lines.")
  1788. (defcustom org-use-fast-tag-selection 'auto
  1789. "Non-nil means, use fast tag selection scheme.
  1790. This is a special interface to select and deselect tags with single keys.
  1791. When nil, fast selection is never used.
  1792. When the symbol `auto', fast selection is used if and only if selection
  1793. characters for tags have been configured, either through the variable
  1794. `org-tag-alist' or through a #+TAGS line in the buffer.
  1795. When t, fast selection is always used and selection keys are assigned
  1796. automatically if necessary."
  1797. :group 'org-tags
  1798. :type '(choice
  1799. (const :tag "Always" t)
  1800. (const :tag "Never" nil)
  1801. (const :tag "When selection characters are configured" 'auto)))
  1802. (defcustom org-fast-tag-selection-single-key nil
  1803. "Non-nil means, fast tag selection exits after first change.
  1804. When nil, you have to press RET to exit it.
  1805. During fast tag selection, you can toggle this flag with `C-c'.
  1806. This variable can also have the value `expert'. In this case, the window
  1807. displaying the tags menu is not even shown, until you press C-c again."
  1808. :group 'org-tags
  1809. :type '(choice
  1810. (const :tag "No" nil)
  1811. (const :tag "Yes" t)
  1812. (const :tag "Expert" expert)))
  1813. (defvar org-fast-tag-selection-include-todo nil
  1814. "Non-nil means, fast tags selection interface will also offer TODO states.
  1815. This is an undocumented feature, you should not rely on it.")
  1816. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1817. "The column to which tags should be indented in a headline.
  1818. If this number is positive, it specifies the column. If it is negative,
  1819. it means that the tags should be flushright to that column. For example,
  1820. -80 works well for a normal 80 character screen."
  1821. :group 'org-tags
  1822. :type 'integer)
  1823. (defcustom org-auto-align-tags t
  1824. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1825. These operations change the length of a headline and therefore shift
  1826. the tags around. With this options turned on, after each such operation
  1827. the tags are again aligned to `org-tags-column'."
  1828. :group 'org-tags
  1829. :type 'boolean)
  1830. (defcustom org-use-tag-inheritance t
  1831. "Non-nil means, tags in levels apply also for sublevels.
  1832. When nil, only the tags directly given in a specific line apply there.
  1833. This may also be a list of tags that should be inherited, or a regexp that
  1834. matches tags that should be inherited. Additional control is possible
  1835. with the variable `org-tags-exclude-from-inheritance' which gives an
  1836. explicit list of tags to be excluded from inheritance., even if the value of
  1837. `org-use-tag-inheritance' would select it for inheritance.
  1838. If this option is t, a match early-on in a tree can lead to a large
  1839. number of matches in the subtree when constructing the agenda or creating
  1840. a sparse tree. If you only want to see the first match in a tree during
  1841. a search, check out the variable `org-tags-match-list-sublevels'."
  1842. :group 'org-tags
  1843. :type '(choice
  1844. (const :tag "Not" nil)
  1845. (const :tag "Always" t)
  1846. (repeat :tag "Specific tags" (string :tag "Tag"))
  1847. (regexp :tag "Tags matched by regexp")))
  1848. (defcustom org-tags-exclude-from-inheritance nil
  1849. "List of tags that should never be inherited.
  1850. This is a way to exclude a few tags from inheritance. For way to do
  1851. the opposite, to actively allow inheritance for selected tags,
  1852. see the variable `org-use-tag-inheritance'."
  1853. :group 'org-tags
  1854. :type '(repeat (string :tag "Tag")))
  1855. (defun org-tag-inherit-p (tag)
  1856. "Check if TAG is one that should be inherited."
  1857. (cond
  1858. ((member tag org-tags-exclude-from-inheritance) nil)
  1859. ((eq org-use-tag-inheritance t) t)
  1860. ((not org-use-tag-inheritance) nil)
  1861. ((stringp org-use-tag-inheritance)
  1862. (string-match org-use-tag-inheritance tag))
  1863. ((listp org-use-tag-inheritance)
  1864. (member tag org-use-tag-inheritance))
  1865. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1866. (defcustom org-tags-match-list-sublevels t
  1867. "Non-nil means list also sublevels of headlines matching tag search.
  1868. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1869. the sublevels of a headline matching a tag search often also match
  1870. the same search. Listing all of them can create very long lists.
  1871. Setting this variable to nil causes subtrees of a match to be skipped.
  1872. This option is off by default, because inheritance in on. If you turn
  1873. inheritance off, you very likely want to turn this option on.
  1874. As a special case, if the tag search is restricted to TODO items, the
  1875. value of this variable is ignored and sublevels are always checked, to
  1876. make sure all corresponding TODO items find their way into the list.
  1877. This variable is semi-obsolete and probably should always be true. It
  1878. is better to limit inheritance to certain tags using the variables
  1879. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1880. :group 'org-tags
  1881. :type 'boolean)
  1882. (defvar org-tags-history nil
  1883. "History of minibuffer reads for tags.")
  1884. (defvar org-last-tags-completion-table nil
  1885. "The last used completion table for tags.")
  1886. (defvar org-after-tags-change-hook nil
  1887. "Hook that is run after the tags in a line have changed.")
  1888. (defgroup org-properties nil
  1889. "Options concerning properties in Org-mode."
  1890. :tag "Org Properties"
  1891. :group 'org)
  1892. (defcustom org-property-format "%-10s %s"
  1893. "How property key/value pairs should be formatted by `indent-line'.
  1894. When `indent-line' hits a property definition, it will format the line
  1895. according to this format, mainly to make sure that the values are
  1896. lined-up with respect to each other."
  1897. :group 'org-properties
  1898. :type 'string)
  1899. (defcustom org-use-property-inheritance nil
  1900. "Non-nil means, properties apply also for sublevels.
  1901. This setting is chiefly used during property searches. Turning it on can
  1902. cause significant overhead when doing a search, which is why it is not
  1903. on by default.
  1904. When nil, only the properties directly given in the current entry count.
  1905. When t, every property is inherited. The value may also be a list of
  1906. properties that should have inheritance, or a regular expression matching
  1907. properties that should be inherited.
  1908. However, note that some special properties use inheritance under special
  1909. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1910. and the properties ending in \"_ALL\" when they are used as descriptor
  1911. for valid values of a property.
  1912. Note for programmers:
  1913. When querying an entry with `org-entry-get', you can control if inheritance
  1914. should be used. By default, `org-entry-get' looks only at the local
  1915. properties. You can request inheritance by setting the inherit argument
  1916. to t (to force inheritance) or to `selective' (to respect the setting
  1917. in this variable)."
  1918. :group 'org-properties
  1919. :type '(choice
  1920. (const :tag "Not" nil)
  1921. (const :tag "Always" t)
  1922. (repeat :tag "Specific properties" (string :tag "Property"))
  1923. (regexp :tag "Properties matched by regexp")))
  1924. (defun org-property-inherit-p (property)
  1925. "Check if PROPERTY is one that should be inherited."
  1926. (cond
  1927. ((eq org-use-property-inheritance t) t)
  1928. ((not org-use-property-inheritance) nil)
  1929. ((stringp org-use-property-inheritance)
  1930. (string-match org-use-property-inheritance property))
  1931. ((listp org-use-property-inheritance)
  1932. (member property org-use-property-inheritance))
  1933. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1934. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1935. "The default column format, if no other format has been defined.
  1936. This variable can be set on the per-file basis by inserting a line
  1937. #+COLUMNS: %25ITEM ....."
  1938. :group 'org-properties
  1939. :type 'string)
  1940. (defcustom org-columns-ellipses ".."
  1941. "The ellipses to be used when a field in column view is truncated.
  1942. When this is the empty string, as many characters as possible are shown,
  1943. but then there will be no visual indication that the field has been truncated.
  1944. When this is a string of length N, the last N characters of a truncated
  1945. field are replaced by this string. If the column is narrower than the
  1946. ellipses string, only part of the ellipses string will be shown."
  1947. :group 'org-properties
  1948. :type 'string)
  1949. (defcustom org-columns-modify-value-for-display-function nil
  1950. "Function that modifies values for display in column view.
  1951. For example, it can be used to cut out a certain part from a time stamp.
  1952. The function must take 2 arguments:
  1953. column-title The title of the column (*not* the property name)
  1954. value The value that should be modified.
  1955. The function should return the value that should be displayed,
  1956. or nil if the normal value should be used."
  1957. :group 'org-properties
  1958. :type 'function)
  1959. (defcustom org-effort-property "Effort"
  1960. "The property that is being used to keep track of effort estimates.
  1961. Effort estimates given in this property need to have the format H:MM."
  1962. :group 'org-properties
  1963. :group 'org-progress
  1964. :type '(string :tag "Property"))
  1965. (defconst org-global-properties-fixed
  1966. '(("VISIBILITY_ALL" . "folded children content all"))
  1967. "List of property/value pairs that can be inherited by any entry.
  1968. These are fixed values, for the preset properties.")
  1969. (defcustom org-global-properties nil
  1970. "List of property/value pairs that can be inherited by any entry.
  1971. You can set buffer-local values for the same purpose in the variable
  1972. `org-file-properties' this by adding lines like
  1973. #+PROPERTY: NAME VALUE"
  1974. :group 'org-properties
  1975. :type '(repeat
  1976. (cons (string :tag "Property")
  1977. (string :tag "Value"))))
  1978. (defvar org-file-properties nil
  1979. "List of property/value pairs that can be inherited by any entry.
  1980. Valid for the current buffer.
  1981. This variable is populated from #+PROPERTY lines.")
  1982. (make-variable-buffer-local 'org-file-properties)
  1983. (defgroup org-agenda nil
  1984. "Options concerning agenda views in Org-mode."
  1985. :tag "Org Agenda"
  1986. :group 'org)
  1987. (defvar org-category nil
  1988. "Variable used by org files to set a category for agenda display.
  1989. Such files should use a file variable to set it, for example
  1990. # -*- mode: org; org-category: \"ELisp\"
  1991. or contain a special line
  1992. #+CATEGORY: ELisp
  1993. If the file does not specify a category, then file's base name
  1994. is used instead.")
  1995. (make-variable-buffer-local 'org-category)
  1996. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1997. (defcustom org-agenda-files nil
  1998. "The files to be used for agenda display.
  1999. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2000. \\[org-remove-file]. You can also use customize to edit the list.
  2001. If an entry is a directory, all files in that directory that are matched by
  2002. `org-agenda-file-regexp' will be part of the file list.
  2003. If the value of the variable is not a list but a single file name, then
  2004. the list of agenda files is actually stored and maintained in that file, one
  2005. agenda file per line."
  2006. :group 'org-agenda
  2007. :type '(choice
  2008. (repeat :tag "List of files and directories" file)
  2009. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2010. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2011. "Regular expression to match files for `org-agenda-files'.
  2012. If any element in the list in that variable contains a directory instead
  2013. of a normal file, all files in that directory that are matched by this
  2014. regular expression will be included."
  2015. :group 'org-agenda
  2016. :type 'regexp)
  2017. (defcustom org-agenda-text-search-extra-files nil
  2018. "List of extra files to be searched by text search commands.
  2019. These files will be search in addition to the agenda files by the
  2020. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2021. Note that these files will only be searched for text search commands,
  2022. not for the other agenda views like todo lists, tag searches or the weekly
  2023. agenda. This variable is intended to list notes and possibly archive files
  2024. that should also be searched by these two commands.
  2025. In fact, if the first element in the list is the symbol `agenda-archives',
  2026. than all archive files of all agenda files will be added to the search
  2027. scope."
  2028. :group 'org-agenda
  2029. :type '(set :greedy t
  2030. (const :tag "Agenda Archives" agenda-archives)
  2031. (repeat :inline t (file))))
  2032. (if (fboundp 'defvaralias)
  2033. (defvaralias 'org-agenda-multi-occur-extra-files
  2034. 'org-agenda-text-search-extra-files))
  2035. (defcustom org-agenda-skip-unavailable-files nil
  2036. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2037. A nil value means to remove them, after a query, from the list."
  2038. :group 'org-agenda
  2039. :type 'boolean)
  2040. (defcustom org-calendar-to-agenda-key [?c]
  2041. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2042. The command `org-calendar-goto-agenda' will be bound to this key. The
  2043. default is the character `c' because then `c' can be used to switch back and
  2044. forth between agenda and calendar."
  2045. :group 'org-agenda
  2046. :type 'sexp)
  2047. (defcustom org-calendar-agenda-action-key [?k]
  2048. "The key to be installed in `calendar-mode-map' for agenda-action.
  2049. The command `org-agenda-action' will be bound to this key. The
  2050. default is the character `k' because we use the same key in the agenda."
  2051. :group 'org-agenda
  2052. :type 'sexp)
  2053. (eval-after-load "calendar"
  2054. '(progn
  2055. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2056. 'org-calendar-goto-agenda)
  2057. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2058. 'org-agenda-action)))
  2059. (defgroup org-latex nil
  2060. "Options for embedding LaTeX code into Org-mode."
  2061. :tag "Org LaTeX"
  2062. :group 'org)
  2063. (defcustom org-format-latex-options
  2064. '(:foreground default :background default :scale 1.0
  2065. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2066. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2067. "Options for creating images from LaTeX fragments.
  2068. This is a property list with the following properties:
  2069. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2070. `default' means use the foreground of the default face.
  2071. :background the background color, or \"Transparent\".
  2072. `default' means use the background of the default face.
  2073. :scale a scaling factor for the size of the images.
  2074. :html-foreground, :html-background, :html-scale
  2075. the same numbers for HTML export.
  2076. :matchers a list indicating which matchers should be used to
  2077. find LaTeX fragments. Valid members of this list are:
  2078. \"begin\" find environments
  2079. \"$1\" find single characters surrounded by $.$
  2080. \"$\" find math expressions surrounded by $...$
  2081. \"$$\" find math expressions surrounded by $$....$$
  2082. \"\\(\" find math expressions surrounded by \\(...\\)
  2083. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2084. :group 'org-latex
  2085. :type 'plist)
  2086. (defcustom org-format-latex-header "\\documentclass{article}
  2087. \\usepackage{fullpage} % do not remove
  2088. \\usepackage{amssymb}
  2089. \\usepackage[usenames]{color}
  2090. \\usepackage{amsmath}
  2091. \\usepackage{latexsym}
  2092. \\usepackage[mathscr]{eucal}
  2093. \\pagestyle{empty} % do not remove"
  2094. "The document header used for processing LaTeX fragments."
  2095. :group 'org-latex
  2096. :type 'string)
  2097. (defgroup org-font-lock nil
  2098. "Font-lock settings for highlighting in Org-mode."
  2099. :tag "Org Font Lock"
  2100. :group 'org)
  2101. (defcustom org-level-color-stars-only nil
  2102. "Non-nil means fontify only the stars in each headline.
  2103. When nil, the entire headline is fontified.
  2104. Changing it requires restart of `font-lock-mode' to become effective
  2105. also in regions already fontified."
  2106. :group 'org-font-lock
  2107. :type 'boolean)
  2108. (defcustom org-hide-leading-stars nil
  2109. "Non-nil means, hide the first N-1 stars in a headline.
  2110. This works by using the face `org-hide' for these stars. This
  2111. face is white for a light background, and black for a dark
  2112. background. You may have to customize the face `org-hide' to
  2113. make this work.
  2114. Changing it requires restart of `font-lock-mode' to become effective
  2115. also in regions already fontified.
  2116. You may also set this on a per-file basis by adding one of the following
  2117. lines to the buffer:
  2118. #+STARTUP: hidestars
  2119. #+STARTUP: showstars"
  2120. :group 'org-font-lock
  2121. :type 'boolean)
  2122. (defcustom org-fontify-done-headline nil
  2123. "Non-nil means, change the face of a headline if it is marked DONE.
  2124. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2125. When this is non-nil, the headline after the keyword is set to the
  2126. `org-headline-done' as an additional indication."
  2127. :group 'org-font-lock
  2128. :type 'boolean)
  2129. (defcustom org-fontify-emphasized-text t
  2130. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2131. Changing this variable requires a restart of Emacs to take effect."
  2132. :group 'org-font-lock
  2133. :type 'boolean)
  2134. (defcustom org-highlight-latex-fragments-and-specials nil
  2135. "Non-nil means, fontify what is treated specially by the exporters."
  2136. :group 'org-font-lock
  2137. :type 'boolean)
  2138. (defcustom org-hide-emphasis-markers nil
  2139. "Non-nil mean font-lock should hide the emphasis marker characters."
  2140. :group 'org-font-lock
  2141. :type 'boolean)
  2142. (defvar org-emph-re nil
  2143. "Regular expression for matching emphasis.")
  2144. (defvar org-verbatim-re nil
  2145. "Regular expression for matching verbatim text.")
  2146. (defvar org-emphasis-regexp-components) ; defined just below
  2147. (defvar org-emphasis-alist) ; defined just below
  2148. (defun org-set-emph-re (var val)
  2149. "Set variable and compute the emphasis regular expression."
  2150. (set var val)
  2151. (when (and (boundp 'org-emphasis-alist)
  2152. (boundp 'org-emphasis-regexp-components)
  2153. org-emphasis-alist org-emphasis-regexp-components)
  2154. (let* ((e org-emphasis-regexp-components)
  2155. (pre (car e))
  2156. (post (nth 1 e))
  2157. (border (nth 2 e))
  2158. (body (nth 3 e))
  2159. (nl (nth 4 e))
  2160. (body1 (concat body "*?"))
  2161. (markers (mapconcat 'car org-emphasis-alist ""))
  2162. (vmarkers (mapconcat
  2163. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2164. org-emphasis-alist "")))
  2165. ;; make sure special characters appear at the right position in the class
  2166. (if (string-match "\\^" markers)
  2167. (setq markers (concat (replace-match "" t t markers) "^")))
  2168. (if (string-match "-" markers)
  2169. (setq markers (concat (replace-match "" t t markers) "-")))
  2170. (if (string-match "\\^" vmarkers)
  2171. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2172. (if (string-match "-" vmarkers)
  2173. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2174. (if (> nl 0)
  2175. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2176. (int-to-string nl) "\\}")))
  2177. ;; Make the regexp
  2178. (setq org-emph-re
  2179. (concat "\\([" pre "]\\|^\\)"
  2180. "\\("
  2181. "\\([" markers "]\\)"
  2182. "\\("
  2183. "[^" border "]\\|"
  2184. "[^" border "]"
  2185. body1
  2186. "[^" border "]"
  2187. "\\)"
  2188. "\\3\\)"
  2189. "\\([" post "]\\|$\\)"))
  2190. (setq org-verbatim-re
  2191. (concat "\\([" pre "]\\|^\\)"
  2192. "\\("
  2193. "\\([" vmarkers "]\\)"
  2194. "\\("
  2195. "[^" border "]\\|"
  2196. "[^" border "]"
  2197. body1
  2198. "[^" border "]"
  2199. "\\)"
  2200. "\\3\\)"
  2201. "\\([" post "]\\|$\\)")))))
  2202. (defcustom org-emphasis-regexp-components
  2203. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2204. "Components used to build the regular expression for emphasis.
  2205. This is a list with 6 entries. Terminology: In an emphasis string
  2206. like \" *strong word* \", we call the initial space PREMATCH, the final
  2207. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2208. and \"trong wor\" is the body. The different components in this variable
  2209. specify what is allowed/forbidden in each part:
  2210. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2211. post Chars allowed as postmatch. End of line will be allowed too.
  2212. border The chars *forbidden* as border characters.
  2213. body-regexp A regexp like \".\" to match a body character. Don't use
  2214. non-shy groups here, and don't allow newline here.
  2215. newline The maximum number of newlines allowed in an emphasis exp.
  2216. Use customize to modify this, or restart Emacs after changing it."
  2217. :group 'org-font-lock
  2218. :set 'org-set-emph-re
  2219. :type '(list
  2220. (sexp :tag "Allowed chars in pre ")
  2221. (sexp :tag "Allowed chars in post ")
  2222. (sexp :tag "Forbidden chars in border ")
  2223. (sexp :tag "Regexp for body ")
  2224. (integer :tag "number of newlines allowed")
  2225. (option (boolean :tag "Please ignore this button"))))
  2226. (defcustom org-emphasis-alist
  2227. `(("*" bold "<b>" "</b>")
  2228. ("/" italic "<i>" "</i>")
  2229. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2230. ("=" org-code "<code>" "</code>" verbatim)
  2231. ("~" org-verbatim "<code>" "</code>" verbatim)
  2232. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2233. "<del>" "</del>")
  2234. )
  2235. "Special syntax for emphasized text.
  2236. Text starting and ending with a special character will be emphasized, for
  2237. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2238. characters, the face to be used by font-lock for highlighting in Org-mode
  2239. Emacs buffers, and the HTML tags to be used for this.
  2240. Use customize to modify this, or restart Emacs after changing it."
  2241. :group 'org-font-lock
  2242. :set 'org-set-emph-re
  2243. :type '(repeat
  2244. (list
  2245. (string :tag "Marker character")
  2246. (choice
  2247. (face :tag "Font-lock-face")
  2248. (plist :tag "Face property list"))
  2249. (string :tag "HTML start tag")
  2250. (string :tag "HTML end tag")
  2251. (option (const verbatim)))))
  2252. ;;; Miscellaneous options
  2253. (defgroup org-completion nil
  2254. "Completion in Org-mode."
  2255. :tag "Org Completion"
  2256. :group 'org)
  2257. (defcustom org-completion-use-ido nil
  2258. "Non-nil means, use ido completion wherever possible.
  2259. Note that `ido-mode' must be active for this variable to be relevant.
  2260. If you decide to turn this variable on, you might well want to turn off
  2261. `org-outline-path-complete-in-steps'."
  2262. :group 'org-completion
  2263. :type 'boolean)
  2264. (defcustom org-completion-fallback-command 'hippie-expand
  2265. "The expansion command called by \\[org-complete] in normal context.
  2266. Normal means, no org-mode-specific context."
  2267. :group 'org-completion
  2268. :type 'function)
  2269. ;;; Functions and variables from ther packages
  2270. ;; Declared here to avoid compiler warnings
  2271. ;; XEmacs only
  2272. (defvar outline-mode-menu-heading)
  2273. (defvar outline-mode-menu-show)
  2274. (defvar outline-mode-menu-hide)
  2275. (defvar zmacs-regions) ; XEmacs regions
  2276. ;; Emacs only
  2277. (defvar mark-active)
  2278. ;; Various packages
  2279. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2280. (declare-function calendar-forward-day "cal-move" (arg))
  2281. (declare-function calendar-goto-date "cal-move" (date))
  2282. (declare-function calendar-goto-today "cal-move" ())
  2283. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2284. (defvar calc-embedded-close-formula)
  2285. (defvar calc-embedded-open-formula)
  2286. (declare-function cdlatex-tab "ext:cdlatex" ())
  2287. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2288. (defvar font-lock-unfontify-region-function)
  2289. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2290. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2291. (defvar iswitchb-temp-buflist)
  2292. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2293. (defvar org-agenda-tags-todo-honor-ignore-options)
  2294. (declare-function org-agenda-skip "org-agenda" ())
  2295. (declare-function org-format-agenda-item "org-agenda"
  2296. (extra txt &optional category tags dotime noprefix remove-re))
  2297. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2298. (declare-function org-agenda-change-all-lines "org-agenda"
  2299. (newhead hdmarker &optional fixface just-this))
  2300. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2301. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2302. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2303. (beg end))
  2304. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2305. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2306. "org-agenda" (&optional end))
  2307. (declare-function parse-time-string "parse-time" (string))
  2308. (declare-function remember "remember" (&optional initial))
  2309. (declare-function remember-buffer-desc "remember" ())
  2310. (declare-function remember-finalize "remember" ())
  2311. (defvar remember-save-after-remembering)
  2312. (defvar remember-data-file)
  2313. (defvar remember-register)
  2314. (defvar remember-buffer)
  2315. (defvar remember-handler-functions)
  2316. (defvar remember-annotation-functions)
  2317. (defvar texmathp-why)
  2318. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2319. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2320. (defvar w3m-current-url)
  2321. (defvar w3m-current-title)
  2322. (defvar org-latex-regexps)
  2323. ;;; Autoload and prepare some org modules
  2324. ;; Some table stuff that needs to be defined here, because it is used
  2325. ;; by the functions setting up org-mode or checking for table context.
  2326. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2327. "Detects an org-type or table-type table.")
  2328. (defconst org-table-line-regexp "^[ \t]*|"
  2329. "Detects an org-type table line.")
  2330. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2331. "Detects an org-type table line.")
  2332. (defconst org-table-hline-regexp "^[ \t]*|-"
  2333. "Detects an org-type table hline.")
  2334. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2335. "Detects a table-type table hline.")
  2336. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2337. "Searching from within a table (any type) this finds the first line
  2338. outside the table.")
  2339. ;; Autoload the functions in org-table.el that are needed by functions here.
  2340. (eval-and-compile
  2341. (org-autoload "org-table"
  2342. '(org-table-align org-table-begin org-table-blank-field
  2343. org-table-convert org-table-convert-region org-table-copy-down
  2344. org-table-copy-region org-table-create
  2345. org-table-create-or-convert-from-region
  2346. org-table-create-with-table.el org-table-current-dline
  2347. org-table-cut-region org-table-delete-column org-table-edit-field
  2348. org-table-edit-formulas org-table-end org-table-eval-formula
  2349. org-table-export org-table-field-info
  2350. org-table-get-stored-formulas org-table-goto-column
  2351. org-table-hline-and-move org-table-import org-table-insert-column
  2352. org-table-insert-hline org-table-insert-row org-table-iterate
  2353. org-table-justify-field-maybe org-table-kill-row
  2354. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2355. org-table-move-column org-table-move-column-left
  2356. org-table-move-column-right org-table-move-row
  2357. org-table-move-row-down org-table-move-row-up
  2358. org-table-next-field org-table-next-row org-table-paste-rectangle
  2359. org-table-previous-field org-table-recalculate
  2360. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2361. org-table-toggle-coordinate-overlays
  2362. org-table-toggle-formula-debugger org-table-wrap-region
  2363. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2364. (defun org-at-table-p (&optional table-type)
  2365. "Return t if the cursor is inside an org-type table.
  2366. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2367. (if org-enable-table-editor
  2368. (save-excursion
  2369. (beginning-of-line 1)
  2370. (looking-at (if table-type org-table-any-line-regexp
  2371. org-table-line-regexp)))
  2372. nil))
  2373. (defsubst org-table-p () (org-at-table-p))
  2374. (defun org-at-table.el-p ()
  2375. "Return t if and only if we are at a table.el table."
  2376. (and (org-at-table-p 'any)
  2377. (save-excursion
  2378. (goto-char (org-table-begin 'any))
  2379. (looking-at org-table1-hline-regexp))))
  2380. (defun org-table-recognize-table.el ()
  2381. "If there is a table.el table nearby, recognize it and move into it."
  2382. (if org-table-tab-recognizes-table.el
  2383. (if (org-at-table.el-p)
  2384. (progn
  2385. (beginning-of-line 1)
  2386. (if (looking-at org-table-dataline-regexp)
  2387. nil
  2388. (if (looking-at org-table1-hline-regexp)
  2389. (progn
  2390. (beginning-of-line 2)
  2391. (if (looking-at org-table-any-border-regexp)
  2392. (beginning-of-line -1)))))
  2393. (if (re-search-forward "|" (org-table-end t) t)
  2394. (progn
  2395. (require 'table)
  2396. (if (table--at-cell-p (point))
  2397. t
  2398. (message "recognizing table.el table...")
  2399. (table-recognize-table)
  2400. (message "recognizing table.el table...done")))
  2401. (error "This should not happen..."))
  2402. t)
  2403. nil)
  2404. nil))
  2405. (defun org-at-table-hline-p ()
  2406. "Return t if the cursor is inside a hline in a table."
  2407. (if org-enable-table-editor
  2408. (save-excursion
  2409. (beginning-of-line 1)
  2410. (looking-at org-table-hline-regexp))
  2411. nil))
  2412. (defvar org-table-clean-did-remove-column nil)
  2413. (defun org-table-map-tables (function)
  2414. "Apply FUNCTION to the start of all tables in the buffer."
  2415. (save-excursion
  2416. (save-restriction
  2417. (widen)
  2418. (goto-char (point-min))
  2419. (while (re-search-forward org-table-any-line-regexp nil t)
  2420. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2421. (beginning-of-line 1)
  2422. (if (looking-at org-table-line-regexp)
  2423. (save-excursion (funcall function)))
  2424. (re-search-forward org-table-any-border-regexp nil 1))))
  2425. (message "Mapping tables: done"))
  2426. ;; Declare and autoload functions from org-exp.el
  2427. (declare-function org-default-export-plist "org-exp")
  2428. (declare-function org-infile-export-plist "org-exp")
  2429. (declare-function org-get-current-options "org-exp")
  2430. (eval-and-compile
  2431. (org-autoload "org-exp"
  2432. '(org-export org-export-as-ascii org-export-visible
  2433. org-insert-export-options-template org-export-as-html-and-open
  2434. org-export-as-html-batch org-export-as-html-to-buffer
  2435. org-replace-region-by-html org-export-region-as-html
  2436. org-export-as-html org-export-icalendar-this-file
  2437. org-export-icalendar-all-agenda-files
  2438. org-table-clean-before-export
  2439. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2440. ;; Declare and autoload functions from org-agenda.el
  2441. (eval-and-compile
  2442. (org-autoload "org-agenda"
  2443. '(org-agenda org-agenda-list org-search-view
  2444. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2445. org-diary org-agenda-to-appt
  2446. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2447. ;; Autoload org-remember
  2448. (eval-and-compile
  2449. (org-autoload "org-remember"
  2450. '(org-remember-insinuate org-remember-annotation
  2451. org-remember-apply-template org-remember org-remember-handler)))
  2452. ;; Autoload org-clock.el
  2453. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2454. (beg end))
  2455. (declare-function org-clock-update-mode-line "org-clock" ())
  2456. (defvar org-clock-start-time)
  2457. (defvar org-clock-marker (make-marker)
  2458. "Marker recording the last clock-in.")
  2459. (eval-and-compile
  2460. (org-autoload
  2461. "org-clock"
  2462. '(org-clock-in org-clock-out org-clock-cancel
  2463. org-clock-goto org-clock-sum org-clock-display
  2464. org-clock-remove-overlays org-clock-report
  2465. org-clocktable-shift org-dblock-write:clocktable
  2466. org-get-clocktable)))
  2467. (defun org-clock-update-time-maybe ()
  2468. "If this is a CLOCK line, update it and return t.
  2469. Otherwise, return nil."
  2470. (interactive)
  2471. (save-excursion
  2472. (beginning-of-line 1)
  2473. (skip-chars-forward " \t")
  2474. (when (looking-at org-clock-string)
  2475. (let ((re (concat "[ \t]*" org-clock-string
  2476. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2477. "\\([ \t]*=>.*\\)?\\)?"))
  2478. ts te h m s neg)
  2479. (cond
  2480. ((not (looking-at re))
  2481. nil)
  2482. ((not (match-end 2))
  2483. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2484. (> org-clock-marker (point))
  2485. (<= org-clock-marker (point-at-eol)))
  2486. ;; The clock is running here
  2487. (setq org-clock-start-time
  2488. (apply 'encode-time
  2489. (org-parse-time-string (match-string 1))))
  2490. (org-clock-update-mode-line)))
  2491. (t
  2492. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2493. (end-of-line 1)
  2494. (setq ts (match-string 1)
  2495. te (match-string 3))
  2496. (setq s (- (time-to-seconds
  2497. (apply 'encode-time (org-parse-time-string te)))
  2498. (time-to-seconds
  2499. (apply 'encode-time (org-parse-time-string ts))))
  2500. neg (< s 0)
  2501. s (abs s)
  2502. h (floor (/ s 3600))
  2503. s (- s (* 3600 h))
  2504. m (floor (/ s 60))
  2505. s (- s (* 60 s)))
  2506. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2507. t))))))
  2508. (defun org-check-running-clock ()
  2509. "Check if the current buffer contains the running clock.
  2510. If yes, offer to stop it and to save the buffer with the changes."
  2511. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2512. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2513. (buffer-name))))
  2514. (org-clock-out)
  2515. (when (y-or-n-p "Save changed buffer?")
  2516. (save-buffer))))
  2517. (defun org-clocktable-try-shift (dir n)
  2518. "Check if this line starts a clock table, if yes, shift the time block."
  2519. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2520. (org-clocktable-shift dir n)))
  2521. ;; Autoload org-timer.el
  2522. ;(declare-function org-timer "org-timer")
  2523. (eval-and-compile
  2524. (org-autoload
  2525. "org-timer"
  2526. '(org-timer-start org-timer org-timer-item
  2527. org-timer-change-times-in-region)))
  2528. ;; Autoload archiving code
  2529. ;; The stuff that is needed for cycling and tags has to be defined here.
  2530. (defgroup org-archive nil
  2531. "Options concerning archiving in Org-mode."
  2532. :tag "Org Archive"
  2533. :group 'org-structure)
  2534. (defcustom org-archive-location "%s_archive::"
  2535. "The location where subtrees should be archived.
  2536. The value of this variable is a string, consisting of two parts,
  2537. separated by a double-colon. The first part is a filename and
  2538. the second part is a headline.
  2539. When the filename is omitted, archiving happens in the same file.
  2540. %s in the filename will be replaced by the current file
  2541. name (without the directory part). Archiving to a different file
  2542. is useful to keep archived entries from contributing to the
  2543. Org-mode Agenda.
  2544. The archived entries will be filed as subtrees of the specified
  2545. headline. When the headline is omitted, the subtrees are simply
  2546. filed away at the end of the file, as top-level entries. Also in
  2547. the heading you can use %s to represent the file name, this can be
  2548. useful when using the same archive for a number of different files.
  2549. Here are a few examples:
  2550. \"%s_archive::\"
  2551. If the current file is Projects.org, archive in file
  2552. Projects.org_archive, as top-level trees. This is the default.
  2553. \"::* Archived Tasks\"
  2554. Archive in the current file, under the top-level headline
  2555. \"* Archived Tasks\".
  2556. \"~/org/archive.org::\"
  2557. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2558. \"~/org/archive.org::From %s\"
  2559. Archive in file ~/org/archive.org (absolute path), und headlines
  2560. \"From FILENAME\" where file name is the current file name.
  2561. \"basement::** Finished Tasks\"
  2562. Archive in file ./basement (relative path), as level 3 trees
  2563. below the level 2 heading \"** Finished Tasks\".
  2564. You may set this option on a per-file basis by adding to the buffer a
  2565. line like
  2566. #+ARCHIVE: basement::** Finished Tasks
  2567. You may also define it locally for a subtree by setting an ARCHIVE property
  2568. in the entry. If such a property is found in an entry, or anywhere up
  2569. the hierarchy, it will be used."
  2570. :group 'org-archive
  2571. :type 'string)
  2572. (defcustom org-archive-tag "ARCHIVE"
  2573. "The tag that marks a subtree as archived.
  2574. An archived subtree does not open during visibility cycling, and does
  2575. not contribute to the agenda listings.
  2576. After changing this, font-lock must be restarted in the relevant buffers to
  2577. get the proper fontification."
  2578. :group 'org-archive
  2579. :group 'org-keywords
  2580. :type 'string)
  2581. (defcustom org-agenda-skip-archived-trees t
  2582. "Non-nil means, the agenda will skip any items located in archived trees.
  2583. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2584. variable is no longer recommended, you should leave it at the value t.
  2585. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2586. :group 'org-archive
  2587. :group 'org-agenda-skip
  2588. :type 'boolean)
  2589. (defcustom org-cycle-open-archived-trees nil
  2590. "Non-nil means, `org-cycle' will open archived trees.
  2591. An archived tree is a tree marked with the tag ARCHIVE.
  2592. When nil, archived trees will stay folded. You can still open them with
  2593. normal outline commands like `show-all', but not with the cycling commands."
  2594. :group 'org-archive
  2595. :group 'org-cycle
  2596. :type 'boolean)
  2597. (defcustom org-sparse-tree-open-archived-trees nil
  2598. "Non-nil means sparse tree construction shows matches in archived trees.
  2599. When nil, matches in these trees are highlighted, but the trees are kept in
  2600. collapsed state."
  2601. :group 'org-archive
  2602. :group 'org-sparse-trees
  2603. :type 'boolean)
  2604. (defun org-cycle-hide-archived-subtrees (state)
  2605. "Re-hide all archived subtrees after a visibility state change."
  2606. (when (and (not org-cycle-open-archived-trees)
  2607. (not (memq state '(overview folded))))
  2608. (save-excursion
  2609. (let* ((globalp (memq state '(contents all)))
  2610. (beg (if globalp (point-min) (point)))
  2611. (end (if globalp (point-max) (org-end-of-subtree t))))
  2612. (org-hide-archived-subtrees beg end)
  2613. (goto-char beg)
  2614. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2615. (message "%s" (substitute-command-keys
  2616. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2617. (defun org-force-cycle-archived ()
  2618. "Cycle subtree even if it is archived."
  2619. (interactive)
  2620. (setq this-command 'org-cycle)
  2621. (let ((org-cycle-open-archived-trees t))
  2622. (call-interactively 'org-cycle)))
  2623. (defun org-hide-archived-subtrees (beg end)
  2624. "Re-hide all archived subtrees after a visibility state change."
  2625. (save-excursion
  2626. (let* ((re (concat ":" org-archive-tag ":")))
  2627. (goto-char beg)
  2628. (while (re-search-forward re end t)
  2629. (and (org-on-heading-p) (hide-subtree))
  2630. (org-end-of-subtree t)))))
  2631. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2632. (eval-and-compile
  2633. (org-autoload "org-archive"
  2634. '(org-add-archive-files org-archive-subtree
  2635. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2636. ;; Autoload Column View Code
  2637. (declare-function org-columns-number-to-string "org-colview")
  2638. (declare-function org-columns-get-format-and-top-level "org-colview")
  2639. (declare-function org-columns-compute "org-colview")
  2640. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2641. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2642. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2643. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2644. ;; Autoload ID code
  2645. (declare-function org-id-store-link "org-id")
  2646. (org-autoload "org-id"
  2647. '(org-id-get-create org-id-new org-id-copy org-id-get
  2648. org-id-get-with-outline-path-completion
  2649. org-id-get-with-outline-drilling
  2650. org-id-goto org-id-find org-id-store-link))
  2651. ;;; Variables for pre-computed regular expressions, all buffer local
  2652. (defvar org-drawer-regexp nil
  2653. "Matches first line of a hidden block.")
  2654. (make-variable-buffer-local 'org-drawer-regexp)
  2655. (defvar org-todo-regexp nil
  2656. "Matches any of the TODO state keywords.")
  2657. (make-variable-buffer-local 'org-todo-regexp)
  2658. (defvar org-not-done-regexp nil
  2659. "Matches any of the TODO state keywords except the last one.")
  2660. (make-variable-buffer-local 'org-not-done-regexp)
  2661. (defvar org-todo-line-regexp nil
  2662. "Matches a headline and puts TODO state into group 2 if present.")
  2663. (make-variable-buffer-local 'org-todo-line-regexp)
  2664. (defvar org-complex-heading-regexp nil
  2665. "Matches a headline and puts everything into groups:
  2666. group 1: the stars
  2667. group 2: The todo keyword, maybe
  2668. group 3: Priority cookie
  2669. group 4: True headline
  2670. group 5: Tags")
  2671. (make-variable-buffer-local 'org-complex-heading-regexp)
  2672. (defvar org-todo-line-tags-regexp nil
  2673. "Matches a headline and puts TODO state into group 2 if present.
  2674. Also put tags into group 4 if tags are present.")
  2675. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2676. (defvar org-nl-done-regexp nil
  2677. "Matches newline followed by a headline with the DONE keyword.")
  2678. (make-variable-buffer-local 'org-nl-done-regexp)
  2679. (defvar org-looking-at-done-regexp nil
  2680. "Matches the DONE keyword a point.")
  2681. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2682. (defvar org-ds-keyword-length 12
  2683. "Maximum length of the Deadline and SCHEDULED keywords.")
  2684. (make-variable-buffer-local 'org-ds-keyword-length)
  2685. (defvar org-deadline-regexp nil
  2686. "Matches the DEADLINE keyword.")
  2687. (make-variable-buffer-local 'org-deadline-regexp)
  2688. (defvar org-deadline-time-regexp nil
  2689. "Matches the DEADLINE keyword together with a time stamp.")
  2690. (make-variable-buffer-local 'org-deadline-time-regexp)
  2691. (defvar org-deadline-line-regexp nil
  2692. "Matches the DEADLINE keyword and the rest of the line.")
  2693. (make-variable-buffer-local 'org-deadline-line-regexp)
  2694. (defvar org-scheduled-regexp nil
  2695. "Matches the SCHEDULED keyword.")
  2696. (make-variable-buffer-local 'org-scheduled-regexp)
  2697. (defvar org-scheduled-time-regexp nil
  2698. "Matches the SCHEDULED keyword together with a time stamp.")
  2699. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2700. (defvar org-closed-time-regexp nil
  2701. "Matches the CLOSED keyword together with a time stamp.")
  2702. (make-variable-buffer-local 'org-closed-time-regexp)
  2703. (defvar org-keyword-time-regexp nil
  2704. "Matches any of the 4 keywords, together with the time stamp.")
  2705. (make-variable-buffer-local 'org-keyword-time-regexp)
  2706. (defvar org-keyword-time-not-clock-regexp nil
  2707. "Matches any of the 3 keywords, together with the time stamp.")
  2708. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2709. (defvar org-maybe-keyword-time-regexp nil
  2710. "Matches a timestamp, possibly preceeded by a keyword.")
  2711. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2712. (defvar org-planning-or-clock-line-re nil
  2713. "Matches a line with planning or clock info.")
  2714. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2715. (defconst org-plain-time-of-day-regexp
  2716. (concat
  2717. "\\(\\<[012]?[0-9]"
  2718. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2719. "\\(--?"
  2720. "\\(\\<[012]?[0-9]"
  2721. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2722. "\\)?")
  2723. "Regular expression to match a plain time or time range.
  2724. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2725. groups carry important information:
  2726. 0 the full match
  2727. 1 the first time, range or not
  2728. 8 the second time, if it is a range.")
  2729. (defconst org-plain-time-extension-regexp
  2730. (concat
  2731. "\\(\\<[012]?[0-9]"
  2732. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2733. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2734. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2735. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2736. groups carry important information:
  2737. 0 the full match
  2738. 7 hours of duration
  2739. 9 minutes of duration")
  2740. (defconst org-stamp-time-of-day-regexp
  2741. (concat
  2742. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2743. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2744. "\\(--?"
  2745. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2746. "Regular expression to match a timestamp time or time range.
  2747. After a match, the following groups carry important information:
  2748. 0 the full match
  2749. 1 date plus weekday, for backreferencing to make sure both times on same day
  2750. 2 the first time, range or not
  2751. 4 the second time, if it is a range.")
  2752. (defconst org-startup-options
  2753. '(("fold" org-startup-folded t)
  2754. ("overview" org-startup-folded t)
  2755. ("nofold" org-startup-folded nil)
  2756. ("showall" org-startup-folded nil)
  2757. ("content" org-startup-folded content)
  2758. ("hidestars" org-hide-leading-stars t)
  2759. ("showstars" org-hide-leading-stars nil)
  2760. ("odd" org-odd-levels-only t)
  2761. ("oddeven" org-odd-levels-only nil)
  2762. ("align" org-startup-align-all-tables t)
  2763. ("noalign" org-startup-align-all-tables nil)
  2764. ("customtime" org-display-custom-times t)
  2765. ("logdone" org-log-done time)
  2766. ("lognotedone" org-log-done note)
  2767. ("nologdone" org-log-done nil)
  2768. ("lognoteclock-out" org-log-note-clock-out t)
  2769. ("nolognoteclock-out" org-log-note-clock-out nil)
  2770. ("logrepeat" org-log-repeat state)
  2771. ("lognoterepeat" org-log-repeat note)
  2772. ("nologrepeat" org-log-repeat nil)
  2773. ("fninline" org-footnote-define-inline t)
  2774. ("nofninline" org-footnote-define-inline nil)
  2775. ("fnlocal" org-footnote-section nil)
  2776. ("fnauto" org-footnote-auto-label t)
  2777. ("fnprompt" org-footnote-auto-label nil)
  2778. ("fnconfirm" org-footnote-auto-label confirm)
  2779. ("fnplain" org-footnote-auto-label plain)
  2780. ("constcgs" constants-unit-system cgs)
  2781. ("constSI" constants-unit-system SI))
  2782. "Variable associated with STARTUP options for org-mode.
  2783. Each element is a list of three items: The startup options as written
  2784. in the #+STARTUP line, the corresponding variable, and the value to
  2785. set this variable to if the option is found. An optional forth element PUSH
  2786. means to push this value onto the list in the variable.")
  2787. (defun org-set-regexps-and-options ()
  2788. "Precompute regular expressions for current buffer."
  2789. (when (org-mode-p)
  2790. (org-set-local 'org-todo-kwd-alist nil)
  2791. (org-set-local 'org-todo-key-alist nil)
  2792. (org-set-local 'org-todo-key-trigger nil)
  2793. (org-set-local 'org-todo-keywords-1 nil)
  2794. (org-set-local 'org-done-keywords nil)
  2795. (org-set-local 'org-todo-heads nil)
  2796. (org-set-local 'org-todo-sets nil)
  2797. (org-set-local 'org-todo-log-states nil)
  2798. (org-set-local 'org-file-properties nil)
  2799. (org-set-local 'org-file-tags nil)
  2800. (let ((re (org-make-options-regexp
  2801. '("CATEGORY" "TODO" "COLUMNS"
  2802. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2803. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2804. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2805. (splitre "[ \t]+")
  2806. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2807. tail sep kws1 prio props ftags drawers
  2808. ext-setup-or-nil setup-contents (start 0))
  2809. (save-excursion
  2810. (save-restriction
  2811. (widen)
  2812. (goto-char (point-min))
  2813. (while (or (and ext-setup-or-nil
  2814. (string-match re ext-setup-or-nil start)
  2815. (setq start (match-end 0)))
  2816. (and (setq ext-setup-or-nil nil start 0)
  2817. (re-search-forward re nil t)))
  2818. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2819. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2820. (cond
  2821. ((equal key "CATEGORY")
  2822. (if (string-match "[ \t]+$" value)
  2823. (setq value (replace-match "" t t value)))
  2824. (setq cat value))
  2825. ((member key '("SEQ_TODO" "TODO"))
  2826. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2827. ((equal key "TYP_TODO")
  2828. (push (cons 'type (org-split-string value splitre)) kwds))
  2829. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2830. ;; general TODO-like setup
  2831. (push (cons (intern (downcase (match-string 1 key)))
  2832. (org-split-string value splitre)) kwds))
  2833. ((equal key "TAGS")
  2834. (setq tags (append tags (org-split-string value splitre))))
  2835. ((equal key "COLUMNS")
  2836. (org-set-local 'org-columns-default-format value))
  2837. ((equal key "LINK")
  2838. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2839. (push (cons (match-string 1 value)
  2840. (org-trim (match-string 2 value)))
  2841. links)))
  2842. ((equal key "PRIORITIES")
  2843. (setq prio (org-split-string value " +")))
  2844. ((equal key "PROPERTY")
  2845. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2846. (push (cons (match-string 1 value) (match-string 2 value))
  2847. props)))
  2848. ((equal key "FILETAGS")
  2849. (when (string-match "\\S-" value)
  2850. (setq ftags
  2851. (append
  2852. ftags
  2853. (apply 'append
  2854. (mapcar (lambda (x) (org-split-string x ":"))
  2855. (org-split-string value)))))))
  2856. ((equal key "DRAWERS")
  2857. (setq drawers (org-split-string value splitre)))
  2858. ((equal key "CONSTANTS")
  2859. (setq const (append const (org-split-string value splitre))))
  2860. ((equal key "STARTUP")
  2861. (let ((opts (org-split-string value splitre))
  2862. l var val)
  2863. (while (setq l (pop opts))
  2864. (when (setq l (assoc l org-startup-options))
  2865. (setq var (nth 1 l) val (nth 2 l))
  2866. (if (not (nth 3 l))
  2867. (set (make-local-variable var) val)
  2868. (if (not (listp (symbol-value var)))
  2869. (set (make-local-variable var) nil))
  2870. (set (make-local-variable var) (symbol-value var))
  2871. (add-to-list var val))))))
  2872. ((equal key "ARCHIVE")
  2873. (string-match " *$" value)
  2874. (setq arch (replace-match "" t t value))
  2875. (remove-text-properties 0 (length arch)
  2876. '(face t fontified t) arch))
  2877. ((equal key "SETUPFILE")
  2878. (setq setup-contents (org-file-contents
  2879. (expand-file-name
  2880. (org-remove-double-quotes value))
  2881. 'noerror))
  2882. (if (not ext-setup-or-nil)
  2883. (setq ext-setup-or-nil setup-contents start 0)
  2884. (setq ext-setup-or-nil
  2885. (concat (substring ext-setup-or-nil 0 start)
  2886. "\n" setup-contents "\n"
  2887. (substring ext-setup-or-nil start)))))
  2888. ))))
  2889. (when cat
  2890. (org-set-local 'org-category (intern cat))
  2891. (push (cons "CATEGORY" cat) props))
  2892. (when prio
  2893. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2894. (setq prio (mapcar 'string-to-char prio))
  2895. (org-set-local 'org-highest-priority (nth 0 prio))
  2896. (org-set-local 'org-lowest-priority (nth 1 prio))
  2897. (org-set-local 'org-default-priority (nth 2 prio)))
  2898. (and props (org-set-local 'org-file-properties (nreverse props)))
  2899. (and ftags (org-set-local 'org-file-tags ftags))
  2900. (and drawers (org-set-local 'org-drawers drawers))
  2901. (and arch (org-set-local 'org-archive-location arch))
  2902. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2903. ;; Process the TODO keywords
  2904. (unless kwds
  2905. ;; Use the global values as if they had been given locally.
  2906. (setq kwds (default-value 'org-todo-keywords))
  2907. (if (stringp (car kwds))
  2908. (setq kwds (list (cons org-todo-interpretation
  2909. (default-value 'org-todo-keywords)))))
  2910. (setq kwds (reverse kwds)))
  2911. (setq kwds (nreverse kwds))
  2912. (let (inter kws kw)
  2913. (while (setq kws (pop kwds))
  2914. (let ((kws (or
  2915. (run-hook-with-args-until-success
  2916. 'org-todo-setup-filter-hook kws)
  2917. kws)))
  2918. (setq inter (pop kws) sep (member "|" kws)
  2919. kws0 (delete "|" (copy-sequence kws))
  2920. kwsa nil
  2921. kws1 (mapcar
  2922. (lambda (x)
  2923. ;; 1 2
  2924. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2925. (progn
  2926. (setq kw (match-string 1 x)
  2927. key (and (match-end 2) (match-string 2 x))
  2928. log (org-extract-log-state-settings x))
  2929. (push (cons kw (and key (string-to-char key))) kwsa)
  2930. (and log (push log org-todo-log-states))
  2931. kw)
  2932. (error "Invalid TODO keyword %s" x)))
  2933. kws0)
  2934. kwsa (if kwsa (append '((:startgroup))
  2935. (nreverse kwsa)
  2936. '((:endgroup))))
  2937. hw (car kws1)
  2938. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2939. tail (list inter hw (car dws) (org-last dws))))
  2940. (add-to-list 'org-todo-heads hw 'append)
  2941. (push kws1 org-todo-sets)
  2942. (setq org-done-keywords (append org-done-keywords dws nil))
  2943. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2944. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2945. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2946. (setq org-todo-sets (nreverse org-todo-sets)
  2947. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2948. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2949. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2950. ;; Process the constants
  2951. (when const
  2952. (let (e cst)
  2953. (while (setq e (pop const))
  2954. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2955. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2956. (setq org-table-formula-constants-local cst)))
  2957. ;; Process the tags.
  2958. (when tags
  2959. (let (e tgs)
  2960. (while (setq e (pop tags))
  2961. (cond
  2962. ((equal e "{") (push '(:startgroup) tgs))
  2963. ((equal e "}") (push '(:endgroup) tgs))
  2964. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2965. (push (cons (match-string 1 e)
  2966. (string-to-char (match-string 2 e)))
  2967. tgs))
  2968. (t (push (list e) tgs))))
  2969. (org-set-local 'org-tag-alist nil)
  2970. (while (setq e (pop tgs))
  2971. (or (and (stringp (car e))
  2972. (assoc (car e) org-tag-alist))
  2973. (push e org-tag-alist)))))
  2974. ;; Compute the regular expressions and other local variables
  2975. (if (not org-done-keywords)
  2976. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2977. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2978. (length org-scheduled-string)
  2979. (length org-clock-string)
  2980. (length org-closed-string)))
  2981. org-drawer-regexp
  2982. (concat "^[ \t]*:\\("
  2983. (mapconcat 'regexp-quote org-drawers "\\|")
  2984. "\\):[ \t]*$")
  2985. org-not-done-keywords
  2986. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2987. org-todo-regexp
  2988. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2989. "\\|") "\\)\\>")
  2990. org-not-done-regexp
  2991. (concat "\\<\\("
  2992. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2993. "\\)\\>")
  2994. org-todo-line-regexp
  2995. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2996. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2997. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2998. org-complex-heading-regexp
  2999. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3000. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3001. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3002. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3003. org-nl-done-regexp
  3004. (concat "\n\\*+[ \t]+"
  3005. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3006. "\\)" "\\>")
  3007. org-todo-line-tags-regexp
  3008. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3009. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3010. (org-re
  3011. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3012. org-looking-at-done-regexp
  3013. (concat "^" "\\(?:"
  3014. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3015. "\\>")
  3016. org-deadline-regexp (concat "\\<" org-deadline-string)
  3017. org-deadline-time-regexp
  3018. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3019. org-deadline-line-regexp
  3020. (concat "\\<\\(" org-deadline-string "\\).*")
  3021. org-scheduled-regexp
  3022. (concat "\\<" org-scheduled-string)
  3023. org-scheduled-time-regexp
  3024. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3025. org-closed-time-regexp
  3026. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3027. org-keyword-time-regexp
  3028. (concat "\\<\\(" org-scheduled-string
  3029. "\\|" org-deadline-string
  3030. "\\|" org-closed-string
  3031. "\\|" org-clock-string "\\)"
  3032. " *[[<]\\([^]>]+\\)[]>]")
  3033. org-keyword-time-not-clock-regexp
  3034. (concat "\\<\\(" org-scheduled-string
  3035. "\\|" org-deadline-string
  3036. "\\|" org-closed-string
  3037. "\\)"
  3038. " *[[<]\\([^]>]+\\)[]>]")
  3039. org-maybe-keyword-time-regexp
  3040. (concat "\\(\\<\\(" org-scheduled-string
  3041. "\\|" org-deadline-string
  3042. "\\|" org-closed-string
  3043. "\\|" org-clock-string "\\)\\)?"
  3044. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3045. org-planning-or-clock-line-re
  3046. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3047. "\\|" org-deadline-string
  3048. "\\|" org-closed-string "\\|" org-clock-string
  3049. "\\)\\>\\)")
  3050. )
  3051. (org-compute-latex-and-specials-regexp)
  3052. (org-set-font-lock-defaults))))
  3053. (defun org-file-contents (file &optional noerror)
  3054. "Return the contents of FILE, as a string."
  3055. (if (or (not file)
  3056. (not (file-readable-p file)))
  3057. (if noerror
  3058. (progn
  3059. (message "Cannot read file %s" file)
  3060. (ding) (sit-for 2)
  3061. "")
  3062. (error "Cannot read file %s" file))
  3063. (with-temp-buffer
  3064. (insert-file-contents file)
  3065. (buffer-string))))
  3066. (defun org-extract-log-state-settings (x)
  3067. "Extract the log state setting from a TODO keyword string.
  3068. This will extract info from a string like \"WAIT(w@/!)\"."
  3069. (let (kw key log1 log2)
  3070. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3071. (setq kw (match-string 1 x)
  3072. key (and (match-end 2) (match-string 2 x))
  3073. log1 (and (match-end 3) (match-string 3 x))
  3074. log2 (and (match-end 4) (match-string 4 x)))
  3075. (and (or log1 log2)
  3076. (list kw
  3077. (and log1 (if (equal log1 "!") 'time 'note))
  3078. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3079. (defun org-remove-keyword-keys (list)
  3080. "Remove a pair of parenthesis at the end of each string in LIST."
  3081. (mapcar (lambda (x)
  3082. (if (string-match "(.*)$" x)
  3083. (substring x 0 (match-beginning 0))
  3084. x))
  3085. list))
  3086. ;; FIXME: this could be done much better, using second characters etc.
  3087. (defun org-assign-fast-keys (alist)
  3088. "Assign fast keys to a keyword-key alist.
  3089. Respect keys that are already there."
  3090. (let (new e k c c1 c2 (char ?a))
  3091. (while (setq e (pop alist))
  3092. (cond
  3093. ((equal e '(:startgroup)) (push e new))
  3094. ((equal e '(:endgroup)) (push e new))
  3095. (t
  3096. (setq k (car e) c2 nil)
  3097. (if (cdr e)
  3098. (setq c (cdr e))
  3099. ;; automatically assign a character.
  3100. (setq c1 (string-to-char
  3101. (downcase (substring
  3102. k (if (= (string-to-char k) ?@) 1 0)))))
  3103. (if (or (rassoc c1 new) (rassoc c1 alist))
  3104. (while (or (rassoc char new) (rassoc char alist))
  3105. (setq char (1+ char)))
  3106. (setq c2 c1))
  3107. (setq c (or c2 char)))
  3108. (push (cons k c) new))))
  3109. (nreverse new)))
  3110. ;;; Some variables used in various places
  3111. (defvar org-window-configuration nil
  3112. "Used in various places to store a window configuration.")
  3113. (defvar org-finish-function nil
  3114. "Function to be called when `C-c C-c' is used.
  3115. This is for getting out of special buffers like remember.")
  3116. ;; FIXME: Occasionally check by commenting these, to make sure
  3117. ;; no other functions uses these, forgetting to let-bind them.
  3118. (defvar entry)
  3119. (defvar state)
  3120. (defvar last-state)
  3121. (defvar date)
  3122. (defvar description)
  3123. ;; Defined somewhere in this file, but used before definition.
  3124. (defvar org-html-entities)
  3125. (defvar org-struct-menu)
  3126. (defvar org-org-menu)
  3127. (defvar org-tbl-menu)
  3128. (defvar org-agenda-keymap)
  3129. ;;;; Define the Org-mode
  3130. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3131. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  3132. ;; We use a before-change function to check if a table might need
  3133. ;; an update.
  3134. (defvar org-table-may-need-update t
  3135. "Indicates that a table might need an update.
  3136. This variable is set by `org-before-change-function'.
  3137. `org-table-align' sets it back to nil.")
  3138. (defun org-before-change-function (beg end)
  3139. "Every change indicates that a table might need an update."
  3140. (setq org-table-may-need-update t))
  3141. (defvar org-mode-map)
  3142. (defvar org-mode-hook nil
  3143. "Mode hook for Org-mode, run after the mode was turned on.")
  3144. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3145. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3146. (defvar org-table-buffer-is-an nil)
  3147. (defconst org-outline-regexp "\\*+ ")
  3148. ;;;###autoload
  3149. (define-derived-mode org-mode outline-mode "Org"
  3150. "Outline-based notes management and organizer, alias
  3151. \"Carsten's outline-mode for keeping track of everything.\"
  3152. Org-mode develops organizational tasks around a NOTES file which
  3153. contains information about projects as plain text. Org-mode is
  3154. implemented on top of outline-mode, which is ideal to keep the content
  3155. of large files well structured. It supports ToDo items, deadlines and
  3156. time stamps, which magically appear in the diary listing of the Emacs
  3157. calendar. Tables are easily created with a built-in table editor.
  3158. Plain text URL-like links connect to websites, emails (VM), Usenet
  3159. messages (Gnus), BBDB entries, and any files related to the project.
  3160. For printing and sharing of notes, an Org-mode file (or a part of it)
  3161. can be exported as a structured ASCII or HTML file.
  3162. The following commands are available:
  3163. \\{org-mode-map}"
  3164. ;; Get rid of Outline menus, they are not needed
  3165. ;; Need to do this here because define-derived-mode sets up
  3166. ;; the keymap so late. Still, it is a waste to call this each time
  3167. ;; we switch another buffer into org-mode.
  3168. (if (featurep 'xemacs)
  3169. (when (boundp 'outline-mode-menu-heading)
  3170. ;; Assume this is Greg's port, it used easymenu
  3171. (easy-menu-remove outline-mode-menu-heading)
  3172. (easy-menu-remove outline-mode-menu-show)
  3173. (easy-menu-remove outline-mode-menu-hide))
  3174. (define-key org-mode-map [menu-bar headings] 'undefined)
  3175. (define-key org-mode-map [menu-bar hide] 'undefined)
  3176. (define-key org-mode-map [menu-bar show] 'undefined))
  3177. (org-load-modules-maybe)
  3178. (easy-menu-add org-org-menu)
  3179. (easy-menu-add org-tbl-menu)
  3180. (org-install-agenda-files-menu)
  3181. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3182. (org-add-to-invisibility-spec '(org-cwidth))
  3183. (when (featurep 'xemacs)
  3184. (org-set-local 'line-move-ignore-invisible t))
  3185. (org-set-local 'outline-regexp org-outline-regexp)
  3186. (org-set-local 'outline-level 'org-outline-level)
  3187. (when (and org-ellipsis
  3188. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3189. (fboundp 'make-glyph-code))
  3190. (unless org-display-table
  3191. (setq org-display-table (make-display-table)))
  3192. (set-display-table-slot
  3193. org-display-table 4
  3194. (vconcat (mapcar
  3195. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3196. org-ellipsis)))
  3197. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3198. (setq buffer-display-table org-display-table))
  3199. (org-set-regexps-and-options)
  3200. (when (and org-tag-faces (not org-tags-special-faces-re))
  3201. ;; tag faces set outside customize.... force initialization.
  3202. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3203. ;; Calc embedded
  3204. (org-set-local 'calc-embedded-open-mode "# ")
  3205. (modify-syntax-entry ?# "<")
  3206. (modify-syntax-entry ?@ "w")
  3207. (if org-startup-truncated (setq truncate-lines t))
  3208. (org-set-local 'font-lock-unfontify-region-function
  3209. 'org-unfontify-region)
  3210. ;; Activate before-change-function
  3211. (org-set-local 'org-table-may-need-update t)
  3212. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3213. 'local)
  3214. ;; Check for running clock before killing a buffer
  3215. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3216. ;; Paragraphs and auto-filling
  3217. (org-set-autofill-regexps)
  3218. (setq indent-line-function 'org-indent-line-function)
  3219. (org-update-radio-target-regexp)
  3220. ;; Make sure dependence stuff works reliably, even for users who set it
  3221. ;; too late :-(
  3222. (if org-enforce-todo-dependencies
  3223. (add-hook 'org-blocker-hook
  3224. 'org-block-todo-from-children-or-siblings)
  3225. (remove-hook 'org-blocker-hook
  3226. 'org-block-todo-from-children-or-siblings))
  3227. (if org-enforce-todo-checkbox-dependencies
  3228. (add-hook 'org-blocker-hook
  3229. 'org-block-todo-from-checkboxes)
  3230. (remove-hook 'org-blocker-hook
  3231. 'org-block-todo-from-checkboxes))
  3232. ;; Comment characters
  3233. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3234. (org-set-local 'comment-padding " ")
  3235. ;; Align options lines
  3236. (org-set-local
  3237. 'align-mode-rules-list
  3238. '((org-in-buffer-settings
  3239. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3240. (modes . '(org-mode)))))
  3241. ;; Imenu
  3242. (org-set-local 'imenu-create-index-function
  3243. 'org-imenu-get-tree)
  3244. ;; Make isearch reveal context
  3245. (if (or (featurep 'xemacs)
  3246. (not (boundp 'outline-isearch-open-invisible-function)))
  3247. ;; Emacs 21 and XEmacs make use of the hook
  3248. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3249. ;; Emacs 22 deals with this through a special variable
  3250. (org-set-local 'outline-isearch-open-invisible-function
  3251. (lambda (&rest ignore) (org-show-context 'isearch))))
  3252. ;; If empty file that did not turn on org-mode automatically, make it to.
  3253. (if (and org-insert-mode-line-in-empty-file
  3254. (interactive-p)
  3255. (= (point-min) (point-max)))
  3256. (insert "# -*- mode: org -*-\n\n"))
  3257. (unless org-inhibit-startup
  3258. (when org-startup-align-all-tables
  3259. (let ((bmp (buffer-modified-p)))
  3260. (org-table-map-tables 'org-table-align)
  3261. (set-buffer-modified-p bmp)))
  3262. (org-set-startup-visibility)))
  3263. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3264. (defun org-current-time ()
  3265. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3266. (if (> (car org-time-stamp-rounding-minutes) 1)
  3267. (let ((r (car org-time-stamp-rounding-minutes))
  3268. (time (decode-time)))
  3269. (apply 'encode-time
  3270. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3271. (nthcdr 2 time))))
  3272. (current-time)))
  3273. ;;;; Font-Lock stuff, including the activators
  3274. (defvar org-mouse-map (make-sparse-keymap))
  3275. (org-defkey org-mouse-map
  3276. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3277. (org-defkey org-mouse-map
  3278. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3279. (when org-mouse-1-follows-link
  3280. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3281. (when org-tab-follows-link
  3282. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3283. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3284. (require 'font-lock)
  3285. (defconst org-non-link-chars "]\t\n\r<>")
  3286. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3287. "shell" "elisp"))
  3288. (defvar org-link-types-re nil
  3289. "Matches a link that has a url-like prefix like \"http:\"")
  3290. (defvar org-link-re-with-space nil
  3291. "Matches a link with spaces, optional angular brackets around it.")
  3292. (defvar org-link-re-with-space2 nil
  3293. "Matches a link with spaces, optional angular brackets around it.")
  3294. (defvar org-link-re-with-space3 nil
  3295. "Matches a link with spaces, only for internal part in bracket links.")
  3296. (defvar org-angle-link-re nil
  3297. "Matches link with angular brackets, spaces are allowed.")
  3298. (defvar org-plain-link-re nil
  3299. "Matches plain link, without spaces.")
  3300. (defvar org-bracket-link-regexp nil
  3301. "Matches a link in double brackets.")
  3302. (defvar org-bracket-link-analytic-regexp nil
  3303. "Regular expression used to analyze links.
  3304. Here is what the match groups contain after a match:
  3305. 1: http:
  3306. 2: http
  3307. 3: path
  3308. 4: [desc]
  3309. 5: desc")
  3310. (defvar org-bracket-link-analytic-regexp++ nil
  3311. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3312. (defvar org-any-link-re nil
  3313. "Regular expression matching any link.")
  3314. (defun org-make-link-regexps ()
  3315. "Update the link regular expressions.
  3316. This should be called after the variable `org-link-types' has changed."
  3317. (setq org-link-types-re
  3318. (concat
  3319. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3320. org-link-re-with-space
  3321. (concat
  3322. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3323. "\\([^" org-non-link-chars " ]"
  3324. "[^" org-non-link-chars "]*"
  3325. "[^" org-non-link-chars " ]\\)>?")
  3326. org-link-re-with-space2
  3327. (concat
  3328. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3329. "\\([^" org-non-link-chars " ]"
  3330. "[^\t\n\r]*"
  3331. "[^" org-non-link-chars " ]\\)>?")
  3332. org-link-re-with-space3
  3333. (concat
  3334. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3335. "\\([^" org-non-link-chars " ]"
  3336. "[^\t\n\r]*\\)")
  3337. org-angle-link-re
  3338. (concat
  3339. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3340. "\\([^" org-non-link-chars " ]"
  3341. "[^" org-non-link-chars "]*"
  3342. "\\)>")
  3343. org-plain-link-re
  3344. (concat
  3345. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3346. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3347. org-bracket-link-regexp
  3348. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3349. org-bracket-link-analytic-regexp
  3350. (concat
  3351. "\\[\\["
  3352. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3353. "\\([^]]+\\)"
  3354. "\\]"
  3355. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3356. "\\]")
  3357. org-bracket-link-analytic-regexp++
  3358. (concat
  3359. "\\[\\["
  3360. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3361. "\\([^]]+\\)"
  3362. "\\]"
  3363. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3364. "\\]")
  3365. org-any-link-re
  3366. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3367. org-angle-link-re "\\)\\|\\("
  3368. org-plain-link-re "\\)")))
  3369. (org-make-link-regexps)
  3370. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3371. "Regular expression for fast time stamp matching.")
  3372. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3373. "Regular expression for fast time stamp matching.")
  3374. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3375. "Regular expression matching time strings for analysis.
  3376. This one does not require the space after the date, so it can be used
  3377. on a string that terminates immediately after the date.")
  3378. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3379. "Regular expression matching time strings for analysis.")
  3380. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3381. "Regular expression matching time stamps, with groups.")
  3382. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3383. "Regular expression matching time stamps (also [..]), with groups.")
  3384. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3385. "Regular expression matching a time stamp range.")
  3386. (defconst org-tr-regexp-both
  3387. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3388. "Regular expression matching a time stamp range.")
  3389. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3390. org-ts-regexp "\\)?")
  3391. "Regular expression matching a time stamp or time stamp range.")
  3392. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3393. org-ts-regexp-both "\\)?")
  3394. "Regular expression matching a time stamp or time stamp range.
  3395. The time stamps may be either active or inactive.")
  3396. (defvar org-emph-face nil)
  3397. (defun org-do-emphasis-faces (limit)
  3398. "Run through the buffer and add overlays to links."
  3399. (let (rtn)
  3400. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3401. (if (not (= (char-after (match-beginning 3))
  3402. (char-after (match-beginning 4))))
  3403. (progn
  3404. (setq rtn t)
  3405. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3406. 'face
  3407. (nth 1 (assoc (match-string 3)
  3408. org-emphasis-alist)))
  3409. (add-text-properties (match-beginning 2) (match-end 2)
  3410. '(font-lock-multiline t))
  3411. (when org-hide-emphasis-markers
  3412. (add-text-properties (match-end 4) (match-beginning 5)
  3413. '(invisible org-link))
  3414. (add-text-properties (match-beginning 3) (match-end 3)
  3415. '(invisible org-link)))))
  3416. (backward-char 1))
  3417. rtn))
  3418. (defun org-emphasize (&optional char)
  3419. "Insert or change an emphasis, i.e. a font like bold or italic.
  3420. If there is an active region, change that region to a new emphasis.
  3421. If there is no region, just insert the marker characters and position
  3422. the cursor between them.
  3423. CHAR should be either the marker character, or the first character of the
  3424. HTML tag associated with that emphasis. If CHAR is a space, the means
  3425. to remove the emphasis of the selected region.
  3426. If char is not given (for example in an interactive call) it
  3427. will be prompted for."
  3428. (interactive)
  3429. (let ((eal org-emphasis-alist) e det
  3430. (erc org-emphasis-regexp-components)
  3431. (prompt "")
  3432. (string "") beg end move tag c s)
  3433. (if (org-region-active-p)
  3434. (setq beg (region-beginning) end (region-end)
  3435. string (buffer-substring beg end))
  3436. (setq move t))
  3437. (while (setq e (pop eal))
  3438. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3439. c (aref tag 0))
  3440. (push (cons c (string-to-char (car e))) det)
  3441. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3442. (substring tag 1)))))
  3443. (setq det (nreverse det))
  3444. (unless char
  3445. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3446. (setq char (read-char-exclusive)))
  3447. (setq char (or (cdr (assoc char det)) char))
  3448. (if (equal char ?\ )
  3449. (setq s "" move nil)
  3450. (unless (assoc (char-to-string char) org-emphasis-alist)
  3451. (error "No such emphasis marker: \"%c\"" char))
  3452. (setq s (char-to-string char)))
  3453. (while (and (> (length string) 1)
  3454. (equal (substring string 0 1) (substring string -1))
  3455. (assoc (substring string 0 1) org-emphasis-alist))
  3456. (setq string (substring string 1 -1)))
  3457. (setq string (concat s string s))
  3458. (if beg (delete-region beg end))
  3459. (unless (or (bolp)
  3460. (string-match (concat "[" (nth 0 erc) "\n]")
  3461. (char-to-string (char-before (point)))))
  3462. (insert " "))
  3463. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3464. (char-to-string (char-after (point))))
  3465. (insert " ") (backward-char 1))
  3466. (insert string)
  3467. (and move (backward-char 1))))
  3468. (defconst org-nonsticky-props
  3469. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3470. (defun org-activate-plain-links (limit)
  3471. "Run through the buffer and add overlays to links."
  3472. (catch 'exit
  3473. (let (f)
  3474. (while (re-search-forward org-plain-link-re limit t)
  3475. (setq f (get-text-property (match-beginning 0) 'face))
  3476. (if (or (eq f 'org-tag)
  3477. (and (listp f) (memq 'org-tag f)))
  3478. nil
  3479. (add-text-properties (match-beginning 0) (match-end 0)
  3480. (list 'mouse-face 'highlight
  3481. 'rear-nonsticky org-nonsticky-props
  3482. 'keymap org-mouse-map
  3483. ))
  3484. (throw 'exit t))))))
  3485. (defun org-activate-code (limit)
  3486. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3487. (progn
  3488. (remove-text-properties (match-beginning 0) (match-end 0)
  3489. '(display t invisible t intangible t))
  3490. t)))
  3491. (defun org-activate-angle-links (limit)
  3492. "Run through the buffer and add overlays to links."
  3493. (if (re-search-forward org-angle-link-re limit t)
  3494. (progn
  3495. (add-text-properties (match-beginning 0) (match-end 0)
  3496. (list 'mouse-face 'highlight
  3497. 'rear-nonsticky org-nonsticky-props
  3498. 'keymap org-mouse-map
  3499. ))
  3500. t)))
  3501. (defun org-activate-footnote-links (limit)
  3502. "Run through the buffer and add overlays to links."
  3503. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3504. limit t)
  3505. (progn
  3506. (add-text-properties (match-beginning 2) (match-end 2)
  3507. (list 'mouse-face 'highlight
  3508. 'rear-nonsticky org-nonsticky-props
  3509. 'keymap org-mouse-map
  3510. 'help-echo
  3511. (if (= (point-at-bol) (match-beginning 2))
  3512. "Footnote definition"
  3513. "Footnote reference")
  3514. ))
  3515. t)))
  3516. (defun org-activate-bracket-links (limit)
  3517. "Run through the buffer and add overlays to bracketed links."
  3518. (if (re-search-forward org-bracket-link-regexp limit t)
  3519. (let* ((help (concat "LINK: "
  3520. (org-match-string-no-properties 1)))
  3521. ;; FIXME: above we should remove the escapes.
  3522. ;; but that requires another match, protecting match data,
  3523. ;; a lot of overhead for font-lock.
  3524. (ip (org-maybe-intangible
  3525. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3526. 'keymap org-mouse-map 'mouse-face 'highlight
  3527. 'font-lock-multiline t 'help-echo help)))
  3528. (vp (list 'rear-nonsticky org-nonsticky-props
  3529. 'keymap org-mouse-map 'mouse-face 'highlight
  3530. ' font-lock-multiline t 'help-echo help)))
  3531. ;; We need to remove the invisible property here. Table narrowing
  3532. ;; may have made some of this invisible.
  3533. (remove-text-properties (match-beginning 0) (match-end 0)
  3534. '(invisible nil))
  3535. (if (match-end 3)
  3536. (progn
  3537. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3538. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3539. (add-text-properties (match-end 3) (match-end 0) ip))
  3540. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3541. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3542. (add-text-properties (match-end 1) (match-end 0) ip))
  3543. t)))
  3544. (defun org-activate-dates (limit)
  3545. "Run through the buffer and add overlays to dates."
  3546. (if (re-search-forward org-tsr-regexp-both limit t)
  3547. (progn
  3548. (add-text-properties (match-beginning 0) (match-end 0)
  3549. (list 'mouse-face 'highlight
  3550. 'rear-nonsticky org-nonsticky-props
  3551. 'keymap org-mouse-map))
  3552. (when org-display-custom-times
  3553. (if (match-end 3)
  3554. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3555. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3556. t)))
  3557. (defvar org-target-link-regexp nil
  3558. "Regular expression matching radio targets in plain text.")
  3559. (make-variable-buffer-local 'org-target-link-regexp)
  3560. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3561. "Regular expression matching a link target.")
  3562. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3563. "Regular expression matching a radio target.")
  3564. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3565. "Regular expression matching any target.")
  3566. (defun org-activate-target-links (limit)
  3567. "Run through the buffer and add overlays to target matches."
  3568. (when org-target-link-regexp
  3569. (let ((case-fold-search t))
  3570. (if (re-search-forward org-target-link-regexp limit t)
  3571. (progn
  3572. (add-text-properties (match-beginning 0) (match-end 0)
  3573. (list 'mouse-face 'highlight
  3574. 'rear-nonsticky org-nonsticky-props
  3575. 'keymap org-mouse-map
  3576. 'help-echo "Radio target link"
  3577. 'org-linked-text t))
  3578. t)))))
  3579. (defun org-update-radio-target-regexp ()
  3580. "Find all radio targets in this file and update the regular expression."
  3581. (interactive)
  3582. (when (memq 'radio org-activate-links)
  3583. (setq org-target-link-regexp
  3584. (org-make-target-link-regexp (org-all-targets 'radio)))
  3585. (org-restart-font-lock)))
  3586. (defun org-hide-wide-columns (limit)
  3587. (let (s e)
  3588. (setq s (text-property-any (point) (or limit (point-max))
  3589. 'org-cwidth t))
  3590. (when s
  3591. (setq e (next-single-property-change s 'org-cwidth))
  3592. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3593. (goto-char e)
  3594. t)))
  3595. (defvar org-latex-and-specials-regexp nil
  3596. "Regular expression for highlighting export special stuff.")
  3597. (defvar org-match-substring-regexp)
  3598. (defvar org-match-substring-with-braces-regexp)
  3599. (defvar org-export-html-special-string-regexps)
  3600. (defun org-compute-latex-and-specials-regexp ()
  3601. "Compute regular expression for stuff treated specially by exporters."
  3602. (if (not org-highlight-latex-fragments-and-specials)
  3603. (org-set-local 'org-latex-and-specials-regexp nil)
  3604. (require 'org-exp)
  3605. (let*
  3606. ((matchers (plist-get org-format-latex-options :matchers))
  3607. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3608. org-latex-regexps)))
  3609. (options (org-combine-plists (org-default-export-plist)
  3610. (org-infile-export-plist)))
  3611. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3612. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3613. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3614. (org-export-html-expand (plist-get options :expand-quoted-html))
  3615. (org-export-with-special-strings (plist-get options :special-strings))
  3616. (re-sub
  3617. (cond
  3618. ((equal org-export-with-sub-superscripts '{})
  3619. (list org-match-substring-with-braces-regexp))
  3620. (org-export-with-sub-superscripts
  3621. (list org-match-substring-regexp))
  3622. (t nil)))
  3623. (re-latex
  3624. (if org-export-with-LaTeX-fragments
  3625. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3626. (re-macros
  3627. (if org-export-with-TeX-macros
  3628. (list (concat "\\\\"
  3629. (regexp-opt
  3630. (append (mapcar 'car org-html-entities)
  3631. (if (boundp 'org-latex-entities)
  3632. org-latex-entities nil))
  3633. 'words))) ; FIXME
  3634. ))
  3635. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3636. (re-special (if org-export-with-special-strings
  3637. (mapcar (lambda (x) (car x))
  3638. org-export-html-special-string-regexps)))
  3639. (re-rest
  3640. (delq nil
  3641. (list
  3642. (if org-export-html-expand "@<[^>\n]+>")
  3643. ))))
  3644. (org-set-local
  3645. 'org-latex-and-specials-regexp
  3646. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3647. re-rest) "\\|")))))
  3648. (defun org-do-latex-and-special-faces (limit)
  3649. "Run through the buffer and add overlays to links."
  3650. (when org-latex-and-specials-regexp
  3651. (let (rtn d)
  3652. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3653. limit t))
  3654. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3655. 'face))
  3656. '(org-code org-verbatim underline)))
  3657. (progn
  3658. (setq rtn t
  3659. d (cond ((member (char-after (1+ (match-beginning 0)))
  3660. '(?_ ?^)) 1)
  3661. (t 0)))
  3662. (font-lock-prepend-text-property
  3663. (+ d (match-beginning 0)) (match-end 0)
  3664. 'face 'org-latex-and-export-specials)
  3665. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3666. '(font-lock-multiline t)))))
  3667. rtn)))
  3668. (defun org-restart-font-lock ()
  3669. "Restart font-lock-mode, to force refontification."
  3670. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3671. (font-lock-mode -1)
  3672. (font-lock-mode 1)))
  3673. (defun org-all-targets (&optional radio)
  3674. "Return a list of all targets in this file.
  3675. With optional argument RADIO, only find radio targets."
  3676. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3677. rtn)
  3678. (save-excursion
  3679. (goto-char (point-min))
  3680. (while (re-search-forward re nil t)
  3681. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3682. rtn)))
  3683. (defun org-make-target-link-regexp (targets)
  3684. "Make regular expression matching all strings in TARGETS.
  3685. The regular expression finds the targets also if there is a line break
  3686. between words."
  3687. (and targets
  3688. (concat
  3689. "\\<\\("
  3690. (mapconcat
  3691. (lambda (x)
  3692. (while (string-match " +" x)
  3693. (setq x (replace-match "\\s-+" t t x)))
  3694. x)
  3695. targets
  3696. "\\|")
  3697. "\\)\\>")))
  3698. (defun org-activate-tags (limit)
  3699. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3700. (progn
  3701. (add-text-properties (match-beginning 1) (match-end 1)
  3702. (list 'mouse-face 'highlight
  3703. 'rear-nonsticky org-nonsticky-props
  3704. 'keymap org-mouse-map))
  3705. t)))
  3706. (defun org-outline-level ()
  3707. (save-excursion
  3708. (looking-at outline-regexp)
  3709. (if (match-beginning 1)
  3710. (+ (org-get-string-indentation (match-string 1)) 1000)
  3711. (1- (- (match-end 0) (match-beginning 0))))))
  3712. (defvar org-font-lock-keywords nil)
  3713. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3714. "Regular expression matching a property line.")
  3715. (defvar org-font-lock-hook nil
  3716. "Functions to be called for special font lock stuff.")
  3717. (defun org-font-lock-hook (limit)
  3718. (run-hook-with-args 'org-font-lock-hook limit))
  3719. (defun org-set-font-lock-defaults ()
  3720. (let* ((em org-fontify-emphasized-text)
  3721. (lk org-activate-links)
  3722. (org-font-lock-extra-keywords
  3723. (list
  3724. ;; Call the hook
  3725. '(org-font-lock-hook)
  3726. ;; Headlines
  3727. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3728. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3729. ;; Table lines
  3730. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3731. (1 'org-table t))
  3732. ;; Table internals
  3733. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3734. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3735. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3736. ;; Drawers
  3737. (list org-drawer-regexp '(0 'org-special-keyword t))
  3738. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3739. ;; Properties
  3740. (list org-property-re
  3741. '(1 'org-special-keyword t)
  3742. '(3 'org-property-value t))
  3743. (if org-format-transports-properties-p
  3744. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3745. ;; Links
  3746. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3747. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3748. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3749. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3750. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3751. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3752. (if (memq 'footnote lk) '(org-activate-footnote-links
  3753. (2 'org-footnote t)))
  3754. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3755. '(org-hide-wide-columns (0 nil append))
  3756. ;; TODO lines
  3757. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3758. '(1 (org-get-todo-face 1) t))
  3759. ;; DONE
  3760. (if org-fontify-done-headline
  3761. (list (concat "^[*]+ +\\<\\("
  3762. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3763. "\\)\\(.*\\)")
  3764. '(2 'org-headline-done t))
  3765. nil)
  3766. ;; Priorities
  3767. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3768. ;; Tags
  3769. '(org-font-lock-add-tag-faces)
  3770. ;; Special keywords
  3771. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3772. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3773. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3774. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3775. ;; Emphasis
  3776. (if em
  3777. (if (featurep 'xemacs)
  3778. '(org-do-emphasis-faces (0 nil append))
  3779. '(org-do-emphasis-faces)))
  3780. ;; Checkboxes
  3781. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3782. 2 'bold prepend)
  3783. (if org-provide-checkbox-statistics
  3784. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3785. (0 (org-get-checkbox-statistics-face) t)))
  3786. ;; Description list items
  3787. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3788. 2 'bold prepend)
  3789. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3790. '(1 'org-archived prepend))
  3791. ;; Specials
  3792. '(org-do-latex-and-special-faces)
  3793. ;; Code
  3794. '(org-activate-code (1 'org-code t))
  3795. ;; COMMENT
  3796. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3797. "\\|" org-quote-string "\\)\\>")
  3798. '(1 'org-special-keyword t))
  3799. '("^#.*" (0 'font-lock-comment-face t))
  3800. )))
  3801. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3802. ;; Now set the full font-lock-keywords
  3803. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3804. (org-set-local 'font-lock-defaults
  3805. '(org-font-lock-keywords t nil nil backward-paragraph))
  3806. (kill-local-variable 'font-lock-keywords) nil))
  3807. (defvar org-m nil)
  3808. (defvar org-l nil)
  3809. (defvar org-f nil)
  3810. (defun org-get-level-face (n)
  3811. "Get the right face for match N in font-lock matching of headlines."
  3812. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3813. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3814. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3815. (cond
  3816. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3817. ((eq n 2) org-f)
  3818. (t (if org-level-color-stars-only nil org-f))))
  3819. (defun org-get-todo-face (kwd)
  3820. "Get the right face for a TODO keyword KWD.
  3821. If KWD is a number, get the corresponding match group."
  3822. (if (numberp kwd) (setq kwd (match-string kwd)))
  3823. (or (cdr (assoc kwd org-todo-keyword-faces))
  3824. (and (member kwd org-done-keywords) 'org-done)
  3825. 'org-todo))
  3826. (defun org-font-lock-add-tag-faces (limit)
  3827. "Add the special tag faces."
  3828. (when (and org-tag-faces org-tags-special-faces-re)
  3829. (while (re-search-forward org-tags-special-faces-re limit t)
  3830. (add-text-properties (match-beginning 1) (match-end 1)
  3831. (list 'face (org-get-tag-face 1)
  3832. 'font-lock-fontified t))
  3833. (backward-char 1))))
  3834. (defun org-get-tag-face (kwd)
  3835. "Get the right face for a TODO keyword KWD.
  3836. If KWD is a number, get the corresponding match group."
  3837. (if (numberp kwd) (setq kwd (match-string kwd)))
  3838. (or (cdr (assoc kwd org-tag-faces))
  3839. 'org-tag))
  3840. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3841. "Remove fontification and activation overlays from links."
  3842. (font-lock-default-unfontify-region beg end)
  3843. (let* ((buffer-undo-list t)
  3844. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3845. (inhibit-modification-hooks t)
  3846. deactivate-mark buffer-file-name buffer-file-truename)
  3847. (remove-text-properties beg end
  3848. '(mouse-face t keymap t org-linked-text t
  3849. invisible t intangible t))))
  3850. ;;;; Visibility cycling, including org-goto and indirect buffer
  3851. ;;; Cycling
  3852. (defvar org-cycle-global-status nil)
  3853. (make-variable-buffer-local 'org-cycle-global-status)
  3854. (defvar org-cycle-subtree-status nil)
  3855. (make-variable-buffer-local 'org-cycle-subtree-status)
  3856. ;;;###autoload
  3857. (defun org-cycle (&optional arg)
  3858. "Visibility cycling for Org-mode.
  3859. - When this function is called with a prefix argument, rotate the entire
  3860. buffer through 3 states (global cycling)
  3861. 1. OVERVIEW: Show only top-level headlines.
  3862. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3863. 3. SHOW ALL: Show everything.
  3864. When called with two C-u C-u prefixes, switch to the startup visibility,
  3865. determined by the variable `org-startup-folded', and by any VISIBILITY
  3866. properties in the buffer.
  3867. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3868. including drawers.
  3869. - When point is at the beginning of a headline, rotate the subtree started
  3870. by this line through 3 different states (local cycling)
  3871. 1. FOLDED: Only the main headline is shown.
  3872. 2. CHILDREN: The main headline and the direct children are shown.
  3873. From this state, you can move to one of the children
  3874. and zoom in further.
  3875. 3. SUBTREE: Show the entire subtree, including body text.
  3876. - When there is a numeric prefix, go up to a heading with level ARG, do
  3877. a `show-subtree' and return to the previous cursor position. If ARG
  3878. is negative, go up that many levels.
  3879. - When point is not at the beginning of a headline, execute the global
  3880. binding for TAB, which is re-indenting the line. See the option
  3881. `org-cycle-emulate-tab' for details.
  3882. - Special case: if point is at the beginning of the buffer and there is
  3883. no headline in line 1, this function will act as if called with prefix arg.
  3884. But only if also the variable `org-cycle-global-at-bob' is t."
  3885. (interactive "P")
  3886. (org-load-modules-maybe)
  3887. (let* ((outline-regexp
  3888. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3889. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3890. outline-regexp))
  3891. (bob-special (and org-cycle-global-at-bob (bobp)
  3892. (not (looking-at outline-regexp))))
  3893. (org-cycle-hook
  3894. (if bob-special
  3895. (delq 'org-optimize-window-after-visibility-change
  3896. (copy-sequence org-cycle-hook))
  3897. org-cycle-hook))
  3898. (pos (point)))
  3899. (if (or bob-special (equal arg '(4)))
  3900. ;; special case: use global cycling
  3901. (setq arg t))
  3902. (cond
  3903. ((equal arg '(16))
  3904. (org-set-startup-visibility)
  3905. (message "Startup visibility, plus VISIBILITY properties"))
  3906. ((equal arg '(64))
  3907. (show-all)
  3908. (message "Entire buffer visible, including drawers"))
  3909. ((org-at-table-p 'any)
  3910. ;; Enter the table or move to the next field in the table
  3911. (or (org-table-recognize-table.el)
  3912. (progn
  3913. (if arg (org-table-edit-field t)
  3914. (org-table-justify-field-maybe)
  3915. (call-interactively 'org-table-next-field)))))
  3916. ((eq arg t) ;; Global cycling
  3917. (cond
  3918. ((and (eq last-command this-command)
  3919. (eq org-cycle-global-status 'overview))
  3920. ;; We just created the overview - now do table of contents
  3921. ;; This can be slow in very large buffers, so indicate action
  3922. (message "CONTENTS...")
  3923. (org-content)
  3924. (message "CONTENTS...done")
  3925. (setq org-cycle-global-status 'contents)
  3926. (run-hook-with-args 'org-cycle-hook 'contents))
  3927. ((and (eq last-command this-command)
  3928. (eq org-cycle-global-status 'contents))
  3929. ;; We just showed the table of contents - now show everything
  3930. (show-all)
  3931. (message "SHOW ALL")
  3932. (setq org-cycle-global-status 'all)
  3933. (run-hook-with-args 'org-cycle-hook 'all))
  3934. (t
  3935. ;; Default action: go to overview
  3936. (org-overview)
  3937. (message "OVERVIEW")
  3938. (setq org-cycle-global-status 'overview)
  3939. (run-hook-with-args 'org-cycle-hook 'overview))))
  3940. ((and org-drawers org-drawer-regexp
  3941. (save-excursion
  3942. (beginning-of-line 1)
  3943. (looking-at org-drawer-regexp)))
  3944. ;; Toggle block visibility
  3945. (org-flag-drawer
  3946. (not (get-char-property (match-end 0) 'invisible))))
  3947. ((integerp arg)
  3948. ;; Show-subtree, ARG levels up from here.
  3949. (save-excursion
  3950. (org-back-to-heading)
  3951. (outline-up-heading (if (< arg 0) (- arg)
  3952. (- (funcall outline-level) arg)))
  3953. (org-show-subtree)))
  3954. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3955. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3956. ;; At a heading: rotate between three different views
  3957. (org-back-to-heading)
  3958. (let ((goal-column 0) eoh eol eos)
  3959. ;; First, some boundaries
  3960. (save-excursion
  3961. (org-back-to-heading)
  3962. (save-excursion
  3963. (beginning-of-line 2)
  3964. (while (and (not (eobp)) ;; this is like `next-line'
  3965. (get-char-property (1- (point)) 'invisible))
  3966. (beginning-of-line 2)) (setq eol (point)))
  3967. (outline-end-of-heading) (setq eoh (point))
  3968. (org-end-of-subtree t)
  3969. (unless (eobp)
  3970. (skip-chars-forward " \t\n")
  3971. (beginning-of-line 1) ; in case this is an item
  3972. )
  3973. (setq eos (1- (point))))
  3974. ;; Find out what to do next and set `this-command'
  3975. (cond
  3976. ((= eos eoh)
  3977. ;; Nothing is hidden behind this heading
  3978. (message "EMPTY ENTRY")
  3979. (setq org-cycle-subtree-status nil)
  3980. (save-excursion
  3981. (goto-char eos)
  3982. (outline-next-heading)
  3983. (if (org-invisible-p) (org-flag-heading nil))))
  3984. ((or (>= eol eos)
  3985. (not (string-match "\\S-" (buffer-substring eol eos))))
  3986. ;; Entire subtree is hidden in one line: open it
  3987. (org-show-entry)
  3988. (show-children)
  3989. (message "CHILDREN")
  3990. (save-excursion
  3991. (goto-char eos)
  3992. (outline-next-heading)
  3993. (if (org-invisible-p) (org-flag-heading nil)))
  3994. (setq org-cycle-subtree-status 'children)
  3995. (run-hook-with-args 'org-cycle-hook 'children))
  3996. ((and (eq last-command this-command)
  3997. (eq org-cycle-subtree-status 'children))
  3998. ;; We just showed the children, now show everything.
  3999. (org-show-subtree)
  4000. (message "SUBTREE")
  4001. (setq org-cycle-subtree-status 'subtree)
  4002. (run-hook-with-args 'org-cycle-hook 'subtree))
  4003. (t
  4004. ;; Default action: hide the subtree.
  4005. (hide-subtree)
  4006. (message "FOLDED")
  4007. (setq org-cycle-subtree-status 'folded)
  4008. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4009. ;; TAB emulation and template completion
  4010. (buffer-read-only (org-back-to-heading))
  4011. ((org-try-structure-completion))
  4012. ((org-try-cdlatex-tab))
  4013. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4014. (or (not (bolp))
  4015. (not (looking-at outline-regexp))))
  4016. (call-interactively (global-key-binding "\t")))
  4017. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4018. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4019. (or (and (eq org-cycle-emulate-tab 'white)
  4020. (= (match-end 0) (point-at-eol)))
  4021. (and (eq org-cycle-emulate-tab 'whitestart)
  4022. (>= (match-end 0) pos))))
  4023. t
  4024. (eq org-cycle-emulate-tab t))
  4025. (call-interactively (global-key-binding "\t")))
  4026. (t (save-excursion
  4027. (org-back-to-heading)
  4028. (org-cycle))))))
  4029. ;;;###autoload
  4030. (defun org-global-cycle (&optional arg)
  4031. "Cycle the global visibility. For details see `org-cycle'.
  4032. With C-u prefix arg, switch to startup visibility.
  4033. With a numeric prefix, show all headlines up to that level."
  4034. (interactive "P")
  4035. (let ((org-cycle-include-plain-lists
  4036. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4037. (cond
  4038. ((integerp arg)
  4039. (show-all)
  4040. (hide-sublevels arg)
  4041. (setq org-cycle-global-status 'contents))
  4042. ((equal arg '(4))
  4043. (org-set-startup-visibility)
  4044. (message "Startup visibility, plus VISIBILITY properties."))
  4045. (t
  4046. (org-cycle '(4))))))
  4047. (defun org-set-startup-visibility ()
  4048. "Set the visibility required by startup options and properties."
  4049. (cond
  4050. ((eq org-startup-folded t)
  4051. (org-cycle '(4)))
  4052. ((eq org-startup-folded 'content)
  4053. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4054. (org-cycle '(4)) (org-cycle '(4)))))
  4055. (org-set-visibility-according-to-property 'no-cleanup)
  4056. (org-cycle-hide-archived-subtrees 'all)
  4057. (org-cycle-hide-drawers 'all)
  4058. (org-cycle-show-empty-lines 'all))
  4059. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4060. "Switch subtree visibilities according to :VISIBILITY: property."
  4061. (interactive)
  4062. (let (org-show-entry-below state)
  4063. (save-excursion
  4064. (goto-char (point-min))
  4065. (while (re-search-forward
  4066. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4067. nil t)
  4068. (setq state (match-string 1))
  4069. (save-excursion
  4070. (org-back-to-heading t)
  4071. (hide-subtree)
  4072. (org-reveal)
  4073. (cond
  4074. ((equal state '("fold" "folded"))
  4075. (hide-subtree))
  4076. ((equal state "children")
  4077. (org-show-hidden-entry)
  4078. (show-children))
  4079. ((equal state "content")
  4080. (save-excursion
  4081. (save-restriction
  4082. (org-narrow-to-subtree)
  4083. (org-content))))
  4084. ((member state '("all" "showall"))
  4085. (show-subtree)))))
  4086. (unless no-cleanup
  4087. (org-cycle-hide-archived-subtrees 'all)
  4088. (org-cycle-hide-drawers 'all)
  4089. (org-cycle-show-empty-lines 'all)))))
  4090. (defun org-overview ()
  4091. "Switch to overview mode, showing only top-level headlines.
  4092. Really, this shows all headlines with level equal or greater than the level
  4093. of the first headline in the buffer. This is important, because if the
  4094. first headline is not level one, then (hide-sublevels 1) gives confusing
  4095. results."
  4096. (interactive)
  4097. (let ((level (save-excursion
  4098. (goto-char (point-min))
  4099. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4100. (progn
  4101. (goto-char (match-beginning 0))
  4102. (funcall outline-level))))))
  4103. (and level (hide-sublevels level))))
  4104. (defun org-content (&optional arg)
  4105. "Show all headlines in the buffer, like a table of contents.
  4106. With numerical argument N, show content up to level N."
  4107. (interactive "P")
  4108. (save-excursion
  4109. ;; Visit all headings and show their offspring
  4110. (and (integerp arg) (org-overview))
  4111. (goto-char (point-max))
  4112. (catch 'exit
  4113. (while (and (progn (condition-case nil
  4114. (outline-previous-visible-heading 1)
  4115. (error (goto-char (point-min))))
  4116. t)
  4117. (looking-at outline-regexp))
  4118. (if (integerp arg)
  4119. (show-children (1- arg))
  4120. (show-branches))
  4121. (if (bobp) (throw 'exit nil))))))
  4122. (defun org-optimize-window-after-visibility-change (state)
  4123. "Adjust the window after a change in outline visibility.
  4124. This function is the default value of the hook `org-cycle-hook'."
  4125. (when (get-buffer-window (current-buffer))
  4126. (cond
  4127. ; ((eq state 'overview) (org-first-headline-recenter 1))
  4128. ; ((eq state 'overview) (org-beginning-of-line))
  4129. ((eq state 'content) nil)
  4130. ((eq state 'all) nil)
  4131. ((eq state 'folded) nil)
  4132. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4133. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4134. (defun org-compact-display-after-subtree-move ()
  4135. "Show a compacter version of the tree of the entry's parent."
  4136. (save-excursion
  4137. (if (org-up-heading-safe)
  4138. (progn
  4139. (hide-subtree)
  4140. (show-entry)
  4141. (show-children)
  4142. (org-cycle-show-empty-lines 'children)
  4143. (org-cycle-hide-drawers 'children))
  4144. (org-overview))))
  4145. (defun org-cycle-show-empty-lines (state)
  4146. "Show empty lines above all visible headlines.
  4147. The region to be covered depends on STATE when called through
  4148. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4149. entire buffer covered. Note that an empty line is only shown if there
  4150. are at least `org-cycle-separator-lines' empty lines before the headline."
  4151. (when (> org-cycle-separator-lines 0)
  4152. (save-excursion
  4153. (let* ((n org-cycle-separator-lines)
  4154. (re (cond
  4155. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4156. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4157. (t (let ((ns (number-to-string (- n 2))))
  4158. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4159. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4160. beg end)
  4161. (cond
  4162. ((memq state '(overview contents t))
  4163. (setq beg (point-min) end (point-max)))
  4164. ((memq state '(children folded))
  4165. (setq beg (point) end (progn (org-end-of-subtree t t)
  4166. (beginning-of-line 2)
  4167. (point)))))
  4168. (when beg
  4169. (goto-char beg)
  4170. (while (re-search-forward re end t)
  4171. (if (not (get-char-property (match-end 1) 'invisible))
  4172. (outline-flag-region
  4173. (match-beginning 1) (match-end 1) nil)))))))
  4174. ;; Never hide empty lines at the end of the file.
  4175. (save-excursion
  4176. (goto-char (point-max))
  4177. (outline-previous-heading)
  4178. (outline-end-of-heading)
  4179. (if (and (looking-at "[ \t\n]+")
  4180. (= (match-end 0) (point-max)))
  4181. (outline-flag-region (point) (match-end 0) nil))))
  4182. (defun org-show-empty-lines-in-parent ()
  4183. "Move to the parent and re-show empty lines before visible headlines."
  4184. (save-excursion
  4185. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4186. (org-cycle-show-empty-lines context))))
  4187. (defun org-cycle-hide-drawers (state)
  4188. "Re-hide all drawers after a visibility state change."
  4189. (when (and (org-mode-p)
  4190. (not (memq state '(overview folded))))
  4191. (save-excursion
  4192. (let* ((globalp (memq state '(contents all)))
  4193. (beg (if globalp (point-min) (point)))
  4194. (end (if globalp (point-max) (org-end-of-subtree t))))
  4195. (goto-char beg)
  4196. (while (re-search-forward org-drawer-regexp end t)
  4197. (org-flag-drawer t))))))
  4198. (defun org-flag-drawer (flag)
  4199. (save-excursion
  4200. (beginning-of-line 1)
  4201. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4202. (let ((b (match-end 0))
  4203. (outline-regexp org-outline-regexp))
  4204. (if (re-search-forward
  4205. "^[ \t]*:END:"
  4206. (save-excursion (outline-next-heading) (point)) t)
  4207. (outline-flag-region b (point-at-eol) flag)
  4208. (error ":END: line missing"))))))
  4209. (defun org-subtree-end-visible-p ()
  4210. "Is the end of the current subtree visible?"
  4211. (pos-visible-in-window-p
  4212. (save-excursion (org-end-of-subtree t) (point))))
  4213. (defun org-first-headline-recenter (&optional N)
  4214. "Move cursor to the first headline and recenter the headline.
  4215. Optional argument N means, put the headline into the Nth line of the window."
  4216. (goto-char (point-min))
  4217. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4218. (beginning-of-line)
  4219. (recenter (prefix-numeric-value N))))
  4220. ;;; Org-goto
  4221. (defvar org-goto-window-configuration nil)
  4222. (defvar org-goto-marker nil)
  4223. (defvar org-goto-map
  4224. (let ((map (make-sparse-keymap)))
  4225. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4226. (while (setq cmd (pop cmds))
  4227. (substitute-key-definition cmd cmd map global-map)))
  4228. (suppress-keymap map)
  4229. (org-defkey map "\C-m" 'org-goto-ret)
  4230. (org-defkey map [(return)] 'org-goto-ret)
  4231. (org-defkey map [(left)] 'org-goto-left)
  4232. (org-defkey map [(right)] 'org-goto-right)
  4233. (org-defkey map [(control ?g)] 'org-goto-quit)
  4234. (org-defkey map "\C-i" 'org-cycle)
  4235. (org-defkey map [(tab)] 'org-cycle)
  4236. (org-defkey map [(down)] 'outline-next-visible-heading)
  4237. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4238. (if org-goto-auto-isearch
  4239. (if (fboundp 'define-key-after)
  4240. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4241. nil)
  4242. (org-defkey map "q" 'org-goto-quit)
  4243. (org-defkey map "n" 'outline-next-visible-heading)
  4244. (org-defkey map "p" 'outline-previous-visible-heading)
  4245. (org-defkey map "f" 'outline-forward-same-level)
  4246. (org-defkey map "b" 'outline-backward-same-level)
  4247. (org-defkey map "u" 'outline-up-heading))
  4248. (org-defkey map "/" 'org-occur)
  4249. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4250. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4251. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4252. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4253. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4254. map))
  4255. (defconst org-goto-help
  4256. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4257. RET=jump to location [Q]uit and return to previous location
  4258. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4259. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4260. ;; FIXME: Docstring doe not mention both interfaces
  4261. (defun org-goto (&optional alternative-interface)
  4262. "Look up a different location in the current file, keeping current visibility.
  4263. When you want look-up or go to a different location in a document, the
  4264. fastest way is often to fold the entire buffer and then dive into the tree.
  4265. This method has the disadvantage, that the previous location will be folded,
  4266. which may not be what you want.
  4267. This command works around this by showing a copy of the current buffer
  4268. in an indirect buffer, in overview mode. You can dive into the tree in
  4269. that copy, use org-occur and incremental search to find a location.
  4270. When pressing RET or `Q', the command returns to the original buffer in
  4271. which the visibility is still unchanged. After RET is will also jump to
  4272. the location selected in the indirect buffer and expose the
  4273. the headline hierarchy above."
  4274. (interactive "P")
  4275. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4276. (org-refile-use-outline-path t)
  4277. (interface
  4278. (if (not alternative-interface)
  4279. org-goto-interface
  4280. (if (eq org-goto-interface 'outline)
  4281. 'outline-path-completion
  4282. 'outline)))
  4283. (org-goto-start-pos (point))
  4284. (selected-point
  4285. (if (eq interface 'outline)
  4286. (car (org-get-location (current-buffer) org-goto-help))
  4287. (nth 3 (org-refile-get-location "Goto: ")))))
  4288. (if selected-point
  4289. (progn
  4290. (org-mark-ring-push org-goto-start-pos)
  4291. (goto-char selected-point)
  4292. (if (or (org-invisible-p) (org-invisible-p2))
  4293. (org-show-context 'org-goto)))
  4294. (message "Quit"))))
  4295. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4296. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4297. (defvar org-goto-local-auto-isearch-map) ; defined below
  4298. (defun org-get-location (buf help)
  4299. "Let the user select a location in the Org-mode buffer BUF.
  4300. This function uses a recursive edit. It returns the selected position
  4301. or nil."
  4302. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4303. (isearch-hide-immediately nil)
  4304. (isearch-search-fun-function
  4305. (lambda () 'org-goto-local-search-headings))
  4306. (org-goto-selected-point org-goto-exit-command))
  4307. (save-excursion
  4308. (save-window-excursion
  4309. (delete-other-windows)
  4310. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4311. (switch-to-buffer
  4312. (condition-case nil
  4313. (make-indirect-buffer (current-buffer) "*org-goto*")
  4314. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4315. (with-output-to-temp-buffer "*Help*"
  4316. (princ help))
  4317. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4318. (setq buffer-read-only nil)
  4319. (let ((org-startup-truncated t)
  4320. (org-startup-folded nil)
  4321. (org-startup-align-all-tables nil))
  4322. (org-mode)
  4323. (org-overview))
  4324. (setq buffer-read-only t)
  4325. (if (and (boundp 'org-goto-start-pos)
  4326. (integer-or-marker-p org-goto-start-pos))
  4327. (let ((org-show-hierarchy-above t)
  4328. (org-show-siblings t)
  4329. (org-show-following-heading t))
  4330. (goto-char org-goto-start-pos)
  4331. (and (org-invisible-p) (org-show-context)))
  4332. (goto-char (point-min)))
  4333. (org-beginning-of-line)
  4334. (message "Select location and press RET")
  4335. (use-local-map org-goto-map)
  4336. (recursive-edit)
  4337. ))
  4338. (kill-buffer "*org-goto*")
  4339. (cons org-goto-selected-point org-goto-exit-command)))
  4340. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4341. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4342. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4343. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4344. (defun org-goto-local-search-headings (string bound noerror)
  4345. "Search and make sure that any matches are in headlines."
  4346. (catch 'return
  4347. (while (if isearch-forward
  4348. (search-forward string bound noerror)
  4349. (search-backward string bound noerror))
  4350. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4351. (and (member :headline context)
  4352. (not (member :tags context))))
  4353. (throw 'return (point))))))
  4354. (defun org-goto-local-auto-isearch ()
  4355. "Start isearch."
  4356. (interactive)
  4357. (goto-char (point-min))
  4358. (let ((keys (this-command-keys)))
  4359. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4360. (isearch-mode t)
  4361. (isearch-process-search-char (string-to-char keys)))))
  4362. (defun org-goto-ret (&optional arg)
  4363. "Finish `org-goto' by going to the new location."
  4364. (interactive "P")
  4365. (setq org-goto-selected-point (point)
  4366. org-goto-exit-command 'return)
  4367. (throw 'exit nil))
  4368. (defun org-goto-left ()
  4369. "Finish `org-goto' by going to the new location."
  4370. (interactive)
  4371. (if (org-on-heading-p)
  4372. (progn
  4373. (beginning-of-line 1)
  4374. (setq org-goto-selected-point (point)
  4375. org-goto-exit-command 'left)
  4376. (throw 'exit nil))
  4377. (error "Not on a heading")))
  4378. (defun org-goto-right ()
  4379. "Finish `org-goto' by going to the new location."
  4380. (interactive)
  4381. (if (org-on-heading-p)
  4382. (progn
  4383. (setq org-goto-selected-point (point)
  4384. org-goto-exit-command 'right)
  4385. (throw 'exit nil))
  4386. (error "Not on a heading")))
  4387. (defun org-goto-quit ()
  4388. "Finish `org-goto' without cursor motion."
  4389. (interactive)
  4390. (setq org-goto-selected-point nil)
  4391. (setq org-goto-exit-command 'quit)
  4392. (throw 'exit nil))
  4393. ;;; Indirect buffer display of subtrees
  4394. (defvar org-indirect-dedicated-frame nil
  4395. "This is the frame being used for indirect tree display.")
  4396. (defvar org-last-indirect-buffer nil)
  4397. (defun org-tree-to-indirect-buffer (&optional arg)
  4398. "Create indirect buffer and narrow it to current subtree.
  4399. With numerical prefix ARG, go up to this level and then take that tree.
  4400. If ARG is negative, go up that many levels.
  4401. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4402. indirect buffer previously made with this command, to avoid proliferation of
  4403. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4404. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4405. is kept so that you can work with several indirect buffers at the same time.
  4406. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4407. requests that a new frame be made for the new buffer, so that the dedicated
  4408. frame is not changed."
  4409. (interactive "P")
  4410. (let ((cbuf (current-buffer))
  4411. (cwin (selected-window))
  4412. (pos (point))
  4413. beg end level heading ibuf)
  4414. (save-excursion
  4415. (org-back-to-heading t)
  4416. (when (numberp arg)
  4417. (setq level (org-outline-level))
  4418. (if (< arg 0) (setq arg (+ level arg)))
  4419. (while (> (setq level (org-outline-level)) arg)
  4420. (outline-up-heading 1 t)))
  4421. (setq beg (point)
  4422. heading (org-get-heading))
  4423. (org-end-of-subtree t) (setq end (point)))
  4424. (if (and (buffer-live-p org-last-indirect-buffer)
  4425. (not (eq org-indirect-buffer-display 'new-frame))
  4426. (not arg))
  4427. (kill-buffer org-last-indirect-buffer))
  4428. (setq ibuf (org-get-indirect-buffer cbuf)
  4429. org-last-indirect-buffer ibuf)
  4430. (cond
  4431. ((or (eq org-indirect-buffer-display 'new-frame)
  4432. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4433. (select-frame (make-frame))
  4434. (delete-other-windows)
  4435. (switch-to-buffer ibuf)
  4436. (org-set-frame-title heading))
  4437. ((eq org-indirect-buffer-display 'dedicated-frame)
  4438. (raise-frame
  4439. (select-frame (or (and org-indirect-dedicated-frame
  4440. (frame-live-p org-indirect-dedicated-frame)
  4441. org-indirect-dedicated-frame)
  4442. (setq org-indirect-dedicated-frame (make-frame)))))
  4443. (delete-other-windows)
  4444. (switch-to-buffer ibuf)
  4445. (org-set-frame-title (concat "Indirect: " heading)))
  4446. ((eq org-indirect-buffer-display 'current-window)
  4447. (switch-to-buffer ibuf))
  4448. ((eq org-indirect-buffer-display 'other-window)
  4449. (pop-to-buffer ibuf))
  4450. (t (error "Invalid value.")))
  4451. (if (featurep 'xemacs)
  4452. (save-excursion (org-mode) (turn-on-font-lock)))
  4453. (narrow-to-region beg end)
  4454. (show-all)
  4455. (goto-char pos)
  4456. (and (window-live-p cwin) (select-window cwin))))
  4457. (defun org-get-indirect-buffer (&optional buffer)
  4458. (setq buffer (or buffer (current-buffer)))
  4459. (let ((n 1) (base (buffer-name buffer)) bname)
  4460. (while (buffer-live-p
  4461. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4462. (setq n (1+ n)))
  4463. (condition-case nil
  4464. (make-indirect-buffer buffer bname 'clone)
  4465. (error (make-indirect-buffer buffer bname)))))
  4466. (defun org-set-frame-title (title)
  4467. "Set the title of the current frame to the string TITLE."
  4468. ;; FIXME: how to name a single frame in XEmacs???
  4469. (unless (featurep 'xemacs)
  4470. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4471. ;;;; Structure editing
  4472. ;;; Inserting headlines
  4473. (defun org-previous-line-empty-p ()
  4474. (save-excursion
  4475. (and (not (bobp))
  4476. (or (beginning-of-line 0) t)
  4477. (save-match-data
  4478. (looking-at "[ \t]*$")))))
  4479. (defun org-insert-heading (&optional force-heading)
  4480. "Insert a new heading or item with same depth at point.
  4481. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4482. If point is at the beginning of a headline, insert a sibling before the
  4483. current headline. If point is not at the beginning, do not split the line,
  4484. but create the new headline after the current line."
  4485. (interactive "P")
  4486. (if (= (buffer-size) 0)
  4487. (insert "\n* ")
  4488. (when (or force-heading (not (org-insert-item)))
  4489. (let* ((empty-line-p nil)
  4490. (head (save-excursion
  4491. (condition-case nil
  4492. (progn
  4493. (org-back-to-heading)
  4494. (setq empty-line-p (org-previous-line-empty-p))
  4495. (match-string 0))
  4496. (error "*"))))
  4497. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4498. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4499. pos hide-previous previous-pos)
  4500. (cond
  4501. ((and (org-on-heading-p) (bolp)
  4502. (or (bobp)
  4503. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4504. ;; insert before the current line
  4505. (open-line (if blank 2 1)))
  4506. ((and (bolp)
  4507. (or (bobp)
  4508. (save-excursion
  4509. (backward-char 1) (not (org-invisible-p)))))
  4510. ;; insert right here
  4511. nil)
  4512. (t
  4513. ;; somewhere in the line
  4514. (save-excursion
  4515. (setq previous-pos (point-at-bol))
  4516. (end-of-line)
  4517. (setq hide-previous (org-invisible-p)))
  4518. (and org-insert-heading-respect-content (org-show-subtree))
  4519. (let ((split
  4520. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4521. (save-excursion
  4522. (let ((p (point)))
  4523. (goto-char (point-at-bol))
  4524. (and (looking-at org-complex-heading-regexp)
  4525. (> p (match-beginning 4)))))))
  4526. tags pos)
  4527. (cond
  4528. (org-insert-heading-respect-content
  4529. (org-end-of-subtree nil t)
  4530. (or (bolp) (newline))
  4531. (or (org-previous-line-empty-p)
  4532. (and blank (newline)))
  4533. (open-line 1))
  4534. ((org-on-heading-p)
  4535. (when hide-previous
  4536. (show-children)
  4537. (org-show-entry))
  4538. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4539. (setq tags (and (match-end 2) (match-string 2)))
  4540. (and (match-end 1)
  4541. (delete-region (match-beginning 1) (match-end 1)))
  4542. (setq pos (point-at-bol))
  4543. (or split (end-of-line 1))
  4544. (delete-horizontal-space)
  4545. (newline (if blank 2 1))
  4546. (when tags
  4547. (save-excursion
  4548. (goto-char pos)
  4549. (end-of-line 1)
  4550. (insert " " tags)
  4551. (org-set-tags nil 'align))))
  4552. (t
  4553. (or split (end-of-line 1))
  4554. (newline (if blank 2 1)))))))
  4555. (insert head) (just-one-space)
  4556. (setq pos (point))
  4557. (end-of-line 1)
  4558. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4559. (when (and org-insert-heading-respect-content hide-previous)
  4560. (save-excursion
  4561. (goto-char previous-pos)
  4562. (hide-subtree)))
  4563. (run-hooks 'org-insert-heading-hook)))))
  4564. (defun org-get-heading (&optional no-tags)
  4565. "Return the heading of the current entry, without the stars."
  4566. (save-excursion
  4567. (org-back-to-heading t)
  4568. (if (looking-at
  4569. (if no-tags
  4570. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4571. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4572. (match-string 1) "")))
  4573. (defun org-heading-components ()
  4574. "Return the components of the current heading.
  4575. This is a list with the following elements:
  4576. - the level as an integer
  4577. - the reduced level, different if `org-odd-levels-only' is set.
  4578. - the TODO keyword, or nil
  4579. - the priority character, like ?A, or nil if no priority is given
  4580. - the headline text itself, or the tags string if no headline text
  4581. - the tags string, or nil."
  4582. (save-excursion
  4583. (org-back-to-heading t)
  4584. (if (looking-at org-complex-heading-regexp)
  4585. (list (length (match-string 1))
  4586. (org-reduced-level (length (match-string 1)))
  4587. (org-match-string-no-properties 2)
  4588. (and (match-end 3) (aref (match-string 3) 2))
  4589. (org-match-string-no-properties 4)
  4590. (org-match-string-no-properties 5)))))
  4591. (defun org-insert-heading-after-current ()
  4592. "Insert a new heading with same level as current, after current subtree."
  4593. (interactive)
  4594. (org-back-to-heading)
  4595. (org-insert-heading)
  4596. (org-move-subtree-down)
  4597. (end-of-line 1))
  4598. (defun org-insert-heading-respect-content ()
  4599. (interactive)
  4600. (let ((org-insert-heading-respect-content t))
  4601. (org-insert-heading t)))
  4602. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4603. (interactive "P")
  4604. (let ((org-insert-heading-respect-content t))
  4605. (org-insert-todo-heading force-state t)))
  4606. (defun org-insert-todo-heading (arg &optional force-heading)
  4607. "Insert a new heading with the same level and TODO state as current heading.
  4608. If the heading has no TODO state, or if the state is DONE, use the first
  4609. state (TODO by default). Also with prefix arg, force first state."
  4610. (interactive "P")
  4611. (when (or force-heading (not (org-insert-item 'checkbox)))
  4612. (org-insert-heading force-heading)
  4613. (save-excursion
  4614. (org-back-to-heading)
  4615. (outline-previous-heading)
  4616. (looking-at org-todo-line-regexp))
  4617. (let*
  4618. ((new-mark-x
  4619. (if (or arg
  4620. (not (match-beginning 2))
  4621. (member (match-string 2) org-done-keywords))
  4622. (car org-todo-keywords-1)
  4623. (match-string 2)))
  4624. (new-mark
  4625. (or
  4626. (run-hook-with-args-until-success
  4627. 'org-todo-get-default-hook new-mark-x nil)
  4628. new-mark-x)))
  4629. (insert new-mark " "))
  4630. (when org-provide-todo-statistics
  4631. (org-update-parent-todo-statistics))))
  4632. (defun org-insert-subheading (arg)
  4633. "Insert a new subheading and demote it.
  4634. Works for outline headings and for plain lists alike."
  4635. (interactive "P")
  4636. (org-insert-heading arg)
  4637. (cond
  4638. ((org-on-heading-p) (org-do-demote))
  4639. ((org-at-item-p) (org-indent-item 1))))
  4640. (defun org-insert-todo-subheading (arg)
  4641. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4642. Works for outline headings and for plain lists alike."
  4643. (interactive "P")
  4644. (org-insert-todo-heading arg)
  4645. (cond
  4646. ((org-on-heading-p) (org-do-demote))
  4647. ((org-at-item-p) (org-indent-item 1))))
  4648. ;;; Promotion and Demotion
  4649. (defun org-promote-subtree ()
  4650. "Promote the entire subtree.
  4651. See also `org-promote'."
  4652. (interactive)
  4653. (save-excursion
  4654. (org-map-tree 'org-promote))
  4655. (org-fix-position-after-promote))
  4656. (defun org-demote-subtree ()
  4657. "Demote the entire subtree. See `org-demote'.
  4658. See also `org-promote'."
  4659. (interactive)
  4660. (save-excursion
  4661. (org-map-tree 'org-demote))
  4662. (org-fix-position-after-promote))
  4663. (defun org-do-promote ()
  4664. "Promote the current heading higher up the tree.
  4665. If the region is active in `transient-mark-mode', promote all headings
  4666. in the region."
  4667. (interactive)
  4668. (save-excursion
  4669. (if (org-region-active-p)
  4670. (org-map-region 'org-promote (region-beginning) (region-end))
  4671. (org-promote)))
  4672. (org-fix-position-after-promote))
  4673. (defun org-do-demote ()
  4674. "Demote the current heading lower down the tree.
  4675. If the region is active in `transient-mark-mode', demote all headings
  4676. in the region."
  4677. (interactive)
  4678. (save-excursion
  4679. (if (org-region-active-p)
  4680. (org-map-region 'org-demote (region-beginning) (region-end))
  4681. (org-demote)))
  4682. (org-fix-position-after-promote))
  4683. (defun org-fix-position-after-promote ()
  4684. "Make sure that after pro/demotion cursor position is right."
  4685. (let ((pos (point)))
  4686. (when (save-excursion
  4687. (beginning-of-line 1)
  4688. (looking-at org-todo-line-regexp)
  4689. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4690. (cond ((eobp) (insert " "))
  4691. ((eolp) (insert " "))
  4692. ((equal (char-after) ?\ ) (forward-char 1))))))
  4693. (defun org-reduced-level (l)
  4694. "Compute the effective level of a heading.
  4695. This takes into account the setting of `org-odd-levels-only'."
  4696. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4697. (defun org-get-valid-level (level &optional change)
  4698. "Rectify a level change under the influence of `org-odd-levels-only'
  4699. LEVEL is a current level, CHANGE is by how much the level should be
  4700. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4701. even level numbers will become the next higher odd number."
  4702. (if org-odd-levels-only
  4703. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4704. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4705. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4706. (max 1 (+ level change))))
  4707. (if (boundp 'define-obsolete-function-alias)
  4708. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4709. (define-obsolete-function-alias 'org-get-legal-level
  4710. 'org-get-valid-level)
  4711. (define-obsolete-function-alias 'org-get-legal-level
  4712. 'org-get-valid-level "23.1")))
  4713. (defun org-promote ()
  4714. "Promote the current heading higher up the tree.
  4715. If the region is active in `transient-mark-mode', promote all headings
  4716. in the region."
  4717. (org-back-to-heading t)
  4718. (let* ((level (save-match-data (funcall outline-level)))
  4719. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4720. (diff (abs (- level (length up-head) -1))))
  4721. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4722. (replace-match up-head nil t)
  4723. ;; Fixup tag positioning
  4724. (and org-auto-align-tags (org-set-tags nil t))
  4725. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4726. (defun org-demote ()
  4727. "Demote the current heading lower down the tree.
  4728. If the region is active in `transient-mark-mode', demote all headings
  4729. in the region."
  4730. (org-back-to-heading t)
  4731. (let* ((level (save-match-data (funcall outline-level)))
  4732. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4733. (diff (abs (- level (length down-head) -1))))
  4734. (replace-match down-head nil t)
  4735. ;; Fixup tag positioning
  4736. (and org-auto-align-tags (org-set-tags nil t))
  4737. (if org-adapt-indentation (org-fixup-indentation diff))))
  4738. (defun org-map-tree (fun)
  4739. "Call FUN for every heading underneath the current one."
  4740. (org-back-to-heading)
  4741. (let ((level (funcall outline-level)))
  4742. (save-excursion
  4743. (funcall fun)
  4744. (while (and (progn
  4745. (outline-next-heading)
  4746. (> (funcall outline-level) level))
  4747. (not (eobp)))
  4748. (funcall fun)))))
  4749. (defun org-map-region (fun beg end)
  4750. "Call FUN for every heading between BEG and END."
  4751. (let ((org-ignore-region t))
  4752. (save-excursion
  4753. (setq end (copy-marker end))
  4754. (goto-char beg)
  4755. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4756. (< (point) end))
  4757. (funcall fun))
  4758. (while (and (progn
  4759. (outline-next-heading)
  4760. (< (point) end))
  4761. (not (eobp)))
  4762. (funcall fun)))))
  4763. (defun org-fixup-indentation (diff)
  4764. "Change the indentation in the current entry by DIFF
  4765. However, if any line in the current entry has no indentation, or if it
  4766. would end up with no indentation after the change, nothing at all is done."
  4767. (save-excursion
  4768. (let ((end (save-excursion (outline-next-heading)
  4769. (point-marker)))
  4770. (prohibit (if (> diff 0)
  4771. "^\\S-"
  4772. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4773. col)
  4774. (unless (save-excursion (end-of-line 1)
  4775. (re-search-forward prohibit end t))
  4776. (while (and (< (point) end)
  4777. (re-search-forward "^[ \t]+" end t))
  4778. (goto-char (match-end 0))
  4779. (setq col (current-column))
  4780. (if (< diff 0) (replace-match ""))
  4781. (org-indent-to-column (+ diff col))))
  4782. (move-marker end nil))))
  4783. (defun org-convert-to-odd-levels ()
  4784. "Convert an org-mode file with all levels allowed to one with odd levels.
  4785. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4786. level 5 etc."
  4787. (interactive)
  4788. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4789. (let ((org-odd-levels-only nil) n)
  4790. (save-excursion
  4791. (goto-char (point-min))
  4792. (while (re-search-forward "^\\*\\*+ " nil t)
  4793. (setq n (- (length (match-string 0)) 2))
  4794. (while (>= (setq n (1- n)) 0)
  4795. (org-demote))
  4796. (end-of-line 1))))))
  4797. (defun org-convert-to-oddeven-levels ()
  4798. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4799. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4800. section with an even level, conversion would destroy the structure of the file. An error
  4801. is signaled in this case."
  4802. (interactive)
  4803. (goto-char (point-min))
  4804. ;; First check if there are no even levels
  4805. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4806. (org-show-context t)
  4807. (error "Not all levels are odd in this file. Conversion not possible."))
  4808. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4809. (let ((org-odd-levels-only nil) n)
  4810. (save-excursion
  4811. (goto-char (point-min))
  4812. (while (re-search-forward "^\\*\\*+ " nil t)
  4813. (setq n (/ (1- (length (match-string 0))) 2))
  4814. (while (>= (setq n (1- n)) 0)
  4815. (org-promote))
  4816. (end-of-line 1))))))
  4817. (defun org-tr-level (n)
  4818. "Make N odd if required."
  4819. (if org-odd-levels-only (1+ (/ n 2)) n))
  4820. ;;; Vertical tree motion, cutting and pasting of subtrees
  4821. (defun org-move-subtree-up (&optional arg)
  4822. "Move the current subtree up past ARG headlines of the same level."
  4823. (interactive "p")
  4824. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4825. (defun org-move-subtree-down (&optional arg)
  4826. "Move the current subtree down past ARG headlines of the same level."
  4827. (interactive "p")
  4828. (setq arg (prefix-numeric-value arg))
  4829. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4830. 'outline-get-last-sibling))
  4831. (ins-point (make-marker))
  4832. (cnt (abs arg))
  4833. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4834. ;; Select the tree
  4835. (org-back-to-heading)
  4836. (setq beg0 (point))
  4837. (save-excursion
  4838. (setq ne-beg (org-back-over-empty-lines))
  4839. (setq beg (point)))
  4840. (save-match-data
  4841. (save-excursion (outline-end-of-heading)
  4842. (setq folded (org-invisible-p)))
  4843. (outline-end-of-subtree))
  4844. (outline-next-heading)
  4845. (setq ne-end (org-back-over-empty-lines))
  4846. (setq end (point))
  4847. (goto-char beg0)
  4848. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4849. ;; include less whitespace
  4850. (save-excursion
  4851. (goto-char beg)
  4852. (forward-line (- ne-beg ne-end))
  4853. (setq beg (point))))
  4854. ;; Find insertion point, with error handling
  4855. (while (> cnt 0)
  4856. (or (and (funcall movfunc) (looking-at outline-regexp))
  4857. (progn (goto-char beg0)
  4858. (error "Cannot move past superior level or buffer limit")))
  4859. (setq cnt (1- cnt)))
  4860. (if (> arg 0)
  4861. ;; Moving forward - still need to move over subtree
  4862. (progn (org-end-of-subtree t t)
  4863. (save-excursion
  4864. (org-back-over-empty-lines)
  4865. (or (bolp) (newline)))))
  4866. (setq ne-ins (org-back-over-empty-lines))
  4867. (move-marker ins-point (point))
  4868. (setq txt (buffer-substring beg end))
  4869. (org-save-markers-in-region beg end)
  4870. (delete-region beg end)
  4871. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4872. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4873. (let ((bbb (point)))
  4874. (insert-before-markers txt)
  4875. (org-reinstall-markers-in-region bbb)
  4876. (move-marker ins-point bbb))
  4877. (or (bolp) (insert "\n"))
  4878. (setq ins-end (point))
  4879. (goto-char ins-point)
  4880. (org-skip-whitespace)
  4881. (when (and (< arg 0)
  4882. (org-first-sibling-p)
  4883. (> ne-ins ne-beg))
  4884. ;; Move whitespace back to beginning
  4885. (save-excursion
  4886. (goto-char ins-end)
  4887. (let ((kill-whole-line t))
  4888. (kill-line (- ne-ins ne-beg)) (point)))
  4889. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4890. (move-marker ins-point nil)
  4891. (org-compact-display-after-subtree-move)
  4892. (org-show-empty-lines-in-parent)
  4893. (unless folded
  4894. (org-show-entry)
  4895. (show-children)
  4896. (org-cycle-hide-drawers 'children))))
  4897. (defvar org-subtree-clip ""
  4898. "Clipboard for cut and paste of subtrees.
  4899. This is actually only a copy of the kill, because we use the normal kill
  4900. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4901. (defvar org-subtree-clip-folded nil
  4902. "Was the last copied subtree folded?
  4903. This is used to fold the tree back after pasting.")
  4904. (defun org-cut-subtree (&optional n)
  4905. "Cut the current subtree into the clipboard.
  4906. With prefix arg N, cut this many sequential subtrees.
  4907. This is a short-hand for marking the subtree and then cutting it."
  4908. (interactive "p")
  4909. (org-copy-subtree n 'cut))
  4910. (defun org-copy-subtree (&optional n cut force-store-markers)
  4911. "Cut the current subtree into the clipboard.
  4912. With prefix arg N, cut this many sequential subtrees.
  4913. This is a short-hand for marking the subtree and then copying it.
  4914. If CUT is non-nil, actually cut the subtree.
  4915. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4916. of some markers in the region, even if CUT is non-nil. This is
  4917. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4918. (interactive "p")
  4919. (let (beg end folded (beg0 (point)))
  4920. (if (interactive-p)
  4921. (org-back-to-heading nil) ; take what looks like a subtree
  4922. (org-back-to-heading t)) ; take what is really there
  4923. (org-back-over-empty-lines)
  4924. (setq beg (point))
  4925. (skip-chars-forward " \t\r\n")
  4926. (save-match-data
  4927. (save-excursion (outline-end-of-heading)
  4928. (setq folded (org-invisible-p)))
  4929. (condition-case nil
  4930. (outline-forward-same-level (1- n))
  4931. (error nil))
  4932. (org-end-of-subtree t t))
  4933. (org-back-over-empty-lines)
  4934. (setq end (point))
  4935. (goto-char beg0)
  4936. (when (> end beg)
  4937. (setq org-subtree-clip-folded folded)
  4938. (when (or cut force-store-markers)
  4939. (org-save-markers-in-region beg end))
  4940. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4941. (setq org-subtree-clip (current-kill 0))
  4942. (message "%s: Subtree(s) with %d characters"
  4943. (if cut "Cut" "Copied")
  4944. (length org-subtree-clip)))))
  4945. (defun org-paste-subtree (&optional level tree for-yank)
  4946. "Paste the clipboard as a subtree, with modification of headline level.
  4947. The entire subtree is promoted or demoted in order to match a new headline
  4948. level.
  4949. If the cursor is at the beginning of a headline, the same level as
  4950. that headline is used to paste the tree
  4951. If not, the new level is derived from the *visible* headings
  4952. before and after the insertion point, and taken to be the inferior headline
  4953. level of the two. So if the previous visible heading is level 3 and the
  4954. next is level 4 (or vice versa), level 4 will be used for insertion.
  4955. This makes sure that the subtree remains an independent subtree and does
  4956. not swallow low level entries.
  4957. You can also force a different level, either by using a numeric prefix
  4958. argument, or by inserting the heading marker by hand. For example, if the
  4959. cursor is after \"*****\", then the tree will be shifted to level 5.
  4960. If optional TREE is given, use this text instead of the kill ring.
  4961. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4962. move back over whitespace before inserting, and move point to the end of
  4963. the inserted text when done."
  4964. (interactive "P")
  4965. (unless (org-kill-is-subtree-p tree)
  4966. (error "%s"
  4967. (substitute-command-keys
  4968. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4969. (let* ((visp (not (org-invisible-p)))
  4970. (txt (or tree (and kill-ring (current-kill 0))))
  4971. (^re (concat "^\\(" outline-regexp "\\)"))
  4972. (re (concat "\\(" outline-regexp "\\)"))
  4973. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4974. (old-level (if (string-match ^re txt)
  4975. (- (match-end 0) (match-beginning 0) 1)
  4976. -1))
  4977. (force-level (cond (level (prefix-numeric-value level))
  4978. ((and (looking-at "[ \t]*$")
  4979. (string-match
  4980. ^re_ (buffer-substring
  4981. (point-at-bol) (point))))
  4982. (- (match-end 1) (match-beginning 1)))
  4983. ((and (bolp)
  4984. (looking-at org-outline-regexp))
  4985. (- (match-end 0) (point) 1))
  4986. (t nil)))
  4987. (previous-level (save-excursion
  4988. (condition-case nil
  4989. (progn
  4990. (outline-previous-visible-heading 1)
  4991. (if (looking-at re)
  4992. (- (match-end 0) (match-beginning 0) 1)
  4993. 1))
  4994. (error 1))))
  4995. (next-level (save-excursion
  4996. (condition-case nil
  4997. (progn
  4998. (or (looking-at outline-regexp)
  4999. (outline-next-visible-heading 1))
  5000. (if (looking-at re)
  5001. (- (match-end 0) (match-beginning 0) 1)
  5002. 1))
  5003. (error 1))))
  5004. (new-level (or force-level (max previous-level next-level)))
  5005. (shift (if (or (= old-level -1)
  5006. (= new-level -1)
  5007. (= old-level new-level))
  5008. 0
  5009. (- new-level old-level)))
  5010. (delta (if (> shift 0) -1 1))
  5011. (func (if (> shift 0) 'org-demote 'org-promote))
  5012. (org-odd-levels-only nil)
  5013. beg end newend)
  5014. ;; Remove the forced level indicator
  5015. (if force-level
  5016. (delete-region (point-at-bol) (point)))
  5017. ;; Paste
  5018. (beginning-of-line 1)
  5019. (unless for-yank (org-back-over-empty-lines))
  5020. (setq beg (point))
  5021. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5022. (insert-before-markers txt)
  5023. (unless (string-match "\n\\'" txt) (insert "\n"))
  5024. (setq newend (point))
  5025. (org-reinstall-markers-in-region beg)
  5026. (setq end (point))
  5027. (goto-char beg)
  5028. (skip-chars-forward " \t\n\r")
  5029. (setq beg (point))
  5030. (if (and (org-invisible-p) visp)
  5031. (save-excursion (outline-show-heading)))
  5032. ;; Shift if necessary
  5033. (unless (= shift 0)
  5034. (save-restriction
  5035. (narrow-to-region beg end)
  5036. (while (not (= shift 0))
  5037. (org-map-region func (point-min) (point-max))
  5038. (setq shift (+ delta shift)))
  5039. (goto-char (point-min))
  5040. (setq newend (point-max))))
  5041. (when (or (interactive-p) for-yank)
  5042. (message "Clipboard pasted as level %d subtree" new-level))
  5043. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5044. kill-ring
  5045. (eq org-subtree-clip (current-kill 0))
  5046. org-subtree-clip-folded)
  5047. ;; The tree was folded before it was killed/copied
  5048. (hide-subtree))
  5049. (and for-yank (goto-char newend))))
  5050. (defun org-kill-is-subtree-p (&optional txt)
  5051. "Check if the current kill is an outline subtree, or a set of trees.
  5052. Returns nil if kill does not start with a headline, or if the first
  5053. headline level is not the largest headline level in the tree.
  5054. So this will actually accept several entries of equal levels as well,
  5055. which is OK for `org-paste-subtree'.
  5056. If optional TXT is given, check this string instead of the current kill."
  5057. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5058. (start-level (and kill
  5059. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5060. org-outline-regexp "\\)")
  5061. kill)
  5062. (- (match-end 2) (match-beginning 2) 1)))
  5063. (re (concat "^" org-outline-regexp))
  5064. (start (1+ (or (match-beginning 2) -1))))
  5065. (if (not start-level)
  5066. (progn
  5067. nil) ;; does not even start with a heading
  5068. (catch 'exit
  5069. (while (setq start (string-match re kill (1+ start)))
  5070. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5071. (throw 'exit nil)))
  5072. t))))
  5073. (defvar org-markers-to-move nil
  5074. "Markers that should be moved with a cut-and-paste operation.
  5075. Those markers are stored together with their positions relative to
  5076. the start of the region.")
  5077. (defun org-save-markers-in-region (beg end)
  5078. "Check markers in region.
  5079. If these markers are between BEG and END, record their position relative
  5080. to BEG, so that after moving the block of text, we can put the markers back
  5081. into place.
  5082. This function gets called just before an entry or tree gets cut from the
  5083. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5084. called immediately, to move the markers with the entries."
  5085. (setq org-markers-to-move nil)
  5086. (when (featurep 'org-clock)
  5087. (org-clock-save-markers-for-cut-and-paste beg end))
  5088. (when (featurep 'org-agenda)
  5089. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5090. (defun org-check-and-save-marker (marker beg end)
  5091. "Check if MARKER is between BEG and END.
  5092. If yes, remember the marker and the distance to BEG."
  5093. (when (and (marker-buffer marker)
  5094. (equal (marker-buffer marker) (current-buffer)))
  5095. (if (and (>= marker beg) (< marker end))
  5096. (push (cons marker (- marker beg)) org-markers-to-move))))
  5097. (defun org-reinstall-markers-in-region (beg)
  5098. "Move all remembered markers to their position relative to BEG."
  5099. (mapc (lambda (x)
  5100. (move-marker (car x) (+ beg (cdr x))))
  5101. org-markers-to-move)
  5102. (setq org-markers-to-move nil))
  5103. (defun org-narrow-to-subtree ()
  5104. "Narrow buffer to the current subtree."
  5105. (interactive)
  5106. (save-excursion
  5107. (save-match-data
  5108. (narrow-to-region
  5109. (progn (org-back-to-heading) (point))
  5110. (progn (org-end-of-subtree t) (point))))))
  5111. ;;; Outline Sorting
  5112. (defun org-sort (with-case)
  5113. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5114. Optional argument WITH-CASE means sort case-sensitively."
  5115. (interactive "P")
  5116. (if (org-at-table-p)
  5117. (org-call-with-arg 'org-table-sort-lines with-case)
  5118. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5119. (defun org-sort-remove-invisible (s)
  5120. (remove-text-properties 0 (length s) org-rm-props s)
  5121. (while (string-match org-bracket-link-regexp s)
  5122. (setq s (replace-match (if (match-end 2)
  5123. (match-string 3 s)
  5124. (match-string 1 s)) t t s)))
  5125. s)
  5126. (defvar org-priority-regexp) ; defined later in the file
  5127. (defun org-sort-entries-or-items
  5128. (&optional with-case sorting-type getkey-func compare-func property)
  5129. "Sort entries on a certain level of an outline tree.
  5130. If there is an active region, the entries in the region are sorted.
  5131. Else, if the cursor is before the first entry, sort the top-level items.
  5132. Else, the children of the entry at point are sorted.
  5133. Sorting can be alphabetically, numerically, and by date/time as given by
  5134. the first time stamp in the entry. The command prompts for the sorting
  5135. type unless it has been given to the function through the SORTING-TYPE
  5136. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5137. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5138. called with point at the beginning of the record. It must return either
  5139. a string or a number that should serve as the sorting key for that record.
  5140. Comparing entries ignores case by default. However, with an optional argument
  5141. WITH-CASE, the sorting considers case as well."
  5142. (interactive "P")
  5143. (let ((case-func (if with-case 'identity 'downcase))
  5144. start beg end stars re re2
  5145. txt what tmp plain-list-p)
  5146. ;; Find beginning and end of region to sort
  5147. (cond
  5148. ((org-region-active-p)
  5149. ;; we will sort the region
  5150. (setq end (region-end)
  5151. what "region")
  5152. (goto-char (region-beginning))
  5153. (if (not (org-on-heading-p)) (outline-next-heading))
  5154. (setq start (point)))
  5155. ((org-at-item-p)
  5156. ;; we will sort this plain list
  5157. (org-beginning-of-item-list) (setq start (point))
  5158. (org-end-of-item-list) (setq end (point))
  5159. (goto-char start)
  5160. (setq plain-list-p t
  5161. what "plain list"))
  5162. ((or (org-on-heading-p)
  5163. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5164. ;; we will sort the children of the current headline
  5165. (org-back-to-heading)
  5166. (setq start (point)
  5167. end (progn (org-end-of-subtree t t)
  5168. (org-back-over-empty-lines)
  5169. (point))
  5170. what "children")
  5171. (goto-char start)
  5172. (show-subtree)
  5173. (outline-next-heading))
  5174. (t
  5175. ;; we will sort the top-level entries in this file
  5176. (goto-char (point-min))
  5177. (or (org-on-heading-p) (outline-next-heading))
  5178. (setq start (point) end (point-max) what "top-level")
  5179. (goto-char start)
  5180. (show-all)))
  5181. (setq beg (point))
  5182. (if (>= beg end) (error "Nothing to sort"))
  5183. (unless plain-list-p
  5184. (looking-at "\\(\\*+\\)")
  5185. (setq stars (match-string 1)
  5186. re (concat "^" (regexp-quote stars) " +")
  5187. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5188. txt (buffer-substring beg end))
  5189. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5190. (if (and (not (equal stars "*")) (string-match re2 txt))
  5191. (error "Region to sort contains a level above the first entry")))
  5192. (unless sorting-type
  5193. (message
  5194. (if plain-list-p
  5195. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5196. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5197. what)
  5198. (setq sorting-type (read-char-exclusive))
  5199. (and (= (downcase sorting-type) ?f)
  5200. (setq getkey-func
  5201. (org-ido-completing-read "Sort using function: "
  5202. obarray 'fboundp t nil nil))
  5203. (setq getkey-func (intern getkey-func)))
  5204. (and (= (downcase sorting-type) ?r)
  5205. (setq property
  5206. (org-ido-completing-read "Property: "
  5207. (mapcar 'list (org-buffer-property-keys t))
  5208. nil t))))
  5209. (message "Sorting entries...")
  5210. (save-restriction
  5211. (narrow-to-region start end)
  5212. (let ((dcst (downcase sorting-type))
  5213. (now (current-time)))
  5214. (sort-subr
  5215. (/= dcst sorting-type)
  5216. ;; This function moves to the beginning character of the "record" to
  5217. ;; be sorted.
  5218. (if plain-list-p
  5219. (lambda nil
  5220. (if (org-at-item-p) t (goto-char (point-max))))
  5221. (lambda nil
  5222. (if (re-search-forward re nil t)
  5223. (goto-char (match-beginning 0))
  5224. (goto-char (point-max)))))
  5225. ;; This function moves to the last character of the "record" being
  5226. ;; sorted.
  5227. (if plain-list-p
  5228. 'org-end-of-item
  5229. (lambda nil
  5230. (save-match-data
  5231. (condition-case nil
  5232. (outline-forward-same-level 1)
  5233. (error
  5234. (goto-char (point-max)))))))
  5235. ;; This function returns the value that gets sorted against.
  5236. (if plain-list-p
  5237. (lambda nil
  5238. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5239. (cond
  5240. ((= dcst ?n)
  5241. (string-to-number (buffer-substring (match-end 0)
  5242. (point-at-eol))))
  5243. ((= dcst ?a)
  5244. (buffer-substring (match-end 0) (point-at-eol)))
  5245. ((= dcst ?t)
  5246. (if (re-search-forward org-ts-regexp
  5247. (point-at-eol) t)
  5248. (org-time-string-to-time (match-string 0))
  5249. now))
  5250. ((= dcst ?f)
  5251. (if getkey-func
  5252. (progn
  5253. (setq tmp (funcall getkey-func))
  5254. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5255. tmp)
  5256. (error "Invalid key function `%s'" getkey-func)))
  5257. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5258. (lambda nil
  5259. (cond
  5260. ((= dcst ?n)
  5261. (if (looking-at org-complex-heading-regexp)
  5262. (string-to-number (match-string 4))
  5263. nil))
  5264. ((= dcst ?a)
  5265. (if (looking-at org-complex-heading-regexp)
  5266. (funcall case-func (match-string 4))
  5267. nil))
  5268. ((= dcst ?t)
  5269. (if (re-search-forward org-ts-regexp
  5270. (save-excursion
  5271. (forward-line 2)
  5272. (point)) t)
  5273. (org-time-string-to-time (match-string 0))
  5274. now))
  5275. ((= dcst ?p)
  5276. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5277. (string-to-char (match-string 2))
  5278. org-default-priority))
  5279. ((= dcst ?r)
  5280. (or (org-entry-get nil property) ""))
  5281. ((= dcst ?o)
  5282. (if (looking-at org-complex-heading-regexp)
  5283. (- 9999 (length (member (match-string 2)
  5284. org-todo-keywords-1)))))
  5285. ((= dcst ?f)
  5286. (if getkey-func
  5287. (progn
  5288. (setq tmp (funcall getkey-func))
  5289. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5290. tmp)
  5291. (error "Invalid key function `%s'" getkey-func)))
  5292. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5293. nil
  5294. (cond
  5295. ((= dcst ?a) 'string<)
  5296. ((= dcst ?t) 'time-less-p)
  5297. ((= dcst ?f) compare-func)
  5298. (t nil)))))
  5299. (message "Sorting entries...done")))
  5300. (defun org-do-sort (table what &optional with-case sorting-type)
  5301. "Sort TABLE of WHAT according to SORTING-TYPE.
  5302. The user will be prompted for the SORTING-TYPE if the call to this
  5303. function does not specify it. WHAT is only for the prompt, to indicate
  5304. what is being sorted. The sorting key will be extracted from
  5305. the car of the elements of the table.
  5306. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5307. (unless sorting-type
  5308. (message
  5309. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5310. what)
  5311. (setq sorting-type (read-char-exclusive)))
  5312. (let ((dcst (downcase sorting-type))
  5313. extractfun comparefun)
  5314. ;; Define the appropriate functions
  5315. (cond
  5316. ((= dcst ?n)
  5317. (setq extractfun 'string-to-number
  5318. comparefun (if (= dcst sorting-type) '< '>)))
  5319. ((= dcst ?a)
  5320. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5321. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5322. comparefun (if (= dcst sorting-type)
  5323. 'string<
  5324. (lambda (a b) (and (not (string< a b))
  5325. (not (string= a b)))))))
  5326. ((= dcst ?t)
  5327. (setq extractfun
  5328. (lambda (x)
  5329. (if (or (string-match org-ts-regexp x)
  5330. (string-match org-ts-regexp-both x))
  5331. (time-to-seconds
  5332. (org-time-string-to-time (match-string 0 x)))
  5333. 0))
  5334. comparefun (if (= dcst sorting-type) '< '>)))
  5335. (t (error "Invalid sorting type `%c'" sorting-type)))
  5336. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5337. table)
  5338. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5339. ;;; Editing source examples
  5340. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5341. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5342. (defvar org-edit-src-force-single-line nil)
  5343. (defvar org-edit-src-from-org-mode nil)
  5344. (defvar org-edit-src-picture nil)
  5345. (define-minor-mode org-exit-edit-mode
  5346. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5347. (defun org-edit-src-code ()
  5348. "Edit the source code example at point.
  5349. An indirect buffer is created, and that buffer is then narrowed to the
  5350. example at point and switched to the correct language mode. When done,
  5351. exit by killing the buffer with \\[org-edit-src-exit]."
  5352. (interactive)
  5353. (let ((line (org-current-line))
  5354. (case-fold-search t)
  5355. (msg (substitute-command-keys
  5356. "Edit, then exit with C-c ' (C-c and single quote)"))
  5357. (info (org-edit-src-find-region-and-lang))
  5358. (org-mode-p (eq major-mode 'org-mode))
  5359. beg end lang lang-f single lfmt)
  5360. (if (not info)
  5361. nil
  5362. (setq beg (nth 0 info)
  5363. end (nth 1 info)
  5364. lang (nth 2 info)
  5365. single (nth 3 info)
  5366. lfmt (nth 4 info)
  5367. lang-f (intern (concat lang "-mode")))
  5368. (unless (functionp lang-f)
  5369. (error "No such language mode: %s" lang-f))
  5370. (goto-line line)
  5371. (if (get-buffer "*Org Edit Src Example*")
  5372. (kill-buffer "*Org Edit Src Example*"))
  5373. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5374. "*Org Edit Src Example*"))
  5375. (narrow-to-region beg end)
  5376. (remove-text-properties beg end '(display nil invisible nil
  5377. intangible nil))
  5378. (let ((org-inhibit-startup t))
  5379. (funcall lang-f))
  5380. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5381. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5382. (when lfmt
  5383. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5384. (when org-mode-p
  5385. (goto-char (point-min))
  5386. (while (re-search-forward "^," nil t)
  5387. (replace-match "")))
  5388. (goto-line line)
  5389. (org-exit-edit-mode)
  5390. (org-set-local 'header-line-format msg)
  5391. (message "%s" msg)
  5392. t)))
  5393. (defun org-edit-fixed-width-region ()
  5394. "Edit the fixed-width ascii drawing at point.
  5395. This must be a region where each line starts with a colon followed by
  5396. a space character.
  5397. An indirect buffer is created, and that buffer is then narrowed to the
  5398. example at point and switched to artist-mode. When done,
  5399. exit by killing the buffer with \\[org-edit-src-exit]."
  5400. (interactive)
  5401. (let ((line (org-current-line))
  5402. (case-fold-search t)
  5403. (msg (substitute-command-keys
  5404. "Edit, then exit with C-c ' (C-c and single quote)"))
  5405. (org-mode-p (eq major-mode 'org-mode))
  5406. beg end)
  5407. (beginning-of-line 1)
  5408. (if (looking-at "[ \t]*[^:\n \t]")
  5409. nil
  5410. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5411. (setq beg (point) end beg)
  5412. (save-excursion
  5413. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5414. (setq beg (point-at-bol 2))
  5415. (setq beg (point))))
  5416. (save-excursion
  5417. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5418. (setq end (1- (match-beginning 0)))
  5419. (setq end (point))))
  5420. (goto-line line))
  5421. (if (get-buffer "*Org Edit Picture*")
  5422. (kill-buffer "*Org Edit Picture*"))
  5423. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5424. "*Org Edit Picture*"))
  5425. (narrow-to-region beg end)
  5426. (remove-text-properties beg end '(display nil invisible nil
  5427. intangible nil))
  5428. (when (fboundp 'font-lock-unfontify-region)
  5429. (font-lock-unfontify-region (point-min) (point-max)))
  5430. (cond
  5431. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5432. (fundamental-mode)
  5433. (artist-mode 1))
  5434. (t (funcall org-edit-fixed-width-region-mode)))
  5435. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5436. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5437. (set (make-local-variable 'org-edit-src-picture) t)
  5438. (goto-char (point-min))
  5439. (while (re-search-forward "^[ \t]*: ?" nil t)
  5440. (replace-match ""))
  5441. (goto-line line)
  5442. (org-exit-edit-mode)
  5443. (org-set-local 'header-line-format msg)
  5444. (message "%s" msg)
  5445. t)))
  5446. (defun org-edit-src-find-region-and-lang ()
  5447. "Find the region and language for a local edit.
  5448. Return a list with beginning and end of the region, a string representing
  5449. the language, a switch telling of the content should be in a single line."
  5450. (let ((re-list
  5451. (append
  5452. org-edit-src-region-extra
  5453. '(
  5454. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5455. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5456. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5457. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5458. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5459. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5460. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5461. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5462. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5463. ("^#\\+html:" "\n" "html" single-line)
  5464. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5465. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5466. ("^#\\+latex:" "\n" "latex" single-line)
  5467. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5468. ("^#\\+ascii:" "\n" "ascii" single-line)
  5469. )))
  5470. (pos (point))
  5471. re1 re2 single beg end lang lfmt match-re1)
  5472. (catch 'exit
  5473. (while (setq entry (pop re-list))
  5474. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5475. single (nth 3 entry))
  5476. (save-excursion
  5477. (if (or (looking-at re1)
  5478. (re-search-backward re1 nil t))
  5479. (progn
  5480. (setq match-re1 (match-string 0))
  5481. (setq beg (match-end 0)
  5482. lang (org-edit-src-get-lang lang)
  5483. lfmt (org-edit-src-get-label-format match-re1))
  5484. (if (and (re-search-forward re2 nil t)
  5485. (>= (match-end 0) pos))
  5486. (throw 'exit (list beg (match-beginning 0)
  5487. lang single lfmt))))
  5488. (if (or (looking-at re2)
  5489. (re-search-forward re2 nil t))
  5490. (progn
  5491. (setq end (match-beginning 0))
  5492. (if (and (re-search-backward re1 nil t)
  5493. (<= (match-beginning 0) pos))
  5494. (progn
  5495. (setq lfmt (org-edit-src-get-label-format
  5496. (match-string 0)))
  5497. (throw 'exit
  5498. (list (match-end 0) end
  5499. (org-edit-src-get-lang lang)
  5500. single lfmt))))))))))))
  5501. (defun org-edit-src-get-lang (lang)
  5502. "Extract the src language."
  5503. (let ((m (match-string 0)))
  5504. (cond
  5505. ((stringp lang) lang)
  5506. ((integerp lang) (match-string lang))
  5507. ((and (eq lang 'lang)
  5508. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5509. (match-string 1 m))
  5510. ((and (eq lang 'style)
  5511. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5512. (match-string 1 m))
  5513. (t "fundamental"))))
  5514. (defun org-edit-src-get-label-format (s)
  5515. "Extract the label format."
  5516. (save-match-data
  5517. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5518. (match-string 1 s))))
  5519. (defun org-edit-src-exit ()
  5520. "Exit special edit and protect problematic lines."
  5521. (interactive)
  5522. (unless (buffer-base-buffer (current-buffer))
  5523. (error "This is not an indirect buffer, something is wrong..."))
  5524. (unless (> (point-min) 1)
  5525. (error "This buffer is not narrowed, something is wrong..."))
  5526. (goto-char (point-min))
  5527. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5528. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5529. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5530. (goto-char (point-min))
  5531. (while (re-search-forward "\n" nil t)
  5532. (replace-match " "))
  5533. (goto-char (point-min))
  5534. (if (looking-at "\\s-*") (replace-match " "))
  5535. (if (re-search-forward "\\s-+\\'" nil t)
  5536. (replace-match "")))
  5537. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5538. (goto-char (point-min))
  5539. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5540. (replace-match ",\\1"))
  5541. (when font-lock-mode
  5542. (font-lock-unfontify-region (point-min) (point-max)))
  5543. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5544. (when (org-bound-and-true-p org-edit-src-picture)
  5545. (untabify (point-min) (point-max))
  5546. (goto-char (point-min))
  5547. (while (re-search-forward "^" nil t)
  5548. (replace-match ": "))
  5549. (when font-lock-mode
  5550. (font-lock-unfontify-region (point-min) (point-max)))
  5551. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5552. (kill-buffer (current-buffer))
  5553. (and (org-mode-p) (org-restart-font-lock)))
  5554. ;;; The orgstruct minor mode
  5555. ;; Define a minor mode which can be used in other modes in order to
  5556. ;; integrate the org-mode structure editing commands.
  5557. ;; This is really a hack, because the org-mode structure commands use
  5558. ;; keys which normally belong to the major mode. Here is how it
  5559. ;; works: The minor mode defines all the keys necessary to operate the
  5560. ;; structure commands, but wraps the commands into a function which
  5561. ;; tests if the cursor is currently at a headline or a plain list
  5562. ;; item. If that is the case, the structure command is used,
  5563. ;; temporarily setting many Org-mode variables like regular
  5564. ;; expressions for filling etc. However, when any of those keys is
  5565. ;; used at a different location, function uses `key-binding' to look
  5566. ;; up if the key has an associated command in another currently active
  5567. ;; keymap (minor modes, major mode, global), and executes that
  5568. ;; command. There might be problems if any of the keys is otherwise
  5569. ;; used as a prefix key.
  5570. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5571. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5572. ;; addresses this by checking explicitly for both bindings.
  5573. (defvar orgstruct-mode-map (make-sparse-keymap)
  5574. "Keymap for the minor `orgstruct-mode'.")
  5575. (defvar org-local-vars nil
  5576. "List of local variables, for use by `orgstruct-mode'")
  5577. ;;;###autoload
  5578. (define-minor-mode orgstruct-mode
  5579. "Toggle the minor more `orgstruct-mode'.
  5580. This mode is for using Org-mode structure commands in other modes.
  5581. The following key behave as if Org-mode was active, if the cursor
  5582. is on a headline, or on a plain list item (both in the definition
  5583. of Org-mode).
  5584. M-up Move entry/item up
  5585. M-down Move entry/item down
  5586. M-left Promote
  5587. M-right Demote
  5588. M-S-up Move entry/item up
  5589. M-S-down Move entry/item down
  5590. M-S-left Promote subtree
  5591. M-S-right Demote subtree
  5592. M-q Fill paragraph and items like in Org-mode
  5593. C-c ^ Sort entries
  5594. C-c - Cycle list bullet
  5595. TAB Cycle item visibility
  5596. M-RET Insert new heading/item
  5597. S-M-RET Insert new TODO heading / Checkbox item
  5598. C-c C-c Set tags / toggle checkbox"
  5599. nil " OrgStruct" nil
  5600. (org-load-modules-maybe)
  5601. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5602. ;;;###autoload
  5603. (defun turn-on-orgstruct ()
  5604. "Unconditionally turn on `orgstruct-mode'."
  5605. (orgstruct-mode 1))
  5606. ;;;###autoload
  5607. (defun turn-on-orgstruct++ ()
  5608. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5609. In addition to setting orgstruct-mode, this also exports all indentation and
  5610. autofilling variables from org-mode into the buffer. Note that turning
  5611. off orgstruct-mode will *not* remove these additional settings."
  5612. (orgstruct-mode 1)
  5613. (let (var val)
  5614. (mapc
  5615. (lambda (x)
  5616. (when (string-match
  5617. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5618. (symbol-name (car x)))
  5619. (setq var (car x) val (nth 1 x))
  5620. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5621. org-local-vars)))
  5622. (defun orgstruct-error ()
  5623. "Error when there is no default binding for a structure key."
  5624. (interactive)
  5625. (error "This key has no function outside structure elements"))
  5626. (defun orgstruct-setup ()
  5627. "Setup orgstruct keymaps."
  5628. (let ((nfunc 0)
  5629. (bindings
  5630. (list
  5631. '([(meta up)] org-metaup)
  5632. '([(meta down)] org-metadown)
  5633. '([(meta left)] org-metaleft)
  5634. '([(meta right)] org-metaright)
  5635. '([(meta shift up)] org-shiftmetaup)
  5636. '([(meta shift down)] org-shiftmetadown)
  5637. '([(meta shift left)] org-shiftmetaleft)
  5638. '([(meta shift right)] org-shiftmetaright)
  5639. '([(shift up)] org-shiftup)
  5640. '([(shift down)] org-shiftdown)
  5641. '([(shift left)] org-shiftleft)
  5642. '([(shift right)] org-shiftright)
  5643. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5644. '("\M-q" fill-paragraph)
  5645. '("\C-c^" org-sort)
  5646. '("\C-c-" org-cycle-list-bullet)))
  5647. elt key fun cmd)
  5648. (while (setq elt (pop bindings))
  5649. (setq nfunc (1+ nfunc))
  5650. (setq key (org-key (car elt))
  5651. fun (nth 1 elt)
  5652. cmd (orgstruct-make-binding fun nfunc key))
  5653. (org-defkey orgstruct-mode-map key cmd))
  5654. ;; Special treatment needed for TAB and RET
  5655. (org-defkey orgstruct-mode-map [(tab)]
  5656. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5657. (org-defkey orgstruct-mode-map "\C-i"
  5658. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5659. (org-defkey orgstruct-mode-map "\M-\C-m"
  5660. (orgstruct-make-binding 'org-insert-heading 105
  5661. "\M-\C-m" [(meta return)]))
  5662. (org-defkey orgstruct-mode-map [(meta return)]
  5663. (orgstruct-make-binding 'org-insert-heading 106
  5664. [(meta return)] "\M-\C-m"))
  5665. (org-defkey orgstruct-mode-map [(shift meta return)]
  5666. (orgstruct-make-binding 'org-insert-todo-heading 107
  5667. [(meta return)] "\M-\C-m"))
  5668. (unless org-local-vars
  5669. (setq org-local-vars (org-get-local-variables)))
  5670. t))
  5671. (defun orgstruct-make-binding (fun n &rest keys)
  5672. "Create a function for binding in the structure minor mode.
  5673. FUN is the command to call inside a table. N is used to create a unique
  5674. command name. KEYS are keys that should be checked in for a command
  5675. to execute outside of tables."
  5676. (eval
  5677. (list 'defun
  5678. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5679. '(arg)
  5680. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5681. "Outside of structure, run the binding of `"
  5682. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5683. "'.")
  5684. '(interactive "p")
  5685. (list 'if
  5686. '(org-context-p 'headline 'item)
  5687. (list 'org-run-like-in-org-mode (list 'quote fun))
  5688. (list 'let '(orgstruct-mode)
  5689. (list 'call-interactively
  5690. (append '(or)
  5691. (mapcar (lambda (k)
  5692. (list 'key-binding k))
  5693. keys)
  5694. '('orgstruct-error))))))))
  5695. (defun org-context-p (&rest contexts)
  5696. "Check if local context is any of CONTEXTS.
  5697. Possible values in the list of contexts are `table', `headline', and `item'."
  5698. (let ((pos (point)))
  5699. (goto-char (point-at-bol))
  5700. (prog1 (or (and (memq 'table contexts)
  5701. (looking-at "[ \t]*|"))
  5702. (and (memq 'headline contexts)
  5703. ;;????????? (looking-at "\\*+"))
  5704. (looking-at outline-regexp))
  5705. (and (memq 'item contexts)
  5706. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5707. (goto-char pos))))
  5708. (defun org-get-local-variables ()
  5709. "Return a list of all local variables in an org-mode buffer."
  5710. (let (varlist)
  5711. (with-current-buffer (get-buffer-create "*Org tmp*")
  5712. (erase-buffer)
  5713. (org-mode)
  5714. (setq varlist (buffer-local-variables)))
  5715. (kill-buffer "*Org tmp*")
  5716. (delq nil
  5717. (mapcar
  5718. (lambda (x)
  5719. (setq x
  5720. (if (symbolp x)
  5721. (list x)
  5722. (list (car x) (list 'quote (cdr x)))))
  5723. (if (string-match
  5724. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5725. (symbol-name (car x)))
  5726. x nil))
  5727. varlist))))
  5728. ;;;###autoload
  5729. (defun org-run-like-in-org-mode (cmd)
  5730. (org-load-modules-maybe)
  5731. (unless org-local-vars
  5732. (setq org-local-vars (org-get-local-variables)))
  5733. (eval (list 'let org-local-vars
  5734. (list 'call-interactively (list 'quote cmd)))))
  5735. ;;;; Archiving
  5736. (defun org-get-category (&optional pos)
  5737. "Get the category applying to position POS."
  5738. (get-text-property (or pos (point)) 'org-category))
  5739. (defun org-refresh-category-properties ()
  5740. "Refresh category text properties in the buffer."
  5741. (let ((def-cat (cond
  5742. ((null org-category)
  5743. (if buffer-file-name
  5744. (file-name-sans-extension
  5745. (file-name-nondirectory buffer-file-name))
  5746. "???"))
  5747. ((symbolp org-category) (symbol-name org-category))
  5748. (t org-category)))
  5749. beg end cat pos optionp)
  5750. (org-unmodified
  5751. (save-excursion
  5752. (save-restriction
  5753. (widen)
  5754. (goto-char (point-min))
  5755. (put-text-property (point) (point-max) 'org-category def-cat)
  5756. (while (re-search-forward
  5757. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5758. (setq pos (match-end 0)
  5759. optionp (equal (char-after (match-beginning 0)) ?#)
  5760. cat (org-trim (match-string 2)))
  5761. (if optionp
  5762. (setq beg (point-at-bol) end (point-max))
  5763. (org-back-to-heading t)
  5764. (setq beg (point) end (org-end-of-subtree t t)))
  5765. (put-text-property beg end 'org-category cat)
  5766. (goto-char pos)))))))
  5767. ;;;; Link Stuff
  5768. ;;; Link abbreviations
  5769. (defun org-link-expand-abbrev (link)
  5770. "Apply replacements as defined in `org-link-abbrev-alist."
  5771. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5772. (let* ((key (match-string 1 link))
  5773. (as (or (assoc key org-link-abbrev-alist-local)
  5774. (assoc key org-link-abbrev-alist)))
  5775. (tag (and (match-end 2) (match-string 3 link)))
  5776. rpl)
  5777. (if (not as)
  5778. link
  5779. (setq rpl (cdr as))
  5780. (cond
  5781. ((symbolp rpl) (funcall rpl tag))
  5782. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5783. ((string-match "%h" rpl)
  5784. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5785. (t (concat rpl tag)))))
  5786. link))
  5787. ;;; Storing and inserting links
  5788. (defvar org-insert-link-history nil
  5789. "Minibuffer history for links inserted with `org-insert-link'.")
  5790. (defvar org-stored-links nil
  5791. "Contains the links stored with `org-store-link'.")
  5792. (defvar org-store-link-plist nil
  5793. "Plist with info about the most recently link created with `org-store-link'.")
  5794. (defvar org-link-protocols nil
  5795. "Link protocols added to Org-mode using `org-add-link-type'.")
  5796. (defvar org-store-link-functions nil
  5797. "List of functions that are called to create and store a link.
  5798. Each function will be called in turn until one returns a non-nil
  5799. value. Each function should check if it is responsible for creating
  5800. this link (for example by looking at the major mode).
  5801. If not, it must exit and return nil.
  5802. If yes, it should return a non-nil value after a calling
  5803. `org-store-link-props' with a list of properties and values.
  5804. Special properties are:
  5805. :type The link prefix. like \"http\". This must be given.
  5806. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5807. This is obligatory as well.
  5808. :description Optional default description for the second pair
  5809. of brackets in an Org-mode link. The user can still change
  5810. this when inserting this link into an Org-mode buffer.
  5811. In addition to these, any additional properties can be specified
  5812. and then used in remember templates.")
  5813. (defun org-add-link-type (type &optional follow export)
  5814. "Add TYPE to the list of `org-link-types'.
  5815. Re-compute all regular expressions depending on `org-link-types'
  5816. FOLLOW and EXPORT are two functions.
  5817. FOLLOW should take the link path as the single argument and do whatever
  5818. is necessary to follow the link, for example find a file or display
  5819. a mail message.
  5820. EXPORT should format the link path for export to one of the export formats.
  5821. It should be a function accepting three arguments:
  5822. path the path of the link, the text after the prefix (like \"http:\")
  5823. desc the description of the link, if any, nil if there was no description
  5824. format the export format, a symbol like `html' or `latex'.
  5825. The function may use the FORMAT information to return different values
  5826. depending on the format. The return value will be put literally into
  5827. the exported file.
  5828. Org-mode has a built-in default for exporting links. If you are happy with
  5829. this default, there is no need to define an export function for the link
  5830. type. For a simple example of an export function, see `org-bbdb.el'."
  5831. (add-to-list 'org-link-types type t)
  5832. (org-make-link-regexps)
  5833. (if (assoc type org-link-protocols)
  5834. (setcdr (assoc type org-link-protocols) (list follow export))
  5835. (push (list type follow export) org-link-protocols)))
  5836. ;;;###autoload
  5837. (defun org-store-link (arg)
  5838. "\\<org-mode-map>Store an org-link to the current location.
  5839. This link is added to `org-stored-links' and can later be inserted
  5840. into an org-buffer with \\[org-insert-link].
  5841. For some link types, a prefix arg is interpreted:
  5842. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5843. For file links, arg negates `org-context-in-file-links'."
  5844. (interactive "P")
  5845. (org-load-modules-maybe)
  5846. (setq org-store-link-plist nil) ; reset
  5847. (let (link cpltxt desc description search txt)
  5848. (cond
  5849. ((run-hook-with-args-until-success 'org-store-link-functions)
  5850. (setq link (plist-get org-store-link-plist :link)
  5851. desc (or (plist-get org-store-link-plist :description) link)))
  5852. ((equal (buffer-name) "*Org Edit Src Example*")
  5853. (let (label gc)
  5854. (while (or (not label)
  5855. (save-excursion
  5856. (save-restriction
  5857. (widen)
  5858. (goto-char (point-min))
  5859. (re-search-forward
  5860. (regexp-quote (format org-coderef-label-format label))
  5861. nil t))))
  5862. (when label (message "Label exists already") (sit-for 2))
  5863. (setq label (read-string "Code line label: " label)))
  5864. (end-of-line 1)
  5865. (setq link (format org-coderef-label-format label))
  5866. (setq gc (- 79 (length link)))
  5867. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5868. (insert link)
  5869. (setq link (concat "(" label ")") desc nil)))
  5870. ((eq major-mode 'calendar-mode)
  5871. (let ((cd (calendar-cursor-to-date)))
  5872. (setq link
  5873. (format-time-string
  5874. (car org-time-stamp-formats)
  5875. (apply 'encode-time
  5876. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5877. nil nil nil))))
  5878. (org-store-link-props :type "calendar" :date cd)))
  5879. ((eq major-mode 'w3-mode)
  5880. (setq cpltxt (url-view-url t)
  5881. link (org-make-link cpltxt))
  5882. (org-store-link-props :type "w3" :url (url-view-url t)))
  5883. ((eq major-mode 'w3m-mode)
  5884. (setq cpltxt (or w3m-current-title w3m-current-url)
  5885. link (org-make-link w3m-current-url))
  5886. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5887. ((setq search (run-hook-with-args-until-success
  5888. 'org-create-file-search-functions))
  5889. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5890. "::" search))
  5891. (setq cpltxt (or description link)))
  5892. ((eq major-mode 'image-mode)
  5893. (setq cpltxt (concat "file:"
  5894. (abbreviate-file-name buffer-file-name))
  5895. link (org-make-link cpltxt))
  5896. (org-store-link-props :type "image" :file buffer-file-name))
  5897. ((eq major-mode 'dired-mode)
  5898. ;; link to the file in the current line
  5899. (setq cpltxt (concat "file:"
  5900. (abbreviate-file-name
  5901. (expand-file-name
  5902. (dired-get-filename nil t))))
  5903. link (org-make-link cpltxt)))
  5904. ((and buffer-file-name (org-mode-p))
  5905. (cond
  5906. ((org-in-regexp "<<\\(.*?\\)>>")
  5907. (setq cpltxt
  5908. (concat "file:"
  5909. (abbreviate-file-name buffer-file-name)
  5910. "::" (match-string 1))
  5911. link (org-make-link cpltxt)))
  5912. ((and (featurep 'org-id)
  5913. (or (eq org-link-to-org-use-id t)
  5914. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5915. (interactive-p))
  5916. (and org-link-to-org-use-id
  5917. (condition-case nil
  5918. (org-entry-get nil "ID")
  5919. (error nil)))))
  5920. ;; We can make a link using the ID.
  5921. (setq link (condition-case nil
  5922. (prog1 (org-id-store-link)
  5923. (setq desc (plist-get org-store-link-plist
  5924. :description)))
  5925. (error
  5926. ;; probably before first headline, link to file only
  5927. (concat "file:"
  5928. (abbreviate-file-name buffer-file-name))))))
  5929. (t
  5930. ;; Just link to current headline
  5931. (setq cpltxt (concat "file:"
  5932. (abbreviate-file-name buffer-file-name)))
  5933. ;; Add a context search string
  5934. (when (org-xor org-context-in-file-links arg)
  5935. (setq txt (cond
  5936. ((org-on-heading-p) nil)
  5937. ((org-region-active-p)
  5938. (buffer-substring (region-beginning) (region-end)))
  5939. (t nil)))
  5940. (when (or (null txt) (string-match "\\S-" txt))
  5941. (setq cpltxt
  5942. (concat cpltxt "::"
  5943. (condition-case nil
  5944. (org-make-org-heading-search-string txt)
  5945. (error "")))
  5946. desc "NONE")))
  5947. (if (string-match "::\\'" cpltxt)
  5948. (setq cpltxt (substring cpltxt 0 -2)))
  5949. (setq link (org-make-link cpltxt)))))
  5950. ((buffer-file-name (buffer-base-buffer))
  5951. ;; Just link to this file here.
  5952. (setq cpltxt (concat "file:"
  5953. (abbreviate-file-name
  5954. (buffer-file-name (buffer-base-buffer)))))
  5955. ;; Add a context string
  5956. (when (org-xor org-context-in-file-links arg)
  5957. (setq txt (if (org-region-active-p)
  5958. (buffer-substring (region-beginning) (region-end))
  5959. (buffer-substring (point-at-bol) (point-at-eol))))
  5960. ;; Only use search option if there is some text.
  5961. (when (string-match "\\S-" txt)
  5962. (setq cpltxt
  5963. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5964. desc "NONE")))
  5965. (setq link (org-make-link cpltxt)))
  5966. ((interactive-p)
  5967. (error "Cannot link to a buffer which is not visiting a file"))
  5968. (t (setq link nil)))
  5969. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5970. (setq link (or link cpltxt)
  5971. desc (or desc cpltxt))
  5972. (if (equal desc "NONE") (setq desc nil))
  5973. (if (and (interactive-p) link)
  5974. (progn
  5975. (setq org-stored-links
  5976. (cons (list link desc) org-stored-links))
  5977. (message "Stored: %s" (or desc link)))
  5978. (and link (org-make-link-string link desc)))))
  5979. (defun org-store-link-props (&rest plist)
  5980. "Store link properties, extract names and addresses."
  5981. (let (x adr)
  5982. (when (setq x (plist-get plist :from))
  5983. (setq adr (mail-extract-address-components x))
  5984. (setq plist (plist-put plist :fromname (car adr)))
  5985. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5986. (when (setq x (plist-get plist :to))
  5987. (setq adr (mail-extract-address-components x))
  5988. (setq plist (plist-put plist :toname (car adr)))
  5989. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5990. (let ((from (plist-get plist :from))
  5991. (to (plist-get plist :to)))
  5992. (when (and from to org-from-is-user-regexp)
  5993. (setq plist
  5994. (plist-put plist :fromto
  5995. (if (string-match org-from-is-user-regexp from)
  5996. (concat "to %t")
  5997. (concat "from %f"))))))
  5998. (setq org-store-link-plist plist))
  5999. (defun org-add-link-props (&rest plist)
  6000. "Add these properties to the link property list."
  6001. (let (key value)
  6002. (while plist
  6003. (setq key (pop plist) value (pop plist))
  6004. (setq org-store-link-plist
  6005. (plist-put org-store-link-plist key value)))))
  6006. (defun org-email-link-description (&optional fmt)
  6007. "Return the description part of an email link.
  6008. This takes information from `org-store-link-plist' and formats it
  6009. according to FMT (default from `org-email-link-description-format')."
  6010. (setq fmt (or fmt org-email-link-description-format))
  6011. (let* ((p org-store-link-plist)
  6012. (to (plist-get p :toaddress))
  6013. (from (plist-get p :fromaddress))
  6014. (table
  6015. (list
  6016. (cons "%c" (plist-get p :fromto))
  6017. (cons "%F" (plist-get p :from))
  6018. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6019. (cons "%T" (plist-get p :to))
  6020. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6021. (cons "%s" (plist-get p :subject))
  6022. (cons "%m" (plist-get p :message-id)))))
  6023. (when (string-match "%c" fmt)
  6024. ;; Check if the user wrote this message
  6025. (if (and org-from-is-user-regexp from to
  6026. (save-match-data (string-match org-from-is-user-regexp from)))
  6027. (setq fmt (replace-match "to %t" t t fmt))
  6028. (setq fmt (replace-match "from %f" t t fmt))))
  6029. (org-replace-escapes fmt table)))
  6030. (defun org-make-org-heading-search-string (&optional string heading)
  6031. "Make search string for STRING or current headline."
  6032. (interactive)
  6033. (let ((s (or string (org-get-heading))))
  6034. (unless (and string (not heading))
  6035. ;; We are using a headline, clean up garbage in there.
  6036. (if (string-match org-todo-regexp s)
  6037. (setq s (replace-match "" t t s)))
  6038. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6039. (setq s (replace-match "" t t s)))
  6040. (setq s (org-trim s))
  6041. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6042. org-comment-string "\\)") s)
  6043. (setq s (replace-match "" t t s)))
  6044. (while (string-match org-ts-regexp s)
  6045. (setq s (replace-match "" t t s))))
  6046. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6047. (setq s (replace-match " " t t s)))
  6048. (or string (setq s (concat "*" s))) ; Add * for headlines
  6049. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6050. (defun org-make-link (&rest strings)
  6051. "Concatenate STRINGS."
  6052. (apply 'concat strings))
  6053. (defun org-make-link-string (link &optional description)
  6054. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6055. (unless (string-match "\\S-" link)
  6056. (error "Empty link"))
  6057. (when (stringp description)
  6058. ;; Remove brackets from the description, they are fatal.
  6059. (while (string-match "\\[" description)
  6060. (setq description (replace-match "{" t t description)))
  6061. (while (string-match "\\]" description)
  6062. (setq description (replace-match "}" t t description))))
  6063. (when (equal (org-link-escape link) description)
  6064. ;; No description needed, it is identical
  6065. (setq description nil))
  6066. (when (and (not description)
  6067. (not (equal link (org-link-escape link))))
  6068. (setq description (org-extract-attributes link)))
  6069. (concat "[[" (org-link-escape link) "]"
  6070. (if description (concat "[" description "]") "")
  6071. "]"))
  6072. (defconst org-link-escape-chars
  6073. '((?\ . "%20")
  6074. (?\[ . "%5B")
  6075. (?\] . "%5D")
  6076. (?\340 . "%E0") ; `a
  6077. (?\342 . "%E2") ; ^a
  6078. (?\347 . "%E7") ; ,c
  6079. (?\350 . "%E8") ; `e
  6080. (?\351 . "%E9") ; 'e
  6081. (?\352 . "%EA") ; ^e
  6082. (?\356 . "%EE") ; ^i
  6083. (?\364 . "%F4") ; ^o
  6084. (?\371 . "%F9") ; `u
  6085. (?\373 . "%FB") ; ^u
  6086. (?\; . "%3B")
  6087. (?? . "%3F")
  6088. (?= . "%3D")
  6089. (?+ . "%2B")
  6090. )
  6091. "Association list of escapes for some characters problematic in links.
  6092. This is the list that is used for internal purposes.")
  6093. (defconst org-link-escape-chars-browser
  6094. '((?\ . "%20")) ; 32 for the SPC char
  6095. "Association list of escapes for some characters problematic in links.
  6096. This is the list that is used before handing over to the browser.")
  6097. (defun org-link-escape (text &optional table)
  6098. "Escape characters in TEXT that are problematic for links."
  6099. (setq table (or table org-link-escape-chars))
  6100. (when text
  6101. (let ((re (mapconcat (lambda (x) (regexp-quote
  6102. (char-to-string (car x))))
  6103. table "\\|")))
  6104. (while (string-match re text)
  6105. (setq text
  6106. (replace-match
  6107. (cdr (assoc (string-to-char (match-string 0 text))
  6108. table))
  6109. t t text)))
  6110. text)))
  6111. (defun org-link-unescape (text &optional table)
  6112. "Reverse the action of `org-link-escape'."
  6113. (setq table (or table org-link-escape-chars))
  6114. (when text
  6115. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6116. table "\\|")))
  6117. (while (string-match re text)
  6118. (setq text
  6119. (replace-match
  6120. (char-to-string (car (rassoc (match-string 0 text) table)))
  6121. t t text)))
  6122. text)))
  6123. (defun org-xor (a b)
  6124. "Exclusive or."
  6125. (if a (not b) b))
  6126. (defun org-fixup-message-id-for-http (s)
  6127. "Replace special characters in a message id, so it can be used in an http query."
  6128. (while (string-match "<" s)
  6129. (setq s (replace-match "%3C" t t s)))
  6130. (while (string-match ">" s)
  6131. (setq s (replace-match "%3E" t t s)))
  6132. (while (string-match "@" s)
  6133. (setq s (replace-match "%40" t t s)))
  6134. s)
  6135. ;;;###autoload
  6136. (defun org-insert-link-global ()
  6137. "Insert a link like Org-mode does.
  6138. This command can be called in any mode to insert a link in Org-mode syntax."
  6139. (interactive)
  6140. (org-load-modules-maybe)
  6141. (org-run-like-in-org-mode 'org-insert-link))
  6142. (defun org-insert-link (&optional complete-file link-location)
  6143. "Insert a link. At the prompt, enter the link.
  6144. Completion can be used to insert any of the link protocol prefixes like
  6145. http or ftp in use.
  6146. The history can be used to select a link previously stored with
  6147. `org-store-link'. When the empty string is entered (i.e. if you just
  6148. press RET at the prompt), the link defaults to the most recently
  6149. stored link. As SPC triggers completion in the minibuffer, you need to
  6150. use M-SPC or C-q SPC to force the insertion of a space character.
  6151. You will also be prompted for a description, and if one is given, it will
  6152. be displayed in the buffer instead of the link.
  6153. If there is already a link at point, this command will allow you to edit link
  6154. and description parts.
  6155. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6156. be selected using completion. The path to the file will be relative to the
  6157. current directory if the file is in the current directory or a subdirectory.
  6158. Otherwise, the link will be the absolute path as completed in the minibuffer
  6159. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6160. option `org-link-file-path-type'.
  6161. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6162. the current directory or below.
  6163. With three \\[universal-argument] prefixes, negate the meaning of
  6164. `org-keep-stored-link-after-insertion'.
  6165. If `org-make-link-description-function' is non-nil, this function will be
  6166. called with the link target, and the result will be the default
  6167. link description.
  6168. If the LINK-LOCATION parameter is non-nil, this value will be
  6169. used as the link location instead of reading one interactively."
  6170. (interactive "P")
  6171. (let* ((wcf (current-window-configuration))
  6172. (region (if (org-region-active-p)
  6173. (buffer-substring (region-beginning) (region-end))))
  6174. (remove (and region (list (region-beginning) (region-end))))
  6175. (desc region)
  6176. tmphist ; byte-compile incorrectly complains about this
  6177. (link link-location)
  6178. entry file)
  6179. (cond
  6180. (link-location) ; specified by arg, just use it.
  6181. ((org-in-regexp org-bracket-link-regexp 1)
  6182. ;; We do have a link at point, and we are going to edit it.
  6183. (setq remove (list (match-beginning 0) (match-end 0)))
  6184. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6185. (setq link (read-string "Link: "
  6186. (org-link-unescape
  6187. (org-match-string-no-properties 1)))))
  6188. ((or (org-in-regexp org-angle-link-re)
  6189. (org-in-regexp org-plain-link-re))
  6190. ;; Convert to bracket link
  6191. (setq remove (list (match-beginning 0) (match-end 0))
  6192. link (read-string "Link: "
  6193. (org-remove-angle-brackets (match-string 0)))))
  6194. ((member complete-file '((4) (16)))
  6195. ;; Completing read for file names.
  6196. (setq file (read-file-name "File: "))
  6197. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6198. (pwd1 (file-name-as-directory (abbreviate-file-name
  6199. (expand-file-name ".")))))
  6200. (cond
  6201. ((equal complete-file '(16))
  6202. (setq link (org-make-link
  6203. "file:"
  6204. (abbreviate-file-name (expand-file-name file)))))
  6205. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6206. (setq link (org-make-link "file:" (match-string 1 file))))
  6207. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6208. (expand-file-name file))
  6209. (setq link (org-make-link
  6210. "file:" (match-string 1 (expand-file-name file)))))
  6211. (t (setq link (org-make-link "file:" file))))))
  6212. (t
  6213. ;; Read link, with completion for stored links.
  6214. (with-output-to-temp-buffer "*Org Links*"
  6215. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6216. (when org-stored-links
  6217. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6218. (princ (mapconcat
  6219. (lambda (x)
  6220. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6221. (reverse org-stored-links) "\n"))))
  6222. (let ((cw (selected-window)))
  6223. (select-window (get-buffer-window "*Org Links*"))
  6224. (setq truncate-lines t)
  6225. (org-fit-window-to-buffer)
  6226. (select-window cw))
  6227. ;; Fake a link history, containing the stored links.
  6228. (setq tmphist (append (mapcar 'car org-stored-links)
  6229. org-insert-link-history))
  6230. (unwind-protect
  6231. (setq link
  6232. (let ((org-completion-use-ido nil))
  6233. (org-completing-read
  6234. "Link: "
  6235. (append
  6236. (mapcar (lambda (x) (list (concat (car x) ":")))
  6237. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6238. (mapcar (lambda (x) (list (concat x ":")))
  6239. org-link-types))
  6240. nil nil nil
  6241. 'tmphist
  6242. (or (car (car org-stored-links))))))
  6243. (set-window-configuration wcf)
  6244. (kill-buffer "*Org Links*"))
  6245. (setq entry (assoc link org-stored-links))
  6246. (or entry (push link org-insert-link-history))
  6247. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6248. (not org-keep-stored-link-after-insertion))
  6249. (setq org-stored-links (delq (assoc link org-stored-links)
  6250. org-stored-links)))
  6251. (setq desc (or desc (nth 1 entry)))))
  6252. (if (string-match org-plain-link-re link)
  6253. ;; URL-like link, normalize the use of angular brackets.
  6254. (setq link (org-make-link (org-remove-angle-brackets link))))
  6255. ;; Check if we are linking to the current file with a search option
  6256. ;; If yes, simplify the link by using only the search option.
  6257. (when (and buffer-file-name
  6258. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6259. (let* ((path (match-string 1 link))
  6260. (case-fold-search nil)
  6261. (search (match-string 2 link)))
  6262. (save-match-data
  6263. (if (equal (file-truename buffer-file-name) (file-truename path))
  6264. ;; We are linking to this same file, with a search option
  6265. (setq link search)))))
  6266. ;; Check if we can/should use a relative path. If yes, simplify the link
  6267. (when (string-match "^file:\\(.*\\)" link)
  6268. (let* ((path (match-string 1 link))
  6269. (origpath path)
  6270. (case-fold-search nil))
  6271. (cond
  6272. ((or (eq org-link-file-path-type 'absolute)
  6273. (equal complete-file '(16)))
  6274. (setq path (abbreviate-file-name (expand-file-name path))))
  6275. ((eq org-link-file-path-type 'noabbrev)
  6276. (setq path (expand-file-name path)))
  6277. ((eq org-link-file-path-type 'relative)
  6278. (setq path (file-relative-name path)))
  6279. (t
  6280. (save-match-data
  6281. (if (string-match (concat "^" (regexp-quote
  6282. (file-name-as-directory
  6283. (expand-file-name "."))))
  6284. (expand-file-name path))
  6285. ;; We are linking a file with relative path name.
  6286. (setq path (substring (expand-file-name path)
  6287. (match-end 0)))
  6288. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6289. (setq link (concat "file:" path))
  6290. (if (equal desc origpath)
  6291. (setq desc path))))
  6292. (if org-make-link-description-function
  6293. (setq desc (funcall org-make-link-description-function link desc)))
  6294. (setq desc (read-string "Description: " desc))
  6295. (unless (string-match "\\S-" desc) (setq desc nil))
  6296. (if remove (apply 'delete-region remove))
  6297. (insert (org-make-link-string link desc))))
  6298. (defun org-completing-read (&rest args)
  6299. "Completing-read with SPACE being a normal character."
  6300. (let ((minibuffer-local-completion-map
  6301. (copy-keymap minibuffer-local-completion-map)))
  6302. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6303. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6304. (apply 'org-ido-completing-read args)))
  6305. (defun org-ido-completing-read (&rest args)
  6306. "Completing-read using `ido-mode' speedups if available"
  6307. (if (and org-completion-use-ido
  6308. (fboundp 'ido-completing-read)
  6309. (boundp 'ido-mode) ido-mode
  6310. (listp (second args)))
  6311. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6312. (apply 'completing-read args)))
  6313. (defun org-extract-attributes (s)
  6314. "Extract the attributes cookie from a string and set as text property."
  6315. (let (a attr (start 0) key value)
  6316. (save-match-data
  6317. (when (string-match "{{\\([^}]+\\)}}$" s)
  6318. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6319. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6320. (setq key (match-string 1 a) value (match-string 2 a)
  6321. start (match-end 0)
  6322. attr (plist-put attr (intern key) value))))
  6323. (org-add-props s nil 'org-attr attr))
  6324. s))
  6325. (defun org-attributes-to-string (plist)
  6326. "Format a property list into an HTML attribute list."
  6327. (let ((s "") key value)
  6328. (while plist
  6329. (setq key (pop plist) value (pop plist))
  6330. (and value
  6331. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6332. s))
  6333. ;;; Opening/following a link
  6334. (defvar org-link-search-failed nil)
  6335. (defun org-next-link ()
  6336. "Move forward to the next link.
  6337. If the link is in hidden text, expose it."
  6338. (interactive)
  6339. (when (and org-link-search-failed (eq this-command last-command))
  6340. (goto-char (point-min))
  6341. (message "Link search wrapped back to beginning of buffer"))
  6342. (setq org-link-search-failed nil)
  6343. (let* ((pos (point))
  6344. (ct (org-context))
  6345. (a (assoc :link ct)))
  6346. (if a (goto-char (nth 2 a)))
  6347. (if (re-search-forward org-any-link-re nil t)
  6348. (progn
  6349. (goto-char (match-beginning 0))
  6350. (if (org-invisible-p) (org-show-context)))
  6351. (goto-char pos)
  6352. (setq org-link-search-failed t)
  6353. (error "No further link found"))))
  6354. (defun org-previous-link ()
  6355. "Move backward to the previous link.
  6356. If the link is in hidden text, expose it."
  6357. (interactive)
  6358. (when (and org-link-search-failed (eq this-command last-command))
  6359. (goto-char (point-max))
  6360. (message "Link search wrapped back to end of buffer"))
  6361. (setq org-link-search-failed nil)
  6362. (let* ((pos (point))
  6363. (ct (org-context))
  6364. (a (assoc :link ct)))
  6365. (if a (goto-char (nth 1 a)))
  6366. (if (re-search-backward org-any-link-re nil t)
  6367. (progn
  6368. (goto-char (match-beginning 0))
  6369. (if (org-invisible-p) (org-show-context)))
  6370. (goto-char pos)
  6371. (setq org-link-search-failed t)
  6372. (error "No further link found"))))
  6373. (defun org-translate-link (s)
  6374. "Translate a link string if a translation function has been defined."
  6375. (if (and org-link-translation-function
  6376. (fboundp org-link-translation-function)
  6377. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6378. (progn
  6379. (setq s (funcall org-link-translation-function
  6380. (match-string 1) (match-string 2)))
  6381. (concat (car s) ":" (cdr s)))
  6382. s))
  6383. (defun org-translate-link-from-planner (type path)
  6384. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6385. This is still an experimental function, your mileage may vary."
  6386. (cond
  6387. ((member type '("http" "https" "news" "ftp"))
  6388. ;; standard Internet links are the same.
  6389. nil)
  6390. ((and (equal type "irc") (string-match "^//" path))
  6391. ;; Planner has two / at the beginning of an irc link, we have 1.
  6392. ;; We should have zero, actually....
  6393. (setq path (substring path 1)))
  6394. ((and (equal type "lisp") (string-match "^/" path))
  6395. ;; Planner has a slash, we do not.
  6396. (setq type "elisp" path (substring path 1)))
  6397. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6398. ;; A typical message link. Planner has the id after the fina slash,
  6399. ;; we separate it with a hash mark
  6400. (setq path (concat (match-string 1 path) "#"
  6401. (org-remove-angle-brackets (match-string 2 path)))))
  6402. )
  6403. (cons type path))
  6404. (defun org-find-file-at-mouse (ev)
  6405. "Open file link or URL at mouse."
  6406. (interactive "e")
  6407. (mouse-set-point ev)
  6408. (org-open-at-point 'in-emacs))
  6409. (defun org-open-at-mouse (ev)
  6410. "Open file link or URL at mouse."
  6411. (interactive "e")
  6412. (mouse-set-point ev)
  6413. (if (eq major-mode 'org-agenda-mode)
  6414. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6415. (org-open-at-point))
  6416. (defvar org-window-config-before-follow-link nil
  6417. "The window configuration before following a link.
  6418. This is saved in case the need arises to restore it.")
  6419. (defvar org-open-link-marker (make-marker)
  6420. "Marker pointing to the location where `org-open-at-point; was called.")
  6421. ;;;###autoload
  6422. (defun org-open-at-point-global ()
  6423. "Follow a link like Org-mode does.
  6424. This command can be called in any mode to follow a link that has
  6425. Org-mode syntax."
  6426. (interactive)
  6427. (org-run-like-in-org-mode 'org-open-at-point))
  6428. ;;;###autoload
  6429. (defun org-open-link-from-string (s &optional arg)
  6430. "Open a link in the string S, as if it was in Org-mode."
  6431. (interactive "sLink: \nP")
  6432. (with-temp-buffer
  6433. (let ((org-inhibit-startup t))
  6434. (org-mode)
  6435. (insert s)
  6436. (goto-char (point-min))
  6437. (org-open-at-point arg))))
  6438. (defun org-open-at-point (&optional in-emacs)
  6439. "Open link at or after point.
  6440. If there is no link at point, this function will search forward up to
  6441. the end of the current subtree.
  6442. Normally, files will be opened by an appropriate application. If the
  6443. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6444. With a double prefix argument, try to open outside of Emacs, in the
  6445. application the system uses for this file type."
  6446. (interactive "P")
  6447. (org-load-modules-maybe)
  6448. (move-marker org-open-link-marker (point))
  6449. (setq org-window-config-before-follow-link (current-window-configuration))
  6450. (org-remove-occur-highlights nil nil t)
  6451. (cond
  6452. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6453. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6454. (org-footnote-action))
  6455. (t
  6456. (let (type path link line search (pos (point)))
  6457. (catch 'match
  6458. (save-excursion
  6459. (skip-chars-forward "^]\n\r")
  6460. (when (org-in-regexp org-bracket-link-regexp)
  6461. (setq link (org-extract-attributes
  6462. (org-link-unescape (org-match-string-no-properties 1))))
  6463. (while (string-match " *\n *" link)
  6464. (setq link (replace-match " " t t link)))
  6465. (setq link (org-link-expand-abbrev link))
  6466. (cond
  6467. ((or (file-name-absolute-p link)
  6468. (string-match "^\\.\\.?/" link))
  6469. (setq type "file" path link))
  6470. ((string-match org-link-re-with-space3 link)
  6471. (setq type (match-string 1 link) path (match-string 2 link)))
  6472. (t (setq type "thisfile" path link)))
  6473. (throw 'match t)))
  6474. (when (get-text-property (point) 'org-linked-text)
  6475. (setq type "thisfile"
  6476. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6477. (1+ (point)) (point))
  6478. path (buffer-substring
  6479. (previous-single-property-change pos 'org-linked-text)
  6480. (next-single-property-change pos 'org-linked-text)))
  6481. (throw 'match t))
  6482. (save-excursion
  6483. (when (or (org-in-regexp org-angle-link-re)
  6484. (org-in-regexp org-plain-link-re))
  6485. (setq type (match-string 1) path (match-string 2))
  6486. (throw 'match t)))
  6487. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6488. (setq type "tree-match"
  6489. path (match-string 1))
  6490. (throw 'match t))
  6491. (save-excursion
  6492. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6493. (setq type "tags"
  6494. path (match-string 1))
  6495. (while (string-match ":" path)
  6496. (setq path (replace-match "+" t t path)))
  6497. (throw 'match t))))
  6498. (unless path
  6499. (error "No link found"))
  6500. ;; Remove any trailing spaces in path
  6501. (if (string-match " +\\'" path)
  6502. (setq path (replace-match "" t t path)))
  6503. (if (and org-link-translation-function
  6504. (fboundp org-link-translation-function))
  6505. ;; Check if we need to translate the link
  6506. (let ((tmp (funcall org-link-translation-function type path)))
  6507. (setq type (car tmp) path (cdr tmp))))
  6508. (cond
  6509. ((assoc type org-link-protocols)
  6510. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6511. ((equal type "mailto")
  6512. (let ((cmd (car org-link-mailto-program))
  6513. (args (cdr org-link-mailto-program)) args1
  6514. (address path) (subject "") a)
  6515. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6516. (setq address (match-string 1 path)
  6517. subject (org-link-escape (match-string 2 path))))
  6518. (while args
  6519. (cond
  6520. ((not (stringp (car args))) (push (pop args) args1))
  6521. (t (setq a (pop args))
  6522. (if (string-match "%a" a)
  6523. (setq a (replace-match address t t a)))
  6524. (if (string-match "%s" a)
  6525. (setq a (replace-match subject t t a)))
  6526. (push a args1))))
  6527. (apply cmd (nreverse args1))))
  6528. ((member type '("http" "https" "ftp" "news"))
  6529. (browse-url (concat type ":" (org-link-escape
  6530. path org-link-escape-chars-browser))))
  6531. ((member type '("message"))
  6532. (browse-url (concat type ":" path)))
  6533. ((string= type "tags")
  6534. (org-tags-view in-emacs path))
  6535. ((string= type "thisfile")
  6536. (if in-emacs
  6537. (switch-to-buffer-other-window
  6538. (org-get-buffer-for-internal-link (current-buffer)))
  6539. (org-mark-ring-push))
  6540. (let ((cmd `(org-link-search
  6541. ,path
  6542. ,(cond ((equal in-emacs '(4)) 'occur)
  6543. ((equal in-emacs '(16)) 'org-occur)
  6544. (t nil))
  6545. ,pos)))
  6546. (condition-case nil (eval cmd)
  6547. (error (progn (widen) (eval cmd))))))
  6548. ((string= type "tree-match")
  6549. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6550. ((string= type "file")
  6551. (if (string-match "::\\([0-9]+\\)\\'" path)
  6552. (setq line (string-to-number (match-string 1 path))
  6553. path (substring path 0 (match-beginning 0)))
  6554. (if (string-match "::\\(.+\\)\\'" path)
  6555. (setq search (match-string 1 path)
  6556. path (substring path 0 (match-beginning 0)))))
  6557. (if (string-match "[*?{]" (file-name-nondirectory path))
  6558. (dired path)
  6559. (org-open-file path in-emacs line search)))
  6560. ((string= type "news")
  6561. (require 'org-gnus)
  6562. (org-gnus-follow-link path))
  6563. ((string= type "shell")
  6564. (let ((cmd path))
  6565. (if (or (not org-confirm-shell-link-function)
  6566. (funcall org-confirm-shell-link-function
  6567. (format "Execute \"%s\" in shell? "
  6568. (org-add-props cmd nil
  6569. 'face 'org-warning))))
  6570. (progn
  6571. (message "Executing %s" cmd)
  6572. (shell-command cmd))
  6573. (error "Abort"))))
  6574. ((string= type "elisp")
  6575. (let ((cmd path))
  6576. (if (or (not org-confirm-elisp-link-function)
  6577. (funcall org-confirm-elisp-link-function
  6578. (format "Execute \"%s\" as elisp? "
  6579. (org-add-props cmd nil
  6580. 'face 'org-warning))))
  6581. (message "%s => %s" cmd
  6582. (if (equal (string-to-char cmd) ?\()
  6583. (eval (read cmd))
  6584. (call-interactively (read cmd))))
  6585. (error "Abort"))))
  6586. (t
  6587. (browse-url-at-point))))))
  6588. (move-marker org-open-link-marker nil)
  6589. (run-hook-with-args 'org-follow-link-hook))
  6590. ;;;; Time estimates
  6591. (defun org-get-effort (&optional pom)
  6592. "Get the effort estimate for the current entry."
  6593. (org-entry-get pom org-effort-property))
  6594. ;;; File search
  6595. (defvar org-create-file-search-functions nil
  6596. "List of functions to construct the right search string for a file link.
  6597. These functions are called in turn with point at the location to
  6598. which the link should point.
  6599. A function in the hook should first test if it would like to
  6600. handle this file type, for example by checking the major-mode or
  6601. the file extension. If it decides not to handle this file, it
  6602. should just return nil to give other functions a chance. If it
  6603. does handle the file, it must return the search string to be used
  6604. when following the link. The search string will be part of the
  6605. file link, given after a double colon, and `org-open-at-point'
  6606. will automatically search for it. If special measures must be
  6607. taken to make the search successful, another function should be
  6608. added to the companion hook `org-execute-file-search-functions',
  6609. which see.
  6610. A function in this hook may also use `setq' to set the variable
  6611. `description' to provide a suggestion for the descriptive text to
  6612. be used for this link when it gets inserted into an Org-mode
  6613. buffer with \\[org-insert-link].")
  6614. (defvar org-execute-file-search-functions nil
  6615. "List of functions to execute a file search triggered by a link.
  6616. Functions added to this hook must accept a single argument, the
  6617. search string that was part of the file link, the part after the
  6618. double colon. The function must first check if it would like to
  6619. handle this search, for example by checking the major-mode or the
  6620. file extension. If it decides not to handle this search, it
  6621. should just return nil to give other functions a chance. If it
  6622. does handle the search, it must return a non-nil value to keep
  6623. other functions from trying.
  6624. Each function can access the current prefix argument through the
  6625. variable `current-prefix-argument'. Note that a single prefix is
  6626. used to force opening a link in Emacs, so it may be good to only
  6627. use a numeric or double prefix to guide the search function.
  6628. In case this is needed, a function in this hook can also restore
  6629. the window configuration before `org-open-at-point' was called using:
  6630. (set-window-configuration org-window-config-before-follow-link)")
  6631. (defun org-link-search (s &optional type avoid-pos)
  6632. "Search for a link search option.
  6633. If S is surrounded by forward slashes, it is interpreted as a
  6634. regular expression. In org-mode files, this will create an `org-occur'
  6635. sparse tree. In ordinary files, `occur' will be used to list matches.
  6636. If the current buffer is in `dired-mode', grep will be used to search
  6637. in all files. If AVOID-POS is given, ignore matches near that position."
  6638. (let ((case-fold-search t)
  6639. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6640. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6641. (append '(("") (" ") ("\t") ("\n"))
  6642. org-emphasis-alist)
  6643. "\\|") "\\)"))
  6644. (pos (point))
  6645. (pre nil) (post nil)
  6646. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6647. (cond
  6648. ;; First check if there are any special
  6649. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6650. ;; Now try the builtin stuff
  6651. ((save-excursion
  6652. (goto-char (point-min))
  6653. (and
  6654. (re-search-forward
  6655. (concat "<<" (regexp-quote s0) ">>") nil t)
  6656. (setq type 'dedicated
  6657. pos (match-beginning 0))))
  6658. ;; There is an exact target for this
  6659. (goto-char pos))
  6660. ((and (string-match "^(\\(.*\\))$" s0)
  6661. (save-excursion
  6662. (goto-char (point-min))
  6663. (and
  6664. (re-search-forward
  6665. (concat "[^[]" (regexp-quote
  6666. (format org-coderef-label-format
  6667. (match-string 1 s0))))
  6668. nil t)
  6669. (setq type 'dedicated
  6670. pos (1+ (match-beginning 0))))))
  6671. ;; There is a coderef target for this
  6672. (goto-char pos))
  6673. ((string-match "^/\\(.*\\)/$" s)
  6674. ;; A regular expression
  6675. (cond
  6676. ((org-mode-p)
  6677. (org-occur (match-string 1 s)))
  6678. ;;((eq major-mode 'dired-mode)
  6679. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6680. (t (org-do-occur (match-string 1 s)))))
  6681. (t
  6682. ;; A normal search strings
  6683. (when (equal (string-to-char s) ?*)
  6684. ;; Anchor on headlines, post may include tags.
  6685. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6686. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6687. s (substring s 1)))
  6688. (remove-text-properties
  6689. 0 (length s)
  6690. '(face nil mouse-face nil keymap nil fontified nil) s)
  6691. ;; Make a series of regular expressions to find a match
  6692. (setq words (org-split-string s "[ \n\r\t]+")
  6693. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6694. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6695. "\\)" markers)
  6696. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6697. re2a (concat "[ \t\r\n]" re2a_)
  6698. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6699. re4 (concat "[^a-zA-Z_]" re4_)
  6700. re1 (concat pre re2 post)
  6701. re3 (concat pre (if pre re4_ re4) post)
  6702. re5 (concat pre ".*" re4)
  6703. re2 (concat pre re2)
  6704. re2a (concat pre (if pre re2a_ re2a))
  6705. re4 (concat pre (if pre re4_ re4))
  6706. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6707. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6708. re5 "\\)"
  6709. ))
  6710. (cond
  6711. ((eq type 'org-occur) (org-occur reall))
  6712. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6713. (t (goto-char (point-min))
  6714. (setq type 'fuzzy)
  6715. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6716. (org-search-not-self 1 re1 nil t)
  6717. (org-search-not-self 1 re2 nil t)
  6718. (org-search-not-self 1 re2a nil t)
  6719. (org-search-not-self 1 re3 nil t)
  6720. (org-search-not-self 1 re4 nil t)
  6721. (org-search-not-self 1 re5 nil t)
  6722. )
  6723. (goto-char (match-beginning 1))
  6724. (goto-char pos)
  6725. (error "No match")))))
  6726. (t
  6727. ;; Normal string-search
  6728. (goto-char (point-min))
  6729. (if (search-forward s nil t)
  6730. (goto-char (match-beginning 0))
  6731. (error "No match"))))
  6732. (and (org-mode-p) (org-show-context 'link-search))
  6733. type))
  6734. (defun org-search-not-self (group &rest args)
  6735. "Execute `re-search-forward', but only accept matches that do not
  6736. enclose the position of `org-open-link-marker'."
  6737. (let ((m org-open-link-marker))
  6738. (catch 'exit
  6739. (while (apply 're-search-forward args)
  6740. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6741. (goto-char (match-end group))
  6742. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6743. (> (match-beginning 0) (marker-position m))
  6744. (< (match-end 0) (marker-position m)))
  6745. (save-match-data
  6746. (or (not (org-in-regexp
  6747. org-bracket-link-analytic-regexp 1))
  6748. (not (match-end 4)) ; no description
  6749. (and (<= (match-beginning 4) (point))
  6750. (>= (match-end 4) (point))))))
  6751. (throw 'exit (point))))))))
  6752. (defun org-get-buffer-for-internal-link (buffer)
  6753. "Return a buffer to be used for displaying the link target of internal links."
  6754. (cond
  6755. ((not org-display-internal-link-with-indirect-buffer)
  6756. buffer)
  6757. ((string-match "(Clone)$" (buffer-name buffer))
  6758. (message "Buffer is already a clone, not making another one")
  6759. ;; we also do not modify visibility in this case
  6760. buffer)
  6761. (t ; make a new indirect buffer for displaying the link
  6762. (let* ((bn (buffer-name buffer))
  6763. (ibn (concat bn "(Clone)"))
  6764. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6765. (with-current-buffer ib (org-overview))
  6766. ib))))
  6767. (defun org-do-occur (regexp &optional cleanup)
  6768. "Call the Emacs command `occur'.
  6769. If CLEANUP is non-nil, remove the printout of the regular expression
  6770. in the *Occur* buffer. This is useful if the regex is long and not useful
  6771. to read."
  6772. (occur regexp)
  6773. (when cleanup
  6774. (let ((cwin (selected-window)) win beg end)
  6775. (when (setq win (get-buffer-window "*Occur*"))
  6776. (select-window win))
  6777. (goto-char (point-min))
  6778. (when (re-search-forward "match[a-z]+" nil t)
  6779. (setq beg (match-end 0))
  6780. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6781. (setq end (1- (match-beginning 0)))))
  6782. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6783. (goto-char (point-min))
  6784. (select-window cwin))))
  6785. ;;; The mark ring for links jumps
  6786. (defvar org-mark-ring nil
  6787. "Mark ring for positions before jumps in Org-mode.")
  6788. (defvar org-mark-ring-last-goto nil
  6789. "Last position in the mark ring used to go back.")
  6790. ;; Fill and close the ring
  6791. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6792. (loop for i from 1 to org-mark-ring-length do
  6793. (push (make-marker) org-mark-ring))
  6794. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6795. org-mark-ring)
  6796. (defun org-mark-ring-push (&optional pos buffer)
  6797. "Put the current position or POS into the mark ring and rotate it."
  6798. (interactive)
  6799. (setq pos (or pos (point)))
  6800. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6801. (move-marker (car org-mark-ring)
  6802. (or pos (point))
  6803. (or buffer (current-buffer)))
  6804. (message "%s"
  6805. (substitute-command-keys
  6806. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6807. (defun org-mark-ring-goto (&optional n)
  6808. "Jump to the previous position in the mark ring.
  6809. With prefix arg N, jump back that many stored positions. When
  6810. called several times in succession, walk through the entire ring.
  6811. Org-mode commands jumping to a different position in the current file,
  6812. or to another Org-mode file, automatically push the old position
  6813. onto the ring."
  6814. (interactive "p")
  6815. (let (p m)
  6816. (if (eq last-command this-command)
  6817. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6818. (setq p org-mark-ring))
  6819. (setq org-mark-ring-last-goto p)
  6820. (setq m (car p))
  6821. (switch-to-buffer (marker-buffer m))
  6822. (goto-char m)
  6823. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6824. (defun org-remove-angle-brackets (s)
  6825. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6826. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6827. s)
  6828. (defun org-add-angle-brackets (s)
  6829. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6830. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6831. s)
  6832. (defun org-remove-double-quotes (s)
  6833. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6834. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6835. s)
  6836. ;;; Following specific links
  6837. (defun org-follow-timestamp-link ()
  6838. (cond
  6839. ((org-at-date-range-p t)
  6840. (let ((org-agenda-start-on-weekday)
  6841. (t1 (match-string 1))
  6842. (t2 (match-string 2)))
  6843. (setq t1 (time-to-days (org-time-string-to-time t1))
  6844. t2 (time-to-days (org-time-string-to-time t2)))
  6845. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6846. ((org-at-timestamp-p t)
  6847. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6848. (substring (match-string 1) 0 10)))
  6849. 1))
  6850. (t (error "This should not happen"))))
  6851. ;;; Following file links
  6852. (defvar org-wait nil)
  6853. (defun org-open-file (path &optional in-emacs line search)
  6854. "Open the file at PATH.
  6855. First, this expands any special file name abbreviations. Then the
  6856. configuration variable `org-file-apps' is checked if it contains an
  6857. entry for this file type, and if yes, the corresponding command is launched.
  6858. If no application is found, Emacs simply visits the file.
  6859. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6860. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6861. and o use an external application to visit the file.
  6862. Optional LINE specifies a line to go to, optional SEARCH a string to
  6863. search for. If LINE or SEARCH is given, the file will always be
  6864. opened in Emacs.
  6865. If the file does not exist, an error is thrown."
  6866. (setq in-emacs (or in-emacs line search))
  6867. (let* ((file (if (equal path "")
  6868. buffer-file-name
  6869. (substitute-in-file-name (expand-file-name path))))
  6870. (apps (append org-file-apps (org-default-apps)))
  6871. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6872. (dirp (if remp nil (file-directory-p file)))
  6873. (file (if (and dirp org-open-directory-means-index-dot-org)
  6874. (concat (file-name-as-directory file) "index.org")
  6875. file))
  6876. (a-m-a-p (assq 'auto-mode apps))
  6877. (dfile (downcase file))
  6878. (old-buffer (current-buffer))
  6879. (old-pos (point))
  6880. (old-mode major-mode)
  6881. ext cmd)
  6882. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6883. (setq ext (match-string 1 dfile))
  6884. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6885. (setq ext (match-string 1 dfile))))
  6886. (cond
  6887. ((equal in-emacs '(16))
  6888. (setq cmd (cdr (assoc 'system apps))))
  6889. (in-emacs (setq cmd 'emacs))
  6890. (t
  6891. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6892. (and dirp (cdr (assoc 'directory apps)))
  6893. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6894. 'string-match)
  6895. (cdr (assoc ext apps))
  6896. (cdr (assoc t apps))))))
  6897. (when (eq cmd 'system)
  6898. (setq cmd (cdr (assoc 'system apps))))
  6899. (when (eq cmd 'default)
  6900. (setq cmd (cdr (assoc t apps))))
  6901. (when (eq cmd 'mailcap)
  6902. (require 'mailcap)
  6903. (mailcap-parse-mailcaps)
  6904. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6905. (command (mailcap-mime-info mime-type)))
  6906. (if (stringp command)
  6907. (setq cmd command)
  6908. (setq cmd 'emacs))))
  6909. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6910. (not (file-exists-p file))
  6911. (not org-open-non-existing-files))
  6912. (error "No such file: %s" file))
  6913. (cond
  6914. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6915. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6916. (while (string-match "['\"]%s['\"]" cmd)
  6917. (setq cmd (replace-match "%s" t t cmd)))
  6918. (while (string-match "%s" cmd)
  6919. (setq cmd (replace-match
  6920. (save-match-data
  6921. (shell-quote-argument
  6922. (convert-standard-filename file)))
  6923. t t cmd)))
  6924. (save-window-excursion
  6925. (start-process-shell-command cmd nil cmd)
  6926. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6927. ))
  6928. ((or (stringp cmd)
  6929. (eq cmd 'emacs))
  6930. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6931. (widen)
  6932. (if line (goto-line line)
  6933. (if search (org-link-search search))))
  6934. ((consp cmd)
  6935. (let ((file (convert-standard-filename file)))
  6936. (eval cmd)))
  6937. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6938. (and (org-mode-p) (eq old-mode 'org-mode)
  6939. (or (not (equal old-buffer (current-buffer)))
  6940. (not (equal old-pos (point))))
  6941. (org-mark-ring-push old-pos old-buffer))))
  6942. (defun org-default-apps ()
  6943. "Return the default applications for this operating system."
  6944. (cond
  6945. ((eq system-type 'darwin)
  6946. org-file-apps-defaults-macosx)
  6947. ((eq system-type 'windows-nt)
  6948. org-file-apps-defaults-windowsnt)
  6949. (t org-file-apps-defaults-gnu)))
  6950. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6951. "Convert extensions to regular expressions in the cars of LIST.
  6952. Also, weed out any non-string entries, because the return value is used
  6953. only for regexp matching.
  6954. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6955. point to the symbol `emacs', indicating that the file should
  6956. be opened in Emacs."
  6957. (append
  6958. (delq nil
  6959. (mapcar (lambda (x)
  6960. (if (not (stringp (car x)))
  6961. nil
  6962. (if (string-match "\\W" (car x))
  6963. x
  6964. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6965. list))
  6966. (if add-auto-mode
  6967. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6968. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6969. (defun org-file-remote-p (file)
  6970. "Test whether FILE specifies a location on a remote system.
  6971. Return non-nil if the location is indeed remote.
  6972. For example, the filename \"/user@host:/foo\" specifies a location
  6973. on the system \"/user@host:\"."
  6974. (cond ((fboundp 'file-remote-p)
  6975. (file-remote-p file))
  6976. ((fboundp 'tramp-handle-file-remote-p)
  6977. (tramp-handle-file-remote-p file))
  6978. ((and (boundp 'ange-ftp-name-format)
  6979. (string-match (car ange-ftp-name-format) file))
  6980. t)
  6981. (t nil)))
  6982. ;;;; Refiling
  6983. (defun org-get-org-file ()
  6984. "Read a filename, with default directory `org-directory'."
  6985. (let ((default (or org-default-notes-file remember-data-file)))
  6986. (read-file-name (format "File name [%s]: " default)
  6987. (file-name-as-directory org-directory)
  6988. default)))
  6989. (defun org-notes-order-reversed-p ()
  6990. "Check if the current file should receive notes in reversed order."
  6991. (cond
  6992. ((not org-reverse-note-order) nil)
  6993. ((eq t org-reverse-note-order) t)
  6994. ((not (listp org-reverse-note-order)) nil)
  6995. (t (catch 'exit
  6996. (let ((all org-reverse-note-order)
  6997. entry)
  6998. (while (setq entry (pop all))
  6999. (if (string-match (car entry) buffer-file-name)
  7000. (throw 'exit (cdr entry))))
  7001. nil)))))
  7002. (defvar org-refile-target-table nil
  7003. "The list of refile targets, created by `org-refile'.")
  7004. (defvar org-agenda-new-buffers nil
  7005. "Buffers created to visit agenda files.")
  7006. (defun org-get-refile-targets (&optional default-buffer)
  7007. "Produce a table with refile targets."
  7008. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7009. targets txt re files f desc descre fast-path-p level)
  7010. (message "Getting targets...")
  7011. (with-current-buffer (or default-buffer (current-buffer))
  7012. (while (setq entry (pop entries))
  7013. (setq files (car entry) desc (cdr entry))
  7014. (setq fast-path-p nil)
  7015. (cond
  7016. ((null files) (setq files (list (current-buffer))))
  7017. ((eq files 'org-agenda-files)
  7018. (setq files (org-agenda-files 'unrestricted)))
  7019. ((and (symbolp files) (fboundp files))
  7020. (setq files (funcall files)))
  7021. ((and (symbolp files) (boundp files))
  7022. (setq files (symbol-value files))))
  7023. (if (stringp files) (setq files (list files)))
  7024. (cond
  7025. ((eq (car desc) :tag)
  7026. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7027. ((eq (car desc) :todo)
  7028. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7029. ((eq (car desc) :regexp)
  7030. (setq descre (cdr desc)))
  7031. ((eq (car desc) :level)
  7032. (setq descre (concat "^\\*\\{" (number-to-string
  7033. (if org-odd-levels-only
  7034. (1- (* 2 (cdr desc)))
  7035. (cdr desc)))
  7036. "\\}[ \t]")))
  7037. ((eq (car desc) :maxlevel)
  7038. (setq fast-path-p t)
  7039. (setq descre (concat "^\\*\\{1," (number-to-string
  7040. (if org-odd-levels-only
  7041. (1- (* 2 (cdr desc)))
  7042. (cdr desc)))
  7043. "\\}[ \t]")))
  7044. (t (error "Bad refiling target description %s" desc)))
  7045. (while (setq f (pop files))
  7046. (save-excursion
  7047. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7048. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7049. (setq f (expand-file-name f))
  7050. (save-excursion
  7051. (save-restriction
  7052. (widen)
  7053. (goto-char (point-min))
  7054. (while (re-search-forward descre nil t)
  7055. (goto-char (point-at-bol))
  7056. (when (looking-at org-complex-heading-regexp)
  7057. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7058. txt (org-link-display-format (match-string 4))
  7059. re (concat "^" (regexp-quote
  7060. (buffer-substring (match-beginning 1)
  7061. (match-end 4)))))
  7062. (if (match-end 5) (setq re (concat re "[ \t]+"
  7063. (regexp-quote
  7064. (match-string 5)))))
  7065. (setq re (concat re "[ \t]*$"))
  7066. (when org-refile-use-outline-path
  7067. (setq txt (mapconcat 'org-protect-slash
  7068. (append
  7069. (if (eq org-refile-use-outline-path 'file)
  7070. (list (file-name-nondirectory
  7071. (buffer-file-name (buffer-base-buffer))))
  7072. (if (eq org-refile-use-outline-path 'full-file-path)
  7073. (list (buffer-file-name (buffer-base-buffer)))))
  7074. (org-get-outline-path fast-path-p level txt)
  7075. (list txt))
  7076. "/")))
  7077. (push (list txt f re (point)) targets))
  7078. (goto-char (point-at-eol))))))))
  7079. (message "Getting targets...done")
  7080. (nreverse targets))))
  7081. (defun org-protect-slash (s)
  7082. (while (string-match "/" s)
  7083. (setq s (replace-match "\\" t t s)))
  7084. s)
  7085. (defvar org-olpa (make-vector 20 nil))
  7086. (defun org-get-outline-path (&optional fastp level heading)
  7087. "Return the outline path to the current entry, as a list."
  7088. (if fastp
  7089. (progn
  7090. (if (> level 19)
  7091. (error "Outline path failure, more than 19 levels."))
  7092. (loop for i from level upto 19 do
  7093. (aset org-olpa i nil))
  7094. (prog1
  7095. (delq nil (append org-olpa nil))
  7096. (aset org-olpa level heading)))
  7097. (let (rtn)
  7098. (save-excursion
  7099. (while (org-up-heading-safe)
  7100. (when (looking-at org-complex-heading-regexp)
  7101. (push (org-match-string-no-properties 4) rtn)))
  7102. rtn))))
  7103. (defvar org-refile-history nil
  7104. "History for refiling operations.")
  7105. (defun org-refile (&optional goto default-buffer)
  7106. "Move the entry at point to another heading.
  7107. The list of target headings is compiled using the information in
  7108. `org-refile-targets', which see. This list is created before each use
  7109. and will therefore always be up-to-date.
  7110. At the target location, the entry is filed as a subitem of the target heading.
  7111. Depending on `org-reverse-note-order', the new subitem will either be the
  7112. first or the last subitem.
  7113. If there is an active region, all entries in that region will be moved.
  7114. However, the region must fulfil the requirement that the first heading
  7115. is the first one sets the top-level of the moved text - at most siblings
  7116. below it are allowed.
  7117. With prefix arg GOTO, the command will only visit the target location,
  7118. not actually move anything.
  7119. With a double prefix `C-u C-u', go to the location where the last refiling
  7120. operation has put the subtree."
  7121. (interactive "P")
  7122. (let* ((cbuf (current-buffer))
  7123. (regionp (org-region-active-p))
  7124. (region-start (and regionp (region-beginning)))
  7125. (region-end (and regionp (region-end)))
  7126. (region-length (and regionp (- region-end region-start)))
  7127. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7128. pos it nbuf file re level reversed)
  7129. (when regionp (goto-char region-start)
  7130. (unless (org-kill-is-subtree-p
  7131. (buffer-substring region-start region-end))
  7132. (error "The region is not a (sequence of) subtree(s)")))
  7133. (if (equal goto '(16))
  7134. (org-refile-goto-last-stored)
  7135. (when (setq it (org-refile-get-location
  7136. (if goto "Goto: " "Refile to: ") default-buffer))
  7137. (setq file (nth 1 it)
  7138. re (nth 2 it)
  7139. pos (nth 3 it))
  7140. (if (and (equal (buffer-file-name) file)
  7141. (if regionp
  7142. (and (>= pos region-start)
  7143. (<= pos region-end))
  7144. (and (>= pos (point))
  7145. (< pos (save-excursion
  7146. (org-end-of-subtree t t))))))
  7147. (error "Cannot refile to position inside the tree or region"))
  7148. (setq nbuf (or (find-buffer-visiting file)
  7149. (find-file-noselect file)))
  7150. (if goto
  7151. (progn
  7152. (switch-to-buffer nbuf)
  7153. (goto-char pos)
  7154. (org-show-context 'org-goto))
  7155. (if regionp
  7156. (progn
  7157. (kill-new (buffer-substring region-start region-end))
  7158. (org-save-markers-in-region region-start region-end))
  7159. (org-copy-subtree 1 nil t))
  7160. (save-excursion
  7161. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7162. (find-file-noselect file))))
  7163. (setq reversed (org-notes-order-reversed-p))
  7164. (save-excursion
  7165. (save-restriction
  7166. (widen)
  7167. (goto-char pos)
  7168. (looking-at outline-regexp)
  7169. (setq level (org-get-valid-level (funcall outline-level) 1))
  7170. (goto-char
  7171. (if reversed
  7172. (or (outline-next-heading) (point-max))
  7173. (or (save-excursion (outline-get-next-sibling))
  7174. (org-end-of-subtree t t)
  7175. (point-max))))
  7176. (if (not (bolp)) (newline))
  7177. (bookmark-set "org-refile-last-stored")
  7178. (org-paste-subtree level))))
  7179. (if regionp
  7180. (delete-region (point) (+ (point) region-length))
  7181. (org-cut-subtree))
  7182. (setq org-markers-to-move nil)
  7183. (message "Refiled to \"%s\"" (car it)))))))
  7184. (defun org-refile-goto-last-stored ()
  7185. "Go to the location where the last refile was stored."
  7186. (interactive)
  7187. (bookmark-jump "org-refile-last-stored")
  7188. (message "This is the location of the last refile"))
  7189. (defun org-refile-get-location (&optional prompt default-buffer)
  7190. "Prompt the user for a refile location, using PROMPT."
  7191. (let ((org-refile-targets org-refile-targets)
  7192. (org-refile-use-outline-path org-refile-use-outline-path))
  7193. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7194. (unless org-refile-target-table
  7195. (error "No refile targets"))
  7196. (let* ((cbuf (current-buffer))
  7197. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7198. (cfunc (if (and org-refile-use-outline-path
  7199. org-outline-path-complete-in-steps)
  7200. 'org-olpath-completing-read
  7201. 'org-ido-completing-read))
  7202. (extra (if org-refile-use-outline-path "/" ""))
  7203. (filename (and cfn (expand-file-name cfn)))
  7204. (tbl (mapcar
  7205. (lambda (x)
  7206. (if (not (equal filename (nth 1 x)))
  7207. (cons (concat (car x) extra " ("
  7208. (file-name-nondirectory (nth 1 x)) ")")
  7209. (cdr x))
  7210. (cons (concat (car x) extra) (cdr x))))
  7211. org-refile-target-table))
  7212. (completion-ignore-case t))
  7213. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7214. tbl)))
  7215. (defun org-olpath-completing-read (prompt collection &rest args)
  7216. "Read an outline path like a file name."
  7217. (let ((thetable collection))
  7218. (apply
  7219. 'org-ido-completing-read prompt
  7220. (lambda (string predicate &optional flag)
  7221. (let (rtn r f (l (length string)))
  7222. (cond
  7223. ((eq flag nil)
  7224. ;; try completion
  7225. (try-completion string thetable))
  7226. ((eq flag t)
  7227. ;; all-completions
  7228. (setq rtn (all-completions string thetable predicate))
  7229. (mapcar
  7230. (lambda (x)
  7231. (setq r (substring x l))
  7232. (if (string-match " ([^)]*)$" x)
  7233. (setq f (match-string 0 x))
  7234. (setq f ""))
  7235. (if (string-match "/" r)
  7236. (concat string (substring r 0 (match-end 0)) f)
  7237. x))
  7238. rtn))
  7239. ((eq flag 'lambda)
  7240. ;; exact match?
  7241. (assoc string thetable)))
  7242. ))
  7243. args)))
  7244. ;;;; Dynamic blocks
  7245. (defun org-find-dblock (name)
  7246. "Find the first dynamic block with name NAME in the buffer.
  7247. If not found, stay at current position and return nil."
  7248. (let (pos)
  7249. (save-excursion
  7250. (goto-char (point-min))
  7251. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7252. nil t)
  7253. (match-beginning 0))))
  7254. (if pos (goto-char pos))
  7255. pos))
  7256. (defconst org-dblock-start-re
  7257. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7258. "Matches the startline of a dynamic block, with parameters.")
  7259. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7260. "Matches the end of a dynamic block.")
  7261. (defun org-create-dblock (plist)
  7262. "Create a dynamic block section, with parameters taken from PLIST.
  7263. PLIST must contain a :name entry which is used as name of the block."
  7264. (unless (bolp) (newline))
  7265. (let ((name (plist-get plist :name)))
  7266. (insert "#+BEGIN: " name)
  7267. (while plist
  7268. (if (eq (car plist) :name)
  7269. (setq plist (cddr plist))
  7270. (insert " " (prin1-to-string (pop plist)))))
  7271. (insert "\n\n#+END:\n")
  7272. (beginning-of-line -2)))
  7273. (defun org-prepare-dblock ()
  7274. "Prepare dynamic block for refresh.
  7275. This empties the block, puts the cursor at the insert position and returns
  7276. the property list including an extra property :name with the block name."
  7277. (unless (looking-at org-dblock-start-re)
  7278. (error "Not at a dynamic block"))
  7279. (let* ((begdel (1+ (match-end 0)))
  7280. (name (org-no-properties (match-string 1)))
  7281. (params (append (list :name name)
  7282. (read (concat "(" (match-string 3) ")")))))
  7283. (unless (re-search-forward org-dblock-end-re nil t)
  7284. (error "Dynamic block not terminated"))
  7285. (setq params
  7286. (append params
  7287. (list :content (buffer-substring
  7288. begdel (match-beginning 0)))))
  7289. (delete-region begdel (match-beginning 0))
  7290. (goto-char begdel)
  7291. (open-line 1)
  7292. params))
  7293. (defun org-map-dblocks (&optional command)
  7294. "Apply COMMAND to all dynamic blocks in the current buffer.
  7295. If COMMAND is not given, use `org-update-dblock'."
  7296. (let ((cmd (or command 'org-update-dblock))
  7297. pos)
  7298. (save-excursion
  7299. (goto-char (point-min))
  7300. (while (re-search-forward org-dblock-start-re nil t)
  7301. (goto-char (setq pos (match-beginning 0)))
  7302. (condition-case nil
  7303. (funcall cmd)
  7304. (error (message "Error during update of dynamic block")))
  7305. (goto-char pos)
  7306. (unless (re-search-forward org-dblock-end-re nil t)
  7307. (error "Dynamic block not terminated"))))))
  7308. (defun org-dblock-update (&optional arg)
  7309. "User command for updating dynamic blocks.
  7310. Update the dynamic block at point. With prefix ARG, update all dynamic
  7311. blocks in the buffer."
  7312. (interactive "P")
  7313. (if arg
  7314. (org-update-all-dblocks)
  7315. (or (looking-at org-dblock-start-re)
  7316. (org-beginning-of-dblock))
  7317. (org-update-dblock)))
  7318. (defun org-update-dblock ()
  7319. "Update the dynamic block at point
  7320. This means to empty the block, parse for parameters and then call
  7321. the correct writing function."
  7322. (save-window-excursion
  7323. (let* ((pos (point))
  7324. (line (org-current-line))
  7325. (params (org-prepare-dblock))
  7326. (name (plist-get params :name))
  7327. (cmd (intern (concat "org-dblock-write:" name))))
  7328. (message "Updating dynamic block `%s' at line %d..." name line)
  7329. (funcall cmd params)
  7330. (message "Updating dynamic block `%s' at line %d...done" name line)
  7331. (goto-char pos))))
  7332. (defun org-beginning-of-dblock ()
  7333. "Find the beginning of the dynamic block at point.
  7334. Error if there is no such block at point."
  7335. (let ((pos (point))
  7336. beg)
  7337. (end-of-line 1)
  7338. (if (and (re-search-backward org-dblock-start-re nil t)
  7339. (setq beg (match-beginning 0))
  7340. (re-search-forward org-dblock-end-re nil t)
  7341. (> (match-end 0) pos))
  7342. (goto-char beg)
  7343. (goto-char pos)
  7344. (error "Not in a dynamic block"))))
  7345. (defun org-update-all-dblocks ()
  7346. "Update all dynamic blocks in the buffer.
  7347. This function can be used in a hook."
  7348. (when (org-mode-p)
  7349. (org-map-dblocks 'org-update-dblock)))
  7350. ;;;; Completion
  7351. (defconst org-additional-option-like-keywords
  7352. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7353. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7354. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7355. "BEGIN_QUOTE" "END_QUOTE"
  7356. "BEGIN_VERSE" "END_VERSE"
  7357. "BEGIN_SRC" "END_SRC"
  7358. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7359. (defcustom org-structure-template-alist
  7360. '(
  7361. ("s" "#+begin_src ?\n\n#+end_src"
  7362. "<src lang=\"?\">\n\n</src>")
  7363. ("e" "#+begin_example\n?\n#+end_example"
  7364. "<example>\n?\n</example>")
  7365. ("q" "#+begin_quote\n?\n#+end_quote"
  7366. "<quote>\n?\n</quote>")
  7367. ("v" "#+begin_verse\n?\n#+end_verse"
  7368. "<verse>\n?\n/verse>")
  7369. ("l" "#+begin_latex\n?\n#+end_latex"
  7370. "<literal style=\"latex\">\n?\n</literal>")
  7371. ("L" "#+latex: "
  7372. "<literal style=\"latex\">?</literal>")
  7373. ("h" "#+begin_html\n?\n#+end_html"
  7374. "<literal style=\"html\">\n?\n</literal>")
  7375. ("H" "#+html: "
  7376. "<literal style=\"html\">?</literal>")
  7377. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7378. ("A" "#+ascii: ")
  7379. ("i" "#+include %file ?"
  7380. "<include file=%file markup=\"?\">")
  7381. )
  7382. "Structure completion elements.
  7383. This is a list of abbreviation keys and values. The value gets inserted
  7384. it you type @samp{.} followed by the key and then the completion key,
  7385. usually `M-TAB'. %file will be replaced by a file name after prompting
  7386. for the file using completion.
  7387. There are two templates for each key, the first uses the original Org syntax,
  7388. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7389. the default when the /org-mtags.el/ module has been loaded. See also the
  7390. variable `org-mtags-prefer-muse-templates'.
  7391. This is an experimental feature, it is undecided if it is going to stay in."
  7392. :group 'org-completion
  7393. :type '(repeat
  7394. (string :tag "Key")
  7395. (string :tag "Template")
  7396. (string :tag "Muse Template")))
  7397. (defun org-try-structure-completion ()
  7398. "Try to complete a structure template before point.
  7399. This looks for strings like \"<e\" on an otherwise empty line and
  7400. expands them."
  7401. (let ((l (buffer-substring (point-at-bol) (point)))
  7402. a)
  7403. (when (and (looking-at "[ \t]*$")
  7404. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7405. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7406. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7407. (match-beginning 1)) a)
  7408. t)))
  7409. (defun org-complete-expand-structure-template (start cell)
  7410. "Expand a structure template."
  7411. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7412. (rpl (nth (if musep 2 1) cell)))
  7413. (delete-region start (point))
  7414. (when (string-match "\\`#\\+" rpl)
  7415. (cond
  7416. ((bolp))
  7417. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7418. (delete-region (point-at-bol) (point)))
  7419. (t (newline))))
  7420. (setq start (point))
  7421. (if (string-match "%file" rpl)
  7422. (setq rpl (replace-match
  7423. (concat
  7424. "\""
  7425. (save-match-data
  7426. (abbreviate-file-name (read-file-name "Include file: ")))
  7427. "\"")
  7428. t t rpl)))
  7429. (insert rpl)
  7430. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7431. (defun org-complete (&optional arg)
  7432. "Perform completion on word at point.
  7433. At the beginning of a headline, this completes TODO keywords as given in
  7434. `org-todo-keywords'.
  7435. If the current word is preceded by a backslash, completes the TeX symbols
  7436. that are supported for HTML support.
  7437. If the current word is preceded by \"#+\", completes special words for
  7438. setting file options.
  7439. In the line after \"#+STARTUP:, complete valid keywords.\"
  7440. At all other locations, this simply calls the value of
  7441. `org-completion-fallback-command'."
  7442. (interactive "P")
  7443. (org-without-partial-completion
  7444. (catch 'exit
  7445. (let* ((a nil)
  7446. (end (point))
  7447. (beg1 (save-excursion
  7448. (skip-chars-backward (org-re "[:alnum:]_@"))
  7449. (point)))
  7450. (beg (save-excursion
  7451. (skip-chars-backward "a-zA-Z0-9_:$")
  7452. (point)))
  7453. (confirm (lambda (x) (stringp (car x))))
  7454. (searchhead (equal (char-before beg) ?*))
  7455. (struct
  7456. (when (and (member (char-before beg1) '(?. ?<))
  7457. (setq a (assoc (buffer-substring beg1 (point))
  7458. org-structure-template-alist)))
  7459. (org-complete-expand-structure-template (1- beg1) a)
  7460. (throw 'exit t)))
  7461. (tag (and (equal (char-before beg1) ?:)
  7462. (equal (char-after (point-at-bol)) ?*)))
  7463. (prop (and (equal (char-before beg1) ?:)
  7464. (not (equal (char-after (point-at-bol)) ?*))))
  7465. (texp (equal (char-before beg) ?\\))
  7466. (link (equal (char-before beg) ?\[))
  7467. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7468. beg)
  7469. "#+"))
  7470. (startup (string-match "^#\\+STARTUP:.*"
  7471. (buffer-substring (point-at-bol) (point))))
  7472. (completion-ignore-case opt)
  7473. (type nil)
  7474. (tbl nil)
  7475. (table (cond
  7476. (opt
  7477. (setq type :opt)
  7478. (require 'org-exp)
  7479. (append
  7480. (mapcar
  7481. (lambda (x)
  7482. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7483. (cons (match-string 2 x) (match-string 1 x)))
  7484. (org-split-string (org-get-current-options) "\n"))
  7485. (mapcar 'list org-additional-option-like-keywords)))
  7486. (startup
  7487. (setq type :startup)
  7488. org-startup-options)
  7489. (link (append org-link-abbrev-alist-local
  7490. org-link-abbrev-alist))
  7491. (texp
  7492. (setq type :tex)
  7493. org-html-entities)
  7494. ((string-match "\\`\\*+[ \t]+\\'"
  7495. (buffer-substring (point-at-bol) beg))
  7496. (setq type :todo)
  7497. (mapcar 'list org-todo-keywords-1))
  7498. (searchhead
  7499. (setq type :searchhead)
  7500. (save-excursion
  7501. (goto-char (point-min))
  7502. (while (re-search-forward org-todo-line-regexp nil t)
  7503. (push (list
  7504. (org-make-org-heading-search-string
  7505. (match-string 3) t))
  7506. tbl)))
  7507. tbl)
  7508. (tag (setq type :tag beg beg1)
  7509. (or org-tag-alist (org-get-buffer-tags)))
  7510. (prop (setq type :prop beg beg1)
  7511. (mapcar 'list (org-buffer-property-keys nil t t)))
  7512. (t (progn
  7513. (call-interactively org-completion-fallback-command)
  7514. (throw 'exit nil)))))
  7515. (pattern (buffer-substring-no-properties beg end))
  7516. (completion (try-completion pattern table confirm)))
  7517. (cond ((eq completion t)
  7518. (if (not (assoc (upcase pattern) table))
  7519. (message "Already complete")
  7520. (if (and (equal type :opt)
  7521. (not (member (car (assoc (upcase pattern) table))
  7522. org-additional-option-like-keywords)))
  7523. (insert (substring (cdr (assoc (upcase pattern) table))
  7524. (length pattern)))
  7525. (if (memq type '(:tag :prop)) (insert ":")))))
  7526. ((null completion)
  7527. (message "Can't find completion for \"%s\"" pattern)
  7528. (ding))
  7529. ((not (string= pattern completion))
  7530. (delete-region beg end)
  7531. (if (string-match " +$" completion)
  7532. (setq completion (replace-match "" t t completion)))
  7533. (insert completion)
  7534. (if (get-buffer-window "*Completions*")
  7535. (delete-window (get-buffer-window "*Completions*")))
  7536. (if (assoc completion table)
  7537. (if (eq type :todo) (insert " ")
  7538. (if (memq type '(:tag :prop)) (insert ":"))))
  7539. (if (and (equal type :opt) (assoc completion table))
  7540. (message "%s" (substitute-command-keys
  7541. "Press \\[org-complete] again to insert example settings"))))
  7542. (t
  7543. (message "Making completion list...")
  7544. (let ((list (sort (all-completions pattern table confirm)
  7545. 'string<)))
  7546. (with-output-to-temp-buffer "*Completions*"
  7547. (condition-case nil
  7548. ;; Protection needed for XEmacs and emacs 21
  7549. (display-completion-list list pattern)
  7550. (error (display-completion-list list)))))
  7551. (message "Making completion list...%s" "done")))))))
  7552. ;;;; TODO, DEADLINE, Comments
  7553. (defun org-toggle-comment ()
  7554. "Change the COMMENT state of an entry."
  7555. (interactive)
  7556. (save-excursion
  7557. (org-back-to-heading)
  7558. (let (case-fold-search)
  7559. (if (looking-at (concat outline-regexp
  7560. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7561. (replace-match "" t t nil 1)
  7562. (if (looking-at outline-regexp)
  7563. (progn
  7564. (goto-char (match-end 0))
  7565. (insert org-comment-string " ")))))))
  7566. (defvar org-last-todo-state-is-todo nil
  7567. "This is non-nil when the last TODO state change led to a TODO state.
  7568. If the last change removed the TODO tag or switched to DONE, then
  7569. this is nil.")
  7570. (defvar org-setting-tags nil) ; dynamically skipped
  7571. (defun org-parse-local-options (string var)
  7572. "Parse STRING for startup setting relevant for variable VAR."
  7573. (let ((rtn (symbol-value var))
  7574. e opts)
  7575. (save-match-data
  7576. (if (or (not string) (not (string-match "\\S-" string)))
  7577. rtn
  7578. (setq opts (delq nil (mapcar (lambda (x)
  7579. (setq e (assoc x org-startup-options))
  7580. (if (eq (nth 1 e) var) e nil))
  7581. (org-split-string string "[ \t]+"))))
  7582. (if (not opts)
  7583. rtn
  7584. (setq rtn nil)
  7585. (while (setq e (pop opts))
  7586. (if (not (nth 3 e))
  7587. (setq rtn (nth 2 e))
  7588. (if (not (listp rtn)) (setq rtn nil))
  7589. (push (nth 2 e) rtn)))
  7590. rtn)))))
  7591. (defvar org-todo-setup-filter-hook nil
  7592. "Hook for functions that pre-filter todo specs.
  7593. Each function takes a todo spec and returns either `nil' or the spec
  7594. transformed into canonical form." )
  7595. (defvar org-todo-get-default-hook nil
  7596. "Hook for functions that get a default item for todo.
  7597. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7598. `nil' or a string to be used for the todo mark." )
  7599. (defvar org-agenda-headline-snapshot-before-repeat)
  7600. (defun org-todo (&optional arg)
  7601. "Change the TODO state of an item.
  7602. The state of an item is given by a keyword at the start of the heading,
  7603. like
  7604. *** TODO Write paper
  7605. *** DONE Call mom
  7606. The different keywords are specified in the variable `org-todo-keywords'.
  7607. By default the available states are \"TODO\" and \"DONE\".
  7608. So for this example: when the item starts with TODO, it is changed to DONE.
  7609. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7610. DONE are present, add TODO at the beginning of the heading.
  7611. With C-u prefix arg, use completion to determine the new state.
  7612. With numeric prefix arg, switch to that state.
  7613. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7614. With a tripple C-u prefix, circumvent any state blocking.
  7615. For calling through lisp, arg is also interpreted in the following way:
  7616. 'none -> empty state
  7617. \"\"(empty string) -> switch to empty state
  7618. 'done -> switch to DONE
  7619. 'nextset -> switch to the next set of keywords
  7620. 'previousset -> switch to the previous set of keywords
  7621. \"WAITING\" -> switch to the specified keyword, but only if it
  7622. really is a member of `org-todo-keywords'."
  7623. (interactive "P")
  7624. (if (equal arg '(16)) (setq arg 'nextset))
  7625. (let ((org-blocker-hook org-blocker-hook))
  7626. (when (equal arg '(64))
  7627. (setq arg nil org-blocker-hook nil))
  7628. (save-excursion
  7629. (catch 'exit
  7630. (org-back-to-heading)
  7631. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7632. (or (looking-at (concat " +" org-todo-regexp " *"))
  7633. (looking-at " *"))
  7634. (let* ((match-data (match-data))
  7635. (startpos (point-at-bol))
  7636. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7637. (org-log-done org-log-done)
  7638. (org-log-repeat org-log-repeat)
  7639. (org-todo-log-states org-todo-log-states)
  7640. (this (match-string 1))
  7641. (hl-pos (match-beginning 0))
  7642. (head (org-get-todo-sequence-head this))
  7643. (ass (assoc head org-todo-kwd-alist))
  7644. (interpret (nth 1 ass))
  7645. (done-word (nth 3 ass))
  7646. (final-done-word (nth 4 ass))
  7647. (last-state (or this ""))
  7648. (completion-ignore-case t)
  7649. (member (member this org-todo-keywords-1))
  7650. (tail (cdr member))
  7651. (state (cond
  7652. ((and org-todo-key-trigger
  7653. (or (and (equal arg '(4))
  7654. (eq org-use-fast-todo-selection 'prefix))
  7655. (and (not arg) org-use-fast-todo-selection
  7656. (not (eq org-use-fast-todo-selection
  7657. 'prefix)))))
  7658. ;; Use fast selection
  7659. (org-fast-todo-selection))
  7660. ((and (equal arg '(4))
  7661. (or (not org-use-fast-todo-selection)
  7662. (not org-todo-key-trigger)))
  7663. ;; Read a state with completion
  7664. (org-ido-completing-read
  7665. "State: " (mapcar (lambda(x) (list x))
  7666. org-todo-keywords-1)
  7667. nil t))
  7668. ((eq arg 'right)
  7669. (if this
  7670. (if tail (car tail) nil)
  7671. (car org-todo-keywords-1)))
  7672. ((eq arg 'left)
  7673. (if (equal member org-todo-keywords-1)
  7674. nil
  7675. (if this
  7676. (nth (- (length org-todo-keywords-1)
  7677. (length tail) 2)
  7678. org-todo-keywords-1)
  7679. (org-last org-todo-keywords-1))))
  7680. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7681. (setq arg nil))) ; hack to fall back to cycling
  7682. (arg
  7683. ;; user or caller requests a specific state
  7684. (cond
  7685. ((equal arg "") nil)
  7686. ((eq arg 'none) nil)
  7687. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7688. ((eq arg 'nextset)
  7689. (or (car (cdr (member head org-todo-heads)))
  7690. (car org-todo-heads)))
  7691. ((eq arg 'previousset)
  7692. (let ((org-todo-heads (reverse org-todo-heads)))
  7693. (or (car (cdr (member head org-todo-heads)))
  7694. (car org-todo-heads))))
  7695. ((car (member arg org-todo-keywords-1)))
  7696. ((nth (1- (prefix-numeric-value arg))
  7697. org-todo-keywords-1))))
  7698. ((null member) (or head (car org-todo-keywords-1)))
  7699. ((equal this final-done-word) nil) ;; -> make empty
  7700. ((null tail) nil) ;; -> first entry
  7701. ((memq interpret '(type priority))
  7702. (if (eq this-command last-command)
  7703. (car tail)
  7704. (if (> (length tail) 0)
  7705. (or done-word (car org-done-keywords))
  7706. nil)))
  7707. (t
  7708. (car tail))))
  7709. (state (or
  7710. (run-hook-with-args-until-success
  7711. 'org-todo-get-default-hook state last-state)
  7712. state))
  7713. (next (if state (concat " " state " ") " "))
  7714. (change-plist (list :type 'todo-state-change :from this :to state
  7715. :position startpos))
  7716. dolog now-done-p)
  7717. (when org-blocker-hook
  7718. (setq org-last-todo-state-is-todo
  7719. (not (member this org-done-keywords)))
  7720. (unless (save-excursion
  7721. (save-match-data
  7722. (run-hook-with-args-until-failure
  7723. 'org-blocker-hook change-plist)))
  7724. (if (interactive-p)
  7725. (error "TODO state change from %s to %s blocked" this state)
  7726. ;; fail silently
  7727. (message "TODO state change from %s to %s blocked" this state)
  7728. (throw 'exit nil))))
  7729. (store-match-data match-data)
  7730. (replace-match next t t)
  7731. (unless (pos-visible-in-window-p hl-pos)
  7732. (message "TODO state changed to %s" (org-trim next)))
  7733. (unless head
  7734. (setq head (org-get-todo-sequence-head state)
  7735. ass (assoc head org-todo-kwd-alist)
  7736. interpret (nth 1 ass)
  7737. done-word (nth 3 ass)
  7738. final-done-word (nth 4 ass)))
  7739. (when (memq arg '(nextset previousset))
  7740. (message "Keyword-Set %d/%d: %s"
  7741. (- (length org-todo-sets) -1
  7742. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7743. (length org-todo-sets)
  7744. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7745. (setq org-last-todo-state-is-todo
  7746. (not (member state org-done-keywords)))
  7747. (setq now-done-p (and (member state org-done-keywords)
  7748. (not (member this org-done-keywords))))
  7749. (and logging (org-local-logging logging))
  7750. (when (and (or org-todo-log-states org-log-done)
  7751. (not (memq arg '(nextset previousset))))
  7752. ;; we need to look at recording a time and note
  7753. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7754. (nth 2 (assoc this org-todo-log-states))))
  7755. (when (and state
  7756. (member state org-not-done-keywords)
  7757. (not (member this org-not-done-keywords)))
  7758. ;; This is now a todo state and was not one before
  7759. ;; If there was a CLOSED time stamp, get rid of it.
  7760. (org-add-planning-info nil nil 'closed))
  7761. (when (and now-done-p org-log-done)
  7762. ;; It is now done, and it was not done before
  7763. (org-add-planning-info 'closed (org-current-time))
  7764. (if (and (not dolog) (eq 'note org-log-done))
  7765. (org-add-log-setup 'done state 'findpos 'note)))
  7766. (when (and state dolog)
  7767. ;; This is a non-nil state, and we need to log it
  7768. (org-add-log-setup 'state state 'findpos dolog)))
  7769. ;; Fixup tag positioning
  7770. (org-todo-trigger-tag-changes state)
  7771. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7772. (when org-provide-todo-statistics
  7773. (org-update-parent-todo-statistics))
  7774. (run-hooks 'org-after-todo-state-change-hook)
  7775. (if (and arg (not (member state org-done-keywords)))
  7776. (setq head (org-get-todo-sequence-head state)))
  7777. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7778. ;; Do we need to trigger a repeat?
  7779. (when now-done-p
  7780. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7781. ;; This is for the agenda, take a snapshot of the headline.
  7782. (save-match-data
  7783. (setq org-agenda-headline-snapshot-before-repeat
  7784. (org-get-heading))))
  7785. (org-auto-repeat-maybe state))
  7786. ;; Fixup cursor location if close to the keyword
  7787. (if (and (outline-on-heading-p)
  7788. (not (bolp))
  7789. (save-excursion (beginning-of-line 1)
  7790. (looking-at org-todo-line-regexp))
  7791. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7792. (progn
  7793. (goto-char (or (match-end 2) (match-end 1)))
  7794. (just-one-space)))
  7795. (when org-trigger-hook
  7796. (save-excursion
  7797. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7798. (defun org-block-todo-from-children-or-siblings (change-plist)
  7799. "Block turning an entry into a TODO, using the hierarchy.
  7800. This checks whether the current task should be blocked from state
  7801. changes. Such blocking occurs when:
  7802. 1. The task has children which are not all in a completed state.
  7803. 2. A task has a parent with the property :ORDERED:, and there
  7804. are siblings prior to the current task with incomplete
  7805. status."
  7806. (catch 'dont-block
  7807. ;; If this is not a todo state change, or if this entry is already DONE,
  7808. ;; do not block
  7809. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7810. (member (plist-get change-plist :from)
  7811. (cons 'done org-done-keywords))
  7812. (member (plist-get change-plist :to)
  7813. (cons 'todo org-not-done-keywords)))
  7814. (throw 'dont-block t))
  7815. ;; If this task has children, and any are undone, it's blocked
  7816. (save-excursion
  7817. (org-back-to-heading t)
  7818. (let ((this-level (funcall outline-level)))
  7819. (outline-next-heading)
  7820. (let ((child-level (funcall outline-level)))
  7821. (while (and (not (eobp))
  7822. (> child-level this-level))
  7823. ;; this todo has children, check whether they are all
  7824. ;; completed
  7825. (if (and (not (org-entry-is-done-p))
  7826. (org-entry-is-todo-p))
  7827. (throw 'dont-block nil))
  7828. (outline-next-heading)
  7829. (setq child-level (funcall outline-level))))))
  7830. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7831. ;; any previous siblings are undone, it's blocked
  7832. (save-excursion
  7833. (org-back-to-heading t)
  7834. (when (save-excursion
  7835. (ignore-errors
  7836. (outline-up-heading 1)
  7837. (org-entry-get (point) "ORDERED")))
  7838. (let* ((this-level (funcall outline-level))
  7839. (current-level this-level))
  7840. (while (and (not (bobp))
  7841. (= current-level this-level))
  7842. (outline-previous-heading)
  7843. (setq current-level (funcall outline-level))
  7844. (if (= current-level this-level)
  7845. ;; this todo has children, check whether they are all
  7846. ;; completed
  7847. (if (and (not (org-entry-is-done-p))
  7848. (org-entry-is-todo-p))
  7849. (throw 'dont-block nil)))))))
  7850. t)) ; don't block
  7851. (defun org-toggle-ordered-property ()
  7852. "Toggle the ORDERED property of the current entry."
  7853. (interactive)
  7854. (save-excursion
  7855. (org-back-to-heading)
  7856. (if (org-entry-get nil "ORDERED")
  7857. (progn
  7858. (org-delete-property "ORDERED")
  7859. (message "Subtasks can be completed in arbitrary order or parallel"))
  7860. (org-entry-put nil "ORDERED" "t")
  7861. (message "Subtasks must be completed in sequence"))))
  7862. (defun org-block-todo-from-checkboxes (change-plist)
  7863. "Block turning an entry into a TODO, using checkboxes.
  7864. This checks whether the current task should be blocked from state
  7865. changes because there are uncheckd boxes in this entry."
  7866. (catch 'dont-block
  7867. ;; If this is not a todo state change, or if this entry is already DONE,
  7868. ;; do not block
  7869. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7870. (member (plist-get change-plist :from)
  7871. (cons 'done org-done-keywords))
  7872. (member (plist-get change-plist :to)
  7873. (cons 'todo org-not-done-keywords)))
  7874. (throw 'dont-block t))
  7875. ;; If this task has checkboxes that are not checked, it's blocked
  7876. (save-excursion
  7877. (org-back-to-heading t)
  7878. (let ((beg (point)) end)
  7879. (outline-next-heading)
  7880. (setq end (point))
  7881. (goto-char beg)
  7882. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7883. end t)
  7884. (throw 'dont-block nil))))
  7885. t)) ; do not block
  7886. (defun org-update-parent-todo-statistics ()
  7887. "Update any statistics cookie in the parent of the current headline."
  7888. (interactive)
  7889. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7890. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7891. (catch 'exit
  7892. (save-excursion
  7893. (setq level (org-up-heading-safe))
  7894. (unless level
  7895. (throw 'exit nil))
  7896. (while (re-search-forward box-re (point-at-eol) t)
  7897. (setq cnt-all 0 cnt-done 0 cookie-present t)
  7898. (setq is-percent (match-end 2))
  7899. (save-match-data
  7900. (unless (outline-next-heading) (throw 'exit nil))
  7901. (while (looking-at org-todo-line-regexp)
  7902. (setq kwd (match-string 2))
  7903. (and kwd (setq cnt-all (1+ cnt-all)))
  7904. (and (member kwd org-done-keywords)
  7905. (setq cnt-done (1+ cnt-done)))
  7906. (condition-case nil
  7907. (org-forward-same-level 1)
  7908. (error (end-of-line 1)))))
  7909. (replace-match
  7910. (if is-percent
  7911. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7912. (format "[%d/%d]" cnt-done cnt-all))))
  7913. (when cookie-present
  7914. (run-hook-with-args 'org-after-todo-statistics-hook
  7915. cnt-done (- cnt-all cnt-done)))))))
  7916. (defvar org-after-todo-statistics-hook nil
  7917. "Hook that is called after a TODO statistics cookie has been updated.
  7918. Each function is called with two arguments: the number of not-done entries
  7919. and the number of done entries.
  7920. For example, the following function, when added to this hook, will switch
  7921. an entry to DONE when all children are done, and back to TODO when new
  7922. entries are set to a TODO status. Note that this hook is only called
  7923. when there is a statistics cookie in the headline!
  7924. (defun org-summary-todo (n-done n-not-done)
  7925. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7926. (let (org-log-done org-log-states) ; turn off logging
  7927. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7928. ")
  7929. (defun org-todo-trigger-tag-changes (state)
  7930. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7931. (let ((l org-todo-state-tags-triggers)
  7932. changes)
  7933. (when (or (not state) (equal state ""))
  7934. (setq changes (append changes (cdr (assoc "" l)))))
  7935. (when (and (stringp state) (> (length state) 0))
  7936. (setq changes (append changes (cdr (assoc state l)))))
  7937. (when (member state org-not-done-keywords)
  7938. (setq changes (append changes (cdr (assoc 'todo l)))))
  7939. (when (member state org-done-keywords)
  7940. (setq changes (append changes (cdr (assoc 'done l)))))
  7941. (dolist (c changes)
  7942. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7943. (defun org-local-logging (value)
  7944. "Get logging settings from a property VALUE."
  7945. (let* (words w a)
  7946. ;; directly set the variables, they are already local.
  7947. (setq org-log-done nil
  7948. org-log-repeat nil
  7949. org-todo-log-states nil)
  7950. (setq words (org-split-string value))
  7951. (while (setq w (pop words))
  7952. (cond
  7953. ((setq a (assoc w org-startup-options))
  7954. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7955. (set (nth 1 a) (nth 2 a))))
  7956. ((setq a (org-extract-log-state-settings w))
  7957. (and (member (car a) org-todo-keywords-1)
  7958. (push a org-todo-log-states)))))))
  7959. (defun org-get-todo-sequence-head (kwd)
  7960. "Return the head of the TODO sequence to which KWD belongs.
  7961. If KWD is not set, check if there is a text property remembering the
  7962. right sequence."
  7963. (let (p)
  7964. (cond
  7965. ((not kwd)
  7966. (or (get-text-property (point-at-bol) 'org-todo-head)
  7967. (progn
  7968. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7969. nil (point-at-eol)))
  7970. (get-text-property p 'org-todo-head))))
  7971. ((not (member kwd org-todo-keywords-1))
  7972. (car org-todo-keywords-1))
  7973. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7974. (defun org-fast-todo-selection ()
  7975. "Fast TODO keyword selection with single keys.
  7976. Returns the new TODO keyword, or nil if no state change should occur."
  7977. (let* ((fulltable org-todo-key-alist)
  7978. (done-keywords org-done-keywords) ;; needed for the faces.
  7979. (maxlen (apply 'max (mapcar
  7980. (lambda (x)
  7981. (if (stringp (car x)) (string-width (car x)) 0))
  7982. fulltable)))
  7983. (expert nil)
  7984. (fwidth (+ maxlen 3 1 3))
  7985. (ncol (/ (- (window-width) 4) fwidth))
  7986. tg cnt e c tbl
  7987. groups ingroup)
  7988. (save-excursion
  7989. (save-window-excursion
  7990. (if expert
  7991. (set-buffer (get-buffer-create " *Org todo*"))
  7992. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7993. (erase-buffer)
  7994. (org-set-local 'org-done-keywords done-keywords)
  7995. (setq tbl fulltable cnt 0)
  7996. (while (setq e (pop tbl))
  7997. (cond
  7998. ((equal e '(:startgroup))
  7999. (push '() groups) (setq ingroup t)
  8000. (when (not (= cnt 0))
  8001. (setq cnt 0)
  8002. (insert "\n"))
  8003. (insert "{ "))
  8004. ((equal e '(:endgroup))
  8005. (setq ingroup nil cnt 0)
  8006. (insert "}\n"))
  8007. (t
  8008. (setq tg (car e) c (cdr e))
  8009. (if ingroup (push tg (car groups)))
  8010. (setq tg (org-add-props tg nil 'face
  8011. (org-get-todo-face tg)))
  8012. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8013. (insert "[" c "] " tg (make-string
  8014. (- fwidth 4 (length tg)) ?\ ))
  8015. (when (= (setq cnt (1+ cnt)) ncol)
  8016. (insert "\n")
  8017. (if ingroup (insert " "))
  8018. (setq cnt 0)))))
  8019. (insert "\n")
  8020. (goto-char (point-min))
  8021. (if (not expert) (org-fit-window-to-buffer))
  8022. (message "[a-z..]:Set [SPC]:clear")
  8023. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8024. (cond
  8025. ((or (= c ?\C-g)
  8026. (and (= c ?q) (not (rassoc c fulltable))))
  8027. (setq quit-flag t))
  8028. ((= c ?\ ) nil)
  8029. ((setq e (rassoc c fulltable) tg (car e))
  8030. tg)
  8031. (t (setq quit-flag t)))))))
  8032. (defun org-entry-is-todo-p ()
  8033. (member (org-get-todo-state) org-not-done-keywords))
  8034. (defun org-entry-is-done-p ()
  8035. (member (org-get-todo-state) org-done-keywords))
  8036. (defun org-get-todo-state ()
  8037. (save-excursion
  8038. (org-back-to-heading t)
  8039. (and (looking-at org-todo-line-regexp)
  8040. (match-end 2)
  8041. (match-string 2))))
  8042. (defun org-at-date-range-p (&optional inactive-ok)
  8043. "Is the cursor inside a date range?"
  8044. (interactive)
  8045. (save-excursion
  8046. (catch 'exit
  8047. (let ((pos (point)))
  8048. (skip-chars-backward "^[<\r\n")
  8049. (skip-chars-backward "<[")
  8050. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8051. (>= (match-end 0) pos)
  8052. (throw 'exit t))
  8053. (skip-chars-backward "^<[\r\n")
  8054. (skip-chars-backward "<[")
  8055. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8056. (>= (match-end 0) pos)
  8057. (throw 'exit t)))
  8058. nil)))
  8059. (defun org-get-repeat ()
  8060. "Check if there is a deadline/schedule with repeater in this entry."
  8061. (save-match-data
  8062. (save-excursion
  8063. (org-back-to-heading t)
  8064. (if (re-search-forward
  8065. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8066. (match-string 1)))))
  8067. (defvar org-last-changed-timestamp)
  8068. (defvar org-last-inserted-timestamp)
  8069. (defvar org-log-post-message)
  8070. (defvar org-log-note-purpose)
  8071. (defvar org-log-note-how)
  8072. (defvar org-log-note-extra)
  8073. (defun org-auto-repeat-maybe (done-word)
  8074. "Check if the current headline contains a repeated deadline/schedule.
  8075. If yes, set TODO state back to what it was and change the base date
  8076. of repeating deadline/scheduled time stamps to new date.
  8077. This function is run automatically after each state change to a DONE state."
  8078. ;; last-state is dynamically scoped into this function
  8079. (let* ((repeat (org-get-repeat))
  8080. (aa (assoc last-state org-todo-kwd-alist))
  8081. (interpret (nth 1 aa))
  8082. (head (nth 2 aa))
  8083. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8084. (msg "Entry repeats: ")
  8085. (org-log-done nil)
  8086. (org-todo-log-states nil)
  8087. (nshiftmax 10) (nshift 0)
  8088. re type n what ts time)
  8089. (when repeat
  8090. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8091. (org-todo (if (eq interpret 'type) last-state head))
  8092. (when org-log-repeat
  8093. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8094. (memq 'org-add-log-note post-command-hook))
  8095. ;; OK, we are already setup for some record
  8096. (if (eq org-log-repeat 'note)
  8097. ;; make sure we take a note, not only a time stamp
  8098. (setq org-log-note-how 'note))
  8099. ;; Set up for taking a record
  8100. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8101. 'findpos org-log-repeat)))
  8102. (org-back-to-heading t)
  8103. (org-add-planning-info nil nil 'closed)
  8104. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8105. org-deadline-time-regexp "\\)\\|\\("
  8106. org-ts-regexp "\\)"))
  8107. (while (re-search-forward
  8108. re (save-excursion (outline-next-heading) (point)) t)
  8109. (setq type (if (match-end 1) org-scheduled-string
  8110. (if (match-end 3) org-deadline-string "Plain:"))
  8111. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8112. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8113. (setq n (string-to-number (match-string 2 ts))
  8114. what (match-string 3 ts))
  8115. (if (equal what "w") (setq n (* n 7) what "d"))
  8116. ;; Preparation, see if we need to modify the start date for the change
  8117. (when (match-end 1)
  8118. (setq time (save-match-data (org-time-string-to-time ts)))
  8119. (cond
  8120. ((equal (match-string 1 ts) ".")
  8121. ;; Shift starting date to today
  8122. (org-timestamp-change
  8123. (- (time-to-days (current-time)) (time-to-days time))
  8124. 'day))
  8125. ((equal (match-string 1 ts) "+")
  8126. (while (or (= nshift 0)
  8127. (<= (time-to-days time) (time-to-days (current-time))))
  8128. (when (= (incf nshift) nshiftmax)
  8129. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8130. (error "Abort")))
  8131. (org-timestamp-change n (cdr (assoc what whata)))
  8132. (org-at-timestamp-p t)
  8133. (setq ts (match-string 1))
  8134. (setq time (save-match-data (org-time-string-to-time ts))))
  8135. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8136. ;; rematch, so that we have everything in place for the real shift
  8137. (org-at-timestamp-p t)
  8138. (setq ts (match-string 1))
  8139. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8140. (org-timestamp-change n (cdr (assoc what whata)))
  8141. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8142. (setq org-log-post-message msg)
  8143. (message "%s" msg))))
  8144. (defun org-show-todo-tree (arg)
  8145. "Make a compact tree which shows all headlines marked with TODO.
  8146. The tree will show the lines where the regexp matches, and all higher
  8147. headlines above the match.
  8148. With a \\[universal-argument] prefix, also show the DONE entries.
  8149. With a numeric prefix N, construct a sparse tree for the Nth element
  8150. of `org-todo-keywords-1'."
  8151. (interactive "P")
  8152. (let ((case-fold-search nil)
  8153. (kwd-re
  8154. (cond ((null arg) org-not-done-regexp)
  8155. ((equal arg '(4))
  8156. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8157. (mapcar 'list org-todo-keywords-1))))
  8158. (concat "\\("
  8159. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8160. "\\)\\>")))
  8161. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8162. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8163. org-todo-keywords-1)))
  8164. (t (error "Invalid prefix argument: %s" arg)))))
  8165. (message "%d TODO entries found"
  8166. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8167. (defun org-deadline (&optional remove time)
  8168. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8169. With argument REMOVE, remove any deadline from the item.
  8170. When TIME is set, it should be an internal time specification, and the
  8171. scheduling will use the corresponding date."
  8172. (interactive "P")
  8173. (if remove
  8174. (progn
  8175. (org-remove-timestamp-with-keyword org-deadline-string)
  8176. (message "Item no longer has a deadline."))
  8177. (if (org-get-repeat)
  8178. (error "Cannot change deadline on task with repeater, please do that by hand")
  8179. (org-add-planning-info 'deadline time 'closed)
  8180. (message "Deadline on %s" org-last-inserted-timestamp))))
  8181. (defun org-schedule (&optional remove time)
  8182. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8183. With argument REMOVE, remove any scheduling date from the item.
  8184. When TIME is set, it should be an internal time specification, and the
  8185. scheduling will use the corresponding date."
  8186. (interactive "P")
  8187. (if remove
  8188. (progn
  8189. (org-remove-timestamp-with-keyword org-scheduled-string)
  8190. (message "Item is no longer scheduled."))
  8191. (if (org-get-repeat)
  8192. (error "Cannot reschedule task with repeater, please do that by hand")
  8193. (org-add-planning-info 'scheduled time 'closed)
  8194. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8195. (defun org-get-scheduled-time (pom &optional inherit)
  8196. "Get the scheduled time as a time tuple, of a format suitable
  8197. for calling org-schedule with, or if there is no scheduling,
  8198. returns nil."
  8199. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8200. (when time
  8201. (apply 'encode-time (org-parse-time-string time)))))
  8202. (defun org-get-deadline-time (pom &optional inherit)
  8203. "Get the deadine as a time tuple, of a format suitable for
  8204. calling org-deadlin with, or if there is no scheduling, returns
  8205. nil."
  8206. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8207. (when time
  8208. (apply 'encode-time (org-parse-time-string time)))))
  8209. (defun org-remove-timestamp-with-keyword (keyword)
  8210. "Remove all time stamps with KEYWORD in the current entry."
  8211. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8212. beg)
  8213. (save-excursion
  8214. (org-back-to-heading t)
  8215. (setq beg (point))
  8216. (org-end-of-subtree t t)
  8217. (while (re-search-backward re beg t)
  8218. (replace-match "")
  8219. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8220. (equal (char-before) ?\ ))
  8221. (backward-delete-char 1)
  8222. (if (string-match "^[ \t]*$" (buffer-substring
  8223. (point-at-bol) (point-at-eol)))
  8224. (delete-region (point-at-bol)
  8225. (min (point-max) (1+ (point-at-eol))))))))))
  8226. (defun org-add-planning-info (what &optional time &rest remove)
  8227. "Insert new timestamp with keyword in the line directly after the headline.
  8228. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8229. If non is given, the user is prompted for a date.
  8230. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8231. be removed."
  8232. (interactive)
  8233. (let (org-time-was-given org-end-time-was-given ts
  8234. end default-time default-input)
  8235. (when (and (not time) (memq what '(scheduled deadline)))
  8236. ;; Try to get a default date/time from existing timestamp
  8237. (save-excursion
  8238. (org-back-to-heading t)
  8239. (setq end (save-excursion (outline-next-heading) (point)))
  8240. (when (re-search-forward (if (eq what 'scheduled)
  8241. org-scheduled-time-regexp
  8242. org-deadline-time-regexp)
  8243. end t)
  8244. (setq ts (match-string 1)
  8245. default-time
  8246. (apply 'encode-time (org-parse-time-string ts))
  8247. default-input (and ts (org-get-compact-tod ts))))))
  8248. (when what
  8249. ;; If necessary, get the time from the user
  8250. (setq time (or time (org-read-date nil 'to-time nil nil
  8251. default-time default-input))))
  8252. (when (and org-insert-labeled-timestamps-at-point
  8253. (member what '(scheduled deadline)))
  8254. (insert
  8255. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8256. (org-insert-time-stamp time org-time-was-given
  8257. nil nil nil (list org-end-time-was-given))
  8258. (setq what nil))
  8259. (save-excursion
  8260. (save-restriction
  8261. (let (col list elt ts buffer-invisibility-spec)
  8262. (org-back-to-heading t)
  8263. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8264. (goto-char (match-end 1))
  8265. (setq col (current-column))
  8266. (goto-char (match-end 0))
  8267. (if (eobp) (insert "\n") (forward-char 1))
  8268. (if (and (not (looking-at outline-regexp))
  8269. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8270. "[^\r\n]*"))
  8271. (not (equal (match-string 1) org-clock-string)))
  8272. (narrow-to-region (match-beginning 0) (match-end 0))
  8273. (insert-before-markers "\n")
  8274. (backward-char 1)
  8275. (narrow-to-region (point) (point))
  8276. (and org-adapt-indentation (org-indent-to-column col)))
  8277. ;; Check if we have to remove something.
  8278. (setq list (cons what remove))
  8279. (while list
  8280. (setq elt (pop list))
  8281. (goto-char (point-min))
  8282. (when (or (and (eq elt 'scheduled)
  8283. (re-search-forward org-scheduled-time-regexp nil t))
  8284. (and (eq elt 'deadline)
  8285. (re-search-forward org-deadline-time-regexp nil t))
  8286. (and (eq elt 'closed)
  8287. (re-search-forward org-closed-time-regexp nil t)))
  8288. (replace-match "")
  8289. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8290. (if (looking-at " +") (replace-match ""))))
  8291. (goto-char (point-max))
  8292. (when what
  8293. (insert
  8294. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8295. (cond ((eq what 'scheduled) org-scheduled-string)
  8296. ((eq what 'deadline) org-deadline-string)
  8297. ((eq what 'closed) org-closed-string))
  8298. " ")
  8299. (setq ts (org-insert-time-stamp
  8300. time
  8301. (or org-time-was-given
  8302. (and (eq what 'closed) org-log-done-with-time))
  8303. (eq what 'closed)
  8304. nil nil (list org-end-time-was-given)))
  8305. (end-of-line 1))
  8306. (goto-char (point-min))
  8307. (widen)
  8308. (if (and (looking-at "[ \t]+\n")
  8309. (equal (char-before) ?\n))
  8310. (delete-region (1- (point)) (point-at-eol)))
  8311. ts)))))
  8312. (defvar org-log-note-marker (make-marker))
  8313. (defvar org-log-note-purpose nil)
  8314. (defvar org-log-note-state nil)
  8315. (defvar org-log-note-how nil)
  8316. (defvar org-log-note-extra nil)
  8317. (defvar org-log-note-window-configuration nil)
  8318. (defvar org-log-note-return-to (make-marker))
  8319. (defvar org-log-post-message nil
  8320. "Message to be displayed after a log note has been stored.
  8321. The auto-repeater uses this.")
  8322. (defun org-add-note ()
  8323. "Add a note to the current entry.
  8324. This is done in the same way as adding a state change note."
  8325. (interactive)
  8326. (org-add-log-setup 'note nil 'findpos nil))
  8327. (defvar org-property-end-re)
  8328. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8329. "Set up the post command hook to take a note.
  8330. If this is about to TODO state change, the new state is expected in STATE.
  8331. When FINDPOS is non-nil, find the correct position for the note in
  8332. the current entry. If not, assume that it can be inserted at point.
  8333. HOW is an indicator what kind of note should be created.
  8334. EXTRA is additional text that will be inserted into the notes buffer."
  8335. (save-restriction
  8336. (save-excursion
  8337. (when findpos
  8338. (org-back-to-heading t)
  8339. (narrow-to-region (point) (save-excursion
  8340. (outline-next-heading) (point)))
  8341. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8342. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8343. "[^\r\n]*\\)?"))
  8344. (goto-char (match-end 0))
  8345. (when (and org-log-state-notes-insert-after-drawers
  8346. (save-excursion
  8347. (forward-line) (looking-at org-drawer-regexp)))
  8348. (progn (forward-line)
  8349. (while (looking-at org-drawer-regexp)
  8350. (goto-char (match-end 0))
  8351. (re-search-forward org-property-end-re (point-max) t)
  8352. (forward-line))
  8353. (forward-line -1)))
  8354. (unless org-log-states-order-reversed
  8355. (and (= (char-after) ?\n) (forward-char 1))
  8356. (org-skip-over-state-notes)
  8357. (skip-chars-backward " \t\n\r")))
  8358. (move-marker org-log-note-marker (point))
  8359. (setq org-log-note-purpose purpose
  8360. org-log-note-state state
  8361. org-log-note-how how
  8362. org-log-note-extra extra)
  8363. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8364. (defun org-skip-over-state-notes ()
  8365. "Skip past the list of State notes in an entry."
  8366. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8367. (while (looking-at "[ \t]*- State")
  8368. (condition-case nil
  8369. (org-next-item)
  8370. (error (org-end-of-item)))))
  8371. (defun org-add-log-note (&optional purpose)
  8372. "Pop up a window for taking a note, and add this note later at point."
  8373. (remove-hook 'post-command-hook 'org-add-log-note)
  8374. (setq org-log-note-window-configuration (current-window-configuration))
  8375. (delete-other-windows)
  8376. (move-marker org-log-note-return-to (point))
  8377. (switch-to-buffer (marker-buffer org-log-note-marker))
  8378. (goto-char org-log-note-marker)
  8379. (org-switch-to-buffer-other-window "*Org Note*")
  8380. (erase-buffer)
  8381. (if (memq org-log-note-how '(time state))
  8382. (let (current-prefix-arg) (org-store-log-note))
  8383. (let ((org-inhibit-startup t)) (org-mode))
  8384. (insert (format "# Insert note for %s.
  8385. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8386. (cond
  8387. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8388. ((eq org-log-note-purpose 'done) "closed todo item")
  8389. ((eq org-log-note-purpose 'state)
  8390. (format "state change to \"%s\"" org-log-note-state))
  8391. ((eq org-log-note-purpose 'note)
  8392. "this entry")
  8393. (t (error "This should not happen")))))
  8394. (if org-log-note-extra (insert org-log-note-extra))
  8395. (org-set-local 'org-finish-function 'org-store-log-note)))
  8396. (defvar org-note-abort nil) ; dynamically scoped
  8397. (defun org-store-log-note ()
  8398. "Finish taking a log note, and insert it to where it belongs."
  8399. (let ((txt (buffer-string))
  8400. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8401. lines ind)
  8402. (kill-buffer (current-buffer))
  8403. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8404. (setq txt (replace-match "" t t txt)))
  8405. (if (string-match "\\s-+\\'" txt)
  8406. (setq txt (replace-match "" t t txt)))
  8407. (setq lines (org-split-string txt "\n"))
  8408. (when (and note (string-match "\\S-" note))
  8409. (setq note
  8410. (org-replace-escapes
  8411. note
  8412. (list (cons "%u" (user-login-name))
  8413. (cons "%U" user-full-name)
  8414. (cons "%t" (format-time-string
  8415. (org-time-stamp-format 'long 'inactive)
  8416. (current-time)))
  8417. (cons "%s" (if org-log-note-state
  8418. (concat "\"" org-log-note-state "\"")
  8419. "")))))
  8420. (if lines (setq note (concat note " \\\\")))
  8421. (push note lines))
  8422. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8423. (when lines
  8424. (save-excursion
  8425. (set-buffer (marker-buffer org-log-note-marker))
  8426. (save-excursion
  8427. (goto-char org-log-note-marker)
  8428. (move-marker org-log-note-marker nil)
  8429. (end-of-line 1)
  8430. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8431. (indent-relative nil)
  8432. (insert "- " (pop lines))
  8433. (org-indent-line-function)
  8434. (beginning-of-line 1)
  8435. (looking-at "[ \t]*")
  8436. (setq ind (concat (match-string 0) " "))
  8437. (end-of-line 1)
  8438. (while lines (insert "\n" ind (pop lines)))))))
  8439. (set-window-configuration org-log-note-window-configuration)
  8440. (with-current-buffer (marker-buffer org-log-note-return-to)
  8441. (goto-char org-log-note-return-to))
  8442. (move-marker org-log-note-return-to nil)
  8443. (and org-log-post-message (message "%s" org-log-post-message)))
  8444. (defun org-sparse-tree (&optional arg)
  8445. "Create a sparse tree, prompt for the details.
  8446. This command can create sparse trees. You first need to select the type
  8447. of match used to create the tree:
  8448. t Show entries with a specific TODO keyword.
  8449. T Show entries selected by a tags match.
  8450. p Enter a property name and its value (both with completion on existing
  8451. names/values) and show entries with that property.
  8452. r Show entries matching a regular expression
  8453. d Show deadlines due within `org-deadline-warning-days'."
  8454. (interactive "P")
  8455. (let (ans kwd value)
  8456. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8457. (setq ans (read-char-exclusive))
  8458. (cond
  8459. ((equal ans ?d)
  8460. (call-interactively 'org-check-deadlines))
  8461. ((equal ans ?b)
  8462. (call-interactively 'org-check-before-date))
  8463. ((equal ans ?t)
  8464. (org-show-todo-tree '(4)))
  8465. ((equal ans ?T)
  8466. (call-interactively 'org-tags-sparse-tree))
  8467. ((member ans '(?p ?P))
  8468. (setq kwd (org-ido-completing-read "Property: "
  8469. (mapcar 'list (org-buffer-property-keys))))
  8470. (setq value (org-ido-completing-read "Value: "
  8471. (mapcar 'list (org-property-values kwd))))
  8472. (unless (string-match "\\`{.*}\\'" value)
  8473. (setq value (concat "\"" value "\"")))
  8474. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8475. ((member ans '(?r ?R ?/))
  8476. (call-interactively 'org-occur))
  8477. (t (error "No such sparse tree command \"%c\"" ans)))))
  8478. (defvar org-occur-highlights nil
  8479. "List of overlays used for occur matches.")
  8480. (make-variable-buffer-local 'org-occur-highlights)
  8481. (defvar org-occur-parameters nil
  8482. "Parameters of the active org-occur calls.
  8483. This is a list, each call to org-occur pushes as cons cell,
  8484. containing the regular expression and the callback, onto the list.
  8485. The list can contain several entries if `org-occur' has been called
  8486. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8487. will only contain one set of parameters. When the highlights are
  8488. removed (for example with `C-c C-c', or with the next edit (depending
  8489. on `org-remove-highlights-with-change'), this variable is emptied
  8490. as well.")
  8491. (make-variable-buffer-local 'org-occur-parameters)
  8492. (defun org-occur (regexp &optional keep-previous callback)
  8493. "Make a compact tree which shows all matches of REGEXP.
  8494. The tree will show the lines where the regexp matches, and all higher
  8495. headlines above the match. It will also show the heading after the match,
  8496. to make sure editing the matching entry is easy.
  8497. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8498. call to `org-occur' will be kept, to allow stacking of calls to this
  8499. command.
  8500. If CALLBACK is non-nil, it is a function which is called to confirm
  8501. that the match should indeed be shown."
  8502. (interactive "sRegexp: \nP")
  8503. (unless keep-previous
  8504. (org-remove-occur-highlights nil nil t))
  8505. (push (cons regexp callback) org-occur-parameters)
  8506. (let ((cnt 0))
  8507. (save-excursion
  8508. (goto-char (point-min))
  8509. (if (or (not keep-previous) ; do not want to keep
  8510. (not org-occur-highlights)) ; no previous matches
  8511. ;; hide everything
  8512. (org-overview))
  8513. (while (re-search-forward regexp nil t)
  8514. (when (or (not callback)
  8515. (save-match-data (funcall callback)))
  8516. (setq cnt (1+ cnt))
  8517. (when org-highlight-sparse-tree-matches
  8518. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8519. (org-show-context 'occur-tree))))
  8520. (when org-remove-highlights-with-change
  8521. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8522. nil 'local))
  8523. (unless org-sparse-tree-open-archived-trees
  8524. (org-hide-archived-subtrees (point-min) (point-max)))
  8525. (run-hooks 'org-occur-hook)
  8526. (if (interactive-p)
  8527. (message "%d match(es) for regexp %s" cnt regexp))
  8528. cnt))
  8529. (defun org-show-context (&optional key)
  8530. "Make sure point and context and visible.
  8531. How much context is shown depends upon the variables
  8532. `org-show-hierarchy-above', `org-show-following-heading'. and
  8533. `org-show-siblings'."
  8534. (let ((heading-p (org-on-heading-p t))
  8535. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8536. (following-p (org-get-alist-option org-show-following-heading key))
  8537. (entry-p (org-get-alist-option org-show-entry-below key))
  8538. (siblings-p (org-get-alist-option org-show-siblings key)))
  8539. (catch 'exit
  8540. ;; Show heading or entry text
  8541. (if (and heading-p (not entry-p))
  8542. (org-flag-heading nil) ; only show the heading
  8543. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8544. (org-show-hidden-entry))) ; show entire entry
  8545. (when following-p
  8546. ;; Show next sibling, or heading below text
  8547. (save-excursion
  8548. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8549. (org-flag-heading nil))))
  8550. (when siblings-p (org-show-siblings))
  8551. (when hierarchy-p
  8552. ;; show all higher headings, possibly with siblings
  8553. (save-excursion
  8554. (while (and (condition-case nil
  8555. (progn (org-up-heading-all 1) t)
  8556. (error nil))
  8557. (not (bobp)))
  8558. (org-flag-heading nil)
  8559. (when siblings-p (org-show-siblings))))))))
  8560. (defun org-reveal (&optional siblings)
  8561. "Show current entry, hierarchy above it, and the following headline.
  8562. This can be used to show a consistent set of context around locations
  8563. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8564. not t for the search context.
  8565. With optional argument SIBLINGS, on each level of the hierarchy all
  8566. siblings are shown. This repairs the tree structure to what it would
  8567. look like when opened with hierarchical calls to `org-cycle'."
  8568. (interactive "P")
  8569. (let ((org-show-hierarchy-above t)
  8570. (org-show-following-heading t)
  8571. (org-show-siblings (if siblings t org-show-siblings)))
  8572. (org-show-context nil)))
  8573. (defun org-highlight-new-match (beg end)
  8574. "Highlight from BEG to END and mark the highlight is an occur headline."
  8575. (let ((ov (org-make-overlay beg end)))
  8576. (org-overlay-put ov 'face 'secondary-selection)
  8577. (push ov org-occur-highlights)))
  8578. (defun org-remove-occur-highlights (&optional beg end noremove)
  8579. "Remove the occur highlights from the buffer.
  8580. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8581. from the `before-change-functions' in the current buffer."
  8582. (interactive)
  8583. (unless org-inhibit-highlight-removal
  8584. (mapc 'org-delete-overlay org-occur-highlights)
  8585. (setq org-occur-highlights nil)
  8586. (setq org-occur-parameters nil)
  8587. (unless noremove
  8588. (remove-hook 'before-change-functions
  8589. 'org-remove-occur-highlights 'local))))
  8590. ;;;; Priorities
  8591. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8592. "Regular expression matching the priority indicator.")
  8593. (defvar org-remove-priority-next-time nil)
  8594. (defun org-priority-up ()
  8595. "Increase the priority of the current item."
  8596. (interactive)
  8597. (org-priority 'up))
  8598. (defun org-priority-down ()
  8599. "Decrease the priority of the current item."
  8600. (interactive)
  8601. (org-priority 'down))
  8602. (defun org-priority (&optional action)
  8603. "Change the priority of an item by ARG.
  8604. ACTION can be `set', `up', `down', or a character."
  8605. (interactive)
  8606. (setq action (or action 'set))
  8607. (let (current new news have remove)
  8608. (save-excursion
  8609. (org-back-to-heading)
  8610. (if (looking-at org-priority-regexp)
  8611. (setq current (string-to-char (match-string 2))
  8612. have t)
  8613. (setq current org-default-priority))
  8614. (cond
  8615. ((or (eq action 'set)
  8616. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8617. (if (not (eq action 'set))
  8618. (setq new action)
  8619. (message "Priority %c-%c, SPC to remove: "
  8620. org-highest-priority org-lowest-priority)
  8621. (setq new (read-char-exclusive)))
  8622. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8623. (= (upcase org-lowest-priority) org-lowest-priority))
  8624. (setq new (upcase new)))
  8625. (cond ((equal new ?\ ) (setq remove t))
  8626. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8627. (error "Priority must be between `%c' and `%c'"
  8628. org-highest-priority org-lowest-priority))))
  8629. ((eq action 'up)
  8630. (if (and (not have) (eq last-command this-command))
  8631. (setq new org-lowest-priority)
  8632. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8633. org-default-priority (1- current)))))
  8634. ((eq action 'down)
  8635. (if (and (not have) (eq last-command this-command))
  8636. (setq new org-highest-priority)
  8637. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8638. org-default-priority (1+ current)))))
  8639. (t (error "Invalid action")))
  8640. (if (or (< (upcase new) org-highest-priority)
  8641. (> (upcase new) org-lowest-priority))
  8642. (setq remove t))
  8643. (setq news (format "%c" new))
  8644. (if have
  8645. (if remove
  8646. (replace-match "" t t nil 1)
  8647. (replace-match news t t nil 2))
  8648. (if remove
  8649. (error "No priority cookie found in line")
  8650. (looking-at org-todo-line-regexp)
  8651. (if (match-end 2)
  8652. (progn
  8653. (goto-char (match-end 2))
  8654. (insert " [#" news "]"))
  8655. (goto-char (match-beginning 3))
  8656. (insert "[#" news "] ")))))
  8657. (org-preserve-lc (org-set-tags nil 'align))
  8658. (if remove
  8659. (message "Priority removed")
  8660. (message "Priority of current item set to %s" news))))
  8661. (defun org-get-priority (s)
  8662. "Find priority cookie and return priority."
  8663. (save-match-data
  8664. (if (not (string-match org-priority-regexp s))
  8665. (* 1000 (- org-lowest-priority org-default-priority))
  8666. (* 1000 (- org-lowest-priority
  8667. (string-to-char (match-string 2 s)))))))
  8668. ;;;; Tags
  8669. (defvar org-agenda-archives-mode)
  8670. (defun org-scan-tags (action matcher &optional todo-only)
  8671. "Scan headline tags with inheritance and produce output ACTION.
  8672. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8673. or `agenda' to produce an entry list for an agenda view. It can also be
  8674. a Lisp form or a function that should be called at each matched headline, in
  8675. this case the return value is a list of all return values from these calls.
  8676. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8677. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8678. only lines with a TODO keyword are included in the output."
  8679. (require 'org-agenda)
  8680. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8681. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8682. (org-re
  8683. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8684. (props (list 'face 'default
  8685. 'done-face 'org-done
  8686. 'undone-face 'default
  8687. 'mouse-face 'highlight
  8688. 'org-not-done-regexp org-not-done-regexp
  8689. 'org-todo-regexp org-todo-regexp
  8690. 'keymap org-agenda-keymap
  8691. 'help-echo
  8692. (format "mouse-2 or RET jump to org file %s"
  8693. (abbreviate-file-name
  8694. (or (buffer-file-name (buffer-base-buffer))
  8695. (buffer-name (buffer-base-buffer)))))))
  8696. (case-fold-search nil)
  8697. lspos tags tags-list
  8698. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8699. (llast 0) rtn rtn1 level category i txt
  8700. todo marker entry priority)
  8701. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8702. (setq action (list 'lambda nil action)))
  8703. (save-excursion
  8704. (goto-char (point-min))
  8705. (when (eq action 'sparse-tree)
  8706. (org-overview)
  8707. (org-remove-occur-highlights))
  8708. (while (re-search-forward re nil t)
  8709. (catch :skip
  8710. (setq todo (if (match-end 1) (match-string 2))
  8711. tags (if (match-end 4) (match-string 4)))
  8712. (goto-char (setq lspos (1+ (match-beginning 0))))
  8713. (setq level (org-reduced-level (funcall outline-level))
  8714. category (org-get-category))
  8715. (setq i llast llast level)
  8716. ;; remove tag lists from same and sublevels
  8717. (while (>= i level)
  8718. (when (setq entry (assoc i tags-alist))
  8719. (setq tags-alist (delete entry tags-alist)))
  8720. (setq i (1- i)))
  8721. ;; add the next tags
  8722. (when tags
  8723. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8724. tags-alist
  8725. (cons (cons level tags) tags-alist)))
  8726. ;; compile tags for current headline
  8727. (setq tags-list
  8728. (if org-use-tag-inheritance
  8729. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8730. tags))
  8731. (when org-use-tag-inheritance
  8732. (setcdr (car tags-alist)
  8733. (mapcar (lambda (x)
  8734. (setq x (copy-sequence x))
  8735. (org-add-prop-inherited x))
  8736. (cdar tags-alist))))
  8737. (when (and tags org-use-tag-inheritance
  8738. (not (eq t org-use-tag-inheritance)))
  8739. ;; selective inheritance, remove uninherited ones
  8740. (setcdr (car tags-alist)
  8741. (org-remove-uniherited-tags (cdar tags-alist))))
  8742. (when (and (or (not todo-only)
  8743. (and (member todo org-not-done-keywords)
  8744. (or (not org-agenda-tags-todo-honor-ignore-options)
  8745. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8746. (let ((case-fold-search t)) (eval matcher))
  8747. (or
  8748. (not (member org-archive-tag tags-list))
  8749. ;; we have an archive tag, should we use this anyway?
  8750. (or (not org-agenda-skip-archived-trees)
  8751. (and (eq action 'agenda) org-agenda-archives-mode))))
  8752. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8753. ;; select this headline
  8754. (cond
  8755. ((eq action 'sparse-tree)
  8756. (and org-highlight-sparse-tree-matches
  8757. (org-get-heading) (match-end 0)
  8758. (org-highlight-new-match
  8759. (match-beginning 0) (match-beginning 1)))
  8760. (org-show-context 'tags-tree))
  8761. ((eq action 'agenda)
  8762. (setq txt (org-format-agenda-item
  8763. ""
  8764. (concat
  8765. (if org-tags-match-list-sublevels
  8766. (make-string (1- level) ?.) "")
  8767. (org-get-heading))
  8768. category (org-get-tags-at))
  8769. priority (org-get-priority txt))
  8770. (goto-char lspos)
  8771. (setq marker (org-agenda-new-marker))
  8772. (org-add-props txt props
  8773. 'org-marker marker 'org-hd-marker marker 'org-category category
  8774. 'priority priority 'type "tagsmatch")
  8775. (push txt rtn))
  8776. ((functionp action)
  8777. (save-excursion
  8778. (setq rtn1 (funcall action))
  8779. (push rtn1 rtn))
  8780. (goto-char (point-at-eol)))
  8781. (t (error "Invalid action")))
  8782. ;; if we are to skip sublevels, jump to end of subtree
  8783. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8784. (when (and (eq action 'sparse-tree)
  8785. (not org-sparse-tree-open-archived-trees))
  8786. (org-hide-archived-subtrees (point-min) (point-max)))
  8787. (nreverse rtn)))
  8788. (defun org-remove-uniherited-tags (tags)
  8789. "Remove all tags that are not inherited from the list TAGS."
  8790. (cond
  8791. ((eq org-use-tag-inheritance t)
  8792. (if org-tags-exclude-from-inheritance
  8793. (org-delete-all org-tags-exclude-from-inheritance tags)
  8794. tags))
  8795. ((not org-use-tag-inheritance) nil)
  8796. ((stringp org-use-tag-inheritance)
  8797. (delq nil (mapcar
  8798. (lambda (x)
  8799. (if (and (string-match org-use-tag-inheritance x)
  8800. (not (member x org-tags-exclude-from-inheritance)))
  8801. x nil))
  8802. tags)))
  8803. ((listp org-use-tag-inheritance)
  8804. (delq nil (mapcar
  8805. (lambda (x)
  8806. (if (member x org-use-tag-inheritance) x nil))
  8807. tags)))))
  8808. (defvar todo-only) ;; dynamically scoped
  8809. (defun org-tags-sparse-tree (&optional todo-only match)
  8810. "Create a sparse tree according to tags string MATCH.
  8811. MATCH can contain positive and negative selection of tags, like
  8812. \"+WORK+URGENT-WITHBOSS\".
  8813. If optional argument TODO-ONLY is non-nil, only select lines that are
  8814. also TODO lines."
  8815. (interactive "P")
  8816. (org-prepare-agenda-buffers (list (current-buffer)))
  8817. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8818. (defvar org-cached-props nil)
  8819. (defun org-cached-entry-get (pom property)
  8820. (if (or (eq t org-use-property-inheritance)
  8821. (and (stringp org-use-property-inheritance)
  8822. (string-match org-use-property-inheritance property))
  8823. (and (listp org-use-property-inheritance)
  8824. (member property org-use-property-inheritance)))
  8825. ;; Caching is not possible, check it directly
  8826. (org-entry-get pom property 'inherit)
  8827. ;; Get all properties, so that we can do complicated checks easily
  8828. (cdr (assoc property (or org-cached-props
  8829. (setq org-cached-props
  8830. (org-entry-properties pom)))))))
  8831. (defun org-global-tags-completion-table (&optional files)
  8832. "Return the list of all tags in all agenda buffer/files."
  8833. (save-excursion
  8834. (org-uniquify
  8835. (delq nil
  8836. (apply 'append
  8837. (mapcar
  8838. (lambda (file)
  8839. (set-buffer (find-file-noselect file))
  8840. (append (org-get-buffer-tags)
  8841. (mapcar (lambda (x) (if (stringp (car-safe x))
  8842. (list (car-safe x)) nil))
  8843. org-tag-alist)))
  8844. (if (and files (car files))
  8845. files
  8846. (org-agenda-files))))))))
  8847. (defun org-make-tags-matcher (match)
  8848. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8849. ;; todo-only is scoped dynamically into this function, and the function
  8850. ;; may change it if the matcher asks for it.
  8851. (unless match
  8852. ;; Get a new match request, with completion
  8853. (let ((org-last-tags-completion-table
  8854. (org-global-tags-completion-table)))
  8855. (setq match (org-completing-read
  8856. "Match: " 'org-tags-completion-function nil nil nil
  8857. 'org-tags-history))))
  8858. ;; Parse the string and create a lisp form
  8859. (let ((match0 match)
  8860. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8861. minus tag mm
  8862. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8863. orterms term orlist re-p str-p level-p level-op time-p
  8864. prop-p pn pv po cat-p gv rest)
  8865. (if (string-match "/+" match)
  8866. ;; match contains also a todo-matching request
  8867. (progn
  8868. (setq tagsmatch (substring match 0 (match-beginning 0))
  8869. todomatch (substring match (match-end 0)))
  8870. (if (string-match "^!" todomatch)
  8871. (setq todo-only t todomatch (substring todomatch 1)))
  8872. (if (string-match "^\\s-*$" todomatch)
  8873. (setq todomatch nil)))
  8874. ;; only matching tags
  8875. (setq tagsmatch match todomatch nil))
  8876. ;; Make the tags matcher
  8877. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8878. (setq tagsmatcher t)
  8879. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8880. (while (setq term (pop orterms))
  8881. (while (and (equal (substring term -1) "\\") orterms)
  8882. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8883. (while (string-match re term)
  8884. (setq rest (substring term (match-end 0))
  8885. minus (and (match-end 1)
  8886. (equal (match-string 1 term) "-"))
  8887. tag (match-string 2 term)
  8888. re-p (equal (string-to-char tag) ?{)
  8889. level-p (match-end 4)
  8890. prop-p (match-end 5)
  8891. mm (cond
  8892. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8893. (level-p
  8894. (setq level-op (org-op-to-function (match-string 3 term)))
  8895. `(,level-op level ,(string-to-number
  8896. (match-string 4 term))))
  8897. (prop-p
  8898. (setq pn (match-string 5 term)
  8899. po (match-string 6 term)
  8900. pv (match-string 7 term)
  8901. cat-p (equal pn "CATEGORY")
  8902. re-p (equal (string-to-char pv) ?{)
  8903. str-p (equal (string-to-char pv) ?\")
  8904. time-p (save-match-data
  8905. (string-match "^\"[[<].*[]>]\"$" pv))
  8906. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8907. (if time-p (setq pv (org-matcher-time pv)))
  8908. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8909. (cond
  8910. ((equal pn "CATEGORY")
  8911. (setq gv '(get-text-property (point) 'org-category)))
  8912. ((equal pn "TODO")
  8913. (setq gv 'todo))
  8914. (t
  8915. (setq gv `(org-cached-entry-get nil ,pn))))
  8916. (if re-p
  8917. (if (eq po 'org<>)
  8918. `(not (string-match ,pv (or ,gv "")))
  8919. `(string-match ,pv (or ,gv "")))
  8920. (if str-p
  8921. `(,po (or ,gv "") ,pv)
  8922. `(,po (string-to-number (or ,gv ""))
  8923. ,(string-to-number pv) ))))
  8924. (t `(member ,(downcase tag) tags-list)))
  8925. mm (if minus (list 'not mm) mm)
  8926. term rest)
  8927. (push mm tagsmatcher))
  8928. (push (if (> (length tagsmatcher) 1)
  8929. (cons 'and tagsmatcher)
  8930. (car tagsmatcher))
  8931. orlist)
  8932. (setq tagsmatcher nil))
  8933. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8934. (setq tagsmatcher
  8935. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8936. ;; Make the todo matcher
  8937. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8938. (setq todomatcher t)
  8939. (setq orterms (org-split-string todomatch "|") orlist nil)
  8940. (while (setq term (pop orterms))
  8941. (while (string-match re term)
  8942. (setq minus (and (match-end 1)
  8943. (equal (match-string 1 term) "-"))
  8944. kwd (match-string 2 term)
  8945. re-p (equal (string-to-char kwd) ?{)
  8946. term (substring term (match-end 0))
  8947. mm (if re-p
  8948. `(string-match ,(substring kwd 1 -1) todo)
  8949. (list 'equal 'todo kwd))
  8950. mm (if minus (list 'not mm) mm))
  8951. (push mm todomatcher))
  8952. (push (if (> (length todomatcher) 1)
  8953. (cons 'and todomatcher)
  8954. (car todomatcher))
  8955. orlist)
  8956. (setq todomatcher nil))
  8957. (setq todomatcher (if (> (length orlist) 1)
  8958. (cons 'or orlist) (car orlist))))
  8959. ;; Return the string and lisp forms of the matcher
  8960. (setq matcher (if todomatcher
  8961. (list 'and tagsmatcher todomatcher)
  8962. tagsmatcher))
  8963. (cons match0 matcher)))
  8964. (defun org-op-to-function (op &optional stringp)
  8965. "Turn an operator into the appropriate function."
  8966. (setq op
  8967. (cond
  8968. ((equal op "<" ) '(< string< org-time<))
  8969. ((equal op ">" ) '(> org-string> org-time>))
  8970. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8971. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8972. ((member op '("=" "==")) '(= string= org-time=))
  8973. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8974. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8975. (defun org<> (a b) (not (= a b)))
  8976. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8977. (defun org-string>= (a b) (not (string< a b)))
  8978. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8979. (defun org-string<> (a b) (not (string= a b)))
  8980. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  8981. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  8982. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  8983. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  8984. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  8985. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  8986. (defun org-2ft (s)
  8987. "Convert S to a floating point time.
  8988. If S is already a number, just return it. If it is a string, parse
  8989. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  8990. (cond
  8991. ((numberp s) s)
  8992. ((stringp s)
  8993. (condition-case nil
  8994. (float-time (apply 'encode-time (org-parse-time-string s)))
  8995. (error 0.)))
  8996. (t 0.)))
  8997. (defun org-time-today ()
  8998. "Time in seconds today at 0:00.
  8999. Returns the float number of seconds since the beginning of the
  9000. epoch to the beginning of today (00:00)."
  9001. (float-time (apply 'encode-time
  9002. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9003. (defun org-matcher-time (s)
  9004. "Interpret a time comparison value."
  9005. (save-match-data
  9006. (cond
  9007. ((string= s "<now>") (float-time))
  9008. ((string= s "<today>") (org-time-today))
  9009. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9010. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9011. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9012. (+ (org-time-today)
  9013. (* (string-to-number (match-string 1 s))
  9014. (cdr (assoc (match-string 2 s)
  9015. '(("d" . 86400.0) ("w" . 604800.0)
  9016. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9017. (t (org-2ft s)))))
  9018. (defun org-match-any-p (re list)
  9019. "Does re match any element of list?"
  9020. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9021. (delq nil list))
  9022. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9023. (defvar org-tags-overlay (org-make-overlay 1 1))
  9024. (org-detach-overlay org-tags-overlay)
  9025. (defun org-get-local-tags-at (&optional pos)
  9026. "Get a list of tags defined in the current headline."
  9027. (org-get-tags-at pos 'local))
  9028. (defun org-get-local-tags ()
  9029. "Get a list of tags defined in the current headline."
  9030. (org-get-tags-at nil 'local))
  9031. (defun org-get-tags-at (&optional pos local)
  9032. "Get a list of all headline tags applicable at POS.
  9033. POS defaults to point. If tags are inherited, the list contains
  9034. the targets in the same sequence as the headlines appear, i.e.
  9035. the tags of the current headline come last.
  9036. When LOCAL is non-nil, only return tags from the current headline,
  9037. ignore inherited ones."
  9038. (interactive)
  9039. (let (tags ltags lastpos parent)
  9040. (save-excursion
  9041. (save-restriction
  9042. (widen)
  9043. (goto-char (or pos (point)))
  9044. (save-match-data
  9045. (catch 'done
  9046. (condition-case nil
  9047. (progn
  9048. (org-back-to-heading t)
  9049. (while (not (equal lastpos (point)))
  9050. (setq lastpos (point))
  9051. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9052. (setq ltags (org-split-string
  9053. (org-match-string-no-properties 1) ":"))
  9054. (when parent
  9055. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9056. (setq tags (append
  9057. (if parent
  9058. (org-remove-uniherited-tags ltags)
  9059. ltags)
  9060. tags)))
  9061. (or org-use-tag-inheritance (throw 'done t))
  9062. (if local (throw 'done t))
  9063. (org-up-heading-all 1)
  9064. (setq parent t)))
  9065. (error nil)))))
  9066. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9067. (defun org-add-prop-inherited (s)
  9068. (add-text-properties 0 (length s) '(inherited t) s)
  9069. s)
  9070. (defun org-toggle-tag (tag &optional onoff)
  9071. "Toggle the tag TAG for the current line.
  9072. If ONOFF is `on' or `off', don't toggle but set to this state."
  9073. (let (res current)
  9074. (save-excursion
  9075. (org-back-to-heading t)
  9076. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9077. (point-at-eol) t)
  9078. (progn
  9079. (setq current (match-string 1))
  9080. (replace-match ""))
  9081. (setq current ""))
  9082. (setq current (nreverse (org-split-string current ":")))
  9083. (cond
  9084. ((eq onoff 'on)
  9085. (setq res t)
  9086. (or (member tag current) (push tag current)))
  9087. ((eq onoff 'off)
  9088. (or (not (member tag current)) (setq current (delete tag current))))
  9089. (t (if (member tag current)
  9090. (setq current (delete tag current))
  9091. (setq res t)
  9092. (push tag current))))
  9093. (end-of-line 1)
  9094. (if current
  9095. (progn
  9096. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9097. (org-set-tags nil t))
  9098. (delete-horizontal-space))
  9099. (run-hooks 'org-after-tags-change-hook))
  9100. res))
  9101. (defun org-align-tags-here (to-col)
  9102. ;; Assumes that this is a headline
  9103. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9104. (beginning-of-line 1)
  9105. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9106. (< pos (match-beginning 2)))
  9107. (progn
  9108. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9109. (goto-char (match-beginning 1))
  9110. (insert " ")
  9111. (delete-region (point) (1+ (match-beginning 2)))
  9112. (setq ncol (max (1+ (current-column))
  9113. (1+ col)
  9114. (if (> to-col 0)
  9115. to-col
  9116. (- (abs to-col) tags-l))))
  9117. (setq p (point))
  9118. (insert (make-string (- ncol (current-column)) ?\ ))
  9119. (setq ncol (current-column))
  9120. (when indent-tabs-mode (tabify p (point-at-eol)))
  9121. (org-move-to-column (min ncol col) t))
  9122. (goto-char pos))))
  9123. (defun org-set-tags-command (&optional arg just-align)
  9124. "Call the set-tags command for the current entry."
  9125. (interactive "P")
  9126. (if (org-on-heading-p)
  9127. (org-set-tags arg just-align)
  9128. (save-excursion
  9129. (org-back-to-heading t)
  9130. (org-set-tags arg just-align))))
  9131. (defun org-set-tags (&optional arg just-align)
  9132. "Set the tags for the current headline.
  9133. With prefix ARG, realign all tags in headings in the current buffer."
  9134. (interactive "P")
  9135. (let* ((re (concat "^" outline-regexp))
  9136. (current (org-get-tags-string))
  9137. (col (current-column))
  9138. (org-setting-tags t)
  9139. table current-tags inherited-tags ; computed below when needed
  9140. tags p0 c0 c1 rpl)
  9141. (if arg
  9142. (save-excursion
  9143. (goto-char (point-min))
  9144. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9145. (while (re-search-forward re nil t)
  9146. (org-set-tags nil t)
  9147. (end-of-line 1)))
  9148. (message "All tags realigned to column %d" org-tags-column))
  9149. (if just-align
  9150. (setq tags current)
  9151. ;; Get a new set of tags from the user
  9152. (save-excursion
  9153. (setq table (or org-tag-alist (org-get-buffer-tags))
  9154. org-last-tags-completion-table table
  9155. current-tags (org-split-string current ":")
  9156. inherited-tags (nreverse
  9157. (nthcdr (length current-tags)
  9158. (nreverse (org-get-tags-at))))
  9159. tags
  9160. (if (or (eq t org-use-fast-tag-selection)
  9161. (and org-use-fast-tag-selection
  9162. (delq nil (mapcar 'cdr table))))
  9163. (org-fast-tag-selection
  9164. current-tags inherited-tags table
  9165. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9166. (let ((org-add-colon-after-tag-completion t))
  9167. (org-trim
  9168. (org-without-partial-completion
  9169. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9170. nil nil current 'org-tags-history)))))))
  9171. (while (string-match "[-+&]+" tags)
  9172. ;; No boolean logic, just a list
  9173. (setq tags (replace-match ":" t t tags))))
  9174. (if (string-match "\\`[\t ]*\\'" tags)
  9175. (setq tags "")
  9176. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9177. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9178. ;; Insert new tags at the correct column
  9179. (beginning-of-line 1)
  9180. (cond
  9181. ((and (equal current "") (equal tags "")))
  9182. ((re-search-forward
  9183. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9184. (point-at-eol) t)
  9185. (if (equal tags "")
  9186. (setq rpl "")
  9187. (goto-char (match-beginning 0))
  9188. (setq c0 (current-column) p0 (point)
  9189. c1 (max (1+ c0) (if (> org-tags-column 0)
  9190. org-tags-column
  9191. (- (- org-tags-column) (length tags))))
  9192. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9193. (replace-match rpl t t)
  9194. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9195. tags)
  9196. (t (error "Tags alignment failed")))
  9197. (org-move-to-column col)
  9198. (unless just-align
  9199. (run-hooks 'org-after-tags-change-hook)))))
  9200. (defun org-change-tag-in-region (beg end tag off)
  9201. "Add or remove TAG for each entry in the region.
  9202. This works in the agenda, and also in an org-mode buffer."
  9203. (interactive
  9204. (list (region-beginning) (region-end)
  9205. (let ((org-last-tags-completion-table
  9206. (if (org-mode-p)
  9207. (org-get-buffer-tags)
  9208. (org-global-tags-completion-table))))
  9209. (org-ido-completing-read
  9210. "Tag: " 'org-tags-completion-function nil nil nil
  9211. 'org-tags-history))
  9212. (progn
  9213. (message "[s]et or [r]emove? ")
  9214. (equal (read-char-exclusive) ?r))))
  9215. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9216. (let ((agendap (equal major-mode 'org-agenda-mode))
  9217. l1 l2 m buf pos newhead (cnt 0))
  9218. (goto-char end)
  9219. (setq l2 (1- (org-current-line)))
  9220. (goto-char beg)
  9221. (setq l1 (org-current-line))
  9222. (loop for l from l1 to l2 do
  9223. (goto-line l)
  9224. (setq m (get-text-property (point) 'org-hd-marker))
  9225. (when (or (and (org-mode-p) (org-on-heading-p))
  9226. (and agendap m))
  9227. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9228. pos (if agendap m (point)))
  9229. (with-current-buffer buf
  9230. (save-excursion
  9231. (save-restriction
  9232. (goto-char pos)
  9233. (setq cnt (1+ cnt))
  9234. (org-toggle-tag tag (if off 'off 'on))
  9235. (setq newhead (org-get-heading)))))
  9236. (and agendap (org-agenda-change-all-lines newhead m))))
  9237. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9238. (defun org-tags-completion-function (string predicate &optional flag)
  9239. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9240. (confirm (lambda (x) (stringp (car x)))))
  9241. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9242. (setq s1 (match-string 1 string)
  9243. s2 (match-string 2 string))
  9244. (setq s1 "" s2 string))
  9245. (cond
  9246. ((eq flag nil)
  9247. ;; try completion
  9248. (setq rtn (try-completion s2 ctable confirm))
  9249. (if (stringp rtn)
  9250. (setq rtn
  9251. (concat s1 s2 (substring rtn (length s2))
  9252. (if (and org-add-colon-after-tag-completion
  9253. (assoc rtn ctable))
  9254. ":" ""))))
  9255. rtn)
  9256. ((eq flag t)
  9257. ;; all-completions
  9258. (all-completions s2 ctable confirm)
  9259. )
  9260. ((eq flag 'lambda)
  9261. ;; exact match?
  9262. (assoc s2 ctable)))
  9263. ))
  9264. (defun org-fast-tag-insert (kwd tags face &optional end)
  9265. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9266. (insert (format "%-12s" (concat kwd ":"))
  9267. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9268. (or end "")))
  9269. (defun org-fast-tag-show-exit (flag)
  9270. (save-excursion
  9271. (goto-line 3)
  9272. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9273. (replace-match ""))
  9274. (when flag
  9275. (end-of-line 1)
  9276. (org-move-to-column (- (window-width) 19) t)
  9277. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9278. (defun org-set-current-tags-overlay (current prefix)
  9279. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9280. (if (featurep 'xemacs)
  9281. (org-overlay-display org-tags-overlay (concat prefix s)
  9282. 'secondary-selection)
  9283. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9284. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9285. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9286. "Fast tag selection with single keys.
  9287. CURRENT is the current list of tags in the headline, INHERITED is the
  9288. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9289. possibly with grouping information. TODO-TABLE is a similar table with
  9290. TODO keywords, should these have keys assigned to them.
  9291. If the keys are nil, a-z are automatically assigned.
  9292. Returns the new tags string, or nil to not change the current settings."
  9293. (let* ((fulltable (append table todo-table))
  9294. (maxlen (apply 'max (mapcar
  9295. (lambda (x)
  9296. (if (stringp (car x)) (string-width (car x)) 0))
  9297. fulltable)))
  9298. (buf (current-buffer))
  9299. (expert (eq org-fast-tag-selection-single-key 'expert))
  9300. (buffer-tags nil)
  9301. (fwidth (+ maxlen 3 1 3))
  9302. (ncol (/ (- (window-width) 4) fwidth))
  9303. (i-face 'org-done)
  9304. (c-face 'org-todo)
  9305. tg cnt e c char c1 c2 ntable tbl rtn
  9306. ov-start ov-end ov-prefix
  9307. (exit-after-next org-fast-tag-selection-single-key)
  9308. (done-keywords org-done-keywords)
  9309. groups ingroup)
  9310. (save-excursion
  9311. (beginning-of-line 1)
  9312. (if (looking-at
  9313. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9314. (setq ov-start (match-beginning 1)
  9315. ov-end (match-end 1)
  9316. ov-prefix "")
  9317. (setq ov-start (1- (point-at-eol))
  9318. ov-end (1+ ov-start))
  9319. (skip-chars-forward "^\n\r")
  9320. (setq ov-prefix
  9321. (concat
  9322. (buffer-substring (1- (point)) (point))
  9323. (if (> (current-column) org-tags-column)
  9324. " "
  9325. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9326. (org-move-overlay org-tags-overlay ov-start ov-end)
  9327. (save-window-excursion
  9328. (if expert
  9329. (set-buffer (get-buffer-create " *Org tags*"))
  9330. (delete-other-windows)
  9331. (split-window-vertically)
  9332. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9333. (erase-buffer)
  9334. (org-set-local 'org-done-keywords done-keywords)
  9335. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9336. (org-fast-tag-insert "Current" current c-face "\n\n")
  9337. (org-fast-tag-show-exit exit-after-next)
  9338. (org-set-current-tags-overlay current ov-prefix)
  9339. (setq tbl fulltable char ?a cnt 0)
  9340. (while (setq e (pop tbl))
  9341. (cond
  9342. ((equal e '(:startgroup))
  9343. (push '() groups) (setq ingroup t)
  9344. (when (not (= cnt 0))
  9345. (setq cnt 0)
  9346. (insert "\n"))
  9347. (insert "{ "))
  9348. ((equal e '(:endgroup))
  9349. (setq ingroup nil cnt 0)
  9350. (insert "}\n"))
  9351. (t
  9352. (setq tg (car e) c2 nil)
  9353. (if (cdr e)
  9354. (setq c (cdr e))
  9355. ;; automatically assign a character.
  9356. (setq c1 (string-to-char
  9357. (downcase (substring
  9358. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9359. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9360. (while (or (rassoc char ntable) (rassoc char table))
  9361. (setq char (1+ char)))
  9362. (setq c2 c1))
  9363. (setq c (or c2 char)))
  9364. (if ingroup (push tg (car groups)))
  9365. (setq tg (org-add-props tg nil 'face
  9366. (cond
  9367. ((not (assoc tg table))
  9368. (org-get-todo-face tg))
  9369. ((member tg current) c-face)
  9370. ((member tg inherited) i-face)
  9371. (t nil))))
  9372. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9373. (insert "[" c "] " tg (make-string
  9374. (- fwidth 4 (length tg)) ?\ ))
  9375. (push (cons tg c) ntable)
  9376. (when (= (setq cnt (1+ cnt)) ncol)
  9377. (insert "\n")
  9378. (if ingroup (insert " "))
  9379. (setq cnt 0)))))
  9380. (setq ntable (nreverse ntable))
  9381. (insert "\n")
  9382. (goto-char (point-min))
  9383. (if (not expert) (org-fit-window-to-buffer))
  9384. (setq rtn
  9385. (catch 'exit
  9386. (while t
  9387. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9388. (if groups " [!] no groups" " [!]groups")
  9389. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9390. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9391. (cond
  9392. ((= c ?\r) (throw 'exit t))
  9393. ((= c ?!)
  9394. (setq groups (not groups))
  9395. (goto-char (point-min))
  9396. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9397. ((= c ?\C-c)
  9398. (if (not expert)
  9399. (org-fast-tag-show-exit
  9400. (setq exit-after-next (not exit-after-next)))
  9401. (setq expert nil)
  9402. (delete-other-windows)
  9403. (split-window-vertically)
  9404. (org-switch-to-buffer-other-window " *Org tags*")
  9405. (org-fit-window-to-buffer)))
  9406. ((or (= c ?\C-g)
  9407. (and (= c ?q) (not (rassoc c ntable))))
  9408. (org-detach-overlay org-tags-overlay)
  9409. (setq quit-flag t))
  9410. ((= c ?\ )
  9411. (setq current nil)
  9412. (if exit-after-next (setq exit-after-next 'now)))
  9413. ((= c ?\t)
  9414. (condition-case nil
  9415. (setq tg (org-ido-completing-read
  9416. "Tag: "
  9417. (or buffer-tags
  9418. (with-current-buffer buf
  9419. (org-get-buffer-tags)))))
  9420. (quit (setq tg "")))
  9421. (when (string-match "\\S-" tg)
  9422. (add-to-list 'buffer-tags (list tg))
  9423. (if (member tg current)
  9424. (setq current (delete tg current))
  9425. (push tg current)))
  9426. (if exit-after-next (setq exit-after-next 'now)))
  9427. ((setq e (rassoc c todo-table) tg (car e))
  9428. (with-current-buffer buf
  9429. (save-excursion (org-todo tg)))
  9430. (if exit-after-next (setq exit-after-next 'now)))
  9431. ((setq e (rassoc c ntable) tg (car e))
  9432. (if (member tg current)
  9433. (setq current (delete tg current))
  9434. (loop for g in groups do
  9435. (if (member tg g)
  9436. (mapc (lambda (x)
  9437. (setq current (delete x current)))
  9438. g)))
  9439. (push tg current))
  9440. (if exit-after-next (setq exit-after-next 'now))))
  9441. ;; Create a sorted list
  9442. (setq current
  9443. (sort current
  9444. (lambda (a b)
  9445. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9446. (if (eq exit-after-next 'now) (throw 'exit t))
  9447. (goto-char (point-min))
  9448. (beginning-of-line 2)
  9449. (delete-region (point) (point-at-eol))
  9450. (org-fast-tag-insert "Current" current c-face)
  9451. (org-set-current-tags-overlay current ov-prefix)
  9452. (while (re-search-forward
  9453. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9454. (setq tg (match-string 1))
  9455. (add-text-properties
  9456. (match-beginning 1) (match-end 1)
  9457. (list 'face
  9458. (cond
  9459. ((member tg current) c-face)
  9460. ((member tg inherited) i-face)
  9461. (t (get-text-property (match-beginning 1) 'face))))))
  9462. (goto-char (point-min)))))
  9463. (org-detach-overlay org-tags-overlay)
  9464. (if rtn
  9465. (mapconcat 'identity current ":")
  9466. nil))))
  9467. (defun org-get-tags-string ()
  9468. "Get the TAGS string in the current headline."
  9469. (unless (org-on-heading-p t)
  9470. (error "Not on a heading"))
  9471. (save-excursion
  9472. (beginning-of-line 1)
  9473. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9474. (org-match-string-no-properties 1)
  9475. "")))
  9476. (defun org-get-tags ()
  9477. "Get the list of tags specified in the current headline."
  9478. (org-split-string (org-get-tags-string) ":"))
  9479. (defun org-get-buffer-tags ()
  9480. "Get a table of all tags used in the buffer, for completion."
  9481. (let (tags)
  9482. (save-excursion
  9483. (goto-char (point-min))
  9484. (while (re-search-forward
  9485. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9486. (when (equal (char-after (point-at-bol 0)) ?*)
  9487. (mapc (lambda (x) (add-to-list 'tags x))
  9488. (org-split-string (org-match-string-no-properties 1) ":")))))
  9489. (mapcar 'list tags)))
  9490. ;;;; The mapping API
  9491. ;;;###autoload
  9492. (defun org-map-entries (func &optional match scope &rest skip)
  9493. "Call FUNC at each headline selected by MATCH in SCOPE.
  9494. FUNC is a function or a lisp form. The function will be called without
  9495. arguments, with the cursor positioned at the beginning of the headline.
  9496. The return values of all calls to the function will be collected and
  9497. returned as a list.
  9498. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9499. Only headlines that are matched by this query will be considered during
  9500. the iteration. When MATCH is nil or t, all headlines will be
  9501. visited by the iteration.
  9502. SCOPE determines the scope of this command. It can be any of:
  9503. nil The current buffer, respecting the restriction if any
  9504. tree The subtree started with the entry at point
  9505. file The current buffer, without restriction
  9506. file-with-archives
  9507. The current buffer, and any archives associated with it
  9508. agenda All agenda files
  9509. agenda-with-archives
  9510. All agenda files with any archive files associated with them
  9511. \(file1 file2 ...)
  9512. If this is a list, all files in the list will be scanned
  9513. The remaining args are treated as settings for the skipping facilities of
  9514. the scanner. The following items can be given here:
  9515. archive skip trees with the archive tag.
  9516. comment skip trees with the COMMENT keyword
  9517. function or Emacs Lisp form:
  9518. will be used as value for `org-agenda-skip-function', so whenever
  9519. the the function returns t, FUNC will not be called for that
  9520. entry and search will continue from the point where the
  9521. function leaves it."
  9522. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9523. (org-agenda-skip-archived-trees (memq 'archive skip))
  9524. (org-agenda-skip-comment-trees (memq 'comment skip))
  9525. (org-agenda-skip-function
  9526. (car (org-delete-all '(comment archive) skip)))
  9527. (org-tags-match-list-sublevels t)
  9528. matcher file res
  9529. org-todo-keywords-for-agenda
  9530. org-done-keywords-for-agenda
  9531. org-todo-keyword-alist-for-agenda
  9532. org-tag-alist-for-agenda)
  9533. (cond
  9534. ((eq match t) (setq matcher t))
  9535. ((eq match nil) (setq matcher t))
  9536. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9537. (save-excursion
  9538. (save-restriction
  9539. (when (eq scope 'tree)
  9540. (org-back-to-heading t)
  9541. (org-narrow-to-subtree)
  9542. (setq scope nil))
  9543. (if (not scope)
  9544. (progn
  9545. (org-prepare-agenda-buffers
  9546. (list (buffer-file-name (current-buffer))))
  9547. (setq res (org-scan-tags func matcher)))
  9548. ;; Get the right scope
  9549. (cond
  9550. ((and scope (listp scope) (symbolp (car scope)))
  9551. (setq scope (eval scope)))
  9552. ((eq scope 'agenda)
  9553. (setq scope (org-agenda-files t)))
  9554. ((eq scope 'agenda-with-archives)
  9555. (setq scope (org-agenda-files t))
  9556. (setq scope (org-add-archive-files scope)))
  9557. ((eq scope 'file)
  9558. (setq scope (list (buffer-file-name))))
  9559. ((eq scope 'file-with-archives)
  9560. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9561. (org-prepare-agenda-buffers scope)
  9562. (while (setq file (pop scope))
  9563. (with-current-buffer (org-find-base-buffer-visiting file)
  9564. (save-excursion
  9565. (save-restriction
  9566. (widen)
  9567. (goto-char (point-min))
  9568. (setq res (append res (org-scan-tags func matcher))))))))))
  9569. res))
  9570. ;;;; Properties
  9571. ;;; Setting and retrieving properties
  9572. (defconst org-special-properties
  9573. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9574. "TIMESTAMP" "TIMESTAMP_IA")
  9575. "The special properties valid in Org-mode.
  9576. These are properties that are not defined in the property drawer,
  9577. but in some other way.")
  9578. (defconst org-default-properties
  9579. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9580. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9581. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9582. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9583. "Some properties that are used by Org-mode for various purposes.
  9584. Being in this list makes sure that they are offered for completion.")
  9585. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9586. "Regular expression matching the first line of a property drawer.")
  9587. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9588. "Regular expression matching the first line of a property drawer.")
  9589. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9590. "Regular expression matching the first line of a property drawer.")
  9591. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9592. "Regular expression matching the first line of a property drawer.")
  9593. (defconst org-property-drawer-re
  9594. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9595. org-property-end-re "\\)\n?")
  9596. "Matches an entire property drawer.")
  9597. (defconst org-clock-drawer-re
  9598. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9599. org-property-end-re "\\)\n?")
  9600. "Matches an entire clock drawer.")
  9601. (defun org-property-action ()
  9602. "Do an action on properties."
  9603. (interactive)
  9604. (let (c)
  9605. (org-at-property-p)
  9606. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9607. (setq c (read-char-exclusive))
  9608. (cond
  9609. ((equal c ?s)
  9610. (call-interactively 'org-set-property))
  9611. ((equal c ?d)
  9612. (call-interactively 'org-delete-property))
  9613. ((equal c ?D)
  9614. (call-interactively 'org-delete-property-globally))
  9615. ((equal c ?c)
  9616. (call-interactively 'org-compute-property-at-point))
  9617. (t (error "No such property action %c" c)))))
  9618. (defun org-at-property-p ()
  9619. "Is the cursor in a property line?"
  9620. ;; FIXME: Does not check if we are actually in the drawer.
  9621. ;; FIXME: also returns true on any drawers.....
  9622. ;; This is used by C-c C-c for property action.
  9623. (save-excursion
  9624. (beginning-of-line 1)
  9625. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9626. (defun org-get-property-block (&optional beg end force)
  9627. "Return the (beg . end) range of the body of the property drawer.
  9628. BEG and END can be beginning and end of subtree, if not given
  9629. they will be found.
  9630. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9631. (catch 'exit
  9632. (save-excursion
  9633. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9634. (end (or end (progn (outline-next-heading) (point)))))
  9635. (goto-char beg)
  9636. (if (re-search-forward org-property-start-re end t)
  9637. (setq beg (1+ (match-end 0)))
  9638. (if force
  9639. (save-excursion
  9640. (org-insert-property-drawer)
  9641. (setq end (progn (outline-next-heading) (point))))
  9642. (throw 'exit nil))
  9643. (goto-char beg)
  9644. (if (re-search-forward org-property-start-re end t)
  9645. (setq beg (1+ (match-end 0)))))
  9646. (if (re-search-forward org-property-end-re end t)
  9647. (setq end (match-beginning 0))
  9648. (or force (throw 'exit nil))
  9649. (goto-char beg)
  9650. (setq end beg)
  9651. (org-indent-line-function)
  9652. (insert ":END:\n"))
  9653. (cons beg end)))))
  9654. (defun org-entry-properties (&optional pom which)
  9655. "Get all properties of the entry at point-or-marker POM.
  9656. This includes the TODO keyword, the tags, time strings for deadline,
  9657. scheduled, and clocking, and any additional properties defined in the
  9658. entry. The return value is an alist, keys may occur multiple times
  9659. if the property key was used several times.
  9660. POM may also be nil, in which case the current entry is used.
  9661. If WHICH is nil or `all', get all properties. If WHICH is
  9662. `special' or `standard', only get that subclass."
  9663. (setq which (or which 'all))
  9664. (org-with-point-at pom
  9665. (let ((clockstr (substring org-clock-string 0 -1))
  9666. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9667. beg end range props sum-props key value string clocksum)
  9668. (save-excursion
  9669. (when (condition-case nil
  9670. (and (org-mode-p) (org-back-to-heading t))
  9671. (error nil))
  9672. (setq beg (point))
  9673. (setq sum-props (get-text-property (point) 'org-summaries))
  9674. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9675. (outline-next-heading)
  9676. (setq end (point))
  9677. (when (memq which '(all special))
  9678. ;; Get the special properties, like TODO and tags
  9679. (goto-char beg)
  9680. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9681. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9682. (when (looking-at org-priority-regexp)
  9683. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9684. (when (and (setq value (org-get-tags-string))
  9685. (string-match "\\S-" value))
  9686. (push (cons "TAGS" value) props))
  9687. (when (setq value (org-get-tags-at))
  9688. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9689. props))
  9690. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9691. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9692. string (if (equal key clockstr)
  9693. (org-no-properties
  9694. (org-trim
  9695. (buffer-substring
  9696. (match-beginning 3) (goto-char (point-at-eol)))))
  9697. (substring (org-match-string-no-properties 3) 1 -1)))
  9698. (unless key
  9699. (if (= (char-after (match-beginning 3)) ?\[)
  9700. (setq key "TIMESTAMP_IA")
  9701. (setq key "TIMESTAMP")))
  9702. (when (or (equal key clockstr) (not (assoc key props)))
  9703. (push (cons key string) props)))
  9704. )
  9705. (when (memq which '(all standard))
  9706. ;; Get the standard properties, like :PORP: ...
  9707. (setq range (org-get-property-block beg end))
  9708. (when range
  9709. (goto-char (car range))
  9710. (while (re-search-forward
  9711. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9712. (cdr range) t)
  9713. (setq key (org-match-string-no-properties 1)
  9714. value (org-trim (or (org-match-string-no-properties 2) "")))
  9715. (unless (member key excluded)
  9716. (push (cons key (or value "")) props)))))
  9717. (if clocksum
  9718. (push (cons "CLOCKSUM"
  9719. (org-columns-number-to-string (/ (float clocksum) 60.)
  9720. 'add_times))
  9721. props))
  9722. (unless (assoc "CATEGORY" props)
  9723. (setq value (or (org-get-category)
  9724. (progn (org-refresh-category-properties)
  9725. (org-get-category))))
  9726. (push (cons "CATEGORY" value) props))
  9727. (append sum-props (nreverse props)))))))
  9728. (defun org-entry-get (pom property &optional inherit)
  9729. "Get value of PROPERTY for entry at point-or-marker POM.
  9730. If INHERIT is non-nil and the entry does not have the property,
  9731. then also check higher levels of the hierarchy.
  9732. If INHERIT is the symbol `selective', use inheritance only if the setting
  9733. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9734. If the property is present but empty, the return value is the empty string.
  9735. If the property is not present at all, nil is returned."
  9736. (org-with-point-at pom
  9737. (if (and inherit (if (eq inherit 'selective)
  9738. (org-property-inherit-p property)
  9739. t))
  9740. (org-entry-get-with-inheritance property)
  9741. (if (member property org-special-properties)
  9742. ;; We need a special property. Use brute force, get all properties.
  9743. (cdr (assoc property (org-entry-properties nil 'special)))
  9744. (let ((range (org-get-property-block)))
  9745. (if (and range
  9746. (goto-char (car range))
  9747. (re-search-forward
  9748. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9749. (cdr range) t))
  9750. ;; Found the property, return it.
  9751. (if (match-end 1)
  9752. (org-match-string-no-properties 1)
  9753. "")))))))
  9754. (defun org-property-or-variable-value (var &optional inherit)
  9755. "Check if there is a property fixing the value of VAR.
  9756. If yes, return this value. If not, return the current value of the variable."
  9757. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9758. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9759. (read prop)
  9760. (symbol-value var))))
  9761. (defun org-entry-delete (pom property)
  9762. "Delete the property PROPERTY from entry at point-or-marker POM."
  9763. (org-with-point-at pom
  9764. (if (member property org-special-properties)
  9765. nil ; cannot delete these properties.
  9766. (let ((range (org-get-property-block)))
  9767. (if (and range
  9768. (goto-char (car range))
  9769. (re-search-forward
  9770. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9771. (cdr range) t))
  9772. (progn
  9773. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9774. t)
  9775. nil)))))
  9776. ;; Multi-values properties are properties that contain multiple values
  9777. ;; These values are assumed to be single words, separated by whitespace.
  9778. (defun org-entry-add-to-multivalued-property (pom property value)
  9779. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9780. (let* ((old (org-entry-get pom property))
  9781. (values (and old (org-split-string old "[ \t]"))))
  9782. (setq value (org-entry-protect-space value))
  9783. (unless (member value values)
  9784. (setq values (cons value values))
  9785. (org-entry-put pom property
  9786. (mapconcat 'identity values " ")))))
  9787. (defun org-entry-remove-from-multivalued-property (pom property value)
  9788. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9789. (let* ((old (org-entry-get pom property))
  9790. (values (and old (org-split-string old "[ \t]"))))
  9791. (setq value (org-entry-protect-space value))
  9792. (when (member value values)
  9793. (setq values (delete value values))
  9794. (org-entry-put pom property
  9795. (mapconcat 'identity values " ")))))
  9796. (defun org-entry-member-in-multivalued-property (pom property value)
  9797. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9798. (let* ((old (org-entry-get pom property))
  9799. (values (and old (org-split-string old "[ \t]"))))
  9800. (setq value (org-entry-protect-space value))
  9801. (member value values)))
  9802. (defun org-entry-get-multivalued-property (pom property)
  9803. "Return a list of values in a multivalued property."
  9804. (let* ((value (org-entry-get pom property))
  9805. (values (and value (org-split-string value "[ \t]"))))
  9806. (mapcar 'org-entry-restore-space values)))
  9807. (defun org-entry-put-multivalued-property (pom property &rest values)
  9808. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9809. VALUES should be a list of strings. Spaces will be protected."
  9810. (org-entry-put pom property
  9811. (mapconcat 'org-entry-protect-space values " "))
  9812. (let* ((value (org-entry-get pom property))
  9813. (values (and value (org-split-string value "[ \t]"))))
  9814. (mapcar 'org-entry-restore-space values)))
  9815. (defun org-entry-protect-space (s)
  9816. "Protect spaces and newline in string S."
  9817. (while (string-match " " s)
  9818. (setq s (replace-match "%20" t t s)))
  9819. (while (string-match "\n" s)
  9820. (setq s (replace-match "%0A" t t s)))
  9821. s)
  9822. (defun org-entry-restore-space (s)
  9823. "Restore spaces and newline in string S."
  9824. (while (string-match "%20" s)
  9825. (setq s (replace-match " " t t s)))
  9826. (while (string-match "%0A" s)
  9827. (setq s (replace-match "\n" t t s)))
  9828. s)
  9829. (defvar org-entry-property-inherited-from (make-marker)
  9830. "Marker pointing to the entry from where a property was inherited.
  9831. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9832. location of the entry where the inheritance search matched. If there was
  9833. no match, the marker will point nowhere.
  9834. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9835. is set.")
  9836. (defun org-entry-get-with-inheritance (property)
  9837. "Get entry property, and search higher levels if not present."
  9838. (move-marker org-entry-property-inherited-from nil)
  9839. (let (tmp)
  9840. (save-excursion
  9841. (save-restriction
  9842. (widen)
  9843. (catch 'ex
  9844. (while t
  9845. (when (setq tmp (org-entry-get nil property))
  9846. (org-back-to-heading t)
  9847. (move-marker org-entry-property-inherited-from (point))
  9848. (throw 'ex tmp))
  9849. (or (org-up-heading-safe) (throw 'ex nil)))))
  9850. (or tmp
  9851. (cdr (assoc property org-file-properties))
  9852. (cdr (assoc property org-global-properties))
  9853. (cdr (assoc property org-global-properties-fixed))))))
  9854. (defun org-entry-put (pom property value)
  9855. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9856. (org-with-point-at pom
  9857. (org-back-to-heading t)
  9858. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9859. range)
  9860. (cond
  9861. ((equal property "TODO")
  9862. (when (and (stringp value) (string-match "\\S-" value)
  9863. (not (member value org-todo-keywords-1)))
  9864. (error "\"%s\" is not a valid TODO state" value))
  9865. (if (or (not value)
  9866. (not (string-match "\\S-" value)))
  9867. (setq value 'none))
  9868. (org-todo value)
  9869. (org-set-tags nil 'align))
  9870. ((equal property "PRIORITY")
  9871. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9872. (string-to-char value) ?\ ))
  9873. (org-set-tags nil 'align))
  9874. ((equal property "SCHEDULED")
  9875. (if (re-search-forward org-scheduled-time-regexp end t)
  9876. (cond
  9877. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9878. ((eq value 'later) (org-timestamp-change 1 'day))
  9879. (t (call-interactively 'org-schedule)))
  9880. (call-interactively 'org-schedule)))
  9881. ((equal property "DEADLINE")
  9882. (if (re-search-forward org-deadline-time-regexp end t)
  9883. (cond
  9884. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9885. ((eq value 'later) (org-timestamp-change 1 'day))
  9886. (t (call-interactively 'org-deadline)))
  9887. (call-interactively 'org-deadline)))
  9888. ((member property org-special-properties)
  9889. (error "The %s property can not yet be set with `org-entry-put'"
  9890. property))
  9891. (t ; a non-special property
  9892. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9893. (setq range (org-get-property-block beg end 'force))
  9894. (goto-char (car range))
  9895. (if (re-search-forward
  9896. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9897. (progn
  9898. (delete-region (match-beginning 1) (match-end 1))
  9899. (goto-char (match-beginning 1)))
  9900. (goto-char (cdr range))
  9901. (insert "\n")
  9902. (backward-char 1)
  9903. (org-indent-line-function)
  9904. (insert ":" property ":"))
  9905. (and value (insert " " value))
  9906. (org-indent-line-function)))))))
  9907. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9908. "Get all property keys in the current buffer.
  9909. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9910. like tags and TODO state.
  9911. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9912. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9913. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9914. formats in the current buffer."
  9915. (let (rtn range cfmt s p)
  9916. (save-excursion
  9917. (save-restriction
  9918. (widen)
  9919. (goto-char (point-min))
  9920. (while (re-search-forward org-property-start-re nil t)
  9921. (setq range (org-get-property-block))
  9922. (goto-char (car range))
  9923. (while (re-search-forward
  9924. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9925. (cdr range) t)
  9926. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9927. (outline-next-heading))))
  9928. (when include-specials
  9929. (setq rtn (append org-special-properties rtn)))
  9930. (when include-defaults
  9931. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9932. (when include-columns
  9933. (save-excursion
  9934. (save-restriction
  9935. (widen)
  9936. (goto-char (point-min))
  9937. (while (re-search-forward
  9938. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9939. nil t)
  9940. (setq cfmt (match-string 2) s 0)
  9941. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9942. cfmt s)
  9943. (setq s (match-end 0)
  9944. p (match-string 1 cfmt))
  9945. (unless (or (equal p "ITEM")
  9946. (member p org-special-properties))
  9947. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9948. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9949. (defun org-property-values (key)
  9950. "Return a list of all values of property KEY."
  9951. (save-excursion
  9952. (save-restriction
  9953. (widen)
  9954. (goto-char (point-min))
  9955. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9956. values)
  9957. (while (re-search-forward re nil t)
  9958. (add-to-list 'values (org-trim (match-string 1))))
  9959. (delete "" values)))))
  9960. (defun org-insert-property-drawer ()
  9961. "Insert a property drawer into the current entry."
  9962. (interactive)
  9963. (org-back-to-heading t)
  9964. (looking-at outline-regexp)
  9965. (let ((indent (- (match-end 0)(match-beginning 0)))
  9966. (beg (point))
  9967. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9968. end hiddenp)
  9969. (outline-next-heading)
  9970. (setq end (point))
  9971. (goto-char beg)
  9972. (while (re-search-forward re end t))
  9973. (setq hiddenp (org-invisible-p))
  9974. (end-of-line 1)
  9975. (and (equal (char-after) ?\n) (forward-char 1))
  9976. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9977. (beginning-of-line 2))
  9978. (org-skip-over-state-notes)
  9979. (skip-chars-backward " \t\n\r")
  9980. (if (eq (char-before) ?*) (forward-char 1))
  9981. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9982. (beginning-of-line 0)
  9983. (org-indent-to-column indent)
  9984. (beginning-of-line 2)
  9985. (org-indent-to-column indent)
  9986. (beginning-of-line 0)
  9987. (if hiddenp
  9988. (save-excursion
  9989. (org-back-to-heading t)
  9990. (hide-entry))
  9991. (org-flag-drawer t))))
  9992. (defun org-set-property (property value)
  9993. "In the current entry, set PROPERTY to VALUE.
  9994. When called interactively, this will prompt for a property name, offering
  9995. completion on existing and default properties. And then it will prompt
  9996. for a value, offering completion either on allowed values (via an inherited
  9997. xxx_ALL property) or on existing values in other instances of this property
  9998. in the current file."
  9999. (interactive
  10000. (let* ((completion-ignore-case t)
  10001. (keys (org-buffer-property-keys nil t t))
  10002. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10003. (prop (if (member prop0 keys)
  10004. prop0
  10005. (or (cdr (assoc (downcase prop0)
  10006. (mapcar (lambda (x) (cons (downcase x) x))
  10007. keys)))
  10008. prop0)))
  10009. (cur (org-entry-get nil prop))
  10010. (allowed (org-property-get-allowed-values nil prop 'table))
  10011. (existing (mapcar 'list (org-property-values prop)))
  10012. (val (if allowed
  10013. (org-completing-read "Value: " allowed nil 'req-match)
  10014. (org-completing-read
  10015. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10016. (concat "[" cur "]") "")
  10017. ": ")
  10018. existing nil nil "" nil cur))))
  10019. (list prop (if (equal val "") cur val))))
  10020. (unless (equal (org-entry-get nil property) value)
  10021. (org-entry-put nil property value)))
  10022. (defun org-delete-property (property)
  10023. "In the current entry, delete PROPERTY."
  10024. (interactive
  10025. (let* ((completion-ignore-case t)
  10026. (prop (org-ido-completing-read
  10027. "Property: " (org-entry-properties nil 'standard))))
  10028. (list prop)))
  10029. (message "Property %s %s" property
  10030. (if (org-entry-delete nil property)
  10031. "deleted"
  10032. "was not present in the entry")))
  10033. (defun org-delete-property-globally (property)
  10034. "Remove PROPERTY globally, from all entries."
  10035. (interactive
  10036. (let* ((completion-ignore-case t)
  10037. (prop (org-ido-completing-read
  10038. "Globally remove property: "
  10039. (mapcar 'list (org-buffer-property-keys)))))
  10040. (list prop)))
  10041. (save-excursion
  10042. (save-restriction
  10043. (widen)
  10044. (goto-char (point-min))
  10045. (let ((cnt 0))
  10046. (while (re-search-forward
  10047. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10048. nil t)
  10049. (setq cnt (1+ cnt))
  10050. (replace-match ""))
  10051. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10052. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10053. (defun org-compute-property-at-point ()
  10054. "Compute the property at point.
  10055. This looks for an enclosing column format, extracts the operator and
  10056. then applies it to the property in the column format's scope."
  10057. (interactive)
  10058. (unless (org-at-property-p)
  10059. (error "Not at a property"))
  10060. (let ((prop (org-match-string-no-properties 2)))
  10061. (org-columns-get-format-and-top-level)
  10062. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10063. (error "No operator defined for property %s" prop))
  10064. (org-columns-compute prop)))
  10065. (defun org-property-get-allowed-values (pom property &optional table)
  10066. "Get allowed values for the property PROPERTY.
  10067. When TABLE is non-nil, return an alist that can directly be used for
  10068. completion."
  10069. (let (vals)
  10070. (cond
  10071. ((equal property "TODO")
  10072. (setq vals (org-with-point-at pom
  10073. (append org-todo-keywords-1 '("")))))
  10074. ((equal property "PRIORITY")
  10075. (let ((n org-lowest-priority))
  10076. (while (>= n org-highest-priority)
  10077. (push (char-to-string n) vals)
  10078. (setq n (1- n)))))
  10079. ((member property org-special-properties))
  10080. (t
  10081. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10082. (when (and vals (string-match "\\S-" vals))
  10083. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10084. (setq vals (mapcar (lambda (x)
  10085. (cond ((stringp x) x)
  10086. ((numberp x) (number-to-string x))
  10087. ((symbolp x) (symbol-name x))
  10088. (t "???")))
  10089. vals)))))
  10090. (if table (mapcar 'list vals) vals)))
  10091. (defun org-property-previous-allowed-value (&optional previous)
  10092. "Switch to the next allowed value for this property."
  10093. (interactive)
  10094. (org-property-next-allowed-value t))
  10095. (defun org-property-next-allowed-value (&optional previous)
  10096. "Switch to the next allowed value for this property."
  10097. (interactive)
  10098. (unless (org-at-property-p)
  10099. (error "Not at a property"))
  10100. (let* ((key (match-string 2))
  10101. (value (match-string 3))
  10102. (allowed (or (org-property-get-allowed-values (point) key)
  10103. (and (member value '("[ ]" "[-]" "[X]"))
  10104. '("[ ]" "[X]"))))
  10105. nval)
  10106. (unless allowed
  10107. (error "Allowed values for this property have not been defined"))
  10108. (if previous (setq allowed (reverse allowed)))
  10109. (if (member value allowed)
  10110. (setq nval (car (cdr (member value allowed)))))
  10111. (setq nval (or nval (car allowed)))
  10112. (if (equal nval value)
  10113. (error "Only one allowed value for this property"))
  10114. (org-at-property-p)
  10115. (replace-match (concat " :" key ": " nval) t t)
  10116. (org-indent-line-function)
  10117. (beginning-of-line 1)
  10118. (skip-chars-forward " \t")))
  10119. (defun org-find-entry-with-id (ident)
  10120. "Locate the entry that contains the ID property with exact value IDENT.
  10121. IDENT can be a string, a symbol or a number, this function will search for
  10122. the string representation of it.
  10123. Return the position where this entry starts, or nil if there is no such entry."
  10124. (interactive "sID: ")
  10125. (let ((id (cond
  10126. ((stringp ident) ident)
  10127. ((symbol-name ident) (symbol-name ident))
  10128. ((numberp ident) (number-to-string ident))
  10129. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10130. (case-fold-search nil))
  10131. (save-excursion
  10132. (save-restriction
  10133. (widen)
  10134. (goto-char (point-min))
  10135. (when (re-search-forward
  10136. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10137. nil t)
  10138. (org-back-to-heading)
  10139. (point))))))
  10140. ;;;; Timestamps
  10141. (defvar org-last-changed-timestamp nil)
  10142. (defvar org-last-inserted-timestamp nil
  10143. "The last time stamp inserted with `org-insert-time-stamp'.")
  10144. (defvar org-time-was-given) ; dynamically scoped parameter
  10145. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10146. (defvar org-ts-what) ; dynamically scoped parameter
  10147. (defun org-time-stamp (arg &optional inactive)
  10148. "Prompt for a date/time and insert a time stamp.
  10149. If the user specifies a time like HH:MM, or if this command is called
  10150. with a prefix argument, the time stamp will contain date and time.
  10151. Otherwise, only the date will be included. All parts of a date not
  10152. specified by the user will be filled in from the current date/time.
  10153. So if you press just return without typing anything, the time stamp
  10154. will represent the current date/time. If there is already a timestamp
  10155. at the cursor, it will be modified."
  10156. (interactive "P")
  10157. (let* ((ts nil)
  10158. (default-time
  10159. ;; Default time is either today, or, when entering a range,
  10160. ;; the range start.
  10161. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10162. (save-excursion
  10163. (re-search-backward
  10164. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10165. (- (point) 20) t)))
  10166. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10167. (current-time)))
  10168. (default-input (and ts (org-get-compact-tod ts)))
  10169. org-time-was-given org-end-time-was-given time)
  10170. (cond
  10171. ((and (org-at-timestamp-p t)
  10172. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10173. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10174. (insert "--")
  10175. (setq time (let ((this-command this-command))
  10176. (org-read-date arg 'totime nil nil
  10177. default-time default-input)))
  10178. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10179. ((org-at-timestamp-p t)
  10180. (setq time (let ((this-command this-command))
  10181. (org-read-date arg 'totime nil nil default-time default-input)))
  10182. (when (org-at-timestamp-p t) ; just to get the match data
  10183. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10184. (replace-match "")
  10185. (setq org-last-changed-timestamp
  10186. (org-insert-time-stamp
  10187. time (or org-time-was-given arg)
  10188. inactive nil nil (list org-end-time-was-given))))
  10189. (message "Timestamp updated"))
  10190. (t
  10191. (setq time (let ((this-command this-command))
  10192. (org-read-date arg 'totime nil nil default-time default-input)))
  10193. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10194. nil nil (list org-end-time-was-given))))))
  10195. ;; FIXME: can we use this for something else, like computing time differences?
  10196. (defun org-get-compact-tod (s)
  10197. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10198. (let* ((t1 (match-string 1 s))
  10199. (h1 (string-to-number (match-string 2 s)))
  10200. (m1 (string-to-number (match-string 3 s)))
  10201. (t2 (and (match-end 4) (match-string 5 s)))
  10202. (h2 (and t2 (string-to-number (match-string 6 s))))
  10203. (m2 (and t2 (string-to-number (match-string 7 s))))
  10204. dh dm)
  10205. (if (not t2)
  10206. t1
  10207. (setq dh (- h2 h1) dm (- m2 m1))
  10208. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10209. (concat t1 "+" (number-to-string dh)
  10210. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10211. (defun org-time-stamp-inactive (&optional arg)
  10212. "Insert an inactive time stamp.
  10213. An inactive time stamp is enclosed in square brackets instead of angle
  10214. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10215. does not link to the calendar and cannot be changed with the S-cursor keys.
  10216. So these are more for recording a certain time/date."
  10217. (interactive "P")
  10218. (org-time-stamp arg 'inactive))
  10219. (defvar org-date-ovl (org-make-overlay 1 1))
  10220. (org-overlay-put org-date-ovl 'face 'org-warning)
  10221. (org-detach-overlay org-date-ovl)
  10222. (defvar org-ans1) ; dynamically scoped parameter
  10223. (defvar org-ans2) ; dynamically scoped parameter
  10224. (defvar org-plain-time-of-day-regexp) ; defined below
  10225. (defvar org-overriding-default-time nil) ; dynamically scoped
  10226. (defvar org-read-date-overlay nil)
  10227. (defvar org-dcst nil) ; dynamically scoped
  10228. (defun org-read-date (&optional with-time to-time from-string prompt
  10229. default-time default-input)
  10230. "Read a date, possibly a time, and make things smooth for the user.
  10231. The prompt will suggest to enter an ISO date, but you can also enter anything
  10232. which will at least partially be understood by `parse-time-string'.
  10233. Unrecognized parts of the date will default to the current day, month, year,
  10234. hour and minute. If this command is called to replace a timestamp at point,
  10235. of to enter the second timestamp of a range, the default time is taken from the
  10236. existing stamp. For example,
  10237. 3-2-5 --> 2003-02-05
  10238. feb 15 --> currentyear-02-15
  10239. sep 12 9 --> 2009-09-12
  10240. 12:45 --> today 12:45
  10241. 22 sept 0:34 --> currentyear-09-22 0:34
  10242. 12 --> currentyear-currentmonth-12
  10243. Fri --> nearest Friday (today or later)
  10244. etc.
  10245. Furthermore you can specify a relative date by giving, as the *first* thing
  10246. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10247. change in days weeks, months, years.
  10248. With a single plus or minus, the date is relative to today. With a double
  10249. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10250. +4d --> four days from today
  10251. +4 --> same as above
  10252. +2w --> two weeks from today
  10253. ++5 --> five days from default date
  10254. The function understands only English month and weekday abbreviations,
  10255. but this can be configured with the variables `parse-time-months' and
  10256. `parse-time-weekdays'.
  10257. While prompting, a calendar is popped up - you can also select the
  10258. date with the mouse (button 1). The calendar shows a period of three
  10259. months. To scroll it to other months, use the keys `>' and `<'.
  10260. If you don't like the calendar, turn it off with
  10261. \(setq org-read-date-popup-calendar nil)
  10262. With optional argument TO-TIME, the date will immediately be converted
  10263. to an internal time.
  10264. With an optional argument WITH-TIME, the prompt will suggest to also
  10265. insert a time. Note that when WITH-TIME is not set, you can still
  10266. enter a time, and this function will inform the calling routine about
  10267. this change. The calling routine may then choose to change the format
  10268. used to insert the time stamp into the buffer to include the time.
  10269. With optional argument FROM-STRING, read from this string instead from
  10270. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10271. the time/date that is used for everything that is not specified by the
  10272. user."
  10273. (require 'parse-time)
  10274. (let* ((org-time-stamp-rounding-minutes
  10275. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10276. (org-dcst org-display-custom-times)
  10277. (ct (org-current-time))
  10278. (def (or org-overriding-default-time default-time ct))
  10279. (defdecode (decode-time def))
  10280. (dummy (progn
  10281. (when (< (nth 2 defdecode) org-extend-today-until)
  10282. (setcar (nthcdr 2 defdecode) -1)
  10283. (setcar (nthcdr 1 defdecode) 59)
  10284. (setq def (apply 'encode-time defdecode)
  10285. defdecode (decode-time def)))))
  10286. (calendar-move-hook nil)
  10287. (calendar-view-diary-initially-flag nil)
  10288. (view-diary-entries-initially nil)
  10289. (calendar-view-holidays-initially-flag nil)
  10290. (view-calendar-holidays-initially nil)
  10291. (timestr (format-time-string
  10292. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10293. (prompt (concat (if prompt (concat prompt " ") "")
  10294. (format "Date+time [%s]: " timestr)))
  10295. ans (org-ans0 "") org-ans1 org-ans2 final)
  10296. (cond
  10297. (from-string (setq ans from-string))
  10298. (org-read-date-popup-calendar
  10299. (save-excursion
  10300. (save-window-excursion
  10301. (calendar)
  10302. (calendar-forward-day (- (time-to-days def)
  10303. (calendar-absolute-from-gregorian
  10304. (calendar-current-date))))
  10305. (org-eval-in-calendar nil t)
  10306. (let* ((old-map (current-local-map))
  10307. (map (copy-keymap calendar-mode-map))
  10308. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10309. (org-defkey map (kbd "RET") 'org-calendar-select)
  10310. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10311. 'org-calendar-select-mouse)
  10312. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10313. 'org-calendar-select-mouse)
  10314. (org-defkey minibuffer-local-map [(meta shift left)]
  10315. (lambda () (interactive)
  10316. (org-eval-in-calendar '(calendar-backward-month 1))))
  10317. (org-defkey minibuffer-local-map [(meta shift right)]
  10318. (lambda () (interactive)
  10319. (org-eval-in-calendar '(calendar-forward-month 1))))
  10320. (org-defkey minibuffer-local-map [(meta shift up)]
  10321. (lambda () (interactive)
  10322. (org-eval-in-calendar '(calendar-backward-year 1))))
  10323. (org-defkey minibuffer-local-map [(meta shift down)]
  10324. (lambda () (interactive)
  10325. (org-eval-in-calendar '(calendar-forward-year 1))))
  10326. (org-defkey minibuffer-local-map [(shift up)]
  10327. (lambda () (interactive)
  10328. (org-eval-in-calendar '(calendar-backward-week 1))))
  10329. (org-defkey minibuffer-local-map [(shift down)]
  10330. (lambda () (interactive)
  10331. (org-eval-in-calendar '(calendar-forward-week 1))))
  10332. (org-defkey minibuffer-local-map [(shift left)]
  10333. (lambda () (interactive)
  10334. (org-eval-in-calendar '(calendar-backward-day 1))))
  10335. (org-defkey minibuffer-local-map [(shift right)]
  10336. (lambda () (interactive)
  10337. (org-eval-in-calendar '(calendar-forward-day 1))))
  10338. (org-defkey minibuffer-local-map ">"
  10339. (lambda () (interactive)
  10340. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10341. (org-defkey minibuffer-local-map "<"
  10342. (lambda () (interactive)
  10343. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10344. (unwind-protect
  10345. (progn
  10346. (use-local-map map)
  10347. (add-hook 'post-command-hook 'org-read-date-display)
  10348. (setq org-ans0 (read-string prompt default-input nil nil))
  10349. ;; org-ans0: from prompt
  10350. ;; org-ans1: from mouse click
  10351. ;; org-ans2: from calendar motion
  10352. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10353. (remove-hook 'post-command-hook 'org-read-date-display)
  10354. (use-local-map old-map)
  10355. (when org-read-date-overlay
  10356. (org-delete-overlay org-read-date-overlay)
  10357. (setq org-read-date-overlay nil)))))))
  10358. (t ; Naked prompt only
  10359. (unwind-protect
  10360. (setq ans (read-string prompt default-input nil timestr))
  10361. (when org-read-date-overlay
  10362. (org-delete-overlay org-read-date-overlay)
  10363. (setq org-read-date-overlay nil)))))
  10364. (setq final (org-read-date-analyze ans def defdecode))
  10365. (if to-time
  10366. (apply 'encode-time final)
  10367. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10368. (format "%04d-%02d-%02d %02d:%02d"
  10369. (nth 5 final) (nth 4 final) (nth 3 final)
  10370. (nth 2 final) (nth 1 final))
  10371. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10372. (defvar def)
  10373. (defvar defdecode)
  10374. (defvar with-time)
  10375. (defun org-read-date-display ()
  10376. "Display the current date prompt interpretation in the minibuffer."
  10377. (when org-read-date-display-live
  10378. (when org-read-date-overlay
  10379. (org-delete-overlay org-read-date-overlay))
  10380. (let ((p (point)))
  10381. (end-of-line 1)
  10382. (while (not (equal (buffer-substring
  10383. (max (point-min) (- (point) 4)) (point))
  10384. " "))
  10385. (insert " "))
  10386. (goto-char p))
  10387. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10388. " " (or org-ans1 org-ans2)))
  10389. (org-end-time-was-given nil)
  10390. (f (org-read-date-analyze ans def defdecode))
  10391. (fmts (if org-dcst
  10392. org-time-stamp-custom-formats
  10393. org-time-stamp-formats))
  10394. (fmt (if (or with-time
  10395. (and (boundp 'org-time-was-given) org-time-was-given))
  10396. (cdr fmts)
  10397. (car fmts)))
  10398. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10399. (when (and org-end-time-was-given
  10400. (string-match org-plain-time-of-day-regexp txt))
  10401. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10402. org-end-time-was-given
  10403. (substring txt (match-end 0)))))
  10404. (setq org-read-date-overlay
  10405. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10406. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10407. (defun org-read-date-analyze (ans def defdecode)
  10408. "Analyse the combined answer of the date prompt."
  10409. ;; FIXME: cleanup and comment
  10410. (let (delta deltan deltaw deltadef year month day
  10411. hour minute second wday pm h2 m2 tl wday1
  10412. iso-year iso-weekday iso-week iso-year iso-date)
  10413. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10414. (setq ans "+0"))
  10415. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10416. (setq ans (replace-match "" t t ans)
  10417. deltan (car delta)
  10418. deltaw (nth 1 delta)
  10419. deltadef (nth 2 delta)))
  10420. ;; Check if there is an iso week date in there
  10421. ;; If yes, sore the info and postpone interpreting it until the rest
  10422. ;; of the parsing is done
  10423. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10424. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10425. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10426. iso-week (string-to-number (match-string 2 ans)))
  10427. (setq ans (replace-match "" t t ans)))
  10428. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10429. (when (string-match
  10430. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10431. (setq year (if (match-end 2)
  10432. (string-to-number (match-string 2 ans))
  10433. (string-to-number (format-time-string "%Y")))
  10434. month (string-to-number (match-string 3 ans))
  10435. day (string-to-number (match-string 4 ans)))
  10436. (if (< year 100) (setq year (+ 2000 year)))
  10437. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10438. t nil ans)))
  10439. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10440. ;; If there is a time with am/pm, and *no* time without it, we convert
  10441. ;; so that matching will be successful.
  10442. (loop for i from 1 to 2 do ; twice, for end time as well
  10443. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10444. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10445. (setq hour (string-to-number (match-string 1 ans))
  10446. minute (if (match-end 3)
  10447. (string-to-number (match-string 3 ans))
  10448. 0)
  10449. pm (equal ?p
  10450. (string-to-char (downcase (match-string 4 ans)))))
  10451. (if (and (= hour 12) (not pm))
  10452. (setq hour 0)
  10453. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10454. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10455. t t ans))))
  10456. ;; Check if a time range is given as a duration
  10457. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10458. (setq hour (string-to-number (match-string 1 ans))
  10459. h2 (+ hour (string-to-number (match-string 3 ans)))
  10460. minute (string-to-number (match-string 2 ans))
  10461. m2 (+ minute (if (match-end 5) (string-to-number
  10462. (match-string 5 ans))0)))
  10463. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10464. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10465. t t ans)))
  10466. ;; Check if there is a time range
  10467. (when (boundp 'org-end-time-was-given)
  10468. (setq org-time-was-given nil)
  10469. (when (and (string-match org-plain-time-of-day-regexp ans)
  10470. (match-end 8))
  10471. (setq org-end-time-was-given (match-string 8 ans))
  10472. (setq ans (concat (substring ans 0 (match-beginning 7))
  10473. (substring ans (match-end 7))))))
  10474. (setq tl (parse-time-string ans)
  10475. day (or (nth 3 tl) (nth 3 defdecode))
  10476. month (or (nth 4 tl)
  10477. (if (and org-read-date-prefer-future
  10478. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10479. (1+ (nth 4 defdecode))
  10480. (nth 4 defdecode)))
  10481. year (or (nth 5 tl)
  10482. (if (and org-read-date-prefer-future
  10483. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10484. (1+ (nth 5 defdecode))
  10485. (nth 5 defdecode)))
  10486. hour (or (nth 2 tl) (nth 2 defdecode))
  10487. minute (or (nth 1 tl) (nth 1 defdecode))
  10488. second (or (nth 0 tl) 0)
  10489. wday (nth 6 tl))
  10490. ;; Special date definitions below
  10491. (cond
  10492. (iso-week
  10493. ;; There was an iso week
  10494. (setq year (or iso-year year)
  10495. day (or iso-weekday wday 1)
  10496. wday nil ; to make sure that the trigger below does not match
  10497. iso-date (calendar-gregorian-from-absolute
  10498. (calendar-absolute-from-iso
  10499. (list iso-week day year))))
  10500. ; FIXME: Should we also push ISO weeks into the future?
  10501. ; (when (and org-read-date-prefer-future
  10502. ; (not iso-year)
  10503. ; (< (calendar-absolute-from-gregorian iso-date)
  10504. ; (time-to-days (current-time))))
  10505. ; (setq year (1+ year)
  10506. ; iso-date (calendar-gregorian-from-absolute
  10507. ; (calendar-absolute-from-iso
  10508. ; (list iso-week day year)))))
  10509. (setq month (car iso-date)
  10510. year (nth 2 iso-date)
  10511. day (nth 1 iso-date)))
  10512. (deltan
  10513. (unless deltadef
  10514. (let ((now (decode-time (current-time))))
  10515. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10516. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10517. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10518. ((equal deltaw "m") (setq month (+ month deltan)))
  10519. ((equal deltaw "y") (setq year (+ year deltan)))))
  10520. ((and wday (not (nth 3 tl)))
  10521. ;; Weekday was given, but no day, so pick that day in the week
  10522. ;; on or after the derived date.
  10523. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10524. (unless (equal wday wday1)
  10525. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10526. (if (and (boundp 'org-time-was-given)
  10527. (nth 2 tl))
  10528. (setq org-time-was-given t))
  10529. (if (< year 100) (setq year (+ 2000 year)))
  10530. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10531. (list second minute hour day month year)))
  10532. (defvar parse-time-weekdays)
  10533. (defun org-read-date-get-relative (s today default)
  10534. "Check string S for special relative date string.
  10535. TODAY and DEFAULT are internal times, for today and for a default.
  10536. Return shift list (N what def-flag)
  10537. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10538. N is the number of WHATs to shift.
  10539. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10540. the DEFAULT date rather than TODAY."
  10541. (when (and
  10542. (string-match
  10543. (concat
  10544. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10545. "\\([0-9]+\\)?"
  10546. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10547. "\\([ \t]\\|$\\)") s)
  10548. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10549. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10550. (string-to-char (substring (match-string 1 s) -1))
  10551. ?+))
  10552. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10553. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10554. (what (if (match-end 3) (match-string 3 s) "d"))
  10555. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10556. (date (if rel default today))
  10557. (wday (nth 6 (decode-time date)))
  10558. delta)
  10559. (if wday1
  10560. (progn
  10561. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10562. (if (= dir ?-) (setq delta (- delta 7)))
  10563. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10564. (list delta "d" rel))
  10565. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10566. (defun org-eval-in-calendar (form &optional keepdate)
  10567. "Eval FORM in the calendar window and return to current window.
  10568. Also, store the cursor date in variable org-ans2."
  10569. (let ((sw (selected-window)))
  10570. (select-window (get-buffer-window "*Calendar*"))
  10571. (eval form)
  10572. (when (and (not keepdate) (calendar-cursor-to-date))
  10573. (let* ((date (calendar-cursor-to-date))
  10574. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10575. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10576. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10577. (select-window sw)))
  10578. (defun org-calendar-select ()
  10579. "Return to `org-read-date' with the date currently selected.
  10580. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10581. (interactive)
  10582. (when (calendar-cursor-to-date)
  10583. (let* ((date (calendar-cursor-to-date))
  10584. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10585. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10586. (if (active-minibuffer-window) (exit-minibuffer))))
  10587. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10588. "Insert a date stamp for the date given by the internal TIME.
  10589. WITH-HM means, use the stamp format that includes the time of the day.
  10590. INACTIVE means use square brackets instead of angular ones, so that the
  10591. stamp will not contribute to the agenda.
  10592. PRE and POST are optional strings to be inserted before and after the
  10593. stamp.
  10594. The command returns the inserted time stamp."
  10595. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10596. stamp)
  10597. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10598. (insert-before-markers (or pre ""))
  10599. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10600. (when (listp extra)
  10601. (setq extra (car extra))
  10602. (if (and (stringp extra)
  10603. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10604. (setq extra (format "-%02d:%02d"
  10605. (string-to-number (match-string 1 extra))
  10606. (string-to-number (match-string 2 extra))))
  10607. (setq extra nil)))
  10608. (when extra
  10609. (backward-char 1)
  10610. (insert-before-markers extra)
  10611. (forward-char 1))
  10612. (insert-before-markers (or post ""))
  10613. (setq org-last-inserted-timestamp stamp)))
  10614. (defun org-toggle-time-stamp-overlays ()
  10615. "Toggle the use of custom time stamp formats."
  10616. (interactive)
  10617. (setq org-display-custom-times (not org-display-custom-times))
  10618. (unless org-display-custom-times
  10619. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10620. (while (setq p (next-single-property-change p 'display))
  10621. (if (and (get-text-property p 'display)
  10622. (eq (get-text-property p 'face) 'org-date))
  10623. (remove-text-properties
  10624. p (setq p (next-single-property-change p 'display))
  10625. '(display t))))
  10626. (set-buffer-modified-p bmp)))
  10627. (if (featurep 'xemacs)
  10628. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10629. (org-restart-font-lock)
  10630. (setq org-table-may-need-update t)
  10631. (if org-display-custom-times
  10632. (message "Time stamps are overlayed with custom format")
  10633. (message "Time stamp overlays removed")))
  10634. (defun org-display-custom-time (beg end)
  10635. "Overlay modified time stamp format over timestamp between BEG and END."
  10636. (let* ((ts (buffer-substring beg end))
  10637. t1 w1 with-hm tf time str w2 (off 0))
  10638. (save-match-data
  10639. (setq t1 (org-parse-time-string ts t))
  10640. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10641. (setq off (- (match-end 0) (match-beginning 0)))))
  10642. (setq end (- end off))
  10643. (setq w1 (- end beg)
  10644. with-hm (and (nth 1 t1) (nth 2 t1))
  10645. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10646. time (org-fix-decoded-time t1)
  10647. str (org-add-props
  10648. (format-time-string
  10649. (substring tf 1 -1) (apply 'encode-time time))
  10650. nil 'mouse-face 'highlight)
  10651. w2 (length str))
  10652. (if (not (= w2 w1))
  10653. (add-text-properties (1+ beg) (+ 2 beg)
  10654. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10655. (if (featurep 'xemacs)
  10656. (progn
  10657. (put-text-property beg end 'invisible t)
  10658. (put-text-property beg end 'end-glyph (make-glyph str)))
  10659. (put-text-property beg end 'display str))))
  10660. (defun org-translate-time (string)
  10661. "Translate all timestamps in STRING to custom format.
  10662. But do this only if the variable `org-display-custom-times' is set."
  10663. (when org-display-custom-times
  10664. (save-match-data
  10665. (let* ((start 0)
  10666. (re org-ts-regexp-both)
  10667. t1 with-hm inactive tf time str beg end)
  10668. (while (setq start (string-match re string start))
  10669. (setq beg (match-beginning 0)
  10670. end (match-end 0)
  10671. t1 (save-match-data
  10672. (org-parse-time-string (substring string beg end) t))
  10673. with-hm (and (nth 1 t1) (nth 2 t1))
  10674. inactive (equal (substring string beg (1+ beg)) "[")
  10675. tf (funcall (if with-hm 'cdr 'car)
  10676. org-time-stamp-custom-formats)
  10677. time (org-fix-decoded-time t1)
  10678. str (format-time-string
  10679. (concat
  10680. (if inactive "[" "<") (substring tf 1 -1)
  10681. (if inactive "]" ">"))
  10682. (apply 'encode-time time))
  10683. string (replace-match str t t string)
  10684. start (+ start (length str)))))))
  10685. string)
  10686. (defun org-fix-decoded-time (time)
  10687. "Set 0 instead of nil for the first 6 elements of time.
  10688. Don't touch the rest."
  10689. (let ((n 0))
  10690. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10691. (defun org-days-to-time (timestamp-string)
  10692. "Difference between TIMESTAMP-STRING and now in days."
  10693. (- (time-to-days (org-time-string-to-time timestamp-string))
  10694. (time-to-days (current-time))))
  10695. (defun org-deadline-close (timestamp-string &optional ndays)
  10696. "Is the time in TIMESTAMP-STRING close to the current date?"
  10697. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10698. (and (< (org-days-to-time timestamp-string) ndays)
  10699. (not (org-entry-is-done-p))))
  10700. (defun org-get-wdays (ts)
  10701. "Get the deadline lead time appropriate for timestring TS."
  10702. (cond
  10703. ((<= org-deadline-warning-days 0)
  10704. ;; 0 or negative, enforce this value no matter what
  10705. (- org-deadline-warning-days))
  10706. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10707. ;; lead time is specified.
  10708. (floor (* (string-to-number (match-string 1 ts))
  10709. (cdr (assoc (match-string 2 ts)
  10710. '(("d" . 1) ("w" . 7)
  10711. ("m" . 30.4) ("y" . 365.25)))))))
  10712. ;; go for the default.
  10713. (t org-deadline-warning-days)))
  10714. (defun org-calendar-select-mouse (ev)
  10715. "Return to `org-read-date' with the date currently selected.
  10716. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10717. (interactive "e")
  10718. (mouse-set-point ev)
  10719. (when (calendar-cursor-to-date)
  10720. (let* ((date (calendar-cursor-to-date))
  10721. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10722. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10723. (if (active-minibuffer-window) (exit-minibuffer))))
  10724. (defun org-check-deadlines (ndays)
  10725. "Check if there are any deadlines due or past due.
  10726. A deadline is considered due if it happens within `org-deadline-warning-days'
  10727. days from today's date. If the deadline appears in an entry marked DONE,
  10728. it is not shown. The prefix arg NDAYS can be used to test that many
  10729. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10730. (interactive "P")
  10731. (let* ((org-warn-days
  10732. (cond
  10733. ((equal ndays '(4)) 100000)
  10734. (ndays (prefix-numeric-value ndays))
  10735. (t (abs org-deadline-warning-days))))
  10736. (case-fold-search nil)
  10737. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10738. (callback
  10739. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10740. (message "%d deadlines past-due or due within %d days"
  10741. (org-occur regexp nil callback)
  10742. org-warn-days)))
  10743. (defun org-check-before-date (date)
  10744. "Check if there are deadlines or scheduled entries before DATE."
  10745. (interactive (list (org-read-date)))
  10746. (let ((case-fold-search nil)
  10747. (regexp (concat "\\<\\(" org-deadline-string
  10748. "\\|" org-scheduled-string
  10749. "\\) *<\\([^>]+\\)>"))
  10750. (callback
  10751. (lambda () (time-less-p
  10752. (org-time-string-to-time (match-string 2))
  10753. (org-time-string-to-time date)))))
  10754. (message "%d entries before %s"
  10755. (org-occur regexp nil callback) date)))
  10756. (defun org-evaluate-time-range (&optional to-buffer)
  10757. "Evaluate a time range by computing the difference between start and end.
  10758. Normally the result is just printed in the echo area, but with prefix arg
  10759. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10760. If the time range is actually in a table, the result is inserted into the
  10761. next column.
  10762. For time difference computation, a year is assumed to be exactly 365
  10763. days in order to avoid rounding problems."
  10764. (interactive "P")
  10765. (or
  10766. (org-clock-update-time-maybe)
  10767. (save-excursion
  10768. (unless (org-at-date-range-p t)
  10769. (goto-char (point-at-bol))
  10770. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10771. (if (not (org-at-date-range-p t))
  10772. (error "Not at a time-stamp range, and none found in current line")))
  10773. (let* ((ts1 (match-string 1))
  10774. (ts2 (match-string 2))
  10775. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10776. (match-end (match-end 0))
  10777. (time1 (org-time-string-to-time ts1))
  10778. (time2 (org-time-string-to-time ts2))
  10779. (t1 (time-to-seconds time1))
  10780. (t2 (time-to-seconds time2))
  10781. (diff (abs (- t2 t1)))
  10782. (negative (< (- t2 t1) 0))
  10783. ;; (ys (floor (* 365 24 60 60)))
  10784. (ds (* 24 60 60))
  10785. (hs (* 60 60))
  10786. (fy "%dy %dd %02d:%02d")
  10787. (fy1 "%dy %dd")
  10788. (fd "%dd %02d:%02d")
  10789. (fd1 "%dd")
  10790. (fh "%02d:%02d")
  10791. y d h m align)
  10792. (if havetime
  10793. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10794. y 0
  10795. d (floor (/ diff ds)) diff (mod diff ds)
  10796. h (floor (/ diff hs)) diff (mod diff hs)
  10797. m (floor (/ diff 60)))
  10798. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10799. y 0
  10800. d (floor (+ (/ diff ds) 0.5))
  10801. h 0 m 0))
  10802. (if (not to-buffer)
  10803. (message "%s" (org-make-tdiff-string y d h m))
  10804. (if (org-at-table-p)
  10805. (progn
  10806. (goto-char match-end)
  10807. (setq align t)
  10808. (and (looking-at " *|") (goto-char (match-end 0))))
  10809. (goto-char match-end))
  10810. (if (looking-at
  10811. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10812. (replace-match ""))
  10813. (if negative (insert " -"))
  10814. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10815. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10816. (insert " " (format fh h m))))
  10817. (if align (org-table-align))
  10818. (message "Time difference inserted")))))
  10819. (defun org-make-tdiff-string (y d h m)
  10820. (let ((fmt "")
  10821. (l nil))
  10822. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10823. l (push y l)))
  10824. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10825. l (push d l)))
  10826. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10827. l (push h l)))
  10828. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10829. l (push m l)))
  10830. (apply 'format fmt (nreverse l))))
  10831. (defun org-time-string-to-time (s)
  10832. (apply 'encode-time (org-parse-time-string s)))
  10833. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10834. "Convert a time stamp to an absolute day number.
  10835. If there is a specifyer for a cyclic time stamp, get the closest date to
  10836. DAYNR.
  10837. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10838. (cond
  10839. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10840. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10841. daynr
  10842. (+ daynr 1000)))
  10843. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10844. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10845. (time-to-days (current-time))) (match-string 0 s)
  10846. prefer show-all))
  10847. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10848. (defun org-days-to-iso-week (days)
  10849. "Return the iso week number."
  10850. (require 'cal-iso)
  10851. (car (calendar-iso-from-absolute days)))
  10852. (defun org-small-year-to-year (year)
  10853. "Convert 2-digit years into 4-digit years.
  10854. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10855. The year 2000 cannot be abbreviated. Any year larger than 99
  10856. is returned unchanged."
  10857. (if (< year 38)
  10858. (setq year (+ 2000 year))
  10859. (if (< year 100)
  10860. (setq year (+ 1900 year))))
  10861. year)
  10862. (defun org-time-from-absolute (d)
  10863. "Return the time corresponding to date D.
  10864. D may be an absolute day number, or a calendar-type list (month day year)."
  10865. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10866. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10867. (defun org-calendar-holiday ()
  10868. "List of holidays, for Diary display in Org-mode."
  10869. (require 'holidays)
  10870. (let ((hl (funcall
  10871. (if (fboundp 'calendar-check-holidays)
  10872. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10873. (if hl (mapconcat 'identity hl "; "))))
  10874. (defun org-diary-sexp-entry (sexp entry date)
  10875. "Process a SEXP diary ENTRY for DATE."
  10876. (require 'diary-lib)
  10877. (let ((result (if calendar-debug-sexp
  10878. (let ((stack-trace-on-error t))
  10879. (eval (car (read-from-string sexp))))
  10880. (condition-case nil
  10881. (eval (car (read-from-string sexp)))
  10882. (error
  10883. (beep)
  10884. (message "Bad sexp at line %d in %s: %s"
  10885. (org-current-line)
  10886. (buffer-file-name) sexp)
  10887. (sleep-for 2))))))
  10888. (cond ((stringp result) result)
  10889. ((and (consp result)
  10890. (stringp (cdr result))) (cdr result))
  10891. (result entry)
  10892. (t nil))))
  10893. (defun org-diary-to-ical-string (frombuf)
  10894. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10895. This uses the icalendar.el library."
  10896. (let* ((tmpdir (if (featurep 'xemacs)
  10897. (temp-directory)
  10898. temporary-file-directory))
  10899. (tmpfile (make-temp-name
  10900. (expand-file-name "orgics" tmpdir)))
  10901. buf rtn b e)
  10902. (save-excursion
  10903. (set-buffer frombuf)
  10904. (icalendar-export-region (point-min) (point-max) tmpfile)
  10905. (setq buf (find-buffer-visiting tmpfile))
  10906. (set-buffer buf)
  10907. (goto-char (point-min))
  10908. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10909. (setq b (match-beginning 0)))
  10910. (goto-char (point-max))
  10911. (if (re-search-backward "^END:VEVENT" nil t)
  10912. (setq e (match-end 0)))
  10913. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10914. (kill-buffer buf)
  10915. (delete-file tmpfile)
  10916. rtn))
  10917. (defun org-closest-date (start current change prefer show-all)
  10918. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10919. When PREFER is `past' return a date that is either CURRENT or past.
  10920. When PREFER is `future', return a date that is either CURRENT or future.
  10921. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10922. ;; Make the proper lists from the dates
  10923. (catch 'exit
  10924. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10925. dn dw sday cday n1 n2 n0
  10926. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10927. (setq start (org-date-to-gregorian start)
  10928. current (org-date-to-gregorian
  10929. (if show-all
  10930. current
  10931. (time-to-days (current-time))))
  10932. sday (calendar-absolute-from-gregorian start)
  10933. cday (calendar-absolute-from-gregorian current))
  10934. (if (<= cday sday) (throw 'exit sday))
  10935. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10936. (setq dn (string-to-number (match-string 1 change))
  10937. dw (cdr (assoc (match-string 2 change) a1)))
  10938. (error "Invalid change specifyer: %s" change))
  10939. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10940. (cond
  10941. ((eq dw 'day)
  10942. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10943. n2 (+ n1 dn)))
  10944. ((eq dw 'year)
  10945. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10946. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10947. (setq date1 (list m d y1)
  10948. n1 (calendar-absolute-from-gregorian date1)
  10949. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10950. n2 (calendar-absolute-from-gregorian date2)))
  10951. ((eq dw 'month)
  10952. ;; approx number of month between the two dates
  10953. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10954. ;; How often does dn fit in there?
  10955. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10956. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10957. m (+ m nm)
  10958. ny (floor (/ m 12))
  10959. y (+ y ny)
  10960. m (- m (* ny 12)))
  10961. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10962. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10963. (setq m2 (+ m dn) y2 y)
  10964. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10965. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10966. (while (<= n2 cday)
  10967. (setq n1 n2 m m2 y y2)
  10968. (setq m2 (+ m dn) y2 y)
  10969. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10970. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10971. ;; Make sure n1 is the earlier date
  10972. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  10973. (if show-all
  10974. (cond
  10975. ((eq prefer 'past) n1)
  10976. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10977. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10978. (cond
  10979. ((eq prefer 'past) n1)
  10980. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10981. (t (if (= cday n1) n1 n2)))))))
  10982. (defun org-date-to-gregorian (date)
  10983. "Turn any specification of DATE into a gregorian date for the calendar."
  10984. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10985. ((and (listp date) (= (length date) 3)) date)
  10986. ((stringp date)
  10987. (setq date (org-parse-time-string date))
  10988. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10989. ((listp date)
  10990. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10991. (defun org-parse-time-string (s &optional nodefault)
  10992. "Parse the standard Org-mode time string.
  10993. This should be a lot faster than the normal `parse-time-string'.
  10994. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10995. hour and minute fields will be nil if not given."
  10996. (if (string-match org-ts-regexp0 s)
  10997. (list 0
  10998. (if (or (match-beginning 8) (not nodefault))
  10999. (string-to-number (or (match-string 8 s) "0")))
  11000. (if (or (match-beginning 7) (not nodefault))
  11001. (string-to-number (or (match-string 7 s) "0")))
  11002. (string-to-number (match-string 4 s))
  11003. (string-to-number (match-string 3 s))
  11004. (string-to-number (match-string 2 s))
  11005. nil nil nil)
  11006. (make-list 9 0)))
  11007. (defun org-timestamp-up (&optional arg)
  11008. "Increase the date item at the cursor by one.
  11009. If the cursor is on the year, change the year. If it is on the month or
  11010. the day, change that.
  11011. With prefix ARG, change by that many units."
  11012. (interactive "p")
  11013. (org-timestamp-change (prefix-numeric-value arg)))
  11014. (defun org-timestamp-down (&optional arg)
  11015. "Decrease the date item at the cursor by one.
  11016. If the cursor is on the year, change the year. If it is on the month or
  11017. the day, change that.
  11018. With prefix ARG, change by that many units."
  11019. (interactive "p")
  11020. (org-timestamp-change (- (prefix-numeric-value arg))))
  11021. (defun org-timestamp-up-day (&optional arg)
  11022. "Increase the date in the time stamp by one day.
  11023. With prefix ARG, change that many days."
  11024. (interactive "p")
  11025. (if (and (not (org-at-timestamp-p t))
  11026. (org-on-heading-p))
  11027. (org-todo 'up)
  11028. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11029. (defun org-timestamp-down-day (&optional arg)
  11030. "Decrease the date in the time stamp by one day.
  11031. With prefix ARG, change that many days."
  11032. (interactive "p")
  11033. (if (and (not (org-at-timestamp-p t))
  11034. (org-on-heading-p))
  11035. (org-todo 'down)
  11036. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11037. (defun org-at-timestamp-p (&optional inactive-ok)
  11038. "Determine if the cursor is in or at a timestamp."
  11039. (interactive)
  11040. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11041. (pos (point))
  11042. (ans (or (looking-at tsr)
  11043. (save-excursion
  11044. (skip-chars-backward "^[<\n\r\t")
  11045. (if (> (point) (point-min)) (backward-char 1))
  11046. (and (looking-at tsr)
  11047. (> (- (match-end 0) pos) -1))))))
  11048. (and ans
  11049. (boundp 'org-ts-what)
  11050. (setq org-ts-what
  11051. (cond
  11052. ((= pos (match-beginning 0)) 'bracket)
  11053. ((= pos (1- (match-end 0))) 'bracket)
  11054. ((org-pos-in-match-range pos 2) 'year)
  11055. ((org-pos-in-match-range pos 3) 'month)
  11056. ((org-pos-in-match-range pos 7) 'hour)
  11057. ((org-pos-in-match-range pos 8) 'minute)
  11058. ((or (org-pos-in-match-range pos 4)
  11059. (org-pos-in-match-range pos 5)) 'day)
  11060. ((and (> pos (or (match-end 8) (match-end 5)))
  11061. (< pos (match-end 0)))
  11062. (- pos (or (match-end 8) (match-end 5))))
  11063. (t 'day))))
  11064. ans))
  11065. (defun org-toggle-timestamp-type ()
  11066. "Toggle the type (<active> or [inactive]) of a time stamp."
  11067. (interactive)
  11068. (when (org-at-timestamp-p t)
  11069. (let ((beg (match-beginning 0)) (end (match-end 0))
  11070. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11071. (save-excursion
  11072. (goto-char beg)
  11073. (while (re-search-forward "[][<>]" end t)
  11074. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11075. t t)))
  11076. (message "Timestamp is now %sactive"
  11077. (if (equal (char-after beg) ?<) "" "in")))))
  11078. (defun org-timestamp-change (n &optional what)
  11079. "Change the date in the time stamp at point.
  11080. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11081. `year', `minute', `second'. If WHAT is not given, the cursor position
  11082. in the timestamp determines what will be changed."
  11083. (let ((pos (point))
  11084. with-hm inactive
  11085. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11086. org-ts-what
  11087. extra rem
  11088. ts time time0)
  11089. (if (not (org-at-timestamp-p t))
  11090. (error "Not at a timestamp"))
  11091. (if (and (not what) (eq org-ts-what 'bracket))
  11092. (org-toggle-timestamp-type)
  11093. (if (and (not what) (not (eq org-ts-what 'day))
  11094. org-display-custom-times
  11095. (get-text-property (point) 'display)
  11096. (not (get-text-property (1- (point)) 'display)))
  11097. (setq org-ts-what 'day))
  11098. (setq org-ts-what (or what org-ts-what)
  11099. inactive (= (char-after (match-beginning 0)) ?\[)
  11100. ts (match-string 0))
  11101. (replace-match "")
  11102. (if (string-match
  11103. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11104. ts)
  11105. (setq extra (match-string 1 ts)))
  11106. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11107. (setq with-hm t))
  11108. (setq time0 (org-parse-time-string ts))
  11109. (when (and (eq org-ts-what 'minute)
  11110. (eq current-prefix-arg nil))
  11111. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11112. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11113. (setcar (cdr time0) (+ (nth 1 time0)
  11114. (if (> n 0) (- rem) (- dm rem))))))
  11115. (setq time
  11116. (encode-time (or (car time0) 0)
  11117. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11118. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11119. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11120. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11121. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11122. (nthcdr 6 time0)))
  11123. (when (integerp org-ts-what)
  11124. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11125. (if (eq what 'calendar)
  11126. (let ((cal-date (org-get-date-from-calendar)))
  11127. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11128. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11129. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11130. (setcar time0 (or (car time0) 0))
  11131. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11132. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11133. (setq time (apply 'encode-time time0))))
  11134. (setq org-last-changed-timestamp
  11135. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11136. (org-clock-update-time-maybe)
  11137. (goto-char pos)
  11138. ;; Try to recenter the calendar window, if any
  11139. (if (and org-calendar-follow-timestamp-change
  11140. (get-buffer-window "*Calendar*" t)
  11141. (memq org-ts-what '(day month year)))
  11142. (org-recenter-calendar (time-to-days time))))))
  11143. (defun org-modify-ts-extra (s pos n dm)
  11144. "Change the different parts of the lead-time and repeat fields in timestamp."
  11145. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11146. ng h m new rem)
  11147. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11148. (cond
  11149. ((or (org-pos-in-match-range pos 2)
  11150. (org-pos-in-match-range pos 3))
  11151. (setq m (string-to-number (match-string 3 s))
  11152. h (string-to-number (match-string 2 s)))
  11153. (if (org-pos-in-match-range pos 2)
  11154. (setq h (+ h n))
  11155. (setq n (* dm (org-no-warnings (signum n))))
  11156. (when (not (= 0 (setq rem (% m dm))))
  11157. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11158. (setq m (+ m n)))
  11159. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11160. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11161. (setq h (min 24 (max 0 h)))
  11162. (setq ng 1 new (format "-%02d:%02d" h m)))
  11163. ((org-pos-in-match-range pos 6)
  11164. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11165. ((org-pos-in-match-range pos 5)
  11166. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11167. ((org-pos-in-match-range pos 9)
  11168. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11169. ((org-pos-in-match-range pos 8)
  11170. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11171. (when ng
  11172. (setq s (concat
  11173. (substring s 0 (match-beginning ng))
  11174. new
  11175. (substring s (match-end ng))))))
  11176. s))
  11177. (defun org-recenter-calendar (date)
  11178. "If the calendar is visible, recenter it to DATE."
  11179. (let* ((win (selected-window))
  11180. (cwin (get-buffer-window "*Calendar*" t))
  11181. (calendar-move-hook nil))
  11182. (when cwin
  11183. (select-window cwin)
  11184. (calendar-goto-date (if (listp date) date
  11185. (calendar-gregorian-from-absolute date)))
  11186. (select-window win))))
  11187. (defun org-goto-calendar (&optional arg)
  11188. "Go to the Emacs calendar at the current date.
  11189. If there is a time stamp in the current line, go to that date.
  11190. A prefix ARG can be used to force the current date."
  11191. (interactive "P")
  11192. (let ((tsr org-ts-regexp) diff
  11193. (calendar-move-hook nil)
  11194. (calendar-view-holidays-initially-flag nil)
  11195. (view-calendar-holidays-initially nil)
  11196. (calendar-view-diary-initially-flag nil)
  11197. (view-diary-entries-initially nil))
  11198. (if (or (org-at-timestamp-p)
  11199. (save-excursion
  11200. (beginning-of-line 1)
  11201. (looking-at (concat ".*" tsr))))
  11202. (let ((d1 (time-to-days (current-time)))
  11203. (d2 (time-to-days
  11204. (org-time-string-to-time (match-string 1)))))
  11205. (setq diff (- d2 d1))))
  11206. (calendar)
  11207. (calendar-goto-today)
  11208. (if (and diff (not arg)) (calendar-forward-day diff))))
  11209. (defun org-get-date-from-calendar ()
  11210. "Return a list (month day year) of date at point in calendar."
  11211. (with-current-buffer "*Calendar*"
  11212. (save-match-data
  11213. (calendar-cursor-to-date))))
  11214. (defun org-date-from-calendar ()
  11215. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11216. If there is already a time stamp at the cursor position, update it."
  11217. (interactive)
  11218. (if (org-at-timestamp-p t)
  11219. (org-timestamp-change 0 'calendar)
  11220. (let ((cal-date (org-get-date-from-calendar)))
  11221. (org-insert-time-stamp
  11222. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11223. (defun org-minutes-to-hh:mm-string (m)
  11224. "Compute H:MM from a number of minutes."
  11225. (let ((h (/ m 60)))
  11226. (setq m (- m (* 60 h)))
  11227. (format org-time-clocksum-format h m)))
  11228. (defun org-hh:mm-string-to-minutes (s)
  11229. "Convert a string H:MM to a number of minutes."
  11230. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11231. (+ (* (string-to-number (match-string 1 s)) 60)
  11232. (string-to-number (match-string 2 s)))
  11233. 0))
  11234. ;;;; Files
  11235. (defun org-save-all-org-buffers ()
  11236. "Save all Org-mode buffers without user confirmation."
  11237. (interactive)
  11238. (message "Saving all Org-mode buffers...")
  11239. (save-some-buffers t 'org-mode-p)
  11240. (message "Saving all Org-mode buffers... done"))
  11241. (defun org-revert-all-org-buffers ()
  11242. "Revert all Org-mode buffers.
  11243. Prompt for confirmation when there are unsaved changes."
  11244. (interactive)
  11245. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11246. (error "Abort"))
  11247. (save-excursion
  11248. (save-window-excursion
  11249. (mapc
  11250. (lambda (b)
  11251. (when (and (with-current-buffer b (org-mode-p))
  11252. (with-current-buffer b buffer-file-name))
  11253. (switch-to-buffer b)
  11254. (revert-buffer t 'no-confirm)))
  11255. (buffer-list)))))
  11256. ;;;; Agenda files
  11257. ;;;###autoload
  11258. (defun org-iswitchb (&optional arg)
  11259. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11260. With a prefix argument, restrict available to files.
  11261. With two prefix arguments, restrict available buffers to agenda files.
  11262. Due to some yet unresolved reason, the global function
  11263. `iswitchb-mode' needs to be active for this function to work."
  11264. (interactive "P")
  11265. (require 'iswitchb)
  11266. (let ((enabled iswitchb-mode) blist)
  11267. (or enabled (iswitchb-mode 1))
  11268. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11269. ((equal arg '(16)) (org-buffer-list 'agenda))
  11270. (t (org-buffer-list))))
  11271. (unwind-protect
  11272. (let ((iswitchb-make-buflist-hook
  11273. (lambda ()
  11274. (setq iswitchb-temp-buflist
  11275. (mapcar 'buffer-name blist)))))
  11276. (switch-to-buffer
  11277. (iswitchb-read-buffer
  11278. "Switch-to: " nil t))
  11279. (or enabled (iswitchb-mode -1))))))
  11280. ;;;###autoload
  11281. (defun org-ido-switchb (&optional arg)
  11282. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11283. With a prefix argument, restrict available to files.
  11284. With two prefix arguments, restrict available buffers to agenda files."
  11285. (interactive "P")
  11286. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11287. ((equal arg '(16)) (org-buffer-list 'agenda))
  11288. (t (org-buffer-list)))))
  11289. (switch-to-buffer
  11290. (org-ido-completing-read "Org buffer: "
  11291. (mapcar 'buffer-name blist)
  11292. nil t))))
  11293. (defun org-buffer-list (&optional predicate exclude-tmp)
  11294. "Return a list of Org buffers.
  11295. PREDICATE can be `export', `files' or `agenda'.
  11296. export restrict the list to Export buffers.
  11297. files restrict the list to buffers visiting Org files.
  11298. agenda restrict the list to buffers visiting agenda files.
  11299. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11300. (let* ((bfn nil)
  11301. (agenda-files (and (eq predicate 'agenda)
  11302. (mapcar 'file-truename (org-agenda-files t))))
  11303. (filter
  11304. (cond
  11305. ((eq predicate 'files)
  11306. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11307. ((eq predicate 'export)
  11308. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11309. ((eq predicate 'agenda)
  11310. (lambda (b)
  11311. (with-current-buffer b
  11312. (and (eq major-mode 'org-mode)
  11313. (setq bfn (buffer-file-name b))
  11314. (member (file-truename bfn) agenda-files)))))
  11315. (t (lambda (b) (with-current-buffer b
  11316. (or (eq major-mode 'org-mode)
  11317. (string-match "\*Org .*Export"
  11318. (buffer-name b)))))))))
  11319. (delq nil
  11320. (mapcar
  11321. (lambda(b)
  11322. (if (and (funcall filter b)
  11323. (or (not exclude-tmp)
  11324. (not (string-match "tmp" (buffer-name b)))))
  11325. b
  11326. nil))
  11327. (buffer-list)))))
  11328. (defun org-agenda-files (&optional unrestricted archives)
  11329. "Get the list of agenda files.
  11330. Optional UNRESTRICTED means return the full list even if a restriction
  11331. is currently in place.
  11332. When ARCHIVES is t, include all archive files hat are really being
  11333. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11334. `org-agenda-archives-mode' is t."
  11335. (let ((files
  11336. (cond
  11337. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11338. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11339. ((listp org-agenda-files) org-agenda-files)
  11340. (t (error "Invalid value of `org-agenda-files'")))))
  11341. (setq files (apply 'append
  11342. (mapcar (lambda (f)
  11343. (if (file-directory-p f)
  11344. (directory-files
  11345. f t org-agenda-file-regexp)
  11346. (list f)))
  11347. files)))
  11348. (when org-agenda-skip-unavailable-files
  11349. (setq files (delq nil
  11350. (mapcar (function
  11351. (lambda (file)
  11352. (and (file-readable-p file) file)))
  11353. files))))
  11354. (when (or (eq archives t)
  11355. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11356. (setq files (org-add-archive-files files)))
  11357. files))
  11358. (defun org-edit-agenda-file-list ()
  11359. "Edit the list of agenda files.
  11360. Depending on setup, this either uses customize to edit the variable
  11361. `org-agenda-files', or it visits the file that is holding the list. In the
  11362. latter case, the buffer is set up in a way that saving it automatically kills
  11363. the buffer and restores the previous window configuration."
  11364. (interactive)
  11365. (if (stringp org-agenda-files)
  11366. (let ((cw (current-window-configuration)))
  11367. (find-file org-agenda-files)
  11368. (org-set-local 'org-window-configuration cw)
  11369. (org-add-hook 'after-save-hook
  11370. (lambda ()
  11371. (set-window-configuration
  11372. (prog1 org-window-configuration
  11373. (kill-buffer (current-buffer))))
  11374. (org-install-agenda-files-menu)
  11375. (message "New agenda file list installed"))
  11376. nil 'local)
  11377. (message "%s" (substitute-command-keys
  11378. "Edit list and finish with \\[save-buffer]")))
  11379. (customize-variable 'org-agenda-files)))
  11380. (defun org-store-new-agenda-file-list (list)
  11381. "Set new value for the agenda file list and save it correctly."
  11382. (if (stringp org-agenda-files)
  11383. (let ((f org-agenda-files) b)
  11384. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11385. (with-temp-file f
  11386. (insert (mapconcat 'identity list "\n") "\n")))
  11387. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11388. (setq org-agenda-files list)
  11389. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11390. (defun org-read-agenda-file-list ()
  11391. "Read the list of agenda files from a file."
  11392. (when (file-directory-p org-agenda-files)
  11393. (error "`org-agenda-files' cannot be a single directory"))
  11394. (when (stringp org-agenda-files)
  11395. (with-temp-buffer
  11396. (insert-file-contents org-agenda-files)
  11397. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11398. ;;;###autoload
  11399. (defun org-cycle-agenda-files ()
  11400. "Cycle through the files in `org-agenda-files'.
  11401. If the current buffer visits an agenda file, find the next one in the list.
  11402. If the current buffer does not, find the first agenda file."
  11403. (interactive)
  11404. (let* ((fs (org-agenda-files t))
  11405. (files (append fs (list (car fs))))
  11406. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11407. file)
  11408. (unless files (error "No agenda files"))
  11409. (catch 'exit
  11410. (while (setq file (pop files))
  11411. (if (equal (file-truename file) tcf)
  11412. (when (car files)
  11413. (find-file (car files))
  11414. (throw 'exit t))))
  11415. (find-file (car fs)))
  11416. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11417. (defun org-agenda-file-to-front (&optional to-end)
  11418. "Move/add the current file to the top of the agenda file list.
  11419. If the file is not present in the list, it is added to the front. If it is
  11420. present, it is moved there. With optional argument TO-END, add/move to the
  11421. end of the list."
  11422. (interactive "P")
  11423. (let ((org-agenda-skip-unavailable-files nil)
  11424. (file-alist (mapcar (lambda (x)
  11425. (cons (file-truename x) x))
  11426. (org-agenda-files t)))
  11427. (ctf (file-truename buffer-file-name))
  11428. x had)
  11429. (setq x (assoc ctf file-alist) had x)
  11430. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11431. (if to-end
  11432. (setq file-alist (append (delq x file-alist) (list x)))
  11433. (setq file-alist (cons x (delq x file-alist))))
  11434. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11435. (org-install-agenda-files-menu)
  11436. (message "File %s to %s of agenda file list"
  11437. (if had "moved" "added") (if to-end "end" "front"))))
  11438. (defun org-remove-file (&optional file)
  11439. "Remove current file from the list of files in variable `org-agenda-files'.
  11440. These are the files which are being checked for agenda entries.
  11441. Optional argument FILE means, use this file instead of the current."
  11442. (interactive)
  11443. (let* ((org-agenda-skip-unavailable-files nil)
  11444. (file (or file buffer-file-name))
  11445. (true-file (file-truename file))
  11446. (afile (abbreviate-file-name file))
  11447. (files (delq nil (mapcar
  11448. (lambda (x)
  11449. (if (equal true-file
  11450. (file-truename x))
  11451. nil x))
  11452. (org-agenda-files t)))))
  11453. (if (not (= (length files) (length (org-agenda-files t))))
  11454. (progn
  11455. (org-store-new-agenda-file-list files)
  11456. (org-install-agenda-files-menu)
  11457. (message "Removed file: %s" afile))
  11458. (message "File was not in list: %s (not removed)" afile))))
  11459. (defun org-file-menu-entry (file)
  11460. (vector file (list 'find-file file) t))
  11461. (defun org-check-agenda-file (file)
  11462. "Make sure FILE exists. If not, ask user what to do."
  11463. (when (not (file-exists-p file))
  11464. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11465. (abbreviate-file-name file))
  11466. (let ((r (downcase (read-char-exclusive))))
  11467. (cond
  11468. ((equal r ?r)
  11469. (org-remove-file file)
  11470. (throw 'nextfile t))
  11471. (t (error "Abort"))))))
  11472. (defun org-get-agenda-file-buffer (file)
  11473. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11474. it to the list of buffers which might be released later."
  11475. (let ((buf (org-find-base-buffer-visiting file)))
  11476. (if buf
  11477. buf ; just return it
  11478. ;; Make a new buffer and remember it
  11479. (setq buf (find-file-noselect file))
  11480. (if buf (push buf org-agenda-new-buffers))
  11481. buf)))
  11482. (defun org-release-buffers (blist)
  11483. "Release all buffers in list, asking the user for confirmation when needed.
  11484. When a buffer is unmodified, it is just killed. When modified, it is saved
  11485. \(if the user agrees) and then killed."
  11486. (let (buf file)
  11487. (while (setq buf (pop blist))
  11488. (setq file (buffer-file-name buf))
  11489. (when (and (buffer-modified-p buf)
  11490. file
  11491. (y-or-n-p (format "Save file %s? " file)))
  11492. (with-current-buffer buf (save-buffer)))
  11493. (kill-buffer buf))))
  11494. (defun org-prepare-agenda-buffers (files)
  11495. "Create buffers for all agenda files, protect archived trees and comments."
  11496. (interactive)
  11497. (let ((pa '(:org-archived t))
  11498. (pc '(:org-comment t))
  11499. (pall '(:org-archived t :org-comment t))
  11500. (inhibit-read-only t)
  11501. (rea (concat ":" org-archive-tag ":"))
  11502. bmp file re)
  11503. (save-excursion
  11504. (save-restriction
  11505. (while (setq file (pop files))
  11506. (if (bufferp file)
  11507. (set-buffer file)
  11508. (org-check-agenda-file file)
  11509. (set-buffer (org-get-agenda-file-buffer file)))
  11510. (widen)
  11511. (setq bmp (buffer-modified-p))
  11512. (org-refresh-category-properties)
  11513. (setq org-todo-keywords-for-agenda
  11514. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11515. (setq org-done-keywords-for-agenda
  11516. (append org-done-keywords-for-agenda org-done-keywords))
  11517. (setq org-todo-keyword-alist-for-agenda
  11518. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11519. (setq org-tag-alist-for-agenda
  11520. (append org-tag-alist-for-agenda org-tag-alist))
  11521. (save-excursion
  11522. (remove-text-properties (point-min) (point-max) pall)
  11523. (when org-agenda-skip-archived-trees
  11524. (goto-char (point-min))
  11525. (while (re-search-forward rea nil t)
  11526. (if (org-on-heading-p t)
  11527. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11528. (goto-char (point-min))
  11529. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11530. (while (re-search-forward re nil t)
  11531. (add-text-properties
  11532. (match-beginning 0) (org-end-of-subtree t) pc)))
  11533. (set-buffer-modified-p bmp))))
  11534. (setq org-todo-keyword-alist-for-agenda
  11535. (org-uniquify org-todo-keyword-alist-for-agenda)
  11536. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11537. ;;;; Embedded LaTeX
  11538. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11539. "Keymap for the minor `org-cdlatex-mode'.")
  11540. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11541. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11542. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11543. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11544. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11545. (defvar org-cdlatex-texmathp-advice-is-done nil
  11546. "Flag remembering if we have applied the advice to texmathp already.")
  11547. (define-minor-mode org-cdlatex-mode
  11548. "Toggle the minor `org-cdlatex-mode'.
  11549. This mode supports entering LaTeX environment and math in LaTeX fragments
  11550. in Org-mode.
  11551. \\{org-cdlatex-mode-map}"
  11552. nil " OCDL" nil
  11553. (when org-cdlatex-mode (require 'cdlatex))
  11554. (unless org-cdlatex-texmathp-advice-is-done
  11555. (setq org-cdlatex-texmathp-advice-is-done t)
  11556. (defadvice texmathp (around org-math-always-on activate)
  11557. "Always return t in org-mode buffers.
  11558. This is because we want to insert math symbols without dollars even outside
  11559. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11560. an embedded LaTeX fragment, let texmathp do its job.
  11561. \\[org-cdlatex-mode-map]"
  11562. (interactive)
  11563. (let (p)
  11564. (cond
  11565. ((not (org-mode-p)) ad-do-it)
  11566. ((eq this-command 'cdlatex-math-symbol)
  11567. (setq ad-return-value t
  11568. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11569. (t
  11570. (let ((p (org-inside-LaTeX-fragment-p)))
  11571. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11572. (setq ad-return-value t
  11573. texmathp-why '("Org-mode embedded math" . 0))
  11574. (if p ad-do-it)))))))))
  11575. (defun turn-on-org-cdlatex ()
  11576. "Unconditionally turn on `org-cdlatex-mode'."
  11577. (org-cdlatex-mode 1))
  11578. (defun org-inside-LaTeX-fragment-p ()
  11579. "Test if point is inside a LaTeX fragment.
  11580. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11581. sequence appearing also before point.
  11582. Even though the matchers for math are configurable, this function assumes
  11583. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11584. delimiters are skipped when they have been removed by customization.
  11585. The return value is nil, or a cons cell with the delimiter and
  11586. and the position of this delimiter.
  11587. This function does a reasonably good job, but can locally be fooled by
  11588. for example currency specifications. For example it will assume being in
  11589. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11590. fragments that are properly closed, but during editing, we have to live
  11591. with the uncertainty caused by missing closing delimiters. This function
  11592. looks only before point, not after."
  11593. (catch 'exit
  11594. (let ((pos (point))
  11595. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11596. (lim (progn
  11597. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11598. (point)))
  11599. dd-on str (start 0) m re)
  11600. (goto-char pos)
  11601. (when dodollar
  11602. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11603. re (nth 1 (assoc "$" org-latex-regexps)))
  11604. (while (string-match re str start)
  11605. (cond
  11606. ((= (match-end 0) (length str))
  11607. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11608. ((= (match-end 0) (- (length str) 5))
  11609. (throw 'exit nil))
  11610. (t (setq start (match-end 0))))))
  11611. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11612. (goto-char pos)
  11613. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11614. (and (match-beginning 2) (throw 'exit nil))
  11615. ;; count $$
  11616. (while (re-search-backward "\\$\\$" lim t)
  11617. (setq dd-on (not dd-on)))
  11618. (goto-char pos)
  11619. (if dd-on (cons "$$" m))))))
  11620. (defun org-try-cdlatex-tab ()
  11621. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11622. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11623. - inside a LaTeX fragment, or
  11624. - after the first word in a line, where an abbreviation expansion could
  11625. insert a LaTeX environment."
  11626. (when org-cdlatex-mode
  11627. (cond
  11628. ((save-excursion
  11629. (skip-chars-backward "a-zA-Z0-9*")
  11630. (skip-chars-backward " \t")
  11631. (bolp))
  11632. (cdlatex-tab) t)
  11633. ((org-inside-LaTeX-fragment-p)
  11634. (cdlatex-tab) t)
  11635. (t nil))))
  11636. (defun org-cdlatex-underscore-caret (&optional arg)
  11637. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11638. Revert to the normal definition outside of these fragments."
  11639. (interactive "P")
  11640. (if (org-inside-LaTeX-fragment-p)
  11641. (call-interactively 'cdlatex-sub-superscript)
  11642. (let (org-cdlatex-mode)
  11643. (call-interactively (key-binding (vector last-input-event))))))
  11644. (defun org-cdlatex-math-modify (&optional arg)
  11645. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11646. Revert to the normal definition outside of these fragments."
  11647. (interactive "P")
  11648. (if (org-inside-LaTeX-fragment-p)
  11649. (call-interactively 'cdlatex-math-modify)
  11650. (let (org-cdlatex-mode)
  11651. (call-interactively (key-binding (vector last-input-event))))))
  11652. (defvar org-latex-fragment-image-overlays nil
  11653. "List of overlays carrying the images of latex fragments.")
  11654. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11655. (defun org-remove-latex-fragment-image-overlays ()
  11656. "Remove all overlays with LaTeX fragment images in current buffer."
  11657. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11658. (setq org-latex-fragment-image-overlays nil))
  11659. (defun org-preview-latex-fragment (&optional subtree)
  11660. "Preview the LaTeX fragment at point, or all locally or globally.
  11661. If the cursor is in a LaTeX fragment, create the image and overlay
  11662. it over the source code. If there is no fragment at point, display
  11663. all fragments in the current text, from one headline to the next. With
  11664. prefix SUBTREE, display all fragments in the current subtree. With a
  11665. double prefix `C-u C-u', or when the cursor is before the first headline,
  11666. display all fragments in the buffer.
  11667. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11668. (interactive "P")
  11669. (org-remove-latex-fragment-image-overlays)
  11670. (save-excursion
  11671. (save-restriction
  11672. (let (beg end at msg)
  11673. (cond
  11674. ((or (equal subtree '(16))
  11675. (not (save-excursion
  11676. (re-search-backward (concat "^" outline-regexp) nil t))))
  11677. (setq beg (point-min) end (point-max)
  11678. msg "Creating images for buffer...%s"))
  11679. ((equal subtree '(4))
  11680. (org-back-to-heading)
  11681. (setq beg (point) end (org-end-of-subtree t)
  11682. msg "Creating images for subtree...%s"))
  11683. (t
  11684. (if (setq at (org-inside-LaTeX-fragment-p))
  11685. (goto-char (max (point-min) (- (cdr at) 2)))
  11686. (org-back-to-heading))
  11687. (setq beg (point) end (progn (outline-next-heading) (point))
  11688. msg (if at "Creating image...%s"
  11689. "Creating images for entry...%s"))))
  11690. (message msg "")
  11691. (narrow-to-region beg end)
  11692. (goto-char beg)
  11693. (org-format-latex
  11694. (concat "ltxpng/" (file-name-sans-extension
  11695. (file-name-nondirectory
  11696. buffer-file-name)))
  11697. default-directory 'overlays msg at 'forbuffer)
  11698. (message msg "done. Use `C-c C-c' to remove images.")))))
  11699. (defvar org-latex-regexps
  11700. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11701. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11702. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11703. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11704. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11705. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11706. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11707. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11708. "Regular expressions for matching embedded LaTeX.")
  11709. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11710. "Replace LaTeX fragments with links to an image, and produce images."
  11711. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11712. (let* ((prefixnodir (file-name-nondirectory prefix))
  11713. (absprefix (expand-file-name prefix dir))
  11714. (todir (file-name-directory absprefix))
  11715. (opt org-format-latex-options)
  11716. (matchers (plist-get opt :matchers))
  11717. (re-list org-latex-regexps)
  11718. (cnt 0) txt link beg end re e checkdir
  11719. m n block linkfile movefile ov)
  11720. ;; Check if there are old images files with this prefix, and remove them
  11721. (when (file-directory-p todir)
  11722. (mapc 'delete-file
  11723. (directory-files
  11724. todir 'full
  11725. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11726. ;; Check the different regular expressions
  11727. (while (setq e (pop re-list))
  11728. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11729. block (if (nth 3 e) "\n\n" ""))
  11730. (when (member m matchers)
  11731. (goto-char (point-min))
  11732. (while (re-search-forward re nil t)
  11733. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11734. (not (get-text-property (match-beginning n)
  11735. 'org-protected)))
  11736. (setq txt (match-string n)
  11737. beg (match-beginning n) end (match-end n)
  11738. cnt (1+ cnt)
  11739. linkfile (format "%s_%04d.png" prefix cnt)
  11740. movefile (format "%s_%04d.png" absprefix cnt)
  11741. link (concat block "[[file:" linkfile "]]" block))
  11742. (if msg (message msg cnt))
  11743. (goto-char beg)
  11744. (unless checkdir ; make sure the directory exists
  11745. (setq checkdir t)
  11746. (or (file-directory-p todir) (make-directory todir)))
  11747. (org-create-formula-image
  11748. txt movefile opt forbuffer)
  11749. (if overlays
  11750. (progn
  11751. (setq ov (org-make-overlay beg end))
  11752. (if (featurep 'xemacs)
  11753. (progn
  11754. (org-overlay-put ov 'invisible t)
  11755. (org-overlay-put
  11756. ov 'end-glyph
  11757. (make-glyph (vector 'png :file movefile))))
  11758. (org-overlay-put
  11759. ov 'display
  11760. (list 'image :type 'png :file movefile :ascent 'center)))
  11761. (push ov org-latex-fragment-image-overlays)
  11762. (goto-char end))
  11763. (delete-region beg end)
  11764. (insert link))))))))
  11765. ;; This function borrows from Ganesh Swami's latex2png.el
  11766. (defun org-create-formula-image (string tofile options buffer)
  11767. (let* ((tmpdir (if (featurep 'xemacs)
  11768. (temp-directory)
  11769. temporary-file-directory))
  11770. (texfilebase (make-temp-name
  11771. (expand-file-name "orgtex" tmpdir)))
  11772. (texfile (concat texfilebase ".tex"))
  11773. (dvifile (concat texfilebase ".dvi"))
  11774. (pngfile (concat texfilebase ".png"))
  11775. (fnh (if (featurep 'xemacs)
  11776. (font-height (get-face-font 'default))
  11777. (face-attribute 'default :height nil)))
  11778. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11779. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11780. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11781. "Black"))
  11782. (bg (or (plist-get options (if buffer :background :html-background))
  11783. "Transparent")))
  11784. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11785. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11786. (with-temp-file texfile
  11787. (insert org-format-latex-header
  11788. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11789. (let ((dir default-directory))
  11790. (condition-case nil
  11791. (progn
  11792. (cd tmpdir)
  11793. (call-process "latex" nil nil nil texfile))
  11794. (error nil))
  11795. (cd dir))
  11796. (if (not (file-exists-p dvifile))
  11797. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11798. (condition-case nil
  11799. (call-process "dvipng" nil nil nil
  11800. "-E" "-fg" fg "-bg" bg
  11801. "-D" dpi
  11802. ;;"-x" scale "-y" scale
  11803. "-T" "tight"
  11804. "-o" pngfile
  11805. dvifile)
  11806. (error nil))
  11807. (if (not (file-exists-p pngfile))
  11808. (progn (message "Failed to create png file from %s" texfile) nil)
  11809. ;; Use the requested file name and clean up
  11810. (copy-file pngfile tofile 'replace)
  11811. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11812. (delete-file (concat texfilebase e)))
  11813. pngfile))))
  11814. (defun org-dvipng-color (attr)
  11815. "Return an rgb color specification for dvipng."
  11816. (apply 'format "rgb %s %s %s"
  11817. (mapcar 'org-normalize-color
  11818. (color-values (face-attribute 'default attr nil)))))
  11819. (defun org-normalize-color (value)
  11820. "Return string to be used as color value for an RGB component."
  11821. (format "%g" (/ value 65535.0)))
  11822. ;;;; Key bindings
  11823. ;; Make `C-c C-x' a prefix key
  11824. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11825. ;; TAB key with modifiers
  11826. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11827. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11828. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11829. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11830. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11831. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11832. ;; The following line is necessary under Suse GNU/Linux
  11833. (unless (featurep 'xemacs)
  11834. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11835. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11836. (define-key org-mode-map [backtab] 'org-shifttab)
  11837. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11838. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11839. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11840. ;; Cursor keys with modifiers
  11841. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11842. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11843. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11844. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11845. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11846. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11847. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11848. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11849. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11850. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11851. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11852. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11853. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11854. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11855. ;;; Extra keys for tty access.
  11856. ;; We only set them when really needed because otherwise the
  11857. ;; menus don't show the simple keys
  11858. (when (or org-use-extra-keys
  11859. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11860. (not window-system))
  11861. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11862. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11863. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11864. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11865. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11866. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11867. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11868. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11869. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11870. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11871. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11872. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11873. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11874. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11875. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11876. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11877. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11878. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11879. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11880. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11881. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11882. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11883. ;; All the other keys
  11884. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11885. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11886. (if (boundp 'narrow-map)
  11887. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11888. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11889. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11890. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11891. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11892. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11893. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11894. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11895. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11896. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11897. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11898. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11899. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11900. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11901. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11902. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11903. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11904. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11905. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11906. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11907. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11908. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11909. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11910. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11911. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11912. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11913. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11914. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11915. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11916. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11917. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11918. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11919. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11920. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11921. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11922. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11923. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11924. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11925. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11926. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11927. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11928. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11929. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11930. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11931. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11932. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11933. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11934. (org-defkey org-mode-map "\C-m" 'org-return)
  11935. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11936. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11937. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11938. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11939. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11940. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11941. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11942. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11943. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11944. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11945. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11946. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11947. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11948. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11949. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11950. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11951. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11952. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11953. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11954. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11955. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11956. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11957. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11958. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11959. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11960. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11961. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11962. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11963. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11964. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11965. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11966. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11967. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  11968. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11969. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11970. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11971. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11972. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  11973. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11974. (when (featurep 'xemacs)
  11975. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11976. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11977. (defun org-self-insert-command (N)
  11978. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11979. If the cursor is in a table looking at whitespace, the whitespace is
  11980. overwritten, and the table is not marked as requiring realignment."
  11981. (interactive "p")
  11982. (if (and (org-table-p)
  11983. (progn
  11984. ;; check if we blank the field, and if that triggers align
  11985. (and (featurep 'org-table) org-table-auto-blank-field
  11986. (member last-command
  11987. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11988. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11989. ;; got extra space, this field does not determine column width
  11990. (let (org-table-may-need-update) (org-table-blank-field))
  11991. ;; no extra space, this field may determine column width
  11992. (org-table-blank-field)))
  11993. t)
  11994. (eq N 1)
  11995. (looking-at "[^|\n]* |"))
  11996. (let (org-table-may-need-update)
  11997. (goto-char (1- (match-end 0)))
  11998. (delete-backward-char 1)
  11999. (goto-char (match-beginning 0))
  12000. (self-insert-command N))
  12001. (setq org-table-may-need-update t)
  12002. (self-insert-command N)
  12003. (org-fix-tags-on-the-fly)))
  12004. (defun org-fix-tags-on-the-fly ()
  12005. (when (and (equal (char-after (point-at-bol)) ?*)
  12006. (org-on-heading-p))
  12007. (org-align-tags-here org-tags-column)))
  12008. (defun org-delete-backward-char (N)
  12009. "Like `delete-backward-char', insert whitespace at field end in tables.
  12010. When deleting backwards, in tables this function will insert whitespace in
  12011. front of the next \"|\" separator, to keep the table aligned. The table will
  12012. still be marked for re-alignment if the field did fill the entire column,
  12013. because, in this case the deletion might narrow the column."
  12014. (interactive "p")
  12015. (if (and (org-table-p)
  12016. (eq N 1)
  12017. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12018. (looking-at ".*?|"))
  12019. (let ((pos (point))
  12020. (noalign (looking-at "[^|\n\r]* |"))
  12021. (c org-table-may-need-update))
  12022. (backward-delete-char N)
  12023. (skip-chars-forward "^|")
  12024. (insert " ")
  12025. (goto-char (1- pos))
  12026. ;; noalign: if there were two spaces at the end, this field
  12027. ;; does not determine the width of the column.
  12028. (if noalign (setq org-table-may-need-update c)))
  12029. (backward-delete-char N)
  12030. (org-fix-tags-on-the-fly)))
  12031. (defun org-delete-char (N)
  12032. "Like `delete-char', but insert whitespace at field end in tables.
  12033. When deleting characters, in tables this function will insert whitespace in
  12034. front of the next \"|\" separator, to keep the table aligned. The table will
  12035. still be marked for re-alignment if the field did fill the entire column,
  12036. because, in this case the deletion might narrow the column."
  12037. (interactive "p")
  12038. (if (and (org-table-p)
  12039. (not (bolp))
  12040. (not (= (char-after) ?|))
  12041. (eq N 1))
  12042. (if (looking-at ".*?|")
  12043. (let ((pos (point))
  12044. (noalign (looking-at "[^|\n\r]* |"))
  12045. (c org-table-may-need-update))
  12046. (replace-match (concat
  12047. (substring (match-string 0) 1 -1)
  12048. " |"))
  12049. (goto-char pos)
  12050. ;; noalign: if there were two spaces at the end, this field
  12051. ;; does not determine the width of the column.
  12052. (if noalign (setq org-table-may-need-update c)))
  12053. (delete-char N))
  12054. (delete-char N)
  12055. (org-fix-tags-on-the-fly)))
  12056. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12057. (put 'org-self-insert-command 'delete-selection t)
  12058. (put 'orgtbl-self-insert-command 'delete-selection t)
  12059. (put 'org-delete-char 'delete-selection 'supersede)
  12060. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12061. (put 'org-yank 'delete-selection 'yank)
  12062. ;; Make `flyspell-mode' delay after some commands
  12063. (put 'org-self-insert-command 'flyspell-delayed t)
  12064. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12065. (put 'org-delete-char 'flyspell-delayed t)
  12066. (put 'org-delete-backward-char 'flyspell-delayed t)
  12067. ;; Make pabbrev-mode expand after org-mode commands
  12068. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12069. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12070. ;; How to do this: Measure non-white length of current string
  12071. ;; If equal to column width, we should realign.
  12072. (defun org-remap (map &rest commands)
  12073. "In MAP, remap the functions given in COMMANDS.
  12074. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12075. (let (new old)
  12076. (while commands
  12077. (setq old (pop commands) new (pop commands))
  12078. (if (fboundp 'command-remapping)
  12079. (org-defkey map (vector 'remap old) new)
  12080. (substitute-key-definition old new map global-map)))))
  12081. (when (eq org-enable-table-editor 'optimized)
  12082. ;; If the user wants maximum table support, we need to hijack
  12083. ;; some standard editing functions
  12084. (org-remap org-mode-map
  12085. 'self-insert-command 'org-self-insert-command
  12086. 'delete-char 'org-delete-char
  12087. 'delete-backward-char 'org-delete-backward-char)
  12088. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12089. (defun org-modifier-cursor-error ()
  12090. "Throw an error, a modified cursor command was applied in wrong context."
  12091. (error "This command is active in special context like tables, headlines or items"))
  12092. (defun org-shiftselect-error ()
  12093. "Throw an error because Shift-Cursor command was applied in wrong context."
  12094. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12095. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12096. (error "This command works only in special context like headlines or timestamps.")))
  12097. (defun org-call-for-shift-select (cmd)
  12098. (let ((this-command-keys-shift-translated t))
  12099. (call-interactively cmd)))
  12100. (defun org-shifttab (&optional arg)
  12101. "Global visibility cycling or move to previous table field.
  12102. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12103. on context.
  12104. See the individual commands for more information."
  12105. (interactive "P")
  12106. (cond
  12107. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12108. ((integerp arg)
  12109. (message "Content view to level: %d" arg)
  12110. (org-content (prefix-numeric-value arg))
  12111. (setq org-cycle-global-status 'overview))
  12112. (t (call-interactively 'org-global-cycle))))
  12113. (defun org-shiftmetaleft ()
  12114. "Promote subtree or delete table column.
  12115. Calls `org-promote-subtree', `org-outdent-item',
  12116. or `org-table-delete-column', depending on context.
  12117. See the individual commands for more information."
  12118. (interactive)
  12119. (cond
  12120. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12121. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12122. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12123. (t (org-modifier-cursor-error))))
  12124. (defun org-shiftmetaright ()
  12125. "Demote subtree or insert table column.
  12126. Calls `org-demote-subtree', `org-indent-item',
  12127. or `org-table-insert-column', depending on context.
  12128. See the individual commands for more information."
  12129. (interactive)
  12130. (cond
  12131. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12132. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12133. ((org-at-item-p) (call-interactively 'org-indent-item))
  12134. (t (org-modifier-cursor-error))))
  12135. (defun org-shiftmetaup (&optional arg)
  12136. "Move subtree up or kill table row.
  12137. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12138. `org-move-item-up' depending on context. See the individual commands
  12139. for more information."
  12140. (interactive "P")
  12141. (cond
  12142. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12143. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12144. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12145. (t (org-modifier-cursor-error))))
  12146. (defun org-shiftmetadown (&optional arg)
  12147. "Move subtree down or insert table row.
  12148. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12149. `org-move-item-down', depending on context. See the individual
  12150. commands for more information."
  12151. (interactive "P")
  12152. (cond
  12153. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12154. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12155. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12156. (t (org-modifier-cursor-error))))
  12157. (defun org-metaleft (&optional arg)
  12158. "Promote heading or move table column to left.
  12159. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12160. With no specific context, calls the Emacs default `backward-word'.
  12161. See the individual commands for more information."
  12162. (interactive "P")
  12163. (cond
  12164. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12165. ((or (org-on-heading-p) (org-region-active-p))
  12166. (call-interactively 'org-do-promote))
  12167. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12168. (t (call-interactively 'backward-word))))
  12169. (defun org-metaright (&optional arg)
  12170. "Demote subtree or move table column to right.
  12171. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12172. With no specific context, calls the Emacs default `forward-word'.
  12173. See the individual commands for more information."
  12174. (interactive "P")
  12175. (cond
  12176. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12177. ((or (org-on-heading-p) (org-region-active-p))
  12178. (call-interactively 'org-do-demote))
  12179. ((org-at-item-p) (call-interactively 'org-indent-item))
  12180. (t (call-interactively 'forward-word))))
  12181. (defun org-metaup (&optional arg)
  12182. "Move subtree up or move table row up.
  12183. Calls `org-move-subtree-up' or `org-table-move-row' or
  12184. `org-move-item-up', depending on context. See the individual commands
  12185. for more information."
  12186. (interactive "P")
  12187. (cond
  12188. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12189. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12190. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12191. (t (transpose-lines 1) (beginning-of-line -1))))
  12192. (defun org-metadown (&optional arg)
  12193. "Move subtree down or move table row down.
  12194. Calls `org-move-subtree-down' or `org-table-move-row' or
  12195. `org-move-item-down', depending on context. See the individual
  12196. commands for more information."
  12197. (interactive "P")
  12198. (cond
  12199. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12200. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12201. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12202. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12203. (defun org-shiftup (&optional arg)
  12204. "Increase item in timestamp or increase priority of current headline.
  12205. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12206. depending on context. See the individual commands for more information."
  12207. (interactive "P")
  12208. (cond
  12209. ((and org-support-shift-select (org-region-active-p))
  12210. (org-call-for-shift-select 'previous-line))
  12211. ((org-at-timestamp-p t)
  12212. (call-interactively (if org-edit-timestamp-down-means-later
  12213. 'org-timestamp-down 'org-timestamp-up)))
  12214. ((and (not (eq org-support-shift-select 'always))
  12215. (org-on-heading-p))
  12216. (call-interactively 'org-priority-up))
  12217. ((and (not org-support-shift-select) (org-at-item-p))
  12218. (call-interactively 'org-previous-item))
  12219. ((org-clocktable-try-shift 'up arg))
  12220. (org-support-shift-select
  12221. (org-call-for-shift-select 'previous-line))
  12222. (t (org-shiftselect-error))))
  12223. (defun org-shiftdown (&optional arg)
  12224. "Decrease item in timestamp or decrease priority of current headline.
  12225. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12226. depending on context. See the individual commands for more information."
  12227. (interactive "P")
  12228. (cond
  12229. ((and org-support-shift-select (org-region-active-p))
  12230. (org-call-for-shift-select 'next-line))
  12231. ((org-at-timestamp-p t)
  12232. (call-interactively (if org-edit-timestamp-down-means-later
  12233. 'org-timestamp-up 'org-timestamp-down)))
  12234. ((and (not (eq org-support-shift-select 'always))
  12235. (org-on-heading-p))
  12236. (call-interactively 'org-priority-down))
  12237. ((and (not org-support-shift-select) (org-at-item-p))
  12238. (call-interactively 'org-next-item))
  12239. ((org-clocktable-try-shift 'down arg))
  12240. (org-support-shift-select
  12241. (org-call-for-shift-select 'next-line))
  12242. (t (org-shiftselect-error))))
  12243. (defun org-shiftright (&optional arg)
  12244. "Cycle the thing at point or in the current line, depending on context.
  12245. Depending on context, this does one of the following:
  12246. - switch a timestamp at point one day into the future
  12247. - on a headline, switch to the next TODO keyword.
  12248. - on an item, switch entire list to the next bullet type
  12249. - on a property line, switch to the next allowed value
  12250. - on a clocktable definition line, move time block into the future"
  12251. (interactive "P")
  12252. (cond
  12253. ((and org-support-shift-select (org-region-active-p))
  12254. (org-call-for-shift-select 'forward-char))
  12255. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12256. ((and (not (eq org-support-shift-select 'always))
  12257. (org-on-heading-p))
  12258. (org-call-with-arg 'org-todo 'right))
  12259. ((or (and org-support-shift-select
  12260. (not (eq org-support-shift-select 'always))
  12261. (org-at-item-bullet-p))
  12262. (and (not org-support-shift-select) (org-at-item-p)))
  12263. (org-call-with-arg 'org-cycle-list-bullet nil))
  12264. ((and (not (eq org-support-shift-select 'always))
  12265. (org-at-property-p))
  12266. (call-interactively 'org-property-next-allowed-value))
  12267. ((org-clocktable-try-shift 'right arg))
  12268. (org-support-shift-select
  12269. (org-call-for-shift-select 'forward-char))
  12270. (t (org-shiftselect-error))))
  12271. (defun org-shiftleft (&optional arg)
  12272. "Cycle the thing at point or in the current line, depending on context.
  12273. Depending on context, this does one of the following:
  12274. - switch a timestamp at point one day into the past
  12275. - on a headline, switch to the previous TODO keyword.
  12276. - on an item, switch entire list to the previous bullet type
  12277. - on a property line, switch to the previous allowed value
  12278. - on a clocktable definition line, move time block into the past"
  12279. (interactive "P")
  12280. (cond
  12281. ((and org-support-shift-select (org-region-active-p))
  12282. (org-call-for-shift-select 'backward-char))
  12283. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12284. ((and (not (eq org-support-shift-select 'always))
  12285. (org-on-heading-p))
  12286. (org-call-with-arg 'org-todo 'left))
  12287. ((or (and org-support-shift-select
  12288. (not (eq org-support-shift-select 'always))
  12289. (org-at-item-bullet-p))
  12290. (and (not org-support-shift-select) (org-at-item-p)))
  12291. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12292. ((and (not (eq org-support-shift-select 'always))
  12293. (org-at-property-p))
  12294. (call-interactively 'org-property-previous-allowed-value))
  12295. ((org-clocktable-try-shift 'left arg))
  12296. (org-support-shift-select
  12297. (org-call-for-shift-select 'backward-char))
  12298. (t (org-shiftselect-error))))
  12299. (defun org-shiftcontrolright ()
  12300. "Switch to next TODO set."
  12301. (interactive)
  12302. (cond
  12303. ((and org-support-shift-select (org-region-active-p))
  12304. (org-call-for-shift-select 'forward-word))
  12305. ((and (not (eq org-support-shift-select 'always))
  12306. (org-on-heading-p))
  12307. (org-call-with-arg 'org-todo 'nextset))
  12308. (org-support-shift-select
  12309. (org-call-for-shift-select 'forward-word))
  12310. (t (org-shiftselect-error))))
  12311. (defun org-shiftcontrolleft ()
  12312. "Switch to previous TODO set."
  12313. (interactive)
  12314. (cond
  12315. ((and org-support-shift-select (org-region-active-p))
  12316. (org-call-for-shift-select 'backward-word))
  12317. ((and (not (eq org-support-shift-select 'always))
  12318. (org-on-heading-p))
  12319. (org-call-with-arg 'org-todo 'previousset))
  12320. (org-support-shift-select
  12321. (org-call-for-shift-select 'backward-word))
  12322. (t (org-shiftselect-error))))
  12323. (defun org-ctrl-c-ret ()
  12324. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12325. (interactive)
  12326. (cond
  12327. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12328. (t (call-interactively 'org-insert-heading))))
  12329. (defun org-copy-special ()
  12330. "Copy region in table or copy current subtree.
  12331. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12332. See the individual commands for more information."
  12333. (interactive)
  12334. (call-interactively
  12335. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12336. (defun org-cut-special ()
  12337. "Cut region in table or cut current subtree.
  12338. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12339. See the individual commands for more information."
  12340. (interactive)
  12341. (call-interactively
  12342. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12343. (defun org-paste-special (arg)
  12344. "Paste rectangular region into table, or past subtree relative to level.
  12345. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12346. See the individual commands for more information."
  12347. (interactive "P")
  12348. (if (org-at-table-p)
  12349. (org-table-paste-rectangle)
  12350. (org-paste-subtree arg)))
  12351. (defun org-edit-special ()
  12352. "Call a special editor for the stuff at point.
  12353. When at a table, call the formula editor with `org-table-edit-formulas'.
  12354. When at the first line of an src example, call `org-edit-src-code'.
  12355. When in an #+include line, visit the include file. Otherwise call
  12356. `ffap' to visit the file at point."
  12357. (interactive)
  12358. (cond
  12359. ((org-at-table-p)
  12360. (call-interactively 'org-table-edit-formulas))
  12361. ((save-excursion
  12362. (beginning-of-line 1)
  12363. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12364. (find-file (org-trim (match-string 1))))
  12365. ((org-edit-src-code))
  12366. ((org-edit-fixed-width-region))
  12367. (t (call-interactively 'ffap))))
  12368. (defun org-ctrl-c-ctrl-c (&optional arg)
  12369. "Set tags in headline, or update according to changed information at point.
  12370. This command does many different things, depending on context:
  12371. - If the cursor is in a headline, prompt for tags and insert them
  12372. into the current line, aligned to `org-tags-column'. When called
  12373. with prefix arg, realign all tags in the current buffer.
  12374. - If the cursor is in one of the special #+KEYWORD lines, this
  12375. triggers scanning the buffer for these lines and updating the
  12376. information.
  12377. - If the cursor is inside a table, realign the table. This command
  12378. works even if the automatic table editor has been turned off.
  12379. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12380. the entire table.
  12381. - If the cursor is at a footnote reference or definition, jump to
  12382. the corresponding definition or references, respectively.
  12383. - If the cursor is a the beginning of a dynamic block, update it.
  12384. - If the cursor is inside a table created by the table.el package,
  12385. activate that table.
  12386. - If the current buffer is a remember buffer, close note and file
  12387. it. A prefix argument of 1 files to the default location
  12388. without further interaction. A prefix argument of 2 files to
  12389. the currently clocking task.
  12390. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12391. links in this buffer.
  12392. - If the cursor is on a numbered item in a plain list, renumber the
  12393. ordered list.
  12394. - If the cursor is on a checkbox, toggle it."
  12395. (interactive "P")
  12396. (let ((org-enable-table-editor t))
  12397. (cond
  12398. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12399. org-occur-highlights
  12400. org-latex-fragment-image-overlays)
  12401. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12402. (org-remove-occur-highlights)
  12403. (org-remove-latex-fragment-image-overlays)
  12404. (message "Temporary highlights/overlays removed from current buffer"))
  12405. ((and (local-variable-p 'org-finish-function (current-buffer))
  12406. (fboundp org-finish-function))
  12407. (funcall org-finish-function))
  12408. ((org-at-property-p)
  12409. (call-interactively 'org-property-action))
  12410. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12411. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12412. ((org-at-table.el-p)
  12413. (require 'table)
  12414. (beginning-of-line 1)
  12415. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12416. (call-interactively 'table-recognize-table))
  12417. ((org-at-table-p)
  12418. (org-table-maybe-eval-formula)
  12419. (if arg
  12420. (call-interactively 'org-table-recalculate)
  12421. (org-table-maybe-recalculate-line))
  12422. (call-interactively 'org-table-align))
  12423. ((or (org-footnote-at-reference-p)
  12424. (org-footnote-at-definition-p))
  12425. (call-interactively 'org-footnote-action))
  12426. ((org-at-item-checkbox-p)
  12427. (call-interactively 'org-toggle-checkbox))
  12428. ((org-at-item-p)
  12429. (call-interactively 'org-maybe-renumber-ordered-list))
  12430. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12431. ;; Dynamic block
  12432. (beginning-of-line 1)
  12433. (save-excursion (org-update-dblock)))
  12434. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12435. (cond
  12436. ((equal (match-string 1) "TBLFM")
  12437. ;; Recalculate the table before this line
  12438. (save-excursion
  12439. (beginning-of-line 1)
  12440. (skip-chars-backward " \r\n\t")
  12441. (if (org-at-table-p)
  12442. (org-call-with-arg 'org-table-recalculate t))))
  12443. (t
  12444. ; (org-set-regexps-and-options)
  12445. ; (org-restart-font-lock)
  12446. (let ((org-inhibit-startup t)) (org-mode-restart))
  12447. (message "Local setup has been refreshed"))))
  12448. (t (error "C-c C-c can do nothing useful at this location.")))))
  12449. (defun org-mode-restart ()
  12450. "Restart Org-mode, to scan again for special lines.
  12451. Also updates the keyword regular expressions."
  12452. (interactive)
  12453. (org-mode)
  12454. (message "Org-mode restarted"))
  12455. (defun org-kill-note-or-show-branches ()
  12456. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12457. (interactive)
  12458. (if (not org-finish-function)
  12459. (call-interactively 'show-branches)
  12460. (let ((org-note-abort t))
  12461. (funcall org-finish-function))))
  12462. (defun org-return (&optional indent)
  12463. "Goto next table row or insert a newline.
  12464. Calls `org-table-next-row' or `newline', depending on context.
  12465. See the individual commands for more information."
  12466. (interactive)
  12467. (cond
  12468. ((bobp) (if indent (newline-and-indent) (newline)))
  12469. ((org-at-table-p)
  12470. (org-table-justify-field-maybe)
  12471. (call-interactively 'org-table-next-row))
  12472. ((and org-return-follows-link
  12473. (eq (get-text-property (point) 'face) 'org-link))
  12474. (call-interactively 'org-open-at-point))
  12475. ((and (org-at-heading-p)
  12476. (looking-at
  12477. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12478. (org-show-entry)
  12479. (end-of-line 1)
  12480. (newline))
  12481. (t (if indent (newline-and-indent) (newline)))))
  12482. (defun org-return-indent ()
  12483. "Goto next table row or insert a newline and indent.
  12484. Calls `org-table-next-row' or `newline-and-indent', depending on
  12485. context. See the individual commands for more information."
  12486. (interactive)
  12487. (org-return t))
  12488. (defun org-ctrl-c-star ()
  12489. "Compute table, or change heading status of lines.
  12490. Calls `org-table-recalculate' or `org-toggle-heading',
  12491. depending on context."
  12492. (interactive)
  12493. (cond
  12494. ((org-at-table-p)
  12495. (call-interactively 'org-table-recalculate))
  12496. (t
  12497. ;; Convert all lines in region to list items
  12498. (call-interactively 'org-toggle-heading))))
  12499. (defun org-ctrl-c-minus ()
  12500. "Insert separator line in table or modify bullet status of line.
  12501. Also turns a plain line or a region of lines into list items.
  12502. Calls `org-table-insert-hline', `org-toggle-item', or
  12503. `org-cycle-list-bullet', depending on context."
  12504. (interactive)
  12505. (cond
  12506. ((org-at-table-p)
  12507. (call-interactively 'org-table-insert-hline))
  12508. ((org-region-active-p)
  12509. (call-interactively 'org-toggle-item))
  12510. ((org-in-item-p)
  12511. (call-interactively 'org-cycle-list-bullet))
  12512. (t
  12513. (call-interactively 'org-toggle-item))))
  12514. (defun org-toggle-item ()
  12515. "Convert headings or normal lines to items, items to normal lines.
  12516. If there is no active region, only the current line is considered.
  12517. If the first line in the region is a headline, convert all headlines to items.
  12518. If the first line in the region is an item, convert all items to normal lines.
  12519. If the first line is normal text, add an item bullet to each line."
  12520. (interactive)
  12521. (let (l2 l beg end)
  12522. (if (org-region-active-p)
  12523. (setq beg (region-beginning) end (region-end))
  12524. (setq beg (point-at-bol)
  12525. end (min (1+ (point-at-eol)) (point-max))))
  12526. (save-excursion
  12527. (goto-char end)
  12528. (setq l2 (org-current-line))
  12529. (goto-char beg)
  12530. (beginning-of-line 1)
  12531. (setq l (1- (org-current-line)))
  12532. (if (org-at-item-p)
  12533. ;; We already have items, de-itemize
  12534. (while (< (setq l (1+ l)) l2)
  12535. (when (org-at-item-p)
  12536. (goto-char (match-beginning 2))
  12537. (delete-region (match-beginning 2) (match-end 2))
  12538. (and (looking-at "[ \t]+") (replace-match "")))
  12539. (beginning-of-line 2))
  12540. (if (org-on-heading-p)
  12541. ;; Headings, convert to items
  12542. (while (< (setq l (1+ l)) l2)
  12543. (if (looking-at org-outline-regexp)
  12544. (replace-match "- " t t))
  12545. (beginning-of-line 2))
  12546. ;; normal lines, turn them into items
  12547. (while (< (setq l (1+ l)) l2)
  12548. (unless (org-at-item-p)
  12549. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12550. (replace-match "\\1- \\2")))
  12551. (beginning-of-line 2)))))))
  12552. (defun org-toggle-heading (&optional nstars)
  12553. "Convert headings to normal text, or items or text to headings.
  12554. If there is no active region, only the current line is considered.
  12555. If the first line is a heading, remove the stars from all headlines
  12556. in the region.
  12557. If the first line is a plain list item, turn all plain list items into
  12558. headings.
  12559. If the first line is a normal line, turn each and every line in the region
  12560. into a heading.
  12561. When converting a line into a heading, the number of stars is chosen
  12562. such that the lines become children of the current entry. However, when
  12563. a prefix argument is given, its value determines the number of stars to add."
  12564. (interactive "P")
  12565. (let (l2 l itemp beg end)
  12566. (if (org-region-active-p)
  12567. (setq beg (region-beginning) end (region-end))
  12568. (setq beg (point-at-bol)
  12569. end (min (1+ (point-at-eol)) (point-max))))
  12570. (save-excursion
  12571. (goto-char end)
  12572. (setq l2 (org-current-line))
  12573. (goto-char beg)
  12574. (beginning-of-line 1)
  12575. (setq l (1- (org-current-line)))
  12576. (if (org-on-heading-p)
  12577. ;; We already have headlines, de-star them
  12578. (while (< (setq l (1+ l)) l2)
  12579. (when (org-on-heading-p t)
  12580. (and (looking-at outline-regexp) (replace-match "")))
  12581. (beginning-of-line 2))
  12582. (setq itemp (org-at-item-p))
  12583. (let* ((stars
  12584. (if nstars
  12585. (make-string (prefix-numeric-value current-prefix-arg)
  12586. ?*)
  12587. (save-excursion
  12588. (re-search-backward org-complex-heading-regexp nil t)
  12589. (or (match-string 1) "*"))))
  12590. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12591. (rpl (concat stars add-stars " ")))
  12592. (while (< (setq l (1+ l)) l2)
  12593. (if itemp
  12594. (and (org-at-item-p) (replace-match rpl t t))
  12595. (unless (org-on-heading-p)
  12596. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12597. (replace-match (concat rpl (match-string 2))))))
  12598. (beginning-of-line 2)))))))
  12599. (defun org-meta-return (&optional arg)
  12600. "Insert a new heading or wrap a region in a table.
  12601. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12602. See the individual commands for more information."
  12603. (interactive "P")
  12604. (cond
  12605. ((org-at-table-p)
  12606. (call-interactively 'org-table-wrap-region))
  12607. (t (call-interactively 'org-insert-heading))))
  12608. ;;; Menu entries
  12609. ;; Define the Org-mode menus
  12610. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12611. '("Tbl"
  12612. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12613. ["Next Field" org-cycle (org-at-table-p)]
  12614. ["Previous Field" org-shifttab (org-at-table-p)]
  12615. ["Next Row" org-return (org-at-table-p)]
  12616. "--"
  12617. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12618. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12619. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12620. "--"
  12621. ("Column"
  12622. ["Move Column Left" org-metaleft (org-at-table-p)]
  12623. ["Move Column Right" org-metaright (org-at-table-p)]
  12624. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12625. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12626. ("Row"
  12627. ["Move Row Up" org-metaup (org-at-table-p)]
  12628. ["Move Row Down" org-metadown (org-at-table-p)]
  12629. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12630. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12631. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12632. "--"
  12633. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12634. ("Rectangle"
  12635. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12636. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12637. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12638. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12639. "--"
  12640. ("Calculate"
  12641. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12642. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12643. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12644. "--"
  12645. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12646. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12647. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12648. "--"
  12649. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12650. "--"
  12651. ["Sum Column/Rectangle" org-table-sum
  12652. (or (org-at-table-p) (org-region-active-p))]
  12653. ["Which Column?" org-table-current-column (org-at-table-p)])
  12654. ["Debug Formulas"
  12655. org-table-toggle-formula-debugger
  12656. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12657. ["Show Col/Row Numbers"
  12658. org-table-toggle-coordinate-overlays
  12659. :style toggle
  12660. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12661. "--"
  12662. ["Create" org-table-create (and (not (org-at-table-p))
  12663. org-enable-table-editor)]
  12664. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12665. ["Import from File" org-table-import (not (org-at-table-p))]
  12666. ["Export to File" org-table-export (org-at-table-p)]
  12667. "--"
  12668. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12669. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12670. '("Org"
  12671. ("Show/Hide"
  12672. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12673. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12674. ["Sparse Tree..." org-sparse-tree t]
  12675. ["Reveal Context" org-reveal t]
  12676. ["Show All" show-all t]
  12677. "--"
  12678. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12679. "--"
  12680. ["New Heading" org-insert-heading t]
  12681. ("Navigate Headings"
  12682. ["Up" outline-up-heading t]
  12683. ["Next" outline-next-visible-heading t]
  12684. ["Previous" outline-previous-visible-heading t]
  12685. ["Next Same Level" outline-forward-same-level t]
  12686. ["Previous Same Level" outline-backward-same-level t]
  12687. "--"
  12688. ["Jump" org-goto t])
  12689. ("Edit Structure"
  12690. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12691. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12692. "--"
  12693. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12694. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12695. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12696. "--"
  12697. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12698. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12699. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12700. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12701. "--"
  12702. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12703. "--"
  12704. ["Convert to odd levels" org-convert-to-odd-levels t]
  12705. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12706. ("Editing"
  12707. ["Emphasis..." org-emphasize t]
  12708. ["Edit Source Example" org-edit-special t]
  12709. "--"
  12710. ["Footnote new/jump" org-footnote-action t]
  12711. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12712. ("Archive"
  12713. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12714. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12715. ; :active t :keys "C-u C-c C-x C-a"]
  12716. ["Sparse trees open ARCHIVE trees"
  12717. (setq org-sparse-tree-open-archived-trees
  12718. (not org-sparse-tree-open-archived-trees))
  12719. :style toggle :selected org-sparse-tree-open-archived-trees]
  12720. ["Cycling opens ARCHIVE trees"
  12721. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12722. :style toggle :selected org-cycle-open-archived-trees]
  12723. "--"
  12724. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12725. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12726. ; ["Check and Move Children" (org-archive-subtree '(4))
  12727. ; :active t :keys "C-u C-c C-x C-s"]
  12728. )
  12729. "--"
  12730. ("TODO Lists"
  12731. ["TODO/DONE/-" org-todo t]
  12732. ("Select keyword"
  12733. ["Next keyword" org-shiftright (org-on-heading-p)]
  12734. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12735. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12736. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12737. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12738. ["Show TODO Tree" org-show-todo-tree t]
  12739. ["Global TODO list" org-todo-list t]
  12740. "--"
  12741. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12742. :selected org-enforce-todo-dependencies :style toggle :active t]
  12743. "Settings for tree at point"
  12744. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12745. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12746. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12747. ["Do Children parallel" org-toggle-ordered-property :style radio
  12748. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12749. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12750. "--"
  12751. ["Set Priority" org-priority t]
  12752. ["Priority Up" org-shiftup t]
  12753. ["Priority Down" org-shiftdown t])
  12754. ("TAGS and Properties"
  12755. ["Set Tags" org-set-tags-command t]
  12756. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12757. "--"
  12758. ["Set property" org-set-property t]
  12759. ["Column view of properties" org-columns t]
  12760. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12761. ("Dates and Scheduling"
  12762. ["Timestamp" org-time-stamp t]
  12763. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12764. ("Change Date"
  12765. ["1 Day Later" org-shiftright t]
  12766. ["1 Day Earlier" org-shiftleft t]
  12767. ["1 ... Later" org-shiftup t]
  12768. ["1 ... Earlier" org-shiftdown t])
  12769. ["Compute Time Range" org-evaluate-time-range t]
  12770. ["Schedule Item" org-schedule t]
  12771. ["Deadline" org-deadline t]
  12772. "--"
  12773. ["Custom time format" org-toggle-time-stamp-overlays
  12774. :style radio :selected org-display-custom-times]
  12775. "--"
  12776. ["Goto Calendar" org-goto-calendar t]
  12777. ["Date from Calendar" org-date-from-calendar t]
  12778. "--"
  12779. ["Start/Restart Timer" org-timer-start t]
  12780. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  12781. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  12782. ["Insert Timer String" org-timer t]
  12783. ["Insert Timer Item" org-timer-item t])
  12784. ("Logging work"
  12785. ["Clock in" org-clock-in t]
  12786. ["Clock out" org-clock-out t]
  12787. ["Clock cancel" org-clock-cancel t]
  12788. ["Goto running clock" org-clock-goto t]
  12789. ["Display times" org-clock-display t]
  12790. ["Create clock table" org-clock-report t]
  12791. "--"
  12792. ["Record DONE time"
  12793. (progn (setq org-log-done (not org-log-done))
  12794. (message "Switching to %s will %s record a timestamp"
  12795. (car org-done-keywords)
  12796. (if org-log-done "automatically" "not")))
  12797. :style toggle :selected org-log-done])
  12798. "--"
  12799. ["Agenda Command..." org-agenda t]
  12800. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12801. ("File List for Agenda")
  12802. ("Special views current file"
  12803. ["TODO Tree" org-show-todo-tree t]
  12804. ["Check Deadlines" org-check-deadlines t]
  12805. ["Timeline" org-timeline t]
  12806. ["Tags Tree" org-tags-sparse-tree t])
  12807. "--"
  12808. ("Hyperlinks"
  12809. ["Store Link (Global)" org-store-link t]
  12810. ["Insert Link" org-insert-link t]
  12811. ["Follow Link" org-open-at-point t]
  12812. "--"
  12813. ["Next link" org-next-link t]
  12814. ["Previous link" org-previous-link t]
  12815. "--"
  12816. ["Descriptive Links"
  12817. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12818. :style radio
  12819. :selected (member '(org-link) buffer-invisibility-spec)]
  12820. ["Literal Links"
  12821. (progn
  12822. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12823. :style radio
  12824. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12825. "--"
  12826. ["Export/Publish..." org-export t]
  12827. ("LaTeX"
  12828. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12829. :selected org-cdlatex-mode]
  12830. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12831. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12832. ["Modify math symbol" org-cdlatex-math-modify
  12833. (org-inside-LaTeX-fragment-p)]
  12834. ["Export LaTeX fragments as images"
  12835. (if (featurep 'org-exp)
  12836. (setq org-export-with-LaTeX-fragments
  12837. (not org-export-with-LaTeX-fragments))
  12838. (require 'org-exp))
  12839. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12840. org-export-with-LaTeX-fragments)])
  12841. "--"
  12842. ("Documentation"
  12843. ["Show Version" org-version t]
  12844. ["Info Documentation" org-info t])
  12845. ("Customize"
  12846. ["Browse Org Group" org-customize t]
  12847. "--"
  12848. ["Expand This Menu" org-create-customize-menu
  12849. (fboundp 'customize-menu-create)])
  12850. "--"
  12851. ["Refresh setup" org-mode-restart t]
  12852. ))
  12853. (defun org-info (&optional node)
  12854. "Read documentation for Org-mode in the info system.
  12855. With optional NODE, go directly to that node."
  12856. (interactive)
  12857. (info (format "(org)%s" (or node ""))))
  12858. (defun org-install-agenda-files-menu ()
  12859. (let ((bl (buffer-list)))
  12860. (save-excursion
  12861. (while bl
  12862. (set-buffer (pop bl))
  12863. (if (org-mode-p) (setq bl nil)))
  12864. (when (org-mode-p)
  12865. (easy-menu-change
  12866. '("Org") "File List for Agenda"
  12867. (append
  12868. (list
  12869. ["Edit File List" (org-edit-agenda-file-list) t]
  12870. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12871. ["Remove Current File from List" org-remove-file t]
  12872. ["Cycle through agenda files" org-cycle-agenda-files t]
  12873. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12874. "--")
  12875. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12876. ;;;; Documentation
  12877. ;;;###autoload
  12878. (defun org-require-autoloaded-modules ()
  12879. (interactive)
  12880. (mapc 'require
  12881. '(org-agenda org-archive org-attach org-clock org-colview
  12882. org-exp org-id org-export-latex org-publish
  12883. org-remember org-table org-timer)))
  12884. ;;;###autoload
  12885. (defun org-customize ()
  12886. "Call the customize function with org as argument."
  12887. (interactive)
  12888. (org-load-modules-maybe)
  12889. (org-require-autoloaded-modules)
  12890. (customize-browse 'org))
  12891. (defun org-create-customize-menu ()
  12892. "Create a full customization menu for Org-mode, insert it into the menu."
  12893. (interactive)
  12894. (org-load-modules-maybe)
  12895. (org-require-autoloaded-modules)
  12896. (if (fboundp 'customize-menu-create)
  12897. (progn
  12898. (easy-menu-change
  12899. '("Org") "Customize"
  12900. `(["Browse Org group" org-customize t]
  12901. "--"
  12902. ,(customize-menu-create 'org)
  12903. ["Set" Custom-set t]
  12904. ["Save" Custom-save t]
  12905. ["Reset to Current" Custom-reset-current t]
  12906. ["Reset to Saved" Custom-reset-saved t]
  12907. ["Reset to Standard Settings" Custom-reset-standard t]))
  12908. (message "\"Org\"-menu now contains full customization menu"))
  12909. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12910. ;;;; Miscellaneous stuff
  12911. ;;; Generally useful functions
  12912. (defun org-find-text-property-in-string (prop s)
  12913. "Return the first non-nil value of property PROP in string S."
  12914. (or (get-text-property 0 prop s)
  12915. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12916. prop s)))
  12917. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12918. "Display the given MESSAGE as a warning."
  12919. (if (fboundp 'display-warning)
  12920. (display-warning 'org message
  12921. (if (featurep 'xemacs)
  12922. 'warning
  12923. :warning))
  12924. (let ((buf (get-buffer-create "*Org warnings*")))
  12925. (with-current-buffer buf
  12926. (goto-char (point-max))
  12927. (insert "Warning (Org): " message)
  12928. (unless (bolp)
  12929. (newline)))
  12930. (display-buffer buf)
  12931. (sit-for 0))))
  12932. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12933. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12934. (if (and marker (marker-buffer marker)
  12935. (buffer-live-p (marker-buffer marker)))
  12936. (progn
  12937. (switch-to-buffer (marker-buffer marker))
  12938. (if (or (> marker (point-max)) (< marker (point-min)))
  12939. (widen))
  12940. (goto-char marker)
  12941. (org-show-context 'org-goto))
  12942. (if bookmark
  12943. (bookmark-jump bookmark)
  12944. (error "Cannot find location"))))
  12945. (defun org-quote-csv-field (s)
  12946. "Quote field for inclusion in CSV material."
  12947. (if (string-match "[\",]" s)
  12948. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12949. s))
  12950. (defun org-plist-delete (plist property)
  12951. "Delete PROPERTY from PLIST.
  12952. This is in contrast to merely setting it to 0."
  12953. (let (p)
  12954. (while plist
  12955. (if (not (eq property (car plist)))
  12956. (setq p (plist-put p (car plist) (nth 1 plist))))
  12957. (setq plist (cddr plist)))
  12958. p))
  12959. (defun org-force-self-insert (N)
  12960. "Needed to enforce self-insert under remapping."
  12961. (interactive "p")
  12962. (self-insert-command N))
  12963. (defun org-string-width (s)
  12964. "Compute width of string, ignoring invisible characters.
  12965. This ignores character with invisibility property `org-link', and also
  12966. characters with property `org-cwidth', because these will become invisible
  12967. upon the next fontification round."
  12968. (let (b l)
  12969. (when (or (eq t buffer-invisibility-spec)
  12970. (assq 'org-link buffer-invisibility-spec))
  12971. (while (setq b (text-property-any 0 (length s)
  12972. 'invisible 'org-link s))
  12973. (setq s (concat (substring s 0 b)
  12974. (substring s (or (next-single-property-change
  12975. b 'invisible s) (length s)))))))
  12976. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12977. (setq s (concat (substring s 0 b)
  12978. (substring s (or (next-single-property-change
  12979. b 'org-cwidth s) (length s))))))
  12980. (setq l (string-width s) b -1)
  12981. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12982. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12983. l))
  12984. (defun org-get-indentation (&optional line)
  12985. "Get the indentation of the current line, interpreting tabs.
  12986. When LINE is given, assume it represents a line and compute its indentation."
  12987. (if line
  12988. (if (string-match "^ *" (org-remove-tabs line))
  12989. (match-end 0))
  12990. (save-excursion
  12991. (beginning-of-line 1)
  12992. (skip-chars-forward " \t")
  12993. (current-column))))
  12994. (defun org-remove-tabs (s &optional width)
  12995. "Replace tabulators in S with spaces.
  12996. Assumes that s is a single line, starting in column 0."
  12997. (setq width (or width tab-width))
  12998. (while (string-match "\t" s)
  12999. (setq s (replace-match
  13000. (make-string
  13001. (- (* width (/ (+ (match-beginning 0) width) width))
  13002. (match-beginning 0)) ?\ )
  13003. t t s)))
  13004. s)
  13005. (defun org-fix-indentation (line ind)
  13006. "Fix indentation in LINE.
  13007. IND is a cons cell with target and minimum indentation.
  13008. If the current indentation in LINE is smaller than the minimum,
  13009. leave it alone. If it is larger than ind, set it to the target."
  13010. (let* ((l (org-remove-tabs line))
  13011. (i (org-get-indentation l))
  13012. (i1 (car ind)) (i2 (cdr ind)))
  13013. (if (>= i i2) (setq l (substring line i2)))
  13014. (if (> i1 0)
  13015. (concat (make-string i1 ?\ ) l)
  13016. l)))
  13017. (defun org-base-buffer (buffer)
  13018. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13019. (if (not buffer)
  13020. buffer
  13021. (or (buffer-base-buffer buffer)
  13022. buffer)))
  13023. (defun org-trim (s)
  13024. "Remove whitespace at beginning and end of string."
  13025. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13026. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13027. s)
  13028. (defun org-wrap (string &optional width lines)
  13029. "Wrap string to either a number of lines, or a width in characters.
  13030. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13031. that costs. If there is a word longer than WIDTH, the text is actually
  13032. wrapped to the length of that word.
  13033. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13034. many lines, whatever width that takes.
  13035. The return value is a list of lines, without newlines at the end."
  13036. (let* ((words (org-split-string string "[ \t\n]+"))
  13037. (maxword (apply 'max (mapcar 'org-string-width words)))
  13038. w ll)
  13039. (cond (width
  13040. (org-do-wrap words (max maxword width)))
  13041. (lines
  13042. (setq w maxword)
  13043. (setq ll (org-do-wrap words maxword))
  13044. (if (<= (length ll) lines)
  13045. ll
  13046. (setq ll words)
  13047. (while (> (length ll) lines)
  13048. (setq w (1+ w))
  13049. (setq ll (org-do-wrap words w)))
  13050. ll))
  13051. (t (error "Cannot wrap this")))))
  13052. (defun org-do-wrap (words width)
  13053. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13054. (let (lines line)
  13055. (while words
  13056. (setq line (pop words))
  13057. (while (and words (< (+ (length line) (length (car words))) width))
  13058. (setq line (concat line " " (pop words))))
  13059. (setq lines (push line lines)))
  13060. (nreverse lines)))
  13061. (defun org-split-string (string &optional separators)
  13062. "Splits STRING into substrings at SEPARATORS.
  13063. No empty strings are returned if there are matches at the beginning
  13064. and end of string."
  13065. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13066. (start 0)
  13067. notfirst
  13068. (list nil))
  13069. (while (and (string-match rexp string
  13070. (if (and notfirst
  13071. (= start (match-beginning 0))
  13072. (< start (length string)))
  13073. (1+ start) start))
  13074. (< (match-beginning 0) (length string)))
  13075. (setq notfirst t)
  13076. (or (eq (match-beginning 0) 0)
  13077. (and (eq (match-beginning 0) (match-end 0))
  13078. (eq (match-beginning 0) start))
  13079. (setq list
  13080. (cons (substring string start (match-beginning 0))
  13081. list)))
  13082. (setq start (match-end 0)))
  13083. (or (eq start (length string))
  13084. (setq list
  13085. (cons (substring string start)
  13086. list)))
  13087. (nreverse list)))
  13088. (defun org-context ()
  13089. "Return a list of contexts of the current cursor position.
  13090. If several contexts apply, all are returned.
  13091. Each context entry is a list with a symbol naming the context, and
  13092. two positions indicating start and end of the context. Possible
  13093. contexts are:
  13094. :headline anywhere in a headline
  13095. :headline-stars on the leading stars in a headline
  13096. :todo-keyword on a TODO keyword (including DONE) in a headline
  13097. :tags on the TAGS in a headline
  13098. :priority on the priority cookie in a headline
  13099. :item on the first line of a plain list item
  13100. :item-bullet on the bullet/number of a plain list item
  13101. :checkbox on the checkbox in a plain list item
  13102. :table in an org-mode table
  13103. :table-special on a special filed in a table
  13104. :table-table in a table.el table
  13105. :link on a hyperlink
  13106. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13107. :target on a <<target>>
  13108. :radio-target on a <<<radio-target>>>
  13109. :latex-fragment on a LaTeX fragment
  13110. :latex-preview on a LaTeX fragment with overlayed preview image
  13111. This function expects the position to be visible because it uses font-lock
  13112. faces as a help to recognize the following contexts: :table-special, :link,
  13113. and :keyword."
  13114. (let* ((f (get-text-property (point) 'face))
  13115. (faces (if (listp f) f (list f)))
  13116. (p (point)) clist o)
  13117. ;; First the large context
  13118. (cond
  13119. ((org-on-heading-p t)
  13120. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13121. (when (progn
  13122. (beginning-of-line 1)
  13123. (looking-at org-todo-line-tags-regexp))
  13124. (push (org-point-in-group p 1 :headline-stars) clist)
  13125. (push (org-point-in-group p 2 :todo-keyword) clist)
  13126. (push (org-point-in-group p 4 :tags) clist))
  13127. (goto-char p)
  13128. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13129. (if (looking-at "\\[#[A-Z0-9]\\]")
  13130. (push (org-point-in-group p 0 :priority) clist)))
  13131. ((org-at-item-p)
  13132. (push (org-point-in-group p 2 :item-bullet) clist)
  13133. (push (list :item (point-at-bol)
  13134. (save-excursion (org-end-of-item) (point)))
  13135. clist)
  13136. (and (org-at-item-checkbox-p)
  13137. (push (org-point-in-group p 0 :checkbox) clist)))
  13138. ((org-at-table-p)
  13139. (push (list :table (org-table-begin) (org-table-end)) clist)
  13140. (if (memq 'org-formula faces)
  13141. (push (list :table-special
  13142. (previous-single-property-change p 'face)
  13143. (next-single-property-change p 'face)) clist)))
  13144. ((org-at-table-p 'any)
  13145. (push (list :table-table) clist)))
  13146. (goto-char p)
  13147. ;; Now the small context
  13148. (cond
  13149. ((org-at-timestamp-p)
  13150. (push (org-point-in-group p 0 :timestamp) clist))
  13151. ((memq 'org-link faces)
  13152. (push (list :link
  13153. (previous-single-property-change p 'face)
  13154. (next-single-property-change p 'face)) clist))
  13155. ((memq 'org-special-keyword faces)
  13156. (push (list :keyword
  13157. (previous-single-property-change p 'face)
  13158. (next-single-property-change p 'face)) clist))
  13159. ((org-on-target-p)
  13160. (push (org-point-in-group p 0 :target) clist)
  13161. (goto-char (1- (match-beginning 0)))
  13162. (if (looking-at org-radio-target-regexp)
  13163. (push (org-point-in-group p 0 :radio-target) clist))
  13164. (goto-char p))
  13165. ((setq o (car (delq nil
  13166. (mapcar
  13167. (lambda (x)
  13168. (if (memq x org-latex-fragment-image-overlays) x))
  13169. (org-overlays-at (point))))))
  13170. (push (list :latex-fragment
  13171. (org-overlay-start o) (org-overlay-end o)) clist)
  13172. (push (list :latex-preview
  13173. (org-overlay-start o) (org-overlay-end o)) clist))
  13174. ((org-inside-LaTeX-fragment-p)
  13175. ;; FIXME: positions wrong.
  13176. (push (list :latex-fragment (point) (point)) clist)))
  13177. (setq clist (nreverse (delq nil clist)))
  13178. clist))
  13179. ;; FIXME: Compare with at-regexp-p Do we need both?
  13180. (defun org-in-regexp (re &optional nlines visually)
  13181. "Check if point is inside a match of regexp.
  13182. Normally only the current line is checked, but you can include NLINES extra
  13183. lines both before and after point into the search.
  13184. If VISUALLY is set, require that the cursor is not after the match but
  13185. really on, so that the block visually is on the match."
  13186. (catch 'exit
  13187. (let ((pos (point))
  13188. (eol (point-at-eol (+ 1 (or nlines 0))))
  13189. (inc (if visually 1 0)))
  13190. (save-excursion
  13191. (beginning-of-line (- 1 (or nlines 0)))
  13192. (while (re-search-forward re eol t)
  13193. (if (and (<= (match-beginning 0) pos)
  13194. (>= (+ inc (match-end 0)) pos))
  13195. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13196. (defun org-at-regexp-p (regexp)
  13197. "Is point inside a match of REGEXP in the current line?"
  13198. (catch 'exit
  13199. (save-excursion
  13200. (let ((pos (point)) (end (point-at-eol)))
  13201. (beginning-of-line 1)
  13202. (while (re-search-forward regexp end t)
  13203. (if (and (<= (match-beginning 0) pos)
  13204. (>= (match-end 0) pos))
  13205. (throw 'exit t)))
  13206. nil))))
  13207. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13208. "Call `multi-occur' with buffers for all agenda files."
  13209. (interactive "sOrg-files matching: \np")
  13210. (let* ((files (org-agenda-files))
  13211. (tnames (mapcar 'file-truename files))
  13212. (extra org-agenda-text-search-extra-files)
  13213. f)
  13214. (when (eq (car extra) 'agenda-archives)
  13215. (setq extra (cdr extra))
  13216. (setq files (org-add-archive-files files)))
  13217. (while (setq f (pop extra))
  13218. (unless (member (file-truename f) tnames)
  13219. (add-to-list 'files f 'append)
  13220. (add-to-list 'tnames (file-truename f) 'append)))
  13221. (multi-occur
  13222. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13223. regexp)))
  13224. (if (boundp 'occur-mode-find-occurrence-hook)
  13225. ;; Emacs 23
  13226. (add-hook 'occur-mode-find-occurrence-hook
  13227. (lambda ()
  13228. (when (org-mode-p)
  13229. (org-reveal))))
  13230. ;; Emacs 22
  13231. (defadvice occur-mode-goto-occurrence
  13232. (after org-occur-reveal activate)
  13233. (and (org-mode-p) (org-reveal)))
  13234. (defadvice occur-mode-goto-occurrence-other-window
  13235. (after org-occur-reveal activate)
  13236. (and (org-mode-p) (org-reveal)))
  13237. (defadvice occur-mode-display-occurrence
  13238. (after org-occur-reveal activate)
  13239. (when (org-mode-p)
  13240. (let ((pos (occur-mode-find-occurrence)))
  13241. (with-current-buffer (marker-buffer pos)
  13242. (save-excursion
  13243. (goto-char pos)
  13244. (org-reveal)))))))
  13245. (defun org-uniquify (list)
  13246. "Remove duplicate elements from LIST."
  13247. (let (res)
  13248. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13249. res))
  13250. (defun org-delete-all (elts list)
  13251. "Remove all elements in ELTS from LIST."
  13252. (while elts
  13253. (setq list (delete (pop elts) list)))
  13254. list)
  13255. (defun org-back-over-empty-lines ()
  13256. "Move backwards over whitespace, to the beginning of the first empty line.
  13257. Returns the number of empty lines passed."
  13258. (let ((pos (point)))
  13259. (skip-chars-backward " \t\n\r")
  13260. (beginning-of-line 2)
  13261. (goto-char (min (point) pos))
  13262. (count-lines (point) pos)))
  13263. (defun org-skip-whitespace ()
  13264. (skip-chars-forward " \t\n\r"))
  13265. (defun org-point-in-group (point group &optional context)
  13266. "Check if POINT is in match-group GROUP.
  13267. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13268. match. If the match group does ot exist or point is not inside it,
  13269. return nil."
  13270. (and (match-beginning group)
  13271. (>= point (match-beginning group))
  13272. (<= point (match-end group))
  13273. (if context
  13274. (list context (match-beginning group) (match-end group))
  13275. t)))
  13276. (defun org-switch-to-buffer-other-window (&rest args)
  13277. "Switch to buffer in a second window on the current frame.
  13278. In particular, do not allow pop-up frames."
  13279. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13280. special-display-function)
  13281. (apply 'switch-to-buffer-other-window args)))
  13282. (defun org-combine-plists (&rest plists)
  13283. "Create a single property list from all plists in PLISTS.
  13284. The process starts by copying the first list, and then setting properties
  13285. from the other lists. Settings in the last list are the most significant
  13286. ones and overrule settings in the other lists."
  13287. (let ((rtn (copy-sequence (pop plists)))
  13288. p v ls)
  13289. (while plists
  13290. (setq ls (pop plists))
  13291. (while ls
  13292. (setq p (pop ls) v (pop ls))
  13293. (setq rtn (plist-put rtn p v))))
  13294. rtn))
  13295. (defun org-move-line-down (arg)
  13296. "Move the current line down. With prefix argument, move it past ARG lines."
  13297. (interactive "p")
  13298. (let ((col (current-column))
  13299. beg end pos)
  13300. (beginning-of-line 1) (setq beg (point))
  13301. (beginning-of-line 2) (setq end (point))
  13302. (beginning-of-line (+ 1 arg))
  13303. (setq pos (move-marker (make-marker) (point)))
  13304. (insert (delete-and-extract-region beg end))
  13305. (goto-char pos)
  13306. (org-move-to-column col)))
  13307. (defun org-move-line-up (arg)
  13308. "Move the current line up. With prefix argument, move it past ARG lines."
  13309. (interactive "p")
  13310. (let ((col (current-column))
  13311. beg end pos)
  13312. (beginning-of-line 1) (setq beg (point))
  13313. (beginning-of-line 2) (setq end (point))
  13314. (beginning-of-line (- arg))
  13315. (setq pos (move-marker (make-marker) (point)))
  13316. (insert (delete-and-extract-region beg end))
  13317. (goto-char pos)
  13318. (org-move-to-column col)))
  13319. (defun org-replace-escapes (string table)
  13320. "Replace %-escapes in STRING with values in TABLE.
  13321. TABLE is an association list with keys like \"%a\" and string values.
  13322. The sequences in STRING may contain normal field width and padding information,
  13323. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13324. so values can contain further %-escapes if they are define later in TABLE."
  13325. (let ((case-fold-search nil)
  13326. e re rpl)
  13327. (while (setq e (pop table))
  13328. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13329. (while (string-match re string)
  13330. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13331. (cdr e)))
  13332. (setq string (replace-match rpl t t string))))
  13333. string))
  13334. (defun org-sublist (list start end)
  13335. "Return a section of LIST, from START to END.
  13336. Counting starts at 1."
  13337. (let (rtn (c start))
  13338. (setq list (nthcdr (1- start) list))
  13339. (while (and list (<= c end))
  13340. (push (pop list) rtn)
  13341. (setq c (1+ c)))
  13342. (nreverse rtn)))
  13343. (defun org-find-base-buffer-visiting (file)
  13344. "Like `find-buffer-visiting' but alway return the base buffer and
  13345. not an indirect buffer."
  13346. (let ((buf (find-buffer-visiting file)))
  13347. (if buf
  13348. (or (buffer-base-buffer buf) buf)
  13349. nil)))
  13350. (defun org-image-file-name-regexp (&optional extensions)
  13351. "Return regexp matching the file names of images.
  13352. If EXTENSIONS is given, only match these."
  13353. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13354. (image-file-name-regexp)
  13355. (let ((image-file-name-extensions
  13356. (or extensions
  13357. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13358. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13359. (concat "\\."
  13360. (regexp-opt (nconc (mapcar 'upcase
  13361. image-file-name-extensions)
  13362. image-file-name-extensions)
  13363. t)
  13364. "\\'"))))
  13365. (defun org-file-image-p (file &optional extensions)
  13366. "Return non-nil if FILE is an image."
  13367. (save-match-data
  13368. (string-match (org-image-file-name-regexp extensions) file)))
  13369. (defun org-get-cursor-date ()
  13370. "Return the date at cursor in as a time.
  13371. This works in the calendar and in the agenda, anywhere else it just
  13372. returns the current time."
  13373. (let (date day defd)
  13374. (cond
  13375. ((eq major-mode 'calendar-mode)
  13376. (setq date (calendar-cursor-to-date)
  13377. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13378. ((eq major-mode 'org-agenda-mode)
  13379. (setq day (get-text-property (point) 'day))
  13380. (if day
  13381. (setq date (calendar-gregorian-from-absolute day)
  13382. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13383. (nth 2 date))))))
  13384. (or defd (current-time))))
  13385. (defvar org-agenda-action-marker (make-marker)
  13386. "Marker pointing to the entry for the next agenda action.")
  13387. (defun org-mark-entry-for-agenda-action ()
  13388. "Mark the current entry as target of an agenda action.
  13389. Agenda actions are actions executed from the agenda with the key `k',
  13390. which make use of the date at the cursor."
  13391. (interactive)
  13392. (move-marker org-agenda-action-marker
  13393. (save-excursion (org-back-to-heading t) (point))
  13394. (current-buffer))
  13395. (message
  13396. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13397. ;;; Paragraph filling stuff.
  13398. ;; We want this to be just right, so use the full arsenal.
  13399. (defun org-indent-line-function ()
  13400. "Indent line like previous, but further if previous was headline or item."
  13401. (interactive)
  13402. (let* ((pos (point))
  13403. (itemp (org-at-item-p))
  13404. column bpos bcol tpos tcol bullet btype bullet-type)
  13405. ;; Find the previous relevant line
  13406. (beginning-of-line 1)
  13407. (cond
  13408. ((looking-at "#") (setq column 0))
  13409. ((looking-at "\\*+ ") (setq column 0))
  13410. (t
  13411. (beginning-of-line 0)
  13412. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  13413. (beginning-of-line 0))
  13414. (cond
  13415. ((looking-at "\\*+[ \t]+")
  13416. (if (not org-adapt-indentation)
  13417. (setq column 0)
  13418. (goto-char (match-end 0))
  13419. (setq column (current-column))))
  13420. ((org-in-item-p)
  13421. (org-beginning-of-item)
  13422. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13423. (setq bpos (match-beginning 1) tpos (match-end 0)
  13424. bcol (progn (goto-char bpos) (current-column))
  13425. tcol (progn (goto-char tpos) (current-column))
  13426. bullet (match-string 1)
  13427. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13428. (if (> tcol (+ bcol org-description-max-indent))
  13429. (setq tcol (+ bcol 5)))
  13430. (if (not itemp)
  13431. (setq column tcol)
  13432. (goto-char pos)
  13433. (beginning-of-line 1)
  13434. (if (looking-at "\\S-")
  13435. (progn
  13436. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13437. (setq bullet (match-string 1)
  13438. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13439. (setq column (if (equal btype bullet-type) bcol tcol)))
  13440. (setq column (org-get-indentation)))))
  13441. (t (setq column (org-get-indentation))))))
  13442. (goto-char pos)
  13443. (if (<= (current-column) (current-indentation))
  13444. (org-indent-line-to column)
  13445. (save-excursion (org-indent-line-to column)))
  13446. (setq column (current-column))
  13447. (beginning-of-line 1)
  13448. (if (looking-at
  13449. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13450. (replace-match (concat "\\1" (format org-property-format
  13451. (match-string 2) (match-string 3)))
  13452. t nil))
  13453. (org-move-to-column column)))
  13454. (defun org-set-autofill-regexps ()
  13455. (interactive)
  13456. ;; In the paragraph separator we include headlines, because filling
  13457. ;; text in a line directly attached to a headline would otherwise
  13458. ;; fill the headline as well.
  13459. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13460. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13461. ;; The paragraph starter includes hand-formatted lists.
  13462. (org-set-local 'paragraph-start
  13463. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13464. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13465. ;; But only if the user has not turned off tables or fixed-width regions
  13466. (org-set-local
  13467. 'auto-fill-inhibit-regexp
  13468. (concat "\\*+ \\|#\\+"
  13469. "\\|[ \t]*" org-keyword-time-regexp
  13470. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13471. (concat
  13472. "\\|[ \t]*["
  13473. (if org-enable-table-editor "|" "")
  13474. (if org-enable-fixed-width-editor ":" "")
  13475. "]"))))
  13476. ;; We use our own fill-paragraph function, to make sure that tables
  13477. ;; and fixed-width regions are not wrapped. That function will pass
  13478. ;; through to `fill-paragraph' when appropriate.
  13479. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13480. ; Adaptive filling: To get full control, first make sure that
  13481. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13482. (org-set-local 'adaptive-fill-regexp "\000")
  13483. (org-set-local 'adaptive-fill-function
  13484. 'org-adaptive-fill-function)
  13485. (org-set-local
  13486. 'align-mode-rules-list
  13487. '((org-in-buffer-settings
  13488. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13489. (modes . '(org-mode))))))
  13490. (defun org-fill-paragraph (&optional justify)
  13491. "Re-align a table, pass through to fill-paragraph if no table."
  13492. (let ((table-p (org-at-table-p))
  13493. (table.el-p (org-at-table.el-p)))
  13494. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13495. (save-excursion (goto-char (point-at-bol))
  13496. (looking-at outline-regexp)))
  13497. t) ; skip headlines
  13498. (table.el-p t) ; skip table.el tables
  13499. (table-p (org-table-align) t) ; align org-mode tables
  13500. (t nil)))) ; call paragraph-fill
  13501. ;; For reference, this is the default value of adaptive-fill-regexp
  13502. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13503. (defun org-adaptive-fill-function ()
  13504. "Return a fill prefix for org-mode files.
  13505. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13506. work correctly."
  13507. (cond ((looking-at "#[ \t]+")
  13508. (match-string 0))
  13509. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13510. (save-excursion
  13511. (if (> (match-end 1) (+ (match-beginning 1)
  13512. org-description-max-indent))
  13513. (goto-char (+ (match-beginning 1) 5))
  13514. (goto-char (match-end 0)))
  13515. (make-string (current-column) ?\ )))
  13516. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13517. (save-excursion
  13518. (goto-char (match-end 0))
  13519. (make-string (current-column) ?\ )))
  13520. (t nil)))
  13521. ;;; Other stuff.
  13522. (defun org-toggle-fixed-width-section (arg)
  13523. "Toggle the fixed-width export.
  13524. If there is no active region, the QUOTE keyword at the current headline is
  13525. inserted or removed. When present, it causes the text between this headline
  13526. and the next to be exported as fixed-width text, and unmodified.
  13527. If there is an active region, this command adds or removes a colon as the
  13528. first character of this line. If the first character of a line is a colon,
  13529. this line is also exported in fixed-width font."
  13530. (interactive "P")
  13531. (let* ((cc 0)
  13532. (regionp (org-region-active-p))
  13533. (beg (if regionp (region-beginning) (point)))
  13534. (end (if regionp (region-end)))
  13535. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13536. (case-fold-search nil)
  13537. (re "[ \t]*\\(:\\)")
  13538. off)
  13539. (if regionp
  13540. (save-excursion
  13541. (goto-char beg)
  13542. (setq cc (current-column))
  13543. (beginning-of-line 1)
  13544. (setq off (looking-at re))
  13545. (while (> nlines 0)
  13546. (setq nlines (1- nlines))
  13547. (beginning-of-line 1)
  13548. (cond
  13549. (arg
  13550. (org-move-to-column cc t)
  13551. (insert ":\n")
  13552. (forward-line -1))
  13553. ((and off (looking-at re))
  13554. (replace-match "" t t nil 1))
  13555. ((not off) (org-move-to-column cc t) (insert ":")))
  13556. (forward-line 1)))
  13557. (save-excursion
  13558. (org-back-to-heading)
  13559. (if (looking-at (concat outline-regexp
  13560. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13561. (replace-match "" t t nil 1)
  13562. (if (looking-at outline-regexp)
  13563. (progn
  13564. (goto-char (match-end 0))
  13565. (insert org-quote-string " "))))))))
  13566. ;;;; Functions extending outline functionality
  13567. (defun org-beginning-of-line (&optional arg)
  13568. "Go to the beginning of the current line. If that is invisible, continue
  13569. to a visible line beginning. This makes the function of C-a more intuitive.
  13570. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13571. first attempt, and only move to after the tags when the cursor is already
  13572. beyond the end of the headline."
  13573. (interactive "P")
  13574. (let ((pos (point)) refpos)
  13575. (beginning-of-line 1)
  13576. (if (bobp)
  13577. nil
  13578. (backward-char 1)
  13579. (if (org-invisible-p)
  13580. (while (and (not (bobp)) (org-invisible-p))
  13581. (backward-char 1)
  13582. (beginning-of-line 1))
  13583. (forward-char 1)))
  13584. (when org-special-ctrl-a/e
  13585. (cond
  13586. ((and (looking-at org-complex-heading-regexp)
  13587. (= (char-after (match-end 1)) ?\ ))
  13588. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13589. (point-at-eol)))
  13590. (goto-char
  13591. (if (eq org-special-ctrl-a/e t)
  13592. (cond ((> pos refpos) refpos)
  13593. ((= pos (point)) refpos)
  13594. (t (point)))
  13595. (cond ((> pos (point)) (point))
  13596. ((not (eq last-command this-command)) (point))
  13597. (t refpos)))))
  13598. ((org-at-item-p)
  13599. (goto-char
  13600. (if (eq org-special-ctrl-a/e t)
  13601. (cond ((> pos (match-end 4)) (match-end 4))
  13602. ((= pos (point)) (match-end 4))
  13603. (t (point)))
  13604. (cond ((> pos (point)) (point))
  13605. ((not (eq last-command this-command)) (point))
  13606. (t (match-end 4))))))))
  13607. (org-no-warnings
  13608. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13609. (defun org-end-of-line (&optional arg)
  13610. "Go to the end of the line.
  13611. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13612. first attempt, and only move to after the tags when the cursor is already
  13613. beyond the end of the headline."
  13614. (interactive "P")
  13615. (if (or (not org-special-ctrl-a/e)
  13616. (not (org-on-heading-p)))
  13617. (end-of-line arg)
  13618. (let ((pos (point)))
  13619. (beginning-of-line 1)
  13620. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13621. (if (eq org-special-ctrl-a/e t)
  13622. (if (or (< pos (match-beginning 1))
  13623. (= pos (match-end 0)))
  13624. (goto-char (match-beginning 1))
  13625. (goto-char (match-end 0)))
  13626. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13627. (goto-char (match-end 0))
  13628. (goto-char (match-beginning 1))))
  13629. (end-of-line arg))))
  13630. (org-no-warnings
  13631. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13632. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13633. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13634. (defun org-kill-line (&optional arg)
  13635. "Kill line, to tags or end of line."
  13636. (interactive "P")
  13637. (cond
  13638. ((or (not org-special-ctrl-k)
  13639. (bolp)
  13640. (not (org-on-heading-p)))
  13641. (call-interactively 'kill-line))
  13642. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13643. (kill-region (point) (match-beginning 1))
  13644. (org-set-tags nil t))
  13645. (t (kill-region (point) (point-at-eol)))))
  13646. (define-key org-mode-map "\C-k" 'org-kill-line)
  13647. (defun org-yank (&optional arg)
  13648. "Yank. If the kill is a subtree, treat it specially.
  13649. This command will look at the current kill and check if is a single
  13650. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13651. cursor is at the beginning of a line or after the stars of a currently
  13652. empty headline, then the yank is handled specially. How exactly depends
  13653. on the value of the following variables, both set by default.
  13654. org-yank-folded-subtrees
  13655. When set, the subtree(s) will be folded after insertion, but only
  13656. if doing so would now swallow text after the yanked text.
  13657. org-yank-adjusted-subtrees
  13658. When set, the subtree will be promoted or demoted in order to
  13659. fit into the local outline tree structure, which means that the level
  13660. will be adjusted so that it becomes the smaller one of the two
  13661. *visible* surrounding headings.
  13662. Any prefix to this command will cause `yank' to be called directly with
  13663. no special treatment. In particular, a simple `C-u' prefix will just
  13664. plainly yank the text as it is.
  13665. \[1] Basically, the test checks if the first non-white line is a heading
  13666. and if there are no other headings with fewer stars."
  13667. (interactive "P")
  13668. (setq this-command 'yank)
  13669. (if arg
  13670. (call-interactively 'yank)
  13671. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13672. (and (org-kill-is-subtree-p)
  13673. (or (bolp)
  13674. (and (looking-at "[ \t]*$")
  13675. (string-match
  13676. "\\`\\*+\\'"
  13677. (buffer-substring (point-at-bol) (point)))))))
  13678. swallowp)
  13679. (cond
  13680. ((and subtreep org-yank-folded-subtrees)
  13681. (let ((beg (point))
  13682. end)
  13683. (if (and subtreep org-yank-adjusted-subtrees)
  13684. (org-paste-subtree nil nil 'for-yank)
  13685. (call-interactively 'yank))
  13686. (setq end (point))
  13687. (goto-char beg)
  13688. (when (and (bolp) subtreep
  13689. (not (setq swallowp
  13690. (org-yank-folding-would-swallow-text beg end))))
  13691. (or (looking-at outline-regexp)
  13692. (re-search-forward (concat "^" outline-regexp) end t))
  13693. (while (and (< (point) end) (looking-at outline-regexp))
  13694. (hide-subtree)
  13695. (org-cycle-show-empty-lines 'folded)
  13696. (condition-case nil
  13697. (outline-forward-same-level 1)
  13698. (error (goto-char end)))))
  13699. (when swallowp
  13700. (message
  13701. "Yanked text not folded because that would swallow text"))
  13702. (goto-char end)
  13703. (skip-chars-forward " \t\n\r")
  13704. (beginning-of-line 1)
  13705. (push-mark beg 'nomsg)))
  13706. ((and subtreep org-yank-adjusted-subtrees)
  13707. (let ((beg (point-at-bol)))
  13708. (org-paste-subtree nil nil 'for-yank)
  13709. (push-mark beg 'nomsg)))
  13710. (t
  13711. (call-interactively 'yank))))))
  13712. (defun org-yank-folding-would-swallow-text (beg end)
  13713. "Would hide-subtree at BEG swallow any text after END?"
  13714. (let (level)
  13715. (save-excursion
  13716. (goto-char beg)
  13717. (when (or (looking-at outline-regexp)
  13718. (re-search-forward (concat "^" outline-regexp) end t))
  13719. (setq level (org-outline-level)))
  13720. (goto-char end)
  13721. (skip-chars-forward " \t\r\n\v\f")
  13722. (if (or (eobp)
  13723. (and (bolp) (looking-at org-outline-regexp)
  13724. (<= (org-outline-level) level)))
  13725. nil ; Nothing would be swallowed
  13726. t)))) ; something would swallow
  13727. (define-key org-mode-map "\C-y" 'org-yank)
  13728. (defun org-invisible-p ()
  13729. "Check if point is at a character currently not visible."
  13730. ;; Early versions of noutline don't have `outline-invisible-p'.
  13731. (if (fboundp 'outline-invisible-p)
  13732. (outline-invisible-p)
  13733. (get-char-property (point) 'invisible)))
  13734. (defun org-invisible-p2 ()
  13735. "Check if point is at a character currently not visible."
  13736. (save-excursion
  13737. (if (and (eolp) (not (bobp))) (backward-char 1))
  13738. ;; Early versions of noutline don't have `outline-invisible-p'.
  13739. (if (fboundp 'outline-invisible-p)
  13740. (outline-invisible-p)
  13741. (get-char-property (point) 'invisible))))
  13742. (defun org-back-to-heading (&optional invisible-ok)
  13743. "Call `outline-back-to-heading', but provide a better error message."
  13744. (condition-case nil
  13745. (outline-back-to-heading invisible-ok)
  13746. (error (error "Before first headline at position %d in buffer %s"
  13747. (point) (current-buffer)))))
  13748. (defun org-before-first-heading-p ()
  13749. "Before first heading?"
  13750. (save-excursion
  13751. (null (re-search-backward "^\\*+ " nil t))))
  13752. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13753. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13754. (defun org-at-heading-or-item-p ()
  13755. (or (org-on-heading-p) (org-at-item-p)))
  13756. (defun org-on-target-p ()
  13757. (or (org-in-regexp org-radio-target-regexp)
  13758. (org-in-regexp org-target-regexp)))
  13759. (defun org-up-heading-all (arg)
  13760. "Move to the heading line of which the present line is a subheading.
  13761. This function considers both visible and invisible heading lines.
  13762. With argument, move up ARG levels."
  13763. (if (fboundp 'outline-up-heading-all)
  13764. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13765. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13766. (defun org-up-heading-safe ()
  13767. "Move to the heading line of which the present line is a subheading.
  13768. This version will not throw an error. It will return the level of the
  13769. headline found, or nil if no higher level is found."
  13770. (let (start-level re)
  13771. (org-back-to-heading t)
  13772. (setq start-level (funcall outline-level))
  13773. (if (equal start-level 1)
  13774. nil
  13775. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13776. (if (re-search-backward re nil t)
  13777. (funcall outline-level)))))
  13778. (defun org-first-sibling-p ()
  13779. "Is this heading the first child of its parents?"
  13780. (interactive)
  13781. (let ((re (concat "^" outline-regexp))
  13782. level l)
  13783. (unless (org-at-heading-p t)
  13784. (error "Not at a heading"))
  13785. (setq level (funcall outline-level))
  13786. (save-excursion
  13787. (if (not (re-search-backward re nil t))
  13788. t
  13789. (setq l (funcall outline-level))
  13790. (< l level)))))
  13791. (defun org-goto-sibling (&optional previous)
  13792. "Goto the next sibling, even if it is invisible.
  13793. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13794. when a sibling was found. When none is found, return nil and don't
  13795. move point."
  13796. (let ((fun (if previous 're-search-backward 're-search-forward))
  13797. (pos (point))
  13798. (re (concat "^" outline-regexp))
  13799. level l)
  13800. (when (condition-case nil (org-back-to-heading t) (error nil))
  13801. (setq level (funcall outline-level))
  13802. (catch 'exit
  13803. (or previous (forward-char 1))
  13804. (while (funcall fun re nil t)
  13805. (setq l (funcall outline-level))
  13806. (when (< l level) (goto-char pos) (throw 'exit nil))
  13807. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13808. (goto-char pos)
  13809. nil))))
  13810. (defun org-show-siblings ()
  13811. "Show all siblings of the current headline."
  13812. (save-excursion
  13813. (while (org-goto-sibling) (org-flag-heading nil)))
  13814. (save-excursion
  13815. (while (org-goto-sibling 'previous)
  13816. (org-flag-heading nil))))
  13817. (defun org-show-hidden-entry ()
  13818. "Show an entry where even the heading is hidden."
  13819. (save-excursion
  13820. (org-show-entry)))
  13821. (defun org-flag-heading (flag &optional entry)
  13822. "Flag the current heading. FLAG non-nil means make invisible.
  13823. When ENTRY is non-nil, show the entire entry."
  13824. (save-excursion
  13825. (org-back-to-heading t)
  13826. ;; Check if we should show the entire entry
  13827. (if entry
  13828. (progn
  13829. (org-show-entry)
  13830. (save-excursion
  13831. (and (outline-next-heading)
  13832. (org-flag-heading nil))))
  13833. (outline-flag-region (max (point-min) (1- (point)))
  13834. (save-excursion (outline-end-of-heading) (point))
  13835. flag))))
  13836. (defun org-forward-same-level (arg)
  13837. "Move forward to the ARG'th subheading at same level as this one.
  13838. Stop at the first and last subheadings of a superior heading.
  13839. This is like outline-forward-same-level, but invisible headings are ok."
  13840. (interactive "p")
  13841. (org-back-to-heading t)
  13842. (while (> arg 0)
  13843. (let ((point-to-move-to (save-excursion
  13844. (org-get-next-sibling))))
  13845. (if point-to-move-to
  13846. (progn
  13847. (goto-char point-to-move-to)
  13848. (setq arg (1- arg)))
  13849. (progn
  13850. (setq arg 0)
  13851. (error "No following same-level heading"))))))
  13852. (defun org-get-next-sibling ()
  13853. "Move to next heading of the same level, and return point.
  13854. If there is no such heading, return nil.
  13855. This is like outline-next-sibling, but invisible headings are ok."
  13856. (let ((level (funcall outline-level)))
  13857. (outline-next-heading)
  13858. (while (and (not (eobp)) (> (funcall outline-level) level))
  13859. (outline-next-heading))
  13860. (if (or (eobp) (< (funcall outline-level) level))
  13861. nil
  13862. (point))))
  13863. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13864. ;; This is an exact copy of the original function, but it uses
  13865. ;; `org-back-to-heading', to make it work also in invisible
  13866. ;; trees. And is uses an invisible-OK argument.
  13867. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13868. (org-back-to-heading invisible-OK)
  13869. (let ((first t)
  13870. (level (funcall outline-level)))
  13871. (while (and (not (eobp))
  13872. (or first (> (funcall outline-level) level)))
  13873. (setq first nil)
  13874. (outline-next-heading))
  13875. (unless to-heading
  13876. (if (memq (preceding-char) '(?\n ?\^M))
  13877. (progn
  13878. ;; Go to end of line before heading
  13879. (forward-char -1)
  13880. (if (memq (preceding-char) '(?\n ?\^M))
  13881. ;; leave blank line before heading
  13882. (forward-char -1))))))
  13883. (point))
  13884. (defun org-show-subtree ()
  13885. "Show everything after this heading at deeper levels."
  13886. (outline-flag-region
  13887. (point)
  13888. (save-excursion
  13889. (outline-end-of-subtree) (outline-next-heading) (point))
  13890. nil))
  13891. (defun org-show-entry ()
  13892. "Show the body directly following this heading.
  13893. Show the heading too, if it is currently invisible."
  13894. (interactive)
  13895. (save-excursion
  13896. (condition-case nil
  13897. (progn
  13898. (org-back-to-heading t)
  13899. (outline-flag-region
  13900. (max (point-min) (1- (point)))
  13901. (save-excursion
  13902. (re-search-forward
  13903. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13904. (or (match-beginning 1) (point-max)))
  13905. nil))
  13906. (error nil))))
  13907. (defun org-make-options-regexp (kwds &optional extra)
  13908. "Make a regular expression for keyword lines."
  13909. (concat
  13910. "^"
  13911. "#?[ \t]*\\+\\("
  13912. (mapconcat 'regexp-quote kwds "\\|")
  13913. (if extra (concat "\\|" extra))
  13914. "\\):[ \t]*"
  13915. "\\(.+\\)"))
  13916. ;; Make isearch reveal the necessary context
  13917. (defun org-isearch-end ()
  13918. "Reveal context after isearch exits."
  13919. (when isearch-success ; only if search was successful
  13920. (if (featurep 'xemacs)
  13921. ;; Under XEmacs, the hook is run in the correct place,
  13922. ;; we directly show the context.
  13923. (org-show-context 'isearch)
  13924. ;; In Emacs the hook runs *before* restoring the overlays.
  13925. ;; So we have to use a one-time post-command-hook to do this.
  13926. ;; (Emacs 22 has a special variable, see function `org-mode')
  13927. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13928. isearch-mode-end-hook-quit)
  13929. ;; Only when the isearch was not quitted.
  13930. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13931. 'append 'local)))))
  13932. (defun org-isearch-post-command ()
  13933. "Remove self from hook, and show context."
  13934. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13935. (org-show-context 'isearch))
  13936. ;;;; Integration with and fixes for other packages
  13937. ;;; Imenu support
  13938. (defvar org-imenu-markers nil
  13939. "All markers currently used by Imenu.")
  13940. (make-variable-buffer-local 'org-imenu-markers)
  13941. (defun org-imenu-new-marker (&optional pos)
  13942. "Return a new marker for use by Imenu, and remember the marker."
  13943. (let ((m (make-marker)))
  13944. (move-marker m (or pos (point)))
  13945. (push m org-imenu-markers)
  13946. m))
  13947. (defun org-imenu-get-tree ()
  13948. "Produce the index for Imenu."
  13949. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13950. (setq org-imenu-markers nil)
  13951. (let* ((n org-imenu-depth)
  13952. (re (concat "^" outline-regexp))
  13953. (subs (make-vector (1+ n) nil))
  13954. (last-level 0)
  13955. m level head)
  13956. (save-excursion
  13957. (save-restriction
  13958. (widen)
  13959. (goto-char (point-max))
  13960. (while (re-search-backward re nil t)
  13961. (setq level (org-reduced-level (funcall outline-level)))
  13962. (when (<= level n)
  13963. (looking-at org-complex-heading-regexp)
  13964. (setq head (org-link-display-format
  13965. (org-match-string-no-properties 4))
  13966. m (org-imenu-new-marker))
  13967. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13968. (if (>= level last-level)
  13969. (push (cons head m) (aref subs level))
  13970. (push (cons head (aref subs (1+ level))) (aref subs level))
  13971. (loop for i from (1+ level) to n do (aset subs i nil)))
  13972. (setq last-level level)))))
  13973. (aref subs 1)))
  13974. (eval-after-load "imenu"
  13975. '(progn
  13976. (add-hook 'imenu-after-jump-hook
  13977. (lambda ()
  13978. (if (eq major-mode 'org-mode)
  13979. (org-show-context 'org-goto))))))
  13980. (defun org-link-display-format (link)
  13981. "Replace a link with either the description, or the link target
  13982. if no description is present"
  13983. (save-match-data
  13984. (if (string-match org-bracket-link-analytic-regexp link)
  13985. (replace-match (or (match-string 5 link)
  13986. (concat (match-string 1 link)
  13987. (match-string 3 link)))
  13988. nil nil link)
  13989. link)))
  13990. ;; Speedbar support
  13991. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13992. "Overlay marking the agenda restriction line in speedbar.")
  13993. (org-overlay-put org-speedbar-restriction-lock-overlay
  13994. 'face 'org-agenda-restriction-lock)
  13995. (org-overlay-put org-speedbar-restriction-lock-overlay
  13996. 'help-echo "Agendas are currently limited to this item.")
  13997. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13998. (defun org-speedbar-set-agenda-restriction ()
  13999. "Restrict future agenda commands to the location at point in speedbar.
  14000. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14001. (interactive)
  14002. (require 'org-agenda)
  14003. (let (p m tp np dir txt)
  14004. (cond
  14005. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14006. 'org-imenu t))
  14007. (setq m (get-text-property p 'org-imenu-marker))
  14008. (save-excursion
  14009. (save-restriction
  14010. (set-buffer (marker-buffer m))
  14011. (goto-char m)
  14012. (org-agenda-set-restriction-lock 'subtree))))
  14013. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14014. 'speedbar-function 'speedbar-find-file))
  14015. (setq tp (previous-single-property-change
  14016. (1+ p) 'speedbar-function)
  14017. np (next-single-property-change
  14018. tp 'speedbar-function)
  14019. dir (speedbar-line-directory)
  14020. txt (buffer-substring-no-properties (or tp (point-min))
  14021. (or np (point-max))))
  14022. (save-excursion
  14023. (save-restriction
  14024. (set-buffer (find-file-noselect
  14025. (let ((default-directory dir))
  14026. (expand-file-name txt))))
  14027. (unless (org-mode-p)
  14028. (error "Cannot restrict to non-Org-mode file"))
  14029. (org-agenda-set-restriction-lock 'file))))
  14030. (t (error "Don't know how to restrict Org-mode's agenda")))
  14031. (org-move-overlay org-speedbar-restriction-lock-overlay
  14032. (point-at-bol) (point-at-eol))
  14033. (setq current-prefix-arg nil)
  14034. (org-agenda-maybe-redo)))
  14035. (eval-after-load "speedbar"
  14036. '(progn
  14037. (speedbar-add-supported-extension ".org")
  14038. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14039. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14040. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14041. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14042. (add-hook 'speedbar-visiting-tag-hook
  14043. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14044. ;;; Fixes and Hacks for problems with other packages
  14045. ;; Make flyspell not check words in links, to not mess up our keymap
  14046. (defun org-mode-flyspell-verify ()
  14047. "Don't let flyspell put overlays at active buttons."
  14048. (not (get-text-property (point) 'keymap)))
  14049. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14050. (eval-after-load "bookmark"
  14051. '(if (boundp 'bookmark-after-jump-hook)
  14052. ;; We can use the hook
  14053. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14054. ;; Hook not available, use advice
  14055. (defadvice bookmark-jump (after org-make-visible activate)
  14056. "Make the position visible."
  14057. (org-bookmark-jump-unhide))))
  14058. ;; Make sure saveplace show the location if it was hidden
  14059. (eval-after-load "saveplace"
  14060. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14061. "Make the position visible."
  14062. (org-bookmark-jump-unhide)))
  14063. (defun org-bookmark-jump-unhide ()
  14064. "Unhide the current position, to show the bookmark location."
  14065. (and (org-mode-p)
  14066. (or (org-invisible-p)
  14067. (save-excursion (goto-char (max (point-min) (1- (point))))
  14068. (org-invisible-p)))
  14069. (org-show-context 'bookmark-jump)))
  14070. ;; Make session.el ignore our circular variable
  14071. (eval-after-load "session"
  14072. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14073. ;;;; Experimental code
  14074. (defun org-closed-in-range ()
  14075. "Sparse tree of items closed in a certain time range.
  14076. Still experimental, may disappear in the future."
  14077. (interactive)
  14078. ;; Get the time interval from the user.
  14079. (let* ((time1 (time-to-seconds
  14080. (org-read-date nil 'to-time nil "Starting date: ")))
  14081. (time2 (time-to-seconds
  14082. (org-read-date nil 'to-time nil "End date:")))
  14083. ;; callback function
  14084. (callback (lambda ()
  14085. (let ((time
  14086. (time-to-seconds
  14087. (apply 'encode-time
  14088. (org-parse-time-string
  14089. (match-string 1))))))
  14090. ;; check if time in interval
  14091. (and (>= time time1) (<= time time2))))))
  14092. ;; make tree, check each match with the callback
  14093. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14094. ;;;; Finish up
  14095. (provide 'org)
  14096. (run-hooks 'org-load-hook)
  14097. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14098. ;;; org.el ends here