org.el 874 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro org-with-limited-levels.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches an headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-at-clock-log-p "org-clock" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function orgtbl-mode "org-table" (&optional arg))
  112. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  113. (declare-function org-beamer-mode "org-beamer" ())
  114. (declare-function org-table-edit-field "org-table" (arg))
  115. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  116. (declare-function org-id-get-create "org-id" (&optional force))
  117. (declare-function org-id-find-id-file "org-id" (id))
  118. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  119. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (autoload 'org-element-at-point "org-element")
  125. (autoload 'org-element-type "org-element")
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-type "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-property "org-element" (property element))
  131. (declare-function org-element-put-property "org-element"
  132. (element property value))
  133. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  136. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  137. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  138. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  139. ;; load languages based on value of `org-babel-load-languages'
  140. (defvar org-babel-load-languages)
  141. ;;;###autoload
  142. (defun org-babel-do-load-languages (sym value)
  143. "Load the languages defined in `org-babel-load-languages'."
  144. (set-default sym value)
  145. (mapc (lambda (pair)
  146. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  147. (if active
  148. (progn
  149. (require (intern (concat "ob-" lang))))
  150. (progn
  151. (funcall 'fmakunbound
  152. (intern (concat "org-babel-execute:" lang)))
  153. (funcall 'fmakunbound
  154. (intern (concat "org-babel-expand-body:" lang)))))))
  155. org-babel-load-languages))
  156. (defcustom org-babel-load-languages '((emacs-lisp . t))
  157. "Languages which can be evaluated in Org-mode buffers.
  158. This list can be used to load support for any of the languages
  159. below, note that each language will depend on a different set of
  160. system executables and/or Emacs modes. When a language is
  161. \"loaded\", then code blocks in that language can be evaluated
  162. with `org-babel-execute-src-block' bound by default to C-c
  163. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  164. be set to remove code block evaluation from the C-c C-c
  165. keybinding. By default only Emacs Lisp (which has no
  166. requirements) is loaded."
  167. :group 'org-babel
  168. :set 'org-babel-do-load-languages
  169. :version "24.1"
  170. :type '(alist :tag "Babel Languages"
  171. :key-type
  172. (choice
  173. (const :tag "Awk" awk)
  174. (const :tag "C" C)
  175. (const :tag "R" R)
  176. (const :tag "Asymptote" asymptote)
  177. (const :tag "Calc" calc)
  178. (const :tag "Clojure" clojure)
  179. (const :tag "CSS" css)
  180. (const :tag "Ditaa" ditaa)
  181. (const :tag "Dot" dot)
  182. (const :tag "Emacs Lisp" emacs-lisp)
  183. (const :tag "Fortran" fortran)
  184. (const :tag "Gnuplot" gnuplot)
  185. (const :tag "Haskell" haskell)
  186. (const :tag "IO" io)
  187. (const :tag "Java" java)
  188. (const :tag "Javascript" js)
  189. (const :tag "LaTeX" latex)
  190. (const :tag "Ledger" ledger)
  191. (const :tag "Lilypond" lilypond)
  192. (const :tag "Lisp" lisp)
  193. (const :tag "Makefile" makefile)
  194. (const :tag "Maxima" maxima)
  195. (const :tag "Matlab" matlab)
  196. (const :tag "Mscgen" mscgen)
  197. (const :tag "Ocaml" ocaml)
  198. (const :tag "Octave" octave)
  199. (const :tag "Org" org)
  200. (const :tag "Perl" perl)
  201. (const :tag "Pico Lisp" picolisp)
  202. (const :tag "PlantUML" plantuml)
  203. (const :tag "Python" python)
  204. (const :tag "Ruby" ruby)
  205. (const :tag "Sass" sass)
  206. (const :tag "Scala" scala)
  207. (const :tag "Scheme" scheme)
  208. (const :tag "Screen" screen)
  209. (const :tag "Shell Script" sh)
  210. (const :tag "Shen" shen)
  211. (const :tag "Sql" sql)
  212. (const :tag "Sqlite" sqlite))
  213. :value-type (boolean :tag "Activate" :value t)))
  214. ;;;; Customization variables
  215. (defcustom org-clone-delete-id nil
  216. "Remove ID property of clones of a subtree.
  217. When non-nil, clones of a subtree don't inherit the ID property.
  218. Otherwise they inherit the ID property with a new unique
  219. identifier."
  220. :type 'boolean
  221. :version "24.1"
  222. :group 'org-id)
  223. ;;; Version
  224. (org-check-version)
  225. ;;;###autoload
  226. (defun org-version (&optional here full message)
  227. "Show the org-mode version in the echo area.
  228. With prefix argument HERE, insert it at point.
  229. When FULL is non-nil, use a verbose version string.
  230. When MESSAGE is non-nil, display a message with the version."
  231. (interactive "P")
  232. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  233. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  234. (load-suffixes (list ".el"))
  235. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  236. (org-trash (or
  237. (and (fboundp 'org-release) (fboundp 'org-git-version))
  238. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  239. (load-suffixes save-load-suffixes)
  240. (org-version (org-release))
  241. (git-version (org-git-version))
  242. (version (format "Org-mode version %s (%s @ %s)"
  243. org-version
  244. git-version
  245. (if org-install-dir
  246. (if (string= org-dir org-install-dir)
  247. org-install-dir
  248. (concat "mixed installation! " org-install-dir " and " org-dir))
  249. "org-loaddefs.el can not be found!")))
  250. (_version (if full version org-version)))
  251. (if (org-called-interactively-p 'interactive)
  252. (if here
  253. (insert version)
  254. (message version))
  255. (if message (message _version))
  256. _version)))
  257. (defconst org-version (org-version))
  258. ;;; Compatibility constants
  259. ;;; The custom variables
  260. (defgroup org nil
  261. "Outline-based notes management and organizer."
  262. :tag "Org"
  263. :group 'outlines
  264. :group 'calendar)
  265. (defcustom org-mode-hook nil
  266. "Mode hook for Org-mode, run after the mode was turned on."
  267. :group 'org
  268. :type 'hook)
  269. (defcustom org-load-hook nil
  270. "Hook that is run after org.el has been loaded."
  271. :group 'org
  272. :type 'hook)
  273. (defcustom org-log-buffer-setup-hook nil
  274. "Hook that is run after an Org log buffer is created."
  275. :group 'org
  276. :version "24.1"
  277. :type 'hook)
  278. (defvar org-modules) ; defined below
  279. (defvar org-modules-loaded nil
  280. "Have the modules been loaded already?")
  281. (defun org-load-modules-maybe (&optional force)
  282. "Load all extensions listed in `org-modules'."
  283. (when (or force (not org-modules-loaded))
  284. (mapc (lambda (ext)
  285. (condition-case nil (require ext)
  286. (error (message "Problems while trying to load feature `%s'" ext))))
  287. org-modules)
  288. (setq org-modules-loaded t)))
  289. (defun org-set-modules (var value)
  290. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  291. (set var value)
  292. (when (featurep 'org)
  293. (org-load-modules-maybe 'force)))
  294. (when (org-bound-and-true-p org-modules)
  295. (let ((a (member 'org-infojs org-modules)))
  296. (and a (setcar a 'org-jsinfo))))
  297. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  298. "Modules that should always be loaded together with org.el.
  299. If a description starts with <C>, the file is not part of Emacs
  300. and loading it will require that you have downloaded and properly installed
  301. the org-mode distribution.
  302. You can also use this system to load external packages (i.e. neither Org
  303. core modules, nor modules from the CONTRIB directory). Just add symbols
  304. to the end of the list. If the package is called org-xyz.el, then you need
  305. to add the symbol `xyz', and the package must have a call to
  306. (provide 'org-xyz)"
  307. :group 'org
  308. :set 'org-set-modules
  309. :type
  310. '(set :greedy t
  311. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  312. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  313. (const :tag " crypt: Encryption of subtrees" org-crypt)
  314. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  315. (const :tag " docview: Links to doc-view buffers" org-docview)
  316. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  317. (const :tag " id: Global IDs for identifying entries" org-id)
  318. (const :tag " info: Links to Info nodes" org-info)
  319. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  320. (const :tag " habit: Track your consistency with habits" org-habit)
  321. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  322. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  323. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  324. (const :tag " mew Links to Mew folders/messages" org-mew)
  325. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  326. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  327. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  328. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  329. (const :tag " vm: Links to VM folders/messages" org-vm)
  330. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  331. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  332. (const :tag " mouse: Additional mouse support" org-mouse)
  333. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  334. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  335. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  336. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  337. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  338. (const :tag "C collector: Collect properties into tables" org-collector)
  339. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  340. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  341. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  342. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  343. (const :tag "C eval: Include command output as text" org-eval)
  344. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  345. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  346. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  347. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  348. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  349. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  350. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  351. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  352. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  353. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  354. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  355. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  356. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  357. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  358. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  359. (const :tag "C registry: A registry for Org-mode links" org-registry)
  360. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  361. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  362. (const :tag "C secretary: Team management with org-mode" org-secretary)
  363. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  364. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  365. (const :tag "C track: Keep up with Org-mode development" org-track)
  366. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  367. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  368. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  369. (defcustom org-support-shift-select nil
  370. "Non-nil means make shift-cursor commands select text when possible.
  371. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  372. start selecting a region, or enlarge regions started in this way.
  373. In Org-mode, in special contexts, these same keys are used for
  374. other purposes, important enough to compete with shift selection.
  375. Org tries to balance these needs by supporting `shift-select-mode'
  376. outside these special contexts, under control of this variable.
  377. The default of this variable is nil, to avoid confusing behavior. Shifted
  378. cursor keys will then execute Org commands in the following contexts:
  379. - on a headline, changing TODO state (left/right) and priority (up/down)
  380. - on a time stamp, changing the time
  381. - in a plain list item, changing the bullet type
  382. - in a property definition line, switching between allowed values
  383. - in the BEGIN line of a clock table (changing the time block).
  384. Outside these contexts, the commands will throw an error.
  385. When this variable is t and the cursor is not in a special
  386. context, Org-mode will support shift-selection for making and
  387. enlarging regions. To make this more effective, the bullet
  388. cycling will no longer happen anywhere in an item line, but only
  389. if the cursor is exactly on the bullet.
  390. If you set this variable to the symbol `always', then the keys
  391. will not be special in headlines, property lines, and item lines,
  392. to make shift selection work there as well. If this is what you
  393. want, you can use the following alternative commands: `C-c C-t'
  394. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  395. can be used to switch TODO sets, `C-c -' to cycle item bullet
  396. types, and properties can be edited by hand or in column view.
  397. However, when the cursor is on a timestamp, shift-cursor commands
  398. will still edit the time stamp - this is just too good to give up.
  399. XEmacs user should have this variable set to nil, because
  400. `shift-select-mode' is in Emacs 23 or later only."
  401. :group 'org
  402. :type '(choice
  403. (const :tag "Never" nil)
  404. (const :tag "When outside special context" t)
  405. (const :tag "Everywhere except timestamps" always)))
  406. (defcustom org-loop-over-headlines-in-active-region nil
  407. "Shall some commands act upon headlines in the active region?
  408. When set to `t', some commands will be performed in all headlines
  409. within the active region.
  410. When set to `start-level', some commands will be performed in all
  411. headlines within the active region, provided that these headlines
  412. are of the same level than the first one.
  413. When set to a string, those commands will be performed on the
  414. matching headlines within the active region. Such string must be
  415. a tags/property/todo match as it is used in the agenda tags view.
  416. The list of commands is: `org-schedule', `org-deadline',
  417. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  418. `org-archive-to-archive-sibling'. The archiving commands skip
  419. already archived entries."
  420. :type '(choice (const :tag "Don't loop" nil)
  421. (const :tag "All headlines in active region" t)
  422. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  423. (string :tag "Tags/Property/Todo matcher"))
  424. :version "24.1"
  425. :group 'org-todo
  426. :group 'org-archive)
  427. (defgroup org-startup nil
  428. "Options concerning startup of Org-mode."
  429. :tag "Org Startup"
  430. :group 'org)
  431. (defcustom org-startup-folded t
  432. "Non-nil means entering Org-mode will switch to OVERVIEW.
  433. This can also be configured on a per-file basis by adding one of
  434. the following lines anywhere in the buffer:
  435. #+STARTUP: fold (or `overview', this is equivalent)
  436. #+STARTUP: nofold (or `showall', this is equivalent)
  437. #+STARTUP: content
  438. #+STARTUP: showeverything"
  439. :group 'org-startup
  440. :type '(choice
  441. (const :tag "nofold: show all" nil)
  442. (const :tag "fold: overview" t)
  443. (const :tag "content: all headlines" content)
  444. (const :tag "show everything, even drawers" showeverything)))
  445. (defcustom org-startup-truncated t
  446. "Non-nil means entering Org-mode will set `truncate-lines'.
  447. This is useful since some lines containing links can be very long and
  448. uninteresting. Also tables look terrible when wrapped."
  449. :group 'org-startup
  450. :type 'boolean)
  451. (defcustom org-startup-indented nil
  452. "Non-nil means turn on `org-indent-mode' on startup.
  453. This can also be configured on a per-file basis by adding one of
  454. the following lines anywhere in the buffer:
  455. #+STARTUP: indent
  456. #+STARTUP: noindent"
  457. :group 'org-structure
  458. :type '(choice
  459. (const :tag "Not" nil)
  460. (const :tag "Globally (slow on startup in large files)" t)))
  461. (defcustom org-use-sub-superscripts t
  462. "Non-nil means interpret \"_\" and \"^\" for export.
  463. When this option is turned on, you can use TeX-like syntax for sub- and
  464. superscripts. Several characters after \"_\" or \"^\" will be
  465. considered as a single item - so grouping with {} is normally not
  466. needed. For example, the following things will be parsed as single
  467. sub- or superscripts.
  468. 10^24 or 10^tau several digits will be considered 1 item.
  469. 10^-12 or 10^-tau a leading sign with digits or a word
  470. x^2-y^3 will be read as x^2 - y^3, because items are
  471. terminated by almost any nonword/nondigit char.
  472. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  473. Still, ambiguity is possible - so when in doubt use {} to enclose the
  474. sub/superscript. If you set this variable to the symbol `{}',
  475. the braces are *required* in order to trigger interpretations as
  476. sub/superscript. This can be helpful in documents that need \"_\"
  477. frequently in plain text.
  478. Not all export backends support this, but HTML does.
  479. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  480. :group 'org-startup
  481. :group 'org-export-translation
  482. :version "24.1"
  483. :type '(choice
  484. (const :tag "Always interpret" t)
  485. (const :tag "Only with braces" {})
  486. (const :tag "Never interpret" nil)))
  487. (if (fboundp 'defvaralias)
  488. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  489. (defcustom org-startup-with-beamer-mode nil
  490. "Non-nil means turn on `org-beamer-mode' on startup.
  491. This can also be configured on a per-file basis by adding one of
  492. the following lines anywhere in the buffer:
  493. #+STARTUP: beamer"
  494. :group 'org-startup
  495. :version "24.1"
  496. :type 'boolean)
  497. (defcustom org-startup-align-all-tables nil
  498. "Non-nil means align all tables when visiting a file.
  499. This is useful when the column width in tables is forced with <N> cookies
  500. in table fields. Such tables will look correct only after the first re-align.
  501. This can also be configured on a per-file basis by adding one of
  502. the following lines anywhere in the buffer:
  503. #+STARTUP: align
  504. #+STARTUP: noalign"
  505. :group 'org-startup
  506. :type 'boolean)
  507. (defcustom org-startup-with-inline-images nil
  508. "Non-nil means show inline images when loading a new Org file.
  509. This can also be configured on a per-file basis by adding one of
  510. the following lines anywhere in the buffer:
  511. #+STARTUP: inlineimages
  512. #+STARTUP: noinlineimages"
  513. :group 'org-startup
  514. :version "24.1"
  515. :type 'boolean)
  516. (defcustom org-insert-mode-line-in-empty-file nil
  517. "Non-nil means insert the first line setting Org-mode in empty files.
  518. When the function `org-mode' is called interactively in an empty file, this
  519. normally means that the file name does not automatically trigger Org-mode.
  520. To ensure that the file will always be in Org-mode in the future, a
  521. line enforcing Org-mode will be inserted into the buffer, if this option
  522. has been set."
  523. :group 'org-startup
  524. :type 'boolean)
  525. (defcustom org-replace-disputed-keys nil
  526. "Non-nil means use alternative key bindings for some keys.
  527. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  528. These keys are also used by other packages like shift-selection-mode'
  529. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  530. If you want to use Org-mode together with one of these other modes,
  531. or more generally if you would like to move some Org-mode commands to
  532. other keys, set this variable and configure the keys with the variable
  533. `org-disputed-keys'.
  534. This option is only relevant at load-time of Org-mode, and must be set
  535. *before* org.el is loaded. Changing it requires a restart of Emacs to
  536. become effective."
  537. :group 'org-startup
  538. :type 'boolean)
  539. (defcustom org-use-extra-keys nil
  540. "Non-nil means use extra key sequence definitions for certain commands.
  541. This happens automatically if you run XEmacs or if `window-system'
  542. is nil. This variable lets you do the same manually. You must
  543. set it before loading org.
  544. Example: on Carbon Emacs 22 running graphically, with an external
  545. keyboard on a Powerbook, the default way of setting M-left might
  546. not work for either Alt or ESC. Setting this variable will make
  547. it work for ESC."
  548. :group 'org-startup
  549. :type 'boolean)
  550. (if (fboundp 'defvaralias)
  551. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  552. (defcustom org-disputed-keys
  553. '(([(shift up)] . [(meta p)])
  554. ([(shift down)] . [(meta n)])
  555. ([(shift left)] . [(meta -)])
  556. ([(shift right)] . [(meta +)])
  557. ([(control shift right)] . [(meta shift +)])
  558. ([(control shift left)] . [(meta shift -)]))
  559. "Keys for which Org-mode and other modes compete.
  560. This is an alist, cars are the default keys, second element specifies
  561. the alternative to use when `org-replace-disputed-keys' is t.
  562. Keys can be specified in any syntax supported by `define-key'.
  563. The value of this option takes effect only at Org-mode's startup,
  564. therefore you'll have to restart Emacs to apply it after changing."
  565. :group 'org-startup
  566. :type 'alist)
  567. (defun org-key (key)
  568. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  569. Or return the original if not disputed.
  570. Also apply the translations defined in `org-xemacs-key-equivalents'."
  571. (when org-replace-disputed-keys
  572. (let* ((nkey (key-description key))
  573. (x (org-find-if (lambda (x)
  574. (equal (key-description (car x)) nkey))
  575. org-disputed-keys)))
  576. (setq key (if x (cdr x) key))))
  577. (when (featurep 'xemacs)
  578. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  579. key)
  580. (defun org-find-if (predicate seq)
  581. (catch 'exit
  582. (while seq
  583. (if (funcall predicate (car seq))
  584. (throw 'exit (car seq))
  585. (pop seq)))))
  586. (defun org-defkey (keymap key def)
  587. "Define a key, possibly translated, as returned by `org-key'."
  588. (define-key keymap (org-key key) def))
  589. (defcustom org-ellipsis nil
  590. "The ellipsis to use in the Org-mode outline.
  591. When nil, just use the standard three dots. When a string, use that instead,
  592. When a face, use the standard 3 dots, but with the specified face.
  593. The change affects only Org-mode (which will then use its own display table).
  594. Changing this requires executing `M-x org-mode' in a buffer to become
  595. effective."
  596. :group 'org-startup
  597. :type '(choice (const :tag "Default" nil)
  598. (face :tag "Face" :value org-warning)
  599. (string :tag "String" :value "...#")))
  600. (defvar org-display-table nil
  601. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  602. (defgroup org-keywords nil
  603. "Keywords in Org-mode."
  604. :tag "Org Keywords"
  605. :group 'org)
  606. (defcustom org-deadline-string "DEADLINE:"
  607. "String to mark deadline entries.
  608. A deadline is this string, followed by a time stamp. Should be a word,
  609. terminated by a colon. You can insert a schedule keyword and
  610. a timestamp with \\[org-deadline].
  611. Changes become only effective after restarting Emacs."
  612. :group 'org-keywords
  613. :type 'string)
  614. (defcustom org-scheduled-string "SCHEDULED:"
  615. "String to mark scheduled TODO entries.
  616. A schedule is this string, followed by a time stamp. Should be a word,
  617. terminated by a colon. You can insert a schedule keyword and
  618. a timestamp with \\[org-schedule].
  619. Changes become only effective after restarting Emacs."
  620. :group 'org-keywords
  621. :type 'string)
  622. (defcustom org-closed-string "CLOSED:"
  623. "String used as the prefix for timestamps logging closing a TODO entry."
  624. :group 'org-keywords
  625. :type 'string)
  626. (defcustom org-clock-string "CLOCK:"
  627. "String used as prefix for timestamps clocking work hours on an item."
  628. :group 'org-keywords
  629. :type 'string)
  630. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  631. org-scheduled-string "\\|"
  632. org-deadline-string "\\|"
  633. org-closed-string "\\|"
  634. org-clock-string "\\)")
  635. "Matches a line with planning or clock info.")
  636. (defcustom org-comment-string "COMMENT"
  637. "Entries starting with this keyword will never be exported.
  638. An entry can be toggled between COMMENT and normal with
  639. \\[org-toggle-comment].
  640. Changes become only effective after restarting Emacs."
  641. :group 'org-keywords
  642. :type 'string)
  643. (defcustom org-quote-string "QUOTE"
  644. "Entries starting with this keyword will be exported in fixed-width font.
  645. Quoting applies only to the text in the entry following the headline, and does
  646. not extend beyond the next headline, even if that is lower level.
  647. An entry can be toggled between QUOTE and normal with
  648. \\[org-toggle-fixed-width-section]."
  649. :group 'org-keywords
  650. :type 'string)
  651. (defconst org-repeat-re
  652. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  653. "Regular expression for specifying repeated events.
  654. After a match, group 1 contains the repeat expression.")
  655. (defgroup org-structure nil
  656. "Options concerning the general structure of Org-mode files."
  657. :tag "Org Structure"
  658. :group 'org)
  659. (defgroup org-reveal-location nil
  660. "Options about how to make context of a location visible."
  661. :tag "Org Reveal Location"
  662. :group 'org-structure)
  663. (defconst org-context-choice
  664. '(choice
  665. (const :tag "Always" t)
  666. (const :tag "Never" nil)
  667. (repeat :greedy t :tag "Individual contexts"
  668. (cons
  669. (choice :tag "Context"
  670. (const agenda)
  671. (const org-goto)
  672. (const occur-tree)
  673. (const tags-tree)
  674. (const link-search)
  675. (const mark-goto)
  676. (const bookmark-jump)
  677. (const isearch)
  678. (const default))
  679. (boolean))))
  680. "Contexts for the reveal options.")
  681. (defcustom org-show-hierarchy-above '((default . t))
  682. "Non-nil means show full hierarchy when revealing a location.
  683. Org-mode often shows locations in an org-mode file which might have
  684. been invisible before. When this is set, the hierarchy of headings
  685. above the exposed location is shown.
  686. Turning this off for example for sparse trees makes them very compact.
  687. Instead of t, this can also be an alist specifying this option for different
  688. contexts. Valid contexts are
  689. agenda when exposing an entry from the agenda
  690. org-goto when using the command `org-goto' on key C-c C-j
  691. occur-tree when using the command `org-occur' on key C-c /
  692. tags-tree when constructing a sparse tree based on tags matches
  693. link-search when exposing search matches associated with a link
  694. mark-goto when exposing the jump goal of a mark
  695. bookmark-jump when exposing a bookmark location
  696. isearch when exiting from an incremental search
  697. default default for all contexts not set explicitly"
  698. :group 'org-reveal-location
  699. :type org-context-choice)
  700. (defcustom org-show-following-heading '((default . nil))
  701. "Non-nil means show following heading when revealing a location.
  702. Org-mode often shows locations in an org-mode file which might have
  703. been invisible before. When this is set, the heading following the
  704. match is shown.
  705. Turning this off for example for sparse trees makes them very compact,
  706. but makes it harder to edit the location of the match. In such a case,
  707. use the command \\[org-reveal] to show more context.
  708. Instead of t, this can also be an alist specifying this option for different
  709. contexts. See `org-show-hierarchy-above' for valid contexts."
  710. :group 'org-reveal-location
  711. :type org-context-choice)
  712. (defcustom org-show-siblings '((default . nil) (isearch t))
  713. "Non-nil means show all sibling heading when revealing a location.
  714. Org-mode often shows locations in an org-mode file which might have
  715. been invisible before. When this is set, the sibling of the current entry
  716. heading are all made visible. If `org-show-hierarchy-above' is t,
  717. the same happens on each level of the hierarchy above the current entry.
  718. By default this is on for the isearch context, off for all other contexts.
  719. Turning this off for example for sparse trees makes them very compact,
  720. but makes it harder to edit the location of the match. In such a case,
  721. use the command \\[org-reveal] to show more context.
  722. Instead of t, this can also be an alist specifying this option for different
  723. contexts. See `org-show-hierarchy-above' for valid contexts."
  724. :group 'org-reveal-location
  725. :type org-context-choice)
  726. (defcustom org-show-entry-below '((default . nil))
  727. "Non-nil means show the entry below a headline when revealing a location.
  728. Org-mode often shows locations in an org-mode file which might have
  729. been invisible before. When this is set, the text below the headline that is
  730. exposed is also shown.
  731. By default this is off for all contexts.
  732. Instead of t, this can also be an alist specifying this option for different
  733. contexts. See `org-show-hierarchy-above' for valid contexts."
  734. :group 'org-reveal-location
  735. :type org-context-choice)
  736. (defcustom org-indirect-buffer-display 'other-window
  737. "How should indirect tree buffers be displayed?
  738. This applies to indirect buffers created with the commands
  739. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  740. Valid values are:
  741. current-window Display in the current window
  742. other-window Just display in another window.
  743. dedicated-frame Create one new frame, and re-use it each time.
  744. new-frame Make a new frame each time. Note that in this case
  745. previously-made indirect buffers are kept, and you need to
  746. kill these buffers yourself."
  747. :group 'org-structure
  748. :group 'org-agenda-windows
  749. :type '(choice
  750. (const :tag "In current window" current-window)
  751. (const :tag "In current frame, other window" other-window)
  752. (const :tag "Each time a new frame" new-frame)
  753. (const :tag "One dedicated frame" dedicated-frame)))
  754. (defcustom org-use-speed-commands nil
  755. "Non-nil means activate single letter commands at beginning of a headline.
  756. This may also be a function to test for appropriate locations where speed
  757. commands should be active."
  758. :group 'org-structure
  759. :type '(choice
  760. (const :tag "Never" nil)
  761. (const :tag "At beginning of headline stars" t)
  762. (function)))
  763. (defcustom org-speed-commands-user nil
  764. "Alist of additional speed commands.
  765. This list will be checked before `org-speed-commands-default'
  766. when the variable `org-use-speed-commands' is non-nil
  767. and when the cursor is at the beginning of a headline.
  768. The car if each entry is a string with a single letter, which must
  769. be assigned to `self-insert-command' in the global map.
  770. The cdr is either a command to be called interactively, a function
  771. to be called, or a form to be evaluated.
  772. An entry that is just a list with a single string will be interpreted
  773. as a descriptive headline that will be added when listing the speed
  774. commands in the Help buffer using the `?' speed command."
  775. :group 'org-structure
  776. :type '(repeat :value ("k" . ignore)
  777. (choice :value ("k" . ignore)
  778. (list :tag "Descriptive Headline" (string :tag "Headline"))
  779. (cons :tag "Letter and Command"
  780. (string :tag "Command letter")
  781. (choice
  782. (function)
  783. (sexp))))))
  784. (defgroup org-cycle nil
  785. "Options concerning visibility cycling in Org-mode."
  786. :tag "Org Cycle"
  787. :group 'org-structure)
  788. (defcustom org-cycle-skip-children-state-if-no-children t
  789. "Non-nil means skip CHILDREN state in entries that don't have any."
  790. :group 'org-cycle
  791. :type 'boolean)
  792. (defcustom org-cycle-max-level nil
  793. "Maximum level which should still be subject to visibility cycling.
  794. Levels higher than this will, for cycling, be treated as text, not a headline.
  795. When `org-odd-levels-only' is set, a value of N in this variable actually
  796. means 2N-1 stars as the limiting headline.
  797. When nil, cycle all levels.
  798. Note that the limiting level of cycling is also influenced by
  799. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  800. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  801. than its value."
  802. :group 'org-cycle
  803. :type '(choice
  804. (const :tag "No limit" nil)
  805. (integer :tag "Maximum level")))
  806. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  807. "Names of drawers. Drawers are not opened by cycling on the headline above.
  808. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  809. this:
  810. :DRAWERNAME:
  811. .....
  812. :END:
  813. The drawer \"PROPERTIES\" is special for capturing properties through
  814. the property API.
  815. Drawers can be defined on the per-file basis with a line like:
  816. #+DRAWERS: HIDDEN STATE PROPERTIES"
  817. :group 'org-structure
  818. :group 'org-cycle
  819. :type '(repeat (string :tag "Drawer Name")))
  820. (defcustom org-hide-block-startup nil
  821. "Non-nil means entering Org-mode will fold all blocks.
  822. This can also be set in on a per-file basis with
  823. #+STARTUP: hideblocks
  824. #+STARTUP: showblocks"
  825. :group 'org-startup
  826. :group 'org-cycle
  827. :type 'boolean)
  828. (defcustom org-cycle-global-at-bob nil
  829. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  830. This makes it possible to do global cycling without having to use S-TAB or
  831. \\[universal-argument] TAB. For this special case to work, the first line
  832. of the buffer must not be a headline -- it may be empty or some other text.
  833. When used in this way, `org-cycle-hook' is disabled temporarily to make
  834. sure the cursor stays at the beginning of the buffer. When this option is
  835. nil, don't do anything special at the beginning of the buffer."
  836. :group 'org-cycle
  837. :type 'boolean)
  838. (defcustom org-cycle-level-after-item/entry-creation t
  839. "Non-nil means cycle entry level or item indentation in new empty entries.
  840. When the cursor is at the end of an empty headline, i.e with only stars
  841. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  842. and then all possible ancestor states, before returning to the original state.
  843. This makes data entry extremely fast: M-RET to create a new headline,
  844. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  845. When the cursor is at the end of an empty plain list item, one TAB will
  846. make it a subitem, two or more tabs will back up to make this an item
  847. higher up in the item hierarchy."
  848. :group 'org-cycle
  849. :type 'boolean)
  850. (defcustom org-cycle-emulate-tab t
  851. "Where should `org-cycle' emulate TAB.
  852. nil Never
  853. white Only in completely white lines
  854. whitestart Only at the beginning of lines, before the first non-white char
  855. t Everywhere except in headlines
  856. exc-hl-bol Everywhere except at the start of a headline
  857. If TAB is used in a place where it does not emulate TAB, the current subtree
  858. visibility is cycled."
  859. :group 'org-cycle
  860. :type '(choice (const :tag "Never" nil)
  861. (const :tag "Only in completely white lines" white)
  862. (const :tag "Before first char in a line" whitestart)
  863. (const :tag "Everywhere except in headlines" t)
  864. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  865. ))
  866. (defcustom org-cycle-separator-lines 2
  867. "Number of empty lines needed to keep an empty line between collapsed trees.
  868. If you leave an empty line between the end of a subtree and the following
  869. headline, this empty line is hidden when the subtree is folded.
  870. Org-mode will leave (exactly) one empty line visible if the number of
  871. empty lines is equal or larger to the number given in this variable.
  872. So the default 2 means at least 2 empty lines after the end of a subtree
  873. are needed to produce free space between a collapsed subtree and the
  874. following headline.
  875. If the number is negative, and the number of empty lines is at least -N,
  876. all empty lines are shown.
  877. Special case: when 0, never leave empty lines in collapsed view."
  878. :group 'org-cycle
  879. :type 'integer)
  880. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  881. (defcustom org-pre-cycle-hook nil
  882. "Hook that is run before visibility cycling is happening.
  883. The function(s) in this hook must accept a single argument which indicates
  884. the new state that will be set right after running this hook. The
  885. argument is a symbol. Before a global state change, it can have the values
  886. `overview', `content', or `all'. Before a local state change, it can have
  887. the values `folded', `children', or `subtree'."
  888. :group 'org-cycle
  889. :type 'hook)
  890. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  891. org-cycle-hide-drawers
  892. org-cycle-hide-inline-tasks
  893. org-cycle-show-empty-lines
  894. org-optimize-window-after-visibility-change)
  895. "Hook that is run after `org-cycle' has changed the buffer visibility.
  896. The function(s) in this hook must accept a single argument which indicates
  897. the new state that was set by the most recent `org-cycle' command. The
  898. argument is a symbol. After a global state change, it can have the values
  899. `overview', `contents', or `all'. After a local state change, it can have
  900. the values `folded', `children', or `subtree'."
  901. :group 'org-cycle
  902. :type 'hook)
  903. (defgroup org-edit-structure nil
  904. "Options concerning structure editing in Org-mode."
  905. :tag "Org Edit Structure"
  906. :group 'org-structure)
  907. (defcustom org-odd-levels-only nil
  908. "Non-nil means skip even levels and only use odd levels for the outline.
  909. This has the effect that two stars are being added/taken away in
  910. promotion/demotion commands. It also influences how levels are
  911. handled by the exporters.
  912. Changing it requires restart of `font-lock-mode' to become effective
  913. for fontification also in regions already fontified.
  914. You may also set this on a per-file basis by adding one of the following
  915. lines to the buffer:
  916. #+STARTUP: odd
  917. #+STARTUP: oddeven"
  918. :group 'org-edit-structure
  919. :group 'org-appearance
  920. :type 'boolean)
  921. (defcustom org-adapt-indentation t
  922. "Non-nil means adapt indentation to outline node level.
  923. When this variable is set, Org assumes that you write outlines by
  924. indenting text in each node to align with the headline (after the stars).
  925. The following issues are influenced by this variable:
  926. - When this is set and the *entire* text in an entry is indented, the
  927. indentation is increased by one space in a demotion command, and
  928. decreased by one in a promotion command. If any line in the entry
  929. body starts with text at column 0, indentation is not changed at all.
  930. - Property drawers and planning information is inserted indented when
  931. this variable s set. When nil, they will not be indented.
  932. - TAB indents a line relative to context. The lines below a headline
  933. will be indented when this variable is set.
  934. Note that this is all about true indentation, by adding and removing
  935. space characters. See also `org-indent.el' which does level-dependent
  936. indentation in a virtual way, i.e. at display time in Emacs."
  937. :group 'org-edit-structure
  938. :type 'boolean)
  939. (defcustom org-special-ctrl-a/e nil
  940. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  941. When t, `C-a' will bring back the cursor to the beginning of the
  942. headline text, i.e. after the stars and after a possible TODO
  943. keyword. In an item, this will be the position after bullet and
  944. check-box, if any. When the cursor is already at that position,
  945. another `C-a' will bring it to the beginning of the line.
  946. `C-e' will jump to the end of the headline, ignoring the presence
  947. of tags in the headline. A second `C-e' will then jump to the
  948. true end of the line, after any tags. This also means that, when
  949. this variable is non-nil, `C-e' also will never jump beyond the
  950. end of the heading of a folded section, i.e. not after the
  951. ellipses.
  952. When set to the symbol `reversed', the first `C-a' or `C-e' works
  953. normally, going to the true line boundary first. Only a directly
  954. following, identical keypress will bring the cursor to the
  955. special positions.
  956. This may also be a cons cell where the behavior for `C-a' and
  957. `C-e' is set separately."
  958. :group 'org-edit-structure
  959. :type '(choice
  960. (const :tag "off" nil)
  961. (const :tag "on: after stars/bullet and before tags first" t)
  962. (const :tag "reversed: true line boundary first" reversed)
  963. (cons :tag "Set C-a and C-e separately"
  964. (choice :tag "Special C-a"
  965. (const :tag "off" nil)
  966. (const :tag "on: after stars/bullet first" t)
  967. (const :tag "reversed: before stars/bullet first" reversed))
  968. (choice :tag "Special C-e"
  969. (const :tag "off" nil)
  970. (const :tag "on: before tags first" t)
  971. (const :tag "reversed: after tags first" reversed)))))
  972. (if (fboundp 'defvaralias)
  973. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  974. (defcustom org-special-ctrl-k nil
  975. "Non-nil means `C-k' will behave specially in headlines.
  976. When nil, `C-k' will call the default `kill-line' command.
  977. When t, the following will happen while the cursor is in the headline:
  978. - When the cursor is at the beginning of a headline, kill the entire
  979. line and possible the folded subtree below the line.
  980. - When in the middle of the headline text, kill the headline up to the tags.
  981. - When after the headline text, kill the tags."
  982. :group 'org-edit-structure
  983. :type 'boolean)
  984. (defcustom org-ctrl-k-protect-subtree nil
  985. "Non-nil means, do not delete a hidden subtree with C-k.
  986. When set to the symbol `error', simply throw an error when C-k is
  987. used to kill (part-of) a headline that has hidden text behind it.
  988. Any other non-nil value will result in a query to the user, if it is
  989. OK to kill that hidden subtree. When nil, kill without remorse."
  990. :group 'org-edit-structure
  991. :version "24.1"
  992. :type '(choice
  993. (const :tag "Do not protect hidden subtrees" nil)
  994. (const :tag "Protect hidden subtrees with a security query" t)
  995. (const :tag "Never kill a hidden subtree with C-k" error)))
  996. (defcustom org-catch-invisible-edits nil
  997. "Check if in invisible region before inserting or deleting a character.
  998. Valid values are:
  999. nil Do not check, so just do invisible edits.
  1000. error Throw an error and do nothing.
  1001. show Make point visible, and do the requested edit.
  1002. show-and-error Make point visible, then throw an error and abort the edit.
  1003. smart Make point visible, and do insertion/deletion if it is
  1004. adjacent to visible text and the change feels predictable.
  1005. Never delete a previously invisible character or add in the
  1006. middle or right after an invisible region. Basically, this
  1007. allows insertion and backward-delete right before ellipses.
  1008. FIXME: maybe in this case we should not even show?"
  1009. :group 'org-edit-structure
  1010. :version "24.1"
  1011. :type '(choice
  1012. (const :tag "Do not check" nil)
  1013. (const :tag "Throw error when trying to edit" error)
  1014. (const :tag "Unhide, but do not do the edit" show-and-error)
  1015. (const :tag "Show invisible part and do the edit" show)
  1016. (const :tag "Be smart and do the right thing" smart)))
  1017. (defcustom org-yank-folded-subtrees t
  1018. "Non-nil means when yanking subtrees, fold them.
  1019. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1020. it starts with a heading and all other headings in it are either children
  1021. or siblings, then fold all the subtrees. However, do this only if no
  1022. text after the yank would be swallowed into a folded tree by this action."
  1023. :group 'org-edit-structure
  1024. :type 'boolean)
  1025. (defcustom org-yank-adjusted-subtrees nil
  1026. "Non-nil means when yanking subtrees, adjust the level.
  1027. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1028. this function for details."
  1029. :group 'org-edit-structure
  1030. :type 'boolean)
  1031. (defcustom org-M-RET-may-split-line '((default . t))
  1032. "Non-nil means M-RET will split the line at the cursor position.
  1033. When nil, it will go to the end of the line before making a
  1034. new line.
  1035. You may also set this option in a different way for different
  1036. contexts. Valid contexts are:
  1037. headline when creating a new headline
  1038. item when creating a new item
  1039. table in a table field
  1040. default the value to be used for all contexts not explicitly
  1041. customized"
  1042. :group 'org-structure
  1043. :group 'org-table
  1044. :type '(choice
  1045. (const :tag "Always" t)
  1046. (const :tag "Never" nil)
  1047. (repeat :greedy t :tag "Individual contexts"
  1048. (cons
  1049. (choice :tag "Context"
  1050. (const headline)
  1051. (const item)
  1052. (const table)
  1053. (const default))
  1054. (boolean)))))
  1055. (defcustom org-insert-heading-respect-content nil
  1056. "Non-nil means insert new headings after the current subtree.
  1057. When nil, the new heading is created directly after the current line.
  1058. The commands \\[org-insert-heading-respect-content] and
  1059. \\[org-insert-todo-heading-respect-content] turn this variable on
  1060. for the duration of the command."
  1061. :group 'org-structure
  1062. :type 'boolean)
  1063. (defcustom org-blank-before-new-entry '((heading . auto)
  1064. (plain-list-item . auto))
  1065. "Should `org-insert-heading' leave a blank line before new heading/item?
  1066. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1067. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1068. which case Org will look at the surrounding headings/items and try to
  1069. make an intelligent decision whether to insert a blank line or not.
  1070. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1071. set, the setting here is ignored and no empty line is inserted, to avoid
  1072. breaking the list structure."
  1073. :group 'org-edit-structure
  1074. :type '(list
  1075. (cons (const heading)
  1076. (choice (const :tag "Never" nil)
  1077. (const :tag "Always" t)
  1078. (const :tag "Auto" auto)))
  1079. (cons (const plain-list-item)
  1080. (choice (const :tag "Never" nil)
  1081. (const :tag "Always" t)
  1082. (const :tag "Auto" auto)))))
  1083. (defcustom org-insert-heading-hook nil
  1084. "Hook being run after inserting a new heading."
  1085. :group 'org-edit-structure
  1086. :type 'hook)
  1087. (defcustom org-enable-fixed-width-editor t
  1088. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1089. This currently only means they are never auto-wrapped.
  1090. When nil, such lines will be treated like ordinary lines.
  1091. See also the QUOTE keyword."
  1092. :group 'org-edit-structure
  1093. :type 'boolean)
  1094. (defcustom org-goto-auto-isearch t
  1095. "Non-nil means typing characters in `org-goto' starts incremental search.
  1096. When nil, you can use these keybindings to navigate the buffer:
  1097. q Quit the org-goto interface
  1098. n Go to the next visible heading
  1099. p Go to the previous visible heading
  1100. f Go one heading forward on same level
  1101. b Go one heading backward on same level
  1102. u Go one heading up"
  1103. :group 'org-edit-structure
  1104. :type 'boolean)
  1105. (defgroup org-sparse-trees nil
  1106. "Options concerning sparse trees in Org-mode."
  1107. :tag "Org Sparse Trees"
  1108. :group 'org-structure)
  1109. (defcustom org-highlight-sparse-tree-matches t
  1110. "Non-nil means highlight all matches that define a sparse tree.
  1111. The highlights will automatically disappear the next time the buffer is
  1112. changed by an edit command."
  1113. :group 'org-sparse-trees
  1114. :type 'boolean)
  1115. (defcustom org-remove-highlights-with-change t
  1116. "Non-nil means any change to the buffer will remove temporary highlights.
  1117. Such highlights are created by `org-occur' and `org-clock-display'.
  1118. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1119. The highlights created by `org-preview-latex-fragment' always need
  1120. `C-c C-c' to be removed."
  1121. :group 'org-sparse-trees
  1122. :group 'org-time
  1123. :type 'boolean)
  1124. (defcustom org-occur-hook '(org-first-headline-recenter)
  1125. "Hook that is run after `org-occur' has constructed a sparse tree.
  1126. This can be used to recenter the window to show as much of the structure
  1127. as possible."
  1128. :group 'org-sparse-trees
  1129. :type 'hook)
  1130. (defgroup org-imenu-and-speedbar nil
  1131. "Options concerning imenu and speedbar in Org-mode."
  1132. :tag "Org Imenu and Speedbar"
  1133. :group 'org-structure)
  1134. (defcustom org-imenu-depth 2
  1135. "The maximum level for Imenu access to Org-mode headlines.
  1136. This also applied for speedbar access."
  1137. :group 'org-imenu-and-speedbar
  1138. :type 'integer)
  1139. (defgroup org-table nil
  1140. "Options concerning tables in Org-mode."
  1141. :tag "Org Table"
  1142. :group 'org)
  1143. (defcustom org-enable-table-editor 'optimized
  1144. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1145. When nil, such lines will be treated like ordinary lines.
  1146. When equal to the symbol `optimized', the table editor will be optimized to
  1147. do the following:
  1148. - Automatic overwrite mode in front of whitespace in table fields.
  1149. This makes the structure of the table stay in tact as long as the edited
  1150. field does not exceed the column width.
  1151. - Minimize the number of realigns. Normally, the table is aligned each time
  1152. TAB or RET are pressed to move to another field. With optimization this
  1153. happens only if changes to a field might have changed the column width.
  1154. Optimization requires replacing the functions `self-insert-command',
  1155. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1156. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1157. very good at guessing when a re-align will be necessary, but you can always
  1158. force one with \\[org-ctrl-c-ctrl-c].
  1159. If you would like to use the optimized version in Org-mode, but the
  1160. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1161. This variable can be used to turn on and off the table editor during a session,
  1162. but in order to toggle optimization, a restart is required.
  1163. See also the variable `org-table-auto-blank-field'."
  1164. :group 'org-table
  1165. :type '(choice
  1166. (const :tag "off" nil)
  1167. (const :tag "on" t)
  1168. (const :tag "on, optimized" optimized)))
  1169. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1170. (version<= emacs-version "24.1"))
  1171. "Non-nil means cluster self-insert commands for undo when possible.
  1172. If this is set, then, like in the Emacs command loop, 20 consecutive
  1173. characters will be undone together.
  1174. This is configurable, because there is some impact on typing performance."
  1175. :group 'org-table
  1176. :type 'boolean)
  1177. (defcustom org-table-tab-recognizes-table.el t
  1178. "Non-nil means TAB will automatically notice a table.el table.
  1179. When it sees such a table, it moves point into it and - if necessary -
  1180. calls `table-recognize-table'."
  1181. :group 'org-table-editing
  1182. :type 'boolean)
  1183. (defgroup org-link nil
  1184. "Options concerning links in Org-mode."
  1185. :tag "Org Link"
  1186. :group 'org)
  1187. (defvar org-link-abbrev-alist-local nil
  1188. "Buffer-local version of `org-link-abbrev-alist', which see.
  1189. The value of this is taken from the #+LINK lines.")
  1190. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1191. (defcustom org-link-abbrev-alist nil
  1192. "Alist of link abbreviations.
  1193. The car of each element is a string, to be replaced at the start of a link.
  1194. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1195. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1196. [[linkkey:tag][description]]
  1197. The 'linkkey' must be a word word, starting with a letter, followed
  1198. by letters, numbers, '-' or '_'.
  1199. If REPLACE is a string, the tag will simply be appended to create the link.
  1200. If the string contains \"%s\", the tag will be inserted there. If the string
  1201. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1202. at that point (see the function `url-hexify-string'). If the string contains
  1203. the specifier \"%(my-function)\", then the custom function `my-function' will
  1204. be invoked: this function takes the tag as its only argument and must return
  1205. a string.
  1206. REPLACE may also be a function that will be called with the tag as the
  1207. only argument to create the link, which should be returned as a string.
  1208. See the manual for examples."
  1209. :group 'org-link
  1210. :type '(repeat
  1211. (cons
  1212. (string :tag "Protocol")
  1213. (choice
  1214. (string :tag "Format")
  1215. (function)))))
  1216. (defcustom org-descriptive-links t
  1217. "Non-nil means Org will display descriptive links.
  1218. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1219. \"Org Website\", hiding the link itself and just displaying its
  1220. description. When set to `nil', Org will display the full links
  1221. literally.
  1222. You can interactively set the value of this variable by calling
  1223. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1224. :group 'org-link
  1225. :type 'boolean)
  1226. (defcustom org-link-file-path-type 'adaptive
  1227. "How the path name in file links should be stored.
  1228. Valid values are:
  1229. relative Relative to the current directory, i.e. the directory of the file
  1230. into which the link is being inserted.
  1231. absolute Absolute path, if possible with ~ for home directory.
  1232. noabbrev Absolute path, no abbreviation of home directory.
  1233. adaptive Use relative path for files in the current directory and sub-
  1234. directories of it. For other files, use an absolute path."
  1235. :group 'org-link
  1236. :type '(choice
  1237. (const relative)
  1238. (const absolute)
  1239. (const noabbrev)
  1240. (const adaptive)))
  1241. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1242. "Types of links that should be activated in Org-mode files.
  1243. This is a list of symbols, each leading to the activation of a certain link
  1244. type. In principle, it does not hurt to turn on most link types - there may
  1245. be a small gain when turning off unused link types. The types are:
  1246. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1247. angle Links in angular brackets that may contain whitespace like
  1248. <bbdb:Carsten Dominik>.
  1249. plain Plain links in normal text, no whitespace, like http://google.com.
  1250. radio Text that is matched by a radio target, see manual for details.
  1251. tag Tag settings in a headline (link to tag search).
  1252. date Time stamps (link to calendar).
  1253. footnote Footnote labels.
  1254. Changing this variable requires a restart of Emacs to become effective."
  1255. :group 'org-link
  1256. :type '(set :greedy t
  1257. (const :tag "Double bracket links" bracket)
  1258. (const :tag "Angular bracket links" angle)
  1259. (const :tag "Plain text links" plain)
  1260. (const :tag "Radio target matches" radio)
  1261. (const :tag "Tags" tag)
  1262. (const :tag "Timestamps" date)
  1263. (const :tag "Footnotes" footnote)))
  1264. (defcustom org-make-link-description-function nil
  1265. "Function to use for generating link descriptions from links.
  1266. When nil, the link location will be used. This function must take
  1267. two parameters: the first one is the link, the second one is the
  1268. description generated by `org-insert-link'. The function should
  1269. return the description to use."
  1270. :group 'org-link
  1271. :type 'function)
  1272. (defgroup org-link-store nil
  1273. "Options concerning storing links in Org-mode."
  1274. :tag "Org Store Link"
  1275. :group 'org-link)
  1276. (defcustom org-url-hexify-p t
  1277. "When non-nil, hexify URL when creating a link."
  1278. :type 'boolean
  1279. :version "24.3"
  1280. :group 'org-link-store)
  1281. (defcustom org-email-link-description-format "Email %c: %.30s"
  1282. "Format of the description part of a link to an email or usenet message.
  1283. The following %-escapes will be replaced by corresponding information:
  1284. %F full \"From\" field
  1285. %f name, taken from \"From\" field, address if no name
  1286. %T full \"To\" field
  1287. %t first name in \"To\" field, address if no name
  1288. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1289. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1290. %s subject
  1291. %d date
  1292. %m message-id.
  1293. You may use normal field width specification between the % and the letter.
  1294. This is for example useful to limit the length of the subject.
  1295. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1296. :group 'org-link-store
  1297. :type 'string)
  1298. (defcustom org-from-is-user-regexp
  1299. (let (r1 r2)
  1300. (when (and user-mail-address (not (string= user-mail-address "")))
  1301. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1302. (when (and user-full-name (not (string= user-full-name "")))
  1303. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1304. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1305. "Regexp matched against the \"From:\" header of an email or usenet message.
  1306. It should match if the message is from the user him/herself."
  1307. :group 'org-link-store
  1308. :type 'regexp)
  1309. (defcustom org-context-in-file-links t
  1310. "Non-nil means file links from `org-store-link' contain context.
  1311. A search string will be added to the file name with :: as separator and
  1312. used to find the context when the link is activated by the command
  1313. `org-open-at-point'. When this option is t, the entire active region
  1314. will be placed in the search string of the file link. If set to a
  1315. positive integer, only the first n lines of context will be stored.
  1316. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1317. negates this setting for the duration of the command."
  1318. :group 'org-link-store
  1319. :type '(choice boolean integer))
  1320. (defcustom org-keep-stored-link-after-insertion nil
  1321. "Non-nil means keep link in list for entire session.
  1322. The command `org-store-link' adds a link pointing to the current
  1323. location to an internal list. These links accumulate during a session.
  1324. The command `org-insert-link' can be used to insert links into any
  1325. Org-mode file (offering completion for all stored links). When this
  1326. option is nil, every link which has been inserted once using \\[org-insert-link]
  1327. will be removed from the list, to make completing the unused links
  1328. more efficient."
  1329. :group 'org-link-store
  1330. :type 'boolean)
  1331. (defgroup org-link-follow nil
  1332. "Options concerning following links in Org-mode."
  1333. :tag "Org Follow Link"
  1334. :group 'org-link)
  1335. (defcustom org-link-translation-function nil
  1336. "Function to translate links with different syntax to Org syntax.
  1337. This can be used to translate links created for example by the Planner
  1338. or emacs-wiki packages to Org syntax.
  1339. The function must accept two parameters, a TYPE containing the link
  1340. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1341. which is everything after the link protocol. It should return a cons
  1342. with possibly modified values of type and path.
  1343. Org contains a function for this, so if you set this variable to
  1344. `org-translate-link-from-planner', you should be able follow many
  1345. links created by planner."
  1346. :group 'org-link-follow
  1347. :type 'function)
  1348. (defcustom org-follow-link-hook nil
  1349. "Hook that is run after a link has been followed."
  1350. :group 'org-link-follow
  1351. :type 'hook)
  1352. (defcustom org-tab-follows-link nil
  1353. "Non-nil means on links TAB will follow the link.
  1354. Needs to be set before org.el is loaded.
  1355. This really should not be used, it does not make sense, and the
  1356. implementation is bad."
  1357. :group 'org-link-follow
  1358. :type 'boolean)
  1359. (defcustom org-return-follows-link nil
  1360. "Non-nil means on links RET will follow the link."
  1361. :group 'org-link-follow
  1362. :type 'boolean)
  1363. (defcustom org-mouse-1-follows-link
  1364. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1365. "Non-nil means mouse-1 on a link will follow the link.
  1366. A longer mouse click will still set point. Does not work on XEmacs.
  1367. Needs to be set before org.el is loaded."
  1368. :group 'org-link-follow
  1369. :type 'boolean)
  1370. (defcustom org-mark-ring-length 4
  1371. "Number of different positions to be recorded in the ring.
  1372. Changing this requires a restart of Emacs to work correctly."
  1373. :group 'org-link-follow
  1374. :type 'integer)
  1375. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1376. "Non-nil means internal links in Org files must exactly match a headline.
  1377. When nil, the link search tries to match a phrase with all words
  1378. in the search text."
  1379. :group 'org-link-follow
  1380. :version "24.1"
  1381. :type '(choice
  1382. (const :tag "Use fuzzy text search" nil)
  1383. (const :tag "Match only exact headline" t)
  1384. (const :tag "Match exact headline or query to create it"
  1385. query-to-create)))
  1386. (defcustom org-link-frame-setup
  1387. '((vm . vm-visit-folder-other-frame)
  1388. (vm-imap . vm-visit-imap-folder-other-frame)
  1389. (gnus . org-gnus-no-new-news)
  1390. (file . find-file-other-window)
  1391. (wl . wl-other-frame))
  1392. "Setup the frame configuration for following links.
  1393. When following a link with Emacs, it may often be useful to display
  1394. this link in another window or frame. This variable can be used to
  1395. set this up for the different types of links.
  1396. For VM, use any of
  1397. `vm-visit-folder'
  1398. `vm-visit-folder-other-window'
  1399. `vm-visit-folder-other-frame'
  1400. For Gnus, use any of
  1401. `gnus'
  1402. `gnus-other-frame'
  1403. `org-gnus-no-new-news'
  1404. For FILE, use any of
  1405. `find-file'
  1406. `find-file-other-window'
  1407. `find-file-other-frame'
  1408. For Wanderlust use any of
  1409. `wl'
  1410. `wl-other-frame'
  1411. For the calendar, use the variable `calendar-setup'.
  1412. For BBDB, it is currently only possible to display the matches in
  1413. another window."
  1414. :group 'org-link-follow
  1415. :type '(list
  1416. (cons (const vm)
  1417. (choice
  1418. (const vm-visit-folder)
  1419. (const vm-visit-folder-other-window)
  1420. (const vm-visit-folder-other-frame)))
  1421. (cons (const gnus)
  1422. (choice
  1423. (const gnus)
  1424. (const gnus-other-frame)
  1425. (const org-gnus-no-new-news)))
  1426. (cons (const file)
  1427. (choice
  1428. (const find-file)
  1429. (const find-file-other-window)
  1430. (const find-file-other-frame)))
  1431. (cons (const wl)
  1432. (choice
  1433. (const wl)
  1434. (const wl-other-frame)))))
  1435. (defcustom org-display-internal-link-with-indirect-buffer nil
  1436. "Non-nil means use indirect buffer to display infile links.
  1437. Activating internal links (from one location in a file to another location
  1438. in the same file) normally just jumps to the location. When the link is
  1439. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1440. is displayed in
  1441. another window. When this option is set, the other window actually displays
  1442. an indirect buffer clone of the current buffer, to avoid any visibility
  1443. changes to the current buffer."
  1444. :group 'org-link-follow
  1445. :type 'boolean)
  1446. (defcustom org-open-non-existing-files nil
  1447. "Non-nil means `org-open-file' will open non-existing files.
  1448. When nil, an error will be generated.
  1449. This variable applies only to external applications because they
  1450. might choke on non-existing files. If the link is to a file that
  1451. will be opened in Emacs, the variable is ignored."
  1452. :group 'org-link-follow
  1453. :type 'boolean)
  1454. (defcustom org-open-directory-means-index-dot-org nil
  1455. "Non-nil means a link to a directory really means to index.org.
  1456. When nil, following a directory link will run dired or open a finder/explorer
  1457. window on that directory."
  1458. :group 'org-link-follow
  1459. :type 'boolean)
  1460. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1461. "Function and arguments to call for following mailto links.
  1462. This is a list with the first element being a Lisp function, and the
  1463. remaining elements being arguments to the function. In string arguments,
  1464. %a will be replaced by the address, and %s will be replaced by the subject
  1465. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1466. :group 'org-link-follow
  1467. :type '(choice
  1468. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1469. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1470. (const :tag "message-mail" (message-mail "%a" "%s"))
  1471. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1472. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1473. "Non-nil means ask for confirmation before executing shell links.
  1474. Shell links can be dangerous: just think about a link
  1475. [[shell:rm -rf ~/*][Google Search]]
  1476. This link would show up in your Org-mode document as \"Google Search\",
  1477. but really it would remove your entire home directory.
  1478. Therefore we advise against setting this variable to nil.
  1479. Just change it to `y-or-n-p' if you want to confirm with a
  1480. single keystroke rather than having to type \"yes\"."
  1481. :group 'org-link-follow
  1482. :type '(choice
  1483. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1484. (const :tag "with y-or-n (faster)" y-or-n-p)
  1485. (const :tag "no confirmation (dangerous)" nil)))
  1486. (put 'org-confirm-shell-link-function
  1487. 'safe-local-variable
  1488. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1489. (defcustom org-confirm-shell-link-not-regexp ""
  1490. "A regexp to skip confirmation for shell links."
  1491. :group 'org-link-follow
  1492. :version "24.1"
  1493. :type 'regexp)
  1494. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1495. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1496. Elisp links can be dangerous: just think about a link
  1497. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1498. This link would show up in your Org-mode document as \"Google Search\",
  1499. but really it would remove your entire home directory.
  1500. Therefore we advise against setting this variable to nil.
  1501. Just change it to `y-or-n-p' if you want to confirm with a
  1502. single keystroke rather than having to type \"yes\"."
  1503. :group 'org-link-follow
  1504. :type '(choice
  1505. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1506. (const :tag "with y-or-n (faster)" y-or-n-p)
  1507. (const :tag "no confirmation (dangerous)" nil)))
  1508. (put 'org-confirm-shell-link-function
  1509. 'safe-local-variable
  1510. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1511. (defcustom org-confirm-elisp-link-not-regexp ""
  1512. "A regexp to skip confirmation for Elisp links."
  1513. :group 'org-link-follow
  1514. :version "24.1"
  1515. :type 'regexp)
  1516. (defconst org-file-apps-defaults-gnu
  1517. '((remote . emacs)
  1518. (system . mailcap)
  1519. (t . mailcap))
  1520. "Default file applications on a UNIX or GNU/Linux system.
  1521. See `org-file-apps'.")
  1522. (defconst org-file-apps-defaults-macosx
  1523. '((remote . emacs)
  1524. (t . "open %s")
  1525. (system . "open %s")
  1526. ("ps.gz" . "gv %s")
  1527. ("eps.gz" . "gv %s")
  1528. ("dvi" . "xdvi %s")
  1529. ("fig" . "xfig %s"))
  1530. "Default file applications on a MacOS X system.
  1531. The system \"open\" is known as a default, but we use X11 applications
  1532. for some files for which the OS does not have a good default.
  1533. See `org-file-apps'.")
  1534. (defconst org-file-apps-defaults-windowsnt
  1535. (list
  1536. '(remote . emacs)
  1537. (cons t
  1538. (list (if (featurep 'xemacs)
  1539. 'mswindows-shell-execute
  1540. 'w32-shell-execute)
  1541. "open" 'file))
  1542. (cons 'system
  1543. (list (if (featurep 'xemacs)
  1544. 'mswindows-shell-execute
  1545. 'w32-shell-execute)
  1546. "open" 'file)))
  1547. "Default file applications on a Windows NT system.
  1548. The system \"open\" is used for most files.
  1549. See `org-file-apps'.")
  1550. (defcustom org-file-apps
  1551. '(
  1552. (auto-mode . emacs)
  1553. ("\\.mm\\'" . default)
  1554. ("\\.x?html?\\'" . default)
  1555. ("\\.pdf\\'" . default)
  1556. )
  1557. "External applications for opening `file:path' items in a document.
  1558. Org-mode uses system defaults for different file types, but
  1559. you can use this variable to set the application for a given file
  1560. extension. The entries in this list are cons cells where the car identifies
  1561. files and the cdr the corresponding command. Possible values for the
  1562. file identifier are
  1563. \"string\" A string as a file identifier can be interpreted in different
  1564. ways, depending on its contents:
  1565. - Alphanumeric characters only:
  1566. Match links with this file extension.
  1567. Example: (\"pdf\" . \"evince %s\")
  1568. to open PDFs with evince.
  1569. - Regular expression: Match links where the
  1570. filename matches the regexp. If you want to
  1571. use groups here, use shy groups.
  1572. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1573. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1574. to open *.html and *.xhtml with firefox.
  1575. - Regular expression which contains (non-shy) groups:
  1576. Match links where the whole link, including \"::\", and
  1577. anything after that, matches the regexp.
  1578. In a custom command string, %1, %2, etc. are replaced with
  1579. the parts of the link that were matched by the groups.
  1580. For backwards compatibility, if a command string is given
  1581. that does not use any of the group matches, this case is
  1582. handled identically to the second one (i.e. match against
  1583. file name only).
  1584. In a custom lisp form, you can access the group matches with
  1585. (match-string n link).
  1586. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1587. to open [[file:document.pdf::5]] with evince at page 5.
  1588. `directory' Matches a directory
  1589. `remote' Matches a remote file, accessible through tramp or efs.
  1590. Remote files most likely should be visited through Emacs
  1591. because external applications cannot handle such paths.
  1592. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1593. so all files Emacs knows how to handle. Using this with
  1594. command `emacs' will open most files in Emacs. Beware that this
  1595. will also open html files inside Emacs, unless you add
  1596. (\"html\" . default) to the list as well.
  1597. t Default for files not matched by any of the other options.
  1598. `system' The system command to open files, like `open' on Windows
  1599. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1600. that will be selected if you call `C-c C-o' with a double
  1601. \\[universal-argument] \\[universal-argument] prefix.
  1602. Possible values for the command are:
  1603. `emacs' The file will be visited by the current Emacs process.
  1604. `default' Use the default application for this file type, which is the
  1605. association for t in the list, most likely in the system-specific
  1606. part.
  1607. This can be used to overrule an unwanted setting in the
  1608. system-specific variable.
  1609. `system' Use the system command for opening files, like \"open\".
  1610. This command is specified by the entry whose car is `system'.
  1611. Most likely, the system-specific version of this variable
  1612. does define this command, but you can overrule/replace it
  1613. here.
  1614. string A command to be executed by a shell; %s will be replaced
  1615. by the path to the file.
  1616. sexp A Lisp form which will be evaluated. The file path will
  1617. be available in the Lisp variable `file'.
  1618. For more examples, see the system specific constants
  1619. `org-file-apps-defaults-macosx'
  1620. `org-file-apps-defaults-windowsnt'
  1621. `org-file-apps-defaults-gnu'."
  1622. :group 'org-link-follow
  1623. :type '(repeat
  1624. (cons (choice :value ""
  1625. (string :tag "Extension")
  1626. (const :tag "System command to open files" system)
  1627. (const :tag "Default for unrecognized files" t)
  1628. (const :tag "Remote file" remote)
  1629. (const :tag "Links to a directory" directory)
  1630. (const :tag "Any files that have Emacs modes"
  1631. auto-mode))
  1632. (choice :value ""
  1633. (const :tag "Visit with Emacs" emacs)
  1634. (const :tag "Use default" default)
  1635. (const :tag "Use the system command" system)
  1636. (string :tag "Command")
  1637. (sexp :tag "Lisp form")))))
  1638. (defcustom org-doi-server-url "http://dx.doi.org/"
  1639. "The URL of the DOI server."
  1640. :type 'string
  1641. :version "24.3"
  1642. :group 'org-link-follow)
  1643. (defgroup org-refile nil
  1644. "Options concerning refiling entries in Org-mode."
  1645. :tag "Org Refile"
  1646. :group 'org)
  1647. (defcustom org-directory "~/org"
  1648. "Directory with org files.
  1649. This is just a default location to look for Org files. There is no need
  1650. at all to put your files into this directory. It is only used in the
  1651. following situations:
  1652. 1. When a capture template specifies a target file that is not an
  1653. absolute path. The path will then be interpreted relative to
  1654. `org-directory'
  1655. 2. When a capture note is filed away in an interactive way (when exiting the
  1656. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1657. with `org-directory' as the default path."
  1658. :group 'org-refile
  1659. :group 'org-remember
  1660. :group 'org-capture
  1661. :type 'directory)
  1662. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1663. "Default target for storing notes.
  1664. Used as a fall back file for org-remember.el and org-capture.el, for
  1665. templates that do not specify a target file."
  1666. :group 'org-refile
  1667. :group 'org-remember
  1668. :group 'org-capture
  1669. :type '(choice
  1670. (const :tag "Default from remember-data-file" nil)
  1671. file))
  1672. (defcustom org-goto-interface 'outline
  1673. "The default interface to be used for `org-goto'.
  1674. Allowed values are:
  1675. outline The interface shows an outline of the relevant file
  1676. and the correct heading is found by moving through
  1677. the outline or by searching with incremental search.
  1678. outline-path-completion Headlines in the current buffer are offered via
  1679. completion. This is the interface also used by
  1680. the refile command."
  1681. :group 'org-refile
  1682. :type '(choice
  1683. (const :tag "Outline" outline)
  1684. (const :tag "Outline-path-completion" outline-path-completion)))
  1685. (defcustom org-goto-max-level 5
  1686. "Maximum target level when running `org-goto' with refile interface."
  1687. :group 'org-refile
  1688. :type 'integer)
  1689. (defcustom org-reverse-note-order nil
  1690. "Non-nil means store new notes at the beginning of a file or entry.
  1691. When nil, new notes will be filed to the end of a file or entry.
  1692. This can also be a list with cons cells of regular expressions that
  1693. are matched against file names, and values."
  1694. :group 'org-remember
  1695. :group 'org-capture
  1696. :group 'org-refile
  1697. :type '(choice
  1698. (const :tag "Reverse always" t)
  1699. (const :tag "Reverse never" nil)
  1700. (repeat :tag "By file name regexp"
  1701. (cons regexp boolean))))
  1702. (defcustom org-log-refile nil
  1703. "Information to record when a task is refiled.
  1704. Possible values are:
  1705. nil Don't add anything
  1706. time Add a time stamp to the task
  1707. note Prompt for a note and add it with template `org-log-note-headings'
  1708. This option can also be set with on a per-file-basis with
  1709. #+STARTUP: nologrefile
  1710. #+STARTUP: logrefile
  1711. #+STARTUP: lognoterefile
  1712. You can have local logging settings for a subtree by setting the LOGGING
  1713. property to one or more of these keywords.
  1714. When bulk-refiling from the agenda, the value `note' is forbidden and
  1715. will temporarily be changed to `time'."
  1716. :group 'org-refile
  1717. :group 'org-progress
  1718. :version "24.1"
  1719. :type '(choice
  1720. (const :tag "No logging" nil)
  1721. (const :tag "Record timestamp" time)
  1722. (const :tag "Record timestamp with note." note)))
  1723. (defcustom org-refile-targets nil
  1724. "Targets for refiling entries with \\[org-refile].
  1725. This is a list of cons cells. Each cell contains:
  1726. - a specification of the files to be considered, either a list of files,
  1727. or a symbol whose function or variable value will be used to retrieve
  1728. a file name or a list of file names. If you use `org-agenda-files' for
  1729. that, all agenda files will be scanned for targets. Nil means consider
  1730. headings in the current buffer.
  1731. - A specification of how to find candidate refile targets. This may be
  1732. any of:
  1733. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1734. This tag has to be present in all target headlines, inheritance will
  1735. not be considered.
  1736. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1737. todo keyword.
  1738. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1739. headlines that are refiling targets.
  1740. - a cons cell (:level . N). Any headline of level N is considered a target.
  1741. Note that, when `org-odd-levels-only' is set, level corresponds to
  1742. order in hierarchy, not to the number of stars.
  1743. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1744. Note that, when `org-odd-levels-only' is set, level corresponds to
  1745. order in hierarchy, not to the number of stars.
  1746. Each element of this list generates a set of possible targets.
  1747. The union of these sets is presented (with completion) to
  1748. the user by `org-refile'.
  1749. You can set the variable `org-refile-target-verify-function' to a function
  1750. to verify each headline found by the simple criteria above.
  1751. When this variable is nil, all top-level headlines in the current buffer
  1752. are used, equivalent to the value `((nil . (:level . 1))'."
  1753. :group 'org-refile
  1754. :type '(repeat
  1755. (cons
  1756. (choice :value org-agenda-files
  1757. (const :tag "All agenda files" org-agenda-files)
  1758. (const :tag "Current buffer" nil)
  1759. (function) (variable) (file))
  1760. (choice :tag "Identify target headline by"
  1761. (cons :tag "Specific tag" (const :value :tag) (string))
  1762. (cons :tag "TODO keyword" (const :value :todo) (string))
  1763. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1764. (cons :tag "Level number" (const :value :level) (integer))
  1765. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1766. (defcustom org-refile-target-verify-function nil
  1767. "Function to verify if the headline at point should be a refile target.
  1768. The function will be called without arguments, with point at the
  1769. beginning of the headline. It should return t and leave point
  1770. where it is if the headline is a valid target for refiling.
  1771. If the target should not be selected, the function must return nil.
  1772. In addition to this, it may move point to a place from where the search
  1773. should be continued. For example, the function may decide that the entire
  1774. subtree of the current entry should be excluded and move point to the end
  1775. of the subtree."
  1776. :group 'org-refile
  1777. :type 'function)
  1778. (defcustom org-refile-use-cache nil
  1779. "Non-nil means cache refile targets to speed up the process.
  1780. The cache for a particular file will be updated automatically when
  1781. the buffer has been killed, or when any of the marker used for flagging
  1782. refile targets no longer points at a live buffer.
  1783. If you have added new entries to a buffer that might themselves be targets,
  1784. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1785. find that easier, `C-u C-u C-u C-c C-w'."
  1786. :group 'org-refile
  1787. :version "24.1"
  1788. :type 'boolean)
  1789. (defcustom org-refile-use-outline-path nil
  1790. "Non-nil means provide refile targets as paths.
  1791. So a level 3 headline will be available as level1/level2/level3.
  1792. When the value is `file', also include the file name (without directory)
  1793. into the path. In this case, you can also stop the completion after
  1794. the file name, to get entries inserted as top level in the file.
  1795. When `full-file-path', include the full file path."
  1796. :group 'org-refile
  1797. :type '(choice
  1798. (const :tag "Not" nil)
  1799. (const :tag "Yes" t)
  1800. (const :tag "Start with file name" file)
  1801. (const :tag "Start with full file path" full-file-path)))
  1802. (defcustom org-outline-path-complete-in-steps t
  1803. "Non-nil means complete the outline path in hierarchical steps.
  1804. When Org-mode uses the refile interface to select an outline path
  1805. \(see variable `org-refile-use-outline-path'), the completion of
  1806. the path can be done is a single go, or if can be done in steps down
  1807. the headline hierarchy. Going in steps is probably the best if you
  1808. do not use a special completion package like `ido' or `icicles'.
  1809. However, when using these packages, going in one step can be very
  1810. fast, while still showing the whole path to the entry."
  1811. :group 'org-refile
  1812. :type 'boolean)
  1813. (defcustom org-refile-allow-creating-parent-nodes nil
  1814. "Non-nil means allow to create new nodes as refile targets.
  1815. New nodes are then created by adding \"/new node name\" to the completion
  1816. of an existing node. When the value of this variable is `confirm',
  1817. new node creation must be confirmed by the user (recommended)
  1818. When nil, the completion must match an existing entry.
  1819. Note that, if the new heading is not seen by the criteria
  1820. listed in `org-refile-targets', multiple instances of the same
  1821. heading would be created by trying again to file under the new
  1822. heading."
  1823. :group 'org-refile
  1824. :type '(choice
  1825. (const :tag "Never" nil)
  1826. (const :tag "Always" t)
  1827. (const :tag "Prompt for confirmation" confirm)))
  1828. (defcustom org-refile-active-region-within-subtree nil
  1829. "Non-nil means also refile active region within a subtree.
  1830. By default `org-refile' doesn't allow refiling regions if they
  1831. don't contain a set of subtrees, but it might be convenient to
  1832. do so sometimes: in that case, the first line of the region is
  1833. converted to a headline before refiling."
  1834. :group 'org-refile
  1835. :version "24.1"
  1836. :type 'boolean)
  1837. (defgroup org-todo nil
  1838. "Options concerning TODO items in Org-mode."
  1839. :tag "Org TODO"
  1840. :group 'org)
  1841. (defgroup org-progress nil
  1842. "Options concerning Progress logging in Org-mode."
  1843. :tag "Org Progress"
  1844. :group 'org-time)
  1845. (defvar org-todo-interpretation-widgets
  1846. '((:tag "Sequence (cycling hits every state)" sequence)
  1847. (:tag "Type (cycling directly to DONE)" type))
  1848. "The available interpretation symbols for customizing `org-todo-keywords'.
  1849. Interested libraries should add to this list.")
  1850. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1851. "List of TODO entry keyword sequences and their interpretation.
  1852. \\<org-mode-map>This is a list of sequences.
  1853. Each sequence starts with a symbol, either `sequence' or `type',
  1854. indicating if the keywords should be interpreted as a sequence of
  1855. action steps, or as different types of TODO items. The first
  1856. keywords are states requiring action - these states will select a headline
  1857. for inclusion into the global TODO list Org-mode produces. If one of
  1858. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1859. signify that no further action is necessary. If \"|\" is not found,
  1860. the last keyword is treated as the only DONE state of the sequence.
  1861. The command \\[org-todo] cycles an entry through these states, and one
  1862. additional state where no keyword is present. For details about this
  1863. cycling, see the manual.
  1864. TODO keywords and interpretation can also be set on a per-file basis with
  1865. the special #+SEQ_TODO and #+TYP_TODO lines.
  1866. Each keyword can optionally specify a character for fast state selection
  1867. \(in combination with the variable `org-use-fast-todo-selection')
  1868. and specifiers for state change logging, using the same syntax that
  1869. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1870. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1871. indicates to record a time stamp each time this state is selected.
  1872. Each keyword may also specify if a timestamp or a note should be
  1873. recorded when entering or leaving the state, by adding additional
  1874. characters in the parenthesis after the keyword. This looks like this:
  1875. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1876. record only the time of the state change. With X and Y being either
  1877. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1878. Y when leaving the state if and only if the *target* state does not
  1879. define X. You may omit any of the fast-selection key or X or /Y,
  1880. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1881. For backward compatibility, this variable may also be just a list
  1882. of keywords. In this case the interpretation (sequence or type) will be
  1883. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1884. :group 'org-todo
  1885. :group 'org-keywords
  1886. :type '(choice
  1887. (repeat :tag "Old syntax, just keywords"
  1888. (string :tag "Keyword"))
  1889. (repeat :tag "New syntax"
  1890. (cons
  1891. (choice
  1892. :tag "Interpretation"
  1893. ;;Quick and dirty way to see
  1894. ;;`org-todo-interpretations'. This takes the
  1895. ;;place of item arguments
  1896. :convert-widget
  1897. (lambda (widget)
  1898. (widget-put widget
  1899. :args (mapcar
  1900. #'(lambda (x)
  1901. (widget-convert
  1902. (cons 'const x)))
  1903. org-todo-interpretation-widgets))
  1904. widget))
  1905. (repeat
  1906. (string :tag "Keyword"))))))
  1907. (defvar org-todo-keywords-1 nil
  1908. "All TODO and DONE keywords active in a buffer.")
  1909. (make-variable-buffer-local 'org-todo-keywords-1)
  1910. (defvar org-todo-keywords-for-agenda nil)
  1911. (defvar org-done-keywords-for-agenda nil)
  1912. (defvar org-drawers-for-agenda nil)
  1913. (defvar org-todo-keyword-alist-for-agenda nil)
  1914. (defvar org-tag-alist-for-agenda nil)
  1915. (defvar org-agenda-contributing-files nil)
  1916. (defvar org-not-done-keywords nil)
  1917. (make-variable-buffer-local 'org-not-done-keywords)
  1918. (defvar org-done-keywords nil)
  1919. (make-variable-buffer-local 'org-done-keywords)
  1920. (defvar org-todo-heads nil)
  1921. (make-variable-buffer-local 'org-todo-heads)
  1922. (defvar org-todo-sets nil)
  1923. (make-variable-buffer-local 'org-todo-sets)
  1924. (defvar org-todo-log-states nil)
  1925. (make-variable-buffer-local 'org-todo-log-states)
  1926. (defvar org-todo-kwd-alist nil)
  1927. (make-variable-buffer-local 'org-todo-kwd-alist)
  1928. (defvar org-todo-key-alist nil)
  1929. (make-variable-buffer-local 'org-todo-key-alist)
  1930. (defvar org-todo-key-trigger nil)
  1931. (make-variable-buffer-local 'org-todo-key-trigger)
  1932. (defcustom org-todo-interpretation 'sequence
  1933. "Controls how TODO keywords are interpreted.
  1934. This variable is in principle obsolete and is only used for
  1935. backward compatibility, if the interpretation of todo keywords is
  1936. not given already in `org-todo-keywords'. See that variable for
  1937. more information."
  1938. :group 'org-todo
  1939. :group 'org-keywords
  1940. :type '(choice (const sequence)
  1941. (const type)))
  1942. (defcustom org-use-fast-todo-selection t
  1943. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1944. This variable describes if and under what circumstances the cycling
  1945. mechanism for TODO keywords will be replaced by a single-key, direct
  1946. selection scheme.
  1947. When nil, fast selection is never used.
  1948. When the symbol `prefix', it will be used when `org-todo' is called
  1949. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1950. `C-u t' in an agenda buffer.
  1951. When t, fast selection is used by default. In this case, the prefix
  1952. argument forces cycling instead.
  1953. In all cases, the special interface is only used if access keys have
  1954. actually been assigned by the user, i.e. if keywords in the configuration
  1955. are followed by a letter in parenthesis, like TODO(t)."
  1956. :group 'org-todo
  1957. :type '(choice
  1958. (const :tag "Never" nil)
  1959. (const :tag "By default" t)
  1960. (const :tag "Only with C-u C-c C-t" prefix)))
  1961. (defcustom org-provide-todo-statistics t
  1962. "Non-nil means update todo statistics after insert and toggle.
  1963. ALL-HEADLINES means update todo statistics by including headlines
  1964. with no TODO keyword as well, counting them as not done.
  1965. A list of TODO keywords means the same, but skip keywords that are
  1966. not in this list.
  1967. When this is set, todo statistics is updated in the parent of the
  1968. current entry each time a todo state is changed."
  1969. :group 'org-todo
  1970. :type '(choice
  1971. (const :tag "Yes, only for TODO entries" t)
  1972. (const :tag "Yes, including all entries" 'all-headlines)
  1973. (repeat :tag "Yes, for TODOs in this list"
  1974. (string :tag "TODO keyword"))
  1975. (other :tag "No TODO statistics" nil)))
  1976. (defcustom org-hierarchical-todo-statistics t
  1977. "Non-nil means TODO statistics covers just direct children.
  1978. When nil, all entries in the subtree are considered.
  1979. This has only an effect if `org-provide-todo-statistics' is set.
  1980. To set this to nil for only a single subtree, use a COOKIE_DATA
  1981. property and include the word \"recursive\" into the value."
  1982. :group 'org-todo
  1983. :type 'boolean)
  1984. (defcustom org-after-todo-state-change-hook nil
  1985. "Hook which is run after the state of a TODO item was changed.
  1986. The new state (a string with a TODO keyword, or nil) is available in the
  1987. Lisp variable `org-state'."
  1988. :group 'org-todo
  1989. :type 'hook)
  1990. (defvar org-blocker-hook nil
  1991. "Hook for functions that are allowed to block a state change.
  1992. Functions in this hook should not modify the buffer.
  1993. Each function gets as its single argument a property list,
  1994. see `org-trigger-hook' for more information about this list.
  1995. If any of the functions in this hook returns nil, the state change
  1996. is blocked.")
  1997. (defvar org-trigger-hook nil
  1998. "Hook for functions that are triggered by a state change.
  1999. Each function gets as its single argument a property list with at
  2000. least the following elements:
  2001. (:type type-of-change :position pos-at-entry-start
  2002. :from old-state :to new-state)
  2003. Depending on the type, more properties may be present.
  2004. This mechanism is currently implemented for:
  2005. TODO state changes
  2006. ------------------
  2007. :type todo-state-change
  2008. :from previous state (keyword as a string), or nil, or a symbol
  2009. 'todo' or 'done', to indicate the general type of state.
  2010. :to new state, like in :from")
  2011. (defcustom org-enforce-todo-dependencies nil
  2012. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2013. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2014. be blocked if any prior sibling is not yet done.
  2015. Finally, if the parent is blocked because of ordered siblings of its own,
  2016. the child will also be blocked."
  2017. :set (lambda (var val)
  2018. (set var val)
  2019. (if val
  2020. (add-hook 'org-blocker-hook
  2021. 'org-block-todo-from-children-or-siblings-or-parent)
  2022. (remove-hook 'org-blocker-hook
  2023. 'org-block-todo-from-children-or-siblings-or-parent)))
  2024. :group 'org-todo
  2025. :type 'boolean)
  2026. (defcustom org-enforce-todo-checkbox-dependencies nil
  2027. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2028. When this is nil, checkboxes have no influence on switching TODO states.
  2029. When non-nil, you first need to check off all check boxes before the TODO
  2030. entry can be switched to DONE.
  2031. This variable needs to be set before org.el is loaded, and you need to
  2032. restart Emacs after a change to make the change effective. The only way
  2033. to change is while Emacs is running is through the customize interface."
  2034. :set (lambda (var val)
  2035. (set var val)
  2036. (if val
  2037. (add-hook 'org-blocker-hook
  2038. 'org-block-todo-from-checkboxes)
  2039. (remove-hook 'org-blocker-hook
  2040. 'org-block-todo-from-checkboxes)))
  2041. :group 'org-todo
  2042. :type 'boolean)
  2043. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2044. "Non-nil means inserting a TODO heading is treated as state change.
  2045. So when the command \\[org-insert-todo-heading] is used, state change
  2046. logging will apply if appropriate. When nil, the new TODO item will
  2047. be inserted directly, and no logging will take place."
  2048. :group 'org-todo
  2049. :type 'boolean)
  2050. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2051. "Non-nil means switching TODO states with S-cursor counts as state change.
  2052. This is the default behavior. However, setting this to nil allows a
  2053. convenient way to select a TODO state and bypass any logging associated
  2054. with that."
  2055. :group 'org-todo
  2056. :type 'boolean)
  2057. (defcustom org-todo-state-tags-triggers nil
  2058. "Tag changes that should be triggered by TODO state changes.
  2059. This is a list. Each entry is
  2060. (state-change (tag . flag) .......)
  2061. State-change can be a string with a state, and empty string to indicate the
  2062. state that has no TODO keyword, or it can be one of the symbols `todo'
  2063. or `done', meaning any not-done or done state, respectively."
  2064. :group 'org-todo
  2065. :group 'org-tags
  2066. :type '(repeat
  2067. (cons (choice :tag "When changing to"
  2068. (const :tag "Not-done state" todo)
  2069. (const :tag "Done state" done)
  2070. (string :tag "State"))
  2071. (repeat
  2072. (cons :tag "Tag action"
  2073. (string :tag "Tag")
  2074. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2075. (defcustom org-log-done nil
  2076. "Information to record when a task moves to the DONE state.
  2077. Possible values are:
  2078. nil Don't add anything, just change the keyword
  2079. time Add a time stamp to the task
  2080. note Prompt for a note and add it with template `org-log-note-headings'
  2081. This option can also be set with on a per-file-basis with
  2082. #+STARTUP: nologdone
  2083. #+STARTUP: logdone
  2084. #+STARTUP: lognotedone
  2085. You can have local logging settings for a subtree by setting the LOGGING
  2086. property to one or more of these keywords."
  2087. :group 'org-todo
  2088. :group 'org-progress
  2089. :type '(choice
  2090. (const :tag "No logging" nil)
  2091. (const :tag "Record CLOSED timestamp" time)
  2092. (const :tag "Record CLOSED timestamp with note." note)))
  2093. ;; Normalize old uses of org-log-done.
  2094. (cond
  2095. ((eq org-log-done t) (setq org-log-done 'time))
  2096. ((and (listp org-log-done) (memq 'done org-log-done))
  2097. (setq org-log-done 'note)))
  2098. (defcustom org-log-reschedule nil
  2099. "Information to record when the scheduling date of a tasks is modified.
  2100. Possible values are:
  2101. nil Don't add anything, just change the date
  2102. time Add a time stamp to the task
  2103. note Prompt for a note and add it with template `org-log-note-headings'
  2104. This option can also be set with on a per-file-basis with
  2105. #+STARTUP: nologreschedule
  2106. #+STARTUP: logreschedule
  2107. #+STARTUP: lognotereschedule"
  2108. :group 'org-todo
  2109. :group 'org-progress
  2110. :type '(choice
  2111. (const :tag "No logging" nil)
  2112. (const :tag "Record timestamp" time)
  2113. (const :tag "Record timestamp with note." note)))
  2114. (defcustom org-log-redeadline nil
  2115. "Information to record when the deadline date of a tasks is modified.
  2116. Possible values are:
  2117. nil Don't add anything, just change the date
  2118. time Add a time stamp to the task
  2119. note Prompt for a note and add it with template `org-log-note-headings'
  2120. This option can also be set with on a per-file-basis with
  2121. #+STARTUP: nologredeadline
  2122. #+STARTUP: logredeadline
  2123. #+STARTUP: lognoteredeadline
  2124. You can have local logging settings for a subtree by setting the LOGGING
  2125. property to one or more of these keywords."
  2126. :group 'org-todo
  2127. :group 'org-progress
  2128. :type '(choice
  2129. (const :tag "No logging" nil)
  2130. (const :tag "Record timestamp" time)
  2131. (const :tag "Record timestamp with note." note)))
  2132. (defcustom org-log-note-clock-out nil
  2133. "Non-nil means record a note when clocking out of an item.
  2134. This can also be configured on a per-file basis by adding one of
  2135. the following lines anywhere in the buffer:
  2136. #+STARTUP: lognoteclock-out
  2137. #+STARTUP: nolognoteclock-out"
  2138. :group 'org-todo
  2139. :group 'org-progress
  2140. :type 'boolean)
  2141. (defcustom org-log-done-with-time t
  2142. "Non-nil means the CLOSED time stamp will contain date and time.
  2143. When nil, only the date will be recorded."
  2144. :group 'org-progress
  2145. :type 'boolean)
  2146. (defcustom org-log-note-headings
  2147. '((done . "CLOSING NOTE %t")
  2148. (state . "State %-12s from %-12S %t")
  2149. (note . "Note taken on %t")
  2150. (reschedule . "Rescheduled from %S on %t")
  2151. (delschedule . "Not scheduled, was %S on %t")
  2152. (redeadline . "New deadline from %S on %t")
  2153. (deldeadline . "Removed deadline, was %S on %t")
  2154. (refile . "Refiled on %t")
  2155. (clock-out . ""))
  2156. "Headings for notes added to entries.
  2157. The value is an alist, with the car being a symbol indicating the note
  2158. context, and the cdr is the heading to be used. The heading may also be the
  2159. empty string.
  2160. %t in the heading will be replaced by a time stamp.
  2161. %T will be an active time stamp instead the default inactive one
  2162. %d will be replaced by a short-format time stamp.
  2163. %D will be replaced by an active short-format time stamp.
  2164. %s will be replaced by the new TODO state, in double quotes.
  2165. %S will be replaced by the old TODO state, in double quotes.
  2166. %u will be replaced by the user name.
  2167. %U will be replaced by the full user name.
  2168. In fact, it is not a good idea to change the `state' entry, because
  2169. agenda log mode depends on the format of these entries."
  2170. :group 'org-todo
  2171. :group 'org-progress
  2172. :type '(list :greedy t
  2173. (cons (const :tag "Heading when closing an item" done) string)
  2174. (cons (const :tag
  2175. "Heading when changing todo state (todo sequence only)"
  2176. state) string)
  2177. (cons (const :tag "Heading when just taking a note" note) string)
  2178. (cons (const :tag "Heading when clocking out" clock-out) string)
  2179. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2180. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2181. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2182. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2183. (cons (const :tag "Heading when refiling" refile) string)))
  2184. (unless (assq 'note org-log-note-headings)
  2185. (push '(note . "%t") org-log-note-headings))
  2186. (defcustom org-log-into-drawer nil
  2187. "Non-nil means insert state change notes and time stamps into a drawer.
  2188. When nil, state changes notes will be inserted after the headline and
  2189. any scheduling and clock lines, but not inside a drawer.
  2190. The value of this variable should be the name of the drawer to use.
  2191. LOGBOOK is proposed as the default drawer for this purpose, you can
  2192. also set this to a string to define the drawer of your choice.
  2193. A value of t is also allowed, representing \"LOGBOOK\".
  2194. A value of t or nil can also be set with on a per-file-basis with
  2195. #+STARTUP: logdrawer
  2196. #+STARTUP: nologdrawer
  2197. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2198. will be ignored.
  2199. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2200. a subtree."
  2201. :group 'org-todo
  2202. :group 'org-progress
  2203. :type '(choice
  2204. (const :tag "Not into a drawer" nil)
  2205. (const :tag "LOGBOOK" t)
  2206. (string :tag "Other")))
  2207. (if (fboundp 'defvaralias)
  2208. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2209. (defun org-log-into-drawer ()
  2210. "Return the value of `org-log-into-drawer', but let properties overrule.
  2211. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2212. used instead of the default value."
  2213. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2214. (cond
  2215. ((not p) org-log-into-drawer)
  2216. ((equal p "nil") nil)
  2217. ((equal p "t") "LOGBOOK")
  2218. (t p))))
  2219. (defcustom org-log-state-notes-insert-after-drawers nil
  2220. "Non-nil means insert state change notes after any drawers in entry.
  2221. Only the drawers that *immediately* follow the headline and the
  2222. deadline/scheduled line are skipped.
  2223. When nil, insert notes right after the heading and perhaps the line
  2224. with deadline/scheduling if present.
  2225. This variable will have no effect if `org-log-into-drawer' is
  2226. set."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type 'boolean)
  2230. (defcustom org-log-states-order-reversed t
  2231. "Non-nil means the latest state note will be directly after heading.
  2232. When nil, the state change notes will be ordered according to time.
  2233. This option can also be set with on a per-file-basis with
  2234. #+STARTUP: logstatesreversed
  2235. #+STARTUP: nologstatesreversed"
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type 'boolean)
  2239. (defcustom org-todo-repeat-to-state nil
  2240. "The TODO state to which a repeater should return the repeating task.
  2241. By default this is the first task in a TODO sequence, or the previous state
  2242. in a TODO_TYP set. But you can specify another task here.
  2243. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2244. :group 'org-todo
  2245. :version "24.1"
  2246. :type '(choice (const :tag "Head of sequence" nil)
  2247. (string :tag "Specific state")))
  2248. (defcustom org-log-repeat 'time
  2249. "Non-nil means record moving through the DONE state when triggering repeat.
  2250. An auto-repeating task is immediately switched back to TODO when
  2251. marked DONE. If you are not logging state changes (by adding \"@\"
  2252. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2253. record a closing note, there will be no record of the task moving
  2254. through DONE. This variable forces taking a note anyway.
  2255. nil Don't force a record
  2256. time Record a time stamp
  2257. note Prompt for a note and add it with template `org-log-note-headings'
  2258. This option can also be set with on a per-file-basis with
  2259. #+STARTUP: nologrepeat
  2260. #+STARTUP: logrepeat
  2261. #+STARTUP: lognoterepeat
  2262. You can have local logging settings for a subtree by setting the LOGGING
  2263. property to one or more of these keywords."
  2264. :group 'org-todo
  2265. :group 'org-progress
  2266. :type '(choice
  2267. (const :tag "Don't force a record" nil)
  2268. (const :tag "Force recording the DONE state" time)
  2269. (const :tag "Force recording a note with the DONE state" note)))
  2270. (defgroup org-priorities nil
  2271. "Priorities in Org-mode."
  2272. :tag "Org Priorities"
  2273. :group 'org-todo)
  2274. (defcustom org-enable-priority-commands t
  2275. "Non-nil means priority commands are active.
  2276. When nil, these commands will be disabled, so that you never accidentally
  2277. set a priority."
  2278. :group 'org-priorities
  2279. :type 'boolean)
  2280. (defcustom org-highest-priority ?A
  2281. "The highest priority of TODO items. A character like ?A, ?B etc.
  2282. Must have a smaller ASCII number than `org-lowest-priority'."
  2283. :group 'org-priorities
  2284. :type 'character)
  2285. (defcustom org-lowest-priority ?C
  2286. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2287. Must have a larger ASCII number than `org-highest-priority'."
  2288. :group 'org-priorities
  2289. :type 'character)
  2290. (defcustom org-default-priority ?B
  2291. "The default priority of TODO items.
  2292. This is the priority an item gets if no explicit priority is given.
  2293. When starting to cycle on an empty priority the first step in the cycle
  2294. depends on `org-priority-start-cycle-with-default'. The resulting first
  2295. step priority must not exceed the range from `org-highest-priority' to
  2296. `org-lowest-priority' which means that `org-default-priority' has to be
  2297. in this range exclusive or inclusive the range boundaries. Else the
  2298. first step refuses to set the default and the second will fall back
  2299. to (depending on the command used) the highest or lowest priority."
  2300. :group 'org-priorities
  2301. :type 'character)
  2302. (defcustom org-priority-start-cycle-with-default t
  2303. "Non-nil means start with default priority when starting to cycle.
  2304. When this is nil, the first step in the cycle will be (depending on the
  2305. command used) one higher or lower than the default priority.
  2306. See also `org-default-priority'."
  2307. :group 'org-priorities
  2308. :type 'boolean)
  2309. (defcustom org-get-priority-function nil
  2310. "Function to extract the priority from a string.
  2311. The string is normally the headline. If this is nil Org computes the
  2312. priority from the priority cookie like [#A] in the headline. It returns
  2313. an integer, increasing by 1000 for each priority level.
  2314. The user can set a different function here, which should take a string
  2315. as an argument and return the numeric priority."
  2316. :group 'org-priorities
  2317. :version "24.1"
  2318. :type 'function)
  2319. (defgroup org-time nil
  2320. "Options concerning time stamps and deadlines in Org-mode."
  2321. :tag "Org Time"
  2322. :group 'org)
  2323. (defcustom org-insert-labeled-timestamps-at-point nil
  2324. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2325. When nil, these labeled time stamps are forces into the second line of an
  2326. entry, just after the headline. When scheduling from the global TODO list,
  2327. the time stamp will always be forced into the second line."
  2328. :group 'org-time
  2329. :type 'boolean)
  2330. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2331. "Formats for `format-time-string' which are used for time stamps.
  2332. It is not recommended to change this constant.")
  2333. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2334. "Number of minutes to round time stamps to.
  2335. These are two values, the first applies when first creating a time stamp.
  2336. The second applies when changing it with the commands `S-up' and `S-down'.
  2337. When changing the time stamp, this means that it will change in steps
  2338. of N minutes, as given by the second value.
  2339. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2340. numbers should be factors of 60, so for example 5, 10, 15.
  2341. When this is larger than 1, you can still force an exact time stamp by using
  2342. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2343. and by using a prefix arg to `S-up/down' to specify the exact number
  2344. of minutes to shift."
  2345. :group 'org-time
  2346. :get #'(lambda (var) ; Make sure both elements are there
  2347. (if (integerp (default-value var))
  2348. (list (default-value var) 5)
  2349. (default-value var)))
  2350. :type '(list
  2351. (integer :tag "when inserting times")
  2352. (integer :tag "when modifying times")))
  2353. ;; Normalize old customizations of this variable.
  2354. (when (integerp org-time-stamp-rounding-minutes)
  2355. (setq org-time-stamp-rounding-minutes
  2356. (list org-time-stamp-rounding-minutes
  2357. org-time-stamp-rounding-minutes)))
  2358. (defcustom org-display-custom-times nil
  2359. "Non-nil means overlay custom formats over all time stamps.
  2360. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2361. To turn this on on a per-file basis, insert anywhere in the file:
  2362. #+STARTUP: customtime"
  2363. :group 'org-time
  2364. :set 'set-default
  2365. :type 'sexp)
  2366. (make-variable-buffer-local 'org-display-custom-times)
  2367. (defcustom org-time-stamp-custom-formats
  2368. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2369. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2370. These are overlaid over the default ISO format if the variable
  2371. `org-display-custom-times' is set. Time like %H:%M should be at the
  2372. end of the second format. The custom formats are also honored by export
  2373. commands, if custom time display is turned on at the time of export."
  2374. :group 'org-time
  2375. :type 'sexp)
  2376. (defun org-time-stamp-format (&optional long inactive)
  2377. "Get the right format for a time string."
  2378. (let ((f (if long (cdr org-time-stamp-formats)
  2379. (car org-time-stamp-formats))))
  2380. (if inactive
  2381. (concat "[" (substring f 1 -1) "]")
  2382. f)))
  2383. (defcustom org-time-clocksum-format
  2384. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2385. "The format string used when creating CLOCKSUM lines.
  2386. This is also used when Org mode generates a time duration.
  2387. The value can be a single format string containing two
  2388. %-sequences, which will be filled with the number of hours and
  2389. minutes in that order.
  2390. Alternatively, the value can be a plist associating any of the
  2391. keys :years, :months, :weeks, :days, :hours or :minutes with
  2392. format strings. The time duration is formatted using only the
  2393. time components that are needed and concatenating the results.
  2394. If a time unit in absent, it falls back to the next smallest
  2395. unit.
  2396. The keys :require-years, :require-months, :require-days,
  2397. :require-weeks, :require-hours, :require-minutes are also
  2398. meaningful. A non-nil value for these keys indicates that the
  2399. corresponding time component should always be included, even if
  2400. its value is 0.
  2401. For example,
  2402. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2403. :require-minutes t)
  2404. means durations longer than a day will be expressed in days,
  2405. hours and minutes, and durations less than a day will always be
  2406. expressed in hours and minutes (even for durations less than an
  2407. hour).
  2408. The value
  2409. \(:days \"%dd\" :minutes \"%dm\")
  2410. means durations longer than a day will be expressed in days and
  2411. minutes, and durations less than a day will be expressed entirely
  2412. in minutes (even for durations longer than an hour)."
  2413. :group 'org-time
  2414. :group 'org-clock
  2415. :version "24.3"
  2416. :type '(choice (string :tag "Format string")
  2417. (set :tag "Plist"
  2418. (group :inline t (const :tag "Years" :years)
  2419. (string :tag "Format string"))
  2420. (group :inline t
  2421. (const :tag "Always show years" :require-years)
  2422. (const t))
  2423. (group :inline t (const :tag "Months" :months)
  2424. (string :tag "Format string"))
  2425. (group :inline t
  2426. (const :tag "Always show months" :require-months)
  2427. (const t))
  2428. (group :inline t (const :tag "Weeks" :weeks)
  2429. (string :tag "Format string"))
  2430. (group :inline t
  2431. (const :tag "Always show weeks" :require-weeks)
  2432. (const t))
  2433. (group :inline t (const :tag "Days" :days)
  2434. (string :tag "Format string"))
  2435. (group :inline t
  2436. (const :tag "Always show days" :require-days)
  2437. (const t))
  2438. (group :inline t (const :tag "Hours" :hours)
  2439. (string :tag "Format string"))
  2440. (group :inline t
  2441. (const :tag "Always show hours" :require-hours)
  2442. (const t))
  2443. (group :inline t (const :tag "Minutes" :minutes)
  2444. (string :tag "Format string"))
  2445. (group :inline t
  2446. (const :tag "Always show minutes" :require-minutes)
  2447. (const t)))))
  2448. (defcustom org-time-clocksum-use-fractional nil
  2449. "When non-nil, \\[org-clock-display] uses fractional times.
  2450. See `org-time-clocksum-format' for more on time clock formats."
  2451. :group 'org-time
  2452. :group 'org-clock
  2453. :version "24.3"
  2454. :type 'boolean)
  2455. (defcustom org-time-clocksum-use-effort-durations t
  2456. "When non-nil, \\[org-clock-display] uses effort durations.
  2457. E.g. by default, one day is considered to be a 8 hours effort,
  2458. so a task that has been clocked for 16 hours will be displayed
  2459. as during 2 days in the clock display or in the clocktable.
  2460. See `org-effort-durations' on how to set effort durations
  2461. and `org-time-clocksum-format' for more on time clock formats."
  2462. :group 'org-time
  2463. :group 'org-clock
  2464. :version "24.3"
  2465. :type 'boolean)
  2466. (defcustom org-time-clocksum-fractional-format "%.2f"
  2467. "The format string used when creating CLOCKSUM lines,
  2468. or when Org mode generates a time duration, if
  2469. `org-time-clocksum-use-fractional' is enabled.
  2470. The value can be a single format string containing one
  2471. %-sequence, which will be filled with the number of hours as
  2472. a float.
  2473. Alternatively, the value can be a plist associating any of the
  2474. keys :years, :months, :weeks, :days, :hours or :minutes with
  2475. a format string. The time duration is formatted using the
  2476. largest time unit which gives a non-zero integer part. If all
  2477. specified formats have zero integer part, the smallest time unit
  2478. is used."
  2479. :group 'org-time
  2480. :type '(choice (string :tag "Format string")
  2481. (set (group :inline t (const :tag "Years" :years)
  2482. (string :tag "Format string"))
  2483. (group :inline t (const :tag "Months" :months)
  2484. (string :tag "Format string"))
  2485. (group :inline t (const :tag "Weeks" :weeks)
  2486. (string :tag "Format string"))
  2487. (group :inline t (const :tag "Days" :days)
  2488. (string :tag "Format string"))
  2489. (group :inline t (const :tag "Hours" :hours)
  2490. (string :tag "Format string"))
  2491. (group :inline t (const :tag "Minutes" :minutes)
  2492. (string :tag "Format string")))))
  2493. (defcustom org-deadline-warning-days 14
  2494. "No. of days before expiration during which a deadline becomes active.
  2495. This variable governs the display in sparse trees and in the agenda.
  2496. When 0 or negative, it means use this number (the absolute value of it)
  2497. even if a deadline has a different individual lead time specified.
  2498. Custom commands can set this variable in the options section."
  2499. :group 'org-time
  2500. :group 'org-agenda-daily/weekly
  2501. :type 'integer)
  2502. (defcustom org-read-date-prefer-future t
  2503. "Non-nil means assume future for incomplete date input from user.
  2504. This affects the following situations:
  2505. 1. The user gives a month but not a year.
  2506. For example, if it is April and you enter \"feb 2\", this will be read
  2507. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2508. 2. The user gives a day, but no month.
  2509. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2510. read this as the third of *next* month. However, if you enter \"17\",
  2511. it will be considered as *this* month.
  2512. If you set this variable to the symbol `time', then also the following
  2513. will work:
  2514. 3. If the user gives a time.
  2515. If the time is before now, it will be interpreted as tomorrow.
  2516. Currently none of this works for ISO week specifications.
  2517. When this option is nil, the current day, month and year will always be
  2518. used as defaults.
  2519. See also `org-agenda-jump-prefer-future'."
  2520. :group 'org-time
  2521. :type '(choice
  2522. (const :tag "Never" nil)
  2523. (const :tag "Check month and day" t)
  2524. (const :tag "Check month, day, and time" time)))
  2525. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2526. "Should the agenda jump command prefer the future for incomplete dates?
  2527. The default is to do the same as configured in `org-read-date-prefer-future'.
  2528. But you can also set a deviating value here.
  2529. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2530. :group 'org-agenda
  2531. :group 'org-time
  2532. :version "24.1"
  2533. :type '(choice
  2534. (const :tag "Use org-read-date-prefer-future"
  2535. org-read-date-prefer-future)
  2536. (const :tag "Never" nil)
  2537. (const :tag "Always" t)))
  2538. (defcustom org-read-date-force-compatible-dates t
  2539. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2540. Depending on the system Emacs is running on, certain dates cannot
  2541. be represented with the type used internally to represent time.
  2542. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2543. correctly. Some systems allow for earlier dates, some for later,
  2544. some for both. One way to find out it to insert any date into an
  2545. Org buffer, putting the cursor on the year and hitting S-up and
  2546. S-down to test the range.
  2547. When this variable is set to t, the date/time prompt will not let
  2548. you specify dates outside the 1970-2037 range, so it is certain that
  2549. these dates will work in whatever version of Emacs you are
  2550. running, and also that you can move a file from one Emacs implementation
  2551. to another. WHenever Org is forcing the year for you, it will display
  2552. a message and beep.
  2553. When this variable is nil, Org will check if the date is
  2554. representable in the specific Emacs implementation you are using.
  2555. If not, it will force a year, usually the current year, and beep
  2556. to remind you. Currently this setting is not recommended because
  2557. the likelihood that you will open your Org files in an Emacs that
  2558. has limited date range is not negligible.
  2559. A workaround for this problem is to use diary sexp dates for time
  2560. stamps outside of this range."
  2561. :group 'org-time
  2562. :version "24.1"
  2563. :type 'boolean)
  2564. (defcustom org-read-date-display-live t
  2565. "Non-nil means display current interpretation of date prompt live.
  2566. This display will be in an overlay, in the minibuffer."
  2567. :group 'org-time
  2568. :type 'boolean)
  2569. (defcustom org-read-date-popup-calendar t
  2570. "Non-nil means pop up a calendar when prompting for a date.
  2571. In the calendar, the date can be selected with mouse-1. However, the
  2572. minibuffer will also be active, and you can simply enter the date as well.
  2573. When nil, only the minibuffer will be available."
  2574. :group 'org-time
  2575. :type 'boolean)
  2576. (if (fboundp 'defvaralias)
  2577. (defvaralias 'org-popup-calendar-for-date-prompt
  2578. 'org-read-date-popup-calendar))
  2579. (make-obsolete-variable
  2580. 'org-read-date-minibuffer-setup-hook
  2581. "Set `org-read-date-minibuffer-local-map' instead." "24.3")
  2582. (defcustom org-read-date-minibuffer-setup-hook nil
  2583. "Hook to be used to set up keys for the date/time interface.
  2584. Add key definitions to `minibuffer-local-map', which will be a
  2585. temporary copy.
  2586. WARNING: This option is obsolete, you should use
  2587. `org-read-date-minibuffer-local-map' to set up keys."
  2588. :group 'org-time
  2589. :type 'hook)
  2590. (defcustom org-extend-today-until 0
  2591. "The hour when your day really ends. Must be an integer.
  2592. This has influence for the following applications:
  2593. - When switching the agenda to \"today\". It it is still earlier than
  2594. the time given here, the day recognized as TODAY is actually yesterday.
  2595. - When a date is read from the user and it is still before the time given
  2596. here, the current date and time will be assumed to be yesterday, 23:59.
  2597. Also, timestamps inserted in capture templates follow this rule.
  2598. IMPORTANT: This is a feature whose implementation is and likely will
  2599. remain incomplete. Really, it is only here because past midnight seems to
  2600. be the favorite working time of John Wiegley :-)"
  2601. :group 'org-time
  2602. :type 'integer)
  2603. (defcustom org-use-effective-time nil
  2604. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2605. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2606. \"effective time\" of any timestamps between midnight and 8am will be
  2607. 23:59 of the previous day."
  2608. :group 'org-time
  2609. :version "24.1"
  2610. :type 'boolean)
  2611. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2612. "When non-nil, use the last clock out time for `org-todo'.
  2613. Note that this option has precedence over the combined use of
  2614. `org-use-effective-time' and `org-extend-today-until'."
  2615. :group 'org-time
  2616. ;; :version "24.3"
  2617. :type 'boolean)
  2618. (defcustom org-edit-timestamp-down-means-later nil
  2619. "Non-nil means S-down will increase the time in a time stamp.
  2620. When nil, S-up will increase."
  2621. :group 'org-time
  2622. :type 'boolean)
  2623. (defcustom org-calendar-follow-timestamp-change t
  2624. "Non-nil means make the calendar window follow timestamp changes.
  2625. When a timestamp is modified and the calendar window is visible, it will be
  2626. moved to the new date."
  2627. :group 'org-time
  2628. :type 'boolean)
  2629. (defgroup org-tags nil
  2630. "Options concerning tags in Org-mode."
  2631. :tag "Org Tags"
  2632. :group 'org)
  2633. (defcustom org-tag-alist nil
  2634. "List of tags allowed in Org-mode files.
  2635. When this list is nil, Org-mode will base TAG input on what is already in the
  2636. buffer.
  2637. The value of this variable is an alist, the car of each entry must be a
  2638. keyword as a string, the cdr may be a character that is used to select
  2639. that tag through the fast-tag-selection interface.
  2640. See the manual for details."
  2641. :group 'org-tags
  2642. :type '(repeat
  2643. (choice
  2644. (cons (string :tag "Tag name")
  2645. (character :tag "Access char"))
  2646. (list :tag "Start radio group"
  2647. (const :startgroup)
  2648. (option (string :tag "Group description")))
  2649. (list :tag "End radio group"
  2650. (const :endgroup)
  2651. (option (string :tag "Group description")))
  2652. (const :tag "New line" (:newline)))))
  2653. (defcustom org-tag-persistent-alist nil
  2654. "List of tags that will always appear in all Org-mode files.
  2655. This is in addition to any in buffer settings or customizations
  2656. of `org-tag-alist'.
  2657. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2658. The value of this variable is an alist, the car of each entry must be a
  2659. keyword as a string, the cdr may be a character that is used to select
  2660. that tag through the fast-tag-selection interface.
  2661. See the manual for details.
  2662. To disable these tags on a per-file basis, insert anywhere in the file:
  2663. #+STARTUP: noptag"
  2664. :group 'org-tags
  2665. :type '(repeat
  2666. (choice
  2667. (cons (string :tag "Tag name")
  2668. (character :tag "Access char"))
  2669. (const :tag "Start radio group" (:startgroup))
  2670. (const :tag "End radio group" (:endgroup))
  2671. (const :tag "New line" (:newline)))))
  2672. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2673. "If non-nil, always offer completion for all tags of all agenda files.
  2674. Instead of customizing this variable directly, you might want to
  2675. set it locally for capture buffers, because there no list of
  2676. tags in that file can be created dynamically (there are none).
  2677. (add-hook 'org-capture-mode-hook
  2678. (lambda ()
  2679. (set (make-local-variable
  2680. 'org-complete-tags-always-offer-all-agenda-tags)
  2681. t)))"
  2682. :group 'org-tags
  2683. :version "24.1"
  2684. :type 'boolean)
  2685. (defvar org-file-tags nil
  2686. "List of tags that can be inherited by all entries in the file.
  2687. The tags will be inherited if the variable `org-use-tag-inheritance'
  2688. says they should be.
  2689. This variable is populated from #+FILETAGS lines.")
  2690. (defcustom org-use-fast-tag-selection 'auto
  2691. "Non-nil means use fast tag selection scheme.
  2692. This is a special interface to select and deselect tags with single keys.
  2693. When nil, fast selection is never used.
  2694. When the symbol `auto', fast selection is used if and only if selection
  2695. characters for tags have been configured, either through the variable
  2696. `org-tag-alist' or through a #+TAGS line in the buffer.
  2697. When t, fast selection is always used and selection keys are assigned
  2698. automatically if necessary."
  2699. :group 'org-tags
  2700. :type '(choice
  2701. (const :tag "Always" t)
  2702. (const :tag "Never" nil)
  2703. (const :tag "When selection characters are configured" 'auto)))
  2704. (defcustom org-fast-tag-selection-single-key nil
  2705. "Non-nil means fast tag selection exits after first change.
  2706. When nil, you have to press RET to exit it.
  2707. During fast tag selection, you can toggle this flag with `C-c'.
  2708. This variable can also have the value `expert'. In this case, the window
  2709. displaying the tags menu is not even shown, until you press C-c again."
  2710. :group 'org-tags
  2711. :type '(choice
  2712. (const :tag "No" nil)
  2713. (const :tag "Yes" t)
  2714. (const :tag "Expert" expert)))
  2715. (defvar org-fast-tag-selection-include-todo nil
  2716. "Non-nil means fast tags selection interface will also offer TODO states.
  2717. This is an undocumented feature, you should not rely on it.")
  2718. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2719. "The column to which tags should be indented in a headline.
  2720. If this number is positive, it specifies the column. If it is negative,
  2721. it means that the tags should be flushright to that column. For example,
  2722. -80 works well for a normal 80 character screen.
  2723. When 0, place tags directly after headline text, with only one space in
  2724. between."
  2725. :group 'org-tags
  2726. :type 'integer)
  2727. (defcustom org-auto-align-tags t
  2728. "Non-nil keeps tags aligned when modifying headlines.
  2729. Some operations (i.e. demoting) change the length of a headline and
  2730. therefore shift the tags around. With this option turned on, after
  2731. each such operation the tags are again aligned to `org-tags-column'."
  2732. :group 'org-tags
  2733. :type 'boolean)
  2734. (defcustom org-use-tag-inheritance t
  2735. "Non-nil means tags in levels apply also for sublevels.
  2736. When nil, only the tags directly given in a specific line apply there.
  2737. This may also be a list of tags that should be inherited, or a regexp that
  2738. matches tags that should be inherited. Additional control is possible
  2739. with the variable `org-tags-exclude-from-inheritance' which gives an
  2740. explicit list of tags to be excluded from inheritance, even if the value of
  2741. `org-use-tag-inheritance' would select it for inheritance.
  2742. If this option is t, a match early-on in a tree can lead to a large
  2743. number of matches in the subtree when constructing the agenda or creating
  2744. a sparse tree. If you only want to see the first match in a tree during
  2745. a search, check out the variable `org-tags-match-list-sublevels'."
  2746. :group 'org-tags
  2747. :type '(choice
  2748. (const :tag "Not" nil)
  2749. (const :tag "Always" t)
  2750. (repeat :tag "Specific tags" (string :tag "Tag"))
  2751. (regexp :tag "Tags matched by regexp")))
  2752. (defcustom org-tags-exclude-from-inheritance nil
  2753. "List of tags that should never be inherited.
  2754. This is a way to exclude a few tags from inheritance. For way to do
  2755. the opposite, to actively allow inheritance for selected tags,
  2756. see the variable `org-use-tag-inheritance'."
  2757. :group 'org-tags
  2758. :type '(repeat (string :tag "Tag")))
  2759. (defun org-tag-inherit-p (tag)
  2760. "Check if TAG is one that should be inherited."
  2761. (cond
  2762. ((member tag org-tags-exclude-from-inheritance) nil)
  2763. ((eq org-use-tag-inheritance t) t)
  2764. ((not org-use-tag-inheritance) nil)
  2765. ((stringp org-use-tag-inheritance)
  2766. (string-match org-use-tag-inheritance tag))
  2767. ((listp org-use-tag-inheritance)
  2768. (member tag org-use-tag-inheritance))
  2769. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2770. (defcustom org-tags-match-list-sublevels t
  2771. "Non-nil means list also sublevels of headlines matching a search.
  2772. This variable applies to tags/property searches, and also to stuck
  2773. projects because this search is based on a tags match as well.
  2774. When set to the symbol `indented', sublevels are indented with
  2775. leading dots.
  2776. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2777. the sublevels of a headline matching a tag search often also match
  2778. the same search. Listing all of them can create very long lists.
  2779. Setting this variable to nil causes subtrees of a match to be skipped.
  2780. This variable is semi-obsolete and probably should always be true. It
  2781. is better to limit inheritance to certain tags using the variables
  2782. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2783. :group 'org-tags
  2784. :type '(choice
  2785. (const :tag "No, don't list them" nil)
  2786. (const :tag "Yes, do list them" t)
  2787. (const :tag "List them, indented with leading dots" indented)))
  2788. (defcustom org-tags-sort-function nil
  2789. "When set, tags are sorted using this function as a comparator."
  2790. :group 'org-tags
  2791. :type '(choice
  2792. (const :tag "No sorting" nil)
  2793. (const :tag "Alphabetical" string<)
  2794. (const :tag "Reverse alphabetical" string>)
  2795. (function :tag "Custom function" nil)))
  2796. (defvar org-tags-history nil
  2797. "History of minibuffer reads for tags.")
  2798. (defvar org-last-tags-completion-table nil
  2799. "The last used completion table for tags.")
  2800. (defvar org-after-tags-change-hook nil
  2801. "Hook that is run after the tags in a line have changed.")
  2802. (defgroup org-properties nil
  2803. "Options concerning properties in Org-mode."
  2804. :tag "Org Properties"
  2805. :group 'org)
  2806. (defcustom org-property-format "%-10s %s"
  2807. "How property key/value pairs should be formatted by `indent-line'.
  2808. When `indent-line' hits a property definition, it will format the line
  2809. according to this format, mainly to make sure that the values are
  2810. lined-up with respect to each other."
  2811. :group 'org-properties
  2812. :type 'string)
  2813. (defcustom org-properties-postprocess-alist nil
  2814. "Alist of properties and functions to adjust inserted values.
  2815. Elements of this alist must be of the form
  2816. ([string] [function])
  2817. where [string] must be a property name and [function] must be a
  2818. lambda expression: this lambda expression must take one argument,
  2819. the value to adjust, and return the new value as a string.
  2820. For example, this element will allow the property \"Remaining\"
  2821. to be updated wrt the relation between the \"Effort\" property
  2822. and the clock summary:
  2823. ((\"Remaining\" (lambda(value)
  2824. (let ((clocksum (org-clock-sum-current-item))
  2825. (effort (org-duration-string-to-minutes
  2826. (org-entry-get (point) \"Effort\"))))
  2827. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2828. :group 'org-properties
  2829. :version "24.1"
  2830. :type '(alist :key-type (string :tag "Property")
  2831. :value-type (function :tag "Function")))
  2832. (defcustom org-use-property-inheritance nil
  2833. "Non-nil means properties apply also for sublevels.
  2834. This setting is chiefly used during property searches. Turning it on can
  2835. cause significant overhead when doing a search, which is why it is not
  2836. on by default.
  2837. When nil, only the properties directly given in the current entry count.
  2838. When t, every property is inherited. The value may also be a list of
  2839. properties that should have inheritance, or a regular expression matching
  2840. properties that should be inherited.
  2841. However, note that some special properties use inheritance under special
  2842. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2843. and the properties ending in \"_ALL\" when they are used as descriptor
  2844. for valid values of a property.
  2845. Note for programmers:
  2846. When querying an entry with `org-entry-get', you can control if inheritance
  2847. should be used. By default, `org-entry-get' looks only at the local
  2848. properties. You can request inheritance by setting the inherit argument
  2849. to t (to force inheritance) or to `selective' (to respect the setting
  2850. in this variable)."
  2851. :group 'org-properties
  2852. :type '(choice
  2853. (const :tag "Not" nil)
  2854. (const :tag "Always" t)
  2855. (repeat :tag "Specific properties" (string :tag "Property"))
  2856. (regexp :tag "Properties matched by regexp")))
  2857. (defun org-property-inherit-p (property)
  2858. "Check if PROPERTY is one that should be inherited."
  2859. (cond
  2860. ((eq org-use-property-inheritance t) t)
  2861. ((not org-use-property-inheritance) nil)
  2862. ((stringp org-use-property-inheritance)
  2863. (string-match org-use-property-inheritance property))
  2864. ((listp org-use-property-inheritance)
  2865. (member property org-use-property-inheritance))
  2866. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2867. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2868. "The default column format, if no other format has been defined.
  2869. This variable can be set on the per-file basis by inserting a line
  2870. #+COLUMNS: %25ITEM ....."
  2871. :group 'org-properties
  2872. :type 'string)
  2873. (defcustom org-columns-ellipses ".."
  2874. "The ellipses to be used when a field in column view is truncated.
  2875. When this is the empty string, as many characters as possible are shown,
  2876. but then there will be no visual indication that the field has been truncated.
  2877. When this is a string of length N, the last N characters of a truncated
  2878. field are replaced by this string. If the column is narrower than the
  2879. ellipses string, only part of the ellipses string will be shown."
  2880. :group 'org-properties
  2881. :type 'string)
  2882. (defcustom org-columns-modify-value-for-display-function nil
  2883. "Function that modifies values for display in column view.
  2884. For example, it can be used to cut out a certain part from a time stamp.
  2885. The function must take 2 arguments:
  2886. column-title The title of the column (*not* the property name)
  2887. value The value that should be modified.
  2888. The function should return the value that should be displayed,
  2889. or nil if the normal value should be used."
  2890. :group 'org-properties
  2891. :type 'function)
  2892. (defcustom org-effort-property "Effort"
  2893. "The property that is being used to keep track of effort estimates.
  2894. Effort estimates given in this property need to have the format H:MM."
  2895. :group 'org-properties
  2896. :group 'org-progress
  2897. :type '(string :tag "Property"))
  2898. (defconst org-global-properties-fixed
  2899. '(("VISIBILITY_ALL" . "folded children content all")
  2900. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2901. "List of property/value pairs that can be inherited by any entry.
  2902. These are fixed values, for the preset properties. The user variable
  2903. that can be used to add to this list is `org-global-properties'.
  2904. The entries in this list are cons cells where the car is a property
  2905. name and cdr is a string with the value. If the value represents
  2906. multiple items like an \"_ALL\" property, separate the items by
  2907. spaces.")
  2908. (defcustom org-global-properties nil
  2909. "List of property/value pairs that can be inherited by any entry.
  2910. This list will be combined with the constant `org-global-properties-fixed'.
  2911. The entries in this list are cons cells where the car is a property
  2912. name and cdr is a string with the value.
  2913. You can set buffer-local values for the same purpose in the variable
  2914. `org-file-properties' this by adding lines like
  2915. #+PROPERTY: NAME VALUE"
  2916. :group 'org-properties
  2917. :type '(repeat
  2918. (cons (string :tag "Property")
  2919. (string :tag "Value"))))
  2920. (defvar org-file-properties nil
  2921. "List of property/value pairs that can be inherited by any entry.
  2922. Valid for the current buffer.
  2923. This variable is populated from #+PROPERTY lines.")
  2924. (make-variable-buffer-local 'org-file-properties)
  2925. (defgroup org-agenda nil
  2926. "Options concerning agenda views in Org-mode."
  2927. :tag "Org Agenda"
  2928. :group 'org)
  2929. (defvar org-category nil
  2930. "Variable used by org files to set a category for agenda display.
  2931. Such files should use a file variable to set it, for example
  2932. # -*- mode: org; org-category: \"ELisp\"
  2933. or contain a special line
  2934. #+CATEGORY: ELisp
  2935. If the file does not specify a category, then file's base name
  2936. is used instead.")
  2937. (make-variable-buffer-local 'org-category)
  2938. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2939. (defcustom org-agenda-files nil
  2940. "The files to be used for agenda display.
  2941. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2942. \\[org-remove-file]. You can also use customize to edit the list.
  2943. If an entry is a directory, all files in that directory that are matched by
  2944. `org-agenda-file-regexp' will be part of the file list.
  2945. If the value of the variable is not a list but a single file name, then
  2946. the list of agenda files is actually stored and maintained in that file, one
  2947. agenda file per line. In this file paths can be given relative to
  2948. `org-directory'. Tilde expansion and environment variable substitution
  2949. are also made."
  2950. :group 'org-agenda
  2951. :type '(choice
  2952. (repeat :tag "List of files and directories" file)
  2953. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2954. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2955. "Regular expression to match files for `org-agenda-files'.
  2956. If any element in the list in that variable contains a directory instead
  2957. of a normal file, all files in that directory that are matched by this
  2958. regular expression will be included."
  2959. :group 'org-agenda
  2960. :type 'regexp)
  2961. (defcustom org-agenda-text-search-extra-files nil
  2962. "List of extra files to be searched by text search commands.
  2963. These files will be search in addition to the agenda files by the
  2964. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2965. Note that these files will only be searched for text search commands,
  2966. not for the other agenda views like todo lists, tag searches or the weekly
  2967. agenda. This variable is intended to list notes and possibly archive files
  2968. that should also be searched by these two commands.
  2969. In fact, if the first element in the list is the symbol `agenda-archives',
  2970. than all archive files of all agenda files will be added to the search
  2971. scope."
  2972. :group 'org-agenda
  2973. :type '(set :greedy t
  2974. (const :tag "Agenda Archives" agenda-archives)
  2975. (repeat :inline t (file))))
  2976. (if (fboundp 'defvaralias)
  2977. (defvaralias 'org-agenda-multi-occur-extra-files
  2978. 'org-agenda-text-search-extra-files))
  2979. (defcustom org-agenda-skip-unavailable-files nil
  2980. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2981. A nil value means to remove them, after a query, from the list."
  2982. :group 'org-agenda
  2983. :type 'boolean)
  2984. (defcustom org-calendar-to-agenda-key [?c]
  2985. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2986. The command `org-calendar-goto-agenda' will be bound to this key. The
  2987. default is the character `c' because then `c' can be used to switch back and
  2988. forth between agenda and calendar."
  2989. :group 'org-agenda
  2990. :type 'sexp)
  2991. (defcustom org-calendar-insert-diary-entry-key [?i]
  2992. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2993. This option is irrelevant until `org-agenda-diary-file' has been configured
  2994. to point to an Org-mode file. When that is the case, the command
  2995. `org-agenda-diary-entry' will be bound to the key given here, by default
  2996. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2997. if you want to continue doing this, you need to change this to a different
  2998. key."
  2999. :group 'org-agenda
  3000. :type 'sexp)
  3001. (defcustom org-agenda-diary-file 'diary-file
  3002. "File to which to add new entries with the `i' key in agenda and calendar.
  3003. When this is the symbol `diary-file', the functionality in the Emacs
  3004. calendar will be used to add entries to the `diary-file'. But when this
  3005. points to a file, `org-agenda-diary-entry' will be used instead."
  3006. :group 'org-agenda
  3007. :type '(choice
  3008. (const :tag "The standard Emacs diary file" diary-file)
  3009. (file :tag "Special Org file diary entries")))
  3010. (eval-after-load "calendar"
  3011. '(progn
  3012. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3013. 'org-calendar-goto-agenda)
  3014. (add-hook 'calendar-mode-hook
  3015. (lambda ()
  3016. (unless (eq org-agenda-diary-file 'diary-file)
  3017. (define-key calendar-mode-map
  3018. org-calendar-insert-diary-entry-key
  3019. 'org-agenda-diary-entry))))))
  3020. (defgroup org-latex nil
  3021. "Options for embedding LaTeX code into Org-mode."
  3022. :tag "Org LaTeX"
  3023. :group 'org)
  3024. (defcustom org-format-latex-options
  3025. '(:foreground default :background default :scale 1.0
  3026. :html-foreground "Black" :html-background "Transparent"
  3027. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3028. "Options for creating images from LaTeX fragments.
  3029. This is a property list with the following properties:
  3030. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3031. `default' means use the foreground of the default face.
  3032. `auto' means use the foreground from the text face.
  3033. :background the background color, or \"Transparent\".
  3034. `default' means use the background of the default face.
  3035. `auto' means use the background from the text face.
  3036. :scale a scaling factor for the size of the images, to get more pixels
  3037. :html-foreground, :html-background, :html-scale
  3038. the same numbers for HTML export.
  3039. :matchers a list indicating which matchers should be used to
  3040. find LaTeX fragments. Valid members of this list are:
  3041. \"begin\" find environments
  3042. \"$1\" find single characters surrounded by $.$
  3043. \"$\" find math expressions surrounded by $...$
  3044. \"$$\" find math expressions surrounded by $$....$$
  3045. \"\\(\" find math expressions surrounded by \\(...\\)
  3046. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3047. :group 'org-latex
  3048. :type 'plist)
  3049. (defcustom org-format-latex-signal-error t
  3050. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3051. When nil, just push out a message."
  3052. :group 'org-latex
  3053. :version "24.1"
  3054. :type 'boolean)
  3055. (defcustom org-latex-to-mathml-jar-file nil
  3056. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3057. Use this to specify additional executable file say a jar file.
  3058. When using MathToWeb as the converter, specify the full-path to
  3059. your mathtoweb.jar file."
  3060. :group 'org-latex
  3061. :version "24.1"
  3062. :type '(choice
  3063. (const :tag "None" nil)
  3064. (file :tag "JAR file" :must-match t)))
  3065. (defcustom org-latex-to-mathml-convert-command nil
  3066. "Command to convert LaTeX fragments to MathML.
  3067. Replace format-specifiers in the command as noted below and use
  3068. `shell-command' to convert LaTeX to MathML.
  3069. %j: Executable file in fully expanded form as specified by
  3070. `org-latex-to-mathml-jar-file'.
  3071. %I: Input LaTeX file in fully expanded form
  3072. %o: Output MathML file
  3073. This command is used by `org-create-math-formula'.
  3074. When using MathToWeb as the converter, set this to
  3075. \"java -jar %j -unicode -force -df %o %I\"."
  3076. :group 'org-latex
  3077. :version "24.1"
  3078. :type '(choice
  3079. (const :tag "None" nil)
  3080. (string :tag "\nShell command")))
  3081. (defcustom org-latex-create-formula-image-program 'dvipng
  3082. "Program to convert LaTeX fragments with.
  3083. dvipng Process the LaTeX fragments to dvi file, then convert
  3084. dvi files to png files using dvipng.
  3085. This will also include processing of non-math environments.
  3086. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3087. to convert pdf files to png files"
  3088. :group 'org-latex
  3089. :version "24.1"
  3090. :type '(choice
  3091. (const :tag "dvipng" dvipng)
  3092. (const :tag "imagemagick" imagemagick)))
  3093. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3094. "Path to store latex preview images.
  3095. A relative path here creates many directories relative to the
  3096. processed org files paths. An absolute path puts all preview
  3097. images at the same place."
  3098. :group 'org-latex
  3099. :version "24.3"
  3100. :type 'string)
  3101. (defun org-format-latex-mathml-available-p ()
  3102. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3103. (save-match-data
  3104. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3105. org-latex-to-mathml-convert-command)
  3106. (let ((executable (car (split-string
  3107. org-latex-to-mathml-convert-command))))
  3108. (when (executable-find executable)
  3109. (if (string-match
  3110. "%j" org-latex-to-mathml-convert-command)
  3111. (file-readable-p org-latex-to-mathml-jar-file)
  3112. t))))))
  3113. (defcustom org-format-latex-header "\\documentclass{article}
  3114. \\usepackage[usenames]{color}
  3115. \\usepackage{amsmath}
  3116. \\usepackage[mathscr]{eucal}
  3117. \\pagestyle{empty} % do not remove
  3118. \[PACKAGES]
  3119. \[DEFAULT-PACKAGES]
  3120. % The settings below are copied from fullpage.sty
  3121. \\setlength{\\textwidth}{\\paperwidth}
  3122. \\addtolength{\\textwidth}{-3cm}
  3123. \\setlength{\\oddsidemargin}{1.5cm}
  3124. \\addtolength{\\oddsidemargin}{-2.54cm}
  3125. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3126. \\setlength{\\textheight}{\\paperheight}
  3127. \\addtolength{\\textheight}{-\\headheight}
  3128. \\addtolength{\\textheight}{-\\headsep}
  3129. \\addtolength{\\textheight}{-\\footskip}
  3130. \\addtolength{\\textheight}{-3cm}
  3131. \\setlength{\\topmargin}{1.5cm}
  3132. \\addtolength{\\topmargin}{-2.54cm}"
  3133. "The document header used for processing LaTeX fragments.
  3134. It is imperative that this header make sure that no page number
  3135. appears on the page. The package defined in the variables
  3136. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3137. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3138. will be appended."
  3139. :group 'org-latex
  3140. :type 'string)
  3141. (defvar org-format-latex-header-extra nil)
  3142. (defun org-set-packages-alist (var val)
  3143. "Set the packages alist and make sure it has 3 elements per entry."
  3144. (set var (mapcar (lambda (x)
  3145. (if (and (consp x) (= (length x) 2))
  3146. (list (car x) (nth 1 x) t)
  3147. x))
  3148. val)))
  3149. (defun org-get-packages-alist (var)
  3150. "Get the packages alist and make sure it has 3 elements per entry."
  3151. (mapcar (lambda (x)
  3152. (if (and (consp x) (= (length x) 2))
  3153. (list (car x) (nth 1 x) t)
  3154. x))
  3155. (default-value var)))
  3156. ;; The following variables are defined here because is it also used
  3157. ;; when formatting latex fragments. Originally it was part of the
  3158. ;; LaTeX exporter, which is why the name includes "export".
  3159. (defcustom org-export-latex-default-packages-alist
  3160. '(("AUTO" "inputenc" t)
  3161. ("T1" "fontenc" t)
  3162. ("" "fixltx2e" nil)
  3163. ("" "graphicx" t)
  3164. ("" "longtable" nil)
  3165. ("" "float" nil)
  3166. ("" "wrapfig" nil)
  3167. ("" "soul" t)
  3168. ("" "textcomp" t)
  3169. ("" "marvosym" t)
  3170. ("" "wasysym" t)
  3171. ("" "latexsym" t)
  3172. ("" "amssymb" t)
  3173. ("" "hyperref" nil)
  3174. "\\tolerance=1000"
  3175. )
  3176. "Alist of default packages to be inserted in the header.
  3177. Change this only if one of the packages here causes an incompatibility
  3178. with another package you are using.
  3179. The packages in this list are needed by one part or another of Org-mode
  3180. to function properly.
  3181. - inputenc, fontenc: for basic font and character selection
  3182. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3183. for interpreting the entities in `org-entities'. You can skip some of these
  3184. packages if you don't use any of the symbols in it.
  3185. - graphicx: for including images
  3186. - float, wrapfig: for figure placement
  3187. - longtable: for long tables
  3188. - hyperref: for cross references
  3189. Therefore you should not modify this variable unless you know what you
  3190. are doing. The one reason to change it anyway is that you might be loading
  3191. some other package that conflicts with one of the default packages.
  3192. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3193. If SNIPPET-FLAG is t, the package also needs to be included when
  3194. compiling LaTeX snippets into images for inclusion into HTML."
  3195. :group 'org-export-latex
  3196. :set 'org-set-packages-alist
  3197. :get 'org-get-packages-alist
  3198. :version "24.1"
  3199. :type '(repeat
  3200. (choice
  3201. (list :tag "options/package pair"
  3202. (string :tag "options")
  3203. (string :tag "package")
  3204. (boolean :tag "Snippet"))
  3205. (string :tag "A line of LaTeX"))))
  3206. (defcustom org-export-latex-packages-alist nil
  3207. "Alist of packages to be inserted in every LaTeX header.
  3208. These will be inserted after `org-export-latex-default-packages-alist'.
  3209. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3210. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3211. turning LaTeX snippets into images for inclusion into HTML.
  3212. Make sure that you only list packages here which:
  3213. - you want in every file
  3214. - do not conflict with the default packages in
  3215. `org-export-latex-default-packages-alist'
  3216. - do not conflict with the setup in `org-format-latex-header'."
  3217. :group 'org-export-latex
  3218. :set 'org-set-packages-alist
  3219. :get 'org-get-packages-alist
  3220. :type '(repeat
  3221. (choice
  3222. (list :tag "options/package pair"
  3223. (string :tag "options")
  3224. (string :tag "package")
  3225. (boolean :tag "Snippet"))
  3226. (string :tag "A line of LaTeX"))))
  3227. (defgroup org-appearance nil
  3228. "Settings for Org-mode appearance."
  3229. :tag "Org Appearance"
  3230. :group 'org)
  3231. (defcustom org-level-color-stars-only nil
  3232. "Non-nil means fontify only the stars in each headline.
  3233. When nil, the entire headline is fontified.
  3234. Changing it requires restart of `font-lock-mode' to become effective
  3235. also in regions already fontified."
  3236. :group 'org-appearance
  3237. :type 'boolean)
  3238. (defcustom org-hide-leading-stars nil
  3239. "Non-nil means hide the first N-1 stars in a headline.
  3240. This works by using the face `org-hide' for these stars. This
  3241. face is white for a light background, and black for a dark
  3242. background. You may have to customize the face `org-hide' to
  3243. make this work.
  3244. Changing it requires restart of `font-lock-mode' to become effective
  3245. also in regions already fontified.
  3246. You may also set this on a per-file basis by adding one of the following
  3247. lines to the buffer:
  3248. #+STARTUP: hidestars
  3249. #+STARTUP: showstars"
  3250. :group 'org-appearance
  3251. :type 'boolean)
  3252. (defcustom org-hidden-keywords nil
  3253. "List of symbols corresponding to keywords to be hidden the org buffer.
  3254. For example, a value '(title) for this list will make the document's title
  3255. appear in the buffer without the initial #+TITLE: keyword."
  3256. :group 'org-appearance
  3257. :version "24.1"
  3258. :type '(set (const :tag "#+AUTHOR" author)
  3259. (const :tag "#+DATE" date)
  3260. (const :tag "#+EMAIL" email)
  3261. (const :tag "#+TITLE" title)))
  3262. (defcustom org-custom-properties nil
  3263. "List of properties (as strings) with a special meaning.
  3264. The default use of these custom properties is to let the user
  3265. hide them with `org-toggle-custom-properties-visibility'."
  3266. :group 'org-properties
  3267. :group 'org-appearance
  3268. :version "24.3"
  3269. :type '(repeat (string :tag "Property Name")))
  3270. (defcustom org-fontify-done-headline nil
  3271. "Non-nil means change the face of a headline if it is marked DONE.
  3272. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3273. When this is non-nil, the headline after the keyword is set to the
  3274. `org-headline-done' as an additional indication."
  3275. :group 'org-appearance
  3276. :type 'boolean)
  3277. (defcustom org-fontify-emphasized-text t
  3278. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3279. Changing this variable requires a restart of Emacs to take effect."
  3280. :group 'org-appearance
  3281. :type 'boolean)
  3282. (defcustom org-fontify-whole-heading-line nil
  3283. "Non-nil means fontify the whole line for headings.
  3284. This is useful when setting a background color for the
  3285. org-level-* faces."
  3286. :group 'org-appearance
  3287. :type 'boolean)
  3288. (defcustom org-highlight-latex-fragments-and-specials nil
  3289. "Non-nil means fontify what is treated specially by the exporters."
  3290. :group 'org-appearance
  3291. :type 'boolean)
  3292. (defcustom org-hide-emphasis-markers nil
  3293. "Non-nil mean font-lock should hide the emphasis marker characters."
  3294. :group 'org-appearance
  3295. :type 'boolean)
  3296. (defcustom org-pretty-entities nil
  3297. "Non-nil means show entities as UTF8 characters.
  3298. When nil, the \\name form remains in the buffer."
  3299. :group 'org-appearance
  3300. :version "24.1"
  3301. :type 'boolean)
  3302. (defcustom org-pretty-entities-include-sub-superscripts t
  3303. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3304. :group 'org-appearance
  3305. :version "24.1"
  3306. :type 'boolean)
  3307. (defvar org-emph-re nil
  3308. "Regular expression for matching emphasis.
  3309. After a match, the match groups contain these elements:
  3310. 0 The match of the full regular expression, including the characters
  3311. before and after the proper match
  3312. 1 The character before the proper match, or empty at beginning of line
  3313. 2 The proper match, including the leading and trailing markers
  3314. 3 The leading marker like * or /, indicating the type of highlighting
  3315. 4 The text between the emphasis markers, not including the markers
  3316. 5 The character after the match, empty at the end of a line")
  3317. (defvar org-verbatim-re nil
  3318. "Regular expression for matching verbatim text.")
  3319. (defvar org-emphasis-regexp-components) ; defined just below
  3320. (defvar org-emphasis-alist) ; defined just below
  3321. (defun org-set-emph-re (var val)
  3322. "Set variable and compute the emphasis regular expression."
  3323. (set var val)
  3324. (when (and (boundp 'org-emphasis-alist)
  3325. (boundp 'org-emphasis-regexp-components)
  3326. org-emphasis-alist org-emphasis-regexp-components)
  3327. (let* ((e org-emphasis-regexp-components)
  3328. (pre (car e))
  3329. (post (nth 1 e))
  3330. (border (nth 2 e))
  3331. (body (nth 3 e))
  3332. (nl (nth 4 e))
  3333. (body1 (concat body "*?"))
  3334. (markers (mapconcat 'car org-emphasis-alist ""))
  3335. (vmarkers (mapconcat
  3336. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3337. org-emphasis-alist "")))
  3338. ;; make sure special characters appear at the right position in the class
  3339. (if (string-match "\\^" markers)
  3340. (setq markers (concat (replace-match "" t t markers) "^")))
  3341. (if (string-match "-" markers)
  3342. (setq markers (concat (replace-match "" t t markers) "-")))
  3343. (if (string-match "\\^" vmarkers)
  3344. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3345. (if (string-match "-" vmarkers)
  3346. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3347. (if (> nl 0)
  3348. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3349. (int-to-string nl) "\\}")))
  3350. ;; Make the regexp
  3351. (setq org-emph-re
  3352. (concat "\\([" pre "]\\|^\\)"
  3353. "\\("
  3354. "\\([" markers "]\\)"
  3355. "\\("
  3356. "[^" border "]\\|"
  3357. "[^" border "]"
  3358. body1
  3359. "[^" border "]"
  3360. "\\)"
  3361. "\\3\\)"
  3362. "\\([" post "]\\|$\\)"))
  3363. (setq org-verbatim-re
  3364. (concat "\\([" pre "]\\|^\\)"
  3365. "\\("
  3366. "\\([" vmarkers "]\\)"
  3367. "\\("
  3368. "[^" border "]\\|"
  3369. "[^" border "]"
  3370. body1
  3371. "[^" border "]"
  3372. "\\)"
  3373. "\\3\\)"
  3374. "\\([" post "]\\|$\\)")))))
  3375. (defcustom org-emphasis-regexp-components
  3376. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3377. "Components used to build the regular expression for emphasis.
  3378. This is a list with five entries. Terminology: In an emphasis string
  3379. like \" *strong word* \", we call the initial space PREMATCH, the final
  3380. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3381. and \"trong wor\" is the body. The different components in this variable
  3382. specify what is allowed/forbidden in each part:
  3383. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3384. post Chars allowed as postmatch. End of line will be allowed too.
  3385. border The chars *forbidden* as border characters.
  3386. body-regexp A regexp like \".\" to match a body character. Don't use
  3387. non-shy groups here, and don't allow newline here.
  3388. newline The maximum number of newlines allowed in an emphasis exp.
  3389. Use customize to modify this, or restart Emacs after changing it."
  3390. :group 'org-appearance
  3391. :set 'org-set-emph-re
  3392. :type '(list
  3393. (sexp :tag "Allowed chars in pre ")
  3394. (sexp :tag "Allowed chars in post ")
  3395. (sexp :tag "Forbidden chars in border ")
  3396. (sexp :tag "Regexp for body ")
  3397. (integer :tag "number of newlines allowed")
  3398. (option (boolean :tag "Please ignore this button"))))
  3399. (defcustom org-emphasis-alist
  3400. `(("*" bold "<b>" "</b>")
  3401. ("/" italic "<i>" "</i>")
  3402. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3403. ("=" org-code "<code>" "</code>" verbatim)
  3404. ("~" org-verbatim "<code>" "</code>" verbatim)
  3405. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3406. "<del>" "</del>")
  3407. )
  3408. "Special syntax for emphasized text.
  3409. Text starting and ending with a special character will be emphasized, for
  3410. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3411. characters, the face to be used by font-lock for highlighting in Org-mode
  3412. Emacs buffers, and the HTML tags to be used for this.
  3413. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3414. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3415. Use customize to modify this, or restart Emacs after changing it."
  3416. :group 'org-appearance
  3417. :set 'org-set-emph-re
  3418. :type '(repeat
  3419. (list
  3420. (string :tag "Marker character")
  3421. (choice
  3422. (face :tag "Font-lock-face")
  3423. (plist :tag "Face property list"))
  3424. (string :tag "HTML start tag")
  3425. (string :tag "HTML end tag")
  3426. (option (const verbatim)))))
  3427. (defvar org-syntax-table
  3428. (let ((st (make-syntax-table)))
  3429. (mapc (lambda(c) (modify-syntax-entry
  3430. (string-to-char (car c)) "w p" st))
  3431. org-emphasis-alist)
  3432. st))
  3433. (defvar org-protecting-blocks
  3434. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3435. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3436. This is needed for font-lock setup.")
  3437. ;;; Miscellaneous options
  3438. (defgroup org-completion nil
  3439. "Completion in Org-mode."
  3440. :tag "Org Completion"
  3441. :group 'org)
  3442. (defcustom org-completion-use-ido nil
  3443. "Non-nil means use ido completion wherever possible.
  3444. Note that `ido-mode' must be active for this variable to be relevant.
  3445. If you decide to turn this variable on, you might well want to turn off
  3446. `org-outline-path-complete-in-steps'.
  3447. See also `org-completion-use-iswitchb'."
  3448. :group 'org-completion
  3449. :type 'boolean)
  3450. (defcustom org-completion-use-iswitchb nil
  3451. "Non-nil means use iswitchb completion wherever possible.
  3452. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3453. If you decide to turn this variable on, you might well want to turn off
  3454. `org-outline-path-complete-in-steps'.
  3455. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3456. :group 'org-completion
  3457. :type 'boolean)
  3458. (defcustom org-completion-fallback-command 'hippie-expand
  3459. "The expansion command called by \\[pcomplete] in normal context.
  3460. Normal means, no org-mode-specific context."
  3461. :group 'org-completion
  3462. :type 'function)
  3463. ;;; Functions and variables from their packages
  3464. ;; Declared here to avoid compiler warnings
  3465. ;; XEmacs only
  3466. (defvar outline-mode-menu-heading)
  3467. (defvar outline-mode-menu-show)
  3468. (defvar outline-mode-menu-hide)
  3469. (defvar zmacs-regions) ; XEmacs regions
  3470. ;; Emacs only
  3471. (defvar mark-active)
  3472. ;; Various packages
  3473. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3474. (declare-function calendar-forward-day "cal-move" (arg))
  3475. (declare-function calendar-goto-date "cal-move" (date))
  3476. (declare-function calendar-goto-today "cal-move" ())
  3477. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3478. (defvar calc-embedded-close-formula)
  3479. (defvar calc-embedded-open-formula)
  3480. (declare-function cdlatex-tab "ext:cdlatex" ())
  3481. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3482. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3483. (defvar font-lock-unfontify-region-function)
  3484. (declare-function iswitchb-read-buffer "iswitchb"
  3485. (prompt &optional default require-match start matches-set))
  3486. (defvar iswitchb-temp-buflist)
  3487. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3488. (defvar org-agenda-tags-todo-honor-ignore-options)
  3489. (declare-function org-agenda-skip "org-agenda" ())
  3490. (declare-function
  3491. org-agenda-format-item "org-agenda"
  3492. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3493. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3494. (declare-function org-agenda-change-all-lines "org-agenda"
  3495. (newhead hdmarker &optional fixface just-this))
  3496. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3497. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3498. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3499. (beg end))
  3500. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3501. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3502. "org-agenda" (&optional end))
  3503. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3504. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3505. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3506. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3507. (declare-function org-indent-mode "org-indent" (&optional arg))
  3508. (declare-function parse-time-string "parse-time" (string))
  3509. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3510. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3511. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3512. (defvar remember-data-file)
  3513. (defvar texmathp-why)
  3514. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3515. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3516. (defvar org-latex-regexps)
  3517. ;;; Autoload and prepare some org modules
  3518. ;; Some table stuff that needs to be defined here, because it is used
  3519. ;; by the functions setting up org-mode or checking for table context.
  3520. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3521. "Detect an org-type or table-type table.")
  3522. (defconst org-table-line-regexp "^[ \t]*|"
  3523. "Detect an org-type table line.")
  3524. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3525. "Detect an org-type table line.")
  3526. (defconst org-table-hline-regexp "^[ \t]*|-"
  3527. "Detect an org-type table hline.")
  3528. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3529. "Detect a table-type table hline.")
  3530. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3531. "Detect the first line outside a table when searching from within it.
  3532. This works for both table types.")
  3533. ;; Autoload the functions in org-table.el that are needed by functions here.
  3534. (eval-and-compile
  3535. (org-autoload "org-table"
  3536. '(org-table-begin org-table-blank-field org-table-end)))
  3537. ;;;###autoload
  3538. (defun turn-on-orgtbl ()
  3539. "Unconditionally turn on `orgtbl-mode'."
  3540. (require 'org-table)
  3541. (orgtbl-mode 1))
  3542. (defun org-at-table-p (&optional table-type)
  3543. "Return t if the cursor is inside an org-type table.
  3544. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3545. (if org-enable-table-editor
  3546. (save-excursion
  3547. (beginning-of-line 1)
  3548. (looking-at (if table-type org-table-any-line-regexp
  3549. org-table-line-regexp)))
  3550. nil))
  3551. (defsubst org-table-p () (org-at-table-p))
  3552. (defun org-at-table.el-p ()
  3553. "Return t if and only if we are at a table.el table."
  3554. (and (org-at-table-p 'any)
  3555. (save-excursion
  3556. (goto-char (org-table-begin 'any))
  3557. (looking-at org-table1-hline-regexp))))
  3558. (defun org-table-recognize-table.el ()
  3559. "If there is a table.el table nearby, recognize it and move into it."
  3560. (if org-table-tab-recognizes-table.el
  3561. (if (org-at-table.el-p)
  3562. (progn
  3563. (beginning-of-line 1)
  3564. (if (looking-at org-table-dataline-regexp)
  3565. nil
  3566. (if (looking-at org-table1-hline-regexp)
  3567. (progn
  3568. (beginning-of-line 2)
  3569. (if (looking-at org-table-any-border-regexp)
  3570. (beginning-of-line -1)))))
  3571. (if (re-search-forward "|" (org-table-end t) t)
  3572. (progn
  3573. (require 'table)
  3574. (if (table--at-cell-p (point))
  3575. t
  3576. (message "recognizing table.el table...")
  3577. (table-recognize-table)
  3578. (message "recognizing table.el table...done")))
  3579. (error "This should not happen"))
  3580. t)
  3581. nil)
  3582. nil))
  3583. (defun org-at-table-hline-p ()
  3584. "Return t if the cursor is inside a hline in a table."
  3585. (if org-enable-table-editor
  3586. (save-excursion
  3587. (beginning-of-line 1)
  3588. (looking-at org-table-hline-regexp))
  3589. nil))
  3590. (defvar org-table-clean-did-remove-column nil)
  3591. (defun org-table-map-tables (function &optional quietly)
  3592. "Apply FUNCTION to the start of all tables in the buffer."
  3593. (save-excursion
  3594. (save-restriction
  3595. (widen)
  3596. (goto-char (point-min))
  3597. (while (re-search-forward org-table-any-line-regexp nil t)
  3598. (unless quietly
  3599. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3600. (beginning-of-line 1)
  3601. (when (and (looking-at org-table-line-regexp)
  3602. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3603. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3604. (save-excursion (funcall function))
  3605. (or (looking-at org-table-line-regexp)
  3606. (forward-char 1)))
  3607. (re-search-forward org-table-any-border-regexp nil 1))))
  3608. (unless quietly (message "Mapping tables: done")))
  3609. ;; Declare and autoload functions from org-exp.el & Co
  3610. (declare-function org-default-export-plist "org-exp")
  3611. (declare-function org-infile-export-plist "org-exp")
  3612. (declare-function org-get-current-options "org-exp")
  3613. ;; Declare and autoload functions from org-agenda.el
  3614. (eval-and-compile
  3615. (org-autoload "org-agenda"
  3616. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3617. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3618. (declare-function org-clock-update-mode-line "org-clock" ())
  3619. (declare-function org-resolve-clocks "org-clock"
  3620. (&optional also-non-dangling-p prompt last-valid))
  3621. (defvar org-clock-start-time)
  3622. (defvar org-clock-marker (make-marker)
  3623. "Marker recording the last clock-in.")
  3624. (defvar org-clock-hd-marker (make-marker)
  3625. "Marker recording the last clock-in, but the headline position.")
  3626. (defvar org-clock-heading ""
  3627. "The heading of the current clock entry.")
  3628. (defun org-clock-is-active ()
  3629. "Return non-nil if clock is currently running.
  3630. The return value is actually the clock marker."
  3631. (marker-buffer org-clock-marker))
  3632. (eval-and-compile
  3633. (org-autoload "org-clock" '(org-clock-remove-overlays
  3634. org-clock-update-time-maybe
  3635. org-clocktable-shift)))
  3636. (defun org-check-running-clock ()
  3637. "Check if the current buffer contains the running clock.
  3638. If yes, offer to stop it and to save the buffer with the changes."
  3639. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3640. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3641. (buffer-name))))
  3642. (org-clock-out)
  3643. (when (y-or-n-p "Save changed buffer?")
  3644. (save-buffer))))
  3645. (defun org-clocktable-try-shift (dir n)
  3646. "Check if this line starts a clock table, if yes, shift the time block."
  3647. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3648. (org-clocktable-shift dir n)))
  3649. ;;;###autoload
  3650. (defun org-clock-persistence-insinuate ()
  3651. "Set up hooks for clock persistence."
  3652. (require 'org-clock)
  3653. (add-hook 'org-mode-hook 'org-clock-load)
  3654. (add-hook 'kill-emacs-hook 'org-clock-save))
  3655. ;; Define the variable already here, to make sure we have it.
  3656. (defvar org-indent-mode nil
  3657. "Non-nil if Org-Indent mode is enabled.
  3658. Use the command `org-indent-mode' to change this variable.")
  3659. ;; Autoload archiving code
  3660. ;; The stuff that is needed for cycling and tags has to be defined here.
  3661. (defgroup org-archive nil
  3662. "Options concerning archiving in Org-mode."
  3663. :tag "Org Archive"
  3664. :group 'org-structure)
  3665. (defcustom org-archive-location "%s_archive::"
  3666. "The location where subtrees should be archived.
  3667. The value of this variable is a string, consisting of two parts,
  3668. separated by a double-colon. The first part is a filename and
  3669. the second part is a headline.
  3670. When the filename is omitted, archiving happens in the same file.
  3671. %s in the filename will be replaced by the current file
  3672. name (without the directory part). Archiving to a different file
  3673. is useful to keep archived entries from contributing to the
  3674. Org-mode Agenda.
  3675. The archived entries will be filed as subtrees of the specified
  3676. headline. When the headline is omitted, the subtrees are simply
  3677. filed away at the end of the file, as top-level entries. Also in
  3678. the heading you can use %s to represent the file name, this can be
  3679. useful when using the same archive for a number of different files.
  3680. Here are a few examples:
  3681. \"%s_archive::\"
  3682. If the current file is Projects.org, archive in file
  3683. Projects.org_archive, as top-level trees. This is the default.
  3684. \"::* Archived Tasks\"
  3685. Archive in the current file, under the top-level headline
  3686. \"* Archived Tasks\".
  3687. \"~/org/archive.org::\"
  3688. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3689. \"~/org/archive.org::* From %s\"
  3690. Archive in file ~/org/archive.org (absolute path), under headlines
  3691. \"From FILENAME\" where file name is the current file name.
  3692. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3693. The \"datetree/\" string is special, signifying to archive
  3694. items to the datetree. Items are placed in either the CLOSED
  3695. date of the item, or the current date if there is no CLOSED date.
  3696. The heading will be a subentry to the current date. There doesn't
  3697. need to be a heading, but there always needs to be a slash after
  3698. datetree. For example, to store archived items directly in the
  3699. datetree, use \"~/org/datetree.org::datetree/\".
  3700. \"basement::** Finished Tasks\"
  3701. Archive in file ./basement (relative path), as level 3 trees
  3702. below the level 2 heading \"** Finished Tasks\".
  3703. You may set this option on a per-file basis by adding to the buffer a
  3704. line like
  3705. #+ARCHIVE: basement::** Finished Tasks
  3706. You may also define it locally for a subtree by setting an ARCHIVE property
  3707. in the entry. If such a property is found in an entry, or anywhere up
  3708. the hierarchy, it will be used."
  3709. :group 'org-archive
  3710. :type 'string)
  3711. (defcustom org-archive-tag "ARCHIVE"
  3712. "The tag that marks a subtree as archived.
  3713. An archived subtree does not open during visibility cycling, and does
  3714. not contribute to the agenda listings.
  3715. After changing this, font-lock must be restarted in the relevant buffers to
  3716. get the proper fontification."
  3717. :group 'org-archive
  3718. :group 'org-keywords
  3719. :type 'string)
  3720. (defcustom org-agenda-skip-archived-trees t
  3721. "Non-nil means the agenda will skip any items located in archived trees.
  3722. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3723. variable is no longer recommended, you should leave it at the value t.
  3724. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3725. :group 'org-archive
  3726. :group 'org-agenda-skip
  3727. :type 'boolean)
  3728. (defcustom org-columns-skip-archived-trees t
  3729. "Non-nil means ignore archived trees when creating column view."
  3730. :group 'org-archive
  3731. :group 'org-properties
  3732. :type 'boolean)
  3733. (defcustom org-cycle-open-archived-trees nil
  3734. "Non-nil means `org-cycle' will open archived trees.
  3735. An archived tree is a tree marked with the tag ARCHIVE.
  3736. When nil, archived trees will stay folded. You can still open them with
  3737. normal outline commands like `show-all', but not with the cycling commands."
  3738. :group 'org-archive
  3739. :group 'org-cycle
  3740. :type 'boolean)
  3741. (defcustom org-sparse-tree-open-archived-trees nil
  3742. "Non-nil means sparse tree construction shows matches in archived trees.
  3743. When nil, matches in these trees are highlighted, but the trees are kept in
  3744. collapsed state."
  3745. :group 'org-archive
  3746. :group 'org-sparse-trees
  3747. :type 'boolean)
  3748. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3749. "The default date type when building a sparse tree.
  3750. When this is nil, a date is a scheduled or a deadline timestamp.
  3751. Otherwise, these types are allowed:
  3752. all: all timestamps
  3753. active: only active timestamps (<...>)
  3754. inactive: only inactive timestamps (<...)
  3755. scheduled: only scheduled timestamps
  3756. deadline: only deadline timestamps"
  3757. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3758. (const :tag "All timestamps" all)
  3759. (const :tag "Only active timestamps" active)
  3760. (const :tag "Only inactive timestamps" inactive)
  3761. (const :tag "Only scheduled timestamps" scheduled)
  3762. (const :tag "Only deadline timestamps" deadline))
  3763. :version "24.3"
  3764. :group 'org-sparse-trees)
  3765. (defun org-cycle-hide-archived-subtrees (state)
  3766. "Re-hide all archived subtrees after a visibility state change."
  3767. (when (and (not org-cycle-open-archived-trees)
  3768. (not (memq state '(overview folded))))
  3769. (save-excursion
  3770. (let* ((globalp (memq state '(contents all)))
  3771. (beg (if globalp (point-min) (point)))
  3772. (end (if globalp (point-max) (org-end-of-subtree t))))
  3773. (org-hide-archived-subtrees beg end)
  3774. (goto-char beg)
  3775. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3776. (message "%s" (substitute-command-keys
  3777. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3778. (defun org-force-cycle-archived ()
  3779. "Cycle subtree even if it is archived."
  3780. (interactive)
  3781. (setq this-command 'org-cycle)
  3782. (let ((org-cycle-open-archived-trees t))
  3783. (call-interactively 'org-cycle)))
  3784. (defun org-hide-archived-subtrees (beg end)
  3785. "Re-hide all archived subtrees after a visibility state change."
  3786. (save-excursion
  3787. (let* ((re (concat ":" org-archive-tag ":")))
  3788. (goto-char beg)
  3789. (while (re-search-forward re end t)
  3790. (when (org-at-heading-p)
  3791. (org-flag-subtree t)
  3792. (org-end-of-subtree t))))))
  3793. (declare-function outline-end-of-heading "outline" ())
  3794. (declare-function outline-flag-region "outline" (from to flag))
  3795. (defun org-flag-subtree (flag)
  3796. (save-excursion
  3797. (org-back-to-heading t)
  3798. (outline-end-of-heading)
  3799. (outline-flag-region (point)
  3800. (progn (org-end-of-subtree t) (point))
  3801. flag)))
  3802. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3803. (eval-and-compile
  3804. (org-autoload "org-archive"
  3805. '(org-add-archive-files)))
  3806. ;; Autoload Column View Code
  3807. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3808. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3809. (declare-function org-columns-compute "org-colview" (property))
  3810. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3811. '(org-columns-number-to-string
  3812. org-columns-get-format-and-top-level
  3813. org-columns-compute
  3814. org-columns-remove-overlays))
  3815. ;; Autoload ID code
  3816. (declare-function org-id-store-link "org-id")
  3817. (declare-function org-id-locations-load "org-id")
  3818. (declare-function org-id-locations-save "org-id")
  3819. (defvar org-id-track-globally)
  3820. (org-autoload "org-id"
  3821. '(org-id-new
  3822. org-id-copy
  3823. org-id-get-with-outline-path-completion
  3824. org-id-get-with-outline-drilling))
  3825. ;;; Variables for pre-computed regular expressions, all buffer local
  3826. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3827. "Matches first line of a hidden block.")
  3828. (make-variable-buffer-local 'org-drawer-regexp)
  3829. (defvar org-todo-regexp nil
  3830. "Matches any of the TODO state keywords.")
  3831. (make-variable-buffer-local 'org-todo-regexp)
  3832. (defvar org-not-done-regexp nil
  3833. "Matches any of the TODO state keywords except the last one.")
  3834. (make-variable-buffer-local 'org-not-done-regexp)
  3835. (defvar org-not-done-heading-regexp nil
  3836. "Matches a TODO headline that is not done.")
  3837. (make-variable-buffer-local 'org-not-done-regexp)
  3838. (defvar org-todo-line-regexp nil
  3839. "Matches a headline and puts TODO state into group 2 if present.")
  3840. (make-variable-buffer-local 'org-todo-line-regexp)
  3841. (defvar org-complex-heading-regexp nil
  3842. "Matches a headline and puts everything into groups:
  3843. group 1: the stars
  3844. group 2: The todo keyword, maybe
  3845. group 3: Priority cookie
  3846. group 4: True headline
  3847. group 5: Tags")
  3848. (make-variable-buffer-local 'org-complex-heading-regexp)
  3849. (defvar org-complex-heading-regexp-format nil
  3850. "Printf format to make regexp to match an exact headline.
  3851. This regexp will match the headline of any node which has the
  3852. exact headline text that is put into the format, but may have any
  3853. TODO state, priority and tags.")
  3854. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3855. (defvar org-todo-line-tags-regexp nil
  3856. "Matches a headline and puts TODO state into group 2 if present.
  3857. Also put tags into group 4 if tags are present.")
  3858. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3859. (defvar org-ds-keyword-length 12
  3860. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3861. (make-variable-buffer-local 'org-ds-keyword-length)
  3862. (defvar org-deadline-regexp nil
  3863. "Matches the DEADLINE keyword.")
  3864. (make-variable-buffer-local 'org-deadline-regexp)
  3865. (defvar org-deadline-time-regexp nil
  3866. "Matches the DEADLINE keyword together with a time stamp.")
  3867. (make-variable-buffer-local 'org-deadline-time-regexp)
  3868. (defvar org-deadline-line-regexp nil
  3869. "Matches the DEADLINE keyword and the rest of the line.")
  3870. (make-variable-buffer-local 'org-deadline-line-regexp)
  3871. (defvar org-scheduled-regexp nil
  3872. "Matches the SCHEDULED keyword.")
  3873. (make-variable-buffer-local 'org-scheduled-regexp)
  3874. (defvar org-scheduled-time-regexp nil
  3875. "Matches the SCHEDULED keyword together with a time stamp.")
  3876. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3877. (defvar org-closed-time-regexp nil
  3878. "Matches the CLOSED keyword together with a time stamp.")
  3879. (make-variable-buffer-local 'org-closed-time-regexp)
  3880. (defvar org-keyword-time-regexp nil
  3881. "Matches any of the 4 keywords, together with the time stamp.")
  3882. (make-variable-buffer-local 'org-keyword-time-regexp)
  3883. (defvar org-keyword-time-not-clock-regexp nil
  3884. "Matches any of the 3 keywords, together with the time stamp.")
  3885. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3886. (defvar org-maybe-keyword-time-regexp nil
  3887. "Matches a timestamp, possibly preceded by a keyword.")
  3888. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3889. (defvar org-all-time-keywords nil
  3890. "List of time keywords.")
  3891. (make-variable-buffer-local 'org-all-time-keywords)
  3892. (defconst org-plain-time-of-day-regexp
  3893. (concat
  3894. "\\(\\<[012]?[0-9]"
  3895. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3896. "\\(--?"
  3897. "\\(\\<[012]?[0-9]"
  3898. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3899. "\\)?")
  3900. "Regular expression to match a plain time or time range.
  3901. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3902. groups carry important information:
  3903. 0 the full match
  3904. 1 the first time, range or not
  3905. 8 the second time, if it is a range.")
  3906. (defconst org-plain-time-extension-regexp
  3907. (concat
  3908. "\\(\\<[012]?[0-9]"
  3909. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3910. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3911. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3912. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3913. groups carry important information:
  3914. 0 the full match
  3915. 7 hours of duration
  3916. 9 minutes of duration")
  3917. (defconst org-stamp-time-of-day-regexp
  3918. (concat
  3919. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3920. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3921. "\\(--?"
  3922. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3923. "Regular expression to match a timestamp time or time range.
  3924. After a match, the following groups carry important information:
  3925. 0 the full match
  3926. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3927. 2 the first time, range or not
  3928. 4 the second time, if it is a range.")
  3929. (defconst org-startup-options
  3930. '(("fold" org-startup-folded t)
  3931. ("overview" org-startup-folded t)
  3932. ("nofold" org-startup-folded nil)
  3933. ("showall" org-startup-folded nil)
  3934. ("showeverything" org-startup-folded showeverything)
  3935. ("content" org-startup-folded content)
  3936. ("indent" org-startup-indented t)
  3937. ("noindent" org-startup-indented nil)
  3938. ("hidestars" org-hide-leading-stars t)
  3939. ("showstars" org-hide-leading-stars nil)
  3940. ("odd" org-odd-levels-only t)
  3941. ("oddeven" org-odd-levels-only nil)
  3942. ("align" org-startup-align-all-tables t)
  3943. ("noalign" org-startup-align-all-tables nil)
  3944. ("inlineimages" org-startup-with-inline-images t)
  3945. ("noinlineimages" org-startup-with-inline-images nil)
  3946. ("customtime" org-display-custom-times t)
  3947. ("logdone" org-log-done time)
  3948. ("lognotedone" org-log-done note)
  3949. ("nologdone" org-log-done nil)
  3950. ("lognoteclock-out" org-log-note-clock-out t)
  3951. ("nolognoteclock-out" org-log-note-clock-out nil)
  3952. ("logrepeat" org-log-repeat state)
  3953. ("lognoterepeat" org-log-repeat note)
  3954. ("logdrawer" org-log-into-drawer t)
  3955. ("nologdrawer" org-log-into-drawer nil)
  3956. ("logstatesreversed" org-log-states-order-reversed t)
  3957. ("nologstatesreversed" org-log-states-order-reversed nil)
  3958. ("nologrepeat" org-log-repeat nil)
  3959. ("logreschedule" org-log-reschedule time)
  3960. ("lognotereschedule" org-log-reschedule note)
  3961. ("nologreschedule" org-log-reschedule nil)
  3962. ("logredeadline" org-log-redeadline time)
  3963. ("lognoteredeadline" org-log-redeadline note)
  3964. ("nologredeadline" org-log-redeadline nil)
  3965. ("logrefile" org-log-refile time)
  3966. ("lognoterefile" org-log-refile note)
  3967. ("nologrefile" org-log-refile nil)
  3968. ("fninline" org-footnote-define-inline t)
  3969. ("nofninline" org-footnote-define-inline nil)
  3970. ("fnlocal" org-footnote-section nil)
  3971. ("fnauto" org-footnote-auto-label t)
  3972. ("fnprompt" org-footnote-auto-label nil)
  3973. ("fnconfirm" org-footnote-auto-label confirm)
  3974. ("fnplain" org-footnote-auto-label plain)
  3975. ("fnadjust" org-footnote-auto-adjust t)
  3976. ("nofnadjust" org-footnote-auto-adjust nil)
  3977. ("constcgs" constants-unit-system cgs)
  3978. ("constSI" constants-unit-system SI)
  3979. ("noptag" org-tag-persistent-alist nil)
  3980. ("hideblocks" org-hide-block-startup t)
  3981. ("nohideblocks" org-hide-block-startup nil)
  3982. ("beamer" org-startup-with-beamer-mode t)
  3983. ("entitiespretty" org-pretty-entities t)
  3984. ("entitiesplain" org-pretty-entities nil))
  3985. "Variable associated with STARTUP options for org-mode.
  3986. Each element is a list of three items: the startup options (as written
  3987. in the #+STARTUP line), the corresponding variable, and the value to set
  3988. this variable to if the option is found. An optional forth element PUSH
  3989. means to push this value onto the list in the variable.")
  3990. (defun org-update-property-plist (key val props)
  3991. "Update PROPS with KEY and VAL."
  3992. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3993. (key (if appending (substring key 0 (- (length key) 1)) key))
  3994. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3995. (previous (cdr (assoc key props))))
  3996. (if appending
  3997. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3998. (cons (cons key val) remainder))))
  3999. (defconst org-block-regexp
  4000. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4001. "Regular expression for hiding blocks.")
  4002. (defconst org-heading-keyword-regexp-format
  4003. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4004. "Printf format for a regexp matching an headline with some keyword.
  4005. This regexp will match the headline of any node which has the
  4006. exact keyword that is put into the format. The keyword isn't in
  4007. any group by default, but the stars and the body are.")
  4008. (defconst org-heading-keyword-maybe-regexp-format
  4009. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4010. "Printf format for a regexp matching an headline, possibly with some keyword.
  4011. This regexp can match any headline with the specified keyword, or
  4012. without a keyword. The keyword isn't in any group by default,
  4013. but the stars and the body are.")
  4014. (defun org-set-regexps-and-options ()
  4015. "Precompute regular expressions for current buffer."
  4016. (when (derived-mode-p 'org-mode)
  4017. (org-set-local 'org-todo-kwd-alist nil)
  4018. (org-set-local 'org-todo-key-alist nil)
  4019. (org-set-local 'org-todo-key-trigger nil)
  4020. (org-set-local 'org-todo-keywords-1 nil)
  4021. (org-set-local 'org-done-keywords nil)
  4022. (org-set-local 'org-todo-heads nil)
  4023. (org-set-local 'org-todo-sets nil)
  4024. (org-set-local 'org-todo-log-states nil)
  4025. (org-set-local 'org-file-properties nil)
  4026. (org-set-local 'org-file-tags nil)
  4027. (let ((re (org-make-options-regexp
  4028. '("CATEGORY" "TODO" "COLUMNS"
  4029. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4030. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4031. "OPTIONS")
  4032. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4033. (splitre "[ \t]+")
  4034. (scripts org-use-sub-superscripts)
  4035. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4036. tail sep kws1 prio props ftags drawers beamer-p
  4037. ext-setup-or-nil setup-contents (start 0))
  4038. (save-excursion
  4039. (save-restriction
  4040. (widen)
  4041. (goto-char (point-min))
  4042. (while (or (and ext-setup-or-nil
  4043. (string-match re ext-setup-or-nil start)
  4044. (setq start (match-end 0)))
  4045. (and (setq ext-setup-or-nil nil start 0)
  4046. (re-search-forward re nil t)))
  4047. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4048. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4049. (if (stringp value) (setq value (org-trim value)))
  4050. (cond
  4051. ((equal key "CATEGORY")
  4052. (setq cat value))
  4053. ((member key '("SEQ_TODO" "TODO"))
  4054. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4055. ((equal key "TYP_TODO")
  4056. (push (cons 'type (org-split-string value splitre)) kwds))
  4057. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4058. ;; general TODO-like setup
  4059. (push (cons (intern (downcase (match-string 1 key)))
  4060. (org-split-string value splitre)) kwds))
  4061. ((equal key "TAGS")
  4062. (setq tags (append tags (if tags '("\\n") nil)
  4063. (org-split-string value splitre))))
  4064. ((equal key "COLUMNS")
  4065. (org-set-local 'org-columns-default-format value))
  4066. ((equal key "LINK")
  4067. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4068. (push (cons (match-string 1 value)
  4069. (org-trim (match-string 2 value)))
  4070. links)))
  4071. ((equal key "PRIORITIES")
  4072. (setq prio (org-split-string value " +")))
  4073. ((equal key "PROPERTY")
  4074. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4075. (setq props (org-update-property-plist (match-string 1 value)
  4076. (match-string 2 value)
  4077. props))))
  4078. ((equal key "FILETAGS")
  4079. (when (string-match "\\S-" value)
  4080. (setq ftags
  4081. (append
  4082. ftags
  4083. (apply 'append
  4084. (mapcar (lambda (x) (org-split-string x ":"))
  4085. (org-split-string value)))))))
  4086. ((equal key "DRAWERS")
  4087. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4088. ((equal key "CONSTANTS")
  4089. (setq const (append const (org-split-string value splitre))))
  4090. ((equal key "STARTUP")
  4091. (let ((opts (org-split-string value splitre))
  4092. l var val)
  4093. (while (setq l (pop opts))
  4094. (when (setq l (assoc l org-startup-options))
  4095. (setq var (nth 1 l) val (nth 2 l))
  4096. (if (not (nth 3 l))
  4097. (set (make-local-variable var) val)
  4098. (if (not (listp (symbol-value var)))
  4099. (set (make-local-variable var) nil))
  4100. (set (make-local-variable var) (symbol-value var))
  4101. (add-to-list var val))))))
  4102. ((equal key "ARCHIVE")
  4103. (setq arch value)
  4104. (remove-text-properties 0 (length arch)
  4105. '(face t fontified t) arch))
  4106. ((equal key "LATEX_CLASS")
  4107. (setq beamer-p (equal value "beamer")))
  4108. ((equal key "OPTIONS")
  4109. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4110. (setq scripts (read (match-string 2 value)))))
  4111. ((equal key "SETUPFILE")
  4112. (setq setup-contents (org-file-contents
  4113. (expand-file-name
  4114. (org-remove-double-quotes value))
  4115. 'noerror))
  4116. (if (not ext-setup-or-nil)
  4117. (setq ext-setup-or-nil setup-contents start 0)
  4118. (setq ext-setup-or-nil
  4119. (concat (substring ext-setup-or-nil 0 start)
  4120. "\n" setup-contents "\n"
  4121. (substring ext-setup-or-nil start)))))))
  4122. ;; search for property blocks
  4123. (goto-char (point-min))
  4124. (while (re-search-forward org-block-regexp nil t)
  4125. (when (equal "PROPERTY" (upcase (match-string 1)))
  4126. (setq value (replace-regexp-in-string
  4127. "[\n\r]" " " (match-string 4)))
  4128. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4129. (setq props (org-update-property-plist (match-string 1 value)
  4130. (match-string 2 value)
  4131. props)))))))
  4132. (org-set-local 'org-use-sub-superscripts scripts)
  4133. (when cat
  4134. (org-set-local 'org-category (intern cat))
  4135. (push (cons "CATEGORY" cat) props))
  4136. (when prio
  4137. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4138. (setq prio (mapcar 'string-to-char prio))
  4139. (org-set-local 'org-highest-priority (nth 0 prio))
  4140. (org-set-local 'org-lowest-priority (nth 1 prio))
  4141. (org-set-local 'org-default-priority (nth 2 prio)))
  4142. (and props (org-set-local 'org-file-properties (nreverse props)))
  4143. (and ftags (org-set-local 'org-file-tags
  4144. (mapcar 'org-add-prop-inherited ftags)))
  4145. (and drawers (org-set-local 'org-drawers drawers))
  4146. (and arch (org-set-local 'org-archive-location arch))
  4147. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4148. ;; Process the TODO keywords
  4149. (unless kwds
  4150. ;; Use the global values as if they had been given locally.
  4151. (setq kwds (default-value 'org-todo-keywords))
  4152. (if (stringp (car kwds))
  4153. (setq kwds (list (cons org-todo-interpretation
  4154. (default-value 'org-todo-keywords)))))
  4155. (setq kwds (reverse kwds)))
  4156. (setq kwds (nreverse kwds))
  4157. (let (inter kws kw)
  4158. (while (setq kws (pop kwds))
  4159. (let ((kws (or
  4160. (run-hook-with-args-until-success
  4161. 'org-todo-setup-filter-hook kws)
  4162. kws)))
  4163. (setq inter (pop kws) sep (member "|" kws)
  4164. kws0 (delete "|" (copy-sequence kws))
  4165. kwsa nil
  4166. kws1 (mapcar
  4167. (lambda (x)
  4168. ;; 1 2
  4169. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4170. (progn
  4171. (setq kw (match-string 1 x)
  4172. key (and (match-end 2) (match-string 2 x))
  4173. log (org-extract-log-state-settings x))
  4174. (push (cons kw (and key (string-to-char key))) kwsa)
  4175. (and log (push log org-todo-log-states))
  4176. kw)
  4177. (error "Invalid TODO keyword %s" x)))
  4178. kws0)
  4179. kwsa (if kwsa (append '((:startgroup))
  4180. (nreverse kwsa)
  4181. '((:endgroup))))
  4182. hw (car kws1)
  4183. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4184. tail (list inter hw (car dws) (org-last dws))))
  4185. (add-to-list 'org-todo-heads hw 'append)
  4186. (push kws1 org-todo-sets)
  4187. (setq org-done-keywords (append org-done-keywords dws nil))
  4188. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4189. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4190. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4191. (setq org-todo-sets (nreverse org-todo-sets)
  4192. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4193. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4194. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4195. ;; Process the constants
  4196. (when const
  4197. (let (e cst)
  4198. (while (setq e (pop const))
  4199. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4200. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4201. (setq org-table-formula-constants-local cst)))
  4202. ;; Process the tags.
  4203. (when tags
  4204. (let (e tgs)
  4205. (while (setq e (pop tags))
  4206. (cond
  4207. ((equal e "{") (push '(:startgroup) tgs))
  4208. ((equal e "}") (push '(:endgroup) tgs))
  4209. ((equal e "\\n") (push '(:newline) tgs))
  4210. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4211. (push (cons (match-string 1 e)
  4212. (string-to-char (match-string 2 e)))
  4213. tgs))
  4214. (t (push (list e) tgs))))
  4215. (org-set-local 'org-tag-alist nil)
  4216. (while (setq e (pop tgs))
  4217. (or (and (stringp (car e))
  4218. (assoc (car e) org-tag-alist))
  4219. (push e org-tag-alist)))))
  4220. ;; Compute the regular expressions and other local variables.
  4221. ;; Using `org-outline-regexp-bol' would complicate them much,
  4222. ;; because of the fixed white space at the end of that string.
  4223. (if (not org-done-keywords)
  4224. (setq org-done-keywords (and org-todo-keywords-1
  4225. (list (org-last org-todo-keywords-1)))))
  4226. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4227. (length org-scheduled-string)
  4228. (length org-clock-string)
  4229. (length org-closed-string)))
  4230. org-drawer-regexp
  4231. (concat "^[ \t]*:\\("
  4232. (mapconcat 'regexp-quote org-drawers "\\|")
  4233. "\\):[ \t]*$")
  4234. org-not-done-keywords
  4235. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4236. org-todo-regexp
  4237. (concat "\\("
  4238. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4239. "\\)")
  4240. org-not-done-regexp
  4241. (concat "\\("
  4242. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4243. "\\)")
  4244. org-not-done-heading-regexp
  4245. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4246. org-todo-line-regexp
  4247. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4248. org-complex-heading-regexp
  4249. (concat "^\\(\\*+\\)"
  4250. "\\(?: +" org-todo-regexp "\\)?"
  4251. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4252. "\\(?: +\\(.*?\\)\\)??"
  4253. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4254. "[ \t]*$")
  4255. org-complex-heading-regexp-format
  4256. (concat "^\\(\\*+\\)"
  4257. "\\(?: +" org-todo-regexp "\\)?"
  4258. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4259. "\\(?: +"
  4260. ;; Stats cookies can be stuck to body.
  4261. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4262. "\\(%s\\)"
  4263. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4264. "\\)"
  4265. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4266. "[ \t]*$")
  4267. org-todo-line-tags-regexp
  4268. (concat "^\\(\\*+\\)"
  4269. "\\(?: +" org-todo-regexp "\\)?"
  4270. "\\(?: +\\(.*?\\)\\)??"
  4271. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4272. "[ \t]*$")
  4273. org-deadline-regexp (concat "\\<" org-deadline-string)
  4274. org-deadline-time-regexp
  4275. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4276. org-deadline-line-regexp
  4277. (concat "\\<\\(" org-deadline-string "\\).*")
  4278. org-scheduled-regexp
  4279. (concat "\\<" org-scheduled-string)
  4280. org-scheduled-time-regexp
  4281. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4282. org-closed-time-regexp
  4283. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4284. org-keyword-time-regexp
  4285. (concat "\\<\\(" org-scheduled-string
  4286. "\\|" org-deadline-string
  4287. "\\|" org-closed-string
  4288. "\\|" org-clock-string "\\)"
  4289. " *[[<]\\([^]>]+\\)[]>]")
  4290. org-keyword-time-not-clock-regexp
  4291. (concat "\\<\\(" org-scheduled-string
  4292. "\\|" org-deadline-string
  4293. "\\|" org-closed-string
  4294. "\\)"
  4295. " *[[<]\\([^]>]+\\)[]>]")
  4296. org-maybe-keyword-time-regexp
  4297. (concat "\\(\\<\\(" org-scheduled-string
  4298. "\\|" org-deadline-string
  4299. "\\|" org-closed-string
  4300. "\\|" org-clock-string "\\)\\)?"
  4301. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4302. org-all-time-keywords
  4303. (mapcar (lambda (w) (substring w 0 -1))
  4304. (list org-scheduled-string org-deadline-string
  4305. org-clock-string org-closed-string))
  4306. )
  4307. (org-compute-latex-and-specials-regexp)
  4308. (org-set-font-lock-defaults))))
  4309. (defun org-file-contents (file &optional noerror)
  4310. "Return the contents of FILE, as a string."
  4311. (if (or (not file)
  4312. (not (file-readable-p file)))
  4313. (if noerror
  4314. (progn
  4315. (message "Cannot read file \"%s\"" file)
  4316. (ding) (sit-for 2)
  4317. "")
  4318. (error "Cannot read file \"%s\"" file))
  4319. (with-temp-buffer
  4320. (insert-file-contents file)
  4321. (buffer-string))))
  4322. (defun org-extract-log-state-settings (x)
  4323. "Extract the log state setting from a TODO keyword string.
  4324. This will extract info from a string like \"WAIT(w@/!)\"."
  4325. (let (kw key log1 log2)
  4326. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4327. (setq kw (match-string 1 x)
  4328. key (and (match-end 2) (match-string 2 x))
  4329. log1 (and (match-end 3) (match-string 3 x))
  4330. log2 (and (match-end 4) (match-string 4 x)))
  4331. (and (or log1 log2)
  4332. (list kw
  4333. (and log1 (if (equal log1 "!") 'time 'note))
  4334. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4335. (defun org-remove-keyword-keys (list)
  4336. "Remove a pair of parenthesis at the end of each string in LIST."
  4337. (mapcar (lambda (x)
  4338. (if (string-match "(.*)$" x)
  4339. (substring x 0 (match-beginning 0))
  4340. x))
  4341. list))
  4342. (defun org-assign-fast-keys (alist)
  4343. "Assign fast keys to a keyword-key alist.
  4344. Respect keys that are already there."
  4345. (let (new e (alt ?0))
  4346. (while (setq e (pop alist))
  4347. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4348. (cdr e)) ;; Key already assigned.
  4349. (push e new)
  4350. (let ((clist (string-to-list (downcase (car e))))
  4351. (used (append new alist)))
  4352. (when (= (car clist) ?@)
  4353. (pop clist))
  4354. (while (and clist (rassoc (car clist) used))
  4355. (pop clist))
  4356. (unless clist
  4357. (while (rassoc alt used)
  4358. (incf alt)))
  4359. (push (cons (car e) (or (car clist) alt)) new))))
  4360. (nreverse new)))
  4361. ;;; Some variables used in various places
  4362. (defvar org-window-configuration nil
  4363. "Used in various places to store a window configuration.")
  4364. (defvar org-selected-window nil
  4365. "Used in various places to store a window configuration.")
  4366. (defvar org-finish-function nil
  4367. "Function to be called when `C-c C-c' is used.
  4368. This is for getting out of special buffers like capture.")
  4369. ;; FIXME: Occasionally check by commenting these, to make sure
  4370. ;; no other functions uses these, forgetting to let-bind them.
  4371. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4372. (defvar org-last-state)
  4373. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4374. ;; Defined somewhere in this file, but used before definition.
  4375. (defvar org-entities) ;; defined in org-entities.el
  4376. (defvar org-struct-menu)
  4377. (defvar org-org-menu)
  4378. (defvar org-tbl-menu)
  4379. ;;;; Define the Org-mode
  4380. ;; We use a before-change function to check if a table might need
  4381. ;; an update.
  4382. (defvar org-table-may-need-update t
  4383. "Indicates that a table might need an update.
  4384. This variable is set by `org-before-change-function'.
  4385. `org-table-align' sets it back to nil.")
  4386. (defun org-before-change-function (beg end)
  4387. "Every change indicates that a table might need an update."
  4388. (setq org-table-may-need-update t))
  4389. (defvar org-mode-map)
  4390. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4391. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4392. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4393. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4394. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4395. (defvar org-table-buffer-is-an nil)
  4396. (defvar bidi-paragraph-direction)
  4397. (defvar buffer-face-mode-face)
  4398. (require 'outline)
  4399. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4400. (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"))
  4401. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4402. ;; Other stuff we need.
  4403. (require 'time-date)
  4404. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4405. (require 'easymenu)
  4406. (require 'overlay)
  4407. ;; (require 'org-macs) moved higher up in the file before it is first used
  4408. (require 'org-entities)
  4409. ;; (require 'org-compat) moved higher up in the file before it is first used
  4410. (require 'org-faces)
  4411. (require 'org-list)
  4412. (require 'org-pcomplete)
  4413. (require 'org-src)
  4414. (require 'org-footnote)
  4415. ;; babel
  4416. (require 'ob)
  4417. ;;;###autoload
  4418. (define-derived-mode org-mode outline-mode "Org"
  4419. "Outline-based notes management and organizer, alias
  4420. \"Carsten's outline-mode for keeping track of everything.\"
  4421. Org-mode develops organizational tasks around a NOTES file which
  4422. contains information about projects as plain text. Org-mode is
  4423. implemented on top of outline-mode, which is ideal to keep the content
  4424. of large files well structured. It supports ToDo items, deadlines and
  4425. time stamps, which magically appear in the diary listing of the Emacs
  4426. calendar. Tables are easily created with a built-in table editor.
  4427. Plain text URL-like links connect to websites, emails (VM), Usenet
  4428. messages (Gnus), BBDB entries, and any files related to the project.
  4429. For printing and sharing of notes, an Org-mode file (or a part of it)
  4430. can be exported as a structured ASCII or HTML file.
  4431. The following commands are available:
  4432. \\{org-mode-map}"
  4433. ;; Get rid of Outline menus, they are not needed
  4434. ;; Need to do this here because define-derived-mode sets up
  4435. ;; the keymap so late. Still, it is a waste to call this each time
  4436. ;; we switch another buffer into org-mode.
  4437. (if (featurep 'xemacs)
  4438. (when (boundp 'outline-mode-menu-heading)
  4439. ;; Assume this is Greg's port, it uses easymenu
  4440. (easy-menu-remove outline-mode-menu-heading)
  4441. (easy-menu-remove outline-mode-menu-show)
  4442. (easy-menu-remove outline-mode-menu-hide))
  4443. (define-key org-mode-map [menu-bar headings] 'undefined)
  4444. (define-key org-mode-map [menu-bar hide] 'undefined)
  4445. (define-key org-mode-map [menu-bar show] 'undefined))
  4446. (org-load-modules-maybe)
  4447. (easy-menu-add org-org-menu)
  4448. (easy-menu-add org-tbl-menu)
  4449. (org-install-agenda-files-menu)
  4450. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4451. (add-to-invisibility-spec '(org-cwidth))
  4452. (add-to-invisibility-spec '(org-hide-block . t))
  4453. (when (featurep 'xemacs)
  4454. (org-set-local 'line-move-ignore-invisible t))
  4455. (org-set-local 'outline-regexp org-outline-regexp)
  4456. (org-set-local 'outline-level 'org-outline-level)
  4457. (setq bidi-paragraph-direction 'left-to-right)
  4458. (when (and org-ellipsis
  4459. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4460. (fboundp 'make-glyph-code))
  4461. (unless org-display-table
  4462. (setq org-display-table (make-display-table)))
  4463. (set-display-table-slot
  4464. org-display-table 4
  4465. (vconcat (mapcar
  4466. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4467. org-ellipsis)))
  4468. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4469. (setq buffer-display-table org-display-table))
  4470. (org-set-regexps-and-options)
  4471. (when (and org-tag-faces (not org-tags-special-faces-re))
  4472. ;; tag faces set outside customize.... force initialization.
  4473. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4474. ;; Calc embedded
  4475. (org-set-local 'calc-embedded-open-mode "# ")
  4476. (modify-syntax-entry ?@ "w")
  4477. (modify-syntax-entry ?\" "\"")
  4478. (if org-startup-truncated (setq truncate-lines t))
  4479. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4480. (org-set-local 'font-lock-unfontify-region-function
  4481. 'org-unfontify-region)
  4482. ;; Activate before-change-function
  4483. (org-set-local 'org-table-may-need-update t)
  4484. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4485. 'local)
  4486. ;; Check for running clock before killing a buffer
  4487. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4488. ;; Initialize macros templates.
  4489. (org-macro-initialize-templates)
  4490. ;; Initialize radio targets.
  4491. (org-update-radio-target-regexp)
  4492. ;; Indentation.
  4493. (org-set-local 'indent-line-function 'org-indent-line)
  4494. (org-set-local 'indent-region-function 'org-indent-region)
  4495. ;; Filling and auto-filling.
  4496. (org-setup-filling)
  4497. ;; Comments.
  4498. (org-setup-comments-handling)
  4499. ;; Beginning/end of defun
  4500. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4501. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4502. ;; Next error for sparse trees
  4503. (org-set-local 'next-error-function 'org-occur-next-match)
  4504. ;; Make sure dependence stuff works reliably, even for users who set it
  4505. ;; too late :-(
  4506. (if org-enforce-todo-dependencies
  4507. (add-hook 'org-blocker-hook
  4508. 'org-block-todo-from-children-or-siblings-or-parent)
  4509. (remove-hook 'org-blocker-hook
  4510. 'org-block-todo-from-children-or-siblings-or-parent))
  4511. (if org-enforce-todo-checkbox-dependencies
  4512. (add-hook 'org-blocker-hook
  4513. 'org-block-todo-from-checkboxes)
  4514. (remove-hook 'org-blocker-hook
  4515. 'org-block-todo-from-checkboxes))
  4516. ;; Align options lines
  4517. (org-set-local
  4518. 'align-mode-rules-list
  4519. '((org-in-buffer-settings
  4520. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4521. (modes . '(org-mode)))))
  4522. ;; Imenu
  4523. (org-set-local 'imenu-create-index-function
  4524. 'org-imenu-get-tree)
  4525. ;; Make isearch reveal context
  4526. (if (or (featurep 'xemacs)
  4527. (not (boundp 'outline-isearch-open-invisible-function)))
  4528. ;; Emacs 21 and XEmacs make use of the hook
  4529. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4530. ;; Emacs 22 deals with this through a special variable
  4531. (org-set-local 'outline-isearch-open-invisible-function
  4532. (lambda (&rest ignore) (org-show-context 'isearch))))
  4533. ;; Setup the pcomplete hooks
  4534. (set (make-local-variable 'pcomplete-command-completion-function)
  4535. 'org-pcomplete-initial)
  4536. (set (make-local-variable 'pcomplete-command-name-function)
  4537. 'org-command-at-point)
  4538. (set (make-local-variable 'pcomplete-default-completion-function)
  4539. 'ignore)
  4540. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4541. 'org-parse-arguments)
  4542. (set (make-local-variable 'pcomplete-termination-string) "")
  4543. (when (>= emacs-major-version 23)
  4544. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4545. ;; If empty file that did not turn on org-mode automatically, make it to.
  4546. (if (and org-insert-mode-line-in-empty-file
  4547. (org-called-interactively-p 'any)
  4548. (= (point-min) (point-max)))
  4549. (insert "# -*- mode: org -*-\n\n"))
  4550. (unless org-inhibit-startup
  4551. (and org-startup-with-beamer-mode (org-beamer-mode))
  4552. (when org-startup-align-all-tables
  4553. (let ((bmp (buffer-modified-p)))
  4554. (org-table-map-tables 'org-table-align 'quietly)
  4555. (set-buffer-modified-p bmp)))
  4556. (when org-startup-with-inline-images
  4557. (org-display-inline-images))
  4558. (unless org-inhibit-startup-visibility-stuff
  4559. (org-set-startup-visibility)))
  4560. ;; Try to set org-hide correctly
  4561. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4562. (when (fboundp 'abbrev-table-put)
  4563. (abbrev-table-put org-mode-abbrev-table
  4564. :parents (list text-mode-abbrev-table)))
  4565. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4566. (defun org-find-invisible-foreground ()
  4567. (let ((candidates (remove
  4568. "unspecified-bg"
  4569. (nconc
  4570. (list (face-background 'default)
  4571. (face-background 'org-default))
  4572. (mapcar
  4573. (lambda (alist)
  4574. (when (boundp alist)
  4575. (cdr (assoc 'background-color (symbol-value alist)))))
  4576. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4577. (list (face-foreground 'org-hide))))))
  4578. (car (remove nil candidates))))
  4579. (defun org-current-time (&optional rounding-minutes)
  4580. "Current time, possibly rounded to ROUNDING-MINUTES.
  4581. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4582. `org-time-stamp-rounding-minutes'."
  4583. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4584. (car org-time-stamp-rounding-minutes)))
  4585. (time (decode-time)))
  4586. (if (> r 1)
  4587. (apply 'encode-time
  4588. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4589. (nthcdr 2 time)))
  4590. (current-time))))
  4591. (defun org-today ()
  4592. "Return today date, considering `org-extend-today-until'."
  4593. (time-to-days
  4594. (time-subtract (current-time)
  4595. (list 0 (* 3600 org-extend-today-until) 0))))
  4596. ;;;; Font-Lock stuff, including the activators
  4597. (defvar org-mouse-map (make-sparse-keymap))
  4598. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4599. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4600. (when org-mouse-1-follows-link
  4601. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4602. (when org-tab-follows-link
  4603. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4604. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4605. (require 'font-lock)
  4606. (defconst org-non-link-chars "]\t\n\r<>")
  4607. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4608. "shell" "elisp" "doi" "message"))
  4609. (defvar org-link-types-re nil
  4610. "Matches a link that has a url-like prefix like \"http:\"")
  4611. (defvar org-link-re-with-space nil
  4612. "Matches a link with spaces, optional angular brackets around it.")
  4613. (defvar org-link-re-with-space2 nil
  4614. "Matches a link with spaces, optional angular brackets around it.")
  4615. (defvar org-link-re-with-space3 nil
  4616. "Matches a link with spaces, only for internal part in bracket links.")
  4617. (defvar org-angle-link-re nil
  4618. "Matches link with angular brackets, spaces are allowed.")
  4619. (defvar org-plain-link-re nil
  4620. "Matches plain link, without spaces.")
  4621. (defvar org-bracket-link-regexp nil
  4622. "Matches a link in double brackets.")
  4623. (defvar org-bracket-link-analytic-regexp nil
  4624. "Regular expression used to analyze links.
  4625. Here is what the match groups contain after a match:
  4626. 1: http:
  4627. 2: http
  4628. 3: path
  4629. 4: [desc]
  4630. 5: desc")
  4631. (defvar org-bracket-link-analytic-regexp++ nil
  4632. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4633. (defvar org-any-link-re nil
  4634. "Regular expression matching any link.")
  4635. (defcustom org-match-sexp-depth 3
  4636. "Number of stacked braces for sub/superscript matching.
  4637. This has to be set before loading org.el to be effective."
  4638. :group 'org-export-translation ; ??????????????????????????/
  4639. :type 'integer)
  4640. (defun org-create-multibrace-regexp (left right n)
  4641. "Create a regular expression which will match a balanced sexp.
  4642. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4643. as single character strings.
  4644. The regexp returned will match the entire expression including the
  4645. delimiters. It will also define a single group which contains the
  4646. match except for the outermost delimiters. The maximum depth of
  4647. stacked delimiters is N. Escaping delimiters is not possible."
  4648. (let* ((nothing (concat "[^" left right "]*?"))
  4649. (or "\\|")
  4650. (re nothing)
  4651. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4652. (while (> n 1)
  4653. (setq n (1- n)
  4654. re (concat re or next)
  4655. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4656. (concat left "\\(" re "\\)" right)))
  4657. (defvar org-match-substring-regexp
  4658. (concat
  4659. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4660. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4661. "\\|"
  4662. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4663. "\\|"
  4664. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4665. "The regular expression matching a sub- or superscript.")
  4666. (defvar org-match-substring-with-braces-regexp
  4667. (concat
  4668. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4669. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4670. "\\)")
  4671. "The regular expression matching a sub- or superscript, forcing braces.")
  4672. (defun org-make-link-regexps ()
  4673. "Update the link regular expressions.
  4674. This should be called after the variable `org-link-types' has changed."
  4675. (setq org-link-types-re
  4676. (concat
  4677. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4678. org-link-re-with-space
  4679. (concat
  4680. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4681. "\\([^" org-non-link-chars " ]"
  4682. "[^" org-non-link-chars "]*"
  4683. "[^" org-non-link-chars " ]\\)>?")
  4684. org-link-re-with-space2
  4685. (concat
  4686. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4687. "\\([^" org-non-link-chars " ]"
  4688. "[^\t\n\r]*"
  4689. "[^" org-non-link-chars " ]\\)>?")
  4690. org-link-re-with-space3
  4691. (concat
  4692. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4693. "\\([^" org-non-link-chars " ]"
  4694. "[^\t\n\r]*\\)")
  4695. org-angle-link-re
  4696. (concat
  4697. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4698. "\\([^" org-non-link-chars " ]"
  4699. "[^" org-non-link-chars "]*"
  4700. "\\)>")
  4701. org-plain-link-re
  4702. (concat
  4703. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4704. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4705. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4706. org-bracket-link-regexp
  4707. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4708. org-bracket-link-analytic-regexp
  4709. (concat
  4710. "\\[\\["
  4711. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4712. "\\([^]]+\\)"
  4713. "\\]"
  4714. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4715. "\\]")
  4716. org-bracket-link-analytic-regexp++
  4717. (concat
  4718. "\\[\\["
  4719. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4720. "\\([^]]+\\)"
  4721. "\\]"
  4722. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4723. "\\]")
  4724. org-any-link-re
  4725. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4726. org-angle-link-re "\\)\\|\\("
  4727. org-plain-link-re "\\)")))
  4728. (org-make-link-regexps)
  4729. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4730. "Regular expression for fast time stamp matching.")
  4731. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4732. "Regular expression for fast time stamp matching.")
  4733. (defconst org-ts-regexp0
  4734. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4735. "Regular expression matching time strings for analysis.
  4736. This one does not require the space after the date, so it can be used
  4737. on a string that terminates immediately after the date.")
  4738. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4739. "Regular expression matching time strings for analysis.")
  4740. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4741. "Regular expression matching time stamps, with groups.")
  4742. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4743. "Regular expression matching time stamps (also [..]), with groups.")
  4744. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4745. "Regular expression matching a time stamp range.")
  4746. (defconst org-tr-regexp-both
  4747. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4748. "Regular expression matching a time stamp range.")
  4749. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4750. org-ts-regexp "\\)?")
  4751. "Regular expression matching a time stamp or time stamp range.")
  4752. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4753. org-ts-regexp-both "\\)?")
  4754. "Regular expression matching a time stamp or time stamp range.
  4755. The time stamps may be either active or inactive.")
  4756. (defvar org-emph-face nil)
  4757. (defun org-do-emphasis-faces (limit)
  4758. "Run through the buffer and add overlays to emphasized strings."
  4759. (let (rtn a)
  4760. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4761. (if (not (= (char-after (match-beginning 3))
  4762. (char-after (match-beginning 4))))
  4763. (progn
  4764. (setq rtn t)
  4765. (setq a (assoc (match-string 3) org-emphasis-alist))
  4766. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4767. 'face
  4768. (nth 1 a))
  4769. (and (nth 4 a)
  4770. (org-remove-flyspell-overlays-in
  4771. (match-beginning 0) (match-end 0)))
  4772. (add-text-properties (match-beginning 2) (match-end 2)
  4773. '(font-lock-multiline t org-emphasis t))
  4774. (when org-hide-emphasis-markers
  4775. (add-text-properties (match-end 4) (match-beginning 5)
  4776. '(invisible org-link))
  4777. (add-text-properties (match-beginning 3) (match-end 3)
  4778. '(invisible org-link)))))
  4779. (backward-char 1))
  4780. rtn))
  4781. (defun org-emphasize (&optional char)
  4782. "Insert or change an emphasis, i.e. a font like bold or italic.
  4783. If there is an active region, change that region to a new emphasis.
  4784. If there is no region, just insert the marker characters and position
  4785. the cursor between them.
  4786. CHAR should be either the marker character, or the first character of the
  4787. HTML tag associated with that emphasis. If CHAR is a space, the means
  4788. to remove the emphasis of the selected region.
  4789. If char is not given (for example in an interactive call) it
  4790. will be prompted for."
  4791. (interactive)
  4792. (let ((eal org-emphasis-alist) e det
  4793. (erc org-emphasis-regexp-components)
  4794. (prompt "")
  4795. (string "") beg end move tag c s)
  4796. (if (org-region-active-p)
  4797. (setq beg (region-beginning) end (region-end)
  4798. string (buffer-substring beg end))
  4799. (setq move t))
  4800. (while (setq e (pop eal))
  4801. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4802. c (aref tag 0))
  4803. (push (cons c (string-to-char (car e))) det)
  4804. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4805. (substring tag 1)))))
  4806. (setq det (nreverse det))
  4807. (unless char
  4808. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4809. (setq char (read-char-exclusive)))
  4810. (setq char (or (cdr (assoc char det)) char))
  4811. (if (equal char ?\ )
  4812. (setq s "" move nil)
  4813. (unless (assoc (char-to-string char) org-emphasis-alist)
  4814. (error "No such emphasis marker: \"%c\"" char))
  4815. (setq s (char-to-string char)))
  4816. (while (and (> (length string) 1)
  4817. (equal (substring string 0 1) (substring string -1))
  4818. (assoc (substring string 0 1) org-emphasis-alist))
  4819. (setq string (substring string 1 -1)))
  4820. (setq string (concat s string s))
  4821. (if beg (delete-region beg end))
  4822. (unless (or (bolp)
  4823. (string-match (concat "[" (nth 0 erc) "\n]")
  4824. (char-to-string (char-before (point)))))
  4825. (insert " "))
  4826. (unless (or (eobp)
  4827. (string-match (concat "[" (nth 1 erc) "\n]")
  4828. (char-to-string (char-after (point)))))
  4829. (insert " ") (backward-char 1))
  4830. (insert string)
  4831. (and move (backward-char 1))))
  4832. (defconst org-nonsticky-props
  4833. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4834. (defsubst org-rear-nonsticky-at (pos)
  4835. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4836. (defun org-activate-plain-links (limit)
  4837. "Run through the buffer and add overlays to links."
  4838. (catch 'exit
  4839. (let (f hl)
  4840. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4841. (not (org-in-src-block-p)))
  4842. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4843. (setq f (get-text-property (match-beginning 0) 'face))
  4844. (setq hl (org-match-string-no-properties 0))
  4845. (if (or (eq f 'org-tag)
  4846. (and (listp f) (memq 'org-tag f)))
  4847. nil
  4848. (add-text-properties (match-beginning 0) (match-end 0)
  4849. (list 'mouse-face 'highlight
  4850. 'face 'org-link
  4851. 'htmlize-link `(:uri ,hl)
  4852. 'keymap org-mouse-map))
  4853. (org-rear-nonsticky-at (match-end 0)))
  4854. t))))
  4855. (defun org-activate-code (limit)
  4856. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4857. (progn
  4858. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4859. (remove-text-properties (match-beginning 0) (match-end 0)
  4860. '(display t invisible t intangible t))
  4861. t)))
  4862. (defcustom org-src-fontify-natively nil
  4863. "When non-nil, fontify code in code blocks."
  4864. :type 'boolean
  4865. :version "24.1"
  4866. :group 'org-appearance
  4867. :group 'org-babel)
  4868. (defcustom org-allow-promoting-top-level-subtree nil
  4869. "When non-nil, allow promoting a top level subtree.
  4870. The leading star of the top level headline will be replaced
  4871. by a #."
  4872. :type 'boolean
  4873. :version "24.1"
  4874. :group 'org-appearance)
  4875. (defun org-fontify-meta-lines-and-blocks (limit)
  4876. (condition-case nil
  4877. (org-fontify-meta-lines-and-blocks-1 limit)
  4878. (error (message "org-mode fontification error"))))
  4879. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4880. "Fontify #+ lines and blocks, in the correct ways."
  4881. (let ((case-fold-search t))
  4882. (if (re-search-forward
  4883. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4884. limit t)
  4885. (let ((beg (match-beginning 0))
  4886. (block-start (match-end 0))
  4887. (block-end nil)
  4888. (lang (match-string 7))
  4889. (beg1 (line-beginning-position 2))
  4890. (dc1 (downcase (match-string 2)))
  4891. (dc3 (downcase (match-string 3)))
  4892. end end1 quoting block-type ovl)
  4893. (cond
  4894. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4895. ;; a single line of backend-specific content
  4896. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4897. (remove-text-properties (match-beginning 0) (match-end 0)
  4898. '(display t invisible t intangible t))
  4899. (add-text-properties (match-beginning 1) (match-end 3)
  4900. '(font-lock-fontified t face org-meta-line))
  4901. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4902. '(font-lock-fontified t face org-block))
  4903. ; for backend-specific code
  4904. t)
  4905. ((and (match-end 4) (equal dc3 "+begin"))
  4906. ;; Truly a block
  4907. (setq block-type (downcase (match-string 5))
  4908. quoting (member block-type org-protecting-blocks))
  4909. (when (re-search-forward
  4910. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4911. nil t) ;; on purpose, we look further than LIMIT
  4912. (setq end (min (point-max) (match-end 0))
  4913. end1 (min (point-max) (1- (match-beginning 0))))
  4914. (setq block-end (match-beginning 0))
  4915. (when quoting
  4916. (remove-text-properties beg end
  4917. '(display t invisible t intangible t)))
  4918. (add-text-properties
  4919. beg end
  4920. '(font-lock-fontified t font-lock-multiline t))
  4921. (add-text-properties beg beg1 '(face org-meta-line))
  4922. (add-text-properties end1 (min (point-max) (1+ end))
  4923. '(face org-meta-line)) ; for end_src
  4924. (cond
  4925. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4926. (org-src-font-lock-fontify-block lang block-start block-end)
  4927. ;; remove old background overlays
  4928. (mapc (lambda (ov)
  4929. (if (eq (overlay-get ov 'face) 'org-block-background)
  4930. (delete-overlay ov)))
  4931. (overlays-at (/ (+ beg1 block-end) 2)))
  4932. ;; add a background overlay
  4933. (setq ovl (make-overlay beg1 block-end))
  4934. (overlay-put ovl 'face 'org-block-background)
  4935. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4936. (quoting
  4937. (add-text-properties beg1 (min (point-max) (1+ end1))
  4938. '(face org-block))) ; end of source block
  4939. ((not org-fontify-quote-and-verse-blocks))
  4940. ((string= block-type "quote")
  4941. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4942. ((string= block-type "verse")
  4943. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4944. (add-text-properties beg beg1 '(face org-block-begin-line))
  4945. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4946. '(face org-block-end-line))
  4947. t))
  4948. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4949. (add-text-properties
  4950. beg (match-end 3)
  4951. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4952. '(font-lock-fontified t invisible t)
  4953. '(font-lock-fontified t face org-document-info-keyword)))
  4954. (add-text-properties
  4955. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  4956. (if (string-equal dc1 "+title:")
  4957. '(font-lock-fontified t face org-document-title)
  4958. '(font-lock-fontified t face org-document-info))))
  4959. ((or (equal dc1 "+results")
  4960. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4961. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4962. "+call:" "+header:" "+headers:" "+name:"))
  4963. (and (match-end 4) (equal dc3 "+attr")))
  4964. (add-text-properties
  4965. beg (match-end 0)
  4966. '(font-lock-fontified t face org-meta-line))
  4967. t)
  4968. ((member dc3 '(" " ""))
  4969. (add-text-properties
  4970. beg (match-end 0)
  4971. '(font-lock-fontified t face font-lock-comment-face)))
  4972. ((not (member (char-after beg) '(?\ ?\t)))
  4973. ;; just any other in-buffer setting, but not indented
  4974. (add-text-properties
  4975. beg (match-end 0)
  4976. '(font-lock-fontified t face org-meta-line))
  4977. t)
  4978. (t nil))))))
  4979. (defun org-activate-angle-links (limit)
  4980. "Run through the buffer and add overlays to links."
  4981. (if (and (re-search-forward org-angle-link-re limit t)
  4982. (not (org-in-src-block-p)))
  4983. (progn
  4984. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4985. (add-text-properties (match-beginning 0) (match-end 0)
  4986. (list 'mouse-face 'highlight
  4987. 'keymap org-mouse-map))
  4988. (org-rear-nonsticky-at (match-end 0))
  4989. t)))
  4990. (defun org-activate-footnote-links (limit)
  4991. "Run through the buffer and add overlays to footnotes."
  4992. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4993. (when fn
  4994. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4995. (org-remove-flyspell-overlays-in beg end)
  4996. (add-text-properties beg end
  4997. (list 'mouse-face 'highlight
  4998. 'keymap org-mouse-map
  4999. 'help-echo
  5000. (if (= (point-at-bol) beg)
  5001. "Footnote definition"
  5002. "Footnote reference")
  5003. 'font-lock-fontified t
  5004. 'font-lock-multiline t
  5005. 'face 'org-footnote))))))
  5006. (defun org-activate-bracket-links (limit)
  5007. "Run through the buffer and add overlays to bracketed links."
  5008. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5009. (not (org-in-src-block-p)))
  5010. (let* ((hl (org-match-string-no-properties 1))
  5011. (help (concat "LINK: " hl))
  5012. ;; FIXME: Above we should remove the escapes. But that
  5013. ;; requires another match, protecting match data, a lot
  5014. ;; of overhead for font-lock.
  5015. (ip (org-maybe-intangible
  5016. (list 'invisible 'org-link
  5017. 'keymap org-mouse-map 'mouse-face 'highlight
  5018. 'font-lock-multiline t 'help-echo help
  5019. 'htmlize-link `(:uri ,hl))))
  5020. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5021. 'font-lock-multiline t 'help-echo help
  5022. 'htmlize-link `(:uri ,hl))))
  5023. ;; We need to remove the invisible property here. Table narrowing
  5024. ;; may have made some of this invisible.
  5025. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5026. (remove-text-properties (match-beginning 0) (match-end 0)
  5027. '(invisible nil))
  5028. (if (match-end 3)
  5029. (progn
  5030. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5031. (org-rear-nonsticky-at (match-beginning 3))
  5032. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5033. (org-rear-nonsticky-at (match-end 3))
  5034. (add-text-properties (match-end 3) (match-end 0) ip)
  5035. (org-rear-nonsticky-at (match-end 0)))
  5036. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5037. (org-rear-nonsticky-at (match-beginning 1))
  5038. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5039. (org-rear-nonsticky-at (match-end 1))
  5040. (add-text-properties (match-end 1) (match-end 0) ip)
  5041. (org-rear-nonsticky-at (match-end 0)))
  5042. t)))
  5043. (defun org-activate-dates (limit)
  5044. "Run through the buffer and add overlays to dates."
  5045. (if (re-search-forward org-tsr-regexp-both limit t)
  5046. (progn
  5047. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5048. (add-text-properties (match-beginning 0) (match-end 0)
  5049. (list 'mouse-face 'highlight
  5050. 'keymap org-mouse-map))
  5051. (org-rear-nonsticky-at (match-end 0))
  5052. (when org-display-custom-times
  5053. (if (match-end 3)
  5054. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5055. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5056. t)))
  5057. (defvar org-target-link-regexp nil
  5058. "Regular expression matching radio targets in plain text.")
  5059. (make-variable-buffer-local 'org-target-link-regexp)
  5060. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5061. "Regular expression matching a link target.")
  5062. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5063. "Regular expression matching a radio target.")
  5064. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5065. "Regular expression matching any target.")
  5066. (defun org-activate-target-links (limit)
  5067. "Run through the buffer and add overlays to target matches."
  5068. (when org-target-link-regexp
  5069. (let ((case-fold-search t))
  5070. (if (re-search-forward org-target-link-regexp limit t)
  5071. (progn
  5072. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5073. (add-text-properties (match-beginning 0) (match-end 0)
  5074. (list 'mouse-face 'highlight
  5075. 'keymap org-mouse-map
  5076. 'help-echo "Radio target link"
  5077. 'org-linked-text t))
  5078. (org-rear-nonsticky-at (match-end 0))
  5079. t)))))
  5080. (defun org-update-radio-target-regexp ()
  5081. "Find all radio targets in this file and update the regular expression."
  5082. (interactive)
  5083. (when (memq 'radio org-activate-links)
  5084. (setq org-target-link-regexp
  5085. (org-make-target-link-regexp (org-all-targets 'radio)))
  5086. (org-restart-font-lock)))
  5087. (defun org-hide-wide-columns (limit)
  5088. (let (s e)
  5089. (setq s (text-property-any (point) (or limit (point-max))
  5090. 'org-cwidth t))
  5091. (when s
  5092. (setq e (next-single-property-change s 'org-cwidth))
  5093. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5094. (goto-char e)
  5095. t)))
  5096. (defvar org-latex-and-specials-regexp nil
  5097. "Regular expression for highlighting export special stuff.")
  5098. (defvar org-match-substring-regexp)
  5099. (defvar org-match-substring-with-braces-regexp)
  5100. ;; This should be with the exporter code, but we also use if for font-locking
  5101. (defconst org-export-html-special-string-regexps
  5102. '(("\\\\-" . "&shy;")
  5103. ("---\\([^-]\\)" . "&mdash;\\1")
  5104. ("--\\([^-]\\)" . "&ndash;\\1")
  5105. ("\\.\\.\\." . "&hellip;"))
  5106. "Regular expressions for special string conversion.")
  5107. (defun org-compute-latex-and-specials-regexp ()
  5108. "Compute regular expression for stuff treated specially by exporters."
  5109. (if (not org-highlight-latex-fragments-and-specials)
  5110. (org-set-local 'org-latex-and-specials-regexp nil)
  5111. (require 'org-exp)
  5112. (let*
  5113. ((matchers (plist-get org-format-latex-options :matchers))
  5114. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5115. org-latex-regexps)))
  5116. (org-export-allow-BIND nil)
  5117. (options (org-combine-plists (org-default-export-plist)
  5118. (org-infile-export-plist)))
  5119. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5120. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5121. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5122. (org-export-html-expand (plist-get options :expand-quoted-html))
  5123. (org-export-with-special-strings (plist-get options :special-strings))
  5124. (re-sub
  5125. (cond
  5126. ((equal org-export-with-sub-superscripts '{})
  5127. (list org-match-substring-with-braces-regexp))
  5128. (org-export-with-sub-superscripts
  5129. (list org-match-substring-regexp))))
  5130. (re-latex
  5131. (if org-export-with-LaTeX-fragments
  5132. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5133. (re-macros
  5134. (if org-export-with-TeX-macros
  5135. (list (concat "\\\\"
  5136. (regexp-opt
  5137. (append
  5138. (delq nil
  5139. (mapcar 'car-safe
  5140. (append org-entities-user
  5141. org-entities)))
  5142. (if (boundp 'org-latex-entities)
  5143. (mapcar (lambda (x)
  5144. (or (car-safe x) x))
  5145. org-latex-entities)
  5146. nil))
  5147. 'words))) ; FIXME
  5148. ))
  5149. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5150. (re-special (if org-export-with-special-strings
  5151. (mapcar (lambda (x) (car x))
  5152. org-export-html-special-string-regexps)))
  5153. (re-rest
  5154. (delq nil
  5155. (list
  5156. (if org-export-html-expand "@<[^>\n]+>")
  5157. ))))
  5158. (org-set-local
  5159. 'org-latex-and-specials-regexp
  5160. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5161. re-rest) "\\|")))))
  5162. (defun org-do-latex-and-special-faces (limit)
  5163. "Run through the buffer and add overlays to links."
  5164. (when org-latex-and-specials-regexp
  5165. (let (rtn d)
  5166. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5167. limit t))
  5168. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5169. 'face))
  5170. '(org-code org-verbatim underline)))
  5171. (progn
  5172. (setq rtn t
  5173. d (cond ((member (char-after (1+ (match-beginning 0)))
  5174. '(?_ ?^)) 1)
  5175. (t 0)))
  5176. (font-lock-prepend-text-property
  5177. (+ d (match-beginning 0)) (match-end 0)
  5178. 'face 'org-latex-and-export-specials)
  5179. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5180. '(font-lock-multiline t)))))
  5181. rtn)))
  5182. (defun org-restart-font-lock ()
  5183. "Restart `font-lock-mode', to force refontification."
  5184. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5185. (font-lock-mode -1)
  5186. (font-lock-mode 1)))
  5187. (defun org-all-targets (&optional radio)
  5188. "Return a list of all targets in this file.
  5189. When optional argument RADIO is non-nil, only find radio
  5190. targets."
  5191. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5192. (save-excursion
  5193. (goto-char (point-min))
  5194. (while (re-search-forward re nil t)
  5195. ;; Make sure point is really within the object.
  5196. (backward-char)
  5197. (let ((obj (org-element-context)))
  5198. (when (memq (org-element-type obj) '(radio-target target))
  5199. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5200. rtn)))
  5201. (defun org-make-target-link-regexp (targets)
  5202. "Make regular expression matching all strings in TARGETS.
  5203. The regular expression finds the targets also if there is a line break
  5204. between words."
  5205. (and targets
  5206. (concat
  5207. "\\<\\("
  5208. (mapconcat
  5209. (lambda (x)
  5210. (setq x (regexp-quote x))
  5211. (while (string-match " +" x)
  5212. (setq x (replace-match "\\s-+" t t x)))
  5213. x)
  5214. targets
  5215. "\\|")
  5216. "\\)\\>")))
  5217. (defun org-activate-tags (limit)
  5218. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5219. (progn
  5220. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5221. (add-text-properties (match-beginning 1) (match-end 1)
  5222. (list 'mouse-face 'highlight
  5223. 'keymap org-mouse-map))
  5224. (org-rear-nonsticky-at (match-end 1))
  5225. t)))
  5226. (defun org-outline-level ()
  5227. "Compute the outline level of the heading at point.
  5228. If this is called at a normal headline, the level is the number of stars.
  5229. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5230. (save-excursion
  5231. (if (not (condition-case nil
  5232. (org-back-to-heading t)
  5233. (error nil)))
  5234. 0
  5235. (looking-at org-outline-regexp)
  5236. (1- (- (match-end 0) (match-beginning 0))))))
  5237. (defvar org-font-lock-keywords nil)
  5238. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5239. "Regular expression matching a property line.")
  5240. (defvar org-font-lock-hook nil
  5241. "Functions to be called for special font lock stuff.")
  5242. (defvar org-font-lock-set-keywords-hook nil
  5243. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5244. This is called after `org-font-lock-extra-keywords' is defined, but before
  5245. it is installed to be used by font lock. This can be useful if something
  5246. needs to be inserted at a specific position in the font-lock sequence.")
  5247. (defun org-font-lock-hook (limit)
  5248. "Run `org-font-lock-hook' within LIMIT."
  5249. (run-hook-with-args 'org-font-lock-hook limit))
  5250. (defun org-set-font-lock-defaults ()
  5251. "Set font lock defaults for the current buffer."
  5252. (let* ((em org-fontify-emphasized-text)
  5253. (lk org-activate-links)
  5254. (org-font-lock-extra-keywords
  5255. (list
  5256. ;; Call the hook
  5257. '(org-font-lock-hook)
  5258. ;; Headlines
  5259. `(,(if org-fontify-whole-heading-line
  5260. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5261. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5262. (1 (org-get-level-face 1))
  5263. (2 (org-get-level-face 2))
  5264. (3 (org-get-level-face 3)))
  5265. ;; Table lines
  5266. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5267. (1 'org-table t))
  5268. ;; Table internals
  5269. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5270. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5271. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5272. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5273. ;; Drawers
  5274. (list org-drawer-regexp '(0 'org-special-keyword t))
  5275. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5276. ;; Properties
  5277. (list org-property-re
  5278. '(1 'org-special-keyword t)
  5279. '(3 'org-property-value t))
  5280. ;; Links
  5281. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5282. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5283. (if (memq 'plain lk) '(org-activate-plain-links))
  5284. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5285. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5286. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5287. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5288. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5289. '(org-hide-wide-columns (0 nil append))
  5290. ;; TODO keyword
  5291. (list (format org-heading-keyword-regexp-format
  5292. org-todo-regexp)
  5293. '(2 (org-get-todo-face 2) t))
  5294. ;; DONE
  5295. (if org-fontify-done-headline
  5296. (list (format org-heading-keyword-regexp-format
  5297. (concat
  5298. "\\(?:"
  5299. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5300. "\\)"))
  5301. '(2 'org-headline-done t))
  5302. nil)
  5303. ;; Priorities
  5304. '(org-font-lock-add-priority-faces)
  5305. ;; Tags
  5306. '(org-font-lock-add-tag-faces)
  5307. ;; Special keywords
  5308. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5309. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5310. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5311. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5312. ;; Emphasis
  5313. (if em
  5314. (if (featurep 'xemacs)
  5315. '(org-do-emphasis-faces (0 nil append))
  5316. '(org-do-emphasis-faces)))
  5317. ;; Checkboxes
  5318. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5319. 1 'org-checkbox prepend)
  5320. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5321. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5322. (0 (org-get-checkbox-statistics-face) t)))
  5323. ;; Description list items
  5324. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5325. 1 'org-list-dt prepend)
  5326. ;; ARCHIVEd headings
  5327. (list (concat
  5328. org-outline-regexp-bol
  5329. "\\(.*:" org-archive-tag ":.*\\)")
  5330. '(1 'org-archived prepend))
  5331. ;; Specials
  5332. '(org-do-latex-and-special-faces)
  5333. '(org-fontify-entities)
  5334. '(org-raise-scripts)
  5335. ;; Code
  5336. '(org-activate-code (1 'org-code t))
  5337. ;; COMMENT
  5338. (list (format org-heading-keyword-regexp-format
  5339. (concat "\\("
  5340. org-comment-string "\\|" org-quote-string
  5341. "\\)"))
  5342. '(2 'org-special-keyword t))
  5343. ;; Blocks and meta lines
  5344. '(org-fontify-meta-lines-and-blocks)
  5345. )))
  5346. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5347. (run-hooks 'org-font-lock-set-keywords-hook)
  5348. ;; Now set the full font-lock-keywords
  5349. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5350. (org-set-local 'font-lock-defaults
  5351. '(org-font-lock-keywords t nil nil backward-paragraph))
  5352. (kill-local-variable 'font-lock-keywords) nil))
  5353. (defun org-toggle-pretty-entities ()
  5354. "Toggle the composition display of entities as UTF8 characters."
  5355. (interactive)
  5356. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5357. (org-restart-font-lock)
  5358. (if org-pretty-entities
  5359. (message "Entities are displayed as UTF8 characters")
  5360. (save-restriction
  5361. (widen)
  5362. (org-decompose-region (point-min) (point-max))
  5363. (message "Entities are displayed plain"))))
  5364. (defvar org-custom-properties-overlays nil
  5365. "List of overlays used for custom properties.")
  5366. (make-variable-buffer-local 'org-custom-properties-overlays)
  5367. (defun org-toggle-custom-properties-visibility ()
  5368. "Display or hide properties in `org-custom-properties'."
  5369. (interactive)
  5370. (if org-custom-properties-overlays
  5371. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5372. (setq org-custom-properties-overlays nil))
  5373. (unless (not org-custom-properties)
  5374. (save-excursion
  5375. (save-restriction
  5376. (widen)
  5377. (goto-char (point-min))
  5378. (while (re-search-forward org-property-re nil t)
  5379. (mapc (lambda(p)
  5380. (when (equal p (substring (match-string 1) 1 -1))
  5381. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5382. (overlay-put o 'invisible t)
  5383. (overlay-put o 'org-custom-property t)
  5384. (push o org-custom-properties-overlays))))
  5385. org-custom-properties)))))))
  5386. (defun org-fontify-entities (limit)
  5387. "Find an entity to fontify."
  5388. (let (ee)
  5389. (when org-pretty-entities
  5390. (catch 'match
  5391. (while (re-search-forward
  5392. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5393. limit t)
  5394. (if (and (not (org-in-indented-comment-line))
  5395. (setq ee (org-entity-get (match-string 1)))
  5396. (= (length (nth 6 ee)) 1))
  5397. (let*
  5398. ((end (if (equal (match-string 2) "{}")
  5399. (match-end 2)
  5400. (match-end 1))))
  5401. (add-text-properties
  5402. (match-beginning 0) end
  5403. (list 'font-lock-fontified t))
  5404. (compose-region (match-beginning 0) end
  5405. (nth 6 ee) nil)
  5406. (backward-char 1)
  5407. (throw 'match t))))
  5408. nil))))
  5409. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5410. "Fontify string S like in Org-mode."
  5411. (with-temp-buffer
  5412. (insert s)
  5413. (let ((org-odd-levels-only odd-levels))
  5414. (org-mode)
  5415. (font-lock-fontify-buffer)
  5416. (buffer-string))))
  5417. (defvar org-m nil)
  5418. (defvar org-l nil)
  5419. (defvar org-f nil)
  5420. (defun org-get-level-face (n)
  5421. "Get the right face for match N in font-lock matching of headlines."
  5422. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5423. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5424. (if org-cycle-level-faces
  5425. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5426. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5427. (cond
  5428. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5429. ((eq n 2) org-f)
  5430. (t (if org-level-color-stars-only nil org-f))))
  5431. (defun org-get-todo-face (kwd)
  5432. "Get the right face for a TODO keyword KWD.
  5433. If KWD is a number, get the corresponding match group."
  5434. (if (numberp kwd) (setq kwd (match-string kwd)))
  5435. (or (org-face-from-face-or-color
  5436. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5437. (and (member kwd org-done-keywords) 'org-done)
  5438. 'org-todo))
  5439. (defun org-face-from-face-or-color (context inherit face-or-color)
  5440. "Create a face list that inherits INHERIT, but sets the foreground color.
  5441. When FACE-OR-COLOR is not a string, just return it."
  5442. (if (stringp face-or-color)
  5443. (list :inherit inherit
  5444. (cdr (assoc context org-faces-easy-properties))
  5445. face-or-color)
  5446. face-or-color))
  5447. (defun org-font-lock-add-tag-faces (limit)
  5448. "Add the special tag faces."
  5449. (when (and org-tag-faces org-tags-special-faces-re)
  5450. (while (re-search-forward org-tags-special-faces-re limit t)
  5451. (add-text-properties (match-beginning 1) (match-end 1)
  5452. (list 'face (org-get-tag-face 1)
  5453. 'font-lock-fontified t))
  5454. (backward-char 1))))
  5455. (defun org-font-lock-add-priority-faces (limit)
  5456. "Add the special priority faces."
  5457. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5458. (when (save-match-data (org-at-heading-p))
  5459. (add-text-properties
  5460. (match-beginning 0) (match-end 0)
  5461. (list 'face (or (org-face-from-face-or-color
  5462. 'priority 'org-priority
  5463. (cdr (assoc (char-after (match-beginning 1))
  5464. org-priority-faces)))
  5465. 'org-priority)
  5466. 'font-lock-fontified t)))))
  5467. (defun org-get-tag-face (kwd)
  5468. "Get the right face for a TODO keyword KWD.
  5469. If KWD is a number, get the corresponding match group."
  5470. (if (numberp kwd) (setq kwd (match-string kwd)))
  5471. (or (org-face-from-face-or-color
  5472. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5473. 'org-tag))
  5474. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5475. "Remove fontification and activation overlays from links."
  5476. (font-lock-default-unfontify-region beg end)
  5477. (let* ((buffer-undo-list t)
  5478. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5479. (inhibit-modification-hooks t)
  5480. deactivate-mark buffer-file-name buffer-file-truename)
  5481. (org-decompose-region beg end)
  5482. (remove-text-properties beg end
  5483. '(mouse-face t keymap t org-linked-text t
  5484. invisible t intangible t
  5485. org-no-flyspell t org-emphasis t))
  5486. (org-remove-font-lock-display-properties beg end)))
  5487. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5488. ((raise 0.3) (height 0.7))
  5489. ((raise -0.5))
  5490. ((raise 0.5)))
  5491. "Display properties for showing superscripts and subscripts.")
  5492. (defun org-remove-font-lock-display-properties (beg end)
  5493. "Remove specific display properties that have been added by font lock.
  5494. The will remove the raise properties that are used to show superscripts
  5495. and subscripts."
  5496. (let (next prop)
  5497. (while (< beg end)
  5498. (setq next (next-single-property-change beg 'display nil end)
  5499. prop (get-text-property beg 'display))
  5500. (if (member prop org-script-display)
  5501. (put-text-property beg next 'display nil))
  5502. (setq beg next))))
  5503. (defun org-raise-scripts (limit)
  5504. "Add raise properties to sub/superscripts."
  5505. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5506. (if (re-search-forward
  5507. (if (eq org-use-sub-superscripts t)
  5508. org-match-substring-regexp
  5509. org-match-substring-with-braces-regexp)
  5510. limit t)
  5511. (let* ((pos (point)) table-p comment-p
  5512. (mpos (match-beginning 3))
  5513. (emph-p (get-text-property mpos 'org-emphasis))
  5514. (link-p (get-text-property mpos 'mouse-face))
  5515. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5516. (goto-char (point-at-bol))
  5517. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5518. comment-p (org-looking-at-p "[ \t]*#"))
  5519. (goto-char pos)
  5520. ;; FIXME: Should we go back one character here, for a_b^c
  5521. ;; (goto-char (1- pos)) ;????????????????????
  5522. (if (or comment-p emph-p link-p keyw-p)
  5523. t
  5524. (put-text-property (match-beginning 3) (match-end 0)
  5525. 'display
  5526. (if (equal (char-after (match-beginning 2)) ?^)
  5527. (nth (if table-p 3 1) org-script-display)
  5528. (nth (if table-p 2 0) org-script-display)))
  5529. (add-text-properties (match-beginning 2) (match-end 2)
  5530. (list 'invisible t
  5531. 'org-dwidth t 'org-dwidth-n 1))
  5532. (if (and (eq (char-after (match-beginning 3)) ?{)
  5533. (eq (char-before (match-end 3)) ?}))
  5534. (progn
  5535. (add-text-properties
  5536. (match-beginning 3) (1+ (match-beginning 3))
  5537. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5538. (add-text-properties
  5539. (1- (match-end 3)) (match-end 3)
  5540. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5541. t)))))
  5542. ;;;; Visibility cycling, including org-goto and indirect buffer
  5543. ;;; Cycling
  5544. (defvar org-cycle-global-status nil)
  5545. (make-variable-buffer-local 'org-cycle-global-status)
  5546. (defvar org-cycle-subtree-status nil)
  5547. (make-variable-buffer-local 'org-cycle-subtree-status)
  5548. (defvar org-inlinetask-min-level)
  5549. ;;;###autoload
  5550. (defun org-cycle (&optional arg)
  5551. "TAB-action and visibility cycling for Org-mode.
  5552. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5553. is outline visibility cycling, but it also invokes other actions
  5554. in special contexts.
  5555. - When this function is called with a prefix argument, rotate the entire
  5556. buffer through 3 states (global cycling)
  5557. 1. OVERVIEW: Show only top-level headlines.
  5558. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5559. 3. SHOW ALL: Show everything.
  5560. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5561. determined by the variable `org-startup-folded', and by any VISIBILITY
  5562. properties in the buffer.
  5563. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5564. including any drawers.
  5565. - When inside a table, re-align the table and move to the next field.
  5566. - When point is at the beginning of a headline, rotate the subtree started
  5567. by this line through 3 different states (local cycling)
  5568. 1. FOLDED: Only the main headline is shown.
  5569. 2. CHILDREN: The main headline and the direct children are shown.
  5570. From this state, you can move to one of the children
  5571. and zoom in further.
  5572. 3. SUBTREE: Show the entire subtree, including body text.
  5573. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5574. - When point is at the beginning of an empty headline and the variable
  5575. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5576. of the headline by demoting and promoting it to likely levels. This
  5577. speeds up creation document structure by pressing TAB once or several
  5578. times right after creating a new headline.
  5579. - When there is a numeric prefix, go up to a heading with level ARG, do
  5580. a `show-subtree' and return to the previous cursor position. If ARG
  5581. is negative, go up that many levels.
  5582. - When point is not at the beginning of a headline, execute the global
  5583. binding for TAB, which is re-indenting the line. See the option
  5584. `org-cycle-emulate-tab' for details.
  5585. - Special case: if point is at the beginning of the buffer and there is
  5586. no headline in line 1, this function will act as if called with prefix arg
  5587. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5588. But only if also the variable `org-cycle-global-at-bob' is t."
  5589. (interactive "P")
  5590. (org-load-modules-maybe)
  5591. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5592. (and org-cycle-level-after-item/entry-creation
  5593. (or (org-cycle-level)
  5594. (org-cycle-item-indentation))))
  5595. (let* (message-log-max ; Don't populate the *Messages* buffer
  5596. (limit-level
  5597. (or org-cycle-max-level
  5598. (and (boundp 'org-inlinetask-min-level)
  5599. org-inlinetask-min-level
  5600. (1- org-inlinetask-min-level))))
  5601. (nstars (and limit-level
  5602. (if org-odd-levels-only
  5603. (and limit-level (1- (* limit-level 2)))
  5604. limit-level)))
  5605. (org-outline-regexp
  5606. (if (not (derived-mode-p 'org-mode))
  5607. outline-regexp
  5608. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5609. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5610. (not (looking-at org-outline-regexp))))
  5611. (org-cycle-hook
  5612. (if bob-special
  5613. (delq 'org-optimize-window-after-visibility-change
  5614. (copy-sequence org-cycle-hook))
  5615. org-cycle-hook))
  5616. (pos (point)))
  5617. (if (or bob-special (equal arg '(4)))
  5618. ;; special case: use global cycling
  5619. (setq arg t))
  5620. (cond
  5621. ((equal arg '(16))
  5622. (setq last-command 'dummy)
  5623. (org-set-startup-visibility)
  5624. (message "Startup visibility, plus VISIBILITY properties"))
  5625. ((equal arg '(64))
  5626. (show-all)
  5627. (message "Entire buffer visible, including drawers"))
  5628. ;; Table: enter it or move to the next field.
  5629. ((org-at-table-p 'any)
  5630. (if (org-at-table.el-p)
  5631. (message "Use C-c ' to edit table.el tables")
  5632. (if arg (org-table-edit-field t)
  5633. (org-table-justify-field-maybe)
  5634. (call-interactively 'org-table-next-field))))
  5635. ((run-hook-with-args-until-success
  5636. 'org-tab-after-check-for-table-hook))
  5637. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5638. ((eq arg t) (org-cycle-internal-global))
  5639. ;; Drawers: delegate to `org-flag-drawer'.
  5640. ((and org-drawers org-drawer-regexp
  5641. (save-excursion
  5642. (beginning-of-line 1)
  5643. (looking-at org-drawer-regexp)))
  5644. (org-flag-drawer ; toggle block visibility
  5645. (not (get-char-property (match-end 0) 'invisible))))
  5646. ;; Show-subtree, ARG levels up from here.
  5647. ((integerp arg)
  5648. (save-excursion
  5649. (org-back-to-heading)
  5650. (outline-up-heading (if (< arg 0) (- arg)
  5651. (- (funcall outline-level) arg)))
  5652. (org-show-subtree)))
  5653. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5654. ((and (featurep 'org-inlinetask)
  5655. (org-inlinetask-at-task-p)
  5656. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5657. (org-inlinetask-toggle-visibility))
  5658. ((org-try-cdlatex-tab))
  5659. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5660. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5661. (save-excursion (beginning-of-line 1)
  5662. (looking-at org-outline-regexp)))
  5663. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5664. (org-cycle-internal-local))
  5665. ;; From there: TAB emulation and template completion.
  5666. (buffer-read-only (org-back-to-heading))
  5667. ((run-hook-with-args-until-success
  5668. 'org-tab-after-check-for-cycling-hook))
  5669. ((org-try-structure-completion))
  5670. ((run-hook-with-args-until-success
  5671. 'org-tab-before-tab-emulation-hook))
  5672. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5673. (or (not (bolp))
  5674. (not (looking-at org-outline-regexp))))
  5675. (call-interactively (global-key-binding "\t")))
  5676. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5677. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5678. (or (and (eq org-cycle-emulate-tab 'white)
  5679. (= (match-end 0) (point-at-eol)))
  5680. (and (eq org-cycle-emulate-tab 'whitestart)
  5681. (>= (match-end 0) pos))))
  5682. t
  5683. (eq org-cycle-emulate-tab t))
  5684. (call-interactively (global-key-binding "\t")))
  5685. (t (save-excursion
  5686. (org-back-to-heading)
  5687. (org-cycle)))))))
  5688. (defun org-cycle-internal-global ()
  5689. "Do the global cycling action."
  5690. ;; Hack to avoid display of messages for .org attachments in Gnus
  5691. (let (message-log-max ; Don't populate the *Messages* buffer
  5692. (ga (string-match "\\*fontification" (buffer-name))))
  5693. (cond
  5694. ((and (eq last-command this-command)
  5695. (eq org-cycle-global-status 'overview))
  5696. ;; We just created the overview - now do table of contents
  5697. ;; This can be slow in very large buffers, so indicate action
  5698. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5699. (unless ga (message "CONTENTS..."))
  5700. (org-content)
  5701. (unless ga (message "CONTENTS...done"))
  5702. (setq org-cycle-global-status 'contents)
  5703. (run-hook-with-args 'org-cycle-hook 'contents))
  5704. ((and (eq last-command this-command)
  5705. (eq org-cycle-global-status 'contents))
  5706. ;; We just showed the table of contents - now show everything
  5707. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5708. (show-all)
  5709. (unless ga (message "SHOW ALL"))
  5710. (setq org-cycle-global-status 'all)
  5711. (run-hook-with-args 'org-cycle-hook 'all))
  5712. (t
  5713. ;; Default action: go to overview
  5714. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5715. (org-overview)
  5716. (unless ga (message "OVERVIEW"))
  5717. (setq org-cycle-global-status 'overview)
  5718. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5719. (defun org-cycle-internal-local ()
  5720. "Do the local cycling action."
  5721. (let (message-log-max ; Don't populate the *Messages* buffer
  5722. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5723. ;; First, determine end of headline (EOH), end of subtree or item
  5724. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5725. (save-excursion
  5726. (if (org-at-item-p)
  5727. (progn
  5728. (beginning-of-line)
  5729. (setq struct (org-list-struct))
  5730. (setq eoh (point-at-eol))
  5731. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5732. (setq has-children (org-list-has-child-p (point) struct)))
  5733. (org-back-to-heading)
  5734. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5735. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5736. (setq has-children
  5737. (or (save-excursion
  5738. (let ((level (funcall outline-level)))
  5739. (outline-next-heading)
  5740. (and (org-at-heading-p t)
  5741. (> (funcall outline-level) level))))
  5742. (save-excursion
  5743. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5744. ;; Determine end invisible part of buffer (EOL)
  5745. (beginning-of-line 2)
  5746. ;; XEmacs doesn't have `next-single-char-property-change'
  5747. (if (featurep 'xemacs)
  5748. (while (and (not (eobp)) ;; this is like `next-line'
  5749. (get-char-property (1- (point)) 'invisible))
  5750. (beginning-of-line 2))
  5751. (while (and (not (eobp)) ;; this is like `next-line'
  5752. (get-char-property (1- (point)) 'invisible))
  5753. (goto-char (next-single-char-property-change (point) 'invisible))
  5754. (and (eolp) (beginning-of-line 2))))
  5755. (setq eol (point)))
  5756. ;; Find out what to do next and set `this-command'
  5757. (cond
  5758. ((= eos eoh)
  5759. ;; Nothing is hidden behind this heading
  5760. (unless (org-before-first-heading-p)
  5761. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5762. (message "EMPTY ENTRY")
  5763. (setq org-cycle-subtree-status nil)
  5764. (save-excursion
  5765. (goto-char eos)
  5766. (outline-next-heading)
  5767. (if (outline-invisible-p) (org-flag-heading nil))))
  5768. ((and (or (>= eol eos)
  5769. (not (string-match "\\S-" (buffer-substring eol eos))))
  5770. (or has-children
  5771. (not (setq children-skipped
  5772. org-cycle-skip-children-state-if-no-children))))
  5773. ;; Entire subtree is hidden in one line: children view
  5774. (unless (org-before-first-heading-p)
  5775. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5776. (if (org-at-item-p)
  5777. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5778. (org-show-entry)
  5779. (org-with-limited-levels (show-children))
  5780. ;; FIXME: This slows down the func way too much.
  5781. ;; How keep drawers hidden in subtree anyway?
  5782. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5783. ;; (org-cycle-hide-drawers 'subtree))
  5784. ;; Fold every list in subtree to top-level items.
  5785. (when (eq org-cycle-include-plain-lists 'integrate)
  5786. (save-excursion
  5787. (org-back-to-heading)
  5788. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5789. (beginning-of-line 1)
  5790. (let* ((struct (org-list-struct))
  5791. (prevs (org-list-prevs-alist struct))
  5792. (end (org-list-get-bottom-point struct)))
  5793. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5794. (org-list-get-all-items (point) struct prevs))
  5795. (goto-char end))))))
  5796. (message "CHILDREN")
  5797. (save-excursion
  5798. (goto-char eos)
  5799. (outline-next-heading)
  5800. (if (outline-invisible-p) (org-flag-heading nil)))
  5801. (setq org-cycle-subtree-status 'children)
  5802. (unless (org-before-first-heading-p)
  5803. (run-hook-with-args 'org-cycle-hook 'children)))
  5804. ((or children-skipped
  5805. (and (eq last-command this-command)
  5806. (eq org-cycle-subtree-status 'children)))
  5807. ;; We just showed the children, or no children are there,
  5808. ;; now show everything.
  5809. (unless (org-before-first-heading-p)
  5810. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5811. (outline-flag-region eoh eos nil)
  5812. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5813. (setq org-cycle-subtree-status 'subtree)
  5814. (unless (org-before-first-heading-p)
  5815. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5816. (t
  5817. ;; Default action: hide the subtree.
  5818. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5819. (outline-flag-region eoh eos t)
  5820. (message "FOLDED")
  5821. (setq org-cycle-subtree-status 'folded)
  5822. (unless (org-before-first-heading-p)
  5823. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5824. ;;;###autoload
  5825. (defun org-global-cycle (&optional arg)
  5826. "Cycle the global visibility. For details see `org-cycle'.
  5827. With \\[universal-argument] prefix arg, switch to startup visibility.
  5828. With a numeric prefix, show all headlines up to that level."
  5829. (interactive "P")
  5830. (let ((org-cycle-include-plain-lists
  5831. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5832. (cond
  5833. ((integerp arg)
  5834. (show-all)
  5835. (hide-sublevels arg)
  5836. (setq org-cycle-global-status 'contents))
  5837. ((equal arg '(4))
  5838. (org-set-startup-visibility)
  5839. (message "Startup visibility, plus VISIBILITY properties."))
  5840. (t
  5841. (org-cycle '(4))))))
  5842. (defun org-set-startup-visibility ()
  5843. "Set the visibility required by startup options and properties."
  5844. (cond
  5845. ((eq org-startup-folded t)
  5846. (org-cycle '(4)))
  5847. ((eq org-startup-folded 'content)
  5848. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5849. (org-cycle '(4)) (org-cycle '(4)))))
  5850. (unless (eq org-startup-folded 'showeverything)
  5851. (if org-hide-block-startup (org-hide-block-all))
  5852. (org-set-visibility-according-to-property 'no-cleanup)
  5853. (org-cycle-hide-archived-subtrees 'all)
  5854. (org-cycle-hide-drawers 'all)
  5855. (org-cycle-show-empty-lines t)))
  5856. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5857. "Switch subtree visibilities according to :VISIBILITY: property."
  5858. (interactive)
  5859. (let (org-show-entry-below state)
  5860. (save-excursion
  5861. (goto-char (point-min))
  5862. (while (re-search-forward
  5863. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5864. nil t)
  5865. (setq state (match-string 1))
  5866. (save-excursion
  5867. (org-back-to-heading t)
  5868. (hide-subtree)
  5869. (org-reveal)
  5870. (cond
  5871. ((equal state '("fold" "folded"))
  5872. (hide-subtree))
  5873. ((equal state "children")
  5874. (org-show-hidden-entry)
  5875. (show-children))
  5876. ((equal state "content")
  5877. (save-excursion
  5878. (save-restriction
  5879. (org-narrow-to-subtree)
  5880. (org-content))))
  5881. ((member state '("all" "showall"))
  5882. (show-subtree)))))
  5883. (unless no-cleanup
  5884. (org-cycle-hide-archived-subtrees 'all)
  5885. (org-cycle-hide-drawers 'all)
  5886. (org-cycle-show-empty-lines 'all)))))
  5887. ;; This function uses outline-regexp instead of the more fundamental
  5888. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5889. ;; buffers, where outline-regexp is needed.
  5890. (defun org-overview ()
  5891. "Switch to overview mode, showing only top-level headlines.
  5892. Really, this shows all headlines with level equal or greater than the level
  5893. of the first headline in the buffer. This is important, because if the
  5894. first headline is not level one, then (hide-sublevels 1) gives confusing
  5895. results."
  5896. (interactive)
  5897. (let ((level (save-excursion
  5898. (goto-char (point-min))
  5899. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5900. (progn
  5901. (goto-char (match-beginning 0))
  5902. (funcall outline-level))))))
  5903. (and level (hide-sublevels level))))
  5904. (defun org-content (&optional arg)
  5905. "Show all headlines in the buffer, like a table of contents.
  5906. With numerical argument N, show content up to level N."
  5907. (interactive "P")
  5908. (save-excursion
  5909. ;; Visit all headings and show their offspring
  5910. (and (integerp arg) (org-overview))
  5911. (goto-char (point-max))
  5912. (catch 'exit
  5913. (while (and (progn (condition-case nil
  5914. (outline-previous-visible-heading 1)
  5915. (error (goto-char (point-min))))
  5916. t)
  5917. (looking-at org-outline-regexp))
  5918. (if (integerp arg)
  5919. (show-children (1- arg))
  5920. (show-branches))
  5921. (if (bobp) (throw 'exit nil))))))
  5922. (defun org-optimize-window-after-visibility-change (state)
  5923. "Adjust the window after a change in outline visibility.
  5924. This function is the default value of the hook `org-cycle-hook'."
  5925. (when (get-buffer-window (current-buffer))
  5926. (cond
  5927. ((eq state 'content) nil)
  5928. ((eq state 'all) nil)
  5929. ((eq state 'folded) nil)
  5930. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5931. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5932. (defun org-remove-empty-overlays-at (pos)
  5933. "Remove outline overlays that do not contain non-white stuff."
  5934. (mapc
  5935. (lambda (o)
  5936. (and (eq 'outline (overlay-get o 'invisible))
  5937. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5938. (overlay-end o))))
  5939. (delete-overlay o)))
  5940. (overlays-at pos)))
  5941. (defun org-clean-visibility-after-subtree-move ()
  5942. "Fix visibility issues after moving a subtree."
  5943. ;; First, find a reasonable region to look at:
  5944. ;; Start two siblings above, end three below
  5945. (let* ((beg (save-excursion
  5946. (and (org-get-last-sibling)
  5947. (org-get-last-sibling))
  5948. (point)))
  5949. (end (save-excursion
  5950. (and (org-get-next-sibling)
  5951. (org-get-next-sibling)
  5952. (org-get-next-sibling))
  5953. (if (org-at-heading-p)
  5954. (point-at-eol)
  5955. (point))))
  5956. (level (looking-at "\\*+"))
  5957. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5958. (save-excursion
  5959. (save-restriction
  5960. (narrow-to-region beg end)
  5961. (when re
  5962. ;; Properly fold already folded siblings
  5963. (goto-char (point-min))
  5964. (while (re-search-forward re nil t)
  5965. (if (and (not (outline-invisible-p))
  5966. (save-excursion
  5967. (goto-char (point-at-eol)) (outline-invisible-p)))
  5968. (hide-entry))))
  5969. (org-cycle-show-empty-lines 'overview)
  5970. (org-cycle-hide-drawers 'overview)))))
  5971. (defun org-cycle-show-empty-lines (state)
  5972. "Show empty lines above all visible headlines.
  5973. The region to be covered depends on STATE when called through
  5974. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5975. entire buffer covered. Note that an empty line is only shown if there
  5976. are at least `org-cycle-separator-lines' empty lines before the headline."
  5977. (when (not (= org-cycle-separator-lines 0))
  5978. (save-excursion
  5979. (let* ((n (abs org-cycle-separator-lines))
  5980. (re (cond
  5981. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5982. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5983. (t (let ((ns (number-to-string (- n 2))))
  5984. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5985. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5986. beg end b e)
  5987. (cond
  5988. ((memq state '(overview contents t))
  5989. (setq beg (point-min) end (point-max)))
  5990. ((memq state '(children folded))
  5991. (setq beg (point) end (progn (org-end-of-subtree t t)
  5992. (beginning-of-line 2)
  5993. (point)))))
  5994. (when beg
  5995. (goto-char beg)
  5996. (while (re-search-forward re end t)
  5997. (unless (get-char-property (match-end 1) 'invisible)
  5998. (setq e (match-end 1))
  5999. (if (< org-cycle-separator-lines 0)
  6000. (setq b (save-excursion
  6001. (goto-char (match-beginning 0))
  6002. (org-back-over-empty-lines)
  6003. (if (save-excursion
  6004. (goto-char (max (point-min) (1- (point))))
  6005. (org-at-heading-p))
  6006. (1- (point))
  6007. (point))))
  6008. (setq b (match-beginning 1)))
  6009. (outline-flag-region b e nil)))))))
  6010. ;; Never hide empty lines at the end of the file.
  6011. (save-excursion
  6012. (goto-char (point-max))
  6013. (outline-previous-heading)
  6014. (outline-end-of-heading)
  6015. (if (and (looking-at "[ \t\n]+")
  6016. (= (match-end 0) (point-max)))
  6017. (outline-flag-region (point) (match-end 0) nil))))
  6018. (defun org-show-empty-lines-in-parent ()
  6019. "Move to the parent and re-show empty lines before visible headlines."
  6020. (save-excursion
  6021. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6022. (org-cycle-show-empty-lines context))))
  6023. (defun org-files-list ()
  6024. "Return `org-agenda-files' list, plus all open org-mode files.
  6025. This is useful for operations that need to scan all of a user's
  6026. open and agenda-wise Org files."
  6027. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6028. (dolist (buf (buffer-list))
  6029. (with-current-buffer buf
  6030. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6031. (let ((file (expand-file-name (buffer-file-name))))
  6032. (unless (member file files)
  6033. (push file files))))))
  6034. files))
  6035. (defsubst org-entry-beginning-position ()
  6036. "Return the beginning position of the current entry."
  6037. (save-excursion (outline-back-to-heading t) (point)))
  6038. (defsubst org-entry-end-position ()
  6039. "Return the end position of the current entry."
  6040. (save-excursion (outline-next-heading) (point)))
  6041. (defun org-cycle-hide-drawers (state)
  6042. "Re-hide all drawers after a visibility state change."
  6043. (when (and (derived-mode-p 'org-mode)
  6044. (not (memq state '(overview folded contents))))
  6045. (save-excursion
  6046. (let* ((globalp (memq state '(contents all)))
  6047. (beg (if globalp (point-min) (point)))
  6048. (end (if globalp (point-max)
  6049. (if (eq state 'children)
  6050. (save-excursion (outline-next-heading) (point))
  6051. (org-end-of-subtree t)))))
  6052. (goto-char beg)
  6053. (while (re-search-forward org-drawer-regexp end t)
  6054. (org-flag-drawer t))))))
  6055. (defun org-cycle-hide-inline-tasks (state)
  6056. "Re-hide inline task when switching to 'contents visibility state."
  6057. (when (and (eq state 'contents)
  6058. (boundp 'org-inlinetask-min-level)
  6059. org-inlinetask-min-level)
  6060. (hide-sublevels (1- org-inlinetask-min-level))))
  6061. (defun org-flag-drawer (flag)
  6062. "When FLAG is non-nil, hide the drawer we are within.
  6063. Otherwise make it visible."
  6064. (save-excursion
  6065. (beginning-of-line 1)
  6066. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6067. (let ((b (match-end 0)))
  6068. (if (re-search-forward
  6069. "^[ \t]*:END:"
  6070. (save-excursion (outline-next-heading) (point)) t)
  6071. (outline-flag-region b (point-at-eol) flag)
  6072. (error ":END: line missing at position %s" b))))))
  6073. (defun org-subtree-end-visible-p ()
  6074. "Is the end of the current subtree visible?"
  6075. (pos-visible-in-window-p
  6076. (save-excursion (org-end-of-subtree t) (point))))
  6077. (defun org-first-headline-recenter (&optional N)
  6078. "Move cursor to the first headline and recenter the headline.
  6079. Optional argument N means put the headline into the Nth line of the window."
  6080. (goto-char (point-min))
  6081. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6082. (beginning-of-line)
  6083. (recenter (prefix-numeric-value N))))
  6084. ;;; Saving and restoring visibility
  6085. (defun org-outline-overlay-data (&optional use-markers)
  6086. "Return a list of the locations of all outline overlays.
  6087. These are overlays with the `invisible' property value `outline'.
  6088. The return value is a list of cons cells, with start and stop
  6089. positions for each overlay.
  6090. If USE-MARKERS is set, return the positions as markers."
  6091. (let (beg end)
  6092. (save-excursion
  6093. (save-restriction
  6094. (widen)
  6095. (delq nil
  6096. (mapcar (lambda (o)
  6097. (when (eq (overlay-get o 'invisible) 'outline)
  6098. (setq beg (overlay-start o)
  6099. end (overlay-end o))
  6100. (and beg end (> end beg)
  6101. (if use-markers
  6102. (cons (move-marker (make-marker) beg)
  6103. (move-marker (make-marker) end))
  6104. (cons beg end)))))
  6105. (overlays-in (point-min) (point-max))))))))
  6106. (defun org-set-outline-overlay-data (data)
  6107. "Create visibility overlays for all positions in DATA.
  6108. DATA should have been made by `org-outline-overlay-data'."
  6109. (let (o)
  6110. (save-excursion
  6111. (save-restriction
  6112. (widen)
  6113. (show-all)
  6114. (mapc (lambda (c)
  6115. (outline-flag-region (car c) (cdr c) t))
  6116. data)))))
  6117. ;;; Folding of blocks
  6118. (defvar org-hide-block-overlays nil
  6119. "Overlays hiding blocks.")
  6120. (make-variable-buffer-local 'org-hide-block-overlays)
  6121. (defun org-block-map (function &optional start end)
  6122. "Call FUNCTION at the head of all source blocks in the current buffer.
  6123. Optional arguments START and END can be used to limit the range."
  6124. (let ((start (or start (point-min)))
  6125. (end (or end (point-max))))
  6126. (save-excursion
  6127. (goto-char start)
  6128. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6129. (save-excursion
  6130. (save-match-data
  6131. (goto-char (match-beginning 0))
  6132. (funcall function)))))))
  6133. (defun org-hide-block-toggle-all ()
  6134. "Toggle the visibility of all blocks in the current buffer."
  6135. (org-block-map #'org-hide-block-toggle))
  6136. (defun org-hide-block-all ()
  6137. "Fold all blocks in the current buffer."
  6138. (interactive)
  6139. (org-show-block-all)
  6140. (org-block-map #'org-hide-block-toggle-maybe))
  6141. (defun org-show-block-all ()
  6142. "Unfold all blocks in the current buffer."
  6143. (interactive)
  6144. (mapc 'delete-overlay org-hide-block-overlays)
  6145. (setq org-hide-block-overlays nil))
  6146. (defun org-hide-block-toggle-maybe ()
  6147. "Toggle visibility of block at point."
  6148. (interactive)
  6149. (let ((case-fold-search t))
  6150. (if (save-excursion
  6151. (beginning-of-line 1)
  6152. (looking-at org-block-regexp))
  6153. (progn (org-hide-block-toggle)
  6154. t) ;; to signal that we took action
  6155. nil))) ;; to signal that we did not
  6156. (defun org-hide-block-toggle (&optional force)
  6157. "Toggle the visibility of the current block."
  6158. (interactive)
  6159. (save-excursion
  6160. (beginning-of-line)
  6161. (if (re-search-forward org-block-regexp nil t)
  6162. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6163. (end (match-end 0)) ;; end of entire body
  6164. ov)
  6165. (if (memq t (mapcar (lambda (overlay)
  6166. (eq (overlay-get overlay 'invisible)
  6167. 'org-hide-block))
  6168. (overlays-at start)))
  6169. (if (or (not force) (eq force 'off))
  6170. (mapc (lambda (ov)
  6171. (when (member ov org-hide-block-overlays)
  6172. (setq org-hide-block-overlays
  6173. (delq ov org-hide-block-overlays)))
  6174. (when (eq (overlay-get ov 'invisible)
  6175. 'org-hide-block)
  6176. (delete-overlay ov)))
  6177. (overlays-at start)))
  6178. (setq ov (make-overlay start end))
  6179. (overlay-put ov 'invisible 'org-hide-block)
  6180. ;; make the block accessible to isearch
  6181. (overlay-put
  6182. ov 'isearch-open-invisible
  6183. (lambda (ov)
  6184. (when (member ov org-hide-block-overlays)
  6185. (setq org-hide-block-overlays
  6186. (delq ov org-hide-block-overlays)))
  6187. (when (eq (overlay-get ov 'invisible)
  6188. 'org-hide-block)
  6189. (delete-overlay ov))))
  6190. (push ov org-hide-block-overlays)))
  6191. (error "Not looking at a source block"))))
  6192. ;; org-tab-after-check-for-cycling-hook
  6193. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6194. ;; Remove overlays when changing major mode
  6195. (add-hook 'org-mode-hook
  6196. (lambda () (org-add-hook 'change-major-mode-hook
  6197. 'org-show-block-all 'append 'local)))
  6198. ;;; Org-goto
  6199. (defvar org-goto-window-configuration nil)
  6200. (defvar org-goto-marker nil)
  6201. (defvar org-goto-map)
  6202. (defun org-goto-map ()
  6203. "Set the keymap `org-goto'."
  6204. (setq org-goto-map
  6205. (let ((map (make-sparse-keymap)))
  6206. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6207. mouse-drag-region universal-argument org-occur))
  6208. cmd)
  6209. (while (setq cmd (pop cmds))
  6210. (substitute-key-definition cmd cmd map global-map)))
  6211. (suppress-keymap map)
  6212. (org-defkey map "\C-m" 'org-goto-ret)
  6213. (org-defkey map [(return)] 'org-goto-ret)
  6214. (org-defkey map [(left)] 'org-goto-left)
  6215. (org-defkey map [(right)] 'org-goto-right)
  6216. (org-defkey map [(control ?g)] 'org-goto-quit)
  6217. (org-defkey map "\C-i" 'org-cycle)
  6218. (org-defkey map [(tab)] 'org-cycle)
  6219. (org-defkey map [(down)] 'outline-next-visible-heading)
  6220. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6221. (if org-goto-auto-isearch
  6222. (if (fboundp 'define-key-after)
  6223. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6224. nil)
  6225. (org-defkey map "q" 'org-goto-quit)
  6226. (org-defkey map "n" 'outline-next-visible-heading)
  6227. (org-defkey map "p" 'outline-previous-visible-heading)
  6228. (org-defkey map "f" 'outline-forward-same-level)
  6229. (org-defkey map "b" 'outline-backward-same-level)
  6230. (org-defkey map "u" 'outline-up-heading))
  6231. (org-defkey map "/" 'org-occur)
  6232. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6233. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6234. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6235. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6236. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6237. map)))
  6238. (defconst org-goto-help
  6239. "Browse buffer copy, to find location or copy text.%s
  6240. RET=jump to location C-g=quit and return to previous location
  6241. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6242. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6243. ;; FIXME: Docstring does not mention both interfaces
  6244. (defun org-goto (&optional alternative-interface)
  6245. "Look up a different location in the current file, keeping current visibility.
  6246. When you want look-up or go to a different location in a
  6247. document, the fastest way is often to fold the entire buffer and
  6248. then dive into the tree. This method has the disadvantage, that
  6249. the previous location will be folded, which may not be what you
  6250. want.
  6251. This command works around this by showing a copy of the current
  6252. buffer in an indirect buffer, in overview mode. You can dive
  6253. into the tree in that copy, use org-occur and incremental search
  6254. to find a location. When pressing RET or `Q', the command
  6255. returns to the original buffer in which the visibility is still
  6256. unchanged. After RET it will also jump to the location selected
  6257. in the indirect buffer and expose the headline hierarchy above.
  6258. With a prefix argument, use the alternative interface: e.g. if
  6259. `org-goto-interface' is 'outline use 'outline-path-completion."
  6260. (interactive "P")
  6261. (org-goto-map)
  6262. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6263. (org-refile-use-outline-path t)
  6264. (org-refile-target-verify-function nil)
  6265. (interface
  6266. (if (not alternative-interface)
  6267. org-goto-interface
  6268. (if (eq org-goto-interface 'outline)
  6269. 'outline-path-completion
  6270. 'outline)))
  6271. (org-goto-start-pos (point))
  6272. (selected-point
  6273. (if (eq interface 'outline)
  6274. (car (org-get-location (current-buffer) org-goto-help))
  6275. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6276. (org-refile-check-position pa)
  6277. (nth 3 pa)))))
  6278. (if selected-point
  6279. (progn
  6280. (org-mark-ring-push org-goto-start-pos)
  6281. (goto-char selected-point)
  6282. (if (or (outline-invisible-p) (org-invisible-p2))
  6283. (org-show-context 'org-goto)))
  6284. (message "Quit"))))
  6285. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6286. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6287. (defvar org-goto-local-auto-isearch-map) ; defined below
  6288. (defun org-get-location (buf help)
  6289. "Let the user select a location in the Org-mode buffer BUF.
  6290. This function uses a recursive edit. It returns the selected position
  6291. or nil."
  6292. (org-no-popups
  6293. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6294. (isearch-hide-immediately nil)
  6295. (isearch-search-fun-function
  6296. (lambda () 'org-goto-local-search-headings))
  6297. (org-goto-selected-point org-goto-exit-command))
  6298. (save-excursion
  6299. (save-window-excursion
  6300. (delete-other-windows)
  6301. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6302. (org-pop-to-buffer-same-window
  6303. (condition-case nil
  6304. (make-indirect-buffer (current-buffer) "*org-goto*")
  6305. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6306. (with-output-to-temp-buffer "*Org Help*"
  6307. (princ (format help (if org-goto-auto-isearch
  6308. " Just type for auto-isearch."
  6309. " n/p/f/b/u to navigate, q to quit."))))
  6310. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6311. (setq buffer-read-only nil)
  6312. (let ((org-startup-truncated t)
  6313. (org-startup-folded nil)
  6314. (org-startup-align-all-tables nil))
  6315. (org-mode)
  6316. (org-overview))
  6317. (setq buffer-read-only t)
  6318. (if (and (boundp 'org-goto-start-pos)
  6319. (integer-or-marker-p org-goto-start-pos))
  6320. (let ((org-show-hierarchy-above t)
  6321. (org-show-siblings t)
  6322. (org-show-following-heading t))
  6323. (goto-char org-goto-start-pos)
  6324. (and (outline-invisible-p) (org-show-context)))
  6325. (goto-char (point-min)))
  6326. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6327. (message "Select location and press RET")
  6328. (use-local-map org-goto-map)
  6329. (recursive-edit)))
  6330. (kill-buffer "*org-goto*")
  6331. (cons org-goto-selected-point org-goto-exit-command))))
  6332. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6333. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6334. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6335. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6336. (defun org-goto-local-search-headings (string bound noerror)
  6337. "Search and make sure that any matches are in headlines."
  6338. (catch 'return
  6339. (while (if isearch-forward
  6340. (search-forward string bound noerror)
  6341. (search-backward string bound noerror))
  6342. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6343. (and (member :headline context)
  6344. (not (member :tags context))))
  6345. (throw 'return (point))))))
  6346. (defun org-goto-local-auto-isearch ()
  6347. "Start isearch."
  6348. (interactive)
  6349. (goto-char (point-min))
  6350. (let ((keys (this-command-keys)))
  6351. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6352. (isearch-mode t)
  6353. (isearch-process-search-char (string-to-char keys)))))
  6354. (defun org-goto-ret (&optional arg)
  6355. "Finish `org-goto' by going to the new location."
  6356. (interactive "P")
  6357. (setq org-goto-selected-point (point)
  6358. org-goto-exit-command 'return)
  6359. (throw 'exit nil))
  6360. (defun org-goto-left ()
  6361. "Finish `org-goto' by going to the new location."
  6362. (interactive)
  6363. (if (org-at-heading-p)
  6364. (progn
  6365. (beginning-of-line 1)
  6366. (setq org-goto-selected-point (point)
  6367. org-goto-exit-command 'left)
  6368. (throw 'exit nil))
  6369. (error "Not on a heading")))
  6370. (defun org-goto-right ()
  6371. "Finish `org-goto' by going to the new location."
  6372. (interactive)
  6373. (if (org-at-heading-p)
  6374. (progn
  6375. (setq org-goto-selected-point (point)
  6376. org-goto-exit-command 'right)
  6377. (throw 'exit nil))
  6378. (error "Not on a heading")))
  6379. (defun org-goto-quit ()
  6380. "Finish `org-goto' without cursor motion."
  6381. (interactive)
  6382. (setq org-goto-selected-point nil)
  6383. (setq org-goto-exit-command 'quit)
  6384. (throw 'exit nil))
  6385. ;;; Indirect buffer display of subtrees
  6386. (defvar org-indirect-dedicated-frame nil
  6387. "This is the frame being used for indirect tree display.")
  6388. (defvar org-last-indirect-buffer nil)
  6389. (defun org-tree-to-indirect-buffer (&optional arg)
  6390. "Create indirect buffer and narrow it to current subtree.
  6391. With a numerical prefix ARG, go up to this level and then take that tree.
  6392. If ARG is negative, go up that many levels.
  6393. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6394. indirect buffer previously made with this command, to avoid proliferation of
  6395. indirect buffers. However, when you call the command with a \
  6396. \\[universal-argument] prefix, or
  6397. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6398. is kept so that you can work with several indirect buffers at the same time.
  6399. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6400. \\[universal-argument] prefix also
  6401. requests that a new frame be made for the new buffer, so that the dedicated
  6402. frame is not changed."
  6403. (interactive "P")
  6404. (let ((cbuf (current-buffer))
  6405. (cwin (selected-window))
  6406. (pos (point))
  6407. beg end level heading ibuf)
  6408. (save-excursion
  6409. (org-back-to-heading t)
  6410. (when (numberp arg)
  6411. (setq level (org-outline-level))
  6412. (if (< arg 0) (setq arg (+ level arg)))
  6413. (while (> (setq level (org-outline-level)) arg)
  6414. (org-up-heading-safe)))
  6415. (setq beg (point)
  6416. heading (org-get-heading))
  6417. (org-end-of-subtree t t)
  6418. (if (org-at-heading-p) (backward-char 1))
  6419. (setq end (point)))
  6420. (if (and (buffer-live-p org-last-indirect-buffer)
  6421. (not (eq org-indirect-buffer-display 'new-frame))
  6422. (not arg))
  6423. (kill-buffer org-last-indirect-buffer))
  6424. (setq ibuf (org-get-indirect-buffer cbuf)
  6425. org-last-indirect-buffer ibuf)
  6426. (cond
  6427. ((or (eq org-indirect-buffer-display 'new-frame)
  6428. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6429. (select-frame (make-frame))
  6430. (delete-other-windows)
  6431. (org-pop-to-buffer-same-window ibuf)
  6432. (org-set-frame-title heading))
  6433. ((eq org-indirect-buffer-display 'dedicated-frame)
  6434. (raise-frame
  6435. (select-frame (or (and org-indirect-dedicated-frame
  6436. (frame-live-p org-indirect-dedicated-frame)
  6437. org-indirect-dedicated-frame)
  6438. (setq org-indirect-dedicated-frame (make-frame)))))
  6439. (delete-other-windows)
  6440. (org-pop-to-buffer-same-window ibuf)
  6441. (org-set-frame-title (concat "Indirect: " heading)))
  6442. ((eq org-indirect-buffer-display 'current-window)
  6443. (org-pop-to-buffer-same-window ibuf))
  6444. ((eq org-indirect-buffer-display 'other-window)
  6445. (pop-to-buffer ibuf))
  6446. (t (error "Invalid value")))
  6447. (if (featurep 'xemacs)
  6448. (save-excursion (org-mode) (turn-on-font-lock)))
  6449. (narrow-to-region beg end)
  6450. (show-all)
  6451. (goto-char pos)
  6452. (run-hook-with-args 'org-cycle-hook 'all)
  6453. (and (window-live-p cwin) (select-window cwin))))
  6454. (defun org-get-indirect-buffer (&optional buffer)
  6455. (setq buffer (or buffer (current-buffer)))
  6456. (let ((n 1) (base (buffer-name buffer)) bname)
  6457. (while (buffer-live-p
  6458. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6459. (setq n (1+ n)))
  6460. (condition-case nil
  6461. (make-indirect-buffer buffer bname 'clone)
  6462. (error (make-indirect-buffer buffer bname)))))
  6463. (defun org-set-frame-title (title)
  6464. "Set the title of the current frame to the string TITLE."
  6465. ;; FIXME: how to name a single frame in XEmacs???
  6466. (unless (featurep 'xemacs)
  6467. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6468. ;;;; Structure editing
  6469. ;;; Inserting headlines
  6470. (defun org-previous-line-empty-p ()
  6471. (save-excursion
  6472. (and (not (bobp))
  6473. (or (beginning-of-line 0) t)
  6474. (save-match-data
  6475. (looking-at "[ \t]*$")))))
  6476. (defun org-insert-heading (&optional force-heading invisible-ok)
  6477. "Insert a new heading or item with same depth at point.
  6478. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6479. If point is at the beginning of a headline, insert a sibling before the
  6480. current headline. If point is not at the beginning, split the line,
  6481. create the new headline with the text in the current line after point
  6482. \(but see also the variable `org-M-RET-may-split-line').
  6483. With a double prefix arg, force the heading to be inserted at the
  6484. end of the parent subtree.
  6485. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6486. This is important for non-interactive uses of the command."
  6487. (interactive "P")
  6488. (if (or (= (buffer-size) 0)
  6489. (and (not (save-excursion
  6490. (and (ignore-errors (org-back-to-heading invisible-ok))
  6491. (org-at-heading-p))))
  6492. (or force-heading (not (org-in-item-p)))))
  6493. (progn
  6494. (insert "\n* ")
  6495. (run-hooks 'org-insert-heading-hook))
  6496. (when (or force-heading (not (org-insert-item)))
  6497. (let* ((empty-line-p nil)
  6498. (level nil)
  6499. (on-heading (org-at-heading-p))
  6500. (head (save-excursion
  6501. (condition-case nil
  6502. (progn
  6503. (org-back-to-heading invisible-ok)
  6504. (when (and (not on-heading)
  6505. (featurep 'org-inlinetask)
  6506. (integerp org-inlinetask-min-level)
  6507. (>= (length (match-string 0))
  6508. org-inlinetask-min-level))
  6509. ;; Find a heading level before the inline task
  6510. (while (and (setq level (org-up-heading-safe))
  6511. (>= level org-inlinetask-min-level)))
  6512. (if (org-at-heading-p)
  6513. (org-back-to-heading invisible-ok)
  6514. (error "This should not happen")))
  6515. (setq empty-line-p (org-previous-line-empty-p))
  6516. (match-string 0))
  6517. (error "*"))))
  6518. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6519. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6520. pos hide-previous previous-pos)
  6521. (cond
  6522. ((and (org-at-heading-p) (bolp)
  6523. (or (bobp)
  6524. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6525. ;; insert before the current line
  6526. (open-line (if blank 2 1)))
  6527. ((and (bolp)
  6528. (not org-insert-heading-respect-content)
  6529. (or (bobp)
  6530. (save-excursion
  6531. (backward-char 1) (not (outline-invisible-p)))))
  6532. ;; insert right here
  6533. nil)
  6534. (t
  6535. ;; somewhere in the line
  6536. (save-excursion
  6537. (setq previous-pos (point-at-bol))
  6538. (end-of-line)
  6539. (setq hide-previous (outline-invisible-p)))
  6540. (and org-insert-heading-respect-content (org-show-subtree))
  6541. (let ((split
  6542. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6543. (save-excursion
  6544. (let ((p (point)))
  6545. (goto-char (point-at-bol))
  6546. (and (looking-at org-complex-heading-regexp)
  6547. (match-beginning 4)
  6548. (> p (match-beginning 4)))))))
  6549. tags pos)
  6550. (cond
  6551. (org-insert-heading-respect-content
  6552. (if (not (equal force-heading '(16)))
  6553. (org-end-of-subtree nil t)
  6554. (org-up-heading-safe)
  6555. (org-end-of-subtree nil t))
  6556. (when (featurep 'org-inlinetask)
  6557. (while (and (not (eobp))
  6558. (looking-at "\\(\\*+\\)[ \t]+")
  6559. (>= (length (match-string 1))
  6560. org-inlinetask-min-level))
  6561. (org-end-of-subtree nil t)))
  6562. (or (bolp) (newline))
  6563. (or (org-previous-line-empty-p)
  6564. (and blank (newline)))
  6565. (open-line 1))
  6566. ((org-at-heading-p)
  6567. (when hide-previous
  6568. (show-children)
  6569. (org-show-entry))
  6570. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6571. (setq tags (and (match-end 2) (match-string 2)))
  6572. (and (match-end 1)
  6573. (delete-region (match-beginning 1) (match-end 1)))
  6574. (setq pos (point-at-bol))
  6575. (or split (end-of-line 1))
  6576. (delete-horizontal-space)
  6577. (if (string-match "\\`\\*+\\'"
  6578. (buffer-substring (point-at-bol) (point)))
  6579. (insert " "))
  6580. (newline (if blank 2 1))
  6581. (when tags
  6582. (save-excursion
  6583. (goto-char pos)
  6584. (end-of-line 1)
  6585. (insert " " tags)
  6586. (org-set-tags nil 'align))))
  6587. (t
  6588. (or split (end-of-line 1))
  6589. (newline (if blank 2 1)))))))
  6590. (insert head) (just-one-space)
  6591. (setq pos (point))
  6592. (end-of-line 1)
  6593. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6594. (when (and org-insert-heading-respect-content hide-previous)
  6595. (save-excursion
  6596. (goto-char previous-pos)
  6597. (hide-subtree)))
  6598. (run-hooks 'org-insert-heading-hook)))))
  6599. (defun org-get-heading (&optional no-tags no-todo)
  6600. "Return the heading of the current entry, without the stars.
  6601. When NO-TAGS is non-nil, don't include tags.
  6602. When NO-TODO is non-nil, don't include TODO keywords."
  6603. (save-excursion
  6604. (org-back-to-heading t)
  6605. (cond
  6606. ((and no-tags no-todo)
  6607. (looking-at org-complex-heading-regexp)
  6608. (match-string 4))
  6609. (no-tags
  6610. (looking-at (concat org-outline-regexp
  6611. "\\(.*?\\)"
  6612. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6613. (match-string 1))
  6614. (no-todo
  6615. (looking-at org-todo-line-regexp)
  6616. (match-string 3))
  6617. (t (looking-at org-heading-regexp)
  6618. (match-string 2)))))
  6619. (defun org-heading-components ()
  6620. "Return the components of the current heading.
  6621. This is a list with the following elements:
  6622. - the level as an integer
  6623. - the reduced level, different if `org-odd-levels-only' is set.
  6624. - the TODO keyword, or nil
  6625. - the priority character, like ?A, or nil if no priority is given
  6626. - the headline text itself, or the tags string if no headline text
  6627. - the tags string, or nil."
  6628. (save-excursion
  6629. (org-back-to-heading t)
  6630. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6631. (list (length (match-string 1))
  6632. (org-reduced-level (length (match-string 1)))
  6633. (org-match-string-no-properties 2)
  6634. (and (match-end 3) (aref (match-string 3) 2))
  6635. (org-match-string-no-properties 4)
  6636. (org-match-string-no-properties 5)))))
  6637. (defun org-get-entry ()
  6638. "Get the entry text, after heading, entire subtree."
  6639. (save-excursion
  6640. (org-back-to-heading t)
  6641. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6642. (defun org-insert-heading-after-current ()
  6643. "Insert a new heading with same level as current, after current subtree."
  6644. (interactive)
  6645. (org-back-to-heading)
  6646. (org-insert-heading)
  6647. (org-move-subtree-down)
  6648. (end-of-line 1))
  6649. (defun org-insert-heading-respect-content ()
  6650. (interactive)
  6651. (let ((org-insert-heading-respect-content t))
  6652. (org-insert-heading t)))
  6653. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6654. (interactive "P")
  6655. (let ((org-insert-heading-respect-content t))
  6656. (org-insert-todo-heading force-state t)))
  6657. (defun org-insert-todo-heading (arg &optional force-heading)
  6658. "Insert a new heading with the same level and TODO state as current heading.
  6659. If the heading has no TODO state, or if the state is DONE, use the first
  6660. state (TODO by default). Also one prefix arg, force first state. With two
  6661. prefix args, force inserting at the end of the parent subtree."
  6662. (interactive "P")
  6663. (when (or force-heading (not (org-insert-item 'checkbox)))
  6664. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6665. force-heading))
  6666. (save-excursion
  6667. (org-back-to-heading)
  6668. (outline-previous-heading)
  6669. (looking-at org-todo-line-regexp))
  6670. (let*
  6671. ((new-mark-x
  6672. (if (or arg
  6673. (not (match-beginning 2))
  6674. (member (match-string 2) org-done-keywords))
  6675. (car org-todo-keywords-1)
  6676. (match-string 2)))
  6677. (new-mark
  6678. (or
  6679. (run-hook-with-args-until-success
  6680. 'org-todo-get-default-hook new-mark-x nil)
  6681. new-mark-x)))
  6682. (beginning-of-line 1)
  6683. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6684. (if org-treat-insert-todo-heading-as-state-change
  6685. (org-todo new-mark)
  6686. (insert new-mark " "))))
  6687. (when org-provide-todo-statistics
  6688. (org-update-parent-todo-statistics))))
  6689. (defun org-insert-subheading (arg)
  6690. "Insert a new subheading and demote it.
  6691. Works for outline headings and for plain lists alike."
  6692. (interactive "P")
  6693. (org-insert-heading arg)
  6694. (cond
  6695. ((org-at-heading-p) (org-do-demote))
  6696. ((org-at-item-p) (org-indent-item))))
  6697. (defun org-insert-todo-subheading (arg)
  6698. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6699. Works for outline headings and for plain lists alike."
  6700. (interactive "P")
  6701. (org-insert-todo-heading arg)
  6702. (cond
  6703. ((org-at-heading-p) (org-do-demote))
  6704. ((org-at-item-p) (org-indent-item))))
  6705. ;;; Promotion and Demotion
  6706. (defvar org-after-demote-entry-hook nil
  6707. "Hook run after an entry has been demoted.
  6708. The cursor will be at the beginning of the entry.
  6709. When a subtree is being demoted, the hook will be called for each node.")
  6710. (defvar org-after-promote-entry-hook nil
  6711. "Hook run after an entry has been promoted.
  6712. The cursor will be at the beginning of the entry.
  6713. When a subtree is being promoted, the hook will be called for each node.")
  6714. (defun org-promote-subtree ()
  6715. "Promote the entire subtree.
  6716. See also `org-promote'."
  6717. (interactive)
  6718. (save-excursion
  6719. (org-with-limited-levels (org-map-tree 'org-promote)))
  6720. (org-fix-position-after-promote))
  6721. (defun org-demote-subtree ()
  6722. "Demote the entire subtree. See `org-demote'.
  6723. See also `org-promote'."
  6724. (interactive)
  6725. (save-excursion
  6726. (org-with-limited-levels (org-map-tree 'org-demote)))
  6727. (org-fix-position-after-promote))
  6728. (defun org-do-promote ()
  6729. "Promote the current heading higher up the tree.
  6730. If the region is active in `transient-mark-mode', promote all headings
  6731. in the region."
  6732. (interactive)
  6733. (save-excursion
  6734. (if (org-region-active-p)
  6735. (org-map-region 'org-promote (region-beginning) (region-end))
  6736. (org-promote)))
  6737. (org-fix-position-after-promote))
  6738. (defun org-do-demote ()
  6739. "Demote the current heading lower down the tree.
  6740. If the region is active in `transient-mark-mode', demote all headings
  6741. in the region."
  6742. (interactive)
  6743. (save-excursion
  6744. (if (org-region-active-p)
  6745. (org-map-region 'org-demote (region-beginning) (region-end))
  6746. (org-demote)))
  6747. (org-fix-position-after-promote))
  6748. (defun org-fix-position-after-promote ()
  6749. "Make sure that after pro/demotion cursor position is right."
  6750. (let ((pos (point)))
  6751. (when (save-excursion
  6752. (beginning-of-line 1)
  6753. (looking-at org-todo-line-regexp)
  6754. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6755. (cond ((eobp) (insert " "))
  6756. ((eolp) (insert " "))
  6757. ((equal (char-after) ?\ ) (forward-char 1))))))
  6758. (defun org-current-level ()
  6759. "Return the level of the current entry, or nil if before the first headline.
  6760. The level is the number of stars at the beginning of the headline."
  6761. (save-excursion
  6762. (org-with-limited-levels
  6763. (if (ignore-errors (org-back-to-heading t))
  6764. (funcall outline-level)))))
  6765. (defun org-get-previous-line-level ()
  6766. "Return the outline depth of the last headline before the current line.
  6767. Returns 0 for the first headline in the buffer, and nil if before the
  6768. first headline."
  6769. (let ((current-level (org-current-level))
  6770. (prev-level (when (> (line-number-at-pos) 1)
  6771. (save-excursion
  6772. (beginning-of-line 0)
  6773. (org-current-level)))))
  6774. (cond ((null current-level) nil) ; Before first headline
  6775. ((null prev-level) 0) ; At first headline
  6776. (prev-level))))
  6777. (defun org-reduced-level (l)
  6778. "Compute the effective level of a heading.
  6779. This takes into account the setting of `org-odd-levels-only'."
  6780. (cond
  6781. ((zerop l) 0)
  6782. (org-odd-levels-only (1+ (floor (/ l 2))))
  6783. (t l)))
  6784. (defun org-level-increment ()
  6785. "Return the number of stars that will be added or removed at a
  6786. time to headlines when structure editing, based on the value of
  6787. `org-odd-levels-only'."
  6788. (if org-odd-levels-only 2 1))
  6789. (defun org-get-valid-level (level &optional change)
  6790. "Rectify a level change under the influence of `org-odd-levels-only'
  6791. LEVEL is a current level, CHANGE is by how much the level should be
  6792. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6793. even level numbers will become the next higher odd number."
  6794. (if org-odd-levels-only
  6795. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6796. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6797. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6798. (max 1 (+ level (or change 0)))))
  6799. (if (boundp 'define-obsolete-function-alias)
  6800. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6801. (define-obsolete-function-alias 'org-get-legal-level
  6802. 'org-get-valid-level)
  6803. (define-obsolete-function-alias 'org-get-legal-level
  6804. 'org-get-valid-level "23.1")))
  6805. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6806. ;; ̀org-with-limited-levels'
  6807. (defun org-promote ()
  6808. "Promote the current heading higher up the tree.
  6809. If the region is active in `transient-mark-mode', promote all headings
  6810. in the region."
  6811. (org-back-to-heading t)
  6812. (let* ((level (save-match-data (funcall outline-level)))
  6813. (after-change-functions (remove 'flyspell-after-change-function
  6814. after-change-functions))
  6815. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6816. (diff (abs (- level (length up-head) -1))))
  6817. (cond ((and (= level 1) org-called-with-limited-levels
  6818. org-allow-promoting-top-level-subtree)
  6819. (replace-match "# " nil t))
  6820. ((= level 1)
  6821. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6822. (t (replace-match up-head nil t)))
  6823. ;; Fixup tag positioning
  6824. (unless (= level 1)
  6825. (and org-auto-align-tags (org-set-tags nil t))
  6826. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6827. (run-hooks 'org-after-promote-entry-hook)))
  6828. (defun org-demote ()
  6829. "Demote the current heading lower down the tree.
  6830. If the region is active in `transient-mark-mode', demote all headings
  6831. in the region."
  6832. (org-back-to-heading t)
  6833. (let* ((level (save-match-data (funcall outline-level)))
  6834. (after-change-functions (remove 'flyspell-after-change-function
  6835. after-change-functions))
  6836. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6837. (diff (abs (- level (length down-head) -1))))
  6838. (replace-match down-head nil t)
  6839. ;; Fixup tag positioning
  6840. (and org-auto-align-tags (org-set-tags nil t))
  6841. (if org-adapt-indentation (org-fixup-indentation diff))
  6842. (run-hooks 'org-after-demote-entry-hook)))
  6843. (defun org-cycle-level ()
  6844. "Cycle the level of an empty headline through possible states.
  6845. This goes first to child, then to parent, level, then up the hierarchy.
  6846. After top level, it switches back to sibling level."
  6847. (interactive)
  6848. (let ((org-adapt-indentation nil))
  6849. (when (org-point-at-end-of-empty-headline)
  6850. (setq this-command 'org-cycle-level) ; Only needed for caching
  6851. (let ((cur-level (org-current-level))
  6852. (prev-level (org-get-previous-line-level)))
  6853. (cond
  6854. ;; If first headline in file, promote to top-level.
  6855. ((= prev-level 0)
  6856. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6857. do (org-do-promote)))
  6858. ;; If same level as prev, demote one.
  6859. ((= prev-level cur-level)
  6860. (org-do-demote))
  6861. ;; If parent is top-level, promote to top level if not already.
  6862. ((= prev-level 1)
  6863. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6864. do (org-do-promote)))
  6865. ;; If top-level, return to prev-level.
  6866. ((= cur-level 1)
  6867. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6868. do (org-do-demote)))
  6869. ;; If less than prev-level, promote one.
  6870. ((< cur-level prev-level)
  6871. (org-do-promote))
  6872. ;; If deeper than prev-level, promote until higher than
  6873. ;; prev-level.
  6874. ((> cur-level prev-level)
  6875. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6876. do (org-do-promote))))
  6877. t))))
  6878. (defun org-map-tree (fun)
  6879. "Call FUN for every heading underneath the current one."
  6880. (org-back-to-heading)
  6881. (let ((level (funcall outline-level)))
  6882. (save-excursion
  6883. (funcall fun)
  6884. (while (and (progn
  6885. (outline-next-heading)
  6886. (> (funcall outline-level) level))
  6887. (not (eobp)))
  6888. (funcall fun)))))
  6889. (defun org-map-region (fun beg end)
  6890. "Call FUN for every heading between BEG and END."
  6891. (let ((org-ignore-region t))
  6892. (save-excursion
  6893. (setq end (copy-marker end))
  6894. (goto-char beg)
  6895. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6896. (< (point) end))
  6897. (funcall fun))
  6898. (while (and (progn
  6899. (outline-next-heading)
  6900. (< (point) end))
  6901. (not (eobp)))
  6902. (funcall fun)))))
  6903. (defvar org-property-end-re) ; silence byte-compiler
  6904. (defun org-fixup-indentation (diff)
  6905. "Change the indentation in the current entry by DIFF.
  6906. However, if any line in the current entry has no indentation, or if it
  6907. would end up with no indentation after the change, nothing at all is done."
  6908. (save-excursion
  6909. (let ((end (save-excursion (outline-next-heading)
  6910. (point-marker)))
  6911. (prohibit (if (> diff 0)
  6912. "^\\S-"
  6913. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6914. col)
  6915. (unless (save-excursion (end-of-line 1)
  6916. (re-search-forward prohibit end t))
  6917. (while (and (< (point) end)
  6918. (re-search-forward "^[ \t]+" end t))
  6919. (goto-char (match-end 0))
  6920. (setq col (current-column))
  6921. (if (< diff 0) (replace-match ""))
  6922. (org-indent-to-column (+ diff col))))
  6923. (move-marker end nil))))
  6924. (defun org-convert-to-odd-levels ()
  6925. "Convert an org-mode file with all levels allowed to one with odd levels.
  6926. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6927. level 5 etc."
  6928. (interactive)
  6929. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6930. (let ((outline-level 'org-outline-level)
  6931. (org-odd-levels-only nil) n)
  6932. (save-excursion
  6933. (goto-char (point-min))
  6934. (while (re-search-forward "^\\*\\*+ " nil t)
  6935. (setq n (- (length (match-string 0)) 2))
  6936. (while (>= (setq n (1- n)) 0)
  6937. (org-demote))
  6938. (end-of-line 1))))))
  6939. (defun org-convert-to-oddeven-levels ()
  6940. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6941. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6942. file contains a section with an even level, conversion would
  6943. destroy the structure of the file. An error is signaled in this
  6944. case."
  6945. (interactive)
  6946. (goto-char (point-min))
  6947. ;; First check if there are no even levels
  6948. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6949. (org-show-context t)
  6950. (error "Not all levels are odd in this file. Conversion not possible"))
  6951. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6952. (let ((outline-regexp org-outline-regexp)
  6953. (outline-level 'org-outline-level)
  6954. (org-odd-levels-only nil) n)
  6955. (save-excursion
  6956. (goto-char (point-min))
  6957. (while (re-search-forward "^\\*\\*+ " nil t)
  6958. (setq n (/ (1- (length (match-string 0))) 2))
  6959. (while (>= (setq n (1- n)) 0)
  6960. (org-promote))
  6961. (end-of-line 1))))))
  6962. (defun org-tr-level (n)
  6963. "Make N odd if required."
  6964. (if org-odd-levels-only (1+ (/ n 2)) n))
  6965. ;;; Vertical tree motion, cutting and pasting of subtrees
  6966. (defun org-move-subtree-up (&optional arg)
  6967. "Move the current subtree up past ARG headlines of the same level."
  6968. (interactive "p")
  6969. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6970. (defun org-move-subtree-down (&optional arg)
  6971. "Move the current subtree down past ARG headlines of the same level."
  6972. (interactive "p")
  6973. (setq arg (prefix-numeric-value arg))
  6974. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6975. 'org-get-last-sibling))
  6976. (ins-point (make-marker))
  6977. (cnt (abs arg))
  6978. (col (current-column))
  6979. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6980. ;; Select the tree
  6981. (org-back-to-heading)
  6982. (setq beg0 (point))
  6983. (save-excursion
  6984. (setq ne-beg (org-back-over-empty-lines))
  6985. (setq beg (point)))
  6986. (save-match-data
  6987. (save-excursion (outline-end-of-heading)
  6988. (setq folded (outline-invisible-p)))
  6989. (outline-end-of-subtree))
  6990. (outline-next-heading)
  6991. (setq ne-end (org-back-over-empty-lines))
  6992. (setq end (point))
  6993. (goto-char beg0)
  6994. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6995. ;; include less whitespace
  6996. (save-excursion
  6997. (goto-char beg)
  6998. (forward-line (- ne-beg ne-end))
  6999. (setq beg (point))))
  7000. ;; Find insertion point, with error handling
  7001. (while (> cnt 0)
  7002. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7003. (progn (goto-char beg0)
  7004. (error "Cannot move past superior level or buffer limit")))
  7005. (setq cnt (1- cnt)))
  7006. (if (> arg 0)
  7007. ;; Moving forward - still need to move over subtree
  7008. (progn (org-end-of-subtree t t)
  7009. (save-excursion
  7010. (org-back-over-empty-lines)
  7011. (or (bolp) (newline)))))
  7012. (setq ne-ins (org-back-over-empty-lines))
  7013. (move-marker ins-point (point))
  7014. (setq txt (buffer-substring beg end))
  7015. (org-save-markers-in-region beg end)
  7016. (delete-region beg end)
  7017. (org-remove-empty-overlays-at beg)
  7018. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7019. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7020. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7021. (let ((bbb (point)))
  7022. (insert-before-markers txt)
  7023. (org-reinstall-markers-in-region bbb)
  7024. (move-marker ins-point bbb))
  7025. (or (bolp) (insert "\n"))
  7026. (setq ins-end (point))
  7027. (goto-char ins-point)
  7028. (org-skip-whitespace)
  7029. (when (and (< arg 0)
  7030. (org-first-sibling-p)
  7031. (> ne-ins ne-beg))
  7032. ;; Move whitespace back to beginning
  7033. (save-excursion
  7034. (goto-char ins-end)
  7035. (let ((kill-whole-line t))
  7036. (kill-line (- ne-ins ne-beg)) (point)))
  7037. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7038. (move-marker ins-point nil)
  7039. (if folded
  7040. (hide-subtree)
  7041. (org-show-entry)
  7042. (show-children)
  7043. (org-cycle-hide-drawers 'children))
  7044. (org-clean-visibility-after-subtree-move)
  7045. ;; move back to the initial column we were at
  7046. (move-to-column col)))
  7047. (defvar org-subtree-clip ""
  7048. "Clipboard for cut and paste of subtrees.
  7049. This is actually only a copy of the kill, because we use the normal kill
  7050. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7051. (defvar org-subtree-clip-folded nil
  7052. "Was the last copied subtree folded?
  7053. This is used to fold the tree back after pasting.")
  7054. (defun org-cut-subtree (&optional n)
  7055. "Cut the current subtree into the clipboard.
  7056. With prefix arg N, cut this many sequential subtrees.
  7057. This is a short-hand for marking the subtree and then cutting it."
  7058. (interactive "p")
  7059. (org-copy-subtree n 'cut))
  7060. (defun org-copy-subtree (&optional n cut force-store-markers)
  7061. "Cut the current subtree into the clipboard.
  7062. With prefix arg N, cut this many sequential subtrees.
  7063. This is a short-hand for marking the subtree and then copying it.
  7064. If CUT is non-nil, actually cut the subtree.
  7065. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7066. of some markers in the region, even if CUT is non-nil. This is
  7067. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7068. (interactive "p")
  7069. (let (beg end folded (beg0 (point)))
  7070. (if (org-called-interactively-p 'any)
  7071. (org-back-to-heading nil) ; take what looks like a subtree
  7072. (org-back-to-heading t)) ; take what is really there
  7073. (setq beg (point))
  7074. (skip-chars-forward " \t\r\n")
  7075. (save-match-data
  7076. (save-excursion (outline-end-of-heading)
  7077. (setq folded (outline-invisible-p)))
  7078. (condition-case nil
  7079. (org-forward-heading-same-level (1- n) t)
  7080. (error nil))
  7081. (org-end-of-subtree t t))
  7082. (setq end (point))
  7083. (goto-char beg0)
  7084. (when (> end beg)
  7085. (setq org-subtree-clip-folded folded)
  7086. (when (or cut force-store-markers)
  7087. (org-save-markers-in-region beg end))
  7088. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7089. (setq org-subtree-clip (current-kill 0))
  7090. (message "%s: Subtree(s) with %d characters"
  7091. (if cut "Cut" "Copied")
  7092. (length org-subtree-clip)))))
  7093. (defun org-paste-subtree (&optional level tree for-yank)
  7094. "Paste the clipboard as a subtree, with modification of headline level.
  7095. The entire subtree is promoted or demoted in order to match a new headline
  7096. level.
  7097. If the cursor is at the beginning of a headline, the same level as
  7098. that headline is used to paste the tree
  7099. If not, the new level is derived from the *visible* headings
  7100. before and after the insertion point, and taken to be the inferior headline
  7101. level of the two. So if the previous visible heading is level 3 and the
  7102. next is level 4 (or vice versa), level 4 will be used for insertion.
  7103. This makes sure that the subtree remains an independent subtree and does
  7104. not swallow low level entries.
  7105. You can also force a different level, either by using a numeric prefix
  7106. argument, or by inserting the heading marker by hand. For example, if the
  7107. cursor is after \"*****\", then the tree will be shifted to level 5.
  7108. If optional TREE is given, use this text instead of the kill ring.
  7109. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7110. move back over whitespace before inserting, and move point to the end of
  7111. the inserted text when done."
  7112. (interactive "P")
  7113. (setq tree (or tree (and kill-ring (current-kill 0))))
  7114. (unless (org-kill-is-subtree-p tree)
  7115. (error "%s"
  7116. (substitute-command-keys
  7117. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7118. (org-with-limited-levels
  7119. (let* ((visp (not (outline-invisible-p)))
  7120. (txt tree)
  7121. (^re_ "\\(\\*+\\)[ \t]*")
  7122. (old-level (if (string-match org-outline-regexp-bol txt)
  7123. (- (match-end 0) (match-beginning 0) 1)
  7124. -1))
  7125. (force-level (cond (level (prefix-numeric-value level))
  7126. ((and (looking-at "[ \t]*$")
  7127. (string-match
  7128. "^\\*+$" (buffer-substring
  7129. (point-at-bol) (point))))
  7130. (- (match-end 1) (match-beginning 1)))
  7131. ((and (bolp)
  7132. (looking-at org-outline-regexp))
  7133. (- (match-end 0) (point) 1))))
  7134. (previous-level (save-excursion
  7135. (condition-case nil
  7136. (progn
  7137. (outline-previous-visible-heading 1)
  7138. (if (looking-at ^re_)
  7139. (- (match-end 0) (match-beginning 0) 1)
  7140. 1))
  7141. (error 1))))
  7142. (next-level (save-excursion
  7143. (condition-case nil
  7144. (progn
  7145. (or (looking-at org-outline-regexp)
  7146. (outline-next-visible-heading 1))
  7147. (if (looking-at ^re_)
  7148. (- (match-end 0) (match-beginning 0) 1)
  7149. 1))
  7150. (error 1))))
  7151. (new-level (or force-level (max previous-level next-level)))
  7152. (shift (if (or (= old-level -1)
  7153. (= new-level -1)
  7154. (= old-level new-level))
  7155. 0
  7156. (- new-level old-level)))
  7157. (delta (if (> shift 0) -1 1))
  7158. (func (if (> shift 0) 'org-demote 'org-promote))
  7159. (org-odd-levels-only nil)
  7160. beg end newend)
  7161. ;; Remove the forced level indicator
  7162. (if force-level
  7163. (delete-region (point-at-bol) (point)))
  7164. ;; Paste
  7165. (beginning-of-line (if (bolp) 1 2))
  7166. (setq beg (point))
  7167. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7168. (insert-before-markers txt)
  7169. (unless (string-match "\n\\'" txt) (insert "\n"))
  7170. (setq newend (point))
  7171. (org-reinstall-markers-in-region beg)
  7172. (setq end (point))
  7173. (goto-char beg)
  7174. (skip-chars-forward " \t\n\r")
  7175. (setq beg (point))
  7176. (if (and (outline-invisible-p) visp)
  7177. (save-excursion (outline-show-heading)))
  7178. ;; Shift if necessary
  7179. (unless (= shift 0)
  7180. (save-restriction
  7181. (narrow-to-region beg end)
  7182. (while (not (= shift 0))
  7183. (org-map-region func (point-min) (point-max))
  7184. (setq shift (+ delta shift)))
  7185. (goto-char (point-min))
  7186. (setq newend (point-max))))
  7187. (when (or (org-called-interactively-p 'interactive) for-yank)
  7188. (message "Clipboard pasted as level %d subtree" new-level))
  7189. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7190. kill-ring
  7191. (eq org-subtree-clip (current-kill 0))
  7192. org-subtree-clip-folded)
  7193. ;; The tree was folded before it was killed/copied
  7194. (hide-subtree))
  7195. (and for-yank (goto-char newend)))))
  7196. (defun org-kill-is-subtree-p (&optional txt)
  7197. "Check if the current kill is an outline subtree, or a set of trees.
  7198. Returns nil if kill does not start with a headline, or if the first
  7199. headline level is not the largest headline level in the tree.
  7200. So this will actually accept several entries of equal levels as well,
  7201. which is OK for `org-paste-subtree'.
  7202. If optional TXT is given, check this string instead of the current kill."
  7203. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7204. (re (org-get-limited-outline-regexp))
  7205. (^re (concat "^" re))
  7206. (start-level (and kill
  7207. (string-match
  7208. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7209. kill)
  7210. (- (match-end 2) (match-beginning 2) 1)))
  7211. (start (1+ (or (match-beginning 2) -1))))
  7212. (if (not start-level)
  7213. (progn
  7214. nil) ;; does not even start with a heading
  7215. (catch 'exit
  7216. (while (setq start (string-match ^re kill (1+ start)))
  7217. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7218. (throw 'exit nil)))
  7219. t))))
  7220. (defvar org-markers-to-move nil
  7221. "Markers that should be moved with a cut-and-paste operation.
  7222. Those markers are stored together with their positions relative to
  7223. the start of the region.")
  7224. (defun org-save-markers-in-region (beg end)
  7225. "Check markers in region.
  7226. If these markers are between BEG and END, record their position relative
  7227. to BEG, so that after moving the block of text, we can put the markers back
  7228. into place.
  7229. This function gets called just before an entry or tree gets cut from the
  7230. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7231. called immediately, to move the markers with the entries."
  7232. (setq org-markers-to-move nil)
  7233. (when (featurep 'org-clock)
  7234. (org-clock-save-markers-for-cut-and-paste beg end))
  7235. (when (featurep 'org-agenda)
  7236. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7237. (defun org-check-and-save-marker (marker beg end)
  7238. "Check if MARKER is between BEG and END.
  7239. If yes, remember the marker and the distance to BEG."
  7240. (when (and (marker-buffer marker)
  7241. (equal (marker-buffer marker) (current-buffer)))
  7242. (if (and (>= marker beg) (< marker end))
  7243. (push (cons marker (- marker beg)) org-markers-to-move))))
  7244. (defun org-reinstall-markers-in-region (beg)
  7245. "Move all remembered markers to their position relative to BEG."
  7246. (mapc (lambda (x)
  7247. (move-marker (car x) (+ beg (cdr x))))
  7248. org-markers-to-move)
  7249. (setq org-markers-to-move nil))
  7250. (defun org-narrow-to-subtree ()
  7251. "Narrow buffer to the current subtree."
  7252. (interactive)
  7253. (save-excursion
  7254. (save-match-data
  7255. (org-with-limited-levels
  7256. (narrow-to-region
  7257. (progn (org-back-to-heading t) (point))
  7258. (progn (org-end-of-subtree t t)
  7259. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7260. (point)))))))
  7261. (defun org-narrow-to-block ()
  7262. "Narrow buffer to the current block."
  7263. (interactive)
  7264. (let* ((case-fold-search t)
  7265. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7266. "^[ \t]*#\\+end_.*")))
  7267. (if blockp
  7268. (narrow-to-region (car blockp) (cdr blockp))
  7269. (error "Not in a block"))))
  7270. (eval-when-compile
  7271. (defvar org-property-drawer-re))
  7272. (defvar org-property-start-re) ;; defined below
  7273. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7274. "Clone the task (subtree) at point N times.
  7275. The clones will be inserted as siblings.
  7276. In interactive use, the user will be prompted for the number of
  7277. clones to be produced, and for a time SHIFT, which may be a
  7278. repeater as used in time stamps, for example `+3d'.
  7279. When a valid repeater is given and the entry contains any time
  7280. stamps, the clones will become a sequence in time, with time
  7281. stamps in the subtree shifted for each clone produced. If SHIFT
  7282. is nil or the empty string, time stamps will be left alone. The
  7283. ID property of the original subtree is removed.
  7284. If the original subtree did contain time stamps with a repeater,
  7285. the following will happen:
  7286. - the repeater will be removed in each clone
  7287. - an additional clone will be produced, with the current, unshifted
  7288. date(s) in the entry.
  7289. - the original entry will be placed *after* all the clones, with
  7290. repeater intact.
  7291. - the start days in the repeater in the original entry will be shifted
  7292. to past the last clone.
  7293. In this way you can spell out a number of instances of a repeating task,
  7294. and still retain the repeater to cover future instances of the task."
  7295. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7296. (let (beg end template task idprop
  7297. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7298. (drawer-re org-drawer-regexp))
  7299. (if (not (and (integerp n) (> n 0)))
  7300. (error "Invalid number of replications %s" n))
  7301. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7302. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7303. shift)))
  7304. (error "Invalid shift specification %s" shift))
  7305. (when doshift
  7306. (setq shift-n (string-to-number (match-string 1 shift))
  7307. shift-what (cdr (assoc (match-string 2 shift)
  7308. '(("d" . day) ("w" . week)
  7309. ("m" . month) ("y" . year))))))
  7310. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7311. (setq nmin 1 nmax n)
  7312. (org-back-to-heading t)
  7313. (setq beg (point))
  7314. (setq idprop (org-entry-get nil "ID"))
  7315. (org-end-of-subtree t t)
  7316. (or (bolp) (insert "\n"))
  7317. (setq end (point))
  7318. (setq template (buffer-substring beg end))
  7319. (when (and doshift
  7320. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7321. (delete-region beg end)
  7322. (setq end beg)
  7323. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7324. (goto-char end)
  7325. (loop for n from nmin to nmax do
  7326. ;; prepare clone
  7327. (with-temp-buffer
  7328. (insert template)
  7329. (org-mode)
  7330. (goto-char (point-min))
  7331. (org-show-subtree)
  7332. (and idprop (if org-clone-delete-id
  7333. (org-entry-delete nil "ID")
  7334. (org-id-get-create t)))
  7335. (unless (= n 0)
  7336. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7337. (kill-whole-line))
  7338. (goto-char (point-min))
  7339. (while (re-search-forward drawer-re nil t)
  7340. (mapc (lambda (d)
  7341. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7342. (goto-char (point-min))
  7343. (when doshift
  7344. (while (re-search-forward org-ts-regexp-both nil t)
  7345. (org-timestamp-change (* n shift-n) shift-what))
  7346. (unless (= n n-no-remove)
  7347. (goto-char (point-min))
  7348. (while (re-search-forward org-ts-regexp nil t)
  7349. (save-excursion
  7350. (goto-char (match-beginning 0))
  7351. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7352. (delete-region (match-beginning 1) (match-end 1)))))))
  7353. (setq task (buffer-string)))
  7354. (insert task))
  7355. (goto-char beg)))
  7356. ;;; Outline Sorting
  7357. (defun org-sort (with-case)
  7358. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7359. Optional argument WITH-CASE means sort case-sensitively."
  7360. (interactive "P")
  7361. (cond
  7362. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7363. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7364. (t
  7365. (org-call-with-arg 'org-sort-entries with-case))))
  7366. (defun org-sort-remove-invisible (s)
  7367. (remove-text-properties 0 (length s) org-rm-props s)
  7368. (while (string-match org-bracket-link-regexp s)
  7369. (setq s (replace-match (if (match-end 2)
  7370. (match-string 3 s)
  7371. (match-string 1 s)) t t s)))
  7372. s)
  7373. (defvar org-priority-regexp) ; defined later in the file
  7374. (defvar org-after-sorting-entries-or-items-hook nil
  7375. "Hook that is run after a bunch of entries or items have been sorted.
  7376. When children are sorted, the cursor is in the parent line when this
  7377. hook gets called. When a region or a plain list is sorted, the cursor
  7378. will be in the first entry of the sorted region/list.")
  7379. (defun org-sort-entries
  7380. (&optional with-case sorting-type getkey-func compare-func property)
  7381. "Sort entries on a certain level of an outline tree.
  7382. If there is an active region, the entries in the region are sorted.
  7383. Else, if the cursor is before the first entry, sort the top-level items.
  7384. Else, the children of the entry at point are sorted.
  7385. Sorting can be alphabetically, numerically, by date/time as given by
  7386. a time stamp, by a property or by priority.
  7387. The command prompts for the sorting type unless it has been given to the
  7388. function through the SORTING-TYPE argument, which needs to be a character,
  7389. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7390. precise meaning of each character:
  7391. n Numerically, by converting the beginning of the entry/item to a number.
  7392. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7393. o By order of TODO keywords.
  7394. t By date/time, either the first active time stamp in the entry, or, if
  7395. none exist, by the first inactive one.
  7396. s By the scheduled date/time.
  7397. d By deadline date/time.
  7398. c By creation time, which is assumed to be the first inactive time stamp
  7399. at the beginning of a line.
  7400. p By priority according to the cookie.
  7401. r By the value of a property.
  7402. Capital letters will reverse the sort order.
  7403. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7404. called with point at the beginning of the record. It must return either
  7405. a string or a number that should serve as the sorting key for that record.
  7406. Comparing entries ignores case by default. However, with an optional argument
  7407. WITH-CASE, the sorting considers case as well."
  7408. (interactive "P")
  7409. (let ((case-func (if with-case 'identity 'downcase))
  7410. (cmstr
  7411. ;; The clock marker is lost when using `sort-subr', let's
  7412. ;; store the clocking string.
  7413. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7414. (save-excursion
  7415. (goto-char org-clock-marker)
  7416. (looking-back "^.*") (match-string-no-properties 0))))
  7417. start beg end stars re re2
  7418. txt what tmp)
  7419. ;; Find beginning and end of region to sort
  7420. (cond
  7421. ((org-region-active-p)
  7422. ;; we will sort the region
  7423. (setq end (region-end)
  7424. what "region")
  7425. (goto-char (region-beginning))
  7426. (if (not (org-at-heading-p)) (outline-next-heading))
  7427. (setq start (point)))
  7428. ((or (org-at-heading-p)
  7429. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7430. ;; we will sort the children of the current headline
  7431. (org-back-to-heading)
  7432. (setq start (point)
  7433. end (progn (org-end-of-subtree t t)
  7434. (or (bolp) (insert "\n"))
  7435. (org-back-over-empty-lines)
  7436. (point))
  7437. what "children")
  7438. (goto-char start)
  7439. (show-subtree)
  7440. (outline-next-heading))
  7441. (t
  7442. ;; we will sort the top-level entries in this file
  7443. (goto-char (point-min))
  7444. (or (org-at-heading-p) (outline-next-heading))
  7445. (setq start (point))
  7446. (goto-char (point-max))
  7447. (beginning-of-line 1)
  7448. (when (looking-at ".*?\\S-")
  7449. ;; File ends in a non-white line
  7450. (end-of-line 1)
  7451. (insert "\n"))
  7452. (setq end (point-max))
  7453. (setq what "top-level")
  7454. (goto-char start)
  7455. (show-all)))
  7456. (setq beg (point))
  7457. (if (>= beg end) (error "Nothing to sort"))
  7458. (looking-at "\\(\\*+\\)")
  7459. (setq stars (match-string 1)
  7460. re (concat "^" (regexp-quote stars) " +")
  7461. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7462. txt (buffer-substring beg end))
  7463. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7464. (if (and (not (equal stars "*")) (string-match re2 txt))
  7465. (error "Region to sort contains a level above the first entry"))
  7466. (unless sorting-type
  7467. (message
  7468. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7469. [t]ime [s]cheduled [d]eadline [c]reated
  7470. A/N/P/R/O/F/T/S/D/C means reversed:"
  7471. what)
  7472. (setq sorting-type (read-char-exclusive))
  7473. (unless getkey-func
  7474. (and (= (downcase sorting-type) ?f)
  7475. (setq getkey-func
  7476. (org-icompleting-read "Sort using function: "
  7477. obarray 'fboundp t nil nil))
  7478. (setq getkey-func (intern getkey-func))))
  7479. (and (= (downcase sorting-type) ?r)
  7480. (not property)
  7481. (setq property
  7482. (org-icompleting-read "Property: "
  7483. (mapcar 'list (org-buffer-property-keys t))
  7484. nil t))))
  7485. (message "Sorting entries...")
  7486. (save-restriction
  7487. (narrow-to-region start end)
  7488. (let ((dcst (downcase sorting-type))
  7489. (case-fold-search nil)
  7490. (now (current-time)))
  7491. (sort-subr
  7492. (/= dcst sorting-type)
  7493. ;; This function moves to the beginning character of the "record" to
  7494. ;; be sorted.
  7495. (lambda nil
  7496. (if (re-search-forward re nil t)
  7497. (goto-char (match-beginning 0))
  7498. (goto-char (point-max))))
  7499. ;; This function moves to the last character of the "record" being
  7500. ;; sorted.
  7501. (lambda nil
  7502. (save-match-data
  7503. (condition-case nil
  7504. (outline-forward-same-level 1)
  7505. (error
  7506. (goto-char (point-max))))))
  7507. ;; This function returns the value that gets sorted against.
  7508. (lambda nil
  7509. (cond
  7510. ((= dcst ?n)
  7511. (if (looking-at org-complex-heading-regexp)
  7512. (string-to-number (match-string 4))
  7513. nil))
  7514. ((= dcst ?a)
  7515. (if (looking-at org-complex-heading-regexp)
  7516. (funcall case-func (match-string 4))
  7517. nil))
  7518. ((= dcst ?t)
  7519. (let ((end (save-excursion (outline-next-heading) (point))))
  7520. (if (or (re-search-forward org-ts-regexp end t)
  7521. (re-search-forward org-ts-regexp-both end t))
  7522. (org-time-string-to-seconds (match-string 0))
  7523. (org-float-time now))))
  7524. ((= dcst ?c)
  7525. (let ((end (save-excursion (outline-next-heading) (point))))
  7526. (if (re-search-forward
  7527. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7528. end t)
  7529. (org-time-string-to-seconds (match-string 0))
  7530. (org-float-time now))))
  7531. ((= dcst ?s)
  7532. (let ((end (save-excursion (outline-next-heading) (point))))
  7533. (if (re-search-forward org-scheduled-time-regexp end t)
  7534. (org-time-string-to-seconds (match-string 1))
  7535. (org-float-time now))))
  7536. ((= dcst ?d)
  7537. (let ((end (save-excursion (outline-next-heading) (point))))
  7538. (if (re-search-forward org-deadline-time-regexp end t)
  7539. (org-time-string-to-seconds (match-string 1))
  7540. (org-float-time now))))
  7541. ((= dcst ?p)
  7542. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7543. (string-to-char (match-string 2))
  7544. org-default-priority))
  7545. ((= dcst ?r)
  7546. (or (org-entry-get nil property) ""))
  7547. ((= dcst ?o)
  7548. (if (looking-at org-complex-heading-regexp)
  7549. (- 9999 (length (member (match-string 2)
  7550. org-todo-keywords-1)))))
  7551. ((= dcst ?f)
  7552. (if getkey-func
  7553. (progn
  7554. (setq tmp (funcall getkey-func))
  7555. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7556. tmp)
  7557. (error "Invalid key function `%s'" getkey-func)))
  7558. (t (error "Invalid sorting type `%c'" sorting-type))))
  7559. nil
  7560. (cond
  7561. ((= dcst ?a) 'string<)
  7562. ((= dcst ?f) compare-func)
  7563. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7564. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7565. ;; Reset the clock marker if needed
  7566. (when cmstr
  7567. (save-excursion
  7568. (goto-char start)
  7569. (search-forward cmstr nil t)
  7570. (move-marker org-clock-marker (point))))
  7571. (message "Sorting entries...done")))
  7572. (defun org-do-sort (table what &optional with-case sorting-type)
  7573. "Sort TABLE of WHAT according to SORTING-TYPE.
  7574. The user will be prompted for the SORTING-TYPE if the call to this
  7575. function does not specify it. WHAT is only for the prompt, to indicate
  7576. what is being sorted. The sorting key will be extracted from
  7577. the car of the elements of the table.
  7578. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7579. (unless sorting-type
  7580. (message
  7581. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7582. what)
  7583. (setq sorting-type (read-char-exclusive)))
  7584. (let ((dcst (downcase sorting-type))
  7585. extractfun comparefun)
  7586. ;; Define the appropriate functions
  7587. (cond
  7588. ((= dcst ?n)
  7589. (setq extractfun 'string-to-number
  7590. comparefun (if (= dcst sorting-type) '< '>)))
  7591. ((= dcst ?a)
  7592. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7593. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7594. comparefun (if (= dcst sorting-type)
  7595. 'string<
  7596. (lambda (a b) (and (not (string< a b))
  7597. (not (string= a b)))))))
  7598. ((= dcst ?t)
  7599. (setq extractfun
  7600. (lambda (x)
  7601. (if (or (string-match org-ts-regexp x)
  7602. (string-match org-ts-regexp-both x))
  7603. (org-float-time
  7604. (org-time-string-to-time (match-string 0 x)))
  7605. 0))
  7606. comparefun (if (= dcst sorting-type) '< '>)))
  7607. (t (error "Invalid sorting type `%c'" sorting-type)))
  7608. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7609. table)
  7610. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7611. ;;; The orgstruct minor mode
  7612. ;; Define a minor mode which can be used in other modes in order to
  7613. ;; integrate the org-mode structure editing commands.
  7614. ;; This is really a hack, because the org-mode structure commands use
  7615. ;; keys which normally belong to the major mode. Here is how it
  7616. ;; works: The minor mode defines all the keys necessary to operate the
  7617. ;; structure commands, but wraps the commands into a function which
  7618. ;; tests if the cursor is currently at a headline or a plain list
  7619. ;; item. If that is the case, the structure command is used,
  7620. ;; temporarily setting many Org-mode variables like regular
  7621. ;; expressions for filling etc. However, when any of those keys is
  7622. ;; used at a different location, function uses `key-binding' to look
  7623. ;; up if the key has an associated command in another currently active
  7624. ;; keymap (minor modes, major mode, global), and executes that
  7625. ;; command. There might be problems if any of the keys is otherwise
  7626. ;; used as a prefix key.
  7627. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7628. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7629. ;; addresses this by checking explicitly for both bindings.
  7630. (defvar orgstruct-mode-map (make-sparse-keymap)
  7631. "Keymap for the minor `orgstruct-mode'.")
  7632. (defvar org-local-vars nil
  7633. "List of local variables, for use by `orgstruct-mode'.")
  7634. ;;;###autoload
  7635. (define-minor-mode orgstruct-mode
  7636. "Toggle the minor mode `orgstruct-mode'.
  7637. This mode is for using Org-mode structure commands in other
  7638. modes. The following keys behave as if Org-mode were active, if
  7639. the cursor is on a headline, or on a plain list item (both as
  7640. defined by Org-mode).
  7641. M-up Move entry/item up
  7642. M-down Move entry/item down
  7643. M-left Promote
  7644. M-right Demote
  7645. M-S-up Move entry/item up
  7646. M-S-down Move entry/item down
  7647. M-S-left Promote subtree
  7648. M-S-right Demote subtree
  7649. M-q Fill paragraph and items like in Org-mode
  7650. C-c ^ Sort entries
  7651. C-c - Cycle list bullet
  7652. TAB Cycle item visibility
  7653. M-RET Insert new heading/item
  7654. S-M-RET Insert new TODO heading / Checkbox item
  7655. C-c C-c Set tags / toggle checkbox"
  7656. nil " OrgStruct" nil
  7657. (org-load-modules-maybe)
  7658. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7659. ;;;###autoload
  7660. (defun turn-on-orgstruct ()
  7661. "Unconditionally turn on `orgstruct-mode'."
  7662. (orgstruct-mode 1))
  7663. (defvar org-fb-vars nil)
  7664. (make-variable-buffer-local 'org-fb-vars)
  7665. (defun orgstruct++-mode (&optional arg)
  7666. "Toggle `orgstruct-mode', the enhanced version of it.
  7667. In addition to setting orgstruct-mode, this also exports all
  7668. indentation and autofilling variables from org-mode into the
  7669. buffer. It will also recognize item context in multiline items."
  7670. (interactive "P")
  7671. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7672. (if (< arg 1)
  7673. (progn (orgstruct-mode -1)
  7674. (mapc (lambda(v)
  7675. (org-set-local (car v)
  7676. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7677. org-fb-vars))
  7678. (orgstruct-mode 1)
  7679. (setq org-fb-vars nil)
  7680. (let (var val)
  7681. (mapc
  7682. (lambda (x)
  7683. (when (string-match
  7684. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7685. (symbol-name (car x)))
  7686. (setq var (car x) val (nth 1 x))
  7687. (push (list var `(quote ,(eval var))) org-fb-vars)
  7688. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7689. org-local-vars)
  7690. (org-set-local 'orgstruct-is-++ t))))
  7691. (defvar orgstruct-is-++ nil
  7692. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7693. (make-variable-buffer-local 'orgstruct-is-++)
  7694. ;;;###autoload
  7695. (defun turn-on-orgstruct++ ()
  7696. "Unconditionally turn on `orgstruct++-mode'."
  7697. (orgstruct++-mode 1))
  7698. (defun orgstruct-error ()
  7699. "Error when there is no default binding for a structure key."
  7700. (interactive)
  7701. (error "This key has no function outside structure elements"))
  7702. (defun orgstruct-setup ()
  7703. "Setup orgstruct keymaps."
  7704. (let ((nfunc 0)
  7705. (bindings
  7706. (list
  7707. '([(meta up)] org-metaup)
  7708. '([(meta down)] org-metadown)
  7709. '([(meta left)] org-metaleft)
  7710. '([(meta right)] org-metaright)
  7711. '([(meta shift up)] org-shiftmetaup)
  7712. '([(meta shift down)] org-shiftmetadown)
  7713. '([(meta shift left)] org-shiftmetaleft)
  7714. '([(meta shift right)] org-shiftmetaright)
  7715. '([?\e (up)] org-metaup)
  7716. '([?\e (down)] org-metadown)
  7717. '([?\e (left)] org-metaleft)
  7718. '([?\e (right)] org-metaright)
  7719. '([?\e (shift up)] org-shiftmetaup)
  7720. '([?\e (shift down)] org-shiftmetadown)
  7721. '([?\e (shift left)] org-shiftmetaleft)
  7722. '([?\e (shift right)] org-shiftmetaright)
  7723. '([(shift up)] org-shiftup)
  7724. '([(shift down)] org-shiftdown)
  7725. '([(shift left)] org-shiftleft)
  7726. '([(shift right)] org-shiftright)
  7727. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7728. '("\M-q" fill-paragraph)
  7729. '("\C-c^" org-sort)
  7730. '("\C-c-" org-cycle-list-bullet)))
  7731. elt key fun cmd)
  7732. (while (setq elt (pop bindings))
  7733. (setq nfunc (1+ nfunc))
  7734. (setq key (org-key (car elt))
  7735. fun (nth 1 elt)
  7736. cmd (orgstruct-make-binding fun nfunc key))
  7737. (org-defkey orgstruct-mode-map key cmd))
  7738. ;; Prevent an error for users who forgot to make autoloads
  7739. (require 'org-element)
  7740. ;; Special treatment needed for TAB and RET
  7741. (org-defkey orgstruct-mode-map [(tab)]
  7742. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7743. (org-defkey orgstruct-mode-map "\C-i"
  7744. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7745. (org-defkey orgstruct-mode-map "\M-\C-m"
  7746. (orgstruct-make-binding 'org-insert-heading 105
  7747. "\M-\C-m" [(meta return)]))
  7748. (org-defkey orgstruct-mode-map [(meta return)]
  7749. (orgstruct-make-binding 'org-insert-heading 106
  7750. [(meta return)] "\M-\C-m"))
  7751. (org-defkey orgstruct-mode-map [(shift meta return)]
  7752. (orgstruct-make-binding 'org-insert-todo-heading 107
  7753. [(meta return)] "\M-\C-m"))
  7754. (org-defkey orgstruct-mode-map "\e\C-m"
  7755. (orgstruct-make-binding 'org-insert-heading 108
  7756. "\e\C-m" [?\e (return)]))
  7757. (org-defkey orgstruct-mode-map [?\e (return)]
  7758. (orgstruct-make-binding 'org-insert-heading 109
  7759. [?\e (return)] "\e\C-m"))
  7760. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7761. (orgstruct-make-binding 'org-insert-todo-heading 110
  7762. [?\e (return)] "\e\C-m"))
  7763. (unless org-local-vars
  7764. (setq org-local-vars (org-get-local-variables)))
  7765. t))
  7766. (defun orgstruct-make-binding (fun n &rest keys)
  7767. "Create a function for binding in the structure minor mode.
  7768. FUN is the command to call inside a table. N is used to create a unique
  7769. command name. KEYS are keys that should be checked in for a command
  7770. to execute outside of tables."
  7771. (eval
  7772. (list 'defun
  7773. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7774. '(arg)
  7775. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7776. "Outside of structure, run the binding of `"
  7777. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7778. "'.")
  7779. '(interactive "p")
  7780. (list 'if
  7781. `(org-context-p 'headline 'item
  7782. (and orgstruct-is-++
  7783. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7784. 'item-body))
  7785. (list 'org-run-like-in-org-mode (list 'quote fun))
  7786. (list 'let '(orgstruct-mode)
  7787. (list 'call-interactively
  7788. (append '(or)
  7789. (mapcar (lambda (k)
  7790. (list 'key-binding k))
  7791. keys)
  7792. '('orgstruct-error))))))))
  7793. (defun org-contextualize-keys (alist contexts)
  7794. "Return valid elements in ALIST depending on CONTEXTS.
  7795. `org-agenda-custom-commands' or `org-capture-templates' are the
  7796. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7797. or `org-capture-templates-contexts' are the associated contexts
  7798. definitions."
  7799. (let ((contexts
  7800. ;; normalize contexts
  7801. (mapcar
  7802. (lambda(c) (cond ((listp (cadr c))
  7803. (list (car c) (car c) (cadr c)))
  7804. ((string= "" (cadr c))
  7805. (list (car c) (car c) (caddr c)))
  7806. (t c))) contexts))
  7807. (a alist) c r s)
  7808. ;; loop over all commands or templates
  7809. (while (setq c (pop a))
  7810. (let (vrules repl)
  7811. (cond
  7812. ((not (assoc (car c) contexts))
  7813. (push c r))
  7814. ((and (assoc (car c) contexts)
  7815. (setq vrules (org-contextualize-validate-key
  7816. (car c) contexts)))
  7817. (mapc (lambda (vr)
  7818. (when (not (equal (car vr) (cadr vr)))
  7819. (setq repl vr))) vrules)
  7820. (if (not repl) (push c r)
  7821. (push (cadr repl) s)
  7822. (push
  7823. (cons (car c)
  7824. (cdr (or (assoc (cadr repl) alist)
  7825. (error "Undefined key `%s' as contextual replacement for `%s'"
  7826. (cadr repl) (car c)))))
  7827. r))))))
  7828. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7829. (delq
  7830. nil
  7831. (delete-dups
  7832. (mapcar (lambda (x)
  7833. (let ((tpl (car x)))
  7834. (when (not (delq
  7835. nil
  7836. (mapcar (lambda(y)
  7837. (equal y tpl)) s))) x)))
  7838. (reverse r))))))
  7839. (defun org-contextualize-validate-key (key contexts)
  7840. "Check CONTEXTS for agenda or capture KEY."
  7841. (let (r rr res)
  7842. (while (setq r (pop contexts))
  7843. (mapc
  7844. (lambda (rr)
  7845. (when
  7846. (and (equal key (car r))
  7847. (if (functionp rr) (funcall rr)
  7848. (or (and (eq (car rr) 'in-file)
  7849. (buffer-file-name)
  7850. (string-match (cdr rr) (buffer-file-name)))
  7851. (and (eq (car rr) 'in-mode)
  7852. (string-match (cdr rr) (symbol-name major-mode)))
  7853. (when (and (eq (car rr) 'not-in-file)
  7854. (buffer-file-name))
  7855. (not (string-match (cdr rr) (buffer-file-name))))
  7856. (when (eq (car rr) 'not-in-mode)
  7857. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7858. (push r res)))
  7859. (car (last r))))
  7860. (delete-dups (delq nil res))))
  7861. (defun org-context-p (&rest contexts)
  7862. "Check if local context is any of CONTEXTS.
  7863. Possible values in the list of contexts are `table', `headline', and `item'."
  7864. (let ((pos (point)))
  7865. (goto-char (point-at-bol))
  7866. (prog1 (or (and (memq 'table contexts)
  7867. (looking-at "[ \t]*|"))
  7868. (and (memq 'headline contexts)
  7869. (looking-at org-outline-regexp))
  7870. (and (memq 'item contexts)
  7871. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7872. (and (memq 'item-body contexts)
  7873. (org-in-item-p)))
  7874. (goto-char pos))))
  7875. (defun org-get-local-variables ()
  7876. "Return a list of all local variables in an Org mode buffer."
  7877. (let (varlist)
  7878. (with-current-buffer (get-buffer-create "*Org tmp*")
  7879. (erase-buffer)
  7880. (org-mode)
  7881. (setq varlist (buffer-local-variables)))
  7882. (kill-buffer "*Org tmp*")
  7883. (delq nil
  7884. (mapcar
  7885. (lambda (x)
  7886. (setq x
  7887. (if (symbolp x)
  7888. (list x)
  7889. (list (car x) (list 'quote (cdr x)))))
  7890. (if (string-match
  7891. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7892. (symbol-name (car x)))
  7893. x nil))
  7894. varlist))))
  7895. (defun org-clone-local-variables (from-buffer &optional regexp)
  7896. "Clone local variables from FROM-BUFFER.
  7897. Optional argument REGEXP selects variables to clone."
  7898. (mapc
  7899. (lambda (pair)
  7900. (and (symbolp (car pair))
  7901. (or (null regexp)
  7902. (string-match regexp (symbol-name (car pair))))
  7903. (set (make-local-variable (car pair))
  7904. (cdr pair))))
  7905. (buffer-local-variables from-buffer)))
  7906. ;;;###autoload
  7907. (defun org-run-like-in-org-mode (cmd)
  7908. "Run a command, pretending that the current buffer is in Org-mode.
  7909. This will temporarily bind local variables that are typically bound in
  7910. Org-mode to the values they have in Org-mode, and then interactively
  7911. call CMD."
  7912. (org-load-modules-maybe)
  7913. (unless org-local-vars
  7914. (setq org-local-vars (org-get-local-variables)))
  7915. (eval (list 'let org-local-vars
  7916. (list 'call-interactively (list 'quote cmd)))))
  7917. ;;;; Archiving
  7918. (defun org-get-category (&optional pos force-refresh)
  7919. "Get the category applying to position POS."
  7920. (save-match-data
  7921. (if force-refresh (org-refresh-category-properties))
  7922. (let ((pos (or pos (point))))
  7923. (or (get-text-property pos 'org-category)
  7924. (progn (org-refresh-category-properties)
  7925. (get-text-property pos 'org-category))))))
  7926. (defun org-refresh-category-properties ()
  7927. "Refresh category text properties in the buffer."
  7928. (let ((case-fold-search t)
  7929. (inhibit-read-only t)
  7930. (def-cat (cond
  7931. ((null org-category)
  7932. (if buffer-file-name
  7933. (file-name-sans-extension
  7934. (file-name-nondirectory buffer-file-name))
  7935. "???"))
  7936. ((symbolp org-category) (symbol-name org-category))
  7937. (t org-category)))
  7938. beg end cat pos optionp)
  7939. (org-unmodified
  7940. (save-excursion
  7941. (save-restriction
  7942. (widen)
  7943. (goto-char (point-min))
  7944. (put-text-property (point) (point-max) 'org-category def-cat)
  7945. (while (re-search-forward
  7946. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7947. (setq pos (match-end 0)
  7948. optionp (equal (char-after (match-beginning 0)) ?#)
  7949. cat (org-trim (match-string 2)))
  7950. (if optionp
  7951. (setq beg (point-at-bol) end (point-max))
  7952. (org-back-to-heading t)
  7953. (setq beg (point) end (org-end-of-subtree t t)))
  7954. (put-text-property beg end 'org-category cat)
  7955. (put-text-property beg end 'org-category-position beg)
  7956. (goto-char pos)))))))
  7957. (defun org-refresh-properties (dprop tprop)
  7958. "Refresh buffer text properties.
  7959. DPROP is the drawer property and TPROP is the corresponding text
  7960. property to set."
  7961. (let ((case-fold-search t)
  7962. (inhibit-read-only t) p)
  7963. (org-unmodified
  7964. (save-excursion
  7965. (save-restriction
  7966. (widen)
  7967. (goto-char (point-min))
  7968. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7969. (setq p (org-match-string-no-properties 1))
  7970. (save-excursion
  7971. (org-back-to-heading t)
  7972. (put-text-property
  7973. (point-at-bol) (point-at-eol) tprop p))))))))
  7974. ;;;; Link Stuff
  7975. ;;; Link abbreviations
  7976. (defun org-link-expand-abbrev (link)
  7977. "Apply replacements as defined in `org-link-abbrev-alist'."
  7978. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7979. (let* ((key (match-string 1 link))
  7980. (as (or (assoc key org-link-abbrev-alist-local)
  7981. (assoc key org-link-abbrev-alist)))
  7982. (tag (and (match-end 2) (match-string 3 link)))
  7983. rpl)
  7984. (if (not as)
  7985. link
  7986. (setq rpl (cdr as))
  7987. (cond
  7988. ((symbolp rpl) (funcall rpl tag))
  7989. ((string-match "%(\\([^)]+\\))" rpl)
  7990. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7991. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7992. ((string-match "%h" rpl)
  7993. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7994. (t (concat rpl tag)))))
  7995. link))
  7996. ;;; Storing and inserting links
  7997. (defvar org-insert-link-history nil
  7998. "Minibuffer history for links inserted with `org-insert-link'.")
  7999. (defvar org-stored-links nil
  8000. "Contains the links stored with `org-store-link'.")
  8001. (defvar org-store-link-plist nil
  8002. "Plist with info about the most recently link created with `org-store-link'.")
  8003. (defvar org-link-protocols nil
  8004. "Link protocols added to Org-mode using `org-add-link-type'.")
  8005. (defvar org-store-link-functions nil
  8006. "List of functions that are called to create and store a link.
  8007. Each function will be called in turn until one returns a non-nil
  8008. value. Each function should check if it is responsible for creating
  8009. this link (for example by looking at the major mode).
  8010. If not, it must exit and return nil.
  8011. If yes, it should return a non-nil value after a calling
  8012. `org-store-link-props' with a list of properties and values.
  8013. Special properties are:
  8014. :type The link prefix, like \"http\". This must be given.
  8015. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8016. This is obligatory as well.
  8017. :description Optional default description for the second pair
  8018. of brackets in an Org-mode link. The user can still change
  8019. this when inserting this link into an Org-mode buffer.
  8020. In addition to these, any additional properties can be specified
  8021. and then used in capture templates.")
  8022. (defun org-add-link-type (type &optional follow export)
  8023. "Add TYPE to the list of `org-link-types'.
  8024. Re-compute all regular expressions depending on `org-link-types'
  8025. FOLLOW and EXPORT are two functions.
  8026. FOLLOW should take the link path as the single argument and do whatever
  8027. is necessary to follow the link, for example find a file or display
  8028. a mail message.
  8029. EXPORT should format the link path for export to one of the export formats.
  8030. It should be a function accepting three arguments:
  8031. path the path of the link, the text after the prefix (like \"http:\")
  8032. desc the description of the link, if any, or a description added by
  8033. org-export-normalize-links if there is none
  8034. format the export format, a symbol like `html' or `latex' or `ascii'..
  8035. The function may use the FORMAT information to return different values
  8036. depending on the format. The return value will be put literally into
  8037. the exported file. If the return value is nil, this means Org should
  8038. do what it normally does with links which do not have EXPORT defined.
  8039. Org-mode has a built-in default for exporting links. If you are happy with
  8040. this default, there is no need to define an export function for the link
  8041. type. For a simple example of an export function, see `org-bbdb.el'."
  8042. (add-to-list 'org-link-types type t)
  8043. (org-make-link-regexps)
  8044. (if (assoc type org-link-protocols)
  8045. (setcdr (assoc type org-link-protocols) (list follow export))
  8046. (push (list type follow export) org-link-protocols)))
  8047. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8048. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8049. ;;;###autoload
  8050. (defun org-store-link (arg)
  8051. "\\<org-mode-map>Store an org-link to the current location.
  8052. This link is added to `org-stored-links' and can later be inserted
  8053. into an org-buffer with \\[org-insert-link].
  8054. For some link types, a prefix arg is interpreted.
  8055. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8056. For file links, arg negates `org-context-in-file-links'.
  8057. A double prefix arg force skipping storing functions that are not
  8058. part of Org's core."
  8059. (interactive "P")
  8060. (org-load-modules-maybe)
  8061. (setq org-store-link-plist nil) ; reset
  8062. (org-with-limited-levels
  8063. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8064. (cond
  8065. ((and (not (equal arg '(16)))
  8066. (setq sfuns
  8067. (delq
  8068. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8069. org-store-link-functions))
  8070. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8071. (or (and (cdr sfuns)
  8072. (funcall (intern
  8073. (completing-read "Which function for creating the link? "
  8074. sfunsn t (car sfunsn)))))
  8075. (funcall (caar sfuns)))
  8076. (setq link (plist-get org-store-link-plist :link)
  8077. desc (or (plist-get org-store-link-plist :description) link))))
  8078. ((org-src-edit-buffer-p)
  8079. (let (label gc)
  8080. (while (or (not label)
  8081. (save-excursion
  8082. (save-restriction
  8083. (widen)
  8084. (goto-char (point-min))
  8085. (re-search-forward
  8086. (regexp-quote (format org-coderef-label-format label))
  8087. nil t))))
  8088. (when label (message "Label exists already") (sit-for 2))
  8089. (setq label (read-string "Code line label: " label)))
  8090. (end-of-line 1)
  8091. (setq link (format org-coderef-label-format label))
  8092. (setq gc (- 79 (length link)))
  8093. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8094. (insert link)
  8095. (setq link (concat "(" label ")") desc nil)))
  8096. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8097. ;; We are in the agenda, link to referenced location
  8098. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8099. (get-text-property (point) 'org-marker))))
  8100. (when m
  8101. (org-with-point-at m
  8102. (setq agenda-link
  8103. (if (org-called-interactively-p 'any)
  8104. (call-interactively 'org-store-link)
  8105. (org-store-link nil)))))))
  8106. ((eq major-mode 'calendar-mode)
  8107. (let ((cd (calendar-cursor-to-date)))
  8108. (setq link
  8109. (format-time-string
  8110. (car org-time-stamp-formats)
  8111. (apply 'encode-time
  8112. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8113. nil nil nil))))
  8114. (org-store-link-props :type "calendar" :date cd)))
  8115. ((eq major-mode 'help-mode)
  8116. (setq link (concat "help:" (save-excursion
  8117. (goto-char (point-min))
  8118. (looking-at "^[^ ]+")
  8119. (match-string 0))))
  8120. (org-store-link-props :type "help"))
  8121. ((eq major-mode 'w3-mode)
  8122. (setq cpltxt (if (and (buffer-name)
  8123. (not (string-match "Untitled" (buffer-name))))
  8124. (buffer-name)
  8125. (url-view-url t))
  8126. link (url-view-url t))
  8127. (org-store-link-props :type "w3" :url (url-view-url t)))
  8128. ((setq search (run-hook-with-args-until-success
  8129. 'org-create-file-search-functions))
  8130. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8131. "::" search))
  8132. (setq cpltxt (or description link)))
  8133. ((eq major-mode 'image-mode)
  8134. (setq cpltxt (concat "file:"
  8135. (abbreviate-file-name buffer-file-name))
  8136. link cpltxt)
  8137. (org-store-link-props :type "image" :file buffer-file-name))
  8138. ((eq major-mode 'dired-mode)
  8139. ;; link to the file in the current line
  8140. (let ((file (dired-get-filename nil t)))
  8141. (setq file (if file
  8142. (abbreviate-file-name
  8143. (expand-file-name (dired-get-filename nil t)))
  8144. ;; otherwise, no file so use current directory.
  8145. default-directory))
  8146. (setq cpltxt (concat "file:" file)
  8147. link cpltxt)))
  8148. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8149. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8150. (cond
  8151. ((org-in-regexp "<<\\(.*?\\)>>")
  8152. (setq cpltxt
  8153. (concat "file:"
  8154. (abbreviate-file-name
  8155. (buffer-file-name (buffer-base-buffer)))
  8156. "::" (match-string 1))
  8157. link cpltxt))
  8158. ((and (featurep 'org-id)
  8159. (or (eq org-id-link-to-org-use-id t)
  8160. (and (org-called-interactively-p 'any)
  8161. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8162. (and (eq org-id-link-to-org-use-id
  8163. 'create-if-interactive-and-no-custom-id)
  8164. (not custom-id))))
  8165. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8166. ;; We can make a link using the ID.
  8167. (setq link (condition-case nil
  8168. (prog1 (org-id-store-link)
  8169. (setq desc (plist-get org-store-link-plist :description)))
  8170. (error
  8171. ;; probably before first headline, link to file only
  8172. (concat "file:"
  8173. (abbreviate-file-name
  8174. (buffer-file-name (buffer-base-buffer))))))))
  8175. (t
  8176. ;; Just link to current headline
  8177. (setq cpltxt (concat "file:"
  8178. (abbreviate-file-name
  8179. (buffer-file-name (buffer-base-buffer)))))
  8180. ;; Add a context search string
  8181. (when (org-xor org-context-in-file-links arg)
  8182. (let* ((ee (org-element-at-point))
  8183. (et (org-element-type ee))
  8184. (ev (plist-get (cadr ee) :value)))
  8185. (setq txt (cond
  8186. ((org-at-heading-p) nil)
  8187. ((eq et 'keyword) ev)
  8188. ((org-region-active-p)
  8189. (buffer-substring (region-beginning) (region-end)))))
  8190. (when (or (null txt) (string-match "\\S-" txt))
  8191. (setq cpltxt
  8192. (concat cpltxt "::"
  8193. (condition-case nil
  8194. (org-make-org-heading-search-string txt)
  8195. (error "")))
  8196. desc (or (and (eq et 'keyword) ev)
  8197. (nth 4 (ignore-errors (org-heading-components)))
  8198. "NONE")))))
  8199. (if (string-match "::\\'" cpltxt)
  8200. (setq cpltxt (substring cpltxt 0 -2)))
  8201. (setq link cpltxt))))
  8202. ((buffer-file-name (buffer-base-buffer))
  8203. ;; Just link to this file here.
  8204. (setq cpltxt (concat "file:"
  8205. (abbreviate-file-name
  8206. (buffer-file-name (buffer-base-buffer)))))
  8207. ;; Add a context string
  8208. (when (org-xor org-context-in-file-links arg)
  8209. (setq txt (if (org-region-active-p)
  8210. (buffer-substring (region-beginning) (region-end))
  8211. (buffer-substring (point-at-bol) (point-at-eol))))
  8212. ;; Only use search option if there is some text.
  8213. (when (string-match "\\S-" txt)
  8214. (setq cpltxt
  8215. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8216. desc "NONE")))
  8217. (setq link cpltxt))
  8218. ((org-called-interactively-p 'interactive)
  8219. (user-error "No method for storing a link from this buffer"))
  8220. (t (setq link nil)))
  8221. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8222. (setq link (or link cpltxt)
  8223. desc (or desc cpltxt))
  8224. (cond ((equal desc "NONE") (setq desc nil))
  8225. ((string-match org-bracket-link-regexp desc)
  8226. (setq desc (replace-regexp-in-string
  8227. org-bracket-link-regexp
  8228. (concat "\\3" (if (equal (length (match-string 0 desc))
  8229. (length desc)) "*" "")) desc))))
  8230. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8231. (progn
  8232. (setq org-stored-links
  8233. (cons (list link desc) org-stored-links))
  8234. (message "Stored: %s" (or desc link))
  8235. (when custom-id
  8236. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8237. "::#" custom-id))
  8238. (setq org-stored-links
  8239. (cons (list link desc) org-stored-links))))
  8240. (or agenda-link (and link (org-make-link-string link desc)))))))
  8241. (defun org-store-link-props (&rest plist)
  8242. "Store link properties, extract names and addresses."
  8243. (let (x adr)
  8244. (when (setq x (plist-get plist :from))
  8245. (setq adr (mail-extract-address-components x))
  8246. (setq plist (plist-put plist :fromname (car adr)))
  8247. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8248. (when (setq x (plist-get plist :to))
  8249. (setq adr (mail-extract-address-components x))
  8250. (setq plist (plist-put plist :toname (car adr)))
  8251. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8252. (let ((from (plist-get plist :from))
  8253. (to (plist-get plist :to)))
  8254. (when (and from to org-from-is-user-regexp)
  8255. (setq plist
  8256. (plist-put plist :fromto
  8257. (if (string-match org-from-is-user-regexp from)
  8258. (concat "to %t")
  8259. (concat "from %f"))))))
  8260. (setq org-store-link-plist plist))
  8261. (defun org-add-link-props (&rest plist)
  8262. "Add these properties to the link property list."
  8263. (let (key value)
  8264. (while plist
  8265. (setq key (pop plist) value (pop plist))
  8266. (setq org-store-link-plist
  8267. (plist-put org-store-link-plist key value)))))
  8268. (defun org-email-link-description (&optional fmt)
  8269. "Return the description part of an email link.
  8270. This takes information from `org-store-link-plist' and formats it
  8271. according to FMT (default from `org-email-link-description-format')."
  8272. (setq fmt (or fmt org-email-link-description-format))
  8273. (let* ((p org-store-link-plist)
  8274. (to (plist-get p :toaddress))
  8275. (from (plist-get p :fromaddress))
  8276. (table
  8277. (list
  8278. (cons "%c" (plist-get p :fromto))
  8279. (cons "%F" (plist-get p :from))
  8280. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8281. (cons "%T" (plist-get p :to))
  8282. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8283. (cons "%s" (plist-get p :subject))
  8284. (cons "%d" (plist-get p :date))
  8285. (cons "%m" (plist-get p :message-id)))))
  8286. (when (string-match "%c" fmt)
  8287. ;; Check if the user wrote this message
  8288. (if (and org-from-is-user-regexp from to
  8289. (save-match-data (string-match org-from-is-user-regexp from)))
  8290. (setq fmt (replace-match "to %t" t t fmt))
  8291. (setq fmt (replace-match "from %f" t t fmt))))
  8292. (org-replace-escapes fmt table)))
  8293. (defun org-make-org-heading-search-string (&optional string)
  8294. "Make search string for the current headline or STRING."
  8295. (let ((s (or string
  8296. (and (derived-mode-p 'org-mode)
  8297. (save-excursion
  8298. (org-back-to-heading t)
  8299. (plist-get (cadr (org-element-at-point))
  8300. :raw-value)))))
  8301. (lines org-context-in-file-links))
  8302. (or string (setq s (concat "*" s))) ; Add * for headlines
  8303. (when (and string (integerp lines) (> lines 0))
  8304. (let ((slines (org-split-string s "\n")))
  8305. (when (< lines (length slines))
  8306. (setq s (mapconcat
  8307. 'identity
  8308. (reverse (nthcdr (- (length slines) lines)
  8309. (reverse slines))) "\n")))))
  8310. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8311. (defun org-make-link-string (link &optional description)
  8312. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8313. (unless (string-match "\\S-" link)
  8314. (error "Empty link"))
  8315. (when (and description
  8316. (stringp description)
  8317. (not (string-match "\\S-" description)))
  8318. (setq description nil))
  8319. (when (stringp description)
  8320. ;; Remove brackets from the description, they are fatal.
  8321. (while (string-match "\\[" description)
  8322. (setq description (replace-match "{" t t description)))
  8323. (while (string-match "\\]" description)
  8324. (setq description (replace-match "}" t t description))))
  8325. (when (equal link description)
  8326. ;; No description needed, it is identical
  8327. (setq description nil))
  8328. (when (and (not description)
  8329. (not (string-match (org-image-file-name-regexp) link))
  8330. (not (equal link (org-link-escape link))))
  8331. (setq description (org-extract-attributes link)))
  8332. (setq link
  8333. (cond ((string-match (org-image-file-name-regexp) link) link)
  8334. ((string-match org-link-types-re link)
  8335. (concat (match-string 1 link)
  8336. (org-link-escape (substring link (match-end 1)))))
  8337. (t (org-link-escape link))))
  8338. (concat "[[" link "]"
  8339. (if description (concat "[" description "]") "")
  8340. "]"))
  8341. (defconst org-link-escape-chars
  8342. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8343. "List of characters that should be escaped in link.
  8344. This is the list that is used for internal purposes.")
  8345. (defconst org-link-escape-chars-browser
  8346. '(?\ )
  8347. "List of escapes for characters that are problematic in links.
  8348. This is the list that is used before handing over to the browser.")
  8349. (defun org-link-escape (text &optional table merge)
  8350. "Return percent escaped representation of TEXT.
  8351. TEXT is a string with the text to escape.
  8352. Optional argument TABLE is a list with characters that should be
  8353. escaped. When nil, `org-link-escape-chars' is used.
  8354. If optional argument MERGE is set, merge TABLE into
  8355. `org-link-escape-chars'."
  8356. (cond
  8357. ((and table merge)
  8358. (mapc (lambda (defchr)
  8359. (unless (member defchr table)
  8360. (setq table (cons defchr table)))) org-link-escape-chars))
  8361. ((null table)
  8362. (setq table org-link-escape-chars)))
  8363. (mapconcat
  8364. (lambda (char)
  8365. (if (or (member char table)
  8366. (and (or (< char 32) (= char 37) (> char 126))
  8367. org-url-hexify-p))
  8368. (mapconcat (lambda (sequence-element)
  8369. (format "%%%.2X" sequence-element))
  8370. (or (encode-coding-char char 'utf-8)
  8371. (error "Unable to percent escape character: %s"
  8372. (char-to-string char))) "")
  8373. (char-to-string char))) text ""))
  8374. (defun org-link-unescape (str)
  8375. "Unhex hexified Unicode strings as returned from the JavaScript function
  8376. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8377. (unless (and (null str) (string= "" str))
  8378. (let ((pos 0) (case-fold-search t) unhexed)
  8379. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8380. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8381. (setq str (replace-match unhexed t t str))
  8382. (setq pos (+ pos (length unhexed))))))
  8383. str)
  8384. (defun org-link-unescape-compound (hex)
  8385. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8386. Note: this function also decodes single byte encodings like
  8387. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8388. (save-match-data
  8389. (let* ((bytes (cdr (split-string hex "%")))
  8390. (ret "")
  8391. (eat 0)
  8392. (sum 0))
  8393. (while bytes
  8394. (let* ((val (string-to-number (pop bytes) 16))
  8395. (shift-xor
  8396. (if (= 0 eat)
  8397. (cond
  8398. ((>= val 252) (cons 6 252))
  8399. ((>= val 248) (cons 5 248))
  8400. ((>= val 240) (cons 4 240))
  8401. ((>= val 224) (cons 3 224))
  8402. ((>= val 192) (cons 2 192))
  8403. (t (cons 0 0)))
  8404. (cons 6 128))))
  8405. (if (>= val 192) (setq eat (car shift-xor)))
  8406. (setq val (logxor val (cdr shift-xor)))
  8407. (setq sum (+ (lsh sum (car shift-xor)) val))
  8408. (if (> eat 0) (setq eat (- eat 1)))
  8409. (cond
  8410. ((= 0 eat) ;multi byte
  8411. (setq ret (concat ret (org-char-to-string sum)))
  8412. (setq sum 0))
  8413. ((not bytes) ; single byte(s)
  8414. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8415. )) ;; end (while bytes
  8416. ret )))
  8417. (defun org-link-unescape-single-byte-sequence (hex)
  8418. "Unhexify hex-encoded single byte character sequences."
  8419. (mapconcat (lambda (byte)
  8420. (char-to-string (string-to-number byte 16)))
  8421. (cdr (split-string hex "%")) ""))
  8422. (defun org-xor (a b)
  8423. "Exclusive or."
  8424. (if a (not b) b))
  8425. (defun org-fixup-message-id-for-http (s)
  8426. "Replace special characters in a message id, so it can be used in an http query."
  8427. (when (string-match "%" s)
  8428. (setq s (mapconcat (lambda (c)
  8429. (if (eq c ?%)
  8430. "%25"
  8431. (char-to-string c)))
  8432. s "")))
  8433. (while (string-match "<" s)
  8434. (setq s (replace-match "%3C" t t s)))
  8435. (while (string-match ">" s)
  8436. (setq s (replace-match "%3E" t t s)))
  8437. (while (string-match "@" s)
  8438. (setq s (replace-match "%40" t t s)))
  8439. s)
  8440. (defun org-link-prettify (link)
  8441. "Return a human-readable representation of LINK.
  8442. The car of LINK must be a raw link the cdr of LINK must be either
  8443. a link description or nil."
  8444. (let ((desc (or (cadr link) "<no description>")))
  8445. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8446. "<" (car link) ">")))
  8447. ;;;###autoload
  8448. (defun org-insert-link-global ()
  8449. "Insert a link like Org-mode does.
  8450. This command can be called in any mode to insert a link in Org-mode syntax."
  8451. (interactive)
  8452. (org-load-modules-maybe)
  8453. (org-run-like-in-org-mode 'org-insert-link))
  8454. (defun org-insert-all-links (&optional keep)
  8455. "Insert all links in `org-stored-links'."
  8456. (interactive "P")
  8457. (let ((links (copy-sequence org-stored-links)) l)
  8458. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8459. (insert "- ")
  8460. (org-insert-link nil (car l) (cadr l))
  8461. (insert "\n"))))
  8462. (defun org-link-fontify-links-to-this-file ()
  8463. "Fontify links to the current file in `org-stored-links'."
  8464. (let ((f (buffer-file-name)) a b)
  8465. (setq a (mapcar (lambda(l)
  8466. (let ((ll (car l)))
  8467. (when (and (string-match "^file:\\(.+\\)::" ll)
  8468. (equal f (expand-file-name (match-string 1 ll))))
  8469. ll)))
  8470. org-stored-links))
  8471. (when (featurep 'org-id)
  8472. (setq b (mapcar (lambda(l)
  8473. (let ((ll (car l)))
  8474. (when (and (string-match "^id:\\(.+\\)$" ll)
  8475. (equal f (expand-file-name
  8476. (or (org-id-find-id-file
  8477. (match-string 1 ll)) ""))))
  8478. ll)))
  8479. org-stored-links)))
  8480. (mapcar (lambda(l)
  8481. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8482. (delq nil (append a b)))))
  8483. (defvar org-link-links-in-this-file nil)
  8484. (defun org-insert-link (&optional complete-file link-location default-description)
  8485. "Insert a link. At the prompt, enter the link.
  8486. Completion can be used to insert any of the link protocol prefixes like
  8487. http or ftp in use.
  8488. The history can be used to select a link previously stored with
  8489. `org-store-link'. When the empty string is entered (i.e. if you just
  8490. press RET at the prompt), the link defaults to the most recently
  8491. stored link. As SPC triggers completion in the minibuffer, you need to
  8492. use M-SPC or C-q SPC to force the insertion of a space character.
  8493. You will also be prompted for a description, and if one is given, it will
  8494. be displayed in the buffer instead of the link.
  8495. If there is already a link at point, this command will allow you to edit link
  8496. and description parts.
  8497. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8498. be selected using completion. The path to the file will be relative to the
  8499. current directory if the file is in the current directory or a subdirectory.
  8500. Otherwise, the link will be the absolute path as completed in the minibuffer
  8501. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8502. option `org-link-file-path-type'.
  8503. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8504. the current directory or below.
  8505. With three \\[universal-argument] prefixes, negate the meaning of
  8506. `org-keep-stored-link-after-insertion'.
  8507. If `org-make-link-description-function' is non-nil, this function will be
  8508. called with the link target, and the result will be the default
  8509. link description.
  8510. If the LINK-LOCATION parameter is non-nil, this value will be
  8511. used as the link location instead of reading one interactively.
  8512. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8513. be used as the default description."
  8514. (interactive "P")
  8515. (let* ((wcf (current-window-configuration))
  8516. (origbuf (current-buffer))
  8517. (region (if (org-region-active-p)
  8518. (buffer-substring (region-beginning) (region-end))))
  8519. (remove (and region (list (region-beginning) (region-end))))
  8520. (desc region)
  8521. tmphist ; byte-compile incorrectly complains about this
  8522. (link link-location)
  8523. (abbrevs org-link-abbrev-alist-local)
  8524. entry file all-prefixes auto-desc)
  8525. (cond
  8526. (link-location) ; specified by arg, just use it.
  8527. ((org-in-regexp org-bracket-link-regexp 1)
  8528. ;; We do have a link at point, and we are going to edit it.
  8529. (setq remove (list (match-beginning 0) (match-end 0)))
  8530. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8531. (setq link (read-string "Link: "
  8532. (org-link-unescape
  8533. (org-match-string-no-properties 1)))))
  8534. ((or (org-in-regexp org-angle-link-re)
  8535. (org-in-regexp org-plain-link-re))
  8536. ;; Convert to bracket link
  8537. (setq remove (list (match-beginning 0) (match-end 0))
  8538. link (read-string "Link: "
  8539. (org-remove-angle-brackets (match-string 0)))))
  8540. ((member complete-file '((4) (16)))
  8541. ;; Completing read for file names.
  8542. (setq link (org-file-complete-link complete-file)))
  8543. (t
  8544. ;; Read link, with completion for stored links.
  8545. (org-link-fontify-links-to-this-file)
  8546. (org-switch-to-buffer-other-window "*Org Links*")
  8547. (with-current-buffer "*Org Links*"
  8548. (erase-buffer)
  8549. (insert "Insert a link.
  8550. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8551. (when org-stored-links
  8552. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8553. (insert (mapconcat 'org-link-prettify
  8554. (reverse org-stored-links) "\n")))
  8555. (goto-char (point-min)))
  8556. (let ((cw (selected-window)))
  8557. (select-window (get-buffer-window "*Org Links*" 'visible))
  8558. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8559. (unless (pos-visible-in-window-p (point-max))
  8560. (org-fit-window-to-buffer))
  8561. (and (window-live-p cw) (select-window cw)))
  8562. ;; Fake a link history, containing the stored links.
  8563. (setq tmphist (append (mapcar 'car org-stored-links)
  8564. org-insert-link-history))
  8565. (setq all-prefixes (append (mapcar 'car abbrevs)
  8566. (mapcar 'car org-link-abbrev-alist)
  8567. org-link-types))
  8568. (unwind-protect
  8569. (progn
  8570. (setq link
  8571. (org-completing-read
  8572. "Link: "
  8573. (append
  8574. (mapcar (lambda (x) (concat x ":"))
  8575. all-prefixes)
  8576. (mapcar 'car org-stored-links))
  8577. nil nil nil
  8578. 'tmphist
  8579. (caar org-stored-links)))
  8580. (if (not (string-match "\\S-" link))
  8581. (error "No link selected"))
  8582. (mapc (lambda(l)
  8583. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8584. org-stored-links)
  8585. (if (or (member link all-prefixes)
  8586. (and (equal ":" (substring link -1))
  8587. (member (substring link 0 -1) all-prefixes)
  8588. (setq link (substring link 0 -1))))
  8589. (setq link (with-current-buffer origbuf
  8590. (org-link-try-special-completion link)))))
  8591. (set-window-configuration wcf)
  8592. (kill-buffer "*Org Links*"))
  8593. (setq entry (assoc link org-stored-links))
  8594. (or entry (push link org-insert-link-history))
  8595. (setq desc (or desc (nth 1 entry)))))
  8596. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8597. (not org-keep-stored-link-after-insertion))
  8598. (setq org-stored-links (delq (assoc link org-stored-links)
  8599. org-stored-links)))
  8600. (if (string-match org-plain-link-re link)
  8601. ;; URL-like link, normalize the use of angular brackets.
  8602. (setq link (org-remove-angle-brackets link)))
  8603. ;; Check if we are linking to the current file with a search
  8604. ;; option If yes, simplify the link by using only the search
  8605. ;; option.
  8606. (when (and buffer-file-name
  8607. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8608. (let* ((path (match-string 1 link))
  8609. (case-fold-search nil)
  8610. (search (match-string 2 link)))
  8611. (save-match-data
  8612. (if (equal (file-truename buffer-file-name) (file-truename path))
  8613. ;; We are linking to this same file, with a search option
  8614. (setq link search)))))
  8615. ;; Check if we can/should use a relative path. If yes, simplify the link
  8616. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8617. (let* ((type (match-string 1 link))
  8618. (path (match-string 2 link))
  8619. (origpath path)
  8620. (case-fold-search nil))
  8621. (cond
  8622. ((or (eq org-link-file-path-type 'absolute)
  8623. (equal complete-file '(16)))
  8624. (setq path (abbreviate-file-name (expand-file-name path))))
  8625. ((eq org-link-file-path-type 'noabbrev)
  8626. (setq path (expand-file-name path)))
  8627. ((eq org-link-file-path-type 'relative)
  8628. (setq path (file-relative-name path)))
  8629. (t
  8630. (save-match-data
  8631. (if (string-match (concat "^" (regexp-quote
  8632. (expand-file-name
  8633. (file-name-as-directory
  8634. default-directory))))
  8635. (expand-file-name path))
  8636. ;; We are linking a file with relative path name.
  8637. (setq path (substring (expand-file-name path)
  8638. (match-end 0)))
  8639. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8640. (setq link (concat type path))
  8641. (if (equal desc origpath)
  8642. (setq desc path))))
  8643. (if org-make-link-description-function
  8644. (setq desc
  8645. (or (condition-case nil
  8646. (funcall org-make-link-description-function link desc)
  8647. (error (progn (message "Can't get link description from `%s'"
  8648. (symbol-name org-make-link-description-function))
  8649. (sit-for 2) nil)))
  8650. (read-string "Description: " default-description)))
  8651. (if default-description (setq desc default-description)
  8652. (setq desc (or (and auto-desc desc)
  8653. (read-string "Description: " desc)))))
  8654. (unless (string-match "\\S-" desc) (setq desc nil))
  8655. (if remove (apply 'delete-region remove))
  8656. (insert (org-make-link-string link desc))))
  8657. (defun org-link-try-special-completion (type)
  8658. "If there is completion support for link type TYPE, offer it."
  8659. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8660. (if (functionp fun)
  8661. (funcall fun)
  8662. (read-string "Link (no completion support): " (concat type ":")))))
  8663. (defun org-file-complete-link (&optional arg)
  8664. "Create a file link using completion."
  8665. (let (file link)
  8666. (setq file (org-iread-file-name "File: "))
  8667. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8668. (pwd1 (file-name-as-directory (abbreviate-file-name
  8669. (expand-file-name ".")))))
  8670. (cond
  8671. ((equal arg '(16))
  8672. (setq link (concat
  8673. "file:"
  8674. (abbreviate-file-name (expand-file-name file)))))
  8675. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8676. (setq link (concat "file:" (match-string 1 file))))
  8677. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8678. (expand-file-name file))
  8679. (setq link (concat
  8680. "file:" (match-string 1 (expand-file-name file)))))
  8681. (t (setq link (concat "file:" file)))))
  8682. link))
  8683. (defun org-iread-file-name (&rest args)
  8684. "Read-file-name using `ido-mode' speedup if available.
  8685. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8686. See `read-file-name' for a description of parameters."
  8687. (org-without-partial-completion
  8688. (if (and org-completion-use-ido
  8689. (fboundp 'ido-read-file-name)
  8690. (boundp 'ido-mode) ido-mode
  8691. (listp (second args)))
  8692. (let ((ido-enter-matching-directory nil))
  8693. (apply 'ido-read-file-name args))
  8694. (apply 'read-file-name args))))
  8695. (defun org-completing-read (&rest args)
  8696. "Completing-read with SPACE being a normal character."
  8697. (let ((enable-recursive-minibuffers t)
  8698. (minibuffer-local-completion-map
  8699. (copy-keymap minibuffer-local-completion-map)))
  8700. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8701. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8702. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8703. (apply 'org-icompleting-read args)))
  8704. (defun org-completing-read-no-i (&rest args)
  8705. (let (org-completion-use-ido org-completion-use-iswitchb)
  8706. (apply 'org-completing-read args)))
  8707. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8708. "Use iswitch as a completing-read replacement to choose from choices.
  8709. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8710. from."
  8711. (let* ((iswitchb-use-virtual-buffers nil)
  8712. (iswitchb-make-buflist-hook
  8713. (lambda ()
  8714. (setq iswitchb-temp-buflist choices))))
  8715. (iswitchb-read-buffer prompt)))
  8716. (defun org-icompleting-read (&rest args)
  8717. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8718. (org-without-partial-completion
  8719. (if (and org-completion-use-ido
  8720. (fboundp 'ido-completing-read)
  8721. (boundp 'ido-mode) ido-mode
  8722. (listp (second args)))
  8723. (let ((ido-enter-matching-directory nil))
  8724. (apply 'ido-completing-read (concat (car args))
  8725. (if (consp (car (nth 1 args)))
  8726. (mapcar 'car (nth 1 args))
  8727. (nth 1 args))
  8728. (cddr args)))
  8729. (if (and org-completion-use-iswitchb
  8730. (boundp 'iswitchb-mode) iswitchb-mode
  8731. (listp (second args)))
  8732. (apply 'org-iswitchb-completing-read (concat (car args))
  8733. (if (consp (car (nth 1 args)))
  8734. (mapcar 'car (nth 1 args))
  8735. (nth 1 args))
  8736. (cddr args))
  8737. (apply 'completing-read args)))))
  8738. (defun org-extract-attributes (s)
  8739. "Extract the attributes cookie from a string and set as text property."
  8740. (let (a attr (start 0) key value)
  8741. (save-match-data
  8742. (when (string-match "{{\\([^}]+\\)}}$" s)
  8743. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8744. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8745. (setq key (match-string 1 a) value (match-string 2 a)
  8746. start (match-end 0)
  8747. attr (plist-put attr (intern key) value))))
  8748. (org-add-props s nil 'org-attr attr))
  8749. s))
  8750. (defun org-extract-attributes-from-string (tag)
  8751. (let (key value attr)
  8752. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8753. (setq key (match-string 1 tag) value (match-string 2 tag)
  8754. tag (replace-match "" t t tag)
  8755. attr (plist-put attr (intern key) value)))
  8756. (cons tag attr)))
  8757. (defun org-attributes-to-string (plist)
  8758. "Format a property list into an HTML attribute list."
  8759. (let ((s "") key value)
  8760. (while plist
  8761. (setq key (pop plist) value (pop plist))
  8762. (and value
  8763. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8764. s))
  8765. ;;; Opening/following a link
  8766. (defvar org-link-search-failed nil)
  8767. (defvar org-open-link-functions nil
  8768. "Hook for functions finding a plain text link.
  8769. These functions must take a single argument, the link content.
  8770. They will be called for links that look like [[link text][description]]
  8771. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8772. like a filename (e.g. \"./blue.png\").
  8773. These functions will be called *before* Org attempts to resolve the
  8774. link by doing text searches in the current buffer - so if you want a
  8775. link \"[[target]]\" to still find \"<<target>>\", your function should
  8776. handle this as a special case.
  8777. When the function does handle the link, it must return a non-nil value.
  8778. If it decides that it is not responsible for this link, it must return
  8779. nil to indicate that that Org-mode can continue with other options
  8780. like exact and fuzzy text search.")
  8781. (defun org-next-link ()
  8782. "Move forward to the next link.
  8783. If the link is in hidden text, expose it."
  8784. (interactive)
  8785. (when (and org-link-search-failed (eq this-command last-command))
  8786. (goto-char (point-min))
  8787. (message "Link search wrapped back to beginning of buffer"))
  8788. (setq org-link-search-failed nil)
  8789. (let* ((pos (point))
  8790. (ct (org-context))
  8791. (a (assoc :link ct)))
  8792. (if a (goto-char (nth 2 a)))
  8793. (if (re-search-forward org-any-link-re nil t)
  8794. (progn
  8795. (goto-char (match-beginning 0))
  8796. (if (outline-invisible-p) (org-show-context)))
  8797. (goto-char pos)
  8798. (setq org-link-search-failed t)
  8799. (error "No further link found"))))
  8800. (defun org-previous-link ()
  8801. "Move backward to the previous link.
  8802. If the link is in hidden text, expose it."
  8803. (interactive)
  8804. (when (and org-link-search-failed (eq this-command last-command))
  8805. (goto-char (point-max))
  8806. (message "Link search wrapped back to end of buffer"))
  8807. (setq org-link-search-failed nil)
  8808. (let* ((pos (point))
  8809. (ct (org-context))
  8810. (a (assoc :link ct)))
  8811. (if a (goto-char (nth 1 a)))
  8812. (if (re-search-backward org-any-link-re nil t)
  8813. (progn
  8814. (goto-char (match-beginning 0))
  8815. (if (outline-invisible-p) (org-show-context)))
  8816. (goto-char pos)
  8817. (setq org-link-search-failed t)
  8818. (error "No further link found"))))
  8819. (defun org-translate-link (s)
  8820. "Translate a link string if a translation function has been defined."
  8821. (if (and org-link-translation-function
  8822. (fboundp org-link-translation-function)
  8823. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8824. (progn
  8825. (setq s (funcall org-link-translation-function
  8826. (match-string 1 s) (match-string 2 s)))
  8827. (concat (car s) ":" (cdr s)))
  8828. s))
  8829. (defun org-translate-link-from-planner (type path)
  8830. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8831. This is still an experimental function, your mileage may vary."
  8832. (cond
  8833. ((member type '("http" "https" "news" "ftp"))
  8834. ;; standard Internet links are the same.
  8835. nil)
  8836. ((and (equal type "irc") (string-match "^//" path))
  8837. ;; Planner has two / at the beginning of an irc link, we have 1.
  8838. ;; We should have zero, actually....
  8839. (setq path (substring path 1)))
  8840. ((and (equal type "lisp") (string-match "^/" path))
  8841. ;; Planner has a slash, we do not.
  8842. (setq type "elisp" path (substring path 1)))
  8843. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8844. ;; A typical message link. Planner has the id after the final slash,
  8845. ;; we separate it with a hash mark
  8846. (setq path (concat (match-string 1 path) "#"
  8847. (org-remove-angle-brackets (match-string 2 path)))))
  8848. )
  8849. (cons type path))
  8850. (defun org-find-file-at-mouse (ev)
  8851. "Open file link or URL at mouse."
  8852. (interactive "e")
  8853. (mouse-set-point ev)
  8854. (org-open-at-point 'in-emacs))
  8855. (defun org-open-at-mouse (ev)
  8856. "Open file link or URL at mouse.
  8857. See the docstring of `org-open-file' for details."
  8858. (interactive "e")
  8859. (mouse-set-point ev)
  8860. (if (eq major-mode 'org-agenda-mode)
  8861. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8862. (org-open-at-point))
  8863. (defvar org-window-config-before-follow-link nil
  8864. "The window configuration before following a link.
  8865. This is saved in case the need arises to restore it.")
  8866. (defvar org-open-link-marker (make-marker)
  8867. "Marker pointing to the location where `org-open-at-point; was called.")
  8868. ;;;###autoload
  8869. (defun org-open-at-point-global ()
  8870. "Follow a link like Org-mode does.
  8871. This command can be called in any mode to follow a link that has
  8872. Org-mode syntax."
  8873. (interactive)
  8874. (org-run-like-in-org-mode 'org-open-at-point))
  8875. ;;;###autoload
  8876. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8877. "Open a link in the string S, as if it was in Org-mode."
  8878. (interactive "sLink: \nP")
  8879. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8880. (with-temp-buffer
  8881. (let ((org-inhibit-startup (not reference-buffer)))
  8882. (org-mode)
  8883. (insert s)
  8884. (goto-char (point-min))
  8885. (when reference-buffer
  8886. (setq org-link-abbrev-alist-local
  8887. (with-current-buffer reference-buffer
  8888. org-link-abbrev-alist-local)))
  8889. (org-open-at-point arg reference-buffer)))))
  8890. (defvar org-open-at-point-functions nil
  8891. "Hook that is run when following a link at point.
  8892. Functions in this hook must return t if they identify and follow
  8893. a link at point. If they don't find anything interesting at point,
  8894. they must return nil.")
  8895. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8896. (defun org-open-at-point (&optional arg reference-buffer)
  8897. "Open link at or after point.
  8898. If there is no link at point, this function will search forward up to
  8899. the end of the current line.
  8900. Normally, files will be opened by an appropriate application. If the
  8901. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8902. With a double prefix argument, try to open outside of Emacs, in the
  8903. application the system uses for this file type."
  8904. (interactive "P")
  8905. ;; if in a code block, then open the block's results
  8906. (unless (call-interactively #'org-babel-open-src-block-result)
  8907. (org-load-modules-maybe)
  8908. (move-marker org-open-link-marker (point))
  8909. (setq org-window-config-before-follow-link (current-window-configuration))
  8910. (org-remove-occur-highlights nil nil t)
  8911. (cond
  8912. ((and (org-at-heading-p)
  8913. (not (org-at-timestamp-p t))
  8914. (not (org-in-regexp
  8915. (concat org-plain-link-re "\\|"
  8916. org-bracket-link-regexp "\\|"
  8917. org-angle-link-re "\\|"
  8918. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8919. (not (get-text-property (point) 'org-linked-text)))
  8920. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8921. (lk0 (car lkall))
  8922. (lk (if (stringp lk0) (list lk0) lk0))
  8923. (lkend (cdr lkall)))
  8924. (mapcar (lambda(l)
  8925. (search-forward l nil lkend)
  8926. (goto-char (match-beginning 0))
  8927. (org-open-at-point))
  8928. lk))
  8929. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8930. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8931. ((and (org-at-timestamp-p t)
  8932. (not (org-in-regexp org-bracket-link-regexp)))
  8933. (org-follow-timestamp-link))
  8934. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8935. (not (org-in-regexp org-any-link-re)))
  8936. (org-footnote-action))
  8937. (t
  8938. (let (type path link line search (pos (point)))
  8939. (catch 'match
  8940. (save-excursion
  8941. (skip-chars-forward "^]\n\r")
  8942. (when (org-in-regexp org-bracket-link-regexp 1)
  8943. (setq link (org-extract-attributes
  8944. (org-link-unescape (org-match-string-no-properties 1))))
  8945. (while (string-match " *\n *" link)
  8946. (setq link (replace-match " " t t link)))
  8947. (setq link (org-link-expand-abbrev link))
  8948. (cond
  8949. ((or (file-name-absolute-p link)
  8950. (string-match "^\\.\\.?/" link))
  8951. (setq type "file" path link))
  8952. ((string-match org-link-re-with-space3 link)
  8953. (setq type (match-string 1 link) path (match-string 2 link)))
  8954. ((string-match "^help:+\\(.+\\)" link)
  8955. (setq type "help" path (match-string 1 link)))
  8956. (t (setq type "thisfile" path link)))
  8957. (throw 'match t)))
  8958. (when (get-text-property (point) 'org-linked-text)
  8959. (setq type "thisfile"
  8960. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8961. (1+ (point)) (point))
  8962. path (buffer-substring
  8963. (or (previous-single-property-change pos 'org-linked-text)
  8964. (point-min))
  8965. (or (next-single-property-change pos 'org-linked-text)
  8966. (point-max))))
  8967. (throw 'match t))
  8968. (save-excursion
  8969. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8970. (when (or (org-in-regexp org-angle-link-re)
  8971. (and plinkpos (goto-char (car plinkpos))
  8972. (save-match-data (not (looking-back "\\[\\[")))))
  8973. (setq type (match-string 1)
  8974. path (org-link-unescape (match-string 2)))
  8975. (throw 'match t))))
  8976. (save-excursion
  8977. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8978. (setq type "tags"
  8979. path (match-string 1))
  8980. (while (string-match ":" path)
  8981. (setq path (replace-match "+" t t path)))
  8982. (throw 'match t)))
  8983. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8984. (setq type "tree-match"
  8985. path (match-string 1))
  8986. (throw 'match t)))
  8987. (unless path
  8988. (user-error "No link found"))
  8989. ;; switch back to reference buffer
  8990. ;; needed when if called in a temporary buffer through
  8991. ;; org-open-link-from-string
  8992. (with-current-buffer (or reference-buffer (current-buffer))
  8993. ;; Remove any trailing spaces in path
  8994. (if (string-match " +\\'" path)
  8995. (setq path (replace-match "" t t path)))
  8996. (if (and org-link-translation-function
  8997. (fboundp org-link-translation-function))
  8998. ;; Check if we need to translate the link
  8999. (let ((tmp (funcall org-link-translation-function type path)))
  9000. (setq type (car tmp) path (cdr tmp))))
  9001. (cond
  9002. ((assoc type org-link-protocols)
  9003. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9004. ((equal type "help")
  9005. (let ((f-or-v (intern path)))
  9006. (cond ((fboundp f-or-v)
  9007. (describe-function f-or-v))
  9008. ((boundp f-or-v)
  9009. (describe-variable f-or-v))
  9010. (t (error "Not a known function or variable")))))
  9011. ((equal type "mailto")
  9012. (let ((cmd (car org-link-mailto-program))
  9013. (args (cdr org-link-mailto-program)) args1
  9014. (address path) (subject "") a)
  9015. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9016. (setq address (match-string 1 path)
  9017. subject (org-link-escape (match-string 2 path))))
  9018. (while args
  9019. (cond
  9020. ((not (stringp (car args))) (push (pop args) args1))
  9021. (t (setq a (pop args))
  9022. (if (string-match "%a" a)
  9023. (setq a (replace-match address t t a)))
  9024. (if (string-match "%s" a)
  9025. (setq a (replace-match subject t t a)))
  9026. (push a args1))))
  9027. (apply cmd (nreverse args1))))
  9028. ((member type '("http" "https" "ftp" "news"))
  9029. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9030. (org-link-escape
  9031. path org-link-escape-chars-browser)
  9032. path))))
  9033. ((string= type "doi")
  9034. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9035. (org-link-escape
  9036. path org-link-escape-chars-browser)
  9037. path))))
  9038. ((member type '("message"))
  9039. (browse-url (concat type ":" path)))
  9040. ((string= type "tags")
  9041. (org-tags-view arg path))
  9042. ((string= type "tree-match")
  9043. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9044. ((string= type "file")
  9045. (if (string-match "::\\([0-9]+\\)\\'" path)
  9046. (setq line (string-to-number (match-string 1 path))
  9047. path (substring path 0 (match-beginning 0)))
  9048. (if (string-match "::\\(.+\\)\\'" path)
  9049. (setq search (match-string 1 path)
  9050. path (substring path 0 (match-beginning 0)))))
  9051. (if (string-match "[*?{]" (file-name-nondirectory path))
  9052. (dired path)
  9053. (org-open-file path arg line search)))
  9054. ((string= type "shell")
  9055. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9056. (cmd path))
  9057. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9058. (string-match org-confirm-shell-link-not-regexp cmd))
  9059. (not org-confirm-shell-link-function)
  9060. (funcall org-confirm-shell-link-function
  9061. (format "Execute \"%s\" in shell? "
  9062. (org-add-props cmd nil
  9063. 'face 'org-warning))))
  9064. (progn
  9065. (message "Executing %s" cmd)
  9066. (shell-command cmd buf)
  9067. (if (featurep 'midnight)
  9068. (setq clean-buffer-list-kill-buffer-names
  9069. (cons buf clean-buffer-list-kill-buffer-names))))
  9070. (error "Abort"))))
  9071. ((string= type "elisp")
  9072. (let ((cmd path))
  9073. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9074. (string-match org-confirm-elisp-link-not-regexp cmd))
  9075. (not org-confirm-elisp-link-function)
  9076. (funcall org-confirm-elisp-link-function
  9077. (format "Execute \"%s\" as elisp? "
  9078. (org-add-props cmd nil
  9079. 'face 'org-warning))))
  9080. (message "%s => %s" cmd
  9081. (if (equal (string-to-char cmd) ?\()
  9082. (eval (read cmd))
  9083. (call-interactively (read cmd))))
  9084. (error "Abort"))))
  9085. ((and (string= type "thisfile")
  9086. (run-hook-with-args-until-success
  9087. 'org-open-link-functions path)))
  9088. ((string= type "thisfile")
  9089. (if arg
  9090. (switch-to-buffer-other-window
  9091. (org-get-buffer-for-internal-link (current-buffer)))
  9092. (org-mark-ring-push))
  9093. (let ((cmd `(org-link-search
  9094. ,path
  9095. ,(cond ((equal arg '(4)) ''occur)
  9096. ((equal arg '(16)) ''org-occur))
  9097. ,pos)))
  9098. (condition-case nil (let ((org-link-search-inhibit-query t))
  9099. (eval cmd))
  9100. (error (progn (widen) (eval cmd))))))
  9101. (t (browse-url-at-point)))))))
  9102. (move-marker org-open-link-marker nil)
  9103. (run-hook-with-args 'org-follow-link-hook)))
  9104. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9105. "Offer links in the current entry and return the selected link.
  9106. If there is only one link, return it.
  9107. If NTH is an integer, return the NTH link found.
  9108. If ZERO is a string, check also this string for a link, and if
  9109. there is one, return it."
  9110. (with-current-buffer buffer
  9111. (save-excursion
  9112. (save-restriction
  9113. (widen)
  9114. (goto-char marker)
  9115. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9116. "\\(" org-angle-link-re "\\)\\|"
  9117. "\\(" org-plain-link-re "\\)"))
  9118. (cnt ?0)
  9119. (in-emacs (if (integerp nth) nil nth))
  9120. have-zero end links link c)
  9121. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9122. (push (match-string 0 zero) links)
  9123. (setq cnt (1- cnt) have-zero t))
  9124. (save-excursion
  9125. (org-back-to-heading t)
  9126. (setq end (save-excursion (outline-next-heading) (point)))
  9127. (while (re-search-forward re end t)
  9128. (push (match-string 0) links))
  9129. (setq links (org-uniquify (reverse links))))
  9130. (cond
  9131. ((null links)
  9132. (message "No links"))
  9133. ((equal (length links) 1)
  9134. (setq link (car links)))
  9135. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9136. (setq link (nth (if have-zero nth (1- nth)) links)))
  9137. (t ; we have to select a link
  9138. (save-excursion
  9139. (save-window-excursion
  9140. (delete-other-windows)
  9141. (with-output-to-temp-buffer "*Select Link*"
  9142. (mapc (lambda (l)
  9143. (if (not (string-match org-bracket-link-regexp l))
  9144. (princ (format "[%c] %s\n" (incf cnt)
  9145. (org-remove-angle-brackets l)))
  9146. (if (match-end 3)
  9147. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9148. (match-string 3 l) (match-string 1 l)))
  9149. (princ (format "[%c] %s\n" (incf cnt)
  9150. (match-string 1 l))))))
  9151. links))
  9152. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9153. (message "Select link to open, RET to open all:")
  9154. (setq c (read-char-exclusive))
  9155. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9156. (when (equal c ?q) (error "Abort"))
  9157. (if (equal c ?\C-m)
  9158. (setq link links)
  9159. (setq nth (- c ?0))
  9160. (if have-zero (setq nth (1+ nth)))
  9161. (unless (and (integerp nth) (>= (length links) nth))
  9162. (error "Invalid link selection"))
  9163. (setq link (nth (1- nth) links)))))
  9164. (cons link end))))))
  9165. ;; Add special file links that specify the way of opening
  9166. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9167. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9168. (defun org-open-file-with-system (path)
  9169. "Open file at PATH using the system way of opening it."
  9170. (org-open-file path 'system))
  9171. (defun org-open-file-with-emacs (path)
  9172. "Open file at PATH in Emacs."
  9173. (org-open-file path 'emacs))
  9174. (defun org-remove-file-link-modifiers ()
  9175. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9176. (goto-char (point-min))
  9177. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9178. (org-if-unprotected
  9179. (replace-match "file:" t t))))
  9180. (eval-after-load "org-exp"
  9181. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9182. 'org-remove-file-link-modifiers))
  9183. ;;; File search
  9184. (defvar org-create-file-search-functions nil
  9185. "List of functions to construct the right search string for a file link.
  9186. These functions are called in turn with point at the location to
  9187. which the link should point.
  9188. A function in the hook should first test if it would like to
  9189. handle this file type, for example by checking the `major-mode'
  9190. or the file extension. If it decides not to handle this file, it
  9191. should just return nil to give other functions a chance. If it
  9192. does handle the file, it must return the search string to be used
  9193. when following the link. The search string will be part of the
  9194. file link, given after a double colon, and `org-open-at-point'
  9195. will automatically search for it. If special measures must be
  9196. taken to make the search successful, another function should be
  9197. added to the companion hook `org-execute-file-search-functions',
  9198. which see.
  9199. A function in this hook may also use `setq' to set the variable
  9200. `description' to provide a suggestion for the descriptive text to
  9201. be used for this link when it gets inserted into an Org-mode
  9202. buffer with \\[org-insert-link].")
  9203. (defvar org-execute-file-search-functions nil
  9204. "List of functions to execute a file search triggered by a link.
  9205. Functions added to this hook must accept a single argument, the
  9206. search string that was part of the file link, the part after the
  9207. double colon. The function must first check if it would like to
  9208. handle this search, for example by checking the `major-mode' or
  9209. the file extension. If it decides not to handle this search, it
  9210. should just return nil to give other functions a chance. If it
  9211. does handle the search, it must return a non-nil value to keep
  9212. other functions from trying.
  9213. Each function can access the current prefix argument through the
  9214. variable `current-prefix-argument'. Note that a single prefix is
  9215. used to force opening a link in Emacs, so it may be good to only
  9216. use a numeric or double prefix to guide the search function.
  9217. In case this is needed, a function in this hook can also restore
  9218. the window configuration before `org-open-at-point' was called using:
  9219. (set-window-configuration org-window-config-before-follow-link)")
  9220. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9221. (defun org-link-search (s &optional type avoid-pos stealth)
  9222. "Search for a link search option.
  9223. If S is surrounded by forward slashes, it is interpreted as a
  9224. regular expression. In org-mode files, this will create an `org-occur'
  9225. sparse tree. In ordinary files, `occur' will be used to list matches.
  9226. If the current buffer is in `dired-mode', grep will be used to search
  9227. in all files. If AVOID-POS is given, ignore matches near that position.
  9228. When optional argument STEALTH is non-nil, do not modify
  9229. visibility around point, thus ignoring
  9230. `org-show-hierarchy-above', `org-show-following-heading' and
  9231. `org-show-siblings' variables."
  9232. (let ((case-fold-search t)
  9233. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9234. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9235. (append '(("") (" ") ("\t") ("\n"))
  9236. org-emphasis-alist)
  9237. "\\|") "\\)"))
  9238. (pos (point))
  9239. (pre nil) (post nil)
  9240. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9241. (cond
  9242. ;; First check if there are any special search functions
  9243. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9244. ;; Now try the builtin stuff
  9245. ((and (equal (string-to-char s0) ?#)
  9246. (> (length s0) 1)
  9247. (save-excursion
  9248. (goto-char (point-min))
  9249. (and
  9250. (re-search-forward
  9251. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9252. (setq type 'dedicated
  9253. pos (match-beginning 0))))
  9254. ;; There is an exact target for this
  9255. (goto-char pos)
  9256. (org-back-to-heading t)))
  9257. ((save-excursion
  9258. (goto-char (point-min))
  9259. (and
  9260. (re-search-forward
  9261. (concat "<<" (regexp-quote s0) ">>") nil t)
  9262. (setq type 'dedicated
  9263. pos (match-beginning 0))))
  9264. ;; There is an exact target for this
  9265. (goto-char pos))
  9266. ((save-excursion
  9267. (goto-char (point-min))
  9268. (and
  9269. (re-search-forward
  9270. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9271. (setq type 'dedicated pos (match-beginning 0))))
  9272. ;; Found an invisible target.
  9273. (goto-char pos))
  9274. ((save-excursion
  9275. (goto-char (point-min))
  9276. (and
  9277. (re-search-forward
  9278. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9279. (setq type 'dedicated pos (match-beginning 0))))
  9280. ;; Found an element with a matching #+name affiliated keyword.
  9281. (goto-char pos))
  9282. ((and (string-match "^(\\(.*\\))$" s0)
  9283. (save-excursion
  9284. (goto-char (point-min))
  9285. (and
  9286. (re-search-forward
  9287. (concat "[^[]" (regexp-quote
  9288. (format org-coderef-label-format
  9289. (match-string 1 s0))))
  9290. nil t)
  9291. (setq type 'dedicated
  9292. pos (1+ (match-beginning 0))))))
  9293. ;; There is a coderef target for this
  9294. (goto-char pos))
  9295. ((string-match "^/\\(.*\\)/$" s)
  9296. ;; A regular expression
  9297. (cond
  9298. ((derived-mode-p 'org-mode)
  9299. (org-occur (match-string 1 s)))
  9300. ;;((eq major-mode 'dired-mode)
  9301. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9302. (t (org-do-occur (match-string 1 s)))))
  9303. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9304. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9305. (goto-char (point-min))
  9306. (cond
  9307. ((let (case-fold-search)
  9308. (re-search-forward (format org-complex-heading-regexp-format
  9309. (regexp-quote s))
  9310. nil t))
  9311. ;; OK, found a match
  9312. (setq type 'dedicated)
  9313. (goto-char (match-beginning 0)))
  9314. ((and (not org-link-search-inhibit-query)
  9315. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9316. (y-or-n-p "No match - create this as a new heading? "))
  9317. (goto-char (point-max))
  9318. (or (bolp) (newline))
  9319. (insert "* " s "\n")
  9320. (beginning-of-line 0))
  9321. (t
  9322. (goto-char pos)
  9323. (error "No match"))))
  9324. (t
  9325. ;; A normal search string
  9326. (when (equal (string-to-char s) ?*)
  9327. ;; Anchor on headlines, post may include tags.
  9328. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9329. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9330. s (substring s 1)))
  9331. (remove-text-properties
  9332. 0 (length s)
  9333. '(face nil mouse-face nil keymap nil fontified nil) s)
  9334. ;; Make a series of regular expressions to find a match
  9335. (setq words (org-split-string s "[ \n\r\t]+")
  9336. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9337. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9338. "\\)" markers)
  9339. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9340. re2a (concat "[ \t\r\n]" re2a_)
  9341. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9342. re4 (concat "[^a-zA-Z_]" re4_)
  9343. re1 (concat pre re2 post)
  9344. re3 (concat pre (if pre re4_ re4) post)
  9345. re5 (concat pre ".*" re4)
  9346. re2 (concat pre re2)
  9347. re2a (concat pre (if pre re2a_ re2a))
  9348. re4 (concat pre (if pre re4_ re4))
  9349. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9350. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9351. re5 "\\)"
  9352. ))
  9353. (cond
  9354. ((eq type 'org-occur) (org-occur reall))
  9355. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9356. (t (goto-char (point-min))
  9357. (setq type 'fuzzy)
  9358. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9359. (org-search-not-self 1 re1 nil t)
  9360. (org-search-not-self 1 re2 nil t)
  9361. (org-search-not-self 1 re2a nil t)
  9362. (org-search-not-self 1 re3 nil t)
  9363. (org-search-not-self 1 re4 nil t)
  9364. (org-search-not-self 1 re5 nil t)
  9365. )
  9366. (goto-char (match-beginning 1))
  9367. (goto-char pos)
  9368. (error "No match"))))))
  9369. (and (derived-mode-p 'org-mode)
  9370. (not stealth)
  9371. (org-show-context 'link-search))
  9372. type))
  9373. (defun org-search-not-self (group &rest args)
  9374. "Execute `re-search-forward', but only accept matches that do not
  9375. enclose the position of `org-open-link-marker'."
  9376. (let ((m org-open-link-marker))
  9377. (catch 'exit
  9378. (while (apply 're-search-forward args)
  9379. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9380. (goto-char (match-end group))
  9381. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9382. (> (match-beginning 0) (marker-position m))
  9383. (< (match-end 0) (marker-position m)))
  9384. (save-match-data
  9385. (or (not (org-in-regexp
  9386. org-bracket-link-analytic-regexp 1))
  9387. (not (match-end 4)) ; no description
  9388. (and (<= (match-beginning 4) (point))
  9389. (>= (match-end 4) (point))))))
  9390. (throw 'exit (point))))))))
  9391. (defun org-get-buffer-for-internal-link (buffer)
  9392. "Return a buffer to be used for displaying the link target of internal links."
  9393. (cond
  9394. ((not org-display-internal-link-with-indirect-buffer)
  9395. buffer)
  9396. ((string-match "(Clone)$" (buffer-name buffer))
  9397. (message "Buffer is already a clone, not making another one")
  9398. ;; we also do not modify visibility in this case
  9399. buffer)
  9400. (t ; make a new indirect buffer for displaying the link
  9401. (let* ((bn (buffer-name buffer))
  9402. (ibn (concat bn "(Clone)"))
  9403. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9404. (with-current-buffer ib (org-overview))
  9405. ib))))
  9406. (defun org-do-occur (regexp &optional cleanup)
  9407. "Call the Emacs command `occur'.
  9408. If CLEANUP is non-nil, remove the printout of the regular expression
  9409. in the *Occur* buffer. This is useful if the regex is long and not useful
  9410. to read."
  9411. (occur regexp)
  9412. (when cleanup
  9413. (let ((cwin (selected-window)) win beg end)
  9414. (when (setq win (get-buffer-window "*Occur*"))
  9415. (select-window win))
  9416. (goto-char (point-min))
  9417. (when (re-search-forward "match[a-z]+" nil t)
  9418. (setq beg (match-end 0))
  9419. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9420. (setq end (1- (match-beginning 0)))))
  9421. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9422. (goto-char (point-min))
  9423. (select-window cwin))))
  9424. ;;; The mark ring for links jumps
  9425. (defvar org-mark-ring nil
  9426. "Mark ring for positions before jumps in Org-mode.")
  9427. (defvar org-mark-ring-last-goto nil
  9428. "Last position in the mark ring used to go back.")
  9429. ;; Fill and close the ring
  9430. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9431. (loop for i from 1 to org-mark-ring-length do
  9432. (push (make-marker) org-mark-ring))
  9433. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9434. org-mark-ring)
  9435. (defun org-mark-ring-push (&optional pos buffer)
  9436. "Put the current position or POS into the mark ring and rotate it."
  9437. (interactive)
  9438. (setq pos (or pos (point)))
  9439. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9440. (move-marker (car org-mark-ring)
  9441. (or pos (point))
  9442. (or buffer (current-buffer)))
  9443. (message "%s"
  9444. (substitute-command-keys
  9445. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9446. (defun org-mark-ring-goto (&optional n)
  9447. "Jump to the previous position in the mark ring.
  9448. With prefix arg N, jump back that many stored positions. When
  9449. called several times in succession, walk through the entire ring.
  9450. Org-mode commands jumping to a different position in the current file,
  9451. or to another Org-mode file, automatically push the old position
  9452. onto the ring."
  9453. (interactive "p")
  9454. (let (p m)
  9455. (if (eq last-command this-command)
  9456. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9457. (setq p org-mark-ring))
  9458. (setq org-mark-ring-last-goto p)
  9459. (setq m (car p))
  9460. (org-pop-to-buffer-same-window (marker-buffer m))
  9461. (goto-char m)
  9462. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9463. (defun org-remove-angle-brackets (s)
  9464. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9465. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9466. s)
  9467. (defun org-add-angle-brackets (s)
  9468. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9469. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9470. s)
  9471. (defun org-remove-double-quotes (s)
  9472. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9473. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9474. s)
  9475. ;;; Following specific links
  9476. (defun org-follow-timestamp-link ()
  9477. "Open an agenda view for the time-stamp date/range at point."
  9478. (cond
  9479. ((org-at-date-range-p t)
  9480. (let ((org-agenda-start-on-weekday)
  9481. (t1 (match-string 1))
  9482. (t2 (match-string 2)) tt1 tt2)
  9483. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9484. tt2 (time-to-days (org-time-string-to-time t2)))
  9485. (let ((org-agenda-buffer-tmp-name
  9486. (format "*Org Agenda(a:%s)"
  9487. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9488. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9489. ((org-at-timestamp-p t)
  9490. (let ((org-agenda-buffer-tmp-name
  9491. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9492. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9493. (substring (match-string 1) 0 10)))
  9494. 1)))
  9495. (t (error "This should not happen"))))
  9496. ;;; Following file links
  9497. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9498. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9499. (declare-function mailcap-mime-info
  9500. "mailcap" (string &optional request no-decode))
  9501. (defvar org-wait nil)
  9502. (defun org-open-file (path &optional in-emacs line search)
  9503. "Open the file at PATH.
  9504. First, this expands any special file name abbreviations. Then the
  9505. configuration variable `org-file-apps' is checked if it contains an
  9506. entry for this file type, and if yes, the corresponding command is launched.
  9507. If no application is found, Emacs simply visits the file.
  9508. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9509. With a double \\[universal-argument] \\[universal-argument] \
  9510. prefix arg, Org tries to avoid opening in Emacs
  9511. and to use an external application to visit the file.
  9512. Optional LINE specifies a line to go to, optional SEARCH a string
  9513. to search for. If LINE or SEARCH is given, the file will be
  9514. opened in Emacs, unless an entry from org-file-apps that makes
  9515. use of groups in a regexp matches.
  9516. If you want to change the way frames are used when following a
  9517. link, please customize `org-link-frame-setup'.
  9518. If the file does not exist, an error is thrown."
  9519. (let* ((file (if (equal path "")
  9520. buffer-file-name
  9521. (substitute-in-file-name (expand-file-name path))))
  9522. (file-apps (append org-file-apps (org-default-apps)))
  9523. (apps (org-remove-if
  9524. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9525. (apps-dlink (org-remove-if-not
  9526. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9527. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9528. (dirp (if remp nil (file-directory-p file)))
  9529. (file (if (and dirp org-open-directory-means-index-dot-org)
  9530. (concat (file-name-as-directory file) "index.org")
  9531. file))
  9532. (a-m-a-p (assq 'auto-mode apps))
  9533. (dfile (downcase file))
  9534. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9535. (link (cond ((and (eq line nil)
  9536. (eq search nil))
  9537. file)
  9538. (line
  9539. (concat file "::" (number-to-string line)))
  9540. (search
  9541. (concat file "::" search))))
  9542. (dlink (downcase link))
  9543. (old-buffer (current-buffer))
  9544. (old-pos (point))
  9545. (old-mode major-mode)
  9546. ext cmd link-match-data)
  9547. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9548. (setq ext (match-string 1 dfile))
  9549. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9550. (setq ext (match-string 1 dfile))))
  9551. (cond
  9552. ((member in-emacs '((16) system))
  9553. (setq cmd (cdr (assoc 'system apps))))
  9554. (in-emacs (setq cmd 'emacs))
  9555. (t
  9556. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9557. (and dirp (cdr (assoc 'directory apps)))
  9558. ; first, try matching against apps-dlink
  9559. ; if we get a match here, store the match data for later
  9560. (let ((match (assoc-default dlink apps-dlink
  9561. 'string-match)))
  9562. (if match
  9563. (progn (setq link-match-data (match-data))
  9564. match)
  9565. (progn (setq in-emacs (or in-emacs line search))
  9566. nil))) ; if we have no match in apps-dlink,
  9567. ; always open the file in emacs if line or search
  9568. ; is given (for backwards compatibility)
  9569. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9570. 'string-match)
  9571. (cdr (assoc ext apps))
  9572. (cdr (assoc t apps))))))
  9573. (when (eq cmd 'system)
  9574. (setq cmd (cdr (assoc 'system apps))))
  9575. (when (eq cmd 'default)
  9576. (setq cmd (cdr (assoc t apps))))
  9577. (when (eq cmd 'mailcap)
  9578. (require 'mailcap)
  9579. (mailcap-parse-mailcaps)
  9580. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9581. (command (mailcap-mime-info mime-type)))
  9582. (if (stringp command)
  9583. (setq cmd command)
  9584. (setq cmd 'emacs))))
  9585. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9586. (not (file-exists-p file))
  9587. (not org-open-non-existing-files))
  9588. (error "No such file: %s" file))
  9589. (cond
  9590. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9591. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9592. (while (string-match "['\"]%s['\"]" cmd)
  9593. (setq cmd (replace-match "%s" t t cmd)))
  9594. (while (string-match "%s" cmd)
  9595. (setq cmd (replace-match
  9596. (save-match-data
  9597. (shell-quote-argument
  9598. (convert-standard-filename file)))
  9599. t t cmd)))
  9600. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9601. (save-match-data
  9602. (let ((match-index 1)
  9603. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9604. (set-match-data link-match-data)
  9605. (while (<= match-index number-of-groups)
  9606. (let ((regex (concat "%" (number-to-string match-index)))
  9607. (replace-with (match-string match-index dlink)))
  9608. (while (string-match regex cmd)
  9609. (setq cmd (replace-match replace-with t t cmd))))
  9610. (setq match-index (+ match-index 1)))))
  9611. (save-window-excursion
  9612. (message "Running %s...done" cmd)
  9613. (start-process-shell-command cmd nil cmd)
  9614. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9615. ((or (stringp cmd)
  9616. (eq cmd 'emacs))
  9617. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9618. (widen)
  9619. (if line (org-goto-line line)
  9620. (if search (org-link-search search))))
  9621. ((consp cmd)
  9622. (let ((file (convert-standard-filename file)))
  9623. (save-match-data
  9624. (set-match-data link-match-data)
  9625. (eval cmd))))
  9626. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9627. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9628. (or (not (equal old-buffer (current-buffer)))
  9629. (not (equal old-pos (point))))
  9630. (org-mark-ring-push old-pos old-buffer))))
  9631. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9632. "This function returns non-nil if `entry' uses a regular
  9633. expression which should be matched against the whole link by
  9634. org-open-file.
  9635. It assumes that is the case when the entry uses a regular
  9636. expression which has at least one grouping construct and the
  9637. action is either a lisp form or a command string containing
  9638. '%1', i.e. using at least one subexpression match as a
  9639. parameter."
  9640. (let ((selector (car entry))
  9641. (action (cdr entry)))
  9642. (if (stringp selector)
  9643. (and (> (regexp-opt-depth selector) 0)
  9644. (or (and (stringp action)
  9645. (string-match "%[0-9]" action))
  9646. (consp action)))
  9647. nil)))
  9648. (defun org-default-apps ()
  9649. "Return the default applications for this operating system."
  9650. (cond
  9651. ((eq system-type 'darwin)
  9652. org-file-apps-defaults-macosx)
  9653. ((eq system-type 'windows-nt)
  9654. org-file-apps-defaults-windowsnt)
  9655. (t org-file-apps-defaults-gnu)))
  9656. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9657. "Convert extensions to regular expressions in the cars of LIST.
  9658. Also, weed out any non-string entries, because the return value is used
  9659. only for regexp matching.
  9660. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9661. point to the symbol `emacs', indicating that the file should
  9662. be opened in Emacs."
  9663. (append
  9664. (delq nil
  9665. (mapcar (lambda (x)
  9666. (if (not (stringp (car x)))
  9667. nil
  9668. (if (string-match "\\W" (car x))
  9669. x
  9670. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9671. list))
  9672. (if add-auto-mode
  9673. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9674. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9675. (defun org-file-remote-p (file)
  9676. "Test whether FILE specifies a location on a remote system.
  9677. Return non-nil if the location is indeed remote.
  9678. For example, the filename \"/user@host:/foo\" specifies a location
  9679. on the system \"/user@host:\"."
  9680. (cond ((fboundp 'file-remote-p)
  9681. (file-remote-p file))
  9682. ((fboundp 'tramp-handle-file-remote-p)
  9683. (tramp-handle-file-remote-p file))
  9684. ((and (boundp 'ange-ftp-name-format)
  9685. (string-match (car ange-ftp-name-format) file))
  9686. t)))
  9687. ;;;; Refiling
  9688. (defun org-get-org-file ()
  9689. "Read a filename, with default directory `org-directory'."
  9690. (let ((default (or org-default-notes-file remember-data-file)))
  9691. (read-file-name (format "File name [%s]: " default)
  9692. (file-name-as-directory org-directory)
  9693. default)))
  9694. (defun org-notes-order-reversed-p ()
  9695. "Check if the current file should receive notes in reversed order."
  9696. (cond
  9697. ((not org-reverse-note-order) nil)
  9698. ((eq t org-reverse-note-order) t)
  9699. ((not (listp org-reverse-note-order)) nil)
  9700. (t (catch 'exit
  9701. (let ((all org-reverse-note-order)
  9702. entry)
  9703. (while (setq entry (pop all))
  9704. (if (string-match (car entry) buffer-file-name)
  9705. (throw 'exit (cdr entry))))
  9706. nil)))))
  9707. (defvar org-refile-target-table nil
  9708. "The list of refile targets, created by `org-refile'.")
  9709. (defvar org-agenda-new-buffers nil
  9710. "Buffers created to visit agenda files.")
  9711. (defvar org-refile-cache nil
  9712. "Cache for refile targets.")
  9713. (defvar org-refile-markers nil
  9714. "All the markers used for caching refile locations.")
  9715. (defun org-refile-marker (pos)
  9716. "Get a new refile marker, but only if caching is in use."
  9717. (if (not org-refile-use-cache)
  9718. pos
  9719. (let ((m (make-marker)))
  9720. (move-marker m pos)
  9721. (push m org-refile-markers)
  9722. m)))
  9723. (defun org-refile-cache-clear ()
  9724. "Clear the refile cache and disable all the markers."
  9725. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9726. (setq org-refile-markers nil)
  9727. (setq org-refile-cache nil)
  9728. (message "Refile cache has been cleared"))
  9729. (defun org-refile-cache-check-set (set)
  9730. "Check if all the markers in the cache still have live buffers."
  9731. (let (marker)
  9732. (catch 'exit
  9733. (while (and set (setq marker (nth 3 (pop set))))
  9734. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9735. (when (and marker (null (marker-buffer marker)))
  9736. (message "not found") (sit-for 3)
  9737. (throw 'exit nil)))
  9738. t)))
  9739. (defun org-refile-cache-put (set &rest identifiers)
  9740. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9741. (let* ((key (sha1 (prin1-to-string identifiers)))
  9742. (entry (assoc key org-refile-cache)))
  9743. (if entry
  9744. (setcdr entry set)
  9745. (push (cons key set) org-refile-cache))))
  9746. (defun org-refile-cache-get (&rest identifiers)
  9747. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9748. (cond
  9749. ((not org-refile-cache) nil)
  9750. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9751. (t
  9752. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9753. org-refile-cache))))
  9754. (and set (org-refile-cache-check-set set) set)))))
  9755. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9756. "Produce a table with refile targets."
  9757. (let ((case-fold-search nil)
  9758. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9759. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9760. targets tgs txt re files f desc descre fast-path-p level pos0)
  9761. (message "Getting targets...")
  9762. (with-current-buffer (or default-buffer (current-buffer))
  9763. (while (setq entry (pop entries))
  9764. (setq files (car entry) desc (cdr entry))
  9765. (setq fast-path-p nil)
  9766. (cond
  9767. ((null files) (setq files (list (current-buffer))))
  9768. ((eq files 'org-agenda-files)
  9769. (setq files (org-agenda-files 'unrestricted)))
  9770. ((and (symbolp files) (fboundp files))
  9771. (setq files (funcall files)))
  9772. ((and (symbolp files) (boundp files))
  9773. (setq files (symbol-value files))))
  9774. (if (stringp files) (setq files (list files)))
  9775. (cond
  9776. ((eq (car desc) :tag)
  9777. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9778. ((eq (car desc) :todo)
  9779. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9780. ((eq (car desc) :regexp)
  9781. (setq descre (cdr desc)))
  9782. ((eq (car desc) :level)
  9783. (setq descre (concat "^\\*\\{" (number-to-string
  9784. (if org-odd-levels-only
  9785. (1- (* 2 (cdr desc)))
  9786. (cdr desc)))
  9787. "\\}[ \t]")))
  9788. ((eq (car desc) :maxlevel)
  9789. (setq fast-path-p t)
  9790. (setq descre (concat "^\\*\\{1," (number-to-string
  9791. (if org-odd-levels-only
  9792. (1- (* 2 (cdr desc)))
  9793. (cdr desc)))
  9794. "\\}[ \t]")))
  9795. (t (error "Bad refiling target description %s" desc)))
  9796. (while (setq f (pop files))
  9797. (with-current-buffer
  9798. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9799. (or
  9800. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9801. (progn
  9802. (if (bufferp f) (setq f (buffer-file-name
  9803. (buffer-base-buffer f))))
  9804. (setq f (and f (expand-file-name f)))
  9805. (if (eq org-refile-use-outline-path 'file)
  9806. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9807. (save-excursion
  9808. (save-restriction
  9809. (widen)
  9810. (goto-char (point-min))
  9811. (while (re-search-forward descre nil t)
  9812. (goto-char (setq pos0 (point-at-bol)))
  9813. (catch 'next
  9814. (when org-refile-target-verify-function
  9815. (save-match-data
  9816. (or (funcall org-refile-target-verify-function)
  9817. (throw 'next t))))
  9818. (when (and (looking-at org-complex-heading-regexp)
  9819. (not (member (match-string 4) excluded-entries))
  9820. (match-string 4))
  9821. (setq level (org-reduced-level
  9822. (- (match-end 1) (match-beginning 1)))
  9823. txt (org-link-display-format (match-string 4))
  9824. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9825. re (format org-complex-heading-regexp-format
  9826. (regexp-quote (match-string 4))))
  9827. (when org-refile-use-outline-path
  9828. (setq txt (mapconcat
  9829. 'org-protect-slash
  9830. (append
  9831. (if (eq org-refile-use-outline-path
  9832. 'file)
  9833. (list (file-name-nondirectory
  9834. (buffer-file-name
  9835. (buffer-base-buffer))))
  9836. (if (eq org-refile-use-outline-path
  9837. 'full-file-path)
  9838. (list (buffer-file-name
  9839. (buffer-base-buffer)))))
  9840. (org-get-outline-path fast-path-p
  9841. level txt)
  9842. (list txt))
  9843. "/")))
  9844. (push (list txt f re (org-refile-marker (point)))
  9845. tgs)))
  9846. (when (= (point) pos0)
  9847. ;; verification function has not moved point
  9848. (goto-char (point-at-eol))))))))
  9849. (when org-refile-use-cache
  9850. (org-refile-cache-put tgs (buffer-file-name) descre))
  9851. (setq targets (append tgs targets))
  9852. ))))
  9853. (message "Getting targets...done")
  9854. (nreverse targets)))
  9855. (defun org-protect-slash (s)
  9856. (while (string-match "/" s)
  9857. (setq s (replace-match "\\" t t s)))
  9858. s)
  9859. (defvar org-olpa (make-vector 20 nil))
  9860. (defun org-get-outline-path (&optional fastp level heading)
  9861. "Return the outline path to the current entry, as a list.
  9862. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9863. routine which makes outline path derivations for an entire file,
  9864. avoiding backtracing. Refile target collection makes use of that."
  9865. (if fastp
  9866. (progn
  9867. (if (> level 19)
  9868. (error "Outline path failure, more than 19 levels"))
  9869. (loop for i from level upto 19 do
  9870. (aset org-olpa i nil))
  9871. (prog1
  9872. (delq nil (append org-olpa nil))
  9873. (aset org-olpa level heading)))
  9874. (let (rtn case-fold-search)
  9875. (save-excursion
  9876. (save-restriction
  9877. (widen)
  9878. (while (org-up-heading-safe)
  9879. (when (looking-at org-complex-heading-regexp)
  9880. (push (org-match-string-no-properties 4) rtn)))
  9881. rtn)))))
  9882. (defun org-format-outline-path (path &optional width prefix separator)
  9883. "Format the outline path PATH for display.
  9884. WIDTH is the maximum number of characters that is available.
  9885. PREFIX is a prefix to be included in the returned string,
  9886. such as the file name.
  9887. SEPARATOR is inserted between the different parts of the path,
  9888. the default is \"/\"."
  9889. (setq width (or width 79))
  9890. (if prefix (setq width (- width (length prefix))))
  9891. (if (not path)
  9892. (or prefix "")
  9893. (let* ((nsteps (length path))
  9894. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9895. (maxwidth (if (<= total-width width)
  9896. 10000 ;; everything fits
  9897. ;; we need to shorten the level headings
  9898. (/ (- width nsteps) nsteps)))
  9899. (org-odd-levels-only nil)
  9900. (n 0)
  9901. (total (1+ (length prefix))))
  9902. (setq maxwidth (max maxwidth 10))
  9903. (concat prefix
  9904. (if prefix (or separator "/"))
  9905. (mapconcat
  9906. (lambda (h)
  9907. (setq n (1+ n))
  9908. (if (and (= n nsteps) (< maxwidth 10000))
  9909. (setq maxwidth (- total-width total)))
  9910. (if (< (length h) maxwidth)
  9911. (progn (setq total (+ total (length h) 1)) h)
  9912. (setq h (substring h 0 (- maxwidth 2))
  9913. total (+ total maxwidth 1))
  9914. (if (string-match "[ \t]+\\'" h)
  9915. (setq h (substring h 0 (match-beginning 0))))
  9916. (setq h (concat h "..")))
  9917. (org-add-props h nil 'face
  9918. (nth (% (1- n) org-n-level-faces)
  9919. org-level-faces))
  9920. h)
  9921. path (or separator "/"))))))
  9922. (defun org-display-outline-path (&optional file current separator just-return-string)
  9923. "Display the current outline path in the echo area.
  9924. If FILE is non-nil, prepend the output with the file name.
  9925. If CURRENT is non-nil, append the current heading to the output.
  9926. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9927. the different parts of the path and defaults to \"/\".
  9928. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9929. (interactive "P")
  9930. (let* (case-fold-search
  9931. message-log-max ; Don't populate the *Messages* buffer
  9932. (bfn (buffer-file-name (buffer-base-buffer)))
  9933. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9934. res)
  9935. (if current (setq path (append path
  9936. (save-excursion
  9937. (org-back-to-heading t)
  9938. (if (looking-at org-complex-heading-regexp)
  9939. (list (match-string 4)))))))
  9940. (setq res
  9941. (org-format-outline-path
  9942. path
  9943. (1- (frame-width))
  9944. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9945. separator))
  9946. (if just-return-string
  9947. (org-no-properties res)
  9948. (message "%s" res))))
  9949. (defvar org-refile-history nil
  9950. "History for refiling operations.")
  9951. (defvar org-after-refile-insert-hook nil
  9952. "Hook run after `org-refile' has inserted its stuff at the new location.
  9953. Note that this is still *before* the stuff will be removed from
  9954. the *old* location.")
  9955. (defvar org-capture-last-stored-marker)
  9956. (defvar org-refile-keep nil
  9957. "Non-nil means `org-refile' will copy instead of refile.")
  9958. (defun org-copy ()
  9959. "Like `org-refile', but copy."
  9960. (interactive)
  9961. (let ((org-refile-keep t))
  9962. (funcall 'org-refile nil nil nil "Copy")))
  9963. (defun org-refile (&optional goto default-buffer rfloc msg)
  9964. "Move the entry or entries at point to another heading.
  9965. The list of target headings is compiled using the information in
  9966. `org-refile-targets', which see.
  9967. At the target location, the entry is filed as a subitem of the target
  9968. heading. Depending on `org-reverse-note-order', the new subitem will
  9969. either be the first or the last subitem.
  9970. If there is an active region, all entries in that region will be moved.
  9971. However, the region must fulfill the requirement that the first heading
  9972. is the first one sets the top-level of the moved text - at most siblings
  9973. below it are allowed.
  9974. With prefix arg GOTO, the command will only visit the target location
  9975. and not actually move anything.
  9976. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9977. go to the location where the last refiling operation has put the subtree.
  9978. With a prefix argument of `2', refile to the running clock.
  9979. RFLOC can be a refile location obtained in a different way.
  9980. MSG is a string to replace \"Refile\" in the default prompt with
  9981. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9982. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9983. If you are using target caching (see `org-refile-use-cache'),
  9984. you have to clear the target cache in order to find new targets.
  9985. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9986. prefix argument (`C-u C-u C-u C-c C-w')."
  9987. (interactive "P")
  9988. (if (member goto '(0 (64)))
  9989. (org-refile-cache-clear)
  9990. (let* ((actionmsg (or msg "Refile"))
  9991. (cbuf (current-buffer))
  9992. (regionp (org-region-active-p))
  9993. (region-start (and regionp (region-beginning)))
  9994. (region-end (and regionp (region-end)))
  9995. (region-length (and regionp (- region-end region-start)))
  9996. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9997. pos it nbuf file re level reversed)
  9998. (setq last-command nil)
  9999. (when regionp
  10000. (goto-char region-start)
  10001. (or (bolp) (goto-char (point-at-bol)))
  10002. (setq region-start (point))
  10003. (unless (or (org-kill-is-subtree-p
  10004. (buffer-substring region-start region-end))
  10005. (prog1 org-refile-active-region-within-subtree
  10006. (org-toggle-heading)))
  10007. (error "The region is not a (sequence of) subtree(s)")))
  10008. (if (equal goto '(16))
  10009. (org-refile-goto-last-stored)
  10010. (when (or
  10011. (and (equal goto 2)
  10012. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10013. (prog1
  10014. (setq it (list (or org-clock-heading "running clock")
  10015. (buffer-file-name
  10016. (marker-buffer org-clock-hd-marker))
  10017. ""
  10018. (marker-position org-clock-hd-marker)))
  10019. (setq goto nil)))
  10020. (setq it (or rfloc
  10021. (let (heading-text)
  10022. (save-excursion
  10023. (unless goto
  10024. (org-back-to-heading t)
  10025. (setq heading-text
  10026. (nth 4 (org-heading-components))))
  10027. (org-refile-get-location
  10028. (cond (goto "Goto")
  10029. (regionp (concat actionmsg " region to"))
  10030. (t (concat actionmsg " subtree \""
  10031. heading-text "\" to")))
  10032. default-buffer
  10033. (and (not (equal '(4) goto))
  10034. org-refile-allow-creating-parent-nodes)
  10035. goto))))))
  10036. (setq file (nth 1 it)
  10037. re (nth 2 it)
  10038. pos (nth 3 it))
  10039. (if (and (not goto)
  10040. pos
  10041. (equal (buffer-file-name) file)
  10042. (if regionp
  10043. (and (>= pos region-start)
  10044. (<= pos region-end))
  10045. (and (>= pos (point))
  10046. (< pos (save-excursion
  10047. (org-end-of-subtree t t))))))
  10048. (error "Cannot refile to position inside the tree or region"))
  10049. (setq nbuf (or (find-buffer-visiting file)
  10050. (find-file-noselect file)))
  10051. (if goto
  10052. (progn
  10053. (org-pop-to-buffer-same-window nbuf)
  10054. (goto-char pos)
  10055. (org-show-context 'org-goto))
  10056. (if regionp
  10057. (progn
  10058. (org-kill-new (buffer-substring region-start region-end))
  10059. (org-save-markers-in-region region-start region-end))
  10060. (org-copy-subtree 1 nil t))
  10061. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10062. (find-file-noselect file)))
  10063. (setq reversed (org-notes-order-reversed-p))
  10064. (save-excursion
  10065. (save-restriction
  10066. (widen)
  10067. (if pos
  10068. (progn
  10069. (goto-char pos)
  10070. (looking-at org-outline-regexp)
  10071. (setq level (org-get-valid-level (funcall outline-level) 1))
  10072. (goto-char
  10073. (if reversed
  10074. (or (outline-next-heading) (point-max))
  10075. (or (save-excursion (org-get-next-sibling))
  10076. (org-end-of-subtree t t)
  10077. (point-max)))))
  10078. (setq level 1)
  10079. (if (not reversed)
  10080. (goto-char (point-max))
  10081. (goto-char (point-min))
  10082. (or (outline-next-heading) (goto-char (point-max)))))
  10083. (if (not (bolp)) (newline))
  10084. (org-paste-subtree level)
  10085. (when org-log-refile
  10086. (org-add-log-setup 'refile nil nil 'findpos
  10087. org-log-refile)
  10088. (unless (eq org-log-refile 'note)
  10089. (save-excursion (org-add-log-note))))
  10090. (and org-auto-align-tags
  10091. (let ((org-loop-over-headlines-in-active-region nil))
  10092. (org-set-tags nil t)))
  10093. (with-demoted-errors
  10094. (bookmark-set "org-refile-last-stored"))
  10095. ;; If we are refiling for capture, make sure that the
  10096. ;; last-capture pointers point here
  10097. (when (org-bound-and-true-p org-refile-for-capture)
  10098. (with-demoted-errors
  10099. (bookmark-set "org-capture-last-stored-marker"))
  10100. (move-marker org-capture-last-stored-marker (point)))
  10101. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10102. (run-hooks 'org-after-refile-insert-hook))))
  10103. (unless org-refile-keep
  10104. (if regionp
  10105. (delete-region (point) (+ (point) region-length))
  10106. (org-cut-subtree)))
  10107. (when (featurep 'org-inlinetask)
  10108. (org-inlinetask-remove-END-maybe))
  10109. (setq org-markers-to-move nil)
  10110. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10111. (defun org-refile-goto-last-stored ()
  10112. "Go to the location where the last refile was stored."
  10113. (interactive)
  10114. (bookmark-jump "org-refile-last-stored")
  10115. (message "This is the location of the last refile"))
  10116. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10117. no-exclude)
  10118. "Prompt the user for a refile location, using PROMPT.
  10119. PROMPT should not be suffixed with a colon and a space, because
  10120. this function appends the default value from
  10121. `org-refile-history' automatically, if that is not empty.
  10122. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10123. this is used for the GOTO interface."
  10124. (let ((org-refile-targets org-refile-targets)
  10125. (org-refile-use-outline-path org-refile-use-outline-path)
  10126. excluded-entries)
  10127. (when (and (derived-mode-p 'org-mode)
  10128. (not org-refile-use-cache)
  10129. (not no-exclude))
  10130. (org-map-tree
  10131. (lambda()
  10132. (setq excluded-entries
  10133. (append excluded-entries (list (org-get-heading t t)))))))
  10134. (setq org-refile-target-table
  10135. (org-refile-get-targets default-buffer excluded-entries)))
  10136. (unless org-refile-target-table
  10137. (error "No refile targets"))
  10138. (let* ((cbuf (current-buffer))
  10139. (partial-completion-mode nil)
  10140. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10141. (cfunc (if (and org-refile-use-outline-path
  10142. org-outline-path-complete-in-steps)
  10143. 'org-olpath-completing-read
  10144. 'org-icompleting-read))
  10145. (extra (if org-refile-use-outline-path "/" ""))
  10146. (cbnex (concat (buffer-name) extra))
  10147. (filename (and cfn (expand-file-name cfn)))
  10148. (tbl (mapcar
  10149. (lambda (x)
  10150. (if (and (not (member org-refile-use-outline-path
  10151. '(file full-file-path)))
  10152. (not (equal filename (nth 1 x))))
  10153. (cons (concat (car x) extra " ("
  10154. (file-name-nondirectory (nth 1 x)) ")")
  10155. (cdr x))
  10156. (cons (concat (car x) extra) (cdr x))))
  10157. org-refile-target-table))
  10158. (completion-ignore-case t)
  10159. cdef
  10160. (prompt (concat prompt
  10161. (or (and (car org-refile-history)
  10162. (concat " (default " (car org-refile-history) ")"))
  10163. (and (assoc cbnex tbl) (setq cdef cbnex)
  10164. (concat " (default " cbnex ")"))) ": "))
  10165. pa answ parent-target child parent old-hist)
  10166. (setq old-hist org-refile-history)
  10167. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10168. nil 'org-refile-history (or cdef (car org-refile-history))))
  10169. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10170. (org-refile-check-position pa)
  10171. (if pa
  10172. (progn
  10173. (when (or (not org-refile-history)
  10174. (not (eq old-hist org-refile-history))
  10175. (not (equal (car pa) (car org-refile-history))))
  10176. (setq org-refile-history
  10177. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10178. org-refile-history
  10179. (cdr org-refile-history))))
  10180. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10181. (pop org-refile-history)))
  10182. pa)
  10183. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10184. (progn
  10185. (setq parent (match-string 1 answ)
  10186. child (match-string 2 answ))
  10187. (setq parent-target (or (assoc parent tbl)
  10188. (assoc (concat parent "/") tbl)))
  10189. (when (and parent-target
  10190. (or (eq new-nodes t)
  10191. (and (eq new-nodes 'confirm)
  10192. (y-or-n-p (format "Create new node \"%s\"? "
  10193. child)))))
  10194. (org-refile-new-child parent-target child)))
  10195. (error "Invalid target location")))))
  10196. (declare-function org-string-nw-p "org-macs" (s))
  10197. (defun org-refile-check-position (refile-pointer)
  10198. "Check if the refile pointer matches the headline to which it points."
  10199. (let* ((file (nth 1 refile-pointer))
  10200. (re (nth 2 refile-pointer))
  10201. (pos (nth 3 refile-pointer))
  10202. buffer)
  10203. (if (and (not (markerp pos)) (not file))
  10204. (error "Please save the buffer to a file before refiling")
  10205. (when (org-string-nw-p re)
  10206. (setq buffer (if (markerp pos)
  10207. (marker-buffer pos)
  10208. (or (find-buffer-visiting file)
  10209. (find-file-noselect file))))
  10210. (with-current-buffer buffer
  10211. (save-excursion
  10212. (save-restriction
  10213. (widen)
  10214. (goto-char pos)
  10215. (beginning-of-line 1)
  10216. (unless (org-looking-at-p re)
  10217. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10218. (defun org-refile-new-child (parent-target child)
  10219. "Use refile target PARENT-TARGET to add new CHILD below it."
  10220. (unless parent-target
  10221. (error "Cannot find parent for new node"))
  10222. (let ((file (nth 1 parent-target))
  10223. (pos (nth 3 parent-target))
  10224. level)
  10225. (with-current-buffer (or (find-buffer-visiting file)
  10226. (find-file-noselect file))
  10227. (save-excursion
  10228. (save-restriction
  10229. (widen)
  10230. (if pos
  10231. (goto-char pos)
  10232. (goto-char (point-max))
  10233. (if (not (bolp)) (newline)))
  10234. (when (looking-at org-outline-regexp)
  10235. (setq level (funcall outline-level))
  10236. (org-end-of-subtree t t))
  10237. (org-back-over-empty-lines)
  10238. (insert "\n" (make-string
  10239. (if pos (org-get-valid-level level 1) 1) ?*)
  10240. " " child "\n")
  10241. (beginning-of-line 0)
  10242. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10243. (defun org-olpath-completing-read (prompt collection &rest args)
  10244. "Read an outline path like a file name."
  10245. (let ((thetable collection)
  10246. (org-completion-use-ido nil) ; does not work with ido.
  10247. (org-completion-use-iswitchb nil)) ; or iswitchb
  10248. (apply
  10249. 'org-icompleting-read prompt
  10250. (lambda (string predicate &optional flag)
  10251. (let (rtn r f (l (length string)))
  10252. (cond
  10253. ((eq flag nil)
  10254. ;; try completion
  10255. (try-completion string thetable))
  10256. ((eq flag t)
  10257. ;; all-completions
  10258. (setq rtn (all-completions string thetable predicate))
  10259. (mapcar
  10260. (lambda (x)
  10261. (setq r (substring x l))
  10262. (if (string-match " ([^)]*)$" x)
  10263. (setq f (match-string 0 x))
  10264. (setq f ""))
  10265. (if (string-match "/" r)
  10266. (concat string (substring r 0 (match-end 0)) f)
  10267. x))
  10268. rtn))
  10269. ((eq flag 'lambda)
  10270. ;; exact match?
  10271. (assoc string thetable)))))
  10272. args)))
  10273. ;;;; Dynamic blocks
  10274. (defun org-find-dblock (name)
  10275. "Find the first dynamic block with name NAME in the buffer.
  10276. If not found, stay at current position and return nil."
  10277. (let ((case-fold-search t) pos)
  10278. (save-excursion
  10279. (goto-char (point-min))
  10280. (setq pos (and (re-search-forward
  10281. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10282. (match-beginning 0))))
  10283. (if pos (goto-char pos))
  10284. pos))
  10285. (defconst org-dblock-start-re
  10286. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10287. "Matches the start line of a dynamic block, with parameters.")
  10288. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10289. "Matches the end of a dynamic block.")
  10290. (defun org-create-dblock (plist)
  10291. "Create a dynamic block section, with parameters taken from PLIST.
  10292. PLIST must contain a :name entry which is used as name of the block."
  10293. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10294. (end-of-line 1)
  10295. (newline))
  10296. (let ((col (current-column))
  10297. (name (plist-get plist :name)))
  10298. (insert "#+BEGIN: " name)
  10299. (while plist
  10300. (if (eq (car plist) :name)
  10301. (setq plist (cddr plist))
  10302. (insert " " (prin1-to-string (pop plist)))))
  10303. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10304. (beginning-of-line -2)))
  10305. (defun org-prepare-dblock ()
  10306. "Prepare dynamic block for refresh.
  10307. This empties the block, puts the cursor at the insert position and returns
  10308. the property list including an extra property :name with the block name."
  10309. (unless (looking-at org-dblock-start-re)
  10310. (error "Not at a dynamic block"))
  10311. (let* ((begdel (1+ (match-end 0)))
  10312. (name (org-no-properties (match-string 1)))
  10313. (params (append (list :name name)
  10314. (read (concat "(" (match-string 3) ")")))))
  10315. (save-excursion
  10316. (beginning-of-line 1)
  10317. (skip-chars-forward " \t")
  10318. (setq params (plist-put params :indentation-column (current-column))))
  10319. (unless (re-search-forward org-dblock-end-re nil t)
  10320. (error "Dynamic block not terminated"))
  10321. (setq params
  10322. (append params
  10323. (list :content (buffer-substring
  10324. begdel (match-beginning 0)))))
  10325. (delete-region begdel (match-beginning 0))
  10326. (goto-char begdel)
  10327. (open-line 1)
  10328. params))
  10329. (defun org-map-dblocks (&optional command)
  10330. "Apply COMMAND to all dynamic blocks in the current buffer.
  10331. If COMMAND is not given, use `org-update-dblock'."
  10332. (let ((cmd (or command 'org-update-dblock)))
  10333. (save-excursion
  10334. (goto-char (point-min))
  10335. (while (re-search-forward org-dblock-start-re nil t)
  10336. (goto-char (match-beginning 0))
  10337. (save-excursion
  10338. (condition-case nil
  10339. (funcall cmd)
  10340. (error (message "Error during update of dynamic block"))))
  10341. (unless (re-search-forward org-dblock-end-re nil t)
  10342. (error "Dynamic block not terminated"))))))
  10343. (defun org-dblock-update (&optional arg)
  10344. "User command for updating dynamic blocks.
  10345. Update the dynamic block at point. With prefix ARG, update all dynamic
  10346. blocks in the buffer."
  10347. (interactive "P")
  10348. (if arg
  10349. (org-update-all-dblocks)
  10350. (or (looking-at org-dblock-start-re)
  10351. (org-beginning-of-dblock))
  10352. (org-update-dblock)))
  10353. (defun org-update-dblock ()
  10354. "Update the dynamic block at point.
  10355. This means to empty the block, parse for parameters and then call
  10356. the correct writing function."
  10357. (interactive)
  10358. (save-window-excursion
  10359. (let* ((pos (point))
  10360. (line (org-current-line))
  10361. (params (org-prepare-dblock))
  10362. (name (plist-get params :name))
  10363. (indent (plist-get params :indentation-column))
  10364. (cmd (intern (concat "org-dblock-write:" name))))
  10365. (message "Updating dynamic block `%s' at line %d..." name line)
  10366. (funcall cmd params)
  10367. (message "Updating dynamic block `%s' at line %d...done" name line)
  10368. (goto-char pos)
  10369. (when (and indent (> indent 0))
  10370. (setq indent (make-string indent ?\ ))
  10371. (save-excursion
  10372. (org-beginning-of-dblock)
  10373. (forward-line 1)
  10374. (while (not (looking-at org-dblock-end-re))
  10375. (insert indent)
  10376. (beginning-of-line 2))
  10377. (when (looking-at org-dblock-end-re)
  10378. (and (looking-at "[ \t]+")
  10379. (replace-match ""))
  10380. (insert indent)))))))
  10381. (defun org-beginning-of-dblock ()
  10382. "Find the beginning of the dynamic block at point.
  10383. Error if there is no such block at point."
  10384. (let ((pos (point))
  10385. beg)
  10386. (end-of-line 1)
  10387. (if (and (re-search-backward org-dblock-start-re nil t)
  10388. (setq beg (match-beginning 0))
  10389. (re-search-forward org-dblock-end-re nil t)
  10390. (> (match-end 0) pos))
  10391. (goto-char beg)
  10392. (goto-char pos)
  10393. (error "Not in a dynamic block"))))
  10394. (defun org-update-all-dblocks ()
  10395. "Update all dynamic blocks in the buffer.
  10396. This function can be used in a hook."
  10397. (interactive)
  10398. (when (derived-mode-p 'org-mode)
  10399. (org-map-dblocks 'org-update-dblock)))
  10400. ;;;; Completion
  10401. (defconst org-additional-option-like-keywords
  10402. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10403. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10404. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10405. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10406. "BEGIN:" "END:"
  10407. "ORGTBL" "TBLFM:" "TBLNAME:"
  10408. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10409. "BEGIN_VERBATIM" "END_VERBATIM"
  10410. "BEGIN_QUOTE" "END_QUOTE"
  10411. "BEGIN_VERSE" "END_VERSE"
  10412. "BEGIN_CENTER" "END_CENTER"
  10413. "BEGIN_SRC" "END_SRC"
  10414. "BEGIN_RESULT" "END_RESULT"
  10415. "BEGIN_lstlisting" "END_lstlisting"
  10416. "NAME:" "RESULTS:"
  10417. "HEADER:" "HEADERS:"
  10418. "COLUMNS:" "PROPERTY:"
  10419. "CAPTION:" "LABEL:"
  10420. "SETUPFILE:"
  10421. "INCLUDE:" "INDEX:"
  10422. "BIND:"
  10423. "MACRO:"))
  10424. (defconst org-options-keywords
  10425. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10426. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10427. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10428. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10429. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10430. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10431. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10432. (defconst org-additional-option-like-keywords-for-flyspell
  10433. (delete-dups
  10434. (split-string
  10435. (mapconcat (lambda(k)
  10436. (replace-regexp-in-string
  10437. "_\\|:" " "
  10438. (concat k " " (downcase k) " " (upcase k))))
  10439. (append org-options-keywords org-additional-option-like-keywords)
  10440. " ")
  10441. " +" t)))
  10442. (defcustom org-structure-template-alist
  10443. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10444. "<src lang=\"?\">\n\n</src>")
  10445. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10446. "<example>\n?\n</example>")
  10447. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10448. "<quote>\n?\n</quote>")
  10449. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10450. "<verse>\n?\n</verse>")
  10451. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10452. "<verbatim>\n?\n</verbatim>")
  10453. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10454. "<center>\n?\n</center>")
  10455. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10456. "<literal style=\"latex\">\n?\n</literal>")
  10457. ("L" "#+LaTeX: "
  10458. "<literal style=\"latex\">?</literal>")
  10459. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10460. "<literal style=\"html\">\n?\n</literal>")
  10461. ("H" "#+HTML: "
  10462. "<literal style=\"html\">?</literal>")
  10463. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10464. ("A" "#+ASCII: ")
  10465. ("i" "#+INDEX: ?"
  10466. "#+INDEX: ?")
  10467. ("I" "#+INCLUDE: %file ?"
  10468. "<include file=%file markup=\"?\">"))
  10469. "Structure completion elements.
  10470. This is a list of abbreviation keys and values. The value gets inserted
  10471. if you type `<' followed by the key and then press the completion key,
  10472. usually `M-TAB'. %file will be replaced by a file name after prompting
  10473. for the file using completion. The cursor will be placed at the position
  10474. of the `?` in the template.
  10475. There are two templates for each key, the first uses the original Org syntax,
  10476. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10477. the default when the /org-mtags.el/ module has been loaded. See also the
  10478. variable `org-mtags-prefer-muse-templates'."
  10479. :group 'org-completion
  10480. :type '(repeat
  10481. (string :tag "Key")
  10482. (string :tag "Template")
  10483. (string :tag "Muse Template")))
  10484. (defun org-try-structure-completion ()
  10485. "Try to complete a structure template before point.
  10486. This looks for strings like \"<e\" on an otherwise empty line and
  10487. expands them."
  10488. (let ((l (buffer-substring (point-at-bol) (point)))
  10489. a)
  10490. (when (and (looking-at "[ \t]*$")
  10491. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10492. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10493. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10494. (match-beginning 1)) a)
  10495. t)))
  10496. (defun org-complete-expand-structure-template (start cell)
  10497. "Expand a structure template."
  10498. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10499. (rpl (nth (if musep 2 1) cell))
  10500. (ind ""))
  10501. (delete-region start (point))
  10502. (when (string-match "\\`#\\+" rpl)
  10503. (cond
  10504. ((bolp))
  10505. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10506. (setq ind (buffer-substring (point-at-bol) (point))))
  10507. (t (newline))))
  10508. (setq start (point))
  10509. (if (string-match "%file" rpl)
  10510. (setq rpl (replace-match
  10511. (concat
  10512. "\""
  10513. (save-match-data
  10514. (abbreviate-file-name (read-file-name "Include file: ")))
  10515. "\"")
  10516. t t rpl)))
  10517. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10518. (concat "\n" ind)))
  10519. (insert rpl)
  10520. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10521. ;;;; TODO, DEADLINE, Comments
  10522. (defun org-toggle-comment ()
  10523. "Change the COMMENT state of an entry."
  10524. (interactive)
  10525. (save-excursion
  10526. (org-back-to-heading)
  10527. (let (case-fold-search)
  10528. (cond
  10529. ((looking-at (format org-heading-keyword-regexp-format
  10530. org-comment-string))
  10531. (goto-char (match-end 1))
  10532. (looking-at (concat " +" org-comment-string))
  10533. (replace-match "" t t)
  10534. (when (eolp) (insert " ")))
  10535. ((looking-at org-outline-regexp)
  10536. (goto-char (match-end 0))
  10537. (insert org-comment-string " "))))))
  10538. (defvar org-last-todo-state-is-todo nil
  10539. "This is non-nil when the last TODO state change led to a TODO state.
  10540. If the last change removed the TODO tag or switched to DONE, then
  10541. this is nil.")
  10542. (defvar org-setting-tags nil) ; dynamically skipped
  10543. (defvar org-todo-setup-filter-hook nil
  10544. "Hook for functions that pre-filter todo specs.
  10545. Each function takes a todo spec and returns either nil or the spec
  10546. transformed into canonical form." )
  10547. (defvar org-todo-get-default-hook nil
  10548. "Hook for functions that get a default item for todo.
  10549. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10550. nil or a string to be used for the todo mark." )
  10551. (defvar org-agenda-headline-snapshot-before-repeat)
  10552. (defun org-current-effective-time ()
  10553. "Return current time adjusted for `org-extend-today-until' variable."
  10554. (let* ((ct (org-current-time))
  10555. (dct (decode-time ct))
  10556. (ct1
  10557. (cond
  10558. (org-use-last-clock-out-time-as-effective-time
  10559. (or (org-clock-get-last-clock-out-time) ct))
  10560. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10561. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10562. (t ct))))
  10563. ct1))
  10564. (defun org-todo-yesterday (&optional arg)
  10565. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10566. (interactive "P")
  10567. (if (eq major-mode 'org-agenda-mode)
  10568. (apply 'org-agenda-todo-yesterday arg)
  10569. (let* ((hour (third (decode-time
  10570. (org-current-time))))
  10571. (org-extend-today-until (1+ hour)))
  10572. (org-todo arg))))
  10573. (defvar org-block-entry-blocking ""
  10574. "First entry preventing the TODO state change.")
  10575. (defun org-todo (&optional arg)
  10576. "Change the TODO state of an item.
  10577. The state of an item is given by a keyword at the start of the heading,
  10578. like
  10579. *** TODO Write paper
  10580. *** DONE Call mom
  10581. The different keywords are specified in the variable `org-todo-keywords'.
  10582. By default the available states are \"TODO\" and \"DONE\".
  10583. So for this example: when the item starts with TODO, it is changed to DONE.
  10584. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10585. DONE are present, add TODO at the beginning of the heading.
  10586. With \\[universal-argument] prefix arg, use completion to determine the new \
  10587. state.
  10588. With numeric prefix arg, switch to that state.
  10589. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10590. keywords (nextset).
  10591. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10592. With a numeric prefix arg of 0, inhibit note taking for the change.
  10593. For calling through lisp, arg is also interpreted in the following way:
  10594. 'none -> empty state
  10595. \"\"(empty string) -> switch to empty state
  10596. 'done -> switch to DONE
  10597. 'nextset -> switch to the next set of keywords
  10598. 'previousset -> switch to the previous set of keywords
  10599. \"WAITING\" -> switch to the specified keyword, but only if it
  10600. really is a member of `org-todo-keywords'."
  10601. (interactive "P")
  10602. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10603. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10604. 'region-start-level 'region))
  10605. org-loop-over-headlines-in-active-region)
  10606. (org-map-entries
  10607. `(org-todo ,arg)
  10608. org-loop-over-headlines-in-active-region
  10609. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10610. (if (equal arg '(16)) (setq arg 'nextset))
  10611. (let ((org-blocker-hook org-blocker-hook)
  10612. commentp
  10613. case-fold-search)
  10614. (when (equal arg '(64))
  10615. (setq arg nil org-blocker-hook nil))
  10616. (when (and org-blocker-hook
  10617. (or org-inhibit-blocking
  10618. (org-entry-get nil "NOBLOCKING")))
  10619. (setq org-blocker-hook nil))
  10620. (save-excursion
  10621. (catch 'exit
  10622. (org-back-to-heading t)
  10623. (when (looking-at (concat "^\\*+ " org-comment-string))
  10624. (org-toggle-comment)
  10625. (setq commentp t))
  10626. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10627. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10628. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10629. (let* ((match-data (match-data))
  10630. (startpos (point-at-bol))
  10631. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10632. (org-log-done org-log-done)
  10633. (org-log-repeat org-log-repeat)
  10634. (org-todo-log-states org-todo-log-states)
  10635. (org-inhibit-logging
  10636. (if (equal arg 0)
  10637. (progn (setq arg nil) 'note) org-inhibit-logging))
  10638. (this (match-string 1))
  10639. (hl-pos (match-beginning 0))
  10640. (head (org-get-todo-sequence-head this))
  10641. (ass (assoc head org-todo-kwd-alist))
  10642. (interpret (nth 1 ass))
  10643. (done-word (nth 3 ass))
  10644. (final-done-word (nth 4 ass))
  10645. (org-last-state (or this ""))
  10646. (completion-ignore-case t)
  10647. (member (member this org-todo-keywords-1))
  10648. (tail (cdr member))
  10649. (org-state (cond
  10650. ((and org-todo-key-trigger
  10651. (or (and (equal arg '(4))
  10652. (eq org-use-fast-todo-selection 'prefix))
  10653. (and (not arg) org-use-fast-todo-selection
  10654. (not (eq org-use-fast-todo-selection
  10655. 'prefix)))))
  10656. ;; Use fast selection
  10657. (org-fast-todo-selection))
  10658. ((and (equal arg '(4))
  10659. (or (not org-use-fast-todo-selection)
  10660. (not org-todo-key-trigger)))
  10661. ;; Read a state with completion
  10662. (org-icompleting-read
  10663. "State: " (mapcar (lambda(x) (list x))
  10664. org-todo-keywords-1)
  10665. nil t))
  10666. ((eq arg 'right)
  10667. (if this
  10668. (if tail (car tail) nil)
  10669. (car org-todo-keywords-1)))
  10670. ((eq arg 'left)
  10671. (if (equal member org-todo-keywords-1)
  10672. nil
  10673. (if this
  10674. (nth (- (length org-todo-keywords-1)
  10675. (length tail) 2)
  10676. org-todo-keywords-1)
  10677. (org-last org-todo-keywords-1))))
  10678. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10679. (setq arg nil))) ; hack to fall back to cycling
  10680. (arg
  10681. ;; user or caller requests a specific state
  10682. (cond
  10683. ((equal arg "") nil)
  10684. ((eq arg 'none) nil)
  10685. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10686. ((eq arg 'nextset)
  10687. (or (car (cdr (member head org-todo-heads)))
  10688. (car org-todo-heads)))
  10689. ((eq arg 'previousset)
  10690. (let ((org-todo-heads (reverse org-todo-heads)))
  10691. (or (car (cdr (member head org-todo-heads)))
  10692. (car org-todo-heads))))
  10693. ((car (member arg org-todo-keywords-1)))
  10694. ((stringp arg)
  10695. (error "State `%s' not valid in this file" arg))
  10696. ((nth (1- (prefix-numeric-value arg))
  10697. org-todo-keywords-1))))
  10698. ((null member) (or head (car org-todo-keywords-1)))
  10699. ((equal this final-done-word) nil) ;; -> make empty
  10700. ((null tail) nil) ;; -> first entry
  10701. ((memq interpret '(type priority))
  10702. (if (eq this-command last-command)
  10703. (car tail)
  10704. (if (> (length tail) 0)
  10705. (or done-word (car org-done-keywords))
  10706. nil)))
  10707. (t
  10708. (car tail))))
  10709. (org-state (or
  10710. (run-hook-with-args-until-success
  10711. 'org-todo-get-default-hook org-state org-last-state)
  10712. org-state))
  10713. (next (if org-state (concat " " org-state " ") " "))
  10714. (change-plist (list :type 'todo-state-change :from this :to org-state
  10715. :position startpos))
  10716. dolog now-done-p)
  10717. (when org-blocker-hook
  10718. (setq org-last-todo-state-is-todo
  10719. (not (member this org-done-keywords)))
  10720. (unless (save-excursion
  10721. (save-match-data
  10722. (org-with-wide-buffer
  10723. (run-hook-with-args-until-failure
  10724. 'org-blocker-hook change-plist))))
  10725. (if (org-called-interactively-p 'interactive)
  10726. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10727. this org-state org-block-entry-blocking)
  10728. ;; fail silently
  10729. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10730. this org-state org-block-entry-blocking)
  10731. (throw 'exit nil))))
  10732. (store-match-data match-data)
  10733. (replace-match next t t)
  10734. (unless (pos-visible-in-window-p hl-pos)
  10735. (message "TODO state changed to %s" (org-trim next)))
  10736. (unless head
  10737. (setq head (org-get-todo-sequence-head org-state)
  10738. ass (assoc head org-todo-kwd-alist)
  10739. interpret (nth 1 ass)
  10740. done-word (nth 3 ass)
  10741. final-done-word (nth 4 ass)))
  10742. (when (memq arg '(nextset previousset))
  10743. (message "Keyword-Set %d/%d: %s"
  10744. (- (length org-todo-sets) -1
  10745. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10746. (length org-todo-sets)
  10747. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10748. (setq org-last-todo-state-is-todo
  10749. (not (member org-state org-done-keywords)))
  10750. (setq now-done-p (and (member org-state org-done-keywords)
  10751. (not (member this org-done-keywords))))
  10752. (and logging (org-local-logging logging))
  10753. (when (and (or org-todo-log-states org-log-done)
  10754. (not (eq org-inhibit-logging t))
  10755. (not (memq arg '(nextset previousset))))
  10756. ;; we need to look at recording a time and note
  10757. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10758. (nth 2 (assoc this org-todo-log-states))))
  10759. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10760. (setq dolog 'time))
  10761. (when (and org-state
  10762. (member org-state org-not-done-keywords)
  10763. (not (member this org-not-done-keywords)))
  10764. ;; This is now a todo state and was not one before
  10765. ;; If there was a CLOSED time stamp, get rid of it.
  10766. (org-add-planning-info nil nil 'closed))
  10767. (when (and now-done-p org-log-done)
  10768. ;; It is now done, and it was not done before
  10769. (org-add-planning-info 'closed (org-current-effective-time))
  10770. (if (and (not dolog) (eq 'note org-log-done))
  10771. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10772. (when (and org-state dolog)
  10773. ;; This is a non-nil state, and we need to log it
  10774. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10775. ;; Fixup tag positioning
  10776. (org-todo-trigger-tag-changes org-state)
  10777. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10778. (when org-provide-todo-statistics
  10779. (org-update-parent-todo-statistics))
  10780. (run-hooks 'org-after-todo-state-change-hook)
  10781. (if (and arg (not (member org-state org-done-keywords)))
  10782. (setq head (org-get-todo-sequence-head org-state)))
  10783. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10784. ;; Do we need to trigger a repeat?
  10785. (when now-done-p
  10786. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10787. ;; This is for the agenda, take a snapshot of the headline.
  10788. (save-match-data
  10789. (setq org-agenda-headline-snapshot-before-repeat
  10790. (org-get-heading))))
  10791. (org-auto-repeat-maybe org-state))
  10792. ;; Fixup cursor location if close to the keyword
  10793. (if (and (outline-on-heading-p)
  10794. (not (bolp))
  10795. (save-excursion (beginning-of-line 1)
  10796. (looking-at org-todo-line-regexp))
  10797. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10798. (progn
  10799. (goto-char (or (match-end 2) (match-end 1)))
  10800. (and (looking-at " ") (just-one-space))))
  10801. (when org-trigger-hook
  10802. (save-excursion
  10803. (run-hook-with-args 'org-trigger-hook change-plist)))
  10804. (when commentp (org-toggle-comment))))))))
  10805. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10806. "Block turning an entry into a TODO, using the hierarchy.
  10807. This checks whether the current task should be blocked from state
  10808. changes. Such blocking occurs when:
  10809. 1. The task has children which are not all in a completed state.
  10810. 2. A task has a parent with the property :ORDERED:, and there
  10811. are siblings prior to the current task with incomplete
  10812. status.
  10813. 3. The parent of the task is blocked because it has siblings that should
  10814. be done first, or is child of a block grandparent TODO entry."
  10815. (if (not org-enforce-todo-dependencies)
  10816. t ; if locally turned off don't block
  10817. (catch 'dont-block
  10818. ;; If this is not a todo state change, or if this entry is already DONE,
  10819. ;; do not block
  10820. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10821. (member (plist-get change-plist :from)
  10822. (cons 'done org-done-keywords))
  10823. (member (plist-get change-plist :to)
  10824. (cons 'todo org-not-done-keywords))
  10825. (not (plist-get change-plist :to)))
  10826. (throw 'dont-block t))
  10827. ;; If this task has children, and any are undone, it's blocked
  10828. (save-excursion
  10829. (org-back-to-heading t)
  10830. (let ((this-level (funcall outline-level)))
  10831. (outline-next-heading)
  10832. (let ((child-level (funcall outline-level)))
  10833. (while (and (not (eobp))
  10834. (> child-level this-level))
  10835. ;; this todo has children, check whether they are all
  10836. ;; completed
  10837. (if (and (not (org-entry-is-done-p))
  10838. (org-entry-is-todo-p))
  10839. (progn (setq org-block-entry-blocking (org-get-heading))
  10840. (throw 'dont-block nil)))
  10841. (outline-next-heading)
  10842. (setq child-level (funcall outline-level))))))
  10843. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10844. ;; any previous siblings are undone, it's blocked
  10845. (save-excursion
  10846. (org-back-to-heading t)
  10847. (let* ((pos (point))
  10848. (parent-pos (and (org-up-heading-safe) (point))))
  10849. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10850. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10851. (forward-line 1)
  10852. (re-search-forward org-not-done-heading-regexp pos t))
  10853. (setq org-block-entry-blocking (match-string 0))
  10854. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10855. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10856. (while t
  10857. (goto-char parent-pos)
  10858. (if (not (looking-at org-not-done-heading-regexp))
  10859. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10860. (setq pos (point))
  10861. (setq parent-pos (and (org-up-heading-safe) (point)))
  10862. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10863. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10864. (forward-line 1)
  10865. (re-search-forward org-not-done-heading-regexp pos t)
  10866. (setq org-block-entry-blocking (org-get-heading)))
  10867. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10868. (defcustom org-track-ordered-property-with-tag nil
  10869. "Should the ORDERED property also be shown as a tag?
  10870. The ORDERED property decides if an entry should require subtasks to be
  10871. completed in sequence. Since a property is not very visible, setting
  10872. this option means that toggling the ORDERED property with the command
  10873. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10874. not relevant for the behavior, but it makes things more visible.
  10875. Note that toggling the tag with tags commands will not change the property
  10876. and therefore not influence behavior!
  10877. This can be t, meaning the tag ORDERED should be used, It can also be a
  10878. string to select a different tag for this task."
  10879. :group 'org-todo
  10880. :type '(choice
  10881. (const :tag "No tracking" nil)
  10882. (const :tag "Track with ORDERED tag" t)
  10883. (string :tag "Use other tag")))
  10884. (defun org-toggle-ordered-property ()
  10885. "Toggle the ORDERED property of the current entry.
  10886. For better visibility, you can track the value of this property with a tag.
  10887. See variable `org-track-ordered-property-with-tag'."
  10888. (interactive)
  10889. (let* ((t1 org-track-ordered-property-with-tag)
  10890. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10891. (save-excursion
  10892. (org-back-to-heading)
  10893. (if (org-entry-get nil "ORDERED")
  10894. (progn
  10895. (org-delete-property "ORDERED")
  10896. (and tag (org-toggle-tag tag 'off))
  10897. (message "Subtasks can be completed in arbitrary order"))
  10898. (org-entry-put nil "ORDERED" "t")
  10899. (and tag (org-toggle-tag tag 'on))
  10900. (message "Subtasks must be completed in sequence")))))
  10901. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10902. (defun org-block-todo-from-checkboxes (change-plist)
  10903. "Block turning an entry into a TODO, using checkboxes.
  10904. This checks whether the current task should be blocked from state
  10905. changes because there are unchecked boxes in this entry."
  10906. (if (not org-enforce-todo-checkbox-dependencies)
  10907. t ; if locally turned off don't block
  10908. (catch 'dont-block
  10909. ;; If this is not a todo state change, or if this entry is already DONE,
  10910. ;; do not block
  10911. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10912. (member (plist-get change-plist :from)
  10913. (cons 'done org-done-keywords))
  10914. (member (plist-get change-plist :to)
  10915. (cons 'todo org-not-done-keywords))
  10916. (not (plist-get change-plist :to)))
  10917. (throw 'dont-block t))
  10918. ;; If this task has checkboxes that are not checked, it's blocked
  10919. (save-excursion
  10920. (org-back-to-heading t)
  10921. (let ((beg (point)) end)
  10922. (outline-next-heading)
  10923. (setq end (point))
  10924. (goto-char beg)
  10925. (if (org-list-search-forward
  10926. (concat (org-item-beginning-re)
  10927. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10928. "\\[[- ]\\]")
  10929. end t)
  10930. (progn
  10931. (if (boundp 'org-blocked-by-checkboxes)
  10932. (setq org-blocked-by-checkboxes t))
  10933. (throw 'dont-block nil)))))
  10934. t))) ; do not block
  10935. (defun org-entry-blocked-p ()
  10936. "Is the current entry blocked?"
  10937. (org-with-buffer-modified-unmodified
  10938. (if (org-entry-get nil "NOBLOCKING")
  10939. nil ;; Never block this entry
  10940. (not
  10941. (run-hook-with-args-until-failure
  10942. 'org-blocker-hook
  10943. (list :type 'todo-state-change
  10944. :position (point)
  10945. :from 'todo
  10946. :to 'done))))))
  10947. (defun org-update-statistics-cookies (all)
  10948. "Update the statistics cookie, either from TODO or from checkboxes.
  10949. This should be called with the cursor in a line with a statistics cookie."
  10950. (interactive "P")
  10951. (if all
  10952. (progn
  10953. (org-update-checkbox-count 'all)
  10954. (org-map-entries 'org-update-parent-todo-statistics))
  10955. (if (not (org-at-heading-p))
  10956. (org-update-checkbox-count)
  10957. (let ((pos (point-marker))
  10958. end l1 l2)
  10959. (ignore-errors (org-back-to-heading t))
  10960. (if (not (org-at-heading-p))
  10961. (org-update-checkbox-count)
  10962. (setq l1 (org-outline-level))
  10963. (setq end (save-excursion
  10964. (outline-next-heading)
  10965. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10966. (point)))
  10967. (if (and (save-excursion
  10968. (re-search-forward
  10969. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10970. (not (save-excursion (re-search-forward
  10971. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10972. (org-update-checkbox-count)
  10973. (if (and l2 (> l2 l1))
  10974. (progn
  10975. (goto-char end)
  10976. (org-update-parent-todo-statistics))
  10977. (goto-char pos)
  10978. (beginning-of-line 1)
  10979. (while (re-search-forward
  10980. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10981. (point-at-eol) t)
  10982. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10983. (goto-char pos)
  10984. (move-marker pos nil)))))
  10985. (defvar org-entry-property-inherited-from) ;; defined below
  10986. (defun org-update-parent-todo-statistics ()
  10987. "Update any statistics cookie in the parent of the current headline.
  10988. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10989. the entire subtree and this will travel up the hierarchy and update
  10990. statistics everywhere."
  10991. (let* ((prop (save-excursion (org-up-heading-safe)
  10992. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10993. (recursive (or (not org-hierarchical-todo-statistics)
  10994. (and prop (string-match "\\<recursive\\>" prop))))
  10995. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10996. 0))
  10997. (first t)
  10998. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10999. level ltoggle l1 new ndel
  11000. (cnt-all 0) (cnt-done 0) is-percent kwd
  11001. checkbox-beg ov ovs ove cookie-present)
  11002. (catch 'exit
  11003. (save-excursion
  11004. (beginning-of-line 1)
  11005. (setq ltoggle (funcall outline-level))
  11006. ;; Three situations are to consider:
  11007. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11008. ;; to the top-level ancestor on the headline;
  11009. ;; 2. If parent has "recursive" property, repeat up to the
  11010. ;; headline setting that property, taking inheritance into
  11011. ;; account;
  11012. ;; 3. Else, move up to direct parent and proceed only once.
  11013. (while (and (setq level (org-up-heading-safe))
  11014. (or recursive first)
  11015. (>= (point) lim))
  11016. (setq first nil cookie-present nil)
  11017. (unless (and level
  11018. (not (string-match
  11019. "\\<checkbox\\>"
  11020. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11021. "")))))
  11022. (throw 'exit nil))
  11023. (while (re-search-forward box-re (point-at-eol) t)
  11024. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11025. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11026. (save-match-data
  11027. (unless (outline-next-heading) (throw 'exit nil))
  11028. (while (and (looking-at org-complex-heading-regexp)
  11029. (> (setq l1 (length (match-string 1))) level))
  11030. (setq kwd (and (or recursive (= l1 ltoggle))
  11031. (match-string 2)))
  11032. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11033. (and (listp org-provide-todo-statistics)
  11034. (or (member kwd org-provide-todo-statistics)
  11035. (member kwd org-done-keywords))))
  11036. (setq cnt-all (1+ cnt-all))
  11037. (if (eq org-provide-todo-statistics t)
  11038. (and kwd (setq cnt-all (1+ cnt-all)))))
  11039. (and (member kwd org-done-keywords)
  11040. (setq cnt-done (1+ cnt-done)))
  11041. (outline-next-heading)))
  11042. (setq new
  11043. (if is-percent
  11044. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11045. (format "[%d/%d]" cnt-done cnt-all))
  11046. ndel (- (match-end 0) checkbox-beg))
  11047. ;; handle overlays when updating cookie from column view
  11048. (when (setq ov (car (overlays-at checkbox-beg)))
  11049. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11050. (delete-overlay ov))
  11051. (goto-char checkbox-beg)
  11052. (insert new)
  11053. (delete-region (point) (+ (point) ndel))
  11054. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11055. (when ov (move-overlay ov ovs ove)))
  11056. (when cookie-present
  11057. (run-hook-with-args 'org-after-todo-statistics-hook
  11058. cnt-done (- cnt-all cnt-done))))))
  11059. (run-hooks 'org-todo-statistics-hook)))
  11060. (defvar org-after-todo-statistics-hook nil
  11061. "Hook that is called after a TODO statistics cookie has been updated.
  11062. Each function is called with two arguments: the number of not-done entries
  11063. and the number of done entries.
  11064. For example, the following function, when added to this hook, will switch
  11065. an entry to DONE when all children are done, and back to TODO when new
  11066. entries are set to a TODO status. Note that this hook is only called
  11067. when there is a statistics cookie in the headline!
  11068. (defun org-summary-todo (n-done n-not-done)
  11069. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11070. (let (org-log-done org-log-states) ; turn off logging
  11071. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11072. ")
  11073. (defvar org-todo-statistics-hook nil
  11074. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11075. This hook runs even if there is no statistics cookie present, in which case
  11076. `org-after-todo-statistics-hook' would not run.")
  11077. (defun org-todo-trigger-tag-changes (state)
  11078. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11079. (let ((l org-todo-state-tags-triggers)
  11080. changes)
  11081. (when (or (not state) (equal state ""))
  11082. (setq changes (append changes (cdr (assoc "" l)))))
  11083. (when (and (stringp state) (> (length state) 0))
  11084. (setq changes (append changes (cdr (assoc state l)))))
  11085. (when (member state org-not-done-keywords)
  11086. (setq changes (append changes (cdr (assoc 'todo l)))))
  11087. (when (member state org-done-keywords)
  11088. (setq changes (append changes (cdr (assoc 'done l)))))
  11089. (dolist (c changes)
  11090. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11091. (defun org-local-logging (value)
  11092. "Get logging settings from a property VALUE."
  11093. (let* (words w a)
  11094. ;; directly set the variables, they are already local.
  11095. (setq org-log-done nil
  11096. org-log-repeat nil
  11097. org-todo-log-states nil)
  11098. (setq words (org-split-string value))
  11099. (while (setq w (pop words))
  11100. (cond
  11101. ((setq a (assoc w org-startup-options))
  11102. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11103. (set (nth 1 a) (nth 2 a))))
  11104. ((setq a (org-extract-log-state-settings w))
  11105. (and (member (car a) org-todo-keywords-1)
  11106. (push a org-todo-log-states)))))))
  11107. (defun org-get-todo-sequence-head (kwd)
  11108. "Return the head of the TODO sequence to which KWD belongs.
  11109. If KWD is not set, check if there is a text property remembering the
  11110. right sequence."
  11111. (let (p)
  11112. (cond
  11113. ((not kwd)
  11114. (or (get-text-property (point-at-bol) 'org-todo-head)
  11115. (progn
  11116. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11117. nil (point-at-eol)))
  11118. (get-text-property p 'org-todo-head))))
  11119. ((not (member kwd org-todo-keywords-1))
  11120. (car org-todo-keywords-1))
  11121. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11122. (defun org-fast-todo-selection ()
  11123. "Fast TODO keyword selection with single keys.
  11124. Returns the new TODO keyword, or nil if no state change should occur."
  11125. (let* ((fulltable org-todo-key-alist)
  11126. (done-keywords org-done-keywords) ;; needed for the faces.
  11127. (maxlen (apply 'max (mapcar
  11128. (lambda (x)
  11129. (if (stringp (car x)) (string-width (car x)) 0))
  11130. fulltable)))
  11131. (expert nil)
  11132. (fwidth (+ maxlen 3 1 3))
  11133. (ncol (/ (- (window-width) 4) fwidth))
  11134. tg cnt e c tbl
  11135. groups ingroup)
  11136. (save-excursion
  11137. (save-window-excursion
  11138. (if expert
  11139. (set-buffer (get-buffer-create " *Org todo*"))
  11140. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11141. (erase-buffer)
  11142. (org-set-local 'org-done-keywords done-keywords)
  11143. (setq tbl fulltable cnt 0)
  11144. (while (setq e (pop tbl))
  11145. (cond
  11146. ((equal e '(:startgroup))
  11147. (push '() groups) (setq ingroup t)
  11148. (when (not (= cnt 0))
  11149. (setq cnt 0)
  11150. (insert "\n"))
  11151. (insert "{ "))
  11152. ((equal e '(:endgroup))
  11153. (setq ingroup nil cnt 0)
  11154. (insert "}\n"))
  11155. ((equal e '(:newline))
  11156. (when (not (= cnt 0))
  11157. (setq cnt 0)
  11158. (insert "\n")
  11159. (setq e (car tbl))
  11160. (while (equal (car tbl) '(:newline))
  11161. (insert "\n")
  11162. (setq tbl (cdr tbl)))))
  11163. (t
  11164. (setq tg (car e) c (cdr e))
  11165. (if ingroup (push tg (car groups)))
  11166. (setq tg (org-add-props tg nil 'face
  11167. (org-get-todo-face tg)))
  11168. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11169. (insert "[" c "] " tg (make-string
  11170. (- fwidth 4 (length tg)) ?\ ))
  11171. (when (= (setq cnt (1+ cnt)) ncol)
  11172. (insert "\n")
  11173. (if ingroup (insert " "))
  11174. (setq cnt 0)))))
  11175. (insert "\n")
  11176. (goto-char (point-min))
  11177. (if (not expert) (org-fit-window-to-buffer))
  11178. (message "[a-z..]:Set [SPC]:clear")
  11179. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11180. (cond
  11181. ((or (= c ?\C-g)
  11182. (and (= c ?q) (not (rassoc c fulltable))))
  11183. (setq quit-flag t))
  11184. ((= c ?\ ) nil)
  11185. ((setq e (rassoc c fulltable) tg (car e))
  11186. tg)
  11187. (t (setq quit-flag t)))))))
  11188. (defun org-entry-is-todo-p ()
  11189. (member (org-get-todo-state) org-not-done-keywords))
  11190. (defun org-entry-is-done-p ()
  11191. (member (org-get-todo-state) org-done-keywords))
  11192. (defun org-get-todo-state ()
  11193. (save-excursion
  11194. (org-back-to-heading t)
  11195. (and (looking-at org-todo-line-regexp)
  11196. (match-end 2)
  11197. (match-string 2))))
  11198. (defun org-at-date-range-p (&optional inactive-ok)
  11199. "Is the cursor inside a date range?"
  11200. (interactive)
  11201. (save-excursion
  11202. (catch 'exit
  11203. (let ((pos (point)))
  11204. (skip-chars-backward "^[<\r\n")
  11205. (skip-chars-backward "<[")
  11206. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11207. (>= (match-end 0) pos)
  11208. (throw 'exit t))
  11209. (skip-chars-backward "^<[\r\n")
  11210. (skip-chars-backward "<[")
  11211. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11212. (>= (match-end 0) pos)
  11213. (throw 'exit t)))
  11214. nil)))
  11215. (defun org-get-repeat (&optional tagline)
  11216. "Check if there is a deadline/schedule with repeater in this entry."
  11217. (save-match-data
  11218. (save-excursion
  11219. (org-back-to-heading t)
  11220. (and (re-search-forward (if tagline
  11221. (concat tagline "\\s-*" org-repeat-re)
  11222. org-repeat-re)
  11223. (org-entry-end-position) t)
  11224. (match-string-no-properties 1)))))
  11225. (defvar org-last-changed-timestamp)
  11226. (defvar org-last-inserted-timestamp)
  11227. (defvar org-log-post-message)
  11228. (defvar org-log-note-purpose)
  11229. (defvar org-log-note-how)
  11230. (defvar org-log-note-extra)
  11231. (defun org-auto-repeat-maybe (done-word)
  11232. "Check if the current headline contains a repeated deadline/schedule.
  11233. If yes, set TODO state back to what it was and change the base date
  11234. of repeating deadline/scheduled time stamps to new date.
  11235. This function is run automatically after each state change to a DONE state."
  11236. ;; last-state is dynamically scoped into this function
  11237. (let* ((repeat (org-get-repeat))
  11238. (aa (assoc org-last-state org-todo-kwd-alist))
  11239. (interpret (nth 1 aa))
  11240. (head (nth 2 aa))
  11241. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11242. (msg "Entry repeats: ")
  11243. (org-log-done nil)
  11244. (org-todo-log-states nil)
  11245. re type n what ts time to-state)
  11246. (when repeat
  11247. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11248. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11249. org-todo-repeat-to-state))
  11250. (unless (and to-state (member to-state org-todo-keywords-1))
  11251. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11252. (org-todo to-state)
  11253. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11254. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11255. (org-time-stamp-format t t))))
  11256. (when org-log-repeat
  11257. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11258. (memq 'org-add-log-note post-command-hook))
  11259. ;; OK, we are already setup for some record
  11260. (if (eq org-log-repeat 'note)
  11261. ;; make sure we take a note, not only a time stamp
  11262. (setq org-log-note-how 'note))
  11263. ;; Set up for taking a record
  11264. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11265. org-last-state
  11266. 'findpos org-log-repeat)))
  11267. (org-back-to-heading t)
  11268. (org-add-planning-info nil nil 'closed)
  11269. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11270. org-deadline-time-regexp "\\)\\|\\("
  11271. org-ts-regexp "\\)"))
  11272. (while (re-search-forward
  11273. re (save-excursion (outline-next-heading) (point)) t)
  11274. (setq type (if (match-end 1) org-scheduled-string
  11275. (if (match-end 3) org-deadline-string "Plain:"))
  11276. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11277. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11278. (setq n (string-to-number (match-string 2 ts))
  11279. what (match-string 3 ts))
  11280. (if (equal what "w") (setq n (* n 7) what "d"))
  11281. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11282. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11283. ;; Preparation, see if we need to modify the start date for the change
  11284. (when (match-end 1)
  11285. (setq time (save-match-data (org-time-string-to-time ts)))
  11286. (cond
  11287. ((equal (match-string 1 ts) ".")
  11288. ;; Shift starting date to today
  11289. (org-timestamp-change
  11290. (- (org-today) (time-to-days time))
  11291. 'day))
  11292. ((equal (match-string 1 ts) "+")
  11293. (let ((nshiftmax 10) (nshift 0))
  11294. (while (or (= nshift 0)
  11295. (<= (time-to-days time)
  11296. (time-to-days (current-time))))
  11297. (when (= (incf nshift) nshiftmax)
  11298. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11299. (error "Abort")))
  11300. (org-timestamp-change n (cdr (assoc what whata)))
  11301. (org-at-timestamp-p t)
  11302. (setq ts (match-string 1))
  11303. (setq time (save-match-data (org-time-string-to-time ts)))))
  11304. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11305. ;; rematch, so that we have everything in place for the real shift
  11306. (org-at-timestamp-p t)
  11307. (setq ts (match-string 1))
  11308. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11309. (org-timestamp-change n (cdr (assoc what whata)))
  11310. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11311. (setq org-log-post-message msg)
  11312. (message "%s" msg))))
  11313. (defun org-show-todo-tree (arg)
  11314. "Make a compact tree which shows all headlines marked with TODO.
  11315. The tree will show the lines where the regexp matches, and all higher
  11316. headlines above the match.
  11317. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11318. With a numeric prefix N, construct a sparse tree for the Nth element
  11319. of `org-todo-keywords-1'."
  11320. (interactive "P")
  11321. (let ((case-fold-search nil)
  11322. (kwd-re
  11323. (cond ((null arg) org-not-done-regexp)
  11324. ((equal arg '(4))
  11325. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11326. (mapcar 'list org-todo-keywords-1))))
  11327. (concat "\\("
  11328. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11329. "\\)\\>")))
  11330. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11331. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11332. org-todo-keywords-1)))
  11333. (t (error "Invalid prefix argument: %s" arg)))))
  11334. (message "%d TODO entries found"
  11335. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11336. (defun org-deadline (&optional remove time)
  11337. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11338. With argument REMOVE, remove any deadline from the item.
  11339. With argument TIME, set the deadline at the corresponding date. TIME
  11340. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11341. (interactive "P")
  11342. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11343. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11344. 'region-start-level 'region))
  11345. org-loop-over-headlines-in-active-region)
  11346. (org-map-entries
  11347. `(org-deadline ',remove ,time)
  11348. org-loop-over-headlines-in-active-region
  11349. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11350. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11351. (repeater (and old-date
  11352. (string-match
  11353. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11354. old-date)
  11355. (match-string 1 old-date))))
  11356. (if remove
  11357. (progn
  11358. (when (and old-date org-log-redeadline)
  11359. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11360. org-log-redeadline))
  11361. (org-remove-timestamp-with-keyword org-deadline-string)
  11362. (message "Item no longer has a deadline."))
  11363. (org-add-planning-info 'deadline time 'closed)
  11364. (when (and old-date org-log-redeadline
  11365. (not (equal old-date
  11366. (substring org-last-inserted-timestamp 1 -1))))
  11367. (org-add-log-setup 'redeadline nil old-date 'findpos
  11368. org-log-redeadline))
  11369. (when repeater
  11370. (save-excursion
  11371. (org-back-to-heading t)
  11372. (when (re-search-forward (concat org-deadline-string " "
  11373. org-last-inserted-timestamp)
  11374. (save-excursion
  11375. (outline-next-heading) (point)) t)
  11376. (goto-char (1- (match-end 0)))
  11377. (insert " " repeater)
  11378. (setq org-last-inserted-timestamp
  11379. (concat (substring org-last-inserted-timestamp 0 -1)
  11380. " " repeater
  11381. (substring org-last-inserted-timestamp -1))))))
  11382. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11383. (defun org-schedule (&optional remove time)
  11384. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11385. With argument REMOVE, remove any scheduling date from the item.
  11386. With argument TIME, scheduled at the corresponding date. TIME can
  11387. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11388. (interactive "P")
  11389. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11390. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11391. 'region-start-level 'region))
  11392. org-loop-over-headlines-in-active-region)
  11393. (org-map-entries
  11394. `(org-schedule ',remove ,time)
  11395. org-loop-over-headlines-in-active-region
  11396. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11397. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11398. (repeater (and old-date
  11399. (string-match
  11400. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11401. old-date)
  11402. (match-string 1 old-date))))
  11403. (if remove
  11404. (progn
  11405. (when (and old-date org-log-reschedule)
  11406. (org-add-log-setup 'delschedule nil old-date 'findpos
  11407. org-log-reschedule))
  11408. (org-remove-timestamp-with-keyword org-scheduled-string)
  11409. (message "Item is no longer scheduled."))
  11410. (org-add-planning-info 'scheduled time 'closed)
  11411. (when (and old-date org-log-reschedule
  11412. (not (equal old-date
  11413. (substring org-last-inserted-timestamp 1 -1))))
  11414. (org-add-log-setup 'reschedule nil old-date 'findpos
  11415. org-log-reschedule))
  11416. (when repeater
  11417. (save-excursion
  11418. (org-back-to-heading t)
  11419. (when (re-search-forward (concat org-scheduled-string " "
  11420. org-last-inserted-timestamp)
  11421. (save-excursion
  11422. (outline-next-heading) (point)) t)
  11423. (goto-char (1- (match-end 0)))
  11424. (insert " " repeater)
  11425. (setq org-last-inserted-timestamp
  11426. (concat (substring org-last-inserted-timestamp 0 -1)
  11427. " " repeater
  11428. (substring org-last-inserted-timestamp -1))))))
  11429. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11430. (defun org-get-scheduled-time (pom &optional inherit)
  11431. "Get the scheduled time as a time tuple, of a format suitable
  11432. for calling org-schedule with, or if there is no scheduling,
  11433. returns nil."
  11434. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11435. (when time
  11436. (apply 'encode-time (org-parse-time-string time)))))
  11437. (defun org-get-deadline-time (pom &optional inherit)
  11438. "Get the deadline as a time tuple, of a format suitable for
  11439. calling org-deadline with, or if there is no scheduling, returns
  11440. nil."
  11441. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11442. (when time
  11443. (apply 'encode-time (org-parse-time-string time)))))
  11444. (defun org-remove-timestamp-with-keyword (keyword)
  11445. "Remove all time stamps with KEYWORD in the current entry."
  11446. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11447. beg)
  11448. (save-excursion
  11449. (org-back-to-heading t)
  11450. (setq beg (point))
  11451. (outline-next-heading)
  11452. (while (re-search-backward re beg t)
  11453. (replace-match "")
  11454. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11455. (equal (char-before) ?\ ))
  11456. (backward-delete-char 1)
  11457. (if (string-match "^[ \t]*$" (buffer-substring
  11458. (point-at-bol) (point-at-eol)))
  11459. (delete-region (point-at-bol)
  11460. (min (point-max) (1+ (point-at-eol))))))))))
  11461. (defun org-add-planning-info (what &optional time &rest remove)
  11462. "Insert new timestamp with keyword in the line directly after the headline.
  11463. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11464. If non is given, the user is prompted for a date.
  11465. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11466. be removed."
  11467. (interactive)
  11468. (let (org-time-was-given org-end-time-was-given ts
  11469. end default-time default-input)
  11470. (catch 'exit
  11471. (when (and (memq what '(scheduled deadline))
  11472. (or (not time)
  11473. (and (stringp time)
  11474. (string-match "^[-+]+[0-9]" time))))
  11475. ;; Try to get a default date/time from existing timestamp
  11476. (save-excursion
  11477. (org-back-to-heading t)
  11478. (setq end (save-excursion (outline-next-heading) (point)))
  11479. (when (re-search-forward (if (eq what 'scheduled)
  11480. org-scheduled-time-regexp
  11481. org-deadline-time-regexp)
  11482. end t)
  11483. (setq ts (match-string 1)
  11484. default-time
  11485. (apply 'encode-time (org-parse-time-string ts))
  11486. default-input (and ts (org-get-compact-tod ts))))))
  11487. (when what
  11488. (setq time
  11489. (if (stringp time)
  11490. ;; This is a string (relative or absolute), set proper date
  11491. (apply 'encode-time
  11492. (org-read-date-analyze
  11493. time default-time (decode-time default-time)))
  11494. ;; If necessary, get the time from the user
  11495. (or time (org-read-date nil 'to-time nil nil
  11496. default-time default-input)))))
  11497. (when (and org-insert-labeled-timestamps-at-point
  11498. (member what '(scheduled deadline)))
  11499. (insert
  11500. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11501. (org-insert-time-stamp time org-time-was-given
  11502. nil nil nil (list org-end-time-was-given))
  11503. (setq what nil))
  11504. (save-excursion
  11505. (save-restriction
  11506. (let (col list elt ts buffer-invisibility-spec)
  11507. (org-back-to-heading t)
  11508. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11509. (goto-char (match-end 1))
  11510. (setq col (current-column))
  11511. (goto-char (match-end 0))
  11512. (if (eobp) (insert "\n") (forward-char 1))
  11513. (when (and (not what)
  11514. (not (looking-at
  11515. (concat "[ \t]*"
  11516. org-keyword-time-not-clock-regexp))))
  11517. ;; Nothing to add, nothing to remove...... :-)
  11518. (throw 'exit nil))
  11519. (if (and (not (looking-at org-outline-regexp))
  11520. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11521. "[^\r\n]*"))
  11522. (not (equal (match-string 1) org-clock-string)))
  11523. (narrow-to-region (match-beginning 0) (match-end 0))
  11524. (insert-before-markers "\n")
  11525. (backward-char 1)
  11526. (narrow-to-region (point) (point))
  11527. (and org-adapt-indentation (org-indent-to-column col)))
  11528. ;; Check if we have to remove something.
  11529. (setq list (cons what remove))
  11530. (while list
  11531. (setq elt (pop list))
  11532. (when (or (and (eq elt 'scheduled)
  11533. (re-search-forward org-scheduled-time-regexp nil t))
  11534. (and (eq elt 'deadline)
  11535. (re-search-forward org-deadline-time-regexp nil t))
  11536. (and (eq elt 'closed)
  11537. (re-search-forward org-closed-time-regexp nil t)))
  11538. (replace-match "")
  11539. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11540. (and (looking-at "[ \t]+") (replace-match ""))
  11541. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11542. (when what
  11543. (insert
  11544. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11545. (cond ((eq what 'scheduled) org-scheduled-string)
  11546. ((eq what 'deadline) org-deadline-string)
  11547. ((eq what 'closed) org-closed-string))
  11548. " ")
  11549. (setq ts (org-insert-time-stamp
  11550. time
  11551. (or org-time-was-given
  11552. (and (eq what 'closed) org-log-done-with-time))
  11553. (eq what 'closed)
  11554. nil nil (list org-end-time-was-given)))
  11555. (insert
  11556. (if (not (or (bolp) (eq (char-before) ?\ )
  11557. (memq (char-after) '(32 10))
  11558. (eobp))) " " ""))
  11559. (end-of-line 1))
  11560. (goto-char (point-min))
  11561. (widen)
  11562. (if (and (looking-at "[ \t]*\n")
  11563. (equal (char-before) ?\n))
  11564. (delete-region (1- (point)) (point-at-eol)))
  11565. ts))))))
  11566. (defvar org-log-note-marker (make-marker))
  11567. (defvar org-log-note-purpose nil)
  11568. (defvar org-log-note-state nil)
  11569. (defvar org-log-note-previous-state nil)
  11570. (defvar org-log-note-how nil)
  11571. (defvar org-log-note-extra nil)
  11572. (defvar org-log-note-window-configuration nil)
  11573. (defvar org-log-note-return-to (make-marker))
  11574. (defvar org-log-note-effective-time nil
  11575. "Remembered current time so that dynamically scoped
  11576. `org-extend-today-until' affects tha timestamps in state change
  11577. log")
  11578. (defvar org-log-post-message nil
  11579. "Message to be displayed after a log note has been stored.
  11580. The auto-repeater uses this.")
  11581. (defun org-add-note ()
  11582. "Add a note to the current entry.
  11583. This is done in the same way as adding a state change note."
  11584. (interactive)
  11585. (org-add-log-setup 'note nil nil 'findpos nil))
  11586. (defvar org-property-end-re)
  11587. (defun org-add-log-setup (&optional purpose state prev-state
  11588. findpos how extra)
  11589. "Set up the post command hook to take a note.
  11590. If this is about to TODO state change, the new state is expected in STATE.
  11591. When FINDPOS is non-nil, find the correct position for the note in
  11592. the current entry. If not, assume that it can be inserted at point.
  11593. HOW is an indicator what kind of note should be created.
  11594. EXTRA is additional text that will be inserted into the notes buffer."
  11595. (let* ((org-log-into-drawer (org-log-into-drawer))
  11596. (drawer (cond ((stringp org-log-into-drawer)
  11597. org-log-into-drawer)
  11598. (org-log-into-drawer "LOGBOOK"))))
  11599. (save-restriction
  11600. (save-excursion
  11601. (when findpos
  11602. (org-back-to-heading t)
  11603. (narrow-to-region (point) (save-excursion
  11604. (outline-next-heading) (point)))
  11605. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11606. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11607. "[^\r\n]*\\)?"))
  11608. (goto-char (match-end 0))
  11609. (cond
  11610. (drawer
  11611. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11612. nil t)
  11613. (progn
  11614. (goto-char (match-end 0))
  11615. (or org-log-states-order-reversed
  11616. (and (re-search-forward org-property-end-re nil t)
  11617. (goto-char (1- (match-beginning 0))))))
  11618. (insert "\n:" drawer ":\n:END:")
  11619. (beginning-of-line 0)
  11620. (org-indent-line)
  11621. (beginning-of-line 2)
  11622. (org-indent-line)
  11623. (end-of-line 0)))
  11624. ((and org-log-state-notes-insert-after-drawers
  11625. (save-excursion
  11626. (forward-line) (looking-at org-drawer-regexp)))
  11627. (forward-line)
  11628. (while (looking-at org-drawer-regexp)
  11629. (goto-char (match-end 0))
  11630. (re-search-forward org-property-end-re (point-max) t)
  11631. (forward-line))
  11632. (forward-line -1)))
  11633. (unless org-log-states-order-reversed
  11634. (and (= (char-after) ?\n) (forward-char 1))
  11635. (org-skip-over-state-notes)
  11636. (skip-chars-backward " \t\n\r")))
  11637. (move-marker org-log-note-marker (point))
  11638. (setq org-log-note-purpose purpose
  11639. org-log-note-state state
  11640. org-log-note-previous-state prev-state
  11641. org-log-note-how how
  11642. org-log-note-extra extra
  11643. org-log-note-effective-time (org-current-effective-time))
  11644. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11645. (defun org-skip-over-state-notes ()
  11646. "Skip past the list of State notes in an entry."
  11647. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11648. (when (ignore-errors (goto-char (org-in-item-p)))
  11649. (let* ((struct (org-list-struct))
  11650. (prevs (org-list-prevs-alist struct)))
  11651. (while (looking-at "[ \t]*- State")
  11652. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11653. (org-list-get-item-end (point) struct)))))))
  11654. (defun org-add-log-note (&optional purpose)
  11655. "Pop up a window for taking a note, and add this note later at point."
  11656. (remove-hook 'post-command-hook 'org-add-log-note)
  11657. (setq org-log-note-window-configuration (current-window-configuration))
  11658. (delete-other-windows)
  11659. (move-marker org-log-note-return-to (point))
  11660. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11661. (goto-char org-log-note-marker)
  11662. (org-switch-to-buffer-other-window "*Org Note*")
  11663. (erase-buffer)
  11664. (if (memq org-log-note-how '(time state))
  11665. (let (current-prefix-arg) (org-store-log-note))
  11666. (let ((org-inhibit-startup t)) (org-mode))
  11667. (insert (format "# Insert note for %s.
  11668. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11669. (cond
  11670. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11671. ((eq org-log-note-purpose 'done) "closed todo item")
  11672. ((eq org-log-note-purpose 'state)
  11673. (format "state change from \"%s\" to \"%s\""
  11674. (or org-log-note-previous-state "")
  11675. (or org-log-note-state "")))
  11676. ((eq org-log-note-purpose 'reschedule)
  11677. "rescheduling")
  11678. ((eq org-log-note-purpose 'delschedule)
  11679. "no longer scheduled")
  11680. ((eq org-log-note-purpose 'redeadline)
  11681. "changing deadline")
  11682. ((eq org-log-note-purpose 'deldeadline)
  11683. "removing deadline")
  11684. ((eq org-log-note-purpose 'refile)
  11685. "refiling")
  11686. ((eq org-log-note-purpose 'note)
  11687. "this entry")
  11688. (t (error "This should not happen")))))
  11689. (if org-log-note-extra (insert org-log-note-extra))
  11690. (org-set-local 'org-finish-function 'org-store-log-note)
  11691. (run-hooks 'org-log-buffer-setup-hook)))
  11692. (defvar org-note-abort nil) ; dynamically scoped
  11693. (defun org-store-log-note ()
  11694. "Finish taking a log note, and insert it to where it belongs."
  11695. (let ((txt (buffer-string)))
  11696. (kill-buffer (current-buffer))
  11697. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11698. lines ind bul)
  11699. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11700. (setq txt (replace-match "" t t txt)))
  11701. (if (string-match "\\s-+\\'" txt)
  11702. (setq txt (replace-match "" t t txt)))
  11703. (setq lines (org-split-string txt "\n"))
  11704. (when (and note (string-match "\\S-" note))
  11705. (setq note
  11706. (org-replace-escapes
  11707. note
  11708. (list (cons "%u" (user-login-name))
  11709. (cons "%U" user-full-name)
  11710. (cons "%t" (format-time-string
  11711. (org-time-stamp-format 'long 'inactive)
  11712. org-log-note-effective-time))
  11713. (cons "%T" (format-time-string
  11714. (org-time-stamp-format 'long nil)
  11715. org-log-note-effective-time))
  11716. (cons "%d" (format-time-string
  11717. (org-time-stamp-format nil 'inactive)
  11718. org-log-note-effective-time))
  11719. (cons "%D" (format-time-string
  11720. (org-time-stamp-format nil nil)
  11721. org-log-note-effective-time))
  11722. (cons "%s" (if org-log-note-state
  11723. (concat "\"" org-log-note-state "\"")
  11724. ""))
  11725. (cons "%S" (if org-log-note-previous-state
  11726. (concat "\"" org-log-note-previous-state "\"")
  11727. "\"\"")))))
  11728. (if lines (setq note (concat note " \\\\")))
  11729. (push note lines))
  11730. (when (or current-prefix-arg org-note-abort)
  11731. (when org-log-into-drawer
  11732. (org-remove-empty-drawer-at
  11733. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11734. org-log-note-marker))
  11735. (setq lines nil))
  11736. (when lines
  11737. (with-current-buffer (marker-buffer org-log-note-marker)
  11738. (save-excursion
  11739. (goto-char org-log-note-marker)
  11740. (move-marker org-log-note-marker nil)
  11741. (end-of-line 1)
  11742. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11743. (setq ind (save-excursion
  11744. (if (ignore-errors (goto-char (org-in-item-p)))
  11745. (let ((struct (org-list-struct)))
  11746. (org-list-get-ind
  11747. (org-list-get-top-point struct) struct))
  11748. (skip-chars-backward " \r\t\n")
  11749. (cond
  11750. ((and (org-at-heading-p)
  11751. org-adapt-indentation)
  11752. (1+ (org-current-level)))
  11753. ((org-at-heading-p) 0)
  11754. (t (org-get-indentation))))))
  11755. (setq bul (org-list-bullet-string "-"))
  11756. (org-indent-line-to ind)
  11757. (insert bul (pop lines))
  11758. (let ((ind-body (+ (length bul) ind)))
  11759. (while lines
  11760. (insert "\n")
  11761. (org-indent-line-to ind-body)
  11762. (insert (pop lines))))
  11763. (message "Note stored")
  11764. (org-back-to-heading t)
  11765. (org-cycle-hide-drawers 'children))))))
  11766. (set-window-configuration org-log-note-window-configuration)
  11767. (with-current-buffer (marker-buffer org-log-note-return-to)
  11768. (goto-char org-log-note-return-to))
  11769. (move-marker org-log-note-return-to nil)
  11770. (and org-log-post-message (message "%s" org-log-post-message)))
  11771. (defun org-remove-empty-drawer-at (drawer pos)
  11772. "Remove an empty drawer DRAWER at position POS.
  11773. POS may also be a marker."
  11774. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11775. (save-excursion
  11776. (save-restriction
  11777. (widen)
  11778. (goto-char pos)
  11779. (if (org-in-regexp
  11780. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11781. (replace-match ""))))))
  11782. (defvar org-ts-type nil)
  11783. (defun org-sparse-tree (&optional arg type)
  11784. "Create a sparse tree, prompt for the details.
  11785. This command can create sparse trees. You first need to select the type
  11786. of match used to create the tree:
  11787. t Show all TODO entries.
  11788. T Show entries with a specific TODO keyword.
  11789. m Show entries selected by a tags/property match.
  11790. p Enter a property name and its value (both with completion on existing
  11791. names/values) and show entries with that property.
  11792. r Show entries matching a regular expression (`/' can be used as well).
  11793. b Show deadlines and scheduled items before a date.
  11794. a Show deadlines and scheduled items after a date.
  11795. d Show deadlines due within `org-deadline-warning-days'.
  11796. D Show deadlines and scheduled items between a date range."
  11797. (interactive "P")
  11798. (let (ans kwd value ts-type)
  11799. (setq type (or type org-sparse-tree-default-date-type))
  11800. (setq org-ts-type type)
  11801. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11802. (cond ((eq type 'all) "all timestamps")
  11803. ((eq type 'scheduled) "only scheduled")
  11804. ((eq type 'deadline) "only deadline")
  11805. ((eq type 'active) "only active timestamps")
  11806. ((eq type 'inactive) "only inactive timestamps")
  11807. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11808. (t "scheduled/deadline")))
  11809. (setq ans (read-char-exclusive))
  11810. (cond
  11811. ((equal ans ?c)
  11812. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11813. ((equal ans ?d)
  11814. (call-interactively 'org-check-deadlines))
  11815. ((equal ans ?b)
  11816. (call-interactively 'org-check-before-date))
  11817. ((equal ans ?a)
  11818. (call-interactively 'org-check-after-date))
  11819. ((equal ans ?D)
  11820. (call-interactively 'org-check-dates-range))
  11821. ((equal ans ?t)
  11822. (call-interactively 'org-show-todo-tree))
  11823. ((equal ans ?T)
  11824. (org-show-todo-tree '(4)))
  11825. ((member ans '(?T ?m))
  11826. (call-interactively 'org-match-sparse-tree))
  11827. ((member ans '(?p ?P))
  11828. (setq kwd (org-icompleting-read "Property: "
  11829. (mapcar 'list (org-buffer-property-keys))))
  11830. (setq value (org-icompleting-read "Value: "
  11831. (mapcar 'list (org-property-values kwd))))
  11832. (unless (string-match "\\`{.*}\\'" value)
  11833. (setq value (concat "\"" value "\"")))
  11834. (org-match-sparse-tree arg (concat kwd "=" value)))
  11835. ((member ans '(?r ?R ?/))
  11836. (call-interactively 'org-occur))
  11837. (t (error "No such sparse tree command \"%c\"" ans)))))
  11838. (defvar org-occur-highlights nil
  11839. "List of overlays used for occur matches.")
  11840. (make-variable-buffer-local 'org-occur-highlights)
  11841. (defvar org-occur-parameters nil
  11842. "Parameters of the active org-occur calls.
  11843. This is a list, each call to org-occur pushes as cons cell,
  11844. containing the regular expression and the callback, onto the list.
  11845. The list can contain several entries if `org-occur' has been called
  11846. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11847. will only contain one set of parameters. When the highlights are
  11848. removed (for example with `C-c C-c', or with the next edit (depending
  11849. on `org-remove-highlights-with-change'), this variable is emptied
  11850. as well.")
  11851. (make-variable-buffer-local 'org-occur-parameters)
  11852. (defun org-occur (regexp &optional keep-previous callback)
  11853. "Make a compact tree which shows all matches of REGEXP.
  11854. The tree will show the lines where the regexp matches, and all higher
  11855. headlines above the match. It will also show the heading after the match,
  11856. to make sure editing the matching entry is easy.
  11857. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11858. call to `org-occur' will be kept, to allow stacking of calls to this
  11859. command.
  11860. If CALLBACK is non-nil, it is a function which is called to confirm
  11861. that the match should indeed be shown."
  11862. (interactive "sRegexp: \nP")
  11863. (when (equal regexp "")
  11864. (error "Regexp cannot be empty"))
  11865. (unless keep-previous
  11866. (org-remove-occur-highlights nil nil t))
  11867. (push (cons regexp callback) org-occur-parameters)
  11868. (let ((cnt 0))
  11869. (save-excursion
  11870. (goto-char (point-min))
  11871. (if (or (not keep-previous) ; do not want to keep
  11872. (not org-occur-highlights)) ; no previous matches
  11873. ;; hide everything
  11874. (org-overview))
  11875. (while (re-search-forward regexp nil t)
  11876. (when (or (not callback)
  11877. (save-match-data (funcall callback)))
  11878. (setq cnt (1+ cnt))
  11879. (when org-highlight-sparse-tree-matches
  11880. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11881. (org-show-context 'occur-tree))))
  11882. (when org-remove-highlights-with-change
  11883. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11884. nil 'local))
  11885. (unless org-sparse-tree-open-archived-trees
  11886. (org-hide-archived-subtrees (point-min) (point-max)))
  11887. (run-hooks 'org-occur-hook)
  11888. (if (org-called-interactively-p 'interactive)
  11889. (message "%d match(es) for regexp %s" cnt regexp))
  11890. cnt))
  11891. (defun org-occur-next-match (&optional n reset)
  11892. "Function for `next-error-function' to find sparse tree matches.
  11893. N is the number of matches to move, when negative move backwards.
  11894. RESET is entirely ignored - this function always goes back to the
  11895. starting point when no match is found."
  11896. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11897. (search-func (if (< n 0)
  11898. 'previous-single-char-property-change
  11899. 'next-single-char-property-change))
  11900. (n (abs n))
  11901. (pos (point))
  11902. p1)
  11903. (catch 'exit
  11904. (while (setq p1 (funcall search-func (point) 'org-type))
  11905. (when (equal p1 limit)
  11906. (goto-char pos)
  11907. (error "No more matches"))
  11908. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11909. (setq n (1- n))
  11910. (when (= n 0)
  11911. (goto-char p1)
  11912. (throw 'exit (point))))
  11913. (goto-char p1))
  11914. (goto-char p1)
  11915. (error "No more matches"))))
  11916. (defun org-show-context (&optional key)
  11917. "Make sure point and context are visible.
  11918. How much context is shown depends upon the variables
  11919. `org-show-hierarchy-above', `org-show-following-heading',
  11920. `org-show-entry-below' and `org-show-siblings'."
  11921. (let ((heading-p (org-at-heading-p t))
  11922. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11923. (following-p (org-get-alist-option org-show-following-heading key))
  11924. (entry-p (org-get-alist-option org-show-entry-below key))
  11925. (siblings-p (org-get-alist-option org-show-siblings key)))
  11926. (catch 'exit
  11927. ;; Show heading or entry text
  11928. (if (and heading-p (not entry-p))
  11929. (org-flag-heading nil) ; only show the heading
  11930. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11931. (org-show-hidden-entry))) ; show entire entry
  11932. (when following-p
  11933. ;; Show next sibling, or heading below text
  11934. (save-excursion
  11935. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11936. (org-flag-heading nil))))
  11937. (when siblings-p (org-show-siblings))
  11938. (when hierarchy-p
  11939. ;; show all higher headings, possibly with siblings
  11940. (save-excursion
  11941. (while (and (condition-case nil
  11942. (progn (org-up-heading-all 1) t)
  11943. (error nil))
  11944. (not (bobp)))
  11945. (org-flag-heading nil)
  11946. (when siblings-p (org-show-siblings))))))))
  11947. (defvar org-reveal-start-hook nil
  11948. "Hook run before revealing a location.")
  11949. (defun org-reveal (&optional siblings)
  11950. "Show current entry, hierarchy above it, and the following headline.
  11951. This can be used to show a consistent set of context around locations
  11952. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11953. not t for the search context.
  11954. With optional argument SIBLINGS, on each level of the hierarchy all
  11955. siblings are shown. This repairs the tree structure to what it would
  11956. look like when opened with hierarchical calls to `org-cycle'.
  11957. With double optional argument \\[universal-argument] \\[universal-argument], \
  11958. go to the parent and show the
  11959. entire tree."
  11960. (interactive "P")
  11961. (run-hooks 'org-reveal-start-hook)
  11962. (let ((org-show-hierarchy-above t)
  11963. (org-show-following-heading t)
  11964. (org-show-siblings (if siblings t org-show-siblings)))
  11965. (org-show-context nil))
  11966. (when (equal siblings '(16))
  11967. (save-excursion
  11968. (when (org-up-heading-safe)
  11969. (org-show-subtree)
  11970. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11971. (defun org-highlight-new-match (beg end)
  11972. "Highlight from BEG to END and mark the highlight is an occur headline."
  11973. (let ((ov (make-overlay beg end)))
  11974. (overlay-put ov 'face 'secondary-selection)
  11975. (overlay-put ov 'org-type 'org-occur)
  11976. (push ov org-occur-highlights)))
  11977. (defun org-remove-occur-highlights (&optional beg end noremove)
  11978. "Remove the occur highlights from the buffer.
  11979. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11980. from the `before-change-functions' in the current buffer."
  11981. (interactive)
  11982. (unless org-inhibit-highlight-removal
  11983. (mapc 'delete-overlay org-occur-highlights)
  11984. (setq org-occur-highlights nil)
  11985. (setq org-occur-parameters nil)
  11986. (unless noremove
  11987. (remove-hook 'before-change-functions
  11988. 'org-remove-occur-highlights 'local))))
  11989. ;;;; Priorities
  11990. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11991. "Regular expression matching the priority indicator.")
  11992. (defvar org-remove-priority-next-time nil)
  11993. (defun org-priority-up ()
  11994. "Increase the priority of the current item."
  11995. (interactive)
  11996. (org-priority 'up))
  11997. (defun org-priority-down ()
  11998. "Decrease the priority of the current item."
  11999. (interactive)
  12000. (org-priority 'down))
  12001. (defun org-priority (&optional action show)
  12002. "Change the priority of an item.
  12003. ACTION can be `set', `up', `down', or a character."
  12004. (interactive "P")
  12005. (if (equal action '(4))
  12006. (org-show-priority)
  12007. (unless org-enable-priority-commands
  12008. (error "Priority commands are disabled"))
  12009. (setq action (or action 'set))
  12010. (let (current new news have remove)
  12011. (save-excursion
  12012. (org-back-to-heading t)
  12013. (if (looking-at org-priority-regexp)
  12014. (setq current (string-to-char (match-string 2))
  12015. have t))
  12016. (cond
  12017. ((eq action 'remove)
  12018. (setq remove t new ?\ ))
  12019. ((or (eq action 'set)
  12020. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12021. (if (not (eq action 'set))
  12022. (setq new action)
  12023. (message "Priority %c-%c, SPC to remove: "
  12024. org-highest-priority org-lowest-priority)
  12025. (save-match-data
  12026. (setq new (read-char-exclusive))))
  12027. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12028. (= (upcase org-lowest-priority) org-lowest-priority))
  12029. (setq new (upcase new)))
  12030. (cond ((equal new ?\ ) (setq remove t))
  12031. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12032. (error "Priority must be between `%c' and `%c'"
  12033. org-highest-priority org-lowest-priority))))
  12034. ((eq action 'up)
  12035. (setq new (if have
  12036. (1- current) ; normal cycling
  12037. ;; last priority was empty
  12038. (if (eq last-command this-command)
  12039. org-lowest-priority ; wrap around empty to lowest
  12040. ;; default
  12041. (if org-priority-start-cycle-with-default
  12042. org-default-priority
  12043. (1- org-default-priority))))))
  12044. ((eq action 'down)
  12045. (setq new (if have
  12046. (1+ current) ; normal cycling
  12047. ;; last priority was empty
  12048. (if (eq last-command this-command)
  12049. org-highest-priority ; wrap around empty to highest
  12050. ;; default
  12051. (if org-priority-start-cycle-with-default
  12052. org-default-priority
  12053. (1+ org-default-priority))))))
  12054. (t (error "Invalid action")))
  12055. (if (or (< (upcase new) org-highest-priority)
  12056. (> (upcase new) org-lowest-priority))
  12057. (if (and (memq action '(up down))
  12058. (not have) (not (eq last-command this-command)))
  12059. ;; `new' is from default priority
  12060. (error
  12061. "The default can not be set, see `org-default-priority' why")
  12062. ;; normal cycling: `new' is beyond highest/lowest priority
  12063. ;; and is wrapped around to the empty priority
  12064. (setq remove t)))
  12065. (setq news (format "%c" new))
  12066. (if have
  12067. (if remove
  12068. (replace-match "" t t nil 1)
  12069. (replace-match news t t nil 2))
  12070. (if remove
  12071. (error "No priority cookie found in line")
  12072. (let ((case-fold-search nil))
  12073. (looking-at org-todo-line-regexp))
  12074. (if (match-end 2)
  12075. (progn
  12076. (goto-char (match-end 2))
  12077. (insert " [#" news "]"))
  12078. (goto-char (match-beginning 3))
  12079. (insert "[#" news "] "))))
  12080. (org-preserve-lc (org-set-tags nil 'align)))
  12081. (if remove
  12082. (message "Priority removed")
  12083. (message "Priority of current item set to %s" news)))))
  12084. (defun org-show-priority ()
  12085. "Show the priority of the current item.
  12086. This priority is composed of the main priority given with the [#A] cookies,
  12087. and by additional input from the age of a schedules or deadline entry."
  12088. (interactive)
  12089. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12090. (org-get-at-bol 'priority)
  12091. (save-excursion
  12092. (save-match-data
  12093. (beginning-of-line)
  12094. (and (looking-at org-heading-regexp)
  12095. (org-get-priority (match-string 0))))))))
  12096. (message "Priority is %d" (if pri pri -1000))))
  12097. (defun org-get-priority (s)
  12098. "Find priority cookie and return priority."
  12099. (save-match-data
  12100. (if (functionp org-get-priority-function)
  12101. (funcall org-get-priority-function)
  12102. (if (not (string-match org-priority-regexp s))
  12103. (* 1000 (- org-lowest-priority org-default-priority))
  12104. (* 1000 (- org-lowest-priority
  12105. (string-to-char (match-string 2 s))))))))
  12106. ;;;; Tags
  12107. (defvar org-agenda-archives-mode)
  12108. (defvar org-map-continue-from nil
  12109. "Position from where mapping should continue.
  12110. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12111. (defvar org-scanner-tags nil
  12112. "The current tag list while the tags scanner is running.")
  12113. (defvar org-trust-scanner-tags nil
  12114. "Should `org-get-tags-at' use the tags for the scanner.
  12115. This is for internal dynamical scoping only.
  12116. When this is non-nil, the function `org-get-tags-at' will return the value
  12117. of `org-scanner-tags' instead of building the list by itself. This
  12118. can lead to large speed-ups when the tags scanner is used in a file with
  12119. many entries, and when the list of tags is retrieved, for example to
  12120. obtain a list of properties. Building the tags list for each entry in such
  12121. a file becomes an N^2 operation - but with this variable set, it scales
  12122. as N.")
  12123. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12124. "Scan headline tags with inheritance and produce output ACTION.
  12125. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12126. or `agenda' to produce an entry list for an agenda view. It can also be
  12127. a Lisp form or a function that should be called at each matched headline, in
  12128. this case the return value is a list of all return values from these calls.
  12129. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12130. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12131. only lines with a not-done TODO keyword are included in the output.
  12132. This should be the same variable that was scoped into
  12133. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12134. START-LEVEL can be a string with asterisks, reducing the scope to
  12135. headlines matching this string."
  12136. (require 'org-agenda)
  12137. (let* ((re (concat "^"
  12138. (if start-level
  12139. ;; Get the correct level to match
  12140. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12141. org-outline-regexp)
  12142. " *\\(\\<\\("
  12143. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12144. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12145. (props (list 'face 'default
  12146. 'done-face 'org-agenda-done
  12147. 'undone-face 'default
  12148. 'mouse-face 'highlight
  12149. 'org-not-done-regexp org-not-done-regexp
  12150. 'org-todo-regexp org-todo-regexp
  12151. 'org-complex-heading-regexp org-complex-heading-regexp
  12152. 'help-echo
  12153. (format "mouse-2 or RET jump to org file %s"
  12154. (abbreviate-file-name
  12155. (or (buffer-file-name (buffer-base-buffer))
  12156. (buffer-name (buffer-base-buffer)))))))
  12157. (case-fold-search nil)
  12158. (org-map-continue-from nil)
  12159. lspos tags tags-list
  12160. (tags-alist (list (cons 0 org-file-tags)))
  12161. (llast 0) rtn rtn1 level category i txt
  12162. todo marker entry priority)
  12163. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12164. (setq action (list 'lambda nil action)))
  12165. (save-excursion
  12166. (goto-char (point-min))
  12167. (when (eq action 'sparse-tree)
  12168. (org-overview)
  12169. (org-remove-occur-highlights))
  12170. (while (re-search-forward re nil t)
  12171. (setq org-map-continue-from nil)
  12172. (catch :skip
  12173. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12174. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12175. (goto-char (setq lspos (match-beginning 0)))
  12176. (setq level (org-reduced-level (org-outline-level))
  12177. category (org-get-category))
  12178. (setq i llast llast level)
  12179. ;; remove tag lists from same and sublevels
  12180. (while (>= i level)
  12181. (when (setq entry (assoc i tags-alist))
  12182. (setq tags-alist (delete entry tags-alist)))
  12183. (setq i (1- i)))
  12184. ;; add the next tags
  12185. (when tags
  12186. (setq tags (org-split-string tags ":")
  12187. tags-alist
  12188. (cons (cons level tags) tags-alist)))
  12189. ;; compile tags for current headline
  12190. (setq tags-list
  12191. (if org-use-tag-inheritance
  12192. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12193. tags)
  12194. org-scanner-tags tags-list)
  12195. (when org-use-tag-inheritance
  12196. (setcdr (car tags-alist)
  12197. (mapcar (lambda (x)
  12198. (setq x (copy-sequence x))
  12199. (org-add-prop-inherited x))
  12200. (cdar tags-alist))))
  12201. (when (and tags org-use-tag-inheritance
  12202. (or (not (eq t org-use-tag-inheritance))
  12203. org-tags-exclude-from-inheritance))
  12204. ;; selective inheritance, remove uninherited ones
  12205. (setcdr (car tags-alist)
  12206. (org-remove-uninherited-tags (cdar tags-alist))))
  12207. (when (and
  12208. ;; eval matcher only when the todo condition is OK
  12209. (and (or (not todo-only) (member todo org-not-done-keywords))
  12210. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12211. (eval matcher)))
  12212. ;; Call the skipper, but return t if it does not skip,
  12213. ;; so that the `and' form continues evaluating
  12214. (progn
  12215. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12216. t)
  12217. ;; Check if timestamps are deselecting this entry
  12218. (or (not todo-only)
  12219. (and (member todo org-not-done-keywords)
  12220. (or (not org-agenda-tags-todo-honor-ignore-options)
  12221. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12222. ;; select this headline
  12223. (cond
  12224. ((eq action 'sparse-tree)
  12225. (and org-highlight-sparse-tree-matches
  12226. (org-get-heading) (match-end 0)
  12227. (org-highlight-new-match
  12228. (match-beginning 1) (match-end 1)))
  12229. (org-show-context 'tags-tree))
  12230. ((eq action 'agenda)
  12231. (setq txt (org-agenda-format-item
  12232. ""
  12233. (concat
  12234. (if (eq org-tags-match-list-sublevels 'indented)
  12235. (make-string (1- level) ?.) "")
  12236. (org-get-heading))
  12237. level category
  12238. tags-list)
  12239. priority (org-get-priority txt))
  12240. (goto-char lspos)
  12241. (setq marker (org-agenda-new-marker))
  12242. (org-add-props txt props
  12243. 'org-marker marker 'org-hd-marker marker 'org-category category
  12244. 'todo-state todo
  12245. 'priority priority 'type "tagsmatch")
  12246. (push txt rtn))
  12247. ((functionp action)
  12248. (setq org-map-continue-from nil)
  12249. (save-excursion
  12250. (setq rtn1 (funcall action))
  12251. (push rtn1 rtn)))
  12252. (t (error "Invalid action")))
  12253. ;; if we are to skip sublevels, jump to end of subtree
  12254. (unless org-tags-match-list-sublevels
  12255. (org-end-of-subtree t)
  12256. (backward-char 1))))
  12257. ;; Get the correct position from where to continue
  12258. (if org-map-continue-from
  12259. (goto-char org-map-continue-from)
  12260. (and (= (point) lspos) (end-of-line 1)))))
  12261. (when (and (eq action 'sparse-tree)
  12262. (not org-sparse-tree-open-archived-trees))
  12263. (org-hide-archived-subtrees (point-min) (point-max)))
  12264. (nreverse rtn)))
  12265. (defun org-remove-uninherited-tags (tags)
  12266. "Remove all tags that are not inherited from the list TAGS."
  12267. (cond
  12268. ((eq org-use-tag-inheritance t)
  12269. (if org-tags-exclude-from-inheritance
  12270. (org-delete-all org-tags-exclude-from-inheritance tags)
  12271. tags))
  12272. ((not org-use-tag-inheritance) nil)
  12273. ((stringp org-use-tag-inheritance)
  12274. (delq nil (mapcar
  12275. (lambda (x)
  12276. (if (and (string-match org-use-tag-inheritance x)
  12277. (not (member x org-tags-exclude-from-inheritance)))
  12278. x nil))
  12279. tags)))
  12280. ((listp org-use-tag-inheritance)
  12281. (delq nil (mapcar
  12282. (lambda (x)
  12283. (if (member x org-use-tag-inheritance) x nil))
  12284. tags)))))
  12285. (defun org-match-sparse-tree (&optional todo-only match)
  12286. "Create a sparse tree according to tags string MATCH.
  12287. MATCH can contain positive and negative selection of tags, like
  12288. \"+WORK+URGENT-WITHBOSS\".
  12289. If optional argument TODO-ONLY is non-nil, only select lines that are
  12290. also TODO lines."
  12291. (interactive "P")
  12292. (org-agenda-prepare-buffers (list (current-buffer)))
  12293. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12294. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12295. (defvar org-cached-props nil)
  12296. (defun org-cached-entry-get (pom property)
  12297. (if (or (eq t org-use-property-inheritance)
  12298. (and (stringp org-use-property-inheritance)
  12299. (string-match org-use-property-inheritance property))
  12300. (and (listp org-use-property-inheritance)
  12301. (member property org-use-property-inheritance)))
  12302. ;; Caching is not possible, check it directly
  12303. (org-entry-get pom property 'inherit)
  12304. ;; Get all properties, so that we can do complicated checks easily
  12305. (cdr (assoc property (or org-cached-props
  12306. (setq org-cached-props
  12307. (org-entry-properties pom)))))))
  12308. (defun org-global-tags-completion-table (&optional files)
  12309. "Return the list of all tags in all agenda buffer/files.
  12310. Optional FILES argument is a list of files which can be used
  12311. instead of the agenda files."
  12312. (save-excursion
  12313. (org-uniquify
  12314. (delq nil
  12315. (apply 'append
  12316. (mapcar
  12317. (lambda (file)
  12318. (set-buffer (find-file-noselect file))
  12319. (append (org-get-buffer-tags)
  12320. (mapcar (lambda (x) (if (stringp (car-safe x))
  12321. (list (car-safe x)) nil))
  12322. org-tag-alist)))
  12323. (if (and files (car files))
  12324. files
  12325. (org-agenda-files))))))))
  12326. (defun org-make-tags-matcher (match)
  12327. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12328. The variable `todo-only' is scoped dynamically into this function.
  12329. It will be set to t if the matcher restricts matching to TODO entries,
  12330. otherwise will not be touched.
  12331. Returns a cons of the selection string MATCH and the constructed
  12332. lisp form implementing the matcher. The matcher is to be evaluated
  12333. at an Org entry, with point on the headline, and returns t if the
  12334. entry matches the selection string MATCH. The returned lisp form
  12335. references two variables with information about the entry, which
  12336. must be bound around the form's evaluation: todo, the TODO keyword
  12337. at the entry (or nil of none); and tags-list, the list of all tags
  12338. at the entry including inherited ones. Additionally, the category
  12339. of the entry (if any) must be specified as the text property
  12340. 'org-category on the headline.
  12341. See also `org-scan-tags'.
  12342. "
  12343. (declare (special todo-only))
  12344. (unless (boundp 'todo-only)
  12345. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12346. (unless match
  12347. ;; Get a new match request, with completion
  12348. (let ((org-last-tags-completion-table
  12349. (org-global-tags-completion-table)))
  12350. (setq match (org-completing-read-no-i
  12351. "Match: " 'org-tags-completion-function nil nil nil
  12352. 'org-tags-history))))
  12353. ;; Parse the string and create a lisp form
  12354. (let ((match0 match)
  12355. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12356. minus tag mm
  12357. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12358. orterms term orlist re-p str-p level-p level-op time-p
  12359. prop-p pn pv po gv rest)
  12360. (if (string-match "/+" match)
  12361. ;; match contains also a todo-matching request
  12362. (progn
  12363. (setq tagsmatch (substring match 0 (match-beginning 0))
  12364. todomatch (substring match (match-end 0)))
  12365. (if (string-match "^!" todomatch)
  12366. (setq todo-only t todomatch (substring todomatch 1)))
  12367. (if (string-match "^\\s-*$" todomatch)
  12368. (setq todomatch nil)))
  12369. ;; only matching tags
  12370. (setq tagsmatch match todomatch nil))
  12371. ;; Make the tags matcher
  12372. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12373. (setq tagsmatcher t)
  12374. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12375. (while (setq term (pop orterms))
  12376. (while (and (equal (substring term -1) "\\") orterms)
  12377. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12378. (while (string-match re term)
  12379. (setq rest (substring term (match-end 0))
  12380. minus (and (match-end 1)
  12381. (equal (match-string 1 term) "-"))
  12382. tag (save-match-data (replace-regexp-in-string
  12383. "\\\\-" "-"
  12384. (match-string 2 term)))
  12385. re-p (equal (string-to-char tag) ?{)
  12386. level-p (match-end 4)
  12387. prop-p (match-end 5)
  12388. mm (cond
  12389. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12390. (level-p
  12391. (setq level-op (org-op-to-function (match-string 3 term)))
  12392. `(,level-op level ,(string-to-number
  12393. (match-string 4 term))))
  12394. (prop-p
  12395. (setq pn (match-string 5 term)
  12396. po (match-string 6 term)
  12397. pv (match-string 7 term)
  12398. re-p (equal (string-to-char pv) ?{)
  12399. str-p (equal (string-to-char pv) ?\")
  12400. time-p (save-match-data
  12401. (string-match "^\"[[<].*[]>]\"$" pv))
  12402. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12403. (if time-p (setq pv (org-matcher-time pv)))
  12404. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12405. (cond
  12406. ((equal pn "CATEGORY")
  12407. (setq gv '(get-text-property (point) 'org-category)))
  12408. ((equal pn "TODO")
  12409. (setq gv 'todo))
  12410. (t
  12411. (setq gv `(org-cached-entry-get nil ,pn))))
  12412. (if re-p
  12413. (if (eq po 'org<>)
  12414. `(not (string-match ,pv (or ,gv "")))
  12415. `(string-match ,pv (or ,gv "")))
  12416. (if str-p
  12417. `(,po (or ,gv "") ,pv)
  12418. `(,po (string-to-number (or ,gv ""))
  12419. ,(string-to-number pv) ))))
  12420. (t `(member ,tag tags-list)))
  12421. mm (if minus (list 'not mm) mm)
  12422. term rest)
  12423. (push mm tagsmatcher))
  12424. (push (if (> (length tagsmatcher) 1)
  12425. (cons 'and tagsmatcher)
  12426. (car tagsmatcher))
  12427. orlist)
  12428. (setq tagsmatcher nil))
  12429. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12430. (setq tagsmatcher
  12431. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12432. ;; Make the todo matcher
  12433. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12434. (setq todomatcher t)
  12435. (setq orterms (org-split-string todomatch "|") orlist nil)
  12436. (while (setq term (pop orterms))
  12437. (while (string-match re term)
  12438. (setq minus (and (match-end 1)
  12439. (equal (match-string 1 term) "-"))
  12440. kwd (match-string 2 term)
  12441. re-p (equal (string-to-char kwd) ?{)
  12442. term (substring term (match-end 0))
  12443. mm (if re-p
  12444. `(string-match ,(substring kwd 1 -1) todo)
  12445. (list 'equal 'todo kwd))
  12446. mm (if minus (list 'not mm) mm))
  12447. (push mm todomatcher))
  12448. (push (if (> (length todomatcher) 1)
  12449. (cons 'and todomatcher)
  12450. (car todomatcher))
  12451. orlist)
  12452. (setq todomatcher nil))
  12453. (setq todomatcher (if (> (length orlist) 1)
  12454. (cons 'or orlist) (car orlist))))
  12455. ;; Return the string and lisp forms of the matcher
  12456. (setq matcher (if todomatcher
  12457. (list 'and tagsmatcher todomatcher)
  12458. tagsmatcher))
  12459. (when todo-only
  12460. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12461. matcher)))
  12462. (cons match0 matcher)))
  12463. (defun org-op-to-function (op &optional stringp)
  12464. "Turn an operator into the appropriate function."
  12465. (setq op
  12466. (cond
  12467. ((equal op "<" ) '(< string< org-time<))
  12468. ((equal op ">" ) '(> org-string> org-time>))
  12469. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12470. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12471. ((member op '("=" "==")) '(= string= org-time=))
  12472. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12473. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12474. (defun org<> (a b) (not (= a b)))
  12475. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12476. (defun org-string>= (a b) (not (string< a b)))
  12477. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12478. (defun org-string<> (a b) (not (string= a b)))
  12479. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12480. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12481. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12482. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12483. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12484. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12485. (defun org-2ft (s)
  12486. "Convert S to a floating point time.
  12487. If S is already a number, just return it. If it is a string, parse
  12488. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12489. (cond
  12490. ((numberp s) s)
  12491. ((stringp s)
  12492. (condition-case nil
  12493. (float-time (apply 'encode-time (org-parse-time-string s)))
  12494. (error 0.)))
  12495. (t 0.)))
  12496. (defun org-time-today ()
  12497. "Time in seconds today at 0:00.
  12498. Returns the float number of seconds since the beginning of the
  12499. epoch to the beginning of today (00:00)."
  12500. (float-time (apply 'encode-time
  12501. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12502. (defun org-matcher-time (s)
  12503. "Interpret a time comparison value."
  12504. (save-match-data
  12505. (cond
  12506. ((string= s "<now>") (float-time))
  12507. ((string= s "<today>") (org-time-today))
  12508. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12509. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12510. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12511. (+ (org-time-today)
  12512. (* (string-to-number (match-string 1 s))
  12513. (cdr (assoc (match-string 2 s)
  12514. '(("d" . 86400.0) ("w" . 604800.0)
  12515. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12516. (t (org-2ft s)))))
  12517. (defun org-match-any-p (re list)
  12518. "Does re match any element of list?"
  12519. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12520. (delq nil list))
  12521. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12522. (defvar org-tags-overlay (make-overlay 1 1))
  12523. (org-detach-overlay org-tags-overlay)
  12524. (defun org-get-local-tags-at (&optional pos)
  12525. "Get a list of tags defined in the current headline."
  12526. (org-get-tags-at pos 'local))
  12527. (defun org-get-local-tags ()
  12528. "Get a list of tags defined in the current headline."
  12529. (org-get-tags-at nil 'local))
  12530. (defun org-get-tags-at (&optional pos local)
  12531. "Get a list of all headline tags applicable at POS.
  12532. POS defaults to point. If tags are inherited, the list contains
  12533. the targets in the same sequence as the headlines appear, i.e.
  12534. the tags of the current headline come last.
  12535. When LOCAL is non-nil, only return tags from the current headline,
  12536. ignore inherited ones."
  12537. (interactive)
  12538. (if (and org-trust-scanner-tags
  12539. (or (not pos) (equal pos (point)))
  12540. (not local))
  12541. org-scanner-tags
  12542. (let (tags ltags lastpos parent)
  12543. (save-excursion
  12544. (save-restriction
  12545. (widen)
  12546. (goto-char (or pos (point)))
  12547. (save-match-data
  12548. (catch 'done
  12549. (condition-case nil
  12550. (progn
  12551. (org-back-to-heading t)
  12552. (while (not (equal lastpos (point)))
  12553. (setq lastpos (point))
  12554. (when (looking-at
  12555. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12556. (setq ltags (org-split-string
  12557. (org-match-string-no-properties 1) ":"))
  12558. (when parent
  12559. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12560. (setq tags (append
  12561. (if parent
  12562. (org-remove-uninherited-tags ltags)
  12563. ltags)
  12564. tags)))
  12565. (or org-use-tag-inheritance (throw 'done t))
  12566. (if local (throw 'done t))
  12567. (or (org-up-heading-safe) (error nil))
  12568. (setq parent t)))
  12569. (error nil)))))
  12570. (if local
  12571. tags
  12572. (reverse (delete-dups
  12573. (reverse (append
  12574. (org-remove-uninherited-tags
  12575. org-file-tags) tags)))))))))
  12576. (defun org-add-prop-inherited (s)
  12577. (add-text-properties 0 (length s) '(inherited t) s)
  12578. s)
  12579. (defun org-toggle-tag (tag &optional onoff)
  12580. "Toggle the tag TAG for the current line.
  12581. If ONOFF is `on' or `off', don't toggle but set to this state."
  12582. (let (res current)
  12583. (save-excursion
  12584. (org-back-to-heading t)
  12585. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12586. (point-at-eol) t)
  12587. (progn
  12588. (setq current (match-string 1))
  12589. (replace-match ""))
  12590. (setq current ""))
  12591. (setq current (nreverse (org-split-string current ":")))
  12592. (cond
  12593. ((eq onoff 'on)
  12594. (setq res t)
  12595. (or (member tag current) (push tag current)))
  12596. ((eq onoff 'off)
  12597. (or (not (member tag current)) (setq current (delete tag current))))
  12598. (t (if (member tag current)
  12599. (setq current (delete tag current))
  12600. (setq res t)
  12601. (push tag current))))
  12602. (end-of-line 1)
  12603. (if current
  12604. (progn
  12605. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12606. (org-set-tags nil t))
  12607. (delete-horizontal-space))
  12608. (run-hooks 'org-after-tags-change-hook))
  12609. res))
  12610. (defun org-align-tags-here (to-col)
  12611. ;; Assumes that this is a headline
  12612. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12613. (beginning-of-line 1)
  12614. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12615. (< pos (match-beginning 2)))
  12616. (progn
  12617. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12618. (goto-char (match-beginning 1))
  12619. (insert " ")
  12620. (delete-region (point) (1+ (match-beginning 2)))
  12621. (setq ncol (max (current-column)
  12622. (1+ col)
  12623. (if (> to-col 0)
  12624. to-col
  12625. (- (abs to-col) tags-l))))
  12626. (setq p (point))
  12627. (insert (make-string (- ncol (current-column)) ?\ ))
  12628. (setq ncol (current-column))
  12629. (when indent-tabs-mode (tabify p (point-at-eol)))
  12630. (org-move-to-column (min ncol col) t))
  12631. (goto-char pos))))
  12632. (defun org-set-tags-command (&optional arg just-align)
  12633. "Call the set-tags command for the current entry."
  12634. (interactive "P")
  12635. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12636. (org-set-tags arg just-align)
  12637. (save-excursion
  12638. (org-back-to-heading t)
  12639. (org-set-tags arg just-align))))
  12640. (defun org-set-tags-to (data)
  12641. "Set the tags of the current entry to DATA, replacing the current tags.
  12642. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12643. If DATA is nil or the empty string, any tags will be removed."
  12644. (interactive "sTags: ")
  12645. (setq data
  12646. (cond
  12647. ((eq data nil) "")
  12648. ((equal data "") "")
  12649. ((stringp data)
  12650. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12651. ":"))
  12652. ((listp data)
  12653. (concat ":" (mapconcat 'identity data ":") ":"))))
  12654. (when data
  12655. (save-excursion
  12656. (org-back-to-heading t)
  12657. (when (looking-at org-complex-heading-regexp)
  12658. (if (match-end 5)
  12659. (progn
  12660. (goto-char (match-beginning 5))
  12661. (insert data)
  12662. (delete-region (point) (point-at-eol))
  12663. (org-set-tags nil 'align))
  12664. (goto-char (point-at-eol))
  12665. (insert " " data)
  12666. (org-set-tags nil 'align)))
  12667. (beginning-of-line 1)
  12668. (if (looking-at ".*?\\([ \t]+\\)$")
  12669. (delete-region (match-beginning 1) (match-end 1))))))
  12670. (defun org-align-all-tags ()
  12671. "Align the tags i all headings."
  12672. (interactive)
  12673. (save-excursion
  12674. (or (ignore-errors (org-back-to-heading t))
  12675. (outline-next-heading))
  12676. (if (org-at-heading-p)
  12677. (org-set-tags t)
  12678. (message "No headings"))))
  12679. (defvar org-indent-indentation-per-level)
  12680. (defun org-set-tags (&optional arg just-align)
  12681. "Set the tags for the current headline.
  12682. With prefix ARG, realign all tags in headings in the current buffer."
  12683. (interactive "P")
  12684. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12685. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12686. 'region-start-level 'region))
  12687. org-loop-over-headlines-in-active-region)
  12688. (org-map-entries
  12689. ;; We don't use ARG and JUST-ALIGN here these args are not
  12690. ;; useful when looping over headlines
  12691. `(org-set-tags)
  12692. org-loop-over-headlines-in-active-region
  12693. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12694. (let* ((re org-outline-regexp-bol)
  12695. (current (unless arg (org-get-tags-string)))
  12696. (col (current-column))
  12697. (org-setting-tags t)
  12698. table current-tags inherited-tags ; computed below when needed
  12699. tags p0 c0 c1 rpl di tc level)
  12700. (if arg
  12701. (save-excursion
  12702. (goto-char (point-min))
  12703. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12704. (while (re-search-forward re nil t)
  12705. (org-set-tags nil t)
  12706. (end-of-line 1)))
  12707. (message "All tags realigned to column %d" org-tags-column))
  12708. (if just-align
  12709. (setq tags current)
  12710. ;; Get a new set of tags from the user
  12711. (save-excursion
  12712. (setq table (append org-tag-persistent-alist
  12713. (or org-tag-alist (org-get-buffer-tags))
  12714. (and
  12715. org-complete-tags-always-offer-all-agenda-tags
  12716. (org-global-tags-completion-table
  12717. (org-agenda-files))))
  12718. org-last-tags-completion-table table
  12719. current-tags (org-split-string current ":")
  12720. inherited-tags (nreverse
  12721. (nthcdr (length current-tags)
  12722. (nreverse (org-get-tags-at))))
  12723. tags
  12724. (if (or (eq t org-use-fast-tag-selection)
  12725. (and org-use-fast-tag-selection
  12726. (delq nil (mapcar 'cdr table))))
  12727. (org-fast-tag-selection
  12728. current-tags inherited-tags table
  12729. (if org-fast-tag-selection-include-todo
  12730. org-todo-key-alist))
  12731. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12732. (org-trim
  12733. (org-icompleting-read "Tags: "
  12734. 'org-tags-completion-function
  12735. nil nil current 'org-tags-history))))))
  12736. (while (string-match "[-+&]+" tags)
  12737. ;; No boolean logic, just a list
  12738. (setq tags (replace-match ":" t t tags))))
  12739. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12740. (if org-tags-sort-function
  12741. (setq tags (mapconcat 'identity
  12742. (sort (org-split-string
  12743. tags (org-re "[^[:alnum:]_@#%]+"))
  12744. org-tags-sort-function) ":")))
  12745. (if (string-match "\\`[\t ]*\\'" tags)
  12746. (setq tags "")
  12747. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12748. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12749. ;; Insert new tags at the correct column
  12750. (beginning-of-line 1)
  12751. (setq level (or (and (looking-at org-outline-regexp)
  12752. (- (match-end 0) (point) 1))
  12753. 1))
  12754. (cond
  12755. ((and (equal current "") (equal tags "")))
  12756. ((re-search-forward
  12757. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12758. (point-at-eol) t)
  12759. (if (equal tags "")
  12760. (setq rpl "")
  12761. (goto-char (match-beginning 0))
  12762. (setq c0 (current-column)
  12763. ;; compute offset for the case of org-indent-mode active
  12764. di (if org-indent-mode
  12765. (* (1- org-indent-indentation-per-level) (1- level))
  12766. 0)
  12767. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12768. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12769. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12770. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12771. (replace-match rpl t t)
  12772. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12773. tags)
  12774. (t (error "Tags alignment failed")))
  12775. (org-move-to-column col)
  12776. (unless just-align
  12777. (run-hooks 'org-after-tags-change-hook))))))
  12778. (defun org-change-tag-in-region (beg end tag off)
  12779. "Add or remove TAG for each entry in the region.
  12780. This works in the agenda, and also in an org-mode buffer."
  12781. (interactive
  12782. (list (region-beginning) (region-end)
  12783. (let ((org-last-tags-completion-table
  12784. (if (derived-mode-p 'org-mode)
  12785. (org-get-buffer-tags)
  12786. (org-global-tags-completion-table))))
  12787. (org-icompleting-read
  12788. "Tag: " 'org-tags-completion-function nil nil nil
  12789. 'org-tags-history))
  12790. (progn
  12791. (message "[s]et or [r]emove? ")
  12792. (equal (read-char-exclusive) ?r))))
  12793. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12794. (let ((agendap (equal major-mode 'org-agenda-mode))
  12795. l1 l2 m buf pos newhead (cnt 0))
  12796. (goto-char end)
  12797. (setq l2 (1- (org-current-line)))
  12798. (goto-char beg)
  12799. (setq l1 (org-current-line))
  12800. (loop for l from l1 to l2 do
  12801. (org-goto-line l)
  12802. (setq m (get-text-property (point) 'org-hd-marker))
  12803. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12804. (and agendap m))
  12805. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12806. pos (if agendap m (point)))
  12807. (with-current-buffer buf
  12808. (save-excursion
  12809. (save-restriction
  12810. (goto-char pos)
  12811. (setq cnt (1+ cnt))
  12812. (org-toggle-tag tag (if off 'off 'on))
  12813. (setq newhead (org-get-heading)))))
  12814. (and agendap (org-agenda-change-all-lines newhead m))))
  12815. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12816. (defun org-tags-completion-function (string predicate &optional flag)
  12817. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12818. (confirm (lambda (x) (stringp (car x)))))
  12819. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12820. (setq s1 (match-string 1 string)
  12821. s2 (match-string 2 string))
  12822. (setq s1 "" s2 string))
  12823. (cond
  12824. ((eq flag nil)
  12825. ;; try completion
  12826. (setq rtn (try-completion s2 ctable confirm))
  12827. (if (stringp rtn)
  12828. (setq rtn
  12829. (concat s1 s2 (substring rtn (length s2))
  12830. (if (and org-add-colon-after-tag-completion
  12831. (assoc rtn ctable))
  12832. ":" ""))))
  12833. rtn)
  12834. ((eq flag t)
  12835. ;; all-completions
  12836. (all-completions s2 ctable confirm)
  12837. )
  12838. ((eq flag 'lambda)
  12839. ;; exact match?
  12840. (assoc s2 ctable)))
  12841. ))
  12842. (defun org-fast-tag-insert (kwd tags face &optional end)
  12843. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12844. (insert (format "%-12s" (concat kwd ":"))
  12845. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12846. (or end "")))
  12847. (defun org-fast-tag-show-exit (flag)
  12848. (save-excursion
  12849. (org-goto-line 3)
  12850. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12851. (replace-match ""))
  12852. (when flag
  12853. (end-of-line 1)
  12854. (org-move-to-column (- (window-width) 19) t)
  12855. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12856. (defun org-set-current-tags-overlay (current prefix)
  12857. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12858. (if (featurep 'xemacs)
  12859. (org-overlay-display org-tags-overlay (concat prefix s)
  12860. 'secondary-selection)
  12861. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12862. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12863. (defvar org-last-tag-selection-key nil)
  12864. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12865. "Fast tag selection with single keys.
  12866. CURRENT is the current list of tags in the headline, INHERITED is the
  12867. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12868. possibly with grouping information. TODO-TABLE is a similar table with
  12869. TODO keywords, should these have keys assigned to them.
  12870. If the keys are nil, a-z are automatically assigned.
  12871. Returns the new tags string, or nil to not change the current settings."
  12872. (let* ((fulltable (append table todo-table))
  12873. (maxlen (apply 'max (mapcar
  12874. (lambda (x)
  12875. (if (stringp (car x)) (string-width (car x)) 0))
  12876. fulltable)))
  12877. (buf (current-buffer))
  12878. (expert (eq org-fast-tag-selection-single-key 'expert))
  12879. (buffer-tags nil)
  12880. (fwidth (+ maxlen 3 1 3))
  12881. (ncol (/ (- (window-width) 4) fwidth))
  12882. (i-face 'org-done)
  12883. (c-face 'org-todo)
  12884. tg cnt e c char c1 c2 ntable tbl rtn
  12885. ov-start ov-end ov-prefix
  12886. (exit-after-next org-fast-tag-selection-single-key)
  12887. (done-keywords org-done-keywords)
  12888. groups ingroup)
  12889. (save-excursion
  12890. (beginning-of-line 1)
  12891. (if (looking-at
  12892. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12893. (setq ov-start (match-beginning 1)
  12894. ov-end (match-end 1)
  12895. ov-prefix "")
  12896. (setq ov-start (1- (point-at-eol))
  12897. ov-end (1+ ov-start))
  12898. (skip-chars-forward "^\n\r")
  12899. (setq ov-prefix
  12900. (concat
  12901. (buffer-substring (1- (point)) (point))
  12902. (if (> (current-column) org-tags-column)
  12903. " "
  12904. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12905. (move-overlay org-tags-overlay ov-start ov-end)
  12906. (save-window-excursion
  12907. (if expert
  12908. (set-buffer (get-buffer-create " *Org tags*"))
  12909. (delete-other-windows)
  12910. (split-window-vertically)
  12911. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12912. (erase-buffer)
  12913. (org-set-local 'org-done-keywords done-keywords)
  12914. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12915. (org-fast-tag-insert "Current" current c-face "\n\n")
  12916. (org-fast-tag-show-exit exit-after-next)
  12917. (org-set-current-tags-overlay current ov-prefix)
  12918. (setq tbl fulltable char ?a cnt 0)
  12919. (while (setq e (pop tbl))
  12920. (cond
  12921. ((equal (car e) :startgroup)
  12922. (push '() groups) (setq ingroup t)
  12923. (when (not (= cnt 0))
  12924. (setq cnt 0)
  12925. (insert "\n"))
  12926. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12927. ((equal (car e) :endgroup)
  12928. (setq ingroup nil cnt 0)
  12929. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12930. ((equal e '(:newline))
  12931. (when (not (= cnt 0))
  12932. (setq cnt 0)
  12933. (insert "\n")
  12934. (setq e (car tbl))
  12935. (while (equal (car tbl) '(:newline))
  12936. (insert "\n")
  12937. (setq tbl (cdr tbl)))))
  12938. (t
  12939. (setq tg (copy-sequence (car e)) c2 nil)
  12940. (if (cdr e)
  12941. (setq c (cdr e))
  12942. ;; automatically assign a character.
  12943. (setq c1 (string-to-char
  12944. (downcase (substring
  12945. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12946. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12947. (while (or (rassoc char ntable) (rassoc char table))
  12948. (setq char (1+ char)))
  12949. (setq c2 c1))
  12950. (setq c (or c2 char)))
  12951. (if ingroup (push tg (car groups)))
  12952. (setq tg (org-add-props tg nil 'face
  12953. (cond
  12954. ((not (assoc tg table))
  12955. (org-get-todo-face tg))
  12956. ((member tg current) c-face)
  12957. ((member tg inherited) i-face))))
  12958. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12959. (insert "[" c "] " tg (make-string
  12960. (- fwidth 4 (length tg)) ?\ ))
  12961. (push (cons tg c) ntable)
  12962. (when (= (setq cnt (1+ cnt)) ncol)
  12963. (insert "\n")
  12964. (if ingroup (insert " "))
  12965. (setq cnt 0)))))
  12966. (setq ntable (nreverse ntable))
  12967. (insert "\n")
  12968. (goto-char (point-min))
  12969. (if (not expert) (org-fit-window-to-buffer))
  12970. (setq rtn
  12971. (catch 'exit
  12972. (while t
  12973. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12974. (if (not groups) "no " "")
  12975. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12976. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12977. (setq org-last-tag-selection-key c)
  12978. (cond
  12979. ((= c ?\r) (throw 'exit t))
  12980. ((= c ?!)
  12981. (setq groups (not groups))
  12982. (goto-char (point-min))
  12983. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12984. ((= c ?\C-c)
  12985. (if (not expert)
  12986. (org-fast-tag-show-exit
  12987. (setq exit-after-next (not exit-after-next)))
  12988. (setq expert nil)
  12989. (delete-other-windows)
  12990. (set-window-buffer (split-window-vertically) " *Org tags*")
  12991. (org-switch-to-buffer-other-window " *Org tags*")
  12992. (org-fit-window-to-buffer)))
  12993. ((or (= c ?\C-g)
  12994. (and (= c ?q) (not (rassoc c ntable))))
  12995. (org-detach-overlay org-tags-overlay)
  12996. (setq quit-flag t))
  12997. ((= c ?\ )
  12998. (setq current nil)
  12999. (if exit-after-next (setq exit-after-next 'now)))
  13000. ((= c ?\t)
  13001. (condition-case nil
  13002. (setq tg (org-icompleting-read
  13003. "Tag: "
  13004. (or buffer-tags
  13005. (with-current-buffer buf
  13006. (org-get-buffer-tags)))))
  13007. (quit (setq tg "")))
  13008. (when (string-match "\\S-" tg)
  13009. (add-to-list 'buffer-tags (list tg))
  13010. (if (member tg current)
  13011. (setq current (delete tg current))
  13012. (push tg current)))
  13013. (if exit-after-next (setq exit-after-next 'now)))
  13014. ((setq e (rassoc c todo-table) tg (car e))
  13015. (with-current-buffer buf
  13016. (save-excursion (org-todo tg)))
  13017. (if exit-after-next (setq exit-after-next 'now)))
  13018. ((setq e (rassoc c ntable) tg (car e))
  13019. (if (member tg current)
  13020. (setq current (delete tg current))
  13021. (loop for g in groups do
  13022. (if (member tg g)
  13023. (mapc (lambda (x)
  13024. (setq current (delete x current)))
  13025. g)))
  13026. (push tg current))
  13027. (if exit-after-next (setq exit-after-next 'now))))
  13028. ;; Create a sorted list
  13029. (setq current
  13030. (sort current
  13031. (lambda (a b)
  13032. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13033. (if (eq exit-after-next 'now) (throw 'exit t))
  13034. (goto-char (point-min))
  13035. (beginning-of-line 2)
  13036. (delete-region (point) (point-at-eol))
  13037. (org-fast-tag-insert "Current" current c-face)
  13038. (org-set-current-tags-overlay current ov-prefix)
  13039. (while (re-search-forward
  13040. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13041. (setq tg (match-string 1))
  13042. (add-text-properties
  13043. (match-beginning 1) (match-end 1)
  13044. (list 'face
  13045. (cond
  13046. ((member tg current) c-face)
  13047. ((member tg inherited) i-face)
  13048. (t (get-text-property (match-beginning 1) 'face))))))
  13049. (goto-char (point-min)))))
  13050. (org-detach-overlay org-tags-overlay)
  13051. (if rtn
  13052. (mapconcat 'identity current ":")
  13053. nil))))
  13054. (defun org-get-tags-string ()
  13055. "Get the TAGS string in the current headline."
  13056. (unless (org-at-heading-p t)
  13057. (error "Not on a heading"))
  13058. (save-excursion
  13059. (beginning-of-line 1)
  13060. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13061. (org-match-string-no-properties 1)
  13062. "")))
  13063. (defun org-get-tags ()
  13064. "Get the list of tags specified in the current headline."
  13065. (org-split-string (org-get-tags-string) ":"))
  13066. (defun org-get-buffer-tags ()
  13067. "Get a table of all tags used in the buffer, for completion."
  13068. (let (tags)
  13069. (save-excursion
  13070. (goto-char (point-min))
  13071. (while (re-search-forward
  13072. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13073. (when (equal (char-after (point-at-bol 0)) ?*)
  13074. (mapc (lambda (x) (add-to-list 'tags x))
  13075. (org-split-string (org-match-string-no-properties 1) ":")))))
  13076. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13077. (mapcar 'list tags)))
  13078. ;;;; The mapping API
  13079. (defun org-map-entries (func &optional match scope &rest skip)
  13080. "Call FUNC at each headline selected by MATCH in SCOPE.
  13081. FUNC is a function or a lisp form. The function will be called without
  13082. arguments, with the cursor positioned at the beginning of the headline.
  13083. The return values of all calls to the function will be collected and
  13084. returned as a list.
  13085. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13086. does not need to preserve point. After evaluation, the cursor will be
  13087. moved to the end of the line (presumably of the headline of the
  13088. processed entry) and search continues from there. Under some
  13089. circumstances, this may not produce the wanted results. For example,
  13090. if you have removed (e.g. archived) the current (sub)tree it could
  13091. mean that the next entry will be skipped entirely. In such cases, you
  13092. can specify the position from where search should continue by making
  13093. FUNC set the variable `org-map-continue-from' to the desired buffer
  13094. position.
  13095. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13096. Only headlines that are matched by this query will be considered during
  13097. the iteration. When MATCH is nil or t, all headlines will be
  13098. visited by the iteration.
  13099. SCOPE determines the scope of this command. It can be any of:
  13100. nil The current buffer, respecting the restriction if any
  13101. tree The subtree started with the entry at point
  13102. region The entries within the active region, if any
  13103. region-start-level
  13104. The entries within the active region, but only those at
  13105. the same level than the first one.
  13106. file The current buffer, without restriction
  13107. file-with-archives
  13108. The current buffer, and any archives associated with it
  13109. agenda All agenda files
  13110. agenda-with-archives
  13111. All agenda files with any archive files associated with them
  13112. \(file1 file2 ...)
  13113. If this is a list, all files in the list will be scanned
  13114. The remaining args are treated as settings for the skipping facilities of
  13115. the scanner. The following items can be given here:
  13116. archive skip trees with the archive tag.
  13117. comment skip trees with the COMMENT keyword
  13118. function or Emacs Lisp form:
  13119. will be used as value for `org-agenda-skip-function', so whenever
  13120. the function returns t, FUNC will not be called for that
  13121. entry and search will continue from the point where the
  13122. function leaves it.
  13123. If your function needs to retrieve the tags including inherited tags
  13124. at the *current* entry, you can use the value of the variable
  13125. `org-scanner-tags' which will be much faster than getting the value
  13126. with `org-get-tags-at'. If your function gets properties with
  13127. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13128. to t around the call to `org-entry-properties' to get the same speedup.
  13129. Note that if your function moves around to retrieve tags and properties at
  13130. a *different* entry, you cannot use these techniques."
  13131. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13132. (not (org-region-active-p)))
  13133. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13134. (org-agenda-skip-archived-trees (memq 'archive skip))
  13135. (org-agenda-skip-comment-trees (memq 'comment skip))
  13136. (org-agenda-skip-function
  13137. (car (org-delete-all '(comment archive) skip)))
  13138. (org-tags-match-list-sublevels t)
  13139. (start-level (eq scope 'region-start-level))
  13140. matcher file res
  13141. org-todo-keywords-for-agenda
  13142. org-done-keywords-for-agenda
  13143. org-todo-keyword-alist-for-agenda
  13144. org-drawers-for-agenda
  13145. org-tag-alist-for-agenda
  13146. todo-only)
  13147. (cond
  13148. ((eq match t) (setq matcher t))
  13149. ((eq match nil) (setq matcher t))
  13150. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13151. (save-excursion
  13152. (save-restriction
  13153. (cond ((eq scope 'tree)
  13154. (org-back-to-heading t)
  13155. (org-narrow-to-subtree)
  13156. (setq scope nil))
  13157. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13158. (org-region-active-p))
  13159. ;; If needed, set start-level to a string like "2"
  13160. (when start-level
  13161. (save-excursion
  13162. (goto-char (region-beginning))
  13163. (unless (org-at-heading-p) (outline-next-heading))
  13164. (setq start-level (org-current-level))))
  13165. (narrow-to-region (region-beginning)
  13166. (save-excursion
  13167. (goto-char (region-end))
  13168. (unless (and (bolp) (org-at-heading-p))
  13169. (outline-next-heading))
  13170. (point)))
  13171. (setq scope nil)))
  13172. (if (not scope)
  13173. (progn
  13174. (org-agenda-prepare-buffers
  13175. (list (buffer-file-name (current-buffer))))
  13176. (setq res (org-scan-tags func matcher todo-only start-level)))
  13177. ;; Get the right scope
  13178. (cond
  13179. ((and scope (listp scope) (symbolp (car scope)))
  13180. (setq scope (eval scope)))
  13181. ((eq scope 'agenda)
  13182. (setq scope (org-agenda-files t)))
  13183. ((eq scope 'agenda-with-archives)
  13184. (setq scope (org-agenda-files t))
  13185. (setq scope (org-add-archive-files scope)))
  13186. ((eq scope 'file)
  13187. (setq scope (list (buffer-file-name))))
  13188. ((eq scope 'file-with-archives)
  13189. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13190. (org-agenda-prepare-buffers scope)
  13191. (while (setq file (pop scope))
  13192. (with-current-buffer (org-find-base-buffer-visiting file)
  13193. (save-excursion
  13194. (save-restriction
  13195. (widen)
  13196. (goto-char (point-min))
  13197. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13198. res)))
  13199. ;;;; Properties
  13200. ;;; Setting and retrieving properties
  13201. (defconst org-special-properties
  13202. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13203. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13204. "The special properties valid in Org-mode.
  13205. These are properties that are not defined in the property drawer,
  13206. but in some other way.")
  13207. (defconst org-default-properties
  13208. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13209. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13210. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13211. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13212. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13213. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13214. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13215. "Some properties that are used by Org-mode for various purposes.
  13216. Being in this list makes sure that they are offered for completion.")
  13217. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13218. "Regular expression matching the first line of a property drawer.")
  13219. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13220. "Regular expression matching the last line of a property drawer.")
  13221. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13222. "Regular expression matching the first line of a property drawer.")
  13223. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13224. "Regular expression matching the first line of a property drawer.")
  13225. (defconst org-property-drawer-re
  13226. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13227. org-property-end-re "\\)\n?")
  13228. "Matches an entire property drawer.")
  13229. (defconst org-clock-drawer-re
  13230. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13231. org-property-end-re "\\)\n?")
  13232. "Matches an entire clock drawer.")
  13233. (defsubst org-re-property (property)
  13234. "Return a regexp matching a PROPERTY line.
  13235. Match group 1 will be set to the value."
  13236. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13237. (defsubst org-re-property-keyword (property)
  13238. "Return a regexp matching a PROPERTY line, possibly with no
  13239. value for the property."
  13240. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13241. (defun org-property-action ()
  13242. "Do an action on properties."
  13243. (interactive)
  13244. (let (c)
  13245. (org-at-property-p)
  13246. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13247. (setq c (read-char-exclusive))
  13248. (cond
  13249. ((equal c ?s)
  13250. (call-interactively 'org-set-property))
  13251. ((equal c ?d)
  13252. (call-interactively 'org-delete-property))
  13253. ((equal c ?D)
  13254. (call-interactively 'org-delete-property-globally))
  13255. ((equal c ?c)
  13256. (call-interactively 'org-compute-property-at-point))
  13257. (t (error "No such property action %c" c)))))
  13258. (defun org-inc-effort ()
  13259. "Increment the value of the effort property in the current entry."
  13260. (interactive)
  13261. (org-set-effort nil t))
  13262. (defun org-set-effort (&optional value increment)
  13263. "Set the effort property of the current entry.
  13264. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13265. 10th allowed value.
  13266. When INCREMENT is non-nil, set the property to the next allowed value."
  13267. (interactive "P")
  13268. (if (equal value 0) (setq value 10))
  13269. (let* ((completion-ignore-case t)
  13270. (prop org-effort-property)
  13271. (cur (org-entry-get nil prop))
  13272. (allowed (org-property-get-allowed-values nil prop 'table))
  13273. (existing (mapcar 'list (org-property-values prop)))
  13274. rpl
  13275. (val (cond
  13276. ((stringp value) value)
  13277. ((and allowed (integerp value))
  13278. (or (car (nth (1- value) allowed))
  13279. (car (org-last allowed))))
  13280. ((and allowed increment)
  13281. (or (caadr (member (list cur) allowed))
  13282. (error "Allowed effort values are not set")))
  13283. (allowed
  13284. (message "Select 1-9,0, [RET%s]: %s"
  13285. (if cur (concat "=" cur) "")
  13286. (mapconcat 'car allowed " "))
  13287. (setq rpl (read-char-exclusive))
  13288. (if (equal rpl ?\r)
  13289. cur
  13290. (setq rpl (- rpl ?0))
  13291. (if (equal rpl 0) (setq rpl 10))
  13292. (if (and (> rpl 0) (<= rpl (length allowed)))
  13293. (car (nth (1- rpl) allowed))
  13294. (org-completing-read "Effort: " allowed nil))))
  13295. (t
  13296. (let (org-completion-use-ido org-completion-use-iswitchb)
  13297. (org-completing-read
  13298. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13299. (concat "[" cur "]") "")
  13300. ": ")
  13301. existing nil nil "" nil cur))))))
  13302. (unless (equal (org-entry-get nil prop) val)
  13303. (org-entry-put nil prop val))
  13304. (save-excursion
  13305. (org-back-to-heading t)
  13306. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13307. (message "%s is now %s" prop val)))
  13308. (defun org-at-property-p ()
  13309. "Is cursor inside a property drawer?"
  13310. (save-excursion
  13311. (beginning-of-line 1)
  13312. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13313. (save-match-data ;; Used by calling procedures
  13314. (let ((p (point))
  13315. (range (unless (org-before-first-heading-p)
  13316. (org-get-property-block))))
  13317. (and range (<= (car range) p) (< p (cdr range))))))))
  13318. (defun org-get-property-block (&optional beg end force)
  13319. "Return the (beg . end) range of the body of the property drawer.
  13320. BEG and END are the beginning and end of the current subtree, or of
  13321. the part before the first headline. If they are not given, they will
  13322. be found. If the drawer does not exist and FORCE is non-nil, create
  13323. the drawer."
  13324. (catch 'exit
  13325. (save-excursion
  13326. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13327. (progn (org-back-to-heading t) (point))))
  13328. (end (or end (and (not (outline-next-heading)) (point-max))
  13329. (point))))
  13330. (goto-char beg)
  13331. (if (re-search-forward org-property-start-re end t)
  13332. (setq beg (1+ (match-end 0)))
  13333. (if force
  13334. (save-excursion
  13335. (org-insert-property-drawer)
  13336. (setq end (progn (outline-next-heading) (point))))
  13337. (throw 'exit nil))
  13338. (goto-char beg)
  13339. (if (re-search-forward org-property-start-re end t)
  13340. (setq beg (1+ (match-end 0)))))
  13341. (if (re-search-forward org-property-end-re end t)
  13342. (setq end (match-beginning 0))
  13343. (or force (throw 'exit nil))
  13344. (goto-char beg)
  13345. (setq end beg)
  13346. (org-indent-line)
  13347. (insert ":END:\n"))
  13348. (cons beg end)))))
  13349. (defun org-entry-properties (&optional pom which specific)
  13350. "Get all properties of the entry at point-or-marker POM.
  13351. This includes the TODO keyword, the tags, time strings for deadline,
  13352. scheduled, and clocking, and any additional properties defined in the
  13353. entry. The return value is an alist, keys may occur multiple times
  13354. if the property key was used several times.
  13355. POM may also be nil, in which case the current entry is used.
  13356. If WHICH is nil or `all', get all properties. If WHICH is
  13357. `special' or `standard', only get that subclass. If WHICH
  13358. is a string only get exactly this property. SPECIFIC can be a string, the
  13359. specific property we are interested in. Specifying it can speed
  13360. things up because then unnecessary parsing is avoided."
  13361. (setq which (or which 'all))
  13362. (org-with-point-at pom
  13363. (let ((clockstr (substring org-clock-string 0 -1))
  13364. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13365. (case-fold-search nil)
  13366. beg end range props sum-props key key1 value string clocksum clocksumt)
  13367. (save-excursion
  13368. (when (condition-case nil
  13369. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13370. (error nil))
  13371. (setq beg (point))
  13372. (setq sum-props (get-text-property (point) 'org-summaries))
  13373. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13374. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13375. (outline-next-heading)
  13376. (setq end (point))
  13377. (when (memq which '(all special))
  13378. ;; Get the special properties, like TODO and tags
  13379. (goto-char beg)
  13380. (when (and (or (not specific) (string= specific "TODO"))
  13381. (looking-at org-todo-line-regexp) (match-end 2))
  13382. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13383. (when (and (or (not specific) (string= specific "PRIORITY"))
  13384. (looking-at org-priority-regexp))
  13385. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13386. (when (or (not specific) (string= specific "FILE"))
  13387. (push (cons "FILE" buffer-file-name) props))
  13388. (when (and (or (not specific) (string= specific "TAGS"))
  13389. (setq value (org-get-tags-string))
  13390. (string-match "\\S-" value))
  13391. (push (cons "TAGS" value) props))
  13392. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13393. (setq value (org-get-tags-at)))
  13394. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13395. ":"))
  13396. props))
  13397. (when (or (not specific) (string= specific "BLOCKED"))
  13398. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13399. (when (or (not specific)
  13400. (member specific
  13401. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13402. "TIMESTAMP" "TIMESTAMP_IA")))
  13403. (catch 'match
  13404. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13405. (setq key (if (match-end 1)
  13406. (substring (org-match-string-no-properties 1)
  13407. 0 -1))
  13408. string (if (equal key clockstr)
  13409. (org-trim
  13410. (buffer-substring-no-properties
  13411. (match-beginning 3) (goto-char
  13412. (point-at-eol))))
  13413. (substring (org-match-string-no-properties 3)
  13414. 1 -1)))
  13415. ;; Get the correct property name from the key. This is
  13416. ;; necessary if the user has configured time keywords.
  13417. (setq key1 (concat key ":"))
  13418. (cond
  13419. ((not key)
  13420. (setq key
  13421. (if (= (char-after (match-beginning 3)) ?\[)
  13422. "TIMESTAMP_IA" "TIMESTAMP")))
  13423. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13424. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13425. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13426. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13427. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13428. (progn
  13429. (push (cons key string) props)
  13430. ;; no need to search further if match is found
  13431. (throw 'match t))
  13432. (when (or (equal key "CLOCK") (not (assoc key props)))
  13433. (push (cons key string) props)))))))
  13434. (when (memq which '(all standard))
  13435. ;; Get the standard properties, like :PROP: ...
  13436. (setq range (org-get-property-block beg end))
  13437. (when range
  13438. (goto-char (car range))
  13439. (while (re-search-forward
  13440. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13441. (cdr range) t)
  13442. (setq key (org-match-string-no-properties 1)
  13443. value (org-trim (or (org-match-string-no-properties 2) "")))
  13444. (unless (member key excluded)
  13445. (push (cons key (or value "")) props)))))
  13446. (if clocksum
  13447. (push (cons "CLOCKSUM"
  13448. (org-columns-number-to-string (/ (float clocksum) 60.)
  13449. 'add_times))
  13450. props))
  13451. (if clocksumt
  13452. (push (cons "CLOCKSUM_T"
  13453. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13454. 'add_times))
  13455. props))
  13456. (unless (assoc "CATEGORY" props)
  13457. (push (cons "CATEGORY" (org-get-category)) props))
  13458. (append sum-props (nreverse props)))))))
  13459. (defun org-entry-get (pom property &optional inherit literal-nil)
  13460. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13461. If INHERIT is non-nil and the entry does not have the property,
  13462. then also check higher levels of the hierarchy.
  13463. If INHERIT is the symbol `selective', use inheritance only if the setting
  13464. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13465. If the property is present but empty, the return value is the empty string.
  13466. If the property is not present at all, nil is returned.
  13467. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13468. do not interpret it as the list atom nil. This is used for inheritance
  13469. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13470. (org-with-point-at pom
  13471. (if (and inherit (if (eq inherit 'selective)
  13472. (org-property-inherit-p property)
  13473. t))
  13474. (org-entry-get-with-inheritance property literal-nil)
  13475. (if (member property org-special-properties)
  13476. ;; We need a special property. Use `org-entry-properties' to
  13477. ;; retrieve it, but specify the wanted property
  13478. (cdr (assoc property (org-entry-properties nil 'special property)))
  13479. (let ((range (org-get-property-block)))
  13480. (when (and range (not (eq (car range) (cdr range))))
  13481. (let* ((props (list (or (assoc property org-file-properties)
  13482. (assoc property org-global-properties)
  13483. (assoc property org-global-properties-fixed))))
  13484. (ap (lambda (key)
  13485. (when (re-search-forward
  13486. (org-re-property key) (cdr range) t)
  13487. (setq props
  13488. (org-update-property-plist
  13489. key
  13490. (if (match-end 1)
  13491. (org-match-string-no-properties 1) "")
  13492. props)))))
  13493. val)
  13494. (goto-char (car range))
  13495. (funcall ap property)
  13496. (goto-char (car range))
  13497. (while (funcall ap (concat property "+")))
  13498. (setq val (cdr (assoc property props)))
  13499. (when val (if literal-nil val (org-not-nil val))))))))))
  13500. (defun org-property-or-variable-value (var &optional inherit)
  13501. "Check if there is a property fixing the value of VAR.
  13502. If yes, return this value. If not, return the current value of the variable."
  13503. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13504. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13505. (read prop)
  13506. (symbol-value var))))
  13507. (defun org-entry-delete (pom property)
  13508. "Delete the property PROPERTY from entry at point-or-marker POM."
  13509. (org-with-point-at pom
  13510. (if (member property org-special-properties)
  13511. nil ; cannot delete these properties.
  13512. (let ((range (org-get-property-block)))
  13513. (if (and range
  13514. (goto-char (car range))
  13515. (re-search-forward
  13516. (org-re-property property)
  13517. (cdr range) t))
  13518. (progn
  13519. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13520. t)
  13521. nil)))))
  13522. ;; Multi-values properties are properties that contain multiple values
  13523. ;; These values are assumed to be single words, separated by whitespace.
  13524. (defun org-entry-add-to-multivalued-property (pom property value)
  13525. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13526. (let* ((old (org-entry-get pom property))
  13527. (values (and old (org-split-string old "[ \t]"))))
  13528. (setq value (org-entry-protect-space value))
  13529. (unless (member value values)
  13530. (setq values (cons value values))
  13531. (org-entry-put pom property
  13532. (mapconcat 'identity values " ")))))
  13533. (defun org-entry-remove-from-multivalued-property (pom property value)
  13534. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13535. (let* ((old (org-entry-get pom property))
  13536. (values (and old (org-split-string old "[ \t]"))))
  13537. (setq value (org-entry-protect-space value))
  13538. (when (member value values)
  13539. (setq values (delete value values))
  13540. (org-entry-put pom property
  13541. (mapconcat 'identity values " ")))))
  13542. (defun org-entry-member-in-multivalued-property (pom property value)
  13543. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13544. (let* ((old (org-entry-get pom property))
  13545. (values (and old (org-split-string old "[ \t]"))))
  13546. (setq value (org-entry-protect-space value))
  13547. (member value values)))
  13548. (defun org-entry-get-multivalued-property (pom property)
  13549. "Return a list of values in a multivalued property."
  13550. (let* ((value (org-entry-get pom property))
  13551. (values (and value (org-split-string value "[ \t]"))))
  13552. (mapcar 'org-entry-restore-space values)))
  13553. (defun org-entry-put-multivalued-property (pom property &rest values)
  13554. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13555. VALUES should be a list of strings. Spaces will be protected."
  13556. (org-entry-put pom property
  13557. (mapconcat 'org-entry-protect-space values " "))
  13558. (let* ((value (org-entry-get pom property))
  13559. (values (and value (org-split-string value "[ \t]"))))
  13560. (mapcar 'org-entry-restore-space values)))
  13561. (defun org-entry-protect-space (s)
  13562. "Protect spaces and newline in string S."
  13563. (while (string-match " " s)
  13564. (setq s (replace-match "%20" t t s)))
  13565. (while (string-match "\n" s)
  13566. (setq s (replace-match "%0A" t t s)))
  13567. s)
  13568. (defun org-entry-restore-space (s)
  13569. "Restore spaces and newline in string S."
  13570. (while (string-match "%20" s)
  13571. (setq s (replace-match " " t t s)))
  13572. (while (string-match "%0A" s)
  13573. (setq s (replace-match "\n" t t s)))
  13574. s)
  13575. (defvar org-entry-property-inherited-from (make-marker)
  13576. "Marker pointing to the entry from where a property was inherited.
  13577. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13578. location of the entry where the inheritance search matched. If there was
  13579. no match, the marker will point nowhere.
  13580. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13581. is set.")
  13582. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13583. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13584. The search will stop at the first ancestor which has the property defined.
  13585. If the value found is \"nil\", return nil to show that the property
  13586. should be considered as undefined (this is the meaning of nil here).
  13587. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13588. (move-marker org-entry-property-inherited-from nil)
  13589. (let (tmp)
  13590. (save-excursion
  13591. (save-restriction
  13592. (widen)
  13593. (catch 'ex
  13594. (while t
  13595. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13596. (or (ignore-errors (org-back-to-heading t))
  13597. (goto-char (point-min)))
  13598. (move-marker org-entry-property-inherited-from (point))
  13599. (throw 'ex tmp))
  13600. (or (ignore-errors (org-up-heading-safe))
  13601. (throw 'ex nil))))))
  13602. (setq tmp (or tmp
  13603. (cdr (assoc property org-file-properties))
  13604. (cdr (assoc property org-global-properties))
  13605. (cdr (assoc property org-global-properties-fixed))))
  13606. (if literal-nil tmp (org-not-nil tmp))))
  13607. (defvar org-property-changed-functions nil
  13608. "Hook called when the value of a property has changed.
  13609. Each hook function should accept two arguments, the name of the property
  13610. and the new value.")
  13611. (defun org-entry-put (pom property value)
  13612. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13613. (org-with-point-at pom
  13614. (org-back-to-heading t)
  13615. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13616. range)
  13617. (cond
  13618. ((equal property "TODO")
  13619. (when (and (stringp value) (string-match "\\S-" value)
  13620. (not (member value org-todo-keywords-1)))
  13621. (error "\"%s\" is not a valid TODO state" value))
  13622. (if (or (not value)
  13623. (not (string-match "\\S-" value)))
  13624. (setq value 'none))
  13625. (org-todo value)
  13626. (org-set-tags nil 'align))
  13627. ((equal property "PRIORITY")
  13628. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13629. (string-to-char value) ?\ ))
  13630. (org-set-tags nil 'align))
  13631. ((equal property "CLOCKSUM")
  13632. (if (not (re-search-forward
  13633. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13634. (error "Cannot find a clock log")
  13635. (goto-char (- (match-end 1) 2))
  13636. (cond
  13637. ((eq value 'earlier) (org-timestamp-down))
  13638. ((eq value 'later) (org-timestamp-up)))
  13639. (org-clock-sum-current-item)))
  13640. ((equal property "SCHEDULED")
  13641. (if (re-search-forward org-scheduled-time-regexp end t)
  13642. (cond
  13643. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13644. ((eq value 'later) (org-timestamp-change 1 'day))
  13645. (t (call-interactively 'org-schedule)))
  13646. (call-interactively 'org-schedule)))
  13647. ((equal property "DEADLINE")
  13648. (if (re-search-forward org-deadline-time-regexp end t)
  13649. (cond
  13650. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13651. ((eq value 'later) (org-timestamp-change 1 'day))
  13652. (t (call-interactively 'org-deadline)))
  13653. (call-interactively 'org-deadline)))
  13654. ((member property org-special-properties)
  13655. (error "The %s property can not yet be set with `org-entry-put'"
  13656. property))
  13657. (t ; a non-special property
  13658. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13659. (setq range (org-get-property-block beg end 'force))
  13660. (goto-char (car range))
  13661. (if (re-search-forward
  13662. (org-re-property-keyword property) (cdr range) t)
  13663. (progn
  13664. (delete-region (match-beginning 0) (match-end 0))
  13665. (goto-char (match-beginning 0)))
  13666. (goto-char (cdr range))
  13667. (insert "\n")
  13668. (backward-char 1)
  13669. (org-indent-line))
  13670. (insert ":" property ":")
  13671. (and value (insert " " value))
  13672. (org-indent-line)))))
  13673. (run-hook-with-args 'org-property-changed-functions property value)))
  13674. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13675. "Get all property keys in the current buffer.
  13676. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13677. like tags and TODO state.
  13678. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13679. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13680. and others.
  13681. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13682. formats in the current buffer."
  13683. (let (rtn range cfmt s p)
  13684. (save-excursion
  13685. (save-restriction
  13686. (widen)
  13687. (goto-char (point-min))
  13688. (while (re-search-forward org-property-start-re nil t)
  13689. (setq range (org-get-property-block))
  13690. (goto-char (car range))
  13691. (while (re-search-forward
  13692. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13693. (cdr range) t)
  13694. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13695. (outline-next-heading))))
  13696. (when include-specials
  13697. (setq rtn (append org-special-properties rtn)))
  13698. (when include-defaults
  13699. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13700. (add-to-list 'rtn org-effort-property))
  13701. (when include-columns
  13702. (save-excursion
  13703. (save-restriction
  13704. (widen)
  13705. (goto-char (point-min))
  13706. (while (re-search-forward
  13707. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13708. nil t)
  13709. (setq cfmt (match-string 2) s 0)
  13710. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13711. cfmt s)
  13712. (setq s (match-end 0)
  13713. p (match-string 1 cfmt))
  13714. (unless (or (equal p "ITEM")
  13715. (member p org-special-properties))
  13716. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13717. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13718. (defun org-property-values (key)
  13719. "Return a list of all values of property KEY in the current buffer."
  13720. (save-excursion
  13721. (save-restriction
  13722. (widen)
  13723. (goto-char (point-min))
  13724. (let ((re (org-re-property key))
  13725. values)
  13726. (while (re-search-forward re nil t)
  13727. (add-to-list 'values (org-trim (match-string 1))))
  13728. (delete "" values)))))
  13729. (defun org-insert-property-drawer ()
  13730. "Insert a property drawer into the current entry."
  13731. (org-back-to-heading t)
  13732. (looking-at org-outline-regexp)
  13733. (let ((indent (if org-adapt-indentation
  13734. (- (match-end 0) (match-beginning 0))
  13735. 0))
  13736. (beg (point))
  13737. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13738. end hiddenp)
  13739. (outline-next-heading)
  13740. (setq end (point))
  13741. (goto-char beg)
  13742. (while (re-search-forward re end t))
  13743. (setq hiddenp (outline-invisible-p))
  13744. (end-of-line 1)
  13745. (and (equal (char-after) ?\n) (forward-char 1))
  13746. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13747. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13748. ;; just skip this line
  13749. (beginning-of-line 2)
  13750. ;; Drawer start, find the end
  13751. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13752. (beginning-of-line 1)))
  13753. (org-skip-over-state-notes)
  13754. (skip-chars-backward " \t\n\r")
  13755. (if (eq (char-before) ?*) (forward-char 1))
  13756. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13757. (beginning-of-line 0)
  13758. (org-indent-to-column indent)
  13759. (beginning-of-line 2)
  13760. (org-indent-to-column indent)
  13761. (beginning-of-line 0)
  13762. (if hiddenp
  13763. (save-excursion
  13764. (org-back-to-heading t)
  13765. (hide-entry))
  13766. (org-flag-drawer t))))
  13767. (defun org-insert-drawer (&optional arg drawer)
  13768. "Insert a drawer at point.
  13769. Optional argument DRAWER, when non-nil, is a string representing
  13770. drawer's name. Otherwise, the user is prompted for a name.
  13771. If a region is active, insert the drawer around that region
  13772. instead.
  13773. Point is left between drawer's boundaries."
  13774. (interactive "P")
  13775. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13776. "LOGBOOK"))
  13777. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13778. ;; internally by Org. They are meant to be inserted
  13779. ;; automatically.
  13780. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13781. ;; Remove system drawers from list. Note: For some reason,
  13782. ;; `org-completing-read' ignores the predicate while
  13783. ;; `completing-read' handles it fine.
  13784. (drawer (if arg "PROPERTIES"
  13785. (or drawer
  13786. (completing-read
  13787. "Drawer: " org-drawers
  13788. (lambda (d) (not (member d system-drawers))))))))
  13789. (cond
  13790. ;; With C-u, fall back on `org-insert-property-drawer'
  13791. (arg (org-insert-property-drawer))
  13792. ;; With an active region, insert a drawer at point.
  13793. ((not (org-region-active-p))
  13794. (progn
  13795. (unless (bolp) (insert "\n"))
  13796. (insert (format ":%s:\n\n:END:\n" drawer))
  13797. (forward-line -2)))
  13798. ;; Otherwise, insert the drawer at point
  13799. (t
  13800. (let ((rbeg (region-beginning))
  13801. (rend (copy-marker (region-end))))
  13802. (unwind-protect
  13803. (progn
  13804. (goto-char rbeg)
  13805. (beginning-of-line)
  13806. (when (save-excursion
  13807. (re-search-forward org-outline-regexp-bol rend t))
  13808. (error "Drawers cannot contain headlines"))
  13809. ;; Position point at the beginning of the first
  13810. ;; non-blank line in region. Insert drawer's opening
  13811. ;; there, then indent it.
  13812. (org-skip-whitespace)
  13813. (beginning-of-line)
  13814. (insert ":" drawer ":\n")
  13815. (forward-line -1)
  13816. (indent-for-tab-command)
  13817. ;; Move point to the beginning of the first blank line
  13818. ;; after the last non-blank line in region. Insert
  13819. ;; drawer's closing, then indent it.
  13820. (goto-char rend)
  13821. (skip-chars-backward " \r\t\n")
  13822. (insert "\n:END:")
  13823. (deactivate-mark t)
  13824. (indent-for-tab-command)
  13825. (unless (eolp) (insert "\n")))
  13826. ;; Clear marker, whatever the outcome of insertion is.
  13827. (set-marker rend nil)))))))
  13828. (defvar org-property-set-functions-alist nil
  13829. "Property set function alist.
  13830. Each entry should have the following format:
  13831. (PROPERTY . READ-FUNCTION)
  13832. The read function will be called with the same argument as
  13833. `org-completing-read'.")
  13834. (defun org-set-property-function (property)
  13835. "Get the function that should be used to set PROPERTY.
  13836. This is computed according to `org-property-set-functions-alist'."
  13837. (or (cdr (assoc property org-property-set-functions-alist))
  13838. 'org-completing-read))
  13839. (defun org-read-property-value (property)
  13840. "Read PROPERTY value from user."
  13841. (let* ((completion-ignore-case t)
  13842. (allowed (org-property-get-allowed-values nil property 'table))
  13843. (cur (org-entry-get nil property))
  13844. (prompt (concat property " value"
  13845. (if (and cur (string-match "\\S-" cur))
  13846. (concat " [" cur "]") "") ": "))
  13847. (set-function (org-set-property-function property))
  13848. (val (if allowed
  13849. (funcall set-function prompt allowed nil
  13850. (not (get-text-property 0 'org-unrestricted
  13851. (caar allowed))))
  13852. (let (org-completion-use-ido org-completion-use-iswitchb)
  13853. (funcall set-function prompt
  13854. (mapcar 'list (org-property-values property))
  13855. nil nil "" nil cur)))))
  13856. (if (equal val "")
  13857. cur
  13858. val)))
  13859. (defvar org-last-set-property nil)
  13860. (defvar org-last-set-property-value nil)
  13861. (defun org-read-property-name ()
  13862. "Read a property name."
  13863. (let* ((completion-ignore-case t)
  13864. (keys (org-buffer-property-keys nil t t))
  13865. (default-prop (or (save-excursion
  13866. (save-match-data
  13867. (beginning-of-line)
  13868. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13869. (null (string= (match-string 1) "END"))
  13870. (match-string 1))))
  13871. org-last-set-property))
  13872. (property (org-icompleting-read
  13873. (concat "Property"
  13874. (if default-prop (concat " [" default-prop "]") "")
  13875. ": ")
  13876. (mapcar 'list keys)
  13877. nil nil nil nil
  13878. default-prop)))
  13879. (if (member property keys)
  13880. property
  13881. (or (cdr (assoc (downcase property)
  13882. (mapcar (lambda (x) (cons (downcase x) x))
  13883. keys)))
  13884. property))))
  13885. (defun org-set-property-and-value (use-last)
  13886. "Allow to set [PROPERTY]: [value] direction from prompt.
  13887. When use-default, don't even ask, just use the last
  13888. \"[PROPERTY]: [value]\" string from the history."
  13889. (interactive "P")
  13890. (let* ((completion-ignore-case t)
  13891. (pv (or (and use-last org-last-set-property-value)
  13892. (org-completing-read
  13893. "Enter a \"[Property]: [value]\" pair: "
  13894. nil nil nil nil nil
  13895. org-last-set-property-value)))
  13896. prop val)
  13897. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13898. (setq prop (match-string 1 pv)
  13899. val (match-string 2 pv))
  13900. (org-set-property prop val))))
  13901. (defun org-set-property (property value)
  13902. "In the current entry, set PROPERTY to VALUE.
  13903. When called interactively, this will prompt for a property name, offering
  13904. completion on existing and default properties. And then it will prompt
  13905. for a value, offering completion either on allowed values (via an inherited
  13906. xxx_ALL property) or on existing values in other instances of this property
  13907. in the current file."
  13908. (interactive (list nil nil))
  13909. (let* ((property (or property (org-read-property-name)))
  13910. (value (or value (org-read-property-value property)))
  13911. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13912. (setq org-last-set-property property)
  13913. (setq org-last-set-property-value (concat property ": " value))
  13914. ;; Possibly postprocess the inserted value:
  13915. (when fn (setq value (funcall fn value)))
  13916. (unless (equal (org-entry-get nil property) value)
  13917. (org-entry-put nil property value))))
  13918. (defun org-delete-property (property)
  13919. "In the current entry, delete PROPERTY."
  13920. (interactive
  13921. (let* ((completion-ignore-case t)
  13922. (prop (org-icompleting-read "Property: "
  13923. (org-entry-properties nil 'standard))))
  13924. (list prop)))
  13925. (message "Property %s %s" property
  13926. (if (org-entry-delete nil property)
  13927. "deleted"
  13928. "was not present in the entry")))
  13929. (defun org-delete-property-globally (property)
  13930. "Remove PROPERTY globally, from all entries."
  13931. (interactive
  13932. (let* ((completion-ignore-case t)
  13933. (prop (org-icompleting-read
  13934. "Globally remove property: "
  13935. (mapcar 'list (org-buffer-property-keys)))))
  13936. (list prop)))
  13937. (save-excursion
  13938. (save-restriction
  13939. (widen)
  13940. (goto-char (point-min))
  13941. (let ((cnt 0))
  13942. (while (re-search-forward
  13943. (org-re-property property)
  13944. nil t)
  13945. (setq cnt (1+ cnt))
  13946. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13947. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13948. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13949. (defun org-compute-property-at-point ()
  13950. "Compute the property at point.
  13951. This looks for an enclosing column format, extracts the operator and
  13952. then applies it to the property in the column format's scope."
  13953. (interactive)
  13954. (unless (org-at-property-p)
  13955. (error "Not at a property"))
  13956. (let ((prop (org-match-string-no-properties 2)))
  13957. (org-columns-get-format-and-top-level)
  13958. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13959. (error "No operator defined for property %s" prop))
  13960. (org-columns-compute prop)))
  13961. (defvar org-property-allowed-value-functions nil
  13962. "Hook for functions supplying allowed values for a specific property.
  13963. The functions must take a single argument, the name of the property, and
  13964. return a flat list of allowed values. If \":ETC\" is one of
  13965. the values, this means that these values are intended as defaults for
  13966. completion, but that other values should be allowed too.
  13967. The functions must return nil if they are not responsible for this
  13968. property.")
  13969. (defun org-property-get-allowed-values (pom property &optional table)
  13970. "Get allowed values for the property PROPERTY.
  13971. When TABLE is non-nil, return an alist that can directly be used for
  13972. completion."
  13973. (let (vals)
  13974. (cond
  13975. ((equal property "TODO")
  13976. (setq vals (org-with-point-at pom
  13977. (append org-todo-keywords-1 '("")))))
  13978. ((equal property "PRIORITY")
  13979. (let ((n org-lowest-priority))
  13980. (while (>= n org-highest-priority)
  13981. (push (char-to-string n) vals)
  13982. (setq n (1- n)))))
  13983. ((member property org-special-properties))
  13984. ((setq vals (run-hook-with-args-until-success
  13985. 'org-property-allowed-value-functions property)))
  13986. (t
  13987. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13988. (when (and vals (string-match "\\S-" vals))
  13989. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13990. (setq vals (mapcar (lambda (x)
  13991. (cond ((stringp x) x)
  13992. ((numberp x) (number-to-string x))
  13993. ((symbolp x) (symbol-name x))
  13994. (t "???")))
  13995. vals)))))
  13996. (when (member ":ETC" vals)
  13997. (setq vals (remove ":ETC" vals))
  13998. (org-add-props (car vals) '(org-unrestricted t)))
  13999. (if table (mapcar 'list vals) vals)))
  14000. (defun org-property-previous-allowed-value (&optional previous)
  14001. "Switch to the next allowed value for this property."
  14002. (interactive)
  14003. (org-property-next-allowed-value t))
  14004. (defun org-property-next-allowed-value (&optional previous)
  14005. "Switch to the next allowed value for this property."
  14006. (interactive)
  14007. (unless (org-at-property-p)
  14008. (error "Not at a property"))
  14009. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14010. (key (match-string 2))
  14011. (value (match-string 3))
  14012. (allowed (or (org-property-get-allowed-values (point) key)
  14013. (and (member value '("[ ]" "[-]" "[X]"))
  14014. '("[ ]" "[X]"))))
  14015. nval)
  14016. (unless allowed
  14017. (error "Allowed values for this property have not been defined"))
  14018. (if previous (setq allowed (reverse allowed)))
  14019. (if (member value allowed)
  14020. (setq nval (car (cdr (member value allowed)))))
  14021. (setq nval (or nval (car allowed)))
  14022. (if (equal nval value)
  14023. (error "Only one allowed value for this property"))
  14024. (org-at-property-p)
  14025. (replace-match (concat " :" key ": " nval) t t)
  14026. (org-indent-line)
  14027. (beginning-of-line 1)
  14028. (skip-chars-forward " \t")
  14029. (when (equal prop org-effort-property)
  14030. (save-excursion
  14031. (org-back-to-heading t)
  14032. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14033. (run-hook-with-args 'org-property-changed-functions key nval)))
  14034. (defun org-find-olp (path &optional this-buffer)
  14035. "Return a marker pointing to the entry at outline path OLP.
  14036. If anything goes wrong, throw an error.
  14037. You can wrap this call to catch the error like this:
  14038. (condition-case msg
  14039. (org-mobile-locate-entry (match-string 4))
  14040. (error (nth 1 msg)))
  14041. The return value will then be either a string with the error message,
  14042. or a marker if everything is OK.
  14043. If THIS-BUFFER is set, the outline path does not contain a file,
  14044. only headings."
  14045. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14046. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14047. (level 1)
  14048. (lmin 1)
  14049. (lmax 1)
  14050. limit re end found pos heading cnt flevel)
  14051. (unless buffer (error "File not found :%s" file))
  14052. (with-current-buffer buffer
  14053. (save-excursion
  14054. (save-restriction
  14055. (widen)
  14056. (setq limit (point-max))
  14057. (goto-char (point-min))
  14058. (while (setq heading (pop path))
  14059. (setq re (format org-complex-heading-regexp-format
  14060. (regexp-quote heading)))
  14061. (setq cnt 0 pos (point))
  14062. (while (re-search-forward re end t)
  14063. (setq level (- (match-end 1) (match-beginning 1)))
  14064. (if (and (>= level lmin) (<= level lmax))
  14065. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14066. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14067. lmax heading))
  14068. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14069. lmax heading))
  14070. (goto-char found)
  14071. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14072. (setq end (save-excursion (org-end-of-subtree t t))))
  14073. (when (org-at-heading-p)
  14074. (point-marker)))))))
  14075. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14076. "Find node HEADING in BUFFER.
  14077. Return a marker to the heading if it was found, or nil if not.
  14078. If POS-ONLY is set, return just the position instead of a marker.
  14079. The heading text must match exact, but it may have a TODO keyword,
  14080. a priority cookie and tags in the standard locations."
  14081. (with-current-buffer (or buffer (current-buffer))
  14082. (save-excursion
  14083. (save-restriction
  14084. (widen)
  14085. (goto-char (point-min))
  14086. (let (case-fold-search)
  14087. (if (re-search-forward
  14088. (format org-complex-heading-regexp-format
  14089. (regexp-quote heading)) nil t)
  14090. (if pos-only
  14091. (match-beginning 0)
  14092. (move-marker (make-marker) (match-beginning 0)))))))))
  14093. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14094. "Find Org node headline HEADING in all .org files in directory DIR.
  14095. When the target headline is found, return a marker to this location."
  14096. (let ((files (directory-files (or dir default-directory)
  14097. nil "\\`[^.#].*\\.org\\'"))
  14098. file visiting m buffer)
  14099. (catch 'found
  14100. (while (setq file (pop files))
  14101. (message "trying %s" file)
  14102. (setq visiting (org-find-base-buffer-visiting file))
  14103. (setq buffer (or visiting (find-file-noselect file)))
  14104. (setq m (org-find-exact-headline-in-buffer
  14105. heading buffer))
  14106. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14107. (and m (throw 'found m))))))
  14108. (defun org-find-entry-with-id (ident)
  14109. "Locate the entry that contains the ID property with exact value IDENT.
  14110. IDENT can be a string, a symbol or a number, this function will search for
  14111. the string representation of it.
  14112. Return the position where this entry starts, or nil if there is no such entry."
  14113. (interactive "sID: ")
  14114. (let ((id (cond
  14115. ((stringp ident) ident)
  14116. ((symbol-name ident) (symbol-name ident))
  14117. ((numberp ident) (number-to-string ident))
  14118. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14119. (case-fold-search nil))
  14120. (save-excursion
  14121. (save-restriction
  14122. (widen)
  14123. (goto-char (point-min))
  14124. (when (re-search-forward
  14125. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14126. nil t)
  14127. (org-back-to-heading t)
  14128. (point))))))
  14129. ;;;; Timestamps
  14130. (defvar org-last-changed-timestamp nil)
  14131. (defvar org-last-inserted-timestamp nil
  14132. "The last time stamp inserted with `org-insert-time-stamp'.")
  14133. (defvar org-time-was-given) ; dynamically scoped parameter
  14134. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14135. (defvar org-ts-what) ; dynamically scoped parameter
  14136. (defun org-time-stamp (arg &optional inactive)
  14137. "Prompt for a date/time and insert a time stamp.
  14138. If the user specifies a time like HH:MM or if this command is
  14139. called with at least one prefix argument, the time stamp contains
  14140. the date and the time. Otherwise, only the date is be included.
  14141. All parts of a date not specified by the user is filled in from
  14142. the current date/time. So if you just press return without
  14143. typing anything, the time stamp will represent the current
  14144. date/time.
  14145. If there is already a timestamp at the cursor, it will be
  14146. modified.
  14147. With two universal prefix arguments, insert an active timestamp
  14148. with the current time without prompting the user."
  14149. (interactive "P")
  14150. (let* ((ts nil)
  14151. (default-time
  14152. ;; Default time is either today, or, when entering a range,
  14153. ;; the range start.
  14154. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14155. (save-excursion
  14156. (re-search-backward
  14157. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14158. (- (point) 20) t)))
  14159. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14160. (current-time)))
  14161. (default-input (and ts (org-get-compact-tod ts)))
  14162. (repeater (save-excursion
  14163. (save-match-data
  14164. (beginning-of-line)
  14165. (when (re-search-forward
  14166. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14167. (save-excursion (progn (end-of-line) (point))) t)
  14168. (match-string 0)))))
  14169. org-time-was-given org-end-time-was-given time)
  14170. (cond
  14171. ((and (org-at-timestamp-p t)
  14172. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14173. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14174. (insert "--")
  14175. (setq time (let ((this-command this-command))
  14176. (org-read-date arg 'totime nil nil
  14177. default-time default-input inactive)))
  14178. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14179. ((org-at-timestamp-p t)
  14180. (setq time (let ((this-command this-command))
  14181. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14182. (when (org-at-timestamp-p t) ; just to get the match data
  14183. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14184. (replace-match "")
  14185. (setq org-last-changed-timestamp
  14186. (org-insert-time-stamp
  14187. time (or org-time-was-given arg)
  14188. inactive nil nil (list org-end-time-was-given)))
  14189. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14190. (setq org-last-changed-timestamp
  14191. (concat (substring org-last-inserted-timestamp 0 -1)
  14192. " " repeater ">"))))
  14193. (message "Timestamp updated"))
  14194. ((equal arg '(16))
  14195. (org-insert-time-stamp (current-time) t))
  14196. (t
  14197. (setq time (let ((this-command this-command))
  14198. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14199. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14200. nil nil (list org-end-time-was-given))))))
  14201. ;; FIXME: can we use this for something else, like computing time differences?
  14202. (defun org-get-compact-tod (s)
  14203. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14204. (let* ((t1 (match-string 1 s))
  14205. (h1 (string-to-number (match-string 2 s)))
  14206. (m1 (string-to-number (match-string 3 s)))
  14207. (t2 (and (match-end 4) (match-string 5 s)))
  14208. (h2 (and t2 (string-to-number (match-string 6 s))))
  14209. (m2 (and t2 (string-to-number (match-string 7 s))))
  14210. dh dm)
  14211. (if (not t2)
  14212. t1
  14213. (setq dh (- h2 h1) dm (- m2 m1))
  14214. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14215. (concat t1 "+" (number-to-string dh)
  14216. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14217. (defun org-time-stamp-inactive (&optional arg)
  14218. "Insert an inactive time stamp.
  14219. An inactive time stamp is enclosed in square brackets instead of angle
  14220. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14221. does not link to the calendar and cannot be changed with the S-cursor keys.
  14222. So these are more for recording a certain time/date."
  14223. (interactive "P")
  14224. (org-time-stamp arg 'inactive))
  14225. (defvar org-date-ovl (make-overlay 1 1))
  14226. (overlay-put org-date-ovl 'face 'org-date-selected)
  14227. (org-detach-overlay org-date-ovl)
  14228. (defvar org-ans1) ; dynamically scoped parameter
  14229. (defvar org-ans2) ; dynamically scoped parameter
  14230. (defvar org-plain-time-of-day-regexp) ; defined below
  14231. (defvar org-overriding-default-time nil) ; dynamically scoped
  14232. (defvar org-read-date-overlay nil)
  14233. (defvar org-dcst nil) ; dynamically scoped
  14234. (defvar org-read-date-history nil)
  14235. (defvar org-read-date-final-answer nil)
  14236. (defvar org-read-date-analyze-futurep nil)
  14237. (defvar org-read-date-analyze-forced-year nil)
  14238. (defvar org-read-date-inactive)
  14239. (defvar org-read-date-minibuffer-local-map
  14240. (let ((map (make-sparse-keymap)))
  14241. (set-keymap-parent map minibuffer-local-map)
  14242. (org-defkey map [(meta shift left)]
  14243. (lambda () (interactive)
  14244. (org-eval-in-calendar '(calendar-backward-month 1))))
  14245. (org-defkey map [(meta shift right)]
  14246. (lambda () (interactive)
  14247. (org-eval-in-calendar '(calendar-forward-month 1))))
  14248. (org-defkey map [(meta shift up)]
  14249. (lambda () (interactive)
  14250. (org-eval-in-calendar '(calendar-backward-year 1))))
  14251. (org-defkey map [(meta shift down)]
  14252. (lambda () (interactive)
  14253. (org-eval-in-calendar '(calendar-forward-year 1))))
  14254. (org-defkey map [?\e (shift left)]
  14255. (lambda () (interactive)
  14256. (org-eval-in-calendar '(calendar-backward-month 1))))
  14257. (org-defkey map [?\e (shift right)]
  14258. (lambda () (interactive)
  14259. (org-eval-in-calendar '(calendar-forward-month 1))))
  14260. (org-defkey map [?\e (shift up)]
  14261. (lambda () (interactive)
  14262. (org-eval-in-calendar '(calendar-backward-year 1))))
  14263. (org-defkey map [?\e (shift down)]
  14264. (lambda () (interactive)
  14265. (org-eval-in-calendar '(calendar-forward-year 1))))
  14266. (org-defkey map [(shift up)]
  14267. (lambda () (interactive)
  14268. (org-eval-in-calendar '(calendar-backward-week 1))))
  14269. (org-defkey map [(shift down)]
  14270. (lambda () (interactive)
  14271. (org-eval-in-calendar '(calendar-forward-week 1))))
  14272. (org-defkey map [(shift left)]
  14273. (lambda () (interactive)
  14274. (org-eval-in-calendar '(calendar-backward-day 1))))
  14275. (org-defkey map [(shift right)]
  14276. (lambda () (interactive)
  14277. (org-eval-in-calendar '(calendar-forward-day 1))))
  14278. (org-defkey map "?"
  14279. (lambda () (interactive)
  14280. (org-eval-in-calendar '(diary-view-entries))
  14281. (message "")))
  14282. (org-defkey map ">"
  14283. (lambda () (interactive)
  14284. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14285. (org-defkey map "<"
  14286. (lambda () (interactive)
  14287. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14288. (org-defkey map "\C-v"
  14289. (lambda () (interactive)
  14290. (org-eval-in-calendar
  14291. '(calendar-scroll-left-three-months 1))))
  14292. (org-defkey map "\M-v"
  14293. (lambda () (interactive)
  14294. (org-eval-in-calendar
  14295. '(calendar-scroll-right-three-months 1))))
  14296. map)
  14297. "Keymap for minibuffer commands when using `org-read-date'.")
  14298. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14299. default-time default-input inactive)
  14300. "Read a date, possibly a time, and make things smooth for the user.
  14301. The prompt will suggest to enter an ISO date, but you can also enter anything
  14302. which will at least partially be understood by `parse-time-string'.
  14303. Unrecognized parts of the date will default to the current day, month, year,
  14304. hour and minute. If this command is called to replace a timestamp at point,
  14305. or to enter the second timestamp of a range, the default time is taken
  14306. from the existing stamp. Furthermore, the command prefers the future,
  14307. so if you are giving a date where the year is not given, and the day-month
  14308. combination is already past in the current year, it will assume you
  14309. mean next year. For details, see the manual. A few examples:
  14310. 3-2-5 --> 2003-02-05
  14311. feb 15 --> currentyear-02-15
  14312. 2/15 --> currentyear-02-15
  14313. sep 12 9 --> 2009-09-12
  14314. 12:45 --> today 12:45
  14315. 22 sept 0:34 --> currentyear-09-22 0:34
  14316. 12 --> currentyear-currentmonth-12
  14317. Fri --> nearest Friday (today or later)
  14318. etc.
  14319. Furthermore you can specify a relative date by giving, as the *first* thing
  14320. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14321. change in days weeks, months, years.
  14322. With a single plus or minus, the date is relative to today. With a double
  14323. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14324. +4d --> four days from today
  14325. +4 --> same as above
  14326. +2w --> two weeks from today
  14327. ++5 --> five days from default date
  14328. The function understands only English month and weekday abbreviations.
  14329. While prompting, a calendar is popped up - you can also select the
  14330. date with the mouse (button 1). The calendar shows a period of three
  14331. months. To scroll it to other months, use the keys `>' and `<'.
  14332. If you don't like the calendar, turn it off with
  14333. \(setq org-read-date-popup-calendar nil)
  14334. With optional argument TO-TIME, the date will immediately be converted
  14335. to an internal time.
  14336. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14337. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14338. still enter a time, and this function will inform the calling routine
  14339. about this change. The calling routine may then choose to change the
  14340. format used to insert the time stamp into the buffer to include the time.
  14341. With optional argument FROM-STRING, read from this string instead from
  14342. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14343. the time/date that is used for everything that is not specified by the
  14344. user."
  14345. (require 'parse-time)
  14346. (let* ((org-time-stamp-rounding-minutes
  14347. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14348. (org-dcst org-display-custom-times)
  14349. (ct (org-current-time))
  14350. (org-def (or org-overriding-default-time default-time ct))
  14351. (org-defdecode (decode-time org-def))
  14352. (dummy (progn
  14353. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14354. (setcar (nthcdr 2 org-defdecode) -1)
  14355. (setcar (nthcdr 1 org-defdecode) 59)
  14356. (setq org-def (apply 'encode-time org-defdecode)
  14357. org-defdecode (decode-time org-def)))))
  14358. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14359. (calendar-frame-setup nil)
  14360. (calendar-setup nil)
  14361. (calendar-move-hook nil)
  14362. (calendar-view-diary-initially-flag nil)
  14363. (calendar-view-holidays-initially-flag nil)
  14364. (timestr (format-time-string
  14365. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14366. (prompt (concat (if prompt (concat prompt " ") "")
  14367. (format "Date+time [%s]: " timestr)))
  14368. ans (org-ans0 "") org-ans1 org-ans2 final)
  14369. (cond
  14370. (from-string (setq ans from-string))
  14371. (org-read-date-popup-calendar
  14372. (save-excursion
  14373. (save-window-excursion
  14374. (calendar)
  14375. (org-eval-in-calendar '(setq cursor-type nil) t)
  14376. (unwind-protect
  14377. (progn
  14378. (calendar-forward-day (- (time-to-days org-def)
  14379. (calendar-absolute-from-gregorian
  14380. (calendar-current-date))))
  14381. (org-eval-in-calendar nil t)
  14382. (let* ((old-map (current-local-map))
  14383. (map (copy-keymap calendar-mode-map))
  14384. (minibuffer-local-map
  14385. (copy-keymap org-read-date-minibuffer-local-map)))
  14386. (org-defkey map (kbd "RET") 'org-calendar-select)
  14387. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14388. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14389. (unwind-protect
  14390. (progn
  14391. (use-local-map map)
  14392. (setq org-read-date-inactive inactive)
  14393. (add-hook 'post-command-hook 'org-read-date-display)
  14394. (setq org-ans0 (read-string prompt default-input
  14395. 'org-read-date-history nil))
  14396. ;; org-ans0: from prompt
  14397. ;; org-ans1: from mouse click
  14398. ;; org-ans2: from calendar motion
  14399. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14400. (remove-hook 'post-command-hook 'org-read-date-display)
  14401. (use-local-map old-map)
  14402. (when org-read-date-overlay
  14403. (delete-overlay org-read-date-overlay)
  14404. (setq org-read-date-overlay nil)))))
  14405. (bury-buffer "*Calendar*")))))
  14406. (t ; Naked prompt only
  14407. (unwind-protect
  14408. (setq ans (read-string prompt default-input
  14409. 'org-read-date-history timestr))
  14410. (when org-read-date-overlay
  14411. (delete-overlay org-read-date-overlay)
  14412. (setq org-read-date-overlay nil)))))
  14413. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14414. (when org-read-date-analyze-forced-year
  14415. (message "Year was forced into %s"
  14416. (if org-read-date-force-compatible-dates
  14417. "compatible range (1970-2037)"
  14418. "range representable on this machine"))
  14419. (ding))
  14420. ;; One round trip to get rid of 34th of August and stuff like that....
  14421. (setq final (decode-time (apply 'encode-time final)))
  14422. (setq org-read-date-final-answer ans)
  14423. (if to-time
  14424. (apply 'encode-time final)
  14425. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14426. (format "%04d-%02d-%02d %02d:%02d"
  14427. (nth 5 final) (nth 4 final) (nth 3 final)
  14428. (nth 2 final) (nth 1 final))
  14429. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14430. (defvar org-def)
  14431. (defvar org-defdecode)
  14432. (defvar org-with-time)
  14433. (defun org-read-date-display ()
  14434. "Display the current date prompt interpretation in the minibuffer."
  14435. (when org-read-date-display-live
  14436. (when org-read-date-overlay
  14437. (delete-overlay org-read-date-overlay))
  14438. (when (minibufferp (current-buffer))
  14439. (save-excursion
  14440. (end-of-line 1)
  14441. (while (not (equal (buffer-substring
  14442. (max (point-min) (- (point) 4)) (point))
  14443. " "))
  14444. (insert " ")))
  14445. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14446. " " (or org-ans1 org-ans2)))
  14447. (org-end-time-was-given nil)
  14448. (f (org-read-date-analyze ans org-def org-defdecode))
  14449. (fmts (if org-dcst
  14450. org-time-stamp-custom-formats
  14451. org-time-stamp-formats))
  14452. (fmt (if (or org-with-time
  14453. (and (boundp 'org-time-was-given) org-time-was-given))
  14454. (cdr fmts)
  14455. (car fmts)))
  14456. (txt (format-time-string fmt (apply 'encode-time f)))
  14457. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14458. (txt (concat "=> " txt)))
  14459. (when (and org-end-time-was-given
  14460. (string-match org-plain-time-of-day-regexp txt))
  14461. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14462. org-end-time-was-given
  14463. (substring txt (match-end 0)))))
  14464. (when org-read-date-analyze-futurep
  14465. (setq txt (concat txt " (=>F)")))
  14466. (setq org-read-date-overlay
  14467. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14468. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14469. (defun org-read-date-analyze (ans org-def org-defdecode)
  14470. "Analyze the combined answer of the date prompt."
  14471. ;; FIXME: cleanup and comment
  14472. (let ((nowdecode (decode-time (current-time)))
  14473. delta deltan deltaw deltadef year month day
  14474. hour minute second wday pm h2 m2 tl wday1
  14475. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14476. (setq org-read-date-analyze-futurep nil
  14477. org-read-date-analyze-forced-year nil)
  14478. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14479. (setq ans "+0"))
  14480. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14481. (setq ans (replace-match "" t t ans)
  14482. deltan (car delta)
  14483. deltaw (nth 1 delta)
  14484. deltadef (nth 2 delta)))
  14485. ;; Check if there is an iso week date in there. If yes, store the
  14486. ;; info and postpone interpreting it until the rest of the parsing
  14487. ;; is done.
  14488. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14489. (setq iso-year (if (match-end 1)
  14490. (org-small-year-to-year
  14491. (string-to-number (match-string 1 ans))))
  14492. iso-weekday (if (match-end 3)
  14493. (string-to-number (match-string 3 ans)))
  14494. iso-week (string-to-number (match-string 2 ans)))
  14495. (setq ans (replace-match "" t t ans)))
  14496. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14497. (when (string-match
  14498. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14499. (setq year (if (match-end 2)
  14500. (string-to-number (match-string 2 ans))
  14501. (progn (setq kill-year t)
  14502. (string-to-number (format-time-string "%Y"))))
  14503. month (string-to-number (match-string 3 ans))
  14504. day (string-to-number (match-string 4 ans)))
  14505. (if (< year 100) (setq year (+ 2000 year)))
  14506. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14507. t nil ans)))
  14508. ;; Help matching dotted european dates
  14509. (when (string-match
  14510. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14511. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14512. (setq kill-year t)
  14513. (string-to-number (format-time-string "%Y")))
  14514. day (string-to-number (match-string 1 ans))
  14515. month (string-to-number (match-string 2 ans))
  14516. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14517. t nil ans)))
  14518. ;; Help matching american dates, like 5/30 or 5/30/7
  14519. (when (string-match
  14520. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14521. (setq year (if (match-end 4)
  14522. (string-to-number (match-string 4 ans))
  14523. (progn (setq kill-year t)
  14524. (string-to-number (format-time-string "%Y"))))
  14525. month (string-to-number (match-string 1 ans))
  14526. day (string-to-number (match-string 2 ans)))
  14527. (if (< year 100) (setq year (+ 2000 year)))
  14528. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14529. t nil ans)))
  14530. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14531. ;; If there is a time with am/pm, and *no* time without it, we convert
  14532. ;; so that matching will be successful.
  14533. (loop for i from 1 to 2 do ; twice, for end time as well
  14534. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14535. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14536. (setq hour (string-to-number (match-string 1 ans))
  14537. minute (if (match-end 3)
  14538. (string-to-number (match-string 3 ans))
  14539. 0)
  14540. pm (equal ?p
  14541. (string-to-char (downcase (match-string 4 ans)))))
  14542. (if (and (= hour 12) (not pm))
  14543. (setq hour 0)
  14544. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14545. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14546. t t ans))))
  14547. ;; Check if a time range is given as a duration
  14548. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14549. (setq hour (string-to-number (match-string 1 ans))
  14550. h2 (+ hour (string-to-number (match-string 3 ans)))
  14551. minute (string-to-number (match-string 2 ans))
  14552. m2 (+ minute (if (match-end 5) (string-to-number
  14553. (match-string 5 ans))0)))
  14554. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14555. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14556. t t ans)))
  14557. ;; Check if there is a time range
  14558. (when (boundp 'org-end-time-was-given)
  14559. (setq org-time-was-given nil)
  14560. (when (and (string-match org-plain-time-of-day-regexp ans)
  14561. (match-end 8))
  14562. (setq org-end-time-was-given (match-string 8 ans))
  14563. (setq ans (concat (substring ans 0 (match-beginning 7))
  14564. (substring ans (match-end 7))))))
  14565. (setq tl (parse-time-string ans)
  14566. day (or (nth 3 tl) (nth 3 org-defdecode))
  14567. month (or (nth 4 tl)
  14568. (if (and org-read-date-prefer-future
  14569. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14570. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14571. (nth 4 org-defdecode)))
  14572. year (or (and (not kill-year) (nth 5 tl))
  14573. (if (and org-read-date-prefer-future
  14574. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14575. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14576. (nth 5 org-defdecode)))
  14577. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14578. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14579. second (or (nth 0 tl) 0)
  14580. wday (nth 6 tl))
  14581. (when (and (eq org-read-date-prefer-future 'time)
  14582. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14583. (equal day (nth 3 nowdecode))
  14584. (equal month (nth 4 nowdecode))
  14585. (equal year (nth 5 nowdecode))
  14586. (nth 2 tl)
  14587. (or (< (nth 2 tl) (nth 2 nowdecode))
  14588. (and (= (nth 2 tl) (nth 2 nowdecode))
  14589. (nth 1 tl)
  14590. (< (nth 1 tl) (nth 1 nowdecode)))))
  14591. (setq day (1+ day)
  14592. futurep t))
  14593. ;; Special date definitions below
  14594. (cond
  14595. (iso-week
  14596. ;; There was an iso week
  14597. (require 'cal-iso)
  14598. (setq futurep nil)
  14599. (setq year (or iso-year year)
  14600. day (or iso-weekday wday 1)
  14601. wday nil ; to make sure that the trigger below does not match
  14602. iso-date (calendar-gregorian-from-absolute
  14603. (calendar-absolute-from-iso
  14604. (list iso-week day year))))
  14605. ; FIXME: Should we also push ISO weeks into the future?
  14606. ; (when (and org-read-date-prefer-future
  14607. ; (not iso-year)
  14608. ; (< (calendar-absolute-from-gregorian iso-date)
  14609. ; (time-to-days (current-time))))
  14610. ; (setq year (1+ year)
  14611. ; iso-date (calendar-gregorian-from-absolute
  14612. ; (calendar-absolute-from-iso
  14613. ; (list iso-week day year)))))
  14614. (setq month (car iso-date)
  14615. year (nth 2 iso-date)
  14616. day (nth 1 iso-date)))
  14617. (deltan
  14618. (setq futurep nil)
  14619. (unless deltadef
  14620. (let ((now (decode-time (current-time))))
  14621. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14622. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14623. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14624. ((equal deltaw "m") (setq month (+ month deltan)))
  14625. ((equal deltaw "y") (setq year (+ year deltan)))))
  14626. ((and wday (not (nth 3 tl)))
  14627. ;; Weekday was given, but no day, so pick that day in the week
  14628. ;; on or after the derived date.
  14629. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14630. (unless (equal wday wday1)
  14631. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14632. (if (and (boundp 'org-time-was-given)
  14633. (nth 2 tl))
  14634. (setq org-time-was-given t))
  14635. (if (< year 100) (setq year (+ 2000 year)))
  14636. ;; Check of the date is representable
  14637. (if org-read-date-force-compatible-dates
  14638. (progn
  14639. (if (< year 1970)
  14640. (setq year 1970 org-read-date-analyze-forced-year t))
  14641. (if (> year 2037)
  14642. (setq year 2037 org-read-date-analyze-forced-year t)))
  14643. (condition-case nil
  14644. (ignore (encode-time second minute hour day month year))
  14645. (error
  14646. (setq year (nth 5 org-defdecode))
  14647. (setq org-read-date-analyze-forced-year t))))
  14648. (setq org-read-date-analyze-futurep futurep)
  14649. (list second minute hour day month year)))
  14650. (defvar parse-time-weekdays)
  14651. (defun org-read-date-get-relative (s today default)
  14652. "Check string S for special relative date string.
  14653. TODAY and DEFAULT are internal times, for today and for a default.
  14654. Return shift list (N what def-flag)
  14655. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14656. N is the number of WHATs to shift.
  14657. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14658. the DEFAULT date rather than TODAY."
  14659. (require 'parse-time)
  14660. (when (and
  14661. (string-match
  14662. (concat
  14663. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14664. "\\([0-9]+\\)?"
  14665. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14666. "\\([ \t]\\|$\\)") s)
  14667. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14668. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14669. (string-to-char (substring (match-string 1 s) -1))
  14670. ?+))
  14671. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14672. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14673. (what (if (match-end 3) (match-string 3 s) "d"))
  14674. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14675. (date (if rel default today))
  14676. (wday (nth 6 (decode-time date)))
  14677. delta)
  14678. (if wday1
  14679. (progn
  14680. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14681. (if (= dir ?-) (setq delta (- delta 7)))
  14682. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14683. (list delta "d" rel))
  14684. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14685. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14686. "Turn a user-specified date into the internal representation.
  14687. The internal representation needed by the calendar is (month day year).
  14688. This is a wrapper to handle the brain-dead convention in calendar that
  14689. user function argument order change dependent on argument order."
  14690. (if (boundp 'calendar-date-style)
  14691. (cond
  14692. ((eq calendar-date-style 'american)
  14693. (list arg1 arg2 arg3))
  14694. ((eq calendar-date-style 'european)
  14695. (list arg2 arg1 arg3))
  14696. ((eq calendar-date-style 'iso)
  14697. (list arg2 arg3 arg1)))
  14698. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14699. (if (org-bound-and-true-p european-calendar-style)
  14700. (list arg2 arg1 arg3)
  14701. (list arg1 arg2 arg3)))))
  14702. (defun org-eval-in-calendar (form &optional keepdate)
  14703. "Eval FORM in the calendar window and return to current window.
  14704. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14705. otherwise stick to the current value of `org-ans2'."
  14706. (let ((sf (selected-frame))
  14707. (sw (selected-window)))
  14708. (select-window (get-buffer-window "*Calendar*" t))
  14709. (eval form)
  14710. (when (and (not keepdate) (calendar-cursor-to-date))
  14711. (let* ((date (calendar-cursor-to-date))
  14712. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14713. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14714. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14715. (select-window sw)
  14716. (org-select-frame-set-input-focus sf)))
  14717. (defun org-calendar-select ()
  14718. "Return to `org-read-date' with the date currently selected.
  14719. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14720. (interactive)
  14721. (when (calendar-cursor-to-date)
  14722. (let* ((date (calendar-cursor-to-date))
  14723. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14724. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14725. (if (active-minibuffer-window) (exit-minibuffer))))
  14726. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14727. "Insert a date stamp for the date given by the internal TIME.
  14728. WITH-HM means use the stamp format that includes the time of the day.
  14729. INACTIVE means use square brackets instead of angular ones, so that the
  14730. stamp will not contribute to the agenda.
  14731. PRE and POST are optional strings to be inserted before and after the
  14732. stamp.
  14733. The command returns the inserted time stamp."
  14734. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14735. stamp)
  14736. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14737. (insert-before-markers (or pre ""))
  14738. (when (listp extra)
  14739. (setq extra (car extra))
  14740. (if (and (stringp extra)
  14741. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14742. (setq extra (format "-%02d:%02d"
  14743. (string-to-number (match-string 1 extra))
  14744. (string-to-number (match-string 2 extra))))
  14745. (setq extra nil)))
  14746. (when extra
  14747. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14748. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14749. (insert-before-markers (or post ""))
  14750. (setq org-last-inserted-timestamp stamp)))
  14751. (defun org-toggle-time-stamp-overlays ()
  14752. "Toggle the use of custom time stamp formats."
  14753. (interactive)
  14754. (setq org-display-custom-times (not org-display-custom-times))
  14755. (unless org-display-custom-times
  14756. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14757. (while (setq p (next-single-property-change p 'display))
  14758. (if (and (get-text-property p 'display)
  14759. (eq (get-text-property p 'face) 'org-date))
  14760. (remove-text-properties
  14761. p (setq p (next-single-property-change p 'display))
  14762. '(display t))))
  14763. (set-buffer-modified-p bmp)))
  14764. (if (featurep 'xemacs)
  14765. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14766. (org-restart-font-lock)
  14767. (setq org-table-may-need-update t)
  14768. (if org-display-custom-times
  14769. (message "Time stamps are overlaid with custom format")
  14770. (message "Time stamp overlays removed")))
  14771. (defun org-display-custom-time (beg end)
  14772. "Overlay modified time stamp format over timestamp between BEG and END."
  14773. (let* ((ts (buffer-substring beg end))
  14774. t1 w1 with-hm tf time str w2 (off 0))
  14775. (save-match-data
  14776. (setq t1 (org-parse-time-string ts t))
  14777. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14778. (setq off (- (match-end 0) (match-beginning 0)))))
  14779. (setq end (- end off))
  14780. (setq w1 (- end beg)
  14781. with-hm (and (nth 1 t1) (nth 2 t1))
  14782. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14783. time (org-fix-decoded-time t1)
  14784. str (org-add-props
  14785. (format-time-string
  14786. (substring tf 1 -1) (apply 'encode-time time))
  14787. nil 'mouse-face 'highlight)
  14788. w2 (length str))
  14789. (if (not (= w2 w1))
  14790. (add-text-properties (1+ beg) (+ 2 beg)
  14791. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14792. (if (featurep 'xemacs)
  14793. (progn
  14794. (put-text-property beg end 'invisible t)
  14795. (put-text-property beg end 'end-glyph (make-glyph str)))
  14796. (put-text-property beg end 'display str))))
  14797. (defun org-translate-time (string)
  14798. "Translate all timestamps in STRING to custom format.
  14799. But do this only if the variable `org-display-custom-times' is set."
  14800. (when org-display-custom-times
  14801. (save-match-data
  14802. (let* ((start 0)
  14803. (re org-ts-regexp-both)
  14804. t1 with-hm inactive tf time str beg end)
  14805. (while (setq start (string-match re string start))
  14806. (setq beg (match-beginning 0)
  14807. end (match-end 0)
  14808. t1 (save-match-data
  14809. (org-parse-time-string (substring string beg end) t))
  14810. with-hm (and (nth 1 t1) (nth 2 t1))
  14811. inactive (equal (substring string beg (1+ beg)) "[")
  14812. tf (funcall (if with-hm 'cdr 'car)
  14813. org-time-stamp-custom-formats)
  14814. time (org-fix-decoded-time t1)
  14815. str (format-time-string
  14816. (concat
  14817. (if inactive "[" "<") (substring tf 1 -1)
  14818. (if inactive "]" ">"))
  14819. (apply 'encode-time time))
  14820. string (replace-match str t t string)
  14821. start (+ start (length str)))))))
  14822. string)
  14823. (defun org-fix-decoded-time (time)
  14824. "Set 0 instead of nil for the first 6 elements of time.
  14825. Don't touch the rest."
  14826. (let ((n 0))
  14827. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14828. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14829. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14830. "Difference between TIMESTAMP-STRING and now in days.
  14831. If SECONDS is non-nil, return the difference in seconds."
  14832. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14833. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14834. (funcall fdiff (current-time)))))
  14835. (defun org-deadline-close (timestamp-string &optional ndays)
  14836. "Is the time in TIMESTAMP-STRING close to the current date?"
  14837. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14838. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14839. (not (org-entry-is-done-p))))
  14840. (defun org-get-wdays (ts)
  14841. "Get the deadline lead time appropriate for timestring TS."
  14842. (cond
  14843. ((<= org-deadline-warning-days 0)
  14844. ;; 0 or negative, enforce this value no matter what
  14845. (- org-deadline-warning-days))
  14846. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14847. ;; lead time is specified.
  14848. (floor (* (string-to-number (match-string 1 ts))
  14849. (cdr (assoc (match-string 2 ts)
  14850. '(("d" . 1) ("w" . 7)
  14851. ("m" . 30.4) ("y" . 365.25)))))))
  14852. ;; go for the default.
  14853. (t org-deadline-warning-days)))
  14854. (defun org-calendar-select-mouse (ev)
  14855. "Return to `org-read-date' with the date currently selected.
  14856. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14857. (interactive "e")
  14858. (mouse-set-point ev)
  14859. (when (calendar-cursor-to-date)
  14860. (let* ((date (calendar-cursor-to-date))
  14861. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14862. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14863. (if (active-minibuffer-window) (exit-minibuffer))))
  14864. (defun org-check-deadlines (ndays)
  14865. "Check if there are any deadlines due or past due.
  14866. A deadline is considered due if it happens within `org-deadline-warning-days'
  14867. days from today's date. If the deadline appears in an entry marked DONE,
  14868. it is not shown. The prefix arg NDAYS can be used to test that many
  14869. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14870. (interactive "P")
  14871. (let* ((org-warn-days
  14872. (cond
  14873. ((equal ndays '(4)) 100000)
  14874. (ndays (prefix-numeric-value ndays))
  14875. (t (abs org-deadline-warning-days))))
  14876. (case-fold-search nil)
  14877. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14878. (callback
  14879. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14880. (message "%d deadlines past-due or due within %d days"
  14881. (org-occur regexp nil callback)
  14882. org-warn-days)))
  14883. (defsubst org-re-timestamp (type)
  14884. "Return a regexp for timestamp TYPE.
  14885. Allowed values for TYPE are:
  14886. all: all timestamps
  14887. active: only active timestamps (<...>)
  14888. inactive: only inactive timestamps ([...])
  14889. scheduled: only scheduled timestamps
  14890. deadline: only deadline timestamps
  14891. When TYPE is nil, fall back on returning a regexp that matches
  14892. both scheduled and deadline timestamps."
  14893. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14894. ((eq type 'active) org-ts-regexp)
  14895. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14896. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14897. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14898. ((eq type 'scheduled-or-deadline)
  14899. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14900. (defun org-check-before-date (date)
  14901. "Check if there are deadlines or scheduled entries before DATE."
  14902. (interactive (list (org-read-date)))
  14903. (let ((case-fold-search nil)
  14904. (regexp (org-re-timestamp org-ts-type))
  14905. (callback
  14906. (lambda () (time-less-p
  14907. (org-time-string-to-time (match-string 1))
  14908. (org-time-string-to-time date)))))
  14909. (message "%d entries before %s"
  14910. (org-occur regexp nil callback) date)))
  14911. (defun org-check-after-date (date)
  14912. "Check if there are deadlines or scheduled entries after DATE."
  14913. (interactive (list (org-read-date)))
  14914. (let ((case-fold-search nil)
  14915. (regexp (org-re-timestamp org-ts-type))
  14916. (callback
  14917. (lambda () (not
  14918. (time-less-p
  14919. (org-time-string-to-time (match-string 1))
  14920. (org-time-string-to-time date))))))
  14921. (message "%d entries after %s"
  14922. (org-occur regexp nil callback) date)))
  14923. (defun org-check-dates-range (start-date end-date)
  14924. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14925. (interactive (list (org-read-date nil nil nil "Range starts")
  14926. (org-read-date nil nil nil "Range end")))
  14927. (let ((case-fold-search nil)
  14928. (regexp (org-re-timestamp org-ts-type))
  14929. (callback
  14930. (lambda ()
  14931. (let ((match (match-string 1)))
  14932. (and
  14933. (not (time-less-p
  14934. (org-time-string-to-time match)
  14935. (org-time-string-to-time start-date)))
  14936. (time-less-p
  14937. (org-time-string-to-time match)
  14938. (org-time-string-to-time end-date)))))))
  14939. (message "%d entries between %s and %s"
  14940. (org-occur regexp nil callback) start-date end-date)))
  14941. (defun org-evaluate-time-range (&optional to-buffer)
  14942. "Evaluate a time range by computing the difference between start and end.
  14943. Normally the result is just printed in the echo area, but with prefix arg
  14944. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14945. If the time range is actually in a table, the result is inserted into the
  14946. next column.
  14947. For time difference computation, a year is assumed to be exactly 365
  14948. days in order to avoid rounding problems."
  14949. (interactive "P")
  14950. (or
  14951. (org-clock-update-time-maybe)
  14952. (save-excursion
  14953. (unless (org-at-date-range-p t)
  14954. (goto-char (point-at-bol))
  14955. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14956. (if (not (org-at-date-range-p t))
  14957. (error "Not at a time-stamp range, and none found in current line")))
  14958. (let* ((ts1 (match-string 1))
  14959. (ts2 (match-string 2))
  14960. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14961. (match-end (match-end 0))
  14962. (time1 (org-time-string-to-time ts1))
  14963. (time2 (org-time-string-to-time ts2))
  14964. (t1 (org-float-time time1))
  14965. (t2 (org-float-time time2))
  14966. (diff (abs (- t2 t1)))
  14967. (negative (< (- t2 t1) 0))
  14968. ;; (ys (floor (* 365 24 60 60)))
  14969. (ds (* 24 60 60))
  14970. (hs (* 60 60))
  14971. (fy "%dy %dd %02d:%02d")
  14972. (fy1 "%dy %dd")
  14973. (fd "%dd %02d:%02d")
  14974. (fd1 "%dd")
  14975. (fh "%02d:%02d")
  14976. y d h m align)
  14977. (if havetime
  14978. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14979. y 0
  14980. d (floor (/ diff ds)) diff (mod diff ds)
  14981. h (floor (/ diff hs)) diff (mod diff hs)
  14982. m (floor (/ diff 60)))
  14983. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14984. y 0
  14985. d (floor (+ (/ diff ds) 0.5))
  14986. h 0 m 0))
  14987. (if (not to-buffer)
  14988. (message "%s" (org-make-tdiff-string y d h m))
  14989. (if (org-at-table-p)
  14990. (progn
  14991. (goto-char match-end)
  14992. (setq align t)
  14993. (and (looking-at " *|") (goto-char (match-end 0))))
  14994. (goto-char match-end))
  14995. (if (looking-at
  14996. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14997. (replace-match ""))
  14998. (if negative (insert " -"))
  14999. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15000. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15001. (insert " " (format fh h m))))
  15002. (if align (org-table-align))
  15003. (message "Time difference inserted")))))
  15004. (defun org-make-tdiff-string (y d h m)
  15005. (let ((fmt "")
  15006. (l nil))
  15007. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15008. l (push y l)))
  15009. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15010. l (push d l)))
  15011. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15012. l (push h l)))
  15013. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15014. l (push m l)))
  15015. (apply 'format fmt (nreverse l))))
  15016. (defun org-time-string-to-time (s &optional buffer pos)
  15017. "Convert a timestamp string into internal time."
  15018. (condition-case errdata
  15019. (apply 'encode-time (org-parse-time-string s))
  15020. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15021. s (if (not (and buffer pos))
  15022. ""
  15023. (format " at %d in buffer `%s'" pos buffer))
  15024. (cdr errdata)))))
  15025. (defun org-time-string-to-seconds (s)
  15026. "Convert a timestamp string to a number of seconds."
  15027. (org-float-time (org-time-string-to-time s)))
  15028. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15029. "Convert a time stamp to an absolute day number.
  15030. If there is a specifier for a cyclic time stamp, get the closest date to
  15031. DAYNR.
  15032. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15033. The variable date is bound by the calendar when this is called."
  15034. (cond
  15035. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15036. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15037. daynr
  15038. (+ daynr 1000)))
  15039. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15040. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15041. (time-to-days (current-time))) (match-string 0 s)
  15042. prefer show-all))
  15043. (t (time-to-days
  15044. (condition-case errdata
  15045. (apply 'encode-time (org-parse-time-string s))
  15046. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15047. s (if (not (and buffer pos))
  15048. ""
  15049. (format " at %d in buffer `%s'" pos buffer))
  15050. (cdr errdata))))))))
  15051. (defun org-days-to-iso-week (days)
  15052. "Return the iso week number."
  15053. (require 'cal-iso)
  15054. (car (calendar-iso-from-absolute days)))
  15055. (defun org-small-year-to-year (year)
  15056. "Convert 2-digit years into 4-digit years.
  15057. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15058. The year 2000 cannot be abbreviated. Any year larger than 99
  15059. is returned unchanged."
  15060. (if (< year 38)
  15061. (setq year (+ 2000 year))
  15062. (if (< year 100)
  15063. (setq year (+ 1900 year))))
  15064. year)
  15065. (defun org-time-from-absolute (d)
  15066. "Return the time corresponding to date D.
  15067. D may be an absolute day number, or a calendar-type list (month day year)."
  15068. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15069. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15070. (defun org-calendar-holiday ()
  15071. "List of holidays, for Diary display in Org-mode."
  15072. (require 'holidays)
  15073. (let ((hl (funcall
  15074. (if (fboundp 'calendar-check-holidays)
  15075. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15076. (if hl (mapconcat 'identity hl "; "))))
  15077. (defun org-diary-sexp-entry (sexp entry date)
  15078. "Process a SEXP diary ENTRY for DATE."
  15079. (require 'diary-lib)
  15080. (let ((result (if calendar-debug-sexp
  15081. (let ((stack-trace-on-error t))
  15082. (eval (car (read-from-string sexp))))
  15083. (condition-case nil
  15084. (eval (car (read-from-string sexp)))
  15085. (error
  15086. (beep)
  15087. (message "Bad sexp at line %d in %s: %s"
  15088. (org-current-line)
  15089. (buffer-file-name) sexp)
  15090. (sleep-for 2))))))
  15091. (cond ((stringp result) (split-string result "; "))
  15092. ((and (consp result)
  15093. (not (consp (cdr result)))
  15094. (stringp (cdr result))) (cdr result))
  15095. ((and (consp result)
  15096. (stringp (car result))) result)
  15097. (result entry))))
  15098. (defun org-diary-to-ical-string (frombuf)
  15099. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15100. This uses the icalendar.el library."
  15101. (let* ((tmpdir (if (featurep 'xemacs)
  15102. (temp-directory)
  15103. temporary-file-directory))
  15104. (tmpfile (make-temp-name
  15105. (expand-file-name "orgics" tmpdir)))
  15106. buf rtn b e)
  15107. (with-current-buffer frombuf
  15108. (icalendar-export-region (point-min) (point-max) tmpfile)
  15109. (setq buf (find-buffer-visiting tmpfile))
  15110. (set-buffer buf)
  15111. (goto-char (point-min))
  15112. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15113. (setq b (match-beginning 0)))
  15114. (goto-char (point-max))
  15115. (if (re-search-backward "^END:VEVENT" nil t)
  15116. (setq e (match-end 0)))
  15117. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15118. (kill-buffer buf)
  15119. (delete-file tmpfile)
  15120. rtn))
  15121. (defun org-closest-date (start current change prefer show-all)
  15122. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15123. When PREFER is `past', return a date that is either CURRENT or past.
  15124. When PREFER is `future', return a date that is either CURRENT or future.
  15125. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15126. ;; Make the proper lists from the dates
  15127. (catch 'exit
  15128. (let ((a1 '(("h" . hour)
  15129. ("d" . day)
  15130. ("w" . week)
  15131. ("m" . month)
  15132. ("y" . year)))
  15133. (shour (nth 2 (org-parse-time-string start)))
  15134. dn dw sday cday n1 n2 n0
  15135. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15136. (setq start (org-date-to-gregorian start)
  15137. current (org-date-to-gregorian
  15138. (if show-all
  15139. current
  15140. (time-to-days (current-time))))
  15141. sday (calendar-absolute-from-gregorian start)
  15142. cday (calendar-absolute-from-gregorian current))
  15143. (if (<= cday sday) (throw 'exit sday))
  15144. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15145. (setq dn (string-to-number (match-string 1 change))
  15146. dw (cdr (assoc (match-string 2 change) a1)))
  15147. (error "Invalid change specifier: %s" change))
  15148. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15149. (cond
  15150. ((eq dw 'hour)
  15151. (let ((missing-hours
  15152. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15153. dn)))
  15154. (setq n1 (if (zerop missing-hours) cday
  15155. (- cday (1+ (floor (/ missing-hours 24)))))
  15156. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15157. ((eq dw 'day)
  15158. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15159. n2 (+ n1 dn)))
  15160. ((eq dw 'year)
  15161. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15162. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15163. (setq date1 (list m d y1)
  15164. n1 (calendar-absolute-from-gregorian date1)
  15165. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15166. n2 (calendar-absolute-from-gregorian date2)))
  15167. ((eq dw 'month)
  15168. ;; approx number of month between the two dates
  15169. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15170. ;; How often does dn fit in there?
  15171. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15172. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15173. m (+ m nm)
  15174. ny (floor (/ m 12))
  15175. y (+ y ny)
  15176. m (- m (* ny 12)))
  15177. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15178. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15179. (setq m2 (+ m dn) y2 y)
  15180. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15181. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15182. (while (<= n2 cday)
  15183. (setq n1 n2 m m2 y y2)
  15184. (setq m2 (+ m dn) y2 y)
  15185. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15186. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15187. ;; Make sure n1 is the earlier date
  15188. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15189. (if show-all
  15190. (cond
  15191. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15192. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15193. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15194. (cond
  15195. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15196. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15197. (t (if (= cday n1) n1 n2)))))))
  15198. (defun org-date-to-gregorian (date)
  15199. "Turn any specification of DATE into a Gregorian date for the calendar."
  15200. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15201. ((and (listp date) (= (length date) 3)) date)
  15202. ((stringp date)
  15203. (setq date (org-parse-time-string date))
  15204. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15205. ((listp date)
  15206. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15207. (defun org-parse-time-string (s &optional nodefault)
  15208. "Parse the standard Org-mode time string.
  15209. This should be a lot faster than the normal `parse-time-string'.
  15210. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15211. hour and minute fields will be nil if not given."
  15212. (cond ((string-match org-ts-regexp0 s)
  15213. (list 0
  15214. (if (or (match-beginning 8) (not nodefault))
  15215. (string-to-number (or (match-string 8 s) "0")))
  15216. (if (or (match-beginning 7) (not nodefault))
  15217. (string-to-number (or (match-string 7 s) "0")))
  15218. (string-to-number (match-string 4 s))
  15219. (string-to-number (match-string 3 s))
  15220. (string-to-number (match-string 2 s))
  15221. nil nil nil))
  15222. ((string-match "^<[^>]+>$" s)
  15223. (decode-time (seconds-to-time (org-matcher-time s))))
  15224. (t (error "Not a standard Org-mode time string: %s" s))))
  15225. (defun org-timestamp-up (&optional arg)
  15226. "Increase the date item at the cursor by one.
  15227. If the cursor is on the year, change the year. If it is on the month,
  15228. the day or the time, change that.
  15229. With prefix ARG, change by that many units."
  15230. (interactive "p")
  15231. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15232. (defun org-timestamp-down (&optional arg)
  15233. "Decrease the date item at the cursor by one.
  15234. If the cursor is on the year, change the year. If it is on the month,
  15235. the day or the time, change that.
  15236. With prefix ARG, change by that many units."
  15237. (interactive "p")
  15238. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15239. (defun org-timestamp-up-day (&optional arg)
  15240. "Increase the date in the time stamp by one day.
  15241. With prefix ARG, change that many days."
  15242. (interactive "p")
  15243. (if (and (not (org-at-timestamp-p t))
  15244. (org-at-heading-p))
  15245. (org-todo 'up)
  15246. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15247. (defun org-timestamp-down-day (&optional arg)
  15248. "Decrease the date in the time stamp by one day.
  15249. With prefix ARG, change that many days."
  15250. (interactive "p")
  15251. (if (and (not (org-at-timestamp-p t))
  15252. (org-at-heading-p))
  15253. (org-todo 'down)
  15254. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15255. (defun org-at-timestamp-p (&optional inactive-ok)
  15256. "Determine if the cursor is in or at a timestamp."
  15257. (interactive)
  15258. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15259. (pos (point))
  15260. (ans (or (looking-at tsr)
  15261. (save-excursion
  15262. (skip-chars-backward "^[<\n\r\t")
  15263. (if (> (point) (point-min)) (backward-char 1))
  15264. (and (looking-at tsr)
  15265. (> (- (match-end 0) pos) -1))))))
  15266. (and ans
  15267. (boundp 'org-ts-what)
  15268. (setq org-ts-what
  15269. (cond
  15270. ((= pos (match-beginning 0)) 'bracket)
  15271. ;; Point is considered to be "on the bracket" whether
  15272. ;; it's really on it or right after it.
  15273. ((= pos (1- (match-end 0))) 'bracket)
  15274. ((= pos (match-end 0)) 'after)
  15275. ((org-pos-in-match-range pos 2) 'year)
  15276. ((org-pos-in-match-range pos 3) 'month)
  15277. ((org-pos-in-match-range pos 7) 'hour)
  15278. ((org-pos-in-match-range pos 8) 'minute)
  15279. ((or (org-pos-in-match-range pos 4)
  15280. (org-pos-in-match-range pos 5)) 'day)
  15281. ((and (> pos (or (match-end 8) (match-end 5)))
  15282. (< pos (match-end 0)))
  15283. (- pos (or (match-end 8) (match-end 5))))
  15284. (t 'day))))
  15285. ans))
  15286. (defun org-toggle-timestamp-type ()
  15287. "Toggle the type (<active> or [inactive]) of a time stamp."
  15288. (interactive)
  15289. (when (org-at-timestamp-p t)
  15290. (let ((beg (match-beginning 0)) (end (match-end 0))
  15291. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15292. (save-excursion
  15293. (goto-char beg)
  15294. (while (re-search-forward "[][<>]" end t)
  15295. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15296. t t)))
  15297. (message "Timestamp is now %sactive"
  15298. (if (equal (char-after beg) ?<) "" "in")))))
  15299. (defvar org-clock-history) ; defined in org-clock.el
  15300. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15301. (defun org-timestamp-change (n &optional what updown)
  15302. "Change the date in the time stamp at point.
  15303. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15304. `year', `minute', `second'. If WHAT is not given, the cursor position
  15305. in the timestamp determines what will be changed."
  15306. (let ((origin (point)) origin-cat
  15307. with-hm inactive
  15308. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15309. org-ts-what
  15310. extra rem
  15311. ts time time0 fixnext clrgx)
  15312. (if (not (org-at-timestamp-p t))
  15313. (error "Not at a timestamp"))
  15314. (if (and (not what) (eq org-ts-what 'bracket))
  15315. (org-toggle-timestamp-type)
  15316. ;; Point isn't on brackets. Remember the part of the time-stamp
  15317. ;; the point was in. Indeed, size of time-stamps may change,
  15318. ;; but point must be kept in the same category nonetheless.
  15319. (setq origin-cat org-ts-what)
  15320. (if (and (not what) (not (eq org-ts-what 'day))
  15321. org-display-custom-times
  15322. (get-text-property (point) 'display)
  15323. (not (get-text-property (1- (point)) 'display)))
  15324. (setq org-ts-what 'day))
  15325. (setq org-ts-what (or what org-ts-what)
  15326. inactive (= (char-after (match-beginning 0)) ?\[)
  15327. ts (match-string 0))
  15328. (replace-match "")
  15329. (if (string-match
  15330. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15331. ts)
  15332. (setq extra (match-string 1 ts)))
  15333. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15334. (setq with-hm t))
  15335. (setq time0 (org-parse-time-string ts))
  15336. (when (and updown
  15337. (eq org-ts-what 'minute)
  15338. (not current-prefix-arg))
  15339. ;; This looks like s-up and s-down. Change by one rounding step.
  15340. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15341. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15342. (setcar (cdr time0) (+ (nth 1 time0)
  15343. (if (> n 0) (- rem) (- dm rem))))))
  15344. (setq time
  15345. (encode-time (or (car time0) 0)
  15346. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15347. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15348. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15349. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15350. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15351. (nthcdr 6 time0)))
  15352. (when (and (member org-ts-what '(hour minute))
  15353. extra
  15354. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15355. (setq extra (org-modify-ts-extra
  15356. extra
  15357. (if (eq org-ts-what 'hour) 2 5)
  15358. n dm)))
  15359. (when (integerp org-ts-what)
  15360. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15361. (if (eq what 'calendar)
  15362. (let ((cal-date (org-get-date-from-calendar)))
  15363. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15364. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15365. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15366. (setcar time0 (or (car time0) 0))
  15367. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15368. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15369. (setq time (apply 'encode-time time0))))
  15370. ;; Insert the new time-stamp, and ensure point stays in the same
  15371. ;; category as before (i.e. not after the last position in that
  15372. ;; category).
  15373. (let ((pos (point)))
  15374. ;; Stay before inserted string. `save-excursion' is of no use.
  15375. (setq org-last-changed-timestamp
  15376. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15377. (goto-char pos))
  15378. (save-match-data
  15379. (looking-at org-ts-regexp3)
  15380. (goto-char (cond
  15381. ;; `day' category ends before `hour' if any, or at
  15382. ;; the end of the day name.
  15383. ((eq origin-cat 'day)
  15384. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15385. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15386. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15387. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15388. ;; `year' and `month' have both fixed size: point
  15389. ;; couldn't have moved into another part.
  15390. (t origin))))
  15391. ;; Update clock if on a CLOCK line.
  15392. (org-clock-update-time-maybe)
  15393. ;; Maybe adjust the closest clock in `org-clock-history'
  15394. (when org-clock-adjust-closest
  15395. (if (not (and (org-at-clock-log-p)
  15396. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15397. org-clock-history))))))
  15398. (message "No clock to adjust")
  15399. (cond ((save-excursion ; fix previous clock?
  15400. (re-search-backward org-ts-regexp0 nil t)
  15401. (org-looking-back (concat org-clock-string " \\[")))
  15402. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15403. ((save-excursion ; fix next clock?
  15404. (re-search-backward org-ts-regexp0 nil t)
  15405. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15406. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15407. (save-window-excursion
  15408. ;; Find closest clock to point, adjust the previous/next one in history
  15409. (let* ((p (save-excursion (org-back-to-heading t)))
  15410. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15411. (clfixnth
  15412. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15413. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15414. (if (not clfixpos)
  15415. (message "No clock to adjust")
  15416. (save-excursion
  15417. (org-goto-marker-or-bmk clfixpos)
  15418. (org-show-subtree)
  15419. (when (re-search-forward clrgx nil t)
  15420. (goto-char (match-beginning 1))
  15421. (let (org-clock-adjust-closest)
  15422. (org-timestamp-change n org-ts-what updown))
  15423. (message "Clock adjusted in %s for heading: %s"
  15424. (file-name-nondirectory (buffer-file-name))
  15425. (org-get-heading t t)))))))))
  15426. ;; Try to recenter the calendar window, if any.
  15427. (if (and org-calendar-follow-timestamp-change
  15428. (get-buffer-window "*Calendar*" t)
  15429. (memq org-ts-what '(day month year)))
  15430. (org-recenter-calendar (time-to-days time))))))
  15431. (defun org-modify-ts-extra (s pos n dm)
  15432. "Change the different parts of the lead-time and repeat fields in timestamp."
  15433. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15434. ng h m new rem)
  15435. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15436. (cond
  15437. ((or (org-pos-in-match-range pos 2)
  15438. (org-pos-in-match-range pos 3))
  15439. (setq m (string-to-number (match-string 3 s))
  15440. h (string-to-number (match-string 2 s)))
  15441. (if (org-pos-in-match-range pos 2)
  15442. (setq h (+ h n))
  15443. (setq n (* dm (org-no-warnings (signum n))))
  15444. (when (not (= 0 (setq rem (% m dm))))
  15445. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15446. (setq m (+ m n)))
  15447. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15448. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15449. (setq h (min 24 (max 0 h)))
  15450. (setq ng 1 new (format "-%02d:%02d" h m)))
  15451. ((org-pos-in-match-range pos 6)
  15452. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15453. ((org-pos-in-match-range pos 5)
  15454. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15455. ((org-pos-in-match-range pos 9)
  15456. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15457. ((org-pos-in-match-range pos 8)
  15458. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15459. (when ng
  15460. (setq s (concat
  15461. (substring s 0 (match-beginning ng))
  15462. new
  15463. (substring s (match-end ng))))))
  15464. s))
  15465. (defun org-recenter-calendar (date)
  15466. "If the calendar is visible, recenter it to DATE."
  15467. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15468. (when cwin
  15469. (let ((calendar-move-hook nil))
  15470. (with-selected-window cwin
  15471. (calendar-goto-date (if (listp date) date
  15472. (calendar-gregorian-from-absolute date))))))))
  15473. (defun org-goto-calendar (&optional arg)
  15474. "Go to the Emacs calendar at the current date.
  15475. If there is a time stamp in the current line, go to that date.
  15476. A prefix ARG can be used to force the current date."
  15477. (interactive "P")
  15478. (let ((tsr org-ts-regexp) diff
  15479. (calendar-move-hook nil)
  15480. (calendar-view-holidays-initially-flag nil)
  15481. (calendar-view-diary-initially-flag nil))
  15482. (if (or (org-at-timestamp-p)
  15483. (save-excursion
  15484. (beginning-of-line 1)
  15485. (looking-at (concat ".*" tsr))))
  15486. (let ((d1 (time-to-days (current-time)))
  15487. (d2 (time-to-days
  15488. (org-time-string-to-time (match-string 1)))))
  15489. (setq diff (- d2 d1))))
  15490. (calendar)
  15491. (calendar-goto-today)
  15492. (if (and diff (not arg)) (calendar-forward-day diff))))
  15493. (defun org-get-date-from-calendar ()
  15494. "Return a list (month day year) of date at point in calendar."
  15495. (with-current-buffer "*Calendar*"
  15496. (save-match-data
  15497. (calendar-cursor-to-date))))
  15498. (defun org-date-from-calendar ()
  15499. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15500. If there is already a time stamp at the cursor position, update it."
  15501. (interactive)
  15502. (if (org-at-timestamp-p t)
  15503. (org-timestamp-change 0 'calendar)
  15504. (let ((cal-date (org-get-date-from-calendar)))
  15505. (org-insert-time-stamp
  15506. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15507. (defcustom org-effort-durations
  15508. `(("h" . 60)
  15509. ("d" . ,(* 60 8))
  15510. ("w" . ,(* 60 8 5))
  15511. ("m" . ,(* 60 8 5 4))
  15512. ("y" . ,(* 60 8 5 40)))
  15513. "Conversion factor to minutes for an effort modifier.
  15514. Each entry has the form (MODIFIER . MINUTES).
  15515. In an effort string, a number followed by MODIFIER is multiplied
  15516. by the specified number of MINUTES to obtain an effort in
  15517. minutes.
  15518. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15519. effort string \"2hours\" is equivalent to 120 minutes."
  15520. :group 'org-agenda
  15521. :version "24.1"
  15522. :type '(alist :key-type (string :tag "Modifier")
  15523. :value-type (number :tag "Minutes")))
  15524. (defun org-minutes-to-clocksum-string (m)
  15525. "Format number of minutes as a clocksum string.
  15526. The format is determined by `org-time-clocksum-format',
  15527. `org-time-clocksum-use-fractional' and
  15528. `org-time-clocksum-fractional-format' and
  15529. `org-time-clocksum-use-effort-durations'."
  15530. (let ((clocksum "") h d w mo y fmt n)
  15531. (setq h (if org-time-clocksum-use-effort-durations
  15532. (cdr (assoc "h" org-effort-durations)) 60)
  15533. d (if org-time-clocksum-use-effort-durations
  15534. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15535. w (if org-time-clocksum-use-effort-durations
  15536. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15537. mo (if org-time-clocksum-use-effort-durations
  15538. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15539. y (if org-time-clocksum-use-effort-durations
  15540. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15541. ;; fractional format
  15542. (if org-time-clocksum-use-fractional
  15543. (cond
  15544. ;; single format string
  15545. ((stringp org-time-clocksum-fractional-format)
  15546. (format org-time-clocksum-fractional-format (/ m (float h))))
  15547. ;; choice of fractional formats for different time units
  15548. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15549. (> (/ (truncate m) (* y d h)) 0))
  15550. (format fmt (/ m (* y d (float h)))))
  15551. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15552. (> (/ (truncate m) (* mo d h)) 0))
  15553. (format fmt (/ m (* mo d (float h)))))
  15554. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15555. (> (/ (truncate m) (* w d h)) 0))
  15556. (format fmt (/ m (* w d (float h)))))
  15557. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15558. (> (/ (truncate m) (* d h)) 0))
  15559. (format fmt (/ m (* d (float h)))))
  15560. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15561. (> (/ (truncate m) h) 0))
  15562. (format fmt (/ m (float h))))
  15563. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15564. (format fmt m))
  15565. ;; fall back to smallest time unit with a format
  15566. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15567. (format fmt (/ m (float h))))
  15568. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15569. (format fmt (/ m (* d (float h)))))
  15570. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15571. (format fmt (/ m (* w d (float h)))))
  15572. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15573. (format fmt (/ m (* mo d (float h)))))
  15574. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15575. (format fmt (/ m (* y d (float h))))))
  15576. ;; standard (non-fractional) format, with single format string
  15577. (if (stringp org-time-clocksum-format)
  15578. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15579. ;; separate formats components
  15580. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15581. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15582. (plist-get org-time-clocksum-format :require-years))
  15583. (setq clocksum (concat clocksum (format fmt n))
  15584. m (- m (* n y d h))))
  15585. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15586. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15587. (plist-get org-time-clocksum-format :require-months))
  15588. (setq clocksum (concat clocksum (format fmt n))
  15589. m (- m (* n mo d h))))
  15590. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15591. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15592. (plist-get org-time-clocksum-format :require-weeks))
  15593. (setq clocksum (concat clocksum (format fmt n))
  15594. m (- m (* n w d h))))
  15595. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15596. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15597. (plist-get org-time-clocksum-format :require-days))
  15598. (setq clocksum (concat clocksum (format fmt n))
  15599. m (- m (* n d h))))
  15600. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15601. (or (> (setq n (/ (truncate m) h)) 0)
  15602. (plist-get org-time-clocksum-format :require-hours))
  15603. (setq clocksum (concat clocksum (format fmt n))
  15604. m (- m (* n h))))
  15605. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15606. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15607. (setq clocksum (concat clocksum (format fmt m))))
  15608. ;; return formatted time duration
  15609. clocksum))))
  15610. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15611. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15612. "Org mode version 8.0")
  15613. (defun org-hours-to-clocksum-string (n)
  15614. (org-minutes-to-clocksum-string (* n 60)))
  15615. (defun org-hh:mm-string-to-minutes (s)
  15616. "Convert a string H:MM to a number of minutes.
  15617. If the string is just a number, interpret it as minutes.
  15618. In fact, the first hh:mm or number in the string will be taken,
  15619. there can be extra stuff in the string.
  15620. If no number is found, the return value is 0."
  15621. (cond
  15622. ((integerp s) s)
  15623. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15624. (+ (* (string-to-number (match-string 1 s)) 60)
  15625. (string-to-number (match-string 2 s))))
  15626. ((string-match "\\([0-9]+\\)" s)
  15627. (string-to-number (match-string 1 s)))
  15628. (t 0)))
  15629. (defcustom org-image-actual-width t
  15630. "Should we use the actual width of images when inlining them?
  15631. When set to `t', always use the image width.
  15632. When set to a number, use imagemagick (when available) to set
  15633. the image's width to this value.
  15634. When set to a number in a list, try to get the width from the
  15635. #+ATTR.* keyword if it matches a width specification like
  15636. width=\"[0-9]+\", and fall back on that number if none is found.
  15637. When set to nil, try to get the width from an #+ATTR.* keyword
  15638. and fall back on the original width if none is found.
  15639. This requires Emacs >= 24.1, build with imagemagick support."
  15640. :group 'org-appearance
  15641. :version "24.3"
  15642. :type '(choice
  15643. (const :tag "Use the image width" t)
  15644. (integer :tag "Use a number of pixels")
  15645. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15646. (const :tag "Use #+ATTR* or don't resize" nil)))
  15647. (defcustom org-agenda-inhibit-startup t
  15648. "Inhibit startup when preparing agenda buffers.
  15649. When this variable is `t' (the default), the initialization of
  15650. the Org agenda buffers is inhibited: e.g. the visibility state
  15651. is not set, the tables are not re-aligned, etc."
  15652. :type 'boolean
  15653. :version "24.3"
  15654. :group 'org-agenda)
  15655. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15656. "Convert a duration string S to minutes.
  15657. A bare number is interpreted as minutes, modifiers can be set by
  15658. customizing `org-effort-durations' (which see).
  15659. Entries containing a colon are interpreted as H:MM by
  15660. `org-hh:mm-string-to-minutes'."
  15661. (let ((result 0)
  15662. (re (concat "\\([0-9.]+\\) *\\("
  15663. (regexp-opt (mapcar 'car org-effort-durations))
  15664. "\\)")))
  15665. (while (string-match re s)
  15666. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15667. (string-to-number (match-string 1 s))))
  15668. (setq s (replace-match "" nil t s)))
  15669. (setq result (floor result))
  15670. (incf result (org-hh:mm-string-to-minutes s))
  15671. (if output-to-string (number-to-string result) result)))
  15672. ;;;; Files
  15673. (defun org-save-all-org-buffers ()
  15674. "Save all Org-mode buffers without user confirmation."
  15675. (interactive)
  15676. (message "Saving all Org-mode buffers...")
  15677. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15678. (when (featurep 'org-id) (org-id-locations-save))
  15679. (message "Saving all Org-mode buffers... done"))
  15680. (defun org-revert-all-org-buffers ()
  15681. "Revert all Org-mode buffers.
  15682. Prompt for confirmation when there are unsaved changes.
  15683. Be sure you know what you are doing before letting this function
  15684. overwrite your changes.
  15685. This function is useful in a setup where one tracks org files
  15686. with a version control system, to revert on one machine after pulling
  15687. changes from another. I believe the procedure must be like this:
  15688. 1. M-x org-save-all-org-buffers
  15689. 2. Pull changes from the other machine, resolve conflicts
  15690. 3. M-x org-revert-all-org-buffers"
  15691. (interactive)
  15692. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15693. (error "Abort"))
  15694. (save-excursion
  15695. (save-window-excursion
  15696. (mapc
  15697. (lambda (b)
  15698. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15699. (with-current-buffer b buffer-file-name))
  15700. (org-pop-to-buffer-same-window b)
  15701. (revert-buffer t 'no-confirm)))
  15702. (buffer-list))
  15703. (when (and (featurep 'org-id) org-id-track-globally)
  15704. (org-id-locations-load)))))
  15705. ;;;; Agenda files
  15706. ;;;###autoload
  15707. (defun org-switchb (&optional arg)
  15708. "Switch between Org buffers.
  15709. With one prefix argument, restrict available buffers to files.
  15710. With two prefix arguments, restrict available buffers to agenda files.
  15711. Defaults to `iswitchb' for buffer name completion.
  15712. Set `org-completion-use-ido' to make it use ido instead."
  15713. (interactive "P")
  15714. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15715. ((equal arg '(16)) (org-buffer-list 'agenda))
  15716. (t (org-buffer-list))))
  15717. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15718. (org-completion-use-ido org-completion-use-ido))
  15719. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15720. (setq org-completion-use-iswitchb t))
  15721. (org-pop-to-buffer-same-window
  15722. (org-icompleting-read "Org buffer: "
  15723. (mapcar 'list (mapcar 'buffer-name blist))
  15724. nil t))))
  15725. ;;; Define some older names previously used for this functionality
  15726. ;;;###autoload
  15727. (defalias 'org-ido-switchb 'org-switchb)
  15728. ;;;###autoload
  15729. (defalias 'org-iswitchb 'org-switchb)
  15730. (defun org-buffer-list (&optional predicate exclude-tmp)
  15731. "Return a list of Org buffers.
  15732. PREDICATE can be `export', `files' or `agenda'.
  15733. export restrict the list to Export buffers.
  15734. files restrict the list to buffers visiting Org files.
  15735. agenda restrict the list to buffers visiting agenda files.
  15736. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15737. (let* ((bfn nil)
  15738. (agenda-files (and (eq predicate 'agenda)
  15739. (mapcar 'file-truename (org-agenda-files t))))
  15740. (filter
  15741. (cond
  15742. ((eq predicate 'files)
  15743. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15744. ((eq predicate 'export)
  15745. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15746. ((eq predicate 'agenda)
  15747. (lambda (b)
  15748. (with-current-buffer b
  15749. (and (derived-mode-p 'org-mode)
  15750. (setq bfn (buffer-file-name b))
  15751. (member (file-truename bfn) agenda-files)))))
  15752. (t (lambda (b) (with-current-buffer b
  15753. (or (derived-mode-p 'org-mode)
  15754. (string-match "\*Org .*Export"
  15755. (buffer-name b)))))))))
  15756. (delq nil
  15757. (mapcar
  15758. (lambda(b)
  15759. (if (and (funcall filter b)
  15760. (or (not exclude-tmp)
  15761. (not (string-match "tmp" (buffer-name b)))))
  15762. b
  15763. nil))
  15764. (buffer-list)))))
  15765. (defun org-agenda-files (&optional unrestricted archives)
  15766. "Get the list of agenda files.
  15767. Optional UNRESTRICTED means return the full list even if a restriction
  15768. is currently in place.
  15769. When ARCHIVES is t, include all archive files that are really being
  15770. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15771. `org-agenda-archives-mode' is t."
  15772. (let ((files
  15773. (cond
  15774. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15775. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15776. ((listp org-agenda-files) org-agenda-files)
  15777. (t (error "Invalid value of `org-agenda-files'")))))
  15778. (setq files (apply 'append
  15779. (mapcar (lambda (f)
  15780. (if (file-directory-p f)
  15781. (directory-files
  15782. f t org-agenda-file-regexp)
  15783. (list f)))
  15784. files)))
  15785. (when org-agenda-skip-unavailable-files
  15786. (setq files (delq nil
  15787. (mapcar (function
  15788. (lambda (file)
  15789. (and (file-readable-p file) file)))
  15790. files))))
  15791. (when (or (eq archives t)
  15792. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15793. (setq files (org-add-archive-files files)))
  15794. files))
  15795. (defun org-agenda-file-p (&optional file)
  15796. "Return non-nil, if FILE is an agenda file.
  15797. If FILE is omitted, use the file associated with the current
  15798. buffer."
  15799. (member (or file (buffer-file-name))
  15800. (org-agenda-files t)))
  15801. (defun org-edit-agenda-file-list ()
  15802. "Edit the list of agenda files.
  15803. Depending on setup, this either uses customize to edit the variable
  15804. `org-agenda-files', or it visits the file that is holding the list. In the
  15805. latter case, the buffer is set up in a way that saving it automatically kills
  15806. the buffer and restores the previous window configuration."
  15807. (interactive)
  15808. (if (stringp org-agenda-files)
  15809. (let ((cw (current-window-configuration)))
  15810. (find-file org-agenda-files)
  15811. (org-set-local 'org-window-configuration cw)
  15812. (org-add-hook 'after-save-hook
  15813. (lambda ()
  15814. (set-window-configuration
  15815. (prog1 org-window-configuration
  15816. (kill-buffer (current-buffer))))
  15817. (org-install-agenda-files-menu)
  15818. (message "New agenda file list installed"))
  15819. nil 'local)
  15820. (message "%s" (substitute-command-keys
  15821. "Edit list and finish with \\[save-buffer]")))
  15822. (customize-variable 'org-agenda-files)))
  15823. (defun org-store-new-agenda-file-list (list)
  15824. "Set new value for the agenda file list and save it correctly."
  15825. (if (stringp org-agenda-files)
  15826. (let ((fe (org-read-agenda-file-list t)) b u)
  15827. (while (setq b (find-buffer-visiting org-agenda-files))
  15828. (kill-buffer b))
  15829. (with-temp-file org-agenda-files
  15830. (insert
  15831. (mapconcat
  15832. (lambda (f) ;; Keep un-expanded entries.
  15833. (if (setq u (assoc f fe))
  15834. (cdr u)
  15835. f))
  15836. list "\n")
  15837. "\n")))
  15838. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15839. (org-insert-mode-line-in-empty-file nil))
  15840. (setq org-agenda-files list)
  15841. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15842. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15843. "Read the list of agenda files from a file.
  15844. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15845. filenames, used by `org-store-new-agenda-file-list' to write back
  15846. un-expanded file names."
  15847. (when (file-directory-p org-agenda-files)
  15848. (error "`org-agenda-files' cannot be a single directory"))
  15849. (when (stringp org-agenda-files)
  15850. (with-temp-buffer
  15851. (insert-file-contents org-agenda-files)
  15852. (mapcar
  15853. (lambda (f)
  15854. (let ((e (expand-file-name (substitute-in-file-name f)
  15855. org-directory)))
  15856. (if pair-with-expansion
  15857. (cons e f)
  15858. e)))
  15859. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15860. ;;;###autoload
  15861. (defun org-cycle-agenda-files ()
  15862. "Cycle through the files in `org-agenda-files'.
  15863. If the current buffer visits an agenda file, find the next one in the list.
  15864. If the current buffer does not, find the first agenda file."
  15865. (interactive)
  15866. (let* ((fs (org-agenda-files t))
  15867. (files (append fs (list (car fs))))
  15868. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15869. file)
  15870. (unless files (error "No agenda files"))
  15871. (catch 'exit
  15872. (while (setq file (pop files))
  15873. (if (equal (file-truename file) tcf)
  15874. (when (car files)
  15875. (find-file (car files))
  15876. (throw 'exit t))))
  15877. (find-file (car fs)))
  15878. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15879. (defun org-agenda-file-to-front (&optional to-end)
  15880. "Move/add the current file to the top of the agenda file list.
  15881. If the file is not present in the list, it is added to the front. If it is
  15882. present, it is moved there. With optional argument TO-END, add/move to the
  15883. end of the list."
  15884. (interactive "P")
  15885. (let ((org-agenda-skip-unavailable-files nil)
  15886. (file-alist (mapcar (lambda (x)
  15887. (cons (file-truename x) x))
  15888. (org-agenda-files t)))
  15889. (ctf (file-truename
  15890. (or buffer-file-name
  15891. (error "Please save the current buffer to a file"))))
  15892. x had)
  15893. (setq x (assoc ctf file-alist) had x)
  15894. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15895. (if to-end
  15896. (setq file-alist (append (delq x file-alist) (list x)))
  15897. (setq file-alist (cons x (delq x file-alist))))
  15898. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15899. (org-install-agenda-files-menu)
  15900. (message "File %s to %s of agenda file list"
  15901. (if had "moved" "added") (if to-end "end" "front"))))
  15902. (defun org-remove-file (&optional file)
  15903. "Remove current file from the list of files in variable `org-agenda-files'.
  15904. These are the files which are being checked for agenda entries.
  15905. Optional argument FILE means use this file instead of the current."
  15906. (interactive)
  15907. (let* ((org-agenda-skip-unavailable-files nil)
  15908. (file (or file buffer-file-name
  15909. (error "Current buffer does not visit a file")))
  15910. (true-file (file-truename file))
  15911. (afile (abbreviate-file-name file))
  15912. (files (delq nil (mapcar
  15913. (lambda (x)
  15914. (if (equal true-file
  15915. (file-truename x))
  15916. nil x))
  15917. (org-agenda-files t)))))
  15918. (if (not (= (length files) (length (org-agenda-files t))))
  15919. (progn
  15920. (org-store-new-agenda-file-list files)
  15921. (org-install-agenda-files-menu)
  15922. (message "Removed file: %s" afile))
  15923. (message "File was not in list: %s (not removed)" afile))))
  15924. (defun org-file-menu-entry (file)
  15925. (vector file (list 'find-file file) t))
  15926. (defun org-check-agenda-file (file)
  15927. "Make sure FILE exists. If not, ask user what to do."
  15928. (when (not (file-exists-p file))
  15929. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15930. (abbreviate-file-name file))
  15931. (let ((r (downcase (read-char-exclusive))))
  15932. (cond
  15933. ((equal r ?r)
  15934. (org-remove-file file)
  15935. (throw 'nextfile t))
  15936. (t (error "Abort"))))))
  15937. (defun org-get-agenda-file-buffer (file)
  15938. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15939. it to the list of buffers which might be released later."
  15940. (let ((buf (org-find-base-buffer-visiting file)))
  15941. (if buf
  15942. buf ; just return it
  15943. ;; Make a new buffer and remember it
  15944. (setq buf (find-file-noselect file))
  15945. (if buf (push buf org-agenda-new-buffers))
  15946. buf)))
  15947. (defun org-release-buffers (blist)
  15948. "Release all buffers in list, asking the user for confirmation when needed.
  15949. When a buffer is unmodified, it is just killed. When modified, it is saved
  15950. \(if the user agrees) and then killed."
  15951. (let (buf file)
  15952. (while (setq buf (pop blist))
  15953. (setq file (buffer-file-name buf))
  15954. (when (and (buffer-modified-p buf)
  15955. file
  15956. (y-or-n-p (format "Save file %s? " file)))
  15957. (with-current-buffer buf (save-buffer)))
  15958. (kill-buffer buf))))
  15959. (defun org-agenda-prepare-buffers (files)
  15960. "Create buffers for all agenda files, protect archived trees and comments."
  15961. (interactive)
  15962. (let ((pa '(:org-archived t))
  15963. (pc '(:org-comment t))
  15964. (pall '(:org-archived t :org-comment t))
  15965. (inhibit-read-only t)
  15966. (org-inhibit-startup org-agenda-inhibit-startup)
  15967. (rea (concat ":" org-archive-tag ":"))
  15968. bmp file re)
  15969. (save-excursion
  15970. (save-restriction
  15971. (while (setq file (pop files))
  15972. (catch 'nextfile
  15973. (if (bufferp file)
  15974. (set-buffer file)
  15975. (org-check-agenda-file file)
  15976. (set-buffer (org-get-agenda-file-buffer file)))
  15977. (widen)
  15978. (setq bmp (buffer-modified-p))
  15979. (org-refresh-category-properties)
  15980. (org-refresh-properties org-effort-property 'org-effort)
  15981. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15982. (setq org-todo-keywords-for-agenda
  15983. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15984. (setq org-done-keywords-for-agenda
  15985. (append org-done-keywords-for-agenda org-done-keywords))
  15986. (setq org-todo-keyword-alist-for-agenda
  15987. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15988. (setq org-drawers-for-agenda
  15989. (append org-drawers-for-agenda org-drawers))
  15990. (setq org-tag-alist-for-agenda
  15991. (append org-tag-alist-for-agenda org-tag-alist))
  15992. (save-excursion
  15993. (remove-text-properties (point-min) (point-max) pall)
  15994. (when org-agenda-skip-archived-trees
  15995. (goto-char (point-min))
  15996. (while (re-search-forward rea nil t)
  15997. (if (org-at-heading-p t)
  15998. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15999. (goto-char (point-min))
  16000. (setq re (format org-heading-keyword-regexp-format
  16001. org-comment-string))
  16002. (while (re-search-forward re nil t)
  16003. (add-text-properties
  16004. (match-beginning 0) (org-end-of-subtree t) pc)))
  16005. (set-buffer-modified-p bmp)))))
  16006. (setq org-todo-keywords-for-agenda
  16007. (org-uniquify org-todo-keywords-for-agenda))
  16008. (setq org-todo-keyword-alist-for-agenda
  16009. (org-uniquify org-todo-keyword-alist-for-agenda)
  16010. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16011. ;;;; Embedded LaTeX
  16012. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16013. "Keymap for the minor `org-cdlatex-mode'.")
  16014. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16015. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16016. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16017. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16018. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16019. (defvar org-cdlatex-texmathp-advice-is-done nil
  16020. "Flag remembering if we have applied the advice to texmathp already.")
  16021. (define-minor-mode org-cdlatex-mode
  16022. "Toggle the minor `org-cdlatex-mode'.
  16023. This mode supports entering LaTeX environment and math in LaTeX fragments
  16024. in Org-mode.
  16025. \\{org-cdlatex-mode-map}"
  16026. nil " OCDL" nil
  16027. (when org-cdlatex-mode
  16028. (require 'cdlatex)
  16029. (run-hooks 'cdlatex-mode-hook)
  16030. (cdlatex-compute-tables))
  16031. (unless org-cdlatex-texmathp-advice-is-done
  16032. (setq org-cdlatex-texmathp-advice-is-done t)
  16033. (defadvice texmathp (around org-math-always-on activate)
  16034. "Always return t in org-mode buffers.
  16035. This is because we want to insert math symbols without dollars even outside
  16036. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16037. an embedded LaTeX fragment, let texmathp do its job.
  16038. \\[org-cdlatex-mode-map]"
  16039. (interactive)
  16040. (let (p)
  16041. (cond
  16042. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16043. ((eq this-command 'cdlatex-math-symbol)
  16044. (setq ad-return-value t
  16045. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16046. (t
  16047. (let ((p (org-inside-LaTeX-fragment-p)))
  16048. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16049. (setq ad-return-value t
  16050. texmathp-why '("Org-mode embedded math" . 0))
  16051. (if p ad-do-it)))))))))
  16052. (defun turn-on-org-cdlatex ()
  16053. "Unconditionally turn on `org-cdlatex-mode'."
  16054. (org-cdlatex-mode 1))
  16055. (defun org-inside-LaTeX-fragment-p ()
  16056. "Test if point is inside a LaTeX fragment.
  16057. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16058. sequence appearing also before point.
  16059. Even though the matchers for math are configurable, this function assumes
  16060. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16061. delimiters are skipped when they have been removed by customization.
  16062. The return value is nil, or a cons cell with the delimiter and the
  16063. position of this delimiter.
  16064. This function does a reasonably good job, but can locally be fooled by
  16065. for example currency specifications. For example it will assume being in
  16066. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16067. fragments that are properly closed, but during editing, we have to live
  16068. with the uncertainty caused by missing closing delimiters. This function
  16069. looks only before point, not after."
  16070. (catch 'exit
  16071. (let ((pos (point))
  16072. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16073. (lim (progn
  16074. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16075. (point)))
  16076. dd-on str (start 0) m re)
  16077. (goto-char pos)
  16078. (when dodollar
  16079. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16080. re (nth 1 (assoc "$" org-latex-regexps)))
  16081. (while (string-match re str start)
  16082. (cond
  16083. ((= (match-end 0) (length str))
  16084. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16085. ((= (match-end 0) (- (length str) 5))
  16086. (throw 'exit nil))
  16087. (t (setq start (match-end 0))))))
  16088. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16089. (goto-char pos)
  16090. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16091. (and (match-beginning 2) (throw 'exit nil))
  16092. ;; count $$
  16093. (while (re-search-backward "\\$\\$" lim t)
  16094. (setq dd-on (not dd-on)))
  16095. (goto-char pos)
  16096. (if dd-on (cons "$$" m))))))
  16097. (defun org-inside-latex-macro-p ()
  16098. "Is point inside a LaTeX macro or its arguments?"
  16099. (save-match-data
  16100. (org-in-regexp
  16101. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16102. (defun org-try-cdlatex-tab ()
  16103. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16104. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16105. - inside a LaTeX fragment, or
  16106. - after the first word in a line, where an abbreviation expansion could
  16107. insert a LaTeX environment."
  16108. (when org-cdlatex-mode
  16109. (cond
  16110. ;; Before any word on the line: No expansion possible.
  16111. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16112. ;; Just after first word on the line: Expand it. Make sure it
  16113. ;; cannot happen on headlines, though.
  16114. ((save-excursion
  16115. (skip-chars-backward "a-zA-Z0-9*")
  16116. (skip-chars-backward " \t")
  16117. (and (bolp) (not (org-at-heading-p))))
  16118. (cdlatex-tab) t)
  16119. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16120. (defun org-cdlatex-underscore-caret (&optional arg)
  16121. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16122. Revert to the normal definition outside of these fragments."
  16123. (interactive "P")
  16124. (if (org-inside-LaTeX-fragment-p)
  16125. (call-interactively 'cdlatex-sub-superscript)
  16126. (let (org-cdlatex-mode)
  16127. (call-interactively (key-binding (vector last-input-event))))))
  16128. (defun org-cdlatex-math-modify (&optional arg)
  16129. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16130. Revert to the normal definition outside of these fragments."
  16131. (interactive "P")
  16132. (if (org-inside-LaTeX-fragment-p)
  16133. (call-interactively 'cdlatex-math-modify)
  16134. (let (org-cdlatex-mode)
  16135. (call-interactively (key-binding (vector last-input-event))))))
  16136. (defvar org-latex-fragment-image-overlays nil
  16137. "List of overlays carrying the images of latex fragments.")
  16138. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16139. (defun org-remove-latex-fragment-image-overlays ()
  16140. "Remove all overlays with LaTeX fragment images in current buffer."
  16141. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16142. (setq org-latex-fragment-image-overlays nil))
  16143. (defun org-preview-latex-fragment (&optional subtree)
  16144. "Preview the LaTeX fragment at point, or all locally or globally.
  16145. If the cursor is in a LaTeX fragment, create the image and overlay
  16146. it over the source code. If there is no fragment at point, display
  16147. all fragments in the current text, from one headline to the next. With
  16148. prefix SUBTREE, display all fragments in the current subtree. With a
  16149. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16150. the cursor is before the first headline,
  16151. display all fragments in the buffer.
  16152. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16153. (interactive "P")
  16154. (unless buffer-file-name
  16155. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16156. (org-remove-latex-fragment-image-overlays)
  16157. (save-excursion
  16158. (save-restriction
  16159. (let (beg end at msg)
  16160. (cond
  16161. ((or (equal subtree '(16))
  16162. (not (save-excursion
  16163. (re-search-backward org-outline-regexp-bol nil t))))
  16164. (setq beg (point-min) end (point-max)
  16165. msg "Creating images for buffer...%s"))
  16166. ((equal subtree '(4))
  16167. (org-back-to-heading)
  16168. (setq beg (point) end (org-end-of-subtree t)
  16169. msg "Creating images for subtree...%s"))
  16170. (t
  16171. (if (setq at (org-inside-LaTeX-fragment-p))
  16172. (goto-char (max (point-min) (- (cdr at) 2)))
  16173. (org-back-to-heading))
  16174. (setq beg (point) end (progn (outline-next-heading) (point))
  16175. msg (if at "Creating image...%s"
  16176. "Creating images for entry...%s"))))
  16177. (message msg "")
  16178. (narrow-to-region beg end)
  16179. (goto-char beg)
  16180. (org-format-latex
  16181. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16182. (file-name-nondirectory
  16183. buffer-file-name)))
  16184. default-directory 'overlays msg at 'forbuffer
  16185. org-latex-create-formula-image-program)
  16186. (message msg "done. Use `C-c C-c' to remove images.")))))
  16187. (defvar org-latex-regexps
  16188. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16189. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16190. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16191. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16192. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16193. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16194. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16195. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16196. "Regular expressions for matching embedded LaTeX.")
  16197. (defvar org-export-have-math nil) ;; dynamic scoping
  16198. (defun org-format-latex (prefix &optional dir overlays msg at
  16199. forbuffer processing-type)
  16200. "Replace LaTeX fragments with links to an image, and produce images.
  16201. Some of the options can be changed using the variable
  16202. `org-format-latex-options'."
  16203. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16204. (let* ((prefixnodir (file-name-nondirectory prefix))
  16205. (absprefix (expand-file-name prefix dir))
  16206. (todir (file-name-directory absprefix))
  16207. (opt org-format-latex-options)
  16208. (optnew org-format-latex-options)
  16209. (matchers (plist-get opt :matchers))
  16210. (re-list org-latex-regexps)
  16211. (org-format-latex-header-extra
  16212. (plist-get (org-infile-export-plist) :latex-header-extra))
  16213. (cnt 0) txt hash link beg end re e checkdir
  16214. string
  16215. m n block-type block linkfile movefile ov)
  16216. ;; Check the different regular expressions
  16217. (while (setq e (pop re-list))
  16218. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16219. block (if block-type "\n\n" ""))
  16220. (when (member m matchers)
  16221. (goto-char (point-min))
  16222. (while (re-search-forward re nil t)
  16223. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16224. (not (get-text-property (match-beginning n)
  16225. 'org-protected))
  16226. (or (not overlays)
  16227. (not (eq (get-char-property (match-beginning n)
  16228. 'org-overlay-type)
  16229. 'org-latex-overlay))))
  16230. (setq org-export-have-math t)
  16231. (cond
  16232. ((eq processing-type 'verbatim)
  16233. ;; Leave the text verbatim, just protect it
  16234. (add-text-properties (match-beginning n) (match-end n)
  16235. '(org-protected t)))
  16236. ((eq processing-type 'mathjax)
  16237. ;; Prepare for MathJax processing
  16238. (setq string (match-string n))
  16239. (if (member m '("$" "$1"))
  16240. (save-excursion
  16241. (delete-region (match-beginning n) (match-end n))
  16242. (goto-char (match-beginning n))
  16243. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16244. "\\)")
  16245. '(org-protected t))))
  16246. (add-text-properties (match-beginning n) (match-end n)
  16247. '(org-protected t))))
  16248. ((or (eq processing-type 'dvipng)
  16249. (eq processing-type 'imagemagick))
  16250. ;; Process to an image
  16251. (setq txt (match-string n)
  16252. beg (match-beginning n) end (match-end n)
  16253. cnt (1+ cnt))
  16254. (let ((face (face-at-point))
  16255. (fg (plist-get opt :foreground))
  16256. (bg (plist-get opt :background))
  16257. print-length print-level) ; make sure full list is printed
  16258. (when forbuffer
  16259. ; Get the colors from the face at point
  16260. (goto-char beg)
  16261. (when (eq fg 'auto)
  16262. (setq fg (face-attribute face :foreground nil 'default)))
  16263. (when (eq bg 'auto)
  16264. (setq bg (face-attribute face :background nil 'default)))
  16265. (setq optnew (copy-sequence opt))
  16266. (plist-put optnew :foreground fg)
  16267. (plist-put optnew :background bg))
  16268. (setq hash (sha1 (prin1-to-string
  16269. (list org-format-latex-header
  16270. org-format-latex-header-extra
  16271. org-export-latex-default-packages-alist
  16272. org-export-latex-packages-alist
  16273. org-format-latex-options
  16274. forbuffer txt fg bg)))
  16275. linkfile (format "%s_%s.png" prefix hash)
  16276. movefile (format "%s_%s.png" absprefix hash)))
  16277. (setq link (concat block "[[file:" linkfile "]]" block))
  16278. (if msg (message msg cnt))
  16279. (goto-char beg)
  16280. (unless checkdir ; make sure the directory exists
  16281. (setq checkdir t)
  16282. (or (file-directory-p todir) (make-directory todir t)))
  16283. (unless (file-exists-p movefile)
  16284. (org-create-formula-image
  16285. txt movefile optnew forbuffer processing-type))
  16286. (if overlays
  16287. (progn
  16288. (mapc (lambda (o)
  16289. (if (eq (overlay-get o 'org-overlay-type)
  16290. 'org-latex-overlay)
  16291. (delete-overlay o)))
  16292. (overlays-in beg end))
  16293. (setq ov (make-overlay beg end))
  16294. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16295. (if (featurep 'xemacs)
  16296. (progn
  16297. (overlay-put ov 'invisible t)
  16298. (overlay-put
  16299. ov 'end-glyph
  16300. (make-glyph (vector 'png :file movefile))))
  16301. (overlay-put
  16302. ov 'display
  16303. (list 'image :type 'png :file movefile :ascent 'center)))
  16304. (push ov org-latex-fragment-image-overlays)
  16305. (goto-char end))
  16306. (delete-region beg end)
  16307. (insert (org-add-props link
  16308. (list 'org-latex-src
  16309. (replace-regexp-in-string
  16310. "\"" "" txt)
  16311. 'org-latex-src-embed-type
  16312. (if block-type 'paragraph 'character))))))
  16313. ((eq processing-type 'mathml)
  16314. ;; Process to MathML
  16315. (unless (save-match-data (org-format-latex-mathml-available-p))
  16316. (error "LaTeX to MathML converter not configured"))
  16317. (setq txt (match-string n)
  16318. beg (match-beginning n) end (match-end n)
  16319. cnt (1+ cnt))
  16320. (if msg (message msg cnt))
  16321. (goto-char beg)
  16322. (delete-region beg end)
  16323. (insert (org-format-latex-as-mathml
  16324. txt block-type prefix dir)))
  16325. (t
  16326. (error "Unknown conversion type %s for latex fragments"
  16327. processing-type)))))))))
  16328. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16329. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16330. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16331. sucessful, return the portion between \"<math...> </math>\"
  16332. elements otherwise return nil. When MATHML-FILE is specified,
  16333. write the results in to that file. When invoked as an
  16334. interactive command, prompt for LATEX-FRAG, with initial value
  16335. set to the current active region and echo the results for user
  16336. inspection."
  16337. (interactive (list (let ((frag (when (org-region-active-p)
  16338. (buffer-substring-no-properties
  16339. (region-beginning) (region-end)))))
  16340. (read-string "LaTeX Fragment: " frag nil frag))))
  16341. (unless latex-frag (error "Invalid latex-frag"))
  16342. (let* ((tmp-in-file (file-relative-name
  16343. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16344. (ignore (write-region latex-frag nil tmp-in-file))
  16345. (tmp-out-file (file-relative-name
  16346. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16347. (cmd (format-spec
  16348. org-latex-to-mathml-convert-command
  16349. `((?j . ,(shell-quote-argument
  16350. (expand-file-name org-latex-to-mathml-jar-file)))
  16351. (?I . ,(shell-quote-argument tmp-in-file))
  16352. (?o . ,(shell-quote-argument tmp-out-file)))))
  16353. mathml shell-command-output)
  16354. (when (org-called-interactively-p 'any)
  16355. (unless (org-format-latex-mathml-available-p)
  16356. (error "LaTeX to MathML converter not configured")))
  16357. (message "Running %s" cmd)
  16358. (setq shell-command-output (shell-command-to-string cmd))
  16359. (setq mathml
  16360. (when (file-readable-p tmp-out-file)
  16361. (with-current-buffer (find-file-noselect tmp-out-file t)
  16362. (goto-char (point-min))
  16363. (when (re-search-forward
  16364. (concat
  16365. (regexp-quote
  16366. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16367. "\\(.\\|\n\\)*"
  16368. (regexp-quote "</math>")) nil t)
  16369. (prog1 (match-string 0) (kill-buffer))))))
  16370. (cond
  16371. (mathml
  16372. (setq mathml
  16373. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16374. (when mathml-file
  16375. (write-region mathml nil mathml-file))
  16376. (when (org-called-interactively-p 'any)
  16377. (message mathml)))
  16378. ((message "LaTeX to MathML conversion failed")
  16379. (message shell-command-output)))
  16380. (delete-file tmp-in-file)
  16381. (when (file-exists-p tmp-out-file)
  16382. (delete-file tmp-out-file))
  16383. mathml))
  16384. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16385. prefix &optional dir)
  16386. "Use `org-create-math-formula' but check local cache first."
  16387. (let* ((absprefix (expand-file-name prefix dir))
  16388. (print-length nil) (print-level nil)
  16389. (formula-id (concat
  16390. "formula-"
  16391. (sha1
  16392. (prin1-to-string
  16393. (list latex-frag
  16394. org-latex-to-mathml-convert-command)))))
  16395. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16396. (formula-cache-dir (file-name-directory formula-cache)))
  16397. (unless (file-directory-p formula-cache-dir)
  16398. (make-directory formula-cache-dir t))
  16399. (unless (file-exists-p formula-cache)
  16400. (org-create-math-formula latex-frag formula-cache))
  16401. (if (file-exists-p formula-cache)
  16402. ;; Successful conversion. Return the link to MathML file.
  16403. (org-add-props
  16404. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16405. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16406. 'org-latex-src-embed-type (if latex-frag-type
  16407. 'paragraph 'character)))
  16408. ;; Failed conversion. Return the LaTeX fragment verbatim
  16409. (add-text-properties
  16410. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16411. latex-frag)))
  16412. (defun org-create-formula-image (string tofile options buffer &optional type)
  16413. "Create an image from LaTeX source using dvipng or convert.
  16414. This function calls either `org-create-formula-image-with-dvipng'
  16415. or `org-create-formula-image-with-imagemagick' depending on the
  16416. value of `org-latex-create-formula-image-program' or on the value
  16417. of the optional TYPE variable.
  16418. Note: ultimately these two function should be combined as they
  16419. share a good deal of logic."
  16420. (org-check-external-command
  16421. "latex" "needed to convert LaTeX fragments to images")
  16422. (funcall
  16423. (case (or type org-latex-create-formula-image-program)
  16424. ('dvipng
  16425. (org-check-external-command
  16426. "dvipng" "needed to convert LaTeX fragments to images")
  16427. #'org-create-formula-image-with-dvipng)
  16428. ('imagemagick
  16429. (org-check-external-command
  16430. "convert" "you need to install imagemagick")
  16431. #'org-create-formula-image-with-imagemagick)
  16432. (t (error
  16433. "invalid value of `org-latex-create-formula-image-program'")))
  16434. string tofile options buffer))
  16435. ;; This function borrows from Ganesh Swami's latex2png.el
  16436. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16437. "This calls dvipng."
  16438. (require 'org-latex)
  16439. (let* ((tmpdir (if (featurep 'xemacs)
  16440. (temp-directory)
  16441. temporary-file-directory))
  16442. (texfilebase (make-temp-name
  16443. (expand-file-name "orgtex" tmpdir)))
  16444. (texfile (concat texfilebase ".tex"))
  16445. (dvifile (concat texfilebase ".dvi"))
  16446. (pngfile (concat texfilebase ".png"))
  16447. (fnh (if (featurep 'xemacs)
  16448. (font-height (face-font 'default))
  16449. (face-attribute 'default :height nil)))
  16450. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16451. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16452. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16453. "Black"))
  16454. (bg (or (plist-get options (if buffer :background :html-background))
  16455. "Transparent")))
  16456. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16457. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16458. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16459. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16460. (with-temp-file texfile
  16461. (insert (org-splice-latex-header
  16462. org-format-latex-header
  16463. org-export-latex-default-packages-alist
  16464. org-export-latex-packages-alist t
  16465. org-format-latex-header-extra))
  16466. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16467. (require 'org-latex)
  16468. (org-export-latex-fix-inputenc))
  16469. (let ((dir default-directory))
  16470. (condition-case nil
  16471. (progn
  16472. (cd tmpdir)
  16473. (call-process "latex" nil nil nil texfile))
  16474. (error nil))
  16475. (cd dir))
  16476. (if (not (file-exists-p dvifile))
  16477. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16478. (condition-case nil
  16479. (if (featurep 'xemacs)
  16480. (call-process "dvipng" nil nil nil
  16481. "-fg" fg "-bg" bg
  16482. "-T" "tight"
  16483. "-o" pngfile
  16484. dvifile)
  16485. (call-process "dvipng" nil nil nil
  16486. "-fg" fg "-bg" bg
  16487. "-D" dpi
  16488. ;;"-x" scale "-y" scale
  16489. "-T" "tight"
  16490. "-o" pngfile
  16491. dvifile))
  16492. (error nil))
  16493. (if (not (file-exists-p pngfile))
  16494. (if org-format-latex-signal-error
  16495. (error "Failed to create png file from %s" texfile)
  16496. (message "Failed to create png file from %s" texfile)
  16497. nil)
  16498. ;; Use the requested file name and clean up
  16499. (copy-file pngfile tofile 'replace)
  16500. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16501. (if (file-exists-p (concat texfilebase e))
  16502. (delete-file (concat texfilebase e))))
  16503. pngfile))))
  16504. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16505. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16506. "This calls convert, which is included into imagemagick."
  16507. (require 'org-latex)
  16508. (let* ((tmpdir (if (featurep 'xemacs)
  16509. (temp-directory)
  16510. temporary-file-directory))
  16511. (texfilebase (make-temp-name
  16512. (expand-file-name "orgtex" tmpdir)))
  16513. (texfile (concat texfilebase ".tex"))
  16514. (pdffile (concat texfilebase ".pdf"))
  16515. (pngfile (concat texfilebase ".png"))
  16516. (fnh (if (featurep 'xemacs)
  16517. (font-height (face-font 'default))
  16518. (face-attribute 'default :height nil)))
  16519. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16520. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16521. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16522. "black"))
  16523. (bg (or (plist-get options (if buffer :background :html-background))
  16524. "white")))
  16525. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16526. (setq fg (org-latex-color-format fg)))
  16527. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16528. (setq bg (org-latex-color-format
  16529. (if (string= bg "Transparent") "white" bg))))
  16530. (with-temp-file texfile
  16531. (insert (org-splice-latex-header
  16532. org-format-latex-header
  16533. org-export-latex-default-packages-alist
  16534. org-export-latex-packages-alist t
  16535. org-format-latex-header-extra))
  16536. (insert "\n\\begin{document}\n"
  16537. "\\definecolor{fg}{rgb}{" fg "}\n"
  16538. "\\definecolor{bg}{rgb}{" bg "}\n"
  16539. "\n\\pagecolor{bg}\n"
  16540. "\n{\\color{fg}\n"
  16541. string
  16542. "\n}\n"
  16543. "\n\\end{document}\n" )
  16544. (require 'org-latex)
  16545. (org-export-latex-fix-inputenc))
  16546. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16547. (condition-case nil
  16548. (progn
  16549. (cd tmpdir)
  16550. (setq cmds org-latex-to-pdf-process)
  16551. (while cmds
  16552. (setq latex-frags-cmds (pop cmds))
  16553. (if (listp latex-frags-cmds)
  16554. (setq cmds nil)
  16555. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16556. (while latex-frags-cmds
  16557. (setq cmd (pop latex-frags-cmds))
  16558. (while (string-match "%b" cmd)
  16559. (setq cmd (replace-match
  16560. (save-match-data
  16561. (shell-quote-argument texfile))
  16562. t t cmd)))
  16563. (while (string-match "%f" cmd)
  16564. (setq cmd (replace-match
  16565. (save-match-data
  16566. (shell-quote-argument (file-name-nondirectory texfile)))
  16567. t t cmd)))
  16568. (while (string-match "%o" cmd)
  16569. (setq cmd (replace-match
  16570. (save-match-data
  16571. (shell-quote-argument (file-name-directory texfile)))
  16572. t t cmd)))
  16573. (setq cmd (split-string cmd))
  16574. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16575. (error nil))
  16576. (cd dir))
  16577. (if (not (file-exists-p pdffile))
  16578. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16579. (condition-case nil
  16580. (if (featurep 'xemacs)
  16581. (call-process "convert" nil nil nil
  16582. "-density" "96"
  16583. "-trim"
  16584. "-antialias"
  16585. pdffile
  16586. "-quality" "100"
  16587. ;; "-sharpen" "0x1.0"
  16588. pngfile)
  16589. (call-process "convert" nil nil nil
  16590. "-density" dpi
  16591. "-trim"
  16592. "-antialias"
  16593. pdffile
  16594. "-quality" "100"
  16595. ; "-sharpen" "0x1.0"
  16596. pngfile))
  16597. (error nil))
  16598. (if (not (file-exists-p pngfile))
  16599. (if org-format-latex-signal-error
  16600. (error "Failed to create png file from %s" texfile)
  16601. (message "Failed to create png file from %s" texfile)
  16602. nil)
  16603. ;; Use the requested file name and clean up
  16604. (copy-file pngfile tofile 'replace)
  16605. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16606. (if (file-exists-p (concat texfilebase e))
  16607. (delete-file (concat texfilebase e))))
  16608. pngfile))))
  16609. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16610. "Fill a LaTeX header template TPL.
  16611. In the template, the following place holders will be recognized:
  16612. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16613. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16614. [PACKAGES] \\usepackage statements for PKG
  16615. [NO-PACKAGES] do not include PKG
  16616. [EXTRA] the string EXTRA
  16617. [NO-EXTRA] do not include EXTRA
  16618. For backward compatibility, if both the positive and the negative place
  16619. holder is missing, the positive one (without the \"NO-\") will be
  16620. assumed to be present at the end of the template.
  16621. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16622. EXTRA is a string.
  16623. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16624. (let (rpl (end ""))
  16625. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16626. (setq rpl (if (or (match-end 1) (not def-pkg))
  16627. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16628. tpl (replace-match rpl t t tpl))
  16629. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16630. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16631. (setq rpl (if (or (match-end 1) (not pkg))
  16632. "" (org-latex-packages-to-string pkg snippets-p t))
  16633. tpl (replace-match rpl t t tpl))
  16634. (if pkg (setq end
  16635. (concat end "\n"
  16636. (org-latex-packages-to-string pkg snippets-p)))))
  16637. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16638. (setq rpl (if (or (match-end 1) (not extra))
  16639. "" (concat extra "\n"))
  16640. tpl (replace-match rpl t t tpl))
  16641. (if (and extra (string-match "\\S-" extra))
  16642. (setq end (concat end "\n" extra))))
  16643. (if (string-match "\\S-" end)
  16644. (concat tpl "\n" end)
  16645. tpl)))
  16646. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16647. "Turn an alist of packages into a string with the \\usepackage macros."
  16648. (setq pkg (mapconcat (lambda(p)
  16649. (cond
  16650. ((stringp p) p)
  16651. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16652. (format "%% Package %s omitted" (cadr p)))
  16653. ((equal "" (car p))
  16654. (format "\\usepackage{%s}" (cadr p)))
  16655. (t
  16656. (format "\\usepackage[%s]{%s}"
  16657. (car p) (cadr p)))))
  16658. pkg
  16659. "\n"))
  16660. (if newline (concat pkg "\n") pkg))
  16661. (defun org-dvipng-color (attr)
  16662. "Return a RGB color specification for dvipng."
  16663. (apply 'format "rgb %s %s %s"
  16664. (mapcar 'org-normalize-color
  16665. (if (featurep 'xemacs)
  16666. (color-rgb-components
  16667. (face-property 'default
  16668. (cond ((eq attr :foreground) 'foreground)
  16669. ((eq attr :background) 'background))))
  16670. (color-values (face-attribute 'default attr nil))))))
  16671. (defun org-dvipng-color-format (color-name)
  16672. "Convert COLOR-NAME to a RGB color value for dvipng."
  16673. (apply 'format "rgb %s %s %s"
  16674. (mapcar 'org-normalize-color
  16675. (color-values color-name))))
  16676. (defun org-latex-color (attr)
  16677. "Return a RGB color for the LaTeX color package."
  16678. (apply 'format "%s,%s,%s"
  16679. (mapcar 'org-normalize-color
  16680. (if (featurep 'xemacs)
  16681. (color-rgb-components
  16682. (face-property 'default
  16683. (cond ((eq attr :foreground) 'foreground)
  16684. ((eq attr :background) 'background))))
  16685. (color-values (face-attribute 'default attr nil))))))
  16686. (defun org-latex-color-format (color-name)
  16687. "Convert COLOR-NAME to a RGB color value."
  16688. (apply 'format "%s,%s,%s"
  16689. (mapcar 'org-normalize-color
  16690. (color-values color-name))))
  16691. (defun org-normalize-color (value)
  16692. "Return string to be used as color value for an RGB component."
  16693. (format "%g" (/ value 65535.0)))
  16694. ;; Image display
  16695. (defvar org-inline-image-overlays nil)
  16696. (make-variable-buffer-local 'org-inline-image-overlays)
  16697. (defun org-toggle-inline-images (&optional include-linked)
  16698. "Toggle the display of inline images.
  16699. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16700. (interactive "P")
  16701. (if org-inline-image-overlays
  16702. (progn
  16703. (org-remove-inline-images)
  16704. (message "Inline image display turned off"))
  16705. (org-display-inline-images include-linked)
  16706. (if (and (org-called-interactively-p)
  16707. org-inline-image-overlays)
  16708. (message "%d images displayed inline"
  16709. (length org-inline-image-overlays))
  16710. (message "No images to display inline"))))
  16711. (defun org-redisplay-inline-images ()
  16712. "Refresh the display of inline images."
  16713. (interactive)
  16714. (if (not org-inline-image-overlays)
  16715. (org-toggle-inline-images)
  16716. (org-toggle-inline-images)
  16717. (org-toggle-inline-images)))
  16718. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16719. "Display inline images.
  16720. Normally only links without a description part are inlined, because this
  16721. is how it will work for export. When INCLUDE-LINKED is set, also links
  16722. with a description part will be inlined. This can be nice for a quick
  16723. look at those images, but it does not reflect what exported files will look
  16724. like.
  16725. When REFRESH is set, refresh existing images between BEG and END.
  16726. This will create new image displays only if necessary.
  16727. BEG and END default to the buffer boundaries."
  16728. (interactive "P")
  16729. (unless refresh
  16730. (org-remove-inline-images)
  16731. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16732. (save-excursion
  16733. (save-restriction
  16734. (widen)
  16735. (setq beg (or beg (point-min)) end (or end (point-max)))
  16736. (goto-char beg)
  16737. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16738. (substring (org-image-file-name-regexp) 0 -2)
  16739. "\\)\\]" (if include-linked "" "\\]")))
  16740. old file ov img type attrwidth width)
  16741. (while (re-search-forward re end t)
  16742. (setq old (get-char-property-and-overlay (match-beginning 1)
  16743. 'org-image-overlay)
  16744. file (expand-file-name
  16745. (concat (or (match-string 3) "") (match-string 4))))
  16746. (when (image-type-available-p 'imagemagick)
  16747. (setq attrwidth (if (or (listp org-image-actual-width)
  16748. (null org-image-actual-width))
  16749. (save-excursion
  16750. (save-match-data
  16751. (when (re-search-backward
  16752. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16753. (save-excursion
  16754. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16755. (string-to-number (match-string 1))))))
  16756. width (cond ((eq org-image-actual-width t) nil)
  16757. ((null org-image-actual-width) attrwidth)
  16758. ((numberp org-image-actual-width)
  16759. org-image-actual-width)
  16760. ((listp org-image-actual-width)
  16761. (or attrwidth (car org-image-actual-width))))
  16762. type (if width 'imagemagick)))
  16763. (when (file-exists-p file)
  16764. (if (and (car-safe old) refresh)
  16765. (image-refresh (overlay-get (cdr old) 'display))
  16766. (setq img (save-match-data (create-image file type nil :width width)))
  16767. (when img
  16768. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16769. (overlay-put ov 'display img)
  16770. (overlay-put ov 'face 'default)
  16771. (overlay-put ov 'org-image-overlay t)
  16772. (overlay-put ov 'modification-hooks
  16773. (list 'org-display-inline-remove-overlay))
  16774. (push ov org-inline-image-overlays)))))))))
  16775. (define-obsolete-function-alias
  16776. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16777. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16778. "Remove inline-display overlay if a corresponding region is modified."
  16779. (let ((inhibit-modification-hooks t))
  16780. (when (and ov after)
  16781. (delete ov org-inline-image-overlays)
  16782. (delete-overlay ov))))
  16783. (defun org-remove-inline-images ()
  16784. "Remove inline display of images."
  16785. (interactive)
  16786. (mapc 'delete-overlay org-inline-image-overlays)
  16787. (setq org-inline-image-overlays nil))
  16788. ;;;; Key bindings
  16789. ;; Outline functions from `outline-mode-prefix-map'
  16790. ;; that can be remapped in Org:
  16791. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16792. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16793. (define-key org-mode-map [remap outline-forward-same-level]
  16794. 'org-forward-heading-same-level)
  16795. (define-key org-mode-map [remap outline-backward-same-level]
  16796. 'org-backward-heading-same-level)
  16797. (define-key org-mode-map [remap show-branches]
  16798. 'org-kill-note-or-show-branches)
  16799. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16800. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16801. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16802. ;; Outline functions from `outline-mode-prefix-map' that can not
  16803. ;; be remapped in Org:
  16804. ;;
  16805. ;; - the column "key binding" shows whether the Outline function is still
  16806. ;; available in Org mode on the same key that it has been bound to in
  16807. ;; Outline mode:
  16808. ;; - "overridden": key used for a different functionality in Org mode
  16809. ;; - else: key still bound to the same Outline function in Org mode
  16810. ;;
  16811. ;; | Outline function | key binding | Org replacement |
  16812. ;; |------------------------------------+-------------+-----------------------|
  16813. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16814. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16815. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16816. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16817. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16818. ;; | `show-entry' | overridden | no replacement |
  16819. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16820. ;; | `show-branches' | `C-c C-k' | still same function |
  16821. ;; | `show-subtree' | overridden | visibility cycling |
  16822. ;; | `show-all' | overridden | no replacement |
  16823. ;; | `hide-subtree' | overridden | visibility cycling |
  16824. ;; | `hide-body' | overridden | no replacement |
  16825. ;; | `hide-entry' | overridden | visibility cycling |
  16826. ;; | `hide-leaves' | overridden | no replacement |
  16827. ;; | `hide-sublevels' | overridden | no replacement |
  16828. ;; | `hide-other' | overridden | no replacement |
  16829. ;; Make `C-c C-x' a prefix key
  16830. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16831. ;; TAB key with modifiers
  16832. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16833. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16834. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16835. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16836. ;; The following line is necessary under Suse GNU/Linux
  16837. (unless (featurep 'xemacs)
  16838. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16839. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16840. (define-key org-mode-map [backtab] 'org-shifttab)
  16841. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16842. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16843. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16844. ;; Cursor keys with modifiers
  16845. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16846. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16847. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16848. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16849. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16850. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16851. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16852. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16853. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16854. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16855. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16856. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16857. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16858. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16859. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16860. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16861. ;; Babel keys
  16862. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16863. (mapc (lambda (pair)
  16864. (define-key org-babel-map (car pair) (cdr pair)))
  16865. org-babel-key-bindings)
  16866. ;;; Extra keys for tty access.
  16867. ;; We only set them when really needed because otherwise the
  16868. ;; menus don't show the simple keys
  16869. (when (or org-use-extra-keys
  16870. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16871. (not window-system))
  16872. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16873. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16874. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16875. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16876. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16877. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16878. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16879. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16880. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16881. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16882. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16883. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16884. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16885. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16886. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16887. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16888. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16889. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16890. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16891. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16892. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16893. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16894. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16895. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16896. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16897. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16898. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16899. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16900. ;; All the other keys
  16901. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16902. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16903. (if (boundp 'narrow-map)
  16904. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16905. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16906. (if (boundp 'narrow-map)
  16907. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16908. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16909. (if (boundp 'narrow-map)
  16910. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16911. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16912. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16913. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16914. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16915. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16916. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16917. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16918. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16919. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16920. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16921. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16922. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16923. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16924. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16925. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16926. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16927. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16928. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16929. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16930. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16931. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16932. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16933. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16934. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16935. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16936. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16937. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16938. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16939. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16940. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16941. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16942. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16943. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16944. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16945. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16946. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16947. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16948. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16949. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16950. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16951. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16952. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16953. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16954. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16955. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16956. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16957. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16958. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16959. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16960. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16961. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16962. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16963. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16964. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16965. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16966. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16967. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16968. (org-defkey org-mode-map "\C-m" 'org-return)
  16969. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16970. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16971. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16972. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16973. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16974. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16975. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16976. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16977. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16978. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16979. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16980. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16981. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16982. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16983. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16984. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16985. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16986. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16987. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16988. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16989. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16990. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16991. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16992. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16993. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16994. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16995. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16996. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16997. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16998. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16999. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17000. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17001. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17002. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17003. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17004. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17005. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17006. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17007. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17008. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17009. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17010. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17011. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17012. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17013. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17014. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17015. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17016. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17017. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17018. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17019. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17020. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17021. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17022. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17023. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17024. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17025. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17026. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17027. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17028. (when (featurep 'xemacs)
  17029. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17030. (defconst org-speed-commands-default
  17031. '(
  17032. ("Outline Navigation")
  17033. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17034. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17035. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17036. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17037. ("u" . (org-speed-move-safe 'outline-up-heading))
  17038. ("j" . org-goto)
  17039. ("g" . (org-refile t))
  17040. ("Outline Visibility")
  17041. ("c" . org-cycle)
  17042. ("C" . org-shifttab)
  17043. (" " . org-display-outline-path)
  17044. ("=" . org-columns)
  17045. ("Outline Structure Editing")
  17046. ("U" . org-shiftmetaup)
  17047. ("D" . org-shiftmetadown)
  17048. ("r" . org-metaright)
  17049. ("l" . org-metaleft)
  17050. ("R" . org-shiftmetaright)
  17051. ("L" . org-shiftmetaleft)
  17052. ("i" . (progn (forward-char 1) (call-interactively
  17053. 'org-insert-heading-respect-content)))
  17054. ("^" . org-sort)
  17055. ("w" . org-refile)
  17056. ("a" . org-archive-subtree-default-with-confirmation)
  17057. ("." . org-mark-subtree)
  17058. ("#" . org-toggle-comment)
  17059. ("Clock Commands")
  17060. ("I" . org-clock-in)
  17061. ("O" . org-clock-out)
  17062. ("Meta Data Editing")
  17063. ("t" . org-todo)
  17064. ("," . (org-priority))
  17065. ("0" . (org-priority ?\ ))
  17066. ("1" . (org-priority ?A))
  17067. ("2" . (org-priority ?B))
  17068. ("3" . (org-priority ?C))
  17069. (":" . org-set-tags-command)
  17070. ("e" . org-set-effort)
  17071. ("E" . org-inc-effort)
  17072. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17073. (org-entry-put (point) "APPT_WARNTIME" m)))
  17074. ("Agenda Views etc")
  17075. ("v" . org-agenda)
  17076. ("/" . org-sparse-tree)
  17077. ("Misc")
  17078. ("o" . org-open-at-point)
  17079. ("?" . org-speed-command-help)
  17080. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17081. (">" . (org-agenda-remove-restriction-lock))
  17082. )
  17083. "The default speed commands.")
  17084. (defun org-print-speed-command (e)
  17085. (if (> (length (car e)) 1)
  17086. (progn
  17087. (princ "\n")
  17088. (princ (car e))
  17089. (princ "\n")
  17090. (princ (make-string (length (car e)) ?-))
  17091. (princ "\n"))
  17092. (princ (car e))
  17093. (princ " ")
  17094. (if (symbolp (cdr e))
  17095. (princ (symbol-name (cdr e)))
  17096. (prin1 (cdr e)))
  17097. (princ "\n")))
  17098. (defun org-speed-command-help ()
  17099. "Show the available speed commands."
  17100. (interactive)
  17101. (if (not org-use-speed-commands)
  17102. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17103. (with-output-to-temp-buffer "*Help*"
  17104. (princ "User-defined Speed commands\n===========================\n")
  17105. (mapc 'org-print-speed-command org-speed-commands-user)
  17106. (princ "\n")
  17107. (princ "Built-in Speed commands\n=======================\n")
  17108. (mapc 'org-print-speed-command org-speed-commands-default))
  17109. (with-current-buffer "*Help*"
  17110. (setq truncate-lines t))))
  17111. (defun org-speed-move-safe (cmd)
  17112. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17113. If not, return to the original position and throw an error."
  17114. (interactive)
  17115. (let ((pos (point)))
  17116. (call-interactively cmd)
  17117. (unless (and (bolp) (org-at-heading-p))
  17118. (goto-char pos)
  17119. (error "Boundary reached while executing %s" cmd))))
  17120. (defvar org-self-insert-command-undo-counter 0)
  17121. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17122. (defvar org-speed-command nil)
  17123. (define-obsolete-function-alias
  17124. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17125. (defun org-speed-command-activate (keys)
  17126. "Hook for activating single-letter speed commands.
  17127. `org-speed-commands-default' specifies a minimal command set.
  17128. Use `org-speed-commands-user' for further customization."
  17129. (when (or (and (bolp) (looking-at org-outline-regexp))
  17130. (and (functionp org-use-speed-commands)
  17131. (funcall org-use-speed-commands)))
  17132. (cdr (assoc keys (append org-speed-commands-user
  17133. org-speed-commands-default)))))
  17134. (define-obsolete-function-alias
  17135. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17136. (defun org-babel-speed-command-activate (keys)
  17137. "Hook for activating single-letter code block commands."
  17138. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17139. (cdr (assoc keys org-babel-key-bindings))))
  17140. (defcustom org-speed-command-hook
  17141. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17142. "Hook for activating speed commands at strategic locations.
  17143. Hook functions are called in sequence until a valid handler is
  17144. found.
  17145. Each hook takes a single argument, a user-pressed command key
  17146. which is also a `self-insert-command' from the global map.
  17147. Within the hook, examine the cursor position and the command key
  17148. and return nil or a valid handler as appropriate. Handler could
  17149. be one of an interactive command, a function, or a form.
  17150. Set `org-use-speed-commands' to non-nil value to enable this
  17151. hook. The default setting is `org-speed-command-activate'."
  17152. :group 'org-structure
  17153. :version "24.1"
  17154. :type 'hook)
  17155. (defun org-self-insert-command (N)
  17156. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17157. If the cursor is in a table looking at whitespace, the whitespace is
  17158. overwritten, and the table is not marked as requiring realignment."
  17159. (interactive "p")
  17160. (org-check-before-invisible-edit 'insert)
  17161. (cond
  17162. ((and org-use-speed-commands
  17163. (setq org-speed-command
  17164. (run-hook-with-args-until-success
  17165. 'org-speed-command-hook (this-command-keys))))
  17166. (cond
  17167. ((commandp org-speed-command)
  17168. (setq this-command org-speed-command)
  17169. (call-interactively org-speed-command))
  17170. ((functionp org-speed-command)
  17171. (funcall org-speed-command))
  17172. ((and org-speed-command (listp org-speed-command))
  17173. (eval org-speed-command))
  17174. (t (let (org-use-speed-commands)
  17175. (call-interactively 'org-self-insert-command)))))
  17176. ((and
  17177. (org-table-p)
  17178. (progn
  17179. ;; check if we blank the field, and if that triggers align
  17180. (and (featurep 'org-table) org-table-auto-blank-field
  17181. (member last-command
  17182. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17183. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17184. ;; got extra space, this field does not determine column width
  17185. (let (org-table-may-need-update) (org-table-blank-field))
  17186. ;; no extra space, this field may determine column width
  17187. (org-table-blank-field)))
  17188. t)
  17189. (eq N 1)
  17190. (looking-at "[^|\n]* |"))
  17191. (let (org-table-may-need-update)
  17192. (goto-char (1- (match-end 0)))
  17193. (backward-delete-char 1)
  17194. (goto-char (match-beginning 0))
  17195. (self-insert-command N)))
  17196. (t
  17197. (setq org-table-may-need-update t)
  17198. (self-insert-command N)
  17199. (org-fix-tags-on-the-fly)
  17200. (if org-self-insert-cluster-for-undo
  17201. (if (not (eq last-command 'org-self-insert-command))
  17202. (setq org-self-insert-command-undo-counter 1)
  17203. (if (>= org-self-insert-command-undo-counter 20)
  17204. (setq org-self-insert-command-undo-counter 1)
  17205. (and (> org-self-insert-command-undo-counter 0)
  17206. buffer-undo-list (listp buffer-undo-list)
  17207. (not (cadr buffer-undo-list)) ; remove nil entry
  17208. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17209. (setq org-self-insert-command-undo-counter
  17210. (1+ org-self-insert-command-undo-counter))))))))
  17211. (defun org-check-before-invisible-edit (kind)
  17212. "Check is editing if kind KIND would be dangerous with invisible text around.
  17213. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17214. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17215. (if (and org-catch-invisible-edits
  17216. (or (not (boundp 'visible-mode)) (not visible-mode))
  17217. (or (get-char-property (point) 'invisible)
  17218. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17219. ;; OK, we need to take a closer look
  17220. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17221. (invisible-before-point (if (bobp) nil (get-char-property
  17222. (1- (point)) 'invisible)))
  17223. (border-and-ok-direction
  17224. (or
  17225. ;; Check if we are acting predictably before invisible text
  17226. (and invisible-at-point (not invisible-before-point)
  17227. (memq kind '(insert delete-backward)))
  17228. ;; Check if we are acting predictably after invisible text
  17229. ;; This works not well, and I have turned it off. It seems
  17230. ;; better to always show and stop after invisible text.
  17231. ;; (and (not invisible-at-point) invisible-before-point
  17232. ;; (memq kind '(insert delete)))
  17233. )))
  17234. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17235. (memq invisible-before-point '(outline org-hide-block t)))
  17236. (if (eq org-catch-invisible-edits 'error)
  17237. (error "Editing in invisible areas is prohibited - make visible first"))
  17238. (if (and org-custom-properties-overlays
  17239. (y-or-n-p "Display invisible properties in this buffer? "))
  17240. (org-toggle-custom-properties-visibility)
  17241. ;; Make the area visible
  17242. (save-excursion
  17243. (if invisible-before-point
  17244. (goto-char (previous-single-char-property-change
  17245. (point) 'invisible)))
  17246. (org-cycle))
  17247. (cond
  17248. ((eq org-catch-invisible-edits 'show)
  17249. ;; That's it, we do the edit after showing
  17250. (message
  17251. "Unfolding invisible region around point before editing")
  17252. (sit-for 1))
  17253. ((and (eq org-catch-invisible-edits 'smart)
  17254. border-and-ok-direction)
  17255. (message "Unfolding invisible region around point before editing"))
  17256. (t
  17257. ;; Don't do the edit, make the user repeat it in full visibility
  17258. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17259. (defun org-fix-tags-on-the-fly ()
  17260. (when (and (equal (char-after (point-at-bol)) ?*)
  17261. (org-at-heading-p))
  17262. (org-align-tags-here org-tags-column)))
  17263. (defun org-delete-backward-char (N)
  17264. "Like `delete-backward-char', insert whitespace at field end in tables.
  17265. When deleting backwards, in tables this function will insert whitespace in
  17266. front of the next \"|\" separator, to keep the table aligned. The table will
  17267. still be marked for re-alignment if the field did fill the entire column,
  17268. because, in this case the deletion might narrow the column."
  17269. (interactive "p")
  17270. (save-match-data
  17271. (org-check-before-invisible-edit 'delete-backward)
  17272. (if (and (org-table-p)
  17273. (eq N 1)
  17274. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17275. (looking-at ".*?|"))
  17276. (let ((pos (point))
  17277. (noalign (looking-at "[^|\n\r]* |"))
  17278. (c org-table-may-need-update))
  17279. (backward-delete-char N)
  17280. (if (not overwrite-mode)
  17281. (progn
  17282. (skip-chars-forward "^|")
  17283. (insert " ")
  17284. (goto-char (1- pos))))
  17285. ;; noalign: if there were two spaces at the end, this field
  17286. ;; does not determine the width of the column.
  17287. (if noalign (setq org-table-may-need-update c)))
  17288. (backward-delete-char N)
  17289. (org-fix-tags-on-the-fly))))
  17290. (defun org-delete-char (N)
  17291. "Like `delete-char', but insert whitespace at field end in tables.
  17292. When deleting characters, in tables this function will insert whitespace in
  17293. front of the next \"|\" separator, to keep the table aligned. The table will
  17294. still be marked for re-alignment if the field did fill the entire column,
  17295. because, in this case the deletion might narrow the column."
  17296. (interactive "p")
  17297. (save-match-data
  17298. (org-check-before-invisible-edit 'delete)
  17299. (if (and (org-table-p)
  17300. (not (bolp))
  17301. (not (= (char-after) ?|))
  17302. (eq N 1))
  17303. (if (looking-at ".*?|")
  17304. (let ((pos (point))
  17305. (noalign (looking-at "[^|\n\r]* |"))
  17306. (c org-table-may-need-update))
  17307. (replace-match (concat
  17308. (substring (match-string 0) 1 -1)
  17309. " |"))
  17310. (goto-char pos)
  17311. ;; noalign: if there were two spaces at the end, this field
  17312. ;; does not determine the width of the column.
  17313. (if noalign (setq org-table-may-need-update c)))
  17314. (delete-char N))
  17315. (delete-char N)
  17316. (org-fix-tags-on-the-fly))))
  17317. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17318. (put 'org-self-insert-command 'delete-selection t)
  17319. (put 'orgtbl-self-insert-command 'delete-selection t)
  17320. (put 'org-delete-char 'delete-selection 'supersede)
  17321. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17322. (put 'org-yank 'delete-selection 'yank)
  17323. ;; Make `flyspell-mode' delay after some commands
  17324. (put 'org-self-insert-command 'flyspell-delayed t)
  17325. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17326. (put 'org-delete-char 'flyspell-delayed t)
  17327. (put 'org-delete-backward-char 'flyspell-delayed t)
  17328. ;; Make pabbrev-mode expand after org-mode commands
  17329. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17330. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17331. ;; How to do this: Measure non-white length of current string
  17332. ;; If equal to column width, we should realign.
  17333. (defun org-remap (map &rest commands)
  17334. "In MAP, remap the functions given in COMMANDS.
  17335. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17336. (let (new old)
  17337. (while commands
  17338. (setq old (pop commands) new (pop commands))
  17339. (if (fboundp 'command-remapping)
  17340. (org-defkey map (vector 'remap old) new)
  17341. (substitute-key-definition old new map global-map)))))
  17342. (when (eq org-enable-table-editor 'optimized)
  17343. ;; If the user wants maximum table support, we need to hijack
  17344. ;; some standard editing functions
  17345. (org-remap org-mode-map
  17346. 'self-insert-command 'org-self-insert-command
  17347. 'delete-char 'org-delete-char
  17348. 'delete-backward-char 'org-delete-backward-char)
  17349. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17350. (defvar org-ctrl-c-ctrl-c-hook nil
  17351. "Hook for functions attaching themselves to `C-c C-c'.
  17352. This can be used to add additional functionality to the C-c C-c
  17353. key which executes context-dependent commands. This hook is run
  17354. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17355. run after the last test.
  17356. Each function will be called with no arguments. The function
  17357. must check if the context is appropriate for it to act. If yes,
  17358. it should do its thing and then return a non-nil value. If the
  17359. context is wrong, just do nothing and return nil.")
  17360. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17361. "Hook for functions attaching themselves to `C-c C-c'.
  17362. This can be used to add additional functionality to the C-c C-c
  17363. key which executes context-dependent commands. This hook is run
  17364. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17365. before the first test.
  17366. Each function will be called with no arguments. The function
  17367. must check if the context is appropriate for it to act. If yes,
  17368. it should do its thing and then return a non-nil value. If the
  17369. context is wrong, just do nothing and return nil.")
  17370. (defvar org-tab-first-hook nil
  17371. "Hook for functions to attach themselves to TAB.
  17372. See `org-ctrl-c-ctrl-c-hook' for more information.
  17373. This hook runs as the first action when TAB is pressed, even before
  17374. `org-cycle' messes around with the `outline-regexp' to cater for
  17375. inline tasks and plain list item folding.
  17376. If any function in this hook returns t, any other actions that
  17377. would have been caused by TAB (such as table field motion or visibility
  17378. cycling) will not occur.")
  17379. (defvar org-tab-after-check-for-table-hook nil
  17380. "Hook for functions to attach themselves to TAB.
  17381. See `org-ctrl-c-ctrl-c-hook' for more information.
  17382. This hook runs after it has been established that the cursor is not in a
  17383. table, but before checking if the cursor is in a headline or if global cycling
  17384. should be done.
  17385. If any function in this hook returns t, not other actions like visibility
  17386. cycling will be done.")
  17387. (defvar org-tab-after-check-for-cycling-hook nil
  17388. "Hook for functions to attach themselves to TAB.
  17389. See `org-ctrl-c-ctrl-c-hook' for more information.
  17390. This hook runs after it has been established that not table field motion and
  17391. not visibility should be done because of current context. This is probably
  17392. the place where a package like yasnippets can hook in.")
  17393. (defvar org-tab-before-tab-emulation-hook nil
  17394. "Hook for functions to attach themselves to TAB.
  17395. See `org-ctrl-c-ctrl-c-hook' for more information.
  17396. This hook runs after every other options for TAB have been exhausted, but
  17397. before indentation and \t insertion takes place.")
  17398. (defvar org-metaleft-hook nil
  17399. "Hook for functions attaching themselves to `M-left'.
  17400. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17401. (defvar org-metaright-hook nil
  17402. "Hook for functions attaching themselves to `M-right'.
  17403. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17404. (defvar org-metaup-hook nil
  17405. "Hook for functions attaching themselves to `M-up'.
  17406. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17407. (defvar org-metadown-hook nil
  17408. "Hook for functions attaching themselves to `M-down'.
  17409. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17410. (defvar org-shiftmetaleft-hook nil
  17411. "Hook for functions attaching themselves to `M-S-left'.
  17412. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17413. (defvar org-shiftmetaright-hook nil
  17414. "Hook for functions attaching themselves to `M-S-right'.
  17415. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17416. (defvar org-shiftmetaup-hook nil
  17417. "Hook for functions attaching themselves to `M-S-up'.
  17418. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17419. (defvar org-shiftmetadown-hook nil
  17420. "Hook for functions attaching themselves to `M-S-down'.
  17421. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17422. (defvar org-metareturn-hook nil
  17423. "Hook for functions attaching themselves to `M-RET'.
  17424. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17425. (defvar org-shiftup-hook nil
  17426. "Hook for functions attaching themselves to `S-up'.
  17427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17428. (defvar org-shiftup-final-hook nil
  17429. "Hook for functions attaching themselves to `S-up'.
  17430. This one runs after all other options except shift-select have been excluded.
  17431. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17432. (defvar org-shiftdown-hook nil
  17433. "Hook for functions attaching themselves to `S-down'.
  17434. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17435. (defvar org-shiftdown-final-hook nil
  17436. "Hook for functions attaching themselves to `S-down'.
  17437. This one runs after all other options except shift-select have been excluded.
  17438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17439. (defvar org-shiftleft-hook nil
  17440. "Hook for functions attaching themselves to `S-left'.
  17441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17442. (defvar org-shiftleft-final-hook nil
  17443. "Hook for functions attaching themselves to `S-left'.
  17444. This one runs after all other options except shift-select have been excluded.
  17445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17446. (defvar org-shiftright-hook nil
  17447. "Hook for functions attaching themselves to `S-right'.
  17448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17449. (defvar org-shiftright-final-hook nil
  17450. "Hook for functions attaching themselves to `S-right'.
  17451. This one runs after all other options except shift-select have been excluded.
  17452. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17453. (defun org-modifier-cursor-error ()
  17454. "Throw an error, a modified cursor command was applied in wrong context."
  17455. (error "This command is active in special context like tables, headlines or items"))
  17456. (defun org-shiftselect-error ()
  17457. "Throw an error because Shift-Cursor command was applied in wrong context."
  17458. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17459. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17460. (error "This command works only in special context like headlines or timestamps")))
  17461. (defun org-call-for-shift-select (cmd)
  17462. (let ((this-command-keys-shift-translated t))
  17463. (call-interactively cmd)))
  17464. (defun org-shifttab (&optional arg)
  17465. "Global visibility cycling or move to previous table field.
  17466. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17467. on context.
  17468. See the individual commands for more information."
  17469. (interactive "P")
  17470. (cond
  17471. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17472. ((integerp arg)
  17473. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17474. (message "Content view to level: %d" arg)
  17475. (org-content (prefix-numeric-value arg2))
  17476. (setq org-cycle-global-status 'overview)))
  17477. (t (call-interactively 'org-global-cycle))))
  17478. (defun org-shiftmetaleft ()
  17479. "Promote subtree or delete table column.
  17480. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17481. `org-table-delete-column', depending on context. See the
  17482. individual commands for more information."
  17483. (interactive)
  17484. (cond
  17485. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17486. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17487. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17488. ((if (not (org-region-active-p)) (org-at-item-p)
  17489. (save-excursion (goto-char (region-beginning))
  17490. (org-at-item-p)))
  17491. (call-interactively 'org-outdent-item-tree))
  17492. (t (org-modifier-cursor-error))))
  17493. (defun org-shiftmetaright ()
  17494. "Demote subtree or insert table column.
  17495. Calls `org-demote-subtree', `org-indent-item-tree', or
  17496. `org-table-insert-column', depending on context. See the
  17497. individual commands for more information."
  17498. (interactive)
  17499. (cond
  17500. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17501. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17502. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17503. ((if (not (org-region-active-p)) (org-at-item-p)
  17504. (save-excursion (goto-char (region-beginning))
  17505. (org-at-item-p)))
  17506. (call-interactively 'org-indent-item-tree))
  17507. (t (org-modifier-cursor-error))))
  17508. (defun org-shiftmetaup (&optional arg)
  17509. "Move subtree up or kill table row.
  17510. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17511. `org-move-item-up' or `org-timestamp-up', depending on context.
  17512. See the individual commands for more information."
  17513. (interactive "P")
  17514. (cond
  17515. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17516. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17517. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17518. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17519. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17520. (call-interactively 'org-timestamp-up)))
  17521. (t (org-modifier-cursor-error))))
  17522. (defun org-shiftmetadown (&optional arg)
  17523. "Move subtree down or insert table row.
  17524. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17525. `org-move-item-down' or `org-timestamp-up', depending on context.
  17526. See the individual commands for more information."
  17527. (interactive "P")
  17528. (cond
  17529. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17530. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17531. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17532. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17533. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17534. (call-interactively 'org-timestamp-down)))
  17535. (t (org-modifier-cursor-error))))
  17536. (defsubst org-hidden-tree-error ()
  17537. (error
  17538. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17539. (defun org-metaleft (&optional arg)
  17540. "Promote heading or move table column to left.
  17541. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17542. With no specific context, calls the Emacs default `backward-word'.
  17543. See the individual commands for more information."
  17544. (interactive "P")
  17545. (cond
  17546. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17547. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17548. ((org-with-limited-levels
  17549. (or (org-at-heading-p)
  17550. (and (org-region-active-p)
  17551. (save-excursion
  17552. (goto-char (region-beginning))
  17553. (org-at-heading-p)))))
  17554. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17555. (call-interactively 'org-do-promote))
  17556. ;; At an inline task.
  17557. ((org-at-heading-p)
  17558. (call-interactively 'org-inlinetask-promote))
  17559. ((or (org-at-item-p)
  17560. (and (org-region-active-p)
  17561. (save-excursion
  17562. (goto-char (region-beginning))
  17563. (org-at-item-p))))
  17564. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17565. (call-interactively 'org-outdent-item))
  17566. (t (call-interactively 'backward-word))))
  17567. (defun org-metaright (&optional arg)
  17568. "Demote a subtree, a list item or move table column to right.
  17569. In front of a drawer or a block keyword, indent it correctly.
  17570. With no specific context, calls the Emacs default `forward-word'.
  17571. See the individual commands for more information."
  17572. (interactive "P")
  17573. (cond
  17574. ((run-hook-with-args-until-success 'org-metaright-hook))
  17575. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17576. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17577. ((org-at-block-p) (call-interactively 'org-indent-block))
  17578. ((org-with-limited-levels
  17579. (or (org-at-heading-p)
  17580. (and (org-region-active-p)
  17581. (save-excursion
  17582. (goto-char (region-beginning))
  17583. (org-at-heading-p)))))
  17584. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17585. (call-interactively 'org-do-demote))
  17586. ;; At an inline task.
  17587. ((org-at-heading-p)
  17588. (call-interactively 'org-inlinetask-demote))
  17589. ((or (org-at-item-p)
  17590. (and (org-region-active-p)
  17591. (save-excursion
  17592. (goto-char (region-beginning))
  17593. (org-at-item-p))))
  17594. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17595. (call-interactively 'org-indent-item))
  17596. (t (call-interactively 'forward-word))))
  17597. (defun org-check-for-hidden (what)
  17598. "Check if there are hidden headlines/items in the current visual line.
  17599. WHAT can be either `headlines' or `items'. If the current line is
  17600. an outline or item heading and it has a folded subtree below it,
  17601. this function returns t, nil otherwise."
  17602. (let ((re (cond
  17603. ((eq what 'headlines) org-outline-regexp-bol)
  17604. ((eq what 'items) (org-item-beginning-re))
  17605. (t (error "This should not happen"))))
  17606. beg end)
  17607. (save-excursion
  17608. (catch 'exit
  17609. (unless (org-region-active-p)
  17610. (setq beg (point-at-bol))
  17611. (beginning-of-line 2)
  17612. (while (and (not (eobp)) ;; this is like `next-line'
  17613. (get-char-property (1- (point)) 'invisible))
  17614. (beginning-of-line 2))
  17615. (setq end (point))
  17616. (goto-char beg)
  17617. (goto-char (point-at-eol))
  17618. (setq end (max end (point)))
  17619. (while (re-search-forward re end t)
  17620. (if (get-char-property (match-beginning 0) 'invisible)
  17621. (throw 'exit t))))
  17622. nil))))
  17623. (defun org-metaup (&optional arg)
  17624. "Move subtree up or move table row up.
  17625. Calls `org-move-subtree-up' or `org-table-move-row' or
  17626. `org-move-item-up', depending on context. See the individual commands
  17627. for more information."
  17628. (interactive "P")
  17629. (cond
  17630. ((run-hook-with-args-until-success 'org-metaup-hook))
  17631. ((org-region-active-p)
  17632. (let* ((a (min (region-beginning) (region-end)))
  17633. (b (1- (max (region-beginning) (region-end))))
  17634. (c (save-excursion (goto-char a)
  17635. (move-beginning-of-line 0)))
  17636. (d (save-excursion (goto-char a)
  17637. (move-end-of-line 0) (point))))
  17638. (transpose-regions a b c d)
  17639. (goto-char c)))
  17640. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17641. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17642. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17643. (t (org-drag-element-backward))))
  17644. (defun org-metadown (&optional arg)
  17645. "Move subtree down or move table row down.
  17646. Calls `org-move-subtree-down' or `org-table-move-row' or
  17647. `org-move-item-down', depending on context. See the individual
  17648. commands for more information."
  17649. (interactive "P")
  17650. (cond
  17651. ((run-hook-with-args-until-success 'org-metadown-hook))
  17652. ((org-region-active-p)
  17653. (let* ((a (min (region-beginning) (region-end)))
  17654. (b (max (region-beginning) (region-end)))
  17655. (c (save-excursion (goto-char b)
  17656. (move-beginning-of-line 1)))
  17657. (d (save-excursion (goto-char b)
  17658. (move-end-of-line 1) (1+ (point)))))
  17659. (transpose-regions a b c d)
  17660. (goto-char d)))
  17661. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17662. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17663. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17664. (t (org-drag-element-forward))))
  17665. (defun org-shiftup (&optional arg)
  17666. "Increase item in timestamp or increase priority of current headline.
  17667. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17668. depending on context. See the individual commands for more information."
  17669. (interactive "P")
  17670. (cond
  17671. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17672. ((and org-support-shift-select (org-region-active-p))
  17673. (org-call-for-shift-select 'previous-line))
  17674. ((org-at-timestamp-p t)
  17675. (call-interactively (if org-edit-timestamp-down-means-later
  17676. 'org-timestamp-down 'org-timestamp-up)))
  17677. ((and (not (eq org-support-shift-select 'always))
  17678. org-enable-priority-commands
  17679. (org-at-heading-p))
  17680. (call-interactively 'org-priority-up))
  17681. ((and (not org-support-shift-select) (org-at-item-p))
  17682. (call-interactively 'org-previous-item))
  17683. ((org-clocktable-try-shift 'up arg))
  17684. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17685. (org-support-shift-select
  17686. (org-call-for-shift-select 'previous-line))
  17687. (t (org-shiftselect-error))))
  17688. (defun org-shiftdown (&optional arg)
  17689. "Decrease item in timestamp or decrease priority of current headline.
  17690. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17691. depending on context. See the individual commands for more information."
  17692. (interactive "P")
  17693. (cond
  17694. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17695. ((and org-support-shift-select (org-region-active-p))
  17696. (org-call-for-shift-select 'next-line))
  17697. ((org-at-timestamp-p t)
  17698. (call-interactively (if org-edit-timestamp-down-means-later
  17699. 'org-timestamp-up 'org-timestamp-down)))
  17700. ((and (not (eq org-support-shift-select 'always))
  17701. org-enable-priority-commands
  17702. (org-at-heading-p))
  17703. (call-interactively 'org-priority-down))
  17704. ((and (not org-support-shift-select) (org-at-item-p))
  17705. (call-interactively 'org-next-item))
  17706. ((org-clocktable-try-shift 'down arg))
  17707. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17708. (org-support-shift-select
  17709. (org-call-for-shift-select 'next-line))
  17710. (t (org-shiftselect-error))))
  17711. (defun org-shiftright (&optional arg)
  17712. "Cycle the thing at point or in the current line, depending on context.
  17713. Depending on context, this does one of the following:
  17714. - switch a timestamp at point one day into the future
  17715. - on a headline, switch to the next TODO keyword.
  17716. - on an item, switch entire list to the next bullet type
  17717. - on a property line, switch to the next allowed value
  17718. - on a clocktable definition line, move time block into the future"
  17719. (interactive "P")
  17720. (cond
  17721. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17722. ((and org-support-shift-select (org-region-active-p))
  17723. (org-call-for-shift-select 'forward-char))
  17724. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17725. ((and (not (eq org-support-shift-select 'always))
  17726. (org-at-heading-p))
  17727. (let ((org-inhibit-logging
  17728. (not org-treat-S-cursor-todo-selection-as-state-change))
  17729. (org-inhibit-blocking
  17730. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17731. (org-call-with-arg 'org-todo 'right)))
  17732. ((or (and org-support-shift-select
  17733. (not (eq org-support-shift-select 'always))
  17734. (org-at-item-bullet-p))
  17735. (and (not org-support-shift-select) (org-at-item-p)))
  17736. (org-call-with-arg 'org-cycle-list-bullet nil))
  17737. ((and (not (eq org-support-shift-select 'always))
  17738. (org-at-property-p))
  17739. (call-interactively 'org-property-next-allowed-value))
  17740. ((org-clocktable-try-shift 'right arg))
  17741. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17742. (org-support-shift-select
  17743. (org-call-for-shift-select 'forward-char))
  17744. (t (org-shiftselect-error))))
  17745. (defun org-shiftleft (&optional arg)
  17746. "Cycle the thing at point or in the current line, depending on context.
  17747. Depending on context, this does one of the following:
  17748. - switch a timestamp at point one day into the past
  17749. - on a headline, switch to the previous TODO keyword.
  17750. - on an item, switch entire list to the previous bullet type
  17751. - on a property line, switch to the previous allowed value
  17752. - on a clocktable definition line, move time block into the past"
  17753. (interactive "P")
  17754. (cond
  17755. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17756. ((and org-support-shift-select (org-region-active-p))
  17757. (org-call-for-shift-select 'backward-char))
  17758. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17759. ((and (not (eq org-support-shift-select 'always))
  17760. (org-at-heading-p))
  17761. (let ((org-inhibit-logging
  17762. (not org-treat-S-cursor-todo-selection-as-state-change))
  17763. (org-inhibit-blocking
  17764. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17765. (org-call-with-arg 'org-todo 'left)))
  17766. ((or (and org-support-shift-select
  17767. (not (eq org-support-shift-select 'always))
  17768. (org-at-item-bullet-p))
  17769. (and (not org-support-shift-select) (org-at-item-p)))
  17770. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17771. ((and (not (eq org-support-shift-select 'always))
  17772. (org-at-property-p))
  17773. (call-interactively 'org-property-previous-allowed-value))
  17774. ((org-clocktable-try-shift 'left arg))
  17775. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17776. (org-support-shift-select
  17777. (org-call-for-shift-select 'backward-char))
  17778. (t (org-shiftselect-error))))
  17779. (defun org-shiftcontrolright ()
  17780. "Switch to next TODO set."
  17781. (interactive)
  17782. (cond
  17783. ((and org-support-shift-select (org-region-active-p))
  17784. (org-call-for-shift-select 'forward-word))
  17785. ((and (not (eq org-support-shift-select 'always))
  17786. (org-at-heading-p))
  17787. (org-call-with-arg 'org-todo 'nextset))
  17788. (org-support-shift-select
  17789. (org-call-for-shift-select 'forward-word))
  17790. (t (org-shiftselect-error))))
  17791. (defun org-shiftcontrolleft ()
  17792. "Switch to previous TODO set."
  17793. (interactive)
  17794. (cond
  17795. ((and org-support-shift-select (org-region-active-p))
  17796. (org-call-for-shift-select 'backward-word))
  17797. ((and (not (eq org-support-shift-select 'always))
  17798. (org-at-heading-p))
  17799. (org-call-with-arg 'org-todo 'previousset))
  17800. (org-support-shift-select
  17801. (org-call-for-shift-select 'backward-word))
  17802. (t (org-shiftselect-error))))
  17803. (defun org-shiftcontrolup (&optional n)
  17804. "Change timestamps synchronously up in CLOCK log lines.
  17805. Optional argument N tells to change by that many units."
  17806. (interactive "P")
  17807. (cond ((and (not org-support-shift-select)
  17808. (org-at-clock-log-p)
  17809. (org-at-timestamp-p t))
  17810. (org-clock-timestamps-up n))
  17811. (t (org-shiftselect-error))))
  17812. (defun org-shiftcontroldown (&optional n)
  17813. "Change timestamps synchronously down in CLOCK log lines.
  17814. Optional argument N tells to change by that many units."
  17815. (interactive "P")
  17816. (cond ((and (not org-support-shift-select)
  17817. (org-at-clock-log-p)
  17818. (org-at-timestamp-p t))
  17819. (org-clock-timestamps-down n))
  17820. (t (org-shiftselect-error))))
  17821. (defun org-ctrl-c-ret ()
  17822. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17823. (interactive)
  17824. (cond
  17825. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17826. (t (call-interactively 'org-insert-heading))))
  17827. (defun org-find-visible ()
  17828. (let ((s (point)))
  17829. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17830. (get-char-property s 'invisible)))
  17831. s))
  17832. (defun org-find-invisible ()
  17833. (let ((s (point)))
  17834. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17835. (not (get-char-property s 'invisible))))
  17836. s))
  17837. (defun org-copy-visible (beg end)
  17838. "Copy the visible parts of the region."
  17839. (interactive "r")
  17840. (let (snippets s)
  17841. (save-excursion
  17842. (save-restriction
  17843. (narrow-to-region beg end)
  17844. (setq s (goto-char (point-min)))
  17845. (while (not (= (point) (point-max)))
  17846. (goto-char (org-find-invisible))
  17847. (push (buffer-substring s (point)) snippets)
  17848. (setq s (goto-char (org-find-visible))))))
  17849. (kill-new (apply 'concat (nreverse snippets)))))
  17850. (defun org-copy-special ()
  17851. "Copy region in table or copy current subtree.
  17852. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17853. See the individual commands for more information."
  17854. (interactive)
  17855. (call-interactively
  17856. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17857. (defun org-cut-special ()
  17858. "Cut region in table or cut current subtree.
  17859. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17860. See the individual commands for more information."
  17861. (interactive)
  17862. (call-interactively
  17863. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17864. (defun org-paste-special (arg)
  17865. "Paste rectangular region into table, or past subtree relative to level.
  17866. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17867. See the individual commands for more information."
  17868. (interactive "P")
  17869. (if (org-at-table-p)
  17870. (org-table-paste-rectangle)
  17871. (org-paste-subtree arg)))
  17872. (defsubst org-in-fixed-width-region-p ()
  17873. "Is point in a fixed-width region?"
  17874. (save-match-data
  17875. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17876. (defun org-edit-special (&optional arg)
  17877. "Call a special editor for the element at point.
  17878. When at a table, call the formula editor with `org-table-edit-formulas'.
  17879. When in a source code block, call `org-edit-src-code'.
  17880. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17881. When at an #+INCLUDE keyword, visit the included file.
  17882. On a link, call `ffap' to visit the link at point.
  17883. Otherwise, return a user error."
  17884. (interactive)
  17885. (let ((element (org-element-at-point)))
  17886. (case (org-element-type element)
  17887. (src-block
  17888. (if (not arg) (org-edit-src-code)
  17889. (let* ((info (org-babel-get-src-block-info))
  17890. (lang (nth 0 info))
  17891. (params (nth 2 info))
  17892. (session (cdr (assq :session params))))
  17893. (if (not session) (org-edit-src-code)
  17894. ;; At a src-block with a session and function called with
  17895. ;; an ARG: switch to the buffer related to the inferior
  17896. ;; process.
  17897. (funcall (intern (concat "org-babel-prep-session:" lang))
  17898. session params)))))
  17899. (keyword
  17900. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17901. (find-file
  17902. (org-remove-double-quotes
  17903. (car (org-split-string (org-element-property :value element)))))
  17904. (user-error "No special environment to edit here")))
  17905. (table
  17906. (if (eq (org-element-property :type element) 'table.el)
  17907. (org-edit-src-code)
  17908. (call-interactively 'org-table-edit-formulas)))
  17909. ;; Only Org tables contain `table-row' type elements.
  17910. (table-row (call-interactively 'org-table-edit-formulas))
  17911. ((example-block export-block) (org-edit-src-code))
  17912. (fixed-width (org-edit-fixed-width-region))
  17913. (otherwise
  17914. ;; No notable element at point. Though, we may be at a link,
  17915. ;; which is an object. Thus, scan deeper.
  17916. (if (eq (org-element-type (org-element-context element)) 'link)
  17917. (call-interactively 'ffap)
  17918. (user-error "No special environment to edit here"))))))
  17919. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17920. (defun org-ctrl-c-ctrl-c (&optional arg)
  17921. "Set tags in headline, or update according to changed information at point.
  17922. This command does many different things, depending on context:
  17923. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17924. this is what we do.
  17925. - If the cursor is on a statistics cookie, update it.
  17926. - If the cursor is in a headline, prompt for tags and insert them
  17927. into the current line, aligned to `org-tags-column'. When called
  17928. with prefix arg, realign all tags in the current buffer.
  17929. - If the cursor is in one of the special #+KEYWORD lines, this
  17930. triggers scanning the buffer for these lines and updating the
  17931. information.
  17932. - If the cursor is inside a table, realign the table. This command
  17933. works even if the automatic table editor has been turned off.
  17934. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17935. the entire table.
  17936. - If the cursor is at a footnote reference or definition, jump to
  17937. the corresponding definition or references, respectively.
  17938. - If the cursor is a the beginning of a dynamic block, update it.
  17939. - If the current buffer is a capture buffer, close note and file it.
  17940. - If the cursor is on a <<<target>>>, update radio targets and
  17941. corresponding links in this buffer.
  17942. - If the cursor is on a numbered item in a plain list, renumber the
  17943. ordered list.
  17944. - If the cursor is on a checkbox, toggle it.
  17945. - If the cursor is on a code block, evaluate it. The variable
  17946. `org-confirm-babel-evaluate' can be used to control prompting
  17947. before code block evaluation, by default every code block
  17948. evaluation requires confirmation. Code block evaluation can be
  17949. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17950. (interactive "P")
  17951. (cond
  17952. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17953. org-occur-highlights
  17954. org-latex-fragment-image-overlays)
  17955. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17956. (org-remove-occur-highlights)
  17957. (org-remove-latex-fragment-image-overlays)
  17958. (message "Temporary highlights/overlays removed from current buffer"))
  17959. ((and (local-variable-p 'org-finish-function (current-buffer))
  17960. (fboundp org-finish-function))
  17961. (funcall org-finish-function))
  17962. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17963. (t
  17964. (let* ((context (org-element-context)) (type (org-element-type context)))
  17965. ;; Test if point is within blanks at the end of an element.
  17966. (if (save-excursion
  17967. (or (not context)
  17968. (beginning-of-line)
  17969. (and (looking-at "[ \t]*$")
  17970. (skip-chars-forward " \r\t\n")
  17971. (>= (point) (org-element-property :end context)))))
  17972. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17973. (user-error "C-c C-c can do nothing useful at this location"))
  17974. ;; For convenience: at the first line of a paragraph on the
  17975. ;; same line as an item, apply function on that item instead.
  17976. (when (eq type 'paragraph)
  17977. (let ((parent (org-element-property :parent context)))
  17978. (when (and (eq (org-element-type parent) 'item)
  17979. (= (point-at-bol) (org-element-property :begin parent)))
  17980. (setq context parent type 'item))))
  17981. ;; Act according to type of element or object at point.
  17982. (case type
  17983. (clock (org-clock-update-time-maybe))
  17984. (dynamic-block
  17985. (save-excursion
  17986. (goto-char (org-element-property :post-affiliated context))
  17987. (org-update-dblock)))
  17988. (footnote-definition
  17989. (goto-char (org-element-property :post-affiliated context))
  17990. (call-interactively 'org-footnote-action))
  17991. (footnote-reference (call-interactively 'org-footnote-action))
  17992. ((headline inlinetask)
  17993. (save-excursion (goto-char (org-element-property :begin context))
  17994. (call-interactively 'org-set-tags)))
  17995. (item
  17996. ;; At an item: a double C-u set checkbox to "[-]"
  17997. ;; unconditionally, whereas a single one will toggle its
  17998. ;; presence. Without an universal argument, if the item
  17999. ;; has a checkbox, toggle it. Otherwise repair the list.
  18000. (let* ((box (org-element-property :checkbox context))
  18001. (struct (org-element-property :structure context))
  18002. (old-struct (copy-tree struct))
  18003. (parents (org-list-parents-alist struct))
  18004. (prevs (org-list-prevs-alist struct))
  18005. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18006. (org-list-set-checkbox
  18007. (org-element-property :begin context) struct
  18008. (cond ((equal arg '(16)) "[-]")
  18009. ((and (not box) (equal arg '(4))) "[ ]")
  18010. ((or (not box) (equal arg '(4))) nil)
  18011. ((eq box 'on) "[ ]")
  18012. (t "[X]")))
  18013. ;; Mimic `org-list-write-struct' but with grabbing
  18014. ;; a return value from `org-list-struct-fix-box'.
  18015. (org-list-struct-fix-ind struct parents 2)
  18016. (org-list-struct-fix-item-end struct)
  18017. (org-list-struct-fix-bul struct prevs)
  18018. (org-list-struct-fix-ind struct parents)
  18019. (let ((block-item
  18020. (org-list-struct-fix-box struct parents prevs orderedp)))
  18021. (if (and box (equal struct old-struct))
  18022. (if (equal arg '(16))
  18023. (message "Checkboxes already reset")
  18024. (user-error "Cannot toggle this checkbox: %s"
  18025. (if (eq box 'on)
  18026. "all subitems checked"
  18027. "unchecked subitems")))
  18028. (org-list-struct-apply-struct struct old-struct)
  18029. (org-update-checkbox-count-maybe))
  18030. (when block-item
  18031. (message "Checkboxes were removed due to empty box at line %d"
  18032. (org-current-line block-item))))))
  18033. (keyword
  18034. (let ((org-inhibit-startup-visibility-stuff t)
  18035. (org-startup-align-all-tables nil))
  18036. (when (boundp 'org-table-coordinate-overlays)
  18037. (mapc 'delete-overlay org-table-coordinate-overlays)
  18038. (setq org-table-coordinate-overlays nil))
  18039. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18040. (message "Local setup has been refreshed"))
  18041. (plain-list
  18042. ;; At a plain list, with a double C-u argument, set
  18043. ;; checkboxes of each item to "[-]", whereas a single one
  18044. ;; will toggle their presence according to the state of the
  18045. ;; first item in the list. Without an argument, repair the
  18046. ;; list.
  18047. (let* ((begin (org-element-property :contents-begin context))
  18048. (struct (org-element-property :structure context))
  18049. (old-struct (copy-tree struct))
  18050. (first-box (save-excursion
  18051. (goto-char begin)
  18052. (looking-at org-list-full-item-re)
  18053. (match-string-no-properties 3)))
  18054. (new-box (cond ((equal arg '(16)) "[-]")
  18055. ((equal arg '(4)) (unless first-box "[ ]"))
  18056. ((equal first-box "[X]") "[ ]")
  18057. (t "[X]"))))
  18058. (cond
  18059. (arg
  18060. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18061. (org-list-get-all-items
  18062. begin struct (org-list-prevs-alist struct))))
  18063. ((and first-box (eq (point) begin))
  18064. ;; For convenience, when point is at bol on the first
  18065. ;; item of the list and no argument is provided, simply
  18066. ;; toggle checkbox of that item, if any.
  18067. (org-list-set-checkbox begin struct new-box)))
  18068. (org-list-write-struct
  18069. struct (org-list-parents-alist struct) old-struct)
  18070. (org-update-checkbox-count-maybe)
  18071. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18072. ((property-drawer node-property)
  18073. (call-interactively 'org-property-action))
  18074. ((radio-target target)
  18075. (call-interactively 'org-update-radio-target-regexp))
  18076. (statistics-cookie
  18077. (call-interactively 'org-update-statistics-cookies))
  18078. ((table table-cell table-row)
  18079. ;; At a table, recalculate every field and align it. Also
  18080. ;; send the table if necessary. If the table has
  18081. ;; a `table.el' type, just give up. At a table row or
  18082. ;; cell, maybe recalculate line but always align table.
  18083. (if (eq (org-element-property :type context) 'table.el)
  18084. (message "Use C-c ' to edit table.el tables")
  18085. (let ((org-enable-table-editor t))
  18086. (if (or (eq type 'table)
  18087. ;; Check if point is at a TBLFM line.
  18088. (and (eq type 'table-row)
  18089. (= (point) (org-element-property :end context))))
  18090. (save-excursion
  18091. (goto-char (org-element-property :contents-begin context))
  18092. (org-call-with-arg 'org-table-recalculate (or arg t))
  18093. (orgtbl-send-table 'maybe))
  18094. (org-table-maybe-eval-formula)
  18095. (cond (arg (call-interactively 'org-table-recalculate))
  18096. ((org-table-maybe-recalculate-line))
  18097. (t (org-table-align)))))))
  18098. (timestamp (org-timestamp-change 0 'day))
  18099. (otherwise
  18100. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18101. (user-error
  18102. "C-c C-c can do nothing useful at this location")))))))))
  18103. (defun org-mode-restart ()
  18104. "Restart Org-mode, to scan again for special lines.
  18105. Also updates the keyword regular expressions."
  18106. (interactive)
  18107. (org-mode)
  18108. (message "Org-mode restarted"))
  18109. (defun org-kill-note-or-show-branches ()
  18110. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18111. (interactive)
  18112. (if (not org-finish-function)
  18113. (progn
  18114. (hide-subtree)
  18115. (call-interactively 'show-branches))
  18116. (let ((org-note-abort t))
  18117. (funcall org-finish-function))))
  18118. (defun org-return (&optional indent)
  18119. "Goto next table row or insert a newline.
  18120. Calls `org-table-next-row' or `newline', depending on context.
  18121. See the individual commands for more information."
  18122. (interactive)
  18123. (let (org-ts-what)
  18124. (cond
  18125. ((or (bobp) (org-in-src-block-p))
  18126. (if indent (newline-and-indent) (newline)))
  18127. ((org-at-table-p)
  18128. (org-table-justify-field-maybe)
  18129. (call-interactively 'org-table-next-row))
  18130. ;; when `newline-and-indent' is called within a list, make sure
  18131. ;; text moved stays inside the item.
  18132. ((and (org-in-item-p) indent)
  18133. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18134. (progn
  18135. (save-match-data (newline))
  18136. (org-indent-line-to (length (match-string 0))))
  18137. (let ((ind (org-get-indentation)))
  18138. (newline)
  18139. (if (org-looking-back org-list-end-re)
  18140. (org-indent-line)
  18141. (org-indent-line-to ind)))))
  18142. ((and org-return-follows-link
  18143. (org-at-timestamp-p t)
  18144. (not (eq org-ts-what 'after)))
  18145. (org-follow-timestamp-link))
  18146. ((and org-return-follows-link
  18147. (let ((tprop (get-text-property (point) 'face)))
  18148. (or (eq tprop 'org-link)
  18149. (and (listp tprop) (memq 'org-link tprop)))))
  18150. (call-interactively 'org-open-at-point))
  18151. ((and (org-at-heading-p)
  18152. (looking-at
  18153. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18154. (org-show-entry)
  18155. (end-of-line 1)
  18156. (newline))
  18157. (t (if indent (newline-and-indent) (newline))))))
  18158. (defun org-return-indent ()
  18159. "Goto next table row or insert a newline and indent.
  18160. Calls `org-table-next-row' or `newline-and-indent', depending on
  18161. context. See the individual commands for more information."
  18162. (interactive)
  18163. (org-return t))
  18164. (defun org-ctrl-c-star ()
  18165. "Compute table, or change heading status of lines.
  18166. Calls `org-table-recalculate' or `org-toggle-heading',
  18167. depending on context."
  18168. (interactive)
  18169. (cond
  18170. ((org-at-table-p)
  18171. (call-interactively 'org-table-recalculate))
  18172. (t
  18173. ;; Convert all lines in region to list items
  18174. (call-interactively 'org-toggle-heading))))
  18175. (defun org-ctrl-c-minus ()
  18176. "Insert separator line in table or modify bullet status of line.
  18177. Also turns a plain line or a region of lines into list items.
  18178. Calls `org-table-insert-hline', `org-toggle-item', or
  18179. `org-cycle-list-bullet', depending on context."
  18180. (interactive)
  18181. (cond
  18182. ((org-at-table-p)
  18183. (call-interactively 'org-table-insert-hline))
  18184. ((org-region-active-p)
  18185. (call-interactively 'org-toggle-item))
  18186. ((org-in-item-p)
  18187. (call-interactively 'org-cycle-list-bullet))
  18188. (t
  18189. (call-interactively 'org-toggle-item))))
  18190. (defun org-toggle-item (arg)
  18191. "Convert headings or normal lines to items, items to normal lines.
  18192. If there is no active region, only the current line is considered.
  18193. If the first non blank line in the region is an headline, convert
  18194. all headlines to items, shifting text accordingly.
  18195. If it is an item, convert all items to normal lines.
  18196. If it is normal text, change region into an item. With a prefix
  18197. argument ARG, change each line in region into an item."
  18198. (interactive "P")
  18199. (let ((shift-text
  18200. (function
  18201. ;; Shift text in current section to IND, from point to END.
  18202. ;; The function leaves point to END line.
  18203. (lambda (ind end)
  18204. (let ((min-i 1000) (end (copy-marker end)))
  18205. ;; First determine the minimum indentation (MIN-I) of
  18206. ;; the text.
  18207. (save-excursion
  18208. (catch 'exit
  18209. (while (< (point) end)
  18210. (let ((i (org-get-indentation)))
  18211. (cond
  18212. ;; Skip blank lines and inline tasks.
  18213. ((looking-at "^[ \t]*$"))
  18214. ((looking-at org-outline-regexp-bol))
  18215. ;; We can't find less than 0 indentation.
  18216. ((zerop i) (throw 'exit (setq min-i 0)))
  18217. ((< i min-i) (setq min-i i))))
  18218. (forward-line))))
  18219. ;; Then indent each line so that a line indented to
  18220. ;; MIN-I becomes indented to IND. Ignore blank lines
  18221. ;; and inline tasks in the process.
  18222. (let ((delta (- ind min-i)))
  18223. (while (< (point) end)
  18224. (unless (or (looking-at "^[ \t]*$")
  18225. (looking-at org-outline-regexp-bol))
  18226. (org-indent-line-to (+ (org-get-indentation) delta)))
  18227. (forward-line)))))))
  18228. (skip-blanks
  18229. (function
  18230. ;; Return beginning of first non-blank line, starting from
  18231. ;; line at POS.
  18232. (lambda (pos)
  18233. (save-excursion
  18234. (goto-char pos)
  18235. (skip-chars-forward " \r\t\n")
  18236. (point-at-bol)))))
  18237. beg end)
  18238. ;; Determine boundaries of changes.
  18239. (if (org-region-active-p)
  18240. (setq beg (funcall skip-blanks (region-beginning))
  18241. end (copy-marker (region-end)))
  18242. (setq beg (funcall skip-blanks (point-at-bol))
  18243. end (copy-marker (point-at-eol))))
  18244. ;; Depending on the starting line, choose an action on the text
  18245. ;; between BEG and END.
  18246. (org-with-limited-levels
  18247. (save-excursion
  18248. (goto-char beg)
  18249. (cond
  18250. ;; Case 1. Start at an item: de-itemize. Note that it only
  18251. ;; happens when a region is active: `org-ctrl-c-minus'
  18252. ;; would call `org-cycle-list-bullet' otherwise.
  18253. ((org-at-item-p)
  18254. (while (< (point) end)
  18255. (when (org-at-item-p)
  18256. (skip-chars-forward " \t")
  18257. (delete-region (point) (match-end 0)))
  18258. (forward-line)))
  18259. ;; Case 2. Start at an heading: convert to items.
  18260. ((org-at-heading-p)
  18261. (let* ((bul (org-list-bullet-string "-"))
  18262. (bul-len (length bul))
  18263. ;; Indentation of the first heading. It should be
  18264. ;; relative to the indentation of its parent, if any.
  18265. (start-ind (save-excursion
  18266. (cond
  18267. ((not org-adapt-indentation) 0)
  18268. ((not (outline-previous-heading)) 0)
  18269. (t (length (match-string 0))))))
  18270. ;; Level of first heading. Further headings will be
  18271. ;; compared to it to determine hierarchy in the list.
  18272. (ref-level (org-reduced-level (org-outline-level))))
  18273. (while (< (point) end)
  18274. (let* ((level (org-reduced-level (org-outline-level)))
  18275. (delta (max 0 (- level ref-level))))
  18276. ;; If current headline is less indented than the first
  18277. ;; one, set it as reference, in order to preserve
  18278. ;; subtrees.
  18279. (when (< level ref-level) (setq ref-level level))
  18280. (replace-match bul t t)
  18281. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18282. ;; Ensure all text down to END (or SECTION-END) belongs
  18283. ;; to the newly created item.
  18284. (let ((section-end (save-excursion
  18285. (or (outline-next-heading) (point)))))
  18286. (forward-line)
  18287. (funcall shift-text
  18288. (+ start-ind (* (1+ delta) bul-len))
  18289. (min end section-end)))))))
  18290. ;; Case 3. Normal line with ARG: turn each non-item line into
  18291. ;; an item.
  18292. (arg
  18293. (while (< (point) end)
  18294. (unless (or (org-at-heading-p) (org-at-item-p))
  18295. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18296. (replace-match
  18297. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18298. (forward-line)))
  18299. ;; Case 4. Normal line without ARG: make the first line of
  18300. ;; region an item, and shift indentation of others
  18301. ;; lines to set them as item's body.
  18302. (t (let* ((bul (org-list-bullet-string "-"))
  18303. (bul-len (length bul))
  18304. (ref-ind (org-get-indentation)))
  18305. (skip-chars-forward " \t")
  18306. (insert bul)
  18307. (forward-line)
  18308. (while (< (point) end)
  18309. ;; Ensure that lines less indented than first one
  18310. ;; still get included in item body.
  18311. (funcall shift-text
  18312. (+ ref-ind bul-len)
  18313. (min end (save-excursion (or (outline-next-heading)
  18314. (point)))))
  18315. (forward-line)))))))))
  18316. (defun org-toggle-heading (&optional nstars)
  18317. "Convert headings to normal text, or items or text to headings.
  18318. If there is no active region, only the current line is considered.
  18319. With a \\[universal-argument] prefix, convert the whole list at
  18320. point into heading.
  18321. In a region:
  18322. - If the first non blank line is an headline, remove the stars
  18323. from all headlines in the region.
  18324. - If it is a normal line turn each and every normal line (i.e. not an
  18325. heading or an item) in the region into a heading.
  18326. - If it is a plain list item, turn all plain list items into headings.
  18327. When converting a line into a heading, the number of stars is chosen
  18328. such that the lines become children of the current entry. However,
  18329. when a prefix argument is given, its value determines the number of
  18330. stars to add."
  18331. (interactive "P")
  18332. (let ((skip-blanks
  18333. (function
  18334. ;; Return beginning of first non-blank line, starting from
  18335. ;; line at POS.
  18336. (lambda (pos)
  18337. (save-excursion
  18338. (goto-char pos)
  18339. (while (org-at-comment-p) (forward-line))
  18340. (skip-chars-forward " \r\t\n")
  18341. (point-at-bol)))))
  18342. beg end toggled)
  18343. ;; Determine boundaries of changes. If a universal prefix has
  18344. ;; been given, put the list in a region. If region ends at a bol,
  18345. ;; do not consider the last line to be in the region.
  18346. (when (and current-prefix-arg (org-at-item-p))
  18347. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18348. (org-mark-element))
  18349. (if (org-region-active-p)
  18350. (setq beg (funcall skip-blanks (region-beginning))
  18351. end (copy-marker (save-excursion
  18352. (goto-char (region-end))
  18353. (if (bolp) (point) (point-at-eol)))))
  18354. (setq beg (funcall skip-blanks (point-at-bol))
  18355. end (copy-marker (point-at-eol))))
  18356. ;; Ensure inline tasks don't count as headings.
  18357. (org-with-limited-levels
  18358. (save-excursion
  18359. (goto-char beg)
  18360. (cond
  18361. ;; Case 1. Started at an heading: de-star headings.
  18362. ((org-at-heading-p)
  18363. (while (< (point) end)
  18364. (when (org-at-heading-p t)
  18365. (looking-at org-outline-regexp) (replace-match "")
  18366. (setq toggled t))
  18367. (forward-line)))
  18368. ;; Case 2. Started at an item: change items into headlines.
  18369. ;; One star will be added by `org-list-to-subtree'.
  18370. ((org-at-item-p)
  18371. (let* ((stars (make-string
  18372. (if nstars
  18373. ;; subtract the star that will be added again by
  18374. ;; `org-list-to-subtree'
  18375. (1- (prefix-numeric-value current-prefix-arg))
  18376. (or (org-current-level) 0))
  18377. ?*))
  18378. (add-stars
  18379. (cond (nstars "") ; stars from prefix only
  18380. ((equal stars "") "") ; before first heading
  18381. (org-odd-levels-only "*") ; inside heading, odd
  18382. (t "")))) ; inside heading, oddeven
  18383. (while (< (point) end)
  18384. (when (org-at-item-p)
  18385. ;; Pay attention to cases when region ends before list.
  18386. (let* ((struct (org-list-struct))
  18387. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18388. (save-restriction
  18389. (narrow-to-region (point) list-end)
  18390. (insert
  18391. (org-list-to-subtree
  18392. (org-list-parse-list t)
  18393. '(:istart (concat stars add-stars (funcall get-stars depth))
  18394. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18395. (setq toggled t))
  18396. (forward-line))))
  18397. ;; Case 3. Started at normal text: make every line an heading,
  18398. ;; skipping headlines and items.
  18399. (t (let* ((stars (make-string
  18400. (if nstars
  18401. (prefix-numeric-value current-prefix-arg)
  18402. (or (org-current-level) 0))
  18403. ?*))
  18404. (add-stars
  18405. (cond (nstars "") ; stars from prefix only
  18406. ((equal stars "") "*") ; before first heading
  18407. (org-odd-levels-only "**") ; inside heading, odd
  18408. (t "*"))) ; inside heading, oddeven
  18409. (rpl (concat stars add-stars " ")))
  18410. (while (< (point) end)
  18411. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18412. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18413. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18414. (forward-line)))))))
  18415. (unless toggled (message "Cannot toggle heading from here"))))
  18416. (defun org-meta-return (&optional arg)
  18417. "Insert a new heading or wrap a region in a table.
  18418. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18419. See the individual commands for more information."
  18420. (interactive "P")
  18421. (cond
  18422. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18423. ((or (org-at-drawer-p) (org-at-property-p))
  18424. (newline-and-indent))
  18425. ((org-at-table-p)
  18426. (call-interactively 'org-table-wrap-region))
  18427. (t (call-interactively 'org-insert-heading))))
  18428. ;;; Menu entries
  18429. (defsubst org-in-subtree-not-table-p ()
  18430. "Are we in a subtree and not in a table?"
  18431. (and (not (org-before-first-heading-p))
  18432. (not (org-at-table-p))))
  18433. ;; Define the Org-mode menus
  18434. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18435. '("Tbl"
  18436. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18437. ["Next Field" org-cycle (org-at-table-p)]
  18438. ["Previous Field" org-shifttab (org-at-table-p)]
  18439. ["Next Row" org-return (org-at-table-p)]
  18440. "--"
  18441. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18442. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18443. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18444. "--"
  18445. ("Column"
  18446. ["Move Column Left" org-metaleft (org-at-table-p)]
  18447. ["Move Column Right" org-metaright (org-at-table-p)]
  18448. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18449. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18450. ("Row"
  18451. ["Move Row Up" org-metaup (org-at-table-p)]
  18452. ["Move Row Down" org-metadown (org-at-table-p)]
  18453. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18454. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18455. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18456. "--"
  18457. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18458. ("Rectangle"
  18459. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18460. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18461. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18462. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18463. "--"
  18464. ("Calculate"
  18465. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18466. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18467. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18468. "--"
  18469. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18470. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18471. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18472. "--"
  18473. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18474. "--"
  18475. ["Sum Column/Rectangle" org-table-sum
  18476. (or (org-at-table-p) (org-region-active-p))]
  18477. ["Which Column?" org-table-current-column (org-at-table-p)])
  18478. ["Debug Formulas"
  18479. org-table-toggle-formula-debugger
  18480. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18481. ["Show Col/Row Numbers"
  18482. org-table-toggle-coordinate-overlays
  18483. :style toggle
  18484. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18485. "--"
  18486. ["Create" org-table-create (and (not (org-at-table-p))
  18487. org-enable-table-editor)]
  18488. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18489. ["Import from File" org-table-import (not (org-at-table-p))]
  18490. ["Export to File" org-table-export (org-at-table-p)]
  18491. "--"
  18492. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18493. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18494. '("Org"
  18495. ("Show/Hide"
  18496. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18497. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18498. ["Sparse Tree..." org-sparse-tree t]
  18499. ["Reveal Context" org-reveal t]
  18500. ["Show All" show-all t]
  18501. "--"
  18502. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18503. "--"
  18504. ["New Heading" org-insert-heading t]
  18505. ("Navigate Headings"
  18506. ["Up" outline-up-heading t]
  18507. ["Next" outline-next-visible-heading t]
  18508. ["Previous" outline-previous-visible-heading t]
  18509. ["Next Same Level" outline-forward-same-level t]
  18510. ["Previous Same Level" outline-backward-same-level t]
  18511. "--"
  18512. ["Jump" org-goto t])
  18513. ("Edit Structure"
  18514. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18515. "--"
  18516. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18517. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18518. "--"
  18519. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18520. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18521. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18522. "--"
  18523. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18524. "--"
  18525. ["Copy visible text" org-copy-visible t]
  18526. "--"
  18527. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18528. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18529. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18530. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18531. "--"
  18532. ["Sort Region/Children" org-sort t]
  18533. "--"
  18534. ["Convert to odd levels" org-convert-to-odd-levels t]
  18535. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18536. ("Editing"
  18537. ["Emphasis..." org-emphasize t]
  18538. ["Edit Source Example" org-edit-special t]
  18539. "--"
  18540. ["Footnote new/jump" org-footnote-action t]
  18541. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18542. ("Archive"
  18543. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18544. "--"
  18545. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18546. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18547. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18548. )
  18549. "--"
  18550. ("Hyperlinks"
  18551. ["Store Link (Global)" org-store-link t]
  18552. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18553. ["Insert Link" org-insert-link t]
  18554. ["Follow Link" org-open-at-point t]
  18555. "--"
  18556. ["Next link" org-next-link t]
  18557. ["Previous link" org-previous-link t]
  18558. "--"
  18559. ["Descriptive Links"
  18560. org-toggle-link-display
  18561. :style radio
  18562. :selected org-descriptive-links
  18563. ]
  18564. ["Literal Links"
  18565. org-toggle-link-display
  18566. :style radio
  18567. :selected (not org-descriptive-links)])
  18568. "--"
  18569. ("TODO Lists"
  18570. ["TODO/DONE/-" org-todo t]
  18571. ("Select keyword"
  18572. ["Next keyword" org-shiftright (org-at-heading-p)]
  18573. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18574. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18575. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18576. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18577. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18578. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18579. "--"
  18580. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18581. :selected org-enforce-todo-dependencies :style toggle :active t]
  18582. "Settings for tree at point"
  18583. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18584. :selected (org-entry-get nil "ORDERED")
  18585. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18586. ["Do Children parallel" org-toggle-ordered-property :style radio
  18587. :selected (not (org-entry-get nil "ORDERED"))
  18588. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18589. "--"
  18590. ["Set Priority" org-priority t]
  18591. ["Priority Up" org-shiftup t]
  18592. ["Priority Down" org-shiftdown t]
  18593. "--"
  18594. ["Get news from all feeds" org-feed-update-all t]
  18595. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18596. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18597. ("TAGS and Properties"
  18598. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18599. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18600. "--"
  18601. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18602. ["Column view of properties" org-columns t]
  18603. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18604. ("Dates and Scheduling"
  18605. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18606. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18607. ("Change Date"
  18608. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18609. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18610. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18611. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18612. ["Compute Time Range" org-evaluate-time-range t]
  18613. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18614. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18615. "--"
  18616. ["Custom time format" org-toggle-time-stamp-overlays
  18617. :style radio :selected org-display-custom-times]
  18618. "--"
  18619. ["Goto Calendar" org-goto-calendar t]
  18620. ["Date from Calendar" org-date-from-calendar t]
  18621. "--"
  18622. ["Start/Restart Timer" org-timer-start t]
  18623. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18624. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18625. ["Insert Timer String" org-timer t]
  18626. ["Insert Timer Item" org-timer-item t])
  18627. ("Logging work"
  18628. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18629. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18630. ["Clock out" org-clock-out t]
  18631. ["Clock cancel" org-clock-cancel t]
  18632. "--"
  18633. ["Mark as default task" org-clock-mark-default-task t]
  18634. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18635. ["Goto running clock" org-clock-goto t]
  18636. "--"
  18637. ["Display times" org-clock-display t]
  18638. ["Create clock table" org-clock-report t]
  18639. "--"
  18640. ["Record DONE time"
  18641. (progn (setq org-log-done (not org-log-done))
  18642. (message "Switching to %s will %s record a timestamp"
  18643. (car org-done-keywords)
  18644. (if org-log-done "automatically" "not")))
  18645. :style toggle :selected org-log-done])
  18646. "--"
  18647. ["Agenda Command..." org-agenda t]
  18648. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18649. ("File List for Agenda")
  18650. ("Special views current file"
  18651. ["TODO Tree" org-show-todo-tree t]
  18652. ["Check Deadlines" org-check-deadlines t]
  18653. ["Timeline" org-timeline t]
  18654. ["Tags/Property tree" org-match-sparse-tree t])
  18655. "--"
  18656. ["Export/Publish..." org-export t]
  18657. ("LaTeX"
  18658. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18659. :selected org-cdlatex-mode]
  18660. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18661. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18662. ["Modify math symbol" org-cdlatex-math-modify
  18663. (org-inside-LaTeX-fragment-p)]
  18664. ["Insert citation" org-reftex-citation t]
  18665. "--"
  18666. ["Template for BEAMER" (progn (require 'org-beamer)
  18667. (org-insert-beamer-options-template)) t])
  18668. "--"
  18669. ("MobileOrg"
  18670. ["Push Files and Views" org-mobile-push t]
  18671. ["Get Captured and Flagged" org-mobile-pull t]
  18672. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18673. "--"
  18674. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18675. "--"
  18676. ("Documentation"
  18677. ["Show Version" org-version t]
  18678. ["Info Documentation" org-info t])
  18679. ("Customize"
  18680. ["Browse Org Group" org-customize t]
  18681. "--"
  18682. ["Expand This Menu" org-create-customize-menu
  18683. (fboundp 'customize-menu-create)])
  18684. ["Send bug report" org-submit-bug-report t]
  18685. "--"
  18686. ("Refresh/Reload"
  18687. ["Refresh setup current buffer" org-mode-restart t]
  18688. ["Reload Org (after update)" org-reload t]
  18689. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18690. ))
  18691. (defun org-info (&optional node)
  18692. "Read documentation for Org-mode in the info system.
  18693. With optional NODE, go directly to that node."
  18694. (interactive)
  18695. (info (format "(org)%s" (or node ""))))
  18696. ;;;###autoload
  18697. (defun org-submit-bug-report ()
  18698. "Submit a bug report on Org-mode via mail.
  18699. Don't hesitate to report any problems or inaccurate documentation.
  18700. If you don't have setup sending mail from (X)Emacs, please copy the
  18701. output buffer into your mail program, as it gives us important
  18702. information about your Org-mode version and configuration."
  18703. (interactive)
  18704. (require 'reporter)
  18705. (org-load-modules-maybe)
  18706. (org-require-autoloaded-modules)
  18707. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18708. (reporter-submit-bug-report
  18709. "emacs-orgmode@gnu.org"
  18710. (org-version nil 'full)
  18711. (let (list)
  18712. (save-window-excursion
  18713. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18714. (delete-other-windows)
  18715. (erase-buffer)
  18716. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18717. We would like to add your full Org-mode and Outline configuration to the
  18718. bug report. This greatly simplifies the work of the maintainer and
  18719. other experts on the mailing list.
  18720. HOWEVER, some variables you have customized may contain private
  18721. information. The names of customers, colleagues, or friends, might
  18722. appear in the form of file names, tags, todo states, or search strings.
  18723. If you answer yes to the prompt, you might want to check and remove
  18724. such private information before sending the email.")
  18725. (add-text-properties (point-min) (point-max) '(face org-warning))
  18726. (when (yes-or-no-p "Include your Org-mode configuration ")
  18727. (mapatoms
  18728. (lambda (v)
  18729. (and (boundp v)
  18730. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18731. (or (and (symbol-value v)
  18732. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18733. (and
  18734. (get v 'custom-type) (get v 'standard-value)
  18735. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18736. (push v list)))))
  18737. (kill-buffer (get-buffer "*Warn about privacy*"))
  18738. list))
  18739. nil nil
  18740. "Remember to cover the basics, that is, what you expected to happen and
  18741. what in fact did happen. You don't know how to make a good report? See
  18742. http://orgmode.org/manual/Feedback.html#Feedback
  18743. Your bug report will be posted to the Org-mode mailing list.
  18744. ------------------------------------------------------------------------")
  18745. (save-excursion
  18746. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18747. (replace-match "\\1Bug: \\3 [\\2]")))))
  18748. (defun org-install-agenda-files-menu ()
  18749. (let ((bl (buffer-list)))
  18750. (save-excursion
  18751. (while bl
  18752. (set-buffer (pop bl))
  18753. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18754. (when (derived-mode-p 'org-mode)
  18755. (easy-menu-change
  18756. '("Org") "File List for Agenda"
  18757. (append
  18758. (list
  18759. ["Edit File List" (org-edit-agenda-file-list) t]
  18760. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18761. ["Remove Current File from List" org-remove-file t]
  18762. ["Cycle through agenda files" org-cycle-agenda-files t]
  18763. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18764. "--")
  18765. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18766. ;;;; Documentation
  18767. (defun org-require-autoloaded-modules ()
  18768. (interactive)
  18769. (mapc 'require
  18770. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18771. org-docbook org-exp org-html org-icalendar
  18772. org-id org-latex
  18773. org-publish org-remember org-table
  18774. org-timer org-xoxo)))
  18775. ;;;###autoload
  18776. (defun org-reload (&optional uncompiled)
  18777. "Reload all org lisp files.
  18778. With prefix arg UNCOMPILED, load the uncompiled versions."
  18779. (interactive "P")
  18780. (require 'loadhist)
  18781. (let* ((org-dir (org-find-library-dir "org"))
  18782. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18783. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18784. (remove-re (mapconcat 'identity
  18785. (mapcar (lambda (f) (concat "^" f "$"))
  18786. (list (if (featurep 'xemacs)
  18787. "org-colview"
  18788. "org-colview-xemacs")
  18789. "org" "org-loaddefs" "org-version"))
  18790. "\\|"))
  18791. (feats (delete-dups
  18792. (mapcar 'file-name-sans-extension
  18793. (mapcar 'file-name-nondirectory
  18794. (delq nil
  18795. (mapcar 'feature-file
  18796. features))))))
  18797. (lfeat (append
  18798. (sort
  18799. (setq feats
  18800. (delq nil (mapcar
  18801. (lambda (f)
  18802. (if (and (string-match feature-re f)
  18803. (not (string-match remove-re f)))
  18804. f nil))
  18805. feats)))
  18806. 'string-lessp)
  18807. (list "org-version" "org")))
  18808. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18809. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18810. load-uncore load-misses)
  18811. (setq load-misses
  18812. (delq 't
  18813. (mapcar (lambda (f)
  18814. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18815. (and (string= org-dir contrib-dir)
  18816. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18817. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18818. (add-to-list 'load-uncore f 'append)
  18819. 't)
  18820. f))
  18821. lfeat)))
  18822. (if load-uncore
  18823. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18824. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18825. (if load-misses
  18826. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18827. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18828. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18829. ;;;###autoload
  18830. (defun org-customize ()
  18831. "Call the customize function with org as argument."
  18832. (interactive)
  18833. (org-load-modules-maybe)
  18834. (org-require-autoloaded-modules)
  18835. (customize-browse 'org))
  18836. (defun org-create-customize-menu ()
  18837. "Create a full customization menu for Org-mode, insert it into the menu."
  18838. (interactive)
  18839. (org-load-modules-maybe)
  18840. (org-require-autoloaded-modules)
  18841. (if (fboundp 'customize-menu-create)
  18842. (progn
  18843. (easy-menu-change
  18844. '("Org") "Customize"
  18845. `(["Browse Org group" org-customize t]
  18846. "--"
  18847. ,(customize-menu-create 'org)
  18848. ["Set" Custom-set t]
  18849. ["Save" Custom-save t]
  18850. ["Reset to Current" Custom-reset-current t]
  18851. ["Reset to Saved" Custom-reset-saved t]
  18852. ["Reset to Standard Settings" Custom-reset-standard t]))
  18853. (message "\"Org\"-menu now contains full customization menu"))
  18854. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18855. ;;;; Miscellaneous stuff
  18856. ;;; Generally useful functions
  18857. (defun org-get-at-bol (property)
  18858. "Get text property PROPERTY at beginning of line."
  18859. (get-text-property (point-at-bol) property))
  18860. (defun org-find-text-property-in-string (prop s)
  18861. "Return the first non-nil value of property PROP in string S."
  18862. (or (get-text-property 0 prop s)
  18863. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18864. prop s)))
  18865. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18866. "Display the given MESSAGE as a warning."
  18867. (if (fboundp 'display-warning)
  18868. (display-warning 'org message
  18869. (if (featurep 'xemacs) 'warning :warning))
  18870. (let ((buf (get-buffer-create "*Org warnings*")))
  18871. (with-current-buffer buf
  18872. (goto-char (point-max))
  18873. (insert "Warning (Org): " message)
  18874. (unless (bolp)
  18875. (newline)))
  18876. (display-buffer buf)
  18877. (sit-for 0))))
  18878. (defun org-eval (form)
  18879. "Eval FORM and return result."
  18880. (condition-case error
  18881. (eval form)
  18882. (error (format "%%![Error: %s]" error))))
  18883. (defun org-in-clocktable-p ()
  18884. "Check if the cursor is in a clocktable."
  18885. (let ((pos (point)) start)
  18886. (save-excursion
  18887. (end-of-line 1)
  18888. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18889. (setq start (match-beginning 0))
  18890. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18891. (>= (match-end 0) pos)
  18892. start))))
  18893. (defun org-in-commented-line ()
  18894. "Is point in a line starting with `#'?"
  18895. (equal (char-after (point-at-bol)) ?#))
  18896. (defun org-in-indented-comment-line ()
  18897. "Is point in a line starting with `#' after some white space?"
  18898. (save-excursion
  18899. (save-match-data
  18900. (goto-char (point-at-bol))
  18901. (looking-at "[ \t]*#"))))
  18902. (defun org-in-verbatim-emphasis ()
  18903. (save-match-data
  18904. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18905. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18906. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18907. (if (and marker (marker-buffer marker)
  18908. (buffer-live-p (marker-buffer marker)))
  18909. (progn
  18910. (org-pop-to-buffer-same-window (marker-buffer marker))
  18911. (if (or (> marker (point-max)) (< marker (point-min)))
  18912. (widen))
  18913. (goto-char marker)
  18914. (org-show-context 'org-goto))
  18915. (if bookmark
  18916. (bookmark-jump bookmark)
  18917. (error "Cannot find location"))))
  18918. (defun org-quote-csv-field (s)
  18919. "Quote field for inclusion in CSV material."
  18920. (if (string-match "[\",]" s)
  18921. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18922. s))
  18923. (defun org-force-self-insert (N)
  18924. "Needed to enforce self-insert under remapping."
  18925. (interactive "p")
  18926. (self-insert-command N))
  18927. (defun org-string-width (s)
  18928. "Compute width of string, ignoring invisible characters.
  18929. This ignores character with invisibility property `org-link', and also
  18930. characters with property `org-cwidth', because these will become invisible
  18931. upon the next fontification round."
  18932. (let (b l)
  18933. (when (or (eq t buffer-invisibility-spec)
  18934. (assq 'org-link buffer-invisibility-spec))
  18935. (while (setq b (text-property-any 0 (length s)
  18936. 'invisible 'org-link s))
  18937. (setq s (concat (substring s 0 b)
  18938. (substring s (or (next-single-property-change
  18939. b 'invisible s) (length s)))))))
  18940. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18941. (setq s (concat (substring s 0 b)
  18942. (substring s (or (next-single-property-change
  18943. b 'org-cwidth s) (length s))))))
  18944. (setq l (string-width s) b -1)
  18945. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18946. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18947. l))
  18948. (defun org-shorten-string (s maxlength)
  18949. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18950. If the string is shorter or has length MAXLENGTH, just return the
  18951. original string. If it is longer, the functions finds a space in the
  18952. string, breaks this string off at that locations and adds three dots
  18953. as ellipsis. Including the ellipsis, the string will not be longer
  18954. than MAXLENGTH. If finding a good breaking point in the string does
  18955. not work, the string is just chopped off in the middle of a word
  18956. if necessary."
  18957. (if (<= (length s) maxlength)
  18958. s
  18959. (let* ((n (max (- maxlength 4) 1))
  18960. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18961. (if (string-match re s)
  18962. (concat (match-string 1 s) "...")
  18963. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18964. (defun org-get-indentation (&optional line)
  18965. "Get the indentation of the current line, interpreting tabs.
  18966. When LINE is given, assume it represents a line and compute its indentation."
  18967. (if line
  18968. (if (string-match "^ *" (org-remove-tabs line))
  18969. (match-end 0))
  18970. (save-excursion
  18971. (beginning-of-line 1)
  18972. (skip-chars-forward " \t")
  18973. (current-column))))
  18974. (defun org-get-string-indentation (s)
  18975. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18976. (let ((n -1) (i 0) (w tab-width) c)
  18977. (catch 'exit
  18978. (while (< (setq n (1+ n)) (length s))
  18979. (setq c (aref s n))
  18980. (cond ((= c ?\ ) (setq i (1+ i)))
  18981. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18982. (t (throw 'exit t)))))
  18983. i))
  18984. (defun org-remove-tabs (s &optional width)
  18985. "Replace tabulators in S with spaces.
  18986. Assumes that s is a single line, starting in column 0."
  18987. (setq width (or width tab-width))
  18988. (while (string-match "\t" s)
  18989. (setq s (replace-match
  18990. (make-string
  18991. (- (* width (/ (+ (match-beginning 0) width) width))
  18992. (match-beginning 0)) ?\ )
  18993. t t s)))
  18994. s)
  18995. (defun org-fix-indentation (line ind)
  18996. "Fix indentation in LINE.
  18997. IND is a cons cell with target and minimum indentation.
  18998. If the current indentation in LINE is smaller than the minimum,
  18999. leave it alone. If it is larger than ind, set it to the target."
  19000. (let* ((l (org-remove-tabs line))
  19001. (i (org-get-indentation l))
  19002. (i1 (car ind)) (i2 (cdr ind)))
  19003. (if (>= i i2) (setq l (substring line i2)))
  19004. (if (> i1 0)
  19005. (concat (make-string i1 ?\ ) l)
  19006. l)))
  19007. (defun org-remove-indentation (code &optional n)
  19008. "Remove the maximum common indentation from the lines in CODE.
  19009. N may optionally be the number of spaces to remove."
  19010. (with-temp-buffer
  19011. (insert code)
  19012. (org-do-remove-indentation n)
  19013. (buffer-string)))
  19014. (defun org-do-remove-indentation (&optional n)
  19015. "Remove the maximum common indentation from the buffer."
  19016. (untabify (point-min) (point-max))
  19017. (let ((min 10000) re)
  19018. (if n
  19019. (setq min n)
  19020. (goto-char (point-min))
  19021. (while (re-search-forward "^ *[^ \n]" nil t)
  19022. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19023. (unless (or (= min 0) (= min 10000))
  19024. (setq re (format "^ \\{%d\\}" min))
  19025. (goto-char (point-min))
  19026. (while (re-search-forward re nil t)
  19027. (replace-match "")
  19028. (end-of-line 1))
  19029. min)))
  19030. (defun org-fill-template (template alist)
  19031. "Find each %key of ALIST in TEMPLATE and replace it."
  19032. (let ((case-fold-search nil)
  19033. entry key value)
  19034. (setq alist (sort (copy-sequence alist)
  19035. (lambda (a b) (< (length (car a)) (length (car b))))))
  19036. (while (setq entry (pop alist))
  19037. (setq template
  19038. (replace-regexp-in-string
  19039. (concat "%" (regexp-quote (car entry)))
  19040. (or (cdr entry) "") template t t)))
  19041. template))
  19042. (defun org-base-buffer (buffer)
  19043. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19044. (if (not buffer)
  19045. buffer
  19046. (or (buffer-base-buffer buffer)
  19047. buffer)))
  19048. (defun org-trim (s)
  19049. "Remove whitespace at beginning and end of string."
  19050. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19051. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19052. s)
  19053. (defun org-wrap (string &optional width lines)
  19054. "Wrap string to either a number of lines, or a width in characters.
  19055. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19056. that costs. If there is a word longer than WIDTH, the text is actually
  19057. wrapped to the length of that word.
  19058. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19059. many lines, whatever width that takes.
  19060. The return value is a list of lines, without newlines at the end."
  19061. (let* ((words (org-split-string string "[ \t\n]+"))
  19062. (maxword (apply 'max (mapcar 'org-string-width words)))
  19063. w ll)
  19064. (cond (width
  19065. (org-do-wrap words (max maxword width)))
  19066. (lines
  19067. (setq w maxword)
  19068. (setq ll (org-do-wrap words maxword))
  19069. (if (<= (length ll) lines)
  19070. ll
  19071. (setq ll words)
  19072. (while (> (length ll) lines)
  19073. (setq w (1+ w))
  19074. (setq ll (org-do-wrap words w)))
  19075. ll))
  19076. (t (error "Cannot wrap this")))))
  19077. (defun org-do-wrap (words width)
  19078. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19079. (let (lines line)
  19080. (while words
  19081. (setq line (pop words))
  19082. (while (and words (< (+ (length line) (length (car words))) width))
  19083. (setq line (concat line " " (pop words))))
  19084. (setq lines (push line lines)))
  19085. (nreverse lines)))
  19086. (defun org-split-string (string &optional separators)
  19087. "Splits STRING into substrings at SEPARATORS.
  19088. No empty strings are returned if there are matches at the beginning
  19089. and end of string."
  19090. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19091. (start 0)
  19092. notfirst
  19093. (list nil))
  19094. (while (and (string-match rexp string
  19095. (if (and notfirst
  19096. (= start (match-beginning 0))
  19097. (< start (length string)))
  19098. (1+ start) start))
  19099. (< (match-beginning 0) (length string)))
  19100. (setq notfirst t)
  19101. (or (eq (match-beginning 0) 0)
  19102. (and (eq (match-beginning 0) (match-end 0))
  19103. (eq (match-beginning 0) start))
  19104. (setq list
  19105. (cons (substring string start (match-beginning 0))
  19106. list)))
  19107. (setq start (match-end 0)))
  19108. (or (eq start (length string))
  19109. (setq list
  19110. (cons (substring string start)
  19111. list)))
  19112. (nreverse list)))
  19113. (defun org-quote-vert (s)
  19114. "Replace \"|\" with \"\\vert\"."
  19115. (while (string-match "|" s)
  19116. (setq s (replace-match "\\vert" t t s)))
  19117. s)
  19118. (defun org-uuidgen-p (s)
  19119. "Is S an ID created by UUIDGEN?"
  19120. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19121. (defun org-in-src-block-p (&optional inside)
  19122. "Whether point is in a code source block.
  19123. When INSIDE is non-nil, don't consider we are within a src block
  19124. when point is at #+BEGIN_SRC or #+END_SRC."
  19125. (let ((case-fold-search t) ov)
  19126. (or (and (setq ov (overlays-at (point)))
  19127. (memq 'org-block-background
  19128. (overlay-properties (car ov))))
  19129. (and (not inside)
  19130. (save-match-data
  19131. (save-excursion
  19132. (beginning-of-line)
  19133. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19134. (defun org-context ()
  19135. "Return a list of contexts of the current cursor position.
  19136. If several contexts apply, all are returned.
  19137. Each context entry is a list with a symbol naming the context, and
  19138. two positions indicating start and end of the context. Possible
  19139. contexts are:
  19140. :headline anywhere in a headline
  19141. :headline-stars on the leading stars in a headline
  19142. :todo-keyword on a TODO keyword (including DONE) in a headline
  19143. :tags on the TAGS in a headline
  19144. :priority on the priority cookie in a headline
  19145. :item on the first line of a plain list item
  19146. :item-bullet on the bullet/number of a plain list item
  19147. :checkbox on the checkbox in a plain list item
  19148. :table in an org-mode table
  19149. :table-special on a special filed in a table
  19150. :table-table in a table.el table
  19151. :clocktable in a clocktable
  19152. :src-block in a source block
  19153. :link on a hyperlink
  19154. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19155. :target on a <<target>>
  19156. :radio-target on a <<<radio-target>>>
  19157. :latex-fragment on a LaTeX fragment
  19158. :latex-preview on a LaTeX fragment with overlaid preview image
  19159. This function expects the position to be visible because it uses font-lock
  19160. faces as a help to recognize the following contexts: :table-special, :link,
  19161. and :keyword."
  19162. (let* ((f (get-text-property (point) 'face))
  19163. (faces (if (listp f) f (list f)))
  19164. (case-fold-search t)
  19165. (p (point)) clist o)
  19166. ;; First the large context
  19167. (cond
  19168. ((org-at-heading-p t)
  19169. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19170. (when (progn
  19171. (beginning-of-line 1)
  19172. (looking-at org-todo-line-tags-regexp))
  19173. (push (org-point-in-group p 1 :headline-stars) clist)
  19174. (push (org-point-in-group p 2 :todo-keyword) clist)
  19175. (push (org-point-in-group p 4 :tags) clist))
  19176. (goto-char p)
  19177. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19178. (if (looking-at "\\[#[A-Z0-9]\\]")
  19179. (push (org-point-in-group p 0 :priority) clist)))
  19180. ((org-at-item-p)
  19181. (push (org-point-in-group p 2 :item-bullet) clist)
  19182. (push (list :item (point-at-bol)
  19183. (save-excursion (org-end-of-item) (point)))
  19184. clist)
  19185. (and (org-at-item-checkbox-p)
  19186. (push (org-point-in-group p 0 :checkbox) clist)))
  19187. ((org-at-table-p)
  19188. (push (list :table (org-table-begin) (org-table-end)) clist)
  19189. (if (memq 'org-formula faces)
  19190. (push (list :table-special
  19191. (previous-single-property-change p 'face)
  19192. (next-single-property-change p 'face)) clist)))
  19193. ((org-at-table-p 'any)
  19194. (push (list :table-table) clist)))
  19195. (goto-char p)
  19196. (let ((case-fold-search t))
  19197. ;; New the "medium" contexts: clocktables, source blocks
  19198. (cond ((org-in-clocktable-p)
  19199. (push (list :clocktable
  19200. (and (or (looking-at "#\\+BEGIN: clocktable")
  19201. (search-backward "#+BEGIN: clocktable" nil t))
  19202. (match-beginning 0))
  19203. (and (re-search-forward "#\\+END:?" nil t)
  19204. (match-end 0))) clist))
  19205. ((org-in-src-block-p)
  19206. (push (list :src-block
  19207. (and (or (looking-at "#\\+BEGIN_SRC")
  19208. (search-backward "#+BEGIN_SRC" nil t))
  19209. (match-beginning 0))
  19210. (and (search-forward "#+END_SRC" nil t)
  19211. (match-beginning 0))) clist))))
  19212. (goto-char p)
  19213. ;; Now the small context
  19214. (cond
  19215. ((org-at-timestamp-p)
  19216. (push (org-point-in-group p 0 :timestamp) clist))
  19217. ((memq 'org-link faces)
  19218. (push (list :link
  19219. (previous-single-property-change p 'face)
  19220. (next-single-property-change p 'face)) clist))
  19221. ((memq 'org-special-keyword faces)
  19222. (push (list :keyword
  19223. (previous-single-property-change p 'face)
  19224. (next-single-property-change p 'face)) clist))
  19225. ((org-at-target-p)
  19226. (push (org-point-in-group p 0 :target) clist)
  19227. (goto-char (1- (match-beginning 0)))
  19228. (if (looking-at org-radio-target-regexp)
  19229. (push (org-point-in-group p 0 :radio-target) clist))
  19230. (goto-char p))
  19231. ((setq o (car (delq nil
  19232. (mapcar
  19233. (lambda (x)
  19234. (if (memq x org-latex-fragment-image-overlays) x))
  19235. (overlays-at (point))))))
  19236. (push (list :latex-fragment
  19237. (overlay-start o) (overlay-end o)) clist)
  19238. (push (list :latex-preview
  19239. (overlay-start o) (overlay-end o)) clist))
  19240. ((org-inside-LaTeX-fragment-p)
  19241. ;; FIXME: positions wrong.
  19242. (push (list :latex-fragment (point) (point)) clist)))
  19243. (setq clist (nreverse (delq nil clist)))
  19244. clist))
  19245. ;; FIXME: Compare with at-regexp-p Do we need both?
  19246. (defun org-in-regexp (re &optional nlines visually)
  19247. "Check if point is inside a match of regexp.
  19248. Normally only the current line is checked, but you can include NLINES extra
  19249. lines both before and after point into the search.
  19250. If VISUALLY is set, require that the cursor is not after the match but
  19251. really on, so that the block visually is on the match."
  19252. (catch 'exit
  19253. (let ((pos (point))
  19254. (eol (point-at-eol (+ 1 (or nlines 0))))
  19255. (inc (if visually 1 0)))
  19256. (save-excursion
  19257. (beginning-of-line (- 1 (or nlines 0)))
  19258. (while (re-search-forward re eol t)
  19259. (if (and (<= (match-beginning 0) pos)
  19260. (>= (+ inc (match-end 0)) pos))
  19261. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19262. (defun org-at-regexp-p (regexp)
  19263. "Is point inside a match of REGEXP in the current line?"
  19264. (catch 'exit
  19265. (save-excursion
  19266. (let ((pos (point)) (end (point-at-eol)))
  19267. (beginning-of-line 1)
  19268. (while (re-search-forward regexp end t)
  19269. (if (and (<= (match-beginning 0) pos)
  19270. (>= (match-end 0) pos))
  19271. (throw 'exit t)))
  19272. nil))))
  19273. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19274. "Non-nil when point is between matches of START-RE and END-RE.
  19275. Also return a non-nil value when point is on one of the matches.
  19276. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19277. buffer positions. Default values are the positions of headlines
  19278. surrounding the point.
  19279. The functions returns a cons cell whose car (resp. cdr) is the
  19280. position before START-RE (resp. after END-RE)."
  19281. (save-match-data
  19282. (let ((pos (point))
  19283. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19284. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19285. beg end)
  19286. (save-excursion
  19287. ;; Point is on a block when on START-RE or if START-RE can be
  19288. ;; found before it...
  19289. (and (or (org-at-regexp-p start-re)
  19290. (re-search-backward start-re limit-up t))
  19291. (setq beg (match-beginning 0))
  19292. ;; ... and END-RE after it...
  19293. (goto-char (match-end 0))
  19294. (re-search-forward end-re limit-down t)
  19295. (> (setq end (match-end 0)) pos)
  19296. ;; ... without another START-RE in-between.
  19297. (goto-char (match-beginning 0))
  19298. (not (re-search-backward start-re (1+ beg) t))
  19299. ;; Return value.
  19300. (cons beg end))))))
  19301. (defun org-in-block-p (names)
  19302. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19303. NAMES is a list of strings containing names of blocks.
  19304. Return first block name matched, or nil. Beware that in case of
  19305. nested blocks, the returned name may not belong to the closest
  19306. block from point."
  19307. (save-match-data
  19308. (catch 'exit
  19309. (let ((case-fold-search t)
  19310. (lim-up (save-excursion (outline-previous-heading)))
  19311. (lim-down (save-excursion (outline-next-heading))))
  19312. (mapc (lambda (name)
  19313. (let ((n (regexp-quote name)))
  19314. (when (org-between-regexps-p
  19315. (concat "^[ \t]*#\\+begin_" n)
  19316. (concat "^[ \t]*#\\+end_" n)
  19317. lim-up lim-down)
  19318. (throw 'exit n))))
  19319. names))
  19320. nil)))
  19321. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19322. "Call `multi-occur' with buffers for all agenda files."
  19323. (interactive "sOrg-files matching: \np")
  19324. (let* ((files (org-agenda-files))
  19325. (tnames (mapcar 'file-truename files))
  19326. (extra org-agenda-text-search-extra-files)
  19327. f)
  19328. (when (eq (car extra) 'agenda-archives)
  19329. (setq extra (cdr extra))
  19330. (setq files (org-add-archive-files files)))
  19331. (while (setq f (pop extra))
  19332. (unless (member (file-truename f) tnames)
  19333. (add-to-list 'files f 'append)
  19334. (add-to-list 'tnames (file-truename f) 'append)))
  19335. (multi-occur
  19336. (mapcar (lambda (x)
  19337. (with-current-buffer
  19338. (or (get-file-buffer x) (find-file-noselect x))
  19339. (widen)
  19340. (current-buffer)))
  19341. files)
  19342. regexp)))
  19343. (if (boundp 'occur-mode-find-occurrence-hook)
  19344. ;; Emacs 23
  19345. (add-hook 'occur-mode-find-occurrence-hook
  19346. (lambda ()
  19347. (when (derived-mode-p 'org-mode)
  19348. (org-reveal))))
  19349. ;; Emacs 22
  19350. (defadvice occur-mode-goto-occurrence
  19351. (after org-occur-reveal activate)
  19352. (and (derived-mode-p 'org-mode) (org-reveal)))
  19353. (defadvice occur-mode-goto-occurrence-other-window
  19354. (after org-occur-reveal activate)
  19355. (and (derived-mode-p 'org-mode) (org-reveal)))
  19356. (defadvice occur-mode-display-occurrence
  19357. (after org-occur-reveal activate)
  19358. (when (derived-mode-p 'org-mode)
  19359. (let ((pos (occur-mode-find-occurrence)))
  19360. (with-current-buffer (marker-buffer pos)
  19361. (save-excursion
  19362. (goto-char pos)
  19363. (org-reveal)))))))
  19364. (defun org-occur-link-in-agenda-files ()
  19365. "Create a link and search for it in the agendas.
  19366. The link is not stored in `org-stored-links', it is just created
  19367. for the search purpose."
  19368. (interactive)
  19369. (let ((link (condition-case nil
  19370. (org-store-link nil)
  19371. (error "Unable to create a link to here"))))
  19372. (org-occur-in-agenda-files (regexp-quote link))))
  19373. (defun org-uniquify (list)
  19374. "Remove duplicate elements from LIST."
  19375. (let (res)
  19376. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19377. res))
  19378. (defun org-delete-all (elts list)
  19379. "Remove all elements in ELTS from LIST."
  19380. (while elts
  19381. (setq list (delete (pop elts) list)))
  19382. list)
  19383. (defun org-count (cl-item cl-seq)
  19384. "Count the number of occurrences of ITEM in SEQ.
  19385. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19386. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19387. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19388. (while (< cl-start cl-end)
  19389. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19390. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19391. (setq cl-start (1+ cl-start)))
  19392. cl-count))
  19393. (defun org-remove-if (predicate seq)
  19394. "Remove everything from SEQ that fulfills PREDICATE."
  19395. (let (res e)
  19396. (while seq
  19397. (setq e (pop seq))
  19398. (if (not (funcall predicate e)) (push e res)))
  19399. (nreverse res)))
  19400. (defun org-remove-if-not (predicate seq)
  19401. "Remove everything from SEQ that does not fulfill PREDICATE."
  19402. (let (res e)
  19403. (while seq
  19404. (setq e (pop seq))
  19405. (if (funcall predicate e) (push e res)))
  19406. (nreverse res)))
  19407. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19408. "Reduce two-argument FUNCTION across SEQ.
  19409. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19410. \":initial-value\" removed."
  19411. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19412. (cadr (memq :initial-value cl-keys)))
  19413. (cl-seq (pop cl-seq))
  19414. (t (funcall cl-func)))))
  19415. (while cl-seq
  19416. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19417. cl-accum))
  19418. (defun org-back-over-empty-lines ()
  19419. "Move backwards over whitespace, to the beginning of the first empty line.
  19420. Returns the number of empty lines passed."
  19421. (let ((pos (point)))
  19422. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19423. (skip-chars-backward " \t\n\r")
  19424. (unless (eobp)
  19425. (forward-line -1)))
  19426. (beginning-of-line 2)
  19427. (goto-char (min (point) pos))
  19428. (count-lines (point) pos)))
  19429. (defun org-skip-whitespace ()
  19430. (skip-chars-forward " \t\n\r"))
  19431. (defun org-point-in-group (point group &optional context)
  19432. "Check if POINT is in match-group GROUP.
  19433. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19434. match. If the match group does not exist or point is not inside it,
  19435. return nil."
  19436. (and (match-beginning group)
  19437. (>= point (match-beginning group))
  19438. (<= point (match-end group))
  19439. (if context
  19440. (list context (match-beginning group) (match-end group))
  19441. t)))
  19442. (defun org-switch-to-buffer-other-window (&rest args)
  19443. "Switch to buffer in a second window on the current frame.
  19444. In particular, do not allow pop-up frames.
  19445. Returns the newly created buffer."
  19446. (org-no-popups
  19447. (apply 'switch-to-buffer-other-window args)))
  19448. (defun org-combine-plists (&rest plists)
  19449. "Create a single property list from all plists in PLISTS.
  19450. The process starts by copying the first list, and then setting properties
  19451. from the other lists. Settings in the last list are the most significant
  19452. ones and overrule settings in the other lists."
  19453. (let ((rtn (copy-sequence (pop plists)))
  19454. p v ls)
  19455. (while plists
  19456. (setq ls (pop plists))
  19457. (while ls
  19458. (setq p (pop ls) v (pop ls))
  19459. (setq rtn (plist-put rtn p v))))
  19460. rtn))
  19461. (defun org-replace-escapes (string table)
  19462. "Replace %-escapes in STRING with values in TABLE.
  19463. TABLE is an association list with keys like \"%a\" and string values.
  19464. The sequences in STRING may contain normal field width and padding information,
  19465. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19466. so values can contain further %-escapes if they are define later in TABLE."
  19467. (let ((tbl (copy-alist table))
  19468. (case-fold-search nil)
  19469. (pchg 0)
  19470. e re rpl)
  19471. (while (setq e (pop tbl))
  19472. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19473. (when (and (cdr e) (string-match re (cdr e)))
  19474. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19475. (safe "SREF"))
  19476. (add-text-properties 0 3 (list 'sref sref) safe)
  19477. (setcdr e (replace-match safe t t (cdr e)))))
  19478. (while (string-match re string)
  19479. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19480. (cdr e)))
  19481. (setq string (replace-match rpl t t string))))
  19482. (while (setq pchg (next-property-change pchg string))
  19483. (let ((sref (get-text-property pchg 'sref string)))
  19484. (when (and sref (string-match "SREF" string pchg))
  19485. (setq string (replace-match sref t t string)))))
  19486. string))
  19487. (defun org-sublist (list start end)
  19488. "Return a section of LIST, from START to END.
  19489. Counting starts at 1."
  19490. (let (rtn (c start))
  19491. (setq list (nthcdr (1- start) list))
  19492. (while (and list (<= c end))
  19493. (push (pop list) rtn)
  19494. (setq c (1+ c)))
  19495. (nreverse rtn)))
  19496. (defun org-find-base-buffer-visiting (file)
  19497. "Like `find-buffer-visiting' but always return the base buffer and
  19498. not an indirect buffer."
  19499. (let ((buf (or (get-file-buffer file)
  19500. (find-buffer-visiting file))))
  19501. (if buf
  19502. (or (buffer-base-buffer buf) buf)
  19503. nil)))
  19504. (defun org-image-file-name-regexp (&optional extensions)
  19505. "Return regexp matching the file names of images.
  19506. If EXTENSIONS is given, only match these."
  19507. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19508. (image-file-name-regexp)
  19509. (let ((image-file-name-extensions
  19510. (or extensions
  19511. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19512. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19513. (concat "\\."
  19514. (regexp-opt (nconc (mapcar 'upcase
  19515. image-file-name-extensions)
  19516. image-file-name-extensions)
  19517. t)
  19518. "\\'"))))
  19519. (defun org-file-image-p (file &optional extensions)
  19520. "Return non-nil if FILE is an image."
  19521. (save-match-data
  19522. (string-match (org-image-file-name-regexp extensions) file)))
  19523. (defun org-get-cursor-date (&optional with-time)
  19524. "Return the date at cursor in as a time.
  19525. This works in the calendar and in the agenda, anywhere else it just
  19526. returns the current time.
  19527. If WITH-TIME is non-nil, returns the time of the event at point (in
  19528. the agenda) or the current time of the day."
  19529. (let (date day defd tp tm hod mod)
  19530. (when with-time
  19531. (setq tp (get-text-property (point) 'time))
  19532. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19533. (setq hod (string-to-number (match-string 1 tp))
  19534. mod (string-to-number (match-string 2 tp))))
  19535. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19536. mod (nth 1 (decode-time (current-time))))))
  19537. (cond
  19538. ((eq major-mode 'calendar-mode)
  19539. (setq date (calendar-cursor-to-date)
  19540. defd (encode-time 0 (or mod 0) (or hod 0)
  19541. (nth 1 date) (nth 0 date) (nth 2 date))))
  19542. ((eq major-mode 'org-agenda-mode)
  19543. (setq day (get-text-property (point) 'day))
  19544. (if day
  19545. (setq date (calendar-gregorian-from-absolute day)
  19546. defd (encode-time 0 (or mod 0) (or hod 0)
  19547. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19548. (or defd (current-time))))
  19549. (defun org-mark-subtree (&optional up)
  19550. "Mark the current subtree.
  19551. This puts point at the start of the current subtree, and mark at
  19552. the end. If a numeric prefix UP is given, move up into the
  19553. hierarchy of headlines by UP levels before marking the subtree."
  19554. (interactive "P")
  19555. (org-with-limited-levels
  19556. (cond ((org-at-heading-p) (beginning-of-line))
  19557. ((org-before-first-heading-p) (error "Not in a subtree"))
  19558. (t (outline-previous-visible-heading 1))))
  19559. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19560. (if (org-called-interactively-p 'any)
  19561. (call-interactively 'org-mark-element)
  19562. (org-mark-element)))
  19563. ;;; Macros
  19564. ;; Macros are expanded with `org-macro-replace-all', which relies
  19565. ;; internally on `org-macro-expand'.
  19566. ;; Default templates for expansion are stored in the buffer-local
  19567. ;; variable `org-macro-templates'. This variable is updated by
  19568. ;; `org-macro-initialize-templates'.
  19569. ;; Along with macros defined through #+MACRO: keyword, default
  19570. ;; templates include the following hard-coded macros:
  19571. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19572. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19573. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19574. ;; {{{title}}} will also be provided.
  19575. (defvar org-macro-templates nil
  19576. "Alist containing all macro templates in current buffer.
  19577. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19578. stands for macro's name and template for its replacement value,
  19579. both as strings. This is an internal variable. Do not set it
  19580. directly, use instead:
  19581. #+MACRO: name template")
  19582. (make-variable-buffer-local 'org-macro-templates)
  19583. (defun org-macro-expand (macro templates)
  19584. "Return expanded MACRO, as a string.
  19585. MACRO is an object, obtained, for example, with
  19586. `org-element-context'. TEMPLATES is an alist of templates used
  19587. for expansion. See `org-macro-templates' for a buffer-local
  19588. default value. Return nil if no template was found."
  19589. (let ((template
  19590. ;; Macro names are case-insensitive.
  19591. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19592. (when template
  19593. (let ((value (replace-regexp-in-string
  19594. "\\$[0-9]+"
  19595. (lambda (arg)
  19596. (or (nth (1- (string-to-number (substring arg 1)))
  19597. (org-element-property :args macro))
  19598. ;; No argument provided: remove
  19599. ;; place-holder.
  19600. ""))
  19601. template)))
  19602. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19603. (when (string-match "\\`(eval\\>" value)
  19604. (setq value (eval (read value))))
  19605. ;; Return string.
  19606. (format "%s" (or value ""))))))
  19607. (defun org-macro-replace-all (templates)
  19608. "Replace all macros in current buffer by their expansion.
  19609. TEMPLATES is an alist of templates used for expansion. See
  19610. `org-macro-templates' for a buffer-local default value."
  19611. (save-excursion
  19612. (goto-char (point-min))
  19613. (let (record)
  19614. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19615. (let ((object (org-element-context)))
  19616. (when (eq (org-element-type object) 'macro)
  19617. (let* ((value (org-macro-expand object templates))
  19618. (begin (org-element-property :begin object))
  19619. (signature (list begin
  19620. object
  19621. (org-element-property :args object))))
  19622. ;; Avoid circular dependencies by checking if the same
  19623. ;; macro with the same arguments is expanded at the same
  19624. ;; position twice.
  19625. (if (member signature record)
  19626. (error "Circular macro expansion: %s"
  19627. (org-element-property :key object))
  19628. (when value
  19629. (push signature record)
  19630. (delete-region
  19631. begin
  19632. ;; Preserve white spaces after the macro.
  19633. (progn (goto-char (org-element-property :end object))
  19634. (skip-chars-backward " \t")
  19635. (point)))
  19636. ;; Leave point before replacement in case of recursive
  19637. ;; expansions.
  19638. (save-excursion (insert value)))))))))))
  19639. (defun org-macro-initialize-templates ()
  19640. "Collect macro templates defined in current buffer.
  19641. Templates are stored in buffer-local variable
  19642. `org-macro-templates'. In addition to buffer-defined macros, the
  19643. function installs the following ones: \"property\",
  19644. \"time\". and, if the buffer is associated to a file,
  19645. \"input-file\" and \"modification-time\"."
  19646. (let ((case-fold-search t)
  19647. (set-template
  19648. (lambda (cell)
  19649. ;; Add CELL to `org-macro-templates' if there's no
  19650. ;; association matching its name already. Otherwise,
  19651. ;; replace old association with the new one in that
  19652. ;; variable.
  19653. (let ((old-template (assoc (car cell) org-macro-templates)))
  19654. (if old-template (setcdr old-template (cdr cell))
  19655. (push cell org-macro-templates))))))
  19656. ;; Install buffer-local macros.
  19657. (org-with-wide-buffer
  19658. (goto-char (point-min))
  19659. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19660. (let ((element (org-element-at-point)))
  19661. (when (eq (org-element-type element) 'keyword)
  19662. (let ((value (org-element-property :value element)))
  19663. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19664. (funcall set-template
  19665. (cons (match-string 1 value)
  19666. (or (match-string 2 value) "")))))))))
  19667. ;; Install hard-coded macros.
  19668. (mapc (lambda (cell) (funcall set-template cell))
  19669. (list
  19670. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19671. (cons "time" "(eval (format-time-string \"$1\"))")))
  19672. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19673. (when (and visited-file (file-exists-p visited-file))
  19674. (mapc (lambda (cell) (funcall set-template cell))
  19675. (list
  19676. (cons "input-file" (file-name-nondirectory visited-file))
  19677. (cons "modification-time"
  19678. (format "(eval (format-time-string \"$1\" '%s))"
  19679. (prin1-to-string
  19680. (nth 5 (file-attributes visited-file)))))))))))
  19681. ;;; Indentation
  19682. (defun org-indent-line ()
  19683. "Indent line depending on context."
  19684. (interactive)
  19685. (let* ((pos (point))
  19686. (itemp (org-at-item-p))
  19687. (case-fold-search t)
  19688. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19689. (inline-task-p (and (featurep 'org-inlinetask)
  19690. (org-inlinetask-in-task-p)))
  19691. (inline-re (and inline-task-p
  19692. (org-inlinetask-outline-regexp)))
  19693. column)
  19694. (if (and orgstruct-is-++ (eq pos (point)))
  19695. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19696. (indent-according-to-mode))
  19697. (beginning-of-line 1)
  19698. (cond
  19699. ;; Headings
  19700. ((looking-at org-outline-regexp) (setq column 0))
  19701. ;; Included files
  19702. ((looking-at "#\\+include:") (setq column 0))
  19703. ;; Footnote definition
  19704. ((looking-at org-footnote-definition-re) (setq column 0))
  19705. ;; Literal examples
  19706. ((looking-at "[ \t]*:\\( \\|$\\)")
  19707. (setq column (org-get-indentation))) ; do nothing
  19708. ;; Lists
  19709. ((ignore-errors (goto-char (org-in-item-p)))
  19710. (setq column (if itemp
  19711. (org-get-indentation)
  19712. (org-list-item-body-column (point))))
  19713. (goto-char pos))
  19714. ;; Drawers
  19715. ((and (looking-at "[ \t]*:END:")
  19716. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19717. (save-excursion
  19718. (goto-char (1- (match-beginning 1)))
  19719. (setq column (current-column))))
  19720. ;; Special blocks
  19721. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19722. (save-excursion
  19723. (re-search-backward
  19724. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19725. (setq column (org-get-indentation (match-string 0))))
  19726. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19727. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19728. (save-excursion
  19729. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19730. (setq column
  19731. (cond ((equal (downcase (match-string 1)) "src")
  19732. ;; src blocks: let `org-edit-src-exit' handle them
  19733. (org-get-indentation))
  19734. ((equal (downcase (match-string 1)) "example")
  19735. (max (org-get-indentation)
  19736. (org-get-indentation (match-string 0))))
  19737. (t
  19738. (org-get-indentation (match-string 0))))))
  19739. ;; This line has nothing special, look at the previous relevant
  19740. ;; line to compute indentation
  19741. (t
  19742. (beginning-of-line 0)
  19743. (while (and (not (bobp))
  19744. (not (looking-at org-table-line-regexp))
  19745. (not (looking-at org-drawer-regexp))
  19746. ;; When point started in an inline task, do not move
  19747. ;; above task starting line.
  19748. (not (and inline-task-p (looking-at inline-re)))
  19749. ;; Skip drawers, blocks, empty lines, verbatim,
  19750. ;; comments, tables, footnotes definitions, lists,
  19751. ;; inline tasks.
  19752. (or (and (looking-at "[ \t]*:END:")
  19753. (re-search-backward org-drawer-regexp nil t))
  19754. (and (looking-at "[ \t]*#\\+end_")
  19755. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19756. (looking-at "[ \t]*[\n:#|]")
  19757. (looking-at org-footnote-definition-re)
  19758. (and (ignore-errors (goto-char (org-in-item-p)))
  19759. (goto-char
  19760. (org-list-get-top-point (org-list-struct))))
  19761. (and (not inline-task-p)
  19762. (featurep 'org-inlinetask)
  19763. (org-inlinetask-in-task-p)
  19764. (or (org-inlinetask-goto-beginning) t))))
  19765. (beginning-of-line 0))
  19766. (cond
  19767. ;; There was an heading above.
  19768. ((looking-at "\\*+[ \t]+")
  19769. (if (not org-adapt-indentation)
  19770. (setq column 0)
  19771. (goto-char (match-end 0))
  19772. (setq column (current-column))))
  19773. ;; A drawer had started and is unfinished
  19774. ((looking-at org-drawer-regexp)
  19775. (goto-char (1- (match-beginning 1)))
  19776. (setq column (current-column)))
  19777. ;; Else, nothing noticeable found: get indentation and go on.
  19778. (t (setq column (org-get-indentation))))))
  19779. ;; Now apply indentation and move cursor accordingly
  19780. (goto-char pos)
  19781. (if (<= (current-column) (current-indentation))
  19782. (org-indent-line-to column)
  19783. (save-excursion (org-indent-line-to column)))
  19784. ;; Special polishing for properties, see `org-property-format'
  19785. (setq column (current-column))
  19786. (beginning-of-line 1)
  19787. (if (looking-at
  19788. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19789. (replace-match (concat (match-string 1)
  19790. (format org-property-format
  19791. (match-string 2) (match-string 3)))
  19792. t t))
  19793. (org-move-to-column column))))
  19794. (defun org-indent-drawer ()
  19795. "Indent the drawer at point."
  19796. (interactive)
  19797. (let ((p (point))
  19798. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19799. (match-end 0)))
  19800. (folded
  19801. (save-excursion
  19802. (end-of-line)
  19803. (when (overlays-at (point))
  19804. (member 'invisible (overlay-properties
  19805. (car (overlays-at (point)))))))))
  19806. (when folded (org-cycle))
  19807. (indent-for-tab-command)
  19808. (while (and (move-beginning-of-line 2) (< (point) e))
  19809. (indent-for-tab-command))
  19810. (goto-char p)
  19811. (when folded (org-cycle)))
  19812. (message "Drawer at point indented"))
  19813. (defun org-indent-block ()
  19814. "Indent the block at point."
  19815. (interactive)
  19816. (let ((p (point))
  19817. (case-fold-search t)
  19818. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19819. (match-end 0)))
  19820. (folded
  19821. (save-excursion
  19822. (end-of-line)
  19823. (when (overlays-at (point))
  19824. (member 'invisible (overlay-properties
  19825. (car (overlays-at (point)))))))))
  19826. (when folded (org-cycle))
  19827. (indent-for-tab-command)
  19828. (while (and (move-beginning-of-line 2) (< (point) e))
  19829. (indent-for-tab-command))
  19830. (goto-char p)
  19831. (when folded (org-cycle)))
  19832. (message "Block at point indented"))
  19833. (defun org-indent-region (start end)
  19834. "Indent region."
  19835. (interactive "r")
  19836. (save-excursion
  19837. (let ((line-end (org-current-line end)))
  19838. (goto-char start)
  19839. (while (< (org-current-line) line-end)
  19840. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19841. (t (call-interactively 'org-indent-line)))
  19842. (move-beginning-of-line 2)))))
  19843. ;;; Filling
  19844. ;; We use our own fill-paragraph and auto-fill functions.
  19845. ;; `org-fill-paragraph' relies on adaptive filling and context
  19846. ;; checking. Appropriate `fill-prefix' is computed with
  19847. ;; `org-adaptive-fill-function'.
  19848. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19849. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19850. ;; `org-adaptive-fill-function' value. Internally,
  19851. ;; `org-comment-line-break-function' breaks the line.
  19852. ;; `org-setup-filling' installs filling and auto-filling related
  19853. ;; variables during `org-mode' initialization.
  19854. (defun org-setup-filling ()
  19855. (interactive)
  19856. ;; Prevent auto-fill from inserting unwanted new items.
  19857. (when (boundp 'fill-nobreak-predicate)
  19858. (org-set-local
  19859. 'fill-nobreak-predicate
  19860. (org-uniquify
  19861. (append fill-nobreak-predicate
  19862. '(org-fill-paragraph-separate-nobreak-p
  19863. org-fill-line-break-nobreak-p)))))
  19864. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19865. (org-set-local 'auto-fill-inhibit-regexp nil)
  19866. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19867. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19868. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19869. (defvar org-element-paragraph-separate) ; org-element.el
  19870. (defun org-fill-paragraph-separate-nobreak-p ()
  19871. "Non-nil when a line break at point would insert a new item."
  19872. (looking-at (substring org-element-paragraph-separate 1)))
  19873. (defun org-fill-line-break-nobreak-p ()
  19874. "Non-nil when a line break at point would create an Org line break."
  19875. (save-excursion
  19876. (skip-chars-backward "[ \t]")
  19877. (skip-chars-backward "\\\\")
  19878. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19879. (declare-function message-in-body-p "message" ())
  19880. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19881. (defun org-adaptive-fill-function ()
  19882. "Compute a fill prefix for the current line.
  19883. Return fill prefix, as a string, or nil if current line isn't
  19884. meant to be filled."
  19885. (let (prefix)
  19886. (catch 'exit
  19887. (when (derived-mode-p 'message-mode)
  19888. (save-excursion
  19889. (beginning-of-line)
  19890. (cond ((or (not (message-in-body-p))
  19891. (looking-at orgtbl-line-start-regexp))
  19892. (throw 'exit nil))
  19893. ((looking-at message-cite-prefix-regexp)
  19894. (throw 'exit (match-string-no-properties 0)))
  19895. ((looking-at org-outline-regexp)
  19896. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19897. (org-with-wide-buffer
  19898. (let* ((p (line-beginning-position))
  19899. (element (save-excursion (beginning-of-line)
  19900. (org-element-at-point)))
  19901. (type (org-element-type element))
  19902. (post-affiliated (org-element-property :post-affiliated element)))
  19903. (unless (and post-affiliated (< p post-affiliated))
  19904. (case type
  19905. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19906. (footnote-definition "")
  19907. ((item plain-list)
  19908. (make-string (org-list-item-body-column
  19909. (or post-affiliated
  19910. (org-element-property :begin element)))
  19911. ? ))
  19912. (paragraph
  19913. ;; Fill prefix is usually the same as the current line,
  19914. ;; except if the paragraph is at the beginning of an item.
  19915. (let ((parent (org-element-property :parent element)))
  19916. (cond ((eq (org-element-type parent) 'item)
  19917. (make-string (org-list-item-body-column
  19918. (org-element-property :begin parent))
  19919. ? ))
  19920. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19921. (match-string 0))
  19922. (t ""))))
  19923. (comment-block
  19924. ;; Only fill contents if P is within block boundaries.
  19925. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19926. (forward-line)
  19927. (point)))
  19928. (cend (save-excursion
  19929. (goto-char (org-element-property :end element))
  19930. (skip-chars-backward " \r\t\n")
  19931. (line-beginning-position))))
  19932. (when (and (>= p cbeg) (< p cend))
  19933. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19934. (match-string 0)
  19935. "")))))))))))
  19936. (declare-function message-goto-body "message" ())
  19937. (defvar message-cite-prefix-regexp) ; From message.el
  19938. (defvar org-element-all-objects) ; From org-element.el
  19939. (defun org-fill-paragraph (&optional justify)
  19940. "Fill element at point, when applicable.
  19941. This function only applies to comment blocks, comments, example
  19942. blocks and paragraphs. Also, as a special case, re-align table
  19943. when point is at one.
  19944. If JUSTIFY is non-nil (interactively, with prefix argument),
  19945. justify as well. If `sentence-end-double-space' is non-nil, then
  19946. period followed by one space does not end a sentence, so don't
  19947. break a line there. The variable `fill-column' controls the
  19948. width for filling.
  19949. For convenience, when point is at a plain list, an item or
  19950. a footnote definition, try to fill the first paragraph within."
  19951. (interactive)
  19952. (if (and (derived-mode-p 'message-mode)
  19953. (or (not (message-in-body-p))
  19954. (save-excursion (move-beginning-of-line 1)
  19955. (looking-at message-cite-prefix-regexp))))
  19956. ;; First ensure filling is correct in message-mode.
  19957. (let ((fill-paragraph-function
  19958. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19959. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19960. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19961. (paragraph-separate
  19962. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19963. (fill-paragraph nil))
  19964. (save-excursion
  19965. ;; Move to end of line in order to get the first paragraph
  19966. ;; within a plain list or a footnote definition.
  19967. (end-of-line)
  19968. (let ((element (org-element-at-point)))
  19969. ;; First check if point is in a blank line at the beginning of
  19970. ;; the buffer. In that case, ignore filling.
  19971. (if (< (point) (org-element-property :begin element)) t
  19972. (case (org-element-type element)
  19973. ;; Use major mode filling function is src blocks.
  19974. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19975. ;; Align Org tables, leave table.el tables as-is.
  19976. (table-row (org-table-align) t)
  19977. (table
  19978. (when (eq (org-element-property :type element) 'org)
  19979. (org-table-align))
  19980. t)
  19981. (paragraph
  19982. ;; Paragraphs may contain `line-break' type objects.
  19983. (let ((beg (max (point-min)
  19984. (org-element-property :contents-begin element)))
  19985. (end (min (point-max)
  19986. (org-element-property :contents-end element))))
  19987. ;; Do nothing if point is at an affiliated keyword.
  19988. (if (< (point) beg) t
  19989. (when (derived-mode-p 'message-mode)
  19990. ;; In `message-mode', do not fill following
  19991. ;; citation in current paragraph nor text before
  19992. ;; message body.
  19993. (let ((body-start (save-excursion (message-goto-body))))
  19994. (when body-start (setq beg (max body-start beg))))
  19995. (when (save-excursion
  19996. (re-search-forward
  19997. (concat "^" message-cite-prefix-regexp) end t))
  19998. (setq end (match-beginning 0))))
  19999. ;; Fill paragraph, taking line breaks into
  20000. ;; consideration. For that, slice the paragraph
  20001. ;; using line breaks as separators, and fill the
  20002. ;; parts in reverse order to avoid messing with
  20003. ;; markers.
  20004. (save-excursion
  20005. (goto-char end)
  20006. (mapc
  20007. (lambda (pos)
  20008. (fill-region-as-paragraph pos (point) justify)
  20009. (goto-char pos))
  20010. ;; Find the list of ending positions for line
  20011. ;; breaks in the current paragraph. Add paragraph
  20012. ;; beginning to include first slice.
  20013. (nreverse
  20014. (cons
  20015. beg
  20016. (org-element-map
  20017. (org-element--parse-objects
  20018. beg end nil org-element-all-objects)
  20019. 'line-break
  20020. (lambda (lb) (org-element-property :end lb)))))))
  20021. t)))
  20022. ;; Contents of `comment-block' type elements should be
  20023. ;; filled as plain text, but only if point is within block
  20024. ;; markers.
  20025. (comment-block
  20026. (let* ((case-fold-search t)
  20027. (beg (save-excursion
  20028. (goto-char (org-element-property :begin element))
  20029. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20030. (forward-line)
  20031. (point)))
  20032. (end (save-excursion
  20033. (goto-char (org-element-property :end element))
  20034. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20035. (line-beginning-position))))
  20036. (when (and (>= (point) beg) (< (point) end))
  20037. (fill-region-as-paragraph
  20038. (save-excursion
  20039. (end-of-line)
  20040. (re-search-backward "^[ \t]*$" beg 'move)
  20041. (line-beginning-position))
  20042. (save-excursion
  20043. (beginning-of-line)
  20044. (re-search-forward "^[ \t]*$" end 'move)
  20045. (line-beginning-position))
  20046. justify)))
  20047. t)
  20048. ;; Fill comments.
  20049. (comment (fill-comment-paragraph justify))
  20050. ;; Ignore every other element.
  20051. (otherwise t)))))))
  20052. (defun org-auto-fill-function ()
  20053. "Auto-fill function."
  20054. ;; Check if auto-filling is meaningful.
  20055. (let ((fc (current-fill-column)))
  20056. (when (and fc (> (current-column) fc))
  20057. (let* ((fill-prefix (org-adaptive-fill-function))
  20058. ;; Enforce empty fill prefix, if required. Otherwise, it
  20059. ;; will be computed again.
  20060. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20061. (when fill-prefix (do-auto-fill))))))
  20062. (defun org-comment-line-break-function (&optional soft)
  20063. "Break line at point and indent, continuing comment if within one.
  20064. The inserted newline is marked hard if variable
  20065. `use-hard-newlines' is true, unless optional argument SOFT is
  20066. non-nil."
  20067. (if soft (insert-and-inherit ?\n) (newline 1))
  20068. (save-excursion (forward-char -1) (delete-horizontal-space))
  20069. (delete-horizontal-space)
  20070. (indent-to-left-margin)
  20071. (insert-before-markers-and-inherit fill-prefix))
  20072. ;;; Comments
  20073. ;; Org comments syntax is quite complex. It requires the entire line
  20074. ;; to be just a comment. Also, even with the right syntax at the
  20075. ;; beginning of line, some some elements (i.e. verse-block or
  20076. ;; example-block) don't accept comments. Usual Emacs comment commands
  20077. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20078. ;; Org still relies on `comment-dwim', but cannot trust
  20079. ;; `comment-only-p'. So, `comment-region-function' and
  20080. ;; `uncomment-region-function' both point
  20081. ;; to`org-comment-or-uncomment-region'. Eventually,
  20082. ;; `org-insert-comment' takes care of insertion of comments at the
  20083. ;; beginning of line.
  20084. ;; `org-setup-comments-handling' install comments related variables
  20085. ;; during `org-mode' initialization.
  20086. (defun org-setup-comments-handling ()
  20087. (interactive)
  20088. (org-set-local 'comment-use-syntax nil)
  20089. (org-set-local 'comment-start "# ")
  20090. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20091. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20092. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20093. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20094. (defun org-insert-comment ()
  20095. "Insert an empty comment above current line.
  20096. If the line is empty, insert comment at its beginning."
  20097. (beginning-of-line)
  20098. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20099. (org-indent-line)
  20100. (insert "# "))
  20101. (defvar comment-empty-lines) ; From newcomment.el.
  20102. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20103. "Comment or uncomment each non-blank line in the region.
  20104. Uncomment each non-blank line between BEG and END if it only
  20105. contains commented lines. Otherwise, comment them."
  20106. (save-restriction
  20107. ;; Restrict region
  20108. (narrow-to-region (save-excursion (goto-char beg)
  20109. (skip-chars-forward " \r\t\n" end)
  20110. (line-beginning-position))
  20111. (save-excursion (goto-char end)
  20112. (skip-chars-backward " \r\t\n" beg)
  20113. (line-end-position)))
  20114. (let ((uncommentp
  20115. ;; UNCOMMENTP is non-nil when every non blank line between
  20116. ;; BEG and END is a comment.
  20117. (save-excursion
  20118. (goto-char (point-min))
  20119. (while (and (not (eobp))
  20120. (let ((element (org-element-at-point)))
  20121. (and (eq (org-element-type element) 'comment)
  20122. (goto-char (min (point-max)
  20123. (org-element-property
  20124. :end element)))))))
  20125. (eobp))))
  20126. (if uncommentp
  20127. ;; Only blank lines and comments in region: uncomment it.
  20128. (save-excursion
  20129. (goto-char (point-min))
  20130. (while (not (eobp))
  20131. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20132. (replace-match "" nil nil nil 1))
  20133. (forward-line)))
  20134. ;; Comment each line in region.
  20135. (let ((min-indent (point-max)))
  20136. ;; First find the minimum indentation across all lines.
  20137. (save-excursion
  20138. (goto-char (point-min))
  20139. (while (and (not (eobp)) (not (zerop min-indent)))
  20140. (unless (looking-at "[ \t]*$")
  20141. (setq min-indent (min min-indent (current-indentation))))
  20142. (forward-line)))
  20143. ;; Then loop over all lines.
  20144. (save-excursion
  20145. (goto-char (point-min))
  20146. (while (not (eobp))
  20147. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20148. (org-move-to-column min-indent t)
  20149. (insert comment-start))
  20150. (forward-line))))))))
  20151. ;;; Planning
  20152. ;; This section contains tools to operate on timestamp objects, as
  20153. ;; returned by, e.g. `org-element-context'.
  20154. (defun org-timestamp-has-time-p (timestamp)
  20155. "Non-nil when TIMESTAMP has a time specified."
  20156. (org-element-property :hour-start timestamp))
  20157. (defun org-timestamp-format (timestamp format &optional end utc)
  20158. "Format a TIMESTAMP element into a string.
  20159. FORMAT is a format specifier to be passed to
  20160. `format-time-string'.
  20161. When optional argument END is non-nil, use end of date-range or
  20162. time-range, if possible.
  20163. When optional argument UTC is non-nil, time will be expressed as
  20164. Universal Time."
  20165. (format-time-string
  20166. format
  20167. (apply 'encode-time
  20168. (cons 0
  20169. (mapcar
  20170. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20171. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20172. '(:minute-start :hour-start :day-start :month-start
  20173. :year-start)))))
  20174. utc))
  20175. (defun org-timestamp-split-range (timestamp &optional end)
  20176. "Extract a timestamp object from a date or time range.
  20177. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20178. the end of the range. Otherwise, extract its start.
  20179. Return a new timestamp object sharing the same parent as
  20180. TIMESTAMP."
  20181. (let ((type (org-element-property :type timestamp)))
  20182. (if (memq type '(active inactive diary)) timestamp
  20183. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20184. ;; Set new type.
  20185. (org-element-put-property
  20186. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20187. ;; Copy start properties over end properties if END is
  20188. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20189. (let ((p-alist '((:minute-start . :minute-end)
  20190. (:hour-start . :hour-end)
  20191. (:day-start . :day-end)
  20192. (:month-start . :month-end)
  20193. (:year-start . :year-end))))
  20194. (dolist (p-cell p-alist)
  20195. (org-element-put-property
  20196. split-ts
  20197. (funcall (if end 'car 'cdr) p-cell)
  20198. (org-element-property
  20199. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20200. ;; Eventually refresh `:raw-value'.
  20201. (org-element-put-property split-ts :raw-value nil)
  20202. (org-element-put-property
  20203. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20204. (defun org-timestamp-translate (timestamp &optional boundary)
  20205. "Apply `org-translate-time' on a TIMESTAMP object.
  20206. When optional argument BOUNDARY is non-nil, it is either the
  20207. symbol `start' or `end'. In this case, only translate the
  20208. starting or ending part of TIMESTAMP if it is a date or time
  20209. range. Otherwise, translate both parts."
  20210. (if (and (not boundary)
  20211. (memq (org-element-property :type timestamp)
  20212. '(active-range inactive-range)))
  20213. (concat
  20214. (org-translate-time
  20215. (org-element-property :raw-value
  20216. (org-timestamp-split-range timestamp)))
  20217. "--"
  20218. (org-translate-time
  20219. (org-element-property :raw-value
  20220. (org-timestamp-split-range timestamp t))))
  20221. (org-translate-time
  20222. (org-element-property
  20223. :raw-value
  20224. (if (not boundary) timestamp
  20225. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20226. ;;; Other stuff.
  20227. (defun org-toggle-fixed-width-section (arg)
  20228. "Toggle the fixed-width export.
  20229. If there is no active region, the QUOTE keyword at the current headline is
  20230. inserted or removed. When present, it causes the text between this headline
  20231. and the next to be exported as fixed-width text, and unmodified.
  20232. If there is an active region, this command adds or removes a colon as the
  20233. first character of this line. If the first character of a line is a colon,
  20234. this line is also exported in fixed-width font."
  20235. (interactive "P")
  20236. (let* ((cc 0)
  20237. (regionp (org-region-active-p))
  20238. (beg (if regionp (region-beginning) (point)))
  20239. (end (if regionp (region-end)))
  20240. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20241. (case-fold-search nil)
  20242. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20243. off)
  20244. (if regionp
  20245. (save-excursion
  20246. (goto-char beg)
  20247. (setq cc (current-column))
  20248. (beginning-of-line 1)
  20249. (setq off (looking-at re))
  20250. (while (> nlines 0)
  20251. (setq nlines (1- nlines))
  20252. (beginning-of-line 1)
  20253. (cond
  20254. (arg
  20255. (org-move-to-column cc t)
  20256. (insert ": \n")
  20257. (forward-line -1))
  20258. ((and off (looking-at re))
  20259. (replace-match "" t t nil 1))
  20260. ((not off) (org-move-to-column cc t) (insert ": ")))
  20261. (forward-line 1)))
  20262. (save-excursion
  20263. (org-back-to-heading)
  20264. (cond
  20265. ((looking-at (format org-heading-keyword-regexp-format
  20266. org-quote-string))
  20267. (goto-char (match-end 1))
  20268. (looking-at (concat " +" org-quote-string))
  20269. (replace-match "" t t)
  20270. (when (eolp) (insert " ")))
  20271. ((looking-at org-outline-regexp)
  20272. (goto-char (match-end 0))
  20273. (insert org-quote-string " ")))))))
  20274. (defun org-reftex-citation ()
  20275. "Use reftex-citation to insert a citation into the buffer.
  20276. This looks for a line like
  20277. #+BIBLIOGRAPHY: foo plain option:-d
  20278. and derives from it that foo.bib is the bibliography file relevant
  20279. for this document. It then installs the necessary environment for RefTeX
  20280. to work in this buffer and calls `reftex-citation' to insert a citation
  20281. into the buffer.
  20282. Export of such citations to both LaTeX and HTML is handled by the contributed
  20283. package org-exp-bibtex by Taru Karttunen."
  20284. (interactive)
  20285. (let ((reftex-docstruct-symbol 'rds)
  20286. (reftex-cite-format "\\cite{%l}")
  20287. rds bib)
  20288. (save-excursion
  20289. (save-restriction
  20290. (widen)
  20291. (let ((case-fold-search t)
  20292. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20293. (if (not (save-excursion
  20294. (or (re-search-forward re nil t)
  20295. (re-search-backward re nil t))))
  20296. (error "No bibliography defined in file")
  20297. (setq bib (concat (match-string 1) ".bib")
  20298. rds (list (list 'bib bib)))))))
  20299. (call-interactively 'reftex-citation)))
  20300. ;;;; Functions extending outline functionality
  20301. (defun org-beginning-of-line (&optional arg)
  20302. "Go to the beginning of the current line. If that is invisible, continue
  20303. to a visible line beginning. This makes the function of C-a more intuitive.
  20304. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20305. first attempt, and only move to after the tags when the cursor is already
  20306. beyond the end of the headline."
  20307. (interactive "P")
  20308. (let ((pos (point))
  20309. (special (if (consp org-special-ctrl-a/e)
  20310. (car org-special-ctrl-a/e)
  20311. org-special-ctrl-a/e))
  20312. refpos)
  20313. (if (org-bound-and-true-p visual-line-mode)
  20314. (beginning-of-visual-line 1)
  20315. (beginning-of-line 1))
  20316. (if (and arg (fboundp 'move-beginning-of-line))
  20317. (call-interactively 'move-beginning-of-line)
  20318. (if (bobp)
  20319. nil
  20320. (backward-char 1)
  20321. (if (org-truely-invisible-p)
  20322. (while (and (not (bobp)) (org-truely-invisible-p))
  20323. (backward-char 1)
  20324. (beginning-of-line 1))
  20325. (forward-char 1))))
  20326. (when special
  20327. (cond
  20328. ((and (looking-at org-complex-heading-regexp)
  20329. (= (char-after (match-end 1)) ?\ ))
  20330. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20331. (point-at-eol)))
  20332. (goto-char
  20333. (if (eq special t)
  20334. (cond ((> pos refpos) refpos)
  20335. ((= pos (point)) refpos)
  20336. (t (point)))
  20337. (cond ((> pos (point)) (point))
  20338. ((not (eq last-command this-command)) (point))
  20339. (t refpos)))))
  20340. ((org-at-item-p)
  20341. ;; Being at an item and not looking at an the item means point
  20342. ;; was previously moved to beginning of a visual line, which
  20343. ;; doesn't contain the item. Therefore, do nothing special,
  20344. ;; just stay here.
  20345. (when (looking-at org-list-full-item-re)
  20346. ;; Set special position at first white space character after
  20347. ;; bullet, and check-box, if any.
  20348. (let ((after-bullet
  20349. (let ((box (match-end 3)))
  20350. (if (not box) (match-end 1)
  20351. (let ((after (char-after box)))
  20352. (if (and after (= after ? )) (1+ box) box))))))
  20353. ;; Special case: Move point to special position when
  20354. ;; currently after it or at beginning of line.
  20355. (if (eq special t)
  20356. (when (or (> pos after-bullet) (= (point) pos))
  20357. (goto-char after-bullet))
  20358. ;; Reversed case: Move point to special position when
  20359. ;; point was already at beginning of line and command is
  20360. ;; repeated.
  20361. (when (and (= (point) pos) (eq last-command this-command))
  20362. (goto-char after-bullet))))))))
  20363. (org-no-warnings
  20364. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20365. (defun org-end-of-line (&optional arg)
  20366. "Go to the end of the line.
  20367. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20368. tags on the first attempt, and only move to after the tags when
  20369. the cursor is already beyond the end of the headline."
  20370. (interactive "P")
  20371. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20372. org-special-ctrl-a/e))
  20373. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20374. 'end-of-visual-line)
  20375. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20376. (t 'end-of-line))))
  20377. (if (or (not special) arg) (call-interactively move-fun)
  20378. (let* ((element (save-excursion (beginning-of-line)
  20379. (org-element-at-point)))
  20380. (type (org-element-type element)))
  20381. (cond
  20382. ((memq type '(headline inlinetask))
  20383. (let ((pos (point)))
  20384. (beginning-of-line 1)
  20385. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20386. (if (eq special t)
  20387. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20388. (goto-char (match-beginning 1))
  20389. (goto-char (match-end 0)))
  20390. (if (or (< pos (match-end 0))
  20391. (not (eq this-command last-command)))
  20392. (goto-char (match-end 0))
  20393. (goto-char (match-beginning 1))))
  20394. (call-interactively move-fun))))
  20395. ((org-element-property :hiddenp element)
  20396. ;; If element is hidden, `move-end-of-line' would put point
  20397. ;; after it. Use `end-of-line' to stay on current line.
  20398. (call-interactively 'end-of-line))
  20399. (t (call-interactively move-fun)))))
  20400. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20401. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20402. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20403. (defun org-backward-sentence (&optional arg)
  20404. "Go to beginning of sentence, or beginning of table field.
  20405. This will call `backward-sentence' or `org-table-beginning-of-field',
  20406. depending on context."
  20407. (interactive "P")
  20408. (cond
  20409. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20410. (t (call-interactively 'backward-sentence))))
  20411. (defun org-forward-sentence (&optional arg)
  20412. "Go to end of sentence, or end of table field.
  20413. This will call `forward-sentence' or `org-table-end-of-field',
  20414. depending on context."
  20415. (interactive "P")
  20416. (cond
  20417. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20418. (t (call-interactively 'forward-sentence))))
  20419. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20420. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20421. (defun org-kill-line (&optional arg)
  20422. "Kill line, to tags or end of line."
  20423. (interactive "P")
  20424. (cond
  20425. ((or (not org-special-ctrl-k)
  20426. (bolp)
  20427. (not (org-at-heading-p)))
  20428. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20429. org-ctrl-k-protect-subtree)
  20430. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20431. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20432. (error "C-k aborted - would kill hidden subtree")))
  20433. (call-interactively
  20434. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20435. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20436. (kill-region (point) (match-beginning 1))
  20437. (org-set-tags nil t))
  20438. (t (kill-region (point) (point-at-eol)))))
  20439. (define-key org-mode-map "\C-k" 'org-kill-line)
  20440. (defun org-yank (&optional arg)
  20441. "Yank. If the kill is a subtree, treat it specially.
  20442. This command will look at the current kill and check if is a single
  20443. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20444. cursor is at the beginning of a line or after the stars of a currently
  20445. empty headline, then the yank is handled specially. How exactly depends
  20446. on the value of the following variables, both set by default.
  20447. org-yank-folded-subtrees
  20448. When set, the subtree(s) will be folded after insertion, but only
  20449. if doing so would now swallow text after the yanked text.
  20450. org-yank-adjusted-subtrees
  20451. When set, the subtree will be promoted or demoted in order to
  20452. fit into the local outline tree structure, which means that the level
  20453. will be adjusted so that it becomes the smaller one of the two
  20454. *visible* surrounding headings.
  20455. Any prefix to this command will cause `yank' to be called directly with
  20456. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20457. will just
  20458. plainly yank the text as it is.
  20459. \[1] The test checks if the first non-white line is a heading
  20460. and if there are no other headings with fewer stars."
  20461. (interactive "P")
  20462. (org-yank-generic 'yank arg))
  20463. (defun org-yank-generic (command arg)
  20464. "Perform some yank-like command.
  20465. This function implements the behavior described in the `org-yank'
  20466. documentation. However, it has been generalized to work for any
  20467. interactive command with similar behavior."
  20468. ;; pretend to be command COMMAND
  20469. (setq this-command command)
  20470. (if arg
  20471. (call-interactively command)
  20472. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20473. (and (org-kill-is-subtree-p)
  20474. (or (bolp)
  20475. (and (looking-at "[ \t]*$")
  20476. (string-match
  20477. "\\`\\*+\\'"
  20478. (buffer-substring (point-at-bol) (point)))))))
  20479. swallowp)
  20480. (cond
  20481. ((and subtreep org-yank-folded-subtrees)
  20482. (let ((beg (point))
  20483. end)
  20484. (if (and subtreep org-yank-adjusted-subtrees)
  20485. (org-paste-subtree nil nil 'for-yank)
  20486. (call-interactively command))
  20487. (setq end (point))
  20488. (goto-char beg)
  20489. (when (and (bolp) subtreep
  20490. (not (setq swallowp
  20491. (org-yank-folding-would-swallow-text beg end))))
  20492. (org-with-limited-levels
  20493. (or (looking-at org-outline-regexp)
  20494. (re-search-forward org-outline-regexp-bol end t))
  20495. (while (and (< (point) end) (looking-at org-outline-regexp))
  20496. (hide-subtree)
  20497. (org-cycle-show-empty-lines 'folded)
  20498. (condition-case nil
  20499. (outline-forward-same-level 1)
  20500. (error (goto-char end))))))
  20501. (when swallowp
  20502. (message
  20503. "Inserted text not folded because that would swallow text"))
  20504. (goto-char end)
  20505. (skip-chars-forward " \t\n\r")
  20506. (beginning-of-line 1)
  20507. (push-mark beg 'nomsg)))
  20508. ((and subtreep org-yank-adjusted-subtrees)
  20509. (let ((beg (point-at-bol)))
  20510. (org-paste-subtree nil nil 'for-yank)
  20511. (push-mark beg 'nomsg)))
  20512. (t
  20513. (call-interactively command))))))
  20514. (defun org-yank-folding-would-swallow-text (beg end)
  20515. "Would hide-subtree at BEG swallow any text after END?"
  20516. (let (level)
  20517. (org-with-limited-levels
  20518. (save-excursion
  20519. (goto-char beg)
  20520. (when (or (looking-at org-outline-regexp)
  20521. (re-search-forward org-outline-regexp-bol end t))
  20522. (setq level (org-outline-level)))
  20523. (goto-char end)
  20524. (skip-chars-forward " \t\r\n\v\f")
  20525. (if (or (eobp)
  20526. (and (bolp) (looking-at org-outline-regexp)
  20527. (<= (org-outline-level) level)))
  20528. nil ; Nothing would be swallowed
  20529. t))))) ; something would swallow
  20530. (define-key org-mode-map "\C-y" 'org-yank)
  20531. (defun org-truely-invisible-p ()
  20532. "Check if point is at a character currently not visible.
  20533. This version does not only check the character property, but also
  20534. `visible-mode'."
  20535. ;; Early versions of noutline don't have `outline-invisible-p'.
  20536. (if (org-bound-and-true-p visible-mode)
  20537. nil
  20538. (outline-invisible-p)))
  20539. (defun org-invisible-p2 ()
  20540. "Check if point is at a character currently not visible."
  20541. (save-excursion
  20542. (if (and (eolp) (not (bobp))) (backward-char 1))
  20543. ;; Early versions of noutline don't have `outline-invisible-p'.
  20544. (outline-invisible-p)))
  20545. (defun org-back-to-heading (&optional invisible-ok)
  20546. "Call `outline-back-to-heading', but provide a better error message."
  20547. (condition-case nil
  20548. (outline-back-to-heading invisible-ok)
  20549. (error (error "Before first headline at position %d in buffer %s"
  20550. (point) (current-buffer)))))
  20551. (defun org-before-first-heading-p ()
  20552. "Before first heading?"
  20553. (save-excursion
  20554. (end-of-line)
  20555. (null (re-search-backward org-outline-regexp-bol nil t))))
  20556. (defun org-at-heading-p (&optional ignored)
  20557. (outline-on-heading-p t))
  20558. ;; Compatibility alias with Org versions < 7.8.03
  20559. (defalias 'org-on-heading-p 'org-at-heading-p)
  20560. (defun org-at-comment-p nil
  20561. "Is cursor in a line starting with a # character?"
  20562. (save-excursion
  20563. (beginning-of-line)
  20564. (looking-at "^#")))
  20565. (defun org-at-drawer-p nil
  20566. "Is cursor at a drawer keyword?"
  20567. (save-excursion
  20568. (move-beginning-of-line 1)
  20569. (looking-at org-drawer-regexp)))
  20570. (defun org-at-block-p nil
  20571. "Is cursor at a block keyword?"
  20572. (save-excursion
  20573. (move-beginning-of-line 1)
  20574. (looking-at org-block-regexp)))
  20575. (defun org-point-at-end-of-empty-headline ()
  20576. "If point is at the end of an empty headline, return t, else nil.
  20577. If the heading only contains a TODO keyword, it is still still considered
  20578. empty."
  20579. (and (looking-at "[ \t]*$")
  20580. (when org-todo-line-regexp
  20581. (save-excursion
  20582. (beginning-of-line 1)
  20583. (let ((case-fold-search nil))
  20584. (looking-at org-todo-line-regexp)
  20585. (string= (match-string 3) ""))))))
  20586. (defun org-at-heading-or-item-p ()
  20587. (or (org-at-heading-p) (org-at-item-p)))
  20588. (defun org-at-target-p ()
  20589. (or (org-in-regexp org-radio-target-regexp)
  20590. (org-in-regexp org-target-regexp)))
  20591. ;; Compatibility alias with Org versions < 7.8.03
  20592. (defalias 'org-on-target-p 'org-at-target-p)
  20593. (defun org-up-heading-all (arg)
  20594. "Move to the heading line of which the present line is a subheading.
  20595. This function considers both visible and invisible heading lines.
  20596. With argument, move up ARG levels."
  20597. (if (fboundp 'outline-up-heading-all)
  20598. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20599. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20600. (defun org-up-heading-safe ()
  20601. "Move to the heading line of which the present line is a subheading.
  20602. This version will not throw an error. It will return the level of the
  20603. headline found, or nil if no higher level is found.
  20604. Also, this function will be a lot faster than `outline-up-heading',
  20605. because it relies on stars being the outline starters. This can really
  20606. make a significant difference in outlines with very many siblings."
  20607. (let (start-level re)
  20608. (org-back-to-heading t)
  20609. (setq start-level (funcall outline-level))
  20610. (if (equal start-level 1)
  20611. nil
  20612. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20613. (if (re-search-backward re nil t)
  20614. (funcall outline-level)))))
  20615. (defun org-first-sibling-p ()
  20616. "Is this heading the first child of its parents?"
  20617. (interactive)
  20618. (let ((re org-outline-regexp-bol)
  20619. level l)
  20620. (unless (org-at-heading-p t)
  20621. (error "Not at a heading"))
  20622. (setq level (funcall outline-level))
  20623. (save-excursion
  20624. (if (not (re-search-backward re nil t))
  20625. t
  20626. (setq l (funcall outline-level))
  20627. (< l level)))))
  20628. (defun org-goto-sibling (&optional previous)
  20629. "Goto the next sibling, even if it is invisible.
  20630. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20631. when a sibling was found. When none is found, return nil and don't
  20632. move point."
  20633. (let ((fun (if previous 're-search-backward 're-search-forward))
  20634. (pos (point))
  20635. (re org-outline-regexp-bol)
  20636. level l)
  20637. (when (condition-case nil (org-back-to-heading t) (error nil))
  20638. (setq level (funcall outline-level))
  20639. (catch 'exit
  20640. (or previous (forward-char 1))
  20641. (while (funcall fun re nil t)
  20642. (setq l (funcall outline-level))
  20643. (when (< l level) (goto-char pos) (throw 'exit nil))
  20644. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20645. (goto-char pos)
  20646. nil))))
  20647. (defun org-show-siblings ()
  20648. "Show all siblings of the current headline."
  20649. (save-excursion
  20650. (while (org-goto-sibling) (org-flag-heading nil)))
  20651. (save-excursion
  20652. (while (org-goto-sibling 'previous)
  20653. (org-flag-heading nil))))
  20654. (defun org-goto-first-child ()
  20655. "Goto the first child, even if it is invisible.
  20656. Return t when a child was found. Otherwise don't move point and
  20657. return nil."
  20658. (let (level (pos (point)) (re org-outline-regexp-bol))
  20659. (when (condition-case nil (org-back-to-heading t) (error nil))
  20660. (setq level (outline-level))
  20661. (forward-char 1)
  20662. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20663. (progn (goto-char (match-beginning 0)) t)
  20664. (goto-char pos) nil))))
  20665. (defun org-show-hidden-entry ()
  20666. "Show an entry where even the heading is hidden."
  20667. (save-excursion
  20668. (org-show-entry)))
  20669. (defun org-flag-heading (flag &optional entry)
  20670. "Flag the current heading. FLAG non-nil means make invisible.
  20671. When ENTRY is non-nil, show the entire entry."
  20672. (save-excursion
  20673. (org-back-to-heading t)
  20674. ;; Check if we should show the entire entry
  20675. (if entry
  20676. (progn
  20677. (org-show-entry)
  20678. (save-excursion
  20679. (and (outline-next-heading)
  20680. (org-flag-heading nil))))
  20681. (outline-flag-region (max (point-min) (1- (point)))
  20682. (save-excursion (outline-end-of-heading) (point))
  20683. flag))))
  20684. (defun org-get-next-sibling ()
  20685. "Move to next heading of the same level, and return point.
  20686. If there is no such heading, return nil.
  20687. This is like outline-next-sibling, but invisible headings are ok."
  20688. (let ((level (funcall outline-level)))
  20689. (outline-next-heading)
  20690. (while (and (not (eobp)) (> (funcall outline-level) level))
  20691. (outline-next-heading))
  20692. (if (or (eobp) (< (funcall outline-level) level))
  20693. nil
  20694. (point))))
  20695. (defun org-get-last-sibling ()
  20696. "Move to previous heading of the same level, and return point.
  20697. If there is no such heading, return nil."
  20698. (let ((opoint (point))
  20699. (level (funcall outline-level)))
  20700. (outline-previous-heading)
  20701. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20702. (while (and (> (funcall outline-level) level)
  20703. (not (bobp)))
  20704. (outline-previous-heading))
  20705. (if (< (funcall outline-level) level)
  20706. nil
  20707. (point)))))
  20708. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20709. "Goto to the end of a subtree."
  20710. ;; This contains an exact copy of the original function, but it uses
  20711. ;; `org-back-to-heading', to make it work also in invisible
  20712. ;; trees. And is uses an invisible-ok argument.
  20713. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20714. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20715. ;; with many children and grandchildren etc, this can be much faster
  20716. ;; than the outline version.
  20717. (org-back-to-heading invisible-ok)
  20718. (let ((first t)
  20719. (level (funcall outline-level)))
  20720. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20721. ;; A true heading (not a plain list item), in Org-mode
  20722. ;; This means we can easily find the end by looking
  20723. ;; only for the right number of stars. Using a regexp to do
  20724. ;; this is so much faster than using a Lisp loop.
  20725. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20726. (forward-char 1)
  20727. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20728. ;; something else, do it the slow way
  20729. (while (and (not (eobp))
  20730. (or first (> (funcall outline-level) level)))
  20731. (setq first nil)
  20732. (outline-next-heading)))
  20733. (unless to-heading
  20734. (if (memq (preceding-char) '(?\n ?\^M))
  20735. (progn
  20736. ;; Go to end of line before heading
  20737. (forward-char -1)
  20738. (if (memq (preceding-char) '(?\n ?\^M))
  20739. ;; leave blank line before heading
  20740. (forward-char -1))))))
  20741. (point))
  20742. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20743. "Use Org version in org-mode, for dramatic speed-up."
  20744. (if (derived-mode-p 'org-mode)
  20745. (progn
  20746. (org-end-of-subtree nil t)
  20747. (unless (eobp) (backward-char 1)))
  20748. ad-do-it))
  20749. (defun org-end-of-meta-data-and-drawers ()
  20750. "Jump to the first text after meta data and drawers in the current entry.
  20751. This will move over empty lines, lines with planning time stamps,
  20752. clocking lines, and drawers."
  20753. (org-back-to-heading t)
  20754. (let ((end (save-excursion (outline-next-heading) (point)))
  20755. (re (concat "\\(" org-drawer-regexp "\\)"
  20756. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20757. (forward-line 1)
  20758. (while (re-search-forward re end t)
  20759. (if (not (match-end 1))
  20760. ;; empty or planning line
  20761. (forward-line 1)
  20762. ;; a drawer, find the end
  20763. (re-search-forward "^[ \t]*:END:" end 'move)
  20764. (forward-line 1)))
  20765. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20766. (point)))
  20767. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20768. "Move forward to the arg'th subheading at same level as this one.
  20769. Stop at the first and last subheadings of a superior heading.
  20770. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20771. non-nil it will also look at invisible ones."
  20772. (interactive "p")
  20773. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20774. (outline-next-heading)
  20775. (org-at-heading-p)
  20776. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20777. (re (format "^\\*\\{1,%d\\} " level))
  20778. l)
  20779. (forward-char 1)
  20780. (while (> arg 0)
  20781. (while (and (re-search-forward re nil 'move)
  20782. (setq l (- (match-end 0) (match-beginning 0) 1))
  20783. (= l level)
  20784. (not invisible-ok)
  20785. (progn (backward-char 1) (outline-invisible-p)))
  20786. (if (< l level) (setq arg 1)))
  20787. (setq arg (1- arg)))
  20788. (beginning-of-line 1))))
  20789. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20790. "Move backward to the arg'th subheading at same level as this one.
  20791. Stop at the first and last subheadings of a superior heading."
  20792. (interactive "p")
  20793. (if (not (ignore-errors (org-back-to-heading)))
  20794. (goto-char (point-min))
  20795. (org-at-heading-p)
  20796. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20797. (re (format "^\\*\\{1,%d\\} " level))
  20798. l)
  20799. (while (> arg 0)
  20800. (while (and (re-search-backward re nil 'move)
  20801. (setq l (- (match-end 0) (match-beginning 0) 1))
  20802. (= l level)
  20803. (not invisible-ok)
  20804. (outline-invisible-p))
  20805. (if (< l level) (setq arg 1)))
  20806. (setq arg (1- arg))))))
  20807. (defun org-forward-element ()
  20808. "Move forward by one element.
  20809. Move to the next element at the same level, when possible."
  20810. (interactive)
  20811. (cond ((eobp) (error "Cannot move further down"))
  20812. ((org-with-limited-levels (org-at-heading-p))
  20813. (let ((origin (point)))
  20814. (org-forward-heading-same-level 1)
  20815. (unless (org-with-limited-levels (org-at-heading-p))
  20816. (goto-char origin)
  20817. (error "Cannot move further down"))))
  20818. (t
  20819. (let* ((elem (org-element-at-point))
  20820. (end (org-element-property :end elem))
  20821. (parent (org-element-property :parent elem)))
  20822. (if (and parent (= (org-element-property :contents-end parent) end))
  20823. (goto-char (org-element-property :end parent))
  20824. (goto-char end))))))
  20825. (defun org-backward-element ()
  20826. "Move backward by one element.
  20827. Move to the previous element at the same level, when possible."
  20828. (interactive)
  20829. (cond ((bobp) (error "Cannot move further up"))
  20830. ((org-with-limited-levels (org-at-heading-p))
  20831. ;; At an headline, move to the previous one, if any, or stay
  20832. ;; here.
  20833. (let ((origin (point)))
  20834. (org-backward-heading-same-level 1)
  20835. (unless (org-with-limited-levels (org-at-heading-p))
  20836. (goto-char origin)
  20837. (error "Cannot move further up"))))
  20838. (t
  20839. (let* ((trail (org-element-at-point 'keep-trail))
  20840. (elem (car trail))
  20841. (prev-elem (nth 1 trail))
  20842. (beg (org-element-property :begin elem)))
  20843. (cond
  20844. ;; Move to beginning of current element if point isn't
  20845. ;; there already.
  20846. ((/= (point) beg) (goto-char beg))
  20847. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20848. ((org-before-first-heading-p) (goto-char (point-min)))
  20849. (t (org-back-to-heading)))))))
  20850. (defun org-up-element ()
  20851. "Move to upper element."
  20852. (interactive)
  20853. (if (org-with-limited-levels (org-at-heading-p))
  20854. (unless (org-up-heading-safe) (error "No surrounding element"))
  20855. (let* ((elem (org-element-at-point))
  20856. (parent (org-element-property :parent elem)))
  20857. (if parent (goto-char (org-element-property :begin parent))
  20858. (if (org-with-limited-levels (org-before-first-heading-p))
  20859. (error "No surrounding element")
  20860. (org-with-limited-levels (org-back-to-heading)))))))
  20861. (defvar org-element-greater-elements)
  20862. (defun org-down-element ()
  20863. "Move to inner element."
  20864. (interactive)
  20865. (let ((element (org-element-at-point)))
  20866. (cond
  20867. ((memq (org-element-type element) '(plain-list table))
  20868. (goto-char (org-element-property :contents-begin element))
  20869. (forward-char))
  20870. ((memq (org-element-type element) org-element-greater-elements)
  20871. ;; If contents are hidden, first disclose them.
  20872. (when (org-element-property :hiddenp element) (org-cycle))
  20873. (goto-char (or (org-element-property :contents-begin element)
  20874. (error "No content for this element"))))
  20875. (t (error "No inner element")))))
  20876. (defun org-drag-element-backward ()
  20877. "Move backward element at point."
  20878. (interactive)
  20879. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20880. (let* ((trail (org-element-at-point 'keep-trail))
  20881. (elem (car trail))
  20882. (prev-elem (nth 1 trail)))
  20883. ;; Error out if no previous element or previous element is
  20884. ;; a parent of the current one.
  20885. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20886. (error "Cannot drag element backward")
  20887. (let ((pos (point)))
  20888. (org-element-swap-A-B prev-elem elem)
  20889. (goto-char (+ (org-element-property :begin prev-elem)
  20890. (- pos (org-element-property :begin elem)))))))))
  20891. (defun org-drag-element-forward ()
  20892. "Move forward element at point."
  20893. (interactive)
  20894. (let* ((pos (point))
  20895. (elem (org-element-at-point)))
  20896. (when (= (point-max) (org-element-property :end elem))
  20897. (error "Cannot drag element forward"))
  20898. (goto-char (org-element-property :end elem))
  20899. (let ((next-elem (org-element-at-point)))
  20900. (when (or (org-element-nested-p elem next-elem)
  20901. (and (eq (org-element-type next-elem) 'headline)
  20902. (not (eq (org-element-type elem) 'headline))))
  20903. (goto-char pos)
  20904. (error "Cannot drag element forward"))
  20905. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20906. ;; body's length (without final blanks) and by the length of
  20907. ;; blanks between ELEM and NEXT-ELEM.
  20908. (let ((size-next (- (save-excursion
  20909. (goto-char (org-element-property :end next-elem))
  20910. (skip-chars-backward " \r\t\n")
  20911. (forward-line)
  20912. ;; Small correction if buffer doesn't end
  20913. ;; with a newline character.
  20914. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20915. (org-element-property :begin next-elem)))
  20916. (size-blank (- (org-element-property :end elem)
  20917. (save-excursion
  20918. (goto-char (org-element-property :end elem))
  20919. (skip-chars-backward " \r\t\n")
  20920. (forward-line)
  20921. (point)))))
  20922. (org-element-swap-A-B elem next-elem)
  20923. (goto-char (+ pos size-next size-blank))))))
  20924. (defun org-mark-element ()
  20925. "Put point at beginning of this element, mark at end.
  20926. Interactively, if this command is repeated or (in Transient Mark
  20927. mode) if the mark is active, it marks the next element after the
  20928. ones already marked."
  20929. (interactive)
  20930. (let (deactivate-mark)
  20931. (if (and (org-called-interactively-p 'any)
  20932. (or (and (eq last-command this-command) (mark t))
  20933. (and transient-mark-mode mark-active)))
  20934. (set-mark
  20935. (save-excursion
  20936. (goto-char (mark))
  20937. (goto-char (org-element-property :end (org-element-at-point)))))
  20938. (let ((element (org-element-at-point)))
  20939. (end-of-line)
  20940. (push-mark (org-element-property :end element) t t)
  20941. (goto-char (org-element-property :begin element))))))
  20942. (defun org-narrow-to-element ()
  20943. "Narrow buffer to current element."
  20944. (interactive)
  20945. (let ((elem (org-element-at-point)))
  20946. (cond
  20947. ((eq (car elem) 'headline)
  20948. (narrow-to-region
  20949. (org-element-property :begin elem)
  20950. (org-element-property :end elem)))
  20951. ((memq (car elem) org-element-greater-elements)
  20952. (narrow-to-region
  20953. (org-element-property :contents-begin elem)
  20954. (org-element-property :contents-end elem)))
  20955. (t
  20956. (narrow-to-region
  20957. (org-element-property :begin elem)
  20958. (org-element-property :end elem))))))
  20959. (defun org-transpose-words ()
  20960. "Transpose words, using `org-mode' syntax table."
  20961. (interactive)
  20962. (with-syntax-table org-syntax-table
  20963. (call-interactively 'transpose-words)))
  20964. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20965. (defun org-transpose-element ()
  20966. "Transpose current and previous elements, keeping blank lines between.
  20967. Point is moved after both elements."
  20968. (interactive)
  20969. (org-skip-whitespace)
  20970. (let ((end (org-element-property :end (org-element-at-point))))
  20971. (org-drag-element-backward)
  20972. (goto-char end)))
  20973. (defun org-unindent-buffer ()
  20974. "Un-indent the visible part of the buffer.
  20975. Relative indentation (between items, inside blocks, etc.) isn't
  20976. modified."
  20977. (interactive)
  20978. (unless (eq major-mode 'org-mode)
  20979. (error "Cannot un-indent a buffer not in Org mode"))
  20980. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20981. unindent-tree ; For byte-compiler.
  20982. (unindent-tree
  20983. (function
  20984. (lambda (contents)
  20985. (mapc
  20986. (lambda (element)
  20987. (if (memq (org-element-type element) '(headline section))
  20988. (funcall unindent-tree (org-element-contents element))
  20989. (save-excursion
  20990. (save-restriction
  20991. (narrow-to-region
  20992. (org-element-property :begin element)
  20993. (org-element-property :end element))
  20994. (org-do-remove-indentation)))))
  20995. (reverse contents))))))
  20996. (funcall unindent-tree (org-element-contents parse-tree))))
  20997. (defun org-show-subtree ()
  20998. "Show everything after this heading at deeper levels."
  20999. (interactive)
  21000. (outline-flag-region
  21001. (point)
  21002. (save-excursion
  21003. (org-end-of-subtree t t))
  21004. nil))
  21005. (defun org-show-entry ()
  21006. "Show the body directly following this heading.
  21007. Show the heading too, if it is currently invisible."
  21008. (interactive)
  21009. (save-excursion
  21010. (condition-case nil
  21011. (progn
  21012. (org-back-to-heading t)
  21013. (outline-flag-region
  21014. (max (point-min) (1- (point)))
  21015. (save-excursion
  21016. (if (re-search-forward
  21017. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21018. (match-beginning 1)
  21019. (point-max)))
  21020. nil)
  21021. (org-cycle-hide-drawers 'children))
  21022. (error nil))))
  21023. (defun org-make-options-regexp (kwds &optional extra)
  21024. "Make a regular expression for keyword lines."
  21025. (concat
  21026. "^#\\+\\("
  21027. (mapconcat 'regexp-quote kwds "\\|")
  21028. (if extra (concat "\\|" extra))
  21029. "\\):[ \t]*\\(.*\\)"))
  21030. ;; Make isearch reveal the necessary context
  21031. (defun org-isearch-end ()
  21032. "Reveal context after isearch exits."
  21033. (when isearch-success ; only if search was successful
  21034. (if (featurep 'xemacs)
  21035. ;; Under XEmacs, the hook is run in the correct place,
  21036. ;; we directly show the context.
  21037. (org-show-context 'isearch)
  21038. ;; In Emacs the hook runs *before* restoring the overlays.
  21039. ;; So we have to use a one-time post-command-hook to do this.
  21040. ;; (Emacs 22 has a special variable, see function `org-mode')
  21041. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21042. isearch-mode-end-hook-quit)
  21043. ;; Only when the isearch was not quitted.
  21044. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21045. 'append 'local)))))
  21046. (defun org-isearch-post-command ()
  21047. "Remove self from hook, and show context."
  21048. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21049. (org-show-context 'isearch))
  21050. ;;;; Integration with and fixes for other packages
  21051. ;;; Imenu support
  21052. (defvar org-imenu-markers nil
  21053. "All markers currently used by Imenu.")
  21054. (make-variable-buffer-local 'org-imenu-markers)
  21055. (defun org-imenu-new-marker (&optional pos)
  21056. "Return a new marker for use by Imenu, and remember the marker."
  21057. (let ((m (make-marker)))
  21058. (move-marker m (or pos (point)))
  21059. (push m org-imenu-markers)
  21060. m))
  21061. (defun org-imenu-get-tree ()
  21062. "Produce the index for Imenu."
  21063. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21064. (setq org-imenu-markers nil)
  21065. (let* ((n org-imenu-depth)
  21066. (re (concat "^" (org-get-limited-outline-regexp)))
  21067. (subs (make-vector (1+ n) nil))
  21068. (last-level 0)
  21069. m level head)
  21070. (save-excursion
  21071. (save-restriction
  21072. (widen)
  21073. (goto-char (point-max))
  21074. (while (re-search-backward re nil t)
  21075. (setq level (org-reduced-level (funcall outline-level)))
  21076. (when (and (<= level n)
  21077. (looking-at org-complex-heading-regexp))
  21078. (setq head (org-link-display-format
  21079. (org-match-string-no-properties 4))
  21080. m (org-imenu-new-marker))
  21081. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21082. (if (>= level last-level)
  21083. (push (cons head m) (aref subs level))
  21084. (push (cons head (aref subs (1+ level))) (aref subs level))
  21085. (loop for i from (1+ level) to n do (aset subs i nil)))
  21086. (setq last-level level)))))
  21087. (aref subs 1)))
  21088. (eval-after-load "imenu"
  21089. '(progn
  21090. (add-hook 'imenu-after-jump-hook
  21091. (lambda ()
  21092. (if (derived-mode-p 'org-mode)
  21093. (org-show-context 'org-goto))))))
  21094. (defun org-link-display-format (link)
  21095. "Replace a link with either the description, or the link target
  21096. if no description is present"
  21097. (save-match-data
  21098. (if (string-match org-bracket-link-analytic-regexp link)
  21099. (replace-match (if (match-end 5)
  21100. (match-string 5 link)
  21101. (concat (match-string 1 link)
  21102. (match-string 3 link)))
  21103. nil t link)
  21104. link)))
  21105. (defun org-toggle-link-display ()
  21106. "Toggle the literal or descriptive display of links."
  21107. (interactive)
  21108. (if org-descriptive-links
  21109. (progn (org-remove-from-invisibility-spec '(org-link))
  21110. (org-restart-font-lock)
  21111. (setq org-descriptive-links nil))
  21112. (progn (add-to-invisibility-spec '(org-link))
  21113. (org-restart-font-lock)
  21114. (setq org-descriptive-links t))))
  21115. ;; Speedbar support
  21116. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21117. "Overlay marking the agenda restriction line in speedbar.")
  21118. (overlay-put org-speedbar-restriction-lock-overlay
  21119. 'face 'org-agenda-restriction-lock)
  21120. (overlay-put org-speedbar-restriction-lock-overlay
  21121. 'help-echo "Agendas are currently limited to this item.")
  21122. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21123. (defun org-speedbar-set-agenda-restriction ()
  21124. "Restrict future agenda commands to the location at point in speedbar.
  21125. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21126. (interactive)
  21127. (require 'org-agenda)
  21128. (let (p m tp np dir txt)
  21129. (cond
  21130. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21131. 'org-imenu t))
  21132. (setq m (get-text-property p 'org-imenu-marker))
  21133. (with-current-buffer (marker-buffer m)
  21134. (goto-char m)
  21135. (org-agenda-set-restriction-lock 'subtree)))
  21136. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21137. 'speedbar-function 'speedbar-find-file))
  21138. (setq tp (previous-single-property-change
  21139. (1+ p) 'speedbar-function)
  21140. np (next-single-property-change
  21141. tp 'speedbar-function)
  21142. dir (speedbar-line-directory)
  21143. txt (buffer-substring-no-properties (or tp (point-min))
  21144. (or np (point-max))))
  21145. (with-current-buffer (find-file-noselect
  21146. (let ((default-directory dir))
  21147. (expand-file-name txt)))
  21148. (unless (derived-mode-p 'org-mode)
  21149. (error "Cannot restrict to non-Org-mode file"))
  21150. (org-agenda-set-restriction-lock 'file)))
  21151. (t (error "Don't know how to restrict Org-mode's agenda")))
  21152. (move-overlay org-speedbar-restriction-lock-overlay
  21153. (point-at-bol) (point-at-eol))
  21154. (setq current-prefix-arg nil)
  21155. (org-agenda-maybe-redo)))
  21156. (eval-after-load "speedbar"
  21157. '(progn
  21158. (speedbar-add-supported-extension ".org")
  21159. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21160. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21161. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21162. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21163. (add-hook 'speedbar-visiting-tag-hook
  21164. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21165. ;;; Fixes and Hacks for problems with other packages
  21166. ;; Make flyspell not check words in links, to not mess up our keymap
  21167. (defun org-mode-flyspell-verify ()
  21168. "Don't let flyspell put overlays at active buttons, or on
  21169. {todo,all-time,additional-option-like}-keywords."
  21170. (let ((pos (max (1- (point)) (point-min)))
  21171. (word (thing-at-point 'word)))
  21172. (and (not (get-text-property pos 'keymap))
  21173. (not (get-text-property pos 'org-no-flyspell))
  21174. (not (member word org-todo-keywords-1))
  21175. (not (member word org-all-time-keywords))
  21176. (not (member word org-options-keywords))
  21177. (not (member word (mapcar 'car org-startup-options)))
  21178. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  21179. (defun org-remove-flyspell-overlays-in (beg end)
  21180. "Remove flyspell overlays in region."
  21181. (and (org-bound-and-true-p flyspell-mode)
  21182. (fboundp 'flyspell-delete-region-overlays)
  21183. (flyspell-delete-region-overlays beg end))
  21184. (add-text-properties beg end '(org-no-flyspell t)))
  21185. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21186. (eval-after-load "bookmark"
  21187. '(if (boundp 'bookmark-after-jump-hook)
  21188. ;; We can use the hook
  21189. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21190. ;; Hook not available, use advice
  21191. (defadvice bookmark-jump (after org-make-visible activate)
  21192. "Make the position visible."
  21193. (org-bookmark-jump-unhide))))
  21194. ;; Make sure saveplace shows the location if it was hidden
  21195. (eval-after-load "saveplace"
  21196. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21197. "Make the position visible."
  21198. (org-bookmark-jump-unhide)))
  21199. ;; Make sure ecb shows the location if it was hidden
  21200. (eval-after-load "ecb"
  21201. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21202. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21203. (if (derived-mode-p 'org-mode)
  21204. (org-show-context))))
  21205. (defun org-bookmark-jump-unhide ()
  21206. "Unhide the current position, to show the bookmark location."
  21207. (and (derived-mode-p 'org-mode)
  21208. (or (outline-invisible-p)
  21209. (save-excursion (goto-char (max (point-min) (1- (point))))
  21210. (outline-invisible-p)))
  21211. (org-show-context 'bookmark-jump)))
  21212. ;; Make session.el ignore our circular variable
  21213. (eval-after-load "session"
  21214. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21215. ;;;; Experimental code
  21216. (defun org-closed-in-range ()
  21217. "Sparse tree of items closed in a certain time range.
  21218. Still experimental, may disappear in the future."
  21219. (interactive)
  21220. ;; Get the time interval from the user.
  21221. (let* ((time1 (org-float-time
  21222. (org-read-date nil 'to-time nil "Starting date: ")))
  21223. (time2 (org-float-time
  21224. (org-read-date nil 'to-time nil "End date:")))
  21225. ;; callback function
  21226. (callback (lambda ()
  21227. (let ((time
  21228. (org-float-time
  21229. (apply 'encode-time
  21230. (org-parse-time-string
  21231. (match-string 1))))))
  21232. ;; check if time in interval
  21233. (and (>= time time1) (<= time time2))))))
  21234. ;; make tree, check each match with the callback
  21235. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21236. ;;;; Finish up
  21237. (provide 'org)
  21238. (run-hooks 'org-load-hook)
  21239. ;;; org.el ends here