org.el 874 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115
  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. (defcustom org-read-date-minibuffer-setup-hook nil
  2580. "Hook to be used to set up keys for the date/time interface.
  2581. Add key definitions to `minibuffer-local-map', which will be a temporary
  2582. copy."
  2583. :group 'org-time
  2584. :type 'hook)
  2585. (defcustom org-extend-today-until 0
  2586. "The hour when your day really ends. Must be an integer.
  2587. This has influence for the following applications:
  2588. - When switching the agenda to \"today\". It it is still earlier than
  2589. the time given here, the day recognized as TODAY is actually yesterday.
  2590. - When a date is read from the user and it is still before the time given
  2591. here, the current date and time will be assumed to be yesterday, 23:59.
  2592. Also, timestamps inserted in capture templates follow this rule.
  2593. IMPORTANT: This is a feature whose implementation is and likely will
  2594. remain incomplete. Really, it is only here because past midnight seems to
  2595. be the favorite working time of John Wiegley :-)"
  2596. :group 'org-time
  2597. :type 'integer)
  2598. (defcustom org-use-effective-time nil
  2599. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2600. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2601. \"effective time\" of any timestamps between midnight and 8am will be
  2602. 23:59 of the previous day."
  2603. :group 'org-time
  2604. :version "24.1"
  2605. :type 'boolean)
  2606. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2607. "When non-nil, use the last clock out time for `org-todo'.
  2608. Note that this option has precedence over the combined use of
  2609. `org-use-effective-time' and `org-extend-today-until'."
  2610. :group 'org-time
  2611. ;; :version "24.3"
  2612. :type 'boolean)
  2613. (defcustom org-edit-timestamp-down-means-later nil
  2614. "Non-nil means S-down will increase the time in a time stamp.
  2615. When nil, S-up will increase."
  2616. :group 'org-time
  2617. :type 'boolean)
  2618. (defcustom org-calendar-follow-timestamp-change t
  2619. "Non-nil means make the calendar window follow timestamp changes.
  2620. When a timestamp is modified and the calendar window is visible, it will be
  2621. moved to the new date."
  2622. :group 'org-time
  2623. :type 'boolean)
  2624. (defgroup org-tags nil
  2625. "Options concerning tags in Org-mode."
  2626. :tag "Org Tags"
  2627. :group 'org)
  2628. (defcustom org-tag-alist nil
  2629. "List of tags allowed in Org-mode files.
  2630. When this list is nil, Org-mode will base TAG input on what is already in the
  2631. buffer.
  2632. The value of this variable is an alist, the car of each entry must be a
  2633. keyword as a string, the cdr may be a character that is used to select
  2634. that tag through the fast-tag-selection interface.
  2635. See the manual for details."
  2636. :group 'org-tags
  2637. :type '(repeat
  2638. (choice
  2639. (cons (string :tag "Tag name")
  2640. (character :tag "Access char"))
  2641. (list :tag "Start radio group"
  2642. (const :startgroup)
  2643. (option (string :tag "Group description")))
  2644. (list :tag "End radio group"
  2645. (const :endgroup)
  2646. (option (string :tag "Group description")))
  2647. (const :tag "New line" (:newline)))))
  2648. (defcustom org-tag-persistent-alist nil
  2649. "List of tags that will always appear in all Org-mode files.
  2650. This is in addition to any in buffer settings or customizations
  2651. of `org-tag-alist'.
  2652. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2653. The value of this variable is an alist, the car of each entry must be a
  2654. keyword as a string, the cdr may be a character that is used to select
  2655. that tag through the fast-tag-selection interface.
  2656. See the manual for details.
  2657. To disable these tags on a per-file basis, insert anywhere in the file:
  2658. #+STARTUP: noptag"
  2659. :group 'org-tags
  2660. :type '(repeat
  2661. (choice
  2662. (cons (string :tag "Tag name")
  2663. (character :tag "Access char"))
  2664. (const :tag "Start radio group" (:startgroup))
  2665. (const :tag "End radio group" (:endgroup))
  2666. (const :tag "New line" (:newline)))))
  2667. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2668. "If non-nil, always offer completion for all tags of all agenda files.
  2669. Instead of customizing this variable directly, you might want to
  2670. set it locally for capture buffers, because there no list of
  2671. tags in that file can be created dynamically (there are none).
  2672. (add-hook 'org-capture-mode-hook
  2673. (lambda ()
  2674. (set (make-local-variable
  2675. 'org-complete-tags-always-offer-all-agenda-tags)
  2676. t)))"
  2677. :group 'org-tags
  2678. :version "24.1"
  2679. :type 'boolean)
  2680. (defvar org-file-tags nil
  2681. "List of tags that can be inherited by all entries in the file.
  2682. The tags will be inherited if the variable `org-use-tag-inheritance'
  2683. says they should be.
  2684. This variable is populated from #+FILETAGS lines.")
  2685. (defcustom org-use-fast-tag-selection 'auto
  2686. "Non-nil means use fast tag selection scheme.
  2687. This is a special interface to select and deselect tags with single keys.
  2688. When nil, fast selection is never used.
  2689. When the symbol `auto', fast selection is used if and only if selection
  2690. characters for tags have been configured, either through the variable
  2691. `org-tag-alist' or through a #+TAGS line in the buffer.
  2692. When t, fast selection is always used and selection keys are assigned
  2693. automatically if necessary."
  2694. :group 'org-tags
  2695. :type '(choice
  2696. (const :tag "Always" t)
  2697. (const :tag "Never" nil)
  2698. (const :tag "When selection characters are configured" 'auto)))
  2699. (defcustom org-fast-tag-selection-single-key nil
  2700. "Non-nil means fast tag selection exits after first change.
  2701. When nil, you have to press RET to exit it.
  2702. During fast tag selection, you can toggle this flag with `C-c'.
  2703. This variable can also have the value `expert'. In this case, the window
  2704. displaying the tags menu is not even shown, until you press C-c again."
  2705. :group 'org-tags
  2706. :type '(choice
  2707. (const :tag "No" nil)
  2708. (const :tag "Yes" t)
  2709. (const :tag "Expert" expert)))
  2710. (defvar org-fast-tag-selection-include-todo nil
  2711. "Non-nil means fast tags selection interface will also offer TODO states.
  2712. This is an undocumented feature, you should not rely on it.")
  2713. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2714. "The column to which tags should be indented in a headline.
  2715. If this number is positive, it specifies the column. If it is negative,
  2716. it means that the tags should be flushright to that column. For example,
  2717. -80 works well for a normal 80 character screen.
  2718. When 0, place tags directly after headline text, with only one space in
  2719. between."
  2720. :group 'org-tags
  2721. :type 'integer)
  2722. (defcustom org-auto-align-tags t
  2723. "Non-nil keeps tags aligned when modifying headlines.
  2724. Some operations (i.e. demoting) change the length of a headline and
  2725. therefore shift the tags around. With this option turned on, after
  2726. each such operation the tags are again aligned to `org-tags-column'."
  2727. :group 'org-tags
  2728. :type 'boolean)
  2729. (defcustom org-use-tag-inheritance t
  2730. "Non-nil means tags in levels apply also for sublevels.
  2731. When nil, only the tags directly given in a specific line apply there.
  2732. This may also be a list of tags that should be inherited, or a regexp that
  2733. matches tags that should be inherited. Additional control is possible
  2734. with the variable `org-tags-exclude-from-inheritance' which gives an
  2735. explicit list of tags to be excluded from inheritance, even if the value of
  2736. `org-use-tag-inheritance' would select it for inheritance.
  2737. If this option is t, a match early-on in a tree can lead to a large
  2738. number of matches in the subtree when constructing the agenda or creating
  2739. a sparse tree. If you only want to see the first match in a tree during
  2740. a search, check out the variable `org-tags-match-list-sublevels'."
  2741. :group 'org-tags
  2742. :type '(choice
  2743. (const :tag "Not" nil)
  2744. (const :tag "Always" t)
  2745. (repeat :tag "Specific tags" (string :tag "Tag"))
  2746. (regexp :tag "Tags matched by regexp")))
  2747. (defcustom org-tags-exclude-from-inheritance nil
  2748. "List of tags that should never be inherited.
  2749. This is a way to exclude a few tags from inheritance. For way to do
  2750. the opposite, to actively allow inheritance for selected tags,
  2751. see the variable `org-use-tag-inheritance'."
  2752. :group 'org-tags
  2753. :type '(repeat (string :tag "Tag")))
  2754. (defun org-tag-inherit-p (tag)
  2755. "Check if TAG is one that should be inherited."
  2756. (cond
  2757. ((member tag org-tags-exclude-from-inheritance) nil)
  2758. ((eq org-use-tag-inheritance t) t)
  2759. ((not org-use-tag-inheritance) nil)
  2760. ((stringp org-use-tag-inheritance)
  2761. (string-match org-use-tag-inheritance tag))
  2762. ((listp org-use-tag-inheritance)
  2763. (member tag org-use-tag-inheritance))
  2764. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2765. (defcustom org-tags-match-list-sublevels t
  2766. "Non-nil means list also sublevels of headlines matching a search.
  2767. This variable applies to tags/property searches, and also to stuck
  2768. projects because this search is based on a tags match as well.
  2769. When set to the symbol `indented', sublevels are indented with
  2770. leading dots.
  2771. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2772. the sublevels of a headline matching a tag search often also match
  2773. the same search. Listing all of them can create very long lists.
  2774. Setting this variable to nil causes subtrees of a match to be skipped.
  2775. This variable is semi-obsolete and probably should always be true. It
  2776. is better to limit inheritance to certain tags using the variables
  2777. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2778. :group 'org-tags
  2779. :type '(choice
  2780. (const :tag "No, don't list them" nil)
  2781. (const :tag "Yes, do list them" t)
  2782. (const :tag "List them, indented with leading dots" indented)))
  2783. (defcustom org-tags-sort-function nil
  2784. "When set, tags are sorted using this function as a comparator."
  2785. :group 'org-tags
  2786. :type '(choice
  2787. (const :tag "No sorting" nil)
  2788. (const :tag "Alphabetical" string<)
  2789. (const :tag "Reverse alphabetical" string>)
  2790. (function :tag "Custom function" nil)))
  2791. (defvar org-tags-history nil
  2792. "History of minibuffer reads for tags.")
  2793. (defvar org-last-tags-completion-table nil
  2794. "The last used completion table for tags.")
  2795. (defvar org-after-tags-change-hook nil
  2796. "Hook that is run after the tags in a line have changed.")
  2797. (defgroup org-properties nil
  2798. "Options concerning properties in Org-mode."
  2799. :tag "Org Properties"
  2800. :group 'org)
  2801. (defcustom org-property-format "%-10s %s"
  2802. "How property key/value pairs should be formatted by `indent-line'.
  2803. When `indent-line' hits a property definition, it will format the line
  2804. according to this format, mainly to make sure that the values are
  2805. lined-up with respect to each other."
  2806. :group 'org-properties
  2807. :type 'string)
  2808. (defcustom org-properties-postprocess-alist nil
  2809. "Alist of properties and functions to adjust inserted values.
  2810. Elements of this alist must be of the form
  2811. ([string] [function])
  2812. where [string] must be a property name and [function] must be a
  2813. lambda expression: this lambda expression must take one argument,
  2814. the value to adjust, and return the new value as a string.
  2815. For example, this element will allow the property \"Remaining\"
  2816. to be updated wrt the relation between the \"Effort\" property
  2817. and the clock summary:
  2818. ((\"Remaining\" (lambda(value)
  2819. (let ((clocksum (org-clock-sum-current-item))
  2820. (effort (org-duration-string-to-minutes
  2821. (org-entry-get (point) \"Effort\"))))
  2822. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2823. :group 'org-properties
  2824. :version "24.1"
  2825. :type '(alist :key-type (string :tag "Property")
  2826. :value-type (function :tag "Function")))
  2827. (defcustom org-use-property-inheritance nil
  2828. "Non-nil means properties apply also for sublevels.
  2829. This setting is chiefly used during property searches. Turning it on can
  2830. cause significant overhead when doing a search, which is why it is not
  2831. on by default.
  2832. When nil, only the properties directly given in the current entry count.
  2833. When t, every property is inherited. The value may also be a list of
  2834. properties that should have inheritance, or a regular expression matching
  2835. properties that should be inherited.
  2836. However, note that some special properties use inheritance under special
  2837. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2838. and the properties ending in \"_ALL\" when they are used as descriptor
  2839. for valid values of a property.
  2840. Note for programmers:
  2841. When querying an entry with `org-entry-get', you can control if inheritance
  2842. should be used. By default, `org-entry-get' looks only at the local
  2843. properties. You can request inheritance by setting the inherit argument
  2844. to t (to force inheritance) or to `selective' (to respect the setting
  2845. in this variable)."
  2846. :group 'org-properties
  2847. :type '(choice
  2848. (const :tag "Not" nil)
  2849. (const :tag "Always" t)
  2850. (repeat :tag "Specific properties" (string :tag "Property"))
  2851. (regexp :tag "Properties matched by regexp")))
  2852. (defun org-property-inherit-p (property)
  2853. "Check if PROPERTY is one that should be inherited."
  2854. (cond
  2855. ((eq org-use-property-inheritance t) t)
  2856. ((not org-use-property-inheritance) nil)
  2857. ((stringp org-use-property-inheritance)
  2858. (string-match org-use-property-inheritance property))
  2859. ((listp org-use-property-inheritance)
  2860. (member property org-use-property-inheritance))
  2861. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2862. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2863. "The default column format, if no other format has been defined.
  2864. This variable can be set on the per-file basis by inserting a line
  2865. #+COLUMNS: %25ITEM ....."
  2866. :group 'org-properties
  2867. :type 'string)
  2868. (defcustom org-columns-ellipses ".."
  2869. "The ellipses to be used when a field in column view is truncated.
  2870. When this is the empty string, as many characters as possible are shown,
  2871. but then there will be no visual indication that the field has been truncated.
  2872. When this is a string of length N, the last N characters of a truncated
  2873. field are replaced by this string. If the column is narrower than the
  2874. ellipses string, only part of the ellipses string will be shown."
  2875. :group 'org-properties
  2876. :type 'string)
  2877. (defcustom org-columns-modify-value-for-display-function nil
  2878. "Function that modifies values for display in column view.
  2879. For example, it can be used to cut out a certain part from a time stamp.
  2880. The function must take 2 arguments:
  2881. column-title The title of the column (*not* the property name)
  2882. value The value that should be modified.
  2883. The function should return the value that should be displayed,
  2884. or nil if the normal value should be used."
  2885. :group 'org-properties
  2886. :type 'function)
  2887. (defcustom org-effort-property "Effort"
  2888. "The property that is being used to keep track of effort estimates.
  2889. Effort estimates given in this property need to have the format H:MM."
  2890. :group 'org-properties
  2891. :group 'org-progress
  2892. :type '(string :tag "Property"))
  2893. (defconst org-global-properties-fixed
  2894. '(("VISIBILITY_ALL" . "folded children content all")
  2895. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2896. "List of property/value pairs that can be inherited by any entry.
  2897. These are fixed values, for the preset properties. The user variable
  2898. that can be used to add to this list is `org-global-properties'.
  2899. The entries in this list are cons cells where the car is a property
  2900. name and cdr is a string with the value. If the value represents
  2901. multiple items like an \"_ALL\" property, separate the items by
  2902. spaces.")
  2903. (defcustom org-global-properties nil
  2904. "List of property/value pairs that can be inherited by any entry.
  2905. This list will be combined with the constant `org-global-properties-fixed'.
  2906. The entries in this list are cons cells where the car is a property
  2907. name and cdr is a string with the value.
  2908. You can set buffer-local values for the same purpose in the variable
  2909. `org-file-properties' this by adding lines like
  2910. #+PROPERTY: NAME VALUE"
  2911. :group 'org-properties
  2912. :type '(repeat
  2913. (cons (string :tag "Property")
  2914. (string :tag "Value"))))
  2915. (defvar org-file-properties nil
  2916. "List of property/value pairs that can be inherited by any entry.
  2917. Valid for the current buffer.
  2918. This variable is populated from #+PROPERTY lines.")
  2919. (make-variable-buffer-local 'org-file-properties)
  2920. (defgroup org-agenda nil
  2921. "Options concerning agenda views in Org-mode."
  2922. :tag "Org Agenda"
  2923. :group 'org)
  2924. (defvar org-category nil
  2925. "Variable used by org files to set a category for agenda display.
  2926. Such files should use a file variable to set it, for example
  2927. # -*- mode: org; org-category: \"ELisp\"
  2928. or contain a special line
  2929. #+CATEGORY: ELisp
  2930. If the file does not specify a category, then file's base name
  2931. is used instead.")
  2932. (make-variable-buffer-local 'org-category)
  2933. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2934. (defcustom org-agenda-files nil
  2935. "The files to be used for agenda display.
  2936. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2937. \\[org-remove-file]. You can also use customize to edit the list.
  2938. If an entry is a directory, all files in that directory that are matched by
  2939. `org-agenda-file-regexp' will be part of the file list.
  2940. If the value of the variable is not a list but a single file name, then
  2941. the list of agenda files is actually stored and maintained in that file, one
  2942. agenda file per line. In this file paths can be given relative to
  2943. `org-directory'. Tilde expansion and environment variable substitution
  2944. are also made."
  2945. :group 'org-agenda
  2946. :type '(choice
  2947. (repeat :tag "List of files and directories" file)
  2948. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2949. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2950. "Regular expression to match files for `org-agenda-files'.
  2951. If any element in the list in that variable contains a directory instead
  2952. of a normal file, all files in that directory that are matched by this
  2953. regular expression will be included."
  2954. :group 'org-agenda
  2955. :type 'regexp)
  2956. (defcustom org-agenda-text-search-extra-files nil
  2957. "List of extra files to be searched by text search commands.
  2958. These files will be search in addition to the agenda files by the
  2959. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2960. Note that these files will only be searched for text search commands,
  2961. not for the other agenda views like todo lists, tag searches or the weekly
  2962. agenda. This variable is intended to list notes and possibly archive files
  2963. that should also be searched by these two commands.
  2964. In fact, if the first element in the list is the symbol `agenda-archives',
  2965. than all archive files of all agenda files will be added to the search
  2966. scope."
  2967. :group 'org-agenda
  2968. :type '(set :greedy t
  2969. (const :tag "Agenda Archives" agenda-archives)
  2970. (repeat :inline t (file))))
  2971. (if (fboundp 'defvaralias)
  2972. (defvaralias 'org-agenda-multi-occur-extra-files
  2973. 'org-agenda-text-search-extra-files))
  2974. (defcustom org-agenda-skip-unavailable-files nil
  2975. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2976. A nil value means to remove them, after a query, from the list."
  2977. :group 'org-agenda
  2978. :type 'boolean)
  2979. (defcustom org-calendar-to-agenda-key [?c]
  2980. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2981. The command `org-calendar-goto-agenda' will be bound to this key. The
  2982. default is the character `c' because then `c' can be used to switch back and
  2983. forth between agenda and calendar."
  2984. :group 'org-agenda
  2985. :type 'sexp)
  2986. (defcustom org-calendar-insert-diary-entry-key [?i]
  2987. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2988. This option is irrelevant until `org-agenda-diary-file' has been configured
  2989. to point to an Org-mode file. When that is the case, the command
  2990. `org-agenda-diary-entry' will be bound to the key given here, by default
  2991. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2992. if you want to continue doing this, you need to change this to a different
  2993. key."
  2994. :group 'org-agenda
  2995. :type 'sexp)
  2996. (defcustom org-agenda-diary-file 'diary-file
  2997. "File to which to add new entries with the `i' key in agenda and calendar.
  2998. When this is the symbol `diary-file', the functionality in the Emacs
  2999. calendar will be used to add entries to the `diary-file'. But when this
  3000. points to a file, `org-agenda-diary-entry' will be used instead."
  3001. :group 'org-agenda
  3002. :type '(choice
  3003. (const :tag "The standard Emacs diary file" diary-file)
  3004. (file :tag "Special Org file diary entries")))
  3005. (eval-after-load "calendar"
  3006. '(progn
  3007. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3008. 'org-calendar-goto-agenda)
  3009. (add-hook 'calendar-mode-hook
  3010. (lambda ()
  3011. (unless (eq org-agenda-diary-file 'diary-file)
  3012. (define-key calendar-mode-map
  3013. org-calendar-insert-diary-entry-key
  3014. 'org-agenda-diary-entry))))))
  3015. (defgroup org-latex nil
  3016. "Options for embedding LaTeX code into Org-mode."
  3017. :tag "Org LaTeX"
  3018. :group 'org)
  3019. (defcustom org-format-latex-options
  3020. '(:foreground default :background default :scale 1.0
  3021. :html-foreground "Black" :html-background "Transparent"
  3022. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3023. "Options for creating images from LaTeX fragments.
  3024. This is a property list with the following properties:
  3025. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3026. `default' means use the foreground of the default face.
  3027. `auto' means use the foreground from the text face.
  3028. :background the background color, or \"Transparent\".
  3029. `default' means use the background of the default face.
  3030. `auto' means use the background from the text face.
  3031. :scale a scaling factor for the size of the images, to get more pixels
  3032. :html-foreground, :html-background, :html-scale
  3033. the same numbers for HTML export.
  3034. :matchers a list indicating which matchers should be used to
  3035. find LaTeX fragments. Valid members of this list are:
  3036. \"begin\" find environments
  3037. \"$1\" find single characters surrounded by $.$
  3038. \"$\" find math expressions surrounded by $...$
  3039. \"$$\" find math expressions surrounded by $$....$$
  3040. \"\\(\" find math expressions surrounded by \\(...\\)
  3041. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3042. :group 'org-latex
  3043. :type 'plist)
  3044. (defcustom org-format-latex-signal-error t
  3045. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3046. When nil, just push out a message."
  3047. :group 'org-latex
  3048. :version "24.1"
  3049. :type 'boolean)
  3050. (defcustom org-latex-to-mathml-jar-file nil
  3051. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3052. Use this to specify additional executable file say a jar file.
  3053. When using MathToWeb as the converter, specify the full-path to
  3054. your mathtoweb.jar file."
  3055. :group 'org-latex
  3056. :version "24.1"
  3057. :type '(choice
  3058. (const :tag "None" nil)
  3059. (file :tag "JAR file" :must-match t)))
  3060. (defcustom org-latex-to-mathml-convert-command nil
  3061. "Command to convert LaTeX fragments to MathML.
  3062. Replace format-specifiers in the command as noted below and use
  3063. `shell-command' to convert LaTeX to MathML.
  3064. %j: Executable file in fully expanded form as specified by
  3065. `org-latex-to-mathml-jar-file'.
  3066. %I: Input LaTeX file in fully expanded form
  3067. %o: Output MathML file
  3068. This command is used by `org-create-math-formula'.
  3069. When using MathToWeb as the converter, set this to
  3070. \"java -jar %j -unicode -force -df %o %I\"."
  3071. :group 'org-latex
  3072. :version "24.1"
  3073. :type '(choice
  3074. (const :tag "None" nil)
  3075. (string :tag "\nShell command")))
  3076. (defcustom org-latex-create-formula-image-program 'dvipng
  3077. "Program to convert LaTeX fragments with.
  3078. dvipng Process the LaTeX fragments to dvi file, then convert
  3079. dvi files to png files using dvipng.
  3080. This will also include processing of non-math environments.
  3081. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3082. to convert pdf files to png files"
  3083. :group 'org-latex
  3084. :version "24.1"
  3085. :type '(choice
  3086. (const :tag "dvipng" dvipng)
  3087. (const :tag "imagemagick" imagemagick)))
  3088. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3089. "Path to store latex preview images.
  3090. A relative path here creates many directories relative to the
  3091. processed org files paths. An absolute path puts all preview
  3092. images at the same place."
  3093. :group 'org-latex
  3094. :version "24.3"
  3095. :type 'string)
  3096. (defun org-format-latex-mathml-available-p ()
  3097. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3098. (save-match-data
  3099. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3100. org-latex-to-mathml-convert-command)
  3101. (let ((executable (car (split-string
  3102. org-latex-to-mathml-convert-command))))
  3103. (when (executable-find executable)
  3104. (if (string-match
  3105. "%j" org-latex-to-mathml-convert-command)
  3106. (file-readable-p org-latex-to-mathml-jar-file)
  3107. t))))))
  3108. (defcustom org-format-latex-header "\\documentclass{article}
  3109. \\usepackage[usenames]{color}
  3110. \\usepackage{amsmath}
  3111. \\usepackage[mathscr]{eucal}
  3112. \\pagestyle{empty} % do not remove
  3113. \[PACKAGES]
  3114. \[DEFAULT-PACKAGES]
  3115. % The settings below are copied from fullpage.sty
  3116. \\setlength{\\textwidth}{\\paperwidth}
  3117. \\addtolength{\\textwidth}{-3cm}
  3118. \\setlength{\\oddsidemargin}{1.5cm}
  3119. \\addtolength{\\oddsidemargin}{-2.54cm}
  3120. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3121. \\setlength{\\textheight}{\\paperheight}
  3122. \\addtolength{\\textheight}{-\\headheight}
  3123. \\addtolength{\\textheight}{-\\headsep}
  3124. \\addtolength{\\textheight}{-\\footskip}
  3125. \\addtolength{\\textheight}{-3cm}
  3126. \\setlength{\\topmargin}{1.5cm}
  3127. \\addtolength{\\topmargin}{-2.54cm}"
  3128. "The document header used for processing LaTeX fragments.
  3129. It is imperative that this header make sure that no page number
  3130. appears on the page. The package defined in the variables
  3131. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3132. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3133. will be appended."
  3134. :group 'org-latex
  3135. :type 'string)
  3136. (defvar org-format-latex-header-extra nil)
  3137. (defun org-set-packages-alist (var val)
  3138. "Set the packages alist and make sure it has 3 elements per entry."
  3139. (set var (mapcar (lambda (x)
  3140. (if (and (consp x) (= (length x) 2))
  3141. (list (car x) (nth 1 x) t)
  3142. x))
  3143. val)))
  3144. (defun org-get-packages-alist (var)
  3145. "Get the packages alist and make sure it has 3 elements per entry."
  3146. (mapcar (lambda (x)
  3147. (if (and (consp x) (= (length x) 2))
  3148. (list (car x) (nth 1 x) t)
  3149. x))
  3150. (default-value var)))
  3151. ;; The following variables are defined here because is it also used
  3152. ;; when formatting latex fragments. Originally it was part of the
  3153. ;; LaTeX exporter, which is why the name includes "export".
  3154. (defcustom org-export-latex-default-packages-alist
  3155. '(("AUTO" "inputenc" t)
  3156. ("T1" "fontenc" t)
  3157. ("" "fixltx2e" nil)
  3158. ("" "graphicx" t)
  3159. ("" "longtable" nil)
  3160. ("" "float" nil)
  3161. ("" "wrapfig" nil)
  3162. ("" "soul" t)
  3163. ("" "textcomp" t)
  3164. ("" "marvosym" t)
  3165. ("" "wasysym" t)
  3166. ("" "latexsym" t)
  3167. ("" "amssymb" t)
  3168. ("" "hyperref" nil)
  3169. "\\tolerance=1000"
  3170. )
  3171. "Alist of default packages to be inserted in the header.
  3172. Change this only if one of the packages here causes an incompatibility
  3173. with another package you are using.
  3174. The packages in this list are needed by one part or another of Org-mode
  3175. to function properly.
  3176. - inputenc, fontenc: for basic font and character selection
  3177. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3178. for interpreting the entities in `org-entities'. You can skip some of these
  3179. packages if you don't use any of the symbols in it.
  3180. - graphicx: for including images
  3181. - float, wrapfig: for figure placement
  3182. - longtable: for long tables
  3183. - hyperref: for cross references
  3184. Therefore you should not modify this variable unless you know what you
  3185. are doing. The one reason to change it anyway is that you might be loading
  3186. some other package that conflicts with one of the default packages.
  3187. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3188. If SNIPPET-FLAG is t, the package also needs to be included when
  3189. compiling LaTeX snippets into images for inclusion into HTML."
  3190. :group 'org-export-latex
  3191. :set 'org-set-packages-alist
  3192. :get 'org-get-packages-alist
  3193. :version "24.1"
  3194. :type '(repeat
  3195. (choice
  3196. (list :tag "options/package pair"
  3197. (string :tag "options")
  3198. (string :tag "package")
  3199. (boolean :tag "Snippet"))
  3200. (string :tag "A line of LaTeX"))))
  3201. (defcustom org-export-latex-packages-alist nil
  3202. "Alist of packages to be inserted in every LaTeX header.
  3203. These will be inserted after `org-export-latex-default-packages-alist'.
  3204. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3205. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3206. turning LaTeX snippets into images for inclusion into HTML.
  3207. Make sure that you only list packages here which:
  3208. - you want in every file
  3209. - do not conflict with the default packages in
  3210. `org-export-latex-default-packages-alist'
  3211. - do not conflict with the setup in `org-format-latex-header'."
  3212. :group 'org-export-latex
  3213. :set 'org-set-packages-alist
  3214. :get 'org-get-packages-alist
  3215. :type '(repeat
  3216. (choice
  3217. (list :tag "options/package pair"
  3218. (string :tag "options")
  3219. (string :tag "package")
  3220. (boolean :tag "Snippet"))
  3221. (string :tag "A line of LaTeX"))))
  3222. (defgroup org-appearance nil
  3223. "Settings for Org-mode appearance."
  3224. :tag "Org Appearance"
  3225. :group 'org)
  3226. (defcustom org-level-color-stars-only nil
  3227. "Non-nil means fontify only the stars in each headline.
  3228. When nil, the entire headline is fontified.
  3229. Changing it requires restart of `font-lock-mode' to become effective
  3230. also in regions already fontified."
  3231. :group 'org-appearance
  3232. :type 'boolean)
  3233. (defcustom org-hide-leading-stars nil
  3234. "Non-nil means hide the first N-1 stars in a headline.
  3235. This works by using the face `org-hide' for these stars. This
  3236. face is white for a light background, and black for a dark
  3237. background. You may have to customize the face `org-hide' to
  3238. make this work.
  3239. Changing it requires restart of `font-lock-mode' to become effective
  3240. also in regions already fontified.
  3241. You may also set this on a per-file basis by adding one of the following
  3242. lines to the buffer:
  3243. #+STARTUP: hidestars
  3244. #+STARTUP: showstars"
  3245. :group 'org-appearance
  3246. :type 'boolean)
  3247. (defcustom org-hidden-keywords nil
  3248. "List of symbols corresponding to keywords to be hidden the org buffer.
  3249. For example, a value '(title) for this list will make the document's title
  3250. appear in the buffer without the initial #+TITLE: keyword."
  3251. :group 'org-appearance
  3252. :version "24.1"
  3253. :type '(set (const :tag "#+AUTHOR" author)
  3254. (const :tag "#+DATE" date)
  3255. (const :tag "#+EMAIL" email)
  3256. (const :tag "#+TITLE" title)))
  3257. (defcustom org-custom-properties nil
  3258. "List of properties (as strings) with a special meaning.
  3259. The default use of these custom properties is to let the user
  3260. hide them with `org-toggle-custom-properties-visibility'."
  3261. :group 'org-properties
  3262. :group 'org-appearance
  3263. :version "24.3"
  3264. :type '(repeat (string :tag "Property Name")))
  3265. (defcustom org-fontify-done-headline nil
  3266. "Non-nil means change the face of a headline if it is marked DONE.
  3267. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3268. When this is non-nil, the headline after the keyword is set to the
  3269. `org-headline-done' as an additional indication."
  3270. :group 'org-appearance
  3271. :type 'boolean)
  3272. (defcustom org-fontify-emphasized-text t
  3273. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3274. Changing this variable requires a restart of Emacs to take effect."
  3275. :group 'org-appearance
  3276. :type 'boolean)
  3277. (defcustom org-fontify-whole-heading-line nil
  3278. "Non-nil means fontify the whole line for headings.
  3279. This is useful when setting a background color for the
  3280. org-level-* faces."
  3281. :group 'org-appearance
  3282. :type 'boolean)
  3283. (defcustom org-highlight-latex-fragments-and-specials nil
  3284. "Non-nil means fontify what is treated specially by the exporters."
  3285. :group 'org-appearance
  3286. :type 'boolean)
  3287. (defcustom org-hide-emphasis-markers nil
  3288. "Non-nil mean font-lock should hide the emphasis marker characters."
  3289. :group 'org-appearance
  3290. :type 'boolean)
  3291. (defcustom org-pretty-entities nil
  3292. "Non-nil means show entities as UTF8 characters.
  3293. When nil, the \\name form remains in the buffer."
  3294. :group 'org-appearance
  3295. :version "24.1"
  3296. :type 'boolean)
  3297. (defcustom org-pretty-entities-include-sub-superscripts t
  3298. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3299. :group 'org-appearance
  3300. :version "24.1"
  3301. :type 'boolean)
  3302. (defvar org-emph-re nil
  3303. "Regular expression for matching emphasis.
  3304. After a match, the match groups contain these elements:
  3305. 0 The match of the full regular expression, including the characters
  3306. before and after the proper match
  3307. 1 The character before the proper match, or empty at beginning of line
  3308. 2 The proper match, including the leading and trailing markers
  3309. 3 The leading marker like * or /, indicating the type of highlighting
  3310. 4 The text between the emphasis markers, not including the markers
  3311. 5 The character after the match, empty at the end of a line")
  3312. (defvar org-verbatim-re nil
  3313. "Regular expression for matching verbatim text.")
  3314. (defvar org-emphasis-regexp-components) ; defined just below
  3315. (defvar org-emphasis-alist) ; defined just below
  3316. (defun org-set-emph-re (var val)
  3317. "Set variable and compute the emphasis regular expression."
  3318. (set var val)
  3319. (when (and (boundp 'org-emphasis-alist)
  3320. (boundp 'org-emphasis-regexp-components)
  3321. org-emphasis-alist org-emphasis-regexp-components)
  3322. (let* ((e org-emphasis-regexp-components)
  3323. (pre (car e))
  3324. (post (nth 1 e))
  3325. (border (nth 2 e))
  3326. (body (nth 3 e))
  3327. (nl (nth 4 e))
  3328. (body1 (concat body "*?"))
  3329. (markers (mapconcat 'car org-emphasis-alist ""))
  3330. (vmarkers (mapconcat
  3331. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3332. org-emphasis-alist "")))
  3333. ;; make sure special characters appear at the right position in the class
  3334. (if (string-match "\\^" markers)
  3335. (setq markers (concat (replace-match "" t t markers) "^")))
  3336. (if (string-match "-" markers)
  3337. (setq markers (concat (replace-match "" t t markers) "-")))
  3338. (if (string-match "\\^" vmarkers)
  3339. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3340. (if (string-match "-" vmarkers)
  3341. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3342. (if (> nl 0)
  3343. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3344. (int-to-string nl) "\\}")))
  3345. ;; Make the regexp
  3346. (setq org-emph-re
  3347. (concat "\\([" pre "]\\|^\\)"
  3348. "\\("
  3349. "\\([" markers "]\\)"
  3350. "\\("
  3351. "[^" border "]\\|"
  3352. "[^" border "]"
  3353. body1
  3354. "[^" border "]"
  3355. "\\)"
  3356. "\\3\\)"
  3357. "\\([" post "]\\|$\\)"))
  3358. (setq org-verbatim-re
  3359. (concat "\\([" pre "]\\|^\\)"
  3360. "\\("
  3361. "\\([" vmarkers "]\\)"
  3362. "\\("
  3363. "[^" border "]\\|"
  3364. "[^" border "]"
  3365. body1
  3366. "[^" border "]"
  3367. "\\)"
  3368. "\\3\\)"
  3369. "\\([" post "]\\|$\\)")))))
  3370. (defcustom org-emphasis-regexp-components
  3371. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3372. "Components used to build the regular expression for emphasis.
  3373. This is a list with five entries. Terminology: In an emphasis string
  3374. like \" *strong word* \", we call the initial space PREMATCH, the final
  3375. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3376. and \"trong wor\" is the body. The different components in this variable
  3377. specify what is allowed/forbidden in each part:
  3378. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3379. post Chars allowed as postmatch. End of line will be allowed too.
  3380. border The chars *forbidden* as border characters.
  3381. body-regexp A regexp like \".\" to match a body character. Don't use
  3382. non-shy groups here, and don't allow newline here.
  3383. newline The maximum number of newlines allowed in an emphasis exp.
  3384. Use customize to modify this, or restart Emacs after changing it."
  3385. :group 'org-appearance
  3386. :set 'org-set-emph-re
  3387. :type '(list
  3388. (sexp :tag "Allowed chars in pre ")
  3389. (sexp :tag "Allowed chars in post ")
  3390. (sexp :tag "Forbidden chars in border ")
  3391. (sexp :tag "Regexp for body ")
  3392. (integer :tag "number of newlines allowed")
  3393. (option (boolean :tag "Please ignore this button"))))
  3394. (defcustom org-emphasis-alist
  3395. `(("*" bold "<b>" "</b>")
  3396. ("/" italic "<i>" "</i>")
  3397. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3398. ("=" org-code "<code>" "</code>" verbatim)
  3399. ("~" org-verbatim "<code>" "</code>" verbatim)
  3400. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3401. "<del>" "</del>")
  3402. )
  3403. "Special syntax for emphasized text.
  3404. Text starting and ending with a special character will be emphasized, for
  3405. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3406. characters, the face to be used by font-lock for highlighting in Org-mode
  3407. Emacs buffers, and the HTML tags to be used for this.
  3408. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3409. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3410. Use customize to modify this, or restart Emacs after changing it."
  3411. :group 'org-appearance
  3412. :set 'org-set-emph-re
  3413. :type '(repeat
  3414. (list
  3415. (string :tag "Marker character")
  3416. (choice
  3417. (face :tag "Font-lock-face")
  3418. (plist :tag "Face property list"))
  3419. (string :tag "HTML start tag")
  3420. (string :tag "HTML end tag")
  3421. (option (const verbatim)))))
  3422. (defvar org-syntax-table
  3423. (let ((st (make-syntax-table)))
  3424. (mapc (lambda(c) (modify-syntax-entry
  3425. (string-to-char (car c)) "w p" st))
  3426. org-emphasis-alist)
  3427. st))
  3428. (defvar org-protecting-blocks
  3429. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3430. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3431. This is needed for font-lock setup.")
  3432. ;;; Miscellaneous options
  3433. (defgroup org-completion nil
  3434. "Completion in Org-mode."
  3435. :tag "Org Completion"
  3436. :group 'org)
  3437. (defcustom org-completion-use-ido nil
  3438. "Non-nil means use ido completion wherever possible.
  3439. Note that `ido-mode' must be active for this variable to be relevant.
  3440. If you decide to turn this variable on, you might well want to turn off
  3441. `org-outline-path-complete-in-steps'.
  3442. See also `org-completion-use-iswitchb'."
  3443. :group 'org-completion
  3444. :type 'boolean)
  3445. (defcustom org-completion-use-iswitchb nil
  3446. "Non-nil means use iswitchb completion wherever possible.
  3447. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3448. If you decide to turn this variable on, you might well want to turn off
  3449. `org-outline-path-complete-in-steps'.
  3450. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3451. :group 'org-completion
  3452. :type 'boolean)
  3453. (defcustom org-completion-fallback-command 'hippie-expand
  3454. "The expansion command called by \\[pcomplete] in normal context.
  3455. Normal means, no org-mode-specific context."
  3456. :group 'org-completion
  3457. :type 'function)
  3458. ;;; Functions and variables from their packages
  3459. ;; Declared here to avoid compiler warnings
  3460. ;; XEmacs only
  3461. (defvar outline-mode-menu-heading)
  3462. (defvar outline-mode-menu-show)
  3463. (defvar outline-mode-menu-hide)
  3464. (defvar zmacs-regions) ; XEmacs regions
  3465. ;; Emacs only
  3466. (defvar mark-active)
  3467. ;; Various packages
  3468. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3469. (declare-function calendar-forward-day "cal-move" (arg))
  3470. (declare-function calendar-goto-date "cal-move" (date))
  3471. (declare-function calendar-goto-today "cal-move" ())
  3472. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3473. (defvar calc-embedded-close-formula)
  3474. (defvar calc-embedded-open-formula)
  3475. (declare-function cdlatex-tab "ext:cdlatex" ())
  3476. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3477. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3478. (defvar font-lock-unfontify-region-function)
  3479. (declare-function iswitchb-read-buffer "iswitchb"
  3480. (prompt &optional default require-match start matches-set))
  3481. (defvar iswitchb-temp-buflist)
  3482. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3483. (defvar org-agenda-tags-todo-honor-ignore-options)
  3484. (declare-function org-agenda-skip "org-agenda" ())
  3485. (declare-function
  3486. org-agenda-format-item "org-agenda"
  3487. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3488. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3489. (declare-function org-agenda-change-all-lines "org-agenda"
  3490. (newhead hdmarker &optional fixface just-this))
  3491. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3492. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3493. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3494. (beg end))
  3495. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3496. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3497. "org-agenda" (&optional end))
  3498. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3499. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3500. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3501. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3502. (declare-function org-indent-mode "org-indent" (&optional arg))
  3503. (declare-function parse-time-string "parse-time" (string))
  3504. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3505. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3506. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3507. (defvar remember-data-file)
  3508. (defvar texmathp-why)
  3509. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3510. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3511. (defvar org-latex-regexps)
  3512. ;;; Autoload and prepare some org modules
  3513. ;; Some table stuff that needs to be defined here, because it is used
  3514. ;; by the functions setting up org-mode or checking for table context.
  3515. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3516. "Detect an org-type or table-type table.")
  3517. (defconst org-table-line-regexp "^[ \t]*|"
  3518. "Detect an org-type table line.")
  3519. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3520. "Detect an org-type table line.")
  3521. (defconst org-table-hline-regexp "^[ \t]*|-"
  3522. "Detect an org-type table hline.")
  3523. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3524. "Detect a table-type table hline.")
  3525. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3526. "Detect the first line outside a table when searching from within it.
  3527. This works for both table types.")
  3528. ;; Autoload the functions in org-table.el that are needed by functions here.
  3529. (eval-and-compile
  3530. (org-autoload "org-table"
  3531. '(org-table-begin org-table-blank-field org-table-end)))
  3532. ;;;###autoload
  3533. (defun turn-on-orgtbl ()
  3534. "Unconditionally turn on `orgtbl-mode'."
  3535. (require 'org-table)
  3536. (orgtbl-mode 1))
  3537. (defun org-at-table-p (&optional table-type)
  3538. "Return t if the cursor is inside an org-type table.
  3539. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3540. (if org-enable-table-editor
  3541. (save-excursion
  3542. (beginning-of-line 1)
  3543. (looking-at (if table-type org-table-any-line-regexp
  3544. org-table-line-regexp)))
  3545. nil))
  3546. (defsubst org-table-p () (org-at-table-p))
  3547. (defun org-at-table.el-p ()
  3548. "Return t if and only if we are at a table.el table."
  3549. (and (org-at-table-p 'any)
  3550. (save-excursion
  3551. (goto-char (org-table-begin 'any))
  3552. (looking-at org-table1-hline-regexp))))
  3553. (defun org-table-recognize-table.el ()
  3554. "If there is a table.el table nearby, recognize it and move into it."
  3555. (if org-table-tab-recognizes-table.el
  3556. (if (org-at-table.el-p)
  3557. (progn
  3558. (beginning-of-line 1)
  3559. (if (looking-at org-table-dataline-regexp)
  3560. nil
  3561. (if (looking-at org-table1-hline-regexp)
  3562. (progn
  3563. (beginning-of-line 2)
  3564. (if (looking-at org-table-any-border-regexp)
  3565. (beginning-of-line -1)))))
  3566. (if (re-search-forward "|" (org-table-end t) t)
  3567. (progn
  3568. (require 'table)
  3569. (if (table--at-cell-p (point))
  3570. t
  3571. (message "recognizing table.el table...")
  3572. (table-recognize-table)
  3573. (message "recognizing table.el table...done")))
  3574. (error "This should not happen"))
  3575. t)
  3576. nil)
  3577. nil))
  3578. (defun org-at-table-hline-p ()
  3579. "Return t if the cursor is inside a hline in a table."
  3580. (if org-enable-table-editor
  3581. (save-excursion
  3582. (beginning-of-line 1)
  3583. (looking-at org-table-hline-regexp))
  3584. nil))
  3585. (defvar org-table-clean-did-remove-column nil)
  3586. (defun org-table-map-tables (function &optional quietly)
  3587. "Apply FUNCTION to the start of all tables in the buffer."
  3588. (save-excursion
  3589. (save-restriction
  3590. (widen)
  3591. (goto-char (point-min))
  3592. (while (re-search-forward org-table-any-line-regexp nil t)
  3593. (unless quietly
  3594. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3595. (beginning-of-line 1)
  3596. (when (and (looking-at org-table-line-regexp)
  3597. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3598. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3599. (save-excursion (funcall function))
  3600. (or (looking-at org-table-line-regexp)
  3601. (forward-char 1)))
  3602. (re-search-forward org-table-any-border-regexp nil 1))))
  3603. (unless quietly (message "Mapping tables: done")))
  3604. ;; Declare and autoload functions from org-exp.el & Co
  3605. (declare-function org-default-export-plist "org-exp")
  3606. (declare-function org-infile-export-plist "org-exp")
  3607. (declare-function org-get-current-options "org-exp")
  3608. ;; Declare and autoload functions from org-agenda.el
  3609. (eval-and-compile
  3610. (org-autoload "org-agenda"
  3611. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3612. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3613. (declare-function org-clock-update-mode-line "org-clock" ())
  3614. (declare-function org-resolve-clocks "org-clock"
  3615. (&optional also-non-dangling-p prompt last-valid))
  3616. (defvar org-clock-start-time)
  3617. (defvar org-clock-marker (make-marker)
  3618. "Marker recording the last clock-in.")
  3619. (defvar org-clock-hd-marker (make-marker)
  3620. "Marker recording the last clock-in, but the headline position.")
  3621. (defvar org-clock-heading ""
  3622. "The heading of the current clock entry.")
  3623. (defun org-clock-is-active ()
  3624. "Return non-nil if clock is currently running.
  3625. The return value is actually the clock marker."
  3626. (marker-buffer org-clock-marker))
  3627. (eval-and-compile
  3628. (org-autoload "org-clock" '(org-clock-remove-overlays
  3629. org-clock-update-time-maybe
  3630. org-clocktable-shift)))
  3631. (defun org-check-running-clock ()
  3632. "Check if the current buffer contains the running clock.
  3633. If yes, offer to stop it and to save the buffer with the changes."
  3634. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3635. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3636. (buffer-name))))
  3637. (org-clock-out)
  3638. (when (y-or-n-p "Save changed buffer?")
  3639. (save-buffer))))
  3640. (defun org-clocktable-try-shift (dir n)
  3641. "Check if this line starts a clock table, if yes, shift the time block."
  3642. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3643. (org-clocktable-shift dir n)))
  3644. ;;;###autoload
  3645. (defun org-clock-persistence-insinuate ()
  3646. "Set up hooks for clock persistence."
  3647. (require 'org-clock)
  3648. (add-hook 'org-mode-hook 'org-clock-load)
  3649. (add-hook 'kill-emacs-hook 'org-clock-save))
  3650. ;; Define the variable already here, to make sure we have it.
  3651. (defvar org-indent-mode nil
  3652. "Non-nil if Org-Indent mode is enabled.
  3653. Use the command `org-indent-mode' to change this variable.")
  3654. ;; Autoload archiving code
  3655. ;; The stuff that is needed for cycling and tags has to be defined here.
  3656. (defgroup org-archive nil
  3657. "Options concerning archiving in Org-mode."
  3658. :tag "Org Archive"
  3659. :group 'org-structure)
  3660. (defcustom org-archive-location "%s_archive::"
  3661. "The location where subtrees should be archived.
  3662. The value of this variable is a string, consisting of two parts,
  3663. separated by a double-colon. The first part is a filename and
  3664. the second part is a headline.
  3665. When the filename is omitted, archiving happens in the same file.
  3666. %s in the filename will be replaced by the current file
  3667. name (without the directory part). Archiving to a different file
  3668. is useful to keep archived entries from contributing to the
  3669. Org-mode Agenda.
  3670. The archived entries will be filed as subtrees of the specified
  3671. headline. When the headline is omitted, the subtrees are simply
  3672. filed away at the end of the file, as top-level entries. Also in
  3673. the heading you can use %s to represent the file name, this can be
  3674. useful when using the same archive for a number of different files.
  3675. Here are a few examples:
  3676. \"%s_archive::\"
  3677. If the current file is Projects.org, archive in file
  3678. Projects.org_archive, as top-level trees. This is the default.
  3679. \"::* Archived Tasks\"
  3680. Archive in the current file, under the top-level headline
  3681. \"* Archived Tasks\".
  3682. \"~/org/archive.org::\"
  3683. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3684. \"~/org/archive.org::* From %s\"
  3685. Archive in file ~/org/archive.org (absolute path), under headlines
  3686. \"From FILENAME\" where file name is the current file name.
  3687. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3688. The \"datetree/\" string is special, signifying to archive
  3689. items to the datetree. Items are placed in either the CLOSED
  3690. date of the item, or the current date if there is no CLOSED date.
  3691. The heading will be a subentry to the current date. There doesn't
  3692. need to be a heading, but there always needs to be a slash after
  3693. datetree. For example, to store archived items directly in the
  3694. datetree, use \"~/org/datetree.org::datetree/\".
  3695. \"basement::** Finished Tasks\"
  3696. Archive in file ./basement (relative path), as level 3 trees
  3697. below the level 2 heading \"** Finished Tasks\".
  3698. You may set this option on a per-file basis by adding to the buffer a
  3699. line like
  3700. #+ARCHIVE: basement::** Finished Tasks
  3701. You may also define it locally for a subtree by setting an ARCHIVE property
  3702. in the entry. If such a property is found in an entry, or anywhere up
  3703. the hierarchy, it will be used."
  3704. :group 'org-archive
  3705. :type 'string)
  3706. (defcustom org-archive-tag "ARCHIVE"
  3707. "The tag that marks a subtree as archived.
  3708. An archived subtree does not open during visibility cycling, and does
  3709. not contribute to the agenda listings.
  3710. After changing this, font-lock must be restarted in the relevant buffers to
  3711. get the proper fontification."
  3712. :group 'org-archive
  3713. :group 'org-keywords
  3714. :type 'string)
  3715. (defcustom org-agenda-skip-archived-trees t
  3716. "Non-nil means the agenda will skip any items located in archived trees.
  3717. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3718. variable is no longer recommended, you should leave it at the value t.
  3719. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3720. :group 'org-archive
  3721. :group 'org-agenda-skip
  3722. :type 'boolean)
  3723. (defcustom org-columns-skip-archived-trees t
  3724. "Non-nil means ignore archived trees when creating column view."
  3725. :group 'org-archive
  3726. :group 'org-properties
  3727. :type 'boolean)
  3728. (defcustom org-cycle-open-archived-trees nil
  3729. "Non-nil means `org-cycle' will open archived trees.
  3730. An archived tree is a tree marked with the tag ARCHIVE.
  3731. When nil, archived trees will stay folded. You can still open them with
  3732. normal outline commands like `show-all', but not with the cycling commands."
  3733. :group 'org-archive
  3734. :group 'org-cycle
  3735. :type 'boolean)
  3736. (defcustom org-sparse-tree-open-archived-trees nil
  3737. "Non-nil means sparse tree construction shows matches in archived trees.
  3738. When nil, matches in these trees are highlighted, but the trees are kept in
  3739. collapsed state."
  3740. :group 'org-archive
  3741. :group 'org-sparse-trees
  3742. :type 'boolean)
  3743. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3744. "The default date type when building a sparse tree.
  3745. When this is nil, a date is a scheduled or a deadline timestamp.
  3746. Otherwise, these types are allowed:
  3747. all: all timestamps
  3748. active: only active timestamps (<...>)
  3749. inactive: only inactive timestamps (<...)
  3750. scheduled: only scheduled timestamps
  3751. deadline: only deadline timestamps"
  3752. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3753. (const :tag "All timestamps" all)
  3754. (const :tag "Only active timestamps" active)
  3755. (const :tag "Only inactive timestamps" inactive)
  3756. (const :tag "Only scheduled timestamps" scheduled)
  3757. (const :tag "Only deadline timestamps" deadline))
  3758. :version "24.3"
  3759. :group 'org-sparse-trees)
  3760. (defun org-cycle-hide-archived-subtrees (state)
  3761. "Re-hide all archived subtrees after a visibility state change."
  3762. (when (and (not org-cycle-open-archived-trees)
  3763. (not (memq state '(overview folded))))
  3764. (save-excursion
  3765. (let* ((globalp (memq state '(contents all)))
  3766. (beg (if globalp (point-min) (point)))
  3767. (end (if globalp (point-max) (org-end-of-subtree t))))
  3768. (org-hide-archived-subtrees beg end)
  3769. (goto-char beg)
  3770. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3771. (message "%s" (substitute-command-keys
  3772. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3773. (defun org-force-cycle-archived ()
  3774. "Cycle subtree even if it is archived."
  3775. (interactive)
  3776. (setq this-command 'org-cycle)
  3777. (let ((org-cycle-open-archived-trees t))
  3778. (call-interactively 'org-cycle)))
  3779. (defun org-hide-archived-subtrees (beg end)
  3780. "Re-hide all archived subtrees after a visibility state change."
  3781. (save-excursion
  3782. (let* ((re (concat ":" org-archive-tag ":")))
  3783. (goto-char beg)
  3784. (while (re-search-forward re end t)
  3785. (when (org-at-heading-p)
  3786. (org-flag-subtree t)
  3787. (org-end-of-subtree t))))))
  3788. (declare-function outline-end-of-heading "outline" ())
  3789. (declare-function outline-flag-region "outline" (from to flag))
  3790. (defun org-flag-subtree (flag)
  3791. (save-excursion
  3792. (org-back-to-heading t)
  3793. (outline-end-of-heading)
  3794. (outline-flag-region (point)
  3795. (progn (org-end-of-subtree t) (point))
  3796. flag)))
  3797. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3798. (eval-and-compile
  3799. (org-autoload "org-archive"
  3800. '(org-add-archive-files)))
  3801. ;; Autoload Column View Code
  3802. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3803. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3804. (declare-function org-columns-compute "org-colview" (property))
  3805. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3806. '(org-columns-number-to-string
  3807. org-columns-get-format-and-top-level
  3808. org-columns-compute
  3809. org-columns-remove-overlays))
  3810. ;; Autoload ID code
  3811. (declare-function org-id-store-link "org-id")
  3812. (declare-function org-id-locations-load "org-id")
  3813. (declare-function org-id-locations-save "org-id")
  3814. (defvar org-id-track-globally)
  3815. (org-autoload "org-id"
  3816. '(org-id-new
  3817. org-id-copy
  3818. org-id-get-with-outline-path-completion
  3819. org-id-get-with-outline-drilling))
  3820. ;;; Variables for pre-computed regular expressions, all buffer local
  3821. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3822. "Matches first line of a hidden block.")
  3823. (make-variable-buffer-local 'org-drawer-regexp)
  3824. (defvar org-todo-regexp nil
  3825. "Matches any of the TODO state keywords.")
  3826. (make-variable-buffer-local 'org-todo-regexp)
  3827. (defvar org-not-done-regexp nil
  3828. "Matches any of the TODO state keywords except the last one.")
  3829. (make-variable-buffer-local 'org-not-done-regexp)
  3830. (defvar org-not-done-heading-regexp nil
  3831. "Matches a TODO headline that is not done.")
  3832. (make-variable-buffer-local 'org-not-done-regexp)
  3833. (defvar org-todo-line-regexp nil
  3834. "Matches a headline and puts TODO state into group 2 if present.")
  3835. (make-variable-buffer-local 'org-todo-line-regexp)
  3836. (defvar org-complex-heading-regexp nil
  3837. "Matches a headline and puts everything into groups:
  3838. group 1: the stars
  3839. group 2: The todo keyword, maybe
  3840. group 3: Priority cookie
  3841. group 4: True headline
  3842. group 5: Tags")
  3843. (make-variable-buffer-local 'org-complex-heading-regexp)
  3844. (defvar org-complex-heading-regexp-format nil
  3845. "Printf format to make regexp to match an exact headline.
  3846. This regexp will match the headline of any node which has the
  3847. exact headline text that is put into the format, but may have any
  3848. TODO state, priority and tags.")
  3849. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3850. (defvar org-todo-line-tags-regexp nil
  3851. "Matches a headline and puts TODO state into group 2 if present.
  3852. Also put tags into group 4 if tags are present.")
  3853. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3854. (defvar org-ds-keyword-length 12
  3855. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3856. (make-variable-buffer-local 'org-ds-keyword-length)
  3857. (defvar org-deadline-regexp nil
  3858. "Matches the DEADLINE keyword.")
  3859. (make-variable-buffer-local 'org-deadline-regexp)
  3860. (defvar org-deadline-time-regexp nil
  3861. "Matches the DEADLINE keyword together with a time stamp.")
  3862. (make-variable-buffer-local 'org-deadline-time-regexp)
  3863. (defvar org-deadline-line-regexp nil
  3864. "Matches the DEADLINE keyword and the rest of the line.")
  3865. (make-variable-buffer-local 'org-deadline-line-regexp)
  3866. (defvar org-scheduled-regexp nil
  3867. "Matches the SCHEDULED keyword.")
  3868. (make-variable-buffer-local 'org-scheduled-regexp)
  3869. (defvar org-scheduled-time-regexp nil
  3870. "Matches the SCHEDULED keyword together with a time stamp.")
  3871. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3872. (defvar org-closed-time-regexp nil
  3873. "Matches the CLOSED keyword together with a time stamp.")
  3874. (make-variable-buffer-local 'org-closed-time-regexp)
  3875. (defvar org-keyword-time-regexp nil
  3876. "Matches any of the 4 keywords, together with the time stamp.")
  3877. (make-variable-buffer-local 'org-keyword-time-regexp)
  3878. (defvar org-keyword-time-not-clock-regexp nil
  3879. "Matches any of the 3 keywords, together with the time stamp.")
  3880. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3881. (defvar org-maybe-keyword-time-regexp nil
  3882. "Matches a timestamp, possibly preceded by a keyword.")
  3883. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3884. (defvar org-all-time-keywords nil
  3885. "List of time keywords.")
  3886. (make-variable-buffer-local 'org-all-time-keywords)
  3887. (defconst org-plain-time-of-day-regexp
  3888. (concat
  3889. "\\(\\<[012]?[0-9]"
  3890. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3891. "\\(--?"
  3892. "\\(\\<[012]?[0-9]"
  3893. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3894. "\\)?")
  3895. "Regular expression to match a plain time or time range.
  3896. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3897. groups carry important information:
  3898. 0 the full match
  3899. 1 the first time, range or not
  3900. 8 the second time, if it is a range.")
  3901. (defconst org-plain-time-extension-regexp
  3902. (concat
  3903. "\\(\\<[012]?[0-9]"
  3904. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3905. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3906. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3907. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3908. groups carry important information:
  3909. 0 the full match
  3910. 7 hours of duration
  3911. 9 minutes of duration")
  3912. (defconst org-stamp-time-of-day-regexp
  3913. (concat
  3914. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3915. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3916. "\\(--?"
  3917. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3918. "Regular expression to match a timestamp time or time range.
  3919. After a match, the following groups carry important information:
  3920. 0 the full match
  3921. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3922. 2 the first time, range or not
  3923. 4 the second time, if it is a range.")
  3924. (defconst org-startup-options
  3925. '(("fold" org-startup-folded t)
  3926. ("overview" org-startup-folded t)
  3927. ("nofold" org-startup-folded nil)
  3928. ("showall" org-startup-folded nil)
  3929. ("showeverything" org-startup-folded showeverything)
  3930. ("content" org-startup-folded content)
  3931. ("indent" org-startup-indented t)
  3932. ("noindent" org-startup-indented nil)
  3933. ("hidestars" org-hide-leading-stars t)
  3934. ("showstars" org-hide-leading-stars nil)
  3935. ("odd" org-odd-levels-only t)
  3936. ("oddeven" org-odd-levels-only nil)
  3937. ("align" org-startup-align-all-tables t)
  3938. ("noalign" org-startup-align-all-tables nil)
  3939. ("inlineimages" org-startup-with-inline-images t)
  3940. ("noinlineimages" org-startup-with-inline-images nil)
  3941. ("customtime" org-display-custom-times t)
  3942. ("logdone" org-log-done time)
  3943. ("lognotedone" org-log-done note)
  3944. ("nologdone" org-log-done nil)
  3945. ("lognoteclock-out" org-log-note-clock-out t)
  3946. ("nolognoteclock-out" org-log-note-clock-out nil)
  3947. ("logrepeat" org-log-repeat state)
  3948. ("lognoterepeat" org-log-repeat note)
  3949. ("logdrawer" org-log-into-drawer t)
  3950. ("nologdrawer" org-log-into-drawer nil)
  3951. ("logstatesreversed" org-log-states-order-reversed t)
  3952. ("nologstatesreversed" org-log-states-order-reversed nil)
  3953. ("nologrepeat" org-log-repeat nil)
  3954. ("logreschedule" org-log-reschedule time)
  3955. ("lognotereschedule" org-log-reschedule note)
  3956. ("nologreschedule" org-log-reschedule nil)
  3957. ("logredeadline" org-log-redeadline time)
  3958. ("lognoteredeadline" org-log-redeadline note)
  3959. ("nologredeadline" org-log-redeadline nil)
  3960. ("logrefile" org-log-refile time)
  3961. ("lognoterefile" org-log-refile note)
  3962. ("nologrefile" org-log-refile nil)
  3963. ("fninline" org-footnote-define-inline t)
  3964. ("nofninline" org-footnote-define-inline nil)
  3965. ("fnlocal" org-footnote-section nil)
  3966. ("fnauto" org-footnote-auto-label t)
  3967. ("fnprompt" org-footnote-auto-label nil)
  3968. ("fnconfirm" org-footnote-auto-label confirm)
  3969. ("fnplain" org-footnote-auto-label plain)
  3970. ("fnadjust" org-footnote-auto-adjust t)
  3971. ("nofnadjust" org-footnote-auto-adjust nil)
  3972. ("constcgs" constants-unit-system cgs)
  3973. ("constSI" constants-unit-system SI)
  3974. ("noptag" org-tag-persistent-alist nil)
  3975. ("hideblocks" org-hide-block-startup t)
  3976. ("nohideblocks" org-hide-block-startup nil)
  3977. ("beamer" org-startup-with-beamer-mode t)
  3978. ("entitiespretty" org-pretty-entities t)
  3979. ("entitiesplain" org-pretty-entities nil))
  3980. "Variable associated with STARTUP options for org-mode.
  3981. Each element is a list of three items: the startup options (as written
  3982. in the #+STARTUP line), the corresponding variable, and the value to set
  3983. this variable to if the option is found. An optional forth element PUSH
  3984. means to push this value onto the list in the variable.")
  3985. (defun org-update-property-plist (key val props)
  3986. "Update PROPS with KEY and VAL."
  3987. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3988. (key (if appending (substring key 0 (- (length key) 1)) key))
  3989. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3990. (previous (cdr (assoc key props))))
  3991. (if appending
  3992. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3993. (cons (cons key val) remainder))))
  3994. (defconst org-block-regexp
  3995. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3996. "Regular expression for hiding blocks.")
  3997. (defconst org-heading-keyword-regexp-format
  3998. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3999. "Printf format for a regexp matching an headline with some keyword.
  4000. This regexp will match the headline of any node which has the
  4001. exact keyword that is put into the format. The keyword isn't in
  4002. any group by default, but the stars and the body are.")
  4003. (defconst org-heading-keyword-maybe-regexp-format
  4004. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4005. "Printf format for a regexp matching an headline, possibly with some keyword.
  4006. This regexp can match any headline with the specified keyword, or
  4007. without a keyword. The keyword isn't in any group by default,
  4008. but the stars and the body are.")
  4009. (defun org-set-regexps-and-options ()
  4010. "Precompute regular expressions for current buffer."
  4011. (when (derived-mode-p 'org-mode)
  4012. (org-set-local 'org-todo-kwd-alist nil)
  4013. (org-set-local 'org-todo-key-alist nil)
  4014. (org-set-local 'org-todo-key-trigger nil)
  4015. (org-set-local 'org-todo-keywords-1 nil)
  4016. (org-set-local 'org-done-keywords nil)
  4017. (org-set-local 'org-todo-heads nil)
  4018. (org-set-local 'org-todo-sets nil)
  4019. (org-set-local 'org-todo-log-states nil)
  4020. (org-set-local 'org-file-properties nil)
  4021. (org-set-local 'org-file-tags nil)
  4022. (let ((re (org-make-options-regexp
  4023. '("CATEGORY" "TODO" "COLUMNS"
  4024. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4025. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4026. "OPTIONS")
  4027. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4028. (splitre "[ \t]+")
  4029. (scripts org-use-sub-superscripts)
  4030. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4031. tail sep kws1 prio props ftags drawers beamer-p
  4032. ext-setup-or-nil setup-contents (start 0))
  4033. (save-excursion
  4034. (save-restriction
  4035. (widen)
  4036. (goto-char (point-min))
  4037. (while (or (and ext-setup-or-nil
  4038. (string-match re ext-setup-or-nil start)
  4039. (setq start (match-end 0)))
  4040. (and (setq ext-setup-or-nil nil start 0)
  4041. (re-search-forward re nil t)))
  4042. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4043. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4044. (if (stringp value) (setq value (org-trim value)))
  4045. (cond
  4046. ((equal key "CATEGORY")
  4047. (setq cat value))
  4048. ((member key '("SEQ_TODO" "TODO"))
  4049. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4050. ((equal key "TYP_TODO")
  4051. (push (cons 'type (org-split-string value splitre)) kwds))
  4052. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4053. ;; general TODO-like setup
  4054. (push (cons (intern (downcase (match-string 1 key)))
  4055. (org-split-string value splitre)) kwds))
  4056. ((equal key "TAGS")
  4057. (setq tags (append tags (if tags '("\\n") nil)
  4058. (org-split-string value splitre))))
  4059. ((equal key "COLUMNS")
  4060. (org-set-local 'org-columns-default-format value))
  4061. ((equal key "LINK")
  4062. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4063. (push (cons (match-string 1 value)
  4064. (org-trim (match-string 2 value)))
  4065. links)))
  4066. ((equal key "PRIORITIES")
  4067. (setq prio (org-split-string value " +")))
  4068. ((equal key "PROPERTY")
  4069. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4070. (setq props (org-update-property-plist (match-string 1 value)
  4071. (match-string 2 value)
  4072. props))))
  4073. ((equal key "FILETAGS")
  4074. (when (string-match "\\S-" value)
  4075. (setq ftags
  4076. (append
  4077. ftags
  4078. (apply 'append
  4079. (mapcar (lambda (x) (org-split-string x ":"))
  4080. (org-split-string value)))))))
  4081. ((equal key "DRAWERS")
  4082. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4083. ((equal key "CONSTANTS")
  4084. (setq const (append const (org-split-string value splitre))))
  4085. ((equal key "STARTUP")
  4086. (let ((opts (org-split-string value splitre))
  4087. l var val)
  4088. (while (setq l (pop opts))
  4089. (when (setq l (assoc l org-startup-options))
  4090. (setq var (nth 1 l) val (nth 2 l))
  4091. (if (not (nth 3 l))
  4092. (set (make-local-variable var) val)
  4093. (if (not (listp (symbol-value var)))
  4094. (set (make-local-variable var) nil))
  4095. (set (make-local-variable var) (symbol-value var))
  4096. (add-to-list var val))))))
  4097. ((equal key "ARCHIVE")
  4098. (setq arch value)
  4099. (remove-text-properties 0 (length arch)
  4100. '(face t fontified t) arch))
  4101. ((equal key "LATEX_CLASS")
  4102. (setq beamer-p (equal value "beamer")))
  4103. ((equal key "OPTIONS")
  4104. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4105. (setq scripts (read (match-string 2 value)))))
  4106. ((equal key "SETUPFILE")
  4107. (setq setup-contents (org-file-contents
  4108. (expand-file-name
  4109. (org-remove-double-quotes value))
  4110. 'noerror))
  4111. (if (not ext-setup-or-nil)
  4112. (setq ext-setup-or-nil setup-contents start 0)
  4113. (setq ext-setup-or-nil
  4114. (concat (substring ext-setup-or-nil 0 start)
  4115. "\n" setup-contents "\n"
  4116. (substring ext-setup-or-nil start)))))))
  4117. ;; search for property blocks
  4118. (goto-char (point-min))
  4119. (while (re-search-forward org-block-regexp nil t)
  4120. (when (equal "PROPERTY" (upcase (match-string 1)))
  4121. (setq value (replace-regexp-in-string
  4122. "[\n\r]" " " (match-string 4)))
  4123. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4124. (setq props (org-update-property-plist (match-string 1 value)
  4125. (match-string 2 value)
  4126. props)))))))
  4127. (org-set-local 'org-use-sub-superscripts scripts)
  4128. (when cat
  4129. (org-set-local 'org-category (intern cat))
  4130. (push (cons "CATEGORY" cat) props))
  4131. (when prio
  4132. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4133. (setq prio (mapcar 'string-to-char prio))
  4134. (org-set-local 'org-highest-priority (nth 0 prio))
  4135. (org-set-local 'org-lowest-priority (nth 1 prio))
  4136. (org-set-local 'org-default-priority (nth 2 prio)))
  4137. (and props (org-set-local 'org-file-properties (nreverse props)))
  4138. (and ftags (org-set-local 'org-file-tags
  4139. (mapcar 'org-add-prop-inherited ftags)))
  4140. (and drawers (org-set-local 'org-drawers drawers))
  4141. (and arch (org-set-local 'org-archive-location arch))
  4142. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4143. ;; Process the TODO keywords
  4144. (unless kwds
  4145. ;; Use the global values as if they had been given locally.
  4146. (setq kwds (default-value 'org-todo-keywords))
  4147. (if (stringp (car kwds))
  4148. (setq kwds (list (cons org-todo-interpretation
  4149. (default-value 'org-todo-keywords)))))
  4150. (setq kwds (reverse kwds)))
  4151. (setq kwds (nreverse kwds))
  4152. (let (inter kws kw)
  4153. (while (setq kws (pop kwds))
  4154. (let ((kws (or
  4155. (run-hook-with-args-until-success
  4156. 'org-todo-setup-filter-hook kws)
  4157. kws)))
  4158. (setq inter (pop kws) sep (member "|" kws)
  4159. kws0 (delete "|" (copy-sequence kws))
  4160. kwsa nil
  4161. kws1 (mapcar
  4162. (lambda (x)
  4163. ;; 1 2
  4164. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4165. (progn
  4166. (setq kw (match-string 1 x)
  4167. key (and (match-end 2) (match-string 2 x))
  4168. log (org-extract-log-state-settings x))
  4169. (push (cons kw (and key (string-to-char key))) kwsa)
  4170. (and log (push log org-todo-log-states))
  4171. kw)
  4172. (error "Invalid TODO keyword %s" x)))
  4173. kws0)
  4174. kwsa (if kwsa (append '((:startgroup))
  4175. (nreverse kwsa)
  4176. '((:endgroup))))
  4177. hw (car kws1)
  4178. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4179. tail (list inter hw (car dws) (org-last dws))))
  4180. (add-to-list 'org-todo-heads hw 'append)
  4181. (push kws1 org-todo-sets)
  4182. (setq org-done-keywords (append org-done-keywords dws nil))
  4183. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4184. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4185. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4186. (setq org-todo-sets (nreverse org-todo-sets)
  4187. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4188. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4189. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4190. ;; Process the constants
  4191. (when const
  4192. (let (e cst)
  4193. (while (setq e (pop const))
  4194. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4195. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4196. (setq org-table-formula-constants-local cst)))
  4197. ;; Process the tags.
  4198. (when tags
  4199. (let (e tgs)
  4200. (while (setq e (pop tags))
  4201. (cond
  4202. ((equal e "{") (push '(:startgroup) tgs))
  4203. ((equal e "}") (push '(:endgroup) tgs))
  4204. ((equal e "\\n") (push '(:newline) tgs))
  4205. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4206. (push (cons (match-string 1 e)
  4207. (string-to-char (match-string 2 e)))
  4208. tgs))
  4209. (t (push (list e) tgs))))
  4210. (org-set-local 'org-tag-alist nil)
  4211. (while (setq e (pop tgs))
  4212. (or (and (stringp (car e))
  4213. (assoc (car e) org-tag-alist))
  4214. (push e org-tag-alist)))))
  4215. ;; Compute the regular expressions and other local variables.
  4216. ;; Using `org-outline-regexp-bol' would complicate them much,
  4217. ;; because of the fixed white space at the end of that string.
  4218. (if (not org-done-keywords)
  4219. (setq org-done-keywords (and org-todo-keywords-1
  4220. (list (org-last org-todo-keywords-1)))))
  4221. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4222. (length org-scheduled-string)
  4223. (length org-clock-string)
  4224. (length org-closed-string)))
  4225. org-drawer-regexp
  4226. (concat "^[ \t]*:\\("
  4227. (mapconcat 'regexp-quote org-drawers "\\|")
  4228. "\\):[ \t]*$")
  4229. org-not-done-keywords
  4230. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4231. org-todo-regexp
  4232. (concat "\\("
  4233. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4234. "\\)")
  4235. org-not-done-regexp
  4236. (concat "\\("
  4237. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4238. "\\)")
  4239. org-not-done-heading-regexp
  4240. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4241. org-todo-line-regexp
  4242. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4243. org-complex-heading-regexp
  4244. (concat "^\\(\\*+\\)"
  4245. "\\(?: +" org-todo-regexp "\\)?"
  4246. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4247. "\\(?: +\\(.*?\\)\\)??"
  4248. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4249. "[ \t]*$")
  4250. org-complex-heading-regexp-format
  4251. (concat "^\\(\\*+\\)"
  4252. "\\(?: +" org-todo-regexp "\\)?"
  4253. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4254. "\\(?: +"
  4255. ;; Stats cookies can be stuck to body.
  4256. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4257. "\\(%s\\)"
  4258. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4259. "\\)"
  4260. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4261. "[ \t]*$")
  4262. org-todo-line-tags-regexp
  4263. (concat "^\\(\\*+\\)"
  4264. "\\(?: +" org-todo-regexp "\\)?"
  4265. "\\(?: +\\(.*?\\)\\)??"
  4266. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4267. "[ \t]*$")
  4268. org-deadline-regexp (concat "\\<" org-deadline-string)
  4269. org-deadline-time-regexp
  4270. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4271. org-deadline-line-regexp
  4272. (concat "\\<\\(" org-deadline-string "\\).*")
  4273. org-scheduled-regexp
  4274. (concat "\\<" org-scheduled-string)
  4275. org-scheduled-time-regexp
  4276. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4277. org-closed-time-regexp
  4278. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4279. org-keyword-time-regexp
  4280. (concat "\\<\\(" org-scheduled-string
  4281. "\\|" org-deadline-string
  4282. "\\|" org-closed-string
  4283. "\\|" org-clock-string "\\)"
  4284. " *[[<]\\([^]>]+\\)[]>]")
  4285. org-keyword-time-not-clock-regexp
  4286. (concat "\\<\\(" org-scheduled-string
  4287. "\\|" org-deadline-string
  4288. "\\|" org-closed-string
  4289. "\\)"
  4290. " *[[<]\\([^]>]+\\)[]>]")
  4291. org-maybe-keyword-time-regexp
  4292. (concat "\\(\\<\\(" org-scheduled-string
  4293. "\\|" org-deadline-string
  4294. "\\|" org-closed-string
  4295. "\\|" org-clock-string "\\)\\)?"
  4296. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4297. org-all-time-keywords
  4298. (mapcar (lambda (w) (substring w 0 -1))
  4299. (list org-scheduled-string org-deadline-string
  4300. org-clock-string org-closed-string))
  4301. )
  4302. (org-compute-latex-and-specials-regexp)
  4303. (org-set-font-lock-defaults))))
  4304. (defun org-file-contents (file &optional noerror)
  4305. "Return the contents of FILE, as a string."
  4306. (if (or (not file)
  4307. (not (file-readable-p file)))
  4308. (if noerror
  4309. (progn
  4310. (message "Cannot read file \"%s\"" file)
  4311. (ding) (sit-for 2)
  4312. "")
  4313. (error "Cannot read file \"%s\"" file))
  4314. (with-temp-buffer
  4315. (insert-file-contents file)
  4316. (buffer-string))))
  4317. (defun org-extract-log-state-settings (x)
  4318. "Extract the log state setting from a TODO keyword string.
  4319. This will extract info from a string like \"WAIT(w@/!)\"."
  4320. (let (kw key log1 log2)
  4321. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4322. (setq kw (match-string 1 x)
  4323. key (and (match-end 2) (match-string 2 x))
  4324. log1 (and (match-end 3) (match-string 3 x))
  4325. log2 (and (match-end 4) (match-string 4 x)))
  4326. (and (or log1 log2)
  4327. (list kw
  4328. (and log1 (if (equal log1 "!") 'time 'note))
  4329. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4330. (defun org-remove-keyword-keys (list)
  4331. "Remove a pair of parenthesis at the end of each string in LIST."
  4332. (mapcar (lambda (x)
  4333. (if (string-match "(.*)$" x)
  4334. (substring x 0 (match-beginning 0))
  4335. x))
  4336. list))
  4337. (defun org-assign-fast-keys (alist)
  4338. "Assign fast keys to a keyword-key alist.
  4339. Respect keys that are already there."
  4340. (let (new e (alt ?0))
  4341. (while (setq e (pop alist))
  4342. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4343. (cdr e)) ;; Key already assigned.
  4344. (push e new)
  4345. (let ((clist (string-to-list (downcase (car e))))
  4346. (used (append new alist)))
  4347. (when (= (car clist) ?@)
  4348. (pop clist))
  4349. (while (and clist (rassoc (car clist) used))
  4350. (pop clist))
  4351. (unless clist
  4352. (while (rassoc alt used)
  4353. (incf alt)))
  4354. (push (cons (car e) (or (car clist) alt)) new))))
  4355. (nreverse new)))
  4356. ;;; Some variables used in various places
  4357. (defvar org-window-configuration nil
  4358. "Used in various places to store a window configuration.")
  4359. (defvar org-selected-window nil
  4360. "Used in various places to store a window configuration.")
  4361. (defvar org-finish-function nil
  4362. "Function to be called when `C-c C-c' is used.
  4363. This is for getting out of special buffers like capture.")
  4364. ;; FIXME: Occasionally check by commenting these, to make sure
  4365. ;; no other functions uses these, forgetting to let-bind them.
  4366. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4367. (defvar org-last-state)
  4368. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4369. ;; Defined somewhere in this file, but used before definition.
  4370. (defvar org-entities) ;; defined in org-entities.el
  4371. (defvar org-struct-menu)
  4372. (defvar org-org-menu)
  4373. (defvar org-tbl-menu)
  4374. ;;;; Define the Org-mode
  4375. ;; We use a before-change function to check if a table might need
  4376. ;; an update.
  4377. (defvar org-table-may-need-update t
  4378. "Indicates that a table might need an update.
  4379. This variable is set by `org-before-change-function'.
  4380. `org-table-align' sets it back to nil.")
  4381. (defun org-before-change-function (beg end)
  4382. "Every change indicates that a table might need an update."
  4383. (setq org-table-may-need-update t))
  4384. (defvar org-mode-map)
  4385. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4386. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4387. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4388. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4389. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4390. (defvar org-table-buffer-is-an nil)
  4391. (defvar bidi-paragraph-direction)
  4392. (defvar buffer-face-mode-face)
  4393. (require 'outline)
  4394. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4395. (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"))
  4396. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4397. ;; Other stuff we need.
  4398. (require 'time-date)
  4399. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4400. (require 'easymenu)
  4401. (require 'overlay)
  4402. ;; (require 'org-macs) moved higher up in the file before it is first used
  4403. (require 'org-entities)
  4404. ;; (require 'org-compat) moved higher up in the file before it is first used
  4405. (require 'org-faces)
  4406. (require 'org-list)
  4407. (require 'org-pcomplete)
  4408. (require 'org-src)
  4409. (require 'org-footnote)
  4410. ;; babel
  4411. (require 'ob)
  4412. ;;;###autoload
  4413. (define-derived-mode org-mode outline-mode "Org"
  4414. "Outline-based notes management and organizer, alias
  4415. \"Carsten's outline-mode for keeping track of everything.\"
  4416. Org-mode develops organizational tasks around a NOTES file which
  4417. contains information about projects as plain text. Org-mode is
  4418. implemented on top of outline-mode, which is ideal to keep the content
  4419. of large files well structured. It supports ToDo items, deadlines and
  4420. time stamps, which magically appear in the diary listing of the Emacs
  4421. calendar. Tables are easily created with a built-in table editor.
  4422. Plain text URL-like links connect to websites, emails (VM), Usenet
  4423. messages (Gnus), BBDB entries, and any files related to the project.
  4424. For printing and sharing of notes, an Org-mode file (or a part of it)
  4425. can be exported as a structured ASCII or HTML file.
  4426. The following commands are available:
  4427. \\{org-mode-map}"
  4428. ;; Get rid of Outline menus, they are not needed
  4429. ;; Need to do this here because define-derived-mode sets up
  4430. ;; the keymap so late. Still, it is a waste to call this each time
  4431. ;; we switch another buffer into org-mode.
  4432. (if (featurep 'xemacs)
  4433. (when (boundp 'outline-mode-menu-heading)
  4434. ;; Assume this is Greg's port, it uses easymenu
  4435. (easy-menu-remove outline-mode-menu-heading)
  4436. (easy-menu-remove outline-mode-menu-show)
  4437. (easy-menu-remove outline-mode-menu-hide))
  4438. (define-key org-mode-map [menu-bar headings] 'undefined)
  4439. (define-key org-mode-map [menu-bar hide] 'undefined)
  4440. (define-key org-mode-map [menu-bar show] 'undefined))
  4441. (org-load-modules-maybe)
  4442. (easy-menu-add org-org-menu)
  4443. (easy-menu-add org-tbl-menu)
  4444. (org-install-agenda-files-menu)
  4445. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4446. (add-to-invisibility-spec '(org-cwidth))
  4447. (add-to-invisibility-spec '(org-hide-block . t))
  4448. (when (featurep 'xemacs)
  4449. (org-set-local 'line-move-ignore-invisible t))
  4450. (org-set-local 'outline-regexp org-outline-regexp)
  4451. (org-set-local 'outline-level 'org-outline-level)
  4452. (setq bidi-paragraph-direction 'left-to-right)
  4453. (when (and org-ellipsis
  4454. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4455. (fboundp 'make-glyph-code))
  4456. (unless org-display-table
  4457. (setq org-display-table (make-display-table)))
  4458. (set-display-table-slot
  4459. org-display-table 4
  4460. (vconcat (mapcar
  4461. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4462. org-ellipsis)))
  4463. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4464. (setq buffer-display-table org-display-table))
  4465. (org-set-regexps-and-options)
  4466. (when (and org-tag-faces (not org-tags-special-faces-re))
  4467. ;; tag faces set outside customize.... force initialization.
  4468. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4469. ;; Calc embedded
  4470. (org-set-local 'calc-embedded-open-mode "# ")
  4471. (modify-syntax-entry ?@ "w")
  4472. (modify-syntax-entry ?\" "\"")
  4473. (if org-startup-truncated (setq truncate-lines t))
  4474. (org-set-local 'font-lock-unfontify-region-function
  4475. 'org-unfontify-region)
  4476. ;; Activate before-change-function
  4477. (org-set-local 'org-table-may-need-update t)
  4478. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4479. 'local)
  4480. ;; Check for running clock before killing a buffer
  4481. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4482. ;; Initialize macros templates.
  4483. (org-macro-initialize-templates)
  4484. ;; Initialize radio targets.
  4485. (org-update-radio-target-regexp)
  4486. ;; Indentation.
  4487. (org-set-local 'indent-line-function 'org-indent-line)
  4488. (org-set-local 'indent-region-function 'org-indent-region)
  4489. ;; Filling and auto-filling.
  4490. (org-setup-filling)
  4491. ;; Comments.
  4492. (org-setup-comments-handling)
  4493. ;; Beginning/end of defun
  4494. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4495. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4496. ;; Next error for sparse trees
  4497. (org-set-local 'next-error-function 'org-occur-next-match)
  4498. ;; Make sure dependence stuff works reliably, even for users who set it
  4499. ;; too late :-(
  4500. (if org-enforce-todo-dependencies
  4501. (add-hook 'org-blocker-hook
  4502. 'org-block-todo-from-children-or-siblings-or-parent)
  4503. (remove-hook 'org-blocker-hook
  4504. 'org-block-todo-from-children-or-siblings-or-parent))
  4505. (if org-enforce-todo-checkbox-dependencies
  4506. (add-hook 'org-blocker-hook
  4507. 'org-block-todo-from-checkboxes)
  4508. (remove-hook 'org-blocker-hook
  4509. 'org-block-todo-from-checkboxes))
  4510. ;; Align options lines
  4511. (org-set-local
  4512. 'align-mode-rules-list
  4513. '((org-in-buffer-settings
  4514. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4515. (modes . '(org-mode)))))
  4516. ;; Imenu
  4517. (org-set-local 'imenu-create-index-function
  4518. 'org-imenu-get-tree)
  4519. ;; Make isearch reveal context
  4520. (if (or (featurep 'xemacs)
  4521. (not (boundp 'outline-isearch-open-invisible-function)))
  4522. ;; Emacs 21 and XEmacs make use of the hook
  4523. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4524. ;; Emacs 22 deals with this through a special variable
  4525. (org-set-local 'outline-isearch-open-invisible-function
  4526. (lambda (&rest ignore) (org-show-context 'isearch))))
  4527. ;; Turn on org-beamer-mode?
  4528. (and org-startup-with-beamer-mode (org-beamer-mode))
  4529. ;; Setup the pcomplete hooks
  4530. (set (make-local-variable 'pcomplete-command-completion-function)
  4531. 'org-pcomplete-initial)
  4532. (set (make-local-variable 'pcomplete-command-name-function)
  4533. 'org-command-at-point)
  4534. (set (make-local-variable 'pcomplete-default-completion-function)
  4535. 'ignore)
  4536. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4537. 'org-parse-arguments)
  4538. (set (make-local-variable 'pcomplete-termination-string) "")
  4539. (when (>= emacs-major-version 23)
  4540. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4541. ;; If empty file that did not turn on org-mode automatically, make it to.
  4542. (if (and org-insert-mode-line-in-empty-file
  4543. (org-called-interactively-p 'any)
  4544. (= (point-min) (point-max)))
  4545. (insert "# -*- mode: org -*-\n\n"))
  4546. (unless org-inhibit-startup
  4547. (when org-startup-align-all-tables
  4548. (let ((bmp (buffer-modified-p)))
  4549. (org-table-map-tables 'org-table-align 'quietly)
  4550. (set-buffer-modified-p bmp)))
  4551. (when org-startup-with-inline-images
  4552. (org-display-inline-images))
  4553. (when org-startup-indented
  4554. (require 'org-indent)
  4555. (org-indent-mode 1))
  4556. (unless org-inhibit-startup-visibility-stuff
  4557. (org-set-startup-visibility)))
  4558. ;; Try to set org-hide correctly
  4559. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4560. (when (fboundp 'abbrev-table-put)
  4561. (abbrev-table-put org-mode-abbrev-table
  4562. :parents (list text-mode-abbrev-table)))
  4563. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4564. (defun org-find-invisible-foreground ()
  4565. (let ((candidates (remove
  4566. "unspecified-bg"
  4567. (nconc
  4568. (list (face-background 'default)
  4569. (face-background 'org-default))
  4570. (mapcar
  4571. (lambda (alist)
  4572. (when (boundp alist)
  4573. (cdr (assoc 'background-color (symbol-value alist)))))
  4574. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4575. (list (face-foreground 'org-hide))))))
  4576. (car (remove nil candidates))))
  4577. (defun org-current-time (&optional rounding-minutes)
  4578. "Current time, possibly rounded to ROUNDING-MINUTES.
  4579. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4580. `org-time-stamp-rounding-minutes'."
  4581. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4582. (car org-time-stamp-rounding-minutes)))
  4583. (time (decode-time)))
  4584. (if (> r 1)
  4585. (apply 'encode-time
  4586. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4587. (nthcdr 2 time)))
  4588. (current-time))))
  4589. (defun org-today ()
  4590. "Return today date, considering `org-extend-today-until'."
  4591. (time-to-days
  4592. (time-subtract (current-time)
  4593. (list 0 (* 3600 org-extend-today-until) 0))))
  4594. ;;;; Font-Lock stuff, including the activators
  4595. (defvar org-mouse-map (make-sparse-keymap))
  4596. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4597. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4598. (when org-mouse-1-follows-link
  4599. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4600. (when org-tab-follows-link
  4601. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4602. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4603. (require 'font-lock)
  4604. (defconst org-non-link-chars "]\t\n\r<>")
  4605. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4606. "shell" "elisp" "doi" "message"))
  4607. (defvar org-link-types-re nil
  4608. "Matches a link that has a url-like prefix like \"http:\"")
  4609. (defvar org-link-re-with-space nil
  4610. "Matches a link with spaces, optional angular brackets around it.")
  4611. (defvar org-link-re-with-space2 nil
  4612. "Matches a link with spaces, optional angular brackets around it.")
  4613. (defvar org-link-re-with-space3 nil
  4614. "Matches a link with spaces, only for internal part in bracket links.")
  4615. (defvar org-angle-link-re nil
  4616. "Matches link with angular brackets, spaces are allowed.")
  4617. (defvar org-plain-link-re nil
  4618. "Matches plain link, without spaces.")
  4619. (defvar org-bracket-link-regexp nil
  4620. "Matches a link in double brackets.")
  4621. (defvar org-bracket-link-analytic-regexp nil
  4622. "Regular expression used to analyze links.
  4623. Here is what the match groups contain after a match:
  4624. 1: http:
  4625. 2: http
  4626. 3: path
  4627. 4: [desc]
  4628. 5: desc")
  4629. (defvar org-bracket-link-analytic-regexp++ nil
  4630. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4631. (defvar org-any-link-re nil
  4632. "Regular expression matching any link.")
  4633. (defcustom org-match-sexp-depth 3
  4634. "Number of stacked braces for sub/superscript matching.
  4635. This has to be set before loading org.el to be effective."
  4636. :group 'org-export-translation ; ??????????????????????????/
  4637. :type 'integer)
  4638. (defun org-create-multibrace-regexp (left right n)
  4639. "Create a regular expression which will match a balanced sexp.
  4640. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4641. as single character strings.
  4642. The regexp returned will match the entire expression including the
  4643. delimiters. It will also define a single group which contains the
  4644. match except for the outermost delimiters. The maximum depth of
  4645. stacked delimiters is N. Escaping delimiters is not possible."
  4646. (let* ((nothing (concat "[^" left right "]*?"))
  4647. (or "\\|")
  4648. (re nothing)
  4649. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4650. (while (> n 1)
  4651. (setq n (1- n)
  4652. re (concat re or next)
  4653. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4654. (concat left "\\(" re "\\)" right)))
  4655. (defvar org-match-substring-regexp
  4656. (concat
  4657. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4658. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4659. "\\|"
  4660. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4661. "\\|"
  4662. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4663. "The regular expression matching a sub- or superscript.")
  4664. (defvar org-match-substring-with-braces-regexp
  4665. (concat
  4666. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4667. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4668. "\\)")
  4669. "The regular expression matching a sub- or superscript, forcing braces.")
  4670. (defun org-make-link-regexps ()
  4671. "Update the link regular expressions.
  4672. This should be called after the variable `org-link-types' has changed."
  4673. (setq org-link-types-re
  4674. (concat
  4675. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4676. org-link-re-with-space
  4677. (concat
  4678. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4679. "\\([^" org-non-link-chars " ]"
  4680. "[^" org-non-link-chars "]*"
  4681. "[^" org-non-link-chars " ]\\)>?")
  4682. org-link-re-with-space2
  4683. (concat
  4684. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4685. "\\([^" org-non-link-chars " ]"
  4686. "[^\t\n\r]*"
  4687. "[^" org-non-link-chars " ]\\)>?")
  4688. org-link-re-with-space3
  4689. (concat
  4690. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4691. "\\([^" org-non-link-chars " ]"
  4692. "[^\t\n\r]*\\)")
  4693. org-angle-link-re
  4694. (concat
  4695. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4696. "\\([^" org-non-link-chars " ]"
  4697. "[^" org-non-link-chars "]*"
  4698. "\\)>")
  4699. org-plain-link-re
  4700. (concat
  4701. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4702. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4703. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4704. org-bracket-link-regexp
  4705. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4706. org-bracket-link-analytic-regexp
  4707. (concat
  4708. "\\[\\["
  4709. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4710. "\\([^]]+\\)"
  4711. "\\]"
  4712. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4713. "\\]")
  4714. org-bracket-link-analytic-regexp++
  4715. (concat
  4716. "\\[\\["
  4717. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4718. "\\([^]]+\\)"
  4719. "\\]"
  4720. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4721. "\\]")
  4722. org-any-link-re
  4723. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4724. org-angle-link-re "\\)\\|\\("
  4725. org-plain-link-re "\\)")))
  4726. (org-make-link-regexps)
  4727. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4728. "Regular expression for fast time stamp matching.")
  4729. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4730. "Regular expression for fast time stamp matching.")
  4731. (defconst org-ts-regexp0
  4732. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4733. "Regular expression matching time strings for analysis.
  4734. This one does not require the space after the date, so it can be used
  4735. on a string that terminates immediately after the date.")
  4736. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4737. "Regular expression matching time strings for analysis.")
  4738. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4739. "Regular expression matching time stamps, with groups.")
  4740. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4741. "Regular expression matching time stamps (also [..]), with groups.")
  4742. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4743. "Regular expression matching a time stamp range.")
  4744. (defconst org-tr-regexp-both
  4745. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4746. "Regular expression matching a time stamp range.")
  4747. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4748. org-ts-regexp "\\)?")
  4749. "Regular expression matching a time stamp or time stamp range.")
  4750. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4751. org-ts-regexp-both "\\)?")
  4752. "Regular expression matching a time stamp or time stamp range.
  4753. The time stamps may be either active or inactive.")
  4754. (defvar org-emph-face nil)
  4755. (defun org-do-emphasis-faces (limit)
  4756. "Run through the buffer and add overlays to emphasized strings."
  4757. (let (rtn a)
  4758. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4759. (if (not (= (char-after (match-beginning 3))
  4760. (char-after (match-beginning 4))))
  4761. (progn
  4762. (setq rtn t)
  4763. (setq a (assoc (match-string 3) org-emphasis-alist))
  4764. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4765. 'face
  4766. (nth 1 a))
  4767. (and (nth 4 a)
  4768. (org-remove-flyspell-overlays-in
  4769. (match-beginning 0) (match-end 0)))
  4770. (add-text-properties (match-beginning 2) (match-end 2)
  4771. '(font-lock-multiline t org-emphasis t))
  4772. (when org-hide-emphasis-markers
  4773. (add-text-properties (match-end 4) (match-beginning 5)
  4774. '(invisible org-link))
  4775. (add-text-properties (match-beginning 3) (match-end 3)
  4776. '(invisible org-link)))))
  4777. (backward-char 1))
  4778. rtn))
  4779. (defun org-emphasize (&optional char)
  4780. "Insert or change an emphasis, i.e. a font like bold or italic.
  4781. If there is an active region, change that region to a new emphasis.
  4782. If there is no region, just insert the marker characters and position
  4783. the cursor between them.
  4784. CHAR should be either the marker character, or the first character of the
  4785. HTML tag associated with that emphasis. If CHAR is a space, the means
  4786. to remove the emphasis of the selected region.
  4787. If char is not given (for example in an interactive call) it
  4788. will be prompted for."
  4789. (interactive)
  4790. (let ((eal org-emphasis-alist) e det
  4791. (erc org-emphasis-regexp-components)
  4792. (prompt "")
  4793. (string "") beg end move tag c s)
  4794. (if (org-region-active-p)
  4795. (setq beg (region-beginning) end (region-end)
  4796. string (buffer-substring beg end))
  4797. (setq move t))
  4798. (while (setq e (pop eal))
  4799. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4800. c (aref tag 0))
  4801. (push (cons c (string-to-char (car e))) det)
  4802. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4803. (substring tag 1)))))
  4804. (setq det (nreverse det))
  4805. (unless char
  4806. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4807. (setq char (read-char-exclusive)))
  4808. (setq char (or (cdr (assoc char det)) char))
  4809. (if (equal char ?\ )
  4810. (setq s "" move nil)
  4811. (unless (assoc (char-to-string char) org-emphasis-alist)
  4812. (error "No such emphasis marker: \"%c\"" char))
  4813. (setq s (char-to-string char)))
  4814. (while (and (> (length string) 1)
  4815. (equal (substring string 0 1) (substring string -1))
  4816. (assoc (substring string 0 1) org-emphasis-alist))
  4817. (setq string (substring string 1 -1)))
  4818. (setq string (concat s string s))
  4819. (if beg (delete-region beg end))
  4820. (unless (or (bolp)
  4821. (string-match (concat "[" (nth 0 erc) "\n]")
  4822. (char-to-string (char-before (point)))))
  4823. (insert " "))
  4824. (unless (or (eobp)
  4825. (string-match (concat "[" (nth 1 erc) "\n]")
  4826. (char-to-string (char-after (point)))))
  4827. (insert " ") (backward-char 1))
  4828. (insert string)
  4829. (and move (backward-char 1))))
  4830. (defconst org-nonsticky-props
  4831. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4832. (defsubst org-rear-nonsticky-at (pos)
  4833. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4834. (defun org-activate-plain-links (limit)
  4835. "Run through the buffer and add overlays to links."
  4836. (catch 'exit
  4837. (let (f hl)
  4838. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4839. (not (org-in-src-block-p)))
  4840. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4841. (setq f (get-text-property (match-beginning 0) 'face))
  4842. (setq hl (org-match-string-no-properties 0))
  4843. (if (or (eq f 'org-tag)
  4844. (and (listp f) (memq 'org-tag f)))
  4845. nil
  4846. (add-text-properties (match-beginning 0) (match-end 0)
  4847. (list 'mouse-face 'highlight
  4848. 'face 'org-link
  4849. 'htmlize-link `(:uri ,hl)
  4850. 'keymap org-mouse-map))
  4851. (org-rear-nonsticky-at (match-end 0)))
  4852. t))))
  4853. (defun org-activate-code (limit)
  4854. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4855. (progn
  4856. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4857. (remove-text-properties (match-beginning 0) (match-end 0)
  4858. '(display t invisible t intangible t))
  4859. t)))
  4860. (defcustom org-src-fontify-natively nil
  4861. "When non-nil, fontify code in code blocks."
  4862. :type 'boolean
  4863. :version "24.1"
  4864. :group 'org-appearance
  4865. :group 'org-babel)
  4866. (defcustom org-allow-promoting-top-level-subtree nil
  4867. "When non-nil, allow promoting a top level subtree.
  4868. The leading star of the top level headline will be replaced
  4869. by a #."
  4870. :type 'boolean
  4871. :version "24.1"
  4872. :group 'org-appearance)
  4873. (defun org-fontify-meta-lines-and-blocks (limit)
  4874. (condition-case nil
  4875. (org-fontify-meta-lines-and-blocks-1 limit)
  4876. (error (message "org-mode fontification error"))))
  4877. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4878. "Fontify #+ lines and blocks, in the correct ways."
  4879. (let ((case-fold-search t))
  4880. (if (re-search-forward
  4881. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4882. limit t)
  4883. (let ((beg (match-beginning 0))
  4884. (block-start (match-end 0))
  4885. (block-end nil)
  4886. (lang (match-string 7))
  4887. (beg1 (line-beginning-position 2))
  4888. (dc1 (downcase (match-string 2)))
  4889. (dc3 (downcase (match-string 3)))
  4890. end end1 quoting block-type ovl)
  4891. (cond
  4892. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4893. ;; a single line of backend-specific content
  4894. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4895. (remove-text-properties (match-beginning 0) (match-end 0)
  4896. '(display t invisible t intangible t))
  4897. (add-text-properties (match-beginning 1) (match-end 3)
  4898. '(font-lock-fontified t face org-meta-line))
  4899. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4900. '(font-lock-fontified t face org-block))
  4901. ; for backend-specific code
  4902. t)
  4903. ((and (match-end 4) (equal dc3 "+begin"))
  4904. ;; Truly a block
  4905. (setq block-type (downcase (match-string 5))
  4906. quoting (member block-type org-protecting-blocks))
  4907. (when (re-search-forward
  4908. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4909. nil t) ;; on purpose, we look further than LIMIT
  4910. (setq end (min (point-max) (match-end 0))
  4911. end1 (min (point-max) (1- (match-beginning 0))))
  4912. (setq block-end (match-beginning 0))
  4913. (when quoting
  4914. (remove-text-properties beg end
  4915. '(display t invisible t intangible t)))
  4916. (add-text-properties
  4917. beg end
  4918. '(font-lock-fontified t font-lock-multiline t))
  4919. (add-text-properties beg beg1 '(face org-meta-line))
  4920. (add-text-properties end1 (min (point-max) (1+ end))
  4921. '(face org-meta-line)) ; for end_src
  4922. (cond
  4923. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4924. (org-src-font-lock-fontify-block lang block-start block-end)
  4925. ;; remove old background overlays
  4926. (mapc (lambda (ov)
  4927. (if (eq (overlay-get ov 'face) 'org-block-background)
  4928. (delete-overlay ov)))
  4929. (overlays-at (/ (+ beg1 block-end) 2)))
  4930. ;; add a background overlay
  4931. (setq ovl (make-overlay beg1 block-end))
  4932. (overlay-put ovl 'face 'org-block-background)
  4933. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4934. (quoting
  4935. (add-text-properties beg1 (min (point-max) (1+ end1))
  4936. '(face org-block))) ; end of source block
  4937. ((not org-fontify-quote-and-verse-blocks))
  4938. ((string= block-type "quote")
  4939. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4940. ((string= block-type "verse")
  4941. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4942. (add-text-properties beg beg1 '(face org-block-begin-line))
  4943. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4944. '(face org-block-end-line))
  4945. t))
  4946. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4947. (add-text-properties
  4948. beg (match-end 3)
  4949. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4950. '(font-lock-fontified t invisible t)
  4951. '(font-lock-fontified t face org-document-info-keyword)))
  4952. (add-text-properties
  4953. (match-beginning 6) (1+ (match-end 6))
  4954. (if (string-equal dc1 "+title:")
  4955. '(font-lock-fontified t face org-document-title)
  4956. '(font-lock-fontified t face org-document-info))))
  4957. ((or (equal dc1 "+results")
  4958. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4959. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4960. "+call:" "+header:" "+headers:" "+name:"))
  4961. (and (match-end 4) (equal dc3 "+attr")))
  4962. (add-text-properties
  4963. beg (match-end 0)
  4964. '(font-lock-fontified t face org-meta-line))
  4965. t)
  4966. ((member dc3 '(" " ""))
  4967. (add-text-properties
  4968. beg (match-end 0)
  4969. '(font-lock-fontified t face font-lock-comment-face)))
  4970. ((not (member (char-after beg) '(?\ ?\t)))
  4971. ;; just any other in-buffer setting, but not indented
  4972. (add-text-properties
  4973. beg (match-end 0)
  4974. '(font-lock-fontified t face org-meta-line))
  4975. t)
  4976. (t nil))))))
  4977. (defun org-activate-angle-links (limit)
  4978. "Run through the buffer and add overlays to links."
  4979. (if (and (re-search-forward org-angle-link-re limit t)
  4980. (not (org-in-src-block-p)))
  4981. (progn
  4982. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4983. (add-text-properties (match-beginning 0) (match-end 0)
  4984. (list 'mouse-face 'highlight
  4985. 'keymap org-mouse-map))
  4986. (org-rear-nonsticky-at (match-end 0))
  4987. t)))
  4988. (defun org-activate-footnote-links (limit)
  4989. "Run through the buffer and add overlays to footnotes."
  4990. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4991. (when fn
  4992. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4993. (org-remove-flyspell-overlays-in beg end)
  4994. (add-text-properties beg end
  4995. (list 'mouse-face 'highlight
  4996. 'keymap org-mouse-map
  4997. 'help-echo
  4998. (if (= (point-at-bol) beg)
  4999. "Footnote definition"
  5000. "Footnote reference")
  5001. 'font-lock-fontified t
  5002. 'font-lock-multiline t
  5003. 'face 'org-footnote))))))
  5004. (defun org-activate-bracket-links (limit)
  5005. "Run through the buffer and add overlays to bracketed links."
  5006. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5007. (not (org-in-src-block-p)))
  5008. (let* ((hl (org-match-string-no-properties 1))
  5009. (help (concat "LINK: " hl))
  5010. ;; FIXME: Above we should remove the escapes. But that
  5011. ;; requires another match, protecting match data, a lot
  5012. ;; of overhead for font-lock.
  5013. (ip (org-maybe-intangible
  5014. (list 'invisible 'org-link
  5015. 'keymap org-mouse-map 'mouse-face 'highlight
  5016. 'font-lock-multiline t 'help-echo help
  5017. 'htmlize-link `(:uri ,hl))))
  5018. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5019. 'font-lock-multiline t 'help-echo help
  5020. 'htmlize-link `(:uri ,hl))))
  5021. ;; We need to remove the invisible property here. Table narrowing
  5022. ;; may have made some of this invisible.
  5023. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5024. (remove-text-properties (match-beginning 0) (match-end 0)
  5025. '(invisible nil))
  5026. (if (match-end 3)
  5027. (progn
  5028. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5029. (org-rear-nonsticky-at (match-beginning 3))
  5030. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5031. (org-rear-nonsticky-at (match-end 3))
  5032. (add-text-properties (match-end 3) (match-end 0) ip)
  5033. (org-rear-nonsticky-at (match-end 0)))
  5034. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5035. (org-rear-nonsticky-at (match-beginning 1))
  5036. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5037. (org-rear-nonsticky-at (match-end 1))
  5038. (add-text-properties (match-end 1) (match-end 0) ip)
  5039. (org-rear-nonsticky-at (match-end 0)))
  5040. t)))
  5041. (defun org-activate-dates (limit)
  5042. "Run through the buffer and add overlays to dates."
  5043. (if (re-search-forward org-tsr-regexp-both limit t)
  5044. (progn
  5045. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5046. (add-text-properties (match-beginning 0) (match-end 0)
  5047. (list 'mouse-face 'highlight
  5048. 'keymap org-mouse-map))
  5049. (org-rear-nonsticky-at (match-end 0))
  5050. (when org-display-custom-times
  5051. (if (match-end 3)
  5052. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5053. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5054. t)))
  5055. (defvar org-target-link-regexp nil
  5056. "Regular expression matching radio targets in plain text.")
  5057. (make-variable-buffer-local 'org-target-link-regexp)
  5058. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5059. "Regular expression matching a link target.")
  5060. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5061. "Regular expression matching a radio target.")
  5062. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5063. "Regular expression matching any target.")
  5064. (defun org-activate-target-links (limit)
  5065. "Run through the buffer and add overlays to target matches."
  5066. (when org-target-link-regexp
  5067. (let ((case-fold-search t))
  5068. (if (re-search-forward org-target-link-regexp limit t)
  5069. (progn
  5070. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5071. (add-text-properties (match-beginning 0) (match-end 0)
  5072. (list 'mouse-face 'highlight
  5073. 'keymap org-mouse-map
  5074. 'help-echo "Radio target link"
  5075. 'org-linked-text t))
  5076. (org-rear-nonsticky-at (match-end 0))
  5077. t)))))
  5078. (defun org-update-radio-target-regexp ()
  5079. "Find all radio targets in this file and update the regular expression."
  5080. (interactive)
  5081. (when (memq 'radio org-activate-links)
  5082. (setq org-target-link-regexp
  5083. (org-make-target-link-regexp (org-all-targets 'radio)))
  5084. (org-restart-font-lock)))
  5085. (defun org-hide-wide-columns (limit)
  5086. (let (s e)
  5087. (setq s (text-property-any (point) (or limit (point-max))
  5088. 'org-cwidth t))
  5089. (when s
  5090. (setq e (next-single-property-change s 'org-cwidth))
  5091. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5092. (goto-char e)
  5093. t)))
  5094. (defvar org-latex-and-specials-regexp nil
  5095. "Regular expression for highlighting export special stuff.")
  5096. (defvar org-match-substring-regexp)
  5097. (defvar org-match-substring-with-braces-regexp)
  5098. ;; This should be with the exporter code, but we also use if for font-locking
  5099. (defconst org-export-html-special-string-regexps
  5100. '(("\\\\-" . "&shy;")
  5101. ("---\\([^-]\\)" . "&mdash;\\1")
  5102. ("--\\([^-]\\)" . "&ndash;\\1")
  5103. ("\\.\\.\\." . "&hellip;"))
  5104. "Regular expressions for special string conversion.")
  5105. (defun org-compute-latex-and-specials-regexp ()
  5106. "Compute regular expression for stuff treated specially by exporters."
  5107. (if (not org-highlight-latex-fragments-and-specials)
  5108. (org-set-local 'org-latex-and-specials-regexp nil)
  5109. (require 'org-exp)
  5110. (let*
  5111. ((matchers (plist-get org-format-latex-options :matchers))
  5112. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5113. org-latex-regexps)))
  5114. (org-export-allow-BIND nil)
  5115. (options (org-combine-plists (org-default-export-plist)
  5116. (org-infile-export-plist)))
  5117. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5118. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5119. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5120. (org-export-html-expand (plist-get options :expand-quoted-html))
  5121. (org-export-with-special-strings (plist-get options :special-strings))
  5122. (re-sub
  5123. (cond
  5124. ((equal org-export-with-sub-superscripts '{})
  5125. (list org-match-substring-with-braces-regexp))
  5126. (org-export-with-sub-superscripts
  5127. (list org-match-substring-regexp))))
  5128. (re-latex
  5129. (if org-export-with-LaTeX-fragments
  5130. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5131. (re-macros
  5132. (if org-export-with-TeX-macros
  5133. (list (concat "\\\\"
  5134. (regexp-opt
  5135. (append
  5136. (delq nil
  5137. (mapcar 'car-safe
  5138. (append org-entities-user
  5139. org-entities)))
  5140. (if (boundp 'org-latex-entities)
  5141. (mapcar (lambda (x)
  5142. (or (car-safe x) x))
  5143. org-latex-entities)
  5144. nil))
  5145. 'words))) ; FIXME
  5146. ))
  5147. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5148. (re-special (if org-export-with-special-strings
  5149. (mapcar (lambda (x) (car x))
  5150. org-export-html-special-string-regexps)))
  5151. (re-rest
  5152. (delq nil
  5153. (list
  5154. (if org-export-html-expand "@<[^>\n]+>")
  5155. ))))
  5156. (org-set-local
  5157. 'org-latex-and-specials-regexp
  5158. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5159. re-rest) "\\|")))))
  5160. (defun org-do-latex-and-special-faces (limit)
  5161. "Run through the buffer and add overlays to links."
  5162. (when org-latex-and-specials-regexp
  5163. (let (rtn d)
  5164. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5165. limit t))
  5166. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5167. 'face))
  5168. '(org-code org-verbatim underline)))
  5169. (progn
  5170. (setq rtn t
  5171. d (cond ((member (char-after (1+ (match-beginning 0)))
  5172. '(?_ ?^)) 1)
  5173. (t 0)))
  5174. (font-lock-prepend-text-property
  5175. (+ d (match-beginning 0)) (match-end 0)
  5176. 'face 'org-latex-and-export-specials)
  5177. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5178. '(font-lock-multiline t)))))
  5179. rtn)))
  5180. (defun org-restart-font-lock ()
  5181. "Restart `font-lock-mode', to force refontification."
  5182. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5183. (font-lock-mode -1)
  5184. (font-lock-mode 1)))
  5185. (defun org-all-targets (&optional radio)
  5186. "Return a list of all targets in this file.
  5187. When optional argument RADIO is non-nil, only find radio
  5188. targets."
  5189. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5190. (save-excursion
  5191. (goto-char (point-min))
  5192. (while (re-search-forward re nil t)
  5193. ;; Make sure point is really within the object.
  5194. (backward-char)
  5195. (let ((obj (org-element-context)))
  5196. (when (memq (org-element-type obj) '(radio-target target))
  5197. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5198. rtn)))
  5199. (defun org-make-target-link-regexp (targets)
  5200. "Make regular expression matching all strings in TARGETS.
  5201. The regular expression finds the targets also if there is a line break
  5202. between words."
  5203. (and targets
  5204. (concat
  5205. "\\<\\("
  5206. (mapconcat
  5207. (lambda (x)
  5208. (setq x (regexp-quote x))
  5209. (while (string-match " +" x)
  5210. (setq x (replace-match "\\s-+" t t x)))
  5211. x)
  5212. targets
  5213. "\\|")
  5214. "\\)\\>")))
  5215. (defun org-activate-tags (limit)
  5216. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5217. (progn
  5218. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5219. (add-text-properties (match-beginning 1) (match-end 1)
  5220. (list 'mouse-face 'highlight
  5221. 'keymap org-mouse-map))
  5222. (org-rear-nonsticky-at (match-end 1))
  5223. t)))
  5224. (defun org-outline-level ()
  5225. "Compute the outline level of the heading at point.
  5226. If this is called at a normal headline, the level is the number of stars.
  5227. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5228. (save-excursion
  5229. (if (not (condition-case nil
  5230. (org-back-to-heading t)
  5231. (error nil)))
  5232. 0
  5233. (looking-at org-outline-regexp)
  5234. (1- (- (match-end 0) (match-beginning 0))))))
  5235. (defvar org-font-lock-keywords nil)
  5236. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5237. "Regular expression matching a property line.")
  5238. (defvar org-font-lock-hook nil
  5239. "Functions to be called for special font lock stuff.")
  5240. (defvar org-font-lock-set-keywords-hook nil
  5241. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5242. This is called after `org-font-lock-extra-keywords' is defined, but before
  5243. it is installed to be used by font lock. This can be useful if something
  5244. needs to be inserted at a specific position in the font-lock sequence.")
  5245. (defun org-font-lock-hook (limit)
  5246. "Run `org-font-lock-hook' within LIMIT."
  5247. (run-hook-with-args 'org-font-lock-hook limit))
  5248. (defun org-set-font-lock-defaults ()
  5249. "Set font lock defaults for the current buffer."
  5250. (let* ((em org-fontify-emphasized-text)
  5251. (lk org-activate-links)
  5252. (org-font-lock-extra-keywords
  5253. (list
  5254. ;; Call the hook
  5255. '(org-font-lock-hook)
  5256. ;; Headlines
  5257. `(,(if org-fontify-whole-heading-line
  5258. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5259. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5260. (1 (org-get-level-face 1))
  5261. (2 (org-get-level-face 2))
  5262. (3 (org-get-level-face 3)))
  5263. ;; Table lines
  5264. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5265. (1 'org-table t))
  5266. ;; Table internals
  5267. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5268. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5269. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5270. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5271. ;; Drawers
  5272. (list org-drawer-regexp '(0 'org-special-keyword t))
  5273. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5274. ;; Properties
  5275. (list org-property-re
  5276. '(1 'org-special-keyword t)
  5277. '(3 'org-property-value t))
  5278. ;; Links
  5279. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5280. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5281. (if (memq 'plain lk) '(org-activate-plain-links))
  5282. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5283. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5284. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5285. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5286. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5287. '(org-hide-wide-columns (0 nil append))
  5288. ;; TODO keyword
  5289. (list (format org-heading-keyword-regexp-format
  5290. org-todo-regexp)
  5291. '(2 (org-get-todo-face 2) t))
  5292. ;; DONE
  5293. (if org-fontify-done-headline
  5294. (list (format org-heading-keyword-regexp-format
  5295. (concat
  5296. "\\(?:"
  5297. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5298. "\\)"))
  5299. '(2 'org-headline-done t))
  5300. nil)
  5301. ;; Priorities
  5302. '(org-font-lock-add-priority-faces)
  5303. ;; Tags
  5304. '(org-font-lock-add-tag-faces)
  5305. ;; Special keywords
  5306. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5307. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5308. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5309. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5310. ;; Emphasis
  5311. (if em
  5312. (if (featurep 'xemacs)
  5313. '(org-do-emphasis-faces (0 nil append))
  5314. '(org-do-emphasis-faces)))
  5315. ;; Checkboxes
  5316. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5317. 1 'org-checkbox prepend)
  5318. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5319. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5320. (0 (org-get-checkbox-statistics-face) t)))
  5321. ;; Description list items
  5322. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5323. 1 'org-list-dt prepend)
  5324. ;; ARCHIVEd headings
  5325. (list (concat
  5326. org-outline-regexp-bol
  5327. "\\(.*:" org-archive-tag ":.*\\)")
  5328. '(1 'org-archived prepend))
  5329. ;; Specials
  5330. '(org-do-latex-and-special-faces)
  5331. '(org-fontify-entities)
  5332. '(org-raise-scripts)
  5333. ;; Code
  5334. '(org-activate-code (1 'org-code t))
  5335. ;; COMMENT
  5336. (list (format org-heading-keyword-regexp-format
  5337. (concat "\\("
  5338. org-comment-string "\\|" org-quote-string
  5339. "\\)"))
  5340. '(2 'org-special-keyword t))
  5341. ;; Blocks and meta lines
  5342. '(org-fontify-meta-lines-and-blocks)
  5343. )))
  5344. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5345. (run-hooks 'org-font-lock-set-keywords-hook)
  5346. ;; Now set the full font-lock-keywords
  5347. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5348. (org-set-local 'font-lock-defaults
  5349. '(org-font-lock-keywords t nil nil backward-paragraph))
  5350. (kill-local-variable 'font-lock-keywords) nil))
  5351. (defun org-toggle-pretty-entities ()
  5352. "Toggle the composition display of entities as UTF8 characters."
  5353. (interactive)
  5354. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5355. (org-restart-font-lock)
  5356. (if org-pretty-entities
  5357. (message "Entities are displayed as UTF8 characters")
  5358. (save-restriction
  5359. (widen)
  5360. (org-decompose-region (point-min) (point-max))
  5361. (message "Entities are displayed plain"))))
  5362. (defvar org-custom-properties-overlays nil
  5363. "List of overlays used for custom properties.")
  5364. (make-variable-buffer-local 'org-custom-properties-overlays)
  5365. (defun org-toggle-custom-properties-visibility ()
  5366. "Display or hide properties in `org-custom-properties'."
  5367. (interactive)
  5368. (if org-custom-properties-overlays
  5369. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5370. (setq org-custom-properties-overlays nil))
  5371. (unless (not org-custom-properties)
  5372. (save-excursion
  5373. (save-restriction
  5374. (widen)
  5375. (goto-char (point-min))
  5376. (while (re-search-forward org-property-re nil t)
  5377. (mapc (lambda(p)
  5378. (when (equal p (substring (match-string 1) 1 -1))
  5379. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5380. (overlay-put o 'invisible t)
  5381. (overlay-put o 'org-custom-property t)
  5382. (push o org-custom-properties-overlays))))
  5383. org-custom-properties)))))))
  5384. (defun org-fontify-entities (limit)
  5385. "Find an entity to fontify."
  5386. (let (ee)
  5387. (when org-pretty-entities
  5388. (catch 'match
  5389. (while (re-search-forward
  5390. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5391. limit t)
  5392. (if (and (not (org-in-indented-comment-line))
  5393. (setq ee (org-entity-get (match-string 1)))
  5394. (= (length (nth 6 ee)) 1))
  5395. (let*
  5396. ((end (if (equal (match-string 2) "{}")
  5397. (match-end 2)
  5398. (match-end 1))))
  5399. (add-text-properties
  5400. (match-beginning 0) end
  5401. (list 'font-lock-fontified t))
  5402. (compose-region (match-beginning 0) end
  5403. (nth 6 ee) nil)
  5404. (backward-char 1)
  5405. (throw 'match t))))
  5406. nil))))
  5407. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5408. "Fontify string S like in Org-mode."
  5409. (with-temp-buffer
  5410. (insert s)
  5411. (let ((org-odd-levels-only odd-levels))
  5412. (org-mode)
  5413. (font-lock-fontify-buffer)
  5414. (buffer-string))))
  5415. (defvar org-m nil)
  5416. (defvar org-l nil)
  5417. (defvar org-f nil)
  5418. (defun org-get-level-face (n)
  5419. "Get the right face for match N in font-lock matching of headlines."
  5420. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5421. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5422. (if org-cycle-level-faces
  5423. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5424. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5425. (cond
  5426. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5427. ((eq n 2) org-f)
  5428. (t (if org-level-color-stars-only nil org-f))))
  5429. (defun org-get-todo-face (kwd)
  5430. "Get the right face for a TODO keyword KWD.
  5431. If KWD is a number, get the corresponding match group."
  5432. (if (numberp kwd) (setq kwd (match-string kwd)))
  5433. (or (org-face-from-face-or-color
  5434. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5435. (and (member kwd org-done-keywords) 'org-done)
  5436. 'org-todo))
  5437. (defun org-face-from-face-or-color (context inherit face-or-color)
  5438. "Create a face list that inherits INHERIT, but sets the foreground color.
  5439. When FACE-OR-COLOR is not a string, just return it."
  5440. (if (stringp face-or-color)
  5441. (list :inherit inherit
  5442. (cdr (assoc context org-faces-easy-properties))
  5443. face-or-color)
  5444. face-or-color))
  5445. (defun org-font-lock-add-tag-faces (limit)
  5446. "Add the special tag faces."
  5447. (when (and org-tag-faces org-tags-special-faces-re)
  5448. (while (re-search-forward org-tags-special-faces-re limit t)
  5449. (add-text-properties (match-beginning 1) (match-end 1)
  5450. (list 'face (org-get-tag-face 1)
  5451. 'font-lock-fontified t))
  5452. (backward-char 1))))
  5453. (defun org-font-lock-add-priority-faces (limit)
  5454. "Add the special priority faces."
  5455. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5456. (when (save-match-data (org-at-heading-p))
  5457. (add-text-properties
  5458. (match-beginning 0) (match-end 0)
  5459. (list 'face (or (org-face-from-face-or-color
  5460. 'priority 'org-priority
  5461. (cdr (assoc (char-after (match-beginning 1))
  5462. org-priority-faces)))
  5463. 'org-priority)
  5464. 'font-lock-fontified t)))))
  5465. (defun org-get-tag-face (kwd)
  5466. "Get the right face for a TODO keyword KWD.
  5467. If KWD is a number, get the corresponding match group."
  5468. (if (numberp kwd) (setq kwd (match-string kwd)))
  5469. (or (org-face-from-face-or-color
  5470. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5471. 'org-tag))
  5472. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5473. "Remove fontification and activation overlays from links."
  5474. (font-lock-default-unfontify-region beg end)
  5475. (let* ((buffer-undo-list t)
  5476. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5477. (inhibit-modification-hooks t)
  5478. deactivate-mark buffer-file-name buffer-file-truename)
  5479. (org-decompose-region beg end)
  5480. (remove-text-properties beg end
  5481. '(mouse-face t keymap t org-linked-text t
  5482. invisible t intangible t
  5483. org-no-flyspell t org-emphasis t))
  5484. (org-remove-font-lock-display-properties beg end)))
  5485. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5486. ((raise 0.3) (height 0.7))
  5487. ((raise -0.5))
  5488. ((raise 0.5)))
  5489. "Display properties for showing superscripts and subscripts.")
  5490. (defun org-remove-font-lock-display-properties (beg end)
  5491. "Remove specific display properties that have been added by font lock.
  5492. The will remove the raise properties that are used to show superscripts
  5493. and subscripts."
  5494. (let (next prop)
  5495. (while (< beg end)
  5496. (setq next (next-single-property-change beg 'display nil end)
  5497. prop (get-text-property beg 'display))
  5498. (if (member prop org-script-display)
  5499. (put-text-property beg next 'display nil))
  5500. (setq beg next))))
  5501. (defun org-raise-scripts (limit)
  5502. "Add raise properties to sub/superscripts."
  5503. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5504. (if (re-search-forward
  5505. (if (eq org-use-sub-superscripts t)
  5506. org-match-substring-regexp
  5507. org-match-substring-with-braces-regexp)
  5508. limit t)
  5509. (let* ((pos (point)) table-p comment-p
  5510. (mpos (match-beginning 3))
  5511. (emph-p (get-text-property mpos 'org-emphasis))
  5512. (link-p (get-text-property mpos 'mouse-face))
  5513. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5514. (goto-char (point-at-bol))
  5515. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5516. comment-p (org-looking-at-p "[ \t]*#"))
  5517. (goto-char pos)
  5518. ;; FIXME: Should we go back one character here, for a_b^c
  5519. ;; (goto-char (1- pos)) ;????????????????????
  5520. (if (or comment-p emph-p link-p keyw-p)
  5521. t
  5522. (put-text-property (match-beginning 3) (match-end 0)
  5523. 'display
  5524. (if (equal (char-after (match-beginning 2)) ?^)
  5525. (nth (if table-p 3 1) org-script-display)
  5526. (nth (if table-p 2 0) org-script-display)))
  5527. (add-text-properties (match-beginning 2) (match-end 2)
  5528. (list 'invisible t
  5529. 'org-dwidth t 'org-dwidth-n 1))
  5530. (if (and (eq (char-after (match-beginning 3)) ?{)
  5531. (eq (char-before (match-end 3)) ?}))
  5532. (progn
  5533. (add-text-properties
  5534. (match-beginning 3) (1+ (match-beginning 3))
  5535. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5536. (add-text-properties
  5537. (1- (match-end 3)) (match-end 3)
  5538. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5539. t)))))
  5540. ;;;; Visibility cycling, including org-goto and indirect buffer
  5541. ;;; Cycling
  5542. (defvar org-cycle-global-status nil)
  5543. (make-variable-buffer-local 'org-cycle-global-status)
  5544. (defvar org-cycle-subtree-status nil)
  5545. (make-variable-buffer-local 'org-cycle-subtree-status)
  5546. (defvar org-inlinetask-min-level)
  5547. ;;;###autoload
  5548. (defun org-cycle (&optional arg)
  5549. "TAB-action and visibility cycling for Org-mode.
  5550. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5551. is outline visibility cycling, but it also invokes other actions
  5552. in special contexts.
  5553. - When this function is called with a prefix argument, rotate the entire
  5554. buffer through 3 states (global cycling)
  5555. 1. OVERVIEW: Show only top-level headlines.
  5556. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5557. 3. SHOW ALL: Show everything.
  5558. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5559. determined by the variable `org-startup-folded', and by any VISIBILITY
  5560. properties in the buffer.
  5561. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5562. including any drawers.
  5563. - When inside a table, re-align the table and move to the next field.
  5564. - When point is at the beginning of a headline, rotate the subtree started
  5565. by this line through 3 different states (local cycling)
  5566. 1. FOLDED: Only the main headline is shown.
  5567. 2. CHILDREN: The main headline and the direct children are shown.
  5568. From this state, you can move to one of the children
  5569. and zoom in further.
  5570. 3. SUBTREE: Show the entire subtree, including body text.
  5571. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5572. - When point is at the beginning of an empty headline and the variable
  5573. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5574. of the headline by demoting and promoting it to likely levels. This
  5575. speeds up creation document structure by pressing TAB once or several
  5576. times right after creating a new headline.
  5577. - When there is a numeric prefix, go up to a heading with level ARG, do
  5578. a `show-subtree' and return to the previous cursor position. If ARG
  5579. is negative, go up that many levels.
  5580. - When point is not at the beginning of a headline, execute the global
  5581. binding for TAB, which is re-indenting the line. See the option
  5582. `org-cycle-emulate-tab' for details.
  5583. - Special case: if point is at the beginning of the buffer and there is
  5584. no headline in line 1, this function will act as if called with prefix arg
  5585. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5586. But only if also the variable `org-cycle-global-at-bob' is t."
  5587. (interactive "P")
  5588. (org-load-modules-maybe)
  5589. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5590. (and org-cycle-level-after-item/entry-creation
  5591. (or (org-cycle-level)
  5592. (org-cycle-item-indentation))))
  5593. (let* (message-log-max ; Don't populate the *Messages* buffer
  5594. (limit-level
  5595. (or org-cycle-max-level
  5596. (and (boundp 'org-inlinetask-min-level)
  5597. org-inlinetask-min-level
  5598. (1- org-inlinetask-min-level))))
  5599. (nstars (and limit-level
  5600. (if org-odd-levels-only
  5601. (and limit-level (1- (* limit-level 2)))
  5602. limit-level)))
  5603. (org-outline-regexp
  5604. (if (not (derived-mode-p 'org-mode))
  5605. outline-regexp
  5606. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5607. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5608. (not (looking-at org-outline-regexp))))
  5609. (org-cycle-hook
  5610. (if bob-special
  5611. (delq 'org-optimize-window-after-visibility-change
  5612. (copy-sequence org-cycle-hook))
  5613. org-cycle-hook))
  5614. (pos (point)))
  5615. (if (or bob-special (equal arg '(4)))
  5616. ;; special case: use global cycling
  5617. (setq arg t))
  5618. (cond
  5619. ((equal arg '(16))
  5620. (setq last-command 'dummy)
  5621. (org-set-startup-visibility)
  5622. (message "Startup visibility, plus VISIBILITY properties"))
  5623. ((equal arg '(64))
  5624. (show-all)
  5625. (message "Entire buffer visible, including drawers"))
  5626. ;; Table: enter it or move to the next field.
  5627. ((org-at-table-p 'any)
  5628. (if (org-at-table.el-p)
  5629. (message "Use C-c ' to edit table.el tables")
  5630. (if arg (org-table-edit-field t)
  5631. (org-table-justify-field-maybe)
  5632. (call-interactively 'org-table-next-field))))
  5633. ((run-hook-with-args-until-success
  5634. 'org-tab-after-check-for-table-hook))
  5635. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5636. ((eq arg t) (org-cycle-internal-global))
  5637. ;; Drawers: delegate to `org-flag-drawer'.
  5638. ((and org-drawers org-drawer-regexp
  5639. (save-excursion
  5640. (beginning-of-line 1)
  5641. (looking-at org-drawer-regexp)))
  5642. (org-flag-drawer ; toggle block visibility
  5643. (not (get-char-property (match-end 0) 'invisible))))
  5644. ;; Show-subtree, ARG levels up from here.
  5645. ((integerp arg)
  5646. (save-excursion
  5647. (org-back-to-heading)
  5648. (outline-up-heading (if (< arg 0) (- arg)
  5649. (- (funcall outline-level) arg)))
  5650. (org-show-subtree)))
  5651. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5652. ((and (featurep 'org-inlinetask)
  5653. (org-inlinetask-at-task-p)
  5654. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5655. (org-inlinetask-toggle-visibility))
  5656. ((org-try-cdlatex-tab))
  5657. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5658. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5659. (save-excursion (beginning-of-line 1)
  5660. (looking-at org-outline-regexp)))
  5661. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5662. (org-cycle-internal-local))
  5663. ;; From there: TAB emulation and template completion.
  5664. (buffer-read-only (org-back-to-heading))
  5665. ((run-hook-with-args-until-success
  5666. 'org-tab-after-check-for-cycling-hook))
  5667. ((org-try-structure-completion))
  5668. ((run-hook-with-args-until-success
  5669. 'org-tab-before-tab-emulation-hook))
  5670. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5671. (or (not (bolp))
  5672. (not (looking-at org-outline-regexp))))
  5673. (call-interactively (global-key-binding "\t")))
  5674. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5675. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5676. (or (and (eq org-cycle-emulate-tab 'white)
  5677. (= (match-end 0) (point-at-eol)))
  5678. (and (eq org-cycle-emulate-tab 'whitestart)
  5679. (>= (match-end 0) pos))))
  5680. t
  5681. (eq org-cycle-emulate-tab t))
  5682. (call-interactively (global-key-binding "\t")))
  5683. (t (save-excursion
  5684. (org-back-to-heading)
  5685. (org-cycle)))))))
  5686. (defun org-cycle-internal-global ()
  5687. "Do the global cycling action."
  5688. ;; Hack to avoid display of messages for .org attachments in Gnus
  5689. (let (message-log-max ; Don't populate the *Messages* buffer
  5690. (ga (string-match "\\*fontification" (buffer-name))))
  5691. (cond
  5692. ((and (eq last-command this-command)
  5693. (eq org-cycle-global-status 'overview))
  5694. ;; We just created the overview - now do table of contents
  5695. ;; This can be slow in very large buffers, so indicate action
  5696. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5697. (unless ga (message "CONTENTS..."))
  5698. (org-content)
  5699. (unless ga (message "CONTENTS...done"))
  5700. (setq org-cycle-global-status 'contents)
  5701. (run-hook-with-args 'org-cycle-hook 'contents))
  5702. ((and (eq last-command this-command)
  5703. (eq org-cycle-global-status 'contents))
  5704. ;; We just showed the table of contents - now show everything
  5705. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5706. (show-all)
  5707. (unless ga (message "SHOW ALL"))
  5708. (setq org-cycle-global-status 'all)
  5709. (run-hook-with-args 'org-cycle-hook 'all))
  5710. (t
  5711. ;; Default action: go to overview
  5712. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5713. (org-overview)
  5714. (unless ga (message "OVERVIEW"))
  5715. (setq org-cycle-global-status 'overview)
  5716. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5717. (defun org-cycle-internal-local ()
  5718. "Do the local cycling action."
  5719. (let (message-log-max ; Don't populate the *Messages* buffer
  5720. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5721. ;; First, determine end of headline (EOH), end of subtree or item
  5722. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5723. (save-excursion
  5724. (if (org-at-item-p)
  5725. (progn
  5726. (beginning-of-line)
  5727. (setq struct (org-list-struct))
  5728. (setq eoh (point-at-eol))
  5729. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5730. (setq has-children (org-list-has-child-p (point) struct)))
  5731. (org-back-to-heading)
  5732. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5733. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5734. (setq has-children
  5735. (or (save-excursion
  5736. (let ((level (funcall outline-level)))
  5737. (outline-next-heading)
  5738. (and (org-at-heading-p t)
  5739. (> (funcall outline-level) level))))
  5740. (save-excursion
  5741. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5742. ;; Determine end invisible part of buffer (EOL)
  5743. (beginning-of-line 2)
  5744. ;; XEmacs doesn't have `next-single-char-property-change'
  5745. (if (featurep 'xemacs)
  5746. (while (and (not (eobp)) ;; this is like `next-line'
  5747. (get-char-property (1- (point)) 'invisible))
  5748. (beginning-of-line 2))
  5749. (while (and (not (eobp)) ;; this is like `next-line'
  5750. (get-char-property (1- (point)) 'invisible))
  5751. (goto-char (next-single-char-property-change (point) 'invisible))
  5752. (and (eolp) (beginning-of-line 2))))
  5753. (setq eol (point)))
  5754. ;; Find out what to do next and set `this-command'
  5755. (cond
  5756. ((= eos eoh)
  5757. ;; Nothing is hidden behind this heading
  5758. (unless (org-before-first-heading-p)
  5759. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5760. (message "EMPTY ENTRY")
  5761. (setq org-cycle-subtree-status nil)
  5762. (save-excursion
  5763. (goto-char eos)
  5764. (outline-next-heading)
  5765. (if (outline-invisible-p) (org-flag-heading nil))))
  5766. ((and (or (>= eol eos)
  5767. (not (string-match "\\S-" (buffer-substring eol eos))))
  5768. (or has-children
  5769. (not (setq children-skipped
  5770. org-cycle-skip-children-state-if-no-children))))
  5771. ;; Entire subtree is hidden in one line: children view
  5772. (unless (org-before-first-heading-p)
  5773. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5774. (if (org-at-item-p)
  5775. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5776. (org-show-entry)
  5777. (org-with-limited-levels (show-children))
  5778. ;; FIXME: This slows down the func way too much.
  5779. ;; How keep drawers hidden in subtree anyway?
  5780. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5781. ;; (org-cycle-hide-drawers 'subtree))
  5782. ;; Fold every list in subtree to top-level items.
  5783. (when (eq org-cycle-include-plain-lists 'integrate)
  5784. (save-excursion
  5785. (org-back-to-heading)
  5786. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5787. (beginning-of-line 1)
  5788. (let* ((struct (org-list-struct))
  5789. (prevs (org-list-prevs-alist struct))
  5790. (end (org-list-get-bottom-point struct)))
  5791. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5792. (org-list-get-all-items (point) struct prevs))
  5793. (goto-char end))))))
  5794. (message "CHILDREN")
  5795. (save-excursion
  5796. (goto-char eos)
  5797. (outline-next-heading)
  5798. (if (outline-invisible-p) (org-flag-heading nil)))
  5799. (setq org-cycle-subtree-status 'children)
  5800. (unless (org-before-first-heading-p)
  5801. (run-hook-with-args 'org-cycle-hook 'children)))
  5802. ((or children-skipped
  5803. (and (eq last-command this-command)
  5804. (eq org-cycle-subtree-status 'children)))
  5805. ;; We just showed the children, or no children are there,
  5806. ;; now show everything.
  5807. (unless (org-before-first-heading-p)
  5808. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5809. (outline-flag-region eoh eos nil)
  5810. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5811. (setq org-cycle-subtree-status 'subtree)
  5812. (unless (org-before-first-heading-p)
  5813. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5814. (t
  5815. ;; Default action: hide the subtree.
  5816. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5817. (outline-flag-region eoh eos t)
  5818. (message "FOLDED")
  5819. (setq org-cycle-subtree-status 'folded)
  5820. (unless (org-before-first-heading-p)
  5821. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5822. ;;;###autoload
  5823. (defun org-global-cycle (&optional arg)
  5824. "Cycle the global visibility. For details see `org-cycle'.
  5825. With \\[universal-argument] prefix arg, switch to startup visibility.
  5826. With a numeric prefix, show all headlines up to that level."
  5827. (interactive "P")
  5828. (let ((org-cycle-include-plain-lists
  5829. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5830. (cond
  5831. ((integerp arg)
  5832. (show-all)
  5833. (hide-sublevels arg)
  5834. (setq org-cycle-global-status 'contents))
  5835. ((equal arg '(4))
  5836. (org-set-startup-visibility)
  5837. (message "Startup visibility, plus VISIBILITY properties."))
  5838. (t
  5839. (org-cycle '(4))))))
  5840. (defun org-set-startup-visibility ()
  5841. "Set the visibility required by startup options and properties."
  5842. (cond
  5843. ((eq org-startup-folded t)
  5844. (org-cycle '(4)))
  5845. ((eq org-startup-folded 'content)
  5846. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5847. (org-cycle '(4)) (org-cycle '(4)))))
  5848. (unless (eq org-startup-folded 'showeverything)
  5849. (if org-hide-block-startup (org-hide-block-all))
  5850. (org-set-visibility-according-to-property 'no-cleanup)
  5851. (org-cycle-hide-archived-subtrees 'all)
  5852. (org-cycle-hide-drawers 'all)
  5853. (org-cycle-show-empty-lines t)))
  5854. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5855. "Switch subtree visibilities according to :VISIBILITY: property."
  5856. (interactive)
  5857. (let (org-show-entry-below state)
  5858. (save-excursion
  5859. (goto-char (point-min))
  5860. (while (re-search-forward
  5861. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5862. nil t)
  5863. (setq state (match-string 1))
  5864. (save-excursion
  5865. (org-back-to-heading t)
  5866. (hide-subtree)
  5867. (org-reveal)
  5868. (cond
  5869. ((equal state '("fold" "folded"))
  5870. (hide-subtree))
  5871. ((equal state "children")
  5872. (org-show-hidden-entry)
  5873. (show-children))
  5874. ((equal state "content")
  5875. (save-excursion
  5876. (save-restriction
  5877. (org-narrow-to-subtree)
  5878. (org-content))))
  5879. ((member state '("all" "showall"))
  5880. (show-subtree)))))
  5881. (unless no-cleanup
  5882. (org-cycle-hide-archived-subtrees 'all)
  5883. (org-cycle-hide-drawers 'all)
  5884. (org-cycle-show-empty-lines 'all)))))
  5885. ;; This function uses outline-regexp instead of the more fundamental
  5886. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5887. ;; buffers, where outline-regexp is needed.
  5888. (defun org-overview ()
  5889. "Switch to overview mode, showing only top-level headlines.
  5890. Really, this shows all headlines with level equal or greater than the level
  5891. of the first headline in the buffer. This is important, because if the
  5892. first headline is not level one, then (hide-sublevels 1) gives confusing
  5893. results."
  5894. (interactive)
  5895. (let ((level (save-excursion
  5896. (goto-char (point-min))
  5897. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5898. (progn
  5899. (goto-char (match-beginning 0))
  5900. (funcall outline-level))))))
  5901. (and level (hide-sublevels level))))
  5902. (defun org-content (&optional arg)
  5903. "Show all headlines in the buffer, like a table of contents.
  5904. With numerical argument N, show content up to level N."
  5905. (interactive "P")
  5906. (save-excursion
  5907. ;; Visit all headings and show their offspring
  5908. (and (integerp arg) (org-overview))
  5909. (goto-char (point-max))
  5910. (catch 'exit
  5911. (while (and (progn (condition-case nil
  5912. (outline-previous-visible-heading 1)
  5913. (error (goto-char (point-min))))
  5914. t)
  5915. (looking-at org-outline-regexp))
  5916. (if (integerp arg)
  5917. (show-children (1- arg))
  5918. (show-branches))
  5919. (if (bobp) (throw 'exit nil))))))
  5920. (defun org-optimize-window-after-visibility-change (state)
  5921. "Adjust the window after a change in outline visibility.
  5922. This function is the default value of the hook `org-cycle-hook'."
  5923. (when (get-buffer-window (current-buffer))
  5924. (cond
  5925. ((eq state 'content) nil)
  5926. ((eq state 'all) nil)
  5927. ((eq state 'folded) nil)
  5928. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5929. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5930. (defun org-remove-empty-overlays-at (pos)
  5931. "Remove outline overlays that do not contain non-white stuff."
  5932. (mapc
  5933. (lambda (o)
  5934. (and (eq 'outline (overlay-get o 'invisible))
  5935. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5936. (overlay-end o))))
  5937. (delete-overlay o)))
  5938. (overlays-at pos)))
  5939. (defun org-clean-visibility-after-subtree-move ()
  5940. "Fix visibility issues after moving a subtree."
  5941. ;; First, find a reasonable region to look at:
  5942. ;; Start two siblings above, end three below
  5943. (let* ((beg (save-excursion
  5944. (and (org-get-last-sibling)
  5945. (org-get-last-sibling))
  5946. (point)))
  5947. (end (save-excursion
  5948. (and (org-get-next-sibling)
  5949. (org-get-next-sibling)
  5950. (org-get-next-sibling))
  5951. (if (org-at-heading-p)
  5952. (point-at-eol)
  5953. (point))))
  5954. (level (looking-at "\\*+"))
  5955. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5956. (save-excursion
  5957. (save-restriction
  5958. (narrow-to-region beg end)
  5959. (when re
  5960. ;; Properly fold already folded siblings
  5961. (goto-char (point-min))
  5962. (while (re-search-forward re nil t)
  5963. (if (and (not (outline-invisible-p))
  5964. (save-excursion
  5965. (goto-char (point-at-eol)) (outline-invisible-p)))
  5966. (hide-entry))))
  5967. (org-cycle-show-empty-lines 'overview)
  5968. (org-cycle-hide-drawers 'overview)))))
  5969. (defun org-cycle-show-empty-lines (state)
  5970. "Show empty lines above all visible headlines.
  5971. The region to be covered depends on STATE when called through
  5972. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5973. entire buffer covered. Note that an empty line is only shown if there
  5974. are at least `org-cycle-separator-lines' empty lines before the headline."
  5975. (when (not (= org-cycle-separator-lines 0))
  5976. (save-excursion
  5977. (let* ((n (abs org-cycle-separator-lines))
  5978. (re (cond
  5979. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5980. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5981. (t (let ((ns (number-to-string (- n 2))))
  5982. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5983. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5984. beg end b e)
  5985. (cond
  5986. ((memq state '(overview contents t))
  5987. (setq beg (point-min) end (point-max)))
  5988. ((memq state '(children folded))
  5989. (setq beg (point) end (progn (org-end-of-subtree t t)
  5990. (beginning-of-line 2)
  5991. (point)))))
  5992. (when beg
  5993. (goto-char beg)
  5994. (while (re-search-forward re end t)
  5995. (unless (get-char-property (match-end 1) 'invisible)
  5996. (setq e (match-end 1))
  5997. (if (< org-cycle-separator-lines 0)
  5998. (setq b (save-excursion
  5999. (goto-char (match-beginning 0))
  6000. (org-back-over-empty-lines)
  6001. (if (save-excursion
  6002. (goto-char (max (point-min) (1- (point))))
  6003. (org-at-heading-p))
  6004. (1- (point))
  6005. (point))))
  6006. (setq b (match-beginning 1)))
  6007. (outline-flag-region b e nil)))))))
  6008. ;; Never hide empty lines at the end of the file.
  6009. (save-excursion
  6010. (goto-char (point-max))
  6011. (outline-previous-heading)
  6012. (outline-end-of-heading)
  6013. (if (and (looking-at "[ \t\n]+")
  6014. (= (match-end 0) (point-max)))
  6015. (outline-flag-region (point) (match-end 0) nil))))
  6016. (defun org-show-empty-lines-in-parent ()
  6017. "Move to the parent and re-show empty lines before visible headlines."
  6018. (save-excursion
  6019. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6020. (org-cycle-show-empty-lines context))))
  6021. (defun org-files-list ()
  6022. "Return `org-agenda-files' list, plus all open org-mode files.
  6023. This is useful for operations that need to scan all of a user's
  6024. open and agenda-wise Org files."
  6025. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6026. (dolist (buf (buffer-list))
  6027. (with-current-buffer buf
  6028. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6029. (let ((file (expand-file-name (buffer-file-name))))
  6030. (unless (member file files)
  6031. (push file files))))))
  6032. files))
  6033. (defsubst org-entry-beginning-position ()
  6034. "Return the beginning position of the current entry."
  6035. (save-excursion (outline-back-to-heading t) (point)))
  6036. (defsubst org-entry-end-position ()
  6037. "Return the end position of the current entry."
  6038. (save-excursion (outline-next-heading) (point)))
  6039. (defun org-cycle-hide-drawers (state)
  6040. "Re-hide all drawers after a visibility state change."
  6041. (when (and (derived-mode-p 'org-mode)
  6042. (not (memq state '(overview folded contents))))
  6043. (save-excursion
  6044. (let* ((globalp (memq state '(contents all)))
  6045. (beg (if globalp (point-min) (point)))
  6046. (end (if globalp (point-max)
  6047. (if (eq state 'children)
  6048. (save-excursion (outline-next-heading) (point))
  6049. (org-end-of-subtree t)))))
  6050. (goto-char beg)
  6051. (while (re-search-forward org-drawer-regexp end t)
  6052. (org-flag-drawer t))))))
  6053. (defun org-cycle-hide-inline-tasks (state)
  6054. "Re-hide inline task when switching to 'contents visibility state."
  6055. (when (and (eq state 'contents)
  6056. (boundp 'org-inlinetask-min-level)
  6057. org-inlinetask-min-level)
  6058. (hide-sublevels (1- org-inlinetask-min-level))))
  6059. (defun org-flag-drawer (flag)
  6060. "When FLAG is non-nil, hide the drawer we are within.
  6061. Otherwise make it visible."
  6062. (save-excursion
  6063. (beginning-of-line 1)
  6064. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6065. (let ((b (match-end 0)))
  6066. (if (re-search-forward
  6067. "^[ \t]*:END:"
  6068. (save-excursion (outline-next-heading) (point)) t)
  6069. (outline-flag-region b (point-at-eol) flag)
  6070. (error ":END: line missing at position %s" b))))))
  6071. (defun org-subtree-end-visible-p ()
  6072. "Is the end of the current subtree visible?"
  6073. (pos-visible-in-window-p
  6074. (save-excursion (org-end-of-subtree t) (point))))
  6075. (defun org-first-headline-recenter (&optional N)
  6076. "Move cursor to the first headline and recenter the headline.
  6077. Optional argument N means put the headline into the Nth line of the window."
  6078. (goto-char (point-min))
  6079. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6080. (beginning-of-line)
  6081. (recenter (prefix-numeric-value N))))
  6082. ;;; Saving and restoring visibility
  6083. (defun org-outline-overlay-data (&optional use-markers)
  6084. "Return a list of the locations of all outline overlays.
  6085. These are overlays with the `invisible' property value `outline'.
  6086. The return value is a list of cons cells, with start and stop
  6087. positions for each overlay.
  6088. If USE-MARKERS is set, return the positions as markers."
  6089. (let (beg end)
  6090. (save-excursion
  6091. (save-restriction
  6092. (widen)
  6093. (delq nil
  6094. (mapcar (lambda (o)
  6095. (when (eq (overlay-get o 'invisible) 'outline)
  6096. (setq beg (overlay-start o)
  6097. end (overlay-end o))
  6098. (and beg end (> end beg)
  6099. (if use-markers
  6100. (cons (move-marker (make-marker) beg)
  6101. (move-marker (make-marker) end))
  6102. (cons beg end)))))
  6103. (overlays-in (point-min) (point-max))))))))
  6104. (defun org-set-outline-overlay-data (data)
  6105. "Create visibility overlays for all positions in DATA.
  6106. DATA should have been made by `org-outline-overlay-data'."
  6107. (let (o)
  6108. (save-excursion
  6109. (save-restriction
  6110. (widen)
  6111. (show-all)
  6112. (mapc (lambda (c)
  6113. (outline-flag-region (car c) (cdr c) t))
  6114. data)))))
  6115. ;;; Folding of blocks
  6116. (defvar org-hide-block-overlays nil
  6117. "Overlays hiding blocks.")
  6118. (make-variable-buffer-local 'org-hide-block-overlays)
  6119. (defun org-block-map (function &optional start end)
  6120. "Call FUNCTION at the head of all source blocks in the current buffer.
  6121. Optional arguments START and END can be used to limit the range."
  6122. (let ((start (or start (point-min)))
  6123. (end (or end (point-max))))
  6124. (save-excursion
  6125. (goto-char start)
  6126. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6127. (save-excursion
  6128. (save-match-data
  6129. (goto-char (match-beginning 0))
  6130. (funcall function)))))))
  6131. (defun org-hide-block-toggle-all ()
  6132. "Toggle the visibility of all blocks in the current buffer."
  6133. (org-block-map #'org-hide-block-toggle))
  6134. (defun org-hide-block-all ()
  6135. "Fold all blocks in the current buffer."
  6136. (interactive)
  6137. (org-show-block-all)
  6138. (org-block-map #'org-hide-block-toggle-maybe))
  6139. (defun org-show-block-all ()
  6140. "Unfold all blocks in the current buffer."
  6141. (interactive)
  6142. (mapc 'delete-overlay org-hide-block-overlays)
  6143. (setq org-hide-block-overlays nil))
  6144. (defun org-hide-block-toggle-maybe ()
  6145. "Toggle visibility of block at point."
  6146. (interactive)
  6147. (let ((case-fold-search t))
  6148. (if (save-excursion
  6149. (beginning-of-line 1)
  6150. (looking-at org-block-regexp))
  6151. (progn (org-hide-block-toggle)
  6152. t) ;; to signal that we took action
  6153. nil))) ;; to signal that we did not
  6154. (defun org-hide-block-toggle (&optional force)
  6155. "Toggle the visibility of the current block."
  6156. (interactive)
  6157. (save-excursion
  6158. (beginning-of-line)
  6159. (if (re-search-forward org-block-regexp nil t)
  6160. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6161. (end (match-end 0)) ;; end of entire body
  6162. ov)
  6163. (if (memq t (mapcar (lambda (overlay)
  6164. (eq (overlay-get overlay 'invisible)
  6165. 'org-hide-block))
  6166. (overlays-at start)))
  6167. (if (or (not force) (eq force 'off))
  6168. (mapc (lambda (ov)
  6169. (when (member ov org-hide-block-overlays)
  6170. (setq org-hide-block-overlays
  6171. (delq ov org-hide-block-overlays)))
  6172. (when (eq (overlay-get ov 'invisible)
  6173. 'org-hide-block)
  6174. (delete-overlay ov)))
  6175. (overlays-at start)))
  6176. (setq ov (make-overlay start end))
  6177. (overlay-put ov 'invisible 'org-hide-block)
  6178. ;; make the block accessible to isearch
  6179. (overlay-put
  6180. ov 'isearch-open-invisible
  6181. (lambda (ov)
  6182. (when (member ov org-hide-block-overlays)
  6183. (setq org-hide-block-overlays
  6184. (delq ov org-hide-block-overlays)))
  6185. (when (eq (overlay-get ov 'invisible)
  6186. 'org-hide-block)
  6187. (delete-overlay ov))))
  6188. (push ov org-hide-block-overlays)))
  6189. (error "Not looking at a source block"))))
  6190. ;; org-tab-after-check-for-cycling-hook
  6191. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6192. ;; Remove overlays when changing major mode
  6193. (add-hook 'org-mode-hook
  6194. (lambda () (org-add-hook 'change-major-mode-hook
  6195. 'org-show-block-all 'append 'local)))
  6196. ;;; Org-goto
  6197. (defvar org-goto-window-configuration nil)
  6198. (defvar org-goto-marker nil)
  6199. (defvar org-goto-map)
  6200. (defun org-goto-map ()
  6201. "Set the keymap `org-goto'."
  6202. (setq org-goto-map
  6203. (let ((map (make-sparse-keymap)))
  6204. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6205. mouse-drag-region universal-argument org-occur))
  6206. cmd)
  6207. (while (setq cmd (pop cmds))
  6208. (substitute-key-definition cmd cmd map global-map)))
  6209. (suppress-keymap map)
  6210. (org-defkey map "\C-m" 'org-goto-ret)
  6211. (org-defkey map [(return)] 'org-goto-ret)
  6212. (org-defkey map [(left)] 'org-goto-left)
  6213. (org-defkey map [(right)] 'org-goto-right)
  6214. (org-defkey map [(control ?g)] 'org-goto-quit)
  6215. (org-defkey map "\C-i" 'org-cycle)
  6216. (org-defkey map [(tab)] 'org-cycle)
  6217. (org-defkey map [(down)] 'outline-next-visible-heading)
  6218. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6219. (if org-goto-auto-isearch
  6220. (if (fboundp 'define-key-after)
  6221. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6222. nil)
  6223. (org-defkey map "q" 'org-goto-quit)
  6224. (org-defkey map "n" 'outline-next-visible-heading)
  6225. (org-defkey map "p" 'outline-previous-visible-heading)
  6226. (org-defkey map "f" 'outline-forward-same-level)
  6227. (org-defkey map "b" 'outline-backward-same-level)
  6228. (org-defkey map "u" 'outline-up-heading))
  6229. (org-defkey map "/" 'org-occur)
  6230. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6231. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6232. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6233. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6234. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6235. map)))
  6236. (defconst org-goto-help
  6237. "Browse buffer copy, to find location or copy text.%s
  6238. RET=jump to location C-g=quit and return to previous location
  6239. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6240. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6241. ;; FIXME: Docstring does not mention both interfaces
  6242. (defun org-goto (&optional alternative-interface)
  6243. "Look up a different location in the current file, keeping current visibility.
  6244. When you want look-up or go to a different location in a
  6245. document, the fastest way is often to fold the entire buffer and
  6246. then dive into the tree. This method has the disadvantage, that
  6247. the previous location will be folded, which may not be what you
  6248. want.
  6249. This command works around this by showing a copy of the current
  6250. buffer in an indirect buffer, in overview mode. You can dive
  6251. into the tree in that copy, use org-occur and incremental search
  6252. to find a location. When pressing RET or `Q', the command
  6253. returns to the original buffer in which the visibility is still
  6254. unchanged. After RET it will also jump to the location selected
  6255. in the indirect buffer and expose the headline hierarchy above.
  6256. With a prefix argument, use the alternative interface: e.g. if
  6257. `org-goto-interface' is 'outline use 'outline-path-completion."
  6258. (interactive "P")
  6259. (org-goto-map)
  6260. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6261. (org-refile-use-outline-path t)
  6262. (org-refile-target-verify-function nil)
  6263. (interface
  6264. (if (not alternative-interface)
  6265. org-goto-interface
  6266. (if (eq org-goto-interface 'outline)
  6267. 'outline-path-completion
  6268. 'outline)))
  6269. (org-goto-start-pos (point))
  6270. (selected-point
  6271. (if (eq interface 'outline)
  6272. (car (org-get-location (current-buffer) org-goto-help))
  6273. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6274. (org-refile-check-position pa)
  6275. (nth 3 pa)))))
  6276. (if selected-point
  6277. (progn
  6278. (org-mark-ring-push org-goto-start-pos)
  6279. (goto-char selected-point)
  6280. (if (or (outline-invisible-p) (org-invisible-p2))
  6281. (org-show-context 'org-goto)))
  6282. (message "Quit"))))
  6283. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6284. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6285. (defvar org-goto-local-auto-isearch-map) ; defined below
  6286. (defun org-get-location (buf help)
  6287. "Let the user select a location in the Org-mode buffer BUF.
  6288. This function uses a recursive edit. It returns the selected position
  6289. or nil."
  6290. (org-no-popups
  6291. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6292. (isearch-hide-immediately nil)
  6293. (isearch-search-fun-function
  6294. (lambda () 'org-goto-local-search-headings))
  6295. (org-goto-selected-point org-goto-exit-command))
  6296. (save-excursion
  6297. (save-window-excursion
  6298. (delete-other-windows)
  6299. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6300. (org-pop-to-buffer-same-window
  6301. (condition-case nil
  6302. (make-indirect-buffer (current-buffer) "*org-goto*")
  6303. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6304. (with-output-to-temp-buffer "*Org Help*"
  6305. (princ (format help (if org-goto-auto-isearch
  6306. " Just type for auto-isearch."
  6307. " n/p/f/b/u to navigate, q to quit."))))
  6308. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6309. (setq buffer-read-only nil)
  6310. (let ((org-startup-truncated t)
  6311. (org-startup-folded nil)
  6312. (org-startup-align-all-tables nil))
  6313. (org-mode)
  6314. (org-overview))
  6315. (setq buffer-read-only t)
  6316. (if (and (boundp 'org-goto-start-pos)
  6317. (integer-or-marker-p org-goto-start-pos))
  6318. (let ((org-show-hierarchy-above t)
  6319. (org-show-siblings t)
  6320. (org-show-following-heading t))
  6321. (goto-char org-goto-start-pos)
  6322. (and (outline-invisible-p) (org-show-context)))
  6323. (goto-char (point-min)))
  6324. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6325. (message "Select location and press RET")
  6326. (use-local-map org-goto-map)
  6327. (recursive-edit)))
  6328. (kill-buffer "*org-goto*")
  6329. (cons org-goto-selected-point org-goto-exit-command))))
  6330. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6331. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6332. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6333. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6334. (defun org-goto-local-search-headings (string bound noerror)
  6335. "Search and make sure that any matches are in headlines."
  6336. (catch 'return
  6337. (while (if isearch-forward
  6338. (search-forward string bound noerror)
  6339. (search-backward string bound noerror))
  6340. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6341. (and (member :headline context)
  6342. (not (member :tags context))))
  6343. (throw 'return (point))))))
  6344. (defun org-goto-local-auto-isearch ()
  6345. "Start isearch."
  6346. (interactive)
  6347. (goto-char (point-min))
  6348. (let ((keys (this-command-keys)))
  6349. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6350. (isearch-mode t)
  6351. (isearch-process-search-char (string-to-char keys)))))
  6352. (defun org-goto-ret (&optional arg)
  6353. "Finish `org-goto' by going to the new location."
  6354. (interactive "P")
  6355. (setq org-goto-selected-point (point)
  6356. org-goto-exit-command 'return)
  6357. (throw 'exit nil))
  6358. (defun org-goto-left ()
  6359. "Finish `org-goto' by going to the new location."
  6360. (interactive)
  6361. (if (org-at-heading-p)
  6362. (progn
  6363. (beginning-of-line 1)
  6364. (setq org-goto-selected-point (point)
  6365. org-goto-exit-command 'left)
  6366. (throw 'exit nil))
  6367. (error "Not on a heading")))
  6368. (defun org-goto-right ()
  6369. "Finish `org-goto' by going to the new location."
  6370. (interactive)
  6371. (if (org-at-heading-p)
  6372. (progn
  6373. (setq org-goto-selected-point (point)
  6374. org-goto-exit-command 'right)
  6375. (throw 'exit nil))
  6376. (error "Not on a heading")))
  6377. (defun org-goto-quit ()
  6378. "Finish `org-goto' without cursor motion."
  6379. (interactive)
  6380. (setq org-goto-selected-point nil)
  6381. (setq org-goto-exit-command 'quit)
  6382. (throw 'exit nil))
  6383. ;;; Indirect buffer display of subtrees
  6384. (defvar org-indirect-dedicated-frame nil
  6385. "This is the frame being used for indirect tree display.")
  6386. (defvar org-last-indirect-buffer nil)
  6387. (defun org-tree-to-indirect-buffer (&optional arg)
  6388. "Create indirect buffer and narrow it to current subtree.
  6389. With a numerical prefix ARG, go up to this level and then take that tree.
  6390. If ARG is negative, go up that many levels.
  6391. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6392. indirect buffer previously made with this command, to avoid proliferation of
  6393. indirect buffers. However, when you call the command with a \
  6394. \\[universal-argument] prefix, or
  6395. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6396. is kept so that you can work with several indirect buffers at the same time.
  6397. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6398. \\[universal-argument] prefix also
  6399. requests that a new frame be made for the new buffer, so that the dedicated
  6400. frame is not changed."
  6401. (interactive "P")
  6402. (let ((cbuf (current-buffer))
  6403. (cwin (selected-window))
  6404. (pos (point))
  6405. beg end level heading ibuf)
  6406. (save-excursion
  6407. (org-back-to-heading t)
  6408. (when (numberp arg)
  6409. (setq level (org-outline-level))
  6410. (if (< arg 0) (setq arg (+ level arg)))
  6411. (while (> (setq level (org-outline-level)) arg)
  6412. (org-up-heading-safe)))
  6413. (setq beg (point)
  6414. heading (org-get-heading))
  6415. (org-end-of-subtree t t)
  6416. (if (org-at-heading-p) (backward-char 1))
  6417. (setq end (point)))
  6418. (if (and (buffer-live-p org-last-indirect-buffer)
  6419. (not (eq org-indirect-buffer-display 'new-frame))
  6420. (not arg))
  6421. (kill-buffer org-last-indirect-buffer))
  6422. (setq ibuf (org-get-indirect-buffer cbuf)
  6423. org-last-indirect-buffer ibuf)
  6424. (cond
  6425. ((or (eq org-indirect-buffer-display 'new-frame)
  6426. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6427. (select-frame (make-frame))
  6428. (delete-other-windows)
  6429. (org-pop-to-buffer-same-window ibuf)
  6430. (org-set-frame-title heading))
  6431. ((eq org-indirect-buffer-display 'dedicated-frame)
  6432. (raise-frame
  6433. (select-frame (or (and org-indirect-dedicated-frame
  6434. (frame-live-p org-indirect-dedicated-frame)
  6435. org-indirect-dedicated-frame)
  6436. (setq org-indirect-dedicated-frame (make-frame)))))
  6437. (delete-other-windows)
  6438. (org-pop-to-buffer-same-window ibuf)
  6439. (org-set-frame-title (concat "Indirect: " heading)))
  6440. ((eq org-indirect-buffer-display 'current-window)
  6441. (org-pop-to-buffer-same-window ibuf))
  6442. ((eq org-indirect-buffer-display 'other-window)
  6443. (pop-to-buffer ibuf))
  6444. (t (error "Invalid value")))
  6445. (if (featurep 'xemacs)
  6446. (save-excursion (org-mode) (turn-on-font-lock)))
  6447. (narrow-to-region beg end)
  6448. (show-all)
  6449. (goto-char pos)
  6450. (run-hook-with-args 'org-cycle-hook 'all)
  6451. (and (window-live-p cwin) (select-window cwin))))
  6452. (defun org-get-indirect-buffer (&optional buffer)
  6453. (setq buffer (or buffer (current-buffer)))
  6454. (let ((n 1) (base (buffer-name buffer)) bname)
  6455. (while (buffer-live-p
  6456. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6457. (setq n (1+ n)))
  6458. (condition-case nil
  6459. (make-indirect-buffer buffer bname 'clone)
  6460. (error (make-indirect-buffer buffer bname)))))
  6461. (defun org-set-frame-title (title)
  6462. "Set the title of the current frame to the string TITLE."
  6463. ;; FIXME: how to name a single frame in XEmacs???
  6464. (unless (featurep 'xemacs)
  6465. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6466. ;;;; Structure editing
  6467. ;;; Inserting headlines
  6468. (defun org-previous-line-empty-p ()
  6469. (save-excursion
  6470. (and (not (bobp))
  6471. (or (beginning-of-line 0) t)
  6472. (save-match-data
  6473. (looking-at "[ \t]*$")))))
  6474. (defun org-insert-heading (&optional force-heading invisible-ok)
  6475. "Insert a new heading or item with same depth at point.
  6476. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6477. If point is at the beginning of a headline, insert a sibling before the
  6478. current headline. If point is not at the beginning, split the line,
  6479. create the new headline with the text in the current line after point
  6480. \(but see also the variable `org-M-RET-may-split-line').
  6481. With a double prefix arg, force the heading to be inserted at the
  6482. end of the parent subtree.
  6483. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6484. This is important for non-interactive uses of the command."
  6485. (interactive "P")
  6486. (if (or (= (buffer-size) 0)
  6487. (and (not (save-excursion
  6488. (and (ignore-errors (org-back-to-heading invisible-ok))
  6489. (org-at-heading-p))))
  6490. (or force-heading (not (org-in-item-p)))))
  6491. (progn
  6492. (insert "\n* ")
  6493. (run-hooks 'org-insert-heading-hook))
  6494. (when (or force-heading (not (org-insert-item)))
  6495. (let* ((empty-line-p nil)
  6496. (level nil)
  6497. (on-heading (org-at-heading-p))
  6498. (head (save-excursion
  6499. (condition-case nil
  6500. (progn
  6501. (org-back-to-heading invisible-ok)
  6502. (when (and (not on-heading)
  6503. (featurep 'org-inlinetask)
  6504. (integerp org-inlinetask-min-level)
  6505. (>= (length (match-string 0))
  6506. org-inlinetask-min-level))
  6507. ;; Find a heading level before the inline task
  6508. (while (and (setq level (org-up-heading-safe))
  6509. (>= level org-inlinetask-min-level)))
  6510. (if (org-at-heading-p)
  6511. (org-back-to-heading invisible-ok)
  6512. (error "This should not happen")))
  6513. (setq empty-line-p (org-previous-line-empty-p))
  6514. (match-string 0))
  6515. (error "*"))))
  6516. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6517. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6518. pos hide-previous previous-pos)
  6519. (cond
  6520. ((and (org-at-heading-p) (bolp)
  6521. (or (bobp)
  6522. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6523. ;; insert before the current line
  6524. (open-line (if blank 2 1)))
  6525. ((and (bolp)
  6526. (not org-insert-heading-respect-content)
  6527. (or (bobp)
  6528. (save-excursion
  6529. (backward-char 1) (not (outline-invisible-p)))))
  6530. ;; insert right here
  6531. nil)
  6532. (t
  6533. ;; somewhere in the line
  6534. (save-excursion
  6535. (setq previous-pos (point-at-bol))
  6536. (end-of-line)
  6537. (setq hide-previous (outline-invisible-p)))
  6538. (and org-insert-heading-respect-content (org-show-subtree))
  6539. (let ((split
  6540. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6541. (save-excursion
  6542. (let ((p (point)))
  6543. (goto-char (point-at-bol))
  6544. (and (looking-at org-complex-heading-regexp)
  6545. (match-beginning 4)
  6546. (> p (match-beginning 4)))))))
  6547. tags pos)
  6548. (cond
  6549. (org-insert-heading-respect-content
  6550. (if (not (equal force-heading '(16)))
  6551. (org-end-of-subtree nil t)
  6552. (org-up-heading-safe)
  6553. (org-end-of-subtree nil t))
  6554. (when (featurep 'org-inlinetask)
  6555. (while (and (not (eobp))
  6556. (looking-at "\\(\\*+\\)[ \t]+")
  6557. (>= (length (match-string 1))
  6558. org-inlinetask-min-level))
  6559. (org-end-of-subtree nil t)))
  6560. (or (bolp) (newline))
  6561. (or (org-previous-line-empty-p)
  6562. (and blank (newline)))
  6563. (open-line 1))
  6564. ((org-at-heading-p)
  6565. (when hide-previous
  6566. (show-children)
  6567. (org-show-entry))
  6568. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6569. (setq tags (and (match-end 2) (match-string 2)))
  6570. (and (match-end 1)
  6571. (delete-region (match-beginning 1) (match-end 1)))
  6572. (setq pos (point-at-bol))
  6573. (or split (end-of-line 1))
  6574. (delete-horizontal-space)
  6575. (if (string-match "\\`\\*+\\'"
  6576. (buffer-substring (point-at-bol) (point)))
  6577. (insert " "))
  6578. (newline (if blank 2 1))
  6579. (when tags
  6580. (save-excursion
  6581. (goto-char pos)
  6582. (end-of-line 1)
  6583. (insert " " tags)
  6584. (org-set-tags nil 'align))))
  6585. (t
  6586. (or split (end-of-line 1))
  6587. (newline (if blank 2 1)))))))
  6588. (insert head) (just-one-space)
  6589. (setq pos (point))
  6590. (end-of-line 1)
  6591. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6592. (when (and org-insert-heading-respect-content hide-previous)
  6593. (save-excursion
  6594. (goto-char previous-pos)
  6595. (hide-subtree)))
  6596. (run-hooks 'org-insert-heading-hook)))))
  6597. (defun org-get-heading (&optional no-tags no-todo)
  6598. "Return the heading of the current entry, without the stars.
  6599. When NO-TAGS is non-nil, don't include tags.
  6600. When NO-TODO is non-nil, don't include TODO keywords."
  6601. (save-excursion
  6602. (org-back-to-heading t)
  6603. (cond
  6604. ((and no-tags no-todo)
  6605. (looking-at org-complex-heading-regexp)
  6606. (match-string 4))
  6607. (no-tags
  6608. (looking-at (concat org-outline-regexp
  6609. "\\(.*?\\)"
  6610. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6611. (match-string 1))
  6612. (no-todo
  6613. (looking-at org-todo-line-regexp)
  6614. (match-string 3))
  6615. (t (looking-at org-heading-regexp)
  6616. (match-string 2)))))
  6617. (defun org-heading-components ()
  6618. "Return the components of the current heading.
  6619. This is a list with the following elements:
  6620. - the level as an integer
  6621. - the reduced level, different if `org-odd-levels-only' is set.
  6622. - the TODO keyword, or nil
  6623. - the priority character, like ?A, or nil if no priority is given
  6624. - the headline text itself, or the tags string if no headline text
  6625. - the tags string, or nil."
  6626. (save-excursion
  6627. (org-back-to-heading t)
  6628. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6629. (list (length (match-string 1))
  6630. (org-reduced-level (length (match-string 1)))
  6631. (org-match-string-no-properties 2)
  6632. (and (match-end 3) (aref (match-string 3) 2))
  6633. (org-match-string-no-properties 4)
  6634. (org-match-string-no-properties 5)))))
  6635. (defun org-get-entry ()
  6636. "Get the entry text, after heading, entire subtree."
  6637. (save-excursion
  6638. (org-back-to-heading t)
  6639. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6640. (defun org-insert-heading-after-current ()
  6641. "Insert a new heading with same level as current, after current subtree."
  6642. (interactive)
  6643. (org-back-to-heading)
  6644. (org-insert-heading)
  6645. (org-move-subtree-down)
  6646. (end-of-line 1))
  6647. (defun org-insert-heading-respect-content ()
  6648. (interactive)
  6649. (let ((org-insert-heading-respect-content t))
  6650. (org-insert-heading t)))
  6651. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6652. (interactive "P")
  6653. (let ((org-insert-heading-respect-content t))
  6654. (org-insert-todo-heading force-state t)))
  6655. (defun org-insert-todo-heading (arg &optional force-heading)
  6656. "Insert a new heading with the same level and TODO state as current heading.
  6657. If the heading has no TODO state, or if the state is DONE, use the first
  6658. state (TODO by default). Also one prefix arg, force first state. With two
  6659. prefix args, force inserting at the end of the parent subtree."
  6660. (interactive "P")
  6661. (when (or force-heading (not (org-insert-item 'checkbox)))
  6662. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6663. force-heading))
  6664. (save-excursion
  6665. (org-back-to-heading)
  6666. (outline-previous-heading)
  6667. (looking-at org-todo-line-regexp))
  6668. (let*
  6669. ((new-mark-x
  6670. (if (or arg
  6671. (not (match-beginning 2))
  6672. (member (match-string 2) org-done-keywords))
  6673. (car org-todo-keywords-1)
  6674. (match-string 2)))
  6675. (new-mark
  6676. (or
  6677. (run-hook-with-args-until-success
  6678. 'org-todo-get-default-hook new-mark-x nil)
  6679. new-mark-x)))
  6680. (beginning-of-line 1)
  6681. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6682. (if org-treat-insert-todo-heading-as-state-change
  6683. (org-todo new-mark)
  6684. (insert new-mark " "))))
  6685. (when org-provide-todo-statistics
  6686. (org-update-parent-todo-statistics))))
  6687. (defun org-insert-subheading (arg)
  6688. "Insert a new subheading and demote it.
  6689. Works for outline headings and for plain lists alike."
  6690. (interactive "P")
  6691. (org-insert-heading arg)
  6692. (cond
  6693. ((org-at-heading-p) (org-do-demote))
  6694. ((org-at-item-p) (org-indent-item))))
  6695. (defun org-insert-todo-subheading (arg)
  6696. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6697. Works for outline headings and for plain lists alike."
  6698. (interactive "P")
  6699. (org-insert-todo-heading arg)
  6700. (cond
  6701. ((org-at-heading-p) (org-do-demote))
  6702. ((org-at-item-p) (org-indent-item))))
  6703. ;;; Promotion and Demotion
  6704. (defvar org-after-demote-entry-hook nil
  6705. "Hook run after an entry has been demoted.
  6706. The cursor will be at the beginning of the entry.
  6707. When a subtree is being demoted, the hook will be called for each node.")
  6708. (defvar org-after-promote-entry-hook nil
  6709. "Hook run after an entry has been promoted.
  6710. The cursor will be at the beginning of the entry.
  6711. When a subtree is being promoted, the hook will be called for each node.")
  6712. (defun org-promote-subtree ()
  6713. "Promote the entire subtree.
  6714. See also `org-promote'."
  6715. (interactive)
  6716. (save-excursion
  6717. (org-with-limited-levels (org-map-tree 'org-promote)))
  6718. (org-fix-position-after-promote))
  6719. (defun org-demote-subtree ()
  6720. "Demote the entire subtree. See `org-demote'.
  6721. See also `org-promote'."
  6722. (interactive)
  6723. (save-excursion
  6724. (org-with-limited-levels (org-map-tree 'org-demote)))
  6725. (org-fix-position-after-promote))
  6726. (defun org-do-promote ()
  6727. "Promote the current heading higher up the tree.
  6728. If the region is active in `transient-mark-mode', promote all headings
  6729. in the region."
  6730. (interactive)
  6731. (save-excursion
  6732. (if (org-region-active-p)
  6733. (org-map-region 'org-promote (region-beginning) (region-end))
  6734. (org-promote)))
  6735. (org-fix-position-after-promote))
  6736. (defun org-do-demote ()
  6737. "Demote the current heading lower down the tree.
  6738. If the region is active in `transient-mark-mode', demote all headings
  6739. in the region."
  6740. (interactive)
  6741. (save-excursion
  6742. (if (org-region-active-p)
  6743. (org-map-region 'org-demote (region-beginning) (region-end))
  6744. (org-demote)))
  6745. (org-fix-position-after-promote))
  6746. (defun org-fix-position-after-promote ()
  6747. "Make sure that after pro/demotion cursor position is right."
  6748. (let ((pos (point)))
  6749. (when (save-excursion
  6750. (beginning-of-line 1)
  6751. (looking-at org-todo-line-regexp)
  6752. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6753. (cond ((eobp) (insert " "))
  6754. ((eolp) (insert " "))
  6755. ((equal (char-after) ?\ ) (forward-char 1))))))
  6756. (defun org-current-level ()
  6757. "Return the level of the current entry, or nil if before the first headline.
  6758. The level is the number of stars at the beginning of the headline."
  6759. (save-excursion
  6760. (org-with-limited-levels
  6761. (if (ignore-errors (org-back-to-heading t))
  6762. (funcall outline-level)))))
  6763. (defun org-get-previous-line-level ()
  6764. "Return the outline depth of the last headline before the current line.
  6765. Returns 0 for the first headline in the buffer, and nil if before the
  6766. first headline."
  6767. (let ((current-level (org-current-level))
  6768. (prev-level (when (> (line-number-at-pos) 1)
  6769. (save-excursion
  6770. (beginning-of-line 0)
  6771. (org-current-level)))))
  6772. (cond ((null current-level) nil) ; Before first headline
  6773. ((null prev-level) 0) ; At first headline
  6774. (prev-level))))
  6775. (defun org-reduced-level (l)
  6776. "Compute the effective level of a heading.
  6777. This takes into account the setting of `org-odd-levels-only'."
  6778. (cond
  6779. ((zerop l) 0)
  6780. (org-odd-levels-only (1+ (floor (/ l 2))))
  6781. (t l)))
  6782. (defun org-level-increment ()
  6783. "Return the number of stars that will be added or removed at a
  6784. time to headlines when structure editing, based on the value of
  6785. `org-odd-levels-only'."
  6786. (if org-odd-levels-only 2 1))
  6787. (defun org-get-valid-level (level &optional change)
  6788. "Rectify a level change under the influence of `org-odd-levels-only'
  6789. LEVEL is a current level, CHANGE is by how much the level should be
  6790. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6791. even level numbers will become the next higher odd number."
  6792. (if org-odd-levels-only
  6793. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6794. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6795. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6796. (max 1 (+ level (or change 0)))))
  6797. (if (boundp 'define-obsolete-function-alias)
  6798. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6799. (define-obsolete-function-alias 'org-get-legal-level
  6800. 'org-get-valid-level)
  6801. (define-obsolete-function-alias 'org-get-legal-level
  6802. 'org-get-valid-level "23.1")))
  6803. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6804. ;; ̀org-with-limited-levels'
  6805. (defun org-promote ()
  6806. "Promote the current heading higher up the tree.
  6807. If the region is active in `transient-mark-mode', promote all headings
  6808. in the region."
  6809. (org-back-to-heading t)
  6810. (let* ((level (save-match-data (funcall outline-level)))
  6811. (after-change-functions (remove 'flyspell-after-change-function
  6812. after-change-functions))
  6813. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6814. (diff (abs (- level (length up-head) -1))))
  6815. (cond ((and (= level 1) org-called-with-limited-levels
  6816. org-allow-promoting-top-level-subtree)
  6817. (replace-match "# " nil t))
  6818. ((= level 1)
  6819. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6820. (t (replace-match up-head nil t)))
  6821. ;; Fixup tag positioning
  6822. (unless (= level 1)
  6823. (and org-auto-align-tags (org-set-tags nil t))
  6824. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6825. (run-hooks 'org-after-promote-entry-hook)))
  6826. (defun org-demote ()
  6827. "Demote the current heading lower down the tree.
  6828. If the region is active in `transient-mark-mode', demote all headings
  6829. in the region."
  6830. (org-back-to-heading t)
  6831. (let* ((level (save-match-data (funcall outline-level)))
  6832. (after-change-functions (remove 'flyspell-after-change-function
  6833. after-change-functions))
  6834. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6835. (diff (abs (- level (length down-head) -1))))
  6836. (replace-match down-head nil t)
  6837. ;; Fixup tag positioning
  6838. (and org-auto-align-tags (org-set-tags nil t))
  6839. (if org-adapt-indentation (org-fixup-indentation diff))
  6840. (run-hooks 'org-after-demote-entry-hook)))
  6841. (defun org-cycle-level ()
  6842. "Cycle the level of an empty headline through possible states.
  6843. This goes first to child, then to parent, level, then up the hierarchy.
  6844. After top level, it switches back to sibling level."
  6845. (interactive)
  6846. (let ((org-adapt-indentation nil))
  6847. (when (org-point-at-end-of-empty-headline)
  6848. (setq this-command 'org-cycle-level) ; Only needed for caching
  6849. (let ((cur-level (org-current-level))
  6850. (prev-level (org-get-previous-line-level)))
  6851. (cond
  6852. ;; If first headline in file, promote to top-level.
  6853. ((= prev-level 0)
  6854. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6855. do (org-do-promote)))
  6856. ;; If same level as prev, demote one.
  6857. ((= prev-level cur-level)
  6858. (org-do-demote))
  6859. ;; If parent is top-level, promote to top level if not already.
  6860. ((= prev-level 1)
  6861. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6862. do (org-do-promote)))
  6863. ;; If top-level, return to prev-level.
  6864. ((= cur-level 1)
  6865. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6866. do (org-do-demote)))
  6867. ;; If less than prev-level, promote one.
  6868. ((< cur-level prev-level)
  6869. (org-do-promote))
  6870. ;; If deeper than prev-level, promote until higher than
  6871. ;; prev-level.
  6872. ((> cur-level prev-level)
  6873. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6874. do (org-do-promote))))
  6875. t))))
  6876. (defun org-map-tree (fun)
  6877. "Call FUN for every heading underneath the current one."
  6878. (org-back-to-heading)
  6879. (let ((level (funcall outline-level)))
  6880. (save-excursion
  6881. (funcall fun)
  6882. (while (and (progn
  6883. (outline-next-heading)
  6884. (> (funcall outline-level) level))
  6885. (not (eobp)))
  6886. (funcall fun)))))
  6887. (defun org-map-region (fun beg end)
  6888. "Call FUN for every heading between BEG and END."
  6889. (let ((org-ignore-region t))
  6890. (save-excursion
  6891. (setq end (copy-marker end))
  6892. (goto-char beg)
  6893. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6894. (< (point) end))
  6895. (funcall fun))
  6896. (while (and (progn
  6897. (outline-next-heading)
  6898. (< (point) end))
  6899. (not (eobp)))
  6900. (funcall fun)))))
  6901. (defvar org-property-end-re) ; silence byte-compiler
  6902. (defun org-fixup-indentation (diff)
  6903. "Change the indentation in the current entry by DIFF.
  6904. However, if any line in the current entry has no indentation, or if it
  6905. would end up with no indentation after the change, nothing at all is done."
  6906. (save-excursion
  6907. (let ((end (save-excursion (outline-next-heading)
  6908. (point-marker)))
  6909. (prohibit (if (> diff 0)
  6910. "^\\S-"
  6911. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6912. col)
  6913. (unless (save-excursion (end-of-line 1)
  6914. (re-search-forward prohibit end t))
  6915. (while (and (< (point) end)
  6916. (re-search-forward "^[ \t]+" end t))
  6917. (goto-char (match-end 0))
  6918. (setq col (current-column))
  6919. (if (< diff 0) (replace-match ""))
  6920. (org-indent-to-column (+ diff col))))
  6921. (move-marker end nil))))
  6922. (defun org-convert-to-odd-levels ()
  6923. "Convert an org-mode file with all levels allowed to one with odd levels.
  6924. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6925. level 5 etc."
  6926. (interactive)
  6927. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6928. (let ((outline-level 'org-outline-level)
  6929. (org-odd-levels-only nil) n)
  6930. (save-excursion
  6931. (goto-char (point-min))
  6932. (while (re-search-forward "^\\*\\*+ " nil t)
  6933. (setq n (- (length (match-string 0)) 2))
  6934. (while (>= (setq n (1- n)) 0)
  6935. (org-demote))
  6936. (end-of-line 1))))))
  6937. (defun org-convert-to-oddeven-levels ()
  6938. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6939. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6940. file contains a section with an even level, conversion would
  6941. destroy the structure of the file. An error is signaled in this
  6942. case."
  6943. (interactive)
  6944. (goto-char (point-min))
  6945. ;; First check if there are no even levels
  6946. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6947. (org-show-context t)
  6948. (error "Not all levels are odd in this file. Conversion not possible"))
  6949. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6950. (let ((outline-regexp org-outline-regexp)
  6951. (outline-level 'org-outline-level)
  6952. (org-odd-levels-only nil) n)
  6953. (save-excursion
  6954. (goto-char (point-min))
  6955. (while (re-search-forward "^\\*\\*+ " nil t)
  6956. (setq n (/ (1- (length (match-string 0))) 2))
  6957. (while (>= (setq n (1- n)) 0)
  6958. (org-promote))
  6959. (end-of-line 1))))))
  6960. (defun org-tr-level (n)
  6961. "Make N odd if required."
  6962. (if org-odd-levels-only (1+ (/ n 2)) n))
  6963. ;;; Vertical tree motion, cutting and pasting of subtrees
  6964. (defun org-move-subtree-up (&optional arg)
  6965. "Move the current subtree up past ARG headlines of the same level."
  6966. (interactive "p")
  6967. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6968. (defun org-move-subtree-down (&optional arg)
  6969. "Move the current subtree down past ARG headlines of the same level."
  6970. (interactive "p")
  6971. (setq arg (prefix-numeric-value arg))
  6972. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6973. 'org-get-last-sibling))
  6974. (ins-point (make-marker))
  6975. (cnt (abs arg))
  6976. (col (current-column))
  6977. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6978. ;; Select the tree
  6979. (org-back-to-heading)
  6980. (setq beg0 (point))
  6981. (save-excursion
  6982. (setq ne-beg (org-back-over-empty-lines))
  6983. (setq beg (point)))
  6984. (save-match-data
  6985. (save-excursion (outline-end-of-heading)
  6986. (setq folded (outline-invisible-p)))
  6987. (outline-end-of-subtree))
  6988. (outline-next-heading)
  6989. (setq ne-end (org-back-over-empty-lines))
  6990. (setq end (point))
  6991. (goto-char beg0)
  6992. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6993. ;; include less whitespace
  6994. (save-excursion
  6995. (goto-char beg)
  6996. (forward-line (- ne-beg ne-end))
  6997. (setq beg (point))))
  6998. ;; Find insertion point, with error handling
  6999. (while (> cnt 0)
  7000. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7001. (progn (goto-char beg0)
  7002. (error "Cannot move past superior level or buffer limit")))
  7003. (setq cnt (1- cnt)))
  7004. (if (> arg 0)
  7005. ;; Moving forward - still need to move over subtree
  7006. (progn (org-end-of-subtree t t)
  7007. (save-excursion
  7008. (org-back-over-empty-lines)
  7009. (or (bolp) (newline)))))
  7010. (setq ne-ins (org-back-over-empty-lines))
  7011. (move-marker ins-point (point))
  7012. (setq txt (buffer-substring beg end))
  7013. (org-save-markers-in-region beg end)
  7014. (delete-region beg end)
  7015. (org-remove-empty-overlays-at beg)
  7016. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7017. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7018. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7019. (let ((bbb (point)))
  7020. (insert-before-markers txt)
  7021. (org-reinstall-markers-in-region bbb)
  7022. (move-marker ins-point bbb))
  7023. (or (bolp) (insert "\n"))
  7024. (setq ins-end (point))
  7025. (goto-char ins-point)
  7026. (org-skip-whitespace)
  7027. (when (and (< arg 0)
  7028. (org-first-sibling-p)
  7029. (> ne-ins ne-beg))
  7030. ;; Move whitespace back to beginning
  7031. (save-excursion
  7032. (goto-char ins-end)
  7033. (let ((kill-whole-line t))
  7034. (kill-line (- ne-ins ne-beg)) (point)))
  7035. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7036. (move-marker ins-point nil)
  7037. (if folded
  7038. (hide-subtree)
  7039. (org-show-entry)
  7040. (show-children)
  7041. (org-cycle-hide-drawers 'children))
  7042. (org-clean-visibility-after-subtree-move)
  7043. ;; move back to the initial column we were at
  7044. (move-to-column col)))
  7045. (defvar org-subtree-clip ""
  7046. "Clipboard for cut and paste of subtrees.
  7047. This is actually only a copy of the kill, because we use the normal kill
  7048. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7049. (defvar org-subtree-clip-folded nil
  7050. "Was the last copied subtree folded?
  7051. This is used to fold the tree back after pasting.")
  7052. (defun org-cut-subtree (&optional n)
  7053. "Cut the current subtree into the clipboard.
  7054. With prefix arg N, cut this many sequential subtrees.
  7055. This is a short-hand for marking the subtree and then cutting it."
  7056. (interactive "p")
  7057. (org-copy-subtree n 'cut))
  7058. (defun org-copy-subtree (&optional n cut force-store-markers)
  7059. "Cut the current subtree into the clipboard.
  7060. With prefix arg N, cut this many sequential subtrees.
  7061. This is a short-hand for marking the subtree and then copying it.
  7062. If CUT is non-nil, actually cut the subtree.
  7063. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7064. of some markers in the region, even if CUT is non-nil. This is
  7065. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7066. (interactive "p")
  7067. (let (beg end folded (beg0 (point)))
  7068. (if (org-called-interactively-p 'any)
  7069. (org-back-to-heading nil) ; take what looks like a subtree
  7070. (org-back-to-heading t)) ; take what is really there
  7071. (setq beg (point))
  7072. (skip-chars-forward " \t\r\n")
  7073. (save-match-data
  7074. (save-excursion (outline-end-of-heading)
  7075. (setq folded (outline-invisible-p)))
  7076. (condition-case nil
  7077. (org-forward-heading-same-level (1- n) t)
  7078. (error nil))
  7079. (org-end-of-subtree t t))
  7080. (setq end (point))
  7081. (goto-char beg0)
  7082. (when (> end beg)
  7083. (setq org-subtree-clip-folded folded)
  7084. (when (or cut force-store-markers)
  7085. (org-save-markers-in-region beg end))
  7086. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7087. (setq org-subtree-clip (current-kill 0))
  7088. (message "%s: Subtree(s) with %d characters"
  7089. (if cut "Cut" "Copied")
  7090. (length org-subtree-clip)))))
  7091. (defun org-paste-subtree (&optional level tree for-yank)
  7092. "Paste the clipboard as a subtree, with modification of headline level.
  7093. The entire subtree is promoted or demoted in order to match a new headline
  7094. level.
  7095. If the cursor is at the beginning of a headline, the same level as
  7096. that headline is used to paste the tree
  7097. If not, the new level is derived from the *visible* headings
  7098. before and after the insertion point, and taken to be the inferior headline
  7099. level of the two. So if the previous visible heading is level 3 and the
  7100. next is level 4 (or vice versa), level 4 will be used for insertion.
  7101. This makes sure that the subtree remains an independent subtree and does
  7102. not swallow low level entries.
  7103. You can also force a different level, either by using a numeric prefix
  7104. argument, or by inserting the heading marker by hand. For example, if the
  7105. cursor is after \"*****\", then the tree will be shifted to level 5.
  7106. If optional TREE is given, use this text instead of the kill ring.
  7107. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7108. move back over whitespace before inserting, and move point to the end of
  7109. the inserted text when done."
  7110. (interactive "P")
  7111. (setq tree (or tree (and kill-ring (current-kill 0))))
  7112. (unless (org-kill-is-subtree-p tree)
  7113. (error "%s"
  7114. (substitute-command-keys
  7115. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7116. (org-with-limited-levels
  7117. (let* ((visp (not (outline-invisible-p)))
  7118. (txt tree)
  7119. (^re_ "\\(\\*+\\)[ \t]*")
  7120. (old-level (if (string-match org-outline-regexp-bol txt)
  7121. (- (match-end 0) (match-beginning 0) 1)
  7122. -1))
  7123. (force-level (cond (level (prefix-numeric-value level))
  7124. ((and (looking-at "[ \t]*$")
  7125. (string-match
  7126. "^\\*+$" (buffer-substring
  7127. (point-at-bol) (point))))
  7128. (- (match-end 1) (match-beginning 1)))
  7129. ((and (bolp)
  7130. (looking-at org-outline-regexp))
  7131. (- (match-end 0) (point) 1))))
  7132. (previous-level (save-excursion
  7133. (condition-case nil
  7134. (progn
  7135. (outline-previous-visible-heading 1)
  7136. (if (looking-at ^re_)
  7137. (- (match-end 0) (match-beginning 0) 1)
  7138. 1))
  7139. (error 1))))
  7140. (next-level (save-excursion
  7141. (condition-case nil
  7142. (progn
  7143. (or (looking-at org-outline-regexp)
  7144. (outline-next-visible-heading 1))
  7145. (if (looking-at ^re_)
  7146. (- (match-end 0) (match-beginning 0) 1)
  7147. 1))
  7148. (error 1))))
  7149. (new-level (or force-level (max previous-level next-level)))
  7150. (shift (if (or (= old-level -1)
  7151. (= new-level -1)
  7152. (= old-level new-level))
  7153. 0
  7154. (- new-level old-level)))
  7155. (delta (if (> shift 0) -1 1))
  7156. (func (if (> shift 0) 'org-demote 'org-promote))
  7157. (org-odd-levels-only nil)
  7158. beg end newend)
  7159. ;; Remove the forced level indicator
  7160. (if force-level
  7161. (delete-region (point-at-bol) (point)))
  7162. ;; Paste
  7163. (beginning-of-line (if (bolp) 1 2))
  7164. (setq beg (point))
  7165. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7166. (insert-before-markers txt)
  7167. (unless (string-match "\n\\'" txt) (insert "\n"))
  7168. (setq newend (point))
  7169. (org-reinstall-markers-in-region beg)
  7170. (setq end (point))
  7171. (goto-char beg)
  7172. (skip-chars-forward " \t\n\r")
  7173. (setq beg (point))
  7174. (if (and (outline-invisible-p) visp)
  7175. (save-excursion (outline-show-heading)))
  7176. ;; Shift if necessary
  7177. (unless (= shift 0)
  7178. (save-restriction
  7179. (narrow-to-region beg end)
  7180. (while (not (= shift 0))
  7181. (org-map-region func (point-min) (point-max))
  7182. (setq shift (+ delta shift)))
  7183. (goto-char (point-min))
  7184. (setq newend (point-max))))
  7185. (when (or (org-called-interactively-p 'interactive) for-yank)
  7186. (message "Clipboard pasted as level %d subtree" new-level))
  7187. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7188. kill-ring
  7189. (eq org-subtree-clip (current-kill 0))
  7190. org-subtree-clip-folded)
  7191. ;; The tree was folded before it was killed/copied
  7192. (hide-subtree))
  7193. (and for-yank (goto-char newend)))))
  7194. (defun org-kill-is-subtree-p (&optional txt)
  7195. "Check if the current kill is an outline subtree, or a set of trees.
  7196. Returns nil if kill does not start with a headline, or if the first
  7197. headline level is not the largest headline level in the tree.
  7198. So this will actually accept several entries of equal levels as well,
  7199. which is OK for `org-paste-subtree'.
  7200. If optional TXT is given, check this string instead of the current kill."
  7201. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7202. (re (org-get-limited-outline-regexp))
  7203. (^re (concat "^" re))
  7204. (start-level (and kill
  7205. (string-match
  7206. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7207. kill)
  7208. (- (match-end 2) (match-beginning 2) 1)))
  7209. (start (1+ (or (match-beginning 2) -1))))
  7210. (if (not start-level)
  7211. (progn
  7212. nil) ;; does not even start with a heading
  7213. (catch 'exit
  7214. (while (setq start (string-match ^re kill (1+ start)))
  7215. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7216. (throw 'exit nil)))
  7217. t))))
  7218. (defvar org-markers-to-move nil
  7219. "Markers that should be moved with a cut-and-paste operation.
  7220. Those markers are stored together with their positions relative to
  7221. the start of the region.")
  7222. (defun org-save-markers-in-region (beg end)
  7223. "Check markers in region.
  7224. If these markers are between BEG and END, record their position relative
  7225. to BEG, so that after moving the block of text, we can put the markers back
  7226. into place.
  7227. This function gets called just before an entry or tree gets cut from the
  7228. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7229. called immediately, to move the markers with the entries."
  7230. (setq org-markers-to-move nil)
  7231. (when (featurep 'org-clock)
  7232. (org-clock-save-markers-for-cut-and-paste beg end))
  7233. (when (featurep 'org-agenda)
  7234. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7235. (defun org-check-and-save-marker (marker beg end)
  7236. "Check if MARKER is between BEG and END.
  7237. If yes, remember the marker and the distance to BEG."
  7238. (when (and (marker-buffer marker)
  7239. (equal (marker-buffer marker) (current-buffer)))
  7240. (if (and (>= marker beg) (< marker end))
  7241. (push (cons marker (- marker beg)) org-markers-to-move))))
  7242. (defun org-reinstall-markers-in-region (beg)
  7243. "Move all remembered markers to their position relative to BEG."
  7244. (mapc (lambda (x)
  7245. (move-marker (car x) (+ beg (cdr x))))
  7246. org-markers-to-move)
  7247. (setq org-markers-to-move nil))
  7248. (defun org-narrow-to-subtree ()
  7249. "Narrow buffer to the current subtree."
  7250. (interactive)
  7251. (save-excursion
  7252. (save-match-data
  7253. (org-with-limited-levels
  7254. (narrow-to-region
  7255. (progn (org-back-to-heading t) (point))
  7256. (progn (org-end-of-subtree t t)
  7257. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7258. (point)))))))
  7259. (defun org-narrow-to-block ()
  7260. "Narrow buffer to the current block."
  7261. (interactive)
  7262. (let* ((case-fold-search t)
  7263. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7264. "^[ \t]*#\\+end_.*")))
  7265. (if blockp
  7266. (narrow-to-region (car blockp) (cdr blockp))
  7267. (error "Not in a block"))))
  7268. (eval-when-compile
  7269. (defvar org-property-drawer-re))
  7270. (defvar org-property-start-re) ;; defined below
  7271. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7272. "Clone the task (subtree) at point N times.
  7273. The clones will be inserted as siblings.
  7274. In interactive use, the user will be prompted for the number of
  7275. clones to be produced, and for a time SHIFT, which may be a
  7276. repeater as used in time stamps, for example `+3d'.
  7277. When a valid repeater is given and the entry contains any time
  7278. stamps, the clones will become a sequence in time, with time
  7279. stamps in the subtree shifted for each clone produced. If SHIFT
  7280. is nil or the empty string, time stamps will be left alone. The
  7281. ID property of the original subtree is removed.
  7282. If the original subtree did contain time stamps with a repeater,
  7283. the following will happen:
  7284. - the repeater will be removed in each clone
  7285. - an additional clone will be produced, with the current, unshifted
  7286. date(s) in the entry.
  7287. - the original entry will be placed *after* all the clones, with
  7288. repeater intact.
  7289. - the start days in the repeater in the original entry will be shifted
  7290. to past the last clone.
  7291. In this way you can spell out a number of instances of a repeating task,
  7292. and still retain the repeater to cover future instances of the task."
  7293. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7294. (let (beg end template task idprop
  7295. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7296. (drawer-re org-drawer-regexp))
  7297. (if (not (and (integerp n) (> n 0)))
  7298. (error "Invalid number of replications %s" n))
  7299. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7300. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7301. shift)))
  7302. (error "Invalid shift specification %s" shift))
  7303. (when doshift
  7304. (setq shift-n (string-to-number (match-string 1 shift))
  7305. shift-what (cdr (assoc (match-string 2 shift)
  7306. '(("d" . day) ("w" . week)
  7307. ("m" . month) ("y" . year))))))
  7308. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7309. (setq nmin 1 nmax n)
  7310. (org-back-to-heading t)
  7311. (setq beg (point))
  7312. (setq idprop (org-entry-get nil "ID"))
  7313. (org-end-of-subtree t t)
  7314. (or (bolp) (insert "\n"))
  7315. (setq end (point))
  7316. (setq template (buffer-substring beg end))
  7317. (when (and doshift
  7318. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7319. (delete-region beg end)
  7320. (setq end beg)
  7321. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7322. (goto-char end)
  7323. (loop for n from nmin to nmax do
  7324. ;; prepare clone
  7325. (with-temp-buffer
  7326. (insert template)
  7327. (org-mode)
  7328. (goto-char (point-min))
  7329. (org-show-subtree)
  7330. (and idprop (if org-clone-delete-id
  7331. (org-entry-delete nil "ID")
  7332. (org-id-get-create t)))
  7333. (unless (= n 0)
  7334. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7335. (kill-whole-line))
  7336. (goto-char (point-min))
  7337. (while (re-search-forward drawer-re nil t)
  7338. (mapc (lambda (d)
  7339. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7340. (goto-char (point-min))
  7341. (when doshift
  7342. (while (re-search-forward org-ts-regexp-both nil t)
  7343. (org-timestamp-change (* n shift-n) shift-what))
  7344. (unless (= n n-no-remove)
  7345. (goto-char (point-min))
  7346. (while (re-search-forward org-ts-regexp nil t)
  7347. (save-excursion
  7348. (goto-char (match-beginning 0))
  7349. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7350. (delete-region (match-beginning 1) (match-end 1)))))))
  7351. (setq task (buffer-string)))
  7352. (insert task))
  7353. (goto-char beg)))
  7354. ;;; Outline Sorting
  7355. (defun org-sort (with-case)
  7356. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7357. Optional argument WITH-CASE means sort case-sensitively."
  7358. (interactive "P")
  7359. (cond
  7360. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7361. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7362. (t
  7363. (org-call-with-arg 'org-sort-entries with-case))))
  7364. (defun org-sort-remove-invisible (s)
  7365. (remove-text-properties 0 (length s) org-rm-props s)
  7366. (while (string-match org-bracket-link-regexp s)
  7367. (setq s (replace-match (if (match-end 2)
  7368. (match-string 3 s)
  7369. (match-string 1 s)) t t s)))
  7370. s)
  7371. (defvar org-priority-regexp) ; defined later in the file
  7372. (defvar org-after-sorting-entries-or-items-hook nil
  7373. "Hook that is run after a bunch of entries or items have been sorted.
  7374. When children are sorted, the cursor is in the parent line when this
  7375. hook gets called. When a region or a plain list is sorted, the cursor
  7376. will be in the first entry of the sorted region/list.")
  7377. (defun org-sort-entries
  7378. (&optional with-case sorting-type getkey-func compare-func property)
  7379. "Sort entries on a certain level of an outline tree.
  7380. If there is an active region, the entries in the region are sorted.
  7381. Else, if the cursor is before the first entry, sort the top-level items.
  7382. Else, the children of the entry at point are sorted.
  7383. Sorting can be alphabetically, numerically, by date/time as given by
  7384. a time stamp, by a property or by priority.
  7385. The command prompts for the sorting type unless it has been given to the
  7386. function through the SORTING-TYPE argument, which needs to be a character,
  7387. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7388. precise meaning of each character:
  7389. n Numerically, by converting the beginning of the entry/item to a number.
  7390. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7391. o By order of TODO keywords.
  7392. t By date/time, either the first active time stamp in the entry, or, if
  7393. none exist, by the first inactive one.
  7394. s By the scheduled date/time.
  7395. d By deadline date/time.
  7396. c By creation time, which is assumed to be the first inactive time stamp
  7397. at the beginning of a line.
  7398. p By priority according to the cookie.
  7399. r By the value of a property.
  7400. Capital letters will reverse the sort order.
  7401. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7402. called with point at the beginning of the record. It must return either
  7403. a string or a number that should serve as the sorting key for that record.
  7404. Comparing entries ignores case by default. However, with an optional argument
  7405. WITH-CASE, the sorting considers case as well."
  7406. (interactive "P")
  7407. (let ((case-func (if with-case 'identity 'downcase))
  7408. (cmstr
  7409. ;; The clock marker is lost when using `sort-subr', let's
  7410. ;; store the clocking string.
  7411. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7412. (save-excursion
  7413. (goto-char org-clock-marker)
  7414. (looking-back "^.*") (match-string-no-properties 0))))
  7415. start beg end stars re re2
  7416. txt what tmp)
  7417. ;; Find beginning and end of region to sort
  7418. (cond
  7419. ((org-region-active-p)
  7420. ;; we will sort the region
  7421. (setq end (region-end)
  7422. what "region")
  7423. (goto-char (region-beginning))
  7424. (if (not (org-at-heading-p)) (outline-next-heading))
  7425. (setq start (point)))
  7426. ((or (org-at-heading-p)
  7427. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7428. ;; we will sort the children of the current headline
  7429. (org-back-to-heading)
  7430. (setq start (point)
  7431. end (progn (org-end-of-subtree t t)
  7432. (or (bolp) (insert "\n"))
  7433. (org-back-over-empty-lines)
  7434. (point))
  7435. what "children")
  7436. (goto-char start)
  7437. (show-subtree)
  7438. (outline-next-heading))
  7439. (t
  7440. ;; we will sort the top-level entries in this file
  7441. (goto-char (point-min))
  7442. (or (org-at-heading-p) (outline-next-heading))
  7443. (setq start (point))
  7444. (goto-char (point-max))
  7445. (beginning-of-line 1)
  7446. (when (looking-at ".*?\\S-")
  7447. ;; File ends in a non-white line
  7448. (end-of-line 1)
  7449. (insert "\n"))
  7450. (setq end (point-max))
  7451. (setq what "top-level")
  7452. (goto-char start)
  7453. (show-all)))
  7454. (setq beg (point))
  7455. (if (>= beg end) (error "Nothing to sort"))
  7456. (looking-at "\\(\\*+\\)")
  7457. (setq stars (match-string 1)
  7458. re (concat "^" (regexp-quote stars) " +")
  7459. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7460. txt (buffer-substring beg end))
  7461. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7462. (if (and (not (equal stars "*")) (string-match re2 txt))
  7463. (error "Region to sort contains a level above the first entry"))
  7464. (unless sorting-type
  7465. (message
  7466. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7467. [t]ime [s]cheduled [d]eadline [c]reated
  7468. A/N/P/R/O/F/T/S/D/C means reversed:"
  7469. what)
  7470. (setq sorting-type (read-char-exclusive))
  7471. (unless getkey-func
  7472. (and (= (downcase sorting-type) ?f)
  7473. (setq getkey-func
  7474. (org-icompleting-read "Sort using function: "
  7475. obarray 'fboundp t nil nil))
  7476. (setq getkey-func (intern getkey-func))))
  7477. (and (= (downcase sorting-type) ?r)
  7478. (not property)
  7479. (setq property
  7480. (org-icompleting-read "Property: "
  7481. (mapcar 'list (org-buffer-property-keys t))
  7482. nil t))))
  7483. (message "Sorting entries...")
  7484. (save-restriction
  7485. (narrow-to-region start end)
  7486. (let ((dcst (downcase sorting-type))
  7487. (case-fold-search nil)
  7488. (now (current-time)))
  7489. (sort-subr
  7490. (/= dcst sorting-type)
  7491. ;; This function moves to the beginning character of the "record" to
  7492. ;; be sorted.
  7493. (lambda nil
  7494. (if (re-search-forward re nil t)
  7495. (goto-char (match-beginning 0))
  7496. (goto-char (point-max))))
  7497. ;; This function moves to the last character of the "record" being
  7498. ;; sorted.
  7499. (lambda nil
  7500. (save-match-data
  7501. (condition-case nil
  7502. (outline-forward-same-level 1)
  7503. (error
  7504. (goto-char (point-max))))))
  7505. ;; This function returns the value that gets sorted against.
  7506. (lambda nil
  7507. (cond
  7508. ((= dcst ?n)
  7509. (if (looking-at org-complex-heading-regexp)
  7510. (string-to-number (match-string 4))
  7511. nil))
  7512. ((= dcst ?a)
  7513. (if (looking-at org-complex-heading-regexp)
  7514. (funcall case-func (match-string 4))
  7515. nil))
  7516. ((= dcst ?t)
  7517. (let ((end (save-excursion (outline-next-heading) (point))))
  7518. (if (or (re-search-forward org-ts-regexp end t)
  7519. (re-search-forward org-ts-regexp-both end t))
  7520. (org-time-string-to-seconds (match-string 0))
  7521. (org-float-time now))))
  7522. ((= dcst ?c)
  7523. (let ((end (save-excursion (outline-next-heading) (point))))
  7524. (if (re-search-forward
  7525. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7526. end t)
  7527. (org-time-string-to-seconds (match-string 0))
  7528. (org-float-time now))))
  7529. ((= dcst ?s)
  7530. (let ((end (save-excursion (outline-next-heading) (point))))
  7531. (if (re-search-forward org-scheduled-time-regexp end t)
  7532. (org-time-string-to-seconds (match-string 1))
  7533. (org-float-time now))))
  7534. ((= dcst ?d)
  7535. (let ((end (save-excursion (outline-next-heading) (point))))
  7536. (if (re-search-forward org-deadline-time-regexp end t)
  7537. (org-time-string-to-seconds (match-string 1))
  7538. (org-float-time now))))
  7539. ((= dcst ?p)
  7540. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7541. (string-to-char (match-string 2))
  7542. org-default-priority))
  7543. ((= dcst ?r)
  7544. (or (org-entry-get nil property) ""))
  7545. ((= dcst ?o)
  7546. (if (looking-at org-complex-heading-regexp)
  7547. (- 9999 (length (member (match-string 2)
  7548. org-todo-keywords-1)))))
  7549. ((= dcst ?f)
  7550. (if getkey-func
  7551. (progn
  7552. (setq tmp (funcall getkey-func))
  7553. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7554. tmp)
  7555. (error "Invalid key function `%s'" getkey-func)))
  7556. (t (error "Invalid sorting type `%c'" sorting-type))))
  7557. nil
  7558. (cond
  7559. ((= dcst ?a) 'string<)
  7560. ((= dcst ?f) compare-func)
  7561. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7562. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7563. ;; Reset the clock marker if needed
  7564. (when cmstr
  7565. (save-excursion
  7566. (goto-char start)
  7567. (search-forward cmstr nil t)
  7568. (move-marker org-clock-marker (point))))
  7569. (message "Sorting entries...done")))
  7570. (defun org-do-sort (table what &optional with-case sorting-type)
  7571. "Sort TABLE of WHAT according to SORTING-TYPE.
  7572. The user will be prompted for the SORTING-TYPE if the call to this
  7573. function does not specify it. WHAT is only for the prompt, to indicate
  7574. what is being sorted. The sorting key will be extracted from
  7575. the car of the elements of the table.
  7576. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7577. (unless sorting-type
  7578. (message
  7579. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7580. what)
  7581. (setq sorting-type (read-char-exclusive)))
  7582. (let ((dcst (downcase sorting-type))
  7583. extractfun comparefun)
  7584. ;; Define the appropriate functions
  7585. (cond
  7586. ((= dcst ?n)
  7587. (setq extractfun 'string-to-number
  7588. comparefun (if (= dcst sorting-type) '< '>)))
  7589. ((= dcst ?a)
  7590. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7591. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7592. comparefun (if (= dcst sorting-type)
  7593. 'string<
  7594. (lambda (a b) (and (not (string< a b))
  7595. (not (string= a b)))))))
  7596. ((= dcst ?t)
  7597. (setq extractfun
  7598. (lambda (x)
  7599. (if (or (string-match org-ts-regexp x)
  7600. (string-match org-ts-regexp-both x))
  7601. (org-float-time
  7602. (org-time-string-to-time (match-string 0 x)))
  7603. 0))
  7604. comparefun (if (= dcst sorting-type) '< '>)))
  7605. (t (error "Invalid sorting type `%c'" sorting-type)))
  7606. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7607. table)
  7608. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7609. ;;; The orgstruct minor mode
  7610. ;; Define a minor mode which can be used in other modes in order to
  7611. ;; integrate the org-mode structure editing commands.
  7612. ;; This is really a hack, because the org-mode structure commands use
  7613. ;; keys which normally belong to the major mode. Here is how it
  7614. ;; works: The minor mode defines all the keys necessary to operate the
  7615. ;; structure commands, but wraps the commands into a function which
  7616. ;; tests if the cursor is currently at a headline or a plain list
  7617. ;; item. If that is the case, the structure command is used,
  7618. ;; temporarily setting many Org-mode variables like regular
  7619. ;; expressions for filling etc. However, when any of those keys is
  7620. ;; used at a different location, function uses `key-binding' to look
  7621. ;; up if the key has an associated command in another currently active
  7622. ;; keymap (minor modes, major mode, global), and executes that
  7623. ;; command. There might be problems if any of the keys is otherwise
  7624. ;; used as a prefix key.
  7625. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7626. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7627. ;; addresses this by checking explicitly for both bindings.
  7628. (defvar orgstruct-mode-map (make-sparse-keymap)
  7629. "Keymap for the minor `orgstruct-mode'.")
  7630. (defvar org-local-vars nil
  7631. "List of local variables, for use by `orgstruct-mode'.")
  7632. ;;;###autoload
  7633. (define-minor-mode orgstruct-mode
  7634. "Toggle the minor mode `orgstruct-mode'.
  7635. This mode is for using Org-mode structure commands in other
  7636. modes. The following keys behave as if Org-mode were active, if
  7637. the cursor is on a headline, or on a plain list item (both as
  7638. defined by Org-mode).
  7639. M-up Move entry/item up
  7640. M-down Move entry/item down
  7641. M-left Promote
  7642. M-right Demote
  7643. M-S-up Move entry/item up
  7644. M-S-down Move entry/item down
  7645. M-S-left Promote subtree
  7646. M-S-right Demote subtree
  7647. M-q Fill paragraph and items like in Org-mode
  7648. C-c ^ Sort entries
  7649. C-c - Cycle list bullet
  7650. TAB Cycle item visibility
  7651. M-RET Insert new heading/item
  7652. S-M-RET Insert new TODO heading / Checkbox item
  7653. C-c C-c Set tags / toggle checkbox"
  7654. nil " OrgStruct" nil
  7655. (org-load-modules-maybe)
  7656. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7657. ;;;###autoload
  7658. (defun turn-on-orgstruct ()
  7659. "Unconditionally turn on `orgstruct-mode'."
  7660. (orgstruct-mode 1))
  7661. (defvar org-fb-vars nil)
  7662. (make-variable-buffer-local 'org-fb-vars)
  7663. (defun orgstruct++-mode (&optional arg)
  7664. "Toggle `orgstruct-mode', the enhanced version of it.
  7665. In addition to setting orgstruct-mode, this also exports all
  7666. indentation and autofilling variables from org-mode into the
  7667. buffer. It will also recognize item context in multiline items."
  7668. (interactive "P")
  7669. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7670. (if (< arg 1)
  7671. (progn (orgstruct-mode -1)
  7672. (mapc (lambda(v)
  7673. (org-set-local (car v)
  7674. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7675. org-fb-vars))
  7676. (orgstruct-mode 1)
  7677. (setq org-fb-vars nil)
  7678. (let (var val)
  7679. (mapc
  7680. (lambda (x)
  7681. (when (string-match
  7682. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7683. (symbol-name (car x)))
  7684. (setq var (car x) val (nth 1 x))
  7685. (push (list var `(quote ,(eval var))) org-fb-vars)
  7686. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7687. org-local-vars)
  7688. (org-set-local 'orgstruct-is-++ t))))
  7689. (defvar orgstruct-is-++ nil
  7690. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7691. (make-variable-buffer-local 'orgstruct-is-++)
  7692. ;;;###autoload
  7693. (defun turn-on-orgstruct++ ()
  7694. "Unconditionally turn on `orgstruct++-mode'."
  7695. (orgstruct++-mode 1))
  7696. (defun orgstruct-error ()
  7697. "Error when there is no default binding for a structure key."
  7698. (interactive)
  7699. (error "This key has no function outside structure elements"))
  7700. (defun orgstruct-setup ()
  7701. "Setup orgstruct keymaps."
  7702. (let ((nfunc 0)
  7703. (bindings
  7704. (list
  7705. '([(meta up)] org-metaup)
  7706. '([(meta down)] org-metadown)
  7707. '([(meta left)] org-metaleft)
  7708. '([(meta right)] org-metaright)
  7709. '([(meta shift up)] org-shiftmetaup)
  7710. '([(meta shift down)] org-shiftmetadown)
  7711. '([(meta shift left)] org-shiftmetaleft)
  7712. '([(meta shift right)] org-shiftmetaright)
  7713. '([?\e (up)] org-metaup)
  7714. '([?\e (down)] org-metadown)
  7715. '([?\e (left)] org-metaleft)
  7716. '([?\e (right)] org-metaright)
  7717. '([?\e (shift up)] org-shiftmetaup)
  7718. '([?\e (shift down)] org-shiftmetadown)
  7719. '([?\e (shift left)] org-shiftmetaleft)
  7720. '([?\e (shift right)] org-shiftmetaright)
  7721. '([(shift up)] org-shiftup)
  7722. '([(shift down)] org-shiftdown)
  7723. '([(shift left)] org-shiftleft)
  7724. '([(shift right)] org-shiftright)
  7725. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7726. '("\M-q" fill-paragraph)
  7727. '("\C-c^" org-sort)
  7728. '("\C-c-" org-cycle-list-bullet)))
  7729. elt key fun cmd)
  7730. (while (setq elt (pop bindings))
  7731. (setq nfunc (1+ nfunc))
  7732. (setq key (org-key (car elt))
  7733. fun (nth 1 elt)
  7734. cmd (orgstruct-make-binding fun nfunc key))
  7735. (org-defkey orgstruct-mode-map key cmd))
  7736. ;; Prevent an error for users who forgot to make autoloads
  7737. (require 'org-element)
  7738. ;; Special treatment needed for TAB and RET
  7739. (org-defkey orgstruct-mode-map [(tab)]
  7740. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7741. (org-defkey orgstruct-mode-map "\C-i"
  7742. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7743. (org-defkey orgstruct-mode-map "\M-\C-m"
  7744. (orgstruct-make-binding 'org-insert-heading 105
  7745. "\M-\C-m" [(meta return)]))
  7746. (org-defkey orgstruct-mode-map [(meta return)]
  7747. (orgstruct-make-binding 'org-insert-heading 106
  7748. [(meta return)] "\M-\C-m"))
  7749. (org-defkey orgstruct-mode-map [(shift meta return)]
  7750. (orgstruct-make-binding 'org-insert-todo-heading 107
  7751. [(meta return)] "\M-\C-m"))
  7752. (org-defkey orgstruct-mode-map "\e\C-m"
  7753. (orgstruct-make-binding 'org-insert-heading 108
  7754. "\e\C-m" [?\e (return)]))
  7755. (org-defkey orgstruct-mode-map [?\e (return)]
  7756. (orgstruct-make-binding 'org-insert-heading 109
  7757. [?\e (return)] "\e\C-m"))
  7758. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7759. (orgstruct-make-binding 'org-insert-todo-heading 110
  7760. [?\e (return)] "\e\C-m"))
  7761. (unless org-local-vars
  7762. (setq org-local-vars (org-get-local-variables)))
  7763. t))
  7764. (defun orgstruct-make-binding (fun n &rest keys)
  7765. "Create a function for binding in the structure minor mode.
  7766. FUN is the command to call inside a table. N is used to create a unique
  7767. command name. KEYS are keys that should be checked in for a command
  7768. to execute outside of tables."
  7769. (eval
  7770. (list 'defun
  7771. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7772. '(arg)
  7773. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7774. "Outside of structure, run the binding of `"
  7775. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7776. "'.")
  7777. '(interactive "p")
  7778. (list 'if
  7779. `(org-context-p 'headline 'item
  7780. (and orgstruct-is-++
  7781. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7782. 'item-body))
  7783. (list 'org-run-like-in-org-mode (list 'quote fun))
  7784. (list 'let '(orgstruct-mode)
  7785. (list 'call-interactively
  7786. (append '(or)
  7787. (mapcar (lambda (k)
  7788. (list 'key-binding k))
  7789. keys)
  7790. '('orgstruct-error))))))))
  7791. (defun org-contextualize-keys (alist contexts)
  7792. "Return valid elements in ALIST depending on CONTEXTS.
  7793. `org-agenda-custom-commands' or `org-capture-templates' are the
  7794. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7795. or `org-capture-templates-contexts' are the associated contexts
  7796. definitions."
  7797. (let ((contexts
  7798. ;; normalize contexts
  7799. (mapcar
  7800. (lambda(c) (cond ((listp (cadr c))
  7801. (list (car c) (car c) (cadr c)))
  7802. ((string= "" (cadr c))
  7803. (list (car c) (car c) (caddr c)))
  7804. (t c))) contexts))
  7805. (a alist) c r s)
  7806. ;; loop over all commands or templates
  7807. (while (setq c (pop a))
  7808. (let (vrules repl)
  7809. (cond
  7810. ((not (assoc (car c) contexts))
  7811. (push c r))
  7812. ((and (assoc (car c) contexts)
  7813. (setq vrules (org-contextualize-validate-key
  7814. (car c) contexts)))
  7815. (mapc (lambda (vr)
  7816. (when (not (equal (car vr) (cadr vr)))
  7817. (setq repl vr))) vrules)
  7818. (if (not repl) (push c r)
  7819. (push (cadr repl) s)
  7820. (push
  7821. (cons (car c)
  7822. (cdr (or (assoc (cadr repl) alist)
  7823. (error "Undefined key `%s' as contextual replacement for `%s'"
  7824. (cadr repl) (car c)))))
  7825. r))))))
  7826. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7827. (delq
  7828. nil
  7829. (delete-dups
  7830. (mapcar (lambda (x)
  7831. (let ((tpl (car x)))
  7832. (when (not (delq
  7833. nil
  7834. (mapcar (lambda(y)
  7835. (equal y tpl)) s))) x)))
  7836. (reverse r))))))
  7837. (defun org-contextualize-validate-key (key contexts)
  7838. "Check CONTEXTS for agenda or capture KEY."
  7839. (let (r rr res)
  7840. (while (setq r (pop contexts))
  7841. (mapc
  7842. (lambda (rr)
  7843. (when
  7844. (and (equal key (car r))
  7845. (if (functionp rr) (funcall rr)
  7846. (or (and (eq (car rr) 'in-file)
  7847. (buffer-file-name)
  7848. (string-match (cdr rr) (buffer-file-name)))
  7849. (and (eq (car rr) 'in-mode)
  7850. (string-match (cdr rr) (symbol-name major-mode)))
  7851. (when (and (eq (car rr) 'not-in-file)
  7852. (buffer-file-name))
  7853. (not (string-match (cdr rr) (buffer-file-name))))
  7854. (when (eq (car rr) 'not-in-mode)
  7855. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7856. (push r res)))
  7857. (car (last r))))
  7858. (delete-dups (delq nil res))))
  7859. (defun org-context-p (&rest contexts)
  7860. "Check if local context is any of CONTEXTS.
  7861. Possible values in the list of contexts are `table', `headline', and `item'."
  7862. (let ((pos (point)))
  7863. (goto-char (point-at-bol))
  7864. (prog1 (or (and (memq 'table contexts)
  7865. (looking-at "[ \t]*|"))
  7866. (and (memq 'headline contexts)
  7867. (looking-at org-outline-regexp))
  7868. (and (memq 'item contexts)
  7869. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7870. (and (memq 'item-body contexts)
  7871. (org-in-item-p)))
  7872. (goto-char pos))))
  7873. (defun org-get-local-variables ()
  7874. "Return a list of all local variables in an Org mode buffer."
  7875. (let (varlist)
  7876. (with-current-buffer (get-buffer-create "*Org tmp*")
  7877. (erase-buffer)
  7878. (org-mode)
  7879. (setq varlist (buffer-local-variables)))
  7880. (kill-buffer "*Org tmp*")
  7881. (delq nil
  7882. (mapcar
  7883. (lambda (x)
  7884. (setq x
  7885. (if (symbolp x)
  7886. (list x)
  7887. (list (car x) (list 'quote (cdr x)))))
  7888. (if (string-match
  7889. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7890. (symbol-name (car x)))
  7891. x nil))
  7892. varlist))))
  7893. (defun org-clone-local-variables (from-buffer &optional regexp)
  7894. "Clone local variables from FROM-BUFFER.
  7895. Optional argument REGEXP selects variables to clone."
  7896. (mapc
  7897. (lambda (pair)
  7898. (and (symbolp (car pair))
  7899. (or (null regexp)
  7900. (string-match regexp (symbol-name (car pair))))
  7901. (set (make-local-variable (car pair))
  7902. (cdr pair))))
  7903. (buffer-local-variables from-buffer)))
  7904. ;;;###autoload
  7905. (defun org-run-like-in-org-mode (cmd)
  7906. "Run a command, pretending that the current buffer is in Org-mode.
  7907. This will temporarily bind local variables that are typically bound in
  7908. Org-mode to the values they have in Org-mode, and then interactively
  7909. call CMD."
  7910. (org-load-modules-maybe)
  7911. (unless org-local-vars
  7912. (setq org-local-vars (org-get-local-variables)))
  7913. (eval (list 'let org-local-vars
  7914. (list 'call-interactively (list 'quote cmd)))))
  7915. ;;;; Archiving
  7916. (defun org-get-category (&optional pos force-refresh)
  7917. "Get the category applying to position POS."
  7918. (save-match-data
  7919. (if force-refresh (org-refresh-category-properties))
  7920. (let ((pos (or pos (point))))
  7921. (or (get-text-property pos 'org-category)
  7922. (progn (org-refresh-category-properties)
  7923. (get-text-property pos 'org-category))))))
  7924. (defun org-refresh-category-properties ()
  7925. "Refresh category text properties in the buffer."
  7926. (let ((case-fold-search t)
  7927. (inhibit-read-only t)
  7928. (def-cat (cond
  7929. ((null org-category)
  7930. (if buffer-file-name
  7931. (file-name-sans-extension
  7932. (file-name-nondirectory buffer-file-name))
  7933. "???"))
  7934. ((symbolp org-category) (symbol-name org-category))
  7935. (t org-category)))
  7936. beg end cat pos optionp)
  7937. (org-unmodified
  7938. (save-excursion
  7939. (save-restriction
  7940. (widen)
  7941. (goto-char (point-min))
  7942. (put-text-property (point) (point-max) 'org-category def-cat)
  7943. (while (re-search-forward
  7944. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7945. (setq pos (match-end 0)
  7946. optionp (equal (char-after (match-beginning 0)) ?#)
  7947. cat (org-trim (match-string 2)))
  7948. (if optionp
  7949. (setq beg (point-at-bol) end (point-max))
  7950. (org-back-to-heading t)
  7951. (setq beg (point) end (org-end-of-subtree t t)))
  7952. (put-text-property beg end 'org-category cat)
  7953. (put-text-property beg end 'org-category-position beg)
  7954. (goto-char pos)))))))
  7955. (defun org-refresh-properties (dprop tprop)
  7956. "Refresh buffer text properties.
  7957. DPROP is the drawer property and TPROP is the corresponding text
  7958. property to set."
  7959. (let ((case-fold-search t)
  7960. (inhibit-read-only t) p)
  7961. (org-unmodified
  7962. (save-excursion
  7963. (save-restriction
  7964. (widen)
  7965. (goto-char (point-min))
  7966. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7967. (setq p (org-match-string-no-properties 1))
  7968. (save-excursion
  7969. (org-back-to-heading t)
  7970. (put-text-property
  7971. (point-at-bol) (point-at-eol) tprop p))))))))
  7972. ;;;; Link Stuff
  7973. ;;; Link abbreviations
  7974. (defun org-link-expand-abbrev (link)
  7975. "Apply replacements as defined in `org-link-abbrev-alist'."
  7976. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7977. (let* ((key (match-string 1 link))
  7978. (as (or (assoc key org-link-abbrev-alist-local)
  7979. (assoc key org-link-abbrev-alist)))
  7980. (tag (and (match-end 2) (match-string 3 link)))
  7981. rpl)
  7982. (if (not as)
  7983. link
  7984. (setq rpl (cdr as))
  7985. (cond
  7986. ((symbolp rpl) (funcall rpl tag))
  7987. ((string-match "%(\\([^)]+\\))" rpl)
  7988. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7989. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7990. ((string-match "%h" rpl)
  7991. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7992. (t (concat rpl tag)))))
  7993. link))
  7994. ;;; Storing and inserting links
  7995. (defvar org-insert-link-history nil
  7996. "Minibuffer history for links inserted with `org-insert-link'.")
  7997. (defvar org-stored-links nil
  7998. "Contains the links stored with `org-store-link'.")
  7999. (defvar org-store-link-plist nil
  8000. "Plist with info about the most recently link created with `org-store-link'.")
  8001. (defvar org-link-protocols nil
  8002. "Link protocols added to Org-mode using `org-add-link-type'.")
  8003. (defvar org-store-link-functions nil
  8004. "List of functions that are called to create and store a link.
  8005. Each function will be called in turn until one returns a non-nil
  8006. value. Each function should check if it is responsible for creating
  8007. this link (for example by looking at the major mode).
  8008. If not, it must exit and return nil.
  8009. If yes, it should return a non-nil value after a calling
  8010. `org-store-link-props' with a list of properties and values.
  8011. Special properties are:
  8012. :type The link prefix, like \"http\". This must be given.
  8013. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8014. This is obligatory as well.
  8015. :description Optional default description for the second pair
  8016. of brackets in an Org-mode link. The user can still change
  8017. this when inserting this link into an Org-mode buffer.
  8018. In addition to these, any additional properties can be specified
  8019. and then used in capture templates.")
  8020. (defun org-add-link-type (type &optional follow export)
  8021. "Add TYPE to the list of `org-link-types'.
  8022. Re-compute all regular expressions depending on `org-link-types'
  8023. FOLLOW and EXPORT are two functions.
  8024. FOLLOW should take the link path as the single argument and do whatever
  8025. is necessary to follow the link, for example find a file or display
  8026. a mail message.
  8027. EXPORT should format the link path for export to one of the export formats.
  8028. It should be a function accepting three arguments:
  8029. path the path of the link, the text after the prefix (like \"http:\")
  8030. desc the description of the link, if any, or a description added by
  8031. org-export-normalize-links if there is none
  8032. format the export format, a symbol like `html' or `latex' or `ascii'..
  8033. The function may use the FORMAT information to return different values
  8034. depending on the format. The return value will be put literally into
  8035. the exported file. If the return value is nil, this means Org should
  8036. do what it normally does with links which do not have EXPORT defined.
  8037. Org-mode has a built-in default for exporting links. If you are happy with
  8038. this default, there is no need to define an export function for the link
  8039. type. For a simple example of an export function, see `org-bbdb.el'."
  8040. (add-to-list 'org-link-types type t)
  8041. (org-make-link-regexps)
  8042. (if (assoc type org-link-protocols)
  8043. (setcdr (assoc type org-link-protocols) (list follow export))
  8044. (push (list type follow export) org-link-protocols)))
  8045. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8046. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8047. ;;;###autoload
  8048. (defun org-store-link (arg)
  8049. "\\<org-mode-map>Store an org-link to the current location.
  8050. This link is added to `org-stored-links' and can later be inserted
  8051. into an org-buffer with \\[org-insert-link].
  8052. For some link types, a prefix arg is interpreted.
  8053. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8054. For file links, arg negates `org-context-in-file-links'.
  8055. A double prefix arg force skipping storing functions that are not
  8056. part of Org's core."
  8057. (interactive "P")
  8058. (org-load-modules-maybe)
  8059. (setq org-store-link-plist nil) ; reset
  8060. (org-with-limited-levels
  8061. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8062. (cond
  8063. ((and (not (equal arg '(16)))
  8064. (setq sfuns
  8065. (delq
  8066. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8067. org-store-link-functions))
  8068. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8069. (or (and (cdr sfuns)
  8070. (funcall (intern
  8071. (completing-read "Which function for creating the link? "
  8072. sfunsn t (car sfunsn)))))
  8073. (funcall (caar sfuns)))
  8074. (setq link (plist-get org-store-link-plist :link)
  8075. desc (or (plist-get org-store-link-plist :description) link))))
  8076. ((org-src-edit-buffer-p)
  8077. (let (label gc)
  8078. (while (or (not label)
  8079. (save-excursion
  8080. (save-restriction
  8081. (widen)
  8082. (goto-char (point-min))
  8083. (re-search-forward
  8084. (regexp-quote (format org-coderef-label-format label))
  8085. nil t))))
  8086. (when label (message "Label exists already") (sit-for 2))
  8087. (setq label (read-string "Code line label: " label)))
  8088. (end-of-line 1)
  8089. (setq link (format org-coderef-label-format label))
  8090. (setq gc (- 79 (length link)))
  8091. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8092. (insert link)
  8093. (setq link (concat "(" label ")") desc nil)))
  8094. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8095. ;; We are in the agenda, link to referenced location
  8096. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8097. (get-text-property (point) 'org-marker))))
  8098. (when m
  8099. (org-with-point-at m
  8100. (setq agenda-link
  8101. (if (org-called-interactively-p 'any)
  8102. (call-interactively 'org-store-link)
  8103. (org-store-link nil)))))))
  8104. ((eq major-mode 'calendar-mode)
  8105. (let ((cd (calendar-cursor-to-date)))
  8106. (setq link
  8107. (format-time-string
  8108. (car org-time-stamp-formats)
  8109. (apply 'encode-time
  8110. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8111. nil nil nil))))
  8112. (org-store-link-props :type "calendar" :date cd)))
  8113. ((eq major-mode 'help-mode)
  8114. (setq link (concat "help:" (save-excursion
  8115. (goto-char (point-min))
  8116. (looking-at "^[^ ]+")
  8117. (match-string 0))))
  8118. (org-store-link-props :type "help"))
  8119. ((eq major-mode 'w3-mode)
  8120. (setq cpltxt (if (and (buffer-name)
  8121. (not (string-match "Untitled" (buffer-name))))
  8122. (buffer-name)
  8123. (url-view-url t))
  8124. link (url-view-url t))
  8125. (org-store-link-props :type "w3" :url (url-view-url t)))
  8126. ((setq search (run-hook-with-args-until-success
  8127. 'org-create-file-search-functions))
  8128. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8129. "::" search))
  8130. (setq cpltxt (or description link)))
  8131. ((eq major-mode 'image-mode)
  8132. (setq cpltxt (concat "file:"
  8133. (abbreviate-file-name buffer-file-name))
  8134. link cpltxt)
  8135. (org-store-link-props :type "image" :file buffer-file-name))
  8136. ((eq major-mode 'dired-mode)
  8137. ;; link to the file in the current line
  8138. (let ((file (dired-get-filename nil t)))
  8139. (setq file (if file
  8140. (abbreviate-file-name
  8141. (expand-file-name (dired-get-filename nil t)))
  8142. ;; otherwise, no file so use current directory.
  8143. default-directory))
  8144. (setq cpltxt (concat "file:" file)
  8145. link cpltxt)))
  8146. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8147. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8148. (cond
  8149. ((org-in-regexp "<<\\(.*?\\)>>")
  8150. (setq cpltxt
  8151. (concat "file:"
  8152. (abbreviate-file-name
  8153. (buffer-file-name (buffer-base-buffer)))
  8154. "::" (match-string 1))
  8155. link cpltxt))
  8156. ((and (featurep 'org-id)
  8157. (or (eq org-id-link-to-org-use-id t)
  8158. (and (org-called-interactively-p 'any)
  8159. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8160. (and (eq org-id-link-to-org-use-id
  8161. 'create-if-interactive-and-no-custom-id)
  8162. (not custom-id))))
  8163. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8164. ;; We can make a link using the ID.
  8165. (setq link (condition-case nil
  8166. (prog1 (org-id-store-link)
  8167. (setq desc (plist-get org-store-link-plist :description)))
  8168. (error
  8169. ;; probably before first headline, link to file only
  8170. (concat "file:"
  8171. (abbreviate-file-name
  8172. (buffer-file-name (buffer-base-buffer))))))))
  8173. (t
  8174. ;; Just link to current headline
  8175. (setq cpltxt (concat "file:"
  8176. (abbreviate-file-name
  8177. (buffer-file-name (buffer-base-buffer)))))
  8178. ;; Add a context search string
  8179. (when (org-xor org-context-in-file-links arg)
  8180. (let* ((ee (org-element-at-point))
  8181. (et (org-element-type ee))
  8182. (ev (plist-get (cadr ee) :value)))
  8183. (setq txt (cond
  8184. ((org-at-heading-p) nil)
  8185. ((eq et 'keyword) ev)
  8186. ((org-region-active-p)
  8187. (buffer-substring (region-beginning) (region-end)))))
  8188. (when (or (null txt) (string-match "\\S-" txt))
  8189. (setq cpltxt
  8190. (concat cpltxt "::"
  8191. (condition-case nil
  8192. (org-make-org-heading-search-string txt)
  8193. (error "")))
  8194. desc (or (and (eq et 'keyword) ev)
  8195. (nth 4 (ignore-errors (org-heading-components)))
  8196. "NONE")))))
  8197. (if (string-match "::\\'" cpltxt)
  8198. (setq cpltxt (substring cpltxt 0 -2)))
  8199. (setq link cpltxt))))
  8200. ((buffer-file-name (buffer-base-buffer))
  8201. ;; Just link to this file here.
  8202. (setq cpltxt (concat "file:"
  8203. (abbreviate-file-name
  8204. (buffer-file-name (buffer-base-buffer)))))
  8205. ;; Add a context string
  8206. (when (org-xor org-context-in-file-links arg)
  8207. (setq txt (if (org-region-active-p)
  8208. (buffer-substring (region-beginning) (region-end))
  8209. (buffer-substring (point-at-bol) (point-at-eol))))
  8210. ;; Only use search option if there is some text.
  8211. (when (string-match "\\S-" txt)
  8212. (setq cpltxt
  8213. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8214. desc "NONE")))
  8215. (setq link cpltxt))
  8216. ((org-called-interactively-p 'interactive)
  8217. (user-error "No method for storing a link from this buffer"))
  8218. (t (setq link nil)))
  8219. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8220. (setq link (or link cpltxt)
  8221. desc (or desc cpltxt))
  8222. (cond ((equal desc "NONE") (setq desc nil))
  8223. ((string-match org-bracket-link-regexp desc)
  8224. (setq desc (replace-regexp-in-string
  8225. org-bracket-link-regexp
  8226. (concat "\\3" (if (equal (length (match-string 0 desc))
  8227. (length desc)) "*" "")) desc))))
  8228. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8229. (progn
  8230. (setq org-stored-links
  8231. (cons (list link desc) org-stored-links))
  8232. (message "Stored: %s" (or desc link))
  8233. (when custom-id
  8234. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8235. "::#" custom-id))
  8236. (setq org-stored-links
  8237. (cons (list link desc) org-stored-links))))
  8238. (or agenda-link (and link (org-make-link-string link desc)))))))
  8239. (defun org-store-link-props (&rest plist)
  8240. "Store link properties, extract names and addresses."
  8241. (let (x adr)
  8242. (when (setq x (plist-get plist :from))
  8243. (setq adr (mail-extract-address-components x))
  8244. (setq plist (plist-put plist :fromname (car adr)))
  8245. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8246. (when (setq x (plist-get plist :to))
  8247. (setq adr (mail-extract-address-components x))
  8248. (setq plist (plist-put plist :toname (car adr)))
  8249. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8250. (let ((from (plist-get plist :from))
  8251. (to (plist-get plist :to)))
  8252. (when (and from to org-from-is-user-regexp)
  8253. (setq plist
  8254. (plist-put plist :fromto
  8255. (if (string-match org-from-is-user-regexp from)
  8256. (concat "to %t")
  8257. (concat "from %f"))))))
  8258. (setq org-store-link-plist plist))
  8259. (defun org-add-link-props (&rest plist)
  8260. "Add these properties to the link property list."
  8261. (let (key value)
  8262. (while plist
  8263. (setq key (pop plist) value (pop plist))
  8264. (setq org-store-link-plist
  8265. (plist-put org-store-link-plist key value)))))
  8266. (defun org-email-link-description (&optional fmt)
  8267. "Return the description part of an email link.
  8268. This takes information from `org-store-link-plist' and formats it
  8269. according to FMT (default from `org-email-link-description-format')."
  8270. (setq fmt (or fmt org-email-link-description-format))
  8271. (let* ((p org-store-link-plist)
  8272. (to (plist-get p :toaddress))
  8273. (from (plist-get p :fromaddress))
  8274. (table
  8275. (list
  8276. (cons "%c" (plist-get p :fromto))
  8277. (cons "%F" (plist-get p :from))
  8278. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8279. (cons "%T" (plist-get p :to))
  8280. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8281. (cons "%s" (plist-get p :subject))
  8282. (cons "%d" (plist-get p :date))
  8283. (cons "%m" (plist-get p :message-id)))))
  8284. (when (string-match "%c" fmt)
  8285. ;; Check if the user wrote this message
  8286. (if (and org-from-is-user-regexp from to
  8287. (save-match-data (string-match org-from-is-user-regexp from)))
  8288. (setq fmt (replace-match "to %t" t t fmt))
  8289. (setq fmt (replace-match "from %f" t t fmt))))
  8290. (org-replace-escapes fmt table)))
  8291. (defun org-make-org-heading-search-string (&optional string)
  8292. "Make search string for the current headline or STRING."
  8293. (let ((s (or string
  8294. (and (derived-mode-p 'org-mode)
  8295. (save-excursion
  8296. (org-back-to-heading t)
  8297. (plist-get (cadr (org-element-at-point))
  8298. :raw-value)))))
  8299. (lines org-context-in-file-links))
  8300. (or string (setq s (concat "*" s))) ; Add * for headlines
  8301. (when (and string (integerp lines) (> lines 0))
  8302. (let ((slines (org-split-string s "\n")))
  8303. (when (< lines (length slines))
  8304. (setq s (mapconcat
  8305. 'identity
  8306. (reverse (nthcdr (- (length slines) lines)
  8307. (reverse slines))) "\n")))))
  8308. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8309. (defun org-make-link-string (link &optional description)
  8310. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8311. (unless (string-match "\\S-" link)
  8312. (error "Empty link"))
  8313. (when (and description
  8314. (stringp description)
  8315. (not (string-match "\\S-" description)))
  8316. (setq description nil))
  8317. (when (stringp description)
  8318. ;; Remove brackets from the description, they are fatal.
  8319. (while (string-match "\\[" description)
  8320. (setq description (replace-match "{" t t description)))
  8321. (while (string-match "\\]" description)
  8322. (setq description (replace-match "}" t t description))))
  8323. (when (equal link description)
  8324. ;; No description needed, it is identical
  8325. (setq description nil))
  8326. (when (and (not description)
  8327. (not (string-match (org-image-file-name-regexp) link))
  8328. (not (equal link (org-link-escape link))))
  8329. (setq description (org-extract-attributes link)))
  8330. (setq link
  8331. (cond ((string-match (org-image-file-name-regexp) link) link)
  8332. ((string-match org-link-types-re link)
  8333. (concat (match-string 1 link)
  8334. (org-link-escape (substring link (match-end 1)))))
  8335. (t (org-link-escape link))))
  8336. (concat "[[" link "]"
  8337. (if description (concat "[" description "]") "")
  8338. "]"))
  8339. (defconst org-link-escape-chars
  8340. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8341. "List of characters that should be escaped in link.
  8342. This is the list that is used for internal purposes.")
  8343. (defconst org-link-escape-chars-browser
  8344. '(?\ )
  8345. "List of escapes for characters that are problematic in links.
  8346. This is the list that is used before handing over to the browser.")
  8347. (defun org-link-escape (text &optional table merge)
  8348. "Return percent escaped representation of TEXT.
  8349. TEXT is a string with the text to escape.
  8350. Optional argument TABLE is a list with characters that should be
  8351. escaped. When nil, `org-link-escape-chars' is used.
  8352. If optional argument MERGE is set, merge TABLE into
  8353. `org-link-escape-chars'."
  8354. (cond
  8355. ((and table merge)
  8356. (mapc (lambda (defchr)
  8357. (unless (member defchr table)
  8358. (setq table (cons defchr table)))) org-link-escape-chars))
  8359. ((null table)
  8360. (setq table org-link-escape-chars)))
  8361. (mapconcat
  8362. (lambda (char)
  8363. (if (or (member char table)
  8364. (and (or (< char 32) (= char 37) (> char 126))
  8365. org-url-hexify-p))
  8366. (mapconcat (lambda (sequence-element)
  8367. (format "%%%.2X" sequence-element))
  8368. (or (encode-coding-char char 'utf-8)
  8369. (error "Unable to percent escape character: %s"
  8370. (char-to-string char))) "")
  8371. (char-to-string char))) text ""))
  8372. (defun org-link-unescape (str)
  8373. "Unhex hexified Unicode strings as returned from the JavaScript function
  8374. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8375. (unless (and (null str) (string= "" str))
  8376. (let ((pos 0) (case-fold-search t) unhexed)
  8377. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8378. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8379. (setq str (replace-match unhexed t t str))
  8380. (setq pos (+ pos (length unhexed))))))
  8381. str)
  8382. (defun org-link-unescape-compound (hex)
  8383. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8384. Note: this function also decodes single byte encodings like
  8385. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8386. (save-match-data
  8387. (let* ((bytes (cdr (split-string hex "%")))
  8388. (ret "")
  8389. (eat 0)
  8390. (sum 0))
  8391. (while bytes
  8392. (let* ((val (string-to-number (pop bytes) 16))
  8393. (shift-xor
  8394. (if (= 0 eat)
  8395. (cond
  8396. ((>= val 252) (cons 6 252))
  8397. ((>= val 248) (cons 5 248))
  8398. ((>= val 240) (cons 4 240))
  8399. ((>= val 224) (cons 3 224))
  8400. ((>= val 192) (cons 2 192))
  8401. (t (cons 0 0)))
  8402. (cons 6 128))))
  8403. (if (>= val 192) (setq eat (car shift-xor)))
  8404. (setq val (logxor val (cdr shift-xor)))
  8405. (setq sum (+ (lsh sum (car shift-xor)) val))
  8406. (if (> eat 0) (setq eat (- eat 1)))
  8407. (cond
  8408. ((= 0 eat) ;multi byte
  8409. (setq ret (concat ret (org-char-to-string sum)))
  8410. (setq sum 0))
  8411. ((not bytes) ; single byte(s)
  8412. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8413. )) ;; end (while bytes
  8414. ret )))
  8415. (defun org-link-unescape-single-byte-sequence (hex)
  8416. "Unhexify hex-encoded single byte character sequences."
  8417. (mapconcat (lambda (byte)
  8418. (char-to-string (string-to-number byte 16)))
  8419. (cdr (split-string hex "%")) ""))
  8420. (defun org-xor (a b)
  8421. "Exclusive or."
  8422. (if a (not b) b))
  8423. (defun org-fixup-message-id-for-http (s)
  8424. "Replace special characters in a message id, so it can be used in an http query."
  8425. (when (string-match "%" s)
  8426. (setq s (mapconcat (lambda (c)
  8427. (if (eq c ?%)
  8428. "%25"
  8429. (char-to-string c)))
  8430. s "")))
  8431. (while (string-match "<" s)
  8432. (setq s (replace-match "%3C" t t s)))
  8433. (while (string-match ">" s)
  8434. (setq s (replace-match "%3E" t t s)))
  8435. (while (string-match "@" s)
  8436. (setq s (replace-match "%40" t t s)))
  8437. s)
  8438. (defun org-link-prettify (link)
  8439. "Return a human-readable representation of LINK.
  8440. The car of LINK must be a raw link the cdr of LINK must be either
  8441. a link description or nil."
  8442. (let ((desc (or (cadr link) "<no description>")))
  8443. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8444. "<" (car link) ">")))
  8445. ;;;###autoload
  8446. (defun org-insert-link-global ()
  8447. "Insert a link like Org-mode does.
  8448. This command can be called in any mode to insert a link in Org-mode syntax."
  8449. (interactive)
  8450. (org-load-modules-maybe)
  8451. (org-run-like-in-org-mode 'org-insert-link))
  8452. (defun org-insert-all-links (&optional keep)
  8453. "Insert all links in `org-stored-links'."
  8454. (interactive "P")
  8455. (let ((links (copy-sequence org-stored-links)) l)
  8456. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8457. (insert "- ")
  8458. (org-insert-link nil (car l) (cadr l))
  8459. (insert "\n"))))
  8460. (defun org-link-fontify-links-to-this-file ()
  8461. "Fontify links to the current file in `org-stored-links'."
  8462. (let ((f (buffer-file-name)) a b)
  8463. (setq a (mapcar (lambda(l)
  8464. (let ((ll (car l)))
  8465. (when (and (string-match "^file:\\(.+\\)::" ll)
  8466. (equal f (expand-file-name (match-string 1 ll))))
  8467. ll)))
  8468. org-stored-links))
  8469. (when (featurep 'org-id)
  8470. (setq b (mapcar (lambda(l)
  8471. (let ((ll (car l)))
  8472. (when (and (string-match "^id:\\(.+\\)$" ll)
  8473. (equal f (expand-file-name
  8474. (or (org-id-find-id-file
  8475. (match-string 1 ll)) ""))))
  8476. ll)))
  8477. org-stored-links)))
  8478. (mapcar (lambda(l)
  8479. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8480. (delq nil (append a b)))))
  8481. (defvar org-link-links-in-this-file nil)
  8482. (defun org-insert-link (&optional complete-file link-location default-description)
  8483. "Insert a link. At the prompt, enter the link.
  8484. Completion can be used to insert any of the link protocol prefixes like
  8485. http or ftp in use.
  8486. The history can be used to select a link previously stored with
  8487. `org-store-link'. When the empty string is entered (i.e. if you just
  8488. press RET at the prompt), the link defaults to the most recently
  8489. stored link. As SPC triggers completion in the minibuffer, you need to
  8490. use M-SPC or C-q SPC to force the insertion of a space character.
  8491. You will also be prompted for a description, and if one is given, it will
  8492. be displayed in the buffer instead of the link.
  8493. If there is already a link at point, this command will allow you to edit link
  8494. and description parts.
  8495. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8496. be selected using completion. The path to the file will be relative to the
  8497. current directory if the file is in the current directory or a subdirectory.
  8498. Otherwise, the link will be the absolute path as completed in the minibuffer
  8499. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8500. option `org-link-file-path-type'.
  8501. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8502. the current directory or below.
  8503. With three \\[universal-argument] prefixes, negate the meaning of
  8504. `org-keep-stored-link-after-insertion'.
  8505. If `org-make-link-description-function' is non-nil, this function will be
  8506. called with the link target, and the result will be the default
  8507. link description.
  8508. If the LINK-LOCATION parameter is non-nil, this value will be
  8509. used as the link location instead of reading one interactively.
  8510. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8511. be used as the default description."
  8512. (interactive "P")
  8513. (let* ((wcf (current-window-configuration))
  8514. (origbuf (current-buffer))
  8515. (region (if (org-region-active-p)
  8516. (buffer-substring (region-beginning) (region-end))))
  8517. (remove (and region (list (region-beginning) (region-end))))
  8518. (desc region)
  8519. tmphist ; byte-compile incorrectly complains about this
  8520. (link link-location)
  8521. (abbrevs org-link-abbrev-alist-local)
  8522. entry file all-prefixes auto-desc)
  8523. (cond
  8524. (link-location) ; specified by arg, just use it.
  8525. ((org-in-regexp org-bracket-link-regexp 1)
  8526. ;; We do have a link at point, and we are going to edit it.
  8527. (setq remove (list (match-beginning 0) (match-end 0)))
  8528. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8529. (setq link (read-string "Link: "
  8530. (org-link-unescape
  8531. (org-match-string-no-properties 1)))))
  8532. ((or (org-in-regexp org-angle-link-re)
  8533. (org-in-regexp org-plain-link-re))
  8534. ;; Convert to bracket link
  8535. (setq remove (list (match-beginning 0) (match-end 0))
  8536. link (read-string "Link: "
  8537. (org-remove-angle-brackets (match-string 0)))))
  8538. ((member complete-file '((4) (16)))
  8539. ;; Completing read for file names.
  8540. (setq link (org-file-complete-link complete-file)))
  8541. (t
  8542. ;; Read link, with completion for stored links.
  8543. (org-link-fontify-links-to-this-file)
  8544. (org-switch-to-buffer-other-window "*Org Links*")
  8545. (with-current-buffer "*Org Links*"
  8546. (erase-buffer)
  8547. (insert "Insert a link.
  8548. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8549. (when org-stored-links
  8550. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8551. (insert (mapconcat 'org-link-prettify
  8552. (reverse org-stored-links) "\n")))
  8553. (goto-char (point-min)))
  8554. (let ((cw (selected-window)))
  8555. (select-window (get-buffer-window "*Org Links*" 'visible))
  8556. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8557. (unless (pos-visible-in-window-p (point-max))
  8558. (org-fit-window-to-buffer))
  8559. (and (window-live-p cw) (select-window cw)))
  8560. ;; Fake a link history, containing the stored links.
  8561. (setq tmphist (append (mapcar 'car org-stored-links)
  8562. org-insert-link-history))
  8563. (setq all-prefixes (append (mapcar 'car abbrevs)
  8564. (mapcar 'car org-link-abbrev-alist)
  8565. org-link-types))
  8566. (unwind-protect
  8567. (progn
  8568. (setq link
  8569. (org-completing-read
  8570. "Link: "
  8571. (append
  8572. (mapcar (lambda (x) (concat x ":"))
  8573. all-prefixes)
  8574. (mapcar 'car org-stored-links))
  8575. nil nil nil
  8576. 'tmphist
  8577. (caar org-stored-links)))
  8578. (if (not (string-match "\\S-" link))
  8579. (error "No link selected"))
  8580. (mapc (lambda(l)
  8581. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8582. org-stored-links)
  8583. (if (or (member link all-prefixes)
  8584. (and (equal ":" (substring link -1))
  8585. (member (substring link 0 -1) all-prefixes)
  8586. (setq link (substring link 0 -1))))
  8587. (setq link (with-current-buffer origbuf
  8588. (org-link-try-special-completion link)))))
  8589. (set-window-configuration wcf)
  8590. (kill-buffer "*Org Links*"))
  8591. (setq entry (assoc link org-stored-links))
  8592. (or entry (push link org-insert-link-history))
  8593. (setq desc (or desc (nth 1 entry)))))
  8594. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8595. (not org-keep-stored-link-after-insertion))
  8596. (setq org-stored-links (delq (assoc link org-stored-links)
  8597. org-stored-links)))
  8598. (if (string-match org-plain-link-re link)
  8599. ;; URL-like link, normalize the use of angular brackets.
  8600. (setq link (org-remove-angle-brackets link)))
  8601. ;; Check if we are linking to the current file with a search
  8602. ;; option If yes, simplify the link by using only the search
  8603. ;; option.
  8604. (when (and buffer-file-name
  8605. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8606. (let* ((path (match-string 1 link))
  8607. (case-fold-search nil)
  8608. (search (match-string 2 link)))
  8609. (save-match-data
  8610. (if (equal (file-truename buffer-file-name) (file-truename path))
  8611. ;; We are linking to this same file, with a search option
  8612. (setq link search)))))
  8613. ;; Check if we can/should use a relative path. If yes, simplify the link
  8614. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8615. (let* ((type (match-string 1 link))
  8616. (path (match-string 2 link))
  8617. (origpath path)
  8618. (case-fold-search nil))
  8619. (cond
  8620. ((or (eq org-link-file-path-type 'absolute)
  8621. (equal complete-file '(16)))
  8622. (setq path (abbreviate-file-name (expand-file-name path))))
  8623. ((eq org-link-file-path-type 'noabbrev)
  8624. (setq path (expand-file-name path)))
  8625. ((eq org-link-file-path-type 'relative)
  8626. (setq path (file-relative-name path)))
  8627. (t
  8628. (save-match-data
  8629. (if (string-match (concat "^" (regexp-quote
  8630. (expand-file-name
  8631. (file-name-as-directory
  8632. default-directory))))
  8633. (expand-file-name path))
  8634. ;; We are linking a file with relative path name.
  8635. (setq path (substring (expand-file-name path)
  8636. (match-end 0)))
  8637. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8638. (setq link (concat type path))
  8639. (if (equal desc origpath)
  8640. (setq desc path))))
  8641. (if org-make-link-description-function
  8642. (setq desc
  8643. (or (condition-case nil
  8644. (funcall org-make-link-description-function link desc)
  8645. (error (progn (message "Can't get link description from `%s'"
  8646. (symbol-name org-make-link-description-function))
  8647. (sit-for 2) nil)))
  8648. (read-string "Description: " default-description)))
  8649. (if default-description (setq desc default-description)
  8650. (setq desc (or (and auto-desc desc)
  8651. (read-string "Description: " desc)))))
  8652. (unless (string-match "\\S-" desc) (setq desc nil))
  8653. (if remove (apply 'delete-region remove))
  8654. (insert (org-make-link-string link desc))))
  8655. (defun org-link-try-special-completion (type)
  8656. "If there is completion support for link type TYPE, offer it."
  8657. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8658. (if (functionp fun)
  8659. (funcall fun)
  8660. (read-string "Link (no completion support): " (concat type ":")))))
  8661. (defun org-file-complete-link (&optional arg)
  8662. "Create a file link using completion."
  8663. (let (file link)
  8664. (setq file (org-iread-file-name "File: "))
  8665. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8666. (pwd1 (file-name-as-directory (abbreviate-file-name
  8667. (expand-file-name ".")))))
  8668. (cond
  8669. ((equal arg '(16))
  8670. (setq link (concat
  8671. "file:"
  8672. (abbreviate-file-name (expand-file-name file)))))
  8673. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8674. (setq link (concat "file:" (match-string 1 file))))
  8675. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8676. (expand-file-name file))
  8677. (setq link (concat
  8678. "file:" (match-string 1 (expand-file-name file)))))
  8679. (t (setq link (concat "file:" file)))))
  8680. link))
  8681. (defun org-iread-file-name (&rest args)
  8682. "Read-file-name using `ido-mode' speedup if available.
  8683. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8684. See `read-file-name' for a description of parameters."
  8685. (org-without-partial-completion
  8686. (if (and org-completion-use-ido
  8687. (fboundp 'ido-read-file-name)
  8688. (boundp 'ido-mode) ido-mode
  8689. (listp (second args)))
  8690. (let ((ido-enter-matching-directory nil))
  8691. (apply 'ido-read-file-name args))
  8692. (apply 'read-file-name args))))
  8693. (defun org-completing-read (&rest args)
  8694. "Completing-read with SPACE being a normal character."
  8695. (let ((enable-recursive-minibuffers t)
  8696. (minibuffer-local-completion-map
  8697. (copy-keymap minibuffer-local-completion-map)))
  8698. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8699. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8700. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8701. (apply 'org-icompleting-read args)))
  8702. (defun org-completing-read-no-i (&rest args)
  8703. (let (org-completion-use-ido org-completion-use-iswitchb)
  8704. (apply 'org-completing-read args)))
  8705. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8706. "Use iswitch as a completing-read replacement to choose from choices.
  8707. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8708. from."
  8709. (let* ((iswitchb-use-virtual-buffers nil)
  8710. (iswitchb-make-buflist-hook
  8711. (lambda ()
  8712. (setq iswitchb-temp-buflist choices))))
  8713. (iswitchb-read-buffer prompt)))
  8714. (defun org-icompleting-read (&rest args)
  8715. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8716. (org-without-partial-completion
  8717. (if (and org-completion-use-ido
  8718. (fboundp 'ido-completing-read)
  8719. (boundp 'ido-mode) ido-mode
  8720. (listp (second args)))
  8721. (let ((ido-enter-matching-directory nil))
  8722. (apply 'ido-completing-read (concat (car args))
  8723. (if (consp (car (nth 1 args)))
  8724. (mapcar 'car (nth 1 args))
  8725. (nth 1 args))
  8726. (cddr args)))
  8727. (if (and org-completion-use-iswitchb
  8728. (boundp 'iswitchb-mode) iswitchb-mode
  8729. (listp (second args)))
  8730. (apply 'org-iswitchb-completing-read (concat (car args))
  8731. (if (consp (car (nth 1 args)))
  8732. (mapcar 'car (nth 1 args))
  8733. (nth 1 args))
  8734. (cddr args))
  8735. (apply 'completing-read args)))))
  8736. (defun org-extract-attributes (s)
  8737. "Extract the attributes cookie from a string and set as text property."
  8738. (let (a attr (start 0) key value)
  8739. (save-match-data
  8740. (when (string-match "{{\\([^}]+\\)}}$" s)
  8741. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8742. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8743. (setq key (match-string 1 a) value (match-string 2 a)
  8744. start (match-end 0)
  8745. attr (plist-put attr (intern key) value))))
  8746. (org-add-props s nil 'org-attr attr))
  8747. s))
  8748. (defun org-extract-attributes-from-string (tag)
  8749. (let (key value attr)
  8750. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8751. (setq key (match-string 1 tag) value (match-string 2 tag)
  8752. tag (replace-match "" t t tag)
  8753. attr (plist-put attr (intern key) value)))
  8754. (cons tag attr)))
  8755. (defun org-attributes-to-string (plist)
  8756. "Format a property list into an HTML attribute list."
  8757. (let ((s "") key value)
  8758. (while plist
  8759. (setq key (pop plist) value (pop plist))
  8760. (and value
  8761. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8762. s))
  8763. ;;; Opening/following a link
  8764. (defvar org-link-search-failed nil)
  8765. (defvar org-open-link-functions nil
  8766. "Hook for functions finding a plain text link.
  8767. These functions must take a single argument, the link content.
  8768. They will be called for links that look like [[link text][description]]
  8769. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8770. like a filename (e.g. \"./blue.png\").
  8771. These functions will be called *before* Org attempts to resolve the
  8772. link by doing text searches in the current buffer - so if you want a
  8773. link \"[[target]]\" to still find \"<<target>>\", your function should
  8774. handle this as a special case.
  8775. When the function does handle the link, it must return a non-nil value.
  8776. If it decides that it is not responsible for this link, it must return
  8777. nil to indicate that that Org-mode can continue with other options
  8778. like exact and fuzzy text search.")
  8779. (defun org-next-link ()
  8780. "Move forward to the next link.
  8781. If the link is in hidden text, expose it."
  8782. (interactive)
  8783. (when (and org-link-search-failed (eq this-command last-command))
  8784. (goto-char (point-min))
  8785. (message "Link search wrapped back to beginning of buffer"))
  8786. (setq org-link-search-failed nil)
  8787. (let* ((pos (point))
  8788. (ct (org-context))
  8789. (a (assoc :link ct)))
  8790. (if a (goto-char (nth 2 a)))
  8791. (if (re-search-forward org-any-link-re nil t)
  8792. (progn
  8793. (goto-char (match-beginning 0))
  8794. (if (outline-invisible-p) (org-show-context)))
  8795. (goto-char pos)
  8796. (setq org-link-search-failed t)
  8797. (error "No further link found"))))
  8798. (defun org-previous-link ()
  8799. "Move backward to the previous link.
  8800. If the link is in hidden text, expose it."
  8801. (interactive)
  8802. (when (and org-link-search-failed (eq this-command last-command))
  8803. (goto-char (point-max))
  8804. (message "Link search wrapped back to end of buffer"))
  8805. (setq org-link-search-failed nil)
  8806. (let* ((pos (point))
  8807. (ct (org-context))
  8808. (a (assoc :link ct)))
  8809. (if a (goto-char (nth 1 a)))
  8810. (if (re-search-backward org-any-link-re nil t)
  8811. (progn
  8812. (goto-char (match-beginning 0))
  8813. (if (outline-invisible-p) (org-show-context)))
  8814. (goto-char pos)
  8815. (setq org-link-search-failed t)
  8816. (error "No further link found"))))
  8817. (defun org-translate-link (s)
  8818. "Translate a link string if a translation function has been defined."
  8819. (if (and org-link-translation-function
  8820. (fboundp org-link-translation-function)
  8821. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8822. (progn
  8823. (setq s (funcall org-link-translation-function
  8824. (match-string 1 s) (match-string 2 s)))
  8825. (concat (car s) ":" (cdr s)))
  8826. s))
  8827. (defun org-translate-link-from-planner (type path)
  8828. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8829. This is still an experimental function, your mileage may vary."
  8830. (cond
  8831. ((member type '("http" "https" "news" "ftp"))
  8832. ;; standard Internet links are the same.
  8833. nil)
  8834. ((and (equal type "irc") (string-match "^//" path))
  8835. ;; Planner has two / at the beginning of an irc link, we have 1.
  8836. ;; We should have zero, actually....
  8837. (setq path (substring path 1)))
  8838. ((and (equal type "lisp") (string-match "^/" path))
  8839. ;; Planner has a slash, we do not.
  8840. (setq type "elisp" path (substring path 1)))
  8841. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8842. ;; A typical message link. Planner has the id after the final slash,
  8843. ;; we separate it with a hash mark
  8844. (setq path (concat (match-string 1 path) "#"
  8845. (org-remove-angle-brackets (match-string 2 path)))))
  8846. )
  8847. (cons type path))
  8848. (defun org-find-file-at-mouse (ev)
  8849. "Open file link or URL at mouse."
  8850. (interactive "e")
  8851. (mouse-set-point ev)
  8852. (org-open-at-point 'in-emacs))
  8853. (defun org-open-at-mouse (ev)
  8854. "Open file link or URL at mouse.
  8855. See the docstring of `org-open-file' for details."
  8856. (interactive "e")
  8857. (mouse-set-point ev)
  8858. (if (eq major-mode 'org-agenda-mode)
  8859. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8860. (org-open-at-point))
  8861. (defvar org-window-config-before-follow-link nil
  8862. "The window configuration before following a link.
  8863. This is saved in case the need arises to restore it.")
  8864. (defvar org-open-link-marker (make-marker)
  8865. "Marker pointing to the location where `org-open-at-point; was called.")
  8866. ;;;###autoload
  8867. (defun org-open-at-point-global ()
  8868. "Follow a link like Org-mode does.
  8869. This command can be called in any mode to follow a link that has
  8870. Org-mode syntax."
  8871. (interactive)
  8872. (org-run-like-in-org-mode 'org-open-at-point))
  8873. ;;;###autoload
  8874. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8875. "Open a link in the string S, as if it was in Org-mode."
  8876. (interactive "sLink: \nP")
  8877. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8878. (with-temp-buffer
  8879. (let ((org-inhibit-startup (not reference-buffer)))
  8880. (org-mode)
  8881. (insert s)
  8882. (goto-char (point-min))
  8883. (when reference-buffer
  8884. (setq org-link-abbrev-alist-local
  8885. (with-current-buffer reference-buffer
  8886. org-link-abbrev-alist-local)))
  8887. (org-open-at-point arg reference-buffer)))))
  8888. (defvar org-open-at-point-functions nil
  8889. "Hook that is run when following a link at point.
  8890. Functions in this hook must return t if they identify and follow
  8891. a link at point. If they don't find anything interesting at point,
  8892. they must return nil.")
  8893. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8894. (defun org-open-at-point (&optional arg reference-buffer)
  8895. "Open link at or after point.
  8896. If there is no link at point, this function will search forward up to
  8897. the end of the current line.
  8898. Normally, files will be opened by an appropriate application. If the
  8899. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8900. With a double prefix argument, try to open outside of Emacs, in the
  8901. application the system uses for this file type."
  8902. (interactive "P")
  8903. ;; if in a code block, then open the block's results
  8904. (unless (call-interactively #'org-babel-open-src-block-result)
  8905. (org-load-modules-maybe)
  8906. (move-marker org-open-link-marker (point))
  8907. (setq org-window-config-before-follow-link (current-window-configuration))
  8908. (org-remove-occur-highlights nil nil t)
  8909. (cond
  8910. ((and (org-at-heading-p)
  8911. (not (org-at-timestamp-p t))
  8912. (not (org-in-regexp
  8913. (concat org-plain-link-re "\\|"
  8914. org-bracket-link-regexp "\\|"
  8915. org-angle-link-re "\\|"
  8916. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8917. (not (get-text-property (point) 'org-linked-text)))
  8918. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8919. (lk0 (car lkall))
  8920. (lk (if (stringp lk0) (list lk0) lk0))
  8921. (lkend (cdr lkall)))
  8922. (mapcar (lambda(l)
  8923. (search-forward l nil lkend)
  8924. (goto-char (match-beginning 0))
  8925. (org-open-at-point))
  8926. lk))
  8927. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8928. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8929. ((and (org-at-timestamp-p t)
  8930. (not (org-in-regexp org-bracket-link-regexp)))
  8931. (org-follow-timestamp-link))
  8932. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8933. (not (org-in-regexp org-any-link-re)))
  8934. (org-footnote-action))
  8935. (t
  8936. (let (type path link line search (pos (point)))
  8937. (catch 'match
  8938. (save-excursion
  8939. (skip-chars-forward "^]\n\r")
  8940. (when (org-in-regexp org-bracket-link-regexp 1)
  8941. (setq link (org-extract-attributes
  8942. (org-link-unescape (org-match-string-no-properties 1))))
  8943. (while (string-match " *\n *" link)
  8944. (setq link (replace-match " " t t link)))
  8945. (setq link (org-link-expand-abbrev link))
  8946. (cond
  8947. ((or (file-name-absolute-p link)
  8948. (string-match "^\\.\\.?/" link))
  8949. (setq type "file" path link))
  8950. ((string-match org-link-re-with-space3 link)
  8951. (setq type (match-string 1 link) path (match-string 2 link)))
  8952. ((string-match "^help:+\\(.+\\)" link)
  8953. (setq type "help" path (match-string 1 link)))
  8954. (t (setq type "thisfile" path link)))
  8955. (throw 'match t)))
  8956. (when (get-text-property (point) 'org-linked-text)
  8957. (setq type "thisfile"
  8958. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8959. (1+ (point)) (point))
  8960. path (buffer-substring
  8961. (or (previous-single-property-change pos 'org-linked-text)
  8962. (point-min))
  8963. (or (next-single-property-change pos 'org-linked-text)
  8964. (point-max))))
  8965. (throw 'match t))
  8966. (save-excursion
  8967. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8968. (when (or (org-in-regexp org-angle-link-re)
  8969. (and plinkpos (goto-char (car plinkpos))
  8970. (save-match-data (not (looking-back "\\[\\[")))))
  8971. (setq type (match-string 1)
  8972. path (org-link-unescape (match-string 2)))
  8973. (throw 'match t))))
  8974. (save-excursion
  8975. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8976. (setq type "tags"
  8977. path (match-string 1))
  8978. (while (string-match ":" path)
  8979. (setq path (replace-match "+" t t path)))
  8980. (throw 'match t)))
  8981. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8982. (setq type "tree-match"
  8983. path (match-string 1))
  8984. (throw 'match t)))
  8985. (unless path
  8986. (user-error "No link found"))
  8987. ;; switch back to reference buffer
  8988. ;; needed when if called in a temporary buffer through
  8989. ;; org-open-link-from-string
  8990. (with-current-buffer (or reference-buffer (current-buffer))
  8991. ;; Remove any trailing spaces in path
  8992. (if (string-match " +\\'" path)
  8993. (setq path (replace-match "" t t path)))
  8994. (if (and org-link-translation-function
  8995. (fboundp org-link-translation-function))
  8996. ;; Check if we need to translate the link
  8997. (let ((tmp (funcall org-link-translation-function type path)))
  8998. (setq type (car tmp) path (cdr tmp))))
  8999. (cond
  9000. ((assoc type org-link-protocols)
  9001. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9002. ((equal type "help")
  9003. (let ((f-or-v (intern path)))
  9004. (cond ((fboundp f-or-v)
  9005. (describe-function f-or-v))
  9006. ((boundp f-or-v)
  9007. (describe-variable f-or-v))
  9008. (t (error "Not a known function or variable")))))
  9009. ((equal type "mailto")
  9010. (let ((cmd (car org-link-mailto-program))
  9011. (args (cdr org-link-mailto-program)) args1
  9012. (address path) (subject "") a)
  9013. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9014. (setq address (match-string 1 path)
  9015. subject (org-link-escape (match-string 2 path))))
  9016. (while args
  9017. (cond
  9018. ((not (stringp (car args))) (push (pop args) args1))
  9019. (t (setq a (pop args))
  9020. (if (string-match "%a" a)
  9021. (setq a (replace-match address t t a)))
  9022. (if (string-match "%s" a)
  9023. (setq a (replace-match subject t t a)))
  9024. (push a args1))))
  9025. (apply cmd (nreverse args1))))
  9026. ((member type '("http" "https" "ftp" "news"))
  9027. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9028. (org-link-escape
  9029. path org-link-escape-chars-browser)
  9030. path))))
  9031. ((string= type "doi")
  9032. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9033. (org-link-escape
  9034. path org-link-escape-chars-browser)
  9035. path))))
  9036. ((member type '("message"))
  9037. (browse-url (concat type ":" path)))
  9038. ((string= type "tags")
  9039. (org-tags-view arg path))
  9040. ((string= type "tree-match")
  9041. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9042. ((string= type "file")
  9043. (if (string-match "::\\([0-9]+\\)\\'" path)
  9044. (setq line (string-to-number (match-string 1 path))
  9045. path (substring path 0 (match-beginning 0)))
  9046. (if (string-match "::\\(.+\\)\\'" path)
  9047. (setq search (match-string 1 path)
  9048. path (substring path 0 (match-beginning 0)))))
  9049. (if (string-match "[*?{]" (file-name-nondirectory path))
  9050. (dired path)
  9051. (org-open-file path arg line search)))
  9052. ((string= type "shell")
  9053. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9054. (cmd path))
  9055. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9056. (string-match org-confirm-shell-link-not-regexp cmd))
  9057. (not org-confirm-shell-link-function)
  9058. (funcall org-confirm-shell-link-function
  9059. (format "Execute \"%s\" in shell? "
  9060. (org-add-props cmd nil
  9061. 'face 'org-warning))))
  9062. (progn
  9063. (message "Executing %s" cmd)
  9064. (shell-command cmd buf)
  9065. (if (featurep 'midnight)
  9066. (setq clean-buffer-list-kill-buffer-names
  9067. (cons buf clean-buffer-list-kill-buffer-names))))
  9068. (error "Abort"))))
  9069. ((string= type "elisp")
  9070. (let ((cmd path))
  9071. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9072. (string-match org-confirm-elisp-link-not-regexp cmd))
  9073. (not org-confirm-elisp-link-function)
  9074. (funcall org-confirm-elisp-link-function
  9075. (format "Execute \"%s\" as elisp? "
  9076. (org-add-props cmd nil
  9077. 'face 'org-warning))))
  9078. (message "%s => %s" cmd
  9079. (if (equal (string-to-char cmd) ?\()
  9080. (eval (read cmd))
  9081. (call-interactively (read cmd))))
  9082. (error "Abort"))))
  9083. ((and (string= type "thisfile")
  9084. (run-hook-with-args-until-success
  9085. 'org-open-link-functions path)))
  9086. ((string= type "thisfile")
  9087. (if arg
  9088. (switch-to-buffer-other-window
  9089. (org-get-buffer-for-internal-link (current-buffer)))
  9090. (org-mark-ring-push))
  9091. (let ((cmd `(org-link-search
  9092. ,path
  9093. ,(cond ((equal arg '(4)) ''occur)
  9094. ((equal arg '(16)) ''org-occur))
  9095. ,pos)))
  9096. (condition-case nil (let ((org-link-search-inhibit-query t))
  9097. (eval cmd))
  9098. (error (progn (widen) (eval cmd))))))
  9099. (t (browse-url-at-point)))))))
  9100. (move-marker org-open-link-marker nil)
  9101. (run-hook-with-args 'org-follow-link-hook)))
  9102. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9103. "Offer links in the current entry and return the selected link.
  9104. If there is only one link, return it.
  9105. If NTH is an integer, return the NTH link found.
  9106. If ZERO is a string, check also this string for a link, and if
  9107. there is one, return it."
  9108. (with-current-buffer buffer
  9109. (save-excursion
  9110. (save-restriction
  9111. (widen)
  9112. (goto-char marker)
  9113. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9114. "\\(" org-angle-link-re "\\)\\|"
  9115. "\\(" org-plain-link-re "\\)"))
  9116. (cnt ?0)
  9117. (in-emacs (if (integerp nth) nil nth))
  9118. have-zero end links link c)
  9119. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9120. (push (match-string 0 zero) links)
  9121. (setq cnt (1- cnt) have-zero t))
  9122. (save-excursion
  9123. (org-back-to-heading t)
  9124. (setq end (save-excursion (outline-next-heading) (point)))
  9125. (while (re-search-forward re end t)
  9126. (push (match-string 0) links))
  9127. (setq links (org-uniquify (reverse links))))
  9128. (cond
  9129. ((null links)
  9130. (message "No links"))
  9131. ((equal (length links) 1)
  9132. (setq link (car links)))
  9133. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9134. (setq link (nth (if have-zero nth (1- nth)) links)))
  9135. (t ; we have to select a link
  9136. (save-excursion
  9137. (save-window-excursion
  9138. (delete-other-windows)
  9139. (with-output-to-temp-buffer "*Select Link*"
  9140. (mapc (lambda (l)
  9141. (if (not (string-match org-bracket-link-regexp l))
  9142. (princ (format "[%c] %s\n" (incf cnt)
  9143. (org-remove-angle-brackets l)))
  9144. (if (match-end 3)
  9145. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9146. (match-string 3 l) (match-string 1 l)))
  9147. (princ (format "[%c] %s\n" (incf cnt)
  9148. (match-string 1 l))))))
  9149. links))
  9150. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9151. (message "Select link to open, RET to open all:")
  9152. (setq c (read-char-exclusive))
  9153. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9154. (when (equal c ?q) (error "Abort"))
  9155. (if (equal c ?\C-m)
  9156. (setq link links)
  9157. (setq nth (- c ?0))
  9158. (if have-zero (setq nth (1+ nth)))
  9159. (unless (and (integerp nth) (>= (length links) nth))
  9160. (error "Invalid link selection"))
  9161. (setq link (nth (1- nth) links)))))
  9162. (cons link end))))))
  9163. ;; Add special file links that specify the way of opening
  9164. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9165. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9166. (defun org-open-file-with-system (path)
  9167. "Open file at PATH using the system way of opening it."
  9168. (org-open-file path 'system))
  9169. (defun org-open-file-with-emacs (path)
  9170. "Open file at PATH in Emacs."
  9171. (org-open-file path 'emacs))
  9172. (defun org-remove-file-link-modifiers ()
  9173. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  9174. (goto-char (point-min))
  9175. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  9176. (org-if-unprotected
  9177. (replace-match "file:" t t))))
  9178. (eval-after-load "org-exp"
  9179. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  9180. 'org-remove-file-link-modifiers))
  9181. ;;; File search
  9182. (defvar org-create-file-search-functions nil
  9183. "List of functions to construct the right search string for a file link.
  9184. These functions are called in turn with point at the location to
  9185. which the link should point.
  9186. A function in the hook should first test if it would like to
  9187. handle this file type, for example by checking the `major-mode'
  9188. or the file extension. If it decides not to handle this file, it
  9189. should just return nil to give other functions a chance. If it
  9190. does handle the file, it must return the search string to be used
  9191. when following the link. The search string will be part of the
  9192. file link, given after a double colon, and `org-open-at-point'
  9193. will automatically search for it. If special measures must be
  9194. taken to make the search successful, another function should be
  9195. added to the companion hook `org-execute-file-search-functions',
  9196. which see.
  9197. A function in this hook may also use `setq' to set the variable
  9198. `description' to provide a suggestion for the descriptive text to
  9199. be used for this link when it gets inserted into an Org-mode
  9200. buffer with \\[org-insert-link].")
  9201. (defvar org-execute-file-search-functions nil
  9202. "List of functions to execute a file search triggered by a link.
  9203. Functions added to this hook must accept a single argument, the
  9204. search string that was part of the file link, the part after the
  9205. double colon. The function must first check if it would like to
  9206. handle this search, for example by checking the `major-mode' or
  9207. the file extension. If it decides not to handle this search, it
  9208. should just return nil to give other functions a chance. If it
  9209. does handle the search, it must return a non-nil value to keep
  9210. other functions from trying.
  9211. Each function can access the current prefix argument through the
  9212. variable `current-prefix-argument'. Note that a single prefix is
  9213. used to force opening a link in Emacs, so it may be good to only
  9214. use a numeric or double prefix to guide the search function.
  9215. In case this is needed, a function in this hook can also restore
  9216. the window configuration before `org-open-at-point' was called using:
  9217. (set-window-configuration org-window-config-before-follow-link)")
  9218. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9219. (defun org-link-search (s &optional type avoid-pos stealth)
  9220. "Search for a link search option.
  9221. If S is surrounded by forward slashes, it is interpreted as a
  9222. regular expression. In org-mode files, this will create an `org-occur'
  9223. sparse tree. In ordinary files, `occur' will be used to list matches.
  9224. If the current buffer is in `dired-mode', grep will be used to search
  9225. in all files. If AVOID-POS is given, ignore matches near that position.
  9226. When optional argument STEALTH is non-nil, do not modify
  9227. visibility around point, thus ignoring
  9228. `org-show-hierarchy-above', `org-show-following-heading' and
  9229. `org-show-siblings' variables."
  9230. (let ((case-fold-search t)
  9231. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9232. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9233. (append '(("") (" ") ("\t") ("\n"))
  9234. org-emphasis-alist)
  9235. "\\|") "\\)"))
  9236. (pos (point))
  9237. (pre nil) (post nil)
  9238. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9239. (cond
  9240. ;; First check if there are any special search functions
  9241. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9242. ;; Now try the builtin stuff
  9243. ((and (equal (string-to-char s0) ?#)
  9244. (> (length s0) 1)
  9245. (save-excursion
  9246. (goto-char (point-min))
  9247. (and
  9248. (re-search-forward
  9249. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9250. (setq type 'dedicated
  9251. pos (match-beginning 0))))
  9252. ;; There is an exact target for this
  9253. (goto-char pos)
  9254. (org-back-to-heading t)))
  9255. ((save-excursion
  9256. (goto-char (point-min))
  9257. (and
  9258. (re-search-forward
  9259. (concat "<<" (regexp-quote s0) ">>") nil t)
  9260. (setq type 'dedicated
  9261. pos (match-beginning 0))))
  9262. ;; There is an exact target for this
  9263. (goto-char pos))
  9264. ((save-excursion
  9265. (goto-char (point-min))
  9266. (and
  9267. (re-search-forward
  9268. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9269. (setq type 'dedicated pos (match-beginning 0))))
  9270. ;; Found an invisible target.
  9271. (goto-char pos))
  9272. ((save-excursion
  9273. (goto-char (point-min))
  9274. (and
  9275. (re-search-forward
  9276. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9277. (setq type 'dedicated pos (match-beginning 0))))
  9278. ;; Found an element with a matching #+name affiliated keyword.
  9279. (goto-char pos))
  9280. ((and (string-match "^(\\(.*\\))$" s0)
  9281. (save-excursion
  9282. (goto-char (point-min))
  9283. (and
  9284. (re-search-forward
  9285. (concat "[^[]" (regexp-quote
  9286. (format org-coderef-label-format
  9287. (match-string 1 s0))))
  9288. nil t)
  9289. (setq type 'dedicated
  9290. pos (1+ (match-beginning 0))))))
  9291. ;; There is a coderef target for this
  9292. (goto-char pos))
  9293. ((string-match "^/\\(.*\\)/$" s)
  9294. ;; A regular expression
  9295. (cond
  9296. ((derived-mode-p 'org-mode)
  9297. (org-occur (match-string 1 s)))
  9298. ;;((eq major-mode 'dired-mode)
  9299. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9300. (t (org-do-occur (match-string 1 s)))))
  9301. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9302. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9303. (goto-char (point-min))
  9304. (cond
  9305. ((let (case-fold-search)
  9306. (re-search-forward (format org-complex-heading-regexp-format
  9307. (regexp-quote s))
  9308. nil t))
  9309. ;; OK, found a match
  9310. (setq type 'dedicated)
  9311. (goto-char (match-beginning 0)))
  9312. ((and (not org-link-search-inhibit-query)
  9313. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9314. (y-or-n-p "No match - create this as a new heading? "))
  9315. (goto-char (point-max))
  9316. (or (bolp) (newline))
  9317. (insert "* " s "\n")
  9318. (beginning-of-line 0))
  9319. (t
  9320. (goto-char pos)
  9321. (error "No match"))))
  9322. (t
  9323. ;; A normal search string
  9324. (when (equal (string-to-char s) ?*)
  9325. ;; Anchor on headlines, post may include tags.
  9326. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9327. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9328. s (substring s 1)))
  9329. (remove-text-properties
  9330. 0 (length s)
  9331. '(face nil mouse-face nil keymap nil fontified nil) s)
  9332. ;; Make a series of regular expressions to find a match
  9333. (setq words (org-split-string s "[ \n\r\t]+")
  9334. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9335. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9336. "\\)" markers)
  9337. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9338. re2a (concat "[ \t\r\n]" re2a_)
  9339. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9340. re4 (concat "[^a-zA-Z_]" re4_)
  9341. re1 (concat pre re2 post)
  9342. re3 (concat pre (if pre re4_ re4) post)
  9343. re5 (concat pre ".*" re4)
  9344. re2 (concat pre re2)
  9345. re2a (concat pre (if pre re2a_ re2a))
  9346. re4 (concat pre (if pre re4_ re4))
  9347. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9348. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9349. re5 "\\)"
  9350. ))
  9351. (cond
  9352. ((eq type 'org-occur) (org-occur reall))
  9353. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9354. (t (goto-char (point-min))
  9355. (setq type 'fuzzy)
  9356. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9357. (org-search-not-self 1 re1 nil t)
  9358. (org-search-not-self 1 re2 nil t)
  9359. (org-search-not-self 1 re2a nil t)
  9360. (org-search-not-self 1 re3 nil t)
  9361. (org-search-not-self 1 re4 nil t)
  9362. (org-search-not-self 1 re5 nil t)
  9363. )
  9364. (goto-char (match-beginning 1))
  9365. (goto-char pos)
  9366. (error "No match"))))))
  9367. (and (derived-mode-p 'org-mode)
  9368. (not stealth)
  9369. (org-show-context 'link-search))
  9370. type))
  9371. (defun org-search-not-self (group &rest args)
  9372. "Execute `re-search-forward', but only accept matches that do not
  9373. enclose the position of `org-open-link-marker'."
  9374. (let ((m org-open-link-marker))
  9375. (catch 'exit
  9376. (while (apply 're-search-forward args)
  9377. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9378. (goto-char (match-end group))
  9379. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9380. (> (match-beginning 0) (marker-position m))
  9381. (< (match-end 0) (marker-position m)))
  9382. (save-match-data
  9383. (or (not (org-in-regexp
  9384. org-bracket-link-analytic-regexp 1))
  9385. (not (match-end 4)) ; no description
  9386. (and (<= (match-beginning 4) (point))
  9387. (>= (match-end 4) (point))))))
  9388. (throw 'exit (point))))))))
  9389. (defun org-get-buffer-for-internal-link (buffer)
  9390. "Return a buffer to be used for displaying the link target of internal links."
  9391. (cond
  9392. ((not org-display-internal-link-with-indirect-buffer)
  9393. buffer)
  9394. ((string-match "(Clone)$" (buffer-name buffer))
  9395. (message "Buffer is already a clone, not making another one")
  9396. ;; we also do not modify visibility in this case
  9397. buffer)
  9398. (t ; make a new indirect buffer for displaying the link
  9399. (let* ((bn (buffer-name buffer))
  9400. (ibn (concat bn "(Clone)"))
  9401. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9402. (with-current-buffer ib (org-overview))
  9403. ib))))
  9404. (defun org-do-occur (regexp &optional cleanup)
  9405. "Call the Emacs command `occur'.
  9406. If CLEANUP is non-nil, remove the printout of the regular expression
  9407. in the *Occur* buffer. This is useful if the regex is long and not useful
  9408. to read."
  9409. (occur regexp)
  9410. (when cleanup
  9411. (let ((cwin (selected-window)) win beg end)
  9412. (when (setq win (get-buffer-window "*Occur*"))
  9413. (select-window win))
  9414. (goto-char (point-min))
  9415. (when (re-search-forward "match[a-z]+" nil t)
  9416. (setq beg (match-end 0))
  9417. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9418. (setq end (1- (match-beginning 0)))))
  9419. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9420. (goto-char (point-min))
  9421. (select-window cwin))))
  9422. ;;; The mark ring for links jumps
  9423. (defvar org-mark-ring nil
  9424. "Mark ring for positions before jumps in Org-mode.")
  9425. (defvar org-mark-ring-last-goto nil
  9426. "Last position in the mark ring used to go back.")
  9427. ;; Fill and close the ring
  9428. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9429. (loop for i from 1 to org-mark-ring-length do
  9430. (push (make-marker) org-mark-ring))
  9431. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9432. org-mark-ring)
  9433. (defun org-mark-ring-push (&optional pos buffer)
  9434. "Put the current position or POS into the mark ring and rotate it."
  9435. (interactive)
  9436. (setq pos (or pos (point)))
  9437. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9438. (move-marker (car org-mark-ring)
  9439. (or pos (point))
  9440. (or buffer (current-buffer)))
  9441. (message "%s"
  9442. (substitute-command-keys
  9443. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9444. (defun org-mark-ring-goto (&optional n)
  9445. "Jump to the previous position in the mark ring.
  9446. With prefix arg N, jump back that many stored positions. When
  9447. called several times in succession, walk through the entire ring.
  9448. Org-mode commands jumping to a different position in the current file,
  9449. or to another Org-mode file, automatically push the old position
  9450. onto the ring."
  9451. (interactive "p")
  9452. (let (p m)
  9453. (if (eq last-command this-command)
  9454. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9455. (setq p org-mark-ring))
  9456. (setq org-mark-ring-last-goto p)
  9457. (setq m (car p))
  9458. (org-pop-to-buffer-same-window (marker-buffer m))
  9459. (goto-char m)
  9460. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9461. (defun org-remove-angle-brackets (s)
  9462. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9463. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9464. s)
  9465. (defun org-add-angle-brackets (s)
  9466. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9467. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9468. s)
  9469. (defun org-remove-double-quotes (s)
  9470. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9471. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9472. s)
  9473. ;;; Following specific links
  9474. (defun org-follow-timestamp-link ()
  9475. "Open an agenda view for the time-stamp date/range at point."
  9476. (cond
  9477. ((org-at-date-range-p t)
  9478. (let ((org-agenda-start-on-weekday)
  9479. (t1 (match-string 1))
  9480. (t2 (match-string 2)) tt1 tt2)
  9481. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9482. tt2 (time-to-days (org-time-string-to-time t2)))
  9483. (let ((org-agenda-buffer-tmp-name
  9484. (format "*Org Agenda(a:%s)"
  9485. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9486. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9487. ((org-at-timestamp-p t)
  9488. (let ((org-agenda-buffer-tmp-name
  9489. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9490. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9491. (substring (match-string 1) 0 10)))
  9492. 1)))
  9493. (t (error "This should not happen"))))
  9494. ;;; Following file links
  9495. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9496. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9497. (declare-function mailcap-mime-info
  9498. "mailcap" (string &optional request no-decode))
  9499. (defvar org-wait nil)
  9500. (defun org-open-file (path &optional in-emacs line search)
  9501. "Open the file at PATH.
  9502. First, this expands any special file name abbreviations. Then the
  9503. configuration variable `org-file-apps' is checked if it contains an
  9504. entry for this file type, and if yes, the corresponding command is launched.
  9505. If no application is found, Emacs simply visits the file.
  9506. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9507. With a double \\[universal-argument] \\[universal-argument] \
  9508. prefix arg, Org tries to avoid opening in Emacs
  9509. and to use an external application to visit the file.
  9510. Optional LINE specifies a line to go to, optional SEARCH a string
  9511. to search for. If LINE or SEARCH is given, the file will be
  9512. opened in Emacs, unless an entry from org-file-apps that makes
  9513. use of groups in a regexp matches.
  9514. If you want to change the way frames are used when following a
  9515. link, please customize `org-link-frame-setup'.
  9516. If the file does not exist, an error is thrown."
  9517. (let* ((file (if (equal path "")
  9518. buffer-file-name
  9519. (substitute-in-file-name (expand-file-name path))))
  9520. (file-apps (append org-file-apps (org-default-apps)))
  9521. (apps (org-remove-if
  9522. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9523. (apps-dlink (org-remove-if-not
  9524. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9525. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9526. (dirp (if remp nil (file-directory-p file)))
  9527. (file (if (and dirp org-open-directory-means-index-dot-org)
  9528. (concat (file-name-as-directory file) "index.org")
  9529. file))
  9530. (a-m-a-p (assq 'auto-mode apps))
  9531. (dfile (downcase file))
  9532. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9533. (link (cond ((and (eq line nil)
  9534. (eq search nil))
  9535. file)
  9536. (line
  9537. (concat file "::" (number-to-string line)))
  9538. (search
  9539. (concat file "::" search))))
  9540. (dlink (downcase link))
  9541. (old-buffer (current-buffer))
  9542. (old-pos (point))
  9543. (old-mode major-mode)
  9544. ext cmd link-match-data)
  9545. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9546. (setq ext (match-string 1 dfile))
  9547. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9548. (setq ext (match-string 1 dfile))))
  9549. (cond
  9550. ((member in-emacs '((16) system))
  9551. (setq cmd (cdr (assoc 'system apps))))
  9552. (in-emacs (setq cmd 'emacs))
  9553. (t
  9554. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9555. (and dirp (cdr (assoc 'directory apps)))
  9556. ; first, try matching against apps-dlink
  9557. ; if we get a match here, store the match data for later
  9558. (let ((match (assoc-default dlink apps-dlink
  9559. 'string-match)))
  9560. (if match
  9561. (progn (setq link-match-data (match-data))
  9562. match)
  9563. (progn (setq in-emacs (or in-emacs line search))
  9564. nil))) ; if we have no match in apps-dlink,
  9565. ; always open the file in emacs if line or search
  9566. ; is given (for backwards compatibility)
  9567. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9568. 'string-match)
  9569. (cdr (assoc ext apps))
  9570. (cdr (assoc t apps))))))
  9571. (when (eq cmd 'system)
  9572. (setq cmd (cdr (assoc 'system apps))))
  9573. (when (eq cmd 'default)
  9574. (setq cmd (cdr (assoc t apps))))
  9575. (when (eq cmd 'mailcap)
  9576. (require 'mailcap)
  9577. (mailcap-parse-mailcaps)
  9578. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9579. (command (mailcap-mime-info mime-type)))
  9580. (if (stringp command)
  9581. (setq cmd command)
  9582. (setq cmd 'emacs))))
  9583. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9584. (not (file-exists-p file))
  9585. (not org-open-non-existing-files))
  9586. (error "No such file: %s" file))
  9587. (cond
  9588. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9589. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9590. (while (string-match "['\"]%s['\"]" cmd)
  9591. (setq cmd (replace-match "%s" t t cmd)))
  9592. (while (string-match "%s" cmd)
  9593. (setq cmd (replace-match
  9594. (save-match-data
  9595. (shell-quote-argument
  9596. (convert-standard-filename file)))
  9597. t t cmd)))
  9598. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9599. (save-match-data
  9600. (let ((match-index 1)
  9601. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9602. (set-match-data link-match-data)
  9603. (while (<= match-index number-of-groups)
  9604. (let ((regex (concat "%" (number-to-string match-index)))
  9605. (replace-with (match-string match-index dlink)))
  9606. (while (string-match regex cmd)
  9607. (setq cmd (replace-match replace-with t t cmd))))
  9608. (setq match-index (+ match-index 1)))))
  9609. (save-window-excursion
  9610. (message "Running %s...done" cmd)
  9611. (start-process-shell-command cmd nil cmd)
  9612. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9613. ((or (stringp cmd)
  9614. (eq cmd 'emacs))
  9615. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9616. (widen)
  9617. (if line (org-goto-line line)
  9618. (if search (org-link-search search))))
  9619. ((consp cmd)
  9620. (let ((file (convert-standard-filename file)))
  9621. (save-match-data
  9622. (set-match-data link-match-data)
  9623. (eval cmd))))
  9624. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9625. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9626. (or (not (equal old-buffer (current-buffer)))
  9627. (not (equal old-pos (point))))
  9628. (org-mark-ring-push old-pos old-buffer))))
  9629. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9630. "This function returns non-nil if `entry' uses a regular
  9631. expression which should be matched against the whole link by
  9632. org-open-file.
  9633. It assumes that is the case when the entry uses a regular
  9634. expression which has at least one grouping construct and the
  9635. action is either a lisp form or a command string containing
  9636. '%1', i.e. using at least one subexpression match as a
  9637. parameter."
  9638. (let ((selector (car entry))
  9639. (action (cdr entry)))
  9640. (if (stringp selector)
  9641. (and (> (regexp-opt-depth selector) 0)
  9642. (or (and (stringp action)
  9643. (string-match "%[0-9]" action))
  9644. (consp action)))
  9645. nil)))
  9646. (defun org-default-apps ()
  9647. "Return the default applications for this operating system."
  9648. (cond
  9649. ((eq system-type 'darwin)
  9650. org-file-apps-defaults-macosx)
  9651. ((eq system-type 'windows-nt)
  9652. org-file-apps-defaults-windowsnt)
  9653. (t org-file-apps-defaults-gnu)))
  9654. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9655. "Convert extensions to regular expressions in the cars of LIST.
  9656. Also, weed out any non-string entries, because the return value is used
  9657. only for regexp matching.
  9658. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9659. point to the symbol `emacs', indicating that the file should
  9660. be opened in Emacs."
  9661. (append
  9662. (delq nil
  9663. (mapcar (lambda (x)
  9664. (if (not (stringp (car x)))
  9665. nil
  9666. (if (string-match "\\W" (car x))
  9667. x
  9668. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9669. list))
  9670. (if add-auto-mode
  9671. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9672. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9673. (defun org-file-remote-p (file)
  9674. "Test whether FILE specifies a location on a remote system.
  9675. Return non-nil if the location is indeed remote.
  9676. For example, the filename \"/user@host:/foo\" specifies a location
  9677. on the system \"/user@host:\"."
  9678. (cond ((fboundp 'file-remote-p)
  9679. (file-remote-p file))
  9680. ((fboundp 'tramp-handle-file-remote-p)
  9681. (tramp-handle-file-remote-p file))
  9682. ((and (boundp 'ange-ftp-name-format)
  9683. (string-match (car ange-ftp-name-format) file))
  9684. t)))
  9685. ;;;; Refiling
  9686. (defun org-get-org-file ()
  9687. "Read a filename, with default directory `org-directory'."
  9688. (let ((default (or org-default-notes-file remember-data-file)))
  9689. (read-file-name (format "File name [%s]: " default)
  9690. (file-name-as-directory org-directory)
  9691. default)))
  9692. (defun org-notes-order-reversed-p ()
  9693. "Check if the current file should receive notes in reversed order."
  9694. (cond
  9695. ((not org-reverse-note-order) nil)
  9696. ((eq t org-reverse-note-order) t)
  9697. ((not (listp org-reverse-note-order)) nil)
  9698. (t (catch 'exit
  9699. (let ((all org-reverse-note-order)
  9700. entry)
  9701. (while (setq entry (pop all))
  9702. (if (string-match (car entry) buffer-file-name)
  9703. (throw 'exit (cdr entry))))
  9704. nil)))))
  9705. (defvar org-refile-target-table nil
  9706. "The list of refile targets, created by `org-refile'.")
  9707. (defvar org-agenda-new-buffers nil
  9708. "Buffers created to visit agenda files.")
  9709. (defvar org-refile-cache nil
  9710. "Cache for refile targets.")
  9711. (defvar org-refile-markers nil
  9712. "All the markers used for caching refile locations.")
  9713. (defun org-refile-marker (pos)
  9714. "Get a new refile marker, but only if caching is in use."
  9715. (if (not org-refile-use-cache)
  9716. pos
  9717. (let ((m (make-marker)))
  9718. (move-marker m pos)
  9719. (push m org-refile-markers)
  9720. m)))
  9721. (defun org-refile-cache-clear ()
  9722. "Clear the refile cache and disable all the markers."
  9723. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9724. (setq org-refile-markers nil)
  9725. (setq org-refile-cache nil)
  9726. (message "Refile cache has been cleared"))
  9727. (defun org-refile-cache-check-set (set)
  9728. "Check if all the markers in the cache still have live buffers."
  9729. (let (marker)
  9730. (catch 'exit
  9731. (while (and set (setq marker (nth 3 (pop set))))
  9732. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9733. (when (and marker (null (marker-buffer marker)))
  9734. (message "not found") (sit-for 3)
  9735. (throw 'exit nil)))
  9736. t)))
  9737. (defun org-refile-cache-put (set &rest identifiers)
  9738. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9739. (let* ((key (sha1 (prin1-to-string identifiers)))
  9740. (entry (assoc key org-refile-cache)))
  9741. (if entry
  9742. (setcdr entry set)
  9743. (push (cons key set) org-refile-cache))))
  9744. (defun org-refile-cache-get (&rest identifiers)
  9745. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9746. (cond
  9747. ((not org-refile-cache) nil)
  9748. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9749. (t
  9750. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9751. org-refile-cache))))
  9752. (and set (org-refile-cache-check-set set) set)))))
  9753. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9754. "Produce a table with refile targets."
  9755. (let ((case-fold-search nil)
  9756. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9757. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9758. targets tgs txt re files f desc descre fast-path-p level pos0)
  9759. (message "Getting targets...")
  9760. (with-current-buffer (or default-buffer (current-buffer))
  9761. (while (setq entry (pop entries))
  9762. (setq files (car entry) desc (cdr entry))
  9763. (setq fast-path-p nil)
  9764. (cond
  9765. ((null files) (setq files (list (current-buffer))))
  9766. ((eq files 'org-agenda-files)
  9767. (setq files (org-agenda-files 'unrestricted)))
  9768. ((and (symbolp files) (fboundp files))
  9769. (setq files (funcall files)))
  9770. ((and (symbolp files) (boundp files))
  9771. (setq files (symbol-value files))))
  9772. (if (stringp files) (setq files (list files)))
  9773. (cond
  9774. ((eq (car desc) :tag)
  9775. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9776. ((eq (car desc) :todo)
  9777. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9778. ((eq (car desc) :regexp)
  9779. (setq descre (cdr desc)))
  9780. ((eq (car desc) :level)
  9781. (setq descre (concat "^\\*\\{" (number-to-string
  9782. (if org-odd-levels-only
  9783. (1- (* 2 (cdr desc)))
  9784. (cdr desc)))
  9785. "\\}[ \t]")))
  9786. ((eq (car desc) :maxlevel)
  9787. (setq fast-path-p t)
  9788. (setq descre (concat "^\\*\\{1," (number-to-string
  9789. (if org-odd-levels-only
  9790. (1- (* 2 (cdr desc)))
  9791. (cdr desc)))
  9792. "\\}[ \t]")))
  9793. (t (error "Bad refiling target description %s" desc)))
  9794. (while (setq f (pop files))
  9795. (with-current-buffer
  9796. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9797. (or
  9798. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9799. (progn
  9800. (if (bufferp f) (setq f (buffer-file-name
  9801. (buffer-base-buffer f))))
  9802. (setq f (and f (expand-file-name f)))
  9803. (if (eq org-refile-use-outline-path 'file)
  9804. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9805. (save-excursion
  9806. (save-restriction
  9807. (widen)
  9808. (goto-char (point-min))
  9809. (while (re-search-forward descre nil t)
  9810. (goto-char (setq pos0 (point-at-bol)))
  9811. (catch 'next
  9812. (when org-refile-target-verify-function
  9813. (save-match-data
  9814. (or (funcall org-refile-target-verify-function)
  9815. (throw 'next t))))
  9816. (when (and (looking-at org-complex-heading-regexp)
  9817. (not (member (match-string 4) excluded-entries))
  9818. (match-string 4))
  9819. (setq level (org-reduced-level
  9820. (- (match-end 1) (match-beginning 1)))
  9821. txt (org-link-display-format (match-string 4))
  9822. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9823. re (format org-complex-heading-regexp-format
  9824. (regexp-quote (match-string 4))))
  9825. (when org-refile-use-outline-path
  9826. (setq txt (mapconcat
  9827. 'org-protect-slash
  9828. (append
  9829. (if (eq org-refile-use-outline-path
  9830. 'file)
  9831. (list (file-name-nondirectory
  9832. (buffer-file-name
  9833. (buffer-base-buffer))))
  9834. (if (eq org-refile-use-outline-path
  9835. 'full-file-path)
  9836. (list (buffer-file-name
  9837. (buffer-base-buffer)))))
  9838. (org-get-outline-path fast-path-p
  9839. level txt)
  9840. (list txt))
  9841. "/")))
  9842. (push (list txt f re (org-refile-marker (point)))
  9843. tgs)))
  9844. (when (= (point) pos0)
  9845. ;; verification function has not moved point
  9846. (goto-char (point-at-eol))))))))
  9847. (when org-refile-use-cache
  9848. (org-refile-cache-put tgs (buffer-file-name) descre))
  9849. (setq targets (append tgs targets))
  9850. ))))
  9851. (message "Getting targets...done")
  9852. (nreverse targets)))
  9853. (defun org-protect-slash (s)
  9854. (while (string-match "/" s)
  9855. (setq s (replace-match "\\" t t s)))
  9856. s)
  9857. (defvar org-olpa (make-vector 20 nil))
  9858. (defun org-get-outline-path (&optional fastp level heading)
  9859. "Return the outline path to the current entry, as a list.
  9860. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9861. routine which makes outline path derivations for an entire file,
  9862. avoiding backtracing. Refile target collection makes use of that."
  9863. (if fastp
  9864. (progn
  9865. (if (> level 19)
  9866. (error "Outline path failure, more than 19 levels"))
  9867. (loop for i from level upto 19 do
  9868. (aset org-olpa i nil))
  9869. (prog1
  9870. (delq nil (append org-olpa nil))
  9871. (aset org-olpa level heading)))
  9872. (let (rtn case-fold-search)
  9873. (save-excursion
  9874. (save-restriction
  9875. (widen)
  9876. (while (org-up-heading-safe)
  9877. (when (looking-at org-complex-heading-regexp)
  9878. (push (org-match-string-no-properties 4) rtn)))
  9879. rtn)))))
  9880. (defun org-format-outline-path (path &optional width prefix separator)
  9881. "Format the outline path PATH for display.
  9882. WIDTH is the maximum number of characters that is available.
  9883. PREFIX is a prefix to be included in the returned string,
  9884. such as the file name.
  9885. SEPARATOR is inserted between the different parts of the path,
  9886. the default is \"/\"."
  9887. (setq width (or width 79))
  9888. (if prefix (setq width (- width (length prefix))))
  9889. (if (not path)
  9890. (or prefix "")
  9891. (let* ((nsteps (length path))
  9892. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9893. (maxwidth (if (<= total-width width)
  9894. 10000 ;; everything fits
  9895. ;; we need to shorten the level headings
  9896. (/ (- width nsteps) nsteps)))
  9897. (org-odd-levels-only nil)
  9898. (n 0)
  9899. (total (1+ (length prefix))))
  9900. (setq maxwidth (max maxwidth 10))
  9901. (concat prefix
  9902. (if prefix (or separator "/"))
  9903. (mapconcat
  9904. (lambda (h)
  9905. (setq n (1+ n))
  9906. (if (and (= n nsteps) (< maxwidth 10000))
  9907. (setq maxwidth (- total-width total)))
  9908. (if (< (length h) maxwidth)
  9909. (progn (setq total (+ total (length h) 1)) h)
  9910. (setq h (substring h 0 (- maxwidth 2))
  9911. total (+ total maxwidth 1))
  9912. (if (string-match "[ \t]+\\'" h)
  9913. (setq h (substring h 0 (match-beginning 0))))
  9914. (setq h (concat h "..")))
  9915. (org-add-props h nil 'face
  9916. (nth (% (1- n) org-n-level-faces)
  9917. org-level-faces))
  9918. h)
  9919. path (or separator "/"))))))
  9920. (defun org-display-outline-path (&optional file current separator just-return-string)
  9921. "Display the current outline path in the echo area.
  9922. If FILE is non-nil, prepend the output with the file name.
  9923. If CURRENT is non-nil, append the current heading to the output.
  9924. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9925. the different parts of the path and defaults to \"/\".
  9926. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9927. (interactive "P")
  9928. (let* (case-fold-search
  9929. message-log-max ; Don't populate the *Messages* buffer
  9930. (bfn (buffer-file-name (buffer-base-buffer)))
  9931. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9932. res)
  9933. (if current (setq path (append path
  9934. (save-excursion
  9935. (org-back-to-heading t)
  9936. (if (looking-at org-complex-heading-regexp)
  9937. (list (match-string 4)))))))
  9938. (setq res
  9939. (org-format-outline-path
  9940. path
  9941. (1- (frame-width))
  9942. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9943. separator))
  9944. (if just-return-string
  9945. (org-no-properties res)
  9946. (message "%s" res))))
  9947. (defvar org-refile-history nil
  9948. "History for refiling operations.")
  9949. (defvar org-after-refile-insert-hook nil
  9950. "Hook run after `org-refile' has inserted its stuff at the new location.
  9951. Note that this is still *before* the stuff will be removed from
  9952. the *old* location.")
  9953. (defvar org-capture-last-stored-marker)
  9954. (defvar org-refile-keep nil
  9955. "Non-nil means `org-refile' will copy instead of refile.")
  9956. (defun org-copy ()
  9957. "Like `org-refile', but copy."
  9958. (interactive)
  9959. (let ((org-refile-keep t))
  9960. (funcall 'org-refile nil nil nil "Copy")))
  9961. (defun org-refile (&optional goto default-buffer rfloc msg)
  9962. "Move the entry or entries at point to another heading.
  9963. The list of target headings is compiled using the information in
  9964. `org-refile-targets', which see.
  9965. At the target location, the entry is filed as a subitem of the target
  9966. heading. Depending on `org-reverse-note-order', the new subitem will
  9967. either be the first or the last subitem.
  9968. If there is an active region, all entries in that region will be moved.
  9969. However, the region must fulfill the requirement that the first heading
  9970. is the first one sets the top-level of the moved text - at most siblings
  9971. below it are allowed.
  9972. With prefix arg GOTO, the command will only visit the target location
  9973. and not actually move anything.
  9974. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9975. go to the location where the last refiling operation has put the subtree.
  9976. With a prefix argument of `2', refile to the running clock.
  9977. RFLOC can be a refile location obtained in a different way.
  9978. MSG is a string to replace \"Refile\" in the default prompt with
  9979. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9980. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9981. If you are using target caching (see `org-refile-use-cache'),
  9982. you have to clear the target cache in order to find new targets.
  9983. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9984. prefix argument (`C-u C-u C-u C-c C-w')."
  9985. (interactive "P")
  9986. (if (member goto '(0 (64)))
  9987. (org-refile-cache-clear)
  9988. (let* ((actionmsg (or msg "Refile"))
  9989. (cbuf (current-buffer))
  9990. (regionp (org-region-active-p))
  9991. (region-start (and regionp (region-beginning)))
  9992. (region-end (and regionp (region-end)))
  9993. (region-length (and regionp (- region-end region-start)))
  9994. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9995. pos it nbuf file re level reversed)
  9996. (setq last-command nil)
  9997. (when regionp
  9998. (goto-char region-start)
  9999. (or (bolp) (goto-char (point-at-bol)))
  10000. (setq region-start (point))
  10001. (unless (or (org-kill-is-subtree-p
  10002. (buffer-substring region-start region-end))
  10003. (prog1 org-refile-active-region-within-subtree
  10004. (org-toggle-heading)))
  10005. (error "The region is not a (sequence of) subtree(s)")))
  10006. (if (equal goto '(16))
  10007. (org-refile-goto-last-stored)
  10008. (when (or
  10009. (and (equal goto 2)
  10010. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10011. (prog1
  10012. (setq it (list (or org-clock-heading "running clock")
  10013. (buffer-file-name
  10014. (marker-buffer org-clock-hd-marker))
  10015. ""
  10016. (marker-position org-clock-hd-marker)))
  10017. (setq goto nil)))
  10018. (setq it (or rfloc
  10019. (let (heading-text)
  10020. (save-excursion
  10021. (unless goto
  10022. (org-back-to-heading t)
  10023. (setq heading-text
  10024. (nth 4 (org-heading-components))))
  10025. (org-refile-get-location
  10026. (cond (goto "Goto")
  10027. (regionp (concat actionmsg " region to"))
  10028. (t (concat actionmsg " subtree \""
  10029. heading-text "\" to")))
  10030. default-buffer
  10031. (and (not (equal '(4) goto))
  10032. org-refile-allow-creating-parent-nodes)
  10033. goto))))))
  10034. (setq file (nth 1 it)
  10035. re (nth 2 it)
  10036. pos (nth 3 it))
  10037. (if (and (not goto)
  10038. pos
  10039. (equal (buffer-file-name) file)
  10040. (if regionp
  10041. (and (>= pos region-start)
  10042. (<= pos region-end))
  10043. (and (>= pos (point))
  10044. (< pos (save-excursion
  10045. (org-end-of-subtree t t))))))
  10046. (error "Cannot refile to position inside the tree or region"))
  10047. (setq nbuf (or (find-buffer-visiting file)
  10048. (find-file-noselect file)))
  10049. (if goto
  10050. (progn
  10051. (org-pop-to-buffer-same-window nbuf)
  10052. (goto-char pos)
  10053. (org-show-context 'org-goto))
  10054. (if regionp
  10055. (progn
  10056. (org-kill-new (buffer-substring region-start region-end))
  10057. (org-save-markers-in-region region-start region-end))
  10058. (org-copy-subtree 1 nil t))
  10059. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10060. (find-file-noselect file)))
  10061. (setq reversed (org-notes-order-reversed-p))
  10062. (save-excursion
  10063. (save-restriction
  10064. (widen)
  10065. (if pos
  10066. (progn
  10067. (goto-char pos)
  10068. (looking-at org-outline-regexp)
  10069. (setq level (org-get-valid-level (funcall outline-level) 1))
  10070. (goto-char
  10071. (if reversed
  10072. (or (outline-next-heading) (point-max))
  10073. (or (save-excursion (org-get-next-sibling))
  10074. (org-end-of-subtree t t)
  10075. (point-max)))))
  10076. (setq level 1)
  10077. (if (not reversed)
  10078. (goto-char (point-max))
  10079. (goto-char (point-min))
  10080. (or (outline-next-heading) (goto-char (point-max)))))
  10081. (if (not (bolp)) (newline))
  10082. (org-paste-subtree level)
  10083. (when org-log-refile
  10084. (org-add-log-setup 'refile nil nil 'findpos
  10085. org-log-refile)
  10086. (unless (eq org-log-refile 'note)
  10087. (save-excursion (org-add-log-note))))
  10088. (and org-auto-align-tags
  10089. (let ((org-loop-over-headlines-in-active-region nil))
  10090. (org-set-tags nil t)))
  10091. (with-demoted-errors
  10092. (bookmark-set "org-refile-last-stored"))
  10093. ;; If we are refiling for capture, make sure that the
  10094. ;; last-capture pointers point here
  10095. (when (org-bound-and-true-p org-refile-for-capture)
  10096. (with-demoted-errors
  10097. (bookmark-set "org-capture-last-stored-marker"))
  10098. (move-marker org-capture-last-stored-marker (point)))
  10099. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10100. (run-hooks 'org-after-refile-insert-hook))))
  10101. (unless org-refile-keep
  10102. (if regionp
  10103. (delete-region (point) (+ (point) region-length))
  10104. (org-cut-subtree)))
  10105. (when (featurep 'org-inlinetask)
  10106. (org-inlinetask-remove-END-maybe))
  10107. (setq org-markers-to-move nil)
  10108. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10109. (defun org-refile-goto-last-stored ()
  10110. "Go to the location where the last refile was stored."
  10111. (interactive)
  10112. (bookmark-jump "org-refile-last-stored")
  10113. (message "This is the location of the last refile"))
  10114. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10115. no-exclude)
  10116. "Prompt the user for a refile location, using PROMPT.
  10117. PROMPT should not be suffixed with a colon and a space, because
  10118. this function appends the default value from
  10119. `org-refile-history' automatically, if that is not empty.
  10120. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10121. this is used for the GOTO interface."
  10122. (let ((org-refile-targets org-refile-targets)
  10123. (org-refile-use-outline-path org-refile-use-outline-path)
  10124. excluded-entries)
  10125. (when (and (derived-mode-p 'org-mode)
  10126. (not org-refile-use-cache)
  10127. (not no-exclude))
  10128. (org-map-tree
  10129. (lambda()
  10130. (setq excluded-entries
  10131. (append excluded-entries (list (org-get-heading t t)))))))
  10132. (setq org-refile-target-table
  10133. (org-refile-get-targets default-buffer excluded-entries)))
  10134. (unless org-refile-target-table
  10135. (error "No refile targets"))
  10136. (let* ((cbuf (current-buffer))
  10137. (partial-completion-mode nil)
  10138. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10139. (cfunc (if (and org-refile-use-outline-path
  10140. org-outline-path-complete-in-steps)
  10141. 'org-olpath-completing-read
  10142. 'org-icompleting-read))
  10143. (extra (if org-refile-use-outline-path "/" ""))
  10144. (cbnex (concat (buffer-name) extra))
  10145. (filename (and cfn (expand-file-name cfn)))
  10146. (tbl (mapcar
  10147. (lambda (x)
  10148. (if (and (not (member org-refile-use-outline-path
  10149. '(file full-file-path)))
  10150. (not (equal filename (nth 1 x))))
  10151. (cons (concat (car x) extra " ("
  10152. (file-name-nondirectory (nth 1 x)) ")")
  10153. (cdr x))
  10154. (cons (concat (car x) extra) (cdr x))))
  10155. org-refile-target-table))
  10156. (completion-ignore-case t)
  10157. cdef
  10158. (prompt (concat prompt
  10159. (or (and (car org-refile-history)
  10160. (concat " (default " (car org-refile-history) ")"))
  10161. (and (assoc cbnex tbl) (setq cdef cbnex)
  10162. (concat " (default " cbnex ")"))) ": "))
  10163. pa answ parent-target child parent old-hist)
  10164. (setq old-hist org-refile-history)
  10165. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10166. nil 'org-refile-history (or cdef (car org-refile-history))))
  10167. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10168. (org-refile-check-position pa)
  10169. (if pa
  10170. (progn
  10171. (when (or (not org-refile-history)
  10172. (not (eq old-hist org-refile-history))
  10173. (not (equal (car pa) (car org-refile-history))))
  10174. (setq org-refile-history
  10175. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10176. org-refile-history
  10177. (cdr org-refile-history))))
  10178. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10179. (pop org-refile-history)))
  10180. pa)
  10181. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10182. (progn
  10183. (setq parent (match-string 1 answ)
  10184. child (match-string 2 answ))
  10185. (setq parent-target (or (assoc parent tbl)
  10186. (assoc (concat parent "/") tbl)))
  10187. (when (and parent-target
  10188. (or (eq new-nodes t)
  10189. (and (eq new-nodes 'confirm)
  10190. (y-or-n-p (format "Create new node \"%s\"? "
  10191. child)))))
  10192. (org-refile-new-child parent-target child)))
  10193. (error "Invalid target location")))))
  10194. (declare-function org-string-nw-p "org-macs" (s))
  10195. (defun org-refile-check-position (refile-pointer)
  10196. "Check if the refile pointer matches the headline to which it points."
  10197. (let* ((file (nth 1 refile-pointer))
  10198. (re (nth 2 refile-pointer))
  10199. (pos (nth 3 refile-pointer))
  10200. buffer)
  10201. (if (and (not (markerp pos)) (not file))
  10202. (error "Please save the buffer to a file before refiling")
  10203. (when (org-string-nw-p re)
  10204. (setq buffer (if (markerp pos)
  10205. (marker-buffer pos)
  10206. (or (find-buffer-visiting file)
  10207. (find-file-noselect file))))
  10208. (with-current-buffer buffer
  10209. (save-excursion
  10210. (save-restriction
  10211. (widen)
  10212. (goto-char pos)
  10213. (beginning-of-line 1)
  10214. (unless (org-looking-at-p re)
  10215. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10216. (defun org-refile-new-child (parent-target child)
  10217. "Use refile target PARENT-TARGET to add new CHILD below it."
  10218. (unless parent-target
  10219. (error "Cannot find parent for new node"))
  10220. (let ((file (nth 1 parent-target))
  10221. (pos (nth 3 parent-target))
  10222. level)
  10223. (with-current-buffer (or (find-buffer-visiting file)
  10224. (find-file-noselect file))
  10225. (save-excursion
  10226. (save-restriction
  10227. (widen)
  10228. (if pos
  10229. (goto-char pos)
  10230. (goto-char (point-max))
  10231. (if (not (bolp)) (newline)))
  10232. (when (looking-at org-outline-regexp)
  10233. (setq level (funcall outline-level))
  10234. (org-end-of-subtree t t))
  10235. (org-back-over-empty-lines)
  10236. (insert "\n" (make-string
  10237. (if pos (org-get-valid-level level 1) 1) ?*)
  10238. " " child "\n")
  10239. (beginning-of-line 0)
  10240. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10241. (defun org-olpath-completing-read (prompt collection &rest args)
  10242. "Read an outline path like a file name."
  10243. (let ((thetable collection)
  10244. (org-completion-use-ido nil) ; does not work with ido.
  10245. (org-completion-use-iswitchb nil)) ; or iswitchb
  10246. (apply
  10247. 'org-icompleting-read prompt
  10248. (lambda (string predicate &optional flag)
  10249. (let (rtn r f (l (length string)))
  10250. (cond
  10251. ((eq flag nil)
  10252. ;; try completion
  10253. (try-completion string thetable))
  10254. ((eq flag t)
  10255. ;; all-completions
  10256. (setq rtn (all-completions string thetable predicate))
  10257. (mapcar
  10258. (lambda (x)
  10259. (setq r (substring x l))
  10260. (if (string-match " ([^)]*)$" x)
  10261. (setq f (match-string 0 x))
  10262. (setq f ""))
  10263. (if (string-match "/" r)
  10264. (concat string (substring r 0 (match-end 0)) f)
  10265. x))
  10266. rtn))
  10267. ((eq flag 'lambda)
  10268. ;; exact match?
  10269. (assoc string thetable)))))
  10270. args)))
  10271. ;;;; Dynamic blocks
  10272. (defun org-find-dblock (name)
  10273. "Find the first dynamic block with name NAME in the buffer.
  10274. If not found, stay at current position and return nil."
  10275. (let ((case-fold-search t) pos)
  10276. (save-excursion
  10277. (goto-char (point-min))
  10278. (setq pos (and (re-search-forward
  10279. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10280. (match-beginning 0))))
  10281. (if pos (goto-char pos))
  10282. pos))
  10283. (defconst org-dblock-start-re
  10284. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10285. "Matches the start line of a dynamic block, with parameters.")
  10286. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10287. "Matches the end of a dynamic block.")
  10288. (defun org-create-dblock (plist)
  10289. "Create a dynamic block section, with parameters taken from PLIST.
  10290. PLIST must contain a :name entry which is used as name of the block."
  10291. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10292. (end-of-line 1)
  10293. (newline))
  10294. (let ((col (current-column))
  10295. (name (plist-get plist :name)))
  10296. (insert "#+BEGIN: " name)
  10297. (while plist
  10298. (if (eq (car plist) :name)
  10299. (setq plist (cddr plist))
  10300. (insert " " (prin1-to-string (pop plist)))))
  10301. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10302. (beginning-of-line -2)))
  10303. (defun org-prepare-dblock ()
  10304. "Prepare dynamic block for refresh.
  10305. This empties the block, puts the cursor at the insert position and returns
  10306. the property list including an extra property :name with the block name."
  10307. (unless (looking-at org-dblock-start-re)
  10308. (error "Not at a dynamic block"))
  10309. (let* ((begdel (1+ (match-end 0)))
  10310. (name (org-no-properties (match-string 1)))
  10311. (params (append (list :name name)
  10312. (read (concat "(" (match-string 3) ")")))))
  10313. (save-excursion
  10314. (beginning-of-line 1)
  10315. (skip-chars-forward " \t")
  10316. (setq params (plist-put params :indentation-column (current-column))))
  10317. (unless (re-search-forward org-dblock-end-re nil t)
  10318. (error "Dynamic block not terminated"))
  10319. (setq params
  10320. (append params
  10321. (list :content (buffer-substring
  10322. begdel (match-beginning 0)))))
  10323. (delete-region begdel (match-beginning 0))
  10324. (goto-char begdel)
  10325. (open-line 1)
  10326. params))
  10327. (defun org-map-dblocks (&optional command)
  10328. "Apply COMMAND to all dynamic blocks in the current buffer.
  10329. If COMMAND is not given, use `org-update-dblock'."
  10330. (let ((cmd (or command 'org-update-dblock)))
  10331. (save-excursion
  10332. (goto-char (point-min))
  10333. (while (re-search-forward org-dblock-start-re nil t)
  10334. (goto-char (match-beginning 0))
  10335. (save-excursion
  10336. (condition-case nil
  10337. (funcall cmd)
  10338. (error (message "Error during update of dynamic block"))))
  10339. (unless (re-search-forward org-dblock-end-re nil t)
  10340. (error "Dynamic block not terminated"))))))
  10341. (defun org-dblock-update (&optional arg)
  10342. "User command for updating dynamic blocks.
  10343. Update the dynamic block at point. With prefix ARG, update all dynamic
  10344. blocks in the buffer."
  10345. (interactive "P")
  10346. (if arg
  10347. (org-update-all-dblocks)
  10348. (or (looking-at org-dblock-start-re)
  10349. (org-beginning-of-dblock))
  10350. (org-update-dblock)))
  10351. (defun org-update-dblock ()
  10352. "Update the dynamic block at point.
  10353. This means to empty the block, parse for parameters and then call
  10354. the correct writing function."
  10355. (interactive)
  10356. (save-window-excursion
  10357. (let* ((pos (point))
  10358. (line (org-current-line))
  10359. (params (org-prepare-dblock))
  10360. (name (plist-get params :name))
  10361. (indent (plist-get params :indentation-column))
  10362. (cmd (intern (concat "org-dblock-write:" name))))
  10363. (message "Updating dynamic block `%s' at line %d..." name line)
  10364. (funcall cmd params)
  10365. (message "Updating dynamic block `%s' at line %d...done" name line)
  10366. (goto-char pos)
  10367. (when (and indent (> indent 0))
  10368. (setq indent (make-string indent ?\ ))
  10369. (save-excursion
  10370. (org-beginning-of-dblock)
  10371. (forward-line 1)
  10372. (while (not (looking-at org-dblock-end-re))
  10373. (insert indent)
  10374. (beginning-of-line 2))
  10375. (when (looking-at org-dblock-end-re)
  10376. (and (looking-at "[ \t]+")
  10377. (replace-match ""))
  10378. (insert indent)))))))
  10379. (defun org-beginning-of-dblock ()
  10380. "Find the beginning of the dynamic block at point.
  10381. Error if there is no such block at point."
  10382. (let ((pos (point))
  10383. beg)
  10384. (end-of-line 1)
  10385. (if (and (re-search-backward org-dblock-start-re nil t)
  10386. (setq beg (match-beginning 0))
  10387. (re-search-forward org-dblock-end-re nil t)
  10388. (> (match-end 0) pos))
  10389. (goto-char beg)
  10390. (goto-char pos)
  10391. (error "Not in a dynamic block"))))
  10392. (defun org-update-all-dblocks ()
  10393. "Update all dynamic blocks in the buffer.
  10394. This function can be used in a hook."
  10395. (interactive)
  10396. (when (derived-mode-p 'org-mode)
  10397. (org-map-dblocks 'org-update-dblock)))
  10398. ;;;; Completion
  10399. (defconst org-additional-option-like-keywords
  10400. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10401. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10402. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10403. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10404. "BEGIN:" "END:"
  10405. "ORGTBL" "TBLFM:" "TBLNAME:"
  10406. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10407. "BEGIN_VERBATIM" "END_VERBATIM"
  10408. "BEGIN_QUOTE" "END_QUOTE"
  10409. "BEGIN_VERSE" "END_VERSE"
  10410. "BEGIN_CENTER" "END_CENTER"
  10411. "BEGIN_SRC" "END_SRC"
  10412. "BEGIN_RESULT" "END_RESULT"
  10413. "BEGIN_lstlisting" "END_lstlisting"
  10414. "NAME:" "RESULTS:"
  10415. "HEADER:" "HEADERS:"
  10416. "COLUMNS:" "PROPERTY:"
  10417. "CAPTION:" "LABEL:"
  10418. "SETUPFILE:"
  10419. "INCLUDE:" "INDEX:"
  10420. "BIND:"
  10421. "MACRO:"))
  10422. (defconst org-options-keywords
  10423. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10424. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10425. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10426. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10427. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10428. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10429. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10430. (defconst org-additional-option-like-keywords-for-flyspell
  10431. (delete-dups
  10432. (split-string
  10433. (mapconcat (lambda(k)
  10434. (replace-regexp-in-string
  10435. "_\\|:" " "
  10436. (concat k " " (downcase k) " " (upcase k))))
  10437. (append org-options-keywords org-additional-option-like-keywords)
  10438. " ")
  10439. " +" t)))
  10440. (defcustom org-structure-template-alist
  10441. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10442. "<src lang=\"?\">\n\n</src>")
  10443. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10444. "<example>\n?\n</example>")
  10445. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10446. "<quote>\n?\n</quote>")
  10447. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10448. "<verse>\n?\n</verse>")
  10449. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10450. "<verbatim>\n?\n</verbatim>")
  10451. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10452. "<center>\n?\n</center>")
  10453. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10454. "<literal style=\"latex\">\n?\n</literal>")
  10455. ("L" "#+LaTeX: "
  10456. "<literal style=\"latex\">?</literal>")
  10457. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10458. "<literal style=\"html\">\n?\n</literal>")
  10459. ("H" "#+HTML: "
  10460. "<literal style=\"html\">?</literal>")
  10461. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10462. ("A" "#+ASCII: ")
  10463. ("i" "#+INDEX: ?"
  10464. "#+INDEX: ?")
  10465. ("I" "#+INCLUDE: %file ?"
  10466. "<include file=%file markup=\"?\">"))
  10467. "Structure completion elements.
  10468. This is a list of abbreviation keys and values. The value gets inserted
  10469. if you type `<' followed by the key and then press the completion key,
  10470. usually `M-TAB'. %file will be replaced by a file name after prompting
  10471. for the file using completion. The cursor will be placed at the position
  10472. of the `?` in the template.
  10473. There are two templates for each key, the first uses the original Org syntax,
  10474. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10475. the default when the /org-mtags.el/ module has been loaded. See also the
  10476. variable `org-mtags-prefer-muse-templates'."
  10477. :group 'org-completion
  10478. :type '(repeat
  10479. (string :tag "Key")
  10480. (string :tag "Template")
  10481. (string :tag "Muse Template")))
  10482. (defun org-try-structure-completion ()
  10483. "Try to complete a structure template before point.
  10484. This looks for strings like \"<e\" on an otherwise empty line and
  10485. expands them."
  10486. (let ((l (buffer-substring (point-at-bol) (point)))
  10487. a)
  10488. (when (and (looking-at "[ \t]*$")
  10489. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10490. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10491. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10492. (match-beginning 1)) a)
  10493. t)))
  10494. (defun org-complete-expand-structure-template (start cell)
  10495. "Expand a structure template."
  10496. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10497. (rpl (nth (if musep 2 1) cell))
  10498. (ind ""))
  10499. (delete-region start (point))
  10500. (when (string-match "\\`#\\+" rpl)
  10501. (cond
  10502. ((bolp))
  10503. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10504. (setq ind (buffer-substring (point-at-bol) (point))))
  10505. (t (newline))))
  10506. (setq start (point))
  10507. (if (string-match "%file" rpl)
  10508. (setq rpl (replace-match
  10509. (concat
  10510. "\""
  10511. (save-match-data
  10512. (abbreviate-file-name (read-file-name "Include file: ")))
  10513. "\"")
  10514. t t rpl)))
  10515. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10516. (concat "\n" ind)))
  10517. (insert rpl)
  10518. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10519. ;;;; TODO, DEADLINE, Comments
  10520. (defun org-toggle-comment ()
  10521. "Change the COMMENT state of an entry."
  10522. (interactive)
  10523. (save-excursion
  10524. (org-back-to-heading)
  10525. (let (case-fold-search)
  10526. (cond
  10527. ((looking-at (format org-heading-keyword-regexp-format
  10528. org-comment-string))
  10529. (goto-char (match-end 1))
  10530. (looking-at (concat " +" org-comment-string))
  10531. (replace-match "" t t)
  10532. (when (eolp) (insert " ")))
  10533. ((looking-at org-outline-regexp)
  10534. (goto-char (match-end 0))
  10535. (insert org-comment-string " "))))))
  10536. (defvar org-last-todo-state-is-todo nil
  10537. "This is non-nil when the last TODO state change led to a TODO state.
  10538. If the last change removed the TODO tag or switched to DONE, then
  10539. this is nil.")
  10540. (defvar org-setting-tags nil) ; dynamically skipped
  10541. (defvar org-todo-setup-filter-hook nil
  10542. "Hook for functions that pre-filter todo specs.
  10543. Each function takes a todo spec and returns either nil or the spec
  10544. transformed into canonical form." )
  10545. (defvar org-todo-get-default-hook nil
  10546. "Hook for functions that get a default item for todo.
  10547. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10548. nil or a string to be used for the todo mark." )
  10549. (defvar org-agenda-headline-snapshot-before-repeat)
  10550. (defun org-current-effective-time ()
  10551. "Return current time adjusted for `org-extend-today-until' variable."
  10552. (let* ((ct (org-current-time))
  10553. (dct (decode-time ct))
  10554. (ct1
  10555. (cond
  10556. (org-use-last-clock-out-time-as-effective-time
  10557. (or (org-clock-get-last-clock-out-time) ct))
  10558. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10559. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10560. (t ct))))
  10561. ct1))
  10562. (defun org-todo-yesterday (&optional arg)
  10563. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10564. (interactive "P")
  10565. (if (eq major-mode 'org-agenda-mode)
  10566. (apply 'org-agenda-todo-yesterday arg)
  10567. (let* ((hour (third (decode-time
  10568. (org-current-time))))
  10569. (org-extend-today-until (1+ hour)))
  10570. (org-todo arg))))
  10571. (defvar org-block-entry-blocking ""
  10572. "First entry preventing the TODO state change.")
  10573. (defun org-todo (&optional arg)
  10574. "Change the TODO state of an item.
  10575. The state of an item is given by a keyword at the start of the heading,
  10576. like
  10577. *** TODO Write paper
  10578. *** DONE Call mom
  10579. The different keywords are specified in the variable `org-todo-keywords'.
  10580. By default the available states are \"TODO\" and \"DONE\".
  10581. So for this example: when the item starts with TODO, it is changed to DONE.
  10582. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10583. DONE are present, add TODO at the beginning of the heading.
  10584. With \\[universal-argument] prefix arg, use completion to determine the new \
  10585. state.
  10586. With numeric prefix arg, switch to that state.
  10587. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10588. keywords (nextset).
  10589. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10590. With a numeric prefix arg of 0, inhibit note taking for the change.
  10591. For calling through lisp, arg is also interpreted in the following way:
  10592. 'none -> empty state
  10593. \"\"(empty string) -> switch to empty state
  10594. 'done -> switch to DONE
  10595. 'nextset -> switch to the next set of keywords
  10596. 'previousset -> switch to the previous set of keywords
  10597. \"WAITING\" -> switch to the specified keyword, but only if it
  10598. really is a member of `org-todo-keywords'."
  10599. (interactive "P")
  10600. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10601. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10602. 'region-start-level 'region))
  10603. org-loop-over-headlines-in-active-region)
  10604. (org-map-entries
  10605. `(org-todo ,arg)
  10606. org-loop-over-headlines-in-active-region
  10607. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10608. (if (equal arg '(16)) (setq arg 'nextset))
  10609. (let ((org-blocker-hook org-blocker-hook)
  10610. commentp
  10611. case-fold-search)
  10612. (when (equal arg '(64))
  10613. (setq arg nil org-blocker-hook nil))
  10614. (when (and org-blocker-hook
  10615. (or org-inhibit-blocking
  10616. (org-entry-get nil "NOBLOCKING")))
  10617. (setq org-blocker-hook nil))
  10618. (save-excursion
  10619. (catch 'exit
  10620. (org-back-to-heading t)
  10621. (when (looking-at (concat "^\\*+ " org-comment-string))
  10622. (org-toggle-comment)
  10623. (setq commentp t))
  10624. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10625. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10626. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10627. (let* ((match-data (match-data))
  10628. (startpos (point-at-bol))
  10629. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10630. (org-log-done org-log-done)
  10631. (org-log-repeat org-log-repeat)
  10632. (org-todo-log-states org-todo-log-states)
  10633. (org-inhibit-logging
  10634. (if (equal arg 0)
  10635. (progn (setq arg nil) 'note) org-inhibit-logging))
  10636. (this (match-string 1))
  10637. (hl-pos (match-beginning 0))
  10638. (head (org-get-todo-sequence-head this))
  10639. (ass (assoc head org-todo-kwd-alist))
  10640. (interpret (nth 1 ass))
  10641. (done-word (nth 3 ass))
  10642. (final-done-word (nth 4 ass))
  10643. (org-last-state (or this ""))
  10644. (completion-ignore-case t)
  10645. (member (member this org-todo-keywords-1))
  10646. (tail (cdr member))
  10647. (org-state (cond
  10648. ((and org-todo-key-trigger
  10649. (or (and (equal arg '(4))
  10650. (eq org-use-fast-todo-selection 'prefix))
  10651. (and (not arg) org-use-fast-todo-selection
  10652. (not (eq org-use-fast-todo-selection
  10653. 'prefix)))))
  10654. ;; Use fast selection
  10655. (org-fast-todo-selection))
  10656. ((and (equal arg '(4))
  10657. (or (not org-use-fast-todo-selection)
  10658. (not org-todo-key-trigger)))
  10659. ;; Read a state with completion
  10660. (org-icompleting-read
  10661. "State: " (mapcar (lambda(x) (list x))
  10662. org-todo-keywords-1)
  10663. nil t))
  10664. ((eq arg 'right)
  10665. (if this
  10666. (if tail (car tail) nil)
  10667. (car org-todo-keywords-1)))
  10668. ((eq arg 'left)
  10669. (if (equal member org-todo-keywords-1)
  10670. nil
  10671. (if this
  10672. (nth (- (length org-todo-keywords-1)
  10673. (length tail) 2)
  10674. org-todo-keywords-1)
  10675. (org-last org-todo-keywords-1))))
  10676. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10677. (setq arg nil))) ; hack to fall back to cycling
  10678. (arg
  10679. ;; user or caller requests a specific state
  10680. (cond
  10681. ((equal arg "") nil)
  10682. ((eq arg 'none) nil)
  10683. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10684. ((eq arg 'nextset)
  10685. (or (car (cdr (member head org-todo-heads)))
  10686. (car org-todo-heads)))
  10687. ((eq arg 'previousset)
  10688. (let ((org-todo-heads (reverse org-todo-heads)))
  10689. (or (car (cdr (member head org-todo-heads)))
  10690. (car org-todo-heads))))
  10691. ((car (member arg org-todo-keywords-1)))
  10692. ((stringp arg)
  10693. (error "State `%s' not valid in this file" arg))
  10694. ((nth (1- (prefix-numeric-value arg))
  10695. org-todo-keywords-1))))
  10696. ((null member) (or head (car org-todo-keywords-1)))
  10697. ((equal this final-done-word) nil) ;; -> make empty
  10698. ((null tail) nil) ;; -> first entry
  10699. ((memq interpret '(type priority))
  10700. (if (eq this-command last-command)
  10701. (car tail)
  10702. (if (> (length tail) 0)
  10703. (or done-word (car org-done-keywords))
  10704. nil)))
  10705. (t
  10706. (car tail))))
  10707. (org-state (or
  10708. (run-hook-with-args-until-success
  10709. 'org-todo-get-default-hook org-state org-last-state)
  10710. org-state))
  10711. (next (if org-state (concat " " org-state " ") " "))
  10712. (change-plist (list :type 'todo-state-change :from this :to org-state
  10713. :position startpos))
  10714. dolog now-done-p)
  10715. (when org-blocker-hook
  10716. (setq org-last-todo-state-is-todo
  10717. (not (member this org-done-keywords)))
  10718. (unless (save-excursion
  10719. (save-match-data
  10720. (org-with-wide-buffer
  10721. (run-hook-with-args-until-failure
  10722. 'org-blocker-hook change-plist))))
  10723. (if (org-called-interactively-p 'interactive)
  10724. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10725. this org-state org-block-entry-blocking)
  10726. ;; fail silently
  10727. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10728. this org-state org-block-entry-blocking)
  10729. (throw 'exit nil))))
  10730. (store-match-data match-data)
  10731. (replace-match next t t)
  10732. (unless (pos-visible-in-window-p hl-pos)
  10733. (message "TODO state changed to %s" (org-trim next)))
  10734. (unless head
  10735. (setq head (org-get-todo-sequence-head org-state)
  10736. ass (assoc head org-todo-kwd-alist)
  10737. interpret (nth 1 ass)
  10738. done-word (nth 3 ass)
  10739. final-done-word (nth 4 ass)))
  10740. (when (memq arg '(nextset previousset))
  10741. (message "Keyword-Set %d/%d: %s"
  10742. (- (length org-todo-sets) -1
  10743. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10744. (length org-todo-sets)
  10745. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10746. (setq org-last-todo-state-is-todo
  10747. (not (member org-state org-done-keywords)))
  10748. (setq now-done-p (and (member org-state org-done-keywords)
  10749. (not (member this org-done-keywords))))
  10750. (and logging (org-local-logging logging))
  10751. (when (and (or org-todo-log-states org-log-done)
  10752. (not (eq org-inhibit-logging t))
  10753. (not (memq arg '(nextset previousset))))
  10754. ;; we need to look at recording a time and note
  10755. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10756. (nth 2 (assoc this org-todo-log-states))))
  10757. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10758. (setq dolog 'time))
  10759. (when (and org-state
  10760. (member org-state org-not-done-keywords)
  10761. (not (member this org-not-done-keywords)))
  10762. ;; This is now a todo state and was not one before
  10763. ;; If there was a CLOSED time stamp, get rid of it.
  10764. (org-add-planning-info nil nil 'closed))
  10765. (when (and now-done-p org-log-done)
  10766. ;; It is now done, and it was not done before
  10767. (org-add-planning-info 'closed (org-current-effective-time))
  10768. (if (and (not dolog) (eq 'note org-log-done))
  10769. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10770. (when (and org-state dolog)
  10771. ;; This is a non-nil state, and we need to log it
  10772. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10773. ;; Fixup tag positioning
  10774. (org-todo-trigger-tag-changes org-state)
  10775. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10776. (when org-provide-todo-statistics
  10777. (org-update-parent-todo-statistics))
  10778. (run-hooks 'org-after-todo-state-change-hook)
  10779. (if (and arg (not (member org-state org-done-keywords)))
  10780. (setq head (org-get-todo-sequence-head org-state)))
  10781. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10782. ;; Do we need to trigger a repeat?
  10783. (when now-done-p
  10784. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10785. ;; This is for the agenda, take a snapshot of the headline.
  10786. (save-match-data
  10787. (setq org-agenda-headline-snapshot-before-repeat
  10788. (org-get-heading))))
  10789. (org-auto-repeat-maybe org-state))
  10790. ;; Fixup cursor location if close to the keyword
  10791. (if (and (outline-on-heading-p)
  10792. (not (bolp))
  10793. (save-excursion (beginning-of-line 1)
  10794. (looking-at org-todo-line-regexp))
  10795. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10796. (progn
  10797. (goto-char (or (match-end 2) (match-end 1)))
  10798. (and (looking-at " ") (just-one-space))))
  10799. (when org-trigger-hook
  10800. (save-excursion
  10801. (run-hook-with-args 'org-trigger-hook change-plist)))
  10802. (when commentp (org-toggle-comment))))))))
  10803. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10804. "Block turning an entry into a TODO, using the hierarchy.
  10805. This checks whether the current task should be blocked from state
  10806. changes. Such blocking occurs when:
  10807. 1. The task has children which are not all in a completed state.
  10808. 2. A task has a parent with the property :ORDERED:, and there
  10809. are siblings prior to the current task with incomplete
  10810. status.
  10811. 3. The parent of the task is blocked because it has siblings that should
  10812. be done first, or is child of a block grandparent TODO entry."
  10813. (if (not org-enforce-todo-dependencies)
  10814. t ; if locally turned off don't block
  10815. (catch 'dont-block
  10816. ;; If this is not a todo state change, or if this entry is already DONE,
  10817. ;; do not block
  10818. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10819. (member (plist-get change-plist :from)
  10820. (cons 'done org-done-keywords))
  10821. (member (plist-get change-plist :to)
  10822. (cons 'todo org-not-done-keywords))
  10823. (not (plist-get change-plist :to)))
  10824. (throw 'dont-block t))
  10825. ;; If this task has children, and any are undone, it's blocked
  10826. (save-excursion
  10827. (org-back-to-heading t)
  10828. (let ((this-level (funcall outline-level)))
  10829. (outline-next-heading)
  10830. (let ((child-level (funcall outline-level)))
  10831. (while (and (not (eobp))
  10832. (> child-level this-level))
  10833. ;; this todo has children, check whether they are all
  10834. ;; completed
  10835. (if (and (not (org-entry-is-done-p))
  10836. (org-entry-is-todo-p))
  10837. (progn (setq org-block-entry-blocking (org-get-heading))
  10838. (throw 'dont-block nil)))
  10839. (outline-next-heading)
  10840. (setq child-level (funcall outline-level))))))
  10841. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10842. ;; any previous siblings are undone, it's blocked
  10843. (save-excursion
  10844. (org-back-to-heading t)
  10845. (let* ((pos (point))
  10846. (parent-pos (and (org-up-heading-safe) (point))))
  10847. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10848. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10849. (forward-line 1)
  10850. (re-search-forward org-not-done-heading-regexp pos t))
  10851. (setq org-block-entry-blocking (match-string 0))
  10852. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10853. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10854. (while t
  10855. (goto-char parent-pos)
  10856. (if (not (looking-at org-not-done-heading-regexp))
  10857. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10858. (setq pos (point))
  10859. (setq parent-pos (and (org-up-heading-safe) (point)))
  10860. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10861. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10862. (forward-line 1)
  10863. (re-search-forward org-not-done-heading-regexp pos t)
  10864. (setq org-block-entry-blocking (org-get-heading)))
  10865. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10866. (defcustom org-track-ordered-property-with-tag nil
  10867. "Should the ORDERED property also be shown as a tag?
  10868. The ORDERED property decides if an entry should require subtasks to be
  10869. completed in sequence. Since a property is not very visible, setting
  10870. this option means that toggling the ORDERED property with the command
  10871. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10872. not relevant for the behavior, but it makes things more visible.
  10873. Note that toggling the tag with tags commands will not change the property
  10874. and therefore not influence behavior!
  10875. This can be t, meaning the tag ORDERED should be used, It can also be a
  10876. string to select a different tag for this task."
  10877. :group 'org-todo
  10878. :type '(choice
  10879. (const :tag "No tracking" nil)
  10880. (const :tag "Track with ORDERED tag" t)
  10881. (string :tag "Use other tag")))
  10882. (defun org-toggle-ordered-property ()
  10883. "Toggle the ORDERED property of the current entry.
  10884. For better visibility, you can track the value of this property with a tag.
  10885. See variable `org-track-ordered-property-with-tag'."
  10886. (interactive)
  10887. (let* ((t1 org-track-ordered-property-with-tag)
  10888. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10889. (save-excursion
  10890. (org-back-to-heading)
  10891. (if (org-entry-get nil "ORDERED")
  10892. (progn
  10893. (org-delete-property "ORDERED")
  10894. (and tag (org-toggle-tag tag 'off))
  10895. (message "Subtasks can be completed in arbitrary order"))
  10896. (org-entry-put nil "ORDERED" "t")
  10897. (and tag (org-toggle-tag tag 'on))
  10898. (message "Subtasks must be completed in sequence")))))
  10899. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10900. (defun org-block-todo-from-checkboxes (change-plist)
  10901. "Block turning an entry into a TODO, using checkboxes.
  10902. This checks whether the current task should be blocked from state
  10903. changes because there are unchecked boxes in this entry."
  10904. (if (not org-enforce-todo-checkbox-dependencies)
  10905. t ; if locally turned off don't block
  10906. (catch 'dont-block
  10907. ;; If this is not a todo state change, or if this entry is already DONE,
  10908. ;; do not block
  10909. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10910. (member (plist-get change-plist :from)
  10911. (cons 'done org-done-keywords))
  10912. (member (plist-get change-plist :to)
  10913. (cons 'todo org-not-done-keywords))
  10914. (not (plist-get change-plist :to)))
  10915. (throw 'dont-block t))
  10916. ;; If this task has checkboxes that are not checked, it's blocked
  10917. (save-excursion
  10918. (org-back-to-heading t)
  10919. (let ((beg (point)) end)
  10920. (outline-next-heading)
  10921. (setq end (point))
  10922. (goto-char beg)
  10923. (if (org-list-search-forward
  10924. (concat (org-item-beginning-re)
  10925. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10926. "\\[[- ]\\]")
  10927. end t)
  10928. (progn
  10929. (if (boundp 'org-blocked-by-checkboxes)
  10930. (setq org-blocked-by-checkboxes t))
  10931. (throw 'dont-block nil)))))
  10932. t))) ; do not block
  10933. (defun org-entry-blocked-p ()
  10934. "Is the current entry blocked?"
  10935. (org-with-buffer-modified-unmodified
  10936. (if (org-entry-get nil "NOBLOCKING")
  10937. nil ;; Never block this entry
  10938. (not
  10939. (run-hook-with-args-until-failure
  10940. 'org-blocker-hook
  10941. (list :type 'todo-state-change
  10942. :position (point)
  10943. :from 'todo
  10944. :to 'done))))))
  10945. (defun org-update-statistics-cookies (all)
  10946. "Update the statistics cookie, either from TODO or from checkboxes.
  10947. This should be called with the cursor in a line with a statistics cookie."
  10948. (interactive "P")
  10949. (if all
  10950. (progn
  10951. (org-update-checkbox-count 'all)
  10952. (org-map-entries 'org-update-parent-todo-statistics))
  10953. (if (not (org-at-heading-p))
  10954. (org-update-checkbox-count)
  10955. (let ((pos (point-marker))
  10956. end l1 l2)
  10957. (ignore-errors (org-back-to-heading t))
  10958. (if (not (org-at-heading-p))
  10959. (org-update-checkbox-count)
  10960. (setq l1 (org-outline-level))
  10961. (setq end (save-excursion
  10962. (outline-next-heading)
  10963. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10964. (point)))
  10965. (if (and (save-excursion
  10966. (re-search-forward
  10967. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10968. (not (save-excursion (re-search-forward
  10969. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10970. (org-update-checkbox-count)
  10971. (if (and l2 (> l2 l1))
  10972. (progn
  10973. (goto-char end)
  10974. (org-update-parent-todo-statistics))
  10975. (goto-char pos)
  10976. (beginning-of-line 1)
  10977. (while (re-search-forward
  10978. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10979. (point-at-eol) t)
  10980. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10981. (goto-char pos)
  10982. (move-marker pos nil)))))
  10983. (defvar org-entry-property-inherited-from) ;; defined below
  10984. (defun org-update-parent-todo-statistics ()
  10985. "Update any statistics cookie in the parent of the current headline.
  10986. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10987. the entire subtree and this will travel up the hierarchy and update
  10988. statistics everywhere."
  10989. (let* ((prop (save-excursion (org-up-heading-safe)
  10990. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10991. (recursive (or (not org-hierarchical-todo-statistics)
  10992. (and prop (string-match "\\<recursive\\>" prop))))
  10993. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10994. 0))
  10995. (first t)
  10996. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10997. level ltoggle l1 new ndel
  10998. (cnt-all 0) (cnt-done 0) is-percent kwd
  10999. checkbox-beg ov ovs ove cookie-present)
  11000. (catch 'exit
  11001. (save-excursion
  11002. (beginning-of-line 1)
  11003. (setq ltoggle (funcall outline-level))
  11004. ;; Three situations are to consider:
  11005. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11006. ;; to the top-level ancestor on the headline;
  11007. ;; 2. If parent has "recursive" property, repeat up to the
  11008. ;; headline setting that property, taking inheritance into
  11009. ;; account;
  11010. ;; 3. Else, move up to direct parent and proceed only once.
  11011. (while (and (setq level (org-up-heading-safe))
  11012. (or recursive first)
  11013. (>= (point) lim))
  11014. (setq first nil cookie-present nil)
  11015. (unless (and level
  11016. (not (string-match
  11017. "\\<checkbox\\>"
  11018. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11019. "")))))
  11020. (throw 'exit nil))
  11021. (while (re-search-forward box-re (point-at-eol) t)
  11022. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11023. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11024. (save-match-data
  11025. (unless (outline-next-heading) (throw 'exit nil))
  11026. (while (and (looking-at org-complex-heading-regexp)
  11027. (> (setq l1 (length (match-string 1))) level))
  11028. (setq kwd (and (or recursive (= l1 ltoggle))
  11029. (match-string 2)))
  11030. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11031. (and (listp org-provide-todo-statistics)
  11032. (or (member kwd org-provide-todo-statistics)
  11033. (member kwd org-done-keywords))))
  11034. (setq cnt-all (1+ cnt-all))
  11035. (if (eq org-provide-todo-statistics t)
  11036. (and kwd (setq cnt-all (1+ cnt-all)))))
  11037. (and (member kwd org-done-keywords)
  11038. (setq cnt-done (1+ cnt-done)))
  11039. (outline-next-heading)))
  11040. (setq new
  11041. (if is-percent
  11042. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11043. (format "[%d/%d]" cnt-done cnt-all))
  11044. ndel (- (match-end 0) checkbox-beg))
  11045. ;; handle overlays when updating cookie from column view
  11046. (when (setq ov (car (overlays-at checkbox-beg)))
  11047. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11048. (delete-overlay ov))
  11049. (goto-char checkbox-beg)
  11050. (insert new)
  11051. (delete-region (point) (+ (point) ndel))
  11052. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11053. (when ov (move-overlay ov ovs ove)))
  11054. (when cookie-present
  11055. (run-hook-with-args 'org-after-todo-statistics-hook
  11056. cnt-done (- cnt-all cnt-done))))))
  11057. (run-hooks 'org-todo-statistics-hook)))
  11058. (defvar org-after-todo-statistics-hook nil
  11059. "Hook that is called after a TODO statistics cookie has been updated.
  11060. Each function is called with two arguments: the number of not-done entries
  11061. and the number of done entries.
  11062. For example, the following function, when added to this hook, will switch
  11063. an entry to DONE when all children are done, and back to TODO when new
  11064. entries are set to a TODO status. Note that this hook is only called
  11065. when there is a statistics cookie in the headline!
  11066. (defun org-summary-todo (n-done n-not-done)
  11067. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11068. (let (org-log-done org-log-states) ; turn off logging
  11069. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11070. ")
  11071. (defvar org-todo-statistics-hook nil
  11072. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11073. This hook runs even if there is no statistics cookie present, in which case
  11074. `org-after-todo-statistics-hook' would not run.")
  11075. (defun org-todo-trigger-tag-changes (state)
  11076. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11077. (let ((l org-todo-state-tags-triggers)
  11078. changes)
  11079. (when (or (not state) (equal state ""))
  11080. (setq changes (append changes (cdr (assoc "" l)))))
  11081. (when (and (stringp state) (> (length state) 0))
  11082. (setq changes (append changes (cdr (assoc state l)))))
  11083. (when (member state org-not-done-keywords)
  11084. (setq changes (append changes (cdr (assoc 'todo l)))))
  11085. (when (member state org-done-keywords)
  11086. (setq changes (append changes (cdr (assoc 'done l)))))
  11087. (dolist (c changes)
  11088. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11089. (defun org-local-logging (value)
  11090. "Get logging settings from a property VALUE."
  11091. (let* (words w a)
  11092. ;; directly set the variables, they are already local.
  11093. (setq org-log-done nil
  11094. org-log-repeat nil
  11095. org-todo-log-states nil)
  11096. (setq words (org-split-string value))
  11097. (while (setq w (pop words))
  11098. (cond
  11099. ((setq a (assoc w org-startup-options))
  11100. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11101. (set (nth 1 a) (nth 2 a))))
  11102. ((setq a (org-extract-log-state-settings w))
  11103. (and (member (car a) org-todo-keywords-1)
  11104. (push a org-todo-log-states)))))))
  11105. (defun org-get-todo-sequence-head (kwd)
  11106. "Return the head of the TODO sequence to which KWD belongs.
  11107. If KWD is not set, check if there is a text property remembering the
  11108. right sequence."
  11109. (let (p)
  11110. (cond
  11111. ((not kwd)
  11112. (or (get-text-property (point-at-bol) 'org-todo-head)
  11113. (progn
  11114. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11115. nil (point-at-eol)))
  11116. (get-text-property p 'org-todo-head))))
  11117. ((not (member kwd org-todo-keywords-1))
  11118. (car org-todo-keywords-1))
  11119. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11120. (defun org-fast-todo-selection ()
  11121. "Fast TODO keyword selection with single keys.
  11122. Returns the new TODO keyword, or nil if no state change should occur."
  11123. (let* ((fulltable org-todo-key-alist)
  11124. (done-keywords org-done-keywords) ;; needed for the faces.
  11125. (maxlen (apply 'max (mapcar
  11126. (lambda (x)
  11127. (if (stringp (car x)) (string-width (car x)) 0))
  11128. fulltable)))
  11129. (expert nil)
  11130. (fwidth (+ maxlen 3 1 3))
  11131. (ncol (/ (- (window-width) 4) fwidth))
  11132. tg cnt e c tbl
  11133. groups ingroup)
  11134. (save-excursion
  11135. (save-window-excursion
  11136. (if expert
  11137. (set-buffer (get-buffer-create " *Org todo*"))
  11138. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11139. (erase-buffer)
  11140. (org-set-local 'org-done-keywords done-keywords)
  11141. (setq tbl fulltable cnt 0)
  11142. (while (setq e (pop tbl))
  11143. (cond
  11144. ((equal e '(:startgroup))
  11145. (push '() groups) (setq ingroup t)
  11146. (when (not (= cnt 0))
  11147. (setq cnt 0)
  11148. (insert "\n"))
  11149. (insert "{ "))
  11150. ((equal e '(:endgroup))
  11151. (setq ingroup nil cnt 0)
  11152. (insert "}\n"))
  11153. ((equal e '(:newline))
  11154. (when (not (= cnt 0))
  11155. (setq cnt 0)
  11156. (insert "\n")
  11157. (setq e (car tbl))
  11158. (while (equal (car tbl) '(:newline))
  11159. (insert "\n")
  11160. (setq tbl (cdr tbl)))))
  11161. (t
  11162. (setq tg (car e) c (cdr e))
  11163. (if ingroup (push tg (car groups)))
  11164. (setq tg (org-add-props tg nil 'face
  11165. (org-get-todo-face tg)))
  11166. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11167. (insert "[" c "] " tg (make-string
  11168. (- fwidth 4 (length tg)) ?\ ))
  11169. (when (= (setq cnt (1+ cnt)) ncol)
  11170. (insert "\n")
  11171. (if ingroup (insert " "))
  11172. (setq cnt 0)))))
  11173. (insert "\n")
  11174. (goto-char (point-min))
  11175. (if (not expert) (org-fit-window-to-buffer))
  11176. (message "[a-z..]:Set [SPC]:clear")
  11177. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11178. (cond
  11179. ((or (= c ?\C-g)
  11180. (and (= c ?q) (not (rassoc c fulltable))))
  11181. (setq quit-flag t))
  11182. ((= c ?\ ) nil)
  11183. ((setq e (rassoc c fulltable) tg (car e))
  11184. tg)
  11185. (t (setq quit-flag t)))))))
  11186. (defun org-entry-is-todo-p ()
  11187. (member (org-get-todo-state) org-not-done-keywords))
  11188. (defun org-entry-is-done-p ()
  11189. (member (org-get-todo-state) org-done-keywords))
  11190. (defun org-get-todo-state ()
  11191. (save-excursion
  11192. (org-back-to-heading t)
  11193. (and (looking-at org-todo-line-regexp)
  11194. (match-end 2)
  11195. (match-string 2))))
  11196. (defun org-at-date-range-p (&optional inactive-ok)
  11197. "Is the cursor inside a date range?"
  11198. (interactive)
  11199. (save-excursion
  11200. (catch 'exit
  11201. (let ((pos (point)))
  11202. (skip-chars-backward "^[<\r\n")
  11203. (skip-chars-backward "<[")
  11204. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11205. (>= (match-end 0) pos)
  11206. (throw 'exit t))
  11207. (skip-chars-backward "^<[\r\n")
  11208. (skip-chars-backward "<[")
  11209. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11210. (>= (match-end 0) pos)
  11211. (throw 'exit t)))
  11212. nil)))
  11213. (defun org-get-repeat (&optional tagline)
  11214. "Check if there is a deadline/schedule with repeater in this entry."
  11215. (save-match-data
  11216. (save-excursion
  11217. (org-back-to-heading t)
  11218. (and (re-search-forward (if tagline
  11219. (concat tagline "\\s-*" org-repeat-re)
  11220. org-repeat-re)
  11221. (org-entry-end-position) t)
  11222. (match-string-no-properties 1)))))
  11223. (defvar org-last-changed-timestamp)
  11224. (defvar org-last-inserted-timestamp)
  11225. (defvar org-log-post-message)
  11226. (defvar org-log-note-purpose)
  11227. (defvar org-log-note-how)
  11228. (defvar org-log-note-extra)
  11229. (defun org-auto-repeat-maybe (done-word)
  11230. "Check if the current headline contains a repeated deadline/schedule.
  11231. If yes, set TODO state back to what it was and change the base date
  11232. of repeating deadline/scheduled time stamps to new date.
  11233. This function is run automatically after each state change to a DONE state."
  11234. ;; last-state is dynamically scoped into this function
  11235. (let* ((repeat (org-get-repeat))
  11236. (aa (assoc org-last-state org-todo-kwd-alist))
  11237. (interpret (nth 1 aa))
  11238. (head (nth 2 aa))
  11239. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11240. (msg "Entry repeats: ")
  11241. (org-log-done nil)
  11242. (org-todo-log-states nil)
  11243. re type n what ts time to-state)
  11244. (when repeat
  11245. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11246. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11247. org-todo-repeat-to-state))
  11248. (unless (and to-state (member to-state org-todo-keywords-1))
  11249. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11250. (org-todo to-state)
  11251. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11252. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11253. (org-time-stamp-format t t))))
  11254. (when org-log-repeat
  11255. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11256. (memq 'org-add-log-note post-command-hook))
  11257. ;; OK, we are already setup for some record
  11258. (if (eq org-log-repeat 'note)
  11259. ;; make sure we take a note, not only a time stamp
  11260. (setq org-log-note-how 'note))
  11261. ;; Set up for taking a record
  11262. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11263. org-last-state
  11264. 'findpos org-log-repeat)))
  11265. (org-back-to-heading t)
  11266. (org-add-planning-info nil nil 'closed)
  11267. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11268. org-deadline-time-regexp "\\)\\|\\("
  11269. org-ts-regexp "\\)"))
  11270. (while (re-search-forward
  11271. re (save-excursion (outline-next-heading) (point)) t)
  11272. (setq type (if (match-end 1) org-scheduled-string
  11273. (if (match-end 3) org-deadline-string "Plain:"))
  11274. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11275. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11276. (setq n (string-to-number (match-string 2 ts))
  11277. what (match-string 3 ts))
  11278. (if (equal what "w") (setq n (* n 7) what "d"))
  11279. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11280. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11281. ;; Preparation, see if we need to modify the start date for the change
  11282. (when (match-end 1)
  11283. (setq time (save-match-data (org-time-string-to-time ts)))
  11284. (cond
  11285. ((equal (match-string 1 ts) ".")
  11286. ;; Shift starting date to today
  11287. (org-timestamp-change
  11288. (- (org-today) (time-to-days time))
  11289. 'day))
  11290. ((equal (match-string 1 ts) "+")
  11291. (let ((nshiftmax 10) (nshift 0))
  11292. (while (or (= nshift 0)
  11293. (<= (time-to-days time)
  11294. (time-to-days (current-time))))
  11295. (when (= (incf nshift) nshiftmax)
  11296. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11297. (error "Abort")))
  11298. (org-timestamp-change n (cdr (assoc what whata)))
  11299. (org-at-timestamp-p t)
  11300. (setq ts (match-string 1))
  11301. (setq time (save-match-data (org-time-string-to-time ts)))))
  11302. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11303. ;; rematch, so that we have everything in place for the real shift
  11304. (org-at-timestamp-p t)
  11305. (setq ts (match-string 1))
  11306. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11307. (org-timestamp-change n (cdr (assoc what whata)))
  11308. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11309. (setq org-log-post-message msg)
  11310. (message "%s" msg))))
  11311. (defun org-show-todo-tree (arg)
  11312. "Make a compact tree which shows all headlines marked with TODO.
  11313. The tree will show the lines where the regexp matches, and all higher
  11314. headlines above the match.
  11315. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11316. With a numeric prefix N, construct a sparse tree for the Nth element
  11317. of `org-todo-keywords-1'."
  11318. (interactive "P")
  11319. (let ((case-fold-search nil)
  11320. (kwd-re
  11321. (cond ((null arg) org-not-done-regexp)
  11322. ((equal arg '(4))
  11323. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11324. (mapcar 'list org-todo-keywords-1))))
  11325. (concat "\\("
  11326. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11327. "\\)\\>")))
  11328. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11329. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11330. org-todo-keywords-1)))
  11331. (t (error "Invalid prefix argument: %s" arg)))))
  11332. (message "%d TODO entries found"
  11333. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11334. (defun org-deadline (&optional remove time)
  11335. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11336. With argument REMOVE, remove any deadline from the item.
  11337. With argument TIME, set the deadline at the corresponding date. TIME
  11338. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11339. (interactive "P")
  11340. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11341. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11342. 'region-start-level 'region))
  11343. org-loop-over-headlines-in-active-region)
  11344. (org-map-entries
  11345. `(org-deadline ',remove ,time)
  11346. org-loop-over-headlines-in-active-region
  11347. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11348. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11349. (repeater (and old-date
  11350. (string-match
  11351. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11352. old-date)
  11353. (match-string 1 old-date))))
  11354. (if remove
  11355. (progn
  11356. (when (and old-date org-log-redeadline)
  11357. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11358. org-log-redeadline))
  11359. (org-remove-timestamp-with-keyword org-deadline-string)
  11360. (message "Item no longer has a deadline."))
  11361. (org-add-planning-info 'deadline time 'closed)
  11362. (when (and old-date org-log-redeadline
  11363. (not (equal old-date
  11364. (substring org-last-inserted-timestamp 1 -1))))
  11365. (org-add-log-setup 'redeadline nil old-date 'findpos
  11366. org-log-redeadline))
  11367. (when repeater
  11368. (save-excursion
  11369. (org-back-to-heading t)
  11370. (when (re-search-forward (concat org-deadline-string " "
  11371. org-last-inserted-timestamp)
  11372. (save-excursion
  11373. (outline-next-heading) (point)) t)
  11374. (goto-char (1- (match-end 0)))
  11375. (insert " " repeater)
  11376. (setq org-last-inserted-timestamp
  11377. (concat (substring org-last-inserted-timestamp 0 -1)
  11378. " " repeater
  11379. (substring org-last-inserted-timestamp -1))))))
  11380. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11381. (defun org-schedule (&optional remove time)
  11382. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11383. With argument REMOVE, remove any scheduling date from the item.
  11384. With argument TIME, scheduled at the corresponding date. TIME can
  11385. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11386. (interactive "P")
  11387. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11388. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11389. 'region-start-level 'region))
  11390. org-loop-over-headlines-in-active-region)
  11391. (org-map-entries
  11392. `(org-schedule ',remove ,time)
  11393. org-loop-over-headlines-in-active-region
  11394. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11395. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11396. (repeater (and old-date
  11397. (string-match
  11398. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11399. old-date)
  11400. (match-string 1 old-date))))
  11401. (if remove
  11402. (progn
  11403. (when (and old-date org-log-reschedule)
  11404. (org-add-log-setup 'delschedule nil old-date 'findpos
  11405. org-log-reschedule))
  11406. (org-remove-timestamp-with-keyword org-scheduled-string)
  11407. (message "Item is no longer scheduled."))
  11408. (org-add-planning-info 'scheduled time 'closed)
  11409. (when (and old-date org-log-reschedule
  11410. (not (equal old-date
  11411. (substring org-last-inserted-timestamp 1 -1))))
  11412. (org-add-log-setup 'reschedule nil old-date 'findpos
  11413. org-log-reschedule))
  11414. (when repeater
  11415. (save-excursion
  11416. (org-back-to-heading t)
  11417. (when (re-search-forward (concat org-scheduled-string " "
  11418. org-last-inserted-timestamp)
  11419. (save-excursion
  11420. (outline-next-heading) (point)) t)
  11421. (goto-char (1- (match-end 0)))
  11422. (insert " " repeater)
  11423. (setq org-last-inserted-timestamp
  11424. (concat (substring org-last-inserted-timestamp 0 -1)
  11425. " " repeater
  11426. (substring org-last-inserted-timestamp -1))))))
  11427. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11428. (defun org-get-scheduled-time (pom &optional inherit)
  11429. "Get the scheduled time as a time tuple, of a format suitable
  11430. for calling org-schedule with, or if there is no scheduling,
  11431. returns nil."
  11432. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11433. (when time
  11434. (apply 'encode-time (org-parse-time-string time)))))
  11435. (defun org-get-deadline-time (pom &optional inherit)
  11436. "Get the deadline as a time tuple, of a format suitable for
  11437. calling org-deadline with, or if there is no scheduling, returns
  11438. nil."
  11439. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11440. (when time
  11441. (apply 'encode-time (org-parse-time-string time)))))
  11442. (defun org-remove-timestamp-with-keyword (keyword)
  11443. "Remove all time stamps with KEYWORD in the current entry."
  11444. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11445. beg)
  11446. (save-excursion
  11447. (org-back-to-heading t)
  11448. (setq beg (point))
  11449. (outline-next-heading)
  11450. (while (re-search-backward re beg t)
  11451. (replace-match "")
  11452. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11453. (equal (char-before) ?\ ))
  11454. (backward-delete-char 1)
  11455. (if (string-match "^[ \t]*$" (buffer-substring
  11456. (point-at-bol) (point-at-eol)))
  11457. (delete-region (point-at-bol)
  11458. (min (point-max) (1+ (point-at-eol))))))))))
  11459. (defun org-add-planning-info (what &optional time &rest remove)
  11460. "Insert new timestamp with keyword in the line directly after the headline.
  11461. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11462. If non is given, the user is prompted for a date.
  11463. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11464. be removed."
  11465. (interactive)
  11466. (let (org-time-was-given org-end-time-was-given ts
  11467. end default-time default-input)
  11468. (catch 'exit
  11469. (when (and (memq what '(scheduled deadline))
  11470. (or (not time)
  11471. (and (stringp time)
  11472. (string-match "^[-+]+[0-9]" time))))
  11473. ;; Try to get a default date/time from existing timestamp
  11474. (save-excursion
  11475. (org-back-to-heading t)
  11476. (setq end (save-excursion (outline-next-heading) (point)))
  11477. (when (re-search-forward (if (eq what 'scheduled)
  11478. org-scheduled-time-regexp
  11479. org-deadline-time-regexp)
  11480. end t)
  11481. (setq ts (match-string 1)
  11482. default-time
  11483. (apply 'encode-time (org-parse-time-string ts))
  11484. default-input (and ts (org-get-compact-tod ts))))))
  11485. (when what
  11486. (setq time
  11487. (if (stringp time)
  11488. ;; This is a string (relative or absolute), set proper date
  11489. (apply 'encode-time
  11490. (org-read-date-analyze
  11491. time default-time (decode-time default-time)))
  11492. ;; If necessary, get the time from the user
  11493. (or time (org-read-date nil 'to-time nil nil
  11494. default-time default-input)))))
  11495. (when (and org-insert-labeled-timestamps-at-point
  11496. (member what '(scheduled deadline)))
  11497. (insert
  11498. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11499. (org-insert-time-stamp time org-time-was-given
  11500. nil nil nil (list org-end-time-was-given))
  11501. (setq what nil))
  11502. (save-excursion
  11503. (save-restriction
  11504. (let (col list elt ts buffer-invisibility-spec)
  11505. (org-back-to-heading t)
  11506. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11507. (goto-char (match-end 1))
  11508. (setq col (current-column))
  11509. (goto-char (match-end 0))
  11510. (if (eobp) (insert "\n") (forward-char 1))
  11511. (when (and (not what)
  11512. (not (looking-at
  11513. (concat "[ \t]*"
  11514. org-keyword-time-not-clock-regexp))))
  11515. ;; Nothing to add, nothing to remove...... :-)
  11516. (throw 'exit nil))
  11517. (if (and (not (looking-at org-outline-regexp))
  11518. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11519. "[^\r\n]*"))
  11520. (not (equal (match-string 1) org-clock-string)))
  11521. (narrow-to-region (match-beginning 0) (match-end 0))
  11522. (insert-before-markers "\n")
  11523. (backward-char 1)
  11524. (narrow-to-region (point) (point))
  11525. (and org-adapt-indentation (org-indent-to-column col)))
  11526. ;; Check if we have to remove something.
  11527. (setq list (cons what remove))
  11528. (while list
  11529. (setq elt (pop list))
  11530. (when (or (and (eq elt 'scheduled)
  11531. (re-search-forward org-scheduled-time-regexp nil t))
  11532. (and (eq elt 'deadline)
  11533. (re-search-forward org-deadline-time-regexp nil t))
  11534. (and (eq elt 'closed)
  11535. (re-search-forward org-closed-time-regexp nil t)))
  11536. (replace-match "")
  11537. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11538. (and (looking-at "[ \t]+") (replace-match ""))
  11539. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11540. (when what
  11541. (insert
  11542. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11543. (cond ((eq what 'scheduled) org-scheduled-string)
  11544. ((eq what 'deadline) org-deadline-string)
  11545. ((eq what 'closed) org-closed-string))
  11546. " ")
  11547. (setq ts (org-insert-time-stamp
  11548. time
  11549. (or org-time-was-given
  11550. (and (eq what 'closed) org-log-done-with-time))
  11551. (eq what 'closed)
  11552. nil nil (list org-end-time-was-given)))
  11553. (insert
  11554. (if (not (or (bolp) (eq (char-before) ?\ )
  11555. (memq (char-after) '(32 10))
  11556. (eobp))) " " ""))
  11557. (end-of-line 1))
  11558. (goto-char (point-min))
  11559. (widen)
  11560. (if (and (looking-at "[ \t]*\n")
  11561. (equal (char-before) ?\n))
  11562. (delete-region (1- (point)) (point-at-eol)))
  11563. ts))))))
  11564. (defvar org-log-note-marker (make-marker))
  11565. (defvar org-log-note-purpose nil)
  11566. (defvar org-log-note-state nil)
  11567. (defvar org-log-note-previous-state nil)
  11568. (defvar org-log-note-how nil)
  11569. (defvar org-log-note-extra nil)
  11570. (defvar org-log-note-window-configuration nil)
  11571. (defvar org-log-note-return-to (make-marker))
  11572. (defvar org-log-note-effective-time nil
  11573. "Remembered current time so that dynamically scoped
  11574. `org-extend-today-until' affects tha timestamps in state change
  11575. log")
  11576. (defvar org-log-post-message nil
  11577. "Message to be displayed after a log note has been stored.
  11578. The auto-repeater uses this.")
  11579. (defun org-add-note ()
  11580. "Add a note to the current entry.
  11581. This is done in the same way as adding a state change note."
  11582. (interactive)
  11583. (org-add-log-setup 'note nil nil 'findpos nil))
  11584. (defvar org-property-end-re)
  11585. (defun org-add-log-setup (&optional purpose state prev-state
  11586. findpos how extra)
  11587. "Set up the post command hook to take a note.
  11588. If this is about to TODO state change, the new state is expected in STATE.
  11589. When FINDPOS is non-nil, find the correct position for the note in
  11590. the current entry. If not, assume that it can be inserted at point.
  11591. HOW is an indicator what kind of note should be created.
  11592. EXTRA is additional text that will be inserted into the notes buffer."
  11593. (let* ((org-log-into-drawer (org-log-into-drawer))
  11594. (drawer (cond ((stringp org-log-into-drawer)
  11595. org-log-into-drawer)
  11596. (org-log-into-drawer "LOGBOOK"))))
  11597. (save-restriction
  11598. (save-excursion
  11599. (when findpos
  11600. (org-back-to-heading t)
  11601. (narrow-to-region (point) (save-excursion
  11602. (outline-next-heading) (point)))
  11603. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11604. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11605. "[^\r\n]*\\)?"))
  11606. (goto-char (match-end 0))
  11607. (cond
  11608. (drawer
  11609. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11610. nil t)
  11611. (progn
  11612. (goto-char (match-end 0))
  11613. (or org-log-states-order-reversed
  11614. (and (re-search-forward org-property-end-re nil t)
  11615. (goto-char (1- (match-beginning 0))))))
  11616. (insert "\n:" drawer ":\n:END:")
  11617. (beginning-of-line 0)
  11618. (org-indent-line)
  11619. (beginning-of-line 2)
  11620. (org-indent-line)
  11621. (end-of-line 0)))
  11622. ((and org-log-state-notes-insert-after-drawers
  11623. (save-excursion
  11624. (forward-line) (looking-at org-drawer-regexp)))
  11625. (forward-line)
  11626. (while (looking-at org-drawer-regexp)
  11627. (goto-char (match-end 0))
  11628. (re-search-forward org-property-end-re (point-max) t)
  11629. (forward-line))
  11630. (forward-line -1)))
  11631. (unless org-log-states-order-reversed
  11632. (and (= (char-after) ?\n) (forward-char 1))
  11633. (org-skip-over-state-notes)
  11634. (skip-chars-backward " \t\n\r")))
  11635. (move-marker org-log-note-marker (point))
  11636. (setq org-log-note-purpose purpose
  11637. org-log-note-state state
  11638. org-log-note-previous-state prev-state
  11639. org-log-note-how how
  11640. org-log-note-extra extra
  11641. org-log-note-effective-time (org-current-effective-time))
  11642. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11643. (defun org-skip-over-state-notes ()
  11644. "Skip past the list of State notes in an entry."
  11645. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11646. (when (ignore-errors (goto-char (org-in-item-p)))
  11647. (let* ((struct (org-list-struct))
  11648. (prevs (org-list-prevs-alist struct)))
  11649. (while (looking-at "[ \t]*- State")
  11650. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11651. (org-list-get-item-end (point) struct)))))))
  11652. (defun org-add-log-note (&optional purpose)
  11653. "Pop up a window for taking a note, and add this note later at point."
  11654. (remove-hook 'post-command-hook 'org-add-log-note)
  11655. (setq org-log-note-window-configuration (current-window-configuration))
  11656. (delete-other-windows)
  11657. (move-marker org-log-note-return-to (point))
  11658. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11659. (goto-char org-log-note-marker)
  11660. (org-switch-to-buffer-other-window "*Org Note*")
  11661. (erase-buffer)
  11662. (if (memq org-log-note-how '(time state))
  11663. (let (current-prefix-arg) (org-store-log-note))
  11664. (let ((org-inhibit-startup t)) (org-mode))
  11665. (insert (format "# Insert note for %s.
  11666. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11667. (cond
  11668. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11669. ((eq org-log-note-purpose 'done) "closed todo item")
  11670. ((eq org-log-note-purpose 'state)
  11671. (format "state change from \"%s\" to \"%s\""
  11672. (or org-log-note-previous-state "")
  11673. (or org-log-note-state "")))
  11674. ((eq org-log-note-purpose 'reschedule)
  11675. "rescheduling")
  11676. ((eq org-log-note-purpose 'delschedule)
  11677. "no longer scheduled")
  11678. ((eq org-log-note-purpose 'redeadline)
  11679. "changing deadline")
  11680. ((eq org-log-note-purpose 'deldeadline)
  11681. "removing deadline")
  11682. ((eq org-log-note-purpose 'refile)
  11683. "refiling")
  11684. ((eq org-log-note-purpose 'note)
  11685. "this entry")
  11686. (t (error "This should not happen")))))
  11687. (if org-log-note-extra (insert org-log-note-extra))
  11688. (org-set-local 'org-finish-function 'org-store-log-note)
  11689. (run-hooks 'org-log-buffer-setup-hook)))
  11690. (defvar org-note-abort nil) ; dynamically scoped
  11691. (defun org-store-log-note ()
  11692. "Finish taking a log note, and insert it to where it belongs."
  11693. (let ((txt (buffer-string)))
  11694. (kill-buffer (current-buffer))
  11695. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11696. lines ind bul)
  11697. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11698. (setq txt (replace-match "" t t txt)))
  11699. (if (string-match "\\s-+\\'" txt)
  11700. (setq txt (replace-match "" t t txt)))
  11701. (setq lines (org-split-string txt "\n"))
  11702. (when (and note (string-match "\\S-" note))
  11703. (setq note
  11704. (org-replace-escapes
  11705. note
  11706. (list (cons "%u" (user-login-name))
  11707. (cons "%U" user-full-name)
  11708. (cons "%t" (format-time-string
  11709. (org-time-stamp-format 'long 'inactive)
  11710. org-log-note-effective-time))
  11711. (cons "%T" (format-time-string
  11712. (org-time-stamp-format 'long nil)
  11713. org-log-note-effective-time))
  11714. (cons "%d" (format-time-string
  11715. (org-time-stamp-format nil 'inactive)
  11716. org-log-note-effective-time))
  11717. (cons "%D" (format-time-string
  11718. (org-time-stamp-format nil nil)
  11719. org-log-note-effective-time))
  11720. (cons "%s" (if org-log-note-state
  11721. (concat "\"" org-log-note-state "\"")
  11722. ""))
  11723. (cons "%S" (if org-log-note-previous-state
  11724. (concat "\"" org-log-note-previous-state "\"")
  11725. "\"\"")))))
  11726. (if lines (setq note (concat note " \\\\")))
  11727. (push note lines))
  11728. (when (or current-prefix-arg org-note-abort)
  11729. (when org-log-into-drawer
  11730. (org-remove-empty-drawer-at
  11731. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11732. org-log-note-marker))
  11733. (setq lines nil))
  11734. (when lines
  11735. (with-current-buffer (marker-buffer org-log-note-marker)
  11736. (save-excursion
  11737. (goto-char org-log-note-marker)
  11738. (move-marker org-log-note-marker nil)
  11739. (end-of-line 1)
  11740. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11741. (setq ind (save-excursion
  11742. (if (ignore-errors (goto-char (org-in-item-p)))
  11743. (let ((struct (org-list-struct)))
  11744. (org-list-get-ind
  11745. (org-list-get-top-point struct) struct))
  11746. (skip-chars-backward " \r\t\n")
  11747. (cond
  11748. ((and (org-at-heading-p)
  11749. org-adapt-indentation)
  11750. (1+ (org-current-level)))
  11751. ((org-at-heading-p) 0)
  11752. (t (org-get-indentation))))))
  11753. (setq bul (org-list-bullet-string "-"))
  11754. (org-indent-line-to ind)
  11755. (insert bul (pop lines))
  11756. (let ((ind-body (+ (length bul) ind)))
  11757. (while lines
  11758. (insert "\n")
  11759. (org-indent-line-to ind-body)
  11760. (insert (pop lines))))
  11761. (message "Note stored")
  11762. (org-back-to-heading t)
  11763. (org-cycle-hide-drawers 'children))))))
  11764. (set-window-configuration org-log-note-window-configuration)
  11765. (with-current-buffer (marker-buffer org-log-note-return-to)
  11766. (goto-char org-log-note-return-to))
  11767. (move-marker org-log-note-return-to nil)
  11768. (and org-log-post-message (message "%s" org-log-post-message)))
  11769. (defun org-remove-empty-drawer-at (drawer pos)
  11770. "Remove an empty drawer DRAWER at position POS.
  11771. POS may also be a marker."
  11772. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11773. (save-excursion
  11774. (save-restriction
  11775. (widen)
  11776. (goto-char pos)
  11777. (if (org-in-regexp
  11778. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11779. (replace-match ""))))))
  11780. (defvar org-ts-type nil)
  11781. (defun org-sparse-tree (&optional arg type)
  11782. "Create a sparse tree, prompt for the details.
  11783. This command can create sparse trees. You first need to select the type
  11784. of match used to create the tree:
  11785. t Show all TODO entries.
  11786. T Show entries with a specific TODO keyword.
  11787. m Show entries selected by a tags/property match.
  11788. p Enter a property name and its value (both with completion on existing
  11789. names/values) and show entries with that property.
  11790. r Show entries matching a regular expression (`/' can be used as well).
  11791. b Show deadlines and scheduled items before a date.
  11792. a Show deadlines and scheduled items after a date.
  11793. d Show deadlines due within `org-deadline-warning-days'.
  11794. D Show deadlines and scheduled items between a date range."
  11795. (interactive "P")
  11796. (let (ans kwd value ts-type)
  11797. (setq type (or type org-sparse-tree-default-date-type))
  11798. (setq org-ts-type type)
  11799. (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"
  11800. (cond ((eq type 'all) "all timestamps")
  11801. ((eq type 'scheduled) "only scheduled")
  11802. ((eq type 'deadline) "only deadline")
  11803. ((eq type 'active) "only active timestamps")
  11804. ((eq type 'inactive) "only inactive timestamps")
  11805. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11806. (t "scheduled/deadline")))
  11807. (setq ans (read-char-exclusive))
  11808. (cond
  11809. ((equal ans ?c)
  11810. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11811. ((equal ans ?d)
  11812. (call-interactively 'org-check-deadlines))
  11813. ((equal ans ?b)
  11814. (call-interactively 'org-check-before-date))
  11815. ((equal ans ?a)
  11816. (call-interactively 'org-check-after-date))
  11817. ((equal ans ?D)
  11818. (call-interactively 'org-check-dates-range))
  11819. ((equal ans ?t)
  11820. (call-interactively 'org-show-todo-tree))
  11821. ((equal ans ?T)
  11822. (org-show-todo-tree '(4)))
  11823. ((member ans '(?T ?m))
  11824. (call-interactively 'org-match-sparse-tree))
  11825. ((member ans '(?p ?P))
  11826. (setq kwd (org-icompleting-read "Property: "
  11827. (mapcar 'list (org-buffer-property-keys))))
  11828. (setq value (org-icompleting-read "Value: "
  11829. (mapcar 'list (org-property-values kwd))))
  11830. (unless (string-match "\\`{.*}\\'" value)
  11831. (setq value (concat "\"" value "\"")))
  11832. (org-match-sparse-tree arg (concat kwd "=" value)))
  11833. ((member ans '(?r ?R ?/))
  11834. (call-interactively 'org-occur))
  11835. (t (error "No such sparse tree command \"%c\"" ans)))))
  11836. (defvar org-occur-highlights nil
  11837. "List of overlays used for occur matches.")
  11838. (make-variable-buffer-local 'org-occur-highlights)
  11839. (defvar org-occur-parameters nil
  11840. "Parameters of the active org-occur calls.
  11841. This is a list, each call to org-occur pushes as cons cell,
  11842. containing the regular expression and the callback, onto the list.
  11843. The list can contain several entries if `org-occur' has been called
  11844. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11845. will only contain one set of parameters. When the highlights are
  11846. removed (for example with `C-c C-c', or with the next edit (depending
  11847. on `org-remove-highlights-with-change'), this variable is emptied
  11848. as well.")
  11849. (make-variable-buffer-local 'org-occur-parameters)
  11850. (defun org-occur (regexp &optional keep-previous callback)
  11851. "Make a compact tree which shows all matches of REGEXP.
  11852. The tree will show the lines where the regexp matches, and all higher
  11853. headlines above the match. It will also show the heading after the match,
  11854. to make sure editing the matching entry is easy.
  11855. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11856. call to `org-occur' will be kept, to allow stacking of calls to this
  11857. command.
  11858. If CALLBACK is non-nil, it is a function which is called to confirm
  11859. that the match should indeed be shown."
  11860. (interactive "sRegexp: \nP")
  11861. (when (equal regexp "")
  11862. (error "Regexp cannot be empty"))
  11863. (unless keep-previous
  11864. (org-remove-occur-highlights nil nil t))
  11865. (push (cons regexp callback) org-occur-parameters)
  11866. (let ((cnt 0))
  11867. (save-excursion
  11868. (goto-char (point-min))
  11869. (if (or (not keep-previous) ; do not want to keep
  11870. (not org-occur-highlights)) ; no previous matches
  11871. ;; hide everything
  11872. (org-overview))
  11873. (while (re-search-forward regexp nil t)
  11874. (when (or (not callback)
  11875. (save-match-data (funcall callback)))
  11876. (setq cnt (1+ cnt))
  11877. (when org-highlight-sparse-tree-matches
  11878. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11879. (org-show-context 'occur-tree))))
  11880. (when org-remove-highlights-with-change
  11881. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11882. nil 'local))
  11883. (unless org-sparse-tree-open-archived-trees
  11884. (org-hide-archived-subtrees (point-min) (point-max)))
  11885. (run-hooks 'org-occur-hook)
  11886. (if (org-called-interactively-p 'interactive)
  11887. (message "%d match(es) for regexp %s" cnt regexp))
  11888. cnt))
  11889. (defun org-occur-next-match (&optional n reset)
  11890. "Function for `next-error-function' to find sparse tree matches.
  11891. N is the number of matches to move, when negative move backwards.
  11892. RESET is entirely ignored - this function always goes back to the
  11893. starting point when no match is found."
  11894. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11895. (search-func (if (< n 0)
  11896. 'previous-single-char-property-change
  11897. 'next-single-char-property-change))
  11898. (n (abs n))
  11899. (pos (point))
  11900. p1)
  11901. (catch 'exit
  11902. (while (setq p1 (funcall search-func (point) 'org-type))
  11903. (when (equal p1 limit)
  11904. (goto-char pos)
  11905. (error "No more matches"))
  11906. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11907. (setq n (1- n))
  11908. (when (= n 0)
  11909. (goto-char p1)
  11910. (throw 'exit (point))))
  11911. (goto-char p1))
  11912. (goto-char p1)
  11913. (error "No more matches"))))
  11914. (defun org-show-context (&optional key)
  11915. "Make sure point and context are visible.
  11916. How much context is shown depends upon the variables
  11917. `org-show-hierarchy-above', `org-show-following-heading',
  11918. `org-show-entry-below' and `org-show-siblings'."
  11919. (let ((heading-p (org-at-heading-p t))
  11920. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11921. (following-p (org-get-alist-option org-show-following-heading key))
  11922. (entry-p (org-get-alist-option org-show-entry-below key))
  11923. (siblings-p (org-get-alist-option org-show-siblings key)))
  11924. (catch 'exit
  11925. ;; Show heading or entry text
  11926. (if (and heading-p (not entry-p))
  11927. (org-flag-heading nil) ; only show the heading
  11928. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11929. (org-show-hidden-entry))) ; show entire entry
  11930. (when following-p
  11931. ;; Show next sibling, or heading below text
  11932. (save-excursion
  11933. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11934. (org-flag-heading nil))))
  11935. (when siblings-p (org-show-siblings))
  11936. (when hierarchy-p
  11937. ;; show all higher headings, possibly with siblings
  11938. (save-excursion
  11939. (while (and (condition-case nil
  11940. (progn (org-up-heading-all 1) t)
  11941. (error nil))
  11942. (not (bobp)))
  11943. (org-flag-heading nil)
  11944. (when siblings-p (org-show-siblings))))))))
  11945. (defvar org-reveal-start-hook nil
  11946. "Hook run before revealing a location.")
  11947. (defun org-reveal (&optional siblings)
  11948. "Show current entry, hierarchy above it, and the following headline.
  11949. This can be used to show a consistent set of context around locations
  11950. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11951. not t for the search context.
  11952. With optional argument SIBLINGS, on each level of the hierarchy all
  11953. siblings are shown. This repairs the tree structure to what it would
  11954. look like when opened with hierarchical calls to `org-cycle'.
  11955. With double optional argument \\[universal-argument] \\[universal-argument], \
  11956. go to the parent and show the
  11957. entire tree."
  11958. (interactive "P")
  11959. (run-hooks 'org-reveal-start-hook)
  11960. (let ((org-show-hierarchy-above t)
  11961. (org-show-following-heading t)
  11962. (org-show-siblings (if siblings t org-show-siblings)))
  11963. (org-show-context nil))
  11964. (when (equal siblings '(16))
  11965. (save-excursion
  11966. (when (org-up-heading-safe)
  11967. (org-show-subtree)
  11968. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11969. (defun org-highlight-new-match (beg end)
  11970. "Highlight from BEG to END and mark the highlight is an occur headline."
  11971. (let ((ov (make-overlay beg end)))
  11972. (overlay-put ov 'face 'secondary-selection)
  11973. (overlay-put ov 'org-type 'org-occur)
  11974. (push ov org-occur-highlights)))
  11975. (defun org-remove-occur-highlights (&optional beg end noremove)
  11976. "Remove the occur highlights from the buffer.
  11977. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11978. from the `before-change-functions' in the current buffer."
  11979. (interactive)
  11980. (unless org-inhibit-highlight-removal
  11981. (mapc 'delete-overlay org-occur-highlights)
  11982. (setq org-occur-highlights nil)
  11983. (setq org-occur-parameters nil)
  11984. (unless noremove
  11985. (remove-hook 'before-change-functions
  11986. 'org-remove-occur-highlights 'local))))
  11987. ;;;; Priorities
  11988. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11989. "Regular expression matching the priority indicator.")
  11990. (defvar org-remove-priority-next-time nil)
  11991. (defun org-priority-up ()
  11992. "Increase the priority of the current item."
  11993. (interactive)
  11994. (org-priority 'up))
  11995. (defun org-priority-down ()
  11996. "Decrease the priority of the current item."
  11997. (interactive)
  11998. (org-priority 'down))
  11999. (defun org-priority (&optional action show)
  12000. "Change the priority of an item.
  12001. ACTION can be `set', `up', `down', or a character."
  12002. (interactive "P")
  12003. (if (equal action '(4))
  12004. (org-show-priority)
  12005. (unless org-enable-priority-commands
  12006. (error "Priority commands are disabled"))
  12007. (setq action (or action 'set))
  12008. (let (current new news have remove)
  12009. (save-excursion
  12010. (org-back-to-heading t)
  12011. (if (looking-at org-priority-regexp)
  12012. (setq current (string-to-char (match-string 2))
  12013. have t))
  12014. (cond
  12015. ((eq action 'remove)
  12016. (setq remove t new ?\ ))
  12017. ((or (eq action 'set)
  12018. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12019. (if (not (eq action 'set))
  12020. (setq new action)
  12021. (message "Priority %c-%c, SPC to remove: "
  12022. org-highest-priority org-lowest-priority)
  12023. (save-match-data
  12024. (setq new (read-char-exclusive))))
  12025. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12026. (= (upcase org-lowest-priority) org-lowest-priority))
  12027. (setq new (upcase new)))
  12028. (cond ((equal new ?\ ) (setq remove t))
  12029. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12030. (error "Priority must be between `%c' and `%c'"
  12031. org-highest-priority org-lowest-priority))))
  12032. ((eq action 'up)
  12033. (setq new (if have
  12034. (1- current) ; normal cycling
  12035. ;; last priority was empty
  12036. (if (eq last-command this-command)
  12037. org-lowest-priority ; wrap around empty to lowest
  12038. ;; default
  12039. (if org-priority-start-cycle-with-default
  12040. org-default-priority
  12041. (1- org-default-priority))))))
  12042. ((eq action 'down)
  12043. (setq new (if have
  12044. (1+ current) ; normal cycling
  12045. ;; last priority was empty
  12046. (if (eq last-command this-command)
  12047. org-highest-priority ; wrap around empty to highest
  12048. ;; default
  12049. (if org-priority-start-cycle-with-default
  12050. org-default-priority
  12051. (1+ org-default-priority))))))
  12052. (t (error "Invalid action")))
  12053. (if (or (< (upcase new) org-highest-priority)
  12054. (> (upcase new) org-lowest-priority))
  12055. (if (and (memq action '(up down))
  12056. (not have) (not (eq last-command this-command)))
  12057. ;; `new' is from default priority
  12058. (error
  12059. "The default can not be set, see `org-default-priority' why")
  12060. ;; normal cycling: `new' is beyond highest/lowest priority
  12061. ;; and is wrapped around to the empty priority
  12062. (setq remove t)))
  12063. (setq news (format "%c" new))
  12064. (if have
  12065. (if remove
  12066. (replace-match "" t t nil 1)
  12067. (replace-match news t t nil 2))
  12068. (if remove
  12069. (error "No priority cookie found in line")
  12070. (let ((case-fold-search nil))
  12071. (looking-at org-todo-line-regexp))
  12072. (if (match-end 2)
  12073. (progn
  12074. (goto-char (match-end 2))
  12075. (insert " [#" news "]"))
  12076. (goto-char (match-beginning 3))
  12077. (insert "[#" news "] "))))
  12078. (org-preserve-lc (org-set-tags nil 'align)))
  12079. (if remove
  12080. (message "Priority removed")
  12081. (message "Priority of current item set to %s" news)))))
  12082. (defun org-show-priority ()
  12083. "Show the priority of the current item.
  12084. This priority is composed of the main priority given with the [#A] cookies,
  12085. and by additional input from the age of a schedules or deadline entry."
  12086. (interactive)
  12087. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12088. (org-get-at-bol 'priority)
  12089. (save-excursion
  12090. (save-match-data
  12091. (beginning-of-line)
  12092. (and (looking-at org-heading-regexp)
  12093. (org-get-priority (match-string 0))))))))
  12094. (message "Priority is %d" (if pri pri -1000))))
  12095. (defun org-get-priority (s)
  12096. "Find priority cookie and return priority."
  12097. (save-match-data
  12098. (if (functionp org-get-priority-function)
  12099. (funcall org-get-priority-function)
  12100. (if (not (string-match org-priority-regexp s))
  12101. (* 1000 (- org-lowest-priority org-default-priority))
  12102. (* 1000 (- org-lowest-priority
  12103. (string-to-char (match-string 2 s))))))))
  12104. ;;;; Tags
  12105. (defvar org-agenda-archives-mode)
  12106. (defvar org-map-continue-from nil
  12107. "Position from where mapping should continue.
  12108. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12109. (defvar org-scanner-tags nil
  12110. "The current tag list while the tags scanner is running.")
  12111. (defvar org-trust-scanner-tags nil
  12112. "Should `org-get-tags-at' use the tags for the scanner.
  12113. This is for internal dynamical scoping only.
  12114. When this is non-nil, the function `org-get-tags-at' will return the value
  12115. of `org-scanner-tags' instead of building the list by itself. This
  12116. can lead to large speed-ups when the tags scanner is used in a file with
  12117. many entries, and when the list of tags is retrieved, for example to
  12118. obtain a list of properties. Building the tags list for each entry in such
  12119. a file becomes an N^2 operation - but with this variable set, it scales
  12120. as N.")
  12121. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12122. "Scan headline tags with inheritance and produce output ACTION.
  12123. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12124. or `agenda' to produce an entry list for an agenda view. It can also be
  12125. a Lisp form or a function that should be called at each matched headline, in
  12126. this case the return value is a list of all return values from these calls.
  12127. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12128. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12129. only lines with a not-done TODO keyword are included in the output.
  12130. This should be the same variable that was scoped into
  12131. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12132. START-LEVEL can be a string with asterisks, reducing the scope to
  12133. headlines matching this string."
  12134. (require 'org-agenda)
  12135. (let* ((re (concat "^"
  12136. (if start-level
  12137. ;; Get the correct level to match
  12138. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12139. org-outline-regexp)
  12140. " *\\(\\<\\("
  12141. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12142. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12143. (props (list 'face 'default
  12144. 'done-face 'org-agenda-done
  12145. 'undone-face 'default
  12146. 'mouse-face 'highlight
  12147. 'org-not-done-regexp org-not-done-regexp
  12148. 'org-todo-regexp org-todo-regexp
  12149. 'org-complex-heading-regexp org-complex-heading-regexp
  12150. 'help-echo
  12151. (format "mouse-2 or RET jump to org file %s"
  12152. (abbreviate-file-name
  12153. (or (buffer-file-name (buffer-base-buffer))
  12154. (buffer-name (buffer-base-buffer)))))))
  12155. (case-fold-search nil)
  12156. (org-map-continue-from nil)
  12157. lspos tags tags-list
  12158. (tags-alist (list (cons 0 org-file-tags)))
  12159. (llast 0) rtn rtn1 level category i txt
  12160. todo marker entry priority)
  12161. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12162. (setq action (list 'lambda nil action)))
  12163. (save-excursion
  12164. (goto-char (point-min))
  12165. (when (eq action 'sparse-tree)
  12166. (org-overview)
  12167. (org-remove-occur-highlights))
  12168. (while (re-search-forward re nil t)
  12169. (setq org-map-continue-from nil)
  12170. (catch :skip
  12171. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12172. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12173. (goto-char (setq lspos (match-beginning 0)))
  12174. (setq level (org-reduced-level (org-outline-level))
  12175. category (org-get-category))
  12176. (setq i llast llast level)
  12177. ;; remove tag lists from same and sublevels
  12178. (while (>= i level)
  12179. (when (setq entry (assoc i tags-alist))
  12180. (setq tags-alist (delete entry tags-alist)))
  12181. (setq i (1- i)))
  12182. ;; add the next tags
  12183. (when tags
  12184. (setq tags (org-split-string tags ":")
  12185. tags-alist
  12186. (cons (cons level tags) tags-alist)))
  12187. ;; compile tags for current headline
  12188. (setq tags-list
  12189. (if org-use-tag-inheritance
  12190. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12191. tags)
  12192. org-scanner-tags tags-list)
  12193. (when org-use-tag-inheritance
  12194. (setcdr (car tags-alist)
  12195. (mapcar (lambda (x)
  12196. (setq x (copy-sequence x))
  12197. (org-add-prop-inherited x))
  12198. (cdar tags-alist))))
  12199. (when (and tags org-use-tag-inheritance
  12200. (or (not (eq t org-use-tag-inheritance))
  12201. org-tags-exclude-from-inheritance))
  12202. ;; selective inheritance, remove uninherited ones
  12203. (setcdr (car tags-alist)
  12204. (org-remove-uninherited-tags (cdar tags-alist))))
  12205. (when (and
  12206. ;; eval matcher only when the todo condition is OK
  12207. (and (or (not todo-only) (member todo org-not-done-keywords))
  12208. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12209. (eval matcher)))
  12210. ;; Call the skipper, but return t if it does not skip,
  12211. ;; so that the `and' form continues evaluating
  12212. (progn
  12213. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12214. t)
  12215. ;; Check if timestamps are deselecting this entry
  12216. (or (not todo-only)
  12217. (and (member todo org-not-done-keywords)
  12218. (or (not org-agenda-tags-todo-honor-ignore-options)
  12219. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12220. ;; select this headline
  12221. (cond
  12222. ((eq action 'sparse-tree)
  12223. (and org-highlight-sparse-tree-matches
  12224. (org-get-heading) (match-end 0)
  12225. (org-highlight-new-match
  12226. (match-beginning 1) (match-end 1)))
  12227. (org-show-context 'tags-tree))
  12228. ((eq action 'agenda)
  12229. (setq txt (org-agenda-format-item
  12230. ""
  12231. (concat
  12232. (if (eq org-tags-match-list-sublevels 'indented)
  12233. (make-string (1- level) ?.) "")
  12234. (org-get-heading))
  12235. level category
  12236. tags-list)
  12237. priority (org-get-priority txt))
  12238. (goto-char lspos)
  12239. (setq marker (org-agenda-new-marker))
  12240. (org-add-props txt props
  12241. 'org-marker marker 'org-hd-marker marker 'org-category category
  12242. 'todo-state todo
  12243. 'priority priority 'type "tagsmatch")
  12244. (push txt rtn))
  12245. ((functionp action)
  12246. (setq org-map-continue-from nil)
  12247. (save-excursion
  12248. (setq rtn1 (funcall action))
  12249. (push rtn1 rtn)))
  12250. (t (error "Invalid action")))
  12251. ;; if we are to skip sublevels, jump to end of subtree
  12252. (unless org-tags-match-list-sublevels
  12253. (org-end-of-subtree t)
  12254. (backward-char 1))))
  12255. ;; Get the correct position from where to continue
  12256. (if org-map-continue-from
  12257. (goto-char org-map-continue-from)
  12258. (and (= (point) lspos) (end-of-line 1)))))
  12259. (when (and (eq action 'sparse-tree)
  12260. (not org-sparse-tree-open-archived-trees))
  12261. (org-hide-archived-subtrees (point-min) (point-max)))
  12262. (nreverse rtn)))
  12263. (defun org-remove-uninherited-tags (tags)
  12264. "Remove all tags that are not inherited from the list TAGS."
  12265. (cond
  12266. ((eq org-use-tag-inheritance t)
  12267. (if org-tags-exclude-from-inheritance
  12268. (org-delete-all org-tags-exclude-from-inheritance tags)
  12269. tags))
  12270. ((not org-use-tag-inheritance) nil)
  12271. ((stringp org-use-tag-inheritance)
  12272. (delq nil (mapcar
  12273. (lambda (x)
  12274. (if (and (string-match org-use-tag-inheritance x)
  12275. (not (member x org-tags-exclude-from-inheritance)))
  12276. x nil))
  12277. tags)))
  12278. ((listp org-use-tag-inheritance)
  12279. (delq nil (mapcar
  12280. (lambda (x)
  12281. (if (member x org-use-tag-inheritance) x nil))
  12282. tags)))))
  12283. (defun org-match-sparse-tree (&optional todo-only match)
  12284. "Create a sparse tree according to tags string MATCH.
  12285. MATCH can contain positive and negative selection of tags, like
  12286. \"+WORK+URGENT-WITHBOSS\".
  12287. If optional argument TODO-ONLY is non-nil, only select lines that are
  12288. also TODO lines."
  12289. (interactive "P")
  12290. (org-agenda-prepare-buffers (list (current-buffer)))
  12291. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12292. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12293. (defvar org-cached-props nil)
  12294. (defun org-cached-entry-get (pom property)
  12295. (if (or (eq t org-use-property-inheritance)
  12296. (and (stringp org-use-property-inheritance)
  12297. (string-match org-use-property-inheritance property))
  12298. (and (listp org-use-property-inheritance)
  12299. (member property org-use-property-inheritance)))
  12300. ;; Caching is not possible, check it directly
  12301. (org-entry-get pom property 'inherit)
  12302. ;; Get all properties, so that we can do complicated checks easily
  12303. (cdr (assoc property (or org-cached-props
  12304. (setq org-cached-props
  12305. (org-entry-properties pom)))))))
  12306. (defun org-global-tags-completion-table (&optional files)
  12307. "Return the list of all tags in all agenda buffer/files.
  12308. Optional FILES argument is a list of files which can be used
  12309. instead of the agenda files."
  12310. (save-excursion
  12311. (org-uniquify
  12312. (delq nil
  12313. (apply 'append
  12314. (mapcar
  12315. (lambda (file)
  12316. (set-buffer (find-file-noselect file))
  12317. (append (org-get-buffer-tags)
  12318. (mapcar (lambda (x) (if (stringp (car-safe x))
  12319. (list (car-safe x)) nil))
  12320. org-tag-alist)))
  12321. (if (and files (car files))
  12322. files
  12323. (org-agenda-files))))))))
  12324. (defun org-make-tags-matcher (match)
  12325. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12326. The variable `todo-only' is scoped dynamically into this function.
  12327. It will be set to t if the matcher restricts matching to TODO entries,
  12328. otherwise will not be touched.
  12329. Returns a cons of the selection string MATCH and the constructed
  12330. lisp form implementing the matcher. The matcher is to be evaluated
  12331. at an Org entry, with point on the headline, and returns t if the
  12332. entry matches the selection string MATCH. The returned lisp form
  12333. references two variables with information about the entry, which
  12334. must be bound around the form's evaluation: todo, the TODO keyword
  12335. at the entry (or nil of none); and tags-list, the list of all tags
  12336. at the entry including inherited ones. Additionally, the category
  12337. of the entry (if any) must be specified as the text property
  12338. 'org-category on the headline.
  12339. See also `org-scan-tags'.
  12340. "
  12341. (declare (special todo-only))
  12342. (unless (boundp 'todo-only)
  12343. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12344. (unless match
  12345. ;; Get a new match request, with completion
  12346. (let ((org-last-tags-completion-table
  12347. (org-global-tags-completion-table)))
  12348. (setq match (org-completing-read-no-i
  12349. "Match: " 'org-tags-completion-function nil nil nil
  12350. 'org-tags-history))))
  12351. ;; Parse the string and create a lisp form
  12352. (let ((match0 match)
  12353. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12354. minus tag mm
  12355. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12356. orterms term orlist re-p str-p level-p level-op time-p
  12357. prop-p pn pv po gv rest)
  12358. (if (string-match "/+" match)
  12359. ;; match contains also a todo-matching request
  12360. (progn
  12361. (setq tagsmatch (substring match 0 (match-beginning 0))
  12362. todomatch (substring match (match-end 0)))
  12363. (if (string-match "^!" todomatch)
  12364. (setq todo-only t todomatch (substring todomatch 1)))
  12365. (if (string-match "^\\s-*$" todomatch)
  12366. (setq todomatch nil)))
  12367. ;; only matching tags
  12368. (setq tagsmatch match todomatch nil))
  12369. ;; Make the tags matcher
  12370. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12371. (setq tagsmatcher t)
  12372. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12373. (while (setq term (pop orterms))
  12374. (while (and (equal (substring term -1) "\\") orterms)
  12375. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12376. (while (string-match re term)
  12377. (setq rest (substring term (match-end 0))
  12378. minus (and (match-end 1)
  12379. (equal (match-string 1 term) "-"))
  12380. tag (save-match-data (replace-regexp-in-string
  12381. "\\\\-" "-"
  12382. (match-string 2 term)))
  12383. re-p (equal (string-to-char tag) ?{)
  12384. level-p (match-end 4)
  12385. prop-p (match-end 5)
  12386. mm (cond
  12387. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12388. (level-p
  12389. (setq level-op (org-op-to-function (match-string 3 term)))
  12390. `(,level-op level ,(string-to-number
  12391. (match-string 4 term))))
  12392. (prop-p
  12393. (setq pn (match-string 5 term)
  12394. po (match-string 6 term)
  12395. pv (match-string 7 term)
  12396. re-p (equal (string-to-char pv) ?{)
  12397. str-p (equal (string-to-char pv) ?\")
  12398. time-p (save-match-data
  12399. (string-match "^\"[[<].*[]>]\"$" pv))
  12400. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12401. (if time-p (setq pv (org-matcher-time pv)))
  12402. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12403. (cond
  12404. ((equal pn "CATEGORY")
  12405. (setq gv '(get-text-property (point) 'org-category)))
  12406. ((equal pn "TODO")
  12407. (setq gv 'todo))
  12408. (t
  12409. (setq gv `(org-cached-entry-get nil ,pn))))
  12410. (if re-p
  12411. (if (eq po 'org<>)
  12412. `(not (string-match ,pv (or ,gv "")))
  12413. `(string-match ,pv (or ,gv "")))
  12414. (if str-p
  12415. `(,po (or ,gv "") ,pv)
  12416. `(,po (string-to-number (or ,gv ""))
  12417. ,(string-to-number pv) ))))
  12418. (t `(member ,tag tags-list)))
  12419. mm (if minus (list 'not mm) mm)
  12420. term rest)
  12421. (push mm tagsmatcher))
  12422. (push (if (> (length tagsmatcher) 1)
  12423. (cons 'and tagsmatcher)
  12424. (car tagsmatcher))
  12425. orlist)
  12426. (setq tagsmatcher nil))
  12427. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12428. (setq tagsmatcher
  12429. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12430. ;; Make the todo matcher
  12431. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12432. (setq todomatcher t)
  12433. (setq orterms (org-split-string todomatch "|") orlist nil)
  12434. (while (setq term (pop orterms))
  12435. (while (string-match re term)
  12436. (setq minus (and (match-end 1)
  12437. (equal (match-string 1 term) "-"))
  12438. kwd (match-string 2 term)
  12439. re-p (equal (string-to-char kwd) ?{)
  12440. term (substring term (match-end 0))
  12441. mm (if re-p
  12442. `(string-match ,(substring kwd 1 -1) todo)
  12443. (list 'equal 'todo kwd))
  12444. mm (if minus (list 'not mm) mm))
  12445. (push mm todomatcher))
  12446. (push (if (> (length todomatcher) 1)
  12447. (cons 'and todomatcher)
  12448. (car todomatcher))
  12449. orlist)
  12450. (setq todomatcher nil))
  12451. (setq todomatcher (if (> (length orlist) 1)
  12452. (cons 'or orlist) (car orlist))))
  12453. ;; Return the string and lisp forms of the matcher
  12454. (setq matcher (if todomatcher
  12455. (list 'and tagsmatcher todomatcher)
  12456. tagsmatcher))
  12457. (when todo-only
  12458. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12459. matcher)))
  12460. (cons match0 matcher)))
  12461. (defun org-op-to-function (op &optional stringp)
  12462. "Turn an operator into the appropriate function."
  12463. (setq op
  12464. (cond
  12465. ((equal op "<" ) '(< string< org-time<))
  12466. ((equal op ">" ) '(> org-string> org-time>))
  12467. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12468. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12469. ((member op '("=" "==")) '(= string= org-time=))
  12470. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12471. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12472. (defun org<> (a b) (not (= a b)))
  12473. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12474. (defun org-string>= (a b) (not (string< a b)))
  12475. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12476. (defun org-string<> (a b) (not (string= a b)))
  12477. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12478. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< 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) (org<> a b)))
  12483. (defun org-2ft (s)
  12484. "Convert S to a floating point time.
  12485. If S is already a number, just return it. If it is a string, parse
  12486. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12487. (cond
  12488. ((numberp s) s)
  12489. ((stringp s)
  12490. (condition-case nil
  12491. (float-time (apply 'encode-time (org-parse-time-string s)))
  12492. (error 0.)))
  12493. (t 0.)))
  12494. (defun org-time-today ()
  12495. "Time in seconds today at 0:00.
  12496. Returns the float number of seconds since the beginning of the
  12497. epoch to the beginning of today (00:00)."
  12498. (float-time (apply 'encode-time
  12499. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12500. (defun org-matcher-time (s)
  12501. "Interpret a time comparison value."
  12502. (save-match-data
  12503. (cond
  12504. ((string= s "<now>") (float-time))
  12505. ((string= s "<today>") (org-time-today))
  12506. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12507. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12508. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12509. (+ (org-time-today)
  12510. (* (string-to-number (match-string 1 s))
  12511. (cdr (assoc (match-string 2 s)
  12512. '(("d" . 86400.0) ("w" . 604800.0)
  12513. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12514. (t (org-2ft s)))))
  12515. (defun org-match-any-p (re list)
  12516. "Does re match any element of list?"
  12517. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12518. (delq nil list))
  12519. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12520. (defvar org-tags-overlay (make-overlay 1 1))
  12521. (org-detach-overlay org-tags-overlay)
  12522. (defun org-get-local-tags-at (&optional pos)
  12523. "Get a list of tags defined in the current headline."
  12524. (org-get-tags-at pos 'local))
  12525. (defun org-get-local-tags ()
  12526. "Get a list of tags defined in the current headline."
  12527. (org-get-tags-at nil 'local))
  12528. (defun org-get-tags-at (&optional pos local)
  12529. "Get a list of all headline tags applicable at POS.
  12530. POS defaults to point. If tags are inherited, the list contains
  12531. the targets in the same sequence as the headlines appear, i.e.
  12532. the tags of the current headline come last.
  12533. When LOCAL is non-nil, only return tags from the current headline,
  12534. ignore inherited ones."
  12535. (interactive)
  12536. (if (and org-trust-scanner-tags
  12537. (or (not pos) (equal pos (point)))
  12538. (not local))
  12539. org-scanner-tags
  12540. (let (tags ltags lastpos parent)
  12541. (save-excursion
  12542. (save-restriction
  12543. (widen)
  12544. (goto-char (or pos (point)))
  12545. (save-match-data
  12546. (catch 'done
  12547. (condition-case nil
  12548. (progn
  12549. (org-back-to-heading t)
  12550. (while (not (equal lastpos (point)))
  12551. (setq lastpos (point))
  12552. (when (looking-at
  12553. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12554. (setq ltags (org-split-string
  12555. (org-match-string-no-properties 1) ":"))
  12556. (when parent
  12557. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12558. (setq tags (append
  12559. (if parent
  12560. (org-remove-uninherited-tags ltags)
  12561. ltags)
  12562. tags)))
  12563. (or org-use-tag-inheritance (throw 'done t))
  12564. (if local (throw 'done t))
  12565. (or (org-up-heading-safe) (error nil))
  12566. (setq parent t)))
  12567. (error nil)))))
  12568. (if local
  12569. tags
  12570. (reverse (delete-dups
  12571. (reverse (append
  12572. (org-remove-uninherited-tags
  12573. org-file-tags) tags)))))))))
  12574. (defun org-add-prop-inherited (s)
  12575. (add-text-properties 0 (length s) '(inherited t) s)
  12576. s)
  12577. (defun org-toggle-tag (tag &optional onoff)
  12578. "Toggle the tag TAG for the current line.
  12579. If ONOFF is `on' or `off', don't toggle but set to this state."
  12580. (let (res current)
  12581. (save-excursion
  12582. (org-back-to-heading t)
  12583. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12584. (point-at-eol) t)
  12585. (progn
  12586. (setq current (match-string 1))
  12587. (replace-match ""))
  12588. (setq current ""))
  12589. (setq current (nreverse (org-split-string current ":")))
  12590. (cond
  12591. ((eq onoff 'on)
  12592. (setq res t)
  12593. (or (member tag current) (push tag current)))
  12594. ((eq onoff 'off)
  12595. (or (not (member tag current)) (setq current (delete tag current))))
  12596. (t (if (member tag current)
  12597. (setq current (delete tag current))
  12598. (setq res t)
  12599. (push tag current))))
  12600. (end-of-line 1)
  12601. (if current
  12602. (progn
  12603. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12604. (org-set-tags nil t))
  12605. (delete-horizontal-space))
  12606. (run-hooks 'org-after-tags-change-hook))
  12607. res))
  12608. (defun org-align-tags-here (to-col)
  12609. ;; Assumes that this is a headline
  12610. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12611. (beginning-of-line 1)
  12612. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12613. (< pos (match-beginning 2)))
  12614. (progn
  12615. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12616. (goto-char (match-beginning 1))
  12617. (insert " ")
  12618. (delete-region (point) (1+ (match-beginning 2)))
  12619. (setq ncol (max (current-column)
  12620. (1+ col)
  12621. (if (> to-col 0)
  12622. to-col
  12623. (- (abs to-col) tags-l))))
  12624. (setq p (point))
  12625. (insert (make-string (- ncol (current-column)) ?\ ))
  12626. (setq ncol (current-column))
  12627. (when indent-tabs-mode (tabify p (point-at-eol)))
  12628. (org-move-to-column (min ncol col) t))
  12629. (goto-char pos))))
  12630. (defun org-set-tags-command (&optional arg just-align)
  12631. "Call the set-tags command for the current entry."
  12632. (interactive "P")
  12633. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12634. (org-set-tags arg just-align)
  12635. (save-excursion
  12636. (org-back-to-heading t)
  12637. (org-set-tags arg just-align))))
  12638. (defun org-set-tags-to (data)
  12639. "Set the tags of the current entry to DATA, replacing the current tags.
  12640. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12641. If DATA is nil or the empty string, any tags will be removed."
  12642. (interactive "sTags: ")
  12643. (setq data
  12644. (cond
  12645. ((eq data nil) "")
  12646. ((equal data "") "")
  12647. ((stringp data)
  12648. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12649. ":"))
  12650. ((listp data)
  12651. (concat ":" (mapconcat 'identity data ":") ":"))))
  12652. (when data
  12653. (save-excursion
  12654. (org-back-to-heading t)
  12655. (when (looking-at org-complex-heading-regexp)
  12656. (if (match-end 5)
  12657. (progn
  12658. (goto-char (match-beginning 5))
  12659. (insert data)
  12660. (delete-region (point) (point-at-eol))
  12661. (org-set-tags nil 'align))
  12662. (goto-char (point-at-eol))
  12663. (insert " " data)
  12664. (org-set-tags nil 'align)))
  12665. (beginning-of-line 1)
  12666. (if (looking-at ".*?\\([ \t]+\\)$")
  12667. (delete-region (match-beginning 1) (match-end 1))))))
  12668. (defun org-align-all-tags ()
  12669. "Align the tags i all headings."
  12670. (interactive)
  12671. (save-excursion
  12672. (or (ignore-errors (org-back-to-heading t))
  12673. (outline-next-heading))
  12674. (if (org-at-heading-p)
  12675. (org-set-tags t)
  12676. (message "No headings"))))
  12677. (defvar org-indent-indentation-per-level)
  12678. (defun org-set-tags (&optional arg just-align)
  12679. "Set the tags for the current headline.
  12680. With prefix ARG, realign all tags in headings in the current buffer."
  12681. (interactive "P")
  12682. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12683. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12684. 'region-start-level 'region))
  12685. org-loop-over-headlines-in-active-region)
  12686. (org-map-entries
  12687. ;; We don't use ARG and JUST-ALIGN here these args are not
  12688. ;; useful when looping over headlines
  12689. `(org-set-tags)
  12690. org-loop-over-headlines-in-active-region
  12691. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12692. (let* ((re org-outline-regexp-bol)
  12693. (current (unless arg (org-get-tags-string)))
  12694. (col (current-column))
  12695. (org-setting-tags t)
  12696. table current-tags inherited-tags ; computed below when needed
  12697. tags p0 c0 c1 rpl di tc level)
  12698. (if arg
  12699. (save-excursion
  12700. (goto-char (point-min))
  12701. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12702. (while (re-search-forward re nil t)
  12703. (org-set-tags nil t)
  12704. (end-of-line 1)))
  12705. (message "All tags realigned to column %d" org-tags-column))
  12706. (if just-align
  12707. (setq tags current)
  12708. ;; Get a new set of tags from the user
  12709. (save-excursion
  12710. (setq table (append org-tag-persistent-alist
  12711. (or org-tag-alist (org-get-buffer-tags))
  12712. (and
  12713. org-complete-tags-always-offer-all-agenda-tags
  12714. (org-global-tags-completion-table
  12715. (org-agenda-files))))
  12716. org-last-tags-completion-table table
  12717. current-tags (org-split-string current ":")
  12718. inherited-tags (nreverse
  12719. (nthcdr (length current-tags)
  12720. (nreverse (org-get-tags-at))))
  12721. tags
  12722. (if (or (eq t org-use-fast-tag-selection)
  12723. (and org-use-fast-tag-selection
  12724. (delq nil (mapcar 'cdr table))))
  12725. (org-fast-tag-selection
  12726. current-tags inherited-tags table
  12727. (if org-fast-tag-selection-include-todo
  12728. org-todo-key-alist))
  12729. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12730. (org-trim
  12731. (org-icompleting-read "Tags: "
  12732. 'org-tags-completion-function
  12733. nil nil current 'org-tags-history))))))
  12734. (while (string-match "[-+&]+" tags)
  12735. ;; No boolean logic, just a list
  12736. (setq tags (replace-match ":" t t tags))))
  12737. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12738. (if org-tags-sort-function
  12739. (setq tags (mapconcat 'identity
  12740. (sort (org-split-string
  12741. tags (org-re "[^[:alnum:]_@#%]+"))
  12742. org-tags-sort-function) ":")))
  12743. (if (string-match "\\`[\t ]*\\'" tags)
  12744. (setq tags "")
  12745. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12746. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12747. ;; Insert new tags at the correct column
  12748. (beginning-of-line 1)
  12749. (setq level (or (and (looking-at org-outline-regexp)
  12750. (- (match-end 0) (point) 1))
  12751. 1))
  12752. (cond
  12753. ((and (equal current "") (equal tags "")))
  12754. ((re-search-forward
  12755. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12756. (point-at-eol) t)
  12757. (if (equal tags "")
  12758. (setq rpl "")
  12759. (goto-char (match-beginning 0))
  12760. (setq c0 (current-column)
  12761. ;; compute offset for the case of org-indent-mode active
  12762. di (if org-indent-mode
  12763. (* (1- org-indent-indentation-per-level) (1- level))
  12764. 0)
  12765. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12766. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12767. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12768. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12769. (replace-match rpl t t)
  12770. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12771. tags)
  12772. (t (error "Tags alignment failed")))
  12773. (org-move-to-column col)
  12774. (unless just-align
  12775. (run-hooks 'org-after-tags-change-hook))))))
  12776. (defun org-change-tag-in-region (beg end tag off)
  12777. "Add or remove TAG for each entry in the region.
  12778. This works in the agenda, and also in an org-mode buffer."
  12779. (interactive
  12780. (list (region-beginning) (region-end)
  12781. (let ((org-last-tags-completion-table
  12782. (if (derived-mode-p 'org-mode)
  12783. (org-get-buffer-tags)
  12784. (org-global-tags-completion-table))))
  12785. (org-icompleting-read
  12786. "Tag: " 'org-tags-completion-function nil nil nil
  12787. 'org-tags-history))
  12788. (progn
  12789. (message "[s]et or [r]emove? ")
  12790. (equal (read-char-exclusive) ?r))))
  12791. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12792. (let ((agendap (equal major-mode 'org-agenda-mode))
  12793. l1 l2 m buf pos newhead (cnt 0))
  12794. (goto-char end)
  12795. (setq l2 (1- (org-current-line)))
  12796. (goto-char beg)
  12797. (setq l1 (org-current-line))
  12798. (loop for l from l1 to l2 do
  12799. (org-goto-line l)
  12800. (setq m (get-text-property (point) 'org-hd-marker))
  12801. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12802. (and agendap m))
  12803. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12804. pos (if agendap m (point)))
  12805. (with-current-buffer buf
  12806. (save-excursion
  12807. (save-restriction
  12808. (goto-char pos)
  12809. (setq cnt (1+ cnt))
  12810. (org-toggle-tag tag (if off 'off 'on))
  12811. (setq newhead (org-get-heading)))))
  12812. (and agendap (org-agenda-change-all-lines newhead m))))
  12813. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12814. (defun org-tags-completion-function (string predicate &optional flag)
  12815. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12816. (confirm (lambda (x) (stringp (car x)))))
  12817. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12818. (setq s1 (match-string 1 string)
  12819. s2 (match-string 2 string))
  12820. (setq s1 "" s2 string))
  12821. (cond
  12822. ((eq flag nil)
  12823. ;; try completion
  12824. (setq rtn (try-completion s2 ctable confirm))
  12825. (if (stringp rtn)
  12826. (setq rtn
  12827. (concat s1 s2 (substring rtn (length s2))
  12828. (if (and org-add-colon-after-tag-completion
  12829. (assoc rtn ctable))
  12830. ":" ""))))
  12831. rtn)
  12832. ((eq flag t)
  12833. ;; all-completions
  12834. (all-completions s2 ctable confirm)
  12835. )
  12836. ((eq flag 'lambda)
  12837. ;; exact match?
  12838. (assoc s2 ctable)))
  12839. ))
  12840. (defun org-fast-tag-insert (kwd tags face &optional end)
  12841. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12842. (insert (format "%-12s" (concat kwd ":"))
  12843. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12844. (or end "")))
  12845. (defun org-fast-tag-show-exit (flag)
  12846. (save-excursion
  12847. (org-goto-line 3)
  12848. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12849. (replace-match ""))
  12850. (when flag
  12851. (end-of-line 1)
  12852. (org-move-to-column (- (window-width) 19) t)
  12853. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12854. (defun org-set-current-tags-overlay (current prefix)
  12855. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12856. (if (featurep 'xemacs)
  12857. (org-overlay-display org-tags-overlay (concat prefix s)
  12858. 'secondary-selection)
  12859. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12860. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12861. (defvar org-last-tag-selection-key nil)
  12862. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12863. "Fast tag selection with single keys.
  12864. CURRENT is the current list of tags in the headline, INHERITED is the
  12865. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12866. possibly with grouping information. TODO-TABLE is a similar table with
  12867. TODO keywords, should these have keys assigned to them.
  12868. If the keys are nil, a-z are automatically assigned.
  12869. Returns the new tags string, or nil to not change the current settings."
  12870. (let* ((fulltable (append table todo-table))
  12871. (maxlen (apply 'max (mapcar
  12872. (lambda (x)
  12873. (if (stringp (car x)) (string-width (car x)) 0))
  12874. fulltable)))
  12875. (buf (current-buffer))
  12876. (expert (eq org-fast-tag-selection-single-key 'expert))
  12877. (buffer-tags nil)
  12878. (fwidth (+ maxlen 3 1 3))
  12879. (ncol (/ (- (window-width) 4) fwidth))
  12880. (i-face 'org-done)
  12881. (c-face 'org-todo)
  12882. tg cnt e c char c1 c2 ntable tbl rtn
  12883. ov-start ov-end ov-prefix
  12884. (exit-after-next org-fast-tag-selection-single-key)
  12885. (done-keywords org-done-keywords)
  12886. groups ingroup)
  12887. (save-excursion
  12888. (beginning-of-line 1)
  12889. (if (looking-at
  12890. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12891. (setq ov-start (match-beginning 1)
  12892. ov-end (match-end 1)
  12893. ov-prefix "")
  12894. (setq ov-start (1- (point-at-eol))
  12895. ov-end (1+ ov-start))
  12896. (skip-chars-forward "^\n\r")
  12897. (setq ov-prefix
  12898. (concat
  12899. (buffer-substring (1- (point)) (point))
  12900. (if (> (current-column) org-tags-column)
  12901. " "
  12902. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12903. (move-overlay org-tags-overlay ov-start ov-end)
  12904. (save-window-excursion
  12905. (if expert
  12906. (set-buffer (get-buffer-create " *Org tags*"))
  12907. (delete-other-windows)
  12908. (split-window-vertically)
  12909. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12910. (erase-buffer)
  12911. (org-set-local 'org-done-keywords done-keywords)
  12912. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12913. (org-fast-tag-insert "Current" current c-face "\n\n")
  12914. (org-fast-tag-show-exit exit-after-next)
  12915. (org-set-current-tags-overlay current ov-prefix)
  12916. (setq tbl fulltable char ?a cnt 0)
  12917. (while (setq e (pop tbl))
  12918. (cond
  12919. ((equal (car e) :startgroup)
  12920. (push '() groups) (setq ingroup t)
  12921. (when (not (= cnt 0))
  12922. (setq cnt 0)
  12923. (insert "\n"))
  12924. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12925. ((equal (car e) :endgroup)
  12926. (setq ingroup nil cnt 0)
  12927. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12928. ((equal e '(:newline))
  12929. (when (not (= cnt 0))
  12930. (setq cnt 0)
  12931. (insert "\n")
  12932. (setq e (car tbl))
  12933. (while (equal (car tbl) '(:newline))
  12934. (insert "\n")
  12935. (setq tbl (cdr tbl)))))
  12936. (t
  12937. (setq tg (copy-sequence (car e)) c2 nil)
  12938. (if (cdr e)
  12939. (setq c (cdr e))
  12940. ;; automatically assign a character.
  12941. (setq c1 (string-to-char
  12942. (downcase (substring
  12943. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12944. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12945. (while (or (rassoc char ntable) (rassoc char table))
  12946. (setq char (1+ char)))
  12947. (setq c2 c1))
  12948. (setq c (or c2 char)))
  12949. (if ingroup (push tg (car groups)))
  12950. (setq tg (org-add-props tg nil 'face
  12951. (cond
  12952. ((not (assoc tg table))
  12953. (org-get-todo-face tg))
  12954. ((member tg current) c-face)
  12955. ((member tg inherited) i-face))))
  12956. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12957. (insert "[" c "] " tg (make-string
  12958. (- fwidth 4 (length tg)) ?\ ))
  12959. (push (cons tg c) ntable)
  12960. (when (= (setq cnt (1+ cnt)) ncol)
  12961. (insert "\n")
  12962. (if ingroup (insert " "))
  12963. (setq cnt 0)))))
  12964. (setq ntable (nreverse ntable))
  12965. (insert "\n")
  12966. (goto-char (point-min))
  12967. (if (not expert) (org-fit-window-to-buffer))
  12968. (setq rtn
  12969. (catch 'exit
  12970. (while t
  12971. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12972. (if (not groups) "no " "")
  12973. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12974. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12975. (setq org-last-tag-selection-key c)
  12976. (cond
  12977. ((= c ?\r) (throw 'exit t))
  12978. ((= c ?!)
  12979. (setq groups (not groups))
  12980. (goto-char (point-min))
  12981. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12982. ((= c ?\C-c)
  12983. (if (not expert)
  12984. (org-fast-tag-show-exit
  12985. (setq exit-after-next (not exit-after-next)))
  12986. (setq expert nil)
  12987. (delete-other-windows)
  12988. (set-window-buffer (split-window-vertically) " *Org tags*")
  12989. (org-switch-to-buffer-other-window " *Org tags*")
  12990. (org-fit-window-to-buffer)))
  12991. ((or (= c ?\C-g)
  12992. (and (= c ?q) (not (rassoc c ntable))))
  12993. (org-detach-overlay org-tags-overlay)
  12994. (setq quit-flag t))
  12995. ((= c ?\ )
  12996. (setq current nil)
  12997. (if exit-after-next (setq exit-after-next 'now)))
  12998. ((= c ?\t)
  12999. (condition-case nil
  13000. (setq tg (org-icompleting-read
  13001. "Tag: "
  13002. (or buffer-tags
  13003. (with-current-buffer buf
  13004. (org-get-buffer-tags)))))
  13005. (quit (setq tg "")))
  13006. (when (string-match "\\S-" tg)
  13007. (add-to-list 'buffer-tags (list tg))
  13008. (if (member tg current)
  13009. (setq current (delete tg current))
  13010. (push tg current)))
  13011. (if exit-after-next (setq exit-after-next 'now)))
  13012. ((setq e (rassoc c todo-table) tg (car e))
  13013. (with-current-buffer buf
  13014. (save-excursion (org-todo tg)))
  13015. (if exit-after-next (setq exit-after-next 'now)))
  13016. ((setq e (rassoc c ntable) tg (car e))
  13017. (if (member tg current)
  13018. (setq current (delete tg current))
  13019. (loop for g in groups do
  13020. (if (member tg g)
  13021. (mapc (lambda (x)
  13022. (setq current (delete x current)))
  13023. g)))
  13024. (push tg current))
  13025. (if exit-after-next (setq exit-after-next 'now))))
  13026. ;; Create a sorted list
  13027. (setq current
  13028. (sort current
  13029. (lambda (a b)
  13030. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13031. (if (eq exit-after-next 'now) (throw 'exit t))
  13032. (goto-char (point-min))
  13033. (beginning-of-line 2)
  13034. (delete-region (point) (point-at-eol))
  13035. (org-fast-tag-insert "Current" current c-face)
  13036. (org-set-current-tags-overlay current ov-prefix)
  13037. (while (re-search-forward
  13038. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13039. (setq tg (match-string 1))
  13040. (add-text-properties
  13041. (match-beginning 1) (match-end 1)
  13042. (list 'face
  13043. (cond
  13044. ((member tg current) c-face)
  13045. ((member tg inherited) i-face)
  13046. (t (get-text-property (match-beginning 1) 'face))))))
  13047. (goto-char (point-min)))))
  13048. (org-detach-overlay org-tags-overlay)
  13049. (if rtn
  13050. (mapconcat 'identity current ":")
  13051. nil))))
  13052. (defun org-get-tags-string ()
  13053. "Get the TAGS string in the current headline."
  13054. (unless (org-at-heading-p t)
  13055. (error "Not on a heading"))
  13056. (save-excursion
  13057. (beginning-of-line 1)
  13058. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13059. (org-match-string-no-properties 1)
  13060. "")))
  13061. (defun org-get-tags ()
  13062. "Get the list of tags specified in the current headline."
  13063. (org-split-string (org-get-tags-string) ":"))
  13064. (defun org-get-buffer-tags ()
  13065. "Get a table of all tags used in the buffer, for completion."
  13066. (let (tags)
  13067. (save-excursion
  13068. (goto-char (point-min))
  13069. (while (re-search-forward
  13070. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13071. (when (equal (char-after (point-at-bol 0)) ?*)
  13072. (mapc (lambda (x) (add-to-list 'tags x))
  13073. (org-split-string (org-match-string-no-properties 1) ":")))))
  13074. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13075. (mapcar 'list tags)))
  13076. ;;;; The mapping API
  13077. (defun org-map-entries (func &optional match scope &rest skip)
  13078. "Call FUNC at each headline selected by MATCH in SCOPE.
  13079. FUNC is a function or a lisp form. The function will be called without
  13080. arguments, with the cursor positioned at the beginning of the headline.
  13081. The return values of all calls to the function will be collected and
  13082. returned as a list.
  13083. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13084. does not need to preserve point. After evaluation, the cursor will be
  13085. moved to the end of the line (presumably of the headline of the
  13086. processed entry) and search continues from there. Under some
  13087. circumstances, this may not produce the wanted results. For example,
  13088. if you have removed (e.g. archived) the current (sub)tree it could
  13089. mean that the next entry will be skipped entirely. In such cases, you
  13090. can specify the position from where search should continue by making
  13091. FUNC set the variable `org-map-continue-from' to the desired buffer
  13092. position.
  13093. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13094. Only headlines that are matched by this query will be considered during
  13095. the iteration. When MATCH is nil or t, all headlines will be
  13096. visited by the iteration.
  13097. SCOPE determines the scope of this command. It can be any of:
  13098. nil The current buffer, respecting the restriction if any
  13099. tree The subtree started with the entry at point
  13100. region The entries within the active region, if any
  13101. region-start-level
  13102. The entries within the active region, but only those at
  13103. the same level than the first one.
  13104. file The current buffer, without restriction
  13105. file-with-archives
  13106. The current buffer, and any archives associated with it
  13107. agenda All agenda files
  13108. agenda-with-archives
  13109. All agenda files with any archive files associated with them
  13110. \(file1 file2 ...)
  13111. If this is a list, all files in the list will be scanned
  13112. The remaining args are treated as settings for the skipping facilities of
  13113. the scanner. The following items can be given here:
  13114. archive skip trees with the archive tag.
  13115. comment skip trees with the COMMENT keyword
  13116. function or Emacs Lisp form:
  13117. will be used as value for `org-agenda-skip-function', so whenever
  13118. the function returns t, FUNC will not be called for that
  13119. entry and search will continue from the point where the
  13120. function leaves it.
  13121. If your function needs to retrieve the tags including inherited tags
  13122. at the *current* entry, you can use the value of the variable
  13123. `org-scanner-tags' which will be much faster than getting the value
  13124. with `org-get-tags-at'. If your function gets properties with
  13125. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13126. to t around the call to `org-entry-properties' to get the same speedup.
  13127. Note that if your function moves around to retrieve tags and properties at
  13128. a *different* entry, you cannot use these techniques."
  13129. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13130. (not (org-region-active-p)))
  13131. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13132. (org-agenda-skip-archived-trees (memq 'archive skip))
  13133. (org-agenda-skip-comment-trees (memq 'comment skip))
  13134. (org-agenda-skip-function
  13135. (car (org-delete-all '(comment archive) skip)))
  13136. (org-tags-match-list-sublevels t)
  13137. (start-level (eq scope 'region-start-level))
  13138. matcher file res
  13139. org-todo-keywords-for-agenda
  13140. org-done-keywords-for-agenda
  13141. org-todo-keyword-alist-for-agenda
  13142. org-drawers-for-agenda
  13143. org-tag-alist-for-agenda
  13144. todo-only)
  13145. (cond
  13146. ((eq match t) (setq matcher t))
  13147. ((eq match nil) (setq matcher t))
  13148. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13149. (save-excursion
  13150. (save-restriction
  13151. (cond ((eq scope 'tree)
  13152. (org-back-to-heading t)
  13153. (org-narrow-to-subtree)
  13154. (setq scope nil))
  13155. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13156. (org-region-active-p))
  13157. ;; If needed, set start-level to a string like "2"
  13158. (when start-level
  13159. (save-excursion
  13160. (goto-char (region-beginning))
  13161. (unless (org-at-heading-p) (outline-next-heading))
  13162. (setq start-level (org-current-level))))
  13163. (narrow-to-region (region-beginning)
  13164. (save-excursion
  13165. (goto-char (region-end))
  13166. (unless (and (bolp) (org-at-heading-p))
  13167. (outline-next-heading))
  13168. (point)))
  13169. (setq scope nil)))
  13170. (if (not scope)
  13171. (progn
  13172. (org-agenda-prepare-buffers
  13173. (list (buffer-file-name (current-buffer))))
  13174. (setq res (org-scan-tags func matcher todo-only start-level)))
  13175. ;; Get the right scope
  13176. (cond
  13177. ((and scope (listp scope) (symbolp (car scope)))
  13178. (setq scope (eval scope)))
  13179. ((eq scope 'agenda)
  13180. (setq scope (org-agenda-files t)))
  13181. ((eq scope 'agenda-with-archives)
  13182. (setq scope (org-agenda-files t))
  13183. (setq scope (org-add-archive-files scope)))
  13184. ((eq scope 'file)
  13185. (setq scope (list (buffer-file-name))))
  13186. ((eq scope 'file-with-archives)
  13187. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13188. (org-agenda-prepare-buffers scope)
  13189. (while (setq file (pop scope))
  13190. (with-current-buffer (org-find-base-buffer-visiting file)
  13191. (save-excursion
  13192. (save-restriction
  13193. (widen)
  13194. (goto-char (point-min))
  13195. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13196. res)))
  13197. ;;;; Properties
  13198. ;;; Setting and retrieving properties
  13199. (defconst org-special-properties
  13200. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13201. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13202. "The special properties valid in Org-mode.
  13203. These are properties that are not defined in the property drawer,
  13204. but in some other way.")
  13205. (defconst org-default-properties
  13206. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13207. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13208. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13209. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13210. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13211. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13212. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13213. "Some properties that are used by Org-mode for various purposes.
  13214. Being in this list makes sure that they are offered for completion.")
  13215. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13216. "Regular expression matching the first line of a property drawer.")
  13217. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13218. "Regular expression matching the last line of a property drawer.")
  13219. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13220. "Regular expression matching the first line of a property drawer.")
  13221. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13222. "Regular expression matching the first line of a property drawer.")
  13223. (defconst org-property-drawer-re
  13224. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13225. org-property-end-re "\\)\n?")
  13226. "Matches an entire property drawer.")
  13227. (defconst org-clock-drawer-re
  13228. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13229. org-property-end-re "\\)\n?")
  13230. "Matches an entire clock drawer.")
  13231. (defsubst org-re-property (property)
  13232. "Return a regexp matching a PROPERTY line.
  13233. Match group 1 will be set to the value."
  13234. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13235. (defsubst org-re-property-keyword (property)
  13236. "Return a regexp matching a PROPERTY line, possibly with no
  13237. value for the property."
  13238. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13239. (defun org-property-action ()
  13240. "Do an action on properties."
  13241. (interactive)
  13242. (let (c)
  13243. (org-at-property-p)
  13244. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13245. (setq c (read-char-exclusive))
  13246. (cond
  13247. ((equal c ?s)
  13248. (call-interactively 'org-set-property))
  13249. ((equal c ?d)
  13250. (call-interactively 'org-delete-property))
  13251. ((equal c ?D)
  13252. (call-interactively 'org-delete-property-globally))
  13253. ((equal c ?c)
  13254. (call-interactively 'org-compute-property-at-point))
  13255. (t (error "No such property action %c" c)))))
  13256. (defun org-inc-effort ()
  13257. "Increment the value of the effort property in the current entry."
  13258. (interactive)
  13259. (org-set-effort nil t))
  13260. (defun org-set-effort (&optional value increment)
  13261. "Set the effort property of the current entry.
  13262. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13263. 10th allowed value.
  13264. When INCREMENT is non-nil, set the property to the next allowed value."
  13265. (interactive "P")
  13266. (if (equal value 0) (setq value 10))
  13267. (let* ((completion-ignore-case t)
  13268. (prop org-effort-property)
  13269. (cur (org-entry-get nil prop))
  13270. (allowed (org-property-get-allowed-values nil prop 'table))
  13271. (existing (mapcar 'list (org-property-values prop)))
  13272. rpl
  13273. (val (cond
  13274. ((stringp value) value)
  13275. ((and allowed (integerp value))
  13276. (or (car (nth (1- value) allowed))
  13277. (car (org-last allowed))))
  13278. ((and allowed increment)
  13279. (or (caadr (member (list cur) allowed))
  13280. (error "Allowed effort values are not set")))
  13281. (allowed
  13282. (message "Select 1-9,0, [RET%s]: %s"
  13283. (if cur (concat "=" cur) "")
  13284. (mapconcat 'car allowed " "))
  13285. (setq rpl (read-char-exclusive))
  13286. (if (equal rpl ?\r)
  13287. cur
  13288. (setq rpl (- rpl ?0))
  13289. (if (equal rpl 0) (setq rpl 10))
  13290. (if (and (> rpl 0) (<= rpl (length allowed)))
  13291. (car (nth (1- rpl) allowed))
  13292. (org-completing-read "Effort: " allowed nil))))
  13293. (t
  13294. (let (org-completion-use-ido org-completion-use-iswitchb)
  13295. (org-completing-read
  13296. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13297. (concat "[" cur "]") "")
  13298. ": ")
  13299. existing nil nil "" nil cur))))))
  13300. (unless (equal (org-entry-get nil prop) val)
  13301. (org-entry-put nil prop val))
  13302. (save-excursion
  13303. (org-back-to-heading t)
  13304. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13305. (message "%s is now %s" prop val)))
  13306. (defun org-at-property-p ()
  13307. "Is cursor inside a property drawer?"
  13308. (save-excursion
  13309. (beginning-of-line 1)
  13310. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13311. (save-match-data ;; Used by calling procedures
  13312. (let ((p (point))
  13313. (range (unless (org-before-first-heading-p)
  13314. (org-get-property-block))))
  13315. (and range (<= (car range) p) (< p (cdr range))))))))
  13316. (defun org-get-property-block (&optional beg end force)
  13317. "Return the (beg . end) range of the body of the property drawer.
  13318. BEG and END are the beginning and end of the current subtree, or of
  13319. the part before the first headline. If they are not given, they will
  13320. be found. If the drawer does not exist and FORCE is non-nil, create
  13321. the drawer."
  13322. (catch 'exit
  13323. (save-excursion
  13324. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13325. (progn (org-back-to-heading t) (point))))
  13326. (end (or end (and (not (outline-next-heading)) (point-max))
  13327. (point))))
  13328. (goto-char beg)
  13329. (if (re-search-forward org-property-start-re end t)
  13330. (setq beg (1+ (match-end 0)))
  13331. (if force
  13332. (save-excursion
  13333. (org-insert-property-drawer)
  13334. (setq end (progn (outline-next-heading) (point))))
  13335. (throw 'exit nil))
  13336. (goto-char beg)
  13337. (if (re-search-forward org-property-start-re end t)
  13338. (setq beg (1+ (match-end 0)))))
  13339. (if (re-search-forward org-property-end-re end t)
  13340. (setq end (match-beginning 0))
  13341. (or force (throw 'exit nil))
  13342. (goto-char beg)
  13343. (setq end beg)
  13344. (org-indent-line)
  13345. (insert ":END:\n"))
  13346. (cons beg end)))))
  13347. (defun org-entry-properties (&optional pom which specific)
  13348. "Get all properties of the entry at point-or-marker POM.
  13349. This includes the TODO keyword, the tags, time strings for deadline,
  13350. scheduled, and clocking, and any additional properties defined in the
  13351. entry. The return value is an alist, keys may occur multiple times
  13352. if the property key was used several times.
  13353. POM may also be nil, in which case the current entry is used.
  13354. If WHICH is nil or `all', get all properties. If WHICH is
  13355. `special' or `standard', only get that subclass. If WHICH
  13356. is a string only get exactly this property. SPECIFIC can be a string, the
  13357. specific property we are interested in. Specifying it can speed
  13358. things up because then unnecessary parsing is avoided."
  13359. (setq which (or which 'all))
  13360. (org-with-point-at pom
  13361. (let ((clockstr (substring org-clock-string 0 -1))
  13362. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13363. (case-fold-search nil)
  13364. beg end range props sum-props key key1 value string clocksum clocksumt)
  13365. (save-excursion
  13366. (when (condition-case nil
  13367. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13368. (error nil))
  13369. (setq beg (point))
  13370. (setq sum-props (get-text-property (point) 'org-summaries))
  13371. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13372. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13373. (outline-next-heading)
  13374. (setq end (point))
  13375. (when (memq which '(all special))
  13376. ;; Get the special properties, like TODO and tags
  13377. (goto-char beg)
  13378. (when (and (or (not specific) (string= specific "TODO"))
  13379. (looking-at org-todo-line-regexp) (match-end 2))
  13380. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13381. (when (and (or (not specific) (string= specific "PRIORITY"))
  13382. (looking-at org-priority-regexp))
  13383. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13384. (when (or (not specific) (string= specific "FILE"))
  13385. (push (cons "FILE" buffer-file-name) props))
  13386. (when (and (or (not specific) (string= specific "TAGS"))
  13387. (setq value (org-get-tags-string))
  13388. (string-match "\\S-" value))
  13389. (push (cons "TAGS" value) props))
  13390. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13391. (setq value (org-get-tags-at)))
  13392. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13393. ":"))
  13394. props))
  13395. (when (or (not specific) (string= specific "BLOCKED"))
  13396. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13397. (when (or (not specific)
  13398. (member specific
  13399. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13400. "TIMESTAMP" "TIMESTAMP_IA")))
  13401. (catch 'match
  13402. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13403. (setq key (if (match-end 1)
  13404. (substring (org-match-string-no-properties 1)
  13405. 0 -1))
  13406. string (if (equal key clockstr)
  13407. (org-trim
  13408. (buffer-substring-no-properties
  13409. (match-beginning 3) (goto-char
  13410. (point-at-eol))))
  13411. (substring (org-match-string-no-properties 3)
  13412. 1 -1)))
  13413. ;; Get the correct property name from the key. This is
  13414. ;; necessary if the user has configured time keywords.
  13415. (setq key1 (concat key ":"))
  13416. (cond
  13417. ((not key)
  13418. (setq key
  13419. (if (= (char-after (match-beginning 3)) ?\[)
  13420. "TIMESTAMP_IA" "TIMESTAMP")))
  13421. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13422. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13423. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13424. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13425. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13426. (progn
  13427. (push (cons key string) props)
  13428. ;; no need to search further if match is found
  13429. (throw 'match t))
  13430. (when (or (equal key "CLOCK") (not (assoc key props)))
  13431. (push (cons key string) props)))))))
  13432. (when (memq which '(all standard))
  13433. ;; Get the standard properties, like :PROP: ...
  13434. (setq range (org-get-property-block beg end))
  13435. (when range
  13436. (goto-char (car range))
  13437. (while (re-search-forward
  13438. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13439. (cdr range) t)
  13440. (setq key (org-match-string-no-properties 1)
  13441. value (org-trim (or (org-match-string-no-properties 2) "")))
  13442. (unless (member key excluded)
  13443. (push (cons key (or value "")) props)))))
  13444. (if clocksum
  13445. (push (cons "CLOCKSUM"
  13446. (org-columns-number-to-string (/ (float clocksum) 60.)
  13447. 'add_times))
  13448. props))
  13449. (if clocksumt
  13450. (push (cons "CLOCKSUM_T"
  13451. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13452. 'add_times))
  13453. props))
  13454. (unless (assoc "CATEGORY" props)
  13455. (push (cons "CATEGORY" (org-get-category)) props))
  13456. (append sum-props (nreverse props)))))))
  13457. (defun org-entry-get (pom property &optional inherit literal-nil)
  13458. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13459. If INHERIT is non-nil and the entry does not have the property,
  13460. then also check higher levels of the hierarchy.
  13461. If INHERIT is the symbol `selective', use inheritance only if the setting
  13462. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13463. If the property is present but empty, the return value is the empty string.
  13464. If the property is not present at all, nil is returned.
  13465. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13466. do not interpret it as the list atom nil. This is used for inheritance
  13467. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13468. (org-with-point-at pom
  13469. (if (and inherit (if (eq inherit 'selective)
  13470. (org-property-inherit-p property)
  13471. t))
  13472. (org-entry-get-with-inheritance property literal-nil)
  13473. (if (member property org-special-properties)
  13474. ;; We need a special property. Use `org-entry-properties' to
  13475. ;; retrieve it, but specify the wanted property
  13476. (cdr (assoc property (org-entry-properties nil 'special property)))
  13477. (let ((range (org-get-property-block)))
  13478. (when (and range (not (eq (car range) (cdr range))))
  13479. (let* ((props (list (or (assoc property org-file-properties)
  13480. (assoc property org-global-properties)
  13481. (assoc property org-global-properties-fixed))))
  13482. (ap (lambda (key)
  13483. (when (re-search-forward
  13484. (org-re-property key) (cdr range) t)
  13485. (setq props
  13486. (org-update-property-plist
  13487. key
  13488. (if (match-end 1)
  13489. (org-match-string-no-properties 1) "")
  13490. props)))))
  13491. val)
  13492. (goto-char (car range))
  13493. (funcall ap property)
  13494. (goto-char (car range))
  13495. (while (funcall ap (concat property "+")))
  13496. (setq val (cdr (assoc property props)))
  13497. (when val (if literal-nil val (org-not-nil val))))))))))
  13498. (defun org-property-or-variable-value (var &optional inherit)
  13499. "Check if there is a property fixing the value of VAR.
  13500. If yes, return this value. If not, return the current value of the variable."
  13501. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13502. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13503. (read prop)
  13504. (symbol-value var))))
  13505. (defun org-entry-delete (pom property)
  13506. "Delete the property PROPERTY from entry at point-or-marker POM."
  13507. (org-with-point-at pom
  13508. (if (member property org-special-properties)
  13509. nil ; cannot delete these properties.
  13510. (let ((range (org-get-property-block)))
  13511. (if (and range
  13512. (goto-char (car range))
  13513. (re-search-forward
  13514. (org-re-property property)
  13515. (cdr range) t))
  13516. (progn
  13517. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13518. t)
  13519. nil)))))
  13520. ;; Multi-values properties are properties that contain multiple values
  13521. ;; These values are assumed to be single words, separated by whitespace.
  13522. (defun org-entry-add-to-multivalued-property (pom property value)
  13523. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13524. (let* ((old (org-entry-get pom property))
  13525. (values (and old (org-split-string old "[ \t]"))))
  13526. (setq value (org-entry-protect-space value))
  13527. (unless (member value values)
  13528. (setq values (cons value values))
  13529. (org-entry-put pom property
  13530. (mapconcat 'identity values " ")))))
  13531. (defun org-entry-remove-from-multivalued-property (pom property value)
  13532. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13533. (let* ((old (org-entry-get pom property))
  13534. (values (and old (org-split-string old "[ \t]"))))
  13535. (setq value (org-entry-protect-space value))
  13536. (when (member value values)
  13537. (setq values (delete value values))
  13538. (org-entry-put pom property
  13539. (mapconcat 'identity values " ")))))
  13540. (defun org-entry-member-in-multivalued-property (pom property value)
  13541. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13542. (let* ((old (org-entry-get pom property))
  13543. (values (and old (org-split-string old "[ \t]"))))
  13544. (setq value (org-entry-protect-space value))
  13545. (member value values)))
  13546. (defun org-entry-get-multivalued-property (pom property)
  13547. "Return a list of values in a multivalued property."
  13548. (let* ((value (org-entry-get pom property))
  13549. (values (and value (org-split-string value "[ \t]"))))
  13550. (mapcar 'org-entry-restore-space values)))
  13551. (defun org-entry-put-multivalued-property (pom property &rest values)
  13552. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13553. VALUES should be a list of strings. Spaces will be protected."
  13554. (org-entry-put pom property
  13555. (mapconcat 'org-entry-protect-space values " "))
  13556. (let* ((value (org-entry-get pom property))
  13557. (values (and value (org-split-string value "[ \t]"))))
  13558. (mapcar 'org-entry-restore-space values)))
  13559. (defun org-entry-protect-space (s)
  13560. "Protect spaces and newline in string S."
  13561. (while (string-match " " s)
  13562. (setq s (replace-match "%20" t t s)))
  13563. (while (string-match "\n" s)
  13564. (setq s (replace-match "%0A" t t s)))
  13565. s)
  13566. (defun org-entry-restore-space (s)
  13567. "Restore spaces and newline in string S."
  13568. (while (string-match "%20" s)
  13569. (setq s (replace-match " " t t s)))
  13570. (while (string-match "%0A" s)
  13571. (setq s (replace-match "\n" t t s)))
  13572. s)
  13573. (defvar org-entry-property-inherited-from (make-marker)
  13574. "Marker pointing to the entry from where a property was inherited.
  13575. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13576. location of the entry where the inheritance search matched. If there was
  13577. no match, the marker will point nowhere.
  13578. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13579. is set.")
  13580. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13581. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13582. The search will stop at the first ancestor which has the property defined.
  13583. If the value found is \"nil\", return nil to show that the property
  13584. should be considered as undefined (this is the meaning of nil here).
  13585. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13586. (move-marker org-entry-property-inherited-from nil)
  13587. (let (tmp)
  13588. (save-excursion
  13589. (save-restriction
  13590. (widen)
  13591. (catch 'ex
  13592. (while t
  13593. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13594. (or (ignore-errors (org-back-to-heading t))
  13595. (goto-char (point-min)))
  13596. (move-marker org-entry-property-inherited-from (point))
  13597. (throw 'ex tmp))
  13598. (or (ignore-errors (org-up-heading-safe))
  13599. (throw 'ex nil))))))
  13600. (setq tmp (or tmp
  13601. (cdr (assoc property org-file-properties))
  13602. (cdr (assoc property org-global-properties))
  13603. (cdr (assoc property org-global-properties-fixed))))
  13604. (if literal-nil tmp (org-not-nil tmp))))
  13605. (defvar org-property-changed-functions nil
  13606. "Hook called when the value of a property has changed.
  13607. Each hook function should accept two arguments, the name of the property
  13608. and the new value.")
  13609. (defun org-entry-put (pom property value)
  13610. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13611. (org-with-point-at pom
  13612. (org-back-to-heading t)
  13613. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13614. range)
  13615. (cond
  13616. ((equal property "TODO")
  13617. (when (and (stringp value) (string-match "\\S-" value)
  13618. (not (member value org-todo-keywords-1)))
  13619. (error "\"%s\" is not a valid TODO state" value))
  13620. (if (or (not value)
  13621. (not (string-match "\\S-" value)))
  13622. (setq value 'none))
  13623. (org-todo value)
  13624. (org-set-tags nil 'align))
  13625. ((equal property "PRIORITY")
  13626. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13627. (string-to-char value) ?\ ))
  13628. (org-set-tags nil 'align))
  13629. ((equal property "CLOCKSUM")
  13630. (if (not (re-search-forward
  13631. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13632. (error "Cannot find a clock log")
  13633. (goto-char (- (match-end 1) 2))
  13634. (cond
  13635. ((eq value 'earlier) (org-timestamp-down))
  13636. ((eq value 'later) (org-timestamp-up)))
  13637. (org-clock-sum-current-item)))
  13638. ((equal property "SCHEDULED")
  13639. (if (re-search-forward org-scheduled-time-regexp end t)
  13640. (cond
  13641. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13642. ((eq value 'later) (org-timestamp-change 1 'day))
  13643. (t (call-interactively 'org-schedule)))
  13644. (call-interactively 'org-schedule)))
  13645. ((equal property "DEADLINE")
  13646. (if (re-search-forward org-deadline-time-regexp end t)
  13647. (cond
  13648. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13649. ((eq value 'later) (org-timestamp-change 1 'day))
  13650. (t (call-interactively 'org-deadline)))
  13651. (call-interactively 'org-deadline)))
  13652. ((member property org-special-properties)
  13653. (error "The %s property can not yet be set with `org-entry-put'"
  13654. property))
  13655. (t ; a non-special property
  13656. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13657. (setq range (org-get-property-block beg end 'force))
  13658. (goto-char (car range))
  13659. (if (re-search-forward
  13660. (org-re-property-keyword property) (cdr range) t)
  13661. (progn
  13662. (delete-region (match-beginning 0) (match-end 0))
  13663. (goto-char (match-beginning 0)))
  13664. (goto-char (cdr range))
  13665. (insert "\n")
  13666. (backward-char 1)
  13667. (org-indent-line))
  13668. (insert ":" property ":")
  13669. (and value (insert " " value))
  13670. (org-indent-line)))))
  13671. (run-hook-with-args 'org-property-changed-functions property value)))
  13672. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13673. "Get all property keys in the current buffer.
  13674. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13675. like tags and TODO state.
  13676. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13677. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13678. and others.
  13679. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13680. formats in the current buffer."
  13681. (let (rtn range cfmt s p)
  13682. (save-excursion
  13683. (save-restriction
  13684. (widen)
  13685. (goto-char (point-min))
  13686. (while (re-search-forward org-property-start-re nil t)
  13687. (setq range (org-get-property-block))
  13688. (goto-char (car range))
  13689. (while (re-search-forward
  13690. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13691. (cdr range) t)
  13692. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13693. (outline-next-heading))))
  13694. (when include-specials
  13695. (setq rtn (append org-special-properties rtn)))
  13696. (when include-defaults
  13697. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13698. (add-to-list 'rtn org-effort-property))
  13699. (when include-columns
  13700. (save-excursion
  13701. (save-restriction
  13702. (widen)
  13703. (goto-char (point-min))
  13704. (while (re-search-forward
  13705. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13706. nil t)
  13707. (setq cfmt (match-string 2) s 0)
  13708. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13709. cfmt s)
  13710. (setq s (match-end 0)
  13711. p (match-string 1 cfmt))
  13712. (unless (or (equal p "ITEM")
  13713. (member p org-special-properties))
  13714. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13715. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13716. (defun org-property-values (key)
  13717. "Return a list of all values of property KEY in the current buffer."
  13718. (save-excursion
  13719. (save-restriction
  13720. (widen)
  13721. (goto-char (point-min))
  13722. (let ((re (org-re-property key))
  13723. values)
  13724. (while (re-search-forward re nil t)
  13725. (add-to-list 'values (org-trim (match-string 1))))
  13726. (delete "" values)))))
  13727. (defun org-insert-property-drawer ()
  13728. "Insert a property drawer into the current entry."
  13729. (org-back-to-heading t)
  13730. (looking-at org-outline-regexp)
  13731. (let ((indent (if org-adapt-indentation
  13732. (- (match-end 0) (match-beginning 0))
  13733. 0))
  13734. (beg (point))
  13735. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13736. end hiddenp)
  13737. (outline-next-heading)
  13738. (setq end (point))
  13739. (goto-char beg)
  13740. (while (re-search-forward re end t))
  13741. (setq hiddenp (outline-invisible-p))
  13742. (end-of-line 1)
  13743. (and (equal (char-after) ?\n) (forward-char 1))
  13744. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13745. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13746. ;; just skip this line
  13747. (beginning-of-line 2)
  13748. ;; Drawer start, find the end
  13749. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13750. (beginning-of-line 1)))
  13751. (org-skip-over-state-notes)
  13752. (skip-chars-backward " \t\n\r")
  13753. (if (eq (char-before) ?*) (forward-char 1))
  13754. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13755. (beginning-of-line 0)
  13756. (org-indent-to-column indent)
  13757. (beginning-of-line 2)
  13758. (org-indent-to-column indent)
  13759. (beginning-of-line 0)
  13760. (if hiddenp
  13761. (save-excursion
  13762. (org-back-to-heading t)
  13763. (hide-entry))
  13764. (org-flag-drawer t))))
  13765. (defun org-insert-drawer (&optional arg drawer)
  13766. "Insert a drawer at point.
  13767. Optional argument DRAWER, when non-nil, is a string representing
  13768. drawer's name. Otherwise, the user is prompted for a name.
  13769. If a region is active, insert the drawer around that region
  13770. instead.
  13771. Point is left between drawer's boundaries."
  13772. (interactive "P")
  13773. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13774. "LOGBOOK"))
  13775. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13776. ;; internally by Org. They are meant to be inserted
  13777. ;; automatically.
  13778. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13779. ;; Remove system drawers from list. Note: For some reason,
  13780. ;; `org-completing-read' ignores the predicate while
  13781. ;; `completing-read' handles it fine.
  13782. (drawer (if arg "PROPERTIES"
  13783. (or drawer
  13784. (completing-read
  13785. "Drawer: " org-drawers
  13786. (lambda (d) (not (member d system-drawers))))))))
  13787. (cond
  13788. ;; With C-u, fall back on `org-insert-property-drawer'
  13789. (arg (org-insert-property-drawer))
  13790. ;; With an active region, insert a drawer at point.
  13791. ((not (org-region-active-p))
  13792. (progn
  13793. (unless (bolp) (insert "\n"))
  13794. (insert (format ":%s:\n\n:END:\n" drawer))
  13795. (forward-line -2)))
  13796. ;; Otherwise, insert the drawer at point
  13797. (t
  13798. (let ((rbeg (region-beginning))
  13799. (rend (copy-marker (region-end))))
  13800. (unwind-protect
  13801. (progn
  13802. (goto-char rbeg)
  13803. (beginning-of-line)
  13804. (when (save-excursion
  13805. (re-search-forward org-outline-regexp-bol rend t))
  13806. (error "Drawers cannot contain headlines"))
  13807. ;; Position point at the beginning of the first
  13808. ;; non-blank line in region. Insert drawer's opening
  13809. ;; there, then indent it.
  13810. (org-skip-whitespace)
  13811. (beginning-of-line)
  13812. (insert ":" drawer ":\n")
  13813. (forward-line -1)
  13814. (indent-for-tab-command)
  13815. ;; Move point to the beginning of the first blank line
  13816. ;; after the last non-blank line in region. Insert
  13817. ;; drawer's closing, then indent it.
  13818. (goto-char rend)
  13819. (skip-chars-backward " \r\t\n")
  13820. (insert "\n:END:")
  13821. (deactivate-mark t)
  13822. (indent-for-tab-command)
  13823. (unless (eolp) (insert "\n")))
  13824. ;; Clear marker, whatever the outcome of insertion is.
  13825. (set-marker rend nil)))))))
  13826. (defvar org-property-set-functions-alist nil
  13827. "Property set function alist.
  13828. Each entry should have the following format:
  13829. (PROPERTY . READ-FUNCTION)
  13830. The read function will be called with the same argument as
  13831. `org-completing-read'.")
  13832. (defun org-set-property-function (property)
  13833. "Get the function that should be used to set PROPERTY.
  13834. This is computed according to `org-property-set-functions-alist'."
  13835. (or (cdr (assoc property org-property-set-functions-alist))
  13836. 'org-completing-read))
  13837. (defun org-read-property-value (property)
  13838. "Read PROPERTY value from user."
  13839. (let* ((completion-ignore-case t)
  13840. (allowed (org-property-get-allowed-values nil property 'table))
  13841. (cur (org-entry-get nil property))
  13842. (prompt (concat property " value"
  13843. (if (and cur (string-match "\\S-" cur))
  13844. (concat " [" cur "]") "") ": "))
  13845. (set-function (org-set-property-function property))
  13846. (val (if allowed
  13847. (funcall set-function prompt allowed nil
  13848. (not (get-text-property 0 'org-unrestricted
  13849. (caar allowed))))
  13850. (let (org-completion-use-ido org-completion-use-iswitchb)
  13851. (funcall set-function prompt
  13852. (mapcar 'list (org-property-values property))
  13853. nil nil "" nil cur)))))
  13854. (if (equal val "")
  13855. cur
  13856. val)))
  13857. (defvar org-last-set-property nil)
  13858. (defvar org-last-set-property-value nil)
  13859. (defun org-read-property-name ()
  13860. "Read a property name."
  13861. (let* ((completion-ignore-case t)
  13862. (keys (org-buffer-property-keys nil t t))
  13863. (default-prop (or (save-excursion
  13864. (save-match-data
  13865. (beginning-of-line)
  13866. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13867. (null (string= (match-string 1) "END"))
  13868. (match-string 1))))
  13869. org-last-set-property))
  13870. (property (org-icompleting-read
  13871. (concat "Property"
  13872. (if default-prop (concat " [" default-prop "]") "")
  13873. ": ")
  13874. (mapcar 'list keys)
  13875. nil nil nil nil
  13876. default-prop)))
  13877. (if (member property keys)
  13878. property
  13879. (or (cdr (assoc (downcase property)
  13880. (mapcar (lambda (x) (cons (downcase x) x))
  13881. keys)))
  13882. property))))
  13883. (defun org-set-property-and-value (use-last)
  13884. "Allow to set [PROPERTY]: [value] direction from prompt.
  13885. When use-default, don't even ask, just use the last
  13886. \"[PROPERTY]: [value]\" string from the history."
  13887. (interactive "P")
  13888. (let* ((completion-ignore-case t)
  13889. (pv (or (and use-last org-last-set-property-value)
  13890. (org-completing-read
  13891. "Enter a \"[Property]: [value]\" pair: "
  13892. nil nil nil nil nil
  13893. org-last-set-property-value)))
  13894. prop val)
  13895. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13896. (setq prop (match-string 1 pv)
  13897. val (match-string 2 pv))
  13898. (org-set-property prop val))))
  13899. (defun org-set-property (property value)
  13900. "In the current entry, set PROPERTY to VALUE.
  13901. When called interactively, this will prompt for a property name, offering
  13902. completion on existing and default properties. And then it will prompt
  13903. for a value, offering completion either on allowed values (via an inherited
  13904. xxx_ALL property) or on existing values in other instances of this property
  13905. in the current file."
  13906. (interactive (list nil nil))
  13907. (let* ((property (or property (org-read-property-name)))
  13908. (value (or value (org-read-property-value property)))
  13909. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13910. (setq org-last-set-property property)
  13911. (setq org-last-set-property-value (concat property ": " value))
  13912. ;; Possibly postprocess the inserted value:
  13913. (when fn (setq value (funcall fn value)))
  13914. (unless (equal (org-entry-get nil property) value)
  13915. (org-entry-put nil property value))))
  13916. (defun org-delete-property (property)
  13917. "In the current entry, delete PROPERTY."
  13918. (interactive
  13919. (let* ((completion-ignore-case t)
  13920. (prop (org-icompleting-read "Property: "
  13921. (org-entry-properties nil 'standard))))
  13922. (list prop)))
  13923. (message "Property %s %s" property
  13924. (if (org-entry-delete nil property)
  13925. "deleted"
  13926. "was not present in the entry")))
  13927. (defun org-delete-property-globally (property)
  13928. "Remove PROPERTY globally, from all entries."
  13929. (interactive
  13930. (let* ((completion-ignore-case t)
  13931. (prop (org-icompleting-read
  13932. "Globally remove property: "
  13933. (mapcar 'list (org-buffer-property-keys)))))
  13934. (list prop)))
  13935. (save-excursion
  13936. (save-restriction
  13937. (widen)
  13938. (goto-char (point-min))
  13939. (let ((cnt 0))
  13940. (while (re-search-forward
  13941. (org-re-property property)
  13942. nil t)
  13943. (setq cnt (1+ cnt))
  13944. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13945. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13946. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13947. (defun org-compute-property-at-point ()
  13948. "Compute the property at point.
  13949. This looks for an enclosing column format, extracts the operator and
  13950. then applies it to the property in the column format's scope."
  13951. (interactive)
  13952. (unless (org-at-property-p)
  13953. (error "Not at a property"))
  13954. (let ((prop (org-match-string-no-properties 2)))
  13955. (org-columns-get-format-and-top-level)
  13956. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13957. (error "No operator defined for property %s" prop))
  13958. (org-columns-compute prop)))
  13959. (defvar org-property-allowed-value-functions nil
  13960. "Hook for functions supplying allowed values for a specific property.
  13961. The functions must take a single argument, the name of the property, and
  13962. return a flat list of allowed values. If \":ETC\" is one of
  13963. the values, this means that these values are intended as defaults for
  13964. completion, but that other values should be allowed too.
  13965. The functions must return nil if they are not responsible for this
  13966. property.")
  13967. (defun org-property-get-allowed-values (pom property &optional table)
  13968. "Get allowed values for the property PROPERTY.
  13969. When TABLE is non-nil, return an alist that can directly be used for
  13970. completion."
  13971. (let (vals)
  13972. (cond
  13973. ((equal property "TODO")
  13974. (setq vals (org-with-point-at pom
  13975. (append org-todo-keywords-1 '("")))))
  13976. ((equal property "PRIORITY")
  13977. (let ((n org-lowest-priority))
  13978. (while (>= n org-highest-priority)
  13979. (push (char-to-string n) vals)
  13980. (setq n (1- n)))))
  13981. ((member property org-special-properties))
  13982. ((setq vals (run-hook-with-args-until-success
  13983. 'org-property-allowed-value-functions property)))
  13984. (t
  13985. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13986. (when (and vals (string-match "\\S-" vals))
  13987. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13988. (setq vals (mapcar (lambda (x)
  13989. (cond ((stringp x) x)
  13990. ((numberp x) (number-to-string x))
  13991. ((symbolp x) (symbol-name x))
  13992. (t "???")))
  13993. vals)))))
  13994. (when (member ":ETC" vals)
  13995. (setq vals (remove ":ETC" vals))
  13996. (org-add-props (car vals) '(org-unrestricted t)))
  13997. (if table (mapcar 'list vals) vals)))
  13998. (defun org-property-previous-allowed-value (&optional previous)
  13999. "Switch to the next allowed value for this property."
  14000. (interactive)
  14001. (org-property-next-allowed-value t))
  14002. (defun org-property-next-allowed-value (&optional previous)
  14003. "Switch to the next allowed value for this property."
  14004. (interactive)
  14005. (unless (org-at-property-p)
  14006. (error "Not at a property"))
  14007. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14008. (key (match-string 2))
  14009. (value (match-string 3))
  14010. (allowed (or (org-property-get-allowed-values (point) key)
  14011. (and (member value '("[ ]" "[-]" "[X]"))
  14012. '("[ ]" "[X]"))))
  14013. nval)
  14014. (unless allowed
  14015. (error "Allowed values for this property have not been defined"))
  14016. (if previous (setq allowed (reverse allowed)))
  14017. (if (member value allowed)
  14018. (setq nval (car (cdr (member value allowed)))))
  14019. (setq nval (or nval (car allowed)))
  14020. (if (equal nval value)
  14021. (error "Only one allowed value for this property"))
  14022. (org-at-property-p)
  14023. (replace-match (concat " :" key ": " nval) t t)
  14024. (org-indent-line)
  14025. (beginning-of-line 1)
  14026. (skip-chars-forward " \t")
  14027. (when (equal prop org-effort-property)
  14028. (save-excursion
  14029. (org-back-to-heading t)
  14030. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14031. (run-hook-with-args 'org-property-changed-functions key nval)))
  14032. (defun org-find-olp (path &optional this-buffer)
  14033. "Return a marker pointing to the entry at outline path OLP.
  14034. If anything goes wrong, throw an error.
  14035. You can wrap this call to catch the error like this:
  14036. (condition-case msg
  14037. (org-mobile-locate-entry (match-string 4))
  14038. (error (nth 1 msg)))
  14039. The return value will then be either a string with the error message,
  14040. or a marker if everything is OK.
  14041. If THIS-BUFFER is set, the outline path does not contain a file,
  14042. only headings."
  14043. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14044. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14045. (level 1)
  14046. (lmin 1)
  14047. (lmax 1)
  14048. limit re end found pos heading cnt flevel)
  14049. (unless buffer (error "File not found :%s" file))
  14050. (with-current-buffer buffer
  14051. (save-excursion
  14052. (save-restriction
  14053. (widen)
  14054. (setq limit (point-max))
  14055. (goto-char (point-min))
  14056. (while (setq heading (pop path))
  14057. (setq re (format org-complex-heading-regexp-format
  14058. (regexp-quote heading)))
  14059. (setq cnt 0 pos (point))
  14060. (while (re-search-forward re end t)
  14061. (setq level (- (match-end 1) (match-beginning 1)))
  14062. (if (and (>= level lmin) (<= level lmax))
  14063. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14064. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14065. lmax heading))
  14066. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14067. lmax heading))
  14068. (goto-char found)
  14069. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14070. (setq end (save-excursion (org-end-of-subtree t t))))
  14071. (when (org-at-heading-p)
  14072. (point-marker)))))))
  14073. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14074. "Find node HEADING in BUFFER.
  14075. Return a marker to the heading if it was found, or nil if not.
  14076. If POS-ONLY is set, return just the position instead of a marker.
  14077. The heading text must match exact, but it may have a TODO keyword,
  14078. a priority cookie and tags in the standard locations."
  14079. (with-current-buffer (or buffer (current-buffer))
  14080. (save-excursion
  14081. (save-restriction
  14082. (widen)
  14083. (goto-char (point-min))
  14084. (let (case-fold-search)
  14085. (if (re-search-forward
  14086. (format org-complex-heading-regexp-format
  14087. (regexp-quote heading)) nil t)
  14088. (if pos-only
  14089. (match-beginning 0)
  14090. (move-marker (make-marker) (match-beginning 0)))))))))
  14091. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14092. "Find Org node headline HEADING in all .org files in directory DIR.
  14093. When the target headline is found, return a marker to this location."
  14094. (let ((files (directory-files (or dir default-directory)
  14095. nil "\\`[^.#].*\\.org\\'"))
  14096. file visiting m buffer)
  14097. (catch 'found
  14098. (while (setq file (pop files))
  14099. (message "trying %s" file)
  14100. (setq visiting (org-find-base-buffer-visiting file))
  14101. (setq buffer (or visiting (find-file-noselect file)))
  14102. (setq m (org-find-exact-headline-in-buffer
  14103. heading buffer))
  14104. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14105. (and m (throw 'found m))))))
  14106. (defun org-find-entry-with-id (ident)
  14107. "Locate the entry that contains the ID property with exact value IDENT.
  14108. IDENT can be a string, a symbol or a number, this function will search for
  14109. the string representation of it.
  14110. Return the position where this entry starts, or nil if there is no such entry."
  14111. (interactive "sID: ")
  14112. (let ((id (cond
  14113. ((stringp ident) ident)
  14114. ((symbol-name ident) (symbol-name ident))
  14115. ((numberp ident) (number-to-string ident))
  14116. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14117. (case-fold-search nil))
  14118. (save-excursion
  14119. (save-restriction
  14120. (widen)
  14121. (goto-char (point-min))
  14122. (when (re-search-forward
  14123. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14124. nil t)
  14125. (org-back-to-heading t)
  14126. (point))))))
  14127. ;;;; Timestamps
  14128. (defvar org-last-changed-timestamp nil)
  14129. (defvar org-last-inserted-timestamp nil
  14130. "The last time stamp inserted with `org-insert-time-stamp'.")
  14131. (defvar org-time-was-given) ; dynamically scoped parameter
  14132. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14133. (defvar org-ts-what) ; dynamically scoped parameter
  14134. (defun org-time-stamp (arg &optional inactive)
  14135. "Prompt for a date/time and insert a time stamp.
  14136. If the user specifies a time like HH:MM or if this command is
  14137. called with at least one prefix argument, the time stamp contains
  14138. the date and the time. Otherwise, only the date is be included.
  14139. All parts of a date not specified by the user is filled in from
  14140. the current date/time. So if you just press return without
  14141. typing anything, the time stamp will represent the current
  14142. date/time.
  14143. If there is already a timestamp at the cursor, it will be
  14144. modified.
  14145. With two universal prefix arguments, insert an active timestamp
  14146. with the current time without prompting the user."
  14147. (interactive "P")
  14148. (let* ((ts nil)
  14149. (default-time
  14150. ;; Default time is either today, or, when entering a range,
  14151. ;; the range start.
  14152. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14153. (save-excursion
  14154. (re-search-backward
  14155. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14156. (- (point) 20) t)))
  14157. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14158. (current-time)))
  14159. (default-input (and ts (org-get-compact-tod ts)))
  14160. (repeater (save-excursion
  14161. (save-match-data
  14162. (beginning-of-line)
  14163. (when (re-search-forward
  14164. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14165. (save-excursion (progn (end-of-line) (point))) t)
  14166. (match-string 0)))))
  14167. org-time-was-given org-end-time-was-given time)
  14168. (cond
  14169. ((and (org-at-timestamp-p t)
  14170. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14171. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14172. (insert "--")
  14173. (setq time (let ((this-command this-command))
  14174. (org-read-date arg 'totime nil nil
  14175. default-time default-input inactive)))
  14176. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14177. ((org-at-timestamp-p t)
  14178. (setq time (let ((this-command this-command))
  14179. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14180. (when (org-at-timestamp-p t) ; just to get the match data
  14181. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14182. (replace-match "")
  14183. (setq org-last-changed-timestamp
  14184. (org-insert-time-stamp
  14185. time (or org-time-was-given arg)
  14186. inactive nil nil (list org-end-time-was-given)))
  14187. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14188. (setq org-last-changed-timestamp
  14189. (concat (substring org-last-inserted-timestamp 0 -1)
  14190. " " repeater ">"))))
  14191. (message "Timestamp updated"))
  14192. ((equal arg '(16))
  14193. (org-insert-time-stamp (current-time) t))
  14194. (t
  14195. (setq time (let ((this-command this-command))
  14196. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14197. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14198. nil nil (list org-end-time-was-given))))))
  14199. ;; FIXME: can we use this for something else, like computing time differences?
  14200. (defun org-get-compact-tod (s)
  14201. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14202. (let* ((t1 (match-string 1 s))
  14203. (h1 (string-to-number (match-string 2 s)))
  14204. (m1 (string-to-number (match-string 3 s)))
  14205. (t2 (and (match-end 4) (match-string 5 s)))
  14206. (h2 (and t2 (string-to-number (match-string 6 s))))
  14207. (m2 (and t2 (string-to-number (match-string 7 s))))
  14208. dh dm)
  14209. (if (not t2)
  14210. t1
  14211. (setq dh (- h2 h1) dm (- m2 m1))
  14212. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14213. (concat t1 "+" (number-to-string dh)
  14214. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14215. (defun org-time-stamp-inactive (&optional arg)
  14216. "Insert an inactive time stamp.
  14217. An inactive time stamp is enclosed in square brackets instead of angle
  14218. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14219. does not link to the calendar and cannot be changed with the S-cursor keys.
  14220. So these are more for recording a certain time/date."
  14221. (interactive "P")
  14222. (org-time-stamp arg 'inactive))
  14223. (defvar org-date-ovl (make-overlay 1 1))
  14224. (overlay-put org-date-ovl 'face 'org-date-selected)
  14225. (org-detach-overlay org-date-ovl)
  14226. (defvar org-ans1) ; dynamically scoped parameter
  14227. (defvar org-ans2) ; dynamically scoped parameter
  14228. (defvar org-plain-time-of-day-regexp) ; defined below
  14229. (defvar org-overriding-default-time nil) ; dynamically scoped
  14230. (defvar org-read-date-overlay nil)
  14231. (defvar org-dcst nil) ; dynamically scoped
  14232. (defvar org-read-date-history nil)
  14233. (defvar org-read-date-final-answer nil)
  14234. (defvar org-read-date-analyze-futurep nil)
  14235. (defvar org-read-date-analyze-forced-year nil)
  14236. (defvar org-read-date-inactive)
  14237. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14238. default-time default-input inactive)
  14239. "Read a date, possibly a time, and make things smooth for the user.
  14240. The prompt will suggest to enter an ISO date, but you can also enter anything
  14241. which will at least partially be understood by `parse-time-string'.
  14242. Unrecognized parts of the date will default to the current day, month, year,
  14243. hour and minute. If this command is called to replace a timestamp at point,
  14244. or to enter the second timestamp of a range, the default time is taken
  14245. from the existing stamp. Furthermore, the command prefers the future,
  14246. so if you are giving a date where the year is not given, and the day-month
  14247. combination is already past in the current year, it will assume you
  14248. mean next year. For details, see the manual. A few examples:
  14249. 3-2-5 --> 2003-02-05
  14250. feb 15 --> currentyear-02-15
  14251. 2/15 --> currentyear-02-15
  14252. sep 12 9 --> 2009-09-12
  14253. 12:45 --> today 12:45
  14254. 22 sept 0:34 --> currentyear-09-22 0:34
  14255. 12 --> currentyear-currentmonth-12
  14256. Fri --> nearest Friday (today or later)
  14257. etc.
  14258. Furthermore you can specify a relative date by giving, as the *first* thing
  14259. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14260. change in days weeks, months, years.
  14261. With a single plus or minus, the date is relative to today. With a double
  14262. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14263. +4d --> four days from today
  14264. +4 --> same as above
  14265. +2w --> two weeks from today
  14266. ++5 --> five days from default date
  14267. The function understands only English month and weekday abbreviations.
  14268. While prompting, a calendar is popped up - you can also select the
  14269. date with the mouse (button 1). The calendar shows a period of three
  14270. months. To scroll it to other months, use the keys `>' and `<'.
  14271. If you don't like the calendar, turn it off with
  14272. \(setq org-read-date-popup-calendar nil)
  14273. With optional argument TO-TIME, the date will immediately be converted
  14274. to an internal time.
  14275. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14276. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14277. still enter a time, and this function will inform the calling routine
  14278. about this change. The calling routine may then choose to change the
  14279. format used to insert the time stamp into the buffer to include the time.
  14280. With optional argument FROM-STRING, read from this string instead from
  14281. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14282. the time/date that is used for everything that is not specified by the
  14283. user."
  14284. (require 'parse-time)
  14285. (let* ((org-time-stamp-rounding-minutes
  14286. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14287. (org-dcst org-display-custom-times)
  14288. (ct (org-current-time))
  14289. (org-def (or org-overriding-default-time default-time ct))
  14290. (org-defdecode (decode-time org-def))
  14291. (dummy (progn
  14292. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14293. (setcar (nthcdr 2 org-defdecode) -1)
  14294. (setcar (nthcdr 1 org-defdecode) 59)
  14295. (setq org-def (apply 'encode-time org-defdecode)
  14296. org-defdecode (decode-time org-def)))))
  14297. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14298. (calendar-frame-setup nil)
  14299. (calendar-setup nil)
  14300. (calendar-move-hook nil)
  14301. (calendar-view-diary-initially-flag nil)
  14302. (calendar-view-holidays-initially-flag nil)
  14303. (timestr (format-time-string
  14304. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14305. (prompt (concat (if prompt (concat prompt " ") "")
  14306. (format "Date+time [%s]: " timestr)))
  14307. ans (org-ans0 "") org-ans1 org-ans2 final)
  14308. (cond
  14309. (from-string (setq ans from-string))
  14310. (org-read-date-popup-calendar
  14311. (save-excursion
  14312. (save-window-excursion
  14313. (calendar)
  14314. (org-eval-in-calendar '(setq cursor-type nil) t)
  14315. (unwind-protect
  14316. (progn
  14317. (calendar-forward-day (- (time-to-days org-def)
  14318. (calendar-absolute-from-gregorian
  14319. (calendar-current-date))))
  14320. (org-eval-in-calendar nil t)
  14321. (let* ((old-map (current-local-map))
  14322. (map (copy-keymap calendar-mode-map))
  14323. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14324. (org-defkey map (kbd "RET") 'org-calendar-select)
  14325. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14326. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14327. (org-defkey minibuffer-local-map [(meta shift left)]
  14328. (lambda () (interactive)
  14329. (org-eval-in-calendar '(calendar-backward-month 1))))
  14330. (org-defkey minibuffer-local-map [(meta shift right)]
  14331. (lambda () (interactive)
  14332. (org-eval-in-calendar '(calendar-forward-month 1))))
  14333. (org-defkey minibuffer-local-map [(meta shift up)]
  14334. (lambda () (interactive)
  14335. (org-eval-in-calendar '(calendar-backward-year 1))))
  14336. (org-defkey minibuffer-local-map [(meta shift down)]
  14337. (lambda () (interactive)
  14338. (org-eval-in-calendar '(calendar-forward-year 1))))
  14339. (org-defkey minibuffer-local-map [?\e (shift left)]
  14340. (lambda () (interactive)
  14341. (org-eval-in-calendar '(calendar-backward-month 1))))
  14342. (org-defkey minibuffer-local-map [?\e (shift right)]
  14343. (lambda () (interactive)
  14344. (org-eval-in-calendar '(calendar-forward-month 1))))
  14345. (org-defkey minibuffer-local-map [?\e (shift up)]
  14346. (lambda () (interactive)
  14347. (org-eval-in-calendar '(calendar-backward-year 1))))
  14348. (org-defkey minibuffer-local-map [?\e (shift down)]
  14349. (lambda () (interactive)
  14350. (org-eval-in-calendar '(calendar-forward-year 1))))
  14351. (org-defkey minibuffer-local-map [(shift up)]
  14352. (lambda () (interactive)
  14353. (org-eval-in-calendar '(calendar-backward-week 1))))
  14354. (org-defkey minibuffer-local-map [(shift down)]
  14355. (lambda () (interactive)
  14356. (org-eval-in-calendar '(calendar-forward-week 1))))
  14357. (org-defkey minibuffer-local-map [(shift left)]
  14358. (lambda () (interactive)
  14359. (org-eval-in-calendar '(calendar-backward-day 1))))
  14360. (org-defkey minibuffer-local-map [(shift right)]
  14361. (lambda () (interactive)
  14362. (org-eval-in-calendar '(calendar-forward-day 1))))
  14363. (org-defkey minibuffer-local-map "?"
  14364. (lambda () (interactive)
  14365. (org-eval-in-calendar '(diary-view-entries))
  14366. (message "")))
  14367. (org-defkey minibuffer-local-map ">"
  14368. (lambda () (interactive)
  14369. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14370. (org-defkey minibuffer-local-map "<"
  14371. (lambda () (interactive)
  14372. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14373. (org-defkey minibuffer-local-map "\C-v"
  14374. (lambda () (interactive)
  14375. (org-eval-in-calendar
  14376. '(calendar-scroll-left-three-months 1))))
  14377. (org-defkey minibuffer-local-map "\M-v"
  14378. (lambda () (interactive)
  14379. (org-eval-in-calendar
  14380. '(calendar-scroll-right-three-months 1))))
  14381. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14382. (unwind-protect
  14383. (progn
  14384. (use-local-map map)
  14385. (setq org-read-date-inactive inactive)
  14386. (add-hook 'post-command-hook 'org-read-date-display)
  14387. (setq org-ans0 (read-string prompt default-input
  14388. 'org-read-date-history nil))
  14389. ;; org-ans0: from prompt
  14390. ;; org-ans1: from mouse click
  14391. ;; org-ans2: from calendar motion
  14392. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14393. (remove-hook 'post-command-hook 'org-read-date-display)
  14394. (use-local-map old-map)
  14395. (when org-read-date-overlay
  14396. (delete-overlay org-read-date-overlay)
  14397. (setq org-read-date-overlay nil)))))
  14398. (bury-buffer "*Calendar*")))))
  14399. (t ; Naked prompt only
  14400. (unwind-protect
  14401. (setq ans (read-string prompt default-input
  14402. 'org-read-date-history timestr))
  14403. (when org-read-date-overlay
  14404. (delete-overlay org-read-date-overlay)
  14405. (setq org-read-date-overlay nil)))))
  14406. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14407. (when org-read-date-analyze-forced-year
  14408. (message "Year was forced into %s"
  14409. (if org-read-date-force-compatible-dates
  14410. "compatible range (1970-2037)"
  14411. "range representable on this machine"))
  14412. (ding))
  14413. ;; One round trip to get rid of 34th of August and stuff like that....
  14414. (setq final (decode-time (apply 'encode-time final)))
  14415. (setq org-read-date-final-answer ans)
  14416. (if to-time
  14417. (apply 'encode-time final)
  14418. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14419. (format "%04d-%02d-%02d %02d:%02d"
  14420. (nth 5 final) (nth 4 final) (nth 3 final)
  14421. (nth 2 final) (nth 1 final))
  14422. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14423. (defvar org-def)
  14424. (defvar org-defdecode)
  14425. (defvar org-with-time)
  14426. (defun org-read-date-display ()
  14427. "Display the current date prompt interpretation in the minibuffer."
  14428. (when org-read-date-display-live
  14429. (when org-read-date-overlay
  14430. (delete-overlay org-read-date-overlay))
  14431. (when (minibufferp (current-buffer))
  14432. (save-excursion
  14433. (end-of-line 1)
  14434. (while (not (equal (buffer-substring
  14435. (max (point-min) (- (point) 4)) (point))
  14436. " "))
  14437. (insert " ")))
  14438. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14439. " " (or org-ans1 org-ans2)))
  14440. (org-end-time-was-given nil)
  14441. (f (org-read-date-analyze ans org-def org-defdecode))
  14442. (fmts (if org-dcst
  14443. org-time-stamp-custom-formats
  14444. org-time-stamp-formats))
  14445. (fmt (if (or org-with-time
  14446. (and (boundp 'org-time-was-given) org-time-was-given))
  14447. (cdr fmts)
  14448. (car fmts)))
  14449. (txt (format-time-string fmt (apply 'encode-time f)))
  14450. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14451. (txt (concat "=> " txt)))
  14452. (when (and org-end-time-was-given
  14453. (string-match org-plain-time-of-day-regexp txt))
  14454. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14455. org-end-time-was-given
  14456. (substring txt (match-end 0)))))
  14457. (when org-read-date-analyze-futurep
  14458. (setq txt (concat txt " (=>F)")))
  14459. (setq org-read-date-overlay
  14460. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14461. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14462. (defun org-read-date-analyze (ans org-def org-defdecode)
  14463. "Analyze the combined answer of the date prompt."
  14464. ;; FIXME: cleanup and comment
  14465. (let ((nowdecode (decode-time (current-time)))
  14466. delta deltan deltaw deltadef year month day
  14467. hour minute second wday pm h2 m2 tl wday1
  14468. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14469. (setq org-read-date-analyze-futurep nil
  14470. org-read-date-analyze-forced-year nil)
  14471. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14472. (setq ans "+0"))
  14473. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14474. (setq ans (replace-match "" t t ans)
  14475. deltan (car delta)
  14476. deltaw (nth 1 delta)
  14477. deltadef (nth 2 delta)))
  14478. ;; Check if there is an iso week date in there. If yes, store the
  14479. ;; info and postpone interpreting it until the rest of the parsing
  14480. ;; is done.
  14481. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14482. (setq iso-year (if (match-end 1)
  14483. (org-small-year-to-year
  14484. (string-to-number (match-string 1 ans))))
  14485. iso-weekday (if (match-end 3)
  14486. (string-to-number (match-string 3 ans)))
  14487. iso-week (string-to-number (match-string 2 ans)))
  14488. (setq ans (replace-match "" t t ans)))
  14489. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14490. (when (string-match
  14491. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14492. (setq year (if (match-end 2)
  14493. (string-to-number (match-string 2 ans))
  14494. (progn (setq kill-year t)
  14495. (string-to-number (format-time-string "%Y"))))
  14496. month (string-to-number (match-string 3 ans))
  14497. day (string-to-number (match-string 4 ans)))
  14498. (if (< year 100) (setq year (+ 2000 year)))
  14499. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14500. t nil ans)))
  14501. ;; Help matching dotted european dates
  14502. (when (string-match
  14503. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14504. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14505. (setq kill-year t)
  14506. (string-to-number (format-time-string "%Y")))
  14507. day (string-to-number (match-string 1 ans))
  14508. month (string-to-number (match-string 2 ans))
  14509. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14510. t nil ans)))
  14511. ;; Help matching american dates, like 5/30 or 5/30/7
  14512. (when (string-match
  14513. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14514. (setq year (if (match-end 4)
  14515. (string-to-number (match-string 4 ans))
  14516. (progn (setq kill-year t)
  14517. (string-to-number (format-time-string "%Y"))))
  14518. month (string-to-number (match-string 1 ans))
  14519. day (string-to-number (match-string 2 ans)))
  14520. (if (< year 100) (setq year (+ 2000 year)))
  14521. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14522. t nil ans)))
  14523. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14524. ;; If there is a time with am/pm, and *no* time without it, we convert
  14525. ;; so that matching will be successful.
  14526. (loop for i from 1 to 2 do ; twice, for end time as well
  14527. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14528. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14529. (setq hour (string-to-number (match-string 1 ans))
  14530. minute (if (match-end 3)
  14531. (string-to-number (match-string 3 ans))
  14532. 0)
  14533. pm (equal ?p
  14534. (string-to-char (downcase (match-string 4 ans)))))
  14535. (if (and (= hour 12) (not pm))
  14536. (setq hour 0)
  14537. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14538. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14539. t t ans))))
  14540. ;; Check if a time range is given as a duration
  14541. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14542. (setq hour (string-to-number (match-string 1 ans))
  14543. h2 (+ hour (string-to-number (match-string 3 ans)))
  14544. minute (string-to-number (match-string 2 ans))
  14545. m2 (+ minute (if (match-end 5) (string-to-number
  14546. (match-string 5 ans))0)))
  14547. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14548. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14549. t t ans)))
  14550. ;; Check if there is a time range
  14551. (when (boundp 'org-end-time-was-given)
  14552. (setq org-time-was-given nil)
  14553. (when (and (string-match org-plain-time-of-day-regexp ans)
  14554. (match-end 8))
  14555. (setq org-end-time-was-given (match-string 8 ans))
  14556. (setq ans (concat (substring ans 0 (match-beginning 7))
  14557. (substring ans (match-end 7))))))
  14558. (setq tl (parse-time-string ans)
  14559. day (or (nth 3 tl) (nth 3 org-defdecode))
  14560. month (or (nth 4 tl)
  14561. (if (and org-read-date-prefer-future
  14562. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14563. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14564. (nth 4 org-defdecode)))
  14565. year (or (and (not kill-year) (nth 5 tl))
  14566. (if (and org-read-date-prefer-future
  14567. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14568. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14569. (nth 5 org-defdecode)))
  14570. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14571. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14572. second (or (nth 0 tl) 0)
  14573. wday (nth 6 tl))
  14574. (when (and (eq org-read-date-prefer-future 'time)
  14575. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14576. (equal day (nth 3 nowdecode))
  14577. (equal month (nth 4 nowdecode))
  14578. (equal year (nth 5 nowdecode))
  14579. (nth 2 tl)
  14580. (or (< (nth 2 tl) (nth 2 nowdecode))
  14581. (and (= (nth 2 tl) (nth 2 nowdecode))
  14582. (nth 1 tl)
  14583. (< (nth 1 tl) (nth 1 nowdecode)))))
  14584. (setq day (1+ day)
  14585. futurep t))
  14586. ;; Special date definitions below
  14587. (cond
  14588. (iso-week
  14589. ;; There was an iso week
  14590. (require 'cal-iso)
  14591. (setq futurep nil)
  14592. (setq year (or iso-year year)
  14593. day (or iso-weekday wday 1)
  14594. wday nil ; to make sure that the trigger below does not match
  14595. iso-date (calendar-gregorian-from-absolute
  14596. (calendar-absolute-from-iso
  14597. (list iso-week day year))))
  14598. ; FIXME: Should we also push ISO weeks into the future?
  14599. ; (when (and org-read-date-prefer-future
  14600. ; (not iso-year)
  14601. ; (< (calendar-absolute-from-gregorian iso-date)
  14602. ; (time-to-days (current-time))))
  14603. ; (setq year (1+ year)
  14604. ; iso-date (calendar-gregorian-from-absolute
  14605. ; (calendar-absolute-from-iso
  14606. ; (list iso-week day year)))))
  14607. (setq month (car iso-date)
  14608. year (nth 2 iso-date)
  14609. day (nth 1 iso-date)))
  14610. (deltan
  14611. (setq futurep nil)
  14612. (unless deltadef
  14613. (let ((now (decode-time (current-time))))
  14614. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14615. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14616. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14617. ((equal deltaw "m") (setq month (+ month deltan)))
  14618. ((equal deltaw "y") (setq year (+ year deltan)))))
  14619. ((and wday (not (nth 3 tl)))
  14620. ;; Weekday was given, but no day, so pick that day in the week
  14621. ;; on or after the derived date.
  14622. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14623. (unless (equal wday wday1)
  14624. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14625. (if (and (boundp 'org-time-was-given)
  14626. (nth 2 tl))
  14627. (setq org-time-was-given t))
  14628. (if (< year 100) (setq year (+ 2000 year)))
  14629. ;; Check of the date is representable
  14630. (if org-read-date-force-compatible-dates
  14631. (progn
  14632. (if (< year 1970)
  14633. (setq year 1970 org-read-date-analyze-forced-year t))
  14634. (if (> year 2037)
  14635. (setq year 2037 org-read-date-analyze-forced-year t)))
  14636. (condition-case nil
  14637. (ignore (encode-time second minute hour day month year))
  14638. (error
  14639. (setq year (nth 5 org-defdecode))
  14640. (setq org-read-date-analyze-forced-year t))))
  14641. (setq org-read-date-analyze-futurep futurep)
  14642. (list second minute hour day month year)))
  14643. (defvar parse-time-weekdays)
  14644. (defun org-read-date-get-relative (s today default)
  14645. "Check string S for special relative date string.
  14646. TODAY and DEFAULT are internal times, for today and for a default.
  14647. Return shift list (N what def-flag)
  14648. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14649. N is the number of WHATs to shift.
  14650. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14651. the DEFAULT date rather than TODAY."
  14652. (require 'parse-time)
  14653. (when (and
  14654. (string-match
  14655. (concat
  14656. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14657. "\\([0-9]+\\)?"
  14658. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14659. "\\([ \t]\\|$\\)") s)
  14660. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14661. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14662. (string-to-char (substring (match-string 1 s) -1))
  14663. ?+))
  14664. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14665. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14666. (what (if (match-end 3) (match-string 3 s) "d"))
  14667. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14668. (date (if rel default today))
  14669. (wday (nth 6 (decode-time date)))
  14670. delta)
  14671. (if wday1
  14672. (progn
  14673. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14674. (if (= dir ?-) (setq delta (- delta 7)))
  14675. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14676. (list delta "d" rel))
  14677. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14678. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14679. "Turn a user-specified date into the internal representation.
  14680. The internal representation needed by the calendar is (month day year).
  14681. This is a wrapper to handle the brain-dead convention in calendar that
  14682. user function argument order change dependent on argument order."
  14683. (if (boundp 'calendar-date-style)
  14684. (cond
  14685. ((eq calendar-date-style 'american)
  14686. (list arg1 arg2 arg3))
  14687. ((eq calendar-date-style 'european)
  14688. (list arg2 arg1 arg3))
  14689. ((eq calendar-date-style 'iso)
  14690. (list arg2 arg3 arg1)))
  14691. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14692. (if (org-bound-and-true-p european-calendar-style)
  14693. (list arg2 arg1 arg3)
  14694. (list arg1 arg2 arg3)))))
  14695. (defun org-eval-in-calendar (form &optional keepdate)
  14696. "Eval FORM in the calendar window and return to current window.
  14697. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14698. otherwise stick to the current value of `org-ans2'."
  14699. (let ((sf (selected-frame))
  14700. (sw (selected-window)))
  14701. (select-window (get-buffer-window "*Calendar*" t))
  14702. (eval form)
  14703. (when (and (not keepdate) (calendar-cursor-to-date))
  14704. (let* ((date (calendar-cursor-to-date))
  14705. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14706. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14707. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14708. (select-window sw)
  14709. (org-select-frame-set-input-focus sf)))
  14710. (defun org-calendar-select ()
  14711. "Return to `org-read-date' with the date currently selected.
  14712. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14713. (interactive)
  14714. (when (calendar-cursor-to-date)
  14715. (let* ((date (calendar-cursor-to-date))
  14716. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14717. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14718. (if (active-minibuffer-window) (exit-minibuffer))))
  14719. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14720. "Insert a date stamp for the date given by the internal TIME.
  14721. WITH-HM means use the stamp format that includes the time of the day.
  14722. INACTIVE means use square brackets instead of angular ones, so that the
  14723. stamp will not contribute to the agenda.
  14724. PRE and POST are optional strings to be inserted before and after the
  14725. stamp.
  14726. The command returns the inserted time stamp."
  14727. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14728. stamp)
  14729. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14730. (insert-before-markers (or pre ""))
  14731. (when (listp extra)
  14732. (setq extra (car extra))
  14733. (if (and (stringp extra)
  14734. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14735. (setq extra (format "-%02d:%02d"
  14736. (string-to-number (match-string 1 extra))
  14737. (string-to-number (match-string 2 extra))))
  14738. (setq extra nil)))
  14739. (when extra
  14740. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14741. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14742. (insert-before-markers (or post ""))
  14743. (setq org-last-inserted-timestamp stamp)))
  14744. (defun org-toggle-time-stamp-overlays ()
  14745. "Toggle the use of custom time stamp formats."
  14746. (interactive)
  14747. (setq org-display-custom-times (not org-display-custom-times))
  14748. (unless org-display-custom-times
  14749. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14750. (while (setq p (next-single-property-change p 'display))
  14751. (if (and (get-text-property p 'display)
  14752. (eq (get-text-property p 'face) 'org-date))
  14753. (remove-text-properties
  14754. p (setq p (next-single-property-change p 'display))
  14755. '(display t))))
  14756. (set-buffer-modified-p bmp)))
  14757. (if (featurep 'xemacs)
  14758. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14759. (org-restart-font-lock)
  14760. (setq org-table-may-need-update t)
  14761. (if org-display-custom-times
  14762. (message "Time stamps are overlaid with custom format")
  14763. (message "Time stamp overlays removed")))
  14764. (defun org-display-custom-time (beg end)
  14765. "Overlay modified time stamp format over timestamp between BEG and END."
  14766. (let* ((ts (buffer-substring beg end))
  14767. t1 w1 with-hm tf time str w2 (off 0))
  14768. (save-match-data
  14769. (setq t1 (org-parse-time-string ts t))
  14770. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14771. (setq off (- (match-end 0) (match-beginning 0)))))
  14772. (setq end (- end off))
  14773. (setq w1 (- end beg)
  14774. with-hm (and (nth 1 t1) (nth 2 t1))
  14775. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14776. time (org-fix-decoded-time t1)
  14777. str (org-add-props
  14778. (format-time-string
  14779. (substring tf 1 -1) (apply 'encode-time time))
  14780. nil 'mouse-face 'highlight)
  14781. w2 (length str))
  14782. (if (not (= w2 w1))
  14783. (add-text-properties (1+ beg) (+ 2 beg)
  14784. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14785. (if (featurep 'xemacs)
  14786. (progn
  14787. (put-text-property beg end 'invisible t)
  14788. (put-text-property beg end 'end-glyph (make-glyph str)))
  14789. (put-text-property beg end 'display str))))
  14790. (defun org-translate-time (string)
  14791. "Translate all timestamps in STRING to custom format.
  14792. But do this only if the variable `org-display-custom-times' is set."
  14793. (when org-display-custom-times
  14794. (save-match-data
  14795. (let* ((start 0)
  14796. (re org-ts-regexp-both)
  14797. t1 with-hm inactive tf time str beg end)
  14798. (while (setq start (string-match re string start))
  14799. (setq beg (match-beginning 0)
  14800. end (match-end 0)
  14801. t1 (save-match-data
  14802. (org-parse-time-string (substring string beg end) t))
  14803. with-hm (and (nth 1 t1) (nth 2 t1))
  14804. inactive (equal (substring string beg (1+ beg)) "[")
  14805. tf (funcall (if with-hm 'cdr 'car)
  14806. org-time-stamp-custom-formats)
  14807. time (org-fix-decoded-time t1)
  14808. str (format-time-string
  14809. (concat
  14810. (if inactive "[" "<") (substring tf 1 -1)
  14811. (if inactive "]" ">"))
  14812. (apply 'encode-time time))
  14813. string (replace-match str t t string)
  14814. start (+ start (length str)))))))
  14815. string)
  14816. (defun org-fix-decoded-time (time)
  14817. "Set 0 instead of nil for the first 6 elements of time.
  14818. Don't touch the rest."
  14819. (let ((n 0))
  14820. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14821. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14822. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14823. "Difference between TIMESTAMP-STRING and now in days.
  14824. If SECONDS is non-nil, return the difference in seconds."
  14825. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14826. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14827. (funcall fdiff (current-time)))))
  14828. (defun org-deadline-close (timestamp-string &optional ndays)
  14829. "Is the time in TIMESTAMP-STRING close to the current date?"
  14830. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14831. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14832. (not (org-entry-is-done-p))))
  14833. (defun org-get-wdays (ts)
  14834. "Get the deadline lead time appropriate for timestring TS."
  14835. (cond
  14836. ((<= org-deadline-warning-days 0)
  14837. ;; 0 or negative, enforce this value no matter what
  14838. (- org-deadline-warning-days))
  14839. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14840. ;; lead time is specified.
  14841. (floor (* (string-to-number (match-string 1 ts))
  14842. (cdr (assoc (match-string 2 ts)
  14843. '(("d" . 1) ("w" . 7)
  14844. ("m" . 30.4) ("y" . 365.25)))))))
  14845. ;; go for the default.
  14846. (t org-deadline-warning-days)))
  14847. (defun org-calendar-select-mouse (ev)
  14848. "Return to `org-read-date' with the date currently selected.
  14849. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14850. (interactive "e")
  14851. (mouse-set-point ev)
  14852. (when (calendar-cursor-to-date)
  14853. (let* ((date (calendar-cursor-to-date))
  14854. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14855. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14856. (if (active-minibuffer-window) (exit-minibuffer))))
  14857. (defun org-check-deadlines (ndays)
  14858. "Check if there are any deadlines due or past due.
  14859. A deadline is considered due if it happens within `org-deadline-warning-days'
  14860. days from today's date. If the deadline appears in an entry marked DONE,
  14861. it is not shown. The prefix arg NDAYS can be used to test that many
  14862. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14863. (interactive "P")
  14864. (let* ((org-warn-days
  14865. (cond
  14866. ((equal ndays '(4)) 100000)
  14867. (ndays (prefix-numeric-value ndays))
  14868. (t (abs org-deadline-warning-days))))
  14869. (case-fold-search nil)
  14870. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14871. (callback
  14872. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14873. (message "%d deadlines past-due or due within %d days"
  14874. (org-occur regexp nil callback)
  14875. org-warn-days)))
  14876. (defsubst org-re-timestamp (type)
  14877. "Return a regexp for timestamp TYPE.
  14878. Allowed values for TYPE are:
  14879. all: all timestamps
  14880. active: only active timestamps (<...>)
  14881. inactive: only inactive timestamps ([...])
  14882. scheduled: only scheduled timestamps
  14883. deadline: only deadline timestamps
  14884. When TYPE is nil, fall back on returning a regexp that matches
  14885. both scheduled and deadline timestamps."
  14886. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14887. ((eq type 'active) org-ts-regexp)
  14888. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14889. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14890. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14891. ((eq type 'scheduled-or-deadline)
  14892. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14893. (defun org-check-before-date (date)
  14894. "Check if there are deadlines or scheduled entries before DATE."
  14895. (interactive (list (org-read-date)))
  14896. (let ((case-fold-search nil)
  14897. (regexp (org-re-timestamp org-ts-type))
  14898. (callback
  14899. (lambda () (time-less-p
  14900. (org-time-string-to-time (match-string 1))
  14901. (org-time-string-to-time date)))))
  14902. (message "%d entries before %s"
  14903. (org-occur regexp nil callback) date)))
  14904. (defun org-check-after-date (date)
  14905. "Check if there are deadlines or scheduled entries after DATE."
  14906. (interactive (list (org-read-date)))
  14907. (let ((case-fold-search nil)
  14908. (regexp (org-re-timestamp org-ts-type))
  14909. (callback
  14910. (lambda () (not
  14911. (time-less-p
  14912. (org-time-string-to-time (match-string 1))
  14913. (org-time-string-to-time date))))))
  14914. (message "%d entries after %s"
  14915. (org-occur regexp nil callback) date)))
  14916. (defun org-check-dates-range (start-date end-date)
  14917. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14918. (interactive (list (org-read-date nil nil nil "Range starts")
  14919. (org-read-date nil nil nil "Range end")))
  14920. (let ((case-fold-search nil)
  14921. (regexp (org-re-timestamp org-ts-type))
  14922. (callback
  14923. (lambda ()
  14924. (let ((match (match-string 1)))
  14925. (and
  14926. (not (time-less-p
  14927. (org-time-string-to-time match)
  14928. (org-time-string-to-time start-date)))
  14929. (time-less-p
  14930. (org-time-string-to-time match)
  14931. (org-time-string-to-time end-date)))))))
  14932. (message "%d entries between %s and %s"
  14933. (org-occur regexp nil callback) start-date end-date)))
  14934. (defun org-evaluate-time-range (&optional to-buffer)
  14935. "Evaluate a time range by computing the difference between start and end.
  14936. Normally the result is just printed in the echo area, but with prefix arg
  14937. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14938. If the time range is actually in a table, the result is inserted into the
  14939. next column.
  14940. For time difference computation, a year is assumed to be exactly 365
  14941. days in order to avoid rounding problems."
  14942. (interactive "P")
  14943. (or
  14944. (org-clock-update-time-maybe)
  14945. (save-excursion
  14946. (unless (org-at-date-range-p t)
  14947. (goto-char (point-at-bol))
  14948. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14949. (if (not (org-at-date-range-p t))
  14950. (error "Not at a time-stamp range, and none found in current line")))
  14951. (let* ((ts1 (match-string 1))
  14952. (ts2 (match-string 2))
  14953. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14954. (match-end (match-end 0))
  14955. (time1 (org-time-string-to-time ts1))
  14956. (time2 (org-time-string-to-time ts2))
  14957. (t1 (org-float-time time1))
  14958. (t2 (org-float-time time2))
  14959. (diff (abs (- t2 t1)))
  14960. (negative (< (- t2 t1) 0))
  14961. ;; (ys (floor (* 365 24 60 60)))
  14962. (ds (* 24 60 60))
  14963. (hs (* 60 60))
  14964. (fy "%dy %dd %02d:%02d")
  14965. (fy1 "%dy %dd")
  14966. (fd "%dd %02d:%02d")
  14967. (fd1 "%dd")
  14968. (fh "%02d:%02d")
  14969. y d h m align)
  14970. (if havetime
  14971. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14972. y 0
  14973. d (floor (/ diff ds)) diff (mod diff ds)
  14974. h (floor (/ diff hs)) diff (mod diff hs)
  14975. m (floor (/ diff 60)))
  14976. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14977. y 0
  14978. d (floor (+ (/ diff ds) 0.5))
  14979. h 0 m 0))
  14980. (if (not to-buffer)
  14981. (message "%s" (org-make-tdiff-string y d h m))
  14982. (if (org-at-table-p)
  14983. (progn
  14984. (goto-char match-end)
  14985. (setq align t)
  14986. (and (looking-at " *|") (goto-char (match-end 0))))
  14987. (goto-char match-end))
  14988. (if (looking-at
  14989. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14990. (replace-match ""))
  14991. (if negative (insert " -"))
  14992. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14993. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14994. (insert " " (format fh h m))))
  14995. (if align (org-table-align))
  14996. (message "Time difference inserted")))))
  14997. (defun org-make-tdiff-string (y d h m)
  14998. (let ((fmt "")
  14999. (l nil))
  15000. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15001. l (push y l)))
  15002. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15003. l (push d l)))
  15004. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15005. l (push h l)))
  15006. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15007. l (push m l)))
  15008. (apply 'format fmt (nreverse l))))
  15009. (defun org-time-string-to-time (s &optional buffer pos)
  15010. "Convert a timestamp string into internal time."
  15011. (condition-case errdata
  15012. (apply 'encode-time (org-parse-time-string s))
  15013. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15014. s (if (not (and buffer pos))
  15015. ""
  15016. (format " at %d in buffer `%s'" pos buffer))
  15017. (cdr errdata)))))
  15018. (defun org-time-string-to-seconds (s)
  15019. "Convert a timestamp string to a number of seconds."
  15020. (org-float-time (org-time-string-to-time s)))
  15021. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15022. "Convert a time stamp to an absolute day number.
  15023. If there is a specifier for a cyclic time stamp, get the closest date to
  15024. DAYNR.
  15025. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15026. The variable date is bound by the calendar when this is called."
  15027. (cond
  15028. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15029. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15030. daynr
  15031. (+ daynr 1000)))
  15032. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15033. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15034. (time-to-days (current-time))) (match-string 0 s)
  15035. prefer show-all))
  15036. (t (time-to-days
  15037. (condition-case errdata
  15038. (apply 'encode-time (org-parse-time-string s))
  15039. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15040. s (if (not (and buffer pos))
  15041. ""
  15042. (format " at %d in buffer `%s'" pos buffer))
  15043. (cdr errdata))))))))
  15044. (defun org-days-to-iso-week (days)
  15045. "Return the iso week number."
  15046. (require 'cal-iso)
  15047. (car (calendar-iso-from-absolute days)))
  15048. (defun org-small-year-to-year (year)
  15049. "Convert 2-digit years into 4-digit years.
  15050. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15051. The year 2000 cannot be abbreviated. Any year larger than 99
  15052. is returned unchanged."
  15053. (if (< year 38)
  15054. (setq year (+ 2000 year))
  15055. (if (< year 100)
  15056. (setq year (+ 1900 year))))
  15057. year)
  15058. (defun org-time-from-absolute (d)
  15059. "Return the time corresponding to date D.
  15060. D may be an absolute day number, or a calendar-type list (month day year)."
  15061. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15062. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15063. (defun org-calendar-holiday ()
  15064. "List of holidays, for Diary display in Org-mode."
  15065. (require 'holidays)
  15066. (let ((hl (funcall
  15067. (if (fboundp 'calendar-check-holidays)
  15068. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15069. (if hl (mapconcat 'identity hl "; "))))
  15070. (defun org-diary-sexp-entry (sexp entry date)
  15071. "Process a SEXP diary ENTRY for DATE."
  15072. (require 'diary-lib)
  15073. (let ((result (if calendar-debug-sexp
  15074. (let ((stack-trace-on-error t))
  15075. (eval (car (read-from-string sexp))))
  15076. (condition-case nil
  15077. (eval (car (read-from-string sexp)))
  15078. (error
  15079. (beep)
  15080. (message "Bad sexp at line %d in %s: %s"
  15081. (org-current-line)
  15082. (buffer-file-name) sexp)
  15083. (sleep-for 2))))))
  15084. (cond ((stringp result) (split-string result "; "))
  15085. ((and (consp result)
  15086. (not (consp (cdr result)))
  15087. (stringp (cdr result))) (cdr result))
  15088. ((and (consp result)
  15089. (stringp (car result))) result)
  15090. (result entry))))
  15091. (defun org-diary-to-ical-string (frombuf)
  15092. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15093. This uses the icalendar.el library."
  15094. (let* ((tmpdir (if (featurep 'xemacs)
  15095. (temp-directory)
  15096. temporary-file-directory))
  15097. (tmpfile (make-temp-name
  15098. (expand-file-name "orgics" tmpdir)))
  15099. buf rtn b e)
  15100. (with-current-buffer frombuf
  15101. (icalendar-export-region (point-min) (point-max) tmpfile)
  15102. (setq buf (find-buffer-visiting tmpfile))
  15103. (set-buffer buf)
  15104. (goto-char (point-min))
  15105. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15106. (setq b (match-beginning 0)))
  15107. (goto-char (point-max))
  15108. (if (re-search-backward "^END:VEVENT" nil t)
  15109. (setq e (match-end 0)))
  15110. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15111. (kill-buffer buf)
  15112. (delete-file tmpfile)
  15113. rtn))
  15114. (defun org-closest-date (start current change prefer show-all)
  15115. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15116. When PREFER is `past', return a date that is either CURRENT or past.
  15117. When PREFER is `future', return a date that is either CURRENT or future.
  15118. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15119. ;; Make the proper lists from the dates
  15120. (catch 'exit
  15121. (let ((a1 '(("h" . hour)
  15122. ("d" . day)
  15123. ("w" . week)
  15124. ("m" . month)
  15125. ("y" . year)))
  15126. (shour (nth 2 (org-parse-time-string start)))
  15127. dn dw sday cday n1 n2 n0
  15128. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15129. (setq start (org-date-to-gregorian start)
  15130. current (org-date-to-gregorian
  15131. (if show-all
  15132. current
  15133. (time-to-days (current-time))))
  15134. sday (calendar-absolute-from-gregorian start)
  15135. cday (calendar-absolute-from-gregorian current))
  15136. (if (<= cday sday) (throw 'exit sday))
  15137. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15138. (setq dn (string-to-number (match-string 1 change))
  15139. dw (cdr (assoc (match-string 2 change) a1)))
  15140. (error "Invalid change specifier: %s" change))
  15141. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15142. (cond
  15143. ((eq dw 'hour)
  15144. (let ((missing-hours
  15145. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15146. dn)))
  15147. (setq n1 (if (zerop missing-hours) cday
  15148. (- cday (1+ (floor (/ missing-hours 24)))))
  15149. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15150. ((eq dw 'day)
  15151. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15152. n2 (+ n1 dn)))
  15153. ((eq dw 'year)
  15154. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15155. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15156. (setq date1 (list m d y1)
  15157. n1 (calendar-absolute-from-gregorian date1)
  15158. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15159. n2 (calendar-absolute-from-gregorian date2)))
  15160. ((eq dw 'month)
  15161. ;; approx number of month between the two dates
  15162. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15163. ;; How often does dn fit in there?
  15164. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15165. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15166. m (+ m nm)
  15167. ny (floor (/ m 12))
  15168. y (+ y ny)
  15169. m (- m (* ny 12)))
  15170. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15171. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15172. (setq m2 (+ m dn) y2 y)
  15173. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15174. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15175. (while (<= n2 cday)
  15176. (setq n1 n2 m m2 y y2)
  15177. (setq m2 (+ m dn) y2 y)
  15178. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15179. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15180. ;; Make sure n1 is the earlier date
  15181. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15182. (if show-all
  15183. (cond
  15184. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15185. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15186. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15187. (cond
  15188. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15189. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15190. (t (if (= cday n1) n1 n2)))))))
  15191. (defun org-date-to-gregorian (date)
  15192. "Turn any specification of DATE into a Gregorian date for the calendar."
  15193. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15194. ((and (listp date) (= (length date) 3)) date)
  15195. ((stringp date)
  15196. (setq date (org-parse-time-string date))
  15197. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15198. ((listp date)
  15199. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15200. (defun org-parse-time-string (s &optional nodefault)
  15201. "Parse the standard Org-mode time string.
  15202. This should be a lot faster than the normal `parse-time-string'.
  15203. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15204. hour and minute fields will be nil if not given."
  15205. (cond ((string-match org-ts-regexp0 s)
  15206. (list 0
  15207. (if (or (match-beginning 8) (not nodefault))
  15208. (string-to-number (or (match-string 8 s) "0")))
  15209. (if (or (match-beginning 7) (not nodefault))
  15210. (string-to-number (or (match-string 7 s) "0")))
  15211. (string-to-number (match-string 4 s))
  15212. (string-to-number (match-string 3 s))
  15213. (string-to-number (match-string 2 s))
  15214. nil nil nil))
  15215. ((string-match "^<[^>]+>$" s)
  15216. (decode-time (seconds-to-time (org-matcher-time s))))
  15217. (t (error "Not a standard Org-mode time string: %s" s))))
  15218. (defun org-timestamp-up (&optional arg)
  15219. "Increase the date item at the cursor by one.
  15220. If the cursor is on the year, change the year. If it is on the month,
  15221. the day or the time, change that.
  15222. With prefix ARG, change by that many units."
  15223. (interactive "p")
  15224. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15225. (defun org-timestamp-down (&optional arg)
  15226. "Decrease 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-up-day (&optional arg)
  15233. "Increase the date in the time stamp by one day.
  15234. With prefix ARG, change that many days."
  15235. (interactive "p")
  15236. (if (and (not (org-at-timestamp-p t))
  15237. (org-at-heading-p))
  15238. (org-todo 'up)
  15239. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15240. (defun org-timestamp-down-day (&optional arg)
  15241. "Decrease the date in the time stamp by one day.
  15242. With prefix ARG, change that many days."
  15243. (interactive "p")
  15244. (if (and (not (org-at-timestamp-p t))
  15245. (org-at-heading-p))
  15246. (org-todo 'down)
  15247. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15248. (defun org-at-timestamp-p (&optional inactive-ok)
  15249. "Determine if the cursor is in or at a timestamp."
  15250. (interactive)
  15251. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15252. (pos (point))
  15253. (ans (or (looking-at tsr)
  15254. (save-excursion
  15255. (skip-chars-backward "^[<\n\r\t")
  15256. (if (> (point) (point-min)) (backward-char 1))
  15257. (and (looking-at tsr)
  15258. (> (- (match-end 0) pos) -1))))))
  15259. (and ans
  15260. (boundp 'org-ts-what)
  15261. (setq org-ts-what
  15262. (cond
  15263. ((= pos (match-beginning 0)) 'bracket)
  15264. ;; Point is considered to be "on the bracket" whether
  15265. ;; it's really on it or right after it.
  15266. ((= pos (1- (match-end 0))) 'bracket)
  15267. ((= pos (match-end 0)) 'after)
  15268. ((org-pos-in-match-range pos 2) 'year)
  15269. ((org-pos-in-match-range pos 3) 'month)
  15270. ((org-pos-in-match-range pos 7) 'hour)
  15271. ((org-pos-in-match-range pos 8) 'minute)
  15272. ((or (org-pos-in-match-range pos 4)
  15273. (org-pos-in-match-range pos 5)) 'day)
  15274. ((and (> pos (or (match-end 8) (match-end 5)))
  15275. (< pos (match-end 0)))
  15276. (- pos (or (match-end 8) (match-end 5))))
  15277. (t 'day))))
  15278. ans))
  15279. (defun org-toggle-timestamp-type ()
  15280. "Toggle the type (<active> or [inactive]) of a time stamp."
  15281. (interactive)
  15282. (when (org-at-timestamp-p t)
  15283. (let ((beg (match-beginning 0)) (end (match-end 0))
  15284. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15285. (save-excursion
  15286. (goto-char beg)
  15287. (while (re-search-forward "[][<>]" end t)
  15288. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15289. t t)))
  15290. (message "Timestamp is now %sactive"
  15291. (if (equal (char-after beg) ?<) "" "in")))))
  15292. (defvar org-clock-history) ; defined in org-clock.el
  15293. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15294. (defun org-timestamp-change (n &optional what updown)
  15295. "Change the date in the time stamp at point.
  15296. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15297. `year', `minute', `second'. If WHAT is not given, the cursor position
  15298. in the timestamp determines what will be changed."
  15299. (let ((origin (point)) origin-cat
  15300. with-hm inactive
  15301. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15302. org-ts-what
  15303. extra rem
  15304. ts time time0 fixnext clrgx)
  15305. (if (not (org-at-timestamp-p t))
  15306. (error "Not at a timestamp"))
  15307. (if (and (not what) (eq org-ts-what 'bracket))
  15308. (org-toggle-timestamp-type)
  15309. ;; Point isn't on brackets. Remember the part of the time-stamp
  15310. ;; the point was in. Indeed, size of time-stamps may change,
  15311. ;; but point must be kept in the same category nonetheless.
  15312. (setq origin-cat org-ts-what)
  15313. (if (and (not what) (not (eq org-ts-what 'day))
  15314. org-display-custom-times
  15315. (get-text-property (point) 'display)
  15316. (not (get-text-property (1- (point)) 'display)))
  15317. (setq org-ts-what 'day))
  15318. (setq org-ts-what (or what org-ts-what)
  15319. inactive (= (char-after (match-beginning 0)) ?\[)
  15320. ts (match-string 0))
  15321. (replace-match "")
  15322. (if (string-match
  15323. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15324. ts)
  15325. (setq extra (match-string 1 ts)))
  15326. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15327. (setq with-hm t))
  15328. (setq time0 (org-parse-time-string ts))
  15329. (when (and updown
  15330. (eq org-ts-what 'minute)
  15331. (not current-prefix-arg))
  15332. ;; This looks like s-up and s-down. Change by one rounding step.
  15333. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15334. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15335. (setcar (cdr time0) (+ (nth 1 time0)
  15336. (if (> n 0) (- rem) (- dm rem))))))
  15337. (setq time
  15338. (encode-time (or (car time0) 0)
  15339. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15340. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15341. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15342. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15343. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15344. (nthcdr 6 time0)))
  15345. (when (and (member org-ts-what '(hour minute))
  15346. extra
  15347. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15348. (setq extra (org-modify-ts-extra
  15349. extra
  15350. (if (eq org-ts-what 'hour) 2 5)
  15351. n dm)))
  15352. (when (integerp org-ts-what)
  15353. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15354. (if (eq what 'calendar)
  15355. (let ((cal-date (org-get-date-from-calendar)))
  15356. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15357. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15358. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15359. (setcar time0 (or (car time0) 0))
  15360. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15361. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15362. (setq time (apply 'encode-time time0))))
  15363. ;; Insert the new time-stamp, and ensure point stays in the same
  15364. ;; category as before (i.e. not after the last position in that
  15365. ;; category).
  15366. (let ((pos (point)))
  15367. ;; Stay before inserted string. `save-excursion' is of no use.
  15368. (setq org-last-changed-timestamp
  15369. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15370. (goto-char pos))
  15371. (save-match-data
  15372. (looking-at org-ts-regexp3)
  15373. (goto-char (cond
  15374. ;; `day' category ends before `hour' if any, or at
  15375. ;; the end of the day name.
  15376. ((eq origin-cat 'day)
  15377. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15378. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15379. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15380. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15381. ;; `year' and `month' have both fixed size: point
  15382. ;; couldn't have moved into another part.
  15383. (t origin))))
  15384. ;; Update clock if on a CLOCK line.
  15385. (org-clock-update-time-maybe)
  15386. ;; Maybe adjust the closest clock in `org-clock-history'
  15387. (when org-clock-adjust-closest
  15388. (if (not (and (org-at-clock-log-p)
  15389. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15390. org-clock-history))))))
  15391. (message "No clock to adjust")
  15392. (cond ((save-excursion ; fix previous clock?
  15393. (re-search-backward org-ts-regexp0 nil t)
  15394. (org-looking-back (concat org-clock-string " \\[")))
  15395. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15396. ((save-excursion ; fix next clock?
  15397. (re-search-backward org-ts-regexp0 nil t)
  15398. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15399. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15400. (save-window-excursion
  15401. ;; Find closest clock to point, adjust the previous/next one in history
  15402. (let* ((p (save-excursion (org-back-to-heading t)))
  15403. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15404. (clfixnth
  15405. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15406. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15407. (if (not clfixpos)
  15408. (message "No clock to adjust")
  15409. (save-excursion
  15410. (org-goto-marker-or-bmk clfixpos)
  15411. (org-show-subtree)
  15412. (when (re-search-forward clrgx nil t)
  15413. (goto-char (match-beginning 1))
  15414. (let (org-clock-adjust-closest)
  15415. (org-timestamp-change n org-ts-what updown))
  15416. (message "Clock adjusted in %s for heading: %s"
  15417. (file-name-nondirectory (buffer-file-name))
  15418. (org-get-heading t t)))))))))
  15419. ;; Try to recenter the calendar window, if any.
  15420. (if (and org-calendar-follow-timestamp-change
  15421. (get-buffer-window "*Calendar*" t)
  15422. (memq org-ts-what '(day month year)))
  15423. (org-recenter-calendar (time-to-days time))))))
  15424. (defun org-modify-ts-extra (s pos n dm)
  15425. "Change the different parts of the lead-time and repeat fields in timestamp."
  15426. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15427. ng h m new rem)
  15428. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15429. (cond
  15430. ((or (org-pos-in-match-range pos 2)
  15431. (org-pos-in-match-range pos 3))
  15432. (setq m (string-to-number (match-string 3 s))
  15433. h (string-to-number (match-string 2 s)))
  15434. (if (org-pos-in-match-range pos 2)
  15435. (setq h (+ h n))
  15436. (setq n (* dm (org-no-warnings (signum n))))
  15437. (when (not (= 0 (setq rem (% m dm))))
  15438. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15439. (setq m (+ m n)))
  15440. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15441. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15442. (setq h (min 24 (max 0 h)))
  15443. (setq ng 1 new (format "-%02d:%02d" h m)))
  15444. ((org-pos-in-match-range pos 6)
  15445. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15446. ((org-pos-in-match-range pos 5)
  15447. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15448. ((org-pos-in-match-range pos 9)
  15449. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15450. ((org-pos-in-match-range pos 8)
  15451. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15452. (when ng
  15453. (setq s (concat
  15454. (substring s 0 (match-beginning ng))
  15455. new
  15456. (substring s (match-end ng))))))
  15457. s))
  15458. (defun org-recenter-calendar (date)
  15459. "If the calendar is visible, recenter it to DATE."
  15460. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15461. (when cwin
  15462. (let ((calendar-move-hook nil))
  15463. (with-selected-window cwin
  15464. (calendar-goto-date (if (listp date) date
  15465. (calendar-gregorian-from-absolute date))))))))
  15466. (defun org-goto-calendar (&optional arg)
  15467. "Go to the Emacs calendar at the current date.
  15468. If there is a time stamp in the current line, go to that date.
  15469. A prefix ARG can be used to force the current date."
  15470. (interactive "P")
  15471. (let ((tsr org-ts-regexp) diff
  15472. (calendar-move-hook nil)
  15473. (calendar-view-holidays-initially-flag nil)
  15474. (calendar-view-diary-initially-flag nil))
  15475. (if (or (org-at-timestamp-p)
  15476. (save-excursion
  15477. (beginning-of-line 1)
  15478. (looking-at (concat ".*" tsr))))
  15479. (let ((d1 (time-to-days (current-time)))
  15480. (d2 (time-to-days
  15481. (org-time-string-to-time (match-string 1)))))
  15482. (setq diff (- d2 d1))))
  15483. (calendar)
  15484. (calendar-goto-today)
  15485. (if (and diff (not arg)) (calendar-forward-day diff))))
  15486. (defun org-get-date-from-calendar ()
  15487. "Return a list (month day year) of date at point in calendar."
  15488. (with-current-buffer "*Calendar*"
  15489. (save-match-data
  15490. (calendar-cursor-to-date))))
  15491. (defun org-date-from-calendar ()
  15492. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15493. If there is already a time stamp at the cursor position, update it."
  15494. (interactive)
  15495. (if (org-at-timestamp-p t)
  15496. (org-timestamp-change 0 'calendar)
  15497. (let ((cal-date (org-get-date-from-calendar)))
  15498. (org-insert-time-stamp
  15499. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15500. (defcustom org-effort-durations
  15501. `(("h" . 60)
  15502. ("d" . ,(* 60 8))
  15503. ("w" . ,(* 60 8 5))
  15504. ("m" . ,(* 60 8 5 4))
  15505. ("y" . ,(* 60 8 5 40)))
  15506. "Conversion factor to minutes for an effort modifier.
  15507. Each entry has the form (MODIFIER . MINUTES).
  15508. In an effort string, a number followed by MODIFIER is multiplied
  15509. by the specified number of MINUTES to obtain an effort in
  15510. minutes.
  15511. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15512. effort string \"2hours\" is equivalent to 120 minutes."
  15513. :group 'org-agenda
  15514. :version "24.1"
  15515. :type '(alist :key-type (string :tag "Modifier")
  15516. :value-type (number :tag "Minutes")))
  15517. (defun org-minutes-to-clocksum-string (m)
  15518. "Format number of minutes as a clocksum string.
  15519. The format is determined by `org-time-clocksum-format',
  15520. `org-time-clocksum-use-fractional' and
  15521. `org-time-clocksum-fractional-format' and
  15522. `org-time-clocksum-use-effort-durations'."
  15523. (let ((clocksum "") h d w mo y fmt n)
  15524. (setq h (if org-time-clocksum-use-effort-durations
  15525. (cdr (assoc "h" org-effort-durations)) 60)
  15526. d (if org-time-clocksum-use-effort-durations
  15527. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15528. w (if org-time-clocksum-use-effort-durations
  15529. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15530. mo (if org-time-clocksum-use-effort-durations
  15531. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15532. y (if org-time-clocksum-use-effort-durations
  15533. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15534. ;; fractional format
  15535. (if org-time-clocksum-use-fractional
  15536. (cond
  15537. ;; single format string
  15538. ((stringp org-time-clocksum-fractional-format)
  15539. (format org-time-clocksum-fractional-format (/ m (float h))))
  15540. ;; choice of fractional formats for different time units
  15541. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15542. (> (/ (truncate m) (* y d h)) 0))
  15543. (format fmt (/ m (* y d (float h)))))
  15544. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15545. (> (/ (truncate m) (* mo d h)) 0))
  15546. (format fmt (/ m (* mo d (float h)))))
  15547. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15548. (> (/ (truncate m) (* w d h)) 0))
  15549. (format fmt (/ m (* w d (float h)))))
  15550. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15551. (> (/ (truncate m) (* d h)) 0))
  15552. (format fmt (/ m (* d (float h)))))
  15553. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15554. (> (/ (truncate m) h) 0))
  15555. (format fmt (/ m (float h))))
  15556. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15557. (format fmt m))
  15558. ;; fall back to smallest time unit with a format
  15559. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15560. (format fmt (/ m (float h))))
  15561. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15562. (format fmt (/ m (* d (float h)))))
  15563. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15564. (format fmt (/ m (* w d (float h)))))
  15565. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15566. (format fmt (/ m (* mo d (float h)))))
  15567. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15568. (format fmt (/ m (* y d (float h))))))
  15569. ;; standard (non-fractional) format, with single format string
  15570. (if (stringp org-time-clocksum-format)
  15571. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15572. ;; separate formats components
  15573. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15574. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15575. (plist-get org-time-clocksum-format :require-years))
  15576. (setq clocksum (concat clocksum (format fmt n))
  15577. m (- m (* n y d h))))
  15578. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15579. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15580. (plist-get org-time-clocksum-format :require-months))
  15581. (setq clocksum (concat clocksum (format fmt n))
  15582. m (- m (* n mo d h))))
  15583. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15584. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15585. (plist-get org-time-clocksum-format :require-weeks))
  15586. (setq clocksum (concat clocksum (format fmt n))
  15587. m (- m (* n w d h))))
  15588. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15589. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15590. (plist-get org-time-clocksum-format :require-days))
  15591. (setq clocksum (concat clocksum (format fmt n))
  15592. m (- m (* n d h))))
  15593. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15594. (or (> (setq n (/ (truncate m) h)) 0)
  15595. (plist-get org-time-clocksum-format :require-hours))
  15596. (setq clocksum (concat clocksum (format fmt n))
  15597. m (- m (* n h))))
  15598. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15599. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15600. (setq clocksum (concat clocksum (format fmt m))))
  15601. ;; return formatted time duration
  15602. clocksum))))
  15603. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15604. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15605. "Org mode version 8.0")
  15606. (defun org-hours-to-clocksum-string (n)
  15607. (org-minutes-to-clocksum-string (* n 60)))
  15608. (defun org-hh:mm-string-to-minutes (s)
  15609. "Convert a string H:MM to a number of minutes.
  15610. If the string is just a number, interpret it as minutes.
  15611. In fact, the first hh:mm or number in the string will be taken,
  15612. there can be extra stuff in the string.
  15613. If no number is found, the return value is 0."
  15614. (cond
  15615. ((integerp s) s)
  15616. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15617. (+ (* (string-to-number (match-string 1 s)) 60)
  15618. (string-to-number (match-string 2 s))))
  15619. ((string-match "\\([0-9]+\\)" s)
  15620. (string-to-number (match-string 1 s)))
  15621. (t 0)))
  15622. (defcustom org-image-actual-width t
  15623. "Should we use the actual width of images when inlining them?
  15624. When set to `t', always use the image width.
  15625. When set to a number, use imagemagick (when available) to set
  15626. the image's width to this value.
  15627. When set to a number in a list, try to get the width from the
  15628. #+ATTR.* keyword if it matches a width specification like
  15629. width=\"[0-9]+\", and fall back on that number if none is found.
  15630. When set to nil, try to get the width from an #+ATTR.* keyword
  15631. and fall back on the original width if none is found.
  15632. This requires Emacs >= 24.1, build with imagemagick support."
  15633. :group 'org-appearance
  15634. :version "24.3"
  15635. :type '(choice
  15636. (const :tag "Use the image width" t)
  15637. (integer :tag "Use a number of pixels")
  15638. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15639. (const :tag "Use #+ATTR* or don't resize" nil)))
  15640. (defcustom org-agenda-inhibit-startup-visibility-cycling t
  15641. "Turn off visibility cycling when preparing agenda buffers.
  15642. When preparing agenda buffers, Org visits agenda files. When
  15643. this variable is `t' (the default), the visited buffers for
  15644. agenda files will not honor `org-startup-folded' or any
  15645. #+STARTUP: fold option. Turning this option off may slow down
  15646. the generation of agenda, both because folding takes time and
  15647. because finding entries in folded buffers takes longer than
  15648. finding entries in unfolded ones."
  15649. :type 'boolean
  15650. :version "24.3"
  15651. :group 'org-agenda)
  15652. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15653. "Convert a duration string S to minutes.
  15654. A bare number is interpreted as minutes, modifiers can be set by
  15655. customizing `org-effort-durations' (which see).
  15656. Entries containing a colon are interpreted as H:MM by
  15657. `org-hh:mm-string-to-minutes'."
  15658. (let ((result 0)
  15659. (re (concat "\\([0-9.]+\\) *\\("
  15660. (regexp-opt (mapcar 'car org-effort-durations))
  15661. "\\)")))
  15662. (while (string-match re s)
  15663. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15664. (string-to-number (match-string 1 s))))
  15665. (setq s (replace-match "" nil t s)))
  15666. (setq result (floor result))
  15667. (incf result (org-hh:mm-string-to-minutes s))
  15668. (if output-to-string (number-to-string result) result)))
  15669. ;;;; Files
  15670. (defun org-save-all-org-buffers ()
  15671. "Save all Org-mode buffers without user confirmation."
  15672. (interactive)
  15673. (message "Saving all Org-mode buffers...")
  15674. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15675. (when (featurep 'org-id) (org-id-locations-save))
  15676. (message "Saving all Org-mode buffers... done"))
  15677. (defun org-revert-all-org-buffers ()
  15678. "Revert all Org-mode buffers.
  15679. Prompt for confirmation when there are unsaved changes.
  15680. Be sure you know what you are doing before letting this function
  15681. overwrite your changes.
  15682. This function is useful in a setup where one tracks org files
  15683. with a version control system, to revert on one machine after pulling
  15684. changes from another. I believe the procedure must be like this:
  15685. 1. M-x org-save-all-org-buffers
  15686. 2. Pull changes from the other machine, resolve conflicts
  15687. 3. M-x org-revert-all-org-buffers"
  15688. (interactive)
  15689. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15690. (error "Abort"))
  15691. (save-excursion
  15692. (save-window-excursion
  15693. (mapc
  15694. (lambda (b)
  15695. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15696. (with-current-buffer b buffer-file-name))
  15697. (org-pop-to-buffer-same-window b)
  15698. (revert-buffer t 'no-confirm)))
  15699. (buffer-list))
  15700. (when (and (featurep 'org-id) org-id-track-globally)
  15701. (org-id-locations-load)))))
  15702. ;;;; Agenda files
  15703. ;;;###autoload
  15704. (defun org-switchb (&optional arg)
  15705. "Switch between Org buffers.
  15706. With one prefix argument, restrict available buffers to files.
  15707. With two prefix arguments, restrict available buffers to agenda files.
  15708. Defaults to `iswitchb' for buffer name completion.
  15709. Set `org-completion-use-ido' to make it use ido instead."
  15710. (interactive "P")
  15711. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15712. ((equal arg '(16)) (org-buffer-list 'agenda))
  15713. (t (org-buffer-list))))
  15714. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15715. (org-completion-use-ido org-completion-use-ido))
  15716. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15717. (setq org-completion-use-iswitchb t))
  15718. (org-pop-to-buffer-same-window
  15719. (org-icompleting-read "Org buffer: "
  15720. (mapcar 'list (mapcar 'buffer-name blist))
  15721. nil t))))
  15722. ;;; Define some older names previously used for this functionality
  15723. ;;;###autoload
  15724. (defalias 'org-ido-switchb 'org-switchb)
  15725. ;;;###autoload
  15726. (defalias 'org-iswitchb 'org-switchb)
  15727. (defun org-buffer-list (&optional predicate exclude-tmp)
  15728. "Return a list of Org buffers.
  15729. PREDICATE can be `export', `files' or `agenda'.
  15730. export restrict the list to Export buffers.
  15731. files restrict the list to buffers visiting Org files.
  15732. agenda restrict the list to buffers visiting agenda files.
  15733. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15734. (let* ((bfn nil)
  15735. (agenda-files (and (eq predicate 'agenda)
  15736. (mapcar 'file-truename (org-agenda-files t))))
  15737. (filter
  15738. (cond
  15739. ((eq predicate 'files)
  15740. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15741. ((eq predicate 'export)
  15742. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15743. ((eq predicate 'agenda)
  15744. (lambda (b)
  15745. (with-current-buffer b
  15746. (and (derived-mode-p 'org-mode)
  15747. (setq bfn (buffer-file-name b))
  15748. (member (file-truename bfn) agenda-files)))))
  15749. (t (lambda (b) (with-current-buffer b
  15750. (or (derived-mode-p 'org-mode)
  15751. (string-match "\*Org .*Export"
  15752. (buffer-name b)))))))))
  15753. (delq nil
  15754. (mapcar
  15755. (lambda(b)
  15756. (if (and (funcall filter b)
  15757. (or (not exclude-tmp)
  15758. (not (string-match "tmp" (buffer-name b)))))
  15759. b
  15760. nil))
  15761. (buffer-list)))))
  15762. (defun org-agenda-files (&optional unrestricted archives)
  15763. "Get the list of agenda files.
  15764. Optional UNRESTRICTED means return the full list even if a restriction
  15765. is currently in place.
  15766. When ARCHIVES is t, include all archive files that are really being
  15767. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15768. `org-agenda-archives-mode' is t."
  15769. (let ((files
  15770. (cond
  15771. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15772. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15773. ((listp org-agenda-files) org-agenda-files)
  15774. (t (error "Invalid value of `org-agenda-files'")))))
  15775. (setq files (apply 'append
  15776. (mapcar (lambda (f)
  15777. (if (file-directory-p f)
  15778. (directory-files
  15779. f t org-agenda-file-regexp)
  15780. (list f)))
  15781. files)))
  15782. (when org-agenda-skip-unavailable-files
  15783. (setq files (delq nil
  15784. (mapcar (function
  15785. (lambda (file)
  15786. (and (file-readable-p file) file)))
  15787. files))))
  15788. (when (or (eq archives t)
  15789. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15790. (setq files (org-add-archive-files files)))
  15791. files))
  15792. (defun org-agenda-file-p (&optional file)
  15793. "Return non-nil, if FILE is an agenda file.
  15794. If FILE is omitted, use the file associated with the current
  15795. buffer."
  15796. (member (or file (buffer-file-name))
  15797. (org-agenda-files t)))
  15798. (defun org-edit-agenda-file-list ()
  15799. "Edit the list of agenda files.
  15800. Depending on setup, this either uses customize to edit the variable
  15801. `org-agenda-files', or it visits the file that is holding the list. In the
  15802. latter case, the buffer is set up in a way that saving it automatically kills
  15803. the buffer and restores the previous window configuration."
  15804. (interactive)
  15805. (if (stringp org-agenda-files)
  15806. (let ((cw (current-window-configuration)))
  15807. (find-file org-agenda-files)
  15808. (org-set-local 'org-window-configuration cw)
  15809. (org-add-hook 'after-save-hook
  15810. (lambda ()
  15811. (set-window-configuration
  15812. (prog1 org-window-configuration
  15813. (kill-buffer (current-buffer))))
  15814. (org-install-agenda-files-menu)
  15815. (message "New agenda file list installed"))
  15816. nil 'local)
  15817. (message "%s" (substitute-command-keys
  15818. "Edit list and finish with \\[save-buffer]")))
  15819. (customize-variable 'org-agenda-files)))
  15820. (defun org-store-new-agenda-file-list (list)
  15821. "Set new value for the agenda file list and save it correctly."
  15822. (if (stringp org-agenda-files)
  15823. (let ((fe (org-read-agenda-file-list t)) b u)
  15824. (while (setq b (find-buffer-visiting org-agenda-files))
  15825. (kill-buffer b))
  15826. (with-temp-file org-agenda-files
  15827. (insert
  15828. (mapconcat
  15829. (lambda (f) ;; Keep un-expanded entries.
  15830. (if (setq u (assoc f fe))
  15831. (cdr u)
  15832. f))
  15833. list "\n")
  15834. "\n")))
  15835. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15836. (org-insert-mode-line-in-empty-file nil))
  15837. (setq org-agenda-files list)
  15838. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15839. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15840. "Read the list of agenda files from a file.
  15841. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15842. filenames, used by `org-store-new-agenda-file-list' to write back
  15843. un-expanded file names."
  15844. (when (file-directory-p org-agenda-files)
  15845. (error "`org-agenda-files' cannot be a single directory"))
  15846. (when (stringp org-agenda-files)
  15847. (with-temp-buffer
  15848. (insert-file-contents org-agenda-files)
  15849. (mapcar
  15850. (lambda (f)
  15851. (let ((e (expand-file-name (substitute-in-file-name f)
  15852. org-directory)))
  15853. (if pair-with-expansion
  15854. (cons e f)
  15855. e)))
  15856. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15857. ;;;###autoload
  15858. (defun org-cycle-agenda-files ()
  15859. "Cycle through the files in `org-agenda-files'.
  15860. If the current buffer visits an agenda file, find the next one in the list.
  15861. If the current buffer does not, find the first agenda file."
  15862. (interactive)
  15863. (let* ((fs (org-agenda-files t))
  15864. (files (append fs (list (car fs))))
  15865. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15866. file)
  15867. (unless files (error "No agenda files"))
  15868. (catch 'exit
  15869. (while (setq file (pop files))
  15870. (if (equal (file-truename file) tcf)
  15871. (when (car files)
  15872. (find-file (car files))
  15873. (throw 'exit t))))
  15874. (find-file (car fs)))
  15875. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15876. (defun org-agenda-file-to-front (&optional to-end)
  15877. "Move/add the current file to the top of the agenda file list.
  15878. If the file is not present in the list, it is added to the front. If it is
  15879. present, it is moved there. With optional argument TO-END, add/move to the
  15880. end of the list."
  15881. (interactive "P")
  15882. (let ((org-agenda-skip-unavailable-files nil)
  15883. (file-alist (mapcar (lambda (x)
  15884. (cons (file-truename x) x))
  15885. (org-agenda-files t)))
  15886. (ctf (file-truename
  15887. (or buffer-file-name
  15888. (error "Please save the current buffer to a file"))))
  15889. x had)
  15890. (setq x (assoc ctf file-alist) had x)
  15891. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15892. (if to-end
  15893. (setq file-alist (append (delq x file-alist) (list x)))
  15894. (setq file-alist (cons x (delq x file-alist))))
  15895. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15896. (org-install-agenda-files-menu)
  15897. (message "File %s to %s of agenda file list"
  15898. (if had "moved" "added") (if to-end "end" "front"))))
  15899. (defun org-remove-file (&optional file)
  15900. "Remove current file from the list of files in variable `org-agenda-files'.
  15901. These are the files which are being checked for agenda entries.
  15902. Optional argument FILE means use this file instead of the current."
  15903. (interactive)
  15904. (let* ((org-agenda-skip-unavailable-files nil)
  15905. (file (or file buffer-file-name
  15906. (error "Current buffer does not visit a file")))
  15907. (true-file (file-truename file))
  15908. (afile (abbreviate-file-name file))
  15909. (files (delq nil (mapcar
  15910. (lambda (x)
  15911. (if (equal true-file
  15912. (file-truename x))
  15913. nil x))
  15914. (org-agenda-files t)))))
  15915. (if (not (= (length files) (length (org-agenda-files t))))
  15916. (progn
  15917. (org-store-new-agenda-file-list files)
  15918. (org-install-agenda-files-menu)
  15919. (message "Removed file: %s" afile))
  15920. (message "File was not in list: %s (not removed)" afile))))
  15921. (defun org-file-menu-entry (file)
  15922. (vector file (list 'find-file file) t))
  15923. (defun org-check-agenda-file (file)
  15924. "Make sure FILE exists. If not, ask user what to do."
  15925. (when (not (file-exists-p file))
  15926. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15927. (abbreviate-file-name file))
  15928. (let ((r (downcase (read-char-exclusive))))
  15929. (cond
  15930. ((equal r ?r)
  15931. (org-remove-file file)
  15932. (throw 'nextfile t))
  15933. (t (error "Abort"))))))
  15934. (defun org-get-agenda-file-buffer (file)
  15935. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15936. it to the list of buffers which might be released later."
  15937. (let ((buf (org-find-base-buffer-visiting file)))
  15938. (if buf
  15939. buf ; just return it
  15940. ;; Make a new buffer and remember it
  15941. (setq buf (find-file-noselect file))
  15942. (if buf (push buf org-agenda-new-buffers))
  15943. buf)))
  15944. (defun org-release-buffers (blist)
  15945. "Release all buffers in list, asking the user for confirmation when needed.
  15946. When a buffer is unmodified, it is just killed. When modified, it is saved
  15947. \(if the user agrees) and then killed."
  15948. (let (buf file)
  15949. (while (setq buf (pop blist))
  15950. (setq file (buffer-file-name buf))
  15951. (when (and (buffer-modified-p buf)
  15952. file
  15953. (y-or-n-p (format "Save file %s? " file)))
  15954. (with-current-buffer buf (save-buffer)))
  15955. (kill-buffer buf))))
  15956. (defun org-agenda-prepare-buffers (files)
  15957. "Create buffers for all agenda files, protect archived trees and comments."
  15958. (interactive)
  15959. (let ((pa '(:org-archived t))
  15960. (pc '(:org-comment t))
  15961. (pall '(:org-archived t :org-comment t))
  15962. (inhibit-read-only t)
  15963. (org-inhibit-startup-visibility-stuff
  15964. org-agenda-inhibit-startup-visibility-cycling)
  15965. (rea (concat ":" org-archive-tag ":"))
  15966. bmp file re)
  15967. (save-excursion
  15968. (save-restriction
  15969. (while (setq file (pop files))
  15970. (catch 'nextfile
  15971. (if (bufferp file)
  15972. (set-buffer file)
  15973. (org-check-agenda-file file)
  15974. (set-buffer (org-get-agenda-file-buffer file)))
  15975. (widen)
  15976. (setq bmp (buffer-modified-p))
  15977. (org-refresh-category-properties)
  15978. (org-refresh-properties org-effort-property 'org-effort)
  15979. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15980. (setq org-todo-keywords-for-agenda
  15981. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15982. (setq org-done-keywords-for-agenda
  15983. (append org-done-keywords-for-agenda org-done-keywords))
  15984. (setq org-todo-keyword-alist-for-agenda
  15985. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15986. (setq org-drawers-for-agenda
  15987. (append org-drawers-for-agenda org-drawers))
  15988. (setq org-tag-alist-for-agenda
  15989. (append org-tag-alist-for-agenda org-tag-alist))
  15990. (save-excursion
  15991. (remove-text-properties (point-min) (point-max) pall)
  15992. (when org-agenda-skip-archived-trees
  15993. (goto-char (point-min))
  15994. (while (re-search-forward rea nil t)
  15995. (if (org-at-heading-p t)
  15996. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15997. (goto-char (point-min))
  15998. (setq re (format org-heading-keyword-regexp-format
  15999. org-comment-string))
  16000. (while (re-search-forward re nil t)
  16001. (add-text-properties
  16002. (match-beginning 0) (org-end-of-subtree t) pc)))
  16003. (set-buffer-modified-p bmp)))))
  16004. (setq org-todo-keywords-for-agenda
  16005. (org-uniquify org-todo-keywords-for-agenda))
  16006. (setq org-todo-keyword-alist-for-agenda
  16007. (org-uniquify org-todo-keyword-alist-for-agenda)
  16008. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16009. ;;;; Embedded LaTeX
  16010. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16011. "Keymap for the minor `org-cdlatex-mode'.")
  16012. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16013. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16014. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16015. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16016. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16017. (defvar org-cdlatex-texmathp-advice-is-done nil
  16018. "Flag remembering if we have applied the advice to texmathp already.")
  16019. (define-minor-mode org-cdlatex-mode
  16020. "Toggle the minor `org-cdlatex-mode'.
  16021. This mode supports entering LaTeX environment and math in LaTeX fragments
  16022. in Org-mode.
  16023. \\{org-cdlatex-mode-map}"
  16024. nil " OCDL" nil
  16025. (when org-cdlatex-mode
  16026. (require 'cdlatex)
  16027. (run-hooks 'cdlatex-mode-hook)
  16028. (cdlatex-compute-tables))
  16029. (unless org-cdlatex-texmathp-advice-is-done
  16030. (setq org-cdlatex-texmathp-advice-is-done t)
  16031. (defadvice texmathp (around org-math-always-on activate)
  16032. "Always return t in org-mode buffers.
  16033. This is because we want to insert math symbols without dollars even outside
  16034. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16035. an embedded LaTeX fragment, let texmathp do its job.
  16036. \\[org-cdlatex-mode-map]"
  16037. (interactive)
  16038. (let (p)
  16039. (cond
  16040. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16041. ((eq this-command 'cdlatex-math-symbol)
  16042. (setq ad-return-value t
  16043. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16044. (t
  16045. (let ((p (org-inside-LaTeX-fragment-p)))
  16046. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16047. (setq ad-return-value t
  16048. texmathp-why '("Org-mode embedded math" . 0))
  16049. (if p ad-do-it)))))))))
  16050. (defun turn-on-org-cdlatex ()
  16051. "Unconditionally turn on `org-cdlatex-mode'."
  16052. (org-cdlatex-mode 1))
  16053. (defun org-inside-LaTeX-fragment-p ()
  16054. "Test if point is inside a LaTeX fragment.
  16055. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16056. sequence appearing also before point.
  16057. Even though the matchers for math are configurable, this function assumes
  16058. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16059. delimiters are skipped when they have been removed by customization.
  16060. The return value is nil, or a cons cell with the delimiter and the
  16061. position of this delimiter.
  16062. This function does a reasonably good job, but can locally be fooled by
  16063. for example currency specifications. For example it will assume being in
  16064. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16065. fragments that are properly closed, but during editing, we have to live
  16066. with the uncertainty caused by missing closing delimiters. This function
  16067. looks only before point, not after."
  16068. (catch 'exit
  16069. (let ((pos (point))
  16070. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16071. (lim (progn
  16072. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16073. (point)))
  16074. dd-on str (start 0) m re)
  16075. (goto-char pos)
  16076. (when dodollar
  16077. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16078. re (nth 1 (assoc "$" org-latex-regexps)))
  16079. (while (string-match re str start)
  16080. (cond
  16081. ((= (match-end 0) (length str))
  16082. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16083. ((= (match-end 0) (- (length str) 5))
  16084. (throw 'exit nil))
  16085. (t (setq start (match-end 0))))))
  16086. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16087. (goto-char pos)
  16088. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16089. (and (match-beginning 2) (throw 'exit nil))
  16090. ;; count $$
  16091. (while (re-search-backward "\\$\\$" lim t)
  16092. (setq dd-on (not dd-on)))
  16093. (goto-char pos)
  16094. (if dd-on (cons "$$" m))))))
  16095. (defun org-inside-latex-macro-p ()
  16096. "Is point inside a LaTeX macro or its arguments?"
  16097. (save-match-data
  16098. (org-in-regexp
  16099. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16100. (defun org-try-cdlatex-tab ()
  16101. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16102. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16103. - inside a LaTeX fragment, or
  16104. - after the first word in a line, where an abbreviation expansion could
  16105. insert a LaTeX environment."
  16106. (when org-cdlatex-mode
  16107. (cond
  16108. ;; Before any word on the line: No expansion possible.
  16109. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16110. ;; Just after first word on the line: Expand it. Make sure it
  16111. ;; cannot happen on headlines, though.
  16112. ((save-excursion
  16113. (skip-chars-backward "a-zA-Z0-9*")
  16114. (skip-chars-backward " \t")
  16115. (and (bolp) (not (org-at-heading-p))))
  16116. (cdlatex-tab) t)
  16117. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16118. (defun org-cdlatex-underscore-caret (&optional arg)
  16119. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16120. Revert to the normal definition outside of these fragments."
  16121. (interactive "P")
  16122. (if (org-inside-LaTeX-fragment-p)
  16123. (call-interactively 'cdlatex-sub-superscript)
  16124. (let (org-cdlatex-mode)
  16125. (call-interactively (key-binding (vector last-input-event))))))
  16126. (defun org-cdlatex-math-modify (&optional arg)
  16127. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16128. Revert to the normal definition outside of these fragments."
  16129. (interactive "P")
  16130. (if (org-inside-LaTeX-fragment-p)
  16131. (call-interactively 'cdlatex-math-modify)
  16132. (let (org-cdlatex-mode)
  16133. (call-interactively (key-binding (vector last-input-event))))))
  16134. (defvar org-latex-fragment-image-overlays nil
  16135. "List of overlays carrying the images of latex fragments.")
  16136. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16137. (defun org-remove-latex-fragment-image-overlays ()
  16138. "Remove all overlays with LaTeX fragment images in current buffer."
  16139. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16140. (setq org-latex-fragment-image-overlays nil))
  16141. (defun org-preview-latex-fragment (&optional subtree)
  16142. "Preview the LaTeX fragment at point, or all locally or globally.
  16143. If the cursor is in a LaTeX fragment, create the image and overlay
  16144. it over the source code. If there is no fragment at point, display
  16145. all fragments in the current text, from one headline to the next. With
  16146. prefix SUBTREE, display all fragments in the current subtree. With a
  16147. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16148. the cursor is before the first headline,
  16149. display all fragments in the buffer.
  16150. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16151. (interactive "P")
  16152. (unless buffer-file-name
  16153. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16154. (org-remove-latex-fragment-image-overlays)
  16155. (save-excursion
  16156. (save-restriction
  16157. (let (beg end at msg)
  16158. (cond
  16159. ((or (equal subtree '(16))
  16160. (not (save-excursion
  16161. (re-search-backward org-outline-regexp-bol nil t))))
  16162. (setq beg (point-min) end (point-max)
  16163. msg "Creating images for buffer...%s"))
  16164. ((equal subtree '(4))
  16165. (org-back-to-heading)
  16166. (setq beg (point) end (org-end-of-subtree t)
  16167. msg "Creating images for subtree...%s"))
  16168. (t
  16169. (if (setq at (org-inside-LaTeX-fragment-p))
  16170. (goto-char (max (point-min) (- (cdr at) 2)))
  16171. (org-back-to-heading))
  16172. (setq beg (point) end (progn (outline-next-heading) (point))
  16173. msg (if at "Creating image...%s"
  16174. "Creating images for entry...%s"))))
  16175. (message msg "")
  16176. (narrow-to-region beg end)
  16177. (goto-char beg)
  16178. (org-format-latex
  16179. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16180. (file-name-nondirectory
  16181. buffer-file-name)))
  16182. default-directory 'overlays msg at 'forbuffer
  16183. org-latex-create-formula-image-program)
  16184. (message msg "done. Use `C-c C-c' to remove images.")))))
  16185. (defvar org-latex-regexps
  16186. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16187. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16188. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16189. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16190. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16191. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16192. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16193. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16194. "Regular expressions for matching embedded LaTeX.")
  16195. (defvar org-export-have-math nil) ;; dynamic scoping
  16196. (defun org-format-latex (prefix &optional dir overlays msg at
  16197. forbuffer processing-type)
  16198. "Replace LaTeX fragments with links to an image, and produce images.
  16199. Some of the options can be changed using the variable
  16200. `org-format-latex-options'."
  16201. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16202. (let* ((prefixnodir (file-name-nondirectory prefix))
  16203. (absprefix (expand-file-name prefix dir))
  16204. (todir (file-name-directory absprefix))
  16205. (opt org-format-latex-options)
  16206. (optnew org-format-latex-options)
  16207. (matchers (plist-get opt :matchers))
  16208. (re-list org-latex-regexps)
  16209. (org-format-latex-header-extra
  16210. (plist-get (org-infile-export-plist) :latex-header-extra))
  16211. (cnt 0) txt hash link beg end re e checkdir
  16212. string
  16213. m n block-type block linkfile movefile ov)
  16214. ;; Check the different regular expressions
  16215. (while (setq e (pop re-list))
  16216. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16217. block (if block-type "\n\n" ""))
  16218. (when (member m matchers)
  16219. (goto-char (point-min))
  16220. (while (re-search-forward re nil t)
  16221. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16222. (not (get-text-property (match-beginning n)
  16223. 'org-protected))
  16224. (or (not overlays)
  16225. (not (eq (get-char-property (match-beginning n)
  16226. 'org-overlay-type)
  16227. 'org-latex-overlay))))
  16228. (setq org-export-have-math t)
  16229. (cond
  16230. ((eq processing-type 'verbatim)
  16231. ;; Leave the text verbatim, just protect it
  16232. (add-text-properties (match-beginning n) (match-end n)
  16233. '(org-protected t)))
  16234. ((eq processing-type 'mathjax)
  16235. ;; Prepare for MathJax processing
  16236. (setq string (match-string n))
  16237. (if (member m '("$" "$1"))
  16238. (save-excursion
  16239. (delete-region (match-beginning n) (match-end n))
  16240. (goto-char (match-beginning n))
  16241. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  16242. "\\)")
  16243. '(org-protected t))))
  16244. (add-text-properties (match-beginning n) (match-end n)
  16245. '(org-protected t))))
  16246. ((or (eq processing-type 'dvipng)
  16247. (eq processing-type 'imagemagick))
  16248. ;; Process to an image
  16249. (setq txt (match-string n)
  16250. beg (match-beginning n) end (match-end n)
  16251. cnt (1+ cnt))
  16252. (let ((face (face-at-point))
  16253. (fg (plist-get opt :foreground))
  16254. (bg (plist-get opt :background))
  16255. print-length print-level) ; make sure full list is printed
  16256. (when forbuffer
  16257. ; Get the colors from the face at point
  16258. (goto-char beg)
  16259. (when (eq fg 'auto)
  16260. (setq fg (face-attribute face :foreground nil 'default)))
  16261. (when (eq bg 'auto)
  16262. (setq bg (face-attribute face :background nil 'default)))
  16263. (setq optnew (copy-sequence opt))
  16264. (plist-put optnew :foreground fg)
  16265. (plist-put optnew :background bg))
  16266. (setq hash (sha1 (prin1-to-string
  16267. (list org-format-latex-header
  16268. org-format-latex-header-extra
  16269. org-export-latex-default-packages-alist
  16270. org-export-latex-packages-alist
  16271. org-format-latex-options
  16272. forbuffer txt fg bg)))
  16273. linkfile (format "%s_%s.png" prefix hash)
  16274. movefile (format "%s_%s.png" absprefix hash)))
  16275. (setq link (concat block "[[file:" linkfile "]]" block))
  16276. (if msg (message msg cnt))
  16277. (goto-char beg)
  16278. (unless checkdir ; make sure the directory exists
  16279. (setq checkdir t)
  16280. (or (file-directory-p todir) (make-directory todir t)))
  16281. (unless (file-exists-p movefile)
  16282. (org-create-formula-image
  16283. txt movefile optnew forbuffer processing-type))
  16284. (if overlays
  16285. (progn
  16286. (mapc (lambda (o)
  16287. (if (eq (overlay-get o 'org-overlay-type)
  16288. 'org-latex-overlay)
  16289. (delete-overlay o)))
  16290. (overlays-in beg end))
  16291. (setq ov (make-overlay beg end))
  16292. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16293. (if (featurep 'xemacs)
  16294. (progn
  16295. (overlay-put ov 'invisible t)
  16296. (overlay-put
  16297. ov 'end-glyph
  16298. (make-glyph (vector 'png :file movefile))))
  16299. (overlay-put
  16300. ov 'display
  16301. (list 'image :type 'png :file movefile :ascent 'center)))
  16302. (push ov org-latex-fragment-image-overlays)
  16303. (goto-char end))
  16304. (delete-region beg end)
  16305. (insert (org-add-props link
  16306. (list 'org-latex-src
  16307. (replace-regexp-in-string
  16308. "\"" "" txt)
  16309. 'org-latex-src-embed-type
  16310. (if block-type 'paragraph 'character))))))
  16311. ((eq processing-type 'mathml)
  16312. ;; Process to MathML
  16313. (unless (save-match-data (org-format-latex-mathml-available-p))
  16314. (error "LaTeX to MathML converter not configured"))
  16315. (setq txt (match-string n)
  16316. beg (match-beginning n) end (match-end n)
  16317. cnt (1+ cnt))
  16318. (if msg (message msg cnt))
  16319. (goto-char beg)
  16320. (delete-region beg end)
  16321. (insert (org-format-latex-as-mathml
  16322. txt block-type prefix dir)))
  16323. (t
  16324. (error "Unknown conversion type %s for latex fragments"
  16325. processing-type)))))))))
  16326. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16327. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16328. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16329. sucessful, return the portion between \"<math...> </math>\"
  16330. elements otherwise return nil. When MATHML-FILE is specified,
  16331. write the results in to that file. When invoked as an
  16332. interactive command, prompt for LATEX-FRAG, with initial value
  16333. set to the current active region and echo the results for user
  16334. inspection."
  16335. (interactive (list (let ((frag (when (org-region-active-p)
  16336. (buffer-substring-no-properties
  16337. (region-beginning) (region-end)))))
  16338. (read-string "LaTeX Fragment: " frag nil frag))))
  16339. (unless latex-frag (error "Invalid latex-frag"))
  16340. (let* ((tmp-in-file (file-relative-name
  16341. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16342. (ignore (write-region latex-frag nil tmp-in-file))
  16343. (tmp-out-file (file-relative-name
  16344. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16345. (cmd (format-spec
  16346. org-latex-to-mathml-convert-command
  16347. `((?j . ,(shell-quote-argument
  16348. (expand-file-name org-latex-to-mathml-jar-file)))
  16349. (?I . ,(shell-quote-argument tmp-in-file))
  16350. (?o . ,(shell-quote-argument tmp-out-file)))))
  16351. mathml shell-command-output)
  16352. (when (org-called-interactively-p 'any)
  16353. (unless (org-format-latex-mathml-available-p)
  16354. (error "LaTeX to MathML converter not configured")))
  16355. (message "Running %s" cmd)
  16356. (setq shell-command-output (shell-command-to-string cmd))
  16357. (setq mathml
  16358. (when (file-readable-p tmp-out-file)
  16359. (with-current-buffer (find-file-noselect tmp-out-file t)
  16360. (goto-char (point-min))
  16361. (when (re-search-forward
  16362. (concat
  16363. (regexp-quote
  16364. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16365. "\\(.\\|\n\\)*"
  16366. (regexp-quote "</math>")) nil t)
  16367. (prog1 (match-string 0) (kill-buffer))))))
  16368. (cond
  16369. (mathml
  16370. (setq mathml
  16371. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16372. (when mathml-file
  16373. (write-region mathml nil mathml-file))
  16374. (when (org-called-interactively-p 'any)
  16375. (message mathml)))
  16376. ((message "LaTeX to MathML conversion failed")
  16377. (message shell-command-output)))
  16378. (delete-file tmp-in-file)
  16379. (when (file-exists-p tmp-out-file)
  16380. (delete-file tmp-out-file))
  16381. mathml))
  16382. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16383. prefix &optional dir)
  16384. "Use `org-create-math-formula' but check local cache first."
  16385. (let* ((absprefix (expand-file-name prefix dir))
  16386. (print-length nil) (print-level nil)
  16387. (formula-id (concat
  16388. "formula-"
  16389. (sha1
  16390. (prin1-to-string
  16391. (list latex-frag
  16392. org-latex-to-mathml-convert-command)))))
  16393. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16394. (formula-cache-dir (file-name-directory formula-cache)))
  16395. (unless (file-directory-p formula-cache-dir)
  16396. (make-directory formula-cache-dir t))
  16397. (unless (file-exists-p formula-cache)
  16398. (org-create-math-formula latex-frag formula-cache))
  16399. (if (file-exists-p formula-cache)
  16400. ;; Successful conversion. Return the link to MathML file.
  16401. (org-add-props
  16402. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16403. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16404. 'org-latex-src-embed-type (if latex-frag-type
  16405. 'paragraph 'character)))
  16406. ;; Failed conversion. Return the LaTeX fragment verbatim
  16407. (add-text-properties
  16408. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16409. latex-frag)))
  16410. (defun org-create-formula-image (string tofile options buffer &optional type)
  16411. "Create an image from LaTeX source using dvipng or convert.
  16412. This function calls either `org-create-formula-image-with-dvipng'
  16413. or `org-create-formula-image-with-imagemagick' depending on the
  16414. value of `org-latex-create-formula-image-program' or on the value
  16415. of the optional TYPE variable.
  16416. Note: ultimately these two function should be combined as they
  16417. share a good deal of logic."
  16418. (org-check-external-command
  16419. "latex" "needed to convert LaTeX fragments to images")
  16420. (funcall
  16421. (case (or type org-latex-create-formula-image-program)
  16422. ('dvipng
  16423. (org-check-external-command
  16424. "dvipng" "needed to convert LaTeX fragments to images")
  16425. #'org-create-formula-image-with-dvipng)
  16426. ('imagemagick
  16427. (org-check-external-command
  16428. "convert" "you need to install imagemagick")
  16429. #'org-create-formula-image-with-imagemagick)
  16430. (t (error
  16431. "invalid value of `org-latex-create-formula-image-program'")))
  16432. string tofile options buffer))
  16433. ;; This function borrows from Ganesh Swami's latex2png.el
  16434. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16435. "This calls dvipng."
  16436. (require 'org-latex)
  16437. (let* ((tmpdir (if (featurep 'xemacs)
  16438. (temp-directory)
  16439. temporary-file-directory))
  16440. (texfilebase (make-temp-name
  16441. (expand-file-name "orgtex" tmpdir)))
  16442. (texfile (concat texfilebase ".tex"))
  16443. (dvifile (concat texfilebase ".dvi"))
  16444. (pngfile (concat texfilebase ".png"))
  16445. (fnh (if (featurep 'xemacs)
  16446. (font-height (face-font 'default))
  16447. (face-attribute 'default :height nil)))
  16448. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16449. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16450. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16451. "Black"))
  16452. (bg (or (plist-get options (if buffer :background :html-background))
  16453. "Transparent")))
  16454. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16455. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16456. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16457. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16458. (with-temp-file texfile
  16459. (insert (org-splice-latex-header
  16460. org-format-latex-header
  16461. org-export-latex-default-packages-alist
  16462. org-export-latex-packages-alist t
  16463. org-format-latex-header-extra))
  16464. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16465. (require 'org-latex)
  16466. (org-export-latex-fix-inputenc))
  16467. (let ((dir default-directory))
  16468. (condition-case nil
  16469. (progn
  16470. (cd tmpdir)
  16471. (call-process "latex" nil nil nil texfile))
  16472. (error nil))
  16473. (cd dir))
  16474. (if (not (file-exists-p dvifile))
  16475. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16476. (condition-case nil
  16477. (if (featurep 'xemacs)
  16478. (call-process "dvipng" nil nil nil
  16479. "-fg" fg "-bg" bg
  16480. "-T" "tight"
  16481. "-o" pngfile
  16482. dvifile)
  16483. (call-process "dvipng" nil nil nil
  16484. "-fg" fg "-bg" bg
  16485. "-D" dpi
  16486. ;;"-x" scale "-y" scale
  16487. "-T" "tight"
  16488. "-o" pngfile
  16489. dvifile))
  16490. (error nil))
  16491. (if (not (file-exists-p pngfile))
  16492. (if org-format-latex-signal-error
  16493. (error "Failed to create png file from %s" texfile)
  16494. (message "Failed to create png file from %s" texfile)
  16495. nil)
  16496. ;; Use the requested file name and clean up
  16497. (copy-file pngfile tofile 'replace)
  16498. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16499. (if (file-exists-p (concat texfilebase e))
  16500. (delete-file (concat texfilebase e))))
  16501. pngfile))))
  16502. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16503. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16504. "This calls convert, which is included into imagemagick."
  16505. (require 'org-latex)
  16506. (let* ((tmpdir (if (featurep 'xemacs)
  16507. (temp-directory)
  16508. temporary-file-directory))
  16509. (texfilebase (make-temp-name
  16510. (expand-file-name "orgtex" tmpdir)))
  16511. (texfile (concat texfilebase ".tex"))
  16512. (pdffile (concat texfilebase ".pdf"))
  16513. (pngfile (concat texfilebase ".png"))
  16514. (fnh (if (featurep 'xemacs)
  16515. (font-height (face-font 'default))
  16516. (face-attribute 'default :height nil)))
  16517. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16518. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16519. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16520. "black"))
  16521. (bg (or (plist-get options (if buffer :background :html-background))
  16522. "white")))
  16523. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16524. (setq fg (org-latex-color-format fg)))
  16525. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16526. (setq bg (org-latex-color-format
  16527. (if (string= bg "Transparent") "white" bg))))
  16528. (with-temp-file texfile
  16529. (insert (org-splice-latex-header
  16530. org-format-latex-header
  16531. org-export-latex-default-packages-alist
  16532. org-export-latex-packages-alist t
  16533. org-format-latex-header-extra))
  16534. (insert "\n\\begin{document}\n"
  16535. "\\definecolor{fg}{rgb}{" fg "}\n"
  16536. "\\definecolor{bg}{rgb}{" bg "}\n"
  16537. "\n\\pagecolor{bg}\n"
  16538. "\n{\\color{fg}\n"
  16539. string
  16540. "\n}\n"
  16541. "\n\\end{document}\n" )
  16542. (require 'org-latex)
  16543. (org-export-latex-fix-inputenc))
  16544. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16545. (condition-case nil
  16546. (progn
  16547. (cd tmpdir)
  16548. (setq cmds org-latex-to-pdf-process)
  16549. (while cmds
  16550. (setq latex-frags-cmds (pop cmds))
  16551. (if (listp latex-frags-cmds)
  16552. (setq cmds nil)
  16553. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16554. (while latex-frags-cmds
  16555. (setq cmd (pop latex-frags-cmds))
  16556. (while (string-match "%b" cmd)
  16557. (setq cmd (replace-match
  16558. (save-match-data
  16559. (shell-quote-argument texfile))
  16560. t t cmd)))
  16561. (while (string-match "%f" cmd)
  16562. (setq cmd (replace-match
  16563. (save-match-data
  16564. (shell-quote-argument (file-name-nondirectory texfile)))
  16565. t t cmd)))
  16566. (while (string-match "%o" cmd)
  16567. (setq cmd (replace-match
  16568. (save-match-data
  16569. (shell-quote-argument (file-name-directory texfile)))
  16570. t t cmd)))
  16571. (setq cmd (split-string cmd))
  16572. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16573. (error nil))
  16574. (cd dir))
  16575. (if (not (file-exists-p pdffile))
  16576. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16577. (condition-case nil
  16578. (if (featurep 'xemacs)
  16579. (call-process "convert" nil nil nil
  16580. "-density" "96"
  16581. "-trim"
  16582. "-antialias"
  16583. pdffile
  16584. "-quality" "100"
  16585. ;; "-sharpen" "0x1.0"
  16586. pngfile)
  16587. (call-process "convert" nil nil nil
  16588. "-density" dpi
  16589. "-trim"
  16590. "-antialias"
  16591. pdffile
  16592. "-quality" "100"
  16593. ; "-sharpen" "0x1.0"
  16594. pngfile))
  16595. (error nil))
  16596. (if (not (file-exists-p pngfile))
  16597. (if org-format-latex-signal-error
  16598. (error "Failed to create png file from %s" texfile)
  16599. (message "Failed to create png file from %s" texfile)
  16600. nil)
  16601. ;; Use the requested file name and clean up
  16602. (copy-file pngfile tofile 'replace)
  16603. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16604. (if (file-exists-p (concat texfilebase e))
  16605. (delete-file (concat texfilebase e))))
  16606. pngfile))))
  16607. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16608. "Fill a LaTeX header template TPL.
  16609. In the template, the following place holders will be recognized:
  16610. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16611. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16612. [PACKAGES] \\usepackage statements for PKG
  16613. [NO-PACKAGES] do not include PKG
  16614. [EXTRA] the string EXTRA
  16615. [NO-EXTRA] do not include EXTRA
  16616. For backward compatibility, if both the positive and the negative place
  16617. holder is missing, the positive one (without the \"NO-\") will be
  16618. assumed to be present at the end of the template.
  16619. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16620. EXTRA is a string.
  16621. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16622. (let (rpl (end ""))
  16623. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16624. (setq rpl (if (or (match-end 1) (not def-pkg))
  16625. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16626. tpl (replace-match rpl t t tpl))
  16627. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16628. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16629. (setq rpl (if (or (match-end 1) (not pkg))
  16630. "" (org-latex-packages-to-string pkg snippets-p t))
  16631. tpl (replace-match rpl t t tpl))
  16632. (if pkg (setq end
  16633. (concat end "\n"
  16634. (org-latex-packages-to-string pkg snippets-p)))))
  16635. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16636. (setq rpl (if (or (match-end 1) (not extra))
  16637. "" (concat extra "\n"))
  16638. tpl (replace-match rpl t t tpl))
  16639. (if (and extra (string-match "\\S-" extra))
  16640. (setq end (concat end "\n" extra))))
  16641. (if (string-match "\\S-" end)
  16642. (concat tpl "\n" end)
  16643. tpl)))
  16644. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16645. "Turn an alist of packages into a string with the \\usepackage macros."
  16646. (setq pkg (mapconcat (lambda(p)
  16647. (cond
  16648. ((stringp p) p)
  16649. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16650. (format "%% Package %s omitted" (cadr p)))
  16651. ((equal "" (car p))
  16652. (format "\\usepackage{%s}" (cadr p)))
  16653. (t
  16654. (format "\\usepackage[%s]{%s}"
  16655. (car p) (cadr p)))))
  16656. pkg
  16657. "\n"))
  16658. (if newline (concat pkg "\n") pkg))
  16659. (defun org-dvipng-color (attr)
  16660. "Return a RGB color specification for dvipng."
  16661. (apply 'format "rgb %s %s %s"
  16662. (mapcar 'org-normalize-color
  16663. (if (featurep 'xemacs)
  16664. (color-rgb-components
  16665. (face-property 'default
  16666. (cond ((eq attr :foreground) 'foreground)
  16667. ((eq attr :background) 'background))))
  16668. (color-values (face-attribute 'default attr nil))))))
  16669. (defun org-dvipng-color-format (color-name)
  16670. "Convert COLOR-NAME to a RGB color value for dvipng."
  16671. (apply 'format "rgb %s %s %s"
  16672. (mapcar 'org-normalize-color
  16673. (color-values color-name))))
  16674. (defun org-latex-color (attr)
  16675. "Return a RGB color for the LaTeX color package."
  16676. (apply 'format "%s,%s,%s"
  16677. (mapcar 'org-normalize-color
  16678. (if (featurep 'xemacs)
  16679. (color-rgb-components
  16680. (face-property 'default
  16681. (cond ((eq attr :foreground) 'foreground)
  16682. ((eq attr :background) 'background))))
  16683. (color-values (face-attribute 'default attr nil))))))
  16684. (defun org-latex-color-format (color-name)
  16685. "Convert COLOR-NAME to a RGB color value."
  16686. (apply 'format "%s,%s,%s"
  16687. (mapcar 'org-normalize-color
  16688. (color-values color-name))))
  16689. (defun org-normalize-color (value)
  16690. "Return string to be used as color value for an RGB component."
  16691. (format "%g" (/ value 65535.0)))
  16692. ;; Image display
  16693. (defvar org-inline-image-overlays nil)
  16694. (make-variable-buffer-local 'org-inline-image-overlays)
  16695. (defun org-toggle-inline-images (&optional include-linked)
  16696. "Toggle the display of inline images.
  16697. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16698. (interactive "P")
  16699. (if org-inline-image-overlays
  16700. (progn
  16701. (org-remove-inline-images)
  16702. (message "Inline image display turned off"))
  16703. (org-display-inline-images include-linked)
  16704. (if (and (org-called-interactively-p)
  16705. org-inline-image-overlays)
  16706. (message "%d images displayed inline"
  16707. (length org-inline-image-overlays))
  16708. (message "No images to display inline"))))
  16709. (defun org-redisplay-inline-images ()
  16710. "Refresh the display of inline images."
  16711. (interactive)
  16712. (if (not org-inline-image-overlays)
  16713. (org-toggle-inline-images)
  16714. (org-toggle-inline-images)
  16715. (org-toggle-inline-images)))
  16716. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16717. "Display inline images.
  16718. Normally only links without a description part are inlined, because this
  16719. is how it will work for export. When INCLUDE-LINKED is set, also links
  16720. with a description part will be inlined. This can be nice for a quick
  16721. look at those images, but it does not reflect what exported files will look
  16722. like.
  16723. When REFRESH is set, refresh existing images between BEG and END.
  16724. This will create new image displays only if necessary.
  16725. BEG and END default to the buffer boundaries."
  16726. (interactive "P")
  16727. (unless refresh
  16728. (org-remove-inline-images)
  16729. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16730. (save-excursion
  16731. (save-restriction
  16732. (widen)
  16733. (setq beg (or beg (point-min)) end (or end (point-max)))
  16734. (goto-char beg)
  16735. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16736. (substring (org-image-file-name-regexp) 0 -2)
  16737. "\\)\\]" (if include-linked "" "\\]")))
  16738. old file ov img type attrwidth width)
  16739. (while (re-search-forward re end t)
  16740. (setq old (get-char-property-and-overlay (match-beginning 1)
  16741. 'org-image-overlay)
  16742. file (expand-file-name
  16743. (concat (or (match-string 3) "") (match-string 4))))
  16744. (when (image-type-available-p 'imagemagick)
  16745. (setq attrwidth (if (or (listp org-image-actual-width)
  16746. (null org-image-actual-width))
  16747. (save-excursion
  16748. (save-match-data
  16749. (when (re-search-backward
  16750. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16751. (save-excursion
  16752. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16753. (string-to-number (match-string 1))))))
  16754. width (cond ((eq org-image-actual-width t) nil)
  16755. ((null org-image-actual-width) attrwidth)
  16756. ((numberp org-image-actual-width)
  16757. org-image-actual-width)
  16758. ((listp org-image-actual-width)
  16759. (or attrwidth (car org-image-actual-width))))
  16760. type (if width 'imagemagick)))
  16761. (when (file-exists-p file)
  16762. (if (and (car-safe old) refresh)
  16763. (image-refresh (overlay-get (cdr old) 'display))
  16764. (setq img (save-match-data (create-image file type nil :width width)))
  16765. (when img
  16766. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16767. (overlay-put ov 'display img)
  16768. (overlay-put ov 'face 'default)
  16769. (overlay-put ov 'org-image-overlay t)
  16770. (overlay-put ov 'modification-hooks
  16771. (list 'org-display-inline-remove-overlay))
  16772. (push ov org-inline-image-overlays)))))))))
  16773. (define-obsolete-function-alias
  16774. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16775. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16776. "Remove inline-display overlay if a corresponding region is modified."
  16777. (let ((inhibit-modification-hooks t))
  16778. (when (and ov after)
  16779. (delete ov org-inline-image-overlays)
  16780. (delete-overlay ov))))
  16781. (defun org-remove-inline-images ()
  16782. "Remove inline display of images."
  16783. (interactive)
  16784. (mapc 'delete-overlay org-inline-image-overlays)
  16785. (setq org-inline-image-overlays nil))
  16786. ;;;; Key bindings
  16787. ;; Outline functions from `outline-mode-prefix-map'
  16788. ;; that can be remapped in Org:
  16789. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16790. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16791. (define-key org-mode-map [remap outline-forward-same-level]
  16792. 'org-forward-heading-same-level)
  16793. (define-key org-mode-map [remap outline-backward-same-level]
  16794. 'org-backward-heading-same-level)
  16795. (define-key org-mode-map [remap show-branches]
  16796. 'org-kill-note-or-show-branches)
  16797. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16798. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16799. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16800. ;; Outline functions from `outline-mode-prefix-map' that can not
  16801. ;; be remapped in Org:
  16802. ;;
  16803. ;; - the column "key binding" shows whether the Outline function is still
  16804. ;; available in Org mode on the same key that it has been bound to in
  16805. ;; Outline mode:
  16806. ;; - "overridden": key used for a different functionality in Org mode
  16807. ;; - else: key still bound to the same Outline function in Org mode
  16808. ;;
  16809. ;; | Outline function | key binding | Org replacement |
  16810. ;; |------------------------------------+-------------+-----------------------|
  16811. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16812. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16813. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16814. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16815. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16816. ;; | `show-entry' | overridden | no replacement |
  16817. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16818. ;; | `show-branches' | `C-c C-k' | still same function |
  16819. ;; | `show-subtree' | overridden | visibility cycling |
  16820. ;; | `show-all' | overridden | no replacement |
  16821. ;; | `hide-subtree' | overridden | visibility cycling |
  16822. ;; | `hide-body' | overridden | no replacement |
  16823. ;; | `hide-entry' | overridden | visibility cycling |
  16824. ;; | `hide-leaves' | overridden | no replacement |
  16825. ;; | `hide-sublevels' | overridden | no replacement |
  16826. ;; | `hide-other' | overridden | no replacement |
  16827. ;; Make `C-c C-x' a prefix key
  16828. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16829. ;; TAB key with modifiers
  16830. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16831. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16832. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16833. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16834. ;; The following line is necessary under Suse GNU/Linux
  16835. (unless (featurep 'xemacs)
  16836. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16837. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16838. (define-key org-mode-map [backtab] 'org-shifttab)
  16839. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16840. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16841. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16842. ;; Cursor keys with modifiers
  16843. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16844. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16845. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16846. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16847. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16848. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16849. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16850. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16851. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16852. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16853. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16854. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16855. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16856. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16857. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16858. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16859. ;; Babel keys
  16860. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16861. (mapc (lambda (pair)
  16862. (define-key org-babel-map (car pair) (cdr pair)))
  16863. org-babel-key-bindings)
  16864. ;;; Extra keys for tty access.
  16865. ;; We only set them when really needed because otherwise the
  16866. ;; menus don't show the simple keys
  16867. (when (or org-use-extra-keys
  16868. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16869. (not window-system))
  16870. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16871. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16872. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16873. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16874. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16875. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16876. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16877. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16878. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16879. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16880. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16881. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16882. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16883. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16884. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16885. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16886. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16887. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16888. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16889. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16890. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16891. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16892. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16893. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16894. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16895. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16896. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16897. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16898. ;; All the other keys
  16899. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16900. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16901. (if (boundp 'narrow-map)
  16902. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16903. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16904. (if (boundp 'narrow-map)
  16905. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16906. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16907. (if (boundp 'narrow-map)
  16908. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16909. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16910. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16911. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16912. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16913. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16914. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16915. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16916. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16917. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16918. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16919. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16920. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16921. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16922. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16923. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16924. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16925. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16926. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16927. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16928. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16929. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16930. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16931. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16932. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16933. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16934. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16935. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16936. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16937. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16938. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16939. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16940. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16941. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16942. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16943. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16944. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16945. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16946. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16947. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16948. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16949. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16950. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16951. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16952. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16953. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16954. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16955. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16956. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16957. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16958. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16959. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16960. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16961. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16962. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16963. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16964. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16965. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16966. (org-defkey org-mode-map "\C-m" 'org-return)
  16967. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16968. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16969. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16970. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16971. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16972. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16973. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16974. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16975. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16976. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16977. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16978. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16979. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16980. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16981. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16982. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16983. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16984. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16985. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16986. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16987. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16988. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16989. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16990. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16991. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16992. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16993. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16994. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16995. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16996. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16997. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16998. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16999. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17000. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17001. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17002. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17003. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17004. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17005. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17006. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17007. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17008. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17009. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17010. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17011. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17012. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17013. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17014. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17015. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17016. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17017. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17018. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17019. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17020. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17021. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17022. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17023. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17024. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17025. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17026. (when (featurep 'xemacs)
  17027. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17028. (defconst org-speed-commands-default
  17029. '(
  17030. ("Outline Navigation")
  17031. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17032. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17033. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17034. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17035. ("u" . (org-speed-move-safe 'outline-up-heading))
  17036. ("j" . org-goto)
  17037. ("g" . (org-refile t))
  17038. ("Outline Visibility")
  17039. ("c" . org-cycle)
  17040. ("C" . org-shifttab)
  17041. (" " . org-display-outline-path)
  17042. ("=" . org-columns)
  17043. ("Outline Structure Editing")
  17044. ("U" . org-shiftmetaup)
  17045. ("D" . org-shiftmetadown)
  17046. ("r" . org-metaright)
  17047. ("l" . org-metaleft)
  17048. ("R" . org-shiftmetaright)
  17049. ("L" . org-shiftmetaleft)
  17050. ("i" . (progn (forward-char 1) (call-interactively
  17051. 'org-insert-heading-respect-content)))
  17052. ("^" . org-sort)
  17053. ("w" . org-refile)
  17054. ("a" . org-archive-subtree-default-with-confirmation)
  17055. ("." . org-mark-subtree)
  17056. ("#" . org-toggle-comment)
  17057. ("Clock Commands")
  17058. ("I" . org-clock-in)
  17059. ("O" . org-clock-out)
  17060. ("Meta Data Editing")
  17061. ("t" . org-todo)
  17062. ("," . (org-priority))
  17063. ("0" . (org-priority ?\ ))
  17064. ("1" . (org-priority ?A))
  17065. ("2" . (org-priority ?B))
  17066. ("3" . (org-priority ?C))
  17067. (":" . org-set-tags-command)
  17068. ("e" . org-set-effort)
  17069. ("E" . org-inc-effort)
  17070. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17071. (org-entry-put (point) "APPT_WARNTIME" m)))
  17072. ("Agenda Views etc")
  17073. ("v" . org-agenda)
  17074. ("/" . org-sparse-tree)
  17075. ("Misc")
  17076. ("o" . org-open-at-point)
  17077. ("?" . org-speed-command-help)
  17078. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17079. (">" . (org-agenda-remove-restriction-lock))
  17080. )
  17081. "The default speed commands.")
  17082. (defun org-print-speed-command (e)
  17083. (if (> (length (car e)) 1)
  17084. (progn
  17085. (princ "\n")
  17086. (princ (car e))
  17087. (princ "\n")
  17088. (princ (make-string (length (car e)) ?-))
  17089. (princ "\n"))
  17090. (princ (car e))
  17091. (princ " ")
  17092. (if (symbolp (cdr e))
  17093. (princ (symbol-name (cdr e)))
  17094. (prin1 (cdr e)))
  17095. (princ "\n")))
  17096. (defun org-speed-command-help ()
  17097. "Show the available speed commands."
  17098. (interactive)
  17099. (if (not org-use-speed-commands)
  17100. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17101. (with-output-to-temp-buffer "*Help*"
  17102. (princ "User-defined Speed commands\n===========================\n")
  17103. (mapc 'org-print-speed-command org-speed-commands-user)
  17104. (princ "\n")
  17105. (princ "Built-in Speed commands\n=======================\n")
  17106. (mapc 'org-print-speed-command org-speed-commands-default))
  17107. (with-current-buffer "*Help*"
  17108. (setq truncate-lines t))))
  17109. (defun org-speed-move-safe (cmd)
  17110. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17111. If not, return to the original position and throw an error."
  17112. (interactive)
  17113. (let ((pos (point)))
  17114. (call-interactively cmd)
  17115. (unless (and (bolp) (org-at-heading-p))
  17116. (goto-char pos)
  17117. (error "Boundary reached while executing %s" cmd))))
  17118. (defvar org-self-insert-command-undo-counter 0)
  17119. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17120. (defvar org-speed-command nil)
  17121. (define-obsolete-function-alias
  17122. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17123. (defun org-speed-command-activate (keys)
  17124. "Hook for activating single-letter speed commands.
  17125. `org-speed-commands-default' specifies a minimal command set.
  17126. Use `org-speed-commands-user' for further customization."
  17127. (when (or (and (bolp) (looking-at org-outline-regexp))
  17128. (and (functionp org-use-speed-commands)
  17129. (funcall org-use-speed-commands)))
  17130. (cdr (assoc keys (append org-speed-commands-user
  17131. org-speed-commands-default)))))
  17132. (define-obsolete-function-alias
  17133. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17134. (defun org-babel-speed-command-activate (keys)
  17135. "Hook for activating single-letter code block commands."
  17136. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17137. (cdr (assoc keys org-babel-key-bindings))))
  17138. (defcustom org-speed-command-hook
  17139. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17140. "Hook for activating speed commands at strategic locations.
  17141. Hook functions are called in sequence until a valid handler is
  17142. found.
  17143. Each hook takes a single argument, a user-pressed command key
  17144. which is also a `self-insert-command' from the global map.
  17145. Within the hook, examine the cursor position and the command key
  17146. and return nil or a valid handler as appropriate. Handler could
  17147. be one of an interactive command, a function, or a form.
  17148. Set `org-use-speed-commands' to non-nil value to enable this
  17149. hook. The default setting is `org-speed-command-activate'."
  17150. :group 'org-structure
  17151. :version "24.1"
  17152. :type 'hook)
  17153. (defun org-self-insert-command (N)
  17154. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17155. If the cursor is in a table looking at whitespace, the whitespace is
  17156. overwritten, and the table is not marked as requiring realignment."
  17157. (interactive "p")
  17158. (org-check-before-invisible-edit 'insert)
  17159. (cond
  17160. ((and org-use-speed-commands
  17161. (setq org-speed-command
  17162. (run-hook-with-args-until-success
  17163. 'org-speed-command-hook (this-command-keys))))
  17164. (cond
  17165. ((commandp org-speed-command)
  17166. (setq this-command org-speed-command)
  17167. (call-interactively org-speed-command))
  17168. ((functionp org-speed-command)
  17169. (funcall org-speed-command))
  17170. ((and org-speed-command (listp org-speed-command))
  17171. (eval org-speed-command))
  17172. (t (let (org-use-speed-commands)
  17173. (call-interactively 'org-self-insert-command)))))
  17174. ((and
  17175. (org-table-p)
  17176. (progn
  17177. ;; check if we blank the field, and if that triggers align
  17178. (and (featurep 'org-table) org-table-auto-blank-field
  17179. (member last-command
  17180. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17181. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17182. ;; got extra space, this field does not determine column width
  17183. (let (org-table-may-need-update) (org-table-blank-field))
  17184. ;; no extra space, this field may determine column width
  17185. (org-table-blank-field)))
  17186. t)
  17187. (eq N 1)
  17188. (looking-at "[^|\n]* |"))
  17189. (let (org-table-may-need-update)
  17190. (goto-char (1- (match-end 0)))
  17191. (backward-delete-char 1)
  17192. (goto-char (match-beginning 0))
  17193. (self-insert-command N)))
  17194. (t
  17195. (setq org-table-may-need-update t)
  17196. (self-insert-command N)
  17197. (org-fix-tags-on-the-fly)
  17198. (if org-self-insert-cluster-for-undo
  17199. (if (not (eq last-command 'org-self-insert-command))
  17200. (setq org-self-insert-command-undo-counter 1)
  17201. (if (>= org-self-insert-command-undo-counter 20)
  17202. (setq org-self-insert-command-undo-counter 1)
  17203. (and (> org-self-insert-command-undo-counter 0)
  17204. buffer-undo-list (listp buffer-undo-list)
  17205. (not (cadr buffer-undo-list)) ; remove nil entry
  17206. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17207. (setq org-self-insert-command-undo-counter
  17208. (1+ org-self-insert-command-undo-counter))))))))
  17209. (defun org-check-before-invisible-edit (kind)
  17210. "Check is editing if kind KIND would be dangerous with invisible text around.
  17211. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17212. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17213. (if (and org-catch-invisible-edits
  17214. (or (not (boundp 'visible-mode)) (not visible-mode))
  17215. (or (get-char-property (point) 'invisible)
  17216. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17217. ;; OK, we need to take a closer look
  17218. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17219. (invisible-before-point (if (bobp) nil (get-char-property
  17220. (1- (point)) 'invisible)))
  17221. (border-and-ok-direction
  17222. (or
  17223. ;; Check if we are acting predictably before invisible text
  17224. (and invisible-at-point (not invisible-before-point)
  17225. (memq kind '(insert delete-backward)))
  17226. ;; Check if we are acting predictably after invisible text
  17227. ;; This works not well, and I have turned it off. It seems
  17228. ;; better to always show and stop after invisible text.
  17229. ;; (and (not invisible-at-point) invisible-before-point
  17230. ;; (memq kind '(insert delete)))
  17231. )))
  17232. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17233. (memq invisible-before-point '(outline org-hide-block t)))
  17234. (if (eq org-catch-invisible-edits 'error)
  17235. (error "Editing in invisible areas is prohibited - make visible first"))
  17236. (if (and org-custom-properties-overlays
  17237. (y-or-n-p "Display invisible properties in this buffer? "))
  17238. (org-toggle-custom-properties-visibility)
  17239. ;; Make the area visible
  17240. (save-excursion
  17241. (if invisible-before-point
  17242. (goto-char (previous-single-char-property-change
  17243. (point) 'invisible)))
  17244. (org-cycle))
  17245. (cond
  17246. ((eq org-catch-invisible-edits 'show)
  17247. ;; That's it, we do the edit after showing
  17248. (message
  17249. "Unfolding invisible region around point before editing")
  17250. (sit-for 1))
  17251. ((and (eq org-catch-invisible-edits 'smart)
  17252. border-and-ok-direction)
  17253. (message "Unfolding invisible region around point before editing"))
  17254. (t
  17255. ;; Don't do the edit, make the user repeat it in full visibility
  17256. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17257. (defun org-fix-tags-on-the-fly ()
  17258. (when (and (equal (char-after (point-at-bol)) ?*)
  17259. (org-at-heading-p))
  17260. (org-align-tags-here org-tags-column)))
  17261. (defun org-delete-backward-char (N)
  17262. "Like `delete-backward-char', insert whitespace at field end in tables.
  17263. When deleting backwards, in tables this function will insert whitespace in
  17264. front of the next \"|\" separator, to keep the table aligned. The table will
  17265. still be marked for re-alignment if the field did fill the entire column,
  17266. because, in this case the deletion might narrow the column."
  17267. (interactive "p")
  17268. (save-match-data
  17269. (org-check-before-invisible-edit 'delete-backward)
  17270. (if (and (org-table-p)
  17271. (eq N 1)
  17272. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17273. (looking-at ".*?|"))
  17274. (let ((pos (point))
  17275. (noalign (looking-at "[^|\n\r]* |"))
  17276. (c org-table-may-need-update))
  17277. (backward-delete-char N)
  17278. (if (not overwrite-mode)
  17279. (progn
  17280. (skip-chars-forward "^|")
  17281. (insert " ")
  17282. (goto-char (1- pos))))
  17283. ;; noalign: if there were two spaces at the end, this field
  17284. ;; does not determine the width of the column.
  17285. (if noalign (setq org-table-may-need-update c)))
  17286. (backward-delete-char N)
  17287. (org-fix-tags-on-the-fly))))
  17288. (defun org-delete-char (N)
  17289. "Like `delete-char', but insert whitespace at field end in tables.
  17290. When deleting characters, in tables this function will insert whitespace in
  17291. front of the next \"|\" separator, to keep the table aligned. The table will
  17292. still be marked for re-alignment if the field did fill the entire column,
  17293. because, in this case the deletion might narrow the column."
  17294. (interactive "p")
  17295. (save-match-data
  17296. (org-check-before-invisible-edit 'delete)
  17297. (if (and (org-table-p)
  17298. (not (bolp))
  17299. (not (= (char-after) ?|))
  17300. (eq N 1))
  17301. (if (looking-at ".*?|")
  17302. (let ((pos (point))
  17303. (noalign (looking-at "[^|\n\r]* |"))
  17304. (c org-table-may-need-update))
  17305. (replace-match (concat
  17306. (substring (match-string 0) 1 -1)
  17307. " |"))
  17308. (goto-char pos)
  17309. ;; noalign: if there were two spaces at the end, this field
  17310. ;; does not determine the width of the column.
  17311. (if noalign (setq org-table-may-need-update c)))
  17312. (delete-char N))
  17313. (delete-char N)
  17314. (org-fix-tags-on-the-fly))))
  17315. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17316. (put 'org-self-insert-command 'delete-selection t)
  17317. (put 'orgtbl-self-insert-command 'delete-selection t)
  17318. (put 'org-delete-char 'delete-selection 'supersede)
  17319. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17320. (put 'org-yank 'delete-selection 'yank)
  17321. ;; Make `flyspell-mode' delay after some commands
  17322. (put 'org-self-insert-command 'flyspell-delayed t)
  17323. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17324. (put 'org-delete-char 'flyspell-delayed t)
  17325. (put 'org-delete-backward-char 'flyspell-delayed t)
  17326. ;; Make pabbrev-mode expand after org-mode commands
  17327. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17328. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17329. ;; How to do this: Measure non-white length of current string
  17330. ;; If equal to column width, we should realign.
  17331. (defun org-remap (map &rest commands)
  17332. "In MAP, remap the functions given in COMMANDS.
  17333. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17334. (let (new old)
  17335. (while commands
  17336. (setq old (pop commands) new (pop commands))
  17337. (if (fboundp 'command-remapping)
  17338. (org-defkey map (vector 'remap old) new)
  17339. (substitute-key-definition old new map global-map)))))
  17340. (when (eq org-enable-table-editor 'optimized)
  17341. ;; If the user wants maximum table support, we need to hijack
  17342. ;; some standard editing functions
  17343. (org-remap org-mode-map
  17344. 'self-insert-command 'org-self-insert-command
  17345. 'delete-char 'org-delete-char
  17346. 'delete-backward-char 'org-delete-backward-char)
  17347. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17348. (defvar org-ctrl-c-ctrl-c-hook nil
  17349. "Hook for functions attaching themselves to `C-c C-c'.
  17350. This can be used to add additional functionality to the C-c C-c
  17351. key which executes context-dependent commands. This hook is run
  17352. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17353. run after the last test.
  17354. Each function will be called with no arguments. The function
  17355. must check if the context is appropriate for it to act. If yes,
  17356. it should do its thing and then return a non-nil value. If the
  17357. context is wrong, just do nothing and return nil.")
  17358. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17359. "Hook for functions attaching themselves to `C-c C-c'.
  17360. This can be used to add additional functionality to the C-c C-c
  17361. key which executes context-dependent commands. This hook is run
  17362. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17363. before the first test.
  17364. Each function will be called with no arguments. The function
  17365. must check if the context is appropriate for it to act. If yes,
  17366. it should do its thing and then return a non-nil value. If the
  17367. context is wrong, just do nothing and return nil.")
  17368. (defvar org-tab-first-hook nil
  17369. "Hook for functions to attach themselves to TAB.
  17370. See `org-ctrl-c-ctrl-c-hook' for more information.
  17371. This hook runs as the first action when TAB is pressed, even before
  17372. `org-cycle' messes around with the `outline-regexp' to cater for
  17373. inline tasks and plain list item folding.
  17374. If any function in this hook returns t, any other actions that
  17375. would have been caused by TAB (such as table field motion or visibility
  17376. cycling) will not occur.")
  17377. (defvar org-tab-after-check-for-table-hook nil
  17378. "Hook for functions to attach themselves to TAB.
  17379. See `org-ctrl-c-ctrl-c-hook' for more information.
  17380. This hook runs after it has been established that the cursor is not in a
  17381. table, but before checking if the cursor is in a headline or if global cycling
  17382. should be done.
  17383. If any function in this hook returns t, not other actions like visibility
  17384. cycling will be done.")
  17385. (defvar org-tab-after-check-for-cycling-hook nil
  17386. "Hook for functions to attach themselves to TAB.
  17387. See `org-ctrl-c-ctrl-c-hook' for more information.
  17388. This hook runs after it has been established that not table field motion and
  17389. not visibility should be done because of current context. This is probably
  17390. the place where a package like yasnippets can hook in.")
  17391. (defvar org-tab-before-tab-emulation-hook nil
  17392. "Hook for functions to attach themselves to TAB.
  17393. See `org-ctrl-c-ctrl-c-hook' for more information.
  17394. This hook runs after every other options for TAB have been exhausted, but
  17395. before indentation and \t insertion takes place.")
  17396. (defvar org-metaleft-hook nil
  17397. "Hook for functions attaching themselves to `M-left'.
  17398. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17399. (defvar org-metaright-hook nil
  17400. "Hook for functions attaching themselves to `M-right'.
  17401. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17402. (defvar org-metaup-hook nil
  17403. "Hook for functions attaching themselves to `M-up'.
  17404. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17405. (defvar org-metadown-hook nil
  17406. "Hook for functions attaching themselves to `M-down'.
  17407. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17408. (defvar org-shiftmetaleft-hook nil
  17409. "Hook for functions attaching themselves to `M-S-left'.
  17410. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17411. (defvar org-shiftmetaright-hook nil
  17412. "Hook for functions attaching themselves to `M-S-right'.
  17413. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17414. (defvar org-shiftmetaup-hook nil
  17415. "Hook for functions attaching themselves to `M-S-up'.
  17416. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17417. (defvar org-shiftmetadown-hook nil
  17418. "Hook for functions attaching themselves to `M-S-down'.
  17419. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17420. (defvar org-metareturn-hook nil
  17421. "Hook for functions attaching themselves to `M-RET'.
  17422. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17423. (defvar org-shiftup-hook nil
  17424. "Hook for functions attaching themselves to `S-up'.
  17425. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17426. (defvar org-shiftup-final-hook nil
  17427. "Hook for functions attaching themselves to `S-up'.
  17428. This one runs after all other options except shift-select have been excluded.
  17429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17430. (defvar org-shiftdown-hook nil
  17431. "Hook for functions attaching themselves to `S-down'.
  17432. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17433. (defvar org-shiftdown-final-hook nil
  17434. "Hook for functions attaching themselves to `S-down'.
  17435. This one runs after all other options except shift-select have been excluded.
  17436. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17437. (defvar org-shiftleft-hook nil
  17438. "Hook for functions attaching themselves to `S-left'.
  17439. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17440. (defvar org-shiftleft-final-hook nil
  17441. "Hook for functions attaching themselves to `S-left'.
  17442. This one runs after all other options except shift-select have been excluded.
  17443. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17444. (defvar org-shiftright-hook nil
  17445. "Hook for functions attaching themselves to `S-right'.
  17446. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17447. (defvar org-shiftright-final-hook nil
  17448. "Hook for functions attaching themselves to `S-right'.
  17449. This one runs after all other options except shift-select have been excluded.
  17450. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17451. (defun org-modifier-cursor-error ()
  17452. "Throw an error, a modified cursor command was applied in wrong context."
  17453. (error "This command is active in special context like tables, headlines or items"))
  17454. (defun org-shiftselect-error ()
  17455. "Throw an error because Shift-Cursor command was applied in wrong context."
  17456. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17457. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17458. (error "This command works only in special context like headlines or timestamps")))
  17459. (defun org-call-for-shift-select (cmd)
  17460. (let ((this-command-keys-shift-translated t))
  17461. (call-interactively cmd)))
  17462. (defun org-shifttab (&optional arg)
  17463. "Global visibility cycling or move to previous table field.
  17464. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17465. on context.
  17466. See the individual commands for more information."
  17467. (interactive "P")
  17468. (cond
  17469. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17470. ((integerp arg)
  17471. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17472. (message "Content view to level: %d" arg)
  17473. (org-content (prefix-numeric-value arg2))
  17474. (setq org-cycle-global-status 'overview)))
  17475. (t (call-interactively 'org-global-cycle))))
  17476. (defun org-shiftmetaleft ()
  17477. "Promote subtree or delete table column.
  17478. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17479. `org-table-delete-column', depending on context. See the
  17480. individual commands for more information."
  17481. (interactive)
  17482. (cond
  17483. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17484. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17485. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17486. ((if (not (org-region-active-p)) (org-at-item-p)
  17487. (save-excursion (goto-char (region-beginning))
  17488. (org-at-item-p)))
  17489. (call-interactively 'org-outdent-item-tree))
  17490. (t (org-modifier-cursor-error))))
  17491. (defun org-shiftmetaright ()
  17492. "Demote subtree or insert table column.
  17493. Calls `org-demote-subtree', `org-indent-item-tree', or
  17494. `org-table-insert-column', depending on context. See the
  17495. individual commands for more information."
  17496. (interactive)
  17497. (cond
  17498. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17499. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17500. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17501. ((if (not (org-region-active-p)) (org-at-item-p)
  17502. (save-excursion (goto-char (region-beginning))
  17503. (org-at-item-p)))
  17504. (call-interactively 'org-indent-item-tree))
  17505. (t (org-modifier-cursor-error))))
  17506. (defun org-shiftmetaup (&optional arg)
  17507. "Move subtree up or kill table row.
  17508. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17509. `org-move-item-up' or `org-timestamp-up', depending on context.
  17510. See the individual commands for more information."
  17511. (interactive "P")
  17512. (cond
  17513. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17514. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17515. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17516. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17517. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17518. (call-interactively 'org-timestamp-up)))
  17519. (t (org-modifier-cursor-error))))
  17520. (defun org-shiftmetadown (&optional arg)
  17521. "Move subtree down or insert table row.
  17522. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17523. `org-move-item-down' or `org-timestamp-up', depending on context.
  17524. See the individual commands for more information."
  17525. (interactive "P")
  17526. (cond
  17527. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17528. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17529. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17530. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17531. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17532. (call-interactively 'org-timestamp-down)))
  17533. (t (org-modifier-cursor-error))))
  17534. (defsubst org-hidden-tree-error ()
  17535. (error
  17536. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17537. (defun org-metaleft (&optional arg)
  17538. "Promote heading or move table column to left.
  17539. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17540. With no specific context, calls the Emacs default `backward-word'.
  17541. See the individual commands for more information."
  17542. (interactive "P")
  17543. (cond
  17544. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17545. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17546. ((org-with-limited-levels
  17547. (or (org-at-heading-p)
  17548. (and (org-region-active-p)
  17549. (save-excursion
  17550. (goto-char (region-beginning))
  17551. (org-at-heading-p)))))
  17552. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17553. (call-interactively 'org-do-promote))
  17554. ;; At an inline task.
  17555. ((org-at-heading-p)
  17556. (call-interactively 'org-inlinetask-promote))
  17557. ((or (org-at-item-p)
  17558. (and (org-region-active-p)
  17559. (save-excursion
  17560. (goto-char (region-beginning))
  17561. (org-at-item-p))))
  17562. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17563. (call-interactively 'org-outdent-item))
  17564. (t (call-interactively 'backward-word))))
  17565. (defun org-metaright (&optional arg)
  17566. "Demote a subtree, a list item or move table column to right.
  17567. In front of a drawer or a block keyword, indent it correctly.
  17568. With no specific context, calls the Emacs default `forward-word'.
  17569. See the individual commands for more information."
  17570. (interactive "P")
  17571. (cond
  17572. ((run-hook-with-args-until-success 'org-metaright-hook))
  17573. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17574. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17575. ((org-at-block-p) (call-interactively 'org-indent-block))
  17576. ((org-with-limited-levels
  17577. (or (org-at-heading-p)
  17578. (and (org-region-active-p)
  17579. (save-excursion
  17580. (goto-char (region-beginning))
  17581. (org-at-heading-p)))))
  17582. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17583. (call-interactively 'org-do-demote))
  17584. ;; At an inline task.
  17585. ((org-at-heading-p)
  17586. (call-interactively 'org-inlinetask-demote))
  17587. ((or (org-at-item-p)
  17588. (and (org-region-active-p)
  17589. (save-excursion
  17590. (goto-char (region-beginning))
  17591. (org-at-item-p))))
  17592. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17593. (call-interactively 'org-indent-item))
  17594. (t (call-interactively 'forward-word))))
  17595. (defun org-check-for-hidden (what)
  17596. "Check if there are hidden headlines/items in the current visual line.
  17597. WHAT can be either `headlines' or `items'. If the current line is
  17598. an outline or item heading and it has a folded subtree below it,
  17599. this function returns t, nil otherwise."
  17600. (let ((re (cond
  17601. ((eq what 'headlines) org-outline-regexp-bol)
  17602. ((eq what 'items) (org-item-beginning-re))
  17603. (t (error "This should not happen"))))
  17604. beg end)
  17605. (save-excursion
  17606. (catch 'exit
  17607. (unless (org-region-active-p)
  17608. (setq beg (point-at-bol))
  17609. (beginning-of-line 2)
  17610. (while (and (not (eobp)) ;; this is like `next-line'
  17611. (get-char-property (1- (point)) 'invisible))
  17612. (beginning-of-line 2))
  17613. (setq end (point))
  17614. (goto-char beg)
  17615. (goto-char (point-at-eol))
  17616. (setq end (max end (point)))
  17617. (while (re-search-forward re end t)
  17618. (if (get-char-property (match-beginning 0) 'invisible)
  17619. (throw 'exit t))))
  17620. nil))))
  17621. (defun org-metaup (&optional arg)
  17622. "Move subtree up or move table row up.
  17623. Calls `org-move-subtree-up' or `org-table-move-row' or
  17624. `org-move-item-up', depending on context. See the individual commands
  17625. for more information."
  17626. (interactive "P")
  17627. (cond
  17628. ((run-hook-with-args-until-success 'org-metaup-hook))
  17629. ((org-region-active-p)
  17630. (let* ((a (min (region-beginning) (region-end)))
  17631. (b (1- (max (region-beginning) (region-end))))
  17632. (c (save-excursion (goto-char a)
  17633. (move-beginning-of-line 0)))
  17634. (d (save-excursion (goto-char a)
  17635. (move-end-of-line 0) (point))))
  17636. (transpose-regions a b c d)
  17637. (goto-char c)))
  17638. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17639. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17640. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17641. (t (org-drag-element-backward))))
  17642. (defun org-metadown (&optional arg)
  17643. "Move subtree down or move table row down.
  17644. Calls `org-move-subtree-down' or `org-table-move-row' or
  17645. `org-move-item-down', depending on context. See the individual
  17646. commands for more information."
  17647. (interactive "P")
  17648. (cond
  17649. ((run-hook-with-args-until-success 'org-metadown-hook))
  17650. ((org-region-active-p)
  17651. (let* ((a (min (region-beginning) (region-end)))
  17652. (b (max (region-beginning) (region-end)))
  17653. (c (save-excursion (goto-char b)
  17654. (move-beginning-of-line 1)))
  17655. (d (save-excursion (goto-char b)
  17656. (move-end-of-line 1) (1+ (point)))))
  17657. (transpose-regions a b c d)
  17658. (goto-char d)))
  17659. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17660. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17661. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17662. (t (org-drag-element-forward))))
  17663. (defun org-shiftup (&optional arg)
  17664. "Increase item in timestamp or increase priority of current headline.
  17665. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17666. depending on context. See the individual commands for more information."
  17667. (interactive "P")
  17668. (cond
  17669. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17670. ((and org-support-shift-select (org-region-active-p))
  17671. (org-call-for-shift-select 'previous-line))
  17672. ((org-at-timestamp-p t)
  17673. (call-interactively (if org-edit-timestamp-down-means-later
  17674. 'org-timestamp-down 'org-timestamp-up)))
  17675. ((and (not (eq org-support-shift-select 'always))
  17676. org-enable-priority-commands
  17677. (org-at-heading-p))
  17678. (call-interactively 'org-priority-up))
  17679. ((and (not org-support-shift-select) (org-at-item-p))
  17680. (call-interactively 'org-previous-item))
  17681. ((org-clocktable-try-shift 'up arg))
  17682. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17683. (org-support-shift-select
  17684. (org-call-for-shift-select 'previous-line))
  17685. (t (org-shiftselect-error))))
  17686. (defun org-shiftdown (&optional arg)
  17687. "Decrease item in timestamp or decrease priority of current headline.
  17688. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17689. depending on context. See the individual commands for more information."
  17690. (interactive "P")
  17691. (cond
  17692. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17693. ((and org-support-shift-select (org-region-active-p))
  17694. (org-call-for-shift-select 'next-line))
  17695. ((org-at-timestamp-p t)
  17696. (call-interactively (if org-edit-timestamp-down-means-later
  17697. 'org-timestamp-up 'org-timestamp-down)))
  17698. ((and (not (eq org-support-shift-select 'always))
  17699. org-enable-priority-commands
  17700. (org-at-heading-p))
  17701. (call-interactively 'org-priority-down))
  17702. ((and (not org-support-shift-select) (org-at-item-p))
  17703. (call-interactively 'org-next-item))
  17704. ((org-clocktable-try-shift 'down arg))
  17705. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17706. (org-support-shift-select
  17707. (org-call-for-shift-select 'next-line))
  17708. (t (org-shiftselect-error))))
  17709. (defun org-shiftright (&optional arg)
  17710. "Cycle the thing at point or in the current line, depending on context.
  17711. Depending on context, this does one of the following:
  17712. - switch a timestamp at point one day into the future
  17713. - on a headline, switch to the next TODO keyword.
  17714. - on an item, switch entire list to the next bullet type
  17715. - on a property line, switch to the next allowed value
  17716. - on a clocktable definition line, move time block into the future"
  17717. (interactive "P")
  17718. (cond
  17719. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17720. ((and org-support-shift-select (org-region-active-p))
  17721. (org-call-for-shift-select 'forward-char))
  17722. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17723. ((and (not (eq org-support-shift-select 'always))
  17724. (org-at-heading-p))
  17725. (let ((org-inhibit-logging
  17726. (not org-treat-S-cursor-todo-selection-as-state-change))
  17727. (org-inhibit-blocking
  17728. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17729. (org-call-with-arg 'org-todo 'right)))
  17730. ((or (and org-support-shift-select
  17731. (not (eq org-support-shift-select 'always))
  17732. (org-at-item-bullet-p))
  17733. (and (not org-support-shift-select) (org-at-item-p)))
  17734. (org-call-with-arg 'org-cycle-list-bullet nil))
  17735. ((and (not (eq org-support-shift-select 'always))
  17736. (org-at-property-p))
  17737. (call-interactively 'org-property-next-allowed-value))
  17738. ((org-clocktable-try-shift 'right arg))
  17739. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17740. (org-support-shift-select
  17741. (org-call-for-shift-select 'forward-char))
  17742. (t (org-shiftselect-error))))
  17743. (defun org-shiftleft (&optional arg)
  17744. "Cycle the thing at point or in the current line, depending on context.
  17745. Depending on context, this does one of the following:
  17746. - switch a timestamp at point one day into the past
  17747. - on a headline, switch to the previous TODO keyword.
  17748. - on an item, switch entire list to the previous bullet type
  17749. - on a property line, switch to the previous allowed value
  17750. - on a clocktable definition line, move time block into the past"
  17751. (interactive "P")
  17752. (cond
  17753. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17754. ((and org-support-shift-select (org-region-active-p))
  17755. (org-call-for-shift-select 'backward-char))
  17756. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17757. ((and (not (eq org-support-shift-select 'always))
  17758. (org-at-heading-p))
  17759. (let ((org-inhibit-logging
  17760. (not org-treat-S-cursor-todo-selection-as-state-change))
  17761. (org-inhibit-blocking
  17762. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17763. (org-call-with-arg 'org-todo 'left)))
  17764. ((or (and org-support-shift-select
  17765. (not (eq org-support-shift-select 'always))
  17766. (org-at-item-bullet-p))
  17767. (and (not org-support-shift-select) (org-at-item-p)))
  17768. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17769. ((and (not (eq org-support-shift-select 'always))
  17770. (org-at-property-p))
  17771. (call-interactively 'org-property-previous-allowed-value))
  17772. ((org-clocktable-try-shift 'left arg))
  17773. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17774. (org-support-shift-select
  17775. (org-call-for-shift-select 'backward-char))
  17776. (t (org-shiftselect-error))))
  17777. (defun org-shiftcontrolright ()
  17778. "Switch to next TODO set."
  17779. (interactive)
  17780. (cond
  17781. ((and org-support-shift-select (org-region-active-p))
  17782. (org-call-for-shift-select 'forward-word))
  17783. ((and (not (eq org-support-shift-select 'always))
  17784. (org-at-heading-p))
  17785. (org-call-with-arg 'org-todo 'nextset))
  17786. (org-support-shift-select
  17787. (org-call-for-shift-select 'forward-word))
  17788. (t (org-shiftselect-error))))
  17789. (defun org-shiftcontrolleft ()
  17790. "Switch to previous TODO set."
  17791. (interactive)
  17792. (cond
  17793. ((and org-support-shift-select (org-region-active-p))
  17794. (org-call-for-shift-select 'backward-word))
  17795. ((and (not (eq org-support-shift-select 'always))
  17796. (org-at-heading-p))
  17797. (org-call-with-arg 'org-todo 'previousset))
  17798. (org-support-shift-select
  17799. (org-call-for-shift-select 'backward-word))
  17800. (t (org-shiftselect-error))))
  17801. (defun org-shiftcontrolup (&optional n)
  17802. "Change timestamps synchronously up in CLOCK log lines.
  17803. Optional argument N tells to change by that many units."
  17804. (interactive "P")
  17805. (cond ((and (not org-support-shift-select)
  17806. (org-at-clock-log-p)
  17807. (org-at-timestamp-p t))
  17808. (org-clock-timestamps-up n))
  17809. (t (org-shiftselect-error))))
  17810. (defun org-shiftcontroldown (&optional n)
  17811. "Change timestamps synchronously down in CLOCK log lines.
  17812. Optional argument N tells to change by that many units."
  17813. (interactive "P")
  17814. (cond ((and (not org-support-shift-select)
  17815. (org-at-clock-log-p)
  17816. (org-at-timestamp-p t))
  17817. (org-clock-timestamps-down n))
  17818. (t (org-shiftselect-error))))
  17819. (defun org-ctrl-c-ret ()
  17820. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17821. (interactive)
  17822. (cond
  17823. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17824. (t (call-interactively 'org-insert-heading))))
  17825. (defun org-find-visible ()
  17826. (let ((s (point)))
  17827. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17828. (get-char-property s 'invisible)))
  17829. s))
  17830. (defun org-find-invisible ()
  17831. (let ((s (point)))
  17832. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17833. (not (get-char-property s 'invisible))))
  17834. s))
  17835. (defun org-copy-visible (beg end)
  17836. "Copy the visible parts of the region."
  17837. (interactive "r")
  17838. (let (snippets s)
  17839. (save-excursion
  17840. (save-restriction
  17841. (narrow-to-region beg end)
  17842. (setq s (goto-char (point-min)))
  17843. (while (not (= (point) (point-max)))
  17844. (goto-char (org-find-invisible))
  17845. (push (buffer-substring s (point)) snippets)
  17846. (setq s (goto-char (org-find-visible))))))
  17847. (kill-new (apply 'concat (nreverse snippets)))))
  17848. (defun org-copy-special ()
  17849. "Copy region in table or copy current subtree.
  17850. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17851. See the individual commands for more information."
  17852. (interactive)
  17853. (call-interactively
  17854. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17855. (defun org-cut-special ()
  17856. "Cut region in table or cut current subtree.
  17857. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17858. See the individual commands for more information."
  17859. (interactive)
  17860. (call-interactively
  17861. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17862. (defun org-paste-special (arg)
  17863. "Paste rectangular region into table, or past subtree relative to level.
  17864. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17865. See the individual commands for more information."
  17866. (interactive "P")
  17867. (if (org-at-table-p)
  17868. (org-table-paste-rectangle)
  17869. (org-paste-subtree arg)))
  17870. (defsubst org-in-fixed-width-region-p ()
  17871. "Is point in a fixed-width region?"
  17872. (save-match-data
  17873. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17874. (defun org-edit-special (&optional arg)
  17875. "Call a special editor for the element at point.
  17876. When at a table, call the formula editor with `org-table-edit-formulas'.
  17877. When in a source code block, call `org-edit-src-code'.
  17878. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17879. When at an #+INCLUDE keyword, visit the included file.
  17880. On a link, call `ffap' to visit the link at point.
  17881. Otherwise, return a user error."
  17882. (interactive)
  17883. (let ((element (org-element-at-point)))
  17884. (case (org-element-type element)
  17885. (src-block
  17886. (if (not arg) (org-edit-src-code)
  17887. (let* ((info (org-babel-get-src-block-info))
  17888. (lang (nth 0 info))
  17889. (params (nth 2 info))
  17890. (session (cdr (assq :session params))))
  17891. (if (not session) (org-edit-src-code)
  17892. ;; At a src-block with a session and function called with
  17893. ;; an ARG: switch to the buffer related to the inferior
  17894. ;; process.
  17895. (funcall (intern (concat "org-babel-prep-session:" lang))
  17896. session params)))))
  17897. (keyword
  17898. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17899. (find-file
  17900. (org-remove-double-quotes
  17901. (car (org-split-string (org-element-property :value element)))))
  17902. (user-error "No special environment to edit here")))
  17903. (table
  17904. (if (eq (org-element-property :type element) 'table.el)
  17905. (org-edit-src-code)
  17906. (call-interactively 'org-table-edit-formulas)))
  17907. ;; Only Org tables contain `table-row' type elements.
  17908. (table-row (call-interactively 'org-table-edit-formulas))
  17909. ((example-block export-block) (org-edit-src-code))
  17910. (fixed-width (org-edit-fixed-width-region))
  17911. (otherwise
  17912. ;; No notable element at point. Though, we may be at a link,
  17913. ;; which is an object. Thus, scan deeper.
  17914. (if (eq (org-element-type (org-element-context element)) 'link)
  17915. (call-interactively 'ffap)
  17916. (user-error "No special environment to edit here"))))))
  17917. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17918. (defun org-ctrl-c-ctrl-c (&optional arg)
  17919. "Set tags in headline, or update according to changed information at point.
  17920. This command does many different things, depending on context:
  17921. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17922. this is what we do.
  17923. - If the cursor is on a statistics cookie, update it.
  17924. - If the cursor is in a headline, prompt for tags and insert them
  17925. into the current line, aligned to `org-tags-column'. When called
  17926. with prefix arg, realign all tags in the current buffer.
  17927. - If the cursor is in one of the special #+KEYWORD lines, this
  17928. triggers scanning the buffer for these lines and updating the
  17929. information.
  17930. - If the cursor is inside a table, realign the table. This command
  17931. works even if the automatic table editor has been turned off.
  17932. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17933. the entire table.
  17934. - If the cursor is at a footnote reference or definition, jump to
  17935. the corresponding definition or references, respectively.
  17936. - If the cursor is a the beginning of a dynamic block, update it.
  17937. - If the current buffer is a capture buffer, close note and file it.
  17938. - If the cursor is on a <<<target>>>, update radio targets and
  17939. corresponding links in this buffer.
  17940. - If the cursor is on a numbered item in a plain list, renumber the
  17941. ordered list.
  17942. - If the cursor is on a checkbox, toggle it.
  17943. - If the cursor is on a code block, evaluate it. The variable
  17944. `org-confirm-babel-evaluate' can be used to control prompting
  17945. before code block evaluation, by default every code block
  17946. evaluation requires confirmation. Code block evaluation can be
  17947. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17948. (interactive "P")
  17949. (cond
  17950. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17951. org-occur-highlights
  17952. org-latex-fragment-image-overlays)
  17953. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17954. (org-remove-occur-highlights)
  17955. (org-remove-latex-fragment-image-overlays)
  17956. (message "Temporary highlights/overlays removed from current buffer"))
  17957. ((and (local-variable-p 'org-finish-function (current-buffer))
  17958. (fboundp org-finish-function))
  17959. (funcall org-finish-function))
  17960. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17961. (t
  17962. (let* ((context (org-element-context)) (type (org-element-type context)))
  17963. ;; Test if point is within blanks at the end of an element.
  17964. (if (save-excursion
  17965. (or (not context)
  17966. (beginning-of-line)
  17967. (and (looking-at "[ \t]*$")
  17968. (skip-chars-forward " \r\t\n")
  17969. (>= (point) (org-element-property :end context)))))
  17970. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17971. (user-error "C-c C-c can do nothing useful at this location"))
  17972. ;; For convenience: at the first line of a paragraph on the
  17973. ;; same line as an item, apply function on that item instead.
  17974. (when (eq type 'paragraph)
  17975. (let ((parent (org-element-property :parent context)))
  17976. (when (and (eq (org-element-type parent) 'item)
  17977. (= (point-at-bol) (org-element-property :begin parent)))
  17978. (setq context parent type 'item))))
  17979. ;; Act according to type of element or object at point.
  17980. (case type
  17981. (clock (org-clock-update-time-maybe))
  17982. (dynamic-block
  17983. (save-excursion
  17984. (goto-char (org-element-property :post-affiliated context))
  17985. (org-update-dblock)))
  17986. (footnote-definition
  17987. (goto-char (org-element-property :post-affiliated context))
  17988. (call-interactively 'org-footnote-action))
  17989. (footnote-reference (call-interactively 'org-footnote-action))
  17990. ((headline inlinetask)
  17991. (save-excursion (goto-char (org-element-property :begin context))
  17992. (call-interactively 'org-set-tags)))
  17993. (item
  17994. ;; At an item: a double C-u set checkbox to "[-]"
  17995. ;; unconditionally, whereas a single one will toggle its
  17996. ;; presence. Without an universal argument, if the item
  17997. ;; has a checkbox, toggle it. Otherwise repair the list.
  17998. (let* ((box (org-element-property :checkbox context))
  17999. (struct (org-element-property :structure context))
  18000. (old-struct (copy-tree struct))
  18001. (parents (org-list-parents-alist struct))
  18002. (prevs (org-list-prevs-alist struct))
  18003. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18004. (org-list-set-checkbox
  18005. (org-element-property :begin context) struct
  18006. (cond ((equal arg '(16)) "[-]")
  18007. ((and (not box) (equal arg '(4))) "[ ]")
  18008. ((or (not box) (equal arg '(4))) nil)
  18009. ((eq box 'on) "[ ]")
  18010. (t "[X]")))
  18011. ;; Mimic `org-list-write-struct' but with grabbing
  18012. ;; a return value from `org-list-struct-fix-box'.
  18013. (org-list-struct-fix-ind struct parents 2)
  18014. (org-list-struct-fix-item-end struct)
  18015. (org-list-struct-fix-bul struct prevs)
  18016. (org-list-struct-fix-ind struct parents)
  18017. (let ((block-item
  18018. (org-list-struct-fix-box struct parents prevs orderedp)))
  18019. (if (and box (equal struct old-struct))
  18020. (if (equal arg '(16))
  18021. (message "Checkboxes already reset")
  18022. (user-error "Cannot toggle this checkbox: %s"
  18023. (if (eq box 'on)
  18024. "all subitems checked"
  18025. "unchecked subitems")))
  18026. (org-list-struct-apply-struct struct old-struct)
  18027. (org-update-checkbox-count-maybe))
  18028. (when block-item
  18029. (message "Checkboxes were removed due to empty box at line %d"
  18030. (org-current-line block-item))))))
  18031. (keyword
  18032. (let ((org-inhibit-startup-visibility-stuff t)
  18033. (org-startup-align-all-tables nil))
  18034. (when (boundp 'org-table-coordinate-overlays)
  18035. (mapc 'delete-overlay org-table-coordinate-overlays)
  18036. (setq org-table-coordinate-overlays nil))
  18037. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18038. (message "Local setup has been refreshed"))
  18039. (plain-list
  18040. ;; At a plain list, with a double C-u argument, set
  18041. ;; checkboxes of each item to "[-]", whereas a single one
  18042. ;; will toggle their presence according to the state of the
  18043. ;; first item in the list. Without an argument, repair the
  18044. ;; list.
  18045. (let* ((begin (org-element-property :contents-begin context))
  18046. (struct (org-element-property :structure context))
  18047. (old-struct (copy-tree struct))
  18048. (first-box (save-excursion
  18049. (goto-char begin)
  18050. (looking-at org-list-full-item-re)
  18051. (match-string-no-properties 3)))
  18052. (new-box (cond ((equal arg '(16)) "[-]")
  18053. ((equal arg '(4)) (unless first-box "[ ]"))
  18054. ((equal first-box "[X]") "[ ]")
  18055. (t "[X]"))))
  18056. (cond
  18057. (arg
  18058. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18059. (org-list-get-all-items
  18060. begin struct (org-list-prevs-alist struct))))
  18061. ((and first-box (eq (point) begin))
  18062. ;; For convenience, when point is at bol on the first
  18063. ;; item of the list and no argument is provided, simply
  18064. ;; toggle checkbox of that item, if any.
  18065. (org-list-set-checkbox begin struct new-box)))
  18066. (org-list-write-struct
  18067. struct (org-list-parents-alist struct) old-struct)
  18068. (org-update-checkbox-count-maybe)
  18069. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18070. ((property-drawer node-property)
  18071. (call-interactively 'org-property-action))
  18072. ((radio-target target)
  18073. (call-interactively 'org-update-radio-target-regexp))
  18074. (statistics-cookie
  18075. (call-interactively 'org-update-statistics-cookies))
  18076. ((table table-cell table-row)
  18077. ;; At a table, recalculate every field and align it. Also
  18078. ;; send the table if necessary. If the table has
  18079. ;; a `table.el' type, just give up. At a table row or
  18080. ;; cell, maybe recalculate line but always align table.
  18081. (if (eq (org-element-property :type context) 'table.el)
  18082. (message "Use C-c ' to edit table.el tables")
  18083. (let ((org-enable-table-editor t))
  18084. (if (or (eq type 'table)
  18085. ;; Check if point is at a TBLFM line.
  18086. (and (eq type 'table-row)
  18087. (= (point) (org-element-property :end context))))
  18088. (save-excursion
  18089. (goto-char (org-element-property :contents-begin context))
  18090. (org-call-with-arg 'org-table-recalculate (or arg t))
  18091. (orgtbl-send-table 'maybe))
  18092. (org-table-maybe-eval-formula)
  18093. (cond (arg (call-interactively 'org-table-recalculate))
  18094. ((org-table-maybe-recalculate-line))
  18095. (t (org-table-align)))))))
  18096. (timestamp (org-timestamp-change 0 'day))
  18097. (otherwise
  18098. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18099. (user-error
  18100. "C-c C-c can do nothing useful at this location")))))))))
  18101. (defun org-mode-restart ()
  18102. "Restart Org-mode, to scan again for special lines.
  18103. Also updates the keyword regular expressions."
  18104. (interactive)
  18105. (org-mode)
  18106. (message "Org-mode restarted"))
  18107. (defun org-kill-note-or-show-branches ()
  18108. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18109. (interactive)
  18110. (if (not org-finish-function)
  18111. (progn
  18112. (hide-subtree)
  18113. (call-interactively 'show-branches))
  18114. (let ((org-note-abort t))
  18115. (funcall org-finish-function))))
  18116. (defun org-return (&optional indent)
  18117. "Goto next table row or insert a newline.
  18118. Calls `org-table-next-row' or `newline', depending on context.
  18119. See the individual commands for more information."
  18120. (interactive)
  18121. (let (org-ts-what)
  18122. (cond
  18123. ((or (bobp) (org-in-src-block-p))
  18124. (if indent (newline-and-indent) (newline)))
  18125. ((org-at-table-p)
  18126. (org-table-justify-field-maybe)
  18127. (call-interactively 'org-table-next-row))
  18128. ;; when `newline-and-indent' is called within a list, make sure
  18129. ;; text moved stays inside the item.
  18130. ((and (org-in-item-p) indent)
  18131. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18132. (progn
  18133. (save-match-data (newline))
  18134. (org-indent-line-to (length (match-string 0))))
  18135. (let ((ind (org-get-indentation)))
  18136. (newline)
  18137. (if (org-looking-back org-list-end-re)
  18138. (org-indent-line)
  18139. (org-indent-line-to ind)))))
  18140. ((and org-return-follows-link
  18141. (org-at-timestamp-p t)
  18142. (not (eq org-ts-what 'after)))
  18143. (org-follow-timestamp-link))
  18144. ((and org-return-follows-link
  18145. (let ((tprop (get-text-property (point) 'face)))
  18146. (or (eq tprop 'org-link)
  18147. (and (listp tprop) (memq 'org-link tprop)))))
  18148. (call-interactively 'org-open-at-point))
  18149. ((and (org-at-heading-p)
  18150. (looking-at
  18151. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18152. (org-show-entry)
  18153. (end-of-line 1)
  18154. (newline))
  18155. (t (if indent (newline-and-indent) (newline))))))
  18156. (defun org-return-indent ()
  18157. "Goto next table row or insert a newline and indent.
  18158. Calls `org-table-next-row' or `newline-and-indent', depending on
  18159. context. See the individual commands for more information."
  18160. (interactive)
  18161. (org-return t))
  18162. (defun org-ctrl-c-star ()
  18163. "Compute table, or change heading status of lines.
  18164. Calls `org-table-recalculate' or `org-toggle-heading',
  18165. depending on context."
  18166. (interactive)
  18167. (cond
  18168. ((org-at-table-p)
  18169. (call-interactively 'org-table-recalculate))
  18170. (t
  18171. ;; Convert all lines in region to list items
  18172. (call-interactively 'org-toggle-heading))))
  18173. (defun org-ctrl-c-minus ()
  18174. "Insert separator line in table or modify bullet status of line.
  18175. Also turns a plain line or a region of lines into list items.
  18176. Calls `org-table-insert-hline', `org-toggle-item', or
  18177. `org-cycle-list-bullet', depending on context."
  18178. (interactive)
  18179. (cond
  18180. ((org-at-table-p)
  18181. (call-interactively 'org-table-insert-hline))
  18182. ((org-region-active-p)
  18183. (call-interactively 'org-toggle-item))
  18184. ((org-in-item-p)
  18185. (call-interactively 'org-cycle-list-bullet))
  18186. (t
  18187. (call-interactively 'org-toggle-item))))
  18188. (defun org-toggle-item (arg)
  18189. "Convert headings or normal lines to items, items to normal lines.
  18190. If there is no active region, only the current line is considered.
  18191. If the first non blank line in the region is an headline, convert
  18192. all headlines to items, shifting text accordingly.
  18193. If it is an item, convert all items to normal lines.
  18194. If it is normal text, change region into an item. With a prefix
  18195. argument ARG, change each line in region into an item."
  18196. (interactive "P")
  18197. (let ((shift-text
  18198. (function
  18199. ;; Shift text in current section to IND, from point to END.
  18200. ;; The function leaves point to END line.
  18201. (lambda (ind end)
  18202. (let ((min-i 1000) (end (copy-marker end)))
  18203. ;; First determine the minimum indentation (MIN-I) of
  18204. ;; the text.
  18205. (save-excursion
  18206. (catch 'exit
  18207. (while (< (point) end)
  18208. (let ((i (org-get-indentation)))
  18209. (cond
  18210. ;; Skip blank lines and inline tasks.
  18211. ((looking-at "^[ \t]*$"))
  18212. ((looking-at org-outline-regexp-bol))
  18213. ;; We can't find less than 0 indentation.
  18214. ((zerop i) (throw 'exit (setq min-i 0)))
  18215. ((< i min-i) (setq min-i i))))
  18216. (forward-line))))
  18217. ;; Then indent each line so that a line indented to
  18218. ;; MIN-I becomes indented to IND. Ignore blank lines
  18219. ;; and inline tasks in the process.
  18220. (let ((delta (- ind min-i)))
  18221. (while (< (point) end)
  18222. (unless (or (looking-at "^[ \t]*$")
  18223. (looking-at org-outline-regexp-bol))
  18224. (org-indent-line-to (+ (org-get-indentation) delta)))
  18225. (forward-line)))))))
  18226. (skip-blanks
  18227. (function
  18228. ;; Return beginning of first non-blank line, starting from
  18229. ;; line at POS.
  18230. (lambda (pos)
  18231. (save-excursion
  18232. (goto-char pos)
  18233. (skip-chars-forward " \r\t\n")
  18234. (point-at-bol)))))
  18235. beg end)
  18236. ;; Determine boundaries of changes.
  18237. (if (org-region-active-p)
  18238. (setq beg (funcall skip-blanks (region-beginning))
  18239. end (copy-marker (region-end)))
  18240. (setq beg (funcall skip-blanks (point-at-bol))
  18241. end (copy-marker (point-at-eol))))
  18242. ;; Depending on the starting line, choose an action on the text
  18243. ;; between BEG and END.
  18244. (org-with-limited-levels
  18245. (save-excursion
  18246. (goto-char beg)
  18247. (cond
  18248. ;; Case 1. Start at an item: de-itemize. Note that it only
  18249. ;; happens when a region is active: `org-ctrl-c-minus'
  18250. ;; would call `org-cycle-list-bullet' otherwise.
  18251. ((org-at-item-p)
  18252. (while (< (point) end)
  18253. (when (org-at-item-p)
  18254. (skip-chars-forward " \t")
  18255. (delete-region (point) (match-end 0)))
  18256. (forward-line)))
  18257. ;; Case 2. Start at an heading: convert to items.
  18258. ((org-at-heading-p)
  18259. (let* ((bul (org-list-bullet-string "-"))
  18260. (bul-len (length bul))
  18261. ;; Indentation of the first heading. It should be
  18262. ;; relative to the indentation of its parent, if any.
  18263. (start-ind (save-excursion
  18264. (cond
  18265. ((not org-adapt-indentation) 0)
  18266. ((not (outline-previous-heading)) 0)
  18267. (t (length (match-string 0))))))
  18268. ;; Level of first heading. Further headings will be
  18269. ;; compared to it to determine hierarchy in the list.
  18270. (ref-level (org-reduced-level (org-outline-level))))
  18271. (while (< (point) end)
  18272. (let* ((level (org-reduced-level (org-outline-level)))
  18273. (delta (max 0 (- level ref-level))))
  18274. ;; If current headline is less indented than the first
  18275. ;; one, set it as reference, in order to preserve
  18276. ;; subtrees.
  18277. (when (< level ref-level) (setq ref-level level))
  18278. (replace-match bul t t)
  18279. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18280. ;; Ensure all text down to END (or SECTION-END) belongs
  18281. ;; to the newly created item.
  18282. (let ((section-end (save-excursion
  18283. (or (outline-next-heading) (point)))))
  18284. (forward-line)
  18285. (funcall shift-text
  18286. (+ start-ind (* (1+ delta) bul-len))
  18287. (min end section-end)))))))
  18288. ;; Case 3. Normal line with ARG: turn each non-item line into
  18289. ;; an item.
  18290. (arg
  18291. (while (< (point) end)
  18292. (unless (or (org-at-heading-p) (org-at-item-p))
  18293. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18294. (replace-match
  18295. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18296. (forward-line)))
  18297. ;; Case 4. Normal line without ARG: make the first line of
  18298. ;; region an item, and shift indentation of others
  18299. ;; lines to set them as item's body.
  18300. (t (let* ((bul (org-list-bullet-string "-"))
  18301. (bul-len (length bul))
  18302. (ref-ind (org-get-indentation)))
  18303. (skip-chars-forward " \t")
  18304. (insert bul)
  18305. (forward-line)
  18306. (while (< (point) end)
  18307. ;; Ensure that lines less indented than first one
  18308. ;; still get included in item body.
  18309. (funcall shift-text
  18310. (+ ref-ind bul-len)
  18311. (min end (save-excursion (or (outline-next-heading)
  18312. (point)))))
  18313. (forward-line)))))))))
  18314. (defun org-toggle-heading (&optional nstars)
  18315. "Convert headings to normal text, or items or text to headings.
  18316. If there is no active region, only the current line is considered.
  18317. With a \\[universal-argument] prefix, convert the whole list at
  18318. point into heading.
  18319. In a region:
  18320. - If the first non blank line is an headline, remove the stars
  18321. from all headlines in the region.
  18322. - If it is a normal line turn each and every normal line (i.e. not an
  18323. heading or an item) in the region into a heading.
  18324. - If it is a plain list item, turn all plain list items into headings.
  18325. When converting a line into a heading, the number of stars is chosen
  18326. such that the lines become children of the current entry. However,
  18327. when a prefix argument is given, its value determines the number of
  18328. stars to add."
  18329. (interactive "P")
  18330. (let ((skip-blanks
  18331. (function
  18332. ;; Return beginning of first non-blank line, starting from
  18333. ;; line at POS.
  18334. (lambda (pos)
  18335. (save-excursion
  18336. (goto-char pos)
  18337. (while (org-at-comment-p) (forward-line))
  18338. (skip-chars-forward " \r\t\n")
  18339. (point-at-bol)))))
  18340. beg end toggled)
  18341. ;; Determine boundaries of changes. If a universal prefix has
  18342. ;; been given, put the list in a region. If region ends at a bol,
  18343. ;; do not consider the last line to be in the region.
  18344. (when (and current-prefix-arg (org-at-item-p))
  18345. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18346. (org-mark-element))
  18347. (if (org-region-active-p)
  18348. (setq beg (funcall skip-blanks (region-beginning))
  18349. end (copy-marker (save-excursion
  18350. (goto-char (region-end))
  18351. (if (bolp) (point) (point-at-eol)))))
  18352. (setq beg (funcall skip-blanks (point-at-bol))
  18353. end (copy-marker (point-at-eol))))
  18354. ;; Ensure inline tasks don't count as headings.
  18355. (org-with-limited-levels
  18356. (save-excursion
  18357. (goto-char beg)
  18358. (cond
  18359. ;; Case 1. Started at an heading: de-star headings.
  18360. ((org-at-heading-p)
  18361. (while (< (point) end)
  18362. (when (org-at-heading-p t)
  18363. (looking-at org-outline-regexp) (replace-match "")
  18364. (setq toggled t))
  18365. (forward-line)))
  18366. ;; Case 2. Started at an item: change items into headlines.
  18367. ;; One star will be added by `org-list-to-subtree'.
  18368. ((org-at-item-p)
  18369. (let* ((stars (make-string
  18370. (if nstars
  18371. ;; subtract the star that will be added again by
  18372. ;; `org-list-to-subtree'
  18373. (1- (prefix-numeric-value current-prefix-arg))
  18374. (or (org-current-level) 0))
  18375. ?*))
  18376. (add-stars
  18377. (cond (nstars "") ; stars from prefix only
  18378. ((equal stars "") "") ; before first heading
  18379. (org-odd-levels-only "*") ; inside heading, odd
  18380. (t "")))) ; inside heading, oddeven
  18381. (while (< (point) end)
  18382. (when (org-at-item-p)
  18383. ;; Pay attention to cases when region ends before list.
  18384. (let* ((struct (org-list-struct))
  18385. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18386. (save-restriction
  18387. (narrow-to-region (point) list-end)
  18388. (insert
  18389. (org-list-to-subtree
  18390. (org-list-parse-list t)
  18391. '(:istart (concat stars add-stars (funcall get-stars depth))
  18392. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18393. (setq toggled t))
  18394. (forward-line))))
  18395. ;; Case 3. Started at normal text: make every line an heading,
  18396. ;; skipping headlines and items.
  18397. (t (let* ((stars (make-string
  18398. (if nstars
  18399. (prefix-numeric-value current-prefix-arg)
  18400. (or (org-current-level) 0))
  18401. ?*))
  18402. (add-stars
  18403. (cond (nstars "") ; stars from prefix only
  18404. ((equal stars "") "*") ; before first heading
  18405. (org-odd-levels-only "**") ; inside heading, odd
  18406. (t "*"))) ; inside heading, oddeven
  18407. (rpl (concat stars add-stars " ")))
  18408. (while (< (point) end)
  18409. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18410. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18411. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18412. (forward-line)))))))
  18413. (unless toggled (message "Cannot toggle heading from here"))))
  18414. (defun org-meta-return (&optional arg)
  18415. "Insert a new heading or wrap a region in a table.
  18416. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18417. See the individual commands for more information."
  18418. (interactive "P")
  18419. (cond
  18420. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18421. ((or (org-at-drawer-p) (org-at-property-p))
  18422. (newline-and-indent))
  18423. ((org-at-table-p)
  18424. (call-interactively 'org-table-wrap-region))
  18425. (t (call-interactively 'org-insert-heading))))
  18426. ;;; Menu entries
  18427. (defsubst org-in-subtree-not-table-p ()
  18428. "Are we in a subtree and not in a table?"
  18429. (and (not (org-before-first-heading-p))
  18430. (not (org-at-table-p))))
  18431. ;; Define the Org-mode menus
  18432. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18433. '("Tbl"
  18434. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18435. ["Next Field" org-cycle (org-at-table-p)]
  18436. ["Previous Field" org-shifttab (org-at-table-p)]
  18437. ["Next Row" org-return (org-at-table-p)]
  18438. "--"
  18439. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18440. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18441. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18442. "--"
  18443. ("Column"
  18444. ["Move Column Left" org-metaleft (org-at-table-p)]
  18445. ["Move Column Right" org-metaright (org-at-table-p)]
  18446. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18447. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18448. ("Row"
  18449. ["Move Row Up" org-metaup (org-at-table-p)]
  18450. ["Move Row Down" org-metadown (org-at-table-p)]
  18451. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18452. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18453. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18454. "--"
  18455. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18456. ("Rectangle"
  18457. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18458. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18459. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18460. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18461. "--"
  18462. ("Calculate"
  18463. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18464. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18465. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18466. "--"
  18467. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18468. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18469. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18470. "--"
  18471. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18472. "--"
  18473. ["Sum Column/Rectangle" org-table-sum
  18474. (or (org-at-table-p) (org-region-active-p))]
  18475. ["Which Column?" org-table-current-column (org-at-table-p)])
  18476. ["Debug Formulas"
  18477. org-table-toggle-formula-debugger
  18478. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18479. ["Show Col/Row Numbers"
  18480. org-table-toggle-coordinate-overlays
  18481. :style toggle
  18482. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18483. "--"
  18484. ["Create" org-table-create (and (not (org-at-table-p))
  18485. org-enable-table-editor)]
  18486. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18487. ["Import from File" org-table-import (not (org-at-table-p))]
  18488. ["Export to File" org-table-export (org-at-table-p)]
  18489. "--"
  18490. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18491. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18492. '("Org"
  18493. ("Show/Hide"
  18494. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18495. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18496. ["Sparse Tree..." org-sparse-tree t]
  18497. ["Reveal Context" org-reveal t]
  18498. ["Show All" show-all t]
  18499. "--"
  18500. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18501. "--"
  18502. ["New Heading" org-insert-heading t]
  18503. ("Navigate Headings"
  18504. ["Up" outline-up-heading t]
  18505. ["Next" outline-next-visible-heading t]
  18506. ["Previous" outline-previous-visible-heading t]
  18507. ["Next Same Level" outline-forward-same-level t]
  18508. ["Previous Same Level" outline-backward-same-level t]
  18509. "--"
  18510. ["Jump" org-goto t])
  18511. ("Edit Structure"
  18512. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18513. "--"
  18514. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18515. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18516. "--"
  18517. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18518. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18519. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18520. "--"
  18521. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18522. "--"
  18523. ["Copy visible text" org-copy-visible t]
  18524. "--"
  18525. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18526. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18527. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18528. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18529. "--"
  18530. ["Sort Region/Children" org-sort t]
  18531. "--"
  18532. ["Convert to odd levels" org-convert-to-odd-levels t]
  18533. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18534. ("Editing"
  18535. ["Emphasis..." org-emphasize t]
  18536. ["Edit Source Example" org-edit-special t]
  18537. "--"
  18538. ["Footnote new/jump" org-footnote-action t]
  18539. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18540. ("Archive"
  18541. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18542. "--"
  18543. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18544. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18545. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18546. )
  18547. "--"
  18548. ("Hyperlinks"
  18549. ["Store Link (Global)" org-store-link t]
  18550. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18551. ["Insert Link" org-insert-link t]
  18552. ["Follow Link" org-open-at-point t]
  18553. "--"
  18554. ["Next link" org-next-link t]
  18555. ["Previous link" org-previous-link t]
  18556. "--"
  18557. ["Descriptive Links"
  18558. org-toggle-link-display
  18559. :style radio
  18560. :selected org-descriptive-links
  18561. ]
  18562. ["Literal Links"
  18563. org-toggle-link-display
  18564. :style radio
  18565. :selected (not org-descriptive-links)])
  18566. "--"
  18567. ("TODO Lists"
  18568. ["TODO/DONE/-" org-todo t]
  18569. ("Select keyword"
  18570. ["Next keyword" org-shiftright (org-at-heading-p)]
  18571. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18572. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18573. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18574. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18575. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18576. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18577. "--"
  18578. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18579. :selected org-enforce-todo-dependencies :style toggle :active t]
  18580. "Settings for tree at point"
  18581. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18582. :selected (org-entry-get nil "ORDERED")
  18583. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18584. ["Do Children parallel" org-toggle-ordered-property :style radio
  18585. :selected (not (org-entry-get nil "ORDERED"))
  18586. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18587. "--"
  18588. ["Set Priority" org-priority t]
  18589. ["Priority Up" org-shiftup t]
  18590. ["Priority Down" org-shiftdown t]
  18591. "--"
  18592. ["Get news from all feeds" org-feed-update-all t]
  18593. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18594. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18595. ("TAGS and Properties"
  18596. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18597. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18598. "--"
  18599. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18600. ["Column view of properties" org-columns t]
  18601. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18602. ("Dates and Scheduling"
  18603. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18604. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18605. ("Change Date"
  18606. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18607. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18608. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18609. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18610. ["Compute Time Range" org-evaluate-time-range t]
  18611. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18612. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18613. "--"
  18614. ["Custom time format" org-toggle-time-stamp-overlays
  18615. :style radio :selected org-display-custom-times]
  18616. "--"
  18617. ["Goto Calendar" org-goto-calendar t]
  18618. ["Date from Calendar" org-date-from-calendar t]
  18619. "--"
  18620. ["Start/Restart Timer" org-timer-start t]
  18621. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18622. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18623. ["Insert Timer String" org-timer t]
  18624. ["Insert Timer Item" org-timer-item t])
  18625. ("Logging work"
  18626. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18627. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18628. ["Clock out" org-clock-out t]
  18629. ["Clock cancel" org-clock-cancel t]
  18630. "--"
  18631. ["Mark as default task" org-clock-mark-default-task t]
  18632. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18633. ["Goto running clock" org-clock-goto t]
  18634. "--"
  18635. ["Display times" org-clock-display t]
  18636. ["Create clock table" org-clock-report t]
  18637. "--"
  18638. ["Record DONE time"
  18639. (progn (setq org-log-done (not org-log-done))
  18640. (message "Switching to %s will %s record a timestamp"
  18641. (car org-done-keywords)
  18642. (if org-log-done "automatically" "not")))
  18643. :style toggle :selected org-log-done])
  18644. "--"
  18645. ["Agenda Command..." org-agenda t]
  18646. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18647. ("File List for Agenda")
  18648. ("Special views current file"
  18649. ["TODO Tree" org-show-todo-tree t]
  18650. ["Check Deadlines" org-check-deadlines t]
  18651. ["Timeline" org-timeline t]
  18652. ["Tags/Property tree" org-match-sparse-tree t])
  18653. "--"
  18654. ["Export/Publish..." org-export t]
  18655. ("LaTeX"
  18656. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18657. :selected org-cdlatex-mode]
  18658. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18659. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18660. ["Modify math symbol" org-cdlatex-math-modify
  18661. (org-inside-LaTeX-fragment-p)]
  18662. ["Insert citation" org-reftex-citation t]
  18663. "--"
  18664. ["Template for BEAMER" (progn (require 'org-beamer)
  18665. (org-insert-beamer-options-template)) t])
  18666. "--"
  18667. ("MobileOrg"
  18668. ["Push Files and Views" org-mobile-push t]
  18669. ["Get Captured and Flagged" org-mobile-pull t]
  18670. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18671. "--"
  18672. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18673. "--"
  18674. ("Documentation"
  18675. ["Show Version" org-version t]
  18676. ["Info Documentation" org-info t])
  18677. ("Customize"
  18678. ["Browse Org Group" org-customize t]
  18679. "--"
  18680. ["Expand This Menu" org-create-customize-menu
  18681. (fboundp 'customize-menu-create)])
  18682. ["Send bug report" org-submit-bug-report t]
  18683. "--"
  18684. ("Refresh/Reload"
  18685. ["Refresh setup current buffer" org-mode-restart t]
  18686. ["Reload Org (after update)" org-reload t]
  18687. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18688. ))
  18689. (defun org-info (&optional node)
  18690. "Read documentation for Org-mode in the info system.
  18691. With optional NODE, go directly to that node."
  18692. (interactive)
  18693. (info (format "(org)%s" (or node ""))))
  18694. ;;;###autoload
  18695. (defun org-submit-bug-report ()
  18696. "Submit a bug report on Org-mode via mail.
  18697. Don't hesitate to report any problems or inaccurate documentation.
  18698. If you don't have setup sending mail from (X)Emacs, please copy the
  18699. output buffer into your mail program, as it gives us important
  18700. information about your Org-mode version and configuration."
  18701. (interactive)
  18702. (require 'reporter)
  18703. (org-load-modules-maybe)
  18704. (org-require-autoloaded-modules)
  18705. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18706. (reporter-submit-bug-report
  18707. "emacs-orgmode@gnu.org"
  18708. (org-version nil 'full)
  18709. (let (list)
  18710. (save-window-excursion
  18711. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18712. (delete-other-windows)
  18713. (erase-buffer)
  18714. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18715. We would like to add your full Org-mode and Outline configuration to the
  18716. bug report. This greatly simplifies the work of the maintainer and
  18717. other experts on the mailing list.
  18718. HOWEVER, some variables you have customized may contain private
  18719. information. The names of customers, colleagues, or friends, might
  18720. appear in the form of file names, tags, todo states, or search strings.
  18721. If you answer yes to the prompt, you might want to check and remove
  18722. such private information before sending the email.")
  18723. (add-text-properties (point-min) (point-max) '(face org-warning))
  18724. (when (yes-or-no-p "Include your Org-mode configuration ")
  18725. (mapatoms
  18726. (lambda (v)
  18727. (and (boundp v)
  18728. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18729. (or (and (symbol-value v)
  18730. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18731. (and
  18732. (get v 'custom-type) (get v 'standard-value)
  18733. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18734. (push v list)))))
  18735. (kill-buffer (get-buffer "*Warn about privacy*"))
  18736. list))
  18737. nil nil
  18738. "Remember to cover the basics, that is, what you expected to happen and
  18739. what in fact did happen. You don't know how to make a good report? See
  18740. http://orgmode.org/manual/Feedback.html#Feedback
  18741. Your bug report will be posted to the Org-mode mailing list.
  18742. ------------------------------------------------------------------------")
  18743. (save-excursion
  18744. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18745. (replace-match "\\1Bug: \\3 [\\2]")))))
  18746. (defun org-install-agenda-files-menu ()
  18747. (let ((bl (buffer-list)))
  18748. (save-excursion
  18749. (while bl
  18750. (set-buffer (pop bl))
  18751. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18752. (when (derived-mode-p 'org-mode)
  18753. (easy-menu-change
  18754. '("Org") "File List for Agenda"
  18755. (append
  18756. (list
  18757. ["Edit File List" (org-edit-agenda-file-list) t]
  18758. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18759. ["Remove Current File from List" org-remove-file t]
  18760. ["Cycle through agenda files" org-cycle-agenda-files t]
  18761. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18762. "--")
  18763. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18764. ;;;; Documentation
  18765. (defun org-require-autoloaded-modules ()
  18766. (interactive)
  18767. (mapc 'require
  18768. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18769. org-docbook org-exp org-html org-icalendar
  18770. org-id org-latex
  18771. org-publish org-remember org-table
  18772. org-timer org-xoxo)))
  18773. ;;;###autoload
  18774. (defun org-reload (&optional uncompiled)
  18775. "Reload all org lisp files.
  18776. With prefix arg UNCOMPILED, load the uncompiled versions."
  18777. (interactive "P")
  18778. (require 'loadhist)
  18779. (let* ((org-dir (org-find-library-dir "org"))
  18780. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18781. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18782. (remove-re (mapconcat 'identity
  18783. (mapcar (lambda (f) (concat "^" f "$"))
  18784. (list (if (featurep 'xemacs)
  18785. "org-colview"
  18786. "org-colview-xemacs")
  18787. "org" "org-loaddefs" "org-version"))
  18788. "\\|"))
  18789. (feats (delete-dups
  18790. (mapcar 'file-name-sans-extension
  18791. (mapcar 'file-name-nondirectory
  18792. (delq nil
  18793. (mapcar 'feature-file
  18794. features))))))
  18795. (lfeat (append
  18796. (sort
  18797. (setq feats
  18798. (delq nil (mapcar
  18799. (lambda (f)
  18800. (if (and (string-match feature-re f)
  18801. (not (string-match remove-re f)))
  18802. f nil))
  18803. feats)))
  18804. 'string-lessp)
  18805. (list "org-version" "org")))
  18806. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18807. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18808. load-uncore load-misses)
  18809. (setq load-misses
  18810. (delq 't
  18811. (mapcar (lambda (f)
  18812. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18813. (and (string= org-dir contrib-dir)
  18814. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18815. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18816. (add-to-list 'load-uncore f 'append)
  18817. 't)
  18818. f))
  18819. lfeat)))
  18820. (if load-uncore
  18821. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18822. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18823. (if load-misses
  18824. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18825. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18826. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18827. ;;;###autoload
  18828. (defun org-customize ()
  18829. "Call the customize function with org as argument."
  18830. (interactive)
  18831. (org-load-modules-maybe)
  18832. (org-require-autoloaded-modules)
  18833. (customize-browse 'org))
  18834. (defun org-create-customize-menu ()
  18835. "Create a full customization menu for Org-mode, insert it into the menu."
  18836. (interactive)
  18837. (org-load-modules-maybe)
  18838. (org-require-autoloaded-modules)
  18839. (if (fboundp 'customize-menu-create)
  18840. (progn
  18841. (easy-menu-change
  18842. '("Org") "Customize"
  18843. `(["Browse Org group" org-customize t]
  18844. "--"
  18845. ,(customize-menu-create 'org)
  18846. ["Set" Custom-set t]
  18847. ["Save" Custom-save t]
  18848. ["Reset to Current" Custom-reset-current t]
  18849. ["Reset to Saved" Custom-reset-saved t]
  18850. ["Reset to Standard Settings" Custom-reset-standard t]))
  18851. (message "\"Org\"-menu now contains full customization menu"))
  18852. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18853. ;;;; Miscellaneous stuff
  18854. ;;; Generally useful functions
  18855. (defun org-get-at-bol (property)
  18856. "Get text property PROPERTY at beginning of line."
  18857. (get-text-property (point-at-bol) property))
  18858. (defun org-find-text-property-in-string (prop s)
  18859. "Return the first non-nil value of property PROP in string S."
  18860. (or (get-text-property 0 prop s)
  18861. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18862. prop s)))
  18863. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18864. "Display the given MESSAGE as a warning."
  18865. (if (fboundp 'display-warning)
  18866. (display-warning 'org message
  18867. (if (featurep 'xemacs) 'warning :warning))
  18868. (let ((buf (get-buffer-create "*Org warnings*")))
  18869. (with-current-buffer buf
  18870. (goto-char (point-max))
  18871. (insert "Warning (Org): " message)
  18872. (unless (bolp)
  18873. (newline)))
  18874. (display-buffer buf)
  18875. (sit-for 0))))
  18876. (defun org-eval (form)
  18877. "Eval FORM and return result."
  18878. (condition-case error
  18879. (eval form)
  18880. (error (format "%%![Error: %s]" error))))
  18881. (defun org-in-clocktable-p ()
  18882. "Check if the cursor is in a clocktable."
  18883. (let ((pos (point)) start)
  18884. (save-excursion
  18885. (end-of-line 1)
  18886. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18887. (setq start (match-beginning 0))
  18888. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18889. (>= (match-end 0) pos)
  18890. start))))
  18891. (defun org-in-commented-line ()
  18892. "Is point in a line starting with `#'?"
  18893. (equal (char-after (point-at-bol)) ?#))
  18894. (defun org-in-indented-comment-line ()
  18895. "Is point in a line starting with `#' after some white space?"
  18896. (save-excursion
  18897. (save-match-data
  18898. (goto-char (point-at-bol))
  18899. (looking-at "[ \t]*#"))))
  18900. (defun org-in-verbatim-emphasis ()
  18901. (save-match-data
  18902. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18903. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18904. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18905. (if (and marker (marker-buffer marker)
  18906. (buffer-live-p (marker-buffer marker)))
  18907. (progn
  18908. (org-pop-to-buffer-same-window (marker-buffer marker))
  18909. (if (or (> marker (point-max)) (< marker (point-min)))
  18910. (widen))
  18911. (goto-char marker)
  18912. (org-show-context 'org-goto))
  18913. (if bookmark
  18914. (bookmark-jump bookmark)
  18915. (error "Cannot find location"))))
  18916. (defun org-quote-csv-field (s)
  18917. "Quote field for inclusion in CSV material."
  18918. (if (string-match "[\",]" s)
  18919. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18920. s))
  18921. (defun org-force-self-insert (N)
  18922. "Needed to enforce self-insert under remapping."
  18923. (interactive "p")
  18924. (self-insert-command N))
  18925. (defun org-string-width (s)
  18926. "Compute width of string, ignoring invisible characters.
  18927. This ignores character with invisibility property `org-link', and also
  18928. characters with property `org-cwidth', because these will become invisible
  18929. upon the next fontification round."
  18930. (let (b l)
  18931. (when (or (eq t buffer-invisibility-spec)
  18932. (assq 'org-link buffer-invisibility-spec))
  18933. (while (setq b (text-property-any 0 (length s)
  18934. 'invisible 'org-link s))
  18935. (setq s (concat (substring s 0 b)
  18936. (substring s (or (next-single-property-change
  18937. b 'invisible s) (length s)))))))
  18938. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18939. (setq s (concat (substring s 0 b)
  18940. (substring s (or (next-single-property-change
  18941. b 'org-cwidth s) (length s))))))
  18942. (setq l (string-width s) b -1)
  18943. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18944. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18945. l))
  18946. (defun org-shorten-string (s maxlength)
  18947. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18948. If the string is shorter or has length MAXLENGTH, just return the
  18949. original string. If it is longer, the functions finds a space in the
  18950. string, breaks this string off at that locations and adds three dots
  18951. as ellipsis. Including the ellipsis, the string will not be longer
  18952. than MAXLENGTH. If finding a good breaking point in the string does
  18953. not work, the string is just chopped off in the middle of a word
  18954. if necessary."
  18955. (if (<= (length s) maxlength)
  18956. s
  18957. (let* ((n (max (- maxlength 4) 1))
  18958. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18959. (if (string-match re s)
  18960. (concat (match-string 1 s) "...")
  18961. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18962. (defun org-get-indentation (&optional line)
  18963. "Get the indentation of the current line, interpreting tabs.
  18964. When LINE is given, assume it represents a line and compute its indentation."
  18965. (if line
  18966. (if (string-match "^ *" (org-remove-tabs line))
  18967. (match-end 0))
  18968. (save-excursion
  18969. (beginning-of-line 1)
  18970. (skip-chars-forward " \t")
  18971. (current-column))))
  18972. (defun org-get-string-indentation (s)
  18973. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18974. (let ((n -1) (i 0) (w tab-width) c)
  18975. (catch 'exit
  18976. (while (< (setq n (1+ n)) (length s))
  18977. (setq c (aref s n))
  18978. (cond ((= c ?\ ) (setq i (1+ i)))
  18979. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18980. (t (throw 'exit t)))))
  18981. i))
  18982. (defun org-remove-tabs (s &optional width)
  18983. "Replace tabulators in S with spaces.
  18984. Assumes that s is a single line, starting in column 0."
  18985. (setq width (or width tab-width))
  18986. (while (string-match "\t" s)
  18987. (setq s (replace-match
  18988. (make-string
  18989. (- (* width (/ (+ (match-beginning 0) width) width))
  18990. (match-beginning 0)) ?\ )
  18991. t t s)))
  18992. s)
  18993. (defun org-fix-indentation (line ind)
  18994. "Fix indentation in LINE.
  18995. IND is a cons cell with target and minimum indentation.
  18996. If the current indentation in LINE is smaller than the minimum,
  18997. leave it alone. If it is larger than ind, set it to the target."
  18998. (let* ((l (org-remove-tabs line))
  18999. (i (org-get-indentation l))
  19000. (i1 (car ind)) (i2 (cdr ind)))
  19001. (if (>= i i2) (setq l (substring line i2)))
  19002. (if (> i1 0)
  19003. (concat (make-string i1 ?\ ) l)
  19004. l)))
  19005. (defun org-remove-indentation (code &optional n)
  19006. "Remove the maximum common indentation from the lines in CODE.
  19007. N may optionally be the number of spaces to remove."
  19008. (with-temp-buffer
  19009. (insert code)
  19010. (org-do-remove-indentation n)
  19011. (buffer-string)))
  19012. (defun org-do-remove-indentation (&optional n)
  19013. "Remove the maximum common indentation from the buffer."
  19014. (untabify (point-min) (point-max))
  19015. (let ((min 10000) re)
  19016. (if n
  19017. (setq min n)
  19018. (goto-char (point-min))
  19019. (while (re-search-forward "^ *[^ \n]" nil t)
  19020. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19021. (unless (or (= min 0) (= min 10000))
  19022. (setq re (format "^ \\{%d\\}" min))
  19023. (goto-char (point-min))
  19024. (while (re-search-forward re nil t)
  19025. (replace-match "")
  19026. (end-of-line 1))
  19027. min)))
  19028. (defun org-fill-template (template alist)
  19029. "Find each %key of ALIST in TEMPLATE and replace it."
  19030. (let ((case-fold-search nil)
  19031. entry key value)
  19032. (setq alist (sort (copy-sequence alist)
  19033. (lambda (a b) (< (length (car a)) (length (car b))))))
  19034. (while (setq entry (pop alist))
  19035. (setq template
  19036. (replace-regexp-in-string
  19037. (concat "%" (regexp-quote (car entry)))
  19038. (or (cdr entry) "") template t t)))
  19039. template))
  19040. (defun org-base-buffer (buffer)
  19041. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19042. (if (not buffer)
  19043. buffer
  19044. (or (buffer-base-buffer buffer)
  19045. buffer)))
  19046. (defun org-trim (s)
  19047. "Remove whitespace at beginning and end of string."
  19048. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19049. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19050. s)
  19051. (defun org-wrap (string &optional width lines)
  19052. "Wrap string to either a number of lines, or a width in characters.
  19053. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19054. that costs. If there is a word longer than WIDTH, the text is actually
  19055. wrapped to the length of that word.
  19056. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19057. many lines, whatever width that takes.
  19058. The return value is a list of lines, without newlines at the end."
  19059. (let* ((words (org-split-string string "[ \t\n]+"))
  19060. (maxword (apply 'max (mapcar 'org-string-width words)))
  19061. w ll)
  19062. (cond (width
  19063. (org-do-wrap words (max maxword width)))
  19064. (lines
  19065. (setq w maxword)
  19066. (setq ll (org-do-wrap words maxword))
  19067. (if (<= (length ll) lines)
  19068. ll
  19069. (setq ll words)
  19070. (while (> (length ll) lines)
  19071. (setq w (1+ w))
  19072. (setq ll (org-do-wrap words w)))
  19073. ll))
  19074. (t (error "Cannot wrap this")))))
  19075. (defun org-do-wrap (words width)
  19076. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19077. (let (lines line)
  19078. (while words
  19079. (setq line (pop words))
  19080. (while (and words (< (+ (length line) (length (car words))) width))
  19081. (setq line (concat line " " (pop words))))
  19082. (setq lines (push line lines)))
  19083. (nreverse lines)))
  19084. (defun org-split-string (string &optional separators)
  19085. "Splits STRING into substrings at SEPARATORS.
  19086. No empty strings are returned if there are matches at the beginning
  19087. and end of string."
  19088. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19089. (start 0)
  19090. notfirst
  19091. (list nil))
  19092. (while (and (string-match rexp string
  19093. (if (and notfirst
  19094. (= start (match-beginning 0))
  19095. (< start (length string)))
  19096. (1+ start) start))
  19097. (< (match-beginning 0) (length string)))
  19098. (setq notfirst t)
  19099. (or (eq (match-beginning 0) 0)
  19100. (and (eq (match-beginning 0) (match-end 0))
  19101. (eq (match-beginning 0) start))
  19102. (setq list
  19103. (cons (substring string start (match-beginning 0))
  19104. list)))
  19105. (setq start (match-end 0)))
  19106. (or (eq start (length string))
  19107. (setq list
  19108. (cons (substring string start)
  19109. list)))
  19110. (nreverse list)))
  19111. (defun org-quote-vert (s)
  19112. "Replace \"|\" with \"\\vert\"."
  19113. (while (string-match "|" s)
  19114. (setq s (replace-match "\\vert" t t s)))
  19115. s)
  19116. (defun org-uuidgen-p (s)
  19117. "Is S an ID created by UUIDGEN?"
  19118. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19119. (defun org-in-src-block-p (&optional inside)
  19120. "Whether point is in a code source block.
  19121. When INSIDE is non-nil, don't consider we are within a src block
  19122. when point is at #+BEGIN_SRC or #+END_SRC."
  19123. (let ((case-fold-search t) ov)
  19124. (or (and (setq ov (overlays-at (point)))
  19125. (memq 'org-block-background
  19126. (overlay-properties (car ov))))
  19127. (and (not inside)
  19128. (save-match-data
  19129. (save-excursion
  19130. (beginning-of-line)
  19131. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19132. (defun org-context ()
  19133. "Return a list of contexts of the current cursor position.
  19134. If several contexts apply, all are returned.
  19135. Each context entry is a list with a symbol naming the context, and
  19136. two positions indicating start and end of the context. Possible
  19137. contexts are:
  19138. :headline anywhere in a headline
  19139. :headline-stars on the leading stars in a headline
  19140. :todo-keyword on a TODO keyword (including DONE) in a headline
  19141. :tags on the TAGS in a headline
  19142. :priority on the priority cookie in a headline
  19143. :item on the first line of a plain list item
  19144. :item-bullet on the bullet/number of a plain list item
  19145. :checkbox on the checkbox in a plain list item
  19146. :table in an org-mode table
  19147. :table-special on a special filed in a table
  19148. :table-table in a table.el table
  19149. :clocktable in a clocktable
  19150. :src-block in a source block
  19151. :link on a hyperlink
  19152. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19153. :target on a <<target>>
  19154. :radio-target on a <<<radio-target>>>
  19155. :latex-fragment on a LaTeX fragment
  19156. :latex-preview on a LaTeX fragment with overlaid preview image
  19157. This function expects the position to be visible because it uses font-lock
  19158. faces as a help to recognize the following contexts: :table-special, :link,
  19159. and :keyword."
  19160. (let* ((f (get-text-property (point) 'face))
  19161. (faces (if (listp f) f (list f)))
  19162. (case-fold-search t)
  19163. (p (point)) clist o)
  19164. ;; First the large context
  19165. (cond
  19166. ((org-at-heading-p t)
  19167. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19168. (when (progn
  19169. (beginning-of-line 1)
  19170. (looking-at org-todo-line-tags-regexp))
  19171. (push (org-point-in-group p 1 :headline-stars) clist)
  19172. (push (org-point-in-group p 2 :todo-keyword) clist)
  19173. (push (org-point-in-group p 4 :tags) clist))
  19174. (goto-char p)
  19175. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19176. (if (looking-at "\\[#[A-Z0-9]\\]")
  19177. (push (org-point-in-group p 0 :priority) clist)))
  19178. ((org-at-item-p)
  19179. (push (org-point-in-group p 2 :item-bullet) clist)
  19180. (push (list :item (point-at-bol)
  19181. (save-excursion (org-end-of-item) (point)))
  19182. clist)
  19183. (and (org-at-item-checkbox-p)
  19184. (push (org-point-in-group p 0 :checkbox) clist)))
  19185. ((org-at-table-p)
  19186. (push (list :table (org-table-begin) (org-table-end)) clist)
  19187. (if (memq 'org-formula faces)
  19188. (push (list :table-special
  19189. (previous-single-property-change p 'face)
  19190. (next-single-property-change p 'face)) clist)))
  19191. ((org-at-table-p 'any)
  19192. (push (list :table-table) clist)))
  19193. (goto-char p)
  19194. (let ((case-fold-search t))
  19195. ;; New the "medium" contexts: clocktables, source blocks
  19196. (cond ((org-in-clocktable-p)
  19197. (push (list :clocktable
  19198. (and (or (looking-at "#\\+BEGIN: clocktable")
  19199. (search-backward "#+BEGIN: clocktable" nil t))
  19200. (match-beginning 0))
  19201. (and (re-search-forward "#\\+END:?" nil t)
  19202. (match-end 0))) clist))
  19203. ((org-in-src-block-p)
  19204. (push (list :src-block
  19205. (and (or (looking-at "#\\+BEGIN_SRC")
  19206. (search-backward "#+BEGIN_SRC" nil t))
  19207. (match-beginning 0))
  19208. (and (search-forward "#+END_SRC" nil t)
  19209. (match-beginning 0))) clist))))
  19210. (goto-char p)
  19211. ;; Now the small context
  19212. (cond
  19213. ((org-at-timestamp-p)
  19214. (push (org-point-in-group p 0 :timestamp) clist))
  19215. ((memq 'org-link faces)
  19216. (push (list :link
  19217. (previous-single-property-change p 'face)
  19218. (next-single-property-change p 'face)) clist))
  19219. ((memq 'org-special-keyword faces)
  19220. (push (list :keyword
  19221. (previous-single-property-change p 'face)
  19222. (next-single-property-change p 'face)) clist))
  19223. ((org-at-target-p)
  19224. (push (org-point-in-group p 0 :target) clist)
  19225. (goto-char (1- (match-beginning 0)))
  19226. (if (looking-at org-radio-target-regexp)
  19227. (push (org-point-in-group p 0 :radio-target) clist))
  19228. (goto-char p))
  19229. ((setq o (car (delq nil
  19230. (mapcar
  19231. (lambda (x)
  19232. (if (memq x org-latex-fragment-image-overlays) x))
  19233. (overlays-at (point))))))
  19234. (push (list :latex-fragment
  19235. (overlay-start o) (overlay-end o)) clist)
  19236. (push (list :latex-preview
  19237. (overlay-start o) (overlay-end o)) clist))
  19238. ((org-inside-LaTeX-fragment-p)
  19239. ;; FIXME: positions wrong.
  19240. (push (list :latex-fragment (point) (point)) clist)))
  19241. (setq clist (nreverse (delq nil clist)))
  19242. clist))
  19243. ;; FIXME: Compare with at-regexp-p Do we need both?
  19244. (defun org-in-regexp (re &optional nlines visually)
  19245. "Check if point is inside a match of regexp.
  19246. Normally only the current line is checked, but you can include NLINES extra
  19247. lines both before and after point into the search.
  19248. If VISUALLY is set, require that the cursor is not after the match but
  19249. really on, so that the block visually is on the match."
  19250. (catch 'exit
  19251. (let ((pos (point))
  19252. (eol (point-at-eol (+ 1 (or nlines 0))))
  19253. (inc (if visually 1 0)))
  19254. (save-excursion
  19255. (beginning-of-line (- 1 (or nlines 0)))
  19256. (while (re-search-forward re eol t)
  19257. (if (and (<= (match-beginning 0) pos)
  19258. (>= (+ inc (match-end 0)) pos))
  19259. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19260. (defun org-at-regexp-p (regexp)
  19261. "Is point inside a match of REGEXP in the current line?"
  19262. (catch 'exit
  19263. (save-excursion
  19264. (let ((pos (point)) (end (point-at-eol)))
  19265. (beginning-of-line 1)
  19266. (while (re-search-forward regexp end t)
  19267. (if (and (<= (match-beginning 0) pos)
  19268. (>= (match-end 0) pos))
  19269. (throw 'exit t)))
  19270. nil))))
  19271. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19272. "Non-nil when point is between matches of START-RE and END-RE.
  19273. Also return a non-nil value when point is on one of the matches.
  19274. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19275. buffer positions. Default values are the positions of headlines
  19276. surrounding the point.
  19277. The functions returns a cons cell whose car (resp. cdr) is the
  19278. position before START-RE (resp. after END-RE)."
  19279. (save-match-data
  19280. (let ((pos (point))
  19281. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19282. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19283. beg end)
  19284. (save-excursion
  19285. ;; Point is on a block when on START-RE or if START-RE can be
  19286. ;; found before it...
  19287. (and (or (org-at-regexp-p start-re)
  19288. (re-search-backward start-re limit-up t))
  19289. (setq beg (match-beginning 0))
  19290. ;; ... and END-RE after it...
  19291. (goto-char (match-end 0))
  19292. (re-search-forward end-re limit-down t)
  19293. (> (setq end (match-end 0)) pos)
  19294. ;; ... without another START-RE in-between.
  19295. (goto-char (match-beginning 0))
  19296. (not (re-search-backward start-re (1+ beg) t))
  19297. ;; Return value.
  19298. (cons beg end))))))
  19299. (defun org-in-block-p (names)
  19300. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19301. NAMES is a list of strings containing names of blocks.
  19302. Return first block name matched, or nil. Beware that in case of
  19303. nested blocks, the returned name may not belong to the closest
  19304. block from point."
  19305. (save-match-data
  19306. (catch 'exit
  19307. (let ((case-fold-search t)
  19308. (lim-up (save-excursion (outline-previous-heading)))
  19309. (lim-down (save-excursion (outline-next-heading))))
  19310. (mapc (lambda (name)
  19311. (let ((n (regexp-quote name)))
  19312. (when (org-between-regexps-p
  19313. (concat "^[ \t]*#\\+begin_" n)
  19314. (concat "^[ \t]*#\\+end_" n)
  19315. lim-up lim-down)
  19316. (throw 'exit n))))
  19317. names))
  19318. nil)))
  19319. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19320. "Call `multi-occur' with buffers for all agenda files."
  19321. (interactive "sOrg-files matching: \np")
  19322. (let* ((files (org-agenda-files))
  19323. (tnames (mapcar 'file-truename files))
  19324. (extra org-agenda-text-search-extra-files)
  19325. f)
  19326. (when (eq (car extra) 'agenda-archives)
  19327. (setq extra (cdr extra))
  19328. (setq files (org-add-archive-files files)))
  19329. (while (setq f (pop extra))
  19330. (unless (member (file-truename f) tnames)
  19331. (add-to-list 'files f 'append)
  19332. (add-to-list 'tnames (file-truename f) 'append)))
  19333. (multi-occur
  19334. (mapcar (lambda (x)
  19335. (with-current-buffer
  19336. (or (get-file-buffer x) (find-file-noselect x))
  19337. (widen)
  19338. (current-buffer)))
  19339. files)
  19340. regexp)))
  19341. (if (boundp 'occur-mode-find-occurrence-hook)
  19342. ;; Emacs 23
  19343. (add-hook 'occur-mode-find-occurrence-hook
  19344. (lambda ()
  19345. (when (derived-mode-p 'org-mode)
  19346. (org-reveal))))
  19347. ;; Emacs 22
  19348. (defadvice occur-mode-goto-occurrence
  19349. (after org-occur-reveal activate)
  19350. (and (derived-mode-p 'org-mode) (org-reveal)))
  19351. (defadvice occur-mode-goto-occurrence-other-window
  19352. (after org-occur-reveal activate)
  19353. (and (derived-mode-p 'org-mode) (org-reveal)))
  19354. (defadvice occur-mode-display-occurrence
  19355. (after org-occur-reveal activate)
  19356. (when (derived-mode-p 'org-mode)
  19357. (let ((pos (occur-mode-find-occurrence)))
  19358. (with-current-buffer (marker-buffer pos)
  19359. (save-excursion
  19360. (goto-char pos)
  19361. (org-reveal)))))))
  19362. (defun org-occur-link-in-agenda-files ()
  19363. "Create a link and search for it in the agendas.
  19364. The link is not stored in `org-stored-links', it is just created
  19365. for the search purpose."
  19366. (interactive)
  19367. (let ((link (condition-case nil
  19368. (org-store-link nil)
  19369. (error "Unable to create a link to here"))))
  19370. (org-occur-in-agenda-files (regexp-quote link))))
  19371. (defun org-uniquify (list)
  19372. "Remove duplicate elements from LIST."
  19373. (let (res)
  19374. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19375. res))
  19376. (defun org-delete-all (elts list)
  19377. "Remove all elements in ELTS from LIST."
  19378. (while elts
  19379. (setq list (delete (pop elts) list)))
  19380. list)
  19381. (defun org-count (cl-item cl-seq)
  19382. "Count the number of occurrences of ITEM in SEQ.
  19383. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19384. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19385. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19386. (while (< cl-start cl-end)
  19387. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19388. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19389. (setq cl-start (1+ cl-start)))
  19390. cl-count))
  19391. (defun org-remove-if (predicate seq)
  19392. "Remove everything from SEQ that fulfills PREDICATE."
  19393. (let (res e)
  19394. (while seq
  19395. (setq e (pop seq))
  19396. (if (not (funcall predicate e)) (push e res)))
  19397. (nreverse res)))
  19398. (defun org-remove-if-not (predicate seq)
  19399. "Remove everything from SEQ that does not fulfill PREDICATE."
  19400. (let (res e)
  19401. (while seq
  19402. (setq e (pop seq))
  19403. (if (funcall predicate e) (push e res)))
  19404. (nreverse res)))
  19405. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19406. "Reduce two-argument FUNCTION across SEQ.
  19407. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19408. \":initial-value\" removed."
  19409. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19410. (cadr (memq :initial-value cl-keys)))
  19411. (cl-seq (pop cl-seq))
  19412. (t (funcall cl-func)))))
  19413. (while cl-seq
  19414. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19415. cl-accum))
  19416. (defun org-back-over-empty-lines ()
  19417. "Move backwards over whitespace, to the beginning of the first empty line.
  19418. Returns the number of empty lines passed."
  19419. (let ((pos (point)))
  19420. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19421. (skip-chars-backward " \t\n\r")
  19422. (unless (eobp)
  19423. (forward-line -1)))
  19424. (beginning-of-line 2)
  19425. (goto-char (min (point) pos))
  19426. (count-lines (point) pos)))
  19427. (defun org-skip-whitespace ()
  19428. (skip-chars-forward " \t\n\r"))
  19429. (defun org-point-in-group (point group &optional context)
  19430. "Check if POINT is in match-group GROUP.
  19431. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19432. match. If the match group does not exist or point is not inside it,
  19433. return nil."
  19434. (and (match-beginning group)
  19435. (>= point (match-beginning group))
  19436. (<= point (match-end group))
  19437. (if context
  19438. (list context (match-beginning group) (match-end group))
  19439. t)))
  19440. (defun org-switch-to-buffer-other-window (&rest args)
  19441. "Switch to buffer in a second window on the current frame.
  19442. In particular, do not allow pop-up frames.
  19443. Returns the newly created buffer."
  19444. (org-no-popups
  19445. (apply 'switch-to-buffer-other-window args)))
  19446. (defun org-combine-plists (&rest plists)
  19447. "Create a single property list from all plists in PLISTS.
  19448. The process starts by copying the first list, and then setting properties
  19449. from the other lists. Settings in the last list are the most significant
  19450. ones and overrule settings in the other lists."
  19451. (let ((rtn (copy-sequence (pop plists)))
  19452. p v ls)
  19453. (while plists
  19454. (setq ls (pop plists))
  19455. (while ls
  19456. (setq p (pop ls) v (pop ls))
  19457. (setq rtn (plist-put rtn p v))))
  19458. rtn))
  19459. (defun org-replace-escapes (string table)
  19460. "Replace %-escapes in STRING with values in TABLE.
  19461. TABLE is an association list with keys like \"%a\" and string values.
  19462. The sequences in STRING may contain normal field width and padding information,
  19463. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19464. so values can contain further %-escapes if they are define later in TABLE."
  19465. (let ((tbl (copy-alist table))
  19466. (case-fold-search nil)
  19467. (pchg 0)
  19468. e re rpl)
  19469. (while (setq e (pop tbl))
  19470. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19471. (when (and (cdr e) (string-match re (cdr e)))
  19472. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19473. (safe "SREF"))
  19474. (add-text-properties 0 3 (list 'sref sref) safe)
  19475. (setcdr e (replace-match safe t t (cdr e)))))
  19476. (while (string-match re string)
  19477. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19478. (cdr e)))
  19479. (setq string (replace-match rpl t t string))))
  19480. (while (setq pchg (next-property-change pchg string))
  19481. (let ((sref (get-text-property pchg 'sref string)))
  19482. (when (and sref (string-match "SREF" string pchg))
  19483. (setq string (replace-match sref t t string)))))
  19484. string))
  19485. (defun org-sublist (list start end)
  19486. "Return a section of LIST, from START to END.
  19487. Counting starts at 1."
  19488. (let (rtn (c start))
  19489. (setq list (nthcdr (1- start) list))
  19490. (while (and list (<= c end))
  19491. (push (pop list) rtn)
  19492. (setq c (1+ c)))
  19493. (nreverse rtn)))
  19494. (defun org-find-base-buffer-visiting (file)
  19495. "Like `find-buffer-visiting' but always return the base buffer and
  19496. not an indirect buffer."
  19497. (let ((buf (or (get-file-buffer file)
  19498. (find-buffer-visiting file))))
  19499. (if buf
  19500. (or (buffer-base-buffer buf) buf)
  19501. nil)))
  19502. (defun org-image-file-name-regexp (&optional extensions)
  19503. "Return regexp matching the file names of images.
  19504. If EXTENSIONS is given, only match these."
  19505. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19506. (image-file-name-regexp)
  19507. (let ((image-file-name-extensions
  19508. (or extensions
  19509. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19510. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19511. (concat "\\."
  19512. (regexp-opt (nconc (mapcar 'upcase
  19513. image-file-name-extensions)
  19514. image-file-name-extensions)
  19515. t)
  19516. "\\'"))))
  19517. (defun org-file-image-p (file &optional extensions)
  19518. "Return non-nil if FILE is an image."
  19519. (save-match-data
  19520. (string-match (org-image-file-name-regexp extensions) file)))
  19521. (defun org-get-cursor-date (&optional with-time)
  19522. "Return the date at cursor in as a time.
  19523. This works in the calendar and in the agenda, anywhere else it just
  19524. returns the current time.
  19525. If WITH-TIME is non-nil, returns the time of the event at point (in
  19526. the agenda) or the current time of the day."
  19527. (let (date day defd tp tm hod mod)
  19528. (when with-time
  19529. (setq tp (get-text-property (point) 'time))
  19530. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19531. (setq hod (string-to-number (match-string 1 tp))
  19532. mod (string-to-number (match-string 2 tp))))
  19533. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19534. mod (nth 1 (decode-time (current-time))))))
  19535. (cond
  19536. ((eq major-mode 'calendar-mode)
  19537. (setq date (calendar-cursor-to-date)
  19538. defd (encode-time 0 (or mod 0) (or hod 0)
  19539. (nth 1 date) (nth 0 date) (nth 2 date))))
  19540. ((eq major-mode 'org-agenda-mode)
  19541. (setq day (get-text-property (point) 'day))
  19542. (if day
  19543. (setq date (calendar-gregorian-from-absolute day)
  19544. defd (encode-time 0 (or mod 0) (or hod 0)
  19545. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19546. (or defd (current-time))))
  19547. (defun org-mark-subtree (&optional up)
  19548. "Mark the current subtree.
  19549. This puts point at the start of the current subtree, and mark at
  19550. the end. If a numeric prefix UP is given, move up into the
  19551. hierarchy of headlines by UP levels before marking the subtree."
  19552. (interactive "P")
  19553. (org-with-limited-levels
  19554. (cond ((org-at-heading-p) (beginning-of-line))
  19555. ((org-before-first-heading-p) (error "Not in a subtree"))
  19556. (t (outline-previous-visible-heading 1))))
  19557. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19558. (if (org-called-interactively-p 'any)
  19559. (call-interactively 'org-mark-element)
  19560. (org-mark-element)))
  19561. ;;; Macros
  19562. ;; Macros are expanded with `org-macro-replace-all', which relies
  19563. ;; internally on `org-macro-expand'.
  19564. ;; Default templates for expansion are stored in the buffer-local
  19565. ;; variable `org-macro-templates'. This variable is updated by
  19566. ;; `org-macro-initialize-templates'.
  19567. ;; Along with macros defined through #+MACRO: keyword, default
  19568. ;; templates include the following hard-coded macros:
  19569. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19570. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19571. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19572. ;; {{{title}}} will also be provided.
  19573. (defvar org-macro-templates nil
  19574. "Alist containing all macro templates in current buffer.
  19575. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19576. stands for macro's name and template for its replacement value,
  19577. both as strings. This is an internal variable. Do not set it
  19578. directly, use instead:
  19579. #+MACRO: name template")
  19580. (make-variable-buffer-local 'org-macro-templates)
  19581. (defun org-macro-expand (macro templates)
  19582. "Return expanded MACRO, as a string.
  19583. MACRO is an object, obtained, for example, with
  19584. `org-element-context'. TEMPLATES is an alist of templates used
  19585. for expansion. See `org-macro-templates' for a buffer-local
  19586. default value. Return nil if no template was found."
  19587. (let ((template
  19588. ;; Macro names are case-insensitive.
  19589. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19590. (when template
  19591. (let ((value (replace-regexp-in-string
  19592. "\\$[0-9]+"
  19593. (lambda (arg)
  19594. (or (nth (1- (string-to-number (substring arg 1)))
  19595. (org-element-property :args macro))
  19596. ;; No argument provided: remove
  19597. ;; place-holder.
  19598. ""))
  19599. template)))
  19600. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19601. (when (string-match "\\`(eval\\>" value)
  19602. (setq value (eval (read value))))
  19603. ;; Return string.
  19604. (format "%s" (or value ""))))))
  19605. (defun org-macro-replace-all (templates)
  19606. "Replace all macros in current buffer by their expansion.
  19607. TEMPLATES is an alist of templates used for expansion. See
  19608. `org-macro-templates' for a buffer-local default value."
  19609. (save-excursion
  19610. (goto-char (point-min))
  19611. (let (record)
  19612. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19613. (let ((object (org-element-context)))
  19614. (when (eq (org-element-type object) 'macro)
  19615. (let* ((value (org-macro-expand object templates))
  19616. (begin (org-element-property :begin object))
  19617. (signature (list begin
  19618. object
  19619. (org-element-property :args object))))
  19620. ;; Avoid circular dependencies by checking if the same
  19621. ;; macro with the same arguments is expanded at the same
  19622. ;; position twice.
  19623. (if (member signature record)
  19624. (error "Circular macro expansion: %s"
  19625. (org-element-property :key object))
  19626. (when value
  19627. (push signature record)
  19628. (delete-region
  19629. begin
  19630. ;; Preserve white spaces after the macro.
  19631. (progn (goto-char (org-element-property :end object))
  19632. (skip-chars-backward " \t")
  19633. (point)))
  19634. ;; Leave point before replacement in case of recursive
  19635. ;; expansions.
  19636. (save-excursion (insert value)))))))))))
  19637. (defun org-macro-initialize-templates ()
  19638. "Collect macro templates defined in current buffer.
  19639. Templates are stored in buffer-local variable
  19640. `org-macro-templates'. In addition to buffer-defined macros, the
  19641. function installs the following ones: \"property\",
  19642. \"time\". and, if the buffer is associated to a file,
  19643. \"input-file\" and \"modification-time\"."
  19644. (let ((case-fold-search t)
  19645. (set-template
  19646. (lambda (cell)
  19647. ;; Add CELL to `org-macro-templates' if there's no
  19648. ;; association matching its name already. Otherwise,
  19649. ;; replace old association with the new one in that
  19650. ;; variable.
  19651. (let ((old-template (assoc (car cell) org-macro-templates)))
  19652. (if old-template (setcdr old-template (cdr cell))
  19653. (push cell org-macro-templates))))))
  19654. ;; Install buffer-local macros.
  19655. (org-with-wide-buffer
  19656. (goto-char (point-min))
  19657. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19658. (let ((element (org-element-at-point)))
  19659. (when (eq (org-element-type element) 'keyword)
  19660. (let ((value (org-element-property :value element)))
  19661. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19662. (funcall set-template
  19663. (cons (match-string 1 value)
  19664. (or (match-string 2 value) "")))))))))
  19665. ;; Install hard-coded macros.
  19666. (mapc (lambda (cell) (funcall set-template cell))
  19667. (list
  19668. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19669. (cons "time" "(eval (format-time-string \"$1\"))")))
  19670. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19671. (when (and visited-file (file-exists-p visited-file))
  19672. (mapc (lambda (cell) (funcall set-template cell))
  19673. (list
  19674. (cons "input-file" (file-name-nondirectory visited-file))
  19675. (cons "modification-time"
  19676. (format "(eval (format-time-string \"$1\" '%s))"
  19677. (prin1-to-string
  19678. (nth 5 (file-attributes visited-file)))))))))))
  19679. ;;; Indentation
  19680. (defun org-indent-line ()
  19681. "Indent line depending on context."
  19682. (interactive)
  19683. (let* ((pos (point))
  19684. (itemp (org-at-item-p))
  19685. (case-fold-search t)
  19686. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19687. (inline-task-p (and (featurep 'org-inlinetask)
  19688. (org-inlinetask-in-task-p)))
  19689. (inline-re (and inline-task-p
  19690. (org-inlinetask-outline-regexp)))
  19691. column)
  19692. (if (and orgstruct-is-++ (eq pos (point)))
  19693. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19694. (indent-according-to-mode))
  19695. (beginning-of-line 1)
  19696. (cond
  19697. ;; Headings
  19698. ((looking-at org-outline-regexp) (setq column 0))
  19699. ;; Included files
  19700. ((looking-at "#\\+include:") (setq column 0))
  19701. ;; Footnote definition
  19702. ((looking-at org-footnote-definition-re) (setq column 0))
  19703. ;; Literal examples
  19704. ((looking-at "[ \t]*:\\( \\|$\\)")
  19705. (setq column (org-get-indentation))) ; do nothing
  19706. ;; Lists
  19707. ((ignore-errors (goto-char (org-in-item-p)))
  19708. (setq column (if itemp
  19709. (org-get-indentation)
  19710. (org-list-item-body-column (point))))
  19711. (goto-char pos))
  19712. ;; Drawers
  19713. ((and (looking-at "[ \t]*:END:")
  19714. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19715. (save-excursion
  19716. (goto-char (1- (match-beginning 1)))
  19717. (setq column (current-column))))
  19718. ;; Special blocks
  19719. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19720. (save-excursion
  19721. (re-search-backward
  19722. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19723. (setq column (org-get-indentation (match-string 0))))
  19724. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19725. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19726. (save-excursion
  19727. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19728. (setq column
  19729. (cond ((equal (downcase (match-string 1)) "src")
  19730. ;; src blocks: let `org-edit-src-exit' handle them
  19731. (org-get-indentation))
  19732. ((equal (downcase (match-string 1)) "example")
  19733. (max (org-get-indentation)
  19734. (org-get-indentation (match-string 0))))
  19735. (t
  19736. (org-get-indentation (match-string 0))))))
  19737. ;; This line has nothing special, look at the previous relevant
  19738. ;; line to compute indentation
  19739. (t
  19740. (beginning-of-line 0)
  19741. (while (and (not (bobp))
  19742. (not (looking-at org-table-line-regexp))
  19743. (not (looking-at org-drawer-regexp))
  19744. ;; When point started in an inline task, do not move
  19745. ;; above task starting line.
  19746. (not (and inline-task-p (looking-at inline-re)))
  19747. ;; Skip drawers, blocks, empty lines, verbatim,
  19748. ;; comments, tables, footnotes definitions, lists,
  19749. ;; inline tasks.
  19750. (or (and (looking-at "[ \t]*:END:")
  19751. (re-search-backward org-drawer-regexp nil t))
  19752. (and (looking-at "[ \t]*#\\+end_")
  19753. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19754. (looking-at "[ \t]*[\n:#|]")
  19755. (looking-at org-footnote-definition-re)
  19756. (and (ignore-errors (goto-char (org-in-item-p)))
  19757. (goto-char
  19758. (org-list-get-top-point (org-list-struct))))
  19759. (and (not inline-task-p)
  19760. (featurep 'org-inlinetask)
  19761. (org-inlinetask-in-task-p)
  19762. (or (org-inlinetask-goto-beginning) t))))
  19763. (beginning-of-line 0))
  19764. (cond
  19765. ;; There was an heading above.
  19766. ((looking-at "\\*+[ \t]+")
  19767. (if (not org-adapt-indentation)
  19768. (setq column 0)
  19769. (goto-char (match-end 0))
  19770. (setq column (current-column))))
  19771. ;; A drawer had started and is unfinished
  19772. ((looking-at org-drawer-regexp)
  19773. (goto-char (1- (match-beginning 1)))
  19774. (setq column (current-column)))
  19775. ;; Else, nothing noticeable found: get indentation and go on.
  19776. (t (setq column (org-get-indentation))))))
  19777. ;; Now apply indentation and move cursor accordingly
  19778. (goto-char pos)
  19779. (if (<= (current-column) (current-indentation))
  19780. (org-indent-line-to column)
  19781. (save-excursion (org-indent-line-to column)))
  19782. ;; Special polishing for properties, see `org-property-format'
  19783. (setq column (current-column))
  19784. (beginning-of-line 1)
  19785. (if (looking-at
  19786. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19787. (replace-match (concat (match-string 1)
  19788. (format org-property-format
  19789. (match-string 2) (match-string 3)))
  19790. t t))
  19791. (org-move-to-column column))))
  19792. (defun org-indent-drawer ()
  19793. "Indent the drawer at point."
  19794. (interactive)
  19795. (let ((p (point))
  19796. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19797. (match-end 0)))
  19798. (folded
  19799. (save-excursion
  19800. (end-of-line)
  19801. (when (overlays-at (point))
  19802. (member 'invisible (overlay-properties
  19803. (car (overlays-at (point)))))))))
  19804. (when folded (org-cycle))
  19805. (indent-for-tab-command)
  19806. (while (and (move-beginning-of-line 2) (< (point) e))
  19807. (indent-for-tab-command))
  19808. (goto-char p)
  19809. (when folded (org-cycle)))
  19810. (message "Drawer at point indented"))
  19811. (defun org-indent-block ()
  19812. "Indent the block at point."
  19813. (interactive)
  19814. (let ((p (point))
  19815. (case-fold-search t)
  19816. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19817. (match-end 0)))
  19818. (folded
  19819. (save-excursion
  19820. (end-of-line)
  19821. (when (overlays-at (point))
  19822. (member 'invisible (overlay-properties
  19823. (car (overlays-at (point)))))))))
  19824. (when folded (org-cycle))
  19825. (indent-for-tab-command)
  19826. (while (and (move-beginning-of-line 2) (< (point) e))
  19827. (indent-for-tab-command))
  19828. (goto-char p)
  19829. (when folded (org-cycle)))
  19830. (message "Block at point indented"))
  19831. (defun org-indent-region (start end)
  19832. "Indent region."
  19833. (interactive "r")
  19834. (save-excursion
  19835. (let ((line-end (org-current-line end)))
  19836. (goto-char start)
  19837. (while (< (org-current-line) line-end)
  19838. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19839. (t (call-interactively 'org-indent-line)))
  19840. (move-beginning-of-line 2)))))
  19841. ;;; Filling
  19842. ;; We use our own fill-paragraph and auto-fill functions.
  19843. ;; `org-fill-paragraph' relies on adaptive filling and context
  19844. ;; checking. Appropriate `fill-prefix' is computed with
  19845. ;; `org-adaptive-fill-function'.
  19846. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19847. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19848. ;; `org-adaptive-fill-function' value. Internally,
  19849. ;; `org-comment-line-break-function' breaks the line.
  19850. ;; `org-setup-filling' installs filling and auto-filling related
  19851. ;; variables during `org-mode' initialization.
  19852. (defun org-setup-filling ()
  19853. (interactive)
  19854. ;; Prevent auto-fill from inserting unwanted new items.
  19855. (when (boundp 'fill-nobreak-predicate)
  19856. (org-set-local
  19857. 'fill-nobreak-predicate
  19858. (org-uniquify
  19859. (append fill-nobreak-predicate
  19860. '(org-fill-paragraph-separate-nobreak-p
  19861. org-fill-line-break-nobreak-p)))))
  19862. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19863. (org-set-local 'auto-fill-inhibit-regexp nil)
  19864. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19865. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19866. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19867. (defvar org-element-paragraph-separate) ; org-element.el
  19868. (defun org-fill-paragraph-separate-nobreak-p ()
  19869. "Non-nil when a line break at point would insert a new item."
  19870. (looking-at (substring org-element-paragraph-separate 1)))
  19871. (defun org-fill-line-break-nobreak-p ()
  19872. "Non-nil when a line break at point would create an Org line break."
  19873. (save-excursion
  19874. (skip-chars-backward "[ \t]")
  19875. (skip-chars-backward "\\\\")
  19876. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19877. (declare-function message-in-body-p "message" ())
  19878. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19879. (defun org-adaptive-fill-function ()
  19880. "Compute a fill prefix for the current line.
  19881. Return fill prefix, as a string, or nil if current line isn't
  19882. meant to be filled."
  19883. (let (prefix)
  19884. (catch 'exit
  19885. (when (derived-mode-p 'message-mode)
  19886. (save-excursion
  19887. (beginning-of-line)
  19888. (cond ((or (not (message-in-body-p))
  19889. (looking-at orgtbl-line-start-regexp))
  19890. (throw 'exit nil))
  19891. ((looking-at message-cite-prefix-regexp)
  19892. (throw 'exit (match-string-no-properties 0)))
  19893. ((looking-at org-outline-regexp)
  19894. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19895. (org-with-wide-buffer
  19896. (let* ((p (line-beginning-position))
  19897. (element (save-excursion (beginning-of-line)
  19898. (org-element-at-point)))
  19899. (type (org-element-type element))
  19900. (post-affiliated (org-element-property :post-affiliated element)))
  19901. (unless (and post-affiliated (< p post-affiliated))
  19902. (case type
  19903. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19904. (footnote-definition "")
  19905. ((item plain-list)
  19906. (make-string (org-list-item-body-column
  19907. (or post-affiliated
  19908. (org-element-property :begin element)))
  19909. ? ))
  19910. (paragraph
  19911. ;; Fill prefix is usually the same as the current line,
  19912. ;; except if the paragraph is at the beginning of an item.
  19913. (let ((parent (org-element-property :parent element)))
  19914. (cond ((eq (org-element-type parent) 'item)
  19915. (make-string (org-list-item-body-column
  19916. (org-element-property :begin parent))
  19917. ? ))
  19918. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19919. (match-string 0))
  19920. (t ""))))
  19921. (comment-block
  19922. ;; Only fill contents if P is within block boundaries.
  19923. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19924. (forward-line)
  19925. (point)))
  19926. (cend (save-excursion
  19927. (goto-char (org-element-property :end element))
  19928. (skip-chars-backward " \r\t\n")
  19929. (line-beginning-position))))
  19930. (when (and (>= p cbeg) (< p cend))
  19931. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19932. (match-string 0)
  19933. "")))))))))))
  19934. (declare-function message-goto-body "message" ())
  19935. (defvar message-cite-prefix-regexp) ; From message.el
  19936. (defvar org-element-all-objects) ; From org-element.el
  19937. (defun org-fill-paragraph (&optional justify)
  19938. "Fill element at point, when applicable.
  19939. This function only applies to comment blocks, comments, example
  19940. blocks and paragraphs. Also, as a special case, re-align table
  19941. when point is at one.
  19942. If JUSTIFY is non-nil (interactively, with prefix argument),
  19943. justify as well. If `sentence-end-double-space' is non-nil, then
  19944. period followed by one space does not end a sentence, so don't
  19945. break a line there. The variable `fill-column' controls the
  19946. width for filling.
  19947. For convenience, when point is at a plain list, an item or
  19948. a footnote definition, try to fill the first paragraph within."
  19949. (interactive)
  19950. (if (and (derived-mode-p 'message-mode)
  19951. (or (not (message-in-body-p))
  19952. (save-excursion (move-beginning-of-line 1)
  19953. (looking-at message-cite-prefix-regexp))))
  19954. ;; First ensure filling is correct in message-mode.
  19955. (let ((fill-paragraph-function
  19956. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19957. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19958. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19959. (paragraph-separate
  19960. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19961. (fill-paragraph nil))
  19962. (save-excursion
  19963. ;; Move to end of line in order to get the first paragraph
  19964. ;; within a plain list or a footnote definition.
  19965. (end-of-line)
  19966. (let ((element (org-element-at-point)))
  19967. ;; First check if point is in a blank line at the beginning of
  19968. ;; the buffer. In that case, ignore filling.
  19969. (if (< (point) (org-element-property :begin element)) t
  19970. (case (org-element-type element)
  19971. ;; Use major mode filling function is src blocks.
  19972. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19973. ;; Align Org tables, leave table.el tables as-is.
  19974. (table-row (org-table-align) t)
  19975. (table
  19976. (when (eq (org-element-property :type element) 'org)
  19977. (org-table-align))
  19978. t)
  19979. (paragraph
  19980. ;; Paragraphs may contain `line-break' type objects.
  19981. (let ((beg (max (point-min)
  19982. (org-element-property :contents-begin element)))
  19983. (end (min (point-max)
  19984. (org-element-property :contents-end element))))
  19985. ;; Do nothing if point is at an affiliated keyword.
  19986. (if (< (point) beg) t
  19987. (when (derived-mode-p 'message-mode)
  19988. ;; In `message-mode', do not fill following
  19989. ;; citation in current paragraph nor text before
  19990. ;; message body.
  19991. (let ((body-start (save-excursion (message-goto-body))))
  19992. (when body-start (setq beg (max body-start beg))))
  19993. (when (save-excursion
  19994. (re-search-forward
  19995. (concat "^" message-cite-prefix-regexp) end t))
  19996. (setq end (match-beginning 0))))
  19997. ;; Fill paragraph, taking line breaks into
  19998. ;; consideration. For that, slice the paragraph
  19999. ;; using line breaks as separators, and fill the
  20000. ;; parts in reverse order to avoid messing with
  20001. ;; markers.
  20002. (save-excursion
  20003. (goto-char end)
  20004. (mapc
  20005. (lambda (pos)
  20006. (fill-region-as-paragraph pos (point) justify)
  20007. (goto-char pos))
  20008. ;; Find the list of ending positions for line
  20009. ;; breaks in the current paragraph. Add paragraph
  20010. ;; beginning to include first slice.
  20011. (nreverse
  20012. (cons
  20013. beg
  20014. (org-element-map
  20015. (org-element--parse-objects
  20016. beg end nil org-element-all-objects)
  20017. 'line-break
  20018. (lambda (lb) (org-element-property :end lb)))))))
  20019. t)))
  20020. ;; Contents of `comment-block' type elements should be
  20021. ;; filled as plain text, but only if point is within block
  20022. ;; markers.
  20023. (comment-block
  20024. (let* ((case-fold-search t)
  20025. (beg (save-excursion
  20026. (goto-char (org-element-property :begin element))
  20027. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20028. (forward-line)
  20029. (point)))
  20030. (end (save-excursion
  20031. (goto-char (org-element-property :end element))
  20032. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20033. (line-beginning-position))))
  20034. (when (and (>= (point) beg) (< (point) end))
  20035. (fill-region-as-paragraph
  20036. (save-excursion
  20037. (end-of-line)
  20038. (re-search-backward "^[ \t]*$" beg 'move)
  20039. (line-beginning-position))
  20040. (save-excursion
  20041. (beginning-of-line)
  20042. (re-search-forward "^[ \t]*$" end 'move)
  20043. (line-beginning-position))
  20044. justify)))
  20045. t)
  20046. ;; Fill comments.
  20047. (comment (fill-comment-paragraph justify))
  20048. ;; Ignore every other element.
  20049. (otherwise t)))))))
  20050. (defun org-auto-fill-function ()
  20051. "Auto-fill function."
  20052. ;; Check if auto-filling is meaningful.
  20053. (let ((fc (current-fill-column)))
  20054. (when (and fc (> (current-column) fc))
  20055. (let* ((fill-prefix (org-adaptive-fill-function))
  20056. ;; Enforce empty fill prefix, if required. Otherwise, it
  20057. ;; will be computed again.
  20058. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20059. (when fill-prefix (do-auto-fill))))))
  20060. (defun org-comment-line-break-function (&optional soft)
  20061. "Break line at point and indent, continuing comment if within one.
  20062. The inserted newline is marked hard if variable
  20063. `use-hard-newlines' is true, unless optional argument SOFT is
  20064. non-nil."
  20065. (if soft (insert-and-inherit ?\n) (newline 1))
  20066. (save-excursion (forward-char -1) (delete-horizontal-space))
  20067. (delete-horizontal-space)
  20068. (indent-to-left-margin)
  20069. (insert-before-markers-and-inherit fill-prefix))
  20070. ;;; Comments
  20071. ;; Org comments syntax is quite complex. It requires the entire line
  20072. ;; to be just a comment. Also, even with the right syntax at the
  20073. ;; beginning of line, some some elements (i.e. verse-block or
  20074. ;; example-block) don't accept comments. Usual Emacs comment commands
  20075. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20076. ;; Org still relies on `comment-dwim', but cannot trust
  20077. ;; `comment-only-p'. So, `comment-region-function' and
  20078. ;; `uncomment-region-function' both point
  20079. ;; to`org-comment-or-uncomment-region'. Eventually,
  20080. ;; `org-insert-comment' takes care of insertion of comments at the
  20081. ;; beginning of line.
  20082. ;; `org-setup-comments-handling' install comments related variables
  20083. ;; during `org-mode' initialization.
  20084. (defun org-setup-comments-handling ()
  20085. (interactive)
  20086. (org-set-local 'comment-use-syntax nil)
  20087. (org-set-local 'comment-start "# ")
  20088. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20089. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20090. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20091. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20092. (defun org-insert-comment ()
  20093. "Insert an empty comment above current line.
  20094. If the line is empty, insert comment at its beginning."
  20095. (beginning-of-line)
  20096. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20097. (org-indent-line)
  20098. (insert "# "))
  20099. (defvar comment-empty-lines) ; From newcomment.el.
  20100. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20101. "Comment or uncomment each non-blank line in the region.
  20102. Uncomment each non-blank line between BEG and END if it only
  20103. contains commented lines. Otherwise, comment them."
  20104. (save-restriction
  20105. ;; Restrict region
  20106. (narrow-to-region (save-excursion (goto-char beg)
  20107. (skip-chars-forward " \r\t\n" end)
  20108. (line-beginning-position))
  20109. (save-excursion (goto-char end)
  20110. (skip-chars-backward " \r\t\n" beg)
  20111. (line-end-position)))
  20112. (let ((uncommentp
  20113. ;; UNCOMMENTP is non-nil when every non blank line between
  20114. ;; BEG and END is a comment.
  20115. (save-excursion
  20116. (goto-char (point-min))
  20117. (while (and (not (eobp))
  20118. (let ((element (org-element-at-point)))
  20119. (and (eq (org-element-type element) 'comment)
  20120. (goto-char (min (point-max)
  20121. (org-element-property
  20122. :end element)))))))
  20123. (eobp))))
  20124. (if uncommentp
  20125. ;; Only blank lines and comments in region: uncomment it.
  20126. (save-excursion
  20127. (goto-char (point-min))
  20128. (while (not (eobp))
  20129. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20130. (replace-match "" nil nil nil 1))
  20131. (forward-line)))
  20132. ;; Comment each line in region.
  20133. (let ((min-indent (point-max)))
  20134. ;; First find the minimum indentation across all lines.
  20135. (save-excursion
  20136. (goto-char (point-min))
  20137. (while (and (not (eobp)) (not (zerop min-indent)))
  20138. (unless (looking-at "[ \t]*$")
  20139. (setq min-indent (min min-indent (current-indentation))))
  20140. (forward-line)))
  20141. ;; Then loop over all lines.
  20142. (save-excursion
  20143. (goto-char (point-min))
  20144. (while (not (eobp))
  20145. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20146. (org-move-to-column min-indent t)
  20147. (insert comment-start))
  20148. (forward-line))))))))
  20149. ;;; Planning
  20150. ;; This section contains tools to operate on timestamp objects, as
  20151. ;; returned by, e.g. `org-element-context'.
  20152. (defun org-timestamp-has-time-p (timestamp)
  20153. "Non-nil when TIMESTAMP has a time specified."
  20154. (org-element-property :hour-start timestamp))
  20155. (defun org-timestamp-format (timestamp format &optional end utc)
  20156. "Format a TIMESTAMP element into a string.
  20157. FORMAT is a format specifier to be passed to
  20158. `format-time-string'.
  20159. When optional argument END is non-nil, use end of date-range or
  20160. time-range, if possible.
  20161. When optional argument UTC is non-nil, time will be expressed as
  20162. Universal Time."
  20163. (format-time-string
  20164. format
  20165. (apply 'encode-time
  20166. (cons 0
  20167. (mapcar
  20168. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20169. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20170. '(:minute-start :hour-start :day-start :month-start
  20171. :year-start)))))
  20172. utc))
  20173. (defun org-timestamp-split-range (timestamp &optional end)
  20174. "Extract a timestamp object from a date or time range.
  20175. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20176. the end of the range. Otherwise, extract its start.
  20177. Return a new timestamp object sharing the same parent as
  20178. TIMESTAMP."
  20179. (let ((type (org-element-property :type timestamp)))
  20180. (if (memq type '(active inactive diary)) timestamp
  20181. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20182. ;; Set new type.
  20183. (org-element-put-property
  20184. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20185. ;; Copy start properties over end properties if END is
  20186. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20187. (let ((p-alist '((:minute-start . :minute-end)
  20188. (:hour-start . :hour-end)
  20189. (:day-start . :day-end)
  20190. (:month-start . :month-end)
  20191. (:year-start . :year-end))))
  20192. (dolist (p-cell p-alist)
  20193. (org-element-put-property
  20194. split-ts
  20195. (funcall (if end 'car 'cdr) p-cell)
  20196. (org-element-property
  20197. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20198. ;; Eventually refresh `:raw-value'.
  20199. (org-element-put-property split-ts :raw-value nil)
  20200. (org-element-put-property
  20201. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20202. (defun org-timestamp-translate (timestamp &optional boundary)
  20203. "Apply `org-translate-time' on a TIMESTAMP object.
  20204. When optional argument BOUNDARY is non-nil, it is either the
  20205. symbol `start' or `end'. In this case, only translate the
  20206. starting or ending part of TIMESTAMP if it is a date or time
  20207. range. Otherwise, translate both parts."
  20208. (if (and (not boundary)
  20209. (memq (org-element-property :type timestamp)
  20210. '(active-range inactive-range)))
  20211. (concat
  20212. (org-translate-time
  20213. (org-element-property :raw-value
  20214. (org-timestamp-split-range timestamp)))
  20215. "--"
  20216. (org-translate-time
  20217. (org-element-property :raw-value
  20218. (org-timestamp-split-range timestamp t))))
  20219. (org-translate-time
  20220. (org-element-property
  20221. :raw-value
  20222. (if (not boundary) timestamp
  20223. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20224. ;;; Other stuff.
  20225. (defun org-toggle-fixed-width-section (arg)
  20226. "Toggle the fixed-width export.
  20227. If there is no active region, the QUOTE keyword at the current headline is
  20228. inserted or removed. When present, it causes the text between this headline
  20229. and the next to be exported as fixed-width text, and unmodified.
  20230. If there is an active region, this command adds or removes a colon as the
  20231. first character of this line. If the first character of a line is a colon,
  20232. this line is also exported in fixed-width font."
  20233. (interactive "P")
  20234. (let* ((cc 0)
  20235. (regionp (org-region-active-p))
  20236. (beg (if regionp (region-beginning) (point)))
  20237. (end (if regionp (region-end)))
  20238. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20239. (case-fold-search nil)
  20240. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20241. off)
  20242. (if regionp
  20243. (save-excursion
  20244. (goto-char beg)
  20245. (setq cc (current-column))
  20246. (beginning-of-line 1)
  20247. (setq off (looking-at re))
  20248. (while (> nlines 0)
  20249. (setq nlines (1- nlines))
  20250. (beginning-of-line 1)
  20251. (cond
  20252. (arg
  20253. (org-move-to-column cc t)
  20254. (insert ": \n")
  20255. (forward-line -1))
  20256. ((and off (looking-at re))
  20257. (replace-match "" t t nil 1))
  20258. ((not off) (org-move-to-column cc t) (insert ": ")))
  20259. (forward-line 1)))
  20260. (save-excursion
  20261. (org-back-to-heading)
  20262. (cond
  20263. ((looking-at (format org-heading-keyword-regexp-format
  20264. org-quote-string))
  20265. (goto-char (match-end 1))
  20266. (looking-at (concat " +" org-quote-string))
  20267. (replace-match "" t t)
  20268. (when (eolp) (insert " ")))
  20269. ((looking-at org-outline-regexp)
  20270. (goto-char (match-end 0))
  20271. (insert org-quote-string " ")))))))
  20272. (defun org-reftex-citation ()
  20273. "Use reftex-citation to insert a citation into the buffer.
  20274. This looks for a line like
  20275. #+BIBLIOGRAPHY: foo plain option:-d
  20276. and derives from it that foo.bib is the bibliography file relevant
  20277. for this document. It then installs the necessary environment for RefTeX
  20278. to work in this buffer and calls `reftex-citation' to insert a citation
  20279. into the buffer.
  20280. Export of such citations to both LaTeX and HTML is handled by the contributed
  20281. package org-exp-bibtex by Taru Karttunen."
  20282. (interactive)
  20283. (let ((reftex-docstruct-symbol 'rds)
  20284. (reftex-cite-format "\\cite{%l}")
  20285. rds bib)
  20286. (save-excursion
  20287. (save-restriction
  20288. (widen)
  20289. (let ((case-fold-search t)
  20290. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20291. (if (not (save-excursion
  20292. (or (re-search-forward re nil t)
  20293. (re-search-backward re nil t))))
  20294. (error "No bibliography defined in file")
  20295. (setq bib (concat (match-string 1) ".bib")
  20296. rds (list (list 'bib bib)))))))
  20297. (call-interactively 'reftex-citation)))
  20298. ;;;; Functions extending outline functionality
  20299. (defun org-beginning-of-line (&optional arg)
  20300. "Go to the beginning of the current line. If that is invisible, continue
  20301. to a visible line beginning. This makes the function of C-a more intuitive.
  20302. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20303. first attempt, and only move to after the tags when the cursor is already
  20304. beyond the end of the headline."
  20305. (interactive "P")
  20306. (let ((pos (point))
  20307. (special (if (consp org-special-ctrl-a/e)
  20308. (car org-special-ctrl-a/e)
  20309. org-special-ctrl-a/e))
  20310. refpos)
  20311. (if (org-bound-and-true-p visual-line-mode)
  20312. (beginning-of-visual-line 1)
  20313. (beginning-of-line 1))
  20314. (if (and arg (fboundp 'move-beginning-of-line))
  20315. (call-interactively 'move-beginning-of-line)
  20316. (if (bobp)
  20317. nil
  20318. (backward-char 1)
  20319. (if (org-truely-invisible-p)
  20320. (while (and (not (bobp)) (org-truely-invisible-p))
  20321. (backward-char 1)
  20322. (beginning-of-line 1))
  20323. (forward-char 1))))
  20324. (when special
  20325. (cond
  20326. ((and (looking-at org-complex-heading-regexp)
  20327. (= (char-after (match-end 1)) ?\ ))
  20328. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20329. (point-at-eol)))
  20330. (goto-char
  20331. (if (eq special t)
  20332. (cond ((> pos refpos) refpos)
  20333. ((= pos (point)) refpos)
  20334. (t (point)))
  20335. (cond ((> pos (point)) (point))
  20336. ((not (eq last-command this-command)) (point))
  20337. (t refpos)))))
  20338. ((org-at-item-p)
  20339. ;; Being at an item and not looking at an the item means point
  20340. ;; was previously moved to beginning of a visual line, which
  20341. ;; doesn't contain the item. Therefore, do nothing special,
  20342. ;; just stay here.
  20343. (when (looking-at org-list-full-item-re)
  20344. ;; Set special position at first white space character after
  20345. ;; bullet, and check-box, if any.
  20346. (let ((after-bullet
  20347. (let ((box (match-end 3)))
  20348. (if (not box) (match-end 1)
  20349. (let ((after (char-after box)))
  20350. (if (and after (= after ? )) (1+ box) box))))))
  20351. ;; Special case: Move point to special position when
  20352. ;; currently after it or at beginning of line.
  20353. (if (eq special t)
  20354. (when (or (> pos after-bullet) (= (point) pos))
  20355. (goto-char after-bullet))
  20356. ;; Reversed case: Move point to special position when
  20357. ;; point was already at beginning of line and command is
  20358. ;; repeated.
  20359. (when (and (= (point) pos) (eq last-command this-command))
  20360. (goto-char after-bullet))))))))
  20361. (org-no-warnings
  20362. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20363. (defun org-end-of-line (&optional arg)
  20364. "Go to the end of the line.
  20365. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20366. tags on the first attempt, and only move to after the tags when
  20367. the cursor is already beyond the end of the headline."
  20368. (interactive "P")
  20369. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20370. org-special-ctrl-a/e))
  20371. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20372. 'end-of-visual-line)
  20373. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20374. (t 'end-of-line))))
  20375. (if (or (not special) arg) (call-interactively move-fun)
  20376. (let* ((element (save-excursion (beginning-of-line)
  20377. (org-element-at-point)))
  20378. (type (org-element-type element)))
  20379. (cond
  20380. ((memq type '(headline inlinetask))
  20381. (let ((pos (point)))
  20382. (beginning-of-line 1)
  20383. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20384. (if (eq special t)
  20385. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20386. (goto-char (match-beginning 1))
  20387. (goto-char (match-end 0)))
  20388. (if (or (< pos (match-end 0))
  20389. (not (eq this-command last-command)))
  20390. (goto-char (match-end 0))
  20391. (goto-char (match-beginning 1))))
  20392. (call-interactively move-fun))))
  20393. ((org-element-property :hiddenp element)
  20394. ;; If element is hidden, `move-end-of-line' would put point
  20395. ;; after it. Use `end-of-line' to stay on current line.
  20396. (call-interactively 'end-of-line))
  20397. (t (call-interactively move-fun)))))
  20398. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20399. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20400. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20401. (defun org-backward-sentence (&optional arg)
  20402. "Go to beginning of sentence, or beginning of table field.
  20403. This will call `backward-sentence' or `org-table-beginning-of-field',
  20404. depending on context."
  20405. (interactive "P")
  20406. (cond
  20407. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20408. (t (call-interactively 'backward-sentence))))
  20409. (defun org-forward-sentence (&optional arg)
  20410. "Go to end of sentence, or end of table field.
  20411. This will call `forward-sentence' or `org-table-end-of-field',
  20412. depending on context."
  20413. (interactive "P")
  20414. (cond
  20415. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20416. (t (call-interactively 'forward-sentence))))
  20417. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20418. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20419. (defun org-kill-line (&optional arg)
  20420. "Kill line, to tags or end of line."
  20421. (interactive "P")
  20422. (cond
  20423. ((or (not org-special-ctrl-k)
  20424. (bolp)
  20425. (not (org-at-heading-p)))
  20426. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20427. org-ctrl-k-protect-subtree)
  20428. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20429. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20430. (error "C-k aborted - would kill hidden subtree")))
  20431. (call-interactively
  20432. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20433. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20434. (kill-region (point) (match-beginning 1))
  20435. (org-set-tags nil t))
  20436. (t (kill-region (point) (point-at-eol)))))
  20437. (define-key org-mode-map "\C-k" 'org-kill-line)
  20438. (defun org-yank (&optional arg)
  20439. "Yank. If the kill is a subtree, treat it specially.
  20440. This command will look at the current kill and check if is a single
  20441. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20442. cursor is at the beginning of a line or after the stars of a currently
  20443. empty headline, then the yank is handled specially. How exactly depends
  20444. on the value of the following variables, both set by default.
  20445. org-yank-folded-subtrees
  20446. When set, the subtree(s) will be folded after insertion, but only
  20447. if doing so would now swallow text after the yanked text.
  20448. org-yank-adjusted-subtrees
  20449. When set, the subtree will be promoted or demoted in order to
  20450. fit into the local outline tree structure, which means that the level
  20451. will be adjusted so that it becomes the smaller one of the two
  20452. *visible* surrounding headings.
  20453. Any prefix to this command will cause `yank' to be called directly with
  20454. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20455. will just
  20456. plainly yank the text as it is.
  20457. \[1] The test checks if the first non-white line is a heading
  20458. and if there are no other headings with fewer stars."
  20459. (interactive "P")
  20460. (org-yank-generic 'yank arg))
  20461. (defun org-yank-generic (command arg)
  20462. "Perform some yank-like command.
  20463. This function implements the behavior described in the `org-yank'
  20464. documentation. However, it has been generalized to work for any
  20465. interactive command with similar behavior."
  20466. ;; pretend to be command COMMAND
  20467. (setq this-command command)
  20468. (if arg
  20469. (call-interactively command)
  20470. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20471. (and (org-kill-is-subtree-p)
  20472. (or (bolp)
  20473. (and (looking-at "[ \t]*$")
  20474. (string-match
  20475. "\\`\\*+\\'"
  20476. (buffer-substring (point-at-bol) (point)))))))
  20477. swallowp)
  20478. (cond
  20479. ((and subtreep org-yank-folded-subtrees)
  20480. (let ((beg (point))
  20481. end)
  20482. (if (and subtreep org-yank-adjusted-subtrees)
  20483. (org-paste-subtree nil nil 'for-yank)
  20484. (call-interactively command))
  20485. (setq end (point))
  20486. (goto-char beg)
  20487. (when (and (bolp) subtreep
  20488. (not (setq swallowp
  20489. (org-yank-folding-would-swallow-text beg end))))
  20490. (org-with-limited-levels
  20491. (or (looking-at org-outline-regexp)
  20492. (re-search-forward org-outline-regexp-bol end t))
  20493. (while (and (< (point) end) (looking-at org-outline-regexp))
  20494. (hide-subtree)
  20495. (org-cycle-show-empty-lines 'folded)
  20496. (condition-case nil
  20497. (outline-forward-same-level 1)
  20498. (error (goto-char end))))))
  20499. (when swallowp
  20500. (message
  20501. "Inserted text not folded because that would swallow text"))
  20502. (goto-char end)
  20503. (skip-chars-forward " \t\n\r")
  20504. (beginning-of-line 1)
  20505. (push-mark beg 'nomsg)))
  20506. ((and subtreep org-yank-adjusted-subtrees)
  20507. (let ((beg (point-at-bol)))
  20508. (org-paste-subtree nil nil 'for-yank)
  20509. (push-mark beg 'nomsg)))
  20510. (t
  20511. (call-interactively command))))))
  20512. (defun org-yank-folding-would-swallow-text (beg end)
  20513. "Would hide-subtree at BEG swallow any text after END?"
  20514. (let (level)
  20515. (org-with-limited-levels
  20516. (save-excursion
  20517. (goto-char beg)
  20518. (when (or (looking-at org-outline-regexp)
  20519. (re-search-forward org-outline-regexp-bol end t))
  20520. (setq level (org-outline-level)))
  20521. (goto-char end)
  20522. (skip-chars-forward " \t\r\n\v\f")
  20523. (if (or (eobp)
  20524. (and (bolp) (looking-at org-outline-regexp)
  20525. (<= (org-outline-level) level)))
  20526. nil ; Nothing would be swallowed
  20527. t))))) ; something would swallow
  20528. (define-key org-mode-map "\C-y" 'org-yank)
  20529. (defun org-truely-invisible-p ()
  20530. "Check if point is at a character currently not visible.
  20531. This version does not only check the character property, but also
  20532. `visible-mode'."
  20533. ;; Early versions of noutline don't have `outline-invisible-p'.
  20534. (if (org-bound-and-true-p visible-mode)
  20535. nil
  20536. (outline-invisible-p)))
  20537. (defun org-invisible-p2 ()
  20538. "Check if point is at a character currently not visible."
  20539. (save-excursion
  20540. (if (and (eolp) (not (bobp))) (backward-char 1))
  20541. ;; Early versions of noutline don't have `outline-invisible-p'.
  20542. (outline-invisible-p)))
  20543. (defun org-back-to-heading (&optional invisible-ok)
  20544. "Call `outline-back-to-heading', but provide a better error message."
  20545. (condition-case nil
  20546. (outline-back-to-heading invisible-ok)
  20547. (error (error "Before first headline at position %d in buffer %s"
  20548. (point) (current-buffer)))))
  20549. (defun org-before-first-heading-p ()
  20550. "Before first heading?"
  20551. (save-excursion
  20552. (end-of-line)
  20553. (null (re-search-backward org-outline-regexp-bol nil t))))
  20554. (defun org-at-heading-p (&optional ignored)
  20555. (outline-on-heading-p t))
  20556. ;; Compatibility alias with Org versions < 7.8.03
  20557. (defalias 'org-on-heading-p 'org-at-heading-p)
  20558. (defun org-at-comment-p nil
  20559. "Is cursor in a line starting with a # character?"
  20560. (save-excursion
  20561. (beginning-of-line)
  20562. (looking-at "^#")))
  20563. (defun org-at-drawer-p nil
  20564. "Is cursor at a drawer keyword?"
  20565. (save-excursion
  20566. (move-beginning-of-line 1)
  20567. (looking-at org-drawer-regexp)))
  20568. (defun org-at-block-p nil
  20569. "Is cursor at a block keyword?"
  20570. (save-excursion
  20571. (move-beginning-of-line 1)
  20572. (looking-at org-block-regexp)))
  20573. (defun org-point-at-end-of-empty-headline ()
  20574. "If point is at the end of an empty headline, return t, else nil.
  20575. If the heading only contains a TODO keyword, it is still still considered
  20576. empty."
  20577. (and (looking-at "[ \t]*$")
  20578. (when org-todo-line-regexp
  20579. (save-excursion
  20580. (beginning-of-line 1)
  20581. (let ((case-fold-search nil))
  20582. (looking-at org-todo-line-regexp)
  20583. (string= (match-string 3) ""))))))
  20584. (defun org-at-heading-or-item-p ()
  20585. (or (org-at-heading-p) (org-at-item-p)))
  20586. (defun org-at-target-p ()
  20587. (or (org-in-regexp org-radio-target-regexp)
  20588. (org-in-regexp org-target-regexp)))
  20589. ;; Compatibility alias with Org versions < 7.8.03
  20590. (defalias 'org-on-target-p 'org-at-target-p)
  20591. (defun org-up-heading-all (arg)
  20592. "Move to the heading line of which the present line is a subheading.
  20593. This function considers both visible and invisible heading lines.
  20594. With argument, move up ARG levels."
  20595. (if (fboundp 'outline-up-heading-all)
  20596. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20597. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20598. (defun org-up-heading-safe ()
  20599. "Move to the heading line of which the present line is a subheading.
  20600. This version will not throw an error. It will return the level of the
  20601. headline found, or nil if no higher level is found.
  20602. Also, this function will be a lot faster than `outline-up-heading',
  20603. because it relies on stars being the outline starters. This can really
  20604. make a significant difference in outlines with very many siblings."
  20605. (let (start-level re)
  20606. (org-back-to-heading t)
  20607. (setq start-level (funcall outline-level))
  20608. (if (equal start-level 1)
  20609. nil
  20610. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20611. (if (re-search-backward re nil t)
  20612. (funcall outline-level)))))
  20613. (defun org-first-sibling-p ()
  20614. "Is this heading the first child of its parents?"
  20615. (interactive)
  20616. (let ((re org-outline-regexp-bol)
  20617. level l)
  20618. (unless (org-at-heading-p t)
  20619. (error "Not at a heading"))
  20620. (setq level (funcall outline-level))
  20621. (save-excursion
  20622. (if (not (re-search-backward re nil t))
  20623. t
  20624. (setq l (funcall outline-level))
  20625. (< l level)))))
  20626. (defun org-goto-sibling (&optional previous)
  20627. "Goto the next sibling, even if it is invisible.
  20628. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20629. when a sibling was found. When none is found, return nil and don't
  20630. move point."
  20631. (let ((fun (if previous 're-search-backward 're-search-forward))
  20632. (pos (point))
  20633. (re org-outline-regexp-bol)
  20634. level l)
  20635. (when (condition-case nil (org-back-to-heading t) (error nil))
  20636. (setq level (funcall outline-level))
  20637. (catch 'exit
  20638. (or previous (forward-char 1))
  20639. (while (funcall fun re nil t)
  20640. (setq l (funcall outline-level))
  20641. (when (< l level) (goto-char pos) (throw 'exit nil))
  20642. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20643. (goto-char pos)
  20644. nil))))
  20645. (defun org-show-siblings ()
  20646. "Show all siblings of the current headline."
  20647. (save-excursion
  20648. (while (org-goto-sibling) (org-flag-heading nil)))
  20649. (save-excursion
  20650. (while (org-goto-sibling 'previous)
  20651. (org-flag-heading nil))))
  20652. (defun org-goto-first-child ()
  20653. "Goto the first child, even if it is invisible.
  20654. Return t when a child was found. Otherwise don't move point and
  20655. return nil."
  20656. (let (level (pos (point)) (re org-outline-regexp-bol))
  20657. (when (condition-case nil (org-back-to-heading t) (error nil))
  20658. (setq level (outline-level))
  20659. (forward-char 1)
  20660. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20661. (progn (goto-char (match-beginning 0)) t)
  20662. (goto-char pos) nil))))
  20663. (defun org-show-hidden-entry ()
  20664. "Show an entry where even the heading is hidden."
  20665. (save-excursion
  20666. (org-show-entry)))
  20667. (defun org-flag-heading (flag &optional entry)
  20668. "Flag the current heading. FLAG non-nil means make invisible.
  20669. When ENTRY is non-nil, show the entire entry."
  20670. (save-excursion
  20671. (org-back-to-heading t)
  20672. ;; Check if we should show the entire entry
  20673. (if entry
  20674. (progn
  20675. (org-show-entry)
  20676. (save-excursion
  20677. (and (outline-next-heading)
  20678. (org-flag-heading nil))))
  20679. (outline-flag-region (max (point-min) (1- (point)))
  20680. (save-excursion (outline-end-of-heading) (point))
  20681. flag))))
  20682. (defun org-get-next-sibling ()
  20683. "Move to next heading of the same level, and return point.
  20684. If there is no such heading, return nil.
  20685. This is like outline-next-sibling, but invisible headings are ok."
  20686. (let ((level (funcall outline-level)))
  20687. (outline-next-heading)
  20688. (while (and (not (eobp)) (> (funcall outline-level) level))
  20689. (outline-next-heading))
  20690. (if (or (eobp) (< (funcall outline-level) level))
  20691. nil
  20692. (point))))
  20693. (defun org-get-last-sibling ()
  20694. "Move to previous heading of the same level, and return point.
  20695. If there is no such heading, return nil."
  20696. (let ((opoint (point))
  20697. (level (funcall outline-level)))
  20698. (outline-previous-heading)
  20699. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20700. (while (and (> (funcall outline-level) level)
  20701. (not (bobp)))
  20702. (outline-previous-heading))
  20703. (if (< (funcall outline-level) level)
  20704. nil
  20705. (point)))))
  20706. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20707. "Goto to the end of a subtree."
  20708. ;; This contains an exact copy of the original function, but it uses
  20709. ;; `org-back-to-heading', to make it work also in invisible
  20710. ;; trees. And is uses an invisible-ok argument.
  20711. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20712. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20713. ;; with many children and grandchildren etc, this can be much faster
  20714. ;; than the outline version.
  20715. (org-back-to-heading invisible-ok)
  20716. (let ((first t)
  20717. (level (funcall outline-level)))
  20718. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20719. ;; A true heading (not a plain list item), in Org-mode
  20720. ;; This means we can easily find the end by looking
  20721. ;; only for the right number of stars. Using a regexp to do
  20722. ;; this is so much faster than using a Lisp loop.
  20723. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20724. (forward-char 1)
  20725. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20726. ;; something else, do it the slow way
  20727. (while (and (not (eobp))
  20728. (or first (> (funcall outline-level) level)))
  20729. (setq first nil)
  20730. (outline-next-heading)))
  20731. (unless to-heading
  20732. (if (memq (preceding-char) '(?\n ?\^M))
  20733. (progn
  20734. ;; Go to end of line before heading
  20735. (forward-char -1)
  20736. (if (memq (preceding-char) '(?\n ?\^M))
  20737. ;; leave blank line before heading
  20738. (forward-char -1))))))
  20739. (point))
  20740. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20741. "Use Org version in org-mode, for dramatic speed-up."
  20742. (if (derived-mode-p 'org-mode)
  20743. (progn
  20744. (org-end-of-subtree nil t)
  20745. (unless (eobp) (backward-char 1)))
  20746. ad-do-it))
  20747. (defun org-end-of-meta-data-and-drawers ()
  20748. "Jump to the first text after meta data and drawers in the current entry.
  20749. This will move over empty lines, lines with planning time stamps,
  20750. clocking lines, and drawers."
  20751. (org-back-to-heading t)
  20752. (let ((end (save-excursion (outline-next-heading) (point)))
  20753. (re (concat "\\(" org-drawer-regexp "\\)"
  20754. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20755. (forward-line 1)
  20756. (while (re-search-forward re end t)
  20757. (if (not (match-end 1))
  20758. ;; empty or planning line
  20759. (forward-line 1)
  20760. ;; a drawer, find the end
  20761. (re-search-forward "^[ \t]*:END:" end 'move)
  20762. (forward-line 1)))
  20763. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20764. (point)))
  20765. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20766. "Move forward to the arg'th subheading at same level as this one.
  20767. Stop at the first and last subheadings of a superior heading.
  20768. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20769. non-nil it will also look at invisible ones."
  20770. (interactive "p")
  20771. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20772. (outline-next-heading)
  20773. (org-at-heading-p)
  20774. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20775. (re (format "^\\*\\{1,%d\\} " level))
  20776. l)
  20777. (forward-char 1)
  20778. (while (> arg 0)
  20779. (while (and (re-search-forward re nil 'move)
  20780. (setq l (- (match-end 0) (match-beginning 0) 1))
  20781. (= l level)
  20782. (not invisible-ok)
  20783. (progn (backward-char 1) (outline-invisible-p)))
  20784. (if (< l level) (setq arg 1)))
  20785. (setq arg (1- arg)))
  20786. (beginning-of-line 1))))
  20787. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20788. "Move backward to the arg'th subheading at same level as this one.
  20789. Stop at the first and last subheadings of a superior heading."
  20790. (interactive "p")
  20791. (if (not (ignore-errors (org-back-to-heading)))
  20792. (goto-char (point-min))
  20793. (org-at-heading-p)
  20794. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20795. (re (format "^\\*\\{1,%d\\} " level))
  20796. l)
  20797. (while (> arg 0)
  20798. (while (and (re-search-backward re nil 'move)
  20799. (setq l (- (match-end 0) (match-beginning 0) 1))
  20800. (= l level)
  20801. (not invisible-ok)
  20802. (outline-invisible-p))
  20803. (if (< l level) (setq arg 1)))
  20804. (setq arg (1- arg))))))
  20805. (defun org-forward-element ()
  20806. "Move forward by one element.
  20807. Move to the next element at the same level, when possible."
  20808. (interactive)
  20809. (cond ((eobp) (error "Cannot move further down"))
  20810. ((org-with-limited-levels (org-at-heading-p))
  20811. (let ((origin (point)))
  20812. (org-forward-heading-same-level 1)
  20813. (unless (org-with-limited-levels (org-at-heading-p))
  20814. (goto-char origin)
  20815. (error "Cannot move further down"))))
  20816. (t
  20817. (let* ((elem (org-element-at-point))
  20818. (end (org-element-property :end elem))
  20819. (parent (org-element-property :parent elem)))
  20820. (if (and parent (= (org-element-property :contents-end parent) end))
  20821. (goto-char (org-element-property :end parent))
  20822. (goto-char end))))))
  20823. (defun org-backward-element ()
  20824. "Move backward by one element.
  20825. Move to the previous element at the same level, when possible."
  20826. (interactive)
  20827. (cond ((bobp) (error "Cannot move further up"))
  20828. ((org-with-limited-levels (org-at-heading-p))
  20829. ;; At an headline, move to the previous one, if any, or stay
  20830. ;; here.
  20831. (let ((origin (point)))
  20832. (org-backward-heading-same-level 1)
  20833. (unless (org-with-limited-levels (org-at-heading-p))
  20834. (goto-char origin)
  20835. (error "Cannot move further up"))))
  20836. (t
  20837. (let* ((trail (org-element-at-point 'keep-trail))
  20838. (elem (car trail))
  20839. (prev-elem (nth 1 trail))
  20840. (beg (org-element-property :begin elem)))
  20841. (cond
  20842. ;; Move to beginning of current element if point isn't
  20843. ;; there already.
  20844. ((/= (point) beg) (goto-char beg))
  20845. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20846. ((org-before-first-heading-p) (goto-char (point-min)))
  20847. (t (org-back-to-heading)))))))
  20848. (defun org-up-element ()
  20849. "Move to upper element."
  20850. (interactive)
  20851. (if (org-with-limited-levels (org-at-heading-p))
  20852. (unless (org-up-heading-safe) (error "No surrounding element"))
  20853. (let* ((elem (org-element-at-point))
  20854. (parent (org-element-property :parent elem)))
  20855. (if parent (goto-char (org-element-property :begin parent))
  20856. (if (org-with-limited-levels (org-before-first-heading-p))
  20857. (error "No surrounding element")
  20858. (org-with-limited-levels (org-back-to-heading)))))))
  20859. (defvar org-element-greater-elements)
  20860. (defun org-down-element ()
  20861. "Move to inner element."
  20862. (interactive)
  20863. (let ((element (org-element-at-point)))
  20864. (cond
  20865. ((memq (org-element-type element) '(plain-list table))
  20866. (goto-char (org-element-property :contents-begin element))
  20867. (forward-char))
  20868. ((memq (org-element-type element) org-element-greater-elements)
  20869. ;; If contents are hidden, first disclose them.
  20870. (when (org-element-property :hiddenp element) (org-cycle))
  20871. (goto-char (or (org-element-property :contents-begin element)
  20872. (error "No content for this element"))))
  20873. (t (error "No inner element")))))
  20874. (defun org-drag-element-backward ()
  20875. "Move backward element at point."
  20876. (interactive)
  20877. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20878. (let* ((trail (org-element-at-point 'keep-trail))
  20879. (elem (car trail))
  20880. (prev-elem (nth 1 trail)))
  20881. ;; Error out if no previous element or previous element is
  20882. ;; a parent of the current one.
  20883. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20884. (error "Cannot drag element backward")
  20885. (let ((pos (point)))
  20886. (org-element-swap-A-B prev-elem elem)
  20887. (goto-char (+ (org-element-property :begin prev-elem)
  20888. (- pos (org-element-property :begin elem)))))))))
  20889. (defun org-drag-element-forward ()
  20890. "Move forward element at point."
  20891. (interactive)
  20892. (let* ((pos (point))
  20893. (elem (org-element-at-point)))
  20894. (when (= (point-max) (org-element-property :end elem))
  20895. (error "Cannot drag element forward"))
  20896. (goto-char (org-element-property :end elem))
  20897. (let ((next-elem (org-element-at-point)))
  20898. (when (or (org-element-nested-p elem next-elem)
  20899. (and (eq (org-element-type next-elem) 'headline)
  20900. (not (eq (org-element-type elem) 'headline))))
  20901. (goto-char pos)
  20902. (error "Cannot drag element forward"))
  20903. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20904. ;; body's length (without final blanks) and by the length of
  20905. ;; blanks between ELEM and NEXT-ELEM.
  20906. (let ((size-next (- (save-excursion
  20907. (goto-char (org-element-property :end next-elem))
  20908. (skip-chars-backward " \r\t\n")
  20909. (forward-line)
  20910. ;; Small correction if buffer doesn't end
  20911. ;; with a newline character.
  20912. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20913. (org-element-property :begin next-elem)))
  20914. (size-blank (- (org-element-property :end elem)
  20915. (save-excursion
  20916. (goto-char (org-element-property :end elem))
  20917. (skip-chars-backward " \r\t\n")
  20918. (forward-line)
  20919. (point)))))
  20920. (org-element-swap-A-B elem next-elem)
  20921. (goto-char (+ pos size-next size-blank))))))
  20922. (defun org-mark-element ()
  20923. "Put point at beginning of this element, mark at end.
  20924. Interactively, if this command is repeated or (in Transient Mark
  20925. mode) if the mark is active, it marks the next element after the
  20926. ones already marked."
  20927. (interactive)
  20928. (let (deactivate-mark)
  20929. (if (and (org-called-interactively-p 'any)
  20930. (or (and (eq last-command this-command) (mark t))
  20931. (and transient-mark-mode mark-active)))
  20932. (set-mark
  20933. (save-excursion
  20934. (goto-char (mark))
  20935. (goto-char (org-element-property :end (org-element-at-point)))))
  20936. (let ((element (org-element-at-point)))
  20937. (end-of-line)
  20938. (push-mark (org-element-property :end element) t t)
  20939. (goto-char (org-element-property :begin element))))))
  20940. (defun org-narrow-to-element ()
  20941. "Narrow buffer to current element."
  20942. (interactive)
  20943. (let ((elem (org-element-at-point)))
  20944. (cond
  20945. ((eq (car elem) 'headline)
  20946. (narrow-to-region
  20947. (org-element-property :begin elem)
  20948. (org-element-property :end elem)))
  20949. ((memq (car elem) org-element-greater-elements)
  20950. (narrow-to-region
  20951. (org-element-property :contents-begin elem)
  20952. (org-element-property :contents-end elem)))
  20953. (t
  20954. (narrow-to-region
  20955. (org-element-property :begin elem)
  20956. (org-element-property :end elem))))))
  20957. (defun org-transpose-words ()
  20958. "Transpose words, using `org-mode' syntax table."
  20959. (interactive)
  20960. (with-syntax-table org-syntax-table
  20961. (call-interactively 'transpose-words)))
  20962. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20963. (defun org-transpose-element ()
  20964. "Transpose current and previous elements, keeping blank lines between.
  20965. Point is moved after both elements."
  20966. (interactive)
  20967. (org-skip-whitespace)
  20968. (let ((end (org-element-property :end (org-element-at-point))))
  20969. (org-drag-element-backward)
  20970. (goto-char end)))
  20971. (defun org-unindent-buffer ()
  20972. "Un-indent the visible part of the buffer.
  20973. Relative indentation (between items, inside blocks, etc.) isn't
  20974. modified."
  20975. (interactive)
  20976. (unless (eq major-mode 'org-mode)
  20977. (error "Cannot un-indent a buffer not in Org mode"))
  20978. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20979. unindent-tree ; For byte-compiler.
  20980. (unindent-tree
  20981. (function
  20982. (lambda (contents)
  20983. (mapc
  20984. (lambda (element)
  20985. (if (memq (org-element-type element) '(headline section))
  20986. (funcall unindent-tree (org-element-contents element))
  20987. (save-excursion
  20988. (save-restriction
  20989. (narrow-to-region
  20990. (org-element-property :begin element)
  20991. (org-element-property :end element))
  20992. (org-do-remove-indentation)))))
  20993. (reverse contents))))))
  20994. (funcall unindent-tree (org-element-contents parse-tree))))
  20995. (defun org-show-subtree ()
  20996. "Show everything after this heading at deeper levels."
  20997. (interactive)
  20998. (outline-flag-region
  20999. (point)
  21000. (save-excursion
  21001. (org-end-of-subtree t t))
  21002. nil))
  21003. (defun org-show-entry ()
  21004. "Show the body directly following this heading.
  21005. Show the heading too, if it is currently invisible."
  21006. (interactive)
  21007. (save-excursion
  21008. (condition-case nil
  21009. (progn
  21010. (org-back-to-heading t)
  21011. (outline-flag-region
  21012. (max (point-min) (1- (point)))
  21013. (save-excursion
  21014. (if (re-search-forward
  21015. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21016. (match-beginning 1)
  21017. (point-max)))
  21018. nil)
  21019. (org-cycle-hide-drawers 'children))
  21020. (error nil))))
  21021. (defun org-make-options-regexp (kwds &optional extra)
  21022. "Make a regular expression for keyword lines."
  21023. (concat
  21024. "^#\\+\\("
  21025. (mapconcat 'regexp-quote kwds "\\|")
  21026. (if extra (concat "\\|" extra))
  21027. "\\):[ \t]*\\(.*\\)"))
  21028. ;; Make isearch reveal the necessary context
  21029. (defun org-isearch-end ()
  21030. "Reveal context after isearch exits."
  21031. (when isearch-success ; only if search was successful
  21032. (if (featurep 'xemacs)
  21033. ;; Under XEmacs, the hook is run in the correct place,
  21034. ;; we directly show the context.
  21035. (org-show-context 'isearch)
  21036. ;; In Emacs the hook runs *before* restoring the overlays.
  21037. ;; So we have to use a one-time post-command-hook to do this.
  21038. ;; (Emacs 22 has a special variable, see function `org-mode')
  21039. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21040. isearch-mode-end-hook-quit)
  21041. ;; Only when the isearch was not quitted.
  21042. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21043. 'append 'local)))))
  21044. (defun org-isearch-post-command ()
  21045. "Remove self from hook, and show context."
  21046. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21047. (org-show-context 'isearch))
  21048. ;;;; Integration with and fixes for other packages
  21049. ;;; Imenu support
  21050. (defvar org-imenu-markers nil
  21051. "All markers currently used by Imenu.")
  21052. (make-variable-buffer-local 'org-imenu-markers)
  21053. (defun org-imenu-new-marker (&optional pos)
  21054. "Return a new marker for use by Imenu, and remember the marker."
  21055. (let ((m (make-marker)))
  21056. (move-marker m (or pos (point)))
  21057. (push m org-imenu-markers)
  21058. m))
  21059. (defun org-imenu-get-tree ()
  21060. "Produce the index for Imenu."
  21061. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21062. (setq org-imenu-markers nil)
  21063. (let* ((n org-imenu-depth)
  21064. (re (concat "^" (org-get-limited-outline-regexp)))
  21065. (subs (make-vector (1+ n) nil))
  21066. (last-level 0)
  21067. m level head)
  21068. (save-excursion
  21069. (save-restriction
  21070. (widen)
  21071. (goto-char (point-max))
  21072. (while (re-search-backward re nil t)
  21073. (setq level (org-reduced-level (funcall outline-level)))
  21074. (when (and (<= level n)
  21075. (looking-at org-complex-heading-regexp))
  21076. (setq head (org-link-display-format
  21077. (org-match-string-no-properties 4))
  21078. m (org-imenu-new-marker))
  21079. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21080. (if (>= level last-level)
  21081. (push (cons head m) (aref subs level))
  21082. (push (cons head (aref subs (1+ level))) (aref subs level))
  21083. (loop for i from (1+ level) to n do (aset subs i nil)))
  21084. (setq last-level level)))))
  21085. (aref subs 1)))
  21086. (eval-after-load "imenu"
  21087. '(progn
  21088. (add-hook 'imenu-after-jump-hook
  21089. (lambda ()
  21090. (if (derived-mode-p 'org-mode)
  21091. (org-show-context 'org-goto))))))
  21092. (defun org-link-display-format (link)
  21093. "Replace a link with either the description, or the link target
  21094. if no description is present"
  21095. (save-match-data
  21096. (if (string-match org-bracket-link-analytic-regexp link)
  21097. (replace-match (if (match-end 5)
  21098. (match-string 5 link)
  21099. (concat (match-string 1 link)
  21100. (match-string 3 link)))
  21101. nil t link)
  21102. link)))
  21103. (defun org-toggle-link-display ()
  21104. "Toggle the literal or descriptive display of links."
  21105. (interactive)
  21106. (if org-descriptive-links
  21107. (progn (org-remove-from-invisibility-spec '(org-link))
  21108. (org-restart-font-lock)
  21109. (setq org-descriptive-links nil))
  21110. (progn (add-to-invisibility-spec '(org-link))
  21111. (org-restart-font-lock)
  21112. (setq org-descriptive-links t))))
  21113. ;; Speedbar support
  21114. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21115. "Overlay marking the agenda restriction line in speedbar.")
  21116. (overlay-put org-speedbar-restriction-lock-overlay
  21117. 'face 'org-agenda-restriction-lock)
  21118. (overlay-put org-speedbar-restriction-lock-overlay
  21119. 'help-echo "Agendas are currently limited to this item.")
  21120. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21121. (defun org-speedbar-set-agenda-restriction ()
  21122. "Restrict future agenda commands to the location at point in speedbar.
  21123. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21124. (interactive)
  21125. (require 'org-agenda)
  21126. (let (p m tp np dir txt)
  21127. (cond
  21128. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21129. 'org-imenu t))
  21130. (setq m (get-text-property p 'org-imenu-marker))
  21131. (with-current-buffer (marker-buffer m)
  21132. (goto-char m)
  21133. (org-agenda-set-restriction-lock 'subtree)))
  21134. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21135. 'speedbar-function 'speedbar-find-file))
  21136. (setq tp (previous-single-property-change
  21137. (1+ p) 'speedbar-function)
  21138. np (next-single-property-change
  21139. tp 'speedbar-function)
  21140. dir (speedbar-line-directory)
  21141. txt (buffer-substring-no-properties (or tp (point-min))
  21142. (or np (point-max))))
  21143. (with-current-buffer (find-file-noselect
  21144. (let ((default-directory dir))
  21145. (expand-file-name txt)))
  21146. (unless (derived-mode-p 'org-mode)
  21147. (error "Cannot restrict to non-Org-mode file"))
  21148. (org-agenda-set-restriction-lock 'file)))
  21149. (t (error "Don't know how to restrict Org-mode's agenda")))
  21150. (move-overlay org-speedbar-restriction-lock-overlay
  21151. (point-at-bol) (point-at-eol))
  21152. (setq current-prefix-arg nil)
  21153. (org-agenda-maybe-redo)))
  21154. (eval-after-load "speedbar"
  21155. '(progn
  21156. (speedbar-add-supported-extension ".org")
  21157. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21158. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21159. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21160. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21161. (add-hook 'speedbar-visiting-tag-hook
  21162. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21163. ;;; Fixes and Hacks for problems with other packages
  21164. ;; Make flyspell not check words in links, to not mess up our keymap
  21165. (defun org-mode-flyspell-verify ()
  21166. "Don't let flyspell put overlays at active buttons, or on
  21167. {todo,all-time,additional-option-like}-keywords."
  21168. (let ((pos (max (1- (point)) (point-min)))
  21169. (word (thing-at-point 'word)))
  21170. (and (not (get-text-property pos 'keymap))
  21171. (not (get-text-property pos 'org-no-flyspell))
  21172. (not (member word org-todo-keywords-1))
  21173. (not (member word org-all-time-keywords))
  21174. (not (member word org-options-keywords))
  21175. (not (member word (mapcar 'car org-startup-options)))
  21176. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  21177. (defun org-remove-flyspell-overlays-in (beg end)
  21178. "Remove flyspell overlays in region."
  21179. (and (org-bound-and-true-p flyspell-mode)
  21180. (fboundp 'flyspell-delete-region-overlays)
  21181. (flyspell-delete-region-overlays beg end))
  21182. (add-text-properties beg end '(org-no-flyspell t)))
  21183. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21184. (eval-after-load "bookmark"
  21185. '(if (boundp 'bookmark-after-jump-hook)
  21186. ;; We can use the hook
  21187. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21188. ;; Hook not available, use advice
  21189. (defadvice bookmark-jump (after org-make-visible activate)
  21190. "Make the position visible."
  21191. (org-bookmark-jump-unhide))))
  21192. ;; Make sure saveplace shows the location if it was hidden
  21193. (eval-after-load "saveplace"
  21194. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21195. "Make the position visible."
  21196. (org-bookmark-jump-unhide)))
  21197. ;; Make sure ecb shows the location if it was hidden
  21198. (eval-after-load "ecb"
  21199. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21200. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21201. (if (derived-mode-p 'org-mode)
  21202. (org-show-context))))
  21203. (defun org-bookmark-jump-unhide ()
  21204. "Unhide the current position, to show the bookmark location."
  21205. (and (derived-mode-p 'org-mode)
  21206. (or (outline-invisible-p)
  21207. (save-excursion (goto-char (max (point-min) (1- (point))))
  21208. (outline-invisible-p)))
  21209. (org-show-context 'bookmark-jump)))
  21210. ;; Make session.el ignore our circular variable
  21211. (eval-after-load "session"
  21212. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21213. ;;;; Experimental code
  21214. (defun org-closed-in-range ()
  21215. "Sparse tree of items closed in a certain time range.
  21216. Still experimental, may disappear in the future."
  21217. (interactive)
  21218. ;; Get the time interval from the user.
  21219. (let* ((time1 (org-float-time
  21220. (org-read-date nil 'to-time nil "Starting date: ")))
  21221. (time2 (org-float-time
  21222. (org-read-date nil 'to-time nil "End date:")))
  21223. ;; callback function
  21224. (callback (lambda ()
  21225. (let ((time
  21226. (org-float-time
  21227. (apply 'encode-time
  21228. (org-parse-time-string
  21229. (match-string 1))))))
  21230. ;; check if time in interval
  21231. (and (>= time time1) (<= time time2))))))
  21232. ;; make tree, check each match with the callback
  21233. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21234. ;;;; Finish up
  21235. (provide 'org)
  21236. (run-hooks 'org-load-hook)
  21237. ;;; org.el ends here