org.el 951 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147
  1. ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode 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. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar-local org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. ;;;; Require other packages
  65. (require 'cl-lib)
  66. (eval-when-compile (require 'gnus-sum))
  67. (require 'calendar)
  68. (require 'find-func)
  69. (require 'format-spec)
  70. (or (equal this-command 'eval-buffer)
  71. (condition-case nil
  72. (load (concat (file-name-directory load-file-name)
  73. "org-loaddefs.el")
  74. nil t t t)
  75. (error
  76. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  77. (sit-for 3)
  78. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  79. (sit-for 3))))
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  83. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  84. ;;
  85. ;; In Org buffers, the value of `outline-regexp' is that of
  86. ;; `org-outline-regexp'. The only function still directly relying on
  87. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  88. ;; job when `orgstruct-mode' is active.
  89. (defvar org-outline-regexp "\\*+ "
  90. "Regexp to match Org headlines.")
  91. (defvar org-outline-regexp-bol "^\\*+ "
  92. "Regexp to match Org headlines.
  93. This is similar to `org-outline-regexp' but additionally makes
  94. sure that we are at the beginning of the line.")
  95. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  96. "Matches a headline, putting stars and text into groups.
  97. Stars are put in group 1 and the trimmed body in group 2.")
  98. (declare-function calendar-check-holidays "holidays" (&optional date))
  99. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  100. (declare-function org-add-archive-files "org-archive" (files))
  101. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  102. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  103. (declare-function org-agenda-redo "org-agenda" (&optional all))
  104. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  105. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  106. (declare-function org-beamer-mode "ox-beamer" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  109. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  110. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  111. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  112. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  113. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  114. (declare-function org-clock-update-time-maybe "org-clock" ())
  115. (declare-function org-clocktable-shift "org-clock" (dir n))
  116. (declare-function org-element-at-point "org-element" ())
  117. (declare-function org-element-cache-refresh "org-element" (pos))
  118. (declare-function org-element-cache-reset "org-element" (&optional all))
  119. (declare-function org-element-contents "org-element" (element))
  120. (declare-function org-element-context "org-element" (&optional element))
  121. (declare-function org-element-copy "org-element" (datum))
  122. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  123. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  124. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  125. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  126. (declare-function org-element-property "org-element" (property element))
  127. (declare-function org-element-put-property "org-element" (element property value))
  128. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  129. (declare-function org-element-type "org-element" (element))
  130. (declare-function org-element-update-syntax "org-element" ())
  131. (declare-function org-id-find-id-file "org-id" (id))
  132. (declare-function org-id-get-create "org-id" (&optional force))
  133. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  134. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  135. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  136. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  137. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  138. (declare-function org-table-align "org-table" ())
  139. (declare-function org-table-begin "org-table" (&optional table-type))
  140. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  143. (declare-function org-table-edit-field "org-table" (arg))
  144. (declare-function org-table-end "org-table" (&optional table-type))
  145. (declare-function org-table-end-of-field "org-table" (&optional n))
  146. (declare-function org-table-insert-row "org-table" (&optional arg))
  147. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  148. (declare-function org-table-maybe-eval-formula "org-table" ())
  149. (declare-function org-table-maybe-recalculate-line "org-table" ())
  150. (declare-function org-table-next-row "org-table" ())
  151. (declare-function org-table-paste-rectangle "org-table" ())
  152. (declare-function org-table-wrap-region "org-table" (arg))
  153. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  154. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  155. (declare-function orgtbl-mode "org-table" (&optional arg))
  156. (defsubst org-uniquify (list)
  157. "Non-destructively remove duplicate elements from LIST."
  158. (let ((res (copy-sequence list))) (delete-dups res)))
  159. (defsubst org-get-at-bol (property)
  160. "Get text property PROPERTY at the beginning of line."
  161. (get-text-property (point-at-bol) property))
  162. (defsubst org-trim (s)
  163. "Remove whitespace at the beginning and the end of string S."
  164. (replace-regexp-in-string
  165. "\\`[ \t\n\r]+" ""
  166. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  167. ;; load languages based on value of `org-babel-load-languages'
  168. (defvar org-babel-load-languages)
  169. ;;;###autoload
  170. (defun org-babel-do-load-languages (sym value)
  171. "Load the languages defined in `org-babel-load-languages'."
  172. (set-default sym value)
  173. (dolist (pair org-babel-load-languages)
  174. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  175. (if active
  176. (require (intern (concat "ob-" lang)))
  177. (funcall 'fmakunbound
  178. (intern (concat "org-babel-execute:" lang)))
  179. (funcall 'fmakunbound
  180. (intern (concat "org-babel-expand-body:" lang)))))))
  181. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  182. ;;;###autoload
  183. (defun org-babel-load-file (file &optional compile)
  184. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  185. This function exports the source code using `org-babel-tangle'
  186. and then loads the resulting file using `load-file'. With prefix
  187. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  188. file to byte-code before it is loaded."
  189. (interactive "fFile to load: \nP")
  190. (let* ((age (lambda (file)
  191. (float-time
  192. (time-subtract (current-time)
  193. (nth 5 (or (file-attributes (file-truename file))
  194. (file-attributes file)))))))
  195. (base-name (file-name-sans-extension file))
  196. (exported-file (concat base-name ".el")))
  197. ;; tangle if the org-mode file is newer than the elisp file
  198. (unless (and (file-exists-p exported-file)
  199. (> (funcall age file) (funcall age exported-file)))
  200. ;; Tangle-file traversal returns reversed list of tangled files
  201. ;; and we want to evaluate the first target.
  202. (setq exported-file
  203. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  204. (message "%s %s"
  205. (if compile
  206. (progn (byte-compile-file exported-file 'load)
  207. "Compiled and loaded")
  208. (progn (load-file exported-file) "Loaded"))
  209. exported-file)))
  210. (defcustom org-babel-load-languages '((emacs-lisp . t))
  211. "Languages which can be evaluated in Org-mode buffers.
  212. This list can be used to load support for any of the languages
  213. below, note that each language will depend on a different set of
  214. system executables and/or Emacs modes. When a language is
  215. \"loaded\", then code blocks in that language can be evaluated
  216. with `org-babel-execute-src-block' bound by default to C-c
  217. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  218. be set to remove code block evaluation from the C-c C-c
  219. keybinding. By default only Emacs Lisp (which has no
  220. requirements) is loaded."
  221. :group 'org-babel
  222. :set 'org-babel-do-load-languages
  223. :version "24.1"
  224. :type '(alist :tag "Babel Languages"
  225. :key-type
  226. (choice
  227. (const :tag "Awk" awk)
  228. (const :tag "C" C)
  229. (const :tag "R" R)
  230. (const :tag "Asymptote" asymptote)
  231. (const :tag "Calc" calc)
  232. (const :tag "Clojure" clojure)
  233. (const :tag "CSS" css)
  234. (const :tag "Ditaa" ditaa)
  235. (const :tag "Dot" dot)
  236. (const :tag "Emacs Lisp" emacs-lisp)
  237. (const :tag "Forth" forth)
  238. (const :tag "Fortran" fortran)
  239. (const :tag "Gnuplot" gnuplot)
  240. (const :tag "Haskell" haskell)
  241. (const :tag "IO" io)
  242. (const :tag "J" J)
  243. (const :tag "Java" java)
  244. (const :tag "Javascript" js)
  245. (const :tag "LaTeX" latex)
  246. (const :tag "Ledger" ledger)
  247. (const :tag "Lilypond" lilypond)
  248. (const :tag "Lisp" lisp)
  249. (const :tag "Makefile" makefile)
  250. (const :tag "Maxima" maxima)
  251. (const :tag "Matlab" matlab)
  252. (const :tag "Mscgen" mscgen)
  253. (const :tag "Ocaml" ocaml)
  254. (const :tag "Octave" octave)
  255. (const :tag "Org" org)
  256. (const :tag "Perl" perl)
  257. (const :tag "Pico Lisp" picolisp)
  258. (const :tag "PlantUML" plantuml)
  259. (const :tag "Python" python)
  260. (const :tag "Ruby" ruby)
  261. (const :tag "Sass" sass)
  262. (const :tag "Scala" scala)
  263. (const :tag "Scheme" scheme)
  264. (const :tag "Screen" screen)
  265. (const :tag "Shell Script" shell)
  266. (const :tag "Shen" shen)
  267. (const :tag "Sql" sql)
  268. (const :tag "Sqlite" sqlite)
  269. (const :tag "Stan" stan)
  270. (const :tag "ebnf2ps" ebnf2ps))
  271. :value-type (boolean :tag "Activate" :value t)))
  272. ;;;; Customization variables
  273. (defcustom org-clone-delete-id nil
  274. "Remove ID property of clones of a subtree.
  275. When non-nil, clones of a subtree don't inherit the ID property.
  276. Otherwise they inherit the ID property with a new unique
  277. identifier."
  278. :type 'boolean
  279. :version "24.1"
  280. :group 'org-id)
  281. ;;; Version
  282. (org-check-version)
  283. ;;;###autoload
  284. (defun org-version (&optional here full message)
  285. "Show the org-mode version.
  286. Interactively, or when MESSAGE is non-nil, show it in echo area.
  287. With prefix argument, or when HERE is non-nil, insert it at point.
  288. In non-interactive uses, a reduced version string is output unless
  289. FULL is given."
  290. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  291. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  292. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  293. (load-suffixes (list ".el"))
  294. (org-install-dir
  295. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  296. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  297. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  298. (let* ((load-suffixes save-load-suffixes)
  299. (release (org-release))
  300. (git-version (org-git-version))
  301. (version (format "Org-mode version %s (%s @ %s)"
  302. release
  303. git-version
  304. (if org-install-dir
  305. (if (string= org-dir org-install-dir)
  306. org-install-dir
  307. (concat "mixed installation! "
  308. org-install-dir
  309. " and "
  310. org-dir))
  311. "org-loaddefs.el can not be found!")))
  312. (version1 (if full version release)))
  313. (when here (insert version1))
  314. (when message (message "%s" version1))
  315. version1)))
  316. (defconst org-version (org-version))
  317. ;;; Syntax Constants
  318. ;;;; Block
  319. (defconst org-block-regexp
  320. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  321. "Regular expression for hiding blocks.")
  322. (defconst org-dblock-start-re
  323. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  324. "Matches the start line of a dynamic block, with parameters.")
  325. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  326. "Matches the end of a dynamic block.")
  327. ;;;; Clock and Planning
  328. (defconst org-clock-string "CLOCK:"
  329. "String used as prefix for timestamps clocking work hours on an item.")
  330. (defvar org-closed-string "CLOSED:"
  331. "String used as the prefix for timestamps logging closing a TODO entry.")
  332. (defvar org-deadline-string "DEADLINE:"
  333. "String to mark deadline entries.
  334. A deadline is this string, followed by a time stamp. Should be a word,
  335. terminated by a colon. You can insert a schedule keyword and
  336. a timestamp with \\[org-deadline].")
  337. (defvar org-scheduled-string "SCHEDULED:"
  338. "String to mark scheduled TODO entries.
  339. A schedule is this string, followed by a time stamp. Should be a word,
  340. terminated by a colon. You can insert a schedule keyword and
  341. a timestamp with \\[org-schedule].")
  342. (defconst org-ds-keyword-length
  343. (+ 2
  344. (apply #'max
  345. (mapcar #'length
  346. (list org-deadline-string org-scheduled-string
  347. org-clock-string org-closed-string))))
  348. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  349. (defconst org-planning-line-re
  350. (concat "^[ \t]*"
  351. (regexp-opt
  352. (list org-closed-string org-deadline-string org-scheduled-string)
  353. t))
  354. "Matches a line with planning info.
  355. Matched keyword is in group 1.")
  356. (defconst org-clock-line-re
  357. (concat "^[ \t]*" org-clock-string)
  358. "Matches a line with clock info.")
  359. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  360. "Matches the DEADLINE keyword.")
  361. (defconst org-deadline-time-regexp
  362. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  363. "Matches the DEADLINE keyword together with a time stamp.")
  364. (defconst org-deadline-time-hour-regexp
  365. (concat "\\<" org-deadline-string
  366. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  367. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  368. (defconst org-deadline-line-regexp
  369. (concat "\\<\\(" org-deadline-string "\\).*")
  370. "Matches the DEADLINE keyword and the rest of the line.")
  371. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  372. "Matches the SCHEDULED keyword.")
  373. (defconst org-scheduled-time-regexp
  374. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  375. "Matches the SCHEDULED keyword together with a time stamp.")
  376. (defconst org-scheduled-time-hour-regexp
  377. (concat "\\<" org-scheduled-string
  378. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  379. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  380. (defconst org-closed-time-regexp
  381. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  382. "Matches the CLOSED keyword together with a time stamp.")
  383. (defconst org-keyword-time-regexp
  384. (concat "\\<"
  385. (regexp-opt
  386. (list org-scheduled-string org-deadline-string org-closed-string
  387. org-clock-string)
  388. t)
  389. " *[[<]\\([^]>]+\\)[]>]")
  390. "Matches any of the 4 keywords, together with the time stamp.")
  391. (defconst org-keyword-time-not-clock-regexp
  392. (concat
  393. "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string) t)
  396. " *[[<]\\([^]>]+\\)[]>]")
  397. "Matches any of the 3 keywords, together with the time stamp.")
  398. (defconst org-maybe-keyword-time-regexp
  399. (concat "\\(\\<"
  400. (regexp-opt
  401. (list org-scheduled-string org-deadline-string org-closed-string
  402. org-clock-string)
  403. t)
  404. "\\)?"
  405. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  406. "\\|"
  407. "<%%([^\r\n>]*>\\)")
  408. "Matches a timestamp, possibly preceded by a keyword.")
  409. (defconst org-all-time-keywords
  410. (mapcar (lambda (w) (substring w 0 -1))
  411. (list org-scheduled-string org-deadline-string
  412. org-clock-string org-closed-string))
  413. "List of time keywords.")
  414. ;;;; Drawer
  415. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  416. "Matches first or last line of a hidden block.
  417. Group 1 contains drawer's name or \"END\".")
  418. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  419. "Regular expression matching the first line of a property drawer.")
  420. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  421. "Regular expression matching the last line of a property drawer.")
  422. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  423. "Regular expression matching the first line of a clock drawer.")
  424. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  425. "Regular expression matching the last line of a clock drawer.")
  426. (defconst org-property-drawer-re
  427. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  428. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  429. "[ \t]*:END:[ \t]*$")
  430. "Matches an entire property drawer.")
  431. (defconst org-clock-drawer-re
  432. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  433. org-clock-drawer-end-re "\\)\n?")
  434. "Matches an entire clock drawer.")
  435. ;;;; Headline
  436. (defconst org-heading-keyword-regexp-format
  437. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  438. "Printf format for a regexp matching a headline with some keyword.
  439. This regexp will match the headline of any node which has the
  440. exact keyword that is put into the format. The keyword isn't in
  441. any group by default, but the stars and the body are.")
  442. (defconst org-heading-keyword-maybe-regexp-format
  443. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  444. "Printf format for a regexp matching a headline, possibly with some keyword.
  445. This regexp can match any headline with the specified keyword, or
  446. without a keyword. The keyword isn't in any group by default,
  447. but the stars and the body are.")
  448. (defconst org-archive-tag "ARCHIVE"
  449. "The tag that marks a subtree as archived.
  450. An archived subtree does not open during visibility cycling, and does
  451. not contribute to the agenda listings.")
  452. (defconst org-comment-string "COMMENT"
  453. "Entries starting with this keyword will never be exported.
  454. An entry can be toggled between COMMENT and normal with
  455. \\[org-toggle-comment].")
  456. ;;;; LaTeX Environments and Fragments
  457. (defconst org-latex-regexps
  458. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  459. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  460. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  461. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  462. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  463. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  464. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  465. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  466. "Regular expressions for matching embedded LaTeX.")
  467. ;;;; Node Property
  468. (defconst org-effort-property "Effort"
  469. "The property that is being used to keep track of effort estimates.
  470. Effort estimates given in this property need to have the format H:MM.")
  471. ;;;; Table
  472. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  473. "Detect an org-type or table-type table.")
  474. (defconst org-table-line-regexp "^[ \t]*|"
  475. "Detect an org-type table line.")
  476. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  477. "Detect an org-type table line.")
  478. (defconst org-table-hline-regexp "^[ \t]*|-"
  479. "Detect an org-type table hline.")
  480. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  481. "Detect a table-type table hline.")
  482. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  483. "Detect the first line outside a table when searching from within it.
  484. This works for both table types.")
  485. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  486. "Detect a #+TBLFM line.")
  487. ;;;; Timestamp
  488. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  489. "Regular expression for fast time stamp matching.")
  490. (defconst org-ts-regexp-inactive
  491. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  492. "Regular expression for fast inactive time stamp matching.")
  493. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  494. "Regular expression for fast time stamp matching.")
  495. (defconst org-ts-regexp0
  496. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  497. "Regular expression matching time strings for analysis.
  498. This one does not require the space after the date, so it can be used
  499. on a string that terminates immediately after the date.")
  500. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  501. "Regular expression matching time strings for analysis.")
  502. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  503. "Regular expression matching time stamps, with groups.")
  504. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  505. "Regular expression matching time stamps (also [..]), with groups.")
  506. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  507. "Regular expression matching a time stamp range.")
  508. (defconst org-tr-regexp-both
  509. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  510. "Regular expression matching a time stamp range.")
  511. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  512. org-ts-regexp "\\)?")
  513. "Regular expression matching a time stamp or time stamp range.")
  514. (defconst org-tsr-regexp-both
  515. (concat org-ts-regexp-both "\\(--?-?"
  516. org-ts-regexp-both "\\)?")
  517. "Regular expression matching a time stamp or time stamp range.
  518. The time stamps may be either active or inactive.")
  519. (defconst org-repeat-re
  520. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  521. "Regular expression for specifying repeated events.
  522. After a match, group 1 contains the repeat expression.")
  523. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  524. "Formats for `format-time-string' which are used for time stamps.")
  525. ;;; The custom variables
  526. (defgroup org nil
  527. "Outline-based notes management and organizer."
  528. :tag "Org"
  529. :group 'outlines
  530. :group 'calendar)
  531. (defcustom org-mode-hook nil
  532. "Mode hook for Org-mode, run after the mode was turned on."
  533. :group 'org
  534. :type 'hook)
  535. (defcustom org-load-hook nil
  536. "Hook that is run after org.el has been loaded."
  537. :group 'org
  538. :type 'hook)
  539. (defcustom org-log-buffer-setup-hook nil
  540. "Hook that is run after an Org log buffer is created."
  541. :group 'org
  542. :version "24.1"
  543. :type 'hook)
  544. (defvar org-modules) ; defined below
  545. (defvar org-modules-loaded nil
  546. "Have the modules been loaded already?")
  547. (defun org-load-modules-maybe (&optional force)
  548. "Load all extensions listed in `org-modules'."
  549. (when (or force (not org-modules-loaded))
  550. (dolist (ext org-modules)
  551. (condition-case nil (require ext)
  552. (error (message "Problems while trying to load feature `%s'" ext))))
  553. (setq org-modules-loaded t)))
  554. (defun org-set-modules (var value)
  555. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  556. (set var value)
  557. (when (featurep 'org)
  558. (org-load-modules-maybe 'force)
  559. (org-element-cache-reset 'all)))
  560. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  561. "Modules that should always be loaded together with org.el.
  562. If a description starts with <C>, the file is not part of Emacs
  563. and loading it will require that you have downloaded and properly
  564. installed the Org mode distribution.
  565. You can also use this system to load external packages (i.e. neither Org
  566. core modules, nor modules from the CONTRIB directory). Just add symbols
  567. to the end of the list. If the package is called org-xyz.el, then you need
  568. to add the symbol `xyz', and the package must have a call to:
  569. (provide \\='org-xyz)
  570. For export specific modules, see also `org-export-backends'."
  571. :group 'org
  572. :set 'org-set-modules
  573. :version "24.4"
  574. :package-version '(Org . "8.0")
  575. :type
  576. '(set :greedy t
  577. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  578. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  579. (const :tag " crypt: Encryption of subtrees" org-crypt)
  580. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  581. (const :tag " docview: Links to doc-view buffers" org-docview)
  582. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  583. (const :tag " habit: Track your consistency with habits" org-habit)
  584. (const :tag " id: Global IDs for identifying entries" org-id)
  585. (const :tag " info: Links to Info nodes" org-info)
  586. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  587. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  588. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  589. (const :tag " mouse: Additional mouse support" org-mouse)
  590. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  591. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  592. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  593. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  594. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  595. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  596. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  597. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  598. (const :tag "C collector: Collect properties into tables" org-collector)
  599. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  600. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  601. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  602. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  603. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  604. (const :tag "C eval: Include command output as text" org-eval)
  605. (const :tag "C eww: Store link to url of eww" org-eww)
  606. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  607. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  608. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  609. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  610. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  611. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  612. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  613. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  614. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  615. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  616. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  617. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  618. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  619. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  620. (const :tag "C registry: A registry for Org-mode links" org-registry)
  621. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  622. (const :tag "C secretary: Team management with org-mode" org-secretary)
  623. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  624. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  625. (const :tag "C track: Keep up with Org-mode development" org-track)
  626. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  627. (const :tag "C vm: Links to VM folders/messages" org-vm)
  628. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  629. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  630. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  631. (defvar org-export-registered-backends) ; From ox.el.
  632. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  633. (declare-function org-export-backend-name "ox" (backend))
  634. (defcustom org-export-backends '(ascii html icalendar latex)
  635. "List of export back-ends that should be always available.
  636. If a description starts with <C>, the file is not part of Emacs
  637. and loading it will require that you have downloaded and properly
  638. installed the Org mode distribution.
  639. Unlike to `org-modules', libraries in this list will not be
  640. loaded along with Org, but only once the export framework is
  641. needed.
  642. This variable needs to be set before org.el is loaded. If you
  643. need to make a change while Emacs is running, use the customize
  644. interface or run the following code, where VAL stands for the new
  645. value of the variable, after updating it:
  646. (progn
  647. (setq org-export-registered-backends
  648. (cl-remove-if-not
  649. (lambda (backend)
  650. (let ((name (org-export-backend-name backend)))
  651. (or (memq name val)
  652. (catch \\='parentp
  653. (dolist (b val)
  654. (and (org-export-derived-backend-p b name)
  655. (throw \\='parentp t)))))))
  656. org-export-registered-backends))
  657. (let ((new-list (mapcar #\\='org-export-backend-name
  658. org-export-registered-backends)))
  659. (dolist (backend val)
  660. (cond
  661. ((not (load (format \"ox-%s\" backend) t t))
  662. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  663. backend))
  664. ((not (memq backend new-list)) (push backend new-list))))
  665. (set-default \\='org-export-backends new-list)))
  666. Adding a back-end to this list will also pull the back-end it
  667. depends on, if any."
  668. :group 'org
  669. :group 'org-export
  670. :version "24.4"
  671. :package-version '(Org . "8.0")
  672. :initialize 'custom-initialize-set
  673. :set (lambda (var val)
  674. (if (not (featurep 'ox)) (set-default var val)
  675. ;; Any back-end not required anymore (not present in VAL and not
  676. ;; a parent of any back-end in the new value) is removed from the
  677. ;; list of registered back-ends.
  678. (setq org-export-registered-backends
  679. (cl-remove-if-not
  680. (lambda (backend)
  681. (let ((name (org-export-backend-name backend)))
  682. (or (memq name val)
  683. (catch 'parentp
  684. (dolist (b val)
  685. (and (org-export-derived-backend-p b name)
  686. (throw 'parentp t)))))))
  687. org-export-registered-backends))
  688. ;; Now build NEW-LIST of both new back-ends and required
  689. ;; parents.
  690. (let ((new-list (mapcar #'org-export-backend-name
  691. org-export-registered-backends)))
  692. (dolist (backend val)
  693. (cond
  694. ((not (load (format "ox-%s" backend) t t))
  695. (message "Problems while trying to load export back-end `%s'"
  696. backend))
  697. ((not (memq backend new-list)) (push backend new-list))))
  698. ;; Set VAR to that list with fixed dependencies.
  699. (set-default var new-list))))
  700. :type '(set :greedy t
  701. (const :tag " ascii Export buffer to ASCII format" ascii)
  702. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  703. (const :tag " html Export buffer to HTML format" html)
  704. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  705. (const :tag " latex Export buffer to LaTeX format" latex)
  706. (const :tag " man Export buffer to MAN format" man)
  707. (const :tag " md Export buffer to Markdown format" md)
  708. (const :tag " odt Export buffer to ODT format" odt)
  709. (const :tag " org Export buffer to Org format" org)
  710. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  711. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  712. (const :tag "C deck Export buffer to deck.js presentations" deck)
  713. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  714. (const :tag "C groff Export buffer to Groff format" groff)
  715. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  716. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  717. (const :tag "C s5 Export buffer to s5 presentations" s5)
  718. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  719. (eval-after-load 'ox
  720. '(dolist (backend org-export-backends)
  721. (condition-case nil (require (intern (format "ox-%s" backend)))
  722. (error (message "Problems while trying to load export back-end `%s'"
  723. backend)))))
  724. (defcustom org-support-shift-select nil
  725. "Non-nil means make shift-cursor commands select text when possible.
  726. \\<org-mode-map>\
  727. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  728. start selecting a region, or enlarge regions started in this way.
  729. In Org-mode, in special contexts, these same keys are used for
  730. other purposes, important enough to compete with shift selection.
  731. Org tries to balance these needs by supporting `shift-select-mode'
  732. outside these special contexts, under control of this variable.
  733. The default of this variable is nil, to avoid confusing behavior. Shifted
  734. cursor keys will then execute Org commands in the following contexts:
  735. - on a headline, changing TODO state (left/right) and priority (up/down)
  736. - on a time stamp, changing the time
  737. - in a plain list item, changing the bullet type
  738. - in a property definition line, switching between allowed values
  739. - in the BEGIN line of a clock table (changing the time block).
  740. Outside these contexts, the commands will throw an error.
  741. When this variable is t and the cursor is not in a special
  742. context, Org-mode will support shift-selection for making and
  743. enlarging regions. To make this more effective, the bullet
  744. cycling will no longer happen anywhere in an item line, but only
  745. if the cursor is exactly on the bullet.
  746. If you set this variable to the symbol `always', then the keys
  747. will not be special in headlines, property lines, and item lines,
  748. to make shift selection work there as well. If this is what you
  749. want, you can use the following alternative commands:
  750. `\\[org-todo]' and `\\[org-priority]' \
  751. to change TODO state and priority,
  752. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  753. can be used to switch TODO sets,
  754. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  755. and properties can be edited by hand or in column view.
  756. However, when the cursor is on a timestamp, shift-cursor commands
  757. will still edit the time stamp - this is just too good to give up.
  758. XEmacs user should have this variable set to nil, because
  759. `shift-select-mode' is in Emacs 23 or later only."
  760. :group 'org
  761. :type '(choice
  762. (const :tag "Never" nil)
  763. (const :tag "When outside special context" t)
  764. (const :tag "Everywhere except timestamps" always)))
  765. (defcustom org-loop-over-headlines-in-active-region nil
  766. "Shall some commands act upon headlines in the active region?
  767. When set to t, some commands will be performed in all headlines
  768. within the active region.
  769. When set to `start-level', some commands will be performed in all
  770. headlines within the active region, provided that these headlines
  771. are of the same level than the first one.
  772. When set to a string, those commands will be performed on the
  773. matching headlines within the active region. Such string must be
  774. a tags/property/todo match as it is used in the agenda tags view.
  775. The list of commands is: `org-schedule', `org-deadline',
  776. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  777. `org-archive-to-archive-sibling'. The archiving commands skip
  778. already archived entries."
  779. :type '(choice (const :tag "Don't loop" nil)
  780. (const :tag "All headlines in active region" t)
  781. (const :tag "In active region, headlines at the same level than the first one" start-level)
  782. (string :tag "Tags/Property/Todo matcher"))
  783. :version "24.1"
  784. :group 'org-todo
  785. :group 'org-archive)
  786. (defgroup org-startup nil
  787. "Options concerning startup of Org-mode."
  788. :tag "Org Startup"
  789. :group 'org)
  790. (defcustom org-startup-folded t
  791. "Non-nil means entering Org-mode will switch to OVERVIEW.
  792. This can also be configured on a per-file basis by adding one of
  793. the following lines anywhere in the buffer:
  794. #+STARTUP: fold (or `overview', this is equivalent)
  795. #+STARTUP: nofold (or `showall', this is equivalent)
  796. #+STARTUP: content
  797. #+STARTUP: showeverything
  798. By default, this option is ignored when Org opens agenda files
  799. for the first time. If you want the agenda to honor the startup
  800. option, set `org-agenda-inhibit-startup' to nil."
  801. :group 'org-startup
  802. :type '(choice
  803. (const :tag "nofold: show all" nil)
  804. (const :tag "fold: overview" t)
  805. (const :tag "content: all headlines" content)
  806. (const :tag "show everything, even drawers" showeverything)))
  807. (defcustom org-startup-truncated t
  808. "Non-nil means entering Org-mode will set `truncate-lines'.
  809. This is useful since some lines containing links can be very long and
  810. uninteresting. Also tables look terrible when wrapped."
  811. :group 'org-startup
  812. :type 'boolean)
  813. (defcustom org-startup-indented nil
  814. "Non-nil means turn on `org-indent-mode' on startup.
  815. This can also be configured on a per-file basis by adding one of
  816. the following lines anywhere in the buffer:
  817. #+STARTUP: indent
  818. #+STARTUP: noindent"
  819. :group 'org-structure
  820. :type '(choice
  821. (const :tag "Not" nil)
  822. (const :tag "Globally (slow on startup in large files)" t)))
  823. (defcustom org-use-sub-superscripts t
  824. "Non-nil means interpret \"_\" and \"^\" for display.
  825. If you want to control how Org exports those characters, see
  826. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  827. used to be an alias for `org-export-with-sub-superscripts' in
  828. Org <8.0, it is not anymore.
  829. When this option is turned on, you can use TeX-like syntax for
  830. sub- and superscripts within the buffer. Several characters after
  831. \"_\" or \"^\" will be considered as a single item - so grouping
  832. with {} is normally not needed. For example, the following things
  833. will be parsed as single sub- or superscripts:
  834. 10^24 or 10^tau several digits will be considered 1 item.
  835. 10^-12 or 10^-tau a leading sign with digits or a word
  836. x^2-y^3 will be read as x^2 - y^3, because items are
  837. terminated by almost any nonword/nondigit char.
  838. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  839. Still, ambiguity is possible. So when in doubt, use {} to enclose
  840. the sub/superscript. If you set this variable to the symbol `{}',
  841. the braces are *required* in order to trigger interpretations as
  842. sub/superscript. This can be helpful in documents that need \"_\"
  843. frequently in plain text."
  844. :group 'org-startup
  845. :version "24.4"
  846. :package-version '(Org . "8.0")
  847. :type '(choice
  848. (const :tag "Always interpret" t)
  849. (const :tag "Only with braces" {})
  850. (const :tag "Never interpret" nil)))
  851. (defcustom org-startup-with-beamer-mode nil
  852. "Non-nil means turn on `org-beamer-mode' on startup.
  853. This can also be configured on a per-file basis by adding one of
  854. the following lines anywhere in the buffer:
  855. #+STARTUP: beamer"
  856. :group 'org-startup
  857. :version "24.1"
  858. :type 'boolean)
  859. (defcustom org-startup-align-all-tables nil
  860. "Non-nil means align all tables when visiting a file.
  861. This is useful when the column width in tables is forced with <N> cookies
  862. in table fields. Such tables will look correct only after the first re-align.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: align
  866. #+STARTUP: noalign"
  867. :group 'org-startup
  868. :type 'boolean)
  869. (defcustom org-startup-with-inline-images nil
  870. "Non-nil means show inline images when loading a new Org file.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: inlineimages
  874. #+STARTUP: noinlineimages"
  875. :group 'org-startup
  876. :version "24.1"
  877. :type 'boolean)
  878. (defcustom org-startup-with-latex-preview nil
  879. "Non-nil means preview LaTeX fragments when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: latexpreview
  883. #+STARTUP: nolatexpreview"
  884. :group 'org-startup
  885. :version "24.4"
  886. :package-version '(Org . "8.0")
  887. :type 'boolean)
  888. (defcustom org-insert-mode-line-in-empty-file nil
  889. "Non-nil means insert the first line setting Org-mode in empty files.
  890. When the function `org-mode' is called interactively in an empty file, this
  891. normally means that the file name does not automatically trigger Org-mode.
  892. To ensure that the file will always be in Org-mode in the future, a
  893. line enforcing Org-mode will be inserted into the buffer, if this option
  894. has been set."
  895. :group 'org-startup
  896. :type 'boolean)
  897. (defcustom org-replace-disputed-keys nil
  898. "Non-nil means use alternative key bindings for some keys.
  899. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  900. These keys are also used by other packages like shift-selection-mode'
  901. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  902. If you want to use Org-mode together with one of these other modes,
  903. or more generally if you would like to move some Org-mode commands to
  904. other keys, set this variable and configure the keys with the variable
  905. `org-disputed-keys'.
  906. This option is only relevant at load-time of Org-mode, and must be set
  907. *before* org.el is loaded. Changing it requires a restart of Emacs to
  908. become effective."
  909. :group 'org-startup
  910. :type 'boolean)
  911. (defcustom org-use-extra-keys nil
  912. "Non-nil means use extra key sequence definitions for certain commands.
  913. This happens automatically if you run XEmacs or if `window-system'
  914. is nil. This variable lets you do the same manually. You must
  915. set it before loading org.
  916. Example: on Carbon Emacs 22 running graphically, with an external
  917. keyboard on a Powerbook, the default way of setting M-left might
  918. not work for either Alt or ESC. Setting this variable will make
  919. it work for ESC."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  923. (defcustom org-disputed-keys
  924. '(([(shift up)] . [(meta p)])
  925. ([(shift down)] . [(meta n)])
  926. ([(shift left)] . [(meta -)])
  927. ([(shift right)] . [(meta +)])
  928. ([(control shift right)] . [(meta shift +)])
  929. ([(control shift left)] . [(meta shift -)]))
  930. "Keys for which Org-mode and other modes compete.
  931. This is an alist, cars are the default keys, second element specifies
  932. the alternative to use when `org-replace-disputed-keys' is t.
  933. Keys can be specified in any syntax supported by `define-key'.
  934. The value of this option takes effect only at Org-mode's startup,
  935. therefore you'll have to restart Emacs to apply it after changing."
  936. :group 'org-startup
  937. :type 'alist)
  938. (defun org-key (key)
  939. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  940. Or return the original if not disputed.
  941. Also apply the translations defined in `org-xemacs-key-equivalents'."
  942. (when org-replace-disputed-keys
  943. (let* ((nkey (key-description key))
  944. (x (org-find-if (lambda (x)
  945. (equal (key-description (car x)) nkey))
  946. org-disputed-keys)))
  947. (setq key (if x (cdr x) key))))
  948. (when (featurep 'xemacs)
  949. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  950. key)
  951. (defun org-find-if (predicate seq)
  952. (catch 'exit
  953. (while seq
  954. (if (funcall predicate (car seq))
  955. (throw 'exit (car seq))
  956. (pop seq)))))
  957. (defun org-defkey (keymap key def)
  958. "Define a key, possibly translated, as returned by `org-key'."
  959. (define-key keymap (org-key key) def))
  960. (defcustom org-ellipsis nil
  961. "The ellipsis to use in the Org-mode outline.
  962. When nil, just use the standard three dots.
  963. When a string, use that string instead.
  964. When a face, use the standard 3 dots, but with the specified face.
  965. The change affects only Org-mode (which will then use its own display table).
  966. Changing this requires executing \\[org-mode] in a buffer to become
  967. effective."
  968. :group 'org-startup
  969. :type '(choice (const :tag "Default" nil)
  970. (face :tag "Face" :value org-warning)
  971. (string :tag "String" :value "...#")))
  972. (defvar org-display-table nil
  973. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  974. (defgroup org-keywords nil
  975. "Keywords in Org-mode."
  976. :tag "Org Keywords"
  977. :group 'org)
  978. (defcustom org-closed-keep-when-no-todo nil
  979. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  980. :group 'org-todo
  981. :group 'org-keywords
  982. :version "24.4"
  983. :package-version '(Org . "8.0")
  984. :type 'boolean)
  985. (defgroup org-structure nil
  986. "Options concerning the general structure of Org-mode files."
  987. :tag "Org Structure"
  988. :group 'org)
  989. (defgroup org-reveal-location nil
  990. "Options about how to make context of a location visible."
  991. :tag "Org Reveal Location"
  992. :group 'org-structure)
  993. (defcustom org-show-context-detail '((isearch . lineage)
  994. (bookmark-jump . lineage)
  995. (default . ancestors))
  996. "Alist between context and visibility span when revealing a location.
  997. \\<org-mode-map>Some actions may move point into invisible
  998. locations. As a consequence, Org always expose a neighborhood
  999. around point. How much is shown depends on the initial action,
  1000. or context. Valid contexts are
  1001. agenda when exposing an entry from the agenda
  1002. org-goto when using the command `org-goto' (\\[org-goto])
  1003. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1004. tags-tree when constructing a sparse tree based on tags matches
  1005. link-search when exposing search matches associated with a link
  1006. mark-goto when exposing the jump goal of a mark
  1007. bookmark-jump when exposing a bookmark location
  1008. isearch when exiting from an incremental search
  1009. default default for all contexts not set explicitly
  1010. Allowed visibility spans are
  1011. minimal show current headline; if point is not on headline,
  1012. also show entry
  1013. local show current headline, entry and next headline
  1014. ancestors show current headline and its direct ancestors; if
  1015. point is not on headline, also show entry
  1016. lineage show current headline, its direct ancestors and all
  1017. their children; if point is not on headline, also show
  1018. entry and first child
  1019. tree show current headline, its direct ancestors and all
  1020. their children; if point is not on headline, also show
  1021. entry and all children
  1022. canonical show current headline, its direct ancestors along with
  1023. their entries and children; if point is not located on
  1024. the headline, also show current entry and all children
  1025. As special cases, a nil or t value means show all contexts in
  1026. `minimal' or `canonical' view, respectively.
  1027. Some views can make displayed information very compact, but also
  1028. make it harder to edit the location of the match. In such
  1029. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1030. more context."
  1031. :group 'org-reveal-location
  1032. :version "25.1"
  1033. :package-version '(Org . "8.3")
  1034. :type '(choice
  1035. (const :tag "Canonical" t)
  1036. (const :tag "Minimal" nil)
  1037. (repeat :greedy t :tag "Individual contexts"
  1038. (cons
  1039. (choice :tag "Context"
  1040. (const agenda)
  1041. (const org-goto)
  1042. (const occur-tree)
  1043. (const tags-tree)
  1044. (const link-search)
  1045. (const mark-goto)
  1046. (const bookmark-jump)
  1047. (const isearch)
  1048. (const default))
  1049. (choice :tag "Detail level"
  1050. (const minimal)
  1051. (const local)
  1052. (const ancestors)
  1053. (const lineage)
  1054. (const tree)
  1055. (const canonical))))))
  1056. (defcustom org-indirect-buffer-display 'other-window
  1057. "How should indirect tree buffers be displayed?
  1058. This applies to indirect buffers created with the commands
  1059. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1060. Valid values are:
  1061. current-window Display in the current window
  1062. other-window Just display in another window.
  1063. dedicated-frame Create one new frame, and re-use it each time.
  1064. new-frame Make a new frame each time. Note that in this case
  1065. previously-made indirect buffers are kept, and you need to
  1066. kill these buffers yourself."
  1067. :group 'org-structure
  1068. :group 'org-agenda-windows
  1069. :type '(choice
  1070. (const :tag "In current window" current-window)
  1071. (const :tag "In current frame, other window" other-window)
  1072. (const :tag "Each time a new frame" new-frame)
  1073. (const :tag "One dedicated frame" dedicated-frame)))
  1074. (defcustom org-use-speed-commands nil
  1075. "Non-nil means activate single letter commands at beginning of a headline.
  1076. This may also be a function to test for appropriate locations where speed
  1077. commands should be active.
  1078. For example, to activate speed commands when the point is on any
  1079. star at the beginning of the headline, you can do this:
  1080. (setq org-use-speed-commands
  1081. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1082. :group 'org-structure
  1083. :type '(choice
  1084. (const :tag "Never" nil)
  1085. (const :tag "At beginning of headline stars" t)
  1086. (function)))
  1087. (defcustom org-speed-commands-user nil
  1088. "Alist of additional speed commands.
  1089. This list will be checked before `org-speed-commands-default'
  1090. when the variable `org-use-speed-commands' is non-nil
  1091. and when the cursor is at the beginning of a headline.
  1092. The car if each entry is a string with a single letter, which must
  1093. be assigned to `self-insert-command' in the global map.
  1094. The cdr is either a command to be called interactively, a function
  1095. to be called, or a form to be evaluated.
  1096. An entry that is just a list with a single string will be interpreted
  1097. as a descriptive headline that will be added when listing the speed
  1098. commands in the Help buffer using the `?' speed command."
  1099. :group 'org-structure
  1100. :type '(repeat :value ("k" . ignore)
  1101. (choice :value ("k" . ignore)
  1102. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1103. (cons :tag "Letter and Command"
  1104. (string :tag "Command letter")
  1105. (choice
  1106. (function)
  1107. (sexp))))))
  1108. (defcustom org-bookmark-names-plist
  1109. '(:last-capture "org-capture-last-stored"
  1110. :last-refile "org-refile-last-stored"
  1111. :last-capture-marker "org-capture-last-stored-marker")
  1112. "Names for bookmarks automatically set by some Org commands.
  1113. This can provide strings as names for a number of bookmarks Org sets
  1114. automatically. The following keys are currently implemented:
  1115. :last-capture
  1116. :last-capture-marker
  1117. :last-refile
  1118. When a key does not show up in the property list, the corresponding bookmark
  1119. is not set."
  1120. :group 'org-structure
  1121. :type 'plist)
  1122. (defgroup org-cycle nil
  1123. "Options concerning visibility cycling in Org-mode."
  1124. :tag "Org Cycle"
  1125. :group 'org-structure)
  1126. (defcustom org-cycle-skip-children-state-if-no-children t
  1127. "Non-nil means skip CHILDREN state in entries that don't have any."
  1128. :group 'org-cycle
  1129. :type 'boolean)
  1130. (defcustom org-cycle-max-level nil
  1131. "Maximum level which should still be subject to visibility cycling.
  1132. Levels higher than this will, for cycling, be treated as text, not a headline.
  1133. When `org-odd-levels-only' is set, a value of N in this variable actually
  1134. means 2N-1 stars as the limiting headline.
  1135. When nil, cycle all levels.
  1136. Note that the limiting level of cycling is also influenced by
  1137. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1138. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1139. than its value."
  1140. :group 'org-cycle
  1141. :type '(choice
  1142. (const :tag "No limit" nil)
  1143. (integer :tag "Maximum level")))
  1144. (defcustom org-hide-block-startup nil
  1145. "Non-nil means entering Org-mode will fold all blocks.
  1146. This can also be set in on a per-file basis with
  1147. #+STARTUP: hideblocks
  1148. #+STARTUP: showblocks"
  1149. :group 'org-startup
  1150. :group 'org-cycle
  1151. :type 'boolean)
  1152. (defcustom org-cycle-global-at-bob nil
  1153. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1154. This makes it possible to do global cycling without having to use S-TAB or
  1155. \\[universal-argument] TAB. For this special case to work, the first line
  1156. of the buffer must not be a headline -- it may be empty or some other text.
  1157. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1158. sure the cursor stays at the beginning of the buffer. When this option is
  1159. nil, don't do anything special at the beginning of the buffer."
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-level-after-item/entry-creation t
  1163. "Non-nil means cycle entry level or item indentation in new empty entries.
  1164. When the cursor is at the end of an empty headline, i.e., with only stars
  1165. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1166. and then all possible ancestor states, before returning to the original state.
  1167. This makes data entry extremely fast: M-RET to create a new headline,
  1168. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1169. When the cursor is at the end of an empty plain list item, one TAB will
  1170. make it a subitem, two or more tabs will back up to make this an item
  1171. higher up in the item hierarchy."
  1172. :group 'org-cycle
  1173. :type 'boolean)
  1174. (defcustom org-cycle-emulate-tab t
  1175. "Where should `org-cycle' emulate TAB.
  1176. nil Never
  1177. white Only in completely white lines
  1178. whitestart Only at the beginning of lines, before the first non-white char
  1179. t Everywhere except in headlines
  1180. exc-hl-bol Everywhere except at the start of a headline
  1181. If TAB is used in a place where it does not emulate TAB, the current subtree
  1182. visibility is cycled."
  1183. :group 'org-cycle
  1184. :type '(choice (const :tag "Never" nil)
  1185. (const :tag "Only in completely white lines" white)
  1186. (const :tag "Before first char in a line" whitestart)
  1187. (const :tag "Everywhere except in headlines" t)
  1188. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1189. (defcustom org-cycle-separator-lines 2
  1190. "Number of empty lines needed to keep an empty line between collapsed trees.
  1191. If you leave an empty line between the end of a subtree and the following
  1192. headline, this empty line is hidden when the subtree is folded.
  1193. Org-mode will leave (exactly) one empty line visible if the number of
  1194. empty lines is equal or larger to the number given in this variable.
  1195. So the default 2 means at least 2 empty lines after the end of a subtree
  1196. are needed to produce free space between a collapsed subtree and the
  1197. following headline.
  1198. If the number is negative, and the number of empty lines is at least -N,
  1199. all empty lines are shown.
  1200. Special case: when 0, never leave empty lines in collapsed view."
  1201. :group 'org-cycle
  1202. :type 'integer)
  1203. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1204. (defcustom org-pre-cycle-hook nil
  1205. "Hook that is run before visibility cycling is happening.
  1206. The function(s) in this hook must accept a single argument which indicates
  1207. the new state that will be set right after running this hook. The
  1208. argument is a symbol. Before a global state change, it can have the values
  1209. `overview', `content', or `all'. Before a local state change, it can have
  1210. the values `folded', `children', or `subtree'."
  1211. :group 'org-cycle
  1212. :type 'hook)
  1213. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1214. org-cycle-hide-drawers
  1215. org-cycle-show-empty-lines
  1216. org-optimize-window-after-visibility-change)
  1217. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1218. The function(s) in this hook must accept a single argument which indicates
  1219. the new state that was set by the most recent `org-cycle' command. The
  1220. argument is a symbol. After a global state change, it can have the values
  1221. `overview', `contents', or `all'. After a local state change, it can have
  1222. the values `folded', `children', or `subtree'."
  1223. :group 'org-cycle
  1224. :type 'hook
  1225. :version "25.1"
  1226. :package-version '(Org . "8.3"))
  1227. (defgroup org-edit-structure nil
  1228. "Options concerning structure editing in Org-mode."
  1229. :tag "Org Edit Structure"
  1230. :group 'org-structure)
  1231. (defcustom org-odd-levels-only nil
  1232. "Non-nil means skip even levels and only use odd levels for the outline.
  1233. This has the effect that two stars are being added/taken away in
  1234. promotion/demotion commands. It also influences how levels are
  1235. handled by the exporters.
  1236. Changing it requires restart of `font-lock-mode' to become effective
  1237. for fontification also in regions already fontified.
  1238. You may also set this on a per-file basis by adding one of the following
  1239. lines to the buffer:
  1240. #+STARTUP: odd
  1241. #+STARTUP: oddeven"
  1242. :group 'org-edit-structure
  1243. :group 'org-appearance
  1244. :type 'boolean)
  1245. (defcustom org-adapt-indentation t
  1246. "Non-nil means adapt indentation to outline node level.
  1247. When this variable is set, Org assumes that you write outlines by
  1248. indenting text in each node to align with the headline (after the
  1249. stars). The following issues are influenced by this variable:
  1250. - The indentation is increased by one space in a demotion
  1251. command, and decreased by one in a promotion command. However,
  1252. in the latter case, if shifting some line in the entry body
  1253. would alter document structure (e.g., insert a new headline),
  1254. indentation is not changed at all.
  1255. - Property drawers and planning information is inserted indented
  1256. when this variable is set. When nil, they will not be indented.
  1257. - TAB indents a line relative to current level. The lines below
  1258. a headline will be indented when this variable is set.
  1259. Note that this is all about true indentation, by adding and
  1260. removing space characters. See also `org-indent.el' which does
  1261. level-dependent indentation in a virtual way, i.e. at display
  1262. time in Emacs."
  1263. :group 'org-edit-structure
  1264. :type 'boolean)
  1265. (defcustom org-special-ctrl-a/e nil
  1266. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1267. When t, `C-a' will bring back the cursor to the beginning of the
  1268. headline text, i.e. after the stars and after a possible TODO
  1269. keyword. In an item, this will be the position after bullet and
  1270. check-box, if any. When the cursor is already at that position,
  1271. another `C-a' will bring it to the beginning of the line.
  1272. `C-e' will jump to the end of the headline, ignoring the presence
  1273. of tags in the headline. A second `C-e' will then jump to the
  1274. true end of the line, after any tags. This also means that, when
  1275. this variable is non-nil, `C-e' also will never jump beyond the
  1276. end of the heading of a folded section, i.e. not after the
  1277. ellipses.
  1278. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1279. normally, going to the true line boundary first. Only a directly
  1280. following, identical keypress will bring the cursor to the
  1281. special positions.
  1282. This may also be a cons cell where the behavior for `C-a' and
  1283. `C-e' is set separately."
  1284. :group 'org-edit-structure
  1285. :type '(choice
  1286. (const :tag "off" nil)
  1287. (const :tag "on: after stars/bullet and before tags first" t)
  1288. (const :tag "reversed: true line boundary first" reversed)
  1289. (cons :tag "Set C-a and C-e separately"
  1290. (choice :tag "Special C-a"
  1291. (const :tag "off" nil)
  1292. (const :tag "on: after stars/bullet first" t)
  1293. (const :tag "reversed: before stars/bullet first" reversed))
  1294. (choice :tag "Special C-e"
  1295. (const :tag "off" nil)
  1296. (const :tag "on: before tags first" t)
  1297. (const :tag "reversed: after tags first" reversed)))))
  1298. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1299. (defcustom org-special-ctrl-k nil
  1300. "Non-nil means `C-k' will behave specially in headlines.
  1301. When nil, `C-k' will call the default `kill-line' command.
  1302. When t, the following will happen while the cursor is in the headline:
  1303. - When the cursor is at the beginning of a headline, kill the entire
  1304. line and possible the folded subtree below the line.
  1305. - When in the middle of the headline text, kill the headline up to the tags.
  1306. - When after the headline text, kill the tags."
  1307. :group 'org-edit-structure
  1308. :type 'boolean)
  1309. (defcustom org-ctrl-k-protect-subtree nil
  1310. "Non-nil means, do not delete a hidden subtree with C-k.
  1311. When set to the symbol `error', simply throw an error when C-k is
  1312. used to kill (part-of) a headline that has hidden text behind it.
  1313. Any other non-nil value will result in a query to the user, if it is
  1314. OK to kill that hidden subtree. When nil, kill without remorse."
  1315. :group 'org-edit-structure
  1316. :version "24.1"
  1317. :type '(choice
  1318. (const :tag "Do not protect hidden subtrees" nil)
  1319. (const :tag "Protect hidden subtrees with a security query" t)
  1320. (const :tag "Never kill a hidden subtree with C-k" error)))
  1321. (defcustom org-special-ctrl-o t
  1322. "Non-nil means, make `C-o' insert a row in tables."
  1323. :group 'org-edit-structure
  1324. :type 'boolean)
  1325. (defcustom org-catch-invisible-edits nil
  1326. "Check if in invisible region before inserting or deleting a character.
  1327. Valid values are:
  1328. nil Do not check, so just do invisible edits.
  1329. error Throw an error and do nothing.
  1330. show Make point visible, and do the requested edit.
  1331. show-and-error Make point visible, then throw an error and abort the edit.
  1332. smart Make point visible, and do insertion/deletion if it is
  1333. adjacent to visible text and the change feels predictable.
  1334. Never delete a previously invisible character or add in the
  1335. middle or right after an invisible region. Basically, this
  1336. allows insertion and backward-delete right before ellipses.
  1337. FIXME: maybe in this case we should not even show?"
  1338. :group 'org-edit-structure
  1339. :version "24.1"
  1340. :type '(choice
  1341. (const :tag "Do not check" nil)
  1342. (const :tag "Throw error when trying to edit" error)
  1343. (const :tag "Unhide, but do not do the edit" show-and-error)
  1344. (const :tag "Show invisible part and do the edit" show)
  1345. (const :tag "Be smart and do the right thing" smart)))
  1346. (defcustom org-yank-folded-subtrees t
  1347. "Non-nil means when yanking subtrees, fold them.
  1348. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1349. it starts with a heading and all other headings in it are either children
  1350. or siblings, then fold all the subtrees. However, do this only if no
  1351. text after the yank would be swallowed into a folded tree by this action."
  1352. :group 'org-edit-structure
  1353. :type 'boolean)
  1354. (defcustom org-yank-adjusted-subtrees nil
  1355. "Non-nil means when yanking subtrees, adjust the level.
  1356. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1357. this function for details."
  1358. :group 'org-edit-structure
  1359. :type 'boolean)
  1360. (defcustom org-M-RET-may-split-line '((default . t))
  1361. "Non-nil means M-RET will split the line at the cursor position.
  1362. When nil, it will go to the end of the line before making a
  1363. new line.
  1364. You may also set this option in a different way for different
  1365. contexts. Valid contexts are:
  1366. headline when creating a new headline
  1367. item when creating a new item
  1368. table in a table field
  1369. default the value to be used for all contexts not explicitly
  1370. customized"
  1371. :group 'org-structure
  1372. :group 'org-table
  1373. :type '(choice
  1374. (const :tag "Always" t)
  1375. (const :tag "Never" nil)
  1376. (repeat :greedy t :tag "Individual contexts"
  1377. (cons
  1378. (choice :tag "Context"
  1379. (const headline)
  1380. (const item)
  1381. (const table)
  1382. (const default))
  1383. (boolean)))))
  1384. (defcustom org-insert-heading-respect-content nil
  1385. "Non-nil means insert new headings after the current subtree.
  1386. When nil, the new heading is created directly after the current line.
  1387. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1388. this variable on for the duration of the command."
  1389. :group 'org-structure
  1390. :type 'boolean)
  1391. (defcustom org-blank-before-new-entry '((heading . auto)
  1392. (plain-list-item . auto))
  1393. "Should `org-insert-heading' leave a blank line before new heading/item?
  1394. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1395. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1396. which case Org will look at the surrounding headings/items and try to
  1397. make an intelligent decision whether to insert a blank line or not."
  1398. :group 'org-edit-structure
  1399. :type '(list
  1400. (cons (const heading)
  1401. (choice (const :tag "Never" nil)
  1402. (const :tag "Always" t)
  1403. (const :tag "Auto" auto)))
  1404. (cons (const plain-list-item)
  1405. (choice (const :tag "Never" nil)
  1406. (const :tag "Always" t)
  1407. (const :tag "Auto" auto)))))
  1408. (defcustom org-insert-heading-hook nil
  1409. "Hook being run after inserting a new heading."
  1410. :group 'org-edit-structure
  1411. :type 'hook)
  1412. (defcustom org-enable-fixed-width-editor t
  1413. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1414. This currently only means they are never auto-wrapped.
  1415. When nil, such lines will be treated like ordinary lines."
  1416. :group 'org-edit-structure
  1417. :type 'boolean)
  1418. (defcustom org-goto-auto-isearch t
  1419. "Non-nil means typing characters in `org-goto' starts incremental search.
  1420. When nil, you can use these keybindings to navigate the buffer:
  1421. q Quit the org-goto interface
  1422. n Go to the next visible heading
  1423. p Go to the previous visible heading
  1424. f Go one heading forward on same level
  1425. b Go one heading backward on same level
  1426. u Go one heading up"
  1427. :group 'org-edit-structure
  1428. :type 'boolean)
  1429. (defgroup org-sparse-trees nil
  1430. "Options concerning sparse trees in Org-mode."
  1431. :tag "Org Sparse Trees"
  1432. :group 'org-structure)
  1433. (defcustom org-highlight-sparse-tree-matches t
  1434. "Non-nil means highlight all matches that define a sparse tree.
  1435. The highlights will automatically disappear the next time the buffer is
  1436. changed by an edit command."
  1437. :group 'org-sparse-trees
  1438. :type 'boolean)
  1439. (defcustom org-remove-highlights-with-change t
  1440. "Non-nil means any change to the buffer will remove temporary highlights.
  1441. \\<org-mode-map>\
  1442. Such highlights are created by `org-occur' and `org-clock-display'.
  1443. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1444. to get rid of the highlights.
  1445. The highlights created by `org-toggle-latex-fragment' always need
  1446. `\\[org-toggle-latex-fragment]' to be removed."
  1447. :group 'org-sparse-trees
  1448. :group 'org-time
  1449. :type 'boolean)
  1450. (defcustom org-occur-hook '(org-first-headline-recenter)
  1451. "Hook that is run after `org-occur' has constructed a sparse tree.
  1452. This can be used to recenter the window to show as much of the structure
  1453. as possible."
  1454. :group 'org-sparse-trees
  1455. :type 'hook)
  1456. (defgroup org-imenu-and-speedbar nil
  1457. "Options concerning imenu and speedbar in Org-mode."
  1458. :tag "Org Imenu and Speedbar"
  1459. :group 'org-structure)
  1460. (defcustom org-imenu-depth 2
  1461. "The maximum level for Imenu access to Org-mode headlines.
  1462. This also applied for speedbar access."
  1463. :group 'org-imenu-and-speedbar
  1464. :type 'integer)
  1465. (defgroup org-table nil
  1466. "Options concerning tables in Org-mode."
  1467. :tag "Org Table"
  1468. :group 'org)
  1469. (defcustom org-enable-table-editor 'optimized
  1470. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1471. When nil, such lines will be treated like ordinary lines.
  1472. When equal to the symbol `optimized', the table editor will be optimized to
  1473. do the following:
  1474. - Automatic overwrite mode in front of whitespace in table fields.
  1475. This makes the structure of the table stay in tact as long as the edited
  1476. field does not exceed the column width.
  1477. - Minimize the number of realigns. Normally, the table is aligned each time
  1478. TAB or RET are pressed to move to another field. With optimization this
  1479. happens only if changes to a field might have changed the column width.
  1480. Optimization requires replacing the functions `self-insert-command',
  1481. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1482. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1483. very good at guessing when a re-align will be necessary, but you can always
  1484. force one with \\[org-ctrl-c-ctrl-c].
  1485. If you would like to use the optimized version in Org-mode, but the
  1486. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1487. This variable can be used to turn on and off the table editor during a session,
  1488. but in order to toggle optimization, a restart is required.
  1489. See also the variable `org-table-auto-blank-field'."
  1490. :group 'org-table
  1491. :type '(choice
  1492. (const :tag "off" nil)
  1493. (const :tag "on" t)
  1494. (const :tag "on, optimized" optimized)))
  1495. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1496. (version<= emacs-version "24.1"))
  1497. "Non-nil means cluster self-insert commands for undo when possible.
  1498. If this is set, then, like in the Emacs command loop, 20 consecutive
  1499. characters will be undone together.
  1500. This is configurable, because there is some impact on typing performance."
  1501. :group 'org-table
  1502. :type 'boolean)
  1503. (defcustom org-table-tab-recognizes-table.el t
  1504. "Non-nil means TAB will automatically notice a table.el table.
  1505. When it sees such a table, it moves point into it and - if necessary -
  1506. calls `table-recognize-table'."
  1507. :group 'org-table-editing
  1508. :type 'boolean)
  1509. (defgroup org-link nil
  1510. "Options concerning links in Org-mode."
  1511. :tag "Org Link"
  1512. :group 'org)
  1513. (defvar-local org-link-abbrev-alist-local nil
  1514. "Buffer-local version of `org-link-abbrev-alist', which see.
  1515. The value of this is taken from the #+LINK lines.")
  1516. (defcustom org-link-abbrev-alist nil
  1517. "Alist of link abbreviations.
  1518. The car of each element is a string, to be replaced at the start of a link.
  1519. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1520. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1521. [[linkkey:tag][description]]
  1522. The `linkkey' must be a word word, starting with a letter, followed
  1523. by letters, numbers, `-' or `_'.
  1524. If REPLACE is a string, the tag will simply be appended to create the link.
  1525. If the string contains \"%s\", the tag will be inserted there. If the string
  1526. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1527. at that point (see the function `url-hexify-string'). If the string contains
  1528. the specifier \"%(my-function)\", then the custom function `my-function' will
  1529. be invoked: this function takes the tag as its only argument and must return
  1530. a string.
  1531. REPLACE may also be a function that will be called with the tag as the
  1532. only argument to create the link, which should be returned as a string.
  1533. See the manual for examples."
  1534. :group 'org-link
  1535. :type '(repeat
  1536. (cons
  1537. (string :tag "Protocol")
  1538. (choice
  1539. (string :tag "Format")
  1540. (function)))))
  1541. (defcustom org-descriptive-links t
  1542. "Non-nil means Org will display descriptive links.
  1543. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1544. \"Org Website\", hiding the link itself and just displaying its
  1545. description. When set to nil, Org will display the full links
  1546. literally.
  1547. You can interactively set the value of this variable by calling
  1548. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1549. :group 'org-link
  1550. :type 'boolean)
  1551. (defcustom org-link-file-path-type 'adaptive
  1552. "How the path name in file links should be stored.
  1553. Valid values are:
  1554. relative Relative to the current directory, i.e. the directory of the file
  1555. into which the link is being inserted.
  1556. absolute Absolute path, if possible with ~ for home directory.
  1557. noabbrev Absolute path, no abbreviation of home directory.
  1558. adaptive Use relative path for files in the current directory and sub-
  1559. directories of it. For other files, use an absolute path."
  1560. :group 'org-link
  1561. :type '(choice
  1562. (const relative)
  1563. (const absolute)
  1564. (const noabbrev)
  1565. (const adaptive)))
  1566. (defvaralias 'org-activate-links 'org-highlight-links)
  1567. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1568. "Types of links that should be highlighted in Org-mode files.
  1569. This is a list of symbols, each one of them leading to the
  1570. highlighting of a certain link type.
  1571. You can still open links that are not highlighted.
  1572. In principle, it does not hurt to turn on highlighting for all
  1573. link types. There may be a small gain when turning off unused
  1574. link types. The types are:
  1575. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1576. angle Links in angular brackets that may contain whitespace like
  1577. <bbdb:Carsten Dominik>.
  1578. plain Plain links in normal text, no whitespace, like http://google.com.
  1579. radio Text that is matched by a radio target, see manual for details.
  1580. tag Tag settings in a headline (link to tag search).
  1581. date Time stamps (link to calendar).
  1582. footnote Footnote labels.
  1583. If you set this variable during an Emacs session, use `org-mode-restart'
  1584. in the Org buffer so that the change takes effect."
  1585. :group 'org-link
  1586. :group 'org-appearance
  1587. :type '(set :greedy t
  1588. (const :tag "Double bracket links" bracket)
  1589. (const :tag "Angular bracket links" angle)
  1590. (const :tag "Plain text links" plain)
  1591. (const :tag "Radio target matches" radio)
  1592. (const :tag "Tags" tag)
  1593. (const :tag "Timestamps" date)
  1594. (const :tag "Footnotes" footnote)))
  1595. (defcustom org-make-link-description-function nil
  1596. "Function to use for generating link descriptions from links.
  1597. When nil, the link location will be used. This function must take
  1598. two parameters: the first one is the link, the second one is the
  1599. description generated by `org-insert-link'. The function should
  1600. return the description to use."
  1601. :group 'org-link
  1602. :type '(choice (const nil) (function)))
  1603. (defgroup org-link-store nil
  1604. "Options concerning storing links in Org-mode."
  1605. :tag "Org Store Link"
  1606. :group 'org-link)
  1607. (defcustom org-url-hexify-p t
  1608. "When non-nil, hexify URL when creating a link."
  1609. :type 'boolean
  1610. :version "24.3"
  1611. :group 'org-link-store)
  1612. (defcustom org-email-link-description-format "Email %c: %.30s"
  1613. "Format of the description part of a link to an email or usenet message.
  1614. The following %-escapes will be replaced by corresponding information:
  1615. %F full \"From\" field
  1616. %f name, taken from \"From\" field, address if no name
  1617. %T full \"To\" field
  1618. %t first name in \"To\" field, address if no name
  1619. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1620. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1621. %s subject
  1622. %d date
  1623. %m message-id.
  1624. You may use normal field width specification between the % and the letter.
  1625. This is for example useful to limit the length of the subject.
  1626. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1627. :group 'org-link-store
  1628. :type 'string)
  1629. (defcustom org-from-is-user-regexp
  1630. (let (r1 r2)
  1631. (when (and user-mail-address (not (string= user-mail-address "")))
  1632. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1633. (when (and user-full-name (not (string= user-full-name "")))
  1634. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1635. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1636. "Regexp matched against the \"From:\" header of an email or usenet message.
  1637. It should match if the message is from the user him/herself."
  1638. :group 'org-link-store
  1639. :type 'regexp)
  1640. (defcustom org-context-in-file-links t
  1641. "Non-nil means file links from `org-store-link' contain context.
  1642. A search string will be added to the file name with :: as separator and
  1643. used to find the context when the link is activated by the command
  1644. `org-open-at-point'. When this option is t, the entire active region
  1645. will be placed in the search string of the file link. If set to a
  1646. positive integer, only the first n lines of context will be stored.
  1647. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1648. negates this setting for the duration of the command."
  1649. :group 'org-link-store
  1650. :type '(choice boolean integer))
  1651. (defcustom org-keep-stored-link-after-insertion nil
  1652. "Non-nil means keep link in list for entire session.
  1653. The command `org-store-link' adds a link pointing to the current
  1654. location to an internal list. These links accumulate during a session.
  1655. The command `org-insert-link' can be used to insert links into any
  1656. Org-mode file (offering completion for all stored links). When this
  1657. option is nil, every link which has been inserted once using \\[org-insert-link]
  1658. will be removed from the list, to make completing the unused links
  1659. more efficient."
  1660. :group 'org-link-store
  1661. :type 'boolean)
  1662. (defgroup org-link-follow nil
  1663. "Options concerning following links in Org-mode."
  1664. :tag "Org Follow Link"
  1665. :group 'org-link)
  1666. (defcustom org-link-translation-function nil
  1667. "Function to translate links with different syntax to Org syntax.
  1668. This can be used to translate links created for example by the Planner
  1669. or emacs-wiki packages to Org syntax.
  1670. The function must accept two parameters, a TYPE containing the link
  1671. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1672. which is everything after the link protocol. It should return a cons
  1673. with possibly modified values of type and path.
  1674. Org contains a function for this, so if you set this variable to
  1675. `org-translate-link-from-planner', you should be able follow many
  1676. links created by planner."
  1677. :group 'org-link-follow
  1678. :type '(choice (const nil) (function)))
  1679. (defcustom org-follow-link-hook nil
  1680. "Hook that is run after a link has been followed."
  1681. :group 'org-link-follow
  1682. :type 'hook)
  1683. (defcustom org-tab-follows-link nil
  1684. "Non-nil means on links TAB will follow the link.
  1685. Needs to be set before org.el is loaded.
  1686. This really should not be used, it does not make sense, and the
  1687. implementation is bad."
  1688. :group 'org-link-follow
  1689. :type 'boolean)
  1690. (defcustom org-return-follows-link nil
  1691. "Non-nil means on links RET will follow the link.
  1692. In tables, the special behavior of RET has precedence."
  1693. :group 'org-link-follow
  1694. :type 'boolean)
  1695. (defcustom org-mouse-1-follows-link
  1696. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1697. "Non-nil means mouse-1 on a link will follow the link.
  1698. A longer mouse click will still set point. Does not work on XEmacs.
  1699. Needs to be set before org.el is loaded."
  1700. :group 'org-link-follow
  1701. :version "24.4"
  1702. :package-version '(Org . "8.3")
  1703. :type '(choice
  1704. (const :tag "A double click follows the link" double)
  1705. (const :tag "Unconditionally follow the link with mouse-1" t)
  1706. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1707. (defcustom org-mark-ring-length 4
  1708. "Number of different positions to be recorded in the ring.
  1709. Changing this requires a restart of Emacs to work correctly."
  1710. :group 'org-link-follow
  1711. :type 'integer)
  1712. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1713. "Non-nil means internal links in Org files must exactly match a headline.
  1714. When nil, the link search tries to match a phrase with all words
  1715. in the search text."
  1716. :group 'org-link-follow
  1717. :version "24.1"
  1718. :type '(choice
  1719. (const :tag "Use fuzzy text search" nil)
  1720. (const :tag "Match only exact headline" t)
  1721. (const :tag "Match exact headline or query to create it"
  1722. query-to-create)))
  1723. (defcustom org-link-frame-setup
  1724. '((vm . vm-visit-folder-other-frame)
  1725. (vm-imap . vm-visit-imap-folder-other-frame)
  1726. (gnus . org-gnus-no-new-news)
  1727. (file . find-file-other-window)
  1728. (wl . wl-other-frame))
  1729. "Setup the frame configuration for following links.
  1730. When following a link with Emacs, it may often be useful to display
  1731. this link in another window or frame. This variable can be used to
  1732. set this up for the different types of links.
  1733. For VM, use any of
  1734. `vm-visit-folder'
  1735. `vm-visit-folder-other-window'
  1736. `vm-visit-folder-other-frame'
  1737. For Gnus, use any of
  1738. `gnus'
  1739. `gnus-other-frame'
  1740. `org-gnus-no-new-news'
  1741. For FILE, use any of
  1742. `find-file'
  1743. `find-file-other-window'
  1744. `find-file-other-frame'
  1745. For Wanderlust use any of
  1746. `wl'
  1747. `wl-other-frame'
  1748. For the calendar, use the variable `calendar-setup'.
  1749. For BBDB, it is currently only possible to display the matches in
  1750. another window."
  1751. :group 'org-link-follow
  1752. :type '(list
  1753. (cons (const vm)
  1754. (choice
  1755. (const vm-visit-folder)
  1756. (const vm-visit-folder-other-window)
  1757. (const vm-visit-folder-other-frame)))
  1758. (cons (const vm-imap)
  1759. (choice
  1760. (const vm-visit-imap-folder)
  1761. (const vm-visit-imap-folder-other-window)
  1762. (const vm-visit-imap-folder-other-frame)))
  1763. (cons (const gnus)
  1764. (choice
  1765. (const gnus)
  1766. (const gnus-other-frame)
  1767. (const org-gnus-no-new-news)))
  1768. (cons (const file)
  1769. (choice
  1770. (const find-file)
  1771. (const find-file-other-window)
  1772. (const find-file-other-frame)))
  1773. (cons (const wl)
  1774. (choice
  1775. (const wl)
  1776. (const wl-other-frame)))))
  1777. (defcustom org-display-internal-link-with-indirect-buffer nil
  1778. "Non-nil means use indirect buffer to display infile links.
  1779. Activating internal links (from one location in a file to another location
  1780. in the same file) normally just jumps to the location. When the link is
  1781. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1782. is displayed in
  1783. another window. When this option is set, the other window actually displays
  1784. an indirect buffer clone of the current buffer, to avoid any visibility
  1785. changes to the current buffer."
  1786. :group 'org-link-follow
  1787. :type 'boolean)
  1788. (defcustom org-open-non-existing-files nil
  1789. "Non-nil means `org-open-file' will open non-existing files.
  1790. When nil, an error will be generated.
  1791. This variable applies only to external applications because they
  1792. might choke on non-existing files. If the link is to a file that
  1793. will be opened in Emacs, the variable is ignored."
  1794. :group 'org-link-follow
  1795. :type 'boolean)
  1796. (defcustom org-open-directory-means-index-dot-org nil
  1797. "Non-nil means a link to a directory really means to index.org.
  1798. When nil, following a directory link will run dired or open a finder/explorer
  1799. window on that directory."
  1800. :group 'org-link-follow
  1801. :type 'boolean)
  1802. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1803. "Non-nil means ask for confirmation before executing shell links.
  1804. Shell links can be dangerous: just think about a link
  1805. [[shell:rm -rf ~/*][Google Search]]
  1806. This link would show up in your Org-mode document as \"Google Search\",
  1807. but really it would remove your entire home directory.
  1808. Therefore we advise against setting this variable to nil.
  1809. Just change it to `y-or-n-p' if you want to confirm with a
  1810. single keystroke rather than having to type \"yes\"."
  1811. :group 'org-link-follow
  1812. :type '(choice
  1813. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1814. (const :tag "with y-or-n (faster)" y-or-n-p)
  1815. (const :tag "no confirmation (dangerous)" nil)))
  1816. (put 'org-confirm-shell-link-function
  1817. 'safe-local-variable
  1818. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1819. (defcustom org-confirm-shell-link-not-regexp ""
  1820. "A regexp to skip confirmation for shell links."
  1821. :group 'org-link-follow
  1822. :version "24.1"
  1823. :type 'regexp)
  1824. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1825. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1826. Elisp links can be dangerous: just think about a link
  1827. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1828. This link would show up in your Org-mode document as \"Google Search\",
  1829. but really it would remove your entire home directory.
  1830. Therefore we advise against setting this variable to nil.
  1831. Just change it to `y-or-n-p' if you want to confirm with a
  1832. single keystroke rather than having to type \"yes\"."
  1833. :group 'org-link-follow
  1834. :type '(choice
  1835. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1836. (const :tag "with y-or-n (faster)" y-or-n-p)
  1837. (const :tag "no confirmation (dangerous)" nil)))
  1838. (put 'org-confirm-shell-link-function
  1839. 'safe-local-variable
  1840. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1841. (defcustom org-confirm-elisp-link-not-regexp ""
  1842. "A regexp to skip confirmation for Elisp links."
  1843. :group 'org-link-follow
  1844. :version "24.1"
  1845. :type 'regexp)
  1846. (defconst org-file-apps-defaults-gnu
  1847. '((remote . emacs)
  1848. (system . mailcap)
  1849. (t . mailcap))
  1850. "Default file applications on a UNIX or GNU/Linux system.
  1851. See `org-file-apps'.")
  1852. (defconst org-file-apps-defaults-macosx
  1853. '((remote . emacs)
  1854. (system . "open %s")
  1855. ("ps.gz" . "gv %s")
  1856. ("eps.gz" . "gv %s")
  1857. ("dvi" . "xdvi %s")
  1858. ("fig" . "xfig %s")
  1859. (t . "open %s"))
  1860. "Default file applications on a MacOS X system.
  1861. The system \"open\" is known as a default, but we use X11 applications
  1862. for some files for which the OS does not have a good default.
  1863. See `org-file-apps'.")
  1864. (defconst org-file-apps-defaults-windowsnt
  1865. (list '(remote . emacs)
  1866. (cons 'system (lambda (file _path)
  1867. (with-no-warnings (w32-shell-execute "open" file))))
  1868. (cons t (lambda (file _path)
  1869. (with-no-warnings (w32-shell-execute "open" file)))))
  1870. "Default file applications on a Windows NT system.
  1871. The system \"open\" is used for most files.
  1872. See `org-file-apps'.")
  1873. (defcustom org-file-apps
  1874. '((auto-mode . emacs)
  1875. ("\\.mm\\'" . default)
  1876. ("\\.x?html?\\'" . default)
  1877. ("\\.pdf\\'" . default))
  1878. "External applications for opening `file:path' items in a document.
  1879. \\<org-mode-map>\
  1880. Org mode uses system defaults for different file types, but
  1881. you can use this variable to set the application for a given file
  1882. extension. The entries in this list are cons cells where the car identifies
  1883. files and the cdr the corresponding command. Possible values for the
  1884. file identifier are
  1885. \"string\" A string as a file identifier can be interpreted in different
  1886. ways, depending on its contents:
  1887. - Alphanumeric characters only:
  1888. Match links with this file extension.
  1889. Example: (\"pdf\" . \"evince %s\")
  1890. to open PDFs with evince.
  1891. - Regular expression: Match links where the
  1892. filename matches the regexp. If you want to
  1893. use groups here, use shy groups.
  1894. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1895. \(\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1896. to open *.html and *.xhtml with firefox.
  1897. - Regular expression which contains (non-shy) groups:
  1898. Match links where the whole link, including \"::\", and
  1899. anything after that, matches the regexp.
  1900. In a custom command string, %1, %2, etc. are replaced with
  1901. the parts of the link that were matched by the groups.
  1902. For backwards compatibility, if a command string is given
  1903. that does not use any of the group matches, this case is
  1904. handled identically to the second one (i.e. match against
  1905. file name only).
  1906. In a custom function, you can access the group matches with
  1907. \(match-string n link).
  1908. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1909. to open [[file:document.pdf::5]] with evince at page 5.
  1910. `directory' Matches a directory
  1911. `remote' Matches a remote file, accessible through tramp or efs.
  1912. Remote files most likely should be visited through Emacs
  1913. because external applications cannot handle such paths.
  1914. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1915. so all files Emacs knows how to handle. Using this with
  1916. command `emacs' will open most files in Emacs. Beware that this
  1917. will also open html files inside Emacs, unless you add
  1918. \(\"html\" . default) to the list as well.
  1919. `system' The system command to open files, like `open' on Windows
  1920. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1921. that will be selected if you call \\[org-open-at-point] with a double
  1922. \\[universal-argument] \\[universal-argument] prefix.
  1923. t Default for files not matched by any of the other options.
  1924. Possible values for the command are:
  1925. `emacs' The file will be visited by the current Emacs process.
  1926. `default' Use the default application for this file type, which is the
  1927. association for t in the list, most likely in the system-specific
  1928. part. This can be used to overrule an unwanted setting in the
  1929. system-specific variable.
  1930. `system' Use the system command for opening files, like \"open\".
  1931. This command is specified by the entry whose car is `system'.
  1932. Most likely, the system-specific version of this variable
  1933. does define this command, but you can overrule/replace it
  1934. here.
  1935. `mailcap' Use command specified in the mailcaps.
  1936. string A command to be executed by a shell; %s will be replaced
  1937. by the path to the file.
  1938. function A Lisp function, which will be called with two arguments:
  1939. the file path and the original link string, without the
  1940. \"file:\" prefix.
  1941. For more examples, see the system specific constants
  1942. `org-file-apps-defaults-macosx'
  1943. `org-file-apps-defaults-windowsnt'
  1944. `org-file-apps-defaults-gnu'."
  1945. :group 'org-link-follow
  1946. :type '(repeat
  1947. (cons (choice :value ""
  1948. (string :tag "Extension")
  1949. (const :tag "System command to open files" system)
  1950. (const :tag "Default for unrecognized files" t)
  1951. (const :tag "Remote file" remote)
  1952. (const :tag "Links to a directory" directory)
  1953. (const :tag "Any files that have Emacs modes"
  1954. auto-mode))
  1955. (choice :value ""
  1956. (const :tag "Visit with Emacs" emacs)
  1957. (const :tag "Use default" default)
  1958. (const :tag "Use the system command" system)
  1959. (string :tag "Command")
  1960. (function :tag "Function")))))
  1961. (defcustom org-doi-server-url "http://dx.doi.org/"
  1962. "The URL of the DOI server."
  1963. :type 'string
  1964. :version "24.3"
  1965. :group 'org-link-follow)
  1966. (defgroup org-refile nil
  1967. "Options concerning refiling entries in Org-mode."
  1968. :tag "Org Refile"
  1969. :group 'org)
  1970. (defcustom org-directory "~/org"
  1971. "Directory with Org files.
  1972. This is just a default location to look for Org files. There is no need
  1973. at all to put your files into this directory. It is used in the
  1974. following situations:
  1975. 1. When a capture template specifies a target file that is not an
  1976. absolute path. The path will then be interpreted relative to
  1977. `org-directory'
  1978. 2. When the value of variable `org-agenda-files' is a single file, any
  1979. relative paths in this file will be taken as relative to
  1980. `org-directory'."
  1981. :group 'org-refile
  1982. :group 'org-capture
  1983. :type 'directory)
  1984. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1985. "Default target for storing notes.
  1986. Used as a fall back file for org-capture.el, for templates that
  1987. do not specify a target file."
  1988. :group 'org-refile
  1989. :group 'org-capture
  1990. :type 'file)
  1991. (defcustom org-goto-interface 'outline
  1992. "The default interface to be used for `org-goto'.
  1993. Allowed values are:
  1994. outline The interface shows an outline of the relevant file
  1995. and the correct heading is found by moving through
  1996. the outline or by searching with incremental search.
  1997. outline-path-completion Headlines in the current buffer are offered via
  1998. completion. This is the interface also used by
  1999. the refile command."
  2000. :group 'org-refile
  2001. :type '(choice
  2002. (const :tag "Outline" outline)
  2003. (const :tag "Outline-path-completion" outline-path-completion)))
  2004. (defcustom org-goto-max-level 5
  2005. "Maximum target level when running `org-goto' with refile interface."
  2006. :group 'org-refile
  2007. :type 'integer)
  2008. (defcustom org-reverse-note-order nil
  2009. "Non-nil means store new notes at the beginning of a file or entry.
  2010. When nil, new notes will be filed to the end of a file or entry.
  2011. This can also be a list with cons cells of regular expressions that
  2012. are matched against file names, and values."
  2013. :group 'org-capture
  2014. :group 'org-refile
  2015. :type '(choice
  2016. (const :tag "Reverse always" t)
  2017. (const :tag "Reverse never" nil)
  2018. (repeat :tag "By file name regexp"
  2019. (cons regexp boolean))))
  2020. (defcustom org-log-refile nil
  2021. "Information to record when a task is refiled.
  2022. Possible values are:
  2023. nil Don't add anything
  2024. time Add a time stamp to the task
  2025. note Prompt for a note and add it with template `org-log-note-headings'
  2026. This option can also be set with on a per-file-basis with
  2027. #+STARTUP: nologrefile
  2028. #+STARTUP: logrefile
  2029. #+STARTUP: lognoterefile
  2030. You can have local logging settings for a subtree by setting the LOGGING
  2031. property to one or more of these keywords.
  2032. When bulk-refiling from the agenda, the value `note' is forbidden and
  2033. will temporarily be changed to `time'."
  2034. :group 'org-refile
  2035. :group 'org-progress
  2036. :version "24.1"
  2037. :type '(choice
  2038. (const :tag "No logging" nil)
  2039. (const :tag "Record timestamp" time)
  2040. (const :tag "Record timestamp with note." note)))
  2041. (defcustom org-refile-targets nil
  2042. "Targets for refiling entries with \\[org-refile].
  2043. This is a list of cons cells. Each cell contains:
  2044. - a specification of the files to be considered, either a list of files,
  2045. or a symbol whose function or variable value will be used to retrieve
  2046. a file name or a list of file names. If you use `org-agenda-files' for
  2047. that, all agenda files will be scanned for targets. Nil means consider
  2048. headings in the current buffer.
  2049. - A specification of how to find candidate refile targets. This may be
  2050. any of:
  2051. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2052. This tag has to be present in all target headlines, inheritance will
  2053. not be considered.
  2054. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2055. todo keyword.
  2056. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2057. headlines that are refiling targets.
  2058. - a cons cell (:level . N). Any headline of level N is considered a target.
  2059. Note that, when `org-odd-levels-only' is set, level corresponds to
  2060. order in hierarchy, not to the number of stars.
  2061. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2062. Note that, when `org-odd-levels-only' is set, level corresponds to
  2063. order in hierarchy, not to the number of stars.
  2064. Each element of this list generates a set of possible targets.
  2065. The union of these sets is presented (with completion) to
  2066. the user by `org-refile'.
  2067. You can set the variable `org-refile-target-verify-function' to a function
  2068. to verify each headline found by the simple criteria above.
  2069. When this variable is nil, all top-level headlines in the current buffer
  2070. are used, equivalent to the value `((nil . (:level . 1))'."
  2071. :group 'org-refile
  2072. :type '(repeat
  2073. (cons
  2074. (choice :value org-agenda-files
  2075. (const :tag "All agenda files" org-agenda-files)
  2076. (const :tag "Current buffer" nil)
  2077. (function) (variable) (file))
  2078. (choice :tag "Identify target headline by"
  2079. (cons :tag "Specific tag" (const :value :tag) (string))
  2080. (cons :tag "TODO keyword" (const :value :todo) (string))
  2081. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2082. (cons :tag "Level number" (const :value :level) (integer))
  2083. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2084. (defcustom org-refile-target-verify-function nil
  2085. "Function to verify if the headline at point should be a refile target.
  2086. The function will be called without arguments, with point at the
  2087. beginning of the headline. It should return t and leave point
  2088. where it is if the headline is a valid target for refiling.
  2089. If the target should not be selected, the function must return nil.
  2090. In addition to this, it may move point to a place from where the search
  2091. should be continued. For example, the function may decide that the entire
  2092. subtree of the current entry should be excluded and move point to the end
  2093. of the subtree."
  2094. :group 'org-refile
  2095. :type '(choice
  2096. (const nil)
  2097. (function)))
  2098. (defcustom org-refile-use-cache nil
  2099. "Non-nil means cache refile targets to speed up the process.
  2100. \\<org-mode-map>\
  2101. The cache for a particular file will be updated automatically when
  2102. the buffer has been killed, or when any of the marker used for flagging
  2103. refile targets no longer points at a live buffer.
  2104. If you have added new entries to a buffer that might themselves be targets,
  2105. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2106. if you find that easier, \
  2107. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2108. \\[org-refile]'."
  2109. :group 'org-refile
  2110. :version "24.1"
  2111. :type 'boolean)
  2112. (defcustom org-refile-use-outline-path nil
  2113. "Non-nil means provide refile targets as paths.
  2114. So a level 3 headline will be available as level1/level2/level3.
  2115. When the value is `file', also include the file name (without directory)
  2116. into the path. In this case, you can also stop the completion after
  2117. the file name, to get entries inserted as top level in the file.
  2118. When `full-file-path', include the full file path."
  2119. :group 'org-refile
  2120. :type '(choice
  2121. (const :tag "Not" nil)
  2122. (const :tag "Yes" t)
  2123. (const :tag "Start with file name" file)
  2124. (const :tag "Start with full file path" full-file-path)))
  2125. (defcustom org-outline-path-complete-in-steps t
  2126. "Non-nil means complete the outline path in hierarchical steps.
  2127. When Org-mode uses the refile interface to select an outline path
  2128. \(see variable `org-refile-use-outline-path'), the completion of
  2129. the path can be done in a single go, or it can be done in steps down
  2130. the headline hierarchy. Going in steps is probably the best if you
  2131. do not use a special completion package like `ido' or `icicles'.
  2132. However, when using these packages, going in one step can be very
  2133. fast, while still showing the whole path to the entry."
  2134. :group 'org-refile
  2135. :type 'boolean)
  2136. (defcustom org-refile-allow-creating-parent-nodes nil
  2137. "Non-nil means allow the creation of new nodes as refile targets.
  2138. New nodes are then created by adding \"/new node name\" to the completion
  2139. of an existing node. When the value of this variable is `confirm',
  2140. new node creation must be confirmed by the user (recommended).
  2141. When nil, the completion must match an existing entry.
  2142. Note that, if the new heading is not seen by the criteria
  2143. listed in `org-refile-targets', multiple instances of the same
  2144. heading would be created by trying again to file under the new
  2145. heading."
  2146. :group 'org-refile
  2147. :type '(choice
  2148. (const :tag "Never" nil)
  2149. (const :tag "Always" t)
  2150. (const :tag "Prompt for confirmation" confirm)))
  2151. (defcustom org-refile-active-region-within-subtree nil
  2152. "Non-nil means also refile active region within a subtree.
  2153. By default `org-refile' doesn't allow refiling regions if they
  2154. don't contain a set of subtrees, but it might be convenient to
  2155. do so sometimes: in that case, the first line of the region is
  2156. converted to a headline before refiling."
  2157. :group 'org-refile
  2158. :version "24.1"
  2159. :type 'boolean)
  2160. (defgroup org-todo nil
  2161. "Options concerning TODO items in Org-mode."
  2162. :tag "Org TODO"
  2163. :group 'org)
  2164. (defgroup org-progress nil
  2165. "Options concerning Progress logging in Org-mode."
  2166. :tag "Org Progress"
  2167. :group 'org-time)
  2168. (defvar org-todo-interpretation-widgets
  2169. '((:tag "Sequence (cycling hits every state)" sequence)
  2170. (:tag "Type (cycling directly to DONE)" type))
  2171. "The available interpretation symbols for customizing `org-todo-keywords'.
  2172. Interested libraries should add to this list.")
  2173. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2174. "List of TODO entry keyword sequences and their interpretation.
  2175. \\<org-mode-map>This is a list of sequences.
  2176. Each sequence starts with a symbol, either `sequence' or `type',
  2177. indicating if the keywords should be interpreted as a sequence of
  2178. action steps, or as different types of TODO items. The first
  2179. keywords are states requiring action - these states will select a headline
  2180. for inclusion into the global TODO list Org-mode produces. If one of
  2181. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2182. signify that no further action is necessary. If \"|\" is not found,
  2183. the last keyword is treated as the only DONE state of the sequence.
  2184. The command \\[org-todo] cycles an entry through these states, and one
  2185. additional state where no keyword is present. For details about this
  2186. cycling, see the manual.
  2187. TODO keywords and interpretation can also be set on a per-file basis with
  2188. the special #+SEQ_TODO and #+TYP_TODO lines.
  2189. Each keyword can optionally specify a character for fast state selection
  2190. \(in combination with the variable `org-use-fast-todo-selection')
  2191. and specifiers for state change logging, using the same syntax that
  2192. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2193. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2194. indicates to record a time stamp each time this state is selected.
  2195. Each keyword may also specify if a timestamp or a note should be
  2196. recorded when entering or leaving the state, by adding additional
  2197. characters in the parenthesis after the keyword. This looks like this:
  2198. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2199. record only the time of the state change. With X and Y being either
  2200. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2201. Y when leaving the state if and only if the *target* state does not
  2202. define X. You may omit any of the fast-selection key or X or /Y,
  2203. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2204. For backward compatibility, this variable may also be just a list
  2205. of keywords. In this case the interpretation (sequence or type) will be
  2206. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2207. :group 'org-todo
  2208. :group 'org-keywords
  2209. :type '(choice
  2210. (repeat :tag "Old syntax, just keywords"
  2211. (string :tag "Keyword"))
  2212. (repeat :tag "New syntax"
  2213. (cons
  2214. (choice
  2215. :tag "Interpretation"
  2216. ;;Quick and dirty way to see
  2217. ;;`org-todo-interpretations'. This takes the
  2218. ;;place of item arguments
  2219. :convert-widget
  2220. (lambda (widget)
  2221. (widget-put widget
  2222. :args (mapcar
  2223. (lambda (x)
  2224. (widget-convert
  2225. (cons 'const x)))
  2226. org-todo-interpretation-widgets))
  2227. widget))
  2228. (repeat
  2229. (string :tag "Keyword"))))))
  2230. (defvar-local org-todo-keywords-1 nil
  2231. "All TODO and DONE keywords active in a buffer.")
  2232. (defvar org-todo-keywords-for-agenda nil)
  2233. (defvar org-done-keywords-for-agenda nil)
  2234. (defvar org-todo-keyword-alist-for-agenda nil)
  2235. (defvar org-tag-alist-for-agenda nil
  2236. "Alist of all tags from all agenda files.")
  2237. (defvar org-tag-groups-alist-for-agenda nil
  2238. "Alist of all groups tags from all current agenda files.")
  2239. (defvar-local org-tag-groups-alist nil)
  2240. (defvar org-agenda-contributing-files nil)
  2241. (defvar-local org-not-done-keywords nil)
  2242. (defvar-local org-done-keywords nil)
  2243. (defvar-local org-todo-heads nil)
  2244. (defvar-local org-todo-sets nil)
  2245. (defvar-local org-todo-log-states nil)
  2246. (defvar-local org-todo-kwd-alist nil)
  2247. (defvar-local org-todo-key-alist nil)
  2248. (defvar-local org-todo-key-trigger nil)
  2249. (defcustom org-todo-interpretation 'sequence
  2250. "Controls how TODO keywords are interpreted.
  2251. This variable is in principle obsolete and is only used for
  2252. backward compatibility, if the interpretation of todo keywords is
  2253. not given already in `org-todo-keywords'. See that variable for
  2254. more information."
  2255. :group 'org-todo
  2256. :group 'org-keywords
  2257. :type '(choice (const sequence)
  2258. (const type)))
  2259. (defcustom org-use-fast-todo-selection t
  2260. "\\<org-mode-map>\
  2261. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2262. This variable describes if and under what circumstances the cycling
  2263. mechanism for TODO keywords will be replaced by a single-key, direct
  2264. selection scheme.
  2265. When nil, fast selection is never used.
  2266. When the symbol `prefix', it will be used when `org-todo' is called
  2267. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2268. in an Org-mode buffer, and
  2269. `\\[universal-argument] t' in an agenda buffer.
  2270. When t, fast selection is used by default. In this case, the prefix
  2271. argument forces cycling instead.
  2272. In all cases, the special interface is only used if access keys have
  2273. actually been assigned by the user, i.e. if keywords in the configuration
  2274. are followed by a letter in parenthesis, like TODO(t)."
  2275. :group 'org-todo
  2276. :type '(choice
  2277. (const :tag "Never" nil)
  2278. (const :tag "By default" t)
  2279. (const :tag "Only with C-u C-c C-t" prefix)))
  2280. (defcustom org-provide-todo-statistics t
  2281. "Non-nil means update todo statistics after insert and toggle.
  2282. ALL-HEADLINES means update todo statistics by including headlines
  2283. with no TODO keyword as well, counting them as not done.
  2284. A list of TODO keywords means the same, but skip keywords that are
  2285. not in this list.
  2286. When set to a list of two lists, the first list contains keywords
  2287. to consider as TODO keywords, the second list contains keywords
  2288. to consider as DONE keywords.
  2289. When this is set, todo statistics is updated in the parent of the
  2290. current entry each time a todo state is changed."
  2291. :group 'org-todo
  2292. :type '(choice
  2293. (const :tag "Yes, only for TODO entries" t)
  2294. (const :tag "Yes, including all entries" all-headlines)
  2295. (repeat :tag "Yes, for TODOs in this list"
  2296. (string :tag "TODO keyword"))
  2297. (list :tag "Yes, for TODOs and DONEs in these lists"
  2298. (repeat (string :tag "TODO keyword"))
  2299. (repeat (string :tag "DONE keyword")))
  2300. (other :tag "No TODO statistics" nil)))
  2301. (defcustom org-hierarchical-todo-statistics t
  2302. "Non-nil means TODO statistics covers just direct children.
  2303. When nil, all entries in the subtree are considered.
  2304. This has only an effect if `org-provide-todo-statistics' is set.
  2305. To set this to nil for only a single subtree, use a COOKIE_DATA
  2306. property and include the word \"recursive\" into the value."
  2307. :group 'org-todo
  2308. :type 'boolean)
  2309. (defcustom org-after-todo-state-change-hook nil
  2310. "Hook which is run after the state of a TODO item was changed.
  2311. The new state (a string with a TODO keyword, or nil) is available in the
  2312. Lisp variable `org-state'."
  2313. :group 'org-todo
  2314. :type 'hook)
  2315. (defvar org-blocker-hook nil
  2316. "Hook for functions that are allowed to block a state change.
  2317. Functions in this hook should not modify the buffer.
  2318. Each function gets as its single argument a property list,
  2319. see `org-trigger-hook' for more information about this list.
  2320. If any of the functions in this hook returns nil, the state change
  2321. is blocked.")
  2322. (defvar org-trigger-hook nil
  2323. "Hook for functions that are triggered by a state change.
  2324. Each function gets as its single argument a property list with at
  2325. least the following elements:
  2326. (:type type-of-change :position pos-at-entry-start
  2327. :from old-state :to new-state)
  2328. Depending on the type, more properties may be present.
  2329. This mechanism is currently implemented for:
  2330. TODO state changes
  2331. ------------------
  2332. :type todo-state-change
  2333. :from previous state (keyword as a string), or nil, or a symbol
  2334. `todo' or `done', to indicate the general type of state.
  2335. :to new state, like in :from")
  2336. (defcustom org-enforce-todo-dependencies nil
  2337. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2338. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2339. be blocked if any prior sibling is not yet done.
  2340. Finally, if the parent is blocked because of ordered siblings of its own,
  2341. the child will also be blocked."
  2342. :set (lambda (var val)
  2343. (set var val)
  2344. (if val
  2345. (add-hook 'org-blocker-hook
  2346. 'org-block-todo-from-children-or-siblings-or-parent)
  2347. (remove-hook 'org-blocker-hook
  2348. 'org-block-todo-from-children-or-siblings-or-parent)))
  2349. :group 'org-todo
  2350. :type 'boolean)
  2351. (defcustom org-enforce-todo-checkbox-dependencies nil
  2352. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2353. When this is nil, checkboxes have no influence on switching TODO states.
  2354. When non-nil, you first need to check off all check boxes before the TODO
  2355. entry can be switched to DONE.
  2356. This variable needs to be set before org.el is loaded, and you need to
  2357. restart Emacs after a change to make the change effective. The only way
  2358. to change is while Emacs is running is through the customize interface."
  2359. :set (lambda (var val)
  2360. (set var val)
  2361. (if val
  2362. (add-hook 'org-blocker-hook
  2363. 'org-block-todo-from-checkboxes)
  2364. (remove-hook 'org-blocker-hook
  2365. 'org-block-todo-from-checkboxes)))
  2366. :group 'org-todo
  2367. :type 'boolean)
  2368. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2369. "Non-nil means inserting a TODO heading is treated as state change.
  2370. So when the command \\[org-insert-todo-heading] is used, state change
  2371. logging will apply if appropriate. When nil, the new TODO item will
  2372. be inserted directly, and no logging will take place."
  2373. :group 'org-todo
  2374. :type 'boolean)
  2375. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2376. "Non-nil means switching TODO states with S-cursor counts as state change.
  2377. This is the default behavior. However, setting this to nil allows a
  2378. convenient way to select a TODO state and bypass any logging associated
  2379. with that."
  2380. :group 'org-todo
  2381. :type 'boolean)
  2382. (defcustom org-todo-state-tags-triggers nil
  2383. "Tag changes that should be triggered by TODO state changes.
  2384. This is a list. Each entry is
  2385. (state-change (tag . flag) .......)
  2386. State-change can be a string with a state, and empty string to indicate the
  2387. state that has no TODO keyword, or it can be one of the symbols `todo'
  2388. or `done', meaning any not-done or done state, respectively."
  2389. :group 'org-todo
  2390. :group 'org-tags
  2391. :type '(repeat
  2392. (cons (choice :tag "When changing to"
  2393. (const :tag "Not-done state" todo)
  2394. (const :tag "Done state" done)
  2395. (string :tag "State"))
  2396. (repeat
  2397. (cons :tag "Tag action"
  2398. (string :tag "Tag")
  2399. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2400. (defcustom org-log-done nil
  2401. "Information to record when a task moves to the DONE state.
  2402. Possible values are:
  2403. nil Don't add anything, just change the keyword
  2404. time Add a time stamp to the task
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologdone
  2408. #+STARTUP: logdone
  2409. #+STARTUP: lognotedone
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "No logging" nil)
  2416. (const :tag "Record CLOSED timestamp" time)
  2417. (const :tag "Record CLOSED timestamp with note." note)))
  2418. ;; Normalize old uses of org-log-done.
  2419. (cond
  2420. ((eq org-log-done t) (setq org-log-done 'time))
  2421. ((and (listp org-log-done) (memq 'done org-log-done))
  2422. (setq org-log-done 'note)))
  2423. (defcustom org-log-reschedule nil
  2424. "Information to record when the scheduling date of a tasks is modified.
  2425. Possible values are:
  2426. nil Don't add anything, just change the date
  2427. time Add a time stamp to the task
  2428. note Prompt for a note and add it with template `org-log-note-headings'
  2429. This option can also be set with on a per-file-basis with
  2430. #+STARTUP: nologreschedule
  2431. #+STARTUP: logreschedule
  2432. #+STARTUP: lognotereschedule"
  2433. :group 'org-todo
  2434. :group 'org-progress
  2435. :type '(choice
  2436. (const :tag "No logging" nil)
  2437. (const :tag "Record timestamp" time)
  2438. (const :tag "Record timestamp with note." note)))
  2439. (defcustom org-log-redeadline nil
  2440. "Information to record when the deadline date of a tasks is modified.
  2441. Possible values are:
  2442. nil Don't add anything, just change the date
  2443. time Add a time stamp to the task
  2444. note Prompt for a note and add it with template `org-log-note-headings'
  2445. This option can also be set with on a per-file-basis with
  2446. #+STARTUP: nologredeadline
  2447. #+STARTUP: logredeadline
  2448. #+STARTUP: lognoteredeadline
  2449. You can have local logging settings for a subtree by setting the LOGGING
  2450. property to one or more of these keywords."
  2451. :group 'org-todo
  2452. :group 'org-progress
  2453. :type '(choice
  2454. (const :tag "No logging" nil)
  2455. (const :tag "Record timestamp" time)
  2456. (const :tag "Record timestamp with note." note)))
  2457. (defcustom org-log-note-clock-out nil
  2458. "Non-nil means record a note when clocking out of an item.
  2459. This can also be configured on a per-file basis by adding one of
  2460. the following lines anywhere in the buffer:
  2461. #+STARTUP: lognoteclock-out
  2462. #+STARTUP: nolognoteclock-out"
  2463. :group 'org-todo
  2464. :group 'org-progress
  2465. :type 'boolean)
  2466. (defcustom org-log-done-with-time t
  2467. "Non-nil means the CLOSED time stamp will contain date and time.
  2468. When nil, only the date will be recorded."
  2469. :group 'org-progress
  2470. :type 'boolean)
  2471. (defcustom org-log-note-headings
  2472. '((done . "CLOSING NOTE %t")
  2473. (state . "State %-12s from %-12S %t")
  2474. (note . "Note taken on %t")
  2475. (reschedule . "Rescheduled from %S on %t")
  2476. (delschedule . "Not scheduled, was %S on %t")
  2477. (redeadline . "New deadline from %S on %t")
  2478. (deldeadline . "Removed deadline, was %S on %t")
  2479. (refile . "Refiled on %t")
  2480. (clock-out . ""))
  2481. "Headings for notes added to entries.
  2482. The value is an alist, with the car being a symbol indicating the
  2483. note context, and the cdr is the heading to be used. The heading
  2484. may also be the empty string. The following placeholders can be
  2485. used:
  2486. %t a time stamp.
  2487. %T an active time stamp instead the default inactive one
  2488. %d a short-format time stamp.
  2489. %D an active short-format time stamp.
  2490. %s the new TODO state or time stamp (inactive), in double quotes.
  2491. %S the old TODO state or time stamp (inactive), in double quotes.
  2492. %u the user name.
  2493. %U full user name.
  2494. In fact, it is not a good idea to change the `state' entry,
  2495. because Agenda Log mode depends on the format of these entries."
  2496. :group 'org-todo
  2497. :group 'org-progress
  2498. :type '(list :greedy t
  2499. (cons (const :tag "Heading when closing an item" done) string)
  2500. (cons (const :tag
  2501. "Heading when changing todo state (todo sequence only)"
  2502. state) string)
  2503. (cons (const :tag "Heading when just taking a note" note) string)
  2504. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2505. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2506. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2507. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2508. (cons (const :tag "Heading when refiling" refile) string)
  2509. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2510. (unless (assq 'note org-log-note-headings)
  2511. (push '(note . "%t") org-log-note-headings))
  2512. (defcustom org-log-into-drawer nil
  2513. "Non-nil means insert state change notes and time stamps into a drawer.
  2514. When nil, state changes notes will be inserted after the headline and
  2515. any scheduling and clock lines, but not inside a drawer.
  2516. The value of this variable should be the name of the drawer to use.
  2517. LOGBOOK is proposed as the default drawer for this purpose, you can
  2518. also set this to a string to define the drawer of your choice.
  2519. A value of t is also allowed, representing \"LOGBOOK\".
  2520. A value of t or nil can also be set with on a per-file-basis with
  2521. #+STARTUP: logdrawer
  2522. #+STARTUP: nologdrawer
  2523. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2524. will be ignored.
  2525. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2526. a subtree.
  2527. Do not check directly this variable in a Lisp program. Call
  2528. function `org-log-into-drawer' instead."
  2529. :group 'org-todo
  2530. :group 'org-progress
  2531. :type '(choice
  2532. (const :tag "Not into a drawer" nil)
  2533. (const :tag "LOGBOOK" t)
  2534. (string :tag "Other")))
  2535. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2536. (defun org-log-into-drawer ()
  2537. "Name of the log drawer, as a string, or nil.
  2538. This is the value of `org-log-into-drawer'. However, if the
  2539. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2540. be used instead of the default value."
  2541. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2542. (cond ((equal p "nil") nil)
  2543. ((equal p "t") "LOGBOOK")
  2544. ((stringp p) p)
  2545. (p "LOGBOOK")
  2546. ((stringp org-log-into-drawer) org-log-into-drawer)
  2547. (org-log-into-drawer "LOGBOOK"))))
  2548. (defcustom org-log-state-notes-insert-after-drawers nil
  2549. "Non-nil means insert state change notes after any drawers in entry.
  2550. Only the drawers that *immediately* follow the headline and the
  2551. deadline/scheduled line are skipped.
  2552. When nil, insert notes right after the heading and perhaps the line
  2553. with deadline/scheduling if present.
  2554. This variable will have no effect if `org-log-into-drawer' is
  2555. set."
  2556. :group 'org-todo
  2557. :group 'org-progress
  2558. :type 'boolean)
  2559. (defcustom org-log-states-order-reversed t
  2560. "Non-nil means the latest state note will be directly after heading.
  2561. When nil, the state change notes will be ordered according to time.
  2562. This option can also be set with on a per-file-basis with
  2563. #+STARTUP: logstatesreversed
  2564. #+STARTUP: nologstatesreversed"
  2565. :group 'org-todo
  2566. :group 'org-progress
  2567. :type 'boolean)
  2568. (defcustom org-todo-repeat-to-state nil
  2569. "The TODO state to which a repeater should return the repeating task.
  2570. By default this is the first task in a TODO sequence, or the previous state
  2571. in a TODO_TYP set. But you can specify another task here.
  2572. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2573. :group 'org-todo
  2574. :version "24.1"
  2575. :type '(choice (const :tag "Head of sequence" nil)
  2576. (string :tag "Specific state")))
  2577. (defcustom org-log-repeat 'time
  2578. "Non-nil means record moving through the DONE state when triggering repeat.
  2579. An auto-repeating task is immediately switched back to TODO when
  2580. marked DONE. If you are not logging state changes (by adding \"@\"
  2581. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2582. record a closing note, there will be no record of the task moving
  2583. through DONE. This variable forces taking a note anyway.
  2584. nil Don't force a record
  2585. time Record a time stamp
  2586. note Prompt for a note and add it with template `org-log-note-headings'
  2587. This option can also be set with on a per-file-basis with
  2588. #+STARTUP: nologrepeat
  2589. #+STARTUP: logrepeat
  2590. #+STARTUP: lognoterepeat
  2591. You can have local logging settings for a subtree by setting the LOGGING
  2592. property to one or more of these keywords."
  2593. :group 'org-todo
  2594. :group 'org-progress
  2595. :type '(choice
  2596. (const :tag "Don't force a record" nil)
  2597. (const :tag "Force recording the DONE state" time)
  2598. (const :tag "Force recording a note with the DONE state" note)))
  2599. (defgroup org-priorities nil
  2600. "Priorities in Org-mode."
  2601. :tag "Org Priorities"
  2602. :group 'org-todo)
  2603. (defcustom org-enable-priority-commands t
  2604. "Non-nil means priority commands are active.
  2605. When nil, these commands will be disabled, so that you never accidentally
  2606. set a priority."
  2607. :group 'org-priorities
  2608. :type 'boolean)
  2609. (defcustom org-highest-priority ?A
  2610. "The highest priority of TODO items. A character like ?A, ?B etc.
  2611. Must have a smaller ASCII number than `org-lowest-priority'."
  2612. :group 'org-priorities
  2613. :type 'character)
  2614. (defcustom org-lowest-priority ?C
  2615. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2616. Must have a larger ASCII number than `org-highest-priority'."
  2617. :group 'org-priorities
  2618. :type 'character)
  2619. (defcustom org-default-priority ?B
  2620. "The default priority of TODO items.
  2621. This is the priority an item gets if no explicit priority is given.
  2622. When starting to cycle on an empty priority the first step in the cycle
  2623. depends on `org-priority-start-cycle-with-default'. The resulting first
  2624. step priority must not exceed the range from `org-highest-priority' to
  2625. `org-lowest-priority' which means that `org-default-priority' has to be
  2626. in this range exclusive or inclusive the range boundaries. Else the
  2627. first step refuses to set the default and the second will fall back
  2628. to (depending on the command used) the highest or lowest priority."
  2629. :group 'org-priorities
  2630. :type 'character)
  2631. (defcustom org-priority-start-cycle-with-default t
  2632. "Non-nil means start with default priority when starting to cycle.
  2633. When this is nil, the first step in the cycle will be (depending on the
  2634. command used) one higher or lower than the default priority.
  2635. See also `org-default-priority'."
  2636. :group 'org-priorities
  2637. :type 'boolean)
  2638. (defcustom org-get-priority-function nil
  2639. "Function to extract the priority from a string.
  2640. The string is normally the headline. If this is nil Org computes the
  2641. priority from the priority cookie like [#A] in the headline. It returns
  2642. an integer, increasing by 1000 for each priority level.
  2643. The user can set a different function here, which should take a string
  2644. as an argument and return the numeric priority."
  2645. :group 'org-priorities
  2646. :version "24.1"
  2647. :type '(choice
  2648. (const nil)
  2649. (function)))
  2650. (defgroup org-time nil
  2651. "Options concerning time stamps and deadlines in Org-mode."
  2652. :tag "Org Time"
  2653. :group 'org)
  2654. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2655. "Number of minutes to round time stamps to.
  2656. \\<org-mode-map>\
  2657. These are two values, the first applies when first creating a time stamp.
  2658. The second applies when changing it with the commands `S-up' and `S-down'.
  2659. When changing the time stamp, this means that it will change in steps
  2660. of N minutes, as given by the second value.
  2661. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2662. numbers should be factors of 60, so for example 5, 10, 15.
  2663. When this is larger than 1, you can still force an exact time stamp by using
  2664. a double prefix argument to a time stamp command like \
  2665. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2666. and by using a prefix arg to `S-up/down' to specify the exact number
  2667. of minutes to shift."
  2668. :group 'org-time
  2669. :get (lambda (var) ; Make sure both elements are there
  2670. (if (integerp (default-value var))
  2671. (list (default-value var) 5)
  2672. (default-value var)))
  2673. :type '(list
  2674. (integer :tag "when inserting times")
  2675. (integer :tag "when modifying times")))
  2676. ;; Normalize old customizations of this variable.
  2677. (when (integerp org-time-stamp-rounding-minutes)
  2678. (setq org-time-stamp-rounding-minutes
  2679. (list org-time-stamp-rounding-minutes
  2680. org-time-stamp-rounding-minutes)))
  2681. (defcustom org-display-custom-times nil
  2682. "Non-nil means overlay custom formats over all time stamps.
  2683. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2684. To turn this on on a per-file basis, insert anywhere in the file:
  2685. #+STARTUP: customtime"
  2686. :group 'org-time
  2687. :set 'set-default
  2688. :type 'sexp)
  2689. (make-variable-buffer-local 'org-display-custom-times)
  2690. (defcustom org-time-stamp-custom-formats
  2691. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2692. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2693. These are overlaid over the default ISO format if the variable
  2694. `org-display-custom-times' is set. Time like %H:%M should be at the
  2695. end of the second format. The custom formats are also honored by export
  2696. commands, if custom time display is turned on at the time of export."
  2697. :group 'org-time
  2698. :type 'sexp)
  2699. (defun org-time-stamp-format (&optional long inactive)
  2700. "Get the right format for a time string."
  2701. (let ((f (if long (cdr org-time-stamp-formats)
  2702. (car org-time-stamp-formats))))
  2703. (if inactive
  2704. (concat "[" (substring f 1 -1) "]")
  2705. f)))
  2706. (defcustom org-time-clocksum-format
  2707. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2708. "The format string used when creating CLOCKSUM lines.
  2709. This is also used when Org mode generates a time duration.
  2710. The value can be a single format string containing two
  2711. %-sequences, which will be filled with the number of hours and
  2712. minutes in that order.
  2713. Alternatively, the value can be a plist associating any of the
  2714. keys :years, :months, :weeks, :days, :hours or :minutes with
  2715. format strings. The time duration is formatted using only the
  2716. time components that are needed and concatenating the results.
  2717. If a time unit in absent, it falls back to the next smallest
  2718. unit.
  2719. The keys :require-years, :require-months, :require-days,
  2720. :require-weeks, :require-hours, :require-minutes are also
  2721. meaningful. A non-nil value for these keys indicates that the
  2722. corresponding time component should always be included, even if
  2723. its value is 0.
  2724. For example,
  2725. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2726. :require-minutes t)
  2727. means durations longer than a day will be expressed in days,
  2728. hours and minutes, and durations less than a day will always be
  2729. expressed in hours and minutes (even for durations less than an
  2730. hour).
  2731. The value
  2732. (:days \"%dd\" :minutes \"%dm\")
  2733. means durations longer than a day will be expressed in days and
  2734. minutes, and durations less than a day will be expressed entirely
  2735. in minutes (even for durations longer than an hour)."
  2736. :group 'org-time
  2737. :group 'org-clock
  2738. :version "24.4"
  2739. :package-version '(Org . "8.0")
  2740. :type '(choice (string :tag "Format string")
  2741. (set :tag "Plist"
  2742. (group :inline t (const :tag "Years" :years)
  2743. (string :tag "Format string"))
  2744. (group :inline t
  2745. (const :tag "Always show years" :require-years)
  2746. (const t))
  2747. (group :inline t (const :tag "Months" :months)
  2748. (string :tag "Format string"))
  2749. (group :inline t
  2750. (const :tag "Always show months" :require-months)
  2751. (const t))
  2752. (group :inline t (const :tag "Weeks" :weeks)
  2753. (string :tag "Format string"))
  2754. (group :inline t
  2755. (const :tag "Always show weeks" :require-weeks)
  2756. (const t))
  2757. (group :inline t (const :tag "Days" :days)
  2758. (string :tag "Format string"))
  2759. (group :inline t
  2760. (const :tag "Always show days" :require-days)
  2761. (const t))
  2762. (group :inline t (const :tag "Hours" :hours)
  2763. (string :tag "Format string"))
  2764. (group :inline t
  2765. (const :tag "Always show hours" :require-hours)
  2766. (const t))
  2767. (group :inline t (const :tag "Minutes" :minutes)
  2768. (string :tag "Format string"))
  2769. (group :inline t
  2770. (const :tag "Always show minutes" :require-minutes)
  2771. (const t)))))
  2772. (defcustom org-time-clocksum-use-fractional nil
  2773. "When non-nil, \\[org-clock-display] uses fractional times.
  2774. See `org-time-clocksum-format' for more on time clock formats."
  2775. :group 'org-time
  2776. :group 'org-clock
  2777. :version "24.3"
  2778. :type 'boolean)
  2779. (defcustom org-time-clocksum-use-effort-durations nil
  2780. "When non-nil, \\[org-clock-display] uses effort durations.
  2781. E.g. by default, one day is considered to be a 8 hours effort,
  2782. so a task that has been clocked for 16 hours will be displayed
  2783. as during 2 days in the clock display or in the clocktable.
  2784. See `org-effort-durations' on how to set effort durations
  2785. and `org-time-clocksum-format' for more on time clock formats."
  2786. :group 'org-time
  2787. :group 'org-clock
  2788. :version "24.4"
  2789. :package-version '(Org . "8.0")
  2790. :type 'boolean)
  2791. (defcustom org-time-clocksum-fractional-format "%.2f"
  2792. "The format string used when creating CLOCKSUM lines,
  2793. or when Org mode generates a time duration, if
  2794. `org-time-clocksum-use-fractional' is enabled.
  2795. The value can be a single format string containing one
  2796. %-sequence, which will be filled with the number of hours as
  2797. a float.
  2798. Alternatively, the value can be a plist associating any of the
  2799. keys :years, :months, :weeks, :days, :hours or :minutes with
  2800. a format string. The time duration is formatted using the
  2801. largest time unit which gives a non-zero integer part. If all
  2802. specified formats have zero integer part, the smallest time unit
  2803. is used."
  2804. :group 'org-time
  2805. :type '(choice (string :tag "Format string")
  2806. (set (group :inline t (const :tag "Years" :years)
  2807. (string :tag "Format string"))
  2808. (group :inline t (const :tag "Months" :months)
  2809. (string :tag "Format string"))
  2810. (group :inline t (const :tag "Weeks" :weeks)
  2811. (string :tag "Format string"))
  2812. (group :inline t (const :tag "Days" :days)
  2813. (string :tag "Format string"))
  2814. (group :inline t (const :tag "Hours" :hours)
  2815. (string :tag "Format string"))
  2816. (group :inline t (const :tag "Minutes" :minutes)
  2817. (string :tag "Format string")))))
  2818. (defcustom org-deadline-warning-days 14
  2819. "Number of days before expiration during which a deadline becomes active.
  2820. This variable governs the display in sparse trees and in the agenda.
  2821. When 0 or negative, it means use this number (the absolute value of it)
  2822. even if a deadline has a different individual lead time specified.
  2823. Custom commands can set this variable in the options section."
  2824. :group 'org-time
  2825. :group 'org-agenda-daily/weekly
  2826. :type 'integer)
  2827. (defcustom org-scheduled-delay-days 0
  2828. "Number of days before a scheduled item becomes active.
  2829. This variable governs the display in sparse trees and in the agenda.
  2830. The default value (i.e. 0) means: don't delay scheduled item.
  2831. When negative, it means use this number (the absolute value of it)
  2832. even if a scheduled item has a different individual delay time
  2833. specified.
  2834. Custom commands can set this variable in the options section."
  2835. :group 'org-time
  2836. :group 'org-agenda-daily/weekly
  2837. :version "24.4"
  2838. :package-version '(Org . "8.0")
  2839. :type 'integer)
  2840. (defcustom org-read-date-prefer-future t
  2841. "Non-nil means assume future for incomplete date input from user.
  2842. This affects the following situations:
  2843. 1. The user gives a month but not a year.
  2844. For example, if it is April and you enter \"feb 2\", this will be read
  2845. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2846. 2. The user gives a day, but no month.
  2847. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2848. read this as the third of *next* month. However, if you enter \"17\",
  2849. it will be considered as *this* month.
  2850. If you set this variable to the symbol `time', then also the following
  2851. will work:
  2852. 3. If the user gives a time.
  2853. If the time is before now, it will be interpreted as tomorrow.
  2854. Currently none of this works for ISO week specifications.
  2855. When this option is nil, the current day, month and year will always be
  2856. used as defaults.
  2857. See also `org-agenda-jump-prefer-future'."
  2858. :group 'org-time
  2859. :type '(choice
  2860. (const :tag "Never" nil)
  2861. (const :tag "Check month and day" t)
  2862. (const :tag "Check month, day, and time" time)))
  2863. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2864. "Should the agenda jump command prefer the future for incomplete dates?
  2865. The default is to do the same as configured in `org-read-date-prefer-future'.
  2866. But you can also set a deviating value here.
  2867. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2868. :group 'org-agenda
  2869. :group 'org-time
  2870. :version "24.1"
  2871. :type '(choice
  2872. (const :tag "Use org-read-date-prefer-future"
  2873. org-read-date-prefer-future)
  2874. (const :tag "Never" nil)
  2875. (const :tag "Always" t)))
  2876. (defcustom org-read-date-force-compatible-dates t
  2877. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2878. Depending on the system Emacs is running on, certain dates cannot
  2879. be represented with the type used internally to represent time.
  2880. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2881. correctly. Some systems allow for earlier dates, some for later,
  2882. some for both. One way to find out it to insert any date into an
  2883. Org buffer, putting the cursor on the year and hitting S-up and
  2884. S-down to test the range.
  2885. When this variable is set to t, the date/time prompt will not let
  2886. you specify dates outside the 1970-2037 range, so it is certain that
  2887. these dates will work in whatever version of Emacs you are
  2888. running, and also that you can move a file from one Emacs implementation
  2889. to another. WHenever Org is forcing the year for you, it will display
  2890. a message and beep.
  2891. When this variable is nil, Org will check if the date is
  2892. representable in the specific Emacs implementation you are using.
  2893. If not, it will force a year, usually the current year, and beep
  2894. to remind you. Currently this setting is not recommended because
  2895. the likelihood that you will open your Org files in an Emacs that
  2896. has limited date range is not negligible.
  2897. A workaround for this problem is to use diary sexp dates for time
  2898. stamps outside of this range."
  2899. :group 'org-time
  2900. :version "24.1"
  2901. :type 'boolean)
  2902. (defcustom org-read-date-display-live t
  2903. "Non-nil means display current interpretation of date prompt live.
  2904. This display will be in an overlay, in the minibuffer."
  2905. :group 'org-time
  2906. :type 'boolean)
  2907. (defcustom org-read-date-popup-calendar t
  2908. "Non-nil means pop up a calendar when prompting for a date.
  2909. In the calendar, the date can be selected with mouse-1. However, the
  2910. minibuffer will also be active, and you can simply enter the date as well.
  2911. When nil, only the minibuffer will be available."
  2912. :group 'org-time
  2913. :type 'boolean)
  2914. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2915. 'org-read-date-popup-calendar)
  2916. (make-obsolete-variable
  2917. 'org-read-date-minibuffer-setup-hook
  2918. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2919. (defcustom org-read-date-minibuffer-setup-hook nil
  2920. "Hook to be used to set up keys for the date/time interface.
  2921. Add key definitions to `minibuffer-local-map', which will be a
  2922. temporary copy.
  2923. WARNING: This option is obsolete, you should use
  2924. `org-read-date-minibuffer-local-map' to set up keys."
  2925. :group 'org-time
  2926. :type 'hook)
  2927. (defcustom org-extend-today-until 0
  2928. "The hour when your day really ends. Must be an integer.
  2929. This has influence for the following applications:
  2930. - When switching the agenda to \"today\". It it is still earlier than
  2931. the time given here, the day recognized as TODAY is actually yesterday.
  2932. - When a date is read from the user and it is still before the time given
  2933. here, the current date and time will be assumed to be yesterday, 23:59.
  2934. Also, timestamps inserted in capture templates follow this rule.
  2935. IMPORTANT: This is a feature whose implementation is and likely will
  2936. remain incomplete. Really, it is only here because past midnight seems to
  2937. be the favorite working time of John Wiegley :-)"
  2938. :group 'org-time
  2939. :type 'integer)
  2940. (defcustom org-use-effective-time nil
  2941. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2942. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2943. \"effective time\" of any timestamps between midnight and 8am will be
  2944. 23:59 of the previous day."
  2945. :group 'org-time
  2946. :version "24.1"
  2947. :type 'boolean)
  2948. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2949. "When non-nil, use the last clock out time for `org-todo'.
  2950. Note that this option has precedence over the combined use of
  2951. `org-use-effective-time' and `org-extend-today-until'."
  2952. :group 'org-time
  2953. :version "24.4"
  2954. :package-version '(Org . "8.0")
  2955. :type 'boolean)
  2956. (defcustom org-edit-timestamp-down-means-later nil
  2957. "Non-nil means S-down will increase the time in a time stamp.
  2958. When nil, S-up will increase."
  2959. :group 'org-time
  2960. :type 'boolean)
  2961. (defcustom org-calendar-follow-timestamp-change t
  2962. "Non-nil means make the calendar window follow timestamp changes.
  2963. When a timestamp is modified and the calendar window is visible, it will be
  2964. moved to the new date."
  2965. :group 'org-time
  2966. :type 'boolean)
  2967. (defgroup org-tags nil
  2968. "Options concerning tags in Org-mode."
  2969. :tag "Org Tags"
  2970. :group 'org)
  2971. (defcustom org-tag-alist nil
  2972. "List of tags allowed in Org-mode files.
  2973. When this list is nil, Org-mode will base TAG input on what is already in the
  2974. buffer.
  2975. The value of this variable is an alist, the car of each entry must be a
  2976. keyword as a string, the cdr may be a character that is used to select
  2977. that tag through the fast-tag-selection interface.
  2978. See the manual for details."
  2979. :group 'org-tags
  2980. :type '(repeat
  2981. (choice
  2982. (cons (string :tag "Tag name")
  2983. (character :tag "Access char"))
  2984. (list :tag "Start radio group"
  2985. (const :startgroup)
  2986. (option (string :tag "Group description")))
  2987. (list :tag "Start tag group, non distinct"
  2988. (const :startgrouptag)
  2989. (option (string :tag "Group description")))
  2990. (list :tag "Group tags delimiter"
  2991. (const :grouptags))
  2992. (list :tag "End radio group"
  2993. (const :endgroup)
  2994. (option (string :tag "Group description")))
  2995. (list :tag "End tag group, non distinct"
  2996. (const :endgrouptag)
  2997. (option (string :tag "Group description")))
  2998. (const :tag "New line" (:newline)))))
  2999. (defcustom org-tag-persistent-alist nil
  3000. "List of tags that will always appear in all Org-mode files.
  3001. This is in addition to any in buffer settings or customizations
  3002. of `org-tag-alist'.
  3003. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3004. The value of this variable is an alist, the car of each entry must be a
  3005. keyword as a string, the cdr may be a character that is used to select
  3006. that tag through the fast-tag-selection interface.
  3007. See the manual for details.
  3008. To disable these tags on a per-file basis, insert anywhere in the file:
  3009. #+STARTUP: noptag"
  3010. :group 'org-tags
  3011. :type '(repeat
  3012. (choice
  3013. (cons (string :tag "Tag name")
  3014. (character :tag "Access char"))
  3015. (const :tag "Start radio group" (:startgroup))
  3016. (const :tag "Group tags delimiter" (:grouptags))
  3017. (const :tag "End radio group" (:endgroup))
  3018. (const :tag "New line" (:newline)))))
  3019. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3020. "If non-nil, always offer completion for all tags of all agenda files.
  3021. Instead of customizing this variable directly, you might want to
  3022. set it locally for capture buffers, because there no list of
  3023. tags in that file can be created dynamically (there are none).
  3024. (add-hook \\='org-capture-mode-hook
  3025. (lambda ()
  3026. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3027. :group 'org-tags
  3028. :version "24.1"
  3029. :type 'boolean)
  3030. (defvar org-file-tags nil
  3031. "List of tags that can be inherited by all entries in the file.
  3032. The tags will be inherited if the variable `org-use-tag-inheritance'
  3033. says they should be.
  3034. This variable is populated from #+FILETAGS lines.")
  3035. (defcustom org-use-fast-tag-selection 'auto
  3036. "Non-nil means use fast tag selection scheme.
  3037. This is a special interface to select and deselect tags with single keys.
  3038. When nil, fast selection is never used.
  3039. When the symbol `auto', fast selection is used if and only if selection
  3040. characters for tags have been configured, either through the variable
  3041. `org-tag-alist' or through a #+TAGS line in the buffer.
  3042. When t, fast selection is always used and selection keys are assigned
  3043. automatically if necessary."
  3044. :group 'org-tags
  3045. :type '(choice
  3046. (const :tag "Always" t)
  3047. (const :tag "Never" nil)
  3048. (const :tag "When selection characters are configured" auto)))
  3049. (defcustom org-fast-tag-selection-single-key nil
  3050. "Non-nil means fast tag selection exits after first change.
  3051. When nil, you have to press RET to exit it.
  3052. During fast tag selection, you can toggle this flag with `C-c'.
  3053. This variable can also have the value `expert'. In this case, the window
  3054. displaying the tags menu is not even shown, until you press C-c again."
  3055. :group 'org-tags
  3056. :type '(choice
  3057. (const :tag "No" nil)
  3058. (const :tag "Yes" t)
  3059. (const :tag "Expert" expert)))
  3060. (defvar org-fast-tag-selection-include-todo nil
  3061. "Non-nil means fast tags selection interface will also offer TODO states.
  3062. This is an undocumented feature, you should not rely on it.")
  3063. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3064. "The column to which tags should be indented in a headline.
  3065. If this number is positive, it specifies the column. If it is negative,
  3066. it means that the tags should be flushright to that column. For example,
  3067. -80 works well for a normal 80 character screen.
  3068. When 0, place tags directly after headline text, with only one space in
  3069. between."
  3070. :group 'org-tags
  3071. :type 'integer)
  3072. (defcustom org-auto-align-tags t
  3073. "Non-nil keeps tags aligned when modifying headlines.
  3074. Some operations (i.e. demoting) change the length of a headline and
  3075. therefore shift the tags around. With this option turned on, after
  3076. each such operation the tags are again aligned to `org-tags-column'."
  3077. :group 'org-tags
  3078. :type 'boolean)
  3079. (defcustom org-use-tag-inheritance t
  3080. "Non-nil means tags in levels apply also for sublevels.
  3081. When nil, only the tags directly given in a specific line apply there.
  3082. This may also be a list of tags that should be inherited, or a regexp that
  3083. matches tags that should be inherited. Additional control is possible
  3084. with the variable `org-tags-exclude-from-inheritance' which gives an
  3085. explicit list of tags to be excluded from inheritance, even if the value of
  3086. `org-use-tag-inheritance' would select it for inheritance.
  3087. If this option is t, a match early-on in a tree can lead to a large
  3088. number of matches in the subtree when constructing the agenda or creating
  3089. a sparse tree. If you only want to see the first match in a tree during
  3090. a search, check out the variable `org-tags-match-list-sublevels'."
  3091. :group 'org-tags
  3092. :type '(choice
  3093. (const :tag "Not" nil)
  3094. (const :tag "Always" t)
  3095. (repeat :tag "Specific tags" (string :tag "Tag"))
  3096. (regexp :tag "Tags matched by regexp")))
  3097. (defcustom org-tags-exclude-from-inheritance nil
  3098. "List of tags that should never be inherited.
  3099. This is a way to exclude a few tags from inheritance. For way to do
  3100. the opposite, to actively allow inheritance for selected tags,
  3101. see the variable `org-use-tag-inheritance'."
  3102. :group 'org-tags
  3103. :type '(repeat (string :tag "Tag")))
  3104. (defun org-tag-inherit-p (tag)
  3105. "Check if TAG is one that should be inherited."
  3106. (cond
  3107. ((member tag org-tags-exclude-from-inheritance) nil)
  3108. ((eq org-use-tag-inheritance t) t)
  3109. ((not org-use-tag-inheritance) nil)
  3110. ((stringp org-use-tag-inheritance)
  3111. (string-match org-use-tag-inheritance tag))
  3112. ((listp org-use-tag-inheritance)
  3113. (member tag org-use-tag-inheritance))
  3114. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3115. (defcustom org-tags-match-list-sublevels t
  3116. "Non-nil means list also sublevels of headlines matching a search.
  3117. This variable applies to tags/property searches, and also to stuck
  3118. projects because this search is based on a tags match as well.
  3119. When set to the symbol `indented', sublevels are indented with
  3120. leading dots.
  3121. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3122. the sublevels of a headline matching a tag search often also match
  3123. the same search. Listing all of them can create very long lists.
  3124. Setting this variable to nil causes subtrees of a match to be skipped.
  3125. This variable is semi-obsolete and probably should always be true. It
  3126. is better to limit inheritance to certain tags using the variables
  3127. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3128. :group 'org-tags
  3129. :type '(choice
  3130. (const :tag "No, don't list them" nil)
  3131. (const :tag "Yes, do list them" t)
  3132. (const :tag "List them, indented with leading dots" indented)))
  3133. (defcustom org-tags-sort-function nil
  3134. "When set, tags are sorted using this function as a comparator."
  3135. :group 'org-tags
  3136. :type '(choice
  3137. (const :tag "No sorting" nil)
  3138. (const :tag "Alphabetical" string<)
  3139. (const :tag "Reverse alphabetical" string>)
  3140. (function :tag "Custom function" nil)))
  3141. (defvar org-tags-history nil
  3142. "History of minibuffer reads for tags.")
  3143. (defvar org-last-tags-completion-table nil
  3144. "The last used completion table for tags.")
  3145. (defvar org-after-tags-change-hook nil
  3146. "Hook that is run after the tags in a line have changed.")
  3147. (defgroup org-properties nil
  3148. "Options concerning properties in Org-mode."
  3149. :tag "Org Properties"
  3150. :group 'org)
  3151. (defcustom org-property-format "%-10s %s"
  3152. "How property key/value pairs should be formatted by `indent-line'.
  3153. When `indent-line' hits a property definition, it will format the line
  3154. according to this format, mainly to make sure that the values are
  3155. lined-up with respect to each other."
  3156. :group 'org-properties
  3157. :type 'string)
  3158. (defcustom org-properties-postprocess-alist nil
  3159. "Alist of properties and functions to adjust inserted values.
  3160. Elements of this alist must be of the form
  3161. ([string] [function])
  3162. where [string] must be a property name and [function] must be a
  3163. lambda expression: this lambda expression must take one argument,
  3164. the value to adjust, and return the new value as a string.
  3165. For example, this element will allow the property \"Remaining\"
  3166. to be updated wrt the relation between the \"Effort\" property
  3167. and the clock summary:
  3168. ((\"Remaining\" (lambda(value)
  3169. (let ((clocksum (org-clock-sum-current-item))
  3170. (effort (org-duration-string-to-minutes
  3171. (org-entry-get (point) \"Effort\"))))
  3172. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3173. :group 'org-properties
  3174. :version "24.1"
  3175. :type '(alist :key-type (string :tag "Property")
  3176. :value-type (function :tag "Function")))
  3177. (defcustom org-use-property-inheritance nil
  3178. "Non-nil means properties apply also for sublevels.
  3179. This setting is chiefly used during property searches. Turning it on can
  3180. cause significant overhead when doing a search, which is why it is not
  3181. on by default.
  3182. When nil, only the properties directly given in the current entry count.
  3183. When t, every property is inherited. The value may also be a list of
  3184. properties that should have inheritance, or a regular expression matching
  3185. properties that should be inherited.
  3186. However, note that some special properties use inheritance under special
  3187. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3188. and the properties ending in \"_ALL\" when they are used as descriptor
  3189. for valid values of a property.
  3190. Note for programmers:
  3191. When querying an entry with `org-entry-get', you can control if inheritance
  3192. should be used. By default, `org-entry-get' looks only at the local
  3193. properties. You can request inheritance by setting the inherit argument
  3194. to t (to force inheritance) or to `selective' (to respect the setting
  3195. in this variable)."
  3196. :group 'org-properties
  3197. :type '(choice
  3198. (const :tag "Not" nil)
  3199. (const :tag "Always" t)
  3200. (repeat :tag "Specific properties" (string :tag "Property"))
  3201. (regexp :tag "Properties matched by regexp")))
  3202. (defun org-property-inherit-p (property)
  3203. "Check if PROPERTY is one that should be inherited."
  3204. (cond
  3205. ((eq org-use-property-inheritance t) t)
  3206. ((not org-use-property-inheritance) nil)
  3207. ((stringp org-use-property-inheritance)
  3208. (string-match org-use-property-inheritance property))
  3209. ((listp org-use-property-inheritance)
  3210. (member property org-use-property-inheritance))
  3211. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3212. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3213. "The default column format, if no other format has been defined.
  3214. This variable can be set on the per-file basis by inserting a line
  3215. #+COLUMNS: %25ITEM ....."
  3216. :group 'org-properties
  3217. :type 'string)
  3218. (defcustom org-columns-ellipses ".."
  3219. "The ellipses to be used when a field in column view is truncated.
  3220. When this is the empty string, as many characters as possible are shown,
  3221. but then there will be no visual indication that the field has been truncated.
  3222. When this is a string of length N, the last N characters of a truncated
  3223. field are replaced by this string. If the column is narrower than the
  3224. ellipses string, only part of the ellipses string will be shown."
  3225. :group 'org-properties
  3226. :type 'string)
  3227. (defcustom org-columns-modify-value-for-display-function nil
  3228. "Function that modifies values for display in column view.
  3229. For example, it can be used to cut out a certain part from a time stamp.
  3230. The function must take 2 arguments:
  3231. column-title The title of the column (*not* the property name)
  3232. value The value that should be modified.
  3233. The function should return the value that should be displayed,
  3234. or nil if the normal value should be used."
  3235. :group 'org-properties
  3236. :type '(choice (const nil) (function)))
  3237. (defconst org-global-properties-fixed
  3238. '(("VISIBILITY_ALL" . "folded children content all")
  3239. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3240. "List of property/value pairs that can be inherited by any entry.
  3241. These are fixed values, for the preset properties. The user variable
  3242. that can be used to add to this list is `org-global-properties'.
  3243. The entries in this list are cons cells where the car is a property
  3244. name and cdr is a string with the value. If the value represents
  3245. multiple items like an \"_ALL\" property, separate the items by
  3246. spaces.")
  3247. (defcustom org-global-properties nil
  3248. "List of property/value pairs that can be inherited by any entry.
  3249. This list will be combined with the constant `org-global-properties-fixed'.
  3250. The entries in this list are cons cells where the car is a property
  3251. name and cdr is a string with the value.
  3252. You can set buffer-local values for the same purpose in the variable
  3253. `org-file-properties' this by adding lines like
  3254. #+PROPERTY: NAME VALUE"
  3255. :group 'org-properties
  3256. :type '(repeat
  3257. (cons (string :tag "Property")
  3258. (string :tag "Value"))))
  3259. (defvar-local org-file-properties nil
  3260. "List of property/value pairs that can be inherited by any entry.
  3261. Valid for the current buffer.
  3262. This variable is populated from #+PROPERTY lines.")
  3263. (defgroup org-agenda nil
  3264. "Options concerning agenda views in Org-mode."
  3265. :tag "Org Agenda"
  3266. :group 'org)
  3267. (defvar-local org-category nil
  3268. "Variable used by org files to set a category for agenda display.
  3269. Such files should use a file variable to set it, for example
  3270. # -*- mode: org; org-category: \"ELisp\"
  3271. or contain a special line
  3272. #+CATEGORY: ELisp
  3273. If the file does not specify a category, then file's base name
  3274. is used instead.")
  3275. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3276. (defcustom org-agenda-files nil
  3277. "The files to be used for agenda display.
  3278. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3279. \\[org-remove-file]. You can also use customize to edit the list.
  3280. If an entry is a directory, all files in that directory that are matched by
  3281. `org-agenda-file-regexp' will be part of the file list.
  3282. If the value of the variable is not a list but a single file name, then
  3283. the list of agenda files is actually stored and maintained in that file, one
  3284. agenda file per line. In this file paths can be given relative to
  3285. `org-directory'. Tilde expansion and environment variable substitution
  3286. are also made."
  3287. :group 'org-agenda
  3288. :type '(choice
  3289. (repeat :tag "List of files and directories" file)
  3290. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3291. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3292. "Regular expression to match files for `org-agenda-files'.
  3293. If any element in the list in that variable contains a directory instead
  3294. of a normal file, all files in that directory that are matched by this
  3295. regular expression will be included."
  3296. :group 'org-agenda
  3297. :type 'regexp)
  3298. (defcustom org-agenda-text-search-extra-files nil
  3299. "List of extra files to be searched by text search commands.
  3300. These files will be searched in addition to the agenda files by the
  3301. commands `org-search-view' (`\\[org-agenda] s') \
  3302. and `org-occur-in-agenda-files'.
  3303. Note that these files will only be searched for text search commands,
  3304. not for the other agenda views like todo lists, tag searches or the weekly
  3305. agenda. This variable is intended to list notes and possibly archive files
  3306. that should also be searched by these two commands.
  3307. In fact, if the first element in the list is the symbol `agenda-archives',
  3308. then all archive files of all agenda files will be added to the search
  3309. scope."
  3310. :group 'org-agenda
  3311. :type '(set :greedy t
  3312. (const :tag "Agenda Archives" agenda-archives)
  3313. (repeat :inline t (file))))
  3314. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3315. 'org-agenda-text-search-extra-files)
  3316. (defcustom org-agenda-skip-unavailable-files nil
  3317. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3318. A nil value means to remove them, after a query, from the list."
  3319. :group 'org-agenda
  3320. :type 'boolean)
  3321. (defcustom org-calendar-to-agenda-key [?c]
  3322. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3323. The command `org-calendar-goto-agenda' will be bound to this key. The
  3324. default is the character `c' because then `c' can be used to switch back and
  3325. forth between agenda and calendar."
  3326. :group 'org-agenda
  3327. :type 'sexp)
  3328. (defcustom org-calendar-insert-diary-entry-key [?i]
  3329. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3330. This option is irrelevant until `org-agenda-diary-file' has been configured
  3331. to point to an Org-mode file. When that is the case, the command
  3332. `org-agenda-diary-entry' will be bound to the key given here, by default
  3333. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3334. if you want to continue doing this, you need to change this to a different
  3335. key."
  3336. :group 'org-agenda
  3337. :type 'sexp)
  3338. (defcustom org-agenda-diary-file 'diary-file
  3339. "File to which to add new entries with the `i' key in agenda and calendar.
  3340. When this is the symbol `diary-file', the functionality in the Emacs
  3341. calendar will be used to add entries to the `diary-file'. But when this
  3342. points to a file, `org-agenda-diary-entry' will be used instead."
  3343. :group 'org-agenda
  3344. :type '(choice
  3345. (const :tag "The standard Emacs diary file" diary-file)
  3346. (file :tag "Special Org file diary entries")))
  3347. (eval-after-load "calendar"
  3348. '(progn
  3349. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3350. 'org-calendar-goto-agenda)
  3351. (add-hook 'calendar-mode-hook
  3352. (lambda ()
  3353. (unless (eq org-agenda-diary-file 'diary-file)
  3354. (define-key calendar-mode-map
  3355. org-calendar-insert-diary-entry-key
  3356. 'org-agenda-diary-entry))))))
  3357. (defgroup org-latex nil
  3358. "Options for embedding LaTeX code into Org-mode."
  3359. :tag "Org LaTeX"
  3360. :group 'org)
  3361. (defcustom org-format-latex-options
  3362. '(:foreground default :background default :scale 1.0
  3363. :html-foreground "Black" :html-background "Transparent"
  3364. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3365. "Options for creating images from LaTeX fragments.
  3366. This is a property list with the following properties:
  3367. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3368. `default' means use the foreground of the default face.
  3369. `auto' means use the foreground from the text face.
  3370. :background the background color, or \"Transparent\".
  3371. `default' means use the background of the default face.
  3372. `auto' means use the background from the text face.
  3373. :scale a scaling factor for the size of the images, to get more pixels
  3374. :html-foreground, :html-background, :html-scale
  3375. the same numbers for HTML export.
  3376. :matchers a list indicating which matchers should be used to
  3377. find LaTeX fragments. Valid members of this list are:
  3378. \"begin\" find environments
  3379. \"$1\" find single characters surrounded by $.$
  3380. \"$\" find math expressions surrounded by $...$
  3381. \"$$\" find math expressions surrounded by $$....$$
  3382. \"\\(\" find math expressions surrounded by \\(...\\)
  3383. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3384. :group 'org-latex
  3385. :type 'plist)
  3386. (defcustom org-format-latex-signal-error t
  3387. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3388. When nil, just push out a message."
  3389. :group 'org-latex
  3390. :version "24.1"
  3391. :type 'boolean)
  3392. (defcustom org-latex-to-mathml-jar-file nil
  3393. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3394. Use this to specify additional executable file say a jar file.
  3395. When using MathToWeb as the converter, specify the full-path to
  3396. your mathtoweb.jar file."
  3397. :group 'org-latex
  3398. :version "24.1"
  3399. :type '(choice
  3400. (const :tag "None" nil)
  3401. (file :tag "JAR file" :must-match t)))
  3402. (defcustom org-latex-to-mathml-convert-command nil
  3403. "Command to convert LaTeX fragments to MathML.
  3404. Replace format-specifiers in the command as noted below and use
  3405. `shell-command' to convert LaTeX to MathML.
  3406. %j: Executable file in fully expanded form as specified by
  3407. `org-latex-to-mathml-jar-file'.
  3408. %I: Input LaTeX file in fully expanded form.
  3409. %i: The latex fragment to be converted.
  3410. %o: Output MathML file.
  3411. This command is used by `org-create-math-formula'.
  3412. When using MathToWeb as the converter, set this option to
  3413. \"java -jar %j -unicode -force -df %o %I\".
  3414. When using LaTeXML set this option to
  3415. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3416. :group 'org-latex
  3417. :version "24.1"
  3418. :type '(choice
  3419. (const :tag "None" nil)
  3420. (string :tag "\nShell command")))
  3421. (defcustom org-latex-create-formula-image-program 'dvipng
  3422. "Program to convert LaTeX fragments with.
  3423. dvipng Process the LaTeX fragments to dvi file, then convert
  3424. dvi files to png files using dvipng.
  3425. This will also include processing of non-math environments.
  3426. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3427. to convert pdf files to png files"
  3428. :group 'org-latex
  3429. :version "24.1"
  3430. :type '(choice
  3431. (const :tag "dvipng" dvipng)
  3432. (const :tag "imagemagick" imagemagick)))
  3433. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3434. "Path to store latex preview images.
  3435. A relative path here creates many directories relative to the
  3436. processed org files paths. An absolute path puts all preview
  3437. images at the same place."
  3438. :group 'org-latex
  3439. :version "24.3"
  3440. :type 'string)
  3441. (defun org-format-latex-mathml-available-p ()
  3442. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3443. (save-match-data
  3444. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3445. org-latex-to-mathml-convert-command)
  3446. (let ((executable (car (split-string
  3447. org-latex-to-mathml-convert-command))))
  3448. (when (executable-find executable)
  3449. (if (string-match
  3450. "%j" org-latex-to-mathml-convert-command)
  3451. (file-readable-p org-latex-to-mathml-jar-file)
  3452. t))))))
  3453. (defcustom org-format-latex-header "\\documentclass{article}
  3454. \\usepackage[usenames]{color}
  3455. \[PACKAGES]
  3456. \[DEFAULT-PACKAGES]
  3457. \\pagestyle{empty} % do not remove
  3458. % The settings below are copied from fullpage.sty
  3459. \\setlength{\\textwidth}{\\paperwidth}
  3460. \\addtolength{\\textwidth}{-3cm}
  3461. \\setlength{\\oddsidemargin}{1.5cm}
  3462. \\addtolength{\\oddsidemargin}{-2.54cm}
  3463. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3464. \\setlength{\\textheight}{\\paperheight}
  3465. \\addtolength{\\textheight}{-\\headheight}
  3466. \\addtolength{\\textheight}{-\\headsep}
  3467. \\addtolength{\\textheight}{-\\footskip}
  3468. \\addtolength{\\textheight}{-3cm}
  3469. \\setlength{\\topmargin}{1.5cm}
  3470. \\addtolength{\\topmargin}{-2.54cm}"
  3471. "The document header used for processing LaTeX fragments.
  3472. It is imperative that this header make sure that no page number
  3473. appears on the page. The package defined in the variables
  3474. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3475. will either replace the placeholder \"[PACKAGES]\" in this
  3476. header, or they will be appended."
  3477. :group 'org-latex
  3478. :type 'string)
  3479. (defun org-set-packages-alist (var val)
  3480. "Set the packages alist and make sure it has 3 elements per entry."
  3481. (set var (mapcar (lambda (x)
  3482. (if (and (consp x) (= (length x) 2))
  3483. (list (car x) (nth 1 x) t)
  3484. x))
  3485. val)))
  3486. (defun org-get-packages-alist (var)
  3487. "Get the packages alist and make sure it has 3 elements per entry."
  3488. (mapcar (lambda (x)
  3489. (if (and (consp x) (= (length x) 2))
  3490. (list (car x) (nth 1 x) t)
  3491. x))
  3492. (default-value var)))
  3493. (defcustom org-latex-default-packages-alist
  3494. '(("AUTO" "inputenc" t ("pdflatex"))
  3495. ("T1" "fontenc" t ("pdflatex"))
  3496. ("" "graphicx" t)
  3497. ("" "grffile" t)
  3498. ("" "longtable" nil)
  3499. ("" "wrapfig" nil)
  3500. ("" "rotating" nil)
  3501. ("normalem" "ulem" t)
  3502. ("" "amsmath" t)
  3503. ("" "textcomp" t)
  3504. ("" "amssymb" t)
  3505. ("" "capt-of" nil)
  3506. ("" "hyperref" nil))
  3507. "Alist of default packages to be inserted in the header.
  3508. Change this only if one of the packages here causes an
  3509. incompatibility with another package you are using.
  3510. The packages in this list are needed by one part or another of
  3511. Org mode to function properly:
  3512. - inputenc, fontenc: for basic font and character selection
  3513. - graphicx: for including images
  3514. - grffile: allow periods and spaces in graphics file names
  3515. - longtable: For multipage tables
  3516. - wrapfig: for figure placement
  3517. - rotating: for sideways figures and tables
  3518. - ulem: for underline and strike-through
  3519. - amsmath: for subscript and superscript and math environments
  3520. - textcomp, amssymb: for various symbols used
  3521. for interpreting the entities in `org-entities'. You can skip
  3522. some of these packages if you don't use any of their symbols.
  3523. - capt-of: for captions outside of floats
  3524. - hyperref: for cross references
  3525. Therefore you should not modify this variable unless you know
  3526. what you are doing. The one reason to change it anyway is that
  3527. you might be loading some other package that conflicts with one
  3528. of the default packages. Each element is either a cell or
  3529. a string.
  3530. A cell is of the format
  3531. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3532. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3533. when compiling LaTeX snippets into images for inclusion into
  3534. non-LaTeX output. COMPILERS is a list of compilers that should
  3535. include the package, see `org-latex-compiler'. If the document
  3536. compiler is not in the list, and the list is non-nil, the package
  3537. will not be inserted in the final document.
  3538. A string will be inserted as-is in the header of the document."
  3539. :group 'org-latex
  3540. :group 'org-export-latex
  3541. :set 'org-set-packages-alist
  3542. :get 'org-get-packages-alist
  3543. :version "25.1"
  3544. :package-version '(Org . "8.3")
  3545. :type '(repeat
  3546. (choice
  3547. (list :tag "options/package pair"
  3548. (string :tag "options")
  3549. (string :tag "package")
  3550. (boolean :tag "Snippet"))
  3551. (string :tag "A line of LaTeX"))))
  3552. (defcustom org-latex-packages-alist nil
  3553. "Alist of packages to be inserted in every LaTeX header.
  3554. These will be inserted after `org-latex-default-packages-alist'.
  3555. Each element is either a cell or a string.
  3556. A cell is of the format:
  3557. (\"options\" \"package\" SNIPPET-FLAG)
  3558. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3559. needed when turning LaTeX snippets into images for inclusion into
  3560. non-LaTeX output.
  3561. A string will be inserted as-is in the header of the document.
  3562. Make sure that you only list packages here which:
  3563. - you want in every file;
  3564. - do not conflict with the setup in `org-format-latex-header';
  3565. - do not conflict with the default packages in
  3566. `org-latex-default-packages-alist'."
  3567. :group 'org-latex
  3568. :group 'org-export-latex
  3569. :set 'org-set-packages-alist
  3570. :get 'org-get-packages-alist
  3571. :type '(repeat
  3572. (choice
  3573. (list :tag "options/package pair"
  3574. (string :tag "options")
  3575. (string :tag "package")
  3576. (boolean :tag "Snippet"))
  3577. (string :tag "A line of LaTeX"))))
  3578. (defgroup org-appearance nil
  3579. "Settings for Org-mode appearance."
  3580. :tag "Org Appearance"
  3581. :group 'org)
  3582. (defcustom org-level-color-stars-only nil
  3583. "Non-nil means fontify only the stars in each headline.
  3584. When nil, the entire headline is fontified.
  3585. Changing it requires restart of `font-lock-mode' to become effective
  3586. also in regions already fontified."
  3587. :group 'org-appearance
  3588. :type 'boolean)
  3589. (defcustom org-hide-leading-stars nil
  3590. "Non-nil means hide the first N-1 stars in a headline.
  3591. This works by using the face `org-hide' for these stars. This
  3592. face is white for a light background, and black for a dark
  3593. background. You may have to customize the face `org-hide' to
  3594. make this work.
  3595. Changing it requires restart of `font-lock-mode' to become effective
  3596. also in regions already fontified.
  3597. You may also set this on a per-file basis by adding one of the following
  3598. lines to the buffer:
  3599. #+STARTUP: hidestars
  3600. #+STARTUP: showstars"
  3601. :group 'org-appearance
  3602. :type 'boolean)
  3603. (defcustom org-hidden-keywords nil
  3604. "List of symbols corresponding to keywords to be hidden the org buffer.
  3605. For example, a value \\='(title) for this list will make the document's title
  3606. appear in the buffer without the initial #+TITLE: keyword."
  3607. :group 'org-appearance
  3608. :version "24.1"
  3609. :type '(set (const :tag "#+AUTHOR" author)
  3610. (const :tag "#+DATE" date)
  3611. (const :tag "#+EMAIL" email)
  3612. (const :tag "#+TITLE" title)))
  3613. (defcustom org-custom-properties nil
  3614. "List of properties (as strings) with a special meaning.
  3615. The default use of these custom properties is to let the user
  3616. hide them with `org-toggle-custom-properties-visibility'."
  3617. :group 'org-properties
  3618. :group 'org-appearance
  3619. :version "24.3"
  3620. :type '(repeat (string :tag "Property Name")))
  3621. (defcustom org-fontify-done-headline nil
  3622. "Non-nil means change the face of a headline if it is marked DONE.
  3623. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3624. When this is non-nil, the headline after the keyword is set to the
  3625. `org-headline-done' as an additional indication."
  3626. :group 'org-appearance
  3627. :type 'boolean)
  3628. (defcustom org-fontify-emphasized-text t
  3629. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3630. Changing this variable requires a restart of Emacs to take effect."
  3631. :group 'org-appearance
  3632. :type 'boolean)
  3633. (defcustom org-fontify-whole-heading-line nil
  3634. "Non-nil means fontify the whole line for headings.
  3635. This is useful when setting a background color for the
  3636. org-level-* faces."
  3637. :group 'org-appearance
  3638. :type 'boolean)
  3639. (defcustom org-highlight-latex-and-related nil
  3640. "Non-nil means highlight LaTeX related syntax in the buffer.
  3641. When non nil, the value should be a list containing any of the
  3642. following symbols:
  3643. `latex' Highlight LaTeX snippets and environments.
  3644. `script' Highlight subscript and superscript.
  3645. `entities' Highlight entities."
  3646. :group 'org-appearance
  3647. :version "24.4"
  3648. :package-version '(Org . "8.0")
  3649. :type '(choice
  3650. (const :tag "No highlighting" nil)
  3651. (set :greedy t :tag "Highlight"
  3652. (const :tag "LaTeX snippets and environments" latex)
  3653. (const :tag "Subscript and superscript" script)
  3654. (const :tag "Entities" entities))))
  3655. (defcustom org-hide-emphasis-markers nil
  3656. "Non-nil mean font-lock should hide the emphasis marker characters."
  3657. :group 'org-appearance
  3658. :type 'boolean)
  3659. (defcustom org-hide-macro-markers nil
  3660. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3661. :group 'org-appearance
  3662. :type 'boolean)
  3663. (defcustom org-pretty-entities nil
  3664. "Non-nil means show entities as UTF8 characters.
  3665. When nil, the \\name form remains in the buffer."
  3666. :group 'org-appearance
  3667. :version "24.1"
  3668. :type 'boolean)
  3669. (defcustom org-pretty-entities-include-sub-superscripts t
  3670. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3671. :group 'org-appearance
  3672. :version "24.1"
  3673. :type 'boolean)
  3674. (defvar org-emph-re nil
  3675. "Regular expression for matching emphasis.
  3676. After a match, the match groups contain these elements:
  3677. 0 The match of the full regular expression, including the characters
  3678. before and after the proper match
  3679. 1 The character before the proper match, or empty at beginning of line
  3680. 2 The proper match, including the leading and trailing markers
  3681. 3 The leading marker like * or /, indicating the type of highlighting
  3682. 4 The text between the emphasis markers, not including the markers
  3683. 5 The character after the match, empty at the end of a line")
  3684. (defvar org-verbatim-re nil
  3685. "Regular expression for matching verbatim text.")
  3686. (defvar org-emphasis-regexp-components) ; defined just below
  3687. (defvar org-emphasis-alist) ; defined just below
  3688. (defun org-set-emph-re (var val)
  3689. "Set variable and compute the emphasis regular expression."
  3690. (set var val)
  3691. (when (and (boundp 'org-emphasis-alist)
  3692. (boundp 'org-emphasis-regexp-components)
  3693. org-emphasis-alist org-emphasis-regexp-components)
  3694. (let* ((e org-emphasis-regexp-components)
  3695. (pre (car e))
  3696. (post (nth 1 e))
  3697. (border (nth 2 e))
  3698. (body (nth 3 e))
  3699. (nl (nth 4 e))
  3700. (body1 (concat body "*?"))
  3701. (markers (mapconcat 'car org-emphasis-alist ""))
  3702. (vmarkers (mapconcat
  3703. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3704. org-emphasis-alist "")))
  3705. ;; make sure special characters appear at the right position in the class
  3706. (if (string-match "\\^" markers)
  3707. (setq markers (concat (replace-match "" t t markers) "^")))
  3708. (if (string-match "-" markers)
  3709. (setq markers (concat (replace-match "" t t markers) "-")))
  3710. (if (string-match "\\^" vmarkers)
  3711. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3712. (if (string-match "-" vmarkers)
  3713. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3714. (if (> nl 0)
  3715. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3716. (int-to-string nl) "\\}")))
  3717. ;; Make the regexp
  3718. (setq org-emph-re
  3719. (concat "\\([" pre "]\\|^\\)"
  3720. "\\("
  3721. "\\([" markers "]\\)"
  3722. "\\("
  3723. "[^" border "]\\|"
  3724. "[^" border "]"
  3725. body1
  3726. "[^" border "]"
  3727. "\\)"
  3728. "\\3\\)"
  3729. "\\([" post "]\\|$\\)"))
  3730. (setq org-verbatim-re
  3731. (concat "\\([" pre "]\\|^\\)"
  3732. "\\("
  3733. "\\([" vmarkers "]\\)"
  3734. "\\("
  3735. "[^" border "]\\|"
  3736. "[^" border "]"
  3737. body1
  3738. "[^" border "]"
  3739. "\\)"
  3740. "\\3\\)"
  3741. "\\([" post "]\\|$\\)")))))
  3742. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3743. ;; set this option proved cumbersome. See this message/thread:
  3744. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3745. (defvar org-emphasis-regexp-components
  3746. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3747. "Components used to build the regular expression for emphasis.
  3748. This is a list with five entries. Terminology: In an emphasis string
  3749. like \" *strong word* \", we call the initial space PREMATCH, the final
  3750. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3751. and \"trong wor\" is the body. The different components in this variable
  3752. specify what is allowed/forbidden in each part:
  3753. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3754. post Chars allowed as postmatch. End of line will be allowed too.
  3755. border The chars *forbidden* as border characters.
  3756. body-regexp A regexp like \".\" to match a body character. Don't use
  3757. non-shy groups here, and don't allow newline here.
  3758. newline The maximum number of newlines allowed in an emphasis exp.
  3759. You need to reload Org or to restart Emacs after customizing this.")
  3760. (defcustom org-emphasis-alist
  3761. `(("*" bold)
  3762. ("/" italic)
  3763. ("_" underline)
  3764. ("=" org-verbatim verbatim)
  3765. ("~" org-code verbatim)
  3766. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3767. "Alist of characters and faces to emphasize text.
  3768. Text starting and ending with a special character will be emphasized,
  3769. for example *bold*, _underlined_ and /italic/. This variable sets the
  3770. marker characters and the face to be used by font-lock for highlighting
  3771. in Org-mode Emacs buffers.
  3772. You need to reload Org or to restart Emacs after customizing this."
  3773. :group 'org-appearance
  3774. :set 'org-set-emph-re
  3775. :version "24.4"
  3776. :package-version '(Org . "8.0")
  3777. :type '(repeat
  3778. (list
  3779. (string :tag "Marker character")
  3780. (choice
  3781. (face :tag "Font-lock-face")
  3782. (plist :tag "Face property list"))
  3783. (option (const verbatim)))))
  3784. (defvar org-protecting-blocks '("src" "example" "export")
  3785. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3786. This is needed for font-lock setup.")
  3787. ;;; Functions and variables from their packages
  3788. ;; Declared here to avoid compiler warnings
  3789. ;; XEmacs only
  3790. (defvar outline-mode-menu-heading)
  3791. (defvar outline-mode-menu-show)
  3792. (defvar outline-mode-menu-hide)
  3793. (defvar zmacs-regions) ; XEmacs regions
  3794. ;; Emacs only
  3795. (defvar mark-active)
  3796. ;; Various packages
  3797. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3798. (declare-function calendar-forward-day "cal-move" (arg))
  3799. (declare-function calendar-goto-date "cal-move" (date))
  3800. (declare-function calendar-goto-today "cal-move" ())
  3801. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3802. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3803. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3804. (declare-function cdlatex-tab "ext:cdlatex" ())
  3805. (declare-function dired-get-filename
  3806. "dired"
  3807. (&optional localp no-error-if-not-filep))
  3808. (declare-function iswitchb-read-buffer
  3809. "iswitchb"
  3810. (prompt &optional default require-match start matches-set))
  3811. (declare-function org-agenda-change-all-lines
  3812. "org-agenda"
  3813. (newhead hdmarker &optional fixface just-this))
  3814. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3815. "org-agenda"
  3816. (&optional end))
  3817. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3818. (declare-function org-agenda-format-item
  3819. "org-agenda"
  3820. (extra txt &optional level category tags dotime noprefix
  3821. remove-re habitp))
  3822. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3823. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3824. (declare-function org-agenda-save-markers-for-cut-and-paste
  3825. "org-agenda"
  3826. (beg end))
  3827. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3828. (declare-function org-agenda-skip "org-agenda" ())
  3829. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3830. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3831. (declare-function org-indent-mode "org-indent" (&optional arg))
  3832. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3833. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3834. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3835. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3836. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3837. (declare-function parse-time-string "parse-time" (string))
  3838. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3839. (declare-function table--at-cell-p
  3840. "table"
  3841. (position &optional object at-column))
  3842. (defvar align-mode-rules-list)
  3843. (defvar calc-embedded-close-formula)
  3844. (defvar calc-embedded-open-formula)
  3845. (defvar calc-embedded-open-mode)
  3846. (defvar font-lock-unfontify-region-function)
  3847. (defvar iswitchb-temp-buflist)
  3848. (defvar org-agenda-tags-todo-honor-ignore-options)
  3849. (defvar remember-data-file)
  3850. (defvar texmathp-why)
  3851. ;;;###autoload
  3852. (defun turn-on-orgtbl ()
  3853. "Unconditionally turn on `orgtbl-mode'."
  3854. (require 'org-table)
  3855. (orgtbl-mode 1))
  3856. (defun org-at-table-p (&optional table-type)
  3857. "Non-nil if the cursor is inside an Org table.
  3858. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3859. If `org-enable-table-editor' is nil, return nil unconditionally."
  3860. (and
  3861. org-enable-table-editor
  3862. (save-excursion
  3863. (beginning-of-line)
  3864. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3865. (or (not (derived-mode-p 'org-mode))
  3866. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3867. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3868. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3869. (defun org-at-table.el-p ()
  3870. "Non-nil when point is at a table.el table."
  3871. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3872. (let ((element (org-element-at-point)))
  3873. (and (eq (org-element-type element) 'table)
  3874. (eq (org-element-property :type element) 'table.el)))))
  3875. (defun org-table-recognize-table.el ()
  3876. "If there is a table.el table nearby, recognize it and move into it."
  3877. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3878. (beginning-of-line)
  3879. (unless (or (looking-at org-table-dataline-regexp)
  3880. (not (looking-at org-table1-hline-regexp)))
  3881. (forward-line)
  3882. (when (looking-at org-table-any-border-regexp)
  3883. (forward-line -2)))
  3884. (if (re-search-forward "|" (org-table-end t) t)
  3885. (progn
  3886. (require 'table)
  3887. (if (table--at-cell-p (point)) t
  3888. (message "recognizing table.el table...")
  3889. (table-recognize-table)
  3890. (message "recognizing table.el table...done")))
  3891. (error "This should not happen"))))
  3892. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3893. (make-obsolete 'org-table-recognize-table.el
  3894. "please notify the org mailing list if you use this function."
  3895. "Org 9.0")
  3896. (defun org-at-table-hline-p ()
  3897. "Non-nil when point is inside a hline in a table.
  3898. Assume point is already in a table. If `org-enable-table-editor'
  3899. is nil, return nil unconditionally."
  3900. (and org-enable-table-editor
  3901. (save-excursion
  3902. (beginning-of-line)
  3903. (looking-at org-table-hline-regexp))))
  3904. (defun org-table-map-tables (function &optional quietly)
  3905. "Apply FUNCTION to the start of all tables in the buffer."
  3906. (save-excursion
  3907. (save-restriction
  3908. (widen)
  3909. (goto-char (point-min))
  3910. (while (re-search-forward org-table-any-line-regexp nil t)
  3911. (unless quietly
  3912. (message "Mapping tables: %d%%"
  3913. (floor (* 100.0 (point)) (buffer-size))))
  3914. (beginning-of-line 1)
  3915. (when (and (looking-at org-table-line-regexp)
  3916. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3917. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3918. (save-excursion (funcall function))
  3919. (or (looking-at org-table-line-regexp)
  3920. (forward-char 1)))
  3921. (re-search-forward org-table-any-border-regexp nil 1))))
  3922. (unless quietly (message "Mapping tables: done")))
  3923. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3924. (declare-function org-clock-update-mode-line "org-clock" ())
  3925. (declare-function org-resolve-clocks "org-clock"
  3926. (&optional also-non-dangling-p prompt last-valid))
  3927. (defun org-at-TBLFM-p (&optional pos)
  3928. "Non-nil when point (or POS) is in #+TBLFM line."
  3929. (save-excursion
  3930. (goto-char (or pos (point)))
  3931. (beginning-of-line)
  3932. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3933. (eq (org-element-type (org-element-at-point)) 'table))))
  3934. (defvar org-clock-start-time)
  3935. (defvar org-clock-marker (make-marker)
  3936. "Marker recording the last clock-in.")
  3937. (defvar org-clock-hd-marker (make-marker)
  3938. "Marker recording the last clock-in, but the headline position.")
  3939. (defvar org-clock-heading ""
  3940. "The heading of the current clock entry.")
  3941. (defun org-clock-is-active ()
  3942. "Return the buffer where the clock is currently running.
  3943. Return nil if no clock is running."
  3944. (marker-buffer org-clock-marker))
  3945. (defun org-check-running-clock ()
  3946. "Check if the current buffer contains the running clock.
  3947. If yes, offer to stop it and to save the buffer with the changes."
  3948. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3949. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3950. (buffer-name))))
  3951. (org-clock-out)
  3952. (when (y-or-n-p "Save changed buffer?")
  3953. (save-buffer))))
  3954. (defun org-clocktable-try-shift (dir n)
  3955. "Check if this line starts a clock table, if yes, shift the time block."
  3956. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3957. (org-clocktable-shift dir n)))
  3958. ;;;###autoload
  3959. (defun org-clock-persistence-insinuate ()
  3960. "Set up hooks for clock persistence."
  3961. (require 'org-clock)
  3962. (add-hook 'org-mode-hook 'org-clock-load)
  3963. (add-hook 'kill-emacs-hook 'org-clock-save))
  3964. (defgroup org-archive nil
  3965. "Options concerning archiving in Org-mode."
  3966. :tag "Org Archive"
  3967. :group 'org-structure)
  3968. (defcustom org-archive-location "%s_archive::"
  3969. "The location where subtrees should be archived.
  3970. The value of this variable is a string, consisting of two parts,
  3971. separated by a double-colon. The first part is a filename and
  3972. the second part is a headline.
  3973. When the filename is omitted, archiving happens in the same file.
  3974. %s in the filename will be replaced by the current file
  3975. name (without the directory part). Archiving to a different file
  3976. is useful to keep archived entries from contributing to the
  3977. Org-mode Agenda.
  3978. The archived entries will be filed as subtrees of the specified
  3979. headline. When the headline is omitted, the subtrees are simply
  3980. filed away at the end of the file, as top-level entries. Also in
  3981. the heading you can use %s to represent the file name, this can be
  3982. useful when using the same archive for a number of different files.
  3983. Here are a few examples:
  3984. \"%s_archive::\"
  3985. If the current file is Projects.org, archive in file
  3986. Projects.org_archive, as top-level trees. This is the default.
  3987. \"::* Archived Tasks\"
  3988. Archive in the current file, under the top-level headline
  3989. \"* Archived Tasks\".
  3990. \"~/org/archive.org::\"
  3991. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3992. \"~/org/archive.org::* From %s\"
  3993. Archive in file ~/org/archive.org (absolute path), under headlines
  3994. \"From FILENAME\" where file name is the current file name.
  3995. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3996. The \"datetree/\" string is special, signifying to archive
  3997. items to the datetree. Items are placed in either the CLOSED
  3998. date of the item, or the current date if there is no CLOSED date.
  3999. The heading will be a subentry to the current date. There doesn't
  4000. need to be a heading, but there always needs to be a slash after
  4001. datetree. For example, to store archived items directly in the
  4002. datetree, use \"~/org/datetree.org::datetree/\".
  4003. \"basement::** Finished Tasks\"
  4004. Archive in file ./basement (relative path), as level 3 trees
  4005. below the level 2 heading \"** Finished Tasks\".
  4006. You may set this option on a per-file basis by adding to the buffer a
  4007. line like
  4008. #+ARCHIVE: basement::** Finished Tasks
  4009. You may also define it locally for a subtree by setting an ARCHIVE property
  4010. in the entry. If such a property is found in an entry, or anywhere up
  4011. the hierarchy, it will be used."
  4012. :group 'org-archive
  4013. :type 'string)
  4014. (defcustom org-agenda-skip-archived-trees t
  4015. "Non-nil means the agenda will skip any items located in archived trees.
  4016. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4017. variable is no longer recommended, you should leave it at the value t.
  4018. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4019. :group 'org-archive
  4020. :group 'org-agenda-skip
  4021. :type 'boolean)
  4022. (defcustom org-columns-skip-archived-trees t
  4023. "Non-nil means ignore archived trees when creating column view."
  4024. :group 'org-archive
  4025. :group 'org-properties
  4026. :type 'boolean)
  4027. (defcustom org-cycle-open-archived-trees nil
  4028. "Non-nil means `org-cycle' will open archived trees.
  4029. An archived tree is a tree marked with the tag ARCHIVE.
  4030. When nil, archived trees will stay folded. You can still open them with
  4031. normal outline commands like `show-all', but not with the cycling commands."
  4032. :group 'org-archive
  4033. :group 'org-cycle
  4034. :type 'boolean)
  4035. (defcustom org-sparse-tree-open-archived-trees nil
  4036. "Non-nil means sparse tree construction shows matches in archived trees.
  4037. When nil, matches in these trees are highlighted, but the trees are kept in
  4038. collapsed state."
  4039. :group 'org-archive
  4040. :group 'org-sparse-trees
  4041. :type 'boolean)
  4042. (defcustom org-sparse-tree-default-date-type nil
  4043. "The default date type when building a sparse tree.
  4044. When this is nil, a date is a scheduled or a deadline timestamp.
  4045. Otherwise, these types are allowed:
  4046. all: all timestamps
  4047. active: only active timestamps (<...>)
  4048. inactive: only inactive timestamps ([...])
  4049. scheduled: only scheduled timestamps
  4050. deadline: only deadline timestamps"
  4051. :type '(choice (const :tag "Scheduled or deadline" nil)
  4052. (const :tag "All timestamps" all)
  4053. (const :tag "Only active timestamps" active)
  4054. (const :tag "Only inactive timestamps" inactive)
  4055. (const :tag "Only scheduled timestamps" scheduled)
  4056. (const :tag "Only deadline timestamps" deadline)
  4057. (const :tag "Only closed timestamps" closed))
  4058. :version "25.1"
  4059. :package-version '(Org . "8.3")
  4060. :group 'org-sparse-trees)
  4061. (defun org-cycle-hide-archived-subtrees (state)
  4062. "Re-hide all archived subtrees after a visibility state change."
  4063. (when (and (not org-cycle-open-archived-trees)
  4064. (not (memq state '(overview folded))))
  4065. (save-excursion
  4066. (let* ((globalp (memq state '(contents all)))
  4067. (beg (if globalp (point-min) (point)))
  4068. (end (if globalp (point-max) (org-end-of-subtree t))))
  4069. (org-hide-archived-subtrees beg end)
  4070. (goto-char beg)
  4071. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4072. (message "%s" (substitute-command-keys
  4073. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4074. (defun org-force-cycle-archived ()
  4075. "Cycle subtree even if it is archived."
  4076. (interactive)
  4077. (setq this-command 'org-cycle)
  4078. (let ((org-cycle-open-archived-trees t))
  4079. (call-interactively 'org-cycle)))
  4080. (defun org-hide-archived-subtrees (beg end)
  4081. "Re-hide all archived subtrees after a visibility state change."
  4082. (org-with-wide-buffer
  4083. (let ((case-fold-search nil)
  4084. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4085. (goto-char beg)
  4086. ;; Include headline point is currently on.
  4087. (beginning-of-line)
  4088. (while (and (< (point) end) (re-search-forward re end t))
  4089. (when (member org-archive-tag (org-get-tags))
  4090. (org-flag-subtree t)
  4091. (org-end-of-subtree t))))))
  4092. (declare-function outline-end-of-heading "outline" ())
  4093. (declare-function outline-flag-region "outline" (from to flag))
  4094. (defun org-flag-subtree (flag)
  4095. (save-excursion
  4096. (org-back-to-heading t)
  4097. (outline-end-of-heading)
  4098. (outline-flag-region (point)
  4099. (progn (org-end-of-subtree t) (point))
  4100. flag)))
  4101. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4102. ;; Declare Column View Code
  4103. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4104. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4105. (declare-function org-columns-compute "org-colview" (property))
  4106. ;; Declare ID code
  4107. (declare-function org-id-store-link "org-id")
  4108. (declare-function org-id-locations-load "org-id")
  4109. (declare-function org-id-locations-save "org-id")
  4110. (defvar org-id-track-globally)
  4111. ;;; Variables for pre-computed regular expressions, all buffer local
  4112. (defvar-local org-todo-regexp nil
  4113. "Matches any of the TODO state keywords.")
  4114. (defvar-local org-not-done-regexp nil
  4115. "Matches any of the TODO state keywords except the last one.")
  4116. (defvar-local org-not-done-heading-regexp nil
  4117. "Matches a TODO headline that is not done.")
  4118. (defvar-local org-todo-line-regexp nil
  4119. "Matches a headline and puts TODO state into group 2 if present.")
  4120. (defvar-local org-complex-heading-regexp nil
  4121. "Matches a headline and puts everything into groups:
  4122. group 1: the stars
  4123. group 2: The todo keyword, maybe
  4124. group 3: Priority cookie
  4125. group 4: True headline
  4126. group 5: Tags")
  4127. (defvar-local org-complex-heading-regexp-format nil
  4128. "Printf format to make regexp to match an exact headline.
  4129. This regexp will match the headline of any node which has the
  4130. exact headline text that is put into the format, but may have any
  4131. TODO state, priority and tags.")
  4132. (defvar-local org-todo-line-tags-regexp nil
  4133. "Matches a headline and puts TODO state into group 2 if present.
  4134. Also put tags into group 4 if tags are present.")
  4135. (defconst org-plain-time-of-day-regexp
  4136. (concat
  4137. "\\(\\<[012]?[0-9]"
  4138. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4139. "\\(--?"
  4140. "\\(\\<[012]?[0-9]"
  4141. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4142. "\\)?")
  4143. "Regular expression to match a plain time or time range.
  4144. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4145. groups carry important information:
  4146. 0 the full match
  4147. 1 the first time, range or not
  4148. 8 the second time, if it is a range.")
  4149. (defconst org-plain-time-extension-regexp
  4150. (concat
  4151. "\\(\\<[012]?[0-9]"
  4152. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4153. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4154. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4155. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4156. groups carry important information:
  4157. 0 the full match
  4158. 7 hours of duration
  4159. 9 minutes of duration")
  4160. (defconst org-stamp-time-of-day-regexp
  4161. (concat
  4162. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4163. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4164. "\\(--?"
  4165. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4166. "Regular expression to match a timestamp time or time range.
  4167. After a match, the following groups carry important information:
  4168. 0 the full match
  4169. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4170. 2 the first time, range or not
  4171. 4 the second time, if it is a range.")
  4172. (defconst org-startup-options
  4173. '(("fold" org-startup-folded t)
  4174. ("overview" org-startup-folded t)
  4175. ("nofold" org-startup-folded nil)
  4176. ("showall" org-startup-folded nil)
  4177. ("showeverything" org-startup-folded showeverything)
  4178. ("content" org-startup-folded content)
  4179. ("indent" org-startup-indented t)
  4180. ("noindent" org-startup-indented nil)
  4181. ("hidestars" org-hide-leading-stars t)
  4182. ("showstars" org-hide-leading-stars nil)
  4183. ("odd" org-odd-levels-only t)
  4184. ("oddeven" org-odd-levels-only nil)
  4185. ("align" org-startup-align-all-tables t)
  4186. ("noalign" org-startup-align-all-tables nil)
  4187. ("inlineimages" org-startup-with-inline-images t)
  4188. ("noinlineimages" org-startup-with-inline-images nil)
  4189. ("latexpreview" org-startup-with-latex-preview t)
  4190. ("nolatexpreview" org-startup-with-latex-preview nil)
  4191. ("customtime" org-display-custom-times t)
  4192. ("logdone" org-log-done time)
  4193. ("lognotedone" org-log-done note)
  4194. ("nologdone" org-log-done nil)
  4195. ("lognoteclock-out" org-log-note-clock-out t)
  4196. ("nolognoteclock-out" org-log-note-clock-out nil)
  4197. ("logrepeat" org-log-repeat state)
  4198. ("lognoterepeat" org-log-repeat note)
  4199. ("logdrawer" org-log-into-drawer t)
  4200. ("nologdrawer" org-log-into-drawer nil)
  4201. ("logstatesreversed" org-log-states-order-reversed t)
  4202. ("nologstatesreversed" org-log-states-order-reversed nil)
  4203. ("nologrepeat" org-log-repeat nil)
  4204. ("logreschedule" org-log-reschedule time)
  4205. ("lognotereschedule" org-log-reschedule note)
  4206. ("nologreschedule" org-log-reschedule nil)
  4207. ("logredeadline" org-log-redeadline time)
  4208. ("lognoteredeadline" org-log-redeadline note)
  4209. ("nologredeadline" org-log-redeadline nil)
  4210. ("logrefile" org-log-refile time)
  4211. ("lognoterefile" org-log-refile note)
  4212. ("nologrefile" org-log-refile nil)
  4213. ("fninline" org-footnote-define-inline t)
  4214. ("nofninline" org-footnote-define-inline nil)
  4215. ("fnlocal" org-footnote-section nil)
  4216. ("fnauto" org-footnote-auto-label t)
  4217. ("fnprompt" org-footnote-auto-label nil)
  4218. ("fnconfirm" org-footnote-auto-label confirm)
  4219. ("fnplain" org-footnote-auto-label plain)
  4220. ("fnadjust" org-footnote-auto-adjust t)
  4221. ("nofnadjust" org-footnote-auto-adjust nil)
  4222. ("constcgs" constants-unit-system cgs)
  4223. ("constSI" constants-unit-system SI)
  4224. ("noptag" org-tag-persistent-alist nil)
  4225. ("hideblocks" org-hide-block-startup t)
  4226. ("nohideblocks" org-hide-block-startup nil)
  4227. ("beamer" org-startup-with-beamer-mode t)
  4228. ("entitiespretty" org-pretty-entities t)
  4229. ("entitiesplain" org-pretty-entities nil))
  4230. "Variable associated with STARTUP options for org-mode.
  4231. Each element is a list of three items: the startup options (as written
  4232. in the #+STARTUP line), the corresponding variable, and the value to set
  4233. this variable to if the option is found. An optional forth element PUSH
  4234. means to push this value onto the list in the variable.")
  4235. (defcustom org-group-tags t
  4236. "When non-nil (the default), use group tags.
  4237. This can be turned on/off through `org-toggle-tags-groups'."
  4238. :group 'org-tags
  4239. :group 'org-startup
  4240. :type 'boolean)
  4241. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4242. (defun org-toggle-tags-groups ()
  4243. "Toggle support for group tags.
  4244. Support for group tags is controlled by the option
  4245. `org-group-tags', which is non-nil by default."
  4246. (interactive)
  4247. (setq org-group-tags (not org-group-tags))
  4248. (cond ((and (derived-mode-p 'org-agenda-mode)
  4249. org-group-tags)
  4250. (org-agenda-redo))
  4251. ((derived-mode-p 'org-mode)
  4252. (let ((org-inhibit-startup t)) (org-mode))))
  4253. (message "Groups tags support has been turned %s"
  4254. (if org-group-tags "on" "off")))
  4255. (defun org-set-regexps-and-options (&optional tags-only)
  4256. "Precompute regular expressions used in the current buffer.
  4257. When optional argument TAGS-ONLY is non-nil, only compute tags
  4258. related expressions."
  4259. (when (derived-mode-p 'org-mode)
  4260. (let ((alist (org--setup-collect-keywords
  4261. (org-make-options-regexp
  4262. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4263. (and (not tags-only)
  4264. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4265. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4266. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4267. (org--setup-process-tags
  4268. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4269. (unless tags-only
  4270. ;; File properties.
  4271. (setq-local org-file-properties (cdr (assq 'property alist)))
  4272. ;; Archive location.
  4273. (let ((archive (cdr (assq 'archive alist))))
  4274. (when archive (setq-local org-archive-location archive)))
  4275. ;; Category.
  4276. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4277. (when cat
  4278. (setq-local org-category (intern cat))
  4279. (setq-local org-file-properties
  4280. (org--update-property-plist
  4281. "CATEGORY" cat org-file-properties))))
  4282. ;; Columns.
  4283. (let ((column (cdr (assq 'columns alist))))
  4284. (when column (setq-local org-columns-default-format column)))
  4285. ;; Constants.
  4286. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4287. ;; Link abbreviations.
  4288. (let ((links (cdr (assq 'link alist))))
  4289. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4290. ;; Priorities.
  4291. (let ((priorities (cdr (assq 'priorities alist))))
  4292. (when priorities
  4293. (setq-local org-highest-priority (nth 0 priorities))
  4294. (setq-local org-lowest-priority (nth 1 priorities))
  4295. (setq-local org-default-priority (nth 2 priorities))))
  4296. ;; Scripts.
  4297. (let ((scripts (assq 'scripts alist)))
  4298. (when scripts
  4299. (setq-local org-use-sub-superscripts (cdr scripts))))
  4300. ;; Startup options.
  4301. (let ((startup (cdr (assq 'startup alist))))
  4302. (dolist (option startup)
  4303. (let ((entry (assoc-string option org-startup-options t)))
  4304. (when entry
  4305. (let ((var (nth 1 entry))
  4306. (val (nth 2 entry)))
  4307. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4308. (unless (listp (symbol-value var))
  4309. (set (make-local-variable var) nil))
  4310. (add-to-list var val)))))))
  4311. ;; TODO keywords.
  4312. (setq-local org-todo-kwd-alist nil)
  4313. (setq-local org-todo-key-alist nil)
  4314. (setq-local org-todo-key-trigger nil)
  4315. (setq-local org-todo-keywords-1 nil)
  4316. (setq-local org-done-keywords nil)
  4317. (setq-local org-todo-heads nil)
  4318. (setq-local org-todo-sets nil)
  4319. (setq-local org-todo-log-states nil)
  4320. (let ((todo-sequences
  4321. (or (nreverse (cdr (assq 'todo alist)))
  4322. (let ((d (default-value 'org-todo-keywords)))
  4323. (if (not (stringp (car d))) d
  4324. ;; XXX: Backward compatibility code.
  4325. (list (cons org-todo-interpretation d)))))))
  4326. (dolist (sequence todo-sequences)
  4327. (let* ((sequence (or (run-hook-with-args-until-success
  4328. 'org-todo-setup-filter-hook sequence)
  4329. sequence))
  4330. (sequence-type (car sequence))
  4331. (keywords (cdr sequence))
  4332. (sep (member "|" keywords))
  4333. names alist)
  4334. (dolist (k (remove "|" keywords))
  4335. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4336. k)
  4337. (error "Invalid TODO keyword %s" k))
  4338. (let ((name (match-string 1 k))
  4339. (key (match-string 2 k))
  4340. (log (org-extract-log-state-settings k)))
  4341. (push name names)
  4342. (push (cons name (and key (string-to-char key))) alist)
  4343. (when log (push log org-todo-log-states))))
  4344. (let* ((names (nreverse names))
  4345. (done (if sep (org-remove-keyword-keys (cdr sep))
  4346. (last names)))
  4347. (head (car names))
  4348. (tail (list sequence-type head (car done) (org-last done))))
  4349. (add-to-list 'org-todo-heads head 'append)
  4350. (push names org-todo-sets)
  4351. (setq org-done-keywords (append org-done-keywords done nil))
  4352. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4353. (setq org-todo-key-alist
  4354. (append org-todo-key-alist
  4355. (and alist
  4356. (append '((:startgroup))
  4357. (nreverse alist)
  4358. '((:endgroup))))))
  4359. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4360. (setq org-todo-sets (nreverse org-todo-sets)
  4361. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4362. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4363. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4364. ;; Compute the regular expressions and other local variables.
  4365. ;; Using `org-outline-regexp-bol' would complicate them much,
  4366. ;; because of the fixed white space at the end of that string.
  4367. (unless org-done-keywords
  4368. (setq org-done-keywords
  4369. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4370. (setq org-not-done-keywords
  4371. (org-delete-all org-done-keywords
  4372. (copy-sequence org-todo-keywords-1))
  4373. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4374. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4375. org-not-done-heading-regexp
  4376. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4377. org-todo-line-regexp
  4378. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4379. org-complex-heading-regexp
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4383. "\\(?: +\\(.*?\\)\\)??"
  4384. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4385. "[ \t]*$")
  4386. org-complex-heading-regexp-format
  4387. (concat "^\\(\\*+\\)"
  4388. "\\(?: +" org-todo-regexp "\\)?"
  4389. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4390. "\\(?: +"
  4391. ;; Stats cookies can be stuck to body.
  4392. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4393. "\\(%s\\)"
  4394. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4395. "\\)"
  4396. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4397. "[ \t]*$")
  4398. org-todo-line-tags-regexp
  4399. (concat "^\\(\\*+\\)"
  4400. "\\(?: +" org-todo-regexp "\\)?"
  4401. "\\(?: +\\(.*?\\)\\)??"
  4402. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4403. "[ \t]*$"))
  4404. (org-compute-latex-and-related-regexp)))))
  4405. (defun org--setup-collect-keywords (regexp &optional files alist)
  4406. "Return setup keywords values as an alist.
  4407. REGEXP matches a subset of setup keywords. FILES is a list of
  4408. file names already visited. It is used to avoid circular setup
  4409. files. ALIST, when non-nil, is the alist computed so far.
  4410. Return value contains the following keys: `archive', `category',
  4411. `columns', `constants', `filetags', `link', `priorities',
  4412. `property', `scripts', `startup', `tags' and `todo'."
  4413. (org-with-wide-buffer
  4414. (goto-char (point-min))
  4415. (let ((case-fold-search t))
  4416. (while (re-search-forward regexp nil t)
  4417. (let ((element (org-element-at-point)))
  4418. (when (eq (org-element-type element) 'keyword)
  4419. (let ((key (org-element-property :key element))
  4420. (value (org-element-property :value element)))
  4421. (cond
  4422. ((equal key "ARCHIVE")
  4423. (when (org-string-nw-p value)
  4424. (push (cons 'archive value) alist)))
  4425. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4426. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4427. ((equal key "CONSTANTS")
  4428. (let* ((constants (assq 'constants alist))
  4429. (store (cdr constants)))
  4430. (dolist (pair (org-split-string value))
  4431. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4432. pair)
  4433. (let* ((name (match-string 1 pair))
  4434. (value (match-string 2 pair))
  4435. (old (assoc name store)))
  4436. (if old (setcdr old value)
  4437. (push (cons name value) store)))))
  4438. (if constants (setcdr constants store)
  4439. (push (cons 'constants store) alist))))
  4440. ((equal key "FILETAGS")
  4441. (when (org-string-nw-p value)
  4442. (let ((old (assq 'filetags alist))
  4443. (new (apply #'nconc
  4444. (mapcar (lambda (x) (org-split-string x ":"))
  4445. (org-split-string value)))))
  4446. (if old (setcdr old (append new (cdr old)))
  4447. (push (cons 'filetags new) alist)))))
  4448. ((equal key "LINK")
  4449. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4450. (let ((links (assq 'link alist))
  4451. (pair (cons (org-match-string-no-properties 1 value)
  4452. (org-match-string-no-properties 2 value))))
  4453. (if links (push pair (cdr links))
  4454. (push (list 'link pair) alist)))))
  4455. ((equal key "OPTIONS")
  4456. (when (and (org-string-nw-p value)
  4457. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4458. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4459. ((equal key "PRIORITIES")
  4460. (push (cons 'priorities
  4461. (let ((prio (org-split-string value)))
  4462. (if (< (length prio) 3) '(?A ?C ?B)
  4463. (mapcar #'string-to-char prio))))
  4464. alist))
  4465. ((equal key "PROPERTY")
  4466. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4467. (let* ((property (assq 'property alist))
  4468. (value (org--update-property-plist
  4469. (org-match-string-no-properties 1 value)
  4470. (org-match-string-no-properties 2 value)
  4471. (cdr property))))
  4472. (if property (setcdr property value)
  4473. (push (cons 'property value) alist)))))
  4474. ((equal key "STARTUP")
  4475. (let ((startup (assq 'startup alist)))
  4476. (if startup
  4477. (setcdr startup
  4478. (append (cdr startup) (org-split-string value)))
  4479. (push (cons 'startup (org-split-string value)) alist))))
  4480. ((equal key "TAGS")
  4481. (let ((tag-cell (assq 'tags alist)))
  4482. (if tag-cell
  4483. (setcdr tag-cell
  4484. (append (cdr tag-cell)
  4485. '("\\n")
  4486. (org-split-string value)))
  4487. (push (cons 'tags (org-split-string value)) alist))))
  4488. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4489. (let ((todo (assq 'todo alist))
  4490. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4491. (org-split-string value))))
  4492. (if todo (push value (cdr todo))
  4493. (push (list 'todo value) alist))))
  4494. ((equal key "SETUPFILE")
  4495. (unless buffer-read-only ; Do not check in Gnus messages.
  4496. (let ((f (and (org-string-nw-p value)
  4497. (expand-file-name
  4498. (org-remove-double-quotes value)))))
  4499. (when (and f (file-readable-p f) (not (member f files)))
  4500. (with-temp-buffer
  4501. (setq default-directory (file-name-directory f))
  4502. (insert-file-contents f)
  4503. (setq alist
  4504. ;; Fake Org mode to benefit from cache
  4505. ;; without recurring needlessly.
  4506. (let ((major-mode 'org-mode))
  4507. (org--setup-collect-keywords
  4508. regexp (cons f files) alist)))))))))))))))
  4509. alist)
  4510. (defun org--setup-process-tags (tags filetags)
  4511. "Precompute variables used for tags.
  4512. TAGS is a list of tags and tag group symbols, as strings.
  4513. FILETAGS is a list of tags, as strings."
  4514. ;; Process the file tags.
  4515. (setq-local org-file-tags
  4516. (mapcar #'org-add-prop-inherited filetags))
  4517. ;; Provide default tags if no local tags are found.
  4518. (when (and (not tags) org-tag-alist)
  4519. (setq tags
  4520. (mapcar (lambda (tag)
  4521. (cl-case (car tag)
  4522. (:startgroup "{")
  4523. (:endgroup "}")
  4524. (:startgrouptag "[")
  4525. (:endgrouptag "]")
  4526. (:grouptags ":")
  4527. (:newline "\\n")
  4528. (otherwise (concat (car tag)
  4529. (and (characterp (cdr tag))
  4530. (format "(%c)" (cdr tag)))))))
  4531. org-tag-alist)))
  4532. ;; Process the tags.
  4533. (setq-local org-tag-groups-alist nil)
  4534. (setq-local org-tag-alist nil)
  4535. (let (group-flag)
  4536. (while tags
  4537. (let ((e (car tags)))
  4538. (setq tags (cdr tags))
  4539. (cond
  4540. ((equal e "{")
  4541. (push '(:startgroup) org-tag-alist)
  4542. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4543. ((equal e "}")
  4544. (push '(:endgroup) org-tag-alist)
  4545. (setq group-flag nil))
  4546. ((equal e "[")
  4547. (push '(:startgrouptag) org-tag-alist)
  4548. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4549. ((equal e "]")
  4550. (push '(:endgrouptag) org-tag-alist)
  4551. (setq group-flag nil))
  4552. ((equal e ":")
  4553. (push '(:grouptags) org-tag-alist)
  4554. (setq group-flag 'append))
  4555. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4556. ((string-match
  4557. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4558. "\\|{.+?}\\)" ; regular expression
  4559. "\\(?:(\\(.\\))\\)?\\'")) e)
  4560. (let ((tag (match-string 1 e))
  4561. (key (and (match-beginning 2)
  4562. (string-to-char (match-string 2 e)))))
  4563. (cond ((eq group-flag 'append)
  4564. (setcar org-tag-groups-alist
  4565. (append (car org-tag-groups-alist) (list tag))))
  4566. (group-flag (push (list tag) org-tag-groups-alist)))
  4567. ;; Push all tags in groups, no matter if they already exist.
  4568. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4569. (push (cons tag key) org-tag-alist))))))))
  4570. (setq org-tag-alist (nreverse org-tag-alist)))
  4571. (defun org-file-contents (file &optional noerror)
  4572. "Return the contents of FILE, as a string."
  4573. (if (and file (file-readable-p file))
  4574. (with-temp-buffer
  4575. (insert-file-contents file)
  4576. (buffer-string))
  4577. (funcall (if noerror 'message 'error)
  4578. "Cannot read file \"%s\"%s"
  4579. file
  4580. (let ((from (buffer-file-name (buffer-base-buffer))))
  4581. (if from (concat " (referenced in file \"" from "\")") "")))))
  4582. (defun org-extract-log-state-settings (x)
  4583. "Extract the log state setting from a TODO keyword string.
  4584. This will extract info from a string like \"WAIT(w@/!)\"."
  4585. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4586. (let ((kw (match-string 1 x))
  4587. (log1 (and (match-end 3) (match-string 3 x)))
  4588. (log2 (and (match-end 4) (match-string 4 x))))
  4589. (and (or log1 log2)
  4590. (list kw
  4591. (and log1 (if (equal log1 "!") 'time 'note))
  4592. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4593. (defun org-remove-keyword-keys (list)
  4594. "Remove a pair of parenthesis at the end of each string in LIST."
  4595. (mapcar (lambda (x)
  4596. (if (string-match "(.*)$" x)
  4597. (substring x 0 (match-beginning 0))
  4598. x))
  4599. list))
  4600. (defun org-assign-fast-keys (alist)
  4601. "Assign fast keys to a keyword-key alist.
  4602. Respect keys that are already there."
  4603. (let (new e (alt ?0))
  4604. (while (setq e (pop alist))
  4605. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4606. (cdr e)) ;; Key already assigned.
  4607. (push e new)
  4608. (let ((clist (string-to-list (downcase (car e))))
  4609. (used (append new alist)))
  4610. (when (= (car clist) ?@)
  4611. (pop clist))
  4612. (while (and clist (rassoc (car clist) used))
  4613. (pop clist))
  4614. (unless clist
  4615. (while (rassoc alt used)
  4616. (cl-incf alt)))
  4617. (push (cons (car e) (or (car clist) alt)) new))))
  4618. (nreverse new)))
  4619. ;;; Some variables used in various places
  4620. (defvar org-window-configuration nil
  4621. "Used in various places to store a window configuration.")
  4622. (defvar org-selected-window nil
  4623. "Used in various places to store a window configuration.")
  4624. (defvar org-finish-function nil
  4625. "Function to be called when `C-c C-c' is used.
  4626. This is for getting out of special buffers like capture.")
  4627. (defvar org-last-state)
  4628. ;; Defined somewhere in this file, but used before definition.
  4629. (defvar org-entities) ;; defined in org-entities.el
  4630. (defvar org-struct-menu)
  4631. (defvar org-org-menu)
  4632. (defvar org-tbl-menu)
  4633. ;;;; Define the Org-mode
  4634. ;; We use a before-change function to check if a table might need
  4635. ;; an update.
  4636. (defvar org-table-may-need-update t
  4637. "Indicates that a table might need an update.
  4638. This variable is set by `org-before-change-function'.
  4639. `org-table-align' sets it back to nil.")
  4640. (defun org-before-change-function (_beg _end)
  4641. "Every change indicates that a table might need an update."
  4642. (setq org-table-may-need-update t))
  4643. (defvar org-mode-map)
  4644. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4645. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4646. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4647. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4648. (defvar org-table-buffer-is-an nil)
  4649. (defvar bidi-paragraph-direction)
  4650. (defvar buffer-face-mode-face)
  4651. (require 'outline)
  4652. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4653. (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"))
  4654. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4655. ;; Other stuff we need.
  4656. (require 'time-date)
  4657. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4658. (require 'easymenu)
  4659. (autoload 'easy-menu-add "easymenu")
  4660. (require 'overlay)
  4661. ;; (require 'org-macs) moved higher up in the file before it is first used
  4662. (require 'org-entities)
  4663. ;; (require 'org-compat) moved higher up in the file before it is first used
  4664. (require 'org-faces)
  4665. (require 'org-list)
  4666. (require 'org-pcomplete)
  4667. (require 'org-src)
  4668. (require 'org-footnote)
  4669. (require 'org-macro)
  4670. ;; babel
  4671. (require 'ob)
  4672. ;;;###autoload
  4673. (define-derived-mode org-mode outline-mode "Org"
  4674. "Outline-based notes management and organizer, alias
  4675. \"Carsten's outline-mode for keeping track of everything.\"
  4676. Org-mode develops organizational tasks around a NOTES file which
  4677. contains information about projects as plain text. Org-mode is
  4678. implemented on top of outline-mode, which is ideal to keep the content
  4679. of large files well structured. It supports ToDo items, deadlines and
  4680. time stamps, which magically appear in the diary listing of the Emacs
  4681. calendar. Tables are easily created with a built-in table editor.
  4682. Plain text URL-like links connect to websites, emails (VM), Usenet
  4683. messages (Gnus), BBDB entries, and any files related to the project.
  4684. For printing and sharing of notes, an Org-mode file (or a part of it)
  4685. can be exported as a structured ASCII or HTML file.
  4686. The following commands are available:
  4687. \\{org-mode-map}"
  4688. ;; Get rid of Outline menus, they are not needed
  4689. ;; Need to do this here because define-derived-mode sets up
  4690. ;; the keymap so late. Still, it is a waste to call this each time
  4691. ;; we switch another buffer into org-mode.
  4692. (if (featurep 'xemacs)
  4693. (when (boundp 'outline-mode-menu-heading)
  4694. ;; Assume this is Greg's port, it uses easymenu
  4695. (easy-menu-remove outline-mode-menu-heading)
  4696. (easy-menu-remove outline-mode-menu-show)
  4697. (easy-menu-remove outline-mode-menu-hide))
  4698. (define-key org-mode-map [menu-bar headings] 'undefined)
  4699. (define-key org-mode-map [menu-bar hide] 'undefined)
  4700. (define-key org-mode-map [menu-bar show] 'undefined))
  4701. (org-load-modules-maybe)
  4702. (when (featurep 'xemacs)
  4703. (easy-menu-add org-org-menu)
  4704. (easy-menu-add org-tbl-menu))
  4705. (org-install-agenda-files-menu)
  4706. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4707. (add-to-invisibility-spec '(org-cwidth))
  4708. (add-to-invisibility-spec '(org-hide-block . t))
  4709. (when (featurep 'xemacs)
  4710. (setq-local line-move-ignore-invisible t))
  4711. (setq-local outline-regexp org-outline-regexp)
  4712. (setq-local outline-level 'org-outline-level)
  4713. (setq bidi-paragraph-direction 'left-to-right)
  4714. (when (and org-ellipsis
  4715. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4716. (fboundp 'make-glyph-code))
  4717. (unless org-display-table
  4718. (setq org-display-table (make-display-table)))
  4719. (set-display-table-slot
  4720. org-display-table 4
  4721. (vconcat (mapcar
  4722. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4723. org-ellipsis)))
  4724. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4725. (setq buffer-display-table org-display-table))
  4726. (org-set-regexps-and-options)
  4727. (org-set-font-lock-defaults)
  4728. (when (and org-tag-faces (not org-tags-special-faces-re))
  4729. ;; tag faces set outside customize.... force initialization.
  4730. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4731. ;; Calc embedded
  4732. (setq-local calc-embedded-open-mode "# ")
  4733. ;; Modify a few syntax entries
  4734. (modify-syntax-entry ?@ "w")
  4735. (modify-syntax-entry ?\" "\"")
  4736. (modify-syntax-entry ?\\ "_")
  4737. (modify-syntax-entry ?~ "_")
  4738. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4739. ;; Activate before-change-function
  4740. (setq-local org-table-may-need-update t)
  4741. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4742. ;; Check for running clock before killing a buffer
  4743. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4744. ;; Initialize macros templates.
  4745. (org-macro-initialize-templates)
  4746. ;; Initialize radio targets.
  4747. (org-update-radio-target-regexp)
  4748. ;; Indentation.
  4749. (setq-local indent-line-function 'org-indent-line)
  4750. (setq-local indent-region-function 'org-indent-region)
  4751. ;; Filling and auto-filling.
  4752. (org-setup-filling)
  4753. ;; Comments.
  4754. (org-setup-comments-handling)
  4755. ;; Initialize cache.
  4756. (org-element-cache-reset)
  4757. ;; Beginning/end of defun
  4758. (setq-local beginning-of-defun-function 'org-backward-element)
  4759. (setq-local end-of-defun-function
  4760. (lambda ()
  4761. (if (not (org-at-heading-p))
  4762. (org-forward-element)
  4763. (org-forward-element)
  4764. (forward-char -1))))
  4765. ;; Next error for sparse trees
  4766. (setq-local next-error-function 'org-occur-next-match)
  4767. ;; Make sure dependence stuff works reliably, even for users who set it
  4768. ;; too late :-(
  4769. (if org-enforce-todo-dependencies
  4770. (add-hook 'org-blocker-hook
  4771. 'org-block-todo-from-children-or-siblings-or-parent)
  4772. (remove-hook 'org-blocker-hook
  4773. 'org-block-todo-from-children-or-siblings-or-parent))
  4774. (if org-enforce-todo-checkbox-dependencies
  4775. (add-hook 'org-blocker-hook
  4776. 'org-block-todo-from-checkboxes)
  4777. (remove-hook 'org-blocker-hook
  4778. 'org-block-todo-from-checkboxes))
  4779. ;; Align options lines
  4780. (setq-local
  4781. align-mode-rules-list
  4782. '((org-in-buffer-settings
  4783. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4784. (modes . '(org-mode)))))
  4785. ;; Imenu
  4786. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4787. ;; Make isearch reveal context
  4788. (setq-local outline-isearch-open-invisible-function
  4789. (lambda (&rest _) (org-show-context 'isearch)))
  4790. ;; Setup the pcomplete hooks
  4791. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4792. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4793. (setq-local pcomplete-default-completion-function 'ignore)
  4794. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4795. (setq-local pcomplete-termination-string "")
  4796. (setq-local buffer-face-mode-face 'org-default)
  4797. ;; If empty file that did not turn on Org mode automatically, make
  4798. ;; it to.
  4799. (when (and org-insert-mode-line-in-empty-file
  4800. (org-called-interactively-p 'any)
  4801. (= (point-min) (point-max)))
  4802. (insert "# -*- mode: org -*-\n\n"))
  4803. (unless org-inhibit-startup
  4804. (org-unmodified
  4805. (when org-startup-with-beamer-mode (org-beamer-mode))
  4806. (when org-startup-align-all-tables
  4807. (org-table-map-tables #'org-table-align t))
  4808. (when org-startup-with-inline-images (org-display-inline-images))
  4809. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4810. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4811. (when org-startup-truncated (setq truncate-lines t))
  4812. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4813. (org-refresh-effort-properties)))
  4814. ;; Try to set `org-hide' face correctly.
  4815. (let ((foreground (org-find-invisible-foreground)))
  4816. (when foreground
  4817. (set-face-foreground 'org-hide foreground))))
  4818. ;; Update `customize-package-emacs-version-alist'
  4819. (add-to-list 'customize-package-emacs-version-alist
  4820. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4821. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4822. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4823. (defvar org-mode-transpose-word-syntax-table
  4824. (let ((st (make-syntax-table text-mode-syntax-table)))
  4825. (dolist (c org-emphasis-alist st)
  4826. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4827. (when (fboundp 'abbrev-table-put)
  4828. (abbrev-table-put org-mode-abbrev-table
  4829. :parents (list text-mode-abbrev-table)))
  4830. (defun org-find-invisible-foreground ()
  4831. (let ((candidates (remove
  4832. "unspecified-bg"
  4833. (nconc
  4834. (list (face-background 'default)
  4835. (face-background 'org-default))
  4836. (mapcar
  4837. (lambda (alist)
  4838. (when (boundp alist)
  4839. (cdr (assoc 'background-color (symbol-value alist)))))
  4840. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4841. (list (face-foreground 'org-hide))))))
  4842. (car (remove nil candidates))))
  4843. (defun org-current-time (&optional rounding-minutes past)
  4844. "Current time, possibly rounded to ROUNDING-MINUTES.
  4845. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4846. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4847. the rounding returns a past time."
  4848. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4849. (car org-time-stamp-rounding-minutes)))
  4850. (time (decode-time)) res)
  4851. (if (< r 1)
  4852. (current-time)
  4853. (setq res
  4854. (apply 'encode-time
  4855. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4856. (nthcdr 2 time))))
  4857. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4858. (seconds-to-time (- (org-float-time res) (* r 60)))
  4859. res))))
  4860. (defun org-today ()
  4861. "Return today date, considering `org-extend-today-until'."
  4862. (time-to-days
  4863. (time-subtract (current-time)
  4864. (list 0 (* 3600 org-extend-today-until) 0))))
  4865. ;;;; Font-Lock stuff, including the activators
  4866. (defvar org-mouse-map (make-sparse-keymap))
  4867. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4868. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4869. (when org-mouse-1-follows-link
  4870. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4871. (when org-tab-follows-link
  4872. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4873. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4874. (require 'font-lock)
  4875. (defconst org-non-link-chars "]\t\n\r<>")
  4876. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4877. "file+sys" "news" "shell" "elisp" "doi" "message"
  4878. "help"))
  4879. (defvar org-link-types-re nil
  4880. "Matches a link that has a url-like prefix like \"http:\"")
  4881. (defvar org-link-re-with-space nil
  4882. "Matches a link with spaces, optional angular brackets around it.")
  4883. (defvar org-link-re-with-space2 nil
  4884. "Matches a link with spaces, optional angular brackets around it.")
  4885. (defvar org-link-re-with-space3 nil
  4886. "Matches a link with spaces, only for internal part in bracket links.")
  4887. (defvar org-angle-link-re nil
  4888. "Matches link with angular brackets, spaces are allowed.")
  4889. (defvar org-plain-link-re nil
  4890. "Matches plain link, without spaces.")
  4891. (defvar org-bracket-link-regexp nil
  4892. "Matches a link in double brackets.")
  4893. (defvar org-bracket-link-analytic-regexp nil
  4894. "Regular expression used to analyze links.
  4895. Here is what the match groups contain after a match:
  4896. 1: http:
  4897. 2: http
  4898. 3: path
  4899. 4: [desc]
  4900. 5: desc")
  4901. (defvar org-bracket-link-analytic-regexp++ nil
  4902. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4903. (defvar org-any-link-re nil
  4904. "Regular expression matching any link.")
  4905. (defconst org-match-sexp-depth 3
  4906. "Number of stacked braces for sub/superscript matching.")
  4907. (defun org-create-multibrace-regexp (left right n)
  4908. "Create a regular expression which will match a balanced sexp.
  4909. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4910. as single character strings.
  4911. The regexp returned will match the entire expression including the
  4912. delimiters. It will also define a single group which contains the
  4913. match except for the outermost delimiters. The maximum depth of
  4914. stacked delimiters is N. Escaping delimiters is not possible."
  4915. (let* ((nothing (concat "[^" left right "]*?"))
  4916. (or "\\|")
  4917. (re nothing)
  4918. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4919. (while (> n 1)
  4920. (setq n (1- n)
  4921. re (concat re or next)
  4922. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4923. (concat left "\\(" re "\\)" right)))
  4924. (defconst org-match-substring-regexp
  4925. (concat
  4926. "\\(\\S-\\)\\([_^]\\)\\("
  4927. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4928. "\\|"
  4929. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4930. "\\|"
  4931. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4932. "The regular expression matching a sub- or superscript.")
  4933. (defconst org-match-substring-with-braces-regexp
  4934. (concat
  4935. "\\(\\S-\\)\\([_^]\\)"
  4936. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4937. "The regular expression matching a sub- or superscript, forcing braces.")
  4938. (defun org-make-link-regexps ()
  4939. "Update the link regular expressions.
  4940. This should be called after the variable `org-link-types' has changed."
  4941. (let ((types-re (regexp-opt org-link-types t)))
  4942. (setq org-link-types-re
  4943. (concat "\\`" types-re ":")
  4944. org-link-re-with-space
  4945. (concat "<?" types-re ":"
  4946. "\\([^" org-non-link-chars " ]"
  4947. "[^" org-non-link-chars "]*"
  4948. "[^" org-non-link-chars " ]\\)>?")
  4949. org-link-re-with-space2
  4950. (concat "<?" types-re ":"
  4951. "\\([^" org-non-link-chars " ]"
  4952. "[^\t\n\r]*"
  4953. "[^" org-non-link-chars " ]\\)>?")
  4954. org-link-re-with-space3
  4955. (concat "<?" types-re ":"
  4956. "\\([^" org-non-link-chars " ]"
  4957. "[^\t\n\r]*\\)")
  4958. org-angle-link-re
  4959. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  4960. types-re)
  4961. org-plain-link-re
  4962. (concat
  4963. "\\<" types-re ":"
  4964. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4965. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4966. org-bracket-link-regexp
  4967. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4968. org-bracket-link-analytic-regexp
  4969. (concat
  4970. "\\[\\["
  4971. "\\(" types-re ":\\)?"
  4972. "\\([^]]+\\)"
  4973. "\\]"
  4974. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4975. "\\]")
  4976. org-bracket-link-analytic-regexp++
  4977. (concat
  4978. "\\[\\["
  4979. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  4980. "\\([^]]+\\)"
  4981. "\\]"
  4982. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4983. "\\]")
  4984. org-any-link-re
  4985. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4986. org-angle-link-re "\\)\\|\\("
  4987. org-plain-link-re "\\)"))))
  4988. (org-make-link-regexps)
  4989. (defvar org-emph-face nil)
  4990. (defun org-do-emphasis-faces (limit)
  4991. "Run through the buffer and emphasize strings."
  4992. (let (rtn a)
  4993. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4994. (let* ((border (char-after (match-beginning 3)))
  4995. (bre (regexp-quote (char-to-string border))))
  4996. (when (and (not (= border (char-after (match-beginning 4))))
  4997. (not (string-match-p (concat bre ".*" bre)
  4998. (replace-regexp-in-string
  4999. "\n" " "
  5000. (substring (match-string 2) 1 -1)))))
  5001. (setq rtn t)
  5002. (setq a (assoc (match-string 3) org-emphasis-alist))
  5003. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5004. 'face
  5005. (nth 1 a))
  5006. (and (nth 2 a)
  5007. (org-remove-flyspell-overlays-in
  5008. (match-beginning 0) (match-end 0)))
  5009. (add-text-properties (match-beginning 2) (match-end 2)
  5010. '(font-lock-multiline t org-emphasis t))
  5011. (when org-hide-emphasis-markers
  5012. (add-text-properties (match-end 4) (match-beginning 5)
  5013. '(invisible org-link))
  5014. (add-text-properties (match-beginning 3) (match-end 3)
  5015. '(invisible org-link)))))
  5016. (goto-char (1+ (match-beginning 0))))
  5017. rtn))
  5018. (defun org-emphasize (&optional char)
  5019. "Insert or change an emphasis, i.e. a font like bold or italic.
  5020. If there is an active region, change that region to a new emphasis.
  5021. If there is no region, just insert the marker characters and position
  5022. the cursor between them.
  5023. CHAR should be the marker character. If it is a space, it means to
  5024. remove the emphasis of the selected region.
  5025. If CHAR is not given (for example in an interactive call) it will be
  5026. prompted for."
  5027. (interactive)
  5028. (let ((erc org-emphasis-regexp-components)
  5029. (string "") beg end move s)
  5030. (if (org-region-active-p)
  5031. (setq beg (region-beginning)
  5032. end (region-end)
  5033. string (buffer-substring beg end))
  5034. (setq move t))
  5035. (unless char
  5036. (message "Emphasis marker or tag: [%s]"
  5037. (mapconcat #'car org-emphasis-alist ""))
  5038. (setq char (read-char-exclusive)))
  5039. (if (equal char ?\s)
  5040. (setq s ""
  5041. move nil)
  5042. (unless (assoc (char-to-string char) org-emphasis-alist)
  5043. (user-error "No such emphasis marker: \"%c\"" char))
  5044. (setq s (char-to-string char)))
  5045. (while (and (> (length string) 1)
  5046. (equal (substring string 0 1) (substring string -1))
  5047. (assoc (substring string 0 1) org-emphasis-alist))
  5048. (setq string (substring string 1 -1)))
  5049. (setq string (concat s string s))
  5050. (when beg (delete-region beg end))
  5051. (unless (or (bolp)
  5052. (string-match (concat "[" (nth 0 erc) "\n]")
  5053. (char-to-string (char-before (point)))))
  5054. (insert " "))
  5055. (unless (or (eobp)
  5056. (string-match (concat "[" (nth 1 erc) "\n]")
  5057. (char-to-string (char-after (point)))))
  5058. (insert " ") (backward-char 1))
  5059. (insert string)
  5060. (and move (backward-char 1))))
  5061. (defconst org-nonsticky-props
  5062. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5063. (defsubst org-rear-nonsticky-at (pos)
  5064. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5065. (defun org-activate-plain-links (limit)
  5066. "Add link properties for plain links."
  5067. (when (and (re-search-forward org-plain-link-re limit t)
  5068. (not (org-in-src-block-p)))
  5069. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5070. 'face))
  5071. (link (org-match-string-no-properties 0)))
  5072. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5073. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5074. (add-text-properties (match-beginning 0) (match-end 0)
  5075. (list 'mouse-face 'highlight
  5076. 'face 'org-link
  5077. 'htmlize-link `(:uri ,link)
  5078. 'keymap org-mouse-map))
  5079. (org-rear-nonsticky-at (match-end 0))
  5080. t))))
  5081. (defun org-activate-code (limit)
  5082. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5083. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5084. (remove-text-properties (match-beginning 0) (match-end 0)
  5085. '(display t invisible t intangible t))
  5086. t))
  5087. (defcustom org-src-fontify-natively t
  5088. "When non-nil, fontify code in code blocks."
  5089. :type 'boolean
  5090. :version "24.4"
  5091. :package-version '(Org . "8.3")
  5092. :group 'org-appearance
  5093. :group 'org-babel)
  5094. (defcustom org-allow-promoting-top-level-subtree nil
  5095. "When non-nil, allow promoting a top level subtree.
  5096. The leading star of the top level headline will be replaced
  5097. by a #."
  5098. :type 'boolean
  5099. :version "24.1"
  5100. :group 'org-appearance)
  5101. (defun org-fontify-meta-lines-and-blocks (limit)
  5102. (condition-case nil
  5103. (org-fontify-meta-lines-and-blocks-1 limit)
  5104. (error (message "org-mode fontification error"))))
  5105. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5106. "Fontify #+ lines and blocks."
  5107. (let ((case-fold-search t))
  5108. (when (re-search-forward
  5109. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5110. limit t)
  5111. (let ((beg (match-beginning 0))
  5112. (block-start (match-end 0))
  5113. (block-end nil)
  5114. (lang (match-string 7))
  5115. (beg1 (line-beginning-position 2))
  5116. (dc1 (downcase (match-string 2)))
  5117. (dc3 (downcase (match-string 3)))
  5118. end end1 quoting block-type)
  5119. (cond
  5120. ((and (match-end 4) (equal dc3 "+begin"))
  5121. ;; Truly a block
  5122. (setq block-type (downcase (match-string 5))
  5123. quoting (member block-type org-protecting-blocks))
  5124. (when (re-search-forward
  5125. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5126. nil t) ;; on purpose, we look further than LIMIT
  5127. (setq end (min (point-max) (match-end 0))
  5128. end1 (min (point-max) (1- (match-beginning 0))))
  5129. (setq block-end (match-beginning 0))
  5130. (when quoting
  5131. (org-remove-flyspell-overlays-in beg1 end1)
  5132. (remove-text-properties beg end
  5133. '(display t invisible t intangible t)))
  5134. (add-text-properties
  5135. beg end '(font-lock-fontified t font-lock-multiline t))
  5136. (add-text-properties beg beg1 '(face org-meta-line))
  5137. (org-remove-flyspell-overlays-in beg beg1)
  5138. (add-text-properties ; For end_src
  5139. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5140. (org-remove-flyspell-overlays-in end1 end)
  5141. (cond
  5142. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5143. (org-src-font-lock-fontify-block lang block-start block-end)
  5144. (add-text-properties beg1 block-end '(src-block t)))
  5145. (quoting
  5146. (add-text-properties beg1 (min (point-max) (1+ end1))
  5147. '(face org-block))) ; end of source block
  5148. ((not org-fontify-quote-and-verse-blocks))
  5149. ((string= block-type "quote")
  5150. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5151. ((string= block-type "verse")
  5152. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5153. (add-text-properties beg beg1 '(face org-block-begin-line))
  5154. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5155. '(face org-block-end-line))
  5156. t))
  5157. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5158. (org-remove-flyspell-overlays-in
  5159. (match-beginning 0)
  5160. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5161. (add-text-properties
  5162. beg (match-end 3)
  5163. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5164. '(font-lock-fontified t invisible t)
  5165. '(font-lock-fontified t face org-document-info-keyword)))
  5166. (add-text-properties
  5167. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5168. (if (string-equal dc1 "+title:")
  5169. '(font-lock-fontified t face org-document-title)
  5170. '(font-lock-fontified t face org-document-info))))
  5171. ((equal dc1 "+caption:")
  5172. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5173. (remove-text-properties (match-beginning 0) (match-end 0)
  5174. '(display t invisible t intangible t))
  5175. (add-text-properties (match-beginning 1) (match-end 3)
  5176. '(font-lock-fontified t face org-meta-line))
  5177. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5178. '(font-lock-fontified t face org-block))
  5179. t)
  5180. ((member dc3 '(" " ""))
  5181. (org-remove-flyspell-overlays-in beg (match-end 0))
  5182. (add-text-properties
  5183. beg (match-end 0)
  5184. '(font-lock-fontified t face font-lock-comment-face)))
  5185. (t ;; just any other in-buffer setting, but not indented
  5186. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5187. (remove-text-properties (match-beginning 0) (match-end 0)
  5188. '(display t invisible t intangible t))
  5189. (add-text-properties beg (match-end 0)
  5190. '(font-lock-fontified t face org-meta-line))
  5191. t))))))
  5192. (defun org-fontify-drawers (limit)
  5193. "Fontify drawers."
  5194. (when (re-search-forward org-drawer-regexp limit t)
  5195. (add-text-properties
  5196. (match-beginning 0) (match-end 0)
  5197. '(font-lock-fontified t face org-special-keyword))
  5198. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5199. t))
  5200. (defun org-fontify-macros (limit)
  5201. "Fontify macros."
  5202. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5203. (add-text-properties
  5204. (match-beginning 0) (match-end 0)
  5205. '(font-lock-fontified t face org-macro))
  5206. (when org-hide-macro-markers
  5207. (add-text-properties (match-end 2) (match-beginning 2)
  5208. '(invisible t))
  5209. (add-text-properties (match-beginning 1) (match-end 1)
  5210. '(invisible t)))
  5211. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5212. t))
  5213. (defun org-activate-angle-links (limit)
  5214. "Add text properties for angle links."
  5215. (when (and (re-search-forward org-angle-link-re limit t)
  5216. (not (org-in-src-block-p)))
  5217. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5218. (add-text-properties (match-beginning 0) (match-end 0)
  5219. (list 'mouse-face 'highlight
  5220. 'keymap org-mouse-map
  5221. 'font-lock-multiline t))
  5222. (org-rear-nonsticky-at (match-end 0))
  5223. t))
  5224. (defun org-activate-footnote-links (limit)
  5225. "Add text properties for footnotes."
  5226. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5227. (when fn
  5228. (let* ((beg (nth 1 fn))
  5229. (end (nth 2 fn))
  5230. (label (car fn))
  5231. (referencep (/= (line-beginning-position) beg)))
  5232. (when (and referencep (nth 3 fn))
  5233. (save-excursion
  5234. (goto-char beg)
  5235. (search-forward (or label "fn:"))
  5236. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5237. (add-text-properties beg end
  5238. (list 'mouse-face 'highlight
  5239. 'keymap org-mouse-map
  5240. 'help-echo
  5241. (if referencep "Footnote reference"
  5242. "Footnote definition")
  5243. 'font-lock-fontified t
  5244. 'font-lock-multiline t
  5245. 'face 'org-footnote))))))
  5246. (defun org-activate-bracket-links (limit)
  5247. "Add text properties for bracketed links."
  5248. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5249. (not (org-in-src-block-p)))
  5250. (let* ((hl (org-match-string-no-properties 1))
  5251. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5252. (ip (org-maybe-intangible
  5253. (list 'invisible 'org-link
  5254. 'keymap org-mouse-map 'mouse-face 'highlight
  5255. 'font-lock-multiline t 'help-echo help
  5256. 'htmlize-link `(:uri ,hl))))
  5257. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5258. 'font-lock-multiline t 'help-echo help
  5259. 'htmlize-link `(:uri ,hl))))
  5260. ;; We need to remove the invisible property here. Table narrowing
  5261. ;; may have made some of this invisible.
  5262. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5263. (remove-text-properties (match-beginning 0) (match-end 0)
  5264. '(invisible nil))
  5265. (if (match-end 3)
  5266. (progn
  5267. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5268. (org-rear-nonsticky-at (match-beginning 3))
  5269. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5270. (org-rear-nonsticky-at (match-end 3))
  5271. (add-text-properties (match-end 3) (match-end 0) ip)
  5272. (org-rear-nonsticky-at (match-end 0)))
  5273. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5274. (org-rear-nonsticky-at (match-beginning 1))
  5275. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5276. (org-rear-nonsticky-at (match-end 1))
  5277. (add-text-properties (match-end 1) (match-end 0) ip)
  5278. (org-rear-nonsticky-at (match-end 0)))
  5279. t)))
  5280. (defun org-activate-dates (limit)
  5281. "Add text properties for dates."
  5282. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5283. (not (equal (char-before (match-beginning 0)) 91)))
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. (add-text-properties (match-beginning 0) (match-end 0)
  5286. (list 'mouse-face 'highlight
  5287. 'keymap org-mouse-map))
  5288. (org-rear-nonsticky-at (match-end 0))
  5289. (when org-display-custom-times
  5290. (if (match-end 3)
  5291. (org-display-custom-time (match-beginning 3) (match-end 3))
  5292. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5293. t))
  5294. (defvar-local org-target-link-regexp nil
  5295. "Regular expression matching radio targets in plain text.")
  5296. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5297. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5298. border border border))
  5299. "Regular expression matching a link target.")
  5300. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5301. "Regular expression matching a radio target.")
  5302. (defconst org-any-target-regexp
  5303. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5304. "Regular expression matching any target.")
  5305. (defun org-activate-target-links (limit)
  5306. "Add text properties for target matches."
  5307. (when org-target-link-regexp
  5308. (let ((case-fold-search t))
  5309. (when (re-search-forward org-target-link-regexp limit t)
  5310. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5311. (add-text-properties (match-beginning 1) (match-end 1)
  5312. (list 'mouse-face 'highlight
  5313. 'keymap org-mouse-map
  5314. 'help-echo "Radio target link"
  5315. 'org-linked-text t))
  5316. (org-rear-nonsticky-at (match-end 1))
  5317. t))))
  5318. (defun org-update-radio-target-regexp ()
  5319. "Find all radio targets in this file and update the regular expression.
  5320. Also refresh fontification if needed."
  5321. (interactive)
  5322. (let ((old-regexp org-target-link-regexp)
  5323. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5324. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5325. (targets
  5326. (org-with-wide-buffer
  5327. (goto-char (point-min))
  5328. (let (rtn)
  5329. (while (re-search-forward org-radio-target-regexp nil t)
  5330. ;; Make sure point is really within the object.
  5331. (backward-char)
  5332. (let ((obj (org-element-context)))
  5333. (when (eq (org-element-type obj) 'radio-target)
  5334. (cl-pushnew (org-element-property :value obj) rtn
  5335. :test #'equal))))
  5336. rtn))))
  5337. (setq org-target-link-regexp
  5338. (and targets
  5339. (concat before-re
  5340. (mapconcat
  5341. (lambda (x)
  5342. (replace-regexp-in-string
  5343. " +" "\\s-+" (regexp-quote x) t t))
  5344. targets
  5345. "\\|")
  5346. after-re)))
  5347. (unless (equal old-regexp org-target-link-regexp)
  5348. ;; Clean-up cache.
  5349. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5350. ((not org-target-link-regexp) old-regexp)
  5351. (t
  5352. (concat before-re
  5353. (mapconcat
  5354. (lambda (re)
  5355. (substring re (length before-re)
  5356. (- (length after-re))))
  5357. (list old-regexp org-target-link-regexp)
  5358. "\\|")
  5359. after-re)))))
  5360. (org-with-wide-buffer
  5361. (goto-char (point-min))
  5362. (while (re-search-forward regexp nil t)
  5363. (org-element-cache-refresh (match-beginning 1)))))
  5364. ;; Re fontify buffer.
  5365. (when (memq 'radio org-highlight-links)
  5366. (org-restart-font-lock)))))
  5367. (defun org-hide-wide-columns (limit)
  5368. (let (s e)
  5369. (setq s (text-property-any (point) (or limit (point-max))
  5370. 'org-cwidth t))
  5371. (when s
  5372. (setq e (next-single-property-change s 'org-cwidth))
  5373. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5374. (goto-char e)
  5375. t)))
  5376. (defvar org-latex-and-related-regexp nil
  5377. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5378. (defun org-compute-latex-and-related-regexp ()
  5379. "Compute regular expression for LaTeX, entities and sub/superscript.
  5380. Result depends on variable `org-highlight-latex-and-related'."
  5381. (setq-local
  5382. org-latex-and-related-regexp
  5383. (let* ((re-sub
  5384. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5385. ((eq org-use-sub-superscripts '{})
  5386. (list org-match-substring-with-braces-regexp))
  5387. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5388. (re-latex
  5389. (when (memq 'latex org-highlight-latex-and-related)
  5390. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5391. (delq nil
  5392. (mapcar (lambda (x)
  5393. (and (member (car x) matchers) (nth 1 x)))
  5394. org-latex-regexps)))))
  5395. (re-entities
  5396. (when (memq 'entities org-highlight-latex-and-related)
  5397. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5398. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5399. (defun org-do-latex-and-related (limit)
  5400. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5401. LIMIT bounds the search for syntax to highlight. Stop at first
  5402. highlighted object, if any. Return t if some highlighting was
  5403. done, nil otherwise."
  5404. (when (org-string-nw-p org-latex-and-related-regexp)
  5405. (catch 'found
  5406. (while (re-search-forward org-latex-and-related-regexp limit t)
  5407. (unless
  5408. (cl-some
  5409. (lambda (f)
  5410. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5411. (save-excursion
  5412. (goto-char (1+ (match-beginning 0)))
  5413. (face-at-point nil t)))
  5414. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5415. '(?_ ?^))
  5416. 1
  5417. 0)))
  5418. (font-lock-prepend-text-property
  5419. (+ offset (match-beginning 0)) (match-end 0)
  5420. 'face 'org-latex-and-related)
  5421. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5422. '(font-lock-multiline t)))
  5423. (throw 'found t)))
  5424. nil)))
  5425. (defun org-restart-font-lock ()
  5426. "Restart `font-lock-mode', to force refontification."
  5427. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5428. (font-lock-mode -1)
  5429. (font-lock-mode 1)))
  5430. (defun org-activate-tags (limit)
  5431. (when (re-search-forward
  5432. (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") limit t)
  5433. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5434. (add-text-properties (match-beginning 1) (match-end 1)
  5435. (list 'mouse-face 'highlight
  5436. 'keymap org-mouse-map))
  5437. (org-rear-nonsticky-at (match-end 1))
  5438. t))
  5439. (defun org-outline-level ()
  5440. "Compute the outline level of the heading at point.
  5441. If this is called at a normal headline, the level is the number
  5442. of stars. Use `org-reduced-level' to remove the effect of
  5443. `org-odd-levels'. Unlike to `org-current-level', this function
  5444. takes into consideration inlinetasks."
  5445. (org-with-wide-buffer
  5446. (end-of-line)
  5447. (if (re-search-backward org-outline-regexp-bol nil t)
  5448. (1- (- (match-end 0) (match-beginning 0)))
  5449. 0)))
  5450. (defvar org-font-lock-keywords nil)
  5451. (defsubst org-re-property (property &optional literal allow-null value)
  5452. "Return a regexp matching a PROPERTY line.
  5453. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5454. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5455. non-nil, match properties even without a value.
  5456. Match group 3 is set to the value when it exists. If there is no
  5457. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5458. With optional argument VALUE, match only property lines with
  5459. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5460. unless LITERAL is non-nil."
  5461. (concat
  5462. "^\\(?4:[ \t]*\\)"
  5463. (format "\\(?1::\\(?2:%s\\):\\)"
  5464. (if literal property (regexp-quote property)))
  5465. (cond (value
  5466. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5467. (if literal value (regexp-quote value))))
  5468. (allow-null
  5469. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5470. (t
  5471. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5472. (defconst org-property-re
  5473. (org-re-property "\\S-+" 'literal t)
  5474. "Regular expression matching a property line.
  5475. There are four matching groups:
  5476. 1: :PROPKEY: including the leading and trailing colon,
  5477. 2: PROPKEY without the leading and trailing colon,
  5478. 3: PROPVAL without leading or trailing spaces,
  5479. 4: the indentation of the current line,
  5480. 5: trailing whitespace.")
  5481. (defvar org-font-lock-hook nil
  5482. "Functions to be called for special font lock stuff.")
  5483. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5484. (defvar org-font-lock-set-keywords-hook nil
  5485. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5486. This is called after `org-font-lock-extra-keywords' is defined, but before
  5487. it is installed to be used by font lock. This can be useful if something
  5488. needs to be inserted at a specific position in the font-lock sequence.")
  5489. (defun org-font-lock-hook (limit)
  5490. "Run `org-font-lock-hook' within LIMIT."
  5491. (run-hook-with-args 'org-font-lock-hook limit))
  5492. (defun org-set-font-lock-defaults ()
  5493. "Set font lock defaults for the current buffer."
  5494. (let* ((em org-fontify-emphasized-text)
  5495. (lk org-highlight-links)
  5496. (org-font-lock-extra-keywords
  5497. (list
  5498. ;; Call the hook
  5499. '(org-font-lock-hook)
  5500. ;; Headlines
  5501. `(,(if org-fontify-whole-heading-line
  5502. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5503. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5504. (1 (org-get-level-face 1))
  5505. (2 (org-get-level-face 2))
  5506. (3 (org-get-level-face 3)))
  5507. ;; Table lines
  5508. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5509. (1 'org-table t))
  5510. ;; Table internals
  5511. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5512. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5513. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5514. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5515. ;; Drawers
  5516. '(org-fontify-drawers)
  5517. ;; Properties
  5518. (list org-property-re
  5519. '(1 'org-special-keyword t)
  5520. '(3 'org-property-value t))
  5521. ;; Links
  5522. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5523. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5524. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5525. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5526. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5527. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5528. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5529. ;; Targets.
  5530. (list org-any-target-regexp '(0 'org-target t))
  5531. ;; Diary sexps.
  5532. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5533. ;; Macro
  5534. '(org-fontify-macros)
  5535. '(org-hide-wide-columns (0 nil append))
  5536. ;; TODO keyword
  5537. (list (format org-heading-keyword-regexp-format
  5538. org-todo-regexp)
  5539. '(2 (org-get-todo-face 2) t))
  5540. ;; DONE
  5541. (if org-fontify-done-headline
  5542. (list (format org-heading-keyword-regexp-format
  5543. (concat
  5544. "\\(?:"
  5545. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5546. "\\)"))
  5547. '(2 'org-headline-done t))
  5548. nil)
  5549. ;; Priorities
  5550. '(org-font-lock-add-priority-faces)
  5551. ;; Tags
  5552. '(org-font-lock-add-tag-faces)
  5553. ;; Tags groups
  5554. (when (and org-group-tags org-tag-groups-alist)
  5555. (list (concat org-outline-regexp-bol ".+\\(:"
  5556. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5557. ":\\).*$")
  5558. '(1 'org-tag-group prepend)))
  5559. ;; Special keywords
  5560. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5561. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5562. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5563. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5564. ;; Emphasis
  5565. (when em
  5566. (if (featurep 'xemacs)
  5567. '(org-do-emphasis-faces (0 nil append))
  5568. '(org-do-emphasis-faces)))
  5569. ;; Checkboxes
  5570. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5571. 1 'org-checkbox prepend)
  5572. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5573. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5574. (0 (org-get-checkbox-statistics-face) t)))
  5575. ;; Description list items
  5576. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5577. 1 'org-list-dt prepend)
  5578. ;; ARCHIVEd headings
  5579. (list (concat
  5580. org-outline-regexp-bol
  5581. "\\(.*:" org-archive-tag ":.*\\)")
  5582. '(1 'org-archived prepend))
  5583. ;; Specials
  5584. '(org-do-latex-and-related)
  5585. '(org-fontify-entities)
  5586. '(org-raise-scripts)
  5587. ;; Code
  5588. '(org-activate-code (1 'org-code t))
  5589. ;; COMMENT
  5590. (list (format
  5591. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5592. org-todo-regexp
  5593. org-comment-string)
  5594. '(9 'org-special-keyword t))
  5595. ;; Blocks and meta lines
  5596. '(org-fontify-meta-lines-and-blocks))))
  5597. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5598. (run-hooks 'org-font-lock-set-keywords-hook)
  5599. ;; Now set the full font-lock-keywords
  5600. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5601. (setq-local font-lock-defaults
  5602. '(org-font-lock-keywords t nil nil backward-paragraph))
  5603. (kill-local-variable 'font-lock-keywords) nil))
  5604. (defun org-toggle-pretty-entities ()
  5605. "Toggle the composition display of entities as UTF8 characters."
  5606. (interactive)
  5607. (setq-local org-pretty-entities (not org-pretty-entities))
  5608. (org-restart-font-lock)
  5609. (if org-pretty-entities
  5610. (message "Entities are now displayed as UTF8 characters")
  5611. (save-restriction
  5612. (widen)
  5613. (org-decompose-region (point-min) (point-max))
  5614. (message "Entities are now displayed as plain text"))))
  5615. (defvar-local org-custom-properties-overlays nil
  5616. "List of overlays used for custom properties.")
  5617. (defun org-toggle-custom-properties-visibility ()
  5618. "Display or hide properties in `org-custom-properties'."
  5619. (interactive)
  5620. (if org-custom-properties-overlays
  5621. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5622. (setq org-custom-properties-overlays nil))
  5623. (when org-custom-properties
  5624. (org-with-wide-buffer
  5625. (goto-char (point-min))
  5626. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5627. (while (re-search-forward regexp nil t)
  5628. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5629. (when (and end (< (point) end))
  5630. ;; Hide first custom property in current drawer.
  5631. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5632. (overlay-put o 'invisible t)
  5633. (overlay-put o 'org-custom-property t)
  5634. (push o org-custom-properties-overlays))
  5635. ;; Hide additional custom properties in the same drawer.
  5636. (while (re-search-forward regexp end t)
  5637. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5638. (overlay-put o 'invisible t)
  5639. (overlay-put o 'org-custom-property t)
  5640. (push o org-custom-properties-overlays)))))
  5641. ;; Each entry is limited to a single property drawer.
  5642. (outline-next-heading)))))))
  5643. (defun org-fontify-entities (limit)
  5644. "Find an entity to fontify."
  5645. (let (ee)
  5646. (when org-pretty-entities
  5647. (catch 'match
  5648. ;; "\_ "-family is left out on purpose. Only the first one,
  5649. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5650. ;; confusing when adding a second white space character.
  5651. (while (re-search-forward
  5652. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5653. limit t)
  5654. (when (and (not (org-at-comment-p))
  5655. (setq ee (org-entity-get (match-string 1)))
  5656. (= (length (nth 6 ee)) 1))
  5657. (let* ((end (if (equal (match-string 2) "{}")
  5658. (match-end 2)
  5659. (match-end 1))))
  5660. (add-text-properties
  5661. (match-beginning 0) end
  5662. (list 'font-lock-fontified t))
  5663. (compose-region (match-beginning 0) end
  5664. (nth 6 ee) nil)
  5665. (backward-char 1)
  5666. (throw 'match t))))
  5667. nil))))
  5668. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5669. "Fontify string S like in Org-mode."
  5670. (with-temp-buffer
  5671. (insert s)
  5672. (let ((org-odd-levels-only odd-levels))
  5673. (org-mode)
  5674. (org-font-lock-ensure)
  5675. (buffer-string))))
  5676. (defvar org-m nil)
  5677. (defvar org-l nil)
  5678. (defvar org-f nil)
  5679. (defun org-get-level-face (n)
  5680. "Get the right face for match N in font-lock matching of headlines."
  5681. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5682. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5683. (if org-cycle-level-faces
  5684. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5685. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5686. (cond
  5687. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5688. ((eq n 2) org-f)
  5689. (t (unless org-level-color-stars-only org-f))))
  5690. (defun org-face-from-face-or-color (context inherit face-or-color)
  5691. "Create a face list that inherits INHERIT, but sets the foreground color.
  5692. When FACE-OR-COLOR is not a string, just return it."
  5693. (if (stringp face-or-color)
  5694. (list :inherit inherit
  5695. (cdr (assoc context org-faces-easy-properties))
  5696. face-or-color)
  5697. face-or-color))
  5698. (defun org-get-todo-face (kwd)
  5699. "Get the right face for a TODO keyword KWD.
  5700. If KWD is a number, get the corresponding match group."
  5701. (when (numberp kwd) (setq kwd (match-string kwd)))
  5702. (or (org-face-from-face-or-color
  5703. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5704. (and (member kwd org-done-keywords) 'org-done)
  5705. 'org-todo))
  5706. (defun org-get-priority-face (priority)
  5707. "Get the right face for PRIORITY.
  5708. PRIORITY is a character."
  5709. (or (org-face-from-face-or-color
  5710. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5711. 'org-priority))
  5712. (defun org-get-tag-face (tag)
  5713. "Get the right face for TAG.
  5714. If TAG is a number, get the corresponding match group."
  5715. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5716. (or (org-face-from-face-or-color
  5717. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5718. 'org-tag)))
  5719. (defun org-font-lock-add-priority-faces (limit)
  5720. "Add the special priority faces."
  5721. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5722. (add-text-properties
  5723. (match-beginning 1) (match-end 1)
  5724. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5725. 'font-lock-fontified t))))
  5726. (defun org-font-lock-add-tag-faces (limit)
  5727. "Add the special tag faces."
  5728. (when (and org-tag-faces org-tags-special-faces-re)
  5729. (while (re-search-forward org-tags-special-faces-re limit t)
  5730. (add-text-properties (match-beginning 1) (match-end 1)
  5731. (list 'face (org-get-tag-face 1)
  5732. 'font-lock-fontified t))
  5733. (backward-char 1))))
  5734. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5735. "Remove fontification and activation overlays from links."
  5736. (font-lock-default-unfontify-region beg end)
  5737. (let* ((buffer-undo-list t)
  5738. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5739. (inhibit-modification-hooks t)
  5740. deactivate-mark buffer-file-name buffer-file-truename)
  5741. (org-decompose-region beg end)
  5742. (remove-text-properties beg end
  5743. '(mouse-face t keymap t org-linked-text t
  5744. invisible t intangible t
  5745. org-emphasis t))
  5746. (org-remove-font-lock-display-properties beg end)))
  5747. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5748. ((raise 0.3) (height 0.7))
  5749. ((raise -0.5))
  5750. ((raise 0.5)))
  5751. "Display properties for showing superscripts and subscripts.")
  5752. (defun org-remove-font-lock-display-properties (beg end)
  5753. "Remove specific display properties that have been added by font lock.
  5754. The will remove the raise properties that are used to show superscripts
  5755. and subscripts."
  5756. (let (next prop)
  5757. (while (< beg end)
  5758. (setq next (next-single-property-change beg 'display nil end)
  5759. prop (get-text-property beg 'display))
  5760. (when (member prop org-script-display)
  5761. (put-text-property beg next 'display nil))
  5762. (setq beg next))))
  5763. (defun org-raise-scripts (limit)
  5764. "Add raise properties to sub/superscripts."
  5765. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5766. (re-search-forward
  5767. (if (eq org-use-sub-superscripts t)
  5768. org-match-substring-regexp
  5769. org-match-substring-with-braces-regexp)
  5770. limit t))
  5771. (let* ((pos (point)) table-p comment-p
  5772. (mpos (match-beginning 3))
  5773. (emph-p (get-text-property mpos 'org-emphasis))
  5774. (link-p (get-text-property mpos 'mouse-face))
  5775. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5776. (goto-char (point-at-bol))
  5777. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5778. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5779. (goto-char pos)
  5780. ;; Handle a_b^c
  5781. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5782. (unless (or comment-p emph-p link-p keyw-p)
  5783. (put-text-property (match-beginning 3) (match-end 0)
  5784. 'display
  5785. (if (equal (char-after (match-beginning 2)) ?^)
  5786. (nth (if table-p 3 1) org-script-display)
  5787. (nth (if table-p 2 0) org-script-display)))
  5788. (add-text-properties (match-beginning 2) (match-end 2)
  5789. (list 'invisible t
  5790. 'org-dwidth t 'org-dwidth-n 1))
  5791. (if (and (eq (char-after (match-beginning 3)) ?{)
  5792. (eq (char-before (match-end 3)) ?}))
  5793. (progn
  5794. (add-text-properties
  5795. (match-beginning 3) (1+ (match-beginning 3))
  5796. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5797. (add-text-properties
  5798. (1- (match-end 3)) (match-end 3)
  5799. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5800. t)))
  5801. ;;;; Visibility cycling, including org-goto and indirect buffer
  5802. ;;; Cycling
  5803. (defvar-local org-cycle-global-status nil)
  5804. (put 'org-cycle-global-status 'org-state t)
  5805. (defvar-local org-cycle-subtree-status nil)
  5806. (put 'org-cycle-subtree-status 'org-state t)
  5807. (defvar org-inlinetask-min-level)
  5808. (defun org-unlogged-message (&rest args)
  5809. "Display a message, but avoid logging it in the *Messages* buffer."
  5810. (let ((message-log-max nil))
  5811. (apply 'message args)))
  5812. ;;;###autoload
  5813. (defun org-cycle (&optional arg)
  5814. "TAB-action and visibility cycling for Org-mode.
  5815. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5816. is outline visibility cycling, but it also invokes other actions
  5817. in special contexts.
  5818. - When this function is called with a prefix argument, rotate the entire
  5819. buffer through 3 states (global cycling)
  5820. 1. OVERVIEW: Show only top-level headlines.
  5821. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5822. 3. SHOW ALL: Show everything.
  5823. With a double \\[universal-argument] prefix argument, \
  5824. switch to the startup visibility,
  5825. determined by the variable `org-startup-folded', and by any VISIBILITY
  5826. properties in the buffer.
  5827. With a triple \\[universal-argument] prefix argument, \
  5828. show the entire buffer, including any drawers.
  5829. - When inside a table, re-align the table and move to the next field.
  5830. - When point is at the beginning of a headline, rotate the subtree started
  5831. by this line through 3 different states (local cycling)
  5832. 1. FOLDED: Only the main headline is shown.
  5833. 2. CHILDREN: The main headline and the direct children are shown.
  5834. From this state, you can move to one of the children
  5835. and zoom in further.
  5836. 3. SUBTREE: Show the entire subtree, including body text.
  5837. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5838. - When point is at the beginning of an empty headline and the variable
  5839. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5840. of the headline by demoting and promoting it to likely levels. This
  5841. speeds up creation document structure by pressing TAB once or several
  5842. times right after creating a new headline.
  5843. - When there is a numeric prefix, go up to a heading with level ARG, do
  5844. a `show-subtree' and return to the previous cursor position. If ARG
  5845. is negative, go up that many levels.
  5846. - When point is not at the beginning of a headline, execute the global
  5847. binding for TAB, which is re-indenting the line. See the option
  5848. `org-cycle-emulate-tab' for details.
  5849. - Special case: if point is at the beginning of the buffer and there is
  5850. no headline in line 1, this function will act as if called with prefix arg
  5851. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5852. But only if also the variable `org-cycle-global-at-bob' is t."
  5853. (interactive "P")
  5854. (org-load-modules-maybe)
  5855. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5856. (and org-cycle-level-after-item/entry-creation
  5857. (or (org-cycle-level)
  5858. (org-cycle-item-indentation))))
  5859. (let* ((limit-level
  5860. (or org-cycle-max-level
  5861. (and (boundp 'org-inlinetask-min-level)
  5862. org-inlinetask-min-level
  5863. (1- org-inlinetask-min-level))))
  5864. (nstars (and limit-level
  5865. (if org-odd-levels-only
  5866. (and limit-level (1- (* limit-level 2)))
  5867. limit-level)))
  5868. (org-outline-regexp
  5869. (if (not (derived-mode-p 'org-mode))
  5870. outline-regexp
  5871. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5872. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5873. (not (looking-at org-outline-regexp))))
  5874. (org-cycle-hook
  5875. (if bob-special
  5876. (delq 'org-optimize-window-after-visibility-change
  5877. (copy-sequence org-cycle-hook))
  5878. org-cycle-hook))
  5879. (pos (point)))
  5880. (when (or bob-special (equal arg '(4)))
  5881. ;; special case: use global cycling
  5882. (setq arg t))
  5883. (cond
  5884. ((equal arg '(16))
  5885. (setq last-command 'dummy)
  5886. (org-set-startup-visibility)
  5887. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5888. ((equal arg '(64))
  5889. (outline-show-all)
  5890. (org-unlogged-message "Entire buffer visible, including drawers"))
  5891. ;; Try cdlatex TAB completion
  5892. ((org-try-cdlatex-tab))
  5893. ;; Table: enter it or move to the next field.
  5894. ((org-at-table-p 'any)
  5895. (if (org-at-table.el-p)
  5896. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5897. Use \\[org-edit-special] to edit table.el tables"))
  5898. (if arg (org-table-edit-field t)
  5899. (org-table-justify-field-maybe)
  5900. (call-interactively 'org-table-next-field))))
  5901. ((run-hook-with-args-until-success
  5902. 'org-tab-after-check-for-table-hook))
  5903. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5904. ((eq arg t) (org-cycle-internal-global))
  5905. ;; Drawers: delegate to `org-flag-drawer'.
  5906. ((save-excursion
  5907. (beginning-of-line 1)
  5908. (looking-at org-drawer-regexp))
  5909. (org-flag-drawer ; toggle block visibility
  5910. (not (get-char-property (match-end 0) 'invisible))))
  5911. ;; Show-subtree, ARG levels up from here.
  5912. ((integerp arg)
  5913. (save-excursion
  5914. (org-back-to-heading)
  5915. (outline-up-heading (if (< arg 0) (- arg)
  5916. (- (funcall outline-level) arg)))
  5917. (org-show-subtree)))
  5918. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5919. ((and (featurep 'org-inlinetask)
  5920. (org-inlinetask-at-task-p)
  5921. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5922. (org-inlinetask-toggle-visibility))
  5923. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5924. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5925. (save-excursion (move-beginning-of-line 1)
  5926. (looking-at org-outline-regexp)))
  5927. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5928. (org-cycle-internal-local))
  5929. ;; From there: TAB emulation and template completion.
  5930. (buffer-read-only (org-back-to-heading))
  5931. ((run-hook-with-args-until-success
  5932. 'org-tab-after-check-for-cycling-hook))
  5933. ((org-try-structure-completion))
  5934. ((run-hook-with-args-until-success
  5935. 'org-tab-before-tab-emulation-hook))
  5936. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5937. (or (not (bolp))
  5938. (not (looking-at org-outline-regexp))))
  5939. (call-interactively (global-key-binding "\t")))
  5940. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5941. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5942. (or (and (eq org-cycle-emulate-tab 'white)
  5943. (= (match-end 0) (point-at-eol)))
  5944. (and (eq org-cycle-emulate-tab 'whitestart)
  5945. (>= (match-end 0) pos))))
  5946. t
  5947. (eq org-cycle-emulate-tab t))
  5948. (call-interactively (global-key-binding "\t")))
  5949. (t (save-excursion
  5950. (org-back-to-heading)
  5951. (org-cycle)))))))
  5952. (defun org-cycle-internal-global ()
  5953. "Do the global cycling action."
  5954. ;; Hack to avoid display of messages for .org attachments in Gnus
  5955. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5956. (cond
  5957. ((and (eq last-command this-command)
  5958. (eq org-cycle-global-status 'overview))
  5959. ;; We just created the overview - now do table of contents
  5960. ;; This can be slow in very large buffers, so indicate action
  5961. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5962. (unless ga (org-unlogged-message "CONTENTS..."))
  5963. (org-content)
  5964. (unless ga (org-unlogged-message "CONTENTS...done"))
  5965. (setq org-cycle-global-status 'contents)
  5966. (run-hook-with-args 'org-cycle-hook 'contents))
  5967. ((and (eq last-command this-command)
  5968. (eq org-cycle-global-status 'contents))
  5969. ;; We just showed the table of contents - now show everything
  5970. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5971. (outline-show-all)
  5972. (unless ga (org-unlogged-message "SHOW ALL"))
  5973. (setq org-cycle-global-status 'all)
  5974. (run-hook-with-args 'org-cycle-hook 'all))
  5975. (t
  5976. ;; Default action: go to overview
  5977. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5978. (org-overview)
  5979. (unless ga (org-unlogged-message "OVERVIEW"))
  5980. (setq org-cycle-global-status 'overview)
  5981. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5982. (defvar org-called-with-limited-levels nil
  5983. "Non-nil when `org-with-limited-levels' is currently active.")
  5984. (defun org-cycle-internal-local ()
  5985. "Do the local cycling action."
  5986. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5987. ;; First, determine end of headline (EOH), end of subtree or item
  5988. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5989. (save-excursion
  5990. (if (org-at-item-p)
  5991. (progn
  5992. (beginning-of-line)
  5993. (setq struct (org-list-struct))
  5994. (setq eoh (point-at-eol))
  5995. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5996. (setq has-children (org-list-has-child-p (point) struct)))
  5997. (org-back-to-heading)
  5998. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5999. (setq eos (save-excursion (org-end-of-subtree t t)
  6000. (when (bolp) (backward-char)) (point)))
  6001. (setq has-children
  6002. (or (save-excursion
  6003. (let ((level (funcall outline-level)))
  6004. (outline-next-heading)
  6005. (and (org-at-heading-p t)
  6006. (> (funcall outline-level) level))))
  6007. (save-excursion
  6008. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6009. ;; Determine end invisible part of buffer (EOL)
  6010. (beginning-of-line 2)
  6011. ;; XEmacs doesn't have `next-single-char-property-change'
  6012. (if (featurep 'xemacs)
  6013. (while (and (not (eobp)) ;; this is like `next-line'
  6014. (get-char-property (1- (point)) 'invisible))
  6015. (beginning-of-line 2))
  6016. (while (and (not (eobp)) ;; this is like `next-line'
  6017. (get-char-property (1- (point)) 'invisible))
  6018. (goto-char (next-single-char-property-change (point) 'invisible))
  6019. (and (eolp) (beginning-of-line 2))))
  6020. (setq eol (point)))
  6021. ;; Find out what to do next and set `this-command'
  6022. (cond
  6023. ((= eos eoh)
  6024. ;; Nothing is hidden behind this heading
  6025. (unless (org-before-first-heading-p)
  6026. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6027. (org-unlogged-message "EMPTY ENTRY")
  6028. (setq org-cycle-subtree-status nil)
  6029. (save-excursion
  6030. (goto-char eos)
  6031. (outline-next-heading)
  6032. (when (outline-invisible-p) (org-flag-heading nil))))
  6033. ((and (or (>= eol eos)
  6034. (not (string-match "\\S-" (buffer-substring eol eos))))
  6035. (or has-children
  6036. (not (setq children-skipped
  6037. org-cycle-skip-children-state-if-no-children))))
  6038. ;; Entire subtree is hidden in one line: children view
  6039. (unless (org-before-first-heading-p)
  6040. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6041. (if (org-at-item-p)
  6042. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6043. (org-show-entry)
  6044. (org-with-limited-levels (org-show-children))
  6045. ;; FIXME: This slows down the func way too much.
  6046. ;; How keep drawers hidden in subtree anyway?
  6047. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6048. ;; (org-cycle-hide-drawers 'subtree))
  6049. ;; Fold every list in subtree to top-level items.
  6050. (when (eq org-cycle-include-plain-lists 'integrate)
  6051. (save-excursion
  6052. (org-back-to-heading)
  6053. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6054. (beginning-of-line 1)
  6055. (let* ((struct (org-list-struct))
  6056. (prevs (org-list-prevs-alist struct))
  6057. (end (org-list-get-bottom-point struct)))
  6058. (dolist (e (org-list-get-all-items (point) struct prevs))
  6059. (org-list-set-item-visibility e struct 'folded))
  6060. (goto-char (if (< end eos) end eos)))))))
  6061. (org-unlogged-message "CHILDREN")
  6062. (save-excursion
  6063. (goto-char eos)
  6064. (outline-next-heading)
  6065. (when (outline-invisible-p) (org-flag-heading nil)))
  6066. (setq org-cycle-subtree-status 'children)
  6067. (unless (org-before-first-heading-p)
  6068. (run-hook-with-args 'org-cycle-hook 'children)))
  6069. ((or children-skipped
  6070. (and (eq last-command this-command)
  6071. (eq org-cycle-subtree-status 'children)))
  6072. ;; We just showed the children, or no children are there,
  6073. ;; now show everything.
  6074. (unless (org-before-first-heading-p)
  6075. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6076. (outline-flag-region eoh eos nil)
  6077. (org-unlogged-message
  6078. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6079. (setq org-cycle-subtree-status 'subtree)
  6080. (unless (org-before-first-heading-p)
  6081. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6082. (t
  6083. ;; Default action: hide the subtree.
  6084. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6085. (outline-flag-region eoh eos t)
  6086. (org-unlogged-message "FOLDED")
  6087. (setq org-cycle-subtree-status 'folded)
  6088. (unless (org-before-first-heading-p)
  6089. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6090. ;;;###autoload
  6091. (defun org-global-cycle (&optional arg)
  6092. "Cycle the global visibility. For details see `org-cycle'.
  6093. With \\[universal-argument] prefix arg, switch to startup visibility.
  6094. With a numeric prefix, show all headlines up to that level."
  6095. (interactive "P")
  6096. (let ((org-cycle-include-plain-lists
  6097. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6098. (cond
  6099. ((integerp arg)
  6100. (outline-show-all)
  6101. (outline-hide-sublevels arg)
  6102. (setq org-cycle-global-status 'contents))
  6103. ((equal arg '(4))
  6104. (org-set-startup-visibility)
  6105. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6106. (t
  6107. (org-cycle '(4))))))
  6108. (defun org-set-startup-visibility ()
  6109. "Set the visibility required by startup options and properties."
  6110. (cond
  6111. ((eq org-startup-folded t)
  6112. (org-overview))
  6113. ((eq org-startup-folded 'content)
  6114. (org-content))
  6115. ((or (eq org-startup-folded 'showeverything)
  6116. (eq org-startup-folded nil))
  6117. (outline-show-all)))
  6118. (unless (eq org-startup-folded 'showeverything)
  6119. (when org-hide-block-startup (org-hide-block-all))
  6120. (org-set-visibility-according-to-property 'no-cleanup)
  6121. (org-cycle-hide-archived-subtrees 'all)
  6122. (org-cycle-hide-drawers 'all)
  6123. (org-cycle-show-empty-lines t)))
  6124. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6125. "Switch subtree visibilities according to :VISIBILITY: property."
  6126. (interactive)
  6127. (org-with-wide-buffer
  6128. (goto-char (point-min))
  6129. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6130. (if (not (org-at-property-p)) (outline-next-heading)
  6131. (let ((state (match-string 3)))
  6132. (save-excursion
  6133. (org-back-to-heading t)
  6134. (outline-hide-subtree)
  6135. (org-reveal)
  6136. (cond
  6137. ((equal state "folded")
  6138. (outline-hide-subtree))
  6139. ((equal state "children")
  6140. (org-show-hidden-entry)
  6141. (org-show-children))
  6142. ((equal state "content")
  6143. (save-excursion
  6144. (save-restriction
  6145. (org-narrow-to-subtree)
  6146. (org-content))))
  6147. ((member state '("all" "showall"))
  6148. (outline-show-subtree)))))))
  6149. (unless no-cleanup
  6150. (org-cycle-hide-archived-subtrees 'all)
  6151. (org-cycle-hide-drawers 'all)
  6152. (org-cycle-show-empty-lines 'all))))
  6153. ;; This function uses outline-regexp instead of the more fundamental
  6154. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6155. ;; buffers, where outline-regexp is needed.
  6156. (defun org-overview ()
  6157. "Switch to overview mode, showing only top-level headlines.
  6158. This shows all headlines with a level equal or greater than the level
  6159. of the first headline in the buffer. This is important, because if the
  6160. first headline is not level one, then (hide-sublevels 1) gives confusing
  6161. results."
  6162. (interactive)
  6163. (save-excursion
  6164. (let ((level
  6165. (save-excursion
  6166. (goto-char (point-min))
  6167. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6168. (goto-char (match-beginning 0))
  6169. (funcall outline-level)))))
  6170. (and level (outline-hide-sublevels level)))))
  6171. (defun org-content (&optional arg)
  6172. "Show all headlines in the buffer, like a table of contents.
  6173. With numerical argument N, show content up to level N."
  6174. (interactive "P")
  6175. (org-overview)
  6176. (save-excursion
  6177. ;; Visit all headings and show their offspring
  6178. (and (integerp arg) (org-overview))
  6179. (goto-char (point-max))
  6180. (catch 'exit
  6181. (while (and (progn (condition-case nil
  6182. (outline-previous-visible-heading 1)
  6183. (error (goto-char (point-min))))
  6184. t)
  6185. (looking-at org-outline-regexp))
  6186. (if (integerp arg)
  6187. (org-show-children (1- arg))
  6188. (outline-show-branches))
  6189. (when (bobp) (throw 'exit nil))))))
  6190. (defun org-optimize-window-after-visibility-change (state)
  6191. "Adjust the window after a change in outline visibility.
  6192. This function is the default value of the hook `org-cycle-hook'."
  6193. (when (get-buffer-window (current-buffer))
  6194. (cond
  6195. ((eq state 'content) nil)
  6196. ((eq state 'all) nil)
  6197. ((eq state 'folded) nil)
  6198. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6199. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6200. (defun org-remove-empty-overlays-at (pos)
  6201. "Remove outline overlays that do not contain non-white stuff."
  6202. (dolist (o (overlays-at pos))
  6203. (and (eq 'outline (overlay-get o 'invisible))
  6204. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6205. (overlay-end o))))
  6206. (delete-overlay o))))
  6207. (defun org-clean-visibility-after-subtree-move ()
  6208. "Fix visibility issues after moving a subtree."
  6209. ;; First, find a reasonable region to look at:
  6210. ;; Start two siblings above, end three below
  6211. (let* ((beg (save-excursion
  6212. (and (org-get-last-sibling)
  6213. (org-get-last-sibling))
  6214. (point)))
  6215. (end (save-excursion
  6216. (and (org-get-next-sibling)
  6217. (org-get-next-sibling)
  6218. (org-get-next-sibling))
  6219. (if (org-at-heading-p)
  6220. (point-at-eol)
  6221. (point))))
  6222. (level (looking-at "\\*+"))
  6223. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6224. (save-excursion
  6225. (save-restriction
  6226. (narrow-to-region beg end)
  6227. (when re
  6228. ;; Properly fold already folded siblings
  6229. (goto-char (point-min))
  6230. (while (re-search-forward re nil t)
  6231. (when (and (not (outline-invisible-p))
  6232. (save-excursion
  6233. (goto-char (point-at-eol)) (outline-invisible-p)))
  6234. (outline-hide-entry))))
  6235. (org-cycle-show-empty-lines 'overview)
  6236. (org-cycle-hide-drawers 'overview)))))
  6237. (defun org-cycle-show-empty-lines (state)
  6238. "Show empty lines above all visible headlines.
  6239. The region to be covered depends on STATE when called through
  6240. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6241. entire buffer covered. Note that an empty line is only shown if there
  6242. are at least `org-cycle-separator-lines' empty lines before the headline."
  6243. (when (/= org-cycle-separator-lines 0)
  6244. (save-excursion
  6245. (let* ((n (abs org-cycle-separator-lines))
  6246. (re (cond
  6247. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6248. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6249. (t (let ((ns (number-to-string (- n 2))))
  6250. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6251. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6252. beg end)
  6253. (cond
  6254. ((memq state '(overview contents t))
  6255. (setq beg (point-min) end (point-max)))
  6256. ((memq state '(children folded))
  6257. (setq beg (point)
  6258. end (progn (org-end-of-subtree t t)
  6259. (line-beginning-position 2)))))
  6260. (when beg
  6261. (goto-char beg)
  6262. (while (re-search-forward re end t)
  6263. (unless (get-char-property (match-end 1) 'invisible)
  6264. (let ((e (match-end 1))
  6265. (b (if (>= org-cycle-separator-lines 0)
  6266. (match-beginning 1)
  6267. (save-excursion
  6268. (goto-char (match-beginning 0))
  6269. (skip-chars-backward " \t\n")
  6270. (line-end-position)))))
  6271. (outline-flag-region b e nil))))))))
  6272. ;; Never hide empty lines at the end of the file.
  6273. (save-excursion
  6274. (goto-char (point-max))
  6275. (outline-previous-heading)
  6276. (outline-end-of-heading)
  6277. (when (and (looking-at "[ \t\n]+")
  6278. (= (match-end 0) (point-max)))
  6279. (outline-flag-region (point) (match-end 0) nil))))
  6280. (defun org-show-empty-lines-in-parent ()
  6281. "Move to the parent and re-show empty lines before visible headlines."
  6282. (save-excursion
  6283. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6284. (org-cycle-show-empty-lines context))))
  6285. (defun org-files-list ()
  6286. "Return `org-agenda-files' list, plus all open org-mode files.
  6287. This is useful for operations that need to scan all of a user's
  6288. open and agenda-wise Org files."
  6289. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6290. (dolist (buf (buffer-list))
  6291. (with-current-buffer buf
  6292. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6293. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6294. files))
  6295. (defsubst org-entry-beginning-position ()
  6296. "Return the beginning position of the current entry."
  6297. (save-excursion (org-back-to-heading t) (point)))
  6298. (defsubst org-entry-end-position ()
  6299. "Return the end position of the current entry."
  6300. (save-excursion (outline-next-heading) (point)))
  6301. (defun org-cycle-hide-drawers (state &optional exceptions)
  6302. "Re-hide all drawers after a visibility state change.
  6303. When non-nil, optional argument EXCEPTIONS is a list of strings
  6304. specifying which drawers should not be hidden."
  6305. (when (and (derived-mode-p 'org-mode)
  6306. (not (memq state '(overview folded contents))))
  6307. (save-excursion
  6308. (let* ((globalp (memq state '(contents all)))
  6309. (beg (if globalp (point-min) (point)))
  6310. (end (if globalp (point-max)
  6311. (if (eq state 'children)
  6312. (save-excursion (outline-next-heading) (point))
  6313. (org-end-of-subtree t)))))
  6314. (goto-char beg)
  6315. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6316. (unless (member-ignore-case (match-string 1) exceptions)
  6317. (let ((drawer (org-element-at-point)))
  6318. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6319. (org-flag-drawer t drawer)
  6320. ;; Make sure to skip drawer entirely or we might flag
  6321. ;; it another time when matching its ending line with
  6322. ;; `org-drawer-regexp'.
  6323. (goto-char (org-element-property :end drawer))))))))))
  6324. (defun org-flag-drawer (flag &optional element)
  6325. "When FLAG is non-nil, hide the drawer we are at.
  6326. Otherwise make it visible. When optional argument ELEMENT is
  6327. a parsed drawer, as returned by `org-element-at-point', hide or
  6328. show that drawer instead."
  6329. (when (save-excursion
  6330. (beginning-of-line)
  6331. (org-looking-at-p org-drawer-regexp))
  6332. (let ((drawer (or element (org-element-at-point))))
  6333. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6334. (let ((post (org-element-property :post-affiliated drawer)))
  6335. (save-excursion
  6336. (outline-flag-region
  6337. (progn (goto-char post) (line-end-position))
  6338. (progn (goto-char (org-element-property :end drawer))
  6339. (skip-chars-backward " \r\t\n")
  6340. (line-end-position))
  6341. flag))
  6342. ;; When the drawer is hidden away, make sure point lies in
  6343. ;; a visible part of the buffer.
  6344. (when (and flag (> (line-beginning-position) post))
  6345. (goto-char post)))))))
  6346. (defun org-subtree-end-visible-p ()
  6347. "Is the end of the current subtree visible?"
  6348. (pos-visible-in-window-p
  6349. (save-excursion (org-end-of-subtree t) (point))))
  6350. (defun org-first-headline-recenter ()
  6351. "Move cursor to the first headline and recenter the headline."
  6352. (goto-char (point-min))
  6353. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6354. (set-window-start (selected-window) (point-at-bol))))
  6355. ;;; Saving and restoring visibility
  6356. (defun org-outline-overlay-data (&optional use-markers)
  6357. "Return a list of the locations of all outline overlays.
  6358. These are overlays with the `invisible' property value `outline'.
  6359. The return value is a list of cons cells, with start and stop
  6360. positions for each overlay.
  6361. If USE-MARKERS is set, return the positions as markers."
  6362. (let (beg end)
  6363. (save-excursion
  6364. (save-restriction
  6365. (widen)
  6366. (delq nil
  6367. (mapcar (lambda (o)
  6368. (when (eq (overlay-get o 'invisible) 'outline)
  6369. (setq beg (overlay-start o)
  6370. end (overlay-end o))
  6371. (and beg end (> end beg)
  6372. (if use-markers
  6373. (cons (copy-marker beg)
  6374. (copy-marker end t))
  6375. (cons beg end)))))
  6376. (overlays-in (point-min) (point-max))))))))
  6377. (defun org-set-outline-overlay-data (data)
  6378. "Create visibility overlays for all positions in DATA.
  6379. DATA should have been made by `org-outline-overlay-data'."
  6380. (org-with-wide-buffer
  6381. (outline-show-all)
  6382. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6383. ;;; Folding of blocks
  6384. (defvar-local org-hide-block-overlays nil
  6385. "Overlays hiding blocks.")
  6386. (defun org-block-map (function &optional start end)
  6387. "Call FUNCTION at the head of all source blocks in the current buffer.
  6388. Optional arguments START and END can be used to limit the range."
  6389. (let ((start (or start (point-min)))
  6390. (end (or end (point-max))))
  6391. (save-excursion
  6392. (goto-char start)
  6393. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6394. (save-excursion
  6395. (save-match-data
  6396. (goto-char (match-beginning 0))
  6397. (funcall function)))))))
  6398. (defun org-hide-block-toggle-all ()
  6399. "Toggle the visibility of all blocks in the current buffer."
  6400. (org-block-map 'org-hide-block-toggle))
  6401. (defun org-hide-block-all ()
  6402. "Fold all blocks in the current buffer."
  6403. (interactive)
  6404. (org-show-block-all)
  6405. (org-block-map 'org-hide-block-toggle-maybe))
  6406. (defun org-show-block-all ()
  6407. "Unfold all blocks in the current buffer."
  6408. (interactive)
  6409. (mapc #'delete-overlay org-hide-block-overlays)
  6410. (setq org-hide-block-overlays nil))
  6411. (defun org-hide-block-toggle-maybe ()
  6412. "Toggle visibility of block at point.
  6413. Unlike to `org-hide-block-toggle', this function does not throw
  6414. an error. Return a non-nil value when toggling is successful."
  6415. (interactive)
  6416. (ignore-errors (org-hide-block-toggle)))
  6417. (defun org-hide-block-toggle (&optional force)
  6418. "Toggle the visibility of the current block.
  6419. When optional argument FORCE is `off', make block visible. If it
  6420. is non-nil, hide it unconditionally. Throw an error when not at
  6421. a block. Return a non-nil value when toggling is successful."
  6422. (interactive)
  6423. (let ((element (org-element-at-point)))
  6424. (unless (memq (org-element-type element)
  6425. '(center-block comment-block dynamic-block example-block
  6426. export-block quote-block special-block
  6427. src-block verse-block))
  6428. (user-error "Not at a block"))
  6429. (let* ((start (save-excursion
  6430. (goto-char (org-element-property :post-affiliated element))
  6431. (line-end-position)))
  6432. (end (save-excursion
  6433. (goto-char (org-element-property :end element))
  6434. (skip-chars-backward " \r\t\n")
  6435. (line-end-position)))
  6436. (overlays (overlays-at start)))
  6437. (cond
  6438. ;; Do nothing when not before or at the block opening line or
  6439. ;; at the block closing line.
  6440. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6441. ((and (not (eq force 'off))
  6442. (not (memq t (mapcar
  6443. (lambda (o)
  6444. (eq (overlay-get o 'invisible) 'org-hide-block))
  6445. overlays))))
  6446. (let ((ov (make-overlay start end)))
  6447. (overlay-put ov 'invisible 'org-hide-block)
  6448. ;; Make the block accessible to `isearch'.
  6449. (overlay-put
  6450. ov 'isearch-open-invisible
  6451. (lambda (ov)
  6452. (when (memq ov org-hide-block-overlays)
  6453. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6454. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6455. (delete-overlay ov))))
  6456. (push ov org-hide-block-overlays)
  6457. ;; When the block is hidden away, make sure point is left in
  6458. ;; a visible part of the buffer.
  6459. (when (> (line-beginning-position) start)
  6460. (goto-char start)
  6461. (beginning-of-line))
  6462. ;; Signal successful toggling.
  6463. t))
  6464. ((or (not force) (eq force 'off))
  6465. (dolist (ov overlays t)
  6466. (when (memq ov org-hide-block-overlays)
  6467. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6468. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6469. (delete-overlay ov))))))))
  6470. ;; org-tab-after-check-for-cycling-hook
  6471. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6472. ;; Remove overlays when changing major mode
  6473. (add-hook 'org-mode-hook
  6474. (lambda () (org-add-hook 'change-major-mode-hook
  6475. 'org-show-block-all 'append 'local)))
  6476. ;;; Org-goto
  6477. (defvar org-goto-window-configuration nil)
  6478. (defvar org-goto-marker nil)
  6479. (defvar org-goto-map)
  6480. (defun org-goto-map ()
  6481. "Set the keymap `org-goto'."
  6482. (setq org-goto-map
  6483. (let ((map (make-sparse-keymap)))
  6484. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6485. mouse-drag-region universal-argument org-occur)))
  6486. (dolist (cmd cmds)
  6487. (substitute-key-definition cmd cmd map global-map)))
  6488. (suppress-keymap map)
  6489. (org-defkey map "\C-m" 'org-goto-ret)
  6490. (org-defkey map [(return)] 'org-goto-ret)
  6491. (org-defkey map [(left)] 'org-goto-left)
  6492. (org-defkey map [(right)] 'org-goto-right)
  6493. (org-defkey map [(control ?g)] 'org-goto-quit)
  6494. (org-defkey map "\C-i" 'org-cycle)
  6495. (org-defkey map [(tab)] 'org-cycle)
  6496. (org-defkey map [(down)] 'outline-next-visible-heading)
  6497. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6498. (if org-goto-auto-isearch
  6499. (if (fboundp 'define-key-after)
  6500. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6501. nil)
  6502. (org-defkey map "q" 'org-goto-quit)
  6503. (org-defkey map "n" 'outline-next-visible-heading)
  6504. (org-defkey map "p" 'outline-previous-visible-heading)
  6505. (org-defkey map "f" 'outline-forward-same-level)
  6506. (org-defkey map "b" 'outline-backward-same-level)
  6507. (org-defkey map "u" 'outline-up-heading))
  6508. (org-defkey map "/" 'org-occur)
  6509. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6510. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6511. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6512. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6513. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6514. map)))
  6515. (defconst org-goto-help
  6516. "Browse buffer copy, to find location or copy text.%s
  6517. RET=jump to location C-g=quit and return to previous location
  6518. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6519. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6520. (defun org-goto (&optional alternative-interface)
  6521. "Look up a different location in the current file, keeping current visibility.
  6522. When you want look-up or go to a different location in a
  6523. document, the fastest way is often to fold the entire buffer and
  6524. then dive into the tree. This method has the disadvantage, that
  6525. the previous location will be folded, which may not be what you
  6526. want.
  6527. This command works around this by showing a copy of the current
  6528. buffer in an indirect buffer, in overview mode. You can dive
  6529. into the tree in that copy, use org-occur and incremental search
  6530. to find a location. When pressing RET or `Q', the command
  6531. returns to the original buffer in which the visibility is still
  6532. unchanged. After RET it will also jump to the location selected
  6533. in the indirect buffer and expose the headline hierarchy above.
  6534. With a prefix argument, use the alternative interface: e.g., if
  6535. `org-goto-interface' is `outline' use `outline-path-completion'."
  6536. (interactive "P")
  6537. (org-goto-map)
  6538. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6539. (org-refile-use-outline-path t)
  6540. (org-refile-target-verify-function nil)
  6541. (interface
  6542. (if (not alternative-interface)
  6543. org-goto-interface
  6544. (if (eq org-goto-interface 'outline)
  6545. 'outline-path-completion
  6546. 'outline)))
  6547. (org-goto-start-pos (point))
  6548. (selected-point
  6549. (if (eq interface 'outline)
  6550. (car (org-get-location (current-buffer) org-goto-help))
  6551. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6552. (org-refile-check-position pa)
  6553. (nth 3 pa)))))
  6554. (if selected-point
  6555. (progn
  6556. (org-mark-ring-push org-goto-start-pos)
  6557. (goto-char selected-point)
  6558. (when (or (outline-invisible-p) (org-invisible-p2))
  6559. (org-show-context 'org-goto)))
  6560. (message "Quit"))))
  6561. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6562. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6563. (defvar org-goto-local-auto-isearch-map) ; defined below
  6564. (defun org-get-location (_buf help)
  6565. "Let the user select a location in current buffer.
  6566. This function uses a recursive edit. It returns the selected position
  6567. or nil."
  6568. (org-no-popups
  6569. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6570. (isearch-hide-immediately nil)
  6571. (isearch-search-fun-function
  6572. (lambda () 'org-goto-local-search-headings))
  6573. (org-goto-selected-point org-goto-exit-command))
  6574. (save-excursion
  6575. (save-window-excursion
  6576. (delete-other-windows)
  6577. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6578. (org-pop-to-buffer-same-window
  6579. (condition-case nil
  6580. (make-indirect-buffer (current-buffer) "*org-goto*")
  6581. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6582. (with-output-to-temp-buffer "*Org Help*"
  6583. (princ (format help (if org-goto-auto-isearch
  6584. " Just type for auto-isearch."
  6585. " n/p/f/b/u to navigate, q to quit."))))
  6586. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6587. (setq buffer-read-only nil)
  6588. (let ((org-startup-truncated t)
  6589. (org-startup-folded nil)
  6590. (org-startup-align-all-tables nil))
  6591. (org-mode)
  6592. (org-overview))
  6593. (setq buffer-read-only t)
  6594. (if (and (boundp 'org-goto-start-pos)
  6595. (integer-or-marker-p org-goto-start-pos))
  6596. (progn (goto-char org-goto-start-pos)
  6597. (when (outline-invisible-p)
  6598. (org-show-set-visibility 'lineage)))
  6599. (goto-char (point-min)))
  6600. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6601. (message "Select location and press RET")
  6602. (use-local-map org-goto-map)
  6603. (recursive-edit)))
  6604. (kill-buffer "*org-goto*")
  6605. (cons org-goto-selected-point org-goto-exit-command))))
  6606. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6607. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6608. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6609. (if (fboundp 'isearch-other-control-char)
  6610. (progn
  6611. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6612. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6613. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6614. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6615. (define-key org-goto-local-auto-isearch-map [return] nil))
  6616. (defun org-goto-local-search-headings (string bound noerror)
  6617. "Search and make sure that any matches are in headlines."
  6618. (catch 'return
  6619. (while (if isearch-forward
  6620. (search-forward string bound noerror)
  6621. (search-backward string bound noerror))
  6622. (when (save-match-data
  6623. (and (save-excursion
  6624. (beginning-of-line)
  6625. (looking-at org-complex-heading-regexp))
  6626. (or (not (match-beginning 5))
  6627. (< (point) (match-beginning 5)))))
  6628. (throw 'return (point))))))
  6629. (defun org-goto-local-auto-isearch ()
  6630. "Start isearch."
  6631. (interactive)
  6632. (goto-char (point-min))
  6633. (let ((keys (this-command-keys)))
  6634. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6635. (isearch-mode t)
  6636. (isearch-process-search-char (string-to-char keys)))))
  6637. (defun org-goto-ret (&optional _arg)
  6638. "Finish `org-goto' by going to the new location."
  6639. (interactive "P")
  6640. (setq org-goto-selected-point (point))
  6641. (setq org-goto-exit-command 'return)
  6642. (throw 'exit nil))
  6643. (defun org-goto-left ()
  6644. "Finish `org-goto' by going to the new location."
  6645. (interactive)
  6646. (if (org-at-heading-p)
  6647. (progn
  6648. (beginning-of-line 1)
  6649. (setq org-goto-selected-point (point)
  6650. org-goto-exit-command 'left)
  6651. (throw 'exit nil))
  6652. (user-error "Not on a heading")))
  6653. (defun org-goto-right ()
  6654. "Finish `org-goto' by going to the new location."
  6655. (interactive)
  6656. (if (org-at-heading-p)
  6657. (progn
  6658. (setq org-goto-selected-point (point)
  6659. org-goto-exit-command 'right)
  6660. (throw 'exit nil))
  6661. (user-error "Not on a heading")))
  6662. (defun org-goto-quit ()
  6663. "Finish `org-goto' without cursor motion."
  6664. (interactive)
  6665. (setq org-goto-selected-point nil)
  6666. (setq org-goto-exit-command 'quit)
  6667. (throw 'exit nil))
  6668. ;;; Indirect buffer display of subtrees
  6669. (defvar org-indirect-dedicated-frame nil
  6670. "This is the frame being used for indirect tree display.")
  6671. (defvar org-last-indirect-buffer nil)
  6672. (defun org-tree-to-indirect-buffer (&optional arg)
  6673. "Create indirect buffer and narrow it to current subtree.
  6674. With a numerical prefix ARG, go up to this level and then take that tree.
  6675. If ARG is negative, go up that many levels.
  6676. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6677. indirect buffer previously made with this command, to avoid proliferation of
  6678. indirect buffers. However, when you call the command with a \
  6679. \\[universal-argument] prefix, or
  6680. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6681. is kept so that you can work with several indirect buffers at the same time.
  6682. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6683. \\[universal-argument] prefix also
  6684. requests that a new frame be made for the new buffer, so that the dedicated
  6685. frame is not changed."
  6686. (interactive "P")
  6687. (let ((cbuf (current-buffer))
  6688. (cwin (selected-window))
  6689. (pos (point))
  6690. beg end level heading ibuf)
  6691. (save-excursion
  6692. (org-back-to-heading t)
  6693. (when (numberp arg)
  6694. (setq level (org-outline-level))
  6695. (when (< arg 0) (setq arg (+ level arg)))
  6696. (while (> (setq level (org-outline-level)) arg)
  6697. (org-up-heading-safe)))
  6698. (setq beg (point)
  6699. heading (org-get-heading 'no-tags))
  6700. (org-end-of-subtree t t)
  6701. (when (org-at-heading-p) (backward-char 1))
  6702. (setq end (point)))
  6703. (when (and (buffer-live-p org-last-indirect-buffer)
  6704. (not (eq org-indirect-buffer-display 'new-frame))
  6705. (not arg))
  6706. (kill-buffer org-last-indirect-buffer))
  6707. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6708. org-last-indirect-buffer ibuf)
  6709. (cond
  6710. ((or (eq org-indirect-buffer-display 'new-frame)
  6711. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6712. (select-frame (make-frame))
  6713. (delete-other-windows)
  6714. (org-pop-to-buffer-same-window ibuf)
  6715. (org-set-frame-title heading))
  6716. ((eq org-indirect-buffer-display 'dedicated-frame)
  6717. (raise-frame
  6718. (select-frame (or (and org-indirect-dedicated-frame
  6719. (frame-live-p org-indirect-dedicated-frame)
  6720. org-indirect-dedicated-frame)
  6721. (setq org-indirect-dedicated-frame (make-frame)))))
  6722. (delete-other-windows)
  6723. (org-pop-to-buffer-same-window ibuf)
  6724. (org-set-frame-title (concat "Indirect: " heading)))
  6725. ((eq org-indirect-buffer-display 'current-window)
  6726. (org-pop-to-buffer-same-window ibuf))
  6727. ((eq org-indirect-buffer-display 'other-window)
  6728. (pop-to-buffer ibuf))
  6729. (t (error "Invalid value")))
  6730. (when (featurep 'xemacs)
  6731. (save-excursion (org-mode) (turn-on-font-lock)))
  6732. (narrow-to-region beg end)
  6733. (outline-show-all)
  6734. (goto-char pos)
  6735. (run-hook-with-args 'org-cycle-hook 'all)
  6736. (and (window-live-p cwin) (select-window cwin))))
  6737. (defun org-get-indirect-buffer (&optional buffer heading)
  6738. (setq buffer (or buffer (current-buffer)))
  6739. (let ((n 1) (base (buffer-name buffer)) bname)
  6740. (while (buffer-live-p
  6741. (get-buffer
  6742. (setq bname
  6743. (concat base "-"
  6744. (if heading (concat heading "-" (number-to-string n))
  6745. (number-to-string n))))))
  6746. (setq n (1+ n)))
  6747. (condition-case nil
  6748. (make-indirect-buffer buffer bname 'clone)
  6749. (error (make-indirect-buffer buffer bname)))))
  6750. (defun org-set-frame-title (title)
  6751. "Set the title of the current frame to the string TITLE."
  6752. ;; FIXME: how to name a single frame in XEmacs???
  6753. (unless (featurep 'xemacs)
  6754. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6755. ;;;; Structure editing
  6756. ;;; Inserting headlines
  6757. (defun org--line-empty-p (n)
  6758. "Is the Nth next line empty?
  6759. Counts the current line as N = 1 and the previous line as N = 0;
  6760. see `beginning-of-line'."
  6761. (save-excursion
  6762. (and (not (bobp))
  6763. (or (beginning-of-line n) t)
  6764. (save-match-data
  6765. (looking-at "[ \t]*$")))))
  6766. (defun org-previous-line-empty-p ()
  6767. "Is the previous line a blank line?
  6768. When NEXT is non-nil, check the next line instead."
  6769. (org--line-empty-p 0))
  6770. (defun org-next-line-empty-p ()
  6771. "Is the previous line a blank line?
  6772. When NEXT is non-nil, check the next line instead."
  6773. (org--line-empty-p 2))
  6774. (defun org-insert-heading (&optional arg invisible-ok top)
  6775. "Insert a new heading or an item with the same depth at point.
  6776. If point is at the beginning of a heading or a list item, insert
  6777. a new heading or a new item above the current one. If point is
  6778. at the beginning of a normal line, turn the line into a heading.
  6779. If point is in the middle of a headline or a list item, split the
  6780. headline or the item and create a new headline/item with the text
  6781. in the current line after point \(see `org-M-RET-may-split-line'
  6782. on how to modify this behavior).
  6783. With one universal prefix argument, set the user option
  6784. `org-insert-heading-respect-content' to t for the duration of
  6785. the command. This modifies the behavior described above in this
  6786. ways: on list items and at the beginning of normal lines, force
  6787. the insertion of a heading after the current subtree.
  6788. With two universal prefix arguments, insert the heading at the
  6789. end of the grandparent subtree. For example, if point is within
  6790. a 2nd-level heading, then it will insert a 2nd-level heading at
  6791. the end of the 1st-level parent heading.
  6792. If point is at the beginning of a headline, insert a sibling
  6793. before the current headline. If point is not at the beginning,
  6794. split the line and create a new headline with the text in the
  6795. current line after point \(see `org-M-RET-may-split-line' on how
  6796. to modify this behavior).
  6797. If point is at the beginning of a normal line, turn this line
  6798. into a heading.
  6799. When INVISIBLE-OK is set, stop at invisible headlines when going
  6800. back. This is important for non-interactive uses of the
  6801. command.
  6802. When optional argument TOP is non-nil, insert a level 1 heading,
  6803. unconditionally."
  6804. (interactive "P")
  6805. (when (org-called-interactively-p 'any) (org-reveal))
  6806. (let ((itemp (and (not top) (org-in-item-p)))
  6807. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6808. (respect-content (or org-insert-heading-respect-content
  6809. (equal arg '(4))))
  6810. (initial-content ""))
  6811. (cond
  6812. ((or (= (buffer-size) 0)
  6813. (and (not (save-excursion
  6814. (and (ignore-errors (org-back-to-heading invisible-ok))
  6815. (org-at-heading-p))))
  6816. (or arg (not itemp))))
  6817. ;; At beginning of buffer or so high up that only a heading
  6818. ;; makes sense.
  6819. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6820. ((not respect-content)
  6821. (unless may-split (end-of-line))
  6822. (insert "\n* "))
  6823. ((re-search-forward org-outline-regexp-bol nil t)
  6824. (beginning-of-line)
  6825. (insert "* \n")
  6826. (backward-char))
  6827. (t (goto-char (point-max))
  6828. (insert "\n* ")))
  6829. (run-hooks 'org-insert-heading-hook))
  6830. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6831. (t
  6832. ;; Maybe move at the end of the subtree
  6833. (when (equal arg '(16))
  6834. (org-up-heading-safe)
  6835. (org-end-of-subtree t))
  6836. ;; Insert a heading
  6837. (save-restriction
  6838. (widen)
  6839. (let* ((level nil)
  6840. (on-heading (org-at-heading-p))
  6841. (empty-line-p (if on-heading
  6842. (org-previous-line-empty-p)
  6843. ;; We will decide later
  6844. nil))
  6845. ;; Get a level string to fall back on.
  6846. (fix-level
  6847. (if (org-before-first-heading-p) "*"
  6848. (save-excursion
  6849. (org-back-to-heading t)
  6850. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6851. (looking-at org-outline-regexp)
  6852. (make-string (1- (length (match-string 0))) ?*))))
  6853. (stars
  6854. (save-excursion
  6855. (condition-case nil
  6856. (if top "* "
  6857. (org-back-to-heading invisible-ok)
  6858. (when (and (not on-heading)
  6859. (featurep 'org-inlinetask)
  6860. (integerp org-inlinetask-min-level)
  6861. (>= (length (match-string 0))
  6862. org-inlinetask-min-level))
  6863. ;; Find a heading level before the inline
  6864. ;; task.
  6865. (while (and (setq level (org-up-heading-safe))
  6866. (>= level org-inlinetask-min-level)))
  6867. (if (org-at-heading-p)
  6868. (org-back-to-heading invisible-ok)
  6869. (error "This should not happen")))
  6870. (unless (and (save-excursion
  6871. (save-match-data
  6872. (org-backward-heading-same-level
  6873. 1 invisible-ok))
  6874. (= (point) (match-beginning 0)))
  6875. (not (org-next-line-empty-p)))
  6876. (setq empty-line-p (or empty-line-p
  6877. (org-previous-line-empty-p))))
  6878. (match-string 0))
  6879. (error (or fix-level "* ")))))
  6880. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6881. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  6882. ;; If we insert after content, move there and clean up
  6883. ;; whitespace.
  6884. (when (and respect-content
  6885. (not (org-looking-at-p org-outline-regexp-bol)))
  6886. (if (not (org-before-first-heading-p))
  6887. (org-end-of-subtree nil t)
  6888. (re-search-forward org-outline-regexp-bol)
  6889. (beginning-of-line 0))
  6890. (skip-chars-backward " \r\t\n")
  6891. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6892. (looking-at "[ \t]+") (replace-match ""))
  6893. (unless (eobp) (forward-char 1))
  6894. (when (looking-at "^\\*")
  6895. (unless (bobp) (backward-char 1))
  6896. (insert "\n")))
  6897. ;; If we are splitting, grab the text that should be moved
  6898. ;; to the new headline.
  6899. (when may-split
  6900. (if (org-at-heading-p)
  6901. ;; This is a heading: split intelligently (keeping
  6902. ;; tags).
  6903. (let ((pos (point)))
  6904. (beginning-of-line)
  6905. (unless (looking-at org-complex-heading-regexp)
  6906. (error "This should not happen"))
  6907. (when (and (match-beginning 4)
  6908. (> pos (match-beginning 4))
  6909. (< pos (match-end 4)))
  6910. (setq initial-content (buffer-substring pos (match-end 4)))
  6911. (goto-char pos)
  6912. (delete-region (point) (match-end 4))
  6913. (if (looking-at "[ \t]*$")
  6914. (replace-match "")
  6915. (insert (make-string (length initial-content) ?\s)))
  6916. (setq initial-content (org-trim initial-content)))
  6917. (goto-char pos))
  6918. ;; A normal line.
  6919. (setq initial-content
  6920. (org-trim
  6921. (delete-and-extract-region (point) (line-end-position))))))
  6922. ;; If we are at the beginning of the line, insert before it.
  6923. ;; Otherwise, after it.
  6924. (cond
  6925. ((and (bolp) (looking-at "[ \t]*$")))
  6926. ((bolp) (save-excursion (insert "\n")))
  6927. (t (end-of-line)
  6928. (insert "\n")))
  6929. ;; Insert the new heading
  6930. (insert stars)
  6931. (just-one-space)
  6932. (insert initial-content)
  6933. (unless (and blank (org-previous-line-empty-p))
  6934. (org-N-empty-lines-before-current (if blank 1 0)))
  6935. ;; Adjust visibility, which may be messed up if we removed
  6936. ;; blank lines while previous entry was hidden.
  6937. (let ((bol (line-beginning-position)))
  6938. (dolist (o (overlays-at (1- bol)))
  6939. (when (and (eq (overlay-get o 'invisible) 'outline)
  6940. (eq (overlay-end o) bol))
  6941. (move-overlay o (overlay-start o) (1- bol)))))
  6942. (run-hooks 'org-insert-heading-hook)))))))
  6943. (defun org-N-empty-lines-before-current (N)
  6944. "Make the number of empty lines before current exactly N.
  6945. So this will delete or add empty lines."
  6946. (save-excursion
  6947. (beginning-of-line)
  6948. (let ((p (point)))
  6949. (skip-chars-backward " \r\t\n")
  6950. (unless (bolp) (forward-line))
  6951. (delete-region (point) p))
  6952. (when (> N 0) (insert (make-string N ?\n)))))
  6953. (defun org-get-heading (&optional no-tags no-todo)
  6954. "Return the heading of the current entry, without the stars.
  6955. When NO-TAGS is non-nil, don't include tags.
  6956. When NO-TODO is non-nil, don't include TODO keywords."
  6957. (save-excursion
  6958. (org-back-to-heading t)
  6959. (cond
  6960. ((and no-tags no-todo)
  6961. (looking-at org-complex-heading-regexp)
  6962. (match-string 4))
  6963. (no-tags
  6964. (looking-at (concat org-outline-regexp
  6965. "\\(.*?\\)"
  6966. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6967. (match-string 1))
  6968. (no-todo
  6969. (looking-at org-todo-line-regexp)
  6970. (match-string 3))
  6971. (t (looking-at org-heading-regexp)
  6972. (match-string 2)))))
  6973. (defvar orgstruct-mode) ; defined below
  6974. (defun org-heading-components ()
  6975. "Return the components of the current heading.
  6976. This is a list with the following elements:
  6977. - the level as an integer
  6978. - the reduced level, different if `org-odd-levels-only' is set.
  6979. - the TODO keyword, or nil
  6980. - the priority character, like ?A, or nil if no priority is given
  6981. - the headline text itself, or the tags string if no headline text
  6982. - the tags string, or nil."
  6983. (save-excursion
  6984. (org-back-to-heading t)
  6985. (when (let (case-fold-search)
  6986. (looking-at
  6987. (if orgstruct-mode
  6988. org-heading-regexp
  6989. org-complex-heading-regexp)))
  6990. (if orgstruct-mode
  6991. (list (length (match-string 1))
  6992. (org-reduced-level (length (match-string 1)))
  6993. nil
  6994. nil
  6995. (match-string 2)
  6996. nil)
  6997. (list (length (match-string 1))
  6998. (org-reduced-level (length (match-string 1)))
  6999. (org-match-string-no-properties 2)
  7000. (and (match-end 3) (aref (match-string 3) 2))
  7001. (org-match-string-no-properties 4)
  7002. (org-match-string-no-properties 5))))))
  7003. (defun org-get-entry ()
  7004. "Get the entry text, after heading, entire subtree."
  7005. (save-excursion
  7006. (org-back-to-heading t)
  7007. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7008. (defun org-insert-heading-after-current ()
  7009. "Insert a new heading with same level as current, after current subtree."
  7010. (interactive)
  7011. (org-back-to-heading)
  7012. (org-insert-heading)
  7013. (org-move-subtree-down)
  7014. (end-of-line 1))
  7015. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7016. "Insert heading with `org-insert-heading-respect-content' set to t."
  7017. (interactive)
  7018. (org-insert-heading '(4) invisible-ok))
  7019. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7020. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7021. (interactive)
  7022. (org-insert-todo-heading force-state '(4)))
  7023. (defun org-insert-todo-heading (arg &optional force-heading)
  7024. "Insert a new heading with the same level and TODO state as current heading.
  7025. If the heading has no TODO state, or if the state is DONE, use the first
  7026. state (TODO by default). Also with one prefix arg, force first state. With
  7027. two prefix args, force inserting at the end of the parent subtree."
  7028. (interactive "P")
  7029. (when (or force-heading (not (org-insert-item 'checkbox)))
  7030. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7031. force-heading))
  7032. (save-excursion
  7033. (org-back-to-heading)
  7034. (outline-previous-heading)
  7035. (looking-at org-todo-line-regexp))
  7036. (let*
  7037. ((new-mark-x
  7038. (if (or (equal arg '(4))
  7039. (not (match-beginning 2))
  7040. (member (match-string 2) org-done-keywords))
  7041. (car org-todo-keywords-1)
  7042. (match-string 2)))
  7043. (new-mark
  7044. (or
  7045. (run-hook-with-args-until-success
  7046. 'org-todo-get-default-hook new-mark-x nil)
  7047. new-mark-x)))
  7048. (beginning-of-line 1)
  7049. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7050. (if org-treat-insert-todo-heading-as-state-change
  7051. (org-todo new-mark)
  7052. (insert new-mark " "))))
  7053. (when org-provide-todo-statistics
  7054. (org-update-parent-todo-statistics))))
  7055. (defun org-insert-subheading (arg)
  7056. "Insert a new subheading and demote it.
  7057. Works for outline headings and for plain lists alike."
  7058. (interactive "P")
  7059. (org-insert-heading arg)
  7060. (cond
  7061. ((org-at-heading-p) (org-do-demote))
  7062. ((org-at-item-p) (org-indent-item))))
  7063. (defun org-insert-todo-subheading (arg)
  7064. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7065. Works for outline headings and for plain lists alike."
  7066. (interactive "P")
  7067. (org-insert-todo-heading arg)
  7068. (cond
  7069. ((org-at-heading-p) (org-do-demote))
  7070. ((org-at-item-p) (org-indent-item))))
  7071. ;;; Promotion and Demotion
  7072. (defvar org-after-demote-entry-hook nil
  7073. "Hook run after an entry has been demoted.
  7074. The cursor will be at the beginning of the entry.
  7075. When a subtree is being demoted, the hook will be called for each node.")
  7076. (defvar org-after-promote-entry-hook nil
  7077. "Hook run after an entry has been promoted.
  7078. The cursor will be at the beginning of the entry.
  7079. When a subtree is being promoted, the hook will be called for each node.")
  7080. (defun org-promote-subtree ()
  7081. "Promote the entire subtree.
  7082. See also `org-promote'."
  7083. (interactive)
  7084. (save-excursion
  7085. (org-with-limited-levels (org-map-tree 'org-promote)))
  7086. (org-fix-position-after-promote))
  7087. (defun org-demote-subtree ()
  7088. "Demote the entire subtree.
  7089. See `org-demote' and `org-promote'."
  7090. (interactive)
  7091. (save-excursion
  7092. (org-with-limited-levels (org-map-tree 'org-demote)))
  7093. (org-fix-position-after-promote))
  7094. (defun org-do-promote ()
  7095. "Promote the current heading higher up the tree.
  7096. If the region is active in `transient-mark-mode', promote all
  7097. headings in the region."
  7098. (interactive)
  7099. (save-excursion
  7100. (if (org-region-active-p)
  7101. (org-map-region 'org-promote (region-beginning) (region-end))
  7102. (org-promote)))
  7103. (org-fix-position-after-promote))
  7104. (defun org-do-demote ()
  7105. "Demote the current heading lower down the tree.
  7106. If the region is active in `transient-mark-mode', demote all
  7107. headings in the region."
  7108. (interactive)
  7109. (save-excursion
  7110. (if (org-region-active-p)
  7111. (org-map-region 'org-demote (region-beginning) (region-end))
  7112. (org-demote)))
  7113. (org-fix-position-after-promote))
  7114. (defun org-fix-position-after-promote ()
  7115. "Fix cursor position and indentation after demoting/promoting."
  7116. (let ((pos (point)))
  7117. (when (save-excursion
  7118. (beginning-of-line 1)
  7119. (looking-at org-todo-line-regexp)
  7120. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7121. (cond ((eobp) (insert " "))
  7122. ((eolp) (insert " "))
  7123. ((equal (char-after) ?\ ) (forward-char 1))))))
  7124. (defun org-current-level ()
  7125. "Return the level of the current entry, or nil if before the first headline.
  7126. The level is the number of stars at the beginning of the
  7127. headline. Use `org-reduced-level' to remove the effect of
  7128. `org-odd-levels'. Unlike to `org-outline-level', this function
  7129. ignores inlinetasks."
  7130. (let ((level (org-with-limited-levels (org-outline-level))))
  7131. (and (> level 0) level)))
  7132. (defun org-get-previous-line-level ()
  7133. "Return the outline depth of the last headline before the current line.
  7134. Returns 0 for the first headline in the buffer, and nil if before the
  7135. first headline."
  7136. (and (org-current-level)
  7137. (or (and (/= (line-beginning-position) (point-min))
  7138. (save-excursion (beginning-of-line 0) (org-current-level)))
  7139. 0)))
  7140. (defun org-reduced-level (l)
  7141. "Compute the effective level of a heading.
  7142. This takes into account the setting of `org-odd-levels-only'."
  7143. (cond
  7144. ((zerop l) 0)
  7145. (org-odd-levels-only (1+ (floor (/ l 2))))
  7146. (t l)))
  7147. (defun org-level-increment ()
  7148. "Return the number of stars that will be added or removed at a
  7149. time to headlines when structure editing, based on the value of
  7150. `org-odd-levels-only'."
  7151. (if org-odd-levels-only 2 1))
  7152. (defun org-get-valid-level (level &optional change)
  7153. "Rectify a level change under the influence of `org-odd-levels-only'
  7154. LEVEL is a current level, CHANGE is by how much the level should be
  7155. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7156. even level numbers will become the next higher odd number."
  7157. (if org-odd-levels-only
  7158. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7159. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7160. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7161. (max 1 (+ level (or change 0)))))
  7162. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7163. (defun org-promote ()
  7164. "Promote the current heading higher up the tree."
  7165. (org-with-wide-buffer
  7166. (org-back-to-heading t)
  7167. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7168. after-change-functions))
  7169. (level (save-match-data (funcall outline-level)))
  7170. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7171. (diff (abs (- level (length up-head) -1))))
  7172. (cond
  7173. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7174. (replace-match "# " nil t))
  7175. ((= level 1)
  7176. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7177. (t (replace-match up-head nil t)))
  7178. (unless (= level 1)
  7179. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7180. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7181. (run-hooks 'org-after-promote-entry-hook))))
  7182. (defun org-demote ()
  7183. "Demote the current heading lower down the tree."
  7184. (org-with-wide-buffer
  7185. (org-back-to-heading t)
  7186. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7187. after-change-functions))
  7188. (level (save-match-data (funcall outline-level)))
  7189. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7190. (diff (abs (- level (length down-head) -1))))
  7191. (replace-match down-head nil t)
  7192. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7193. (when org-adapt-indentation (org-fixup-indentation diff))
  7194. (run-hooks 'org-after-demote-entry-hook))))
  7195. (defun org-cycle-level ()
  7196. "Cycle the level of an empty headline through possible states.
  7197. This goes first to child, then to parent, level, then up the hierarchy.
  7198. After top level, it switches back to sibling level."
  7199. (interactive)
  7200. (let ((org-adapt-indentation nil))
  7201. (when (org-point-at-end-of-empty-headline)
  7202. (setq this-command 'org-cycle-level) ; Only needed for caching
  7203. (let ((cur-level (org-current-level))
  7204. (prev-level (org-get-previous-line-level)))
  7205. (cond
  7206. ;; If first headline in file, promote to top-level.
  7207. ((= prev-level 0)
  7208. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7209. do (org-do-promote)))
  7210. ;; If same level as prev, demote one.
  7211. ((= prev-level cur-level)
  7212. (org-do-demote))
  7213. ;; If parent is top-level, promote to top level if not already.
  7214. ((= prev-level 1)
  7215. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7216. do (org-do-promote)))
  7217. ;; If top-level, return to prev-level.
  7218. ((= cur-level 1)
  7219. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7220. do (org-do-demote)))
  7221. ;; If less than prev-level, promote one.
  7222. ((< cur-level prev-level)
  7223. (org-do-promote))
  7224. ;; If deeper than prev-level, promote until higher than
  7225. ;; prev-level.
  7226. ((> cur-level prev-level)
  7227. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7228. do (org-do-promote))))
  7229. t))))
  7230. (defun org-map-tree (fun)
  7231. "Call FUN for every heading underneath the current one."
  7232. (org-back-to-heading)
  7233. (let ((level (funcall outline-level)))
  7234. (save-excursion
  7235. (funcall fun)
  7236. (while (and (progn
  7237. (outline-next-heading)
  7238. (> (funcall outline-level) level))
  7239. (not (eobp)))
  7240. (funcall fun)))))
  7241. (defun org-map-region (fun beg end)
  7242. "Call FUN for every heading between BEG and END."
  7243. (let ((org-ignore-region t))
  7244. (save-excursion
  7245. (setq end (copy-marker end))
  7246. (goto-char beg)
  7247. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7248. (< (point) end))
  7249. (funcall fun))
  7250. (while (and (progn
  7251. (outline-next-heading)
  7252. (< (point) end))
  7253. (not (eobp)))
  7254. (funcall fun)))))
  7255. (defun org-fixup-indentation (diff)
  7256. "Change the indentation in the current entry by DIFF.
  7257. DIFF is an integer. Indentation is done according to the
  7258. following rules:
  7259. - Planning information and property drawers are always indented
  7260. according to the new level of the headline;
  7261. - Footnote definitions and their contents are ignored;
  7262. - Inlinetasks' boundaries are not shifted;
  7263. - Empty lines are ignored;
  7264. - Other lines' indentation are shifted by DIFF columns, unless
  7265. it would introduce a structural change in the document, in
  7266. which case no shifting is done at all.
  7267. Assume point is at a heading or an inlinetask beginning."
  7268. (org-with-wide-buffer
  7269. (narrow-to-region (line-beginning-position)
  7270. (save-excursion
  7271. (if (org-with-limited-levels (org-at-heading-p))
  7272. (org-with-limited-levels (outline-next-heading))
  7273. (org-inlinetask-goto-end))
  7274. (point)))
  7275. (forward-line)
  7276. ;; Indent properly planning info and property drawer.
  7277. (when (org-looking-at-p org-planning-line-re)
  7278. (org-indent-line)
  7279. (forward-line))
  7280. (when (looking-at org-property-drawer-re)
  7281. (goto-char (match-end 0))
  7282. (forward-line)
  7283. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7284. (catch 'no-shift
  7285. (when (zerop diff) (throw 'no-shift nil))
  7286. ;; If DIFF is negative, first check if a shift is possible at all
  7287. ;; (e.g., it doesn't break structure). This can only happen if
  7288. ;; some contents are not properly indented.
  7289. (let ((case-fold-search t))
  7290. (when (< diff 0)
  7291. (let ((diff (- diff))
  7292. (forbidden-re (concat org-outline-regexp
  7293. "\\|"
  7294. (substring org-footnote-definition-re 1))))
  7295. (save-excursion
  7296. (while (not (eobp))
  7297. (cond
  7298. ((org-looking-at-p "[ \t]*$") (forward-line))
  7299. ((and (org-looking-at-p org-footnote-definition-re)
  7300. (let ((e (org-element-at-point)))
  7301. (and (eq (org-element-type e) 'footnote-definition)
  7302. (goto-char (org-element-property :end e))))))
  7303. ((org-looking-at-p org-outline-regexp) (forward-line))
  7304. ;; Give up if shifting would move before column 0 or
  7305. ;; if it would introduce a headline or a footnote
  7306. ;; definition.
  7307. (t
  7308. (skip-chars-forward " \t")
  7309. (let ((ind (current-column)))
  7310. (when (or (< ind diff)
  7311. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7312. (throw 'no-shift nil)))
  7313. ;; Ignore contents of example blocks and source
  7314. ;; blocks if their indentation is meant to be
  7315. ;; preserved. Jump to block's closing line.
  7316. (beginning-of-line)
  7317. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7318. (let ((e (org-element-at-point)))
  7319. (and (memq (org-element-type e)
  7320. '(example-block src-block))
  7321. (or org-src-preserve-indentation
  7322. (org-element-property :preserve-indent e))
  7323. (goto-char (org-element-property :end e))
  7324. (progn (skip-chars-backward " \r\t\n")
  7325. (beginning-of-line)
  7326. t))))
  7327. (forward-line))))))))
  7328. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7329. ;; by DIFF. Also skip contents of source or example blocks
  7330. ;; when indentation is meant to be preserved.
  7331. (while (not (eobp))
  7332. (cond
  7333. ((and (org-looking-at-p org-footnote-definition-re)
  7334. (let ((e (org-element-at-point)))
  7335. (and (eq (org-element-type e) 'footnote-definition)
  7336. (goto-char (org-element-property :end e))))))
  7337. ((org-looking-at-p org-outline-regexp) (forward-line))
  7338. ((org-looking-at-p "[ \t]*$") (forward-line))
  7339. (t
  7340. (org-indent-line-to (+ (org-get-indentation) diff))
  7341. (beginning-of-line)
  7342. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7343. (let ((e (org-element-at-point)))
  7344. (and (memq (org-element-type e)
  7345. '(example-block src-block))
  7346. (or org-src-preserve-indentation
  7347. (org-element-property :preserve-indent e))
  7348. (goto-char (org-element-property :end e))
  7349. (progn (skip-chars-backward " \r\t\n")
  7350. (beginning-of-line)
  7351. t))))
  7352. (forward-line)))))))))
  7353. (defun org-convert-to-odd-levels ()
  7354. "Convert an org-mode file with all levels allowed to one with odd levels.
  7355. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7356. level 5 etc."
  7357. (interactive)
  7358. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7359. (let ((outline-level 'org-outline-level)
  7360. (org-odd-levels-only nil) n)
  7361. (save-excursion
  7362. (goto-char (point-min))
  7363. (while (re-search-forward "^\\*\\*+ " nil t)
  7364. (setq n (- (length (match-string 0)) 2))
  7365. (while (>= (setq n (1- n)) 0)
  7366. (org-demote))
  7367. (end-of-line 1))))))
  7368. (defun org-convert-to-oddeven-levels ()
  7369. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7370. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7371. file contains a section with an even level, conversion would
  7372. destroy the structure of the file. An error is signaled in this
  7373. case."
  7374. (interactive)
  7375. (goto-char (point-min))
  7376. ;; First check if there are no even levels
  7377. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7378. (org-show-set-visibility 'canonical)
  7379. (error "Not all levels are odd in this file. Conversion not possible"))
  7380. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7381. (let ((outline-regexp org-outline-regexp)
  7382. (outline-level 'org-outline-level)
  7383. (org-odd-levels-only nil) n)
  7384. (save-excursion
  7385. (goto-char (point-min))
  7386. (while (re-search-forward "^\\*\\*+ " nil t)
  7387. (setq n (/ (1- (length (match-string 0))) 2))
  7388. (while (>= (setq n (1- n)) 0)
  7389. (org-promote))
  7390. (end-of-line 1))))))
  7391. (defun org-tr-level (n)
  7392. "Make N odd if required."
  7393. (if org-odd-levels-only (1+ (/ n 2)) n))
  7394. ;;; Vertical tree motion, cutting and pasting of subtrees
  7395. (defun org-move-subtree-up (&optional arg)
  7396. "Move the current subtree up past ARG headlines of the same level."
  7397. (interactive "p")
  7398. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7399. (defun org-move-subtree-down (&optional arg)
  7400. "Move the current subtree down past ARG headlines of the same level."
  7401. (interactive "p")
  7402. (setq arg (prefix-numeric-value arg))
  7403. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7404. 'org-get-last-sibling))
  7405. (ins-point (make-marker))
  7406. (cnt (abs arg))
  7407. (col (current-column))
  7408. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7409. ;; Select the tree
  7410. (org-back-to-heading)
  7411. (setq beg0 (point))
  7412. (save-excursion
  7413. (setq ne-beg (org-back-over-empty-lines))
  7414. (setq beg (point)))
  7415. (save-match-data
  7416. (save-excursion (outline-end-of-heading)
  7417. (setq folded (outline-invisible-p)))
  7418. (progn (org-end-of-subtree nil t)
  7419. (unless (eobp) (backward-char))))
  7420. (outline-next-heading)
  7421. (setq ne-end (org-back-over-empty-lines))
  7422. (setq end (point))
  7423. (goto-char beg0)
  7424. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7425. ;; include less whitespace
  7426. (save-excursion
  7427. (goto-char beg)
  7428. (forward-line (- ne-beg ne-end))
  7429. (setq beg (point))))
  7430. ;; Find insertion point, with error handling
  7431. (while (> cnt 0)
  7432. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7433. (progn (goto-char beg0)
  7434. (user-error "Cannot move past superior level or buffer limit")))
  7435. (setq cnt (1- cnt)))
  7436. (when (> arg 0)
  7437. ;; Moving forward - still need to move over subtree
  7438. (org-end-of-subtree t t)
  7439. (save-excursion
  7440. (org-back-over-empty-lines)
  7441. (or (bolp) (newline))))
  7442. (setq ne-ins (org-back-over-empty-lines))
  7443. (move-marker ins-point (point))
  7444. (setq txt (buffer-substring beg end))
  7445. (org-save-markers-in-region beg end)
  7446. (delete-region beg end)
  7447. (org-remove-empty-overlays-at beg)
  7448. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7449. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7450. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7451. (let ((bbb (point)))
  7452. (insert-before-markers txt)
  7453. (org-reinstall-markers-in-region bbb)
  7454. (move-marker ins-point bbb))
  7455. (or (bolp) (insert "\n"))
  7456. (setq ins-end (point))
  7457. (goto-char ins-point)
  7458. (org-skip-whitespace)
  7459. (when (and (< arg 0)
  7460. (org-first-sibling-p)
  7461. (> ne-ins ne-beg))
  7462. ;; Move whitespace back to beginning
  7463. (save-excursion
  7464. (goto-char ins-end)
  7465. (let ((kill-whole-line t))
  7466. (kill-line (- ne-ins ne-beg)) (point)))
  7467. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7468. (move-marker ins-point nil)
  7469. (if folded
  7470. (outline-hide-subtree)
  7471. (org-show-entry)
  7472. (org-show-children)
  7473. (org-cycle-hide-drawers 'children))
  7474. (org-clean-visibility-after-subtree-move)
  7475. ;; move back to the initial column we were at
  7476. (move-to-column col)))
  7477. (defvar org-subtree-clip ""
  7478. "Clipboard for cut and paste of subtrees.
  7479. This is actually only a copy of the kill, because we use the normal kill
  7480. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7481. (defvar org-subtree-clip-folded nil
  7482. "Was the last copied subtree folded?
  7483. This is used to fold the tree back after pasting.")
  7484. (defun org-cut-subtree (&optional n)
  7485. "Cut the current subtree into the clipboard.
  7486. With prefix arg N, cut this many sequential subtrees.
  7487. This is a short-hand for marking the subtree and then cutting it."
  7488. (interactive "p")
  7489. (org-copy-subtree n 'cut))
  7490. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7491. "Copy the current subtree it in the clipboard.
  7492. With prefix arg N, copy this many sequential subtrees.
  7493. This is a short-hand for marking the subtree and then copying it.
  7494. If CUT is non-nil, actually cut the subtree.
  7495. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7496. of some markers in the region, even if CUT is non-nil. This is
  7497. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7498. (interactive "p")
  7499. (let (beg end folded (beg0 (point)))
  7500. (if (org-called-interactively-p 'any)
  7501. (org-back-to-heading nil) ; take what looks like a subtree
  7502. (org-back-to-heading t)) ; take what is really there
  7503. (setq beg (point))
  7504. (skip-chars-forward " \t\r\n")
  7505. (save-match-data
  7506. (if nosubtrees
  7507. (outline-next-heading)
  7508. (save-excursion (outline-end-of-heading)
  7509. (setq folded (outline-invisible-p)))
  7510. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7511. (org-end-of-subtree t t)))
  7512. ;; Include the end of an inlinetask
  7513. (when (and (featurep 'org-inlinetask)
  7514. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7515. "END[ \t]*$")))
  7516. (end-of-line))
  7517. (setq end (point))
  7518. (goto-char beg0)
  7519. (when (> end beg)
  7520. (setq org-subtree-clip-folded folded)
  7521. (when (or cut force-store-markers)
  7522. (org-save-markers-in-region beg end))
  7523. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7524. (setq org-subtree-clip (current-kill 0))
  7525. (message "%s: Subtree(s) with %d characters"
  7526. (if cut "Cut" "Copied")
  7527. (length org-subtree-clip)))))
  7528. (defun org-paste-subtree (&optional level tree for-yank remove)
  7529. "Paste the clipboard as a subtree, with modification of headline level.
  7530. The entire subtree is promoted or demoted in order to match a new headline
  7531. level.
  7532. If the cursor is at the beginning of a headline, the same level as
  7533. that headline is used to paste the tree.
  7534. If not, the new level is derived from the *visible* headings
  7535. before and after the insertion point, and taken to be the inferior headline
  7536. level of the two. So if the previous visible heading is level 3 and the
  7537. next is level 4 (or vice versa), level 4 will be used for insertion.
  7538. This makes sure that the subtree remains an independent subtree and does
  7539. not swallow low level entries.
  7540. You can also force a different level, either by using a numeric prefix
  7541. argument, or by inserting the heading marker by hand. For example, if the
  7542. cursor is after \"*****\", then the tree will be shifted to level 5.
  7543. If optional TREE is given, use this text instead of the kill ring.
  7544. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7545. move back over whitespace before inserting, and move point to the end of
  7546. the inserted text when done.
  7547. When REMOVE is non-nil, remove the subtree from the clipboard."
  7548. (interactive "P")
  7549. (setq tree (or tree (and kill-ring (current-kill 0))))
  7550. (unless (org-kill-is-subtree-p tree)
  7551. (user-error "%s"
  7552. (substitute-command-keys
  7553. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7554. (org-with-limited-levels
  7555. (let* ((visp (not (outline-invisible-p)))
  7556. (txt tree)
  7557. (^re_ "\\(\\*+\\)[ \t]*")
  7558. (old-level (if (string-match org-outline-regexp-bol txt)
  7559. (- (match-end 0) (match-beginning 0) 1)
  7560. -1))
  7561. (force-level (cond (level (prefix-numeric-value level))
  7562. ((and (looking-at "[ \t]*$")
  7563. (string-match
  7564. "^\\*+$" (buffer-substring
  7565. (point-at-bol) (point))))
  7566. (- (match-end 0) (match-beginning 0)))
  7567. ((and (bolp)
  7568. (looking-at org-outline-regexp))
  7569. (- (match-end 0) (point) 1))))
  7570. (previous-level (save-excursion
  7571. (condition-case nil
  7572. (progn
  7573. (outline-previous-visible-heading 1)
  7574. (if (looking-at ^re_)
  7575. (- (match-end 0) (match-beginning 0) 1)
  7576. 1))
  7577. (error 1))))
  7578. (next-level (save-excursion
  7579. (condition-case nil
  7580. (progn
  7581. (or (looking-at org-outline-regexp)
  7582. (outline-next-visible-heading 1))
  7583. (if (looking-at ^re_)
  7584. (- (match-end 0) (match-beginning 0) 1)
  7585. 1))
  7586. (error 1))))
  7587. (new-level (or force-level (max previous-level next-level)))
  7588. (shift (if (or (= old-level -1)
  7589. (= new-level -1)
  7590. (= old-level new-level))
  7591. 0
  7592. (- new-level old-level)))
  7593. (delta (if (> shift 0) -1 1))
  7594. (func (if (> shift 0) 'org-demote 'org-promote))
  7595. (org-odd-levels-only nil)
  7596. beg end newend)
  7597. ;; Remove the forced level indicator
  7598. (when force-level
  7599. (delete-region (point-at-bol) (point)))
  7600. ;; Paste
  7601. (beginning-of-line (if (bolp) 1 2))
  7602. (setq beg (point))
  7603. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7604. (insert-before-markers txt)
  7605. (unless (string-match "\n\\'" txt) (insert "\n"))
  7606. (setq newend (point))
  7607. (org-reinstall-markers-in-region beg)
  7608. (setq end (point))
  7609. (goto-char beg)
  7610. (skip-chars-forward " \t\n\r")
  7611. (setq beg (point))
  7612. (when (and (outline-invisible-p) visp)
  7613. (save-excursion (outline-show-heading)))
  7614. ;; Shift if necessary
  7615. (unless (= shift 0)
  7616. (save-restriction
  7617. (narrow-to-region beg end)
  7618. (while (not (= shift 0))
  7619. (org-map-region func (point-min) (point-max))
  7620. (setq shift (+ delta shift)))
  7621. (goto-char (point-min))
  7622. (setq newend (point-max))))
  7623. (when (or (org-called-interactively-p 'interactive) for-yank)
  7624. (message "Clipboard pasted as level %d subtree" new-level))
  7625. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7626. kill-ring
  7627. (eq org-subtree-clip (current-kill 0))
  7628. org-subtree-clip-folded)
  7629. ;; The tree was folded before it was killed/copied
  7630. (outline-hide-subtree))
  7631. (and for-yank (goto-char newend))
  7632. (and remove (setq kill-ring (cdr kill-ring))))))
  7633. (defun org-kill-is-subtree-p (&optional txt)
  7634. "Check if the current kill is an outline subtree, or a set of trees.
  7635. Returns nil if kill does not start with a headline, or if the first
  7636. headline level is not the largest headline level in the tree.
  7637. So this will actually accept several entries of equal levels as well,
  7638. which is OK for `org-paste-subtree'.
  7639. If optional TXT is given, check this string instead of the current kill."
  7640. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7641. (re (org-get-limited-outline-regexp))
  7642. (^re (concat "^" re))
  7643. (start-level (and kill
  7644. (string-match
  7645. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7646. kill)
  7647. (- (match-end 2) (match-beginning 2) 1)))
  7648. (start (1+ (or (match-beginning 2) -1))))
  7649. (if (not start-level)
  7650. (progn
  7651. nil) ;; does not even start with a heading
  7652. (catch 'exit
  7653. (while (setq start (string-match ^re kill (1+ start)))
  7654. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7655. (throw 'exit nil)))
  7656. t))))
  7657. (defvar org-markers-to-move nil
  7658. "Markers that should be moved with a cut-and-paste operation.
  7659. Those markers are stored together with their positions relative to
  7660. the start of the region.")
  7661. (defun org-save-markers-in-region (beg end)
  7662. "Check markers in region.
  7663. If these markers are between BEG and END, record their position relative
  7664. to BEG, so that after moving the block of text, we can put the markers back
  7665. into place.
  7666. This function gets called just before an entry or tree gets cut from the
  7667. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7668. called immediately, to move the markers with the entries."
  7669. (setq org-markers-to-move nil)
  7670. (when (featurep 'org-clock)
  7671. (org-clock-save-markers-for-cut-and-paste beg end))
  7672. (when (featurep 'org-agenda)
  7673. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7674. (defun org-check-and-save-marker (marker beg end)
  7675. "Check if MARKER is between BEG and END.
  7676. If yes, remember the marker and the distance to BEG."
  7677. (when (and (marker-buffer marker)
  7678. (equal (marker-buffer marker) (current-buffer))
  7679. (>= marker beg) (< marker end))
  7680. (push (cons marker (- marker beg)) org-markers-to-move)))
  7681. (defun org-reinstall-markers-in-region (beg)
  7682. "Move all remembered markers to their position relative to BEG."
  7683. (dolist (x org-markers-to-move)
  7684. (move-marker (car x) (+ beg (cdr x))))
  7685. (setq org-markers-to-move nil))
  7686. (defun org-narrow-to-subtree ()
  7687. "Narrow buffer to the current subtree."
  7688. (interactive)
  7689. (save-excursion
  7690. (save-match-data
  7691. (org-with-limited-levels
  7692. (narrow-to-region
  7693. (progn (org-back-to-heading t) (point))
  7694. (progn (org-end-of-subtree t t)
  7695. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7696. (point)))))))
  7697. (defun org-narrow-to-block ()
  7698. "Narrow buffer to the current block."
  7699. (interactive)
  7700. (let* ((case-fold-search t)
  7701. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7702. "^[ \t]*#\\+end_.*")))
  7703. (if blockp
  7704. (narrow-to-region (car blockp) (cdr blockp))
  7705. (user-error "Not in a block"))))
  7706. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7707. "Clone the task (subtree) at point N times.
  7708. The clones will be inserted as siblings.
  7709. In interactive use, the user will be prompted for the number of
  7710. clones to be produced. If the entry has a timestamp, the user
  7711. will also be prompted for a time shift, which may be a repeater
  7712. as used in time stamps, for example `+3d'. To disable this,
  7713. you can call the function with a universal prefix argument.
  7714. When a valid repeater is given and the entry contains any time
  7715. stamps, the clones will become a sequence in time, with time
  7716. stamps in the subtree shifted for each clone produced. If SHIFT
  7717. is nil or the empty string, time stamps will be left alone. The
  7718. ID property of the original subtree is removed.
  7719. If the original subtree did contain time stamps with a repeater,
  7720. the following will happen:
  7721. - the repeater will be removed in each clone
  7722. - an additional clone will be produced, with the current, unshifted
  7723. date(s) in the entry.
  7724. - the original entry will be placed *after* all the clones, with
  7725. repeater intact.
  7726. - the start days in the repeater in the original entry will be shifted
  7727. to past the last clone.
  7728. In this way you can spell out a number of instances of a repeating task,
  7729. and still retain the repeater to cover future instances of the task.
  7730. As described above, N+1 clones are produced when the original
  7731. subtree has a repeater. Setting N to 0, then, can be used to
  7732. remove the repeater from a subtree and create a shifted clone
  7733. with the original repeater."
  7734. (interactive "nNumber of clones to produce: ")
  7735. (let ((shift
  7736. (or shift
  7737. (if (and (not (equal current-prefix-arg '(4)))
  7738. (save-excursion
  7739. (re-search-forward org-ts-regexp-both
  7740. (save-excursion
  7741. (org-end-of-subtree t)
  7742. (point)) t)))
  7743. (read-from-minibuffer
  7744. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7745. ""))) ;; No time shift
  7746. (n-no-remove -1)
  7747. (drawer-re org-drawer-regexp)
  7748. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7749. beg end template task idprop
  7750. shift-n shift-what doshift nmin nmax)
  7751. (unless (wholenump n)
  7752. (user-error "Invalid number of replications %s" n))
  7753. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7754. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7755. shift)))
  7756. (user-error "Invalid shift specification %s" shift))
  7757. (when doshift
  7758. (setq shift-n (string-to-number (match-string 1 shift))
  7759. shift-what (cdr (assoc (match-string 2 shift)
  7760. '(("d" . day) ("w" . week)
  7761. ("m" . month) ("y" . year))))))
  7762. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7763. (setq nmin 1 nmax n)
  7764. (org-back-to-heading t)
  7765. (setq beg (point))
  7766. (setq idprop (org-entry-get nil "ID"))
  7767. (org-end-of-subtree t t)
  7768. (or (bolp) (insert "\n"))
  7769. (setq end (point))
  7770. (setq template (buffer-substring beg end))
  7771. (when (and doshift
  7772. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7773. (delete-region beg end)
  7774. (setq end beg)
  7775. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7776. (goto-char end)
  7777. (cl-loop for n from nmin to nmax do
  7778. ;; prepare clone
  7779. (with-temp-buffer
  7780. (insert template)
  7781. (org-mode)
  7782. (goto-char (point-min))
  7783. (org-show-subtree)
  7784. (and idprop (if org-clone-delete-id
  7785. (org-entry-delete nil "ID")
  7786. (org-id-get-create t)))
  7787. (unless (= n 0)
  7788. (while (re-search-forward org-clock-re nil t)
  7789. (kill-whole-line))
  7790. (goto-char (point-min))
  7791. (while (re-search-forward drawer-re nil t)
  7792. (org-remove-empty-drawer-at (point))))
  7793. (goto-char (point-min))
  7794. (when doshift
  7795. (while (re-search-forward org-ts-regexp-both nil t)
  7796. (org-timestamp-change (* n shift-n) shift-what))
  7797. (unless (= n n-no-remove)
  7798. (goto-char (point-min))
  7799. (while (re-search-forward org-ts-regexp nil t)
  7800. (save-excursion
  7801. (goto-char (match-beginning 0))
  7802. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7803. (delete-region (match-beginning 1) (match-end 1)))))))
  7804. (setq task (buffer-string)))
  7805. (insert task))
  7806. (goto-char beg)))
  7807. ;;; Outline Sorting
  7808. (defun org-sort (with-case)
  7809. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7810. Optional argument WITH-CASE means sort case-sensitively."
  7811. (interactive "P")
  7812. (cond
  7813. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7814. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7815. (t
  7816. (org-call-with-arg 'org-sort-entries with-case))))
  7817. (defun org-sort-remove-invisible (s)
  7818. "Remove invisible links from string S."
  7819. (remove-text-properties 0 (length s) org-rm-props s)
  7820. (while (string-match org-bracket-link-regexp s)
  7821. (setq s (replace-match (if (match-end 2)
  7822. (match-string 3 s)
  7823. (match-string 1 s))
  7824. t t s)))
  7825. (let ((st (format " %s " s)))
  7826. (while (string-match org-emph-re st)
  7827. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7828. (setq s (substring st 1 -1)))
  7829. s)
  7830. (defvar org-priority-regexp) ; defined later in the file
  7831. (defvar org-after-sorting-entries-or-items-hook nil
  7832. "Hook that is run after a bunch of entries or items have been sorted.
  7833. When children are sorted, the cursor is in the parent line when this
  7834. hook gets called. When a region or a plain list is sorted, the cursor
  7835. will be in the first entry of the sorted region/list.")
  7836. (defun org-sort-entries
  7837. (&optional with-case sorting-type getkey-func compare-func property)
  7838. "Sort entries on a certain level of an outline tree.
  7839. If there is an active region, the entries in the region are sorted.
  7840. Else, if the cursor is before the first entry, sort the top-level items.
  7841. Else, the children of the entry at point are sorted.
  7842. Sorting can be alphabetically, numerically, by date/time as given by
  7843. a time stamp, by a property, by priority order, or by a custom function.
  7844. The command prompts for the sorting type unless it has been given to the
  7845. function through the SORTING-TYPE argument, which needs to be a character,
  7846. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7847. the precise meaning of each character:
  7848. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7849. c By creation time, which is assumed to be the first inactive time stamp
  7850. at the beginning of a line.
  7851. d By deadline date/time.
  7852. k By clocking time.
  7853. n Numerically, by converting the beginning of the entry/item to a number.
  7854. o By order of TODO keywords.
  7855. p By priority according to the cookie.
  7856. r By the value of a property.
  7857. s By scheduled date/time.
  7858. t By date/time, either the first active time stamp in the entry, or, if
  7859. none exist, by the first inactive one.
  7860. Capital letters will reverse the sort order.
  7861. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7862. called with point at the beginning of the record. It must return either
  7863. a string or a number that should serve as the sorting key for that record.
  7864. Comparing entries ignores case by default. However, with an optional argument
  7865. WITH-CASE, the sorting considers case as well.
  7866. Sorting is done against the visible part of the headlines, it ignores hidden
  7867. links.
  7868. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7869. (interactive "P")
  7870. (let ((case-func (if with-case 'identity 'downcase))
  7871. (cmstr
  7872. ;; The clock marker is lost when using `sort-subr', let's
  7873. ;; store the clocking string.
  7874. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7875. (save-excursion
  7876. (goto-char org-clock-marker)
  7877. (buffer-substring-no-properties (line-beginning-position)
  7878. (point)))))
  7879. start beg end stars re re2
  7880. txt what tmp)
  7881. ;; Find beginning and end of region to sort
  7882. (cond
  7883. ((org-region-active-p)
  7884. ;; we will sort the region
  7885. (setq end (region-end)
  7886. what "region")
  7887. (goto-char (region-beginning))
  7888. (unless (org-at-heading-p) (outline-next-heading))
  7889. (setq start (point)))
  7890. ((or (org-at-heading-p)
  7891. (ignore-errors (progn (org-back-to-heading) t)))
  7892. ;; we will sort the children of the current headline
  7893. (org-back-to-heading)
  7894. (setq start (point)
  7895. end (progn (org-end-of-subtree t t)
  7896. (or (bolp) (insert "\n"))
  7897. (when (>= (org-back-over-empty-lines) 1)
  7898. (forward-line 1))
  7899. (point))
  7900. what "children")
  7901. (goto-char start)
  7902. (outline-show-subtree)
  7903. (outline-next-heading))
  7904. (t
  7905. ;; we will sort the top-level entries in this file
  7906. (goto-char (point-min))
  7907. (or (org-at-heading-p) (outline-next-heading))
  7908. (setq start (point))
  7909. (goto-char (point-max))
  7910. (beginning-of-line 1)
  7911. (when (looking-at ".*?\\S-")
  7912. ;; File ends in a non-white line
  7913. (end-of-line 1)
  7914. (insert "\n"))
  7915. (setq end (point-max))
  7916. (setq what "top-level")
  7917. (goto-char start)
  7918. (outline-show-all)))
  7919. (setq beg (point))
  7920. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7921. (looking-at "\\(\\*+\\)")
  7922. (setq stars (match-string 1)
  7923. re (concat "^" (regexp-quote stars) " +")
  7924. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7925. txt (buffer-substring beg end))
  7926. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7927. (when (and (not (equal stars "*")) (string-match re2 txt))
  7928. (user-error "Region to sort contains a level above the first entry"))
  7929. (unless sorting-type
  7930. (message
  7931. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7932. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7933. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7934. what)
  7935. (setq sorting-type (read-char-exclusive))
  7936. (unless getkey-func
  7937. (and (= (downcase sorting-type) ?f)
  7938. (setq getkey-func
  7939. (completing-read "Sort using function: "
  7940. obarray 'fboundp t nil nil))
  7941. (setq getkey-func (intern getkey-func))))
  7942. (and (= (downcase sorting-type) ?r)
  7943. (not property)
  7944. (setq property
  7945. (completing-read "Property: "
  7946. (mapcar #'list (org-buffer-property-keys t))
  7947. nil t))))
  7948. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7949. (message "Sorting entries...")
  7950. (save-restriction
  7951. (narrow-to-region start end)
  7952. (let ((dcst (downcase sorting-type))
  7953. (case-fold-search nil)
  7954. (now (current-time)))
  7955. (sort-subr
  7956. (/= dcst sorting-type)
  7957. ;; This function moves to the beginning character of the "record" to
  7958. ;; be sorted.
  7959. (lambda nil
  7960. (if (re-search-forward re nil t)
  7961. (goto-char (match-beginning 0))
  7962. (goto-char (point-max))))
  7963. ;; This function moves to the last character of the "record" being
  7964. ;; sorted.
  7965. (lambda nil
  7966. (save-match-data
  7967. (condition-case nil
  7968. (outline-forward-same-level 1)
  7969. (error
  7970. (goto-char (point-max))))))
  7971. ;; This function returns the value that gets sorted against.
  7972. (lambda nil
  7973. (cond
  7974. ((= dcst ?n)
  7975. (if (looking-at org-complex-heading-regexp)
  7976. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7977. nil))
  7978. ((= dcst ?a)
  7979. (if (looking-at org-complex-heading-regexp)
  7980. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7981. nil))
  7982. ((= dcst ?k)
  7983. (or (get-text-property (point) :org-clock-minutes) 0))
  7984. ((= dcst ?t)
  7985. (let ((end (save-excursion (outline-next-heading) (point))))
  7986. (if (or (re-search-forward org-ts-regexp end t)
  7987. (re-search-forward org-ts-regexp-both end t))
  7988. (org-time-string-to-seconds (match-string 0))
  7989. (org-float-time now))))
  7990. ((= dcst ?c)
  7991. (let ((end (save-excursion (outline-next-heading) (point))))
  7992. (if (re-search-forward
  7993. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7994. end t)
  7995. (org-time-string-to-seconds (match-string 0))
  7996. (org-float-time now))))
  7997. ((= dcst ?s)
  7998. (let ((end (save-excursion (outline-next-heading) (point))))
  7999. (if (re-search-forward org-scheduled-time-regexp end t)
  8000. (org-time-string-to-seconds (match-string 1))
  8001. (org-float-time now))))
  8002. ((= dcst ?d)
  8003. (let ((end (save-excursion (outline-next-heading) (point))))
  8004. (if (re-search-forward org-deadline-time-regexp end t)
  8005. (org-time-string-to-seconds (match-string 1))
  8006. (org-float-time now))))
  8007. ((= dcst ?p)
  8008. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8009. (string-to-char (match-string 2))
  8010. org-default-priority))
  8011. ((= dcst ?r)
  8012. (or (org-entry-get nil property) ""))
  8013. ((= dcst ?o)
  8014. (when (looking-at org-complex-heading-regexp)
  8015. (let* ((m (match-string 2))
  8016. (s (if (member m org-done-keywords) '- '+)))
  8017. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8018. ((= dcst ?f)
  8019. (if getkey-func
  8020. (progn
  8021. (setq tmp (funcall getkey-func))
  8022. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8023. tmp)
  8024. (error "Invalid key function `%s'" getkey-func)))
  8025. (t (error "Invalid sorting type `%c'" sorting-type))))
  8026. nil
  8027. (cond
  8028. ((= dcst ?a) 'string<)
  8029. ((= dcst ?f) compare-func)
  8030. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8031. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8032. ;; Reset the clock marker if needed
  8033. (when cmstr
  8034. (save-excursion
  8035. (goto-char start)
  8036. (search-forward cmstr nil t)
  8037. (move-marker org-clock-marker (point))))
  8038. (message "Sorting entries...done")))
  8039. ;;; The orgstruct minor mode
  8040. ;; Define a minor mode which can be used in other modes in order to
  8041. ;; integrate the org-mode structure editing commands.
  8042. ;; This is really a hack, because the org-mode structure commands use
  8043. ;; keys which normally belong to the major mode. Here is how it
  8044. ;; works: The minor mode defines all the keys necessary to operate the
  8045. ;; structure commands, but wraps the commands into a function which
  8046. ;; tests if the cursor is currently at a headline or a plain list
  8047. ;; item. If that is the case, the structure command is used,
  8048. ;; temporarily setting many Org-mode variables like regular
  8049. ;; expressions for filling etc. However, when any of those keys is
  8050. ;; used at a different location, function uses `key-binding' to look
  8051. ;; up if the key has an associated command in another currently active
  8052. ;; keymap (minor modes, major mode, global), and executes that
  8053. ;; command. There might be problems if any of the keys is otherwise
  8054. ;; used as a prefix key.
  8055. (defcustom orgstruct-heading-prefix-regexp ""
  8056. "Regexp that matches the custom prefix of Org headlines in
  8057. orgstruct(++)-mode."
  8058. :group 'org
  8059. :version "24.4"
  8060. :package-version '(Org . "8.3")
  8061. :type 'regexp)
  8062. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8063. (defcustom orgstruct-setup-hook nil
  8064. "Hook run after orgstruct-mode-map is filled."
  8065. :group 'org
  8066. :version "24.4"
  8067. :package-version '(Org . "8.0")
  8068. :type 'hook)
  8069. (defvar orgstruct-initialized nil)
  8070. (defvar org-local-vars nil
  8071. "List of local variables, for use by `orgstruct-mode'.")
  8072. ;;;###autoload
  8073. (define-minor-mode orgstruct-mode
  8074. "Toggle the minor mode `orgstruct-mode'.
  8075. This mode is for using Org-mode structure commands in other
  8076. modes. The following keys behave as if Org-mode were active, if
  8077. the cursor is on a headline, or on a plain list item (both as
  8078. defined by Org-mode)."
  8079. nil " OrgStruct" (make-sparse-keymap)
  8080. (funcall (if orgstruct-mode
  8081. 'add-to-invisibility-spec
  8082. 'remove-from-invisibility-spec)
  8083. '(outline . t))
  8084. (when orgstruct-mode
  8085. (org-load-modules-maybe)
  8086. (unless orgstruct-initialized
  8087. (orgstruct-setup)
  8088. (setq orgstruct-initialized t))))
  8089. ;;;###autoload
  8090. (defun turn-on-orgstruct ()
  8091. "Unconditionally turn on `orgstruct-mode'."
  8092. (orgstruct-mode 1))
  8093. (defvar-local orgstruct-is-++ nil
  8094. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8095. (defvar-local org-fb-vars nil)
  8096. (defun orgstruct++-mode (&optional arg)
  8097. "Toggle `orgstruct-mode', the enhanced version of it.
  8098. In addition to setting orgstruct-mode, this also exports all
  8099. indentation and autofilling variables from org-mode into the
  8100. buffer. It will also recognize item context in multiline items."
  8101. (interactive "P")
  8102. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8103. (if (< arg 1)
  8104. (progn (orgstruct-mode -1)
  8105. (dolist (v org-fb-vars)
  8106. (set (make-local-variable (car v))
  8107. (if (eq (car-safe (cadr v)) 'quote)
  8108. (cl-cadadr v)
  8109. (nth 1 v)))))
  8110. (orgstruct-mode 1)
  8111. (setq org-fb-vars nil)
  8112. (unless org-local-vars
  8113. (setq org-local-vars (org-get-local-variables)))
  8114. (let (var val)
  8115. (dolist (x org-local-vars)
  8116. (when (string-match
  8117. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8118. \\|fill-prefix\\|indent-\\)"
  8119. (symbol-name (car x)))
  8120. (setq var (car x) val (nth 1 x))
  8121. (push (list var `(quote ,(eval var))) org-fb-vars)
  8122. (set (make-local-variable var)
  8123. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8124. (setq-local orgstruct-is-++ t))))
  8125. ;;;###autoload
  8126. (defun turn-on-orgstruct++ ()
  8127. "Unconditionally turn on `orgstruct++-mode'."
  8128. (orgstruct++-mode 1))
  8129. (defun orgstruct-error ()
  8130. "Error when there is no default binding for a structure key."
  8131. (interactive)
  8132. (funcall (if (fboundp 'user-error)
  8133. 'user-error
  8134. 'error)
  8135. "This key has no function outside structure elements"))
  8136. (defun orgstruct-setup ()
  8137. "Setup orgstruct keymap."
  8138. (dolist (cell '((org-demote . t)
  8139. (org-metaleft . t)
  8140. (org-metaright . t)
  8141. (org-promote . t)
  8142. (org-shiftmetaleft . t)
  8143. (org-shiftmetaright . t)
  8144. org-backward-element
  8145. org-backward-heading-same-level
  8146. org-ctrl-c-ret
  8147. org-ctrl-c-minus
  8148. org-ctrl-c-star
  8149. org-cycle
  8150. org-forward-heading-same-level
  8151. org-insert-heading
  8152. org-insert-heading-respect-content
  8153. org-kill-note-or-show-branches
  8154. org-mark-subtree
  8155. org-meta-return
  8156. org-metadown
  8157. org-metaup
  8158. org-narrow-to-subtree
  8159. org-promote-subtree
  8160. org-reveal
  8161. org-shiftdown
  8162. org-shiftleft
  8163. org-shiftmetadown
  8164. org-shiftmetaup
  8165. org-shiftright
  8166. org-shifttab
  8167. org-shifttab
  8168. org-shiftup
  8169. org-show-children
  8170. org-show-subtree
  8171. org-sort
  8172. org-up-element
  8173. outline-demote
  8174. outline-next-visible-heading
  8175. outline-previous-visible-heading
  8176. outline-promote
  8177. outline-up-heading))
  8178. (let ((f (or (car-safe cell) cell))
  8179. (disable-when-heading-prefix (cdr-safe cell)))
  8180. (when (fboundp f)
  8181. (let ((new-bindings))
  8182. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8183. (where-is-internal f outline-mode-map)))
  8184. (push binding new-bindings)
  8185. ;; TODO use local-function-key-map
  8186. (dolist (rep '(("<tab>" . "TAB")
  8187. ("<return>" . "RET")
  8188. ("<escape>" . "ESC")
  8189. ("<delete>" . "DEL")))
  8190. (setq binding (read-kbd-macro
  8191. (let ((case-fold-search))
  8192. (replace-regexp-in-string
  8193. (regexp-quote (cdr rep))
  8194. (car rep)
  8195. (key-description binding)))))
  8196. (cl-pushnew binding new-bindings :test 'equal)))
  8197. (dolist (binding new-bindings)
  8198. (let ((key (lookup-key orgstruct-mode-map binding)))
  8199. (when (or (not key) (numberp key))
  8200. (ignore-errors
  8201. (org-defkey orgstruct-mode-map
  8202. binding
  8203. (orgstruct-make-binding
  8204. f binding disable-when-heading-prefix))))))))))
  8205. (run-hooks 'orgstruct-setup-hook))
  8206. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8207. "Create a function for binding in the structure minor mode.
  8208. FUN is the command to call inside a table. KEY is the key that
  8209. should be checked in for a command to execute outside of tables.
  8210. Non-nil `disable-when-heading-prefix' means to disable the command
  8211. if `orgstruct-heading-prefix-regexp' is not empty."
  8212. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8213. (let ((nname name)
  8214. (i 0))
  8215. (while (fboundp (intern nname))
  8216. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8217. (setq name (intern nname)))
  8218. (eval
  8219. (let ((bindings '((org-heading-regexp
  8220. (concat "^"
  8221. orgstruct-heading-prefix-regexp
  8222. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8223. (org-outline-regexp
  8224. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8225. (org-outline-regexp-bol
  8226. (concat "^" org-outline-regexp))
  8227. (outline-regexp org-outline-regexp)
  8228. (outline-heading-end-regexp "\n")
  8229. (outline-level 'org-outline-level)
  8230. (outline-heading-alist))))
  8231. `(defun ,name (arg)
  8232. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8233. "Outside of structure, run the binding of `"
  8234. (key-description key) "'."
  8235. (when disable-when-heading-prefix
  8236. (concat
  8237. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8238. "back to the default binding due to limitations of Org's implementation of\n"
  8239. "`" (symbol-name fun) "'.")))
  8240. (interactive "p")
  8241. (let* ((disable
  8242. ,(and disable-when-heading-prefix
  8243. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8244. (fallback
  8245. (or disable
  8246. (not
  8247. (let* ,bindings
  8248. (org-context-p 'headline 'item
  8249. ,(when (memq fun
  8250. '(org-insert-heading
  8251. org-insert-heading-respect-content
  8252. org-meta-return))
  8253. '(when orgstruct-is-++
  8254. 'item-body))))))))
  8255. (if fallback
  8256. (let* ((orgstruct-mode)
  8257. (binding
  8258. (let ((key ,key))
  8259. (catch 'exit
  8260. (dolist
  8261. (rep
  8262. '(nil
  8263. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8264. ("<\\([^>]*\\)return>" . "\\1RET")
  8265. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8266. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8267. nil)
  8268. (when rep
  8269. (setq key (read-kbd-macro
  8270. (let ((case-fold-search))
  8271. (replace-regexp-in-string
  8272. (car rep)
  8273. (cdr rep)
  8274. (key-description key))))))
  8275. (when (key-binding key)
  8276. (throw 'exit (key-binding key))))))))
  8277. (if (keymapp binding)
  8278. (org-set-transient-map binding)
  8279. (let ((func (or binding
  8280. (unless disable
  8281. 'orgstruct-error))))
  8282. (when func
  8283. (call-interactively func)))))
  8284. (org-run-like-in-org-mode
  8285. (lambda ()
  8286. (interactive)
  8287. (let* ,bindings
  8288. (call-interactively ',fun)))))))))
  8289. name))
  8290. (defun org-contextualize-keys (alist contexts)
  8291. "Return valid elements in ALIST depending on CONTEXTS.
  8292. `org-agenda-custom-commands' or `org-capture-templates' are the
  8293. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8294. or `org-capture-templates-contexts' are the associated contexts
  8295. definitions."
  8296. (let ((contexts
  8297. ;; normalize contexts
  8298. (mapcar
  8299. (lambda(c) (cond ((listp (cadr c))
  8300. (list (car c) (car c) (nth 1 c)))
  8301. ((string= "" (cadr c))
  8302. (list (car c) (car c) (nth 2 c)))
  8303. (t c)))
  8304. contexts))
  8305. (a alist) r s)
  8306. ;; loop over all commands or templates
  8307. (dolist (c a)
  8308. (let (vrules repl)
  8309. (cond
  8310. ((not (assoc (car c) contexts))
  8311. (push c r))
  8312. ((and (assoc (car c) contexts)
  8313. (setq vrules (org-contextualize-validate-key
  8314. (car c) contexts)))
  8315. (mapc (lambda (vr)
  8316. (unless (equal (car vr) (cadr vr))
  8317. (setq repl vr)))
  8318. vrules)
  8319. (if (not repl) (push c r)
  8320. (push (cadr repl) s)
  8321. (push
  8322. (cons (car c)
  8323. (cdr (or (assoc (cadr repl) alist)
  8324. (error "Undefined key `%s' as contextual replacement for `%s'"
  8325. (cadr repl) (car c)))))
  8326. r))))))
  8327. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8328. (delq
  8329. nil
  8330. (delete-dups
  8331. (mapcar (lambda (x)
  8332. (let ((tpl (car x)))
  8333. (unless (delq
  8334. nil
  8335. (mapcar (lambda (y)
  8336. (equal y tpl))
  8337. s))
  8338. x)))
  8339. (reverse r))))))
  8340. (defun org-contextualize-validate-key (key contexts)
  8341. "Check CONTEXTS for agenda or capture KEY."
  8342. (let (res)
  8343. (dolist (r contexts)
  8344. (dolist (rr (car (last r)))
  8345. (when
  8346. (and (equal key (car r))
  8347. (if (functionp rr) (funcall rr)
  8348. (or (and (eq (car rr) 'in-file)
  8349. (buffer-file-name)
  8350. (string-match (cdr rr) (buffer-file-name)))
  8351. (and (eq (car rr) 'in-mode)
  8352. (string-match (cdr rr) (symbol-name major-mode)))
  8353. (and (eq (car rr) 'in-buffer)
  8354. (string-match (cdr rr) (buffer-name)))
  8355. (when (and (eq (car rr) 'not-in-file)
  8356. (buffer-file-name))
  8357. (not (string-match (cdr rr) (buffer-file-name))))
  8358. (when (eq (car rr) 'not-in-mode)
  8359. (not (string-match (cdr rr) (symbol-name major-mode))))
  8360. (when (eq (car rr) 'not-in-buffer)
  8361. (not (string-match (cdr rr) (buffer-name)))))))
  8362. (push r res))))
  8363. (delete-dups (delq nil res))))
  8364. (defun org-context-p (&rest contexts)
  8365. "Check if local context is any of CONTEXTS.
  8366. Possible values in the list of contexts are `table', `headline', and `item'."
  8367. (let ((pos (point)))
  8368. (goto-char (point-at-bol))
  8369. (prog1 (or (and (memq 'table contexts)
  8370. (looking-at "[ \t]*|"))
  8371. (and (memq 'headline contexts)
  8372. (looking-at org-outline-regexp))
  8373. (and (memq 'item contexts)
  8374. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8375. (and (memq 'item-body contexts)
  8376. (org-in-item-p)))
  8377. (goto-char pos))))
  8378. (defconst org-unique-local-variables
  8379. '(org-element--cache
  8380. org-element--cache-objects
  8381. org-element--cache-sync-keys
  8382. org-element--cache-sync-requests
  8383. org-element--cache-sync-timer)
  8384. "List of local variables that cannot be transferred to another buffer.")
  8385. (defun org-get-local-variables ()
  8386. "Return a list of all local variables in an Org mode buffer."
  8387. (delq nil
  8388. (mapcar
  8389. (lambda (x)
  8390. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8391. (name (car binding)))
  8392. (and (not (get name 'org-state))
  8393. (not (memq name org-unique-local-variables))
  8394. (string-match-p
  8395. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8396. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8397. (symbol-name name))
  8398. binding)))
  8399. (with-temp-buffer
  8400. (org-mode)
  8401. (buffer-local-variables)))))
  8402. (defun org-clone-local-variables (from-buffer &optional regexp)
  8403. "Clone local variables from FROM-BUFFER.
  8404. Optional argument REGEXP selects variables to clone."
  8405. (dolist (pair (buffer-local-variables from-buffer))
  8406. (let ((name (car pair)))
  8407. (when (and (symbolp name)
  8408. (not (memq name org-unique-local-variables))
  8409. (or (null regexp) (string-match regexp (symbol-name name))))
  8410. (set (make-local-variable name) (cdr pair))))))
  8411. ;;;###autoload
  8412. (defun org-run-like-in-org-mode (cmd)
  8413. "Run a command, pretending that the current buffer is in Org-mode.
  8414. This will temporarily bind local variables that are typically bound in
  8415. Org-mode to the values they have in Org-mode, and then interactively
  8416. call CMD."
  8417. (org-load-modules-maybe)
  8418. (unless org-local-vars
  8419. (setq org-local-vars (org-get-local-variables)))
  8420. (let (binds)
  8421. (dolist (var org-local-vars)
  8422. (when (or (not (boundp (car var)))
  8423. (eq (symbol-value (car var))
  8424. (default-value (car var))))
  8425. (push (list (car var) `(quote ,(cadr var))) binds)))
  8426. (eval `(let ,binds
  8427. (call-interactively (quote ,cmd))))))
  8428. (defun org-get-category (&optional pos force-refresh)
  8429. "Get the category applying to position POS."
  8430. (save-match-data
  8431. (when force-refresh (org-refresh-category-properties))
  8432. (let ((pos (or pos (point))))
  8433. (or (get-text-property pos 'org-category)
  8434. (progn (org-refresh-category-properties)
  8435. (get-text-property pos 'org-category))))))
  8436. ;;; Refresh properties
  8437. (defun org-refresh-properties (dprop tprop)
  8438. "Refresh buffer text properties.
  8439. DPROP is the drawer property and TPROP is either the
  8440. corresponding text property to set, or an alist with each element
  8441. being a text property (as a symbol) and a function to apply to
  8442. the value of the drawer property."
  8443. (let ((case-fold-search t)
  8444. (inhibit-read-only t))
  8445. (org-with-silent-modifications
  8446. (save-excursion
  8447. (save-restriction
  8448. (widen)
  8449. (goto-char (point-min))
  8450. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8451. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8452. (defun org-refresh-property (tprop p)
  8453. "Refresh the buffer text property TPROP from the drawer property P.
  8454. The refresh happens only for the current tree (not subtree)."
  8455. (unless (org-before-first-heading-p)
  8456. (save-excursion
  8457. (org-back-to-heading t)
  8458. (if (symbolp tprop)
  8459. ;; TPROP is a text property symbol
  8460. (put-text-property
  8461. (point) (or (outline-next-heading) (point-max)) tprop p)
  8462. ;; TPROP is an alist with (properties . function) elements
  8463. (dolist (al tprop)
  8464. (save-excursion
  8465. (put-text-property
  8466. (line-beginning-position) (or (outline-next-heading) (point-max))
  8467. (car al)
  8468. (funcall (cdr al) p))))))))
  8469. (defun org-refresh-category-properties ()
  8470. "Refresh category text properties in the buffer."
  8471. (let ((case-fold-search t)
  8472. (inhibit-read-only t)
  8473. (default-category
  8474. (cond ((null org-category)
  8475. (if buffer-file-name
  8476. (file-name-sans-extension
  8477. (file-name-nondirectory buffer-file-name))
  8478. "???"))
  8479. ((symbolp org-category) (symbol-name org-category))
  8480. (t org-category))))
  8481. (org-with-silent-modifications
  8482. (org-with-wide-buffer
  8483. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8484. ;; This is the default category for the buffer. If none is
  8485. ;; found, fall-back to `org-category' or buffer file name.
  8486. (put-text-property
  8487. (point-min) (point-max)
  8488. 'org-category
  8489. (catch 'buffer-category
  8490. (goto-char (point-max))
  8491. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8492. (let ((element (org-element-at-point)))
  8493. (when (eq (org-element-type element) 'keyword)
  8494. (throw 'buffer-category
  8495. (org-element-property :value element)))))
  8496. default-category))
  8497. ;; Set sub-tree specific categories.
  8498. (goto-char (point-min))
  8499. (let ((regexp (org-re-property "CATEGORY")))
  8500. (while (re-search-forward regexp nil t)
  8501. (let ((value (org-match-string-no-properties 3)))
  8502. (when (org-at-property-p)
  8503. (put-text-property
  8504. (save-excursion (org-back-to-heading t) (point))
  8505. (save-excursion (org-end-of-subtree t t) (point))
  8506. 'org-category
  8507. value)))))))))
  8508. (defun org-refresh-stats-properties ()
  8509. "Refresh stats text properties in the buffer."
  8510. (let (stats)
  8511. (org-with-silent-modifications
  8512. (save-excursion
  8513. (save-restriction
  8514. (widen)
  8515. (goto-char (point-min))
  8516. (while (re-search-forward
  8517. (concat org-outline-regexp-bol ".*"
  8518. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8519. nil t)
  8520. (setq stats (cond ((equal (match-string 3) "0") 0)
  8521. ((match-string 2)
  8522. (/ (* (string-to-number (match-string 2)) 100)
  8523. (string-to-number (match-string 3))))
  8524. (t (string-to-number (match-string 1)))))
  8525. (org-back-to-heading t)
  8526. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8527. 'org-stats stats)))))))
  8528. (defun org-refresh-effort-properties ()
  8529. "Refresh effort properties"
  8530. (org-refresh-properties
  8531. org-effort-property
  8532. '((effort . identity)
  8533. (effort-minutes . org-duration-string-to-minutes))))
  8534. ;;;; Link Stuff
  8535. ;;; Link abbreviations
  8536. (defun org-link-expand-abbrev (link)
  8537. "Apply replacements as defined in `org-link-abbrev-alist'."
  8538. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8539. (let* ((key (match-string 1 link))
  8540. (as (or (assoc key org-link-abbrev-alist-local)
  8541. (assoc key org-link-abbrev-alist)))
  8542. (tag (and (match-end 2) (match-string 3 link)))
  8543. rpl)
  8544. (if (not as)
  8545. link
  8546. (setq rpl (cdr as))
  8547. (cond
  8548. ((symbolp rpl) (funcall rpl tag))
  8549. ((string-match "%(\\([^)]+\\))" rpl)
  8550. (replace-match
  8551. (save-match-data
  8552. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8553. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8554. ((string-match "%h" rpl)
  8555. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8556. (t (concat rpl tag)))))
  8557. link))
  8558. ;;; Storing and inserting links
  8559. (defvar org-insert-link-history nil
  8560. "Minibuffer history for links inserted with `org-insert-link'.")
  8561. (defvar org-stored-links nil
  8562. "Contains the links stored with `org-store-link'.")
  8563. (defvar org-store-link-plist nil
  8564. "Plist with info about the most recently link created with `org-store-link'.")
  8565. (defvar org-link-protocols nil
  8566. "Link protocols added to Org-mode using `org-add-link-type'.")
  8567. (defvar org-store-link-functions nil
  8568. "List of functions that are called to create and store a link.
  8569. Each function will be called in turn until one returns a non-nil
  8570. value. Each function should check if it is responsible for creating
  8571. this link (for example by looking at the major mode).
  8572. If not, it must exit and return nil.
  8573. If yes, it should return a non-nil value after a calling
  8574. `org-store-link-props' with a list of properties and values.
  8575. Special properties are:
  8576. :type The link prefix, like \"http\". This must be given.
  8577. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8578. This is obligatory as well.
  8579. :description Optional default description for the second pair
  8580. of brackets in an Org-mode link. The user can still change
  8581. this when inserting this link into an Org-mode buffer.
  8582. In addition to these, any additional properties can be specified
  8583. and then used in capture templates.")
  8584. (defun org-add-link-type (type &optional follow export)
  8585. "Add TYPE to the list of `org-link-types'.
  8586. Re-compute all regular expressions depending on `org-link-types'
  8587. FOLLOW and EXPORT are two functions.
  8588. FOLLOW should take the link path as the single argument and do whatever
  8589. is necessary to follow the link, for example find a file or display
  8590. a mail message.
  8591. EXPORT should format the link path for export to one of the export formats.
  8592. It should be a function accepting three arguments:
  8593. path the path of the link, the text after the prefix (like \"http:\")
  8594. desc the description of the link, if any
  8595. format the export format, a symbol like `html' or `latex' or `ascii'.
  8596. The function may use the FORMAT information to return different values
  8597. depending on the format. The return value will be put literally into
  8598. the exported file. If the return value is nil, this means Org should
  8599. do what it normally does with links which do not have EXPORT defined.
  8600. Org mode has a built-in default for exporting links. If you are happy with
  8601. this default, there is no need to define an export function for the link
  8602. type. For a simple example of an export function, see `org-bbdb.el'."
  8603. (add-to-list 'org-link-types type t)
  8604. (org-make-link-regexps)
  8605. (org-element-update-syntax)
  8606. (if (assoc type org-link-protocols)
  8607. (setcdr (assoc type org-link-protocols) (list follow export))
  8608. (push (list type follow export) org-link-protocols)))
  8609. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8610. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8611. ;;;###autoload
  8612. (defun org-store-link (arg)
  8613. "\\<org-mode-map>Store an org-link to the current location.
  8614. This link is added to `org-stored-links' and can later be inserted
  8615. into an Org buffer with \\[org-insert-link].
  8616. For some link types, a prefix ARG is interpreted.
  8617. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8618. For file links, ARG negates `org-context-in-file-links'.
  8619. A double prefix ARG force skipping storing functions that are not
  8620. part of Org's core.
  8621. A triple prefix ARG force storing a link for each line in the
  8622. active region."
  8623. (interactive "P")
  8624. (org-load-modules-maybe)
  8625. (if (and (equal arg '(64)) (org-region-active-p))
  8626. (save-excursion
  8627. (let ((end (region-end)))
  8628. (goto-char (region-beginning))
  8629. (set-mark (point))
  8630. (while (< (point-at-eol) end)
  8631. (move-end-of-line 1) (activate-mark)
  8632. (let (current-prefix-arg)
  8633. (call-interactively 'org-store-link))
  8634. (move-beginning-of-line 2)
  8635. (set-mark (point)))))
  8636. (org-with-limited-levels
  8637. (setq org-store-link-plist nil)
  8638. (let (link cpltxt desc description search
  8639. txt custom-id agenda-link sfuns sfunsn)
  8640. (cond
  8641. ;; Store a link using an external link type
  8642. ((and (not (equal arg '(16)))
  8643. (setq sfuns
  8644. (delq
  8645. nil (mapcar (lambda (f)
  8646. (let (fs) (if (funcall f) (push f fs))))
  8647. org-store-link-functions))
  8648. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8649. (or (and (cdr sfuns)
  8650. (funcall (intern
  8651. (completing-read
  8652. "Which function for creating the link? "
  8653. sfunsn nil t (car sfunsn)))))
  8654. (funcall (caar sfuns)))
  8655. (setq link (plist-get org-store-link-plist :link)
  8656. desc (or (plist-get org-store-link-plist
  8657. :description)
  8658. link))))
  8659. ;; Store a link from a source code buffer.
  8660. ((org-src-edit-buffer-p)
  8661. (cond
  8662. ((save-excursion
  8663. (beginning-of-line)
  8664. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8665. "[ \t]*$")))
  8666. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8667. ((org-called-interactively-p 'any)
  8668. (let (label)
  8669. (while (or (not label)
  8670. (org-with-wide-buffer
  8671. (goto-char (point-min))
  8672. (re-search-forward
  8673. (regexp-quote (format org-coderef-label-format label))
  8674. nil t)))
  8675. (when label (message "Label exists already") (sit-for 2))
  8676. (setq label (read-string "Code line label: " label)))
  8677. (end-of-line)
  8678. (setq link (format org-coderef-label-format label))
  8679. (let ((gc (- 79 (length link))))
  8680. (if (< (current-column) gc) (org-move-to-column gc t)
  8681. (insert " ")))
  8682. (insert link)
  8683. (setq link (concat "(" label ")") desc nil)))
  8684. (t (setq link nil))))
  8685. ;; We are in the agenda, link to referenced location
  8686. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8687. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8688. (get-text-property (point) 'org-marker))))
  8689. (when m
  8690. (org-with-point-at m
  8691. (setq agenda-link
  8692. (if (org-called-interactively-p 'any)
  8693. (call-interactively 'org-store-link)
  8694. (org-store-link nil)))))))
  8695. ((eq major-mode 'calendar-mode)
  8696. (let ((cd (calendar-cursor-to-date)))
  8697. (setq link
  8698. (format-time-string
  8699. (car org-time-stamp-formats)
  8700. (apply 'encode-time
  8701. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8702. nil nil nil))))
  8703. (org-store-link-props :type "calendar" :date cd)))
  8704. ((eq major-mode 'help-mode)
  8705. (setq link (concat "help:" (save-excursion
  8706. (goto-char (point-min))
  8707. (looking-at "^[^ ]+")
  8708. (match-string 0))))
  8709. (org-store-link-props :type "help"))
  8710. ((eq major-mode 'w3-mode)
  8711. (setq cpltxt (if (and (buffer-name)
  8712. (not (string-match "Untitled" (buffer-name))))
  8713. (buffer-name)
  8714. (url-view-url t))
  8715. link (url-view-url t))
  8716. (org-store-link-props :type "w3" :url (url-view-url t)))
  8717. ((eq major-mode 'image-mode)
  8718. (setq cpltxt (concat "file:"
  8719. (abbreviate-file-name buffer-file-name))
  8720. link cpltxt)
  8721. (org-store-link-props :type "image" :file buffer-file-name))
  8722. ;; In dired, store a link to the file of the current line
  8723. ((derived-mode-p 'dired-mode)
  8724. (let ((file (dired-get-filename nil t)))
  8725. (setq file (if file
  8726. (abbreviate-file-name
  8727. (expand-file-name (dired-get-filename nil t)))
  8728. ;; otherwise, no file so use current directory.
  8729. default-directory))
  8730. (setq cpltxt (concat "file:" file)
  8731. link cpltxt)))
  8732. ((setq search (run-hook-with-args-until-success
  8733. 'org-create-file-search-functions))
  8734. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8735. "::" search))
  8736. (setq cpltxt (or description link)))
  8737. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8738. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8739. (cond
  8740. ;; Store a link using the target at point
  8741. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8742. (setq cpltxt
  8743. (concat "file:"
  8744. (abbreviate-file-name
  8745. (buffer-file-name (buffer-base-buffer)))
  8746. "::" (match-string 1))
  8747. link cpltxt))
  8748. ((and (featurep 'org-id)
  8749. (or (eq org-id-link-to-org-use-id t)
  8750. (and (org-called-interactively-p 'any)
  8751. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8752. (and (eq org-id-link-to-org-use-id
  8753. 'create-if-interactive-and-no-custom-id)
  8754. (not custom-id))))
  8755. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8756. ;; Store a link using the ID at point
  8757. (setq link (condition-case nil
  8758. (prog1 (org-id-store-link)
  8759. (setq desc (or (plist-get org-store-link-plist
  8760. :description)
  8761. "")))
  8762. (error
  8763. ;; Probably before first headline, link only to file
  8764. (concat "file:"
  8765. (abbreviate-file-name
  8766. (buffer-file-name (buffer-base-buffer))))))))
  8767. (t
  8768. ;; Just link to current headline
  8769. (setq cpltxt (concat "file:"
  8770. (abbreviate-file-name
  8771. (buffer-file-name (buffer-base-buffer)))))
  8772. ;; Add a context search string
  8773. (when (org-xor org-context-in-file-links arg)
  8774. (let* ((ee (org-element-at-point))
  8775. (et (org-element-type ee))
  8776. (ev (plist-get (cadr ee) :value))
  8777. (ek (plist-get (cadr ee) :key))
  8778. (eok (and (stringp ek) (string-match "name" ek))))
  8779. (setq txt (cond
  8780. ((org-at-heading-p) nil)
  8781. ((and (eq et 'keyword) eok) ev)
  8782. ((org-region-active-p)
  8783. (buffer-substring (region-beginning) (region-end)))))
  8784. (when (or (null txt) (string-match "\\S-" txt))
  8785. (setq cpltxt
  8786. (concat cpltxt "::"
  8787. (condition-case nil
  8788. (org-make-org-heading-search-string txt)
  8789. (error "")))
  8790. desc (or (and (eq et 'keyword) eok ev)
  8791. (nth 4 (ignore-errors (org-heading-components)))
  8792. "NONE")))))
  8793. (when (string-match "::\\'" cpltxt)
  8794. (setq cpltxt (substring cpltxt 0 -2)))
  8795. (setq link cpltxt))))
  8796. ((buffer-file-name (buffer-base-buffer))
  8797. ;; Just link to this file here.
  8798. (setq cpltxt (concat "file:"
  8799. (abbreviate-file-name
  8800. (buffer-file-name (buffer-base-buffer)))))
  8801. ;; Add a context string.
  8802. (when (org-xor org-context-in-file-links arg)
  8803. (setq txt (if (org-region-active-p)
  8804. (buffer-substring (region-beginning) (region-end))
  8805. (buffer-substring (point-at-bol) (point-at-eol))))
  8806. ;; Only use search option if there is some text.
  8807. (when (string-match "\\S-" txt)
  8808. (setq cpltxt
  8809. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8810. desc "NONE")))
  8811. (setq link cpltxt))
  8812. ((org-called-interactively-p 'interactive)
  8813. (user-error "No method for storing a link from this buffer"))
  8814. (t (setq link nil)))
  8815. ;; We're done setting link and desc, clean up
  8816. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8817. (setq link (or link cpltxt)
  8818. desc (or desc cpltxt))
  8819. (cond ((equal desc "NONE") (setq desc nil))
  8820. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8821. (let ((d0 (match-string 3 desc))
  8822. (p0 (match-string 5 desc)))
  8823. (setq desc
  8824. (replace-regexp-in-string
  8825. org-bracket-link-regexp
  8826. (concat (or p0 d0)
  8827. (if (equal (length (match-string 0 desc))
  8828. (length desc)) "*" "")) desc)))))
  8829. ;; Return the link
  8830. (if (not (and (or (org-called-interactively-p 'any)
  8831. executing-kbd-macro)
  8832. link))
  8833. (or agenda-link (and link (org-make-link-string link desc)))
  8834. (push (list link desc) org-stored-links)
  8835. (message "Stored: %s" (or desc link))
  8836. (when custom-id
  8837. (setq link (concat "file:" (abbreviate-file-name
  8838. (buffer-file-name)) "::#" custom-id))
  8839. (push (list link desc) org-stored-links))
  8840. (car org-stored-links))))))
  8841. (defun org-store-link-props (&rest plist)
  8842. "Store link properties, extract names, addresses and dates."
  8843. (let ((x (plist-get plist :from)))
  8844. (when x
  8845. (let ((adr (mail-extract-address-components x)))
  8846. (setq plist (plist-put plist :fromname (car adr)))
  8847. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8848. (let ((x (plist-get plist :to)))
  8849. (when x
  8850. (let ((adr (mail-extract-address-components x)))
  8851. (setq plist (plist-put plist :toname (car adr)))
  8852. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8853. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8854. (when x
  8855. (setq plist (plist-put plist :date-timestamp
  8856. (format-time-string
  8857. (org-time-stamp-format t) x)))
  8858. (setq plist (plist-put plist :date-timestamp-inactive
  8859. (format-time-string
  8860. (org-time-stamp-format t t) x)))))
  8861. (let ((from (plist-get plist :from))
  8862. (to (plist-get plist :to)))
  8863. (when (and from to org-from-is-user-regexp)
  8864. (setq plist
  8865. (plist-put plist :fromto
  8866. (if (string-match org-from-is-user-regexp from)
  8867. (concat "to %t")
  8868. (concat "from %f"))))))
  8869. (setq org-store-link-plist plist))
  8870. (defun org-add-link-props (&rest plist)
  8871. "Add these properties to the link property list."
  8872. (let (key value)
  8873. (while plist
  8874. (setq key (pop plist) value (pop plist))
  8875. (setq org-store-link-plist
  8876. (plist-put org-store-link-plist key value)))))
  8877. (defun org-email-link-description (&optional fmt)
  8878. "Return the description part of an email link.
  8879. This takes information from `org-store-link-plist' and formats it
  8880. according to FMT (default from `org-email-link-description-format')."
  8881. (setq fmt (or fmt org-email-link-description-format))
  8882. (let* ((p org-store-link-plist)
  8883. (to (plist-get p :toaddress))
  8884. (from (plist-get p :fromaddress))
  8885. (table
  8886. (list
  8887. (cons "%c" (plist-get p :fromto))
  8888. (cons "%F" (plist-get p :from))
  8889. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8890. (cons "%T" (plist-get p :to))
  8891. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8892. (cons "%s" (plist-get p :subject))
  8893. (cons "%d" (plist-get p :date))
  8894. (cons "%m" (plist-get p :message-id)))))
  8895. (when (string-match "%c" fmt)
  8896. ;; Check if the user wrote this message
  8897. (if (and org-from-is-user-regexp from to
  8898. (save-match-data (string-match org-from-is-user-regexp from)))
  8899. (setq fmt (replace-match "to %t" t t fmt))
  8900. (setq fmt (replace-match "from %f" t t fmt))))
  8901. (org-replace-escapes fmt table)))
  8902. (defun org-make-org-heading-search-string (&optional string)
  8903. "Make search string for the current headline or STRING."
  8904. (let ((s (or string
  8905. (and (derived-mode-p 'org-mode)
  8906. (save-excursion
  8907. (org-back-to-heading t)
  8908. (org-element-property :raw-value (org-element-at-point))))))
  8909. (lines org-context-in-file-links))
  8910. (or string (setq s (concat "*" s))) ; Add * for headlines
  8911. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8912. (when (and string (integerp lines) (> lines 0))
  8913. (let ((slines (org-split-string s "\n")))
  8914. (when (< lines (length slines))
  8915. (setq s (mapconcat
  8916. 'identity
  8917. (reverse (nthcdr (- (length slines) lines)
  8918. (reverse slines))) "\n")))))
  8919. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8920. (defun org-make-link-string (link &optional description)
  8921. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8922. (unless (org-string-nw-p link) (error "Empty link"))
  8923. (let ((uri (cond ((string-match org-link-types-re link)
  8924. (concat (match-string 1 link)
  8925. (org-link-escape (substring link (match-end 1)))))
  8926. ;; For readability, url-encode internal links only
  8927. ;; when absolutely needed (i.e, when they contain
  8928. ;; square brackets). File links however, are
  8929. ;; encoded since, e.g., spaces are significant.
  8930. ((or (file-name-absolute-p link)
  8931. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8932. (org-link-escape link))
  8933. (t link)))
  8934. (description
  8935. (and (org-string-nw-p description)
  8936. ;; Remove brackets from description, as they are fatal.
  8937. (replace-regexp-in-string
  8938. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8939. (org-trim description)))))
  8940. (format "[[%s]%s]"
  8941. uri
  8942. (if description (format "[%s]" description) ""))))
  8943. (defconst org-link-escape-chars
  8944. ;;%20 %5B %5D %25
  8945. '(?\s ?\[ ?\] ?%)
  8946. "List of characters that should be escaped in a link when stored to Org.
  8947. This is the list that is used for internal purposes.")
  8948. (defconst org-link-escape-chars-browser
  8949. ;;%20 %22
  8950. '(?\s ?\")
  8951. "List of characters to be escaped before handing over to the browser.
  8952. If you consider using this constant then you probably want to use
  8953. the function `org-link-escape-browser' instead. See there why
  8954. this constant is a candidate to be removed once Org drops support
  8955. for Emacs 24.1 and 24.2.")
  8956. (defun org-link-escape (text &optional table merge)
  8957. "Return percent escaped representation of TEXT.
  8958. TEXT is a string with the text to escape.
  8959. Optional argument TABLE is a list with characters that should be
  8960. escaped. When nil, `org-link-escape-chars' is used.
  8961. If optional argument MERGE is set, merge TABLE into
  8962. `org-link-escape-chars'."
  8963. (let ((characters-to-encode
  8964. (cond ((null table) org-link-escape-chars)
  8965. (merge (append org-link-escape-chars table))
  8966. (t table))))
  8967. (mapconcat
  8968. (lambda (c)
  8969. (if (or (memq c characters-to-encode)
  8970. (and org-url-hexify-p (or (< c 32) (> c 126))))
  8971. (mapconcat (lambda (e) (format "%%%.2X" e))
  8972. (or (encode-coding-char c 'utf-8)
  8973. (error "Unable to percent escape character: %c" c))
  8974. "")
  8975. (char-to-string c)))
  8976. text "")))
  8977. (defun org-link-escape-browser (text)
  8978. "Escape some characters before handing over to the browser.
  8979. This function is a candidate to be removed together with the
  8980. constant `org-link-escape-chars-browser' once Org drops support
  8981. for Emacs 24.1 and 24.2. All calls to this function will have to
  8982. be replaced with `url-encode-url' which is available since Emacs
  8983. 24.3.1."
  8984. ;; Example with the Org link
  8985. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  8986. ;; to open the browser with +subject:"Release 8.2" filled into the
  8987. ;; query field: In this case the variable TEXT contains the
  8988. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  8989. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  8990. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  8991. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  8992. (if (fboundp 'url-encode-url)
  8993. (url-encode-url text)
  8994. (if (org-string-match-p
  8995. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8996. text)
  8997. (org-link-escape text org-link-escape-chars-browser)
  8998. text)))
  8999. (defun org-link-unescape (str)
  9000. "Unhex hexified Unicode parts in string STR.
  9001. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9002. reciprocal of `org-link-escape', which see."
  9003. (if (org-string-nw-p str)
  9004. (replace-regexp-in-string
  9005. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9006. str))
  9007. (defun org-link-unescape-compound (hex)
  9008. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9009. Note: this function also decodes single byte encodings like
  9010. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9011. (save-match-data
  9012. (let* ((bytes (cdr (split-string hex "%")))
  9013. (ret "")
  9014. (eat 0)
  9015. (sum 0))
  9016. (while bytes
  9017. (let* ((val (string-to-number (pop bytes) 16))
  9018. (shift-xor
  9019. (if (= 0 eat)
  9020. (cond
  9021. ((>= val 252) (cons 6 252))
  9022. ((>= val 248) (cons 5 248))
  9023. ((>= val 240) (cons 4 240))
  9024. ((>= val 224) (cons 3 224))
  9025. ((>= val 192) (cons 2 192))
  9026. (t (cons 0 0)))
  9027. (cons 6 128))))
  9028. (when (>= val 192) (setq eat (car shift-xor)))
  9029. (setq val (logxor val (cdr shift-xor)))
  9030. (setq sum (+ (lsh sum (car shift-xor)) val))
  9031. (when (> eat 0) (setq eat (- eat 1)))
  9032. (cond
  9033. ((= 0 eat) ;multi byte
  9034. (setq ret (concat ret (org-char-to-string sum)))
  9035. (setq sum 0))
  9036. ((not bytes) ; single byte(s)
  9037. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9038. ret)))
  9039. (defun org-link-unescape-single-byte-sequence (hex)
  9040. "Unhexify hex-encoded single byte character sequences."
  9041. (mapconcat (lambda (byte)
  9042. (char-to-string (string-to-number byte 16)))
  9043. (cdr (split-string hex "%")) ""))
  9044. (defun org-xor (a b)
  9045. "Exclusive or."
  9046. (if a (not b) b))
  9047. (defun org-fixup-message-id-for-http (s)
  9048. "Replace special characters in a message id, so it can be used in an http query."
  9049. (when (string-match "%" s)
  9050. (setq s (mapconcat (lambda (c)
  9051. (if (eq c ?%)
  9052. "%25"
  9053. (char-to-string c)))
  9054. s "")))
  9055. (while (string-match "<" s)
  9056. (setq s (replace-match "%3C" t t s)))
  9057. (while (string-match ">" s)
  9058. (setq s (replace-match "%3E" t t s)))
  9059. (while (string-match "@" s)
  9060. (setq s (replace-match "%40" t t s)))
  9061. s)
  9062. (defun org-link-prettify (link)
  9063. "Return a human-readable representation of LINK.
  9064. The car of LINK must be a raw link.
  9065. The cdr of LINK must be either a link description or nil."
  9066. (let ((desc (or (cadr link) "<no description>")))
  9067. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9068. "<" (car link) ">")))
  9069. ;;;###autoload
  9070. (defun org-insert-link-global ()
  9071. "Insert a link like Org-mode does.
  9072. This command can be called in any mode to insert a link in Org-mode syntax."
  9073. (interactive)
  9074. (org-load-modules-maybe)
  9075. (org-run-like-in-org-mode 'org-insert-link))
  9076. (defun org-insert-all-links (arg &optional pre post)
  9077. "Insert all links in `org-stored-links'.
  9078. When a universal prefix, do not delete the links from `org-stored-links'.
  9079. When `ARG' is a number, insert the last N link(s).
  9080. `PRE' and `POST' are optional arguments to define a string to
  9081. prepend or to append."
  9082. (interactive "P")
  9083. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9084. (links (copy-sequence org-stored-links))
  9085. (pr (or pre "- "))
  9086. (po (or post "\n"))
  9087. (cnt 1) l)
  9088. (if (null org-stored-links)
  9089. (message "No link to insert")
  9090. (while (and (or (listp arg) (>= arg cnt))
  9091. (setq l (if (listp arg)
  9092. (pop links)
  9093. (pop org-stored-links))))
  9094. (setq cnt (1+ cnt))
  9095. (insert pr)
  9096. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9097. (insert po)))))
  9098. (defun org-insert-last-stored-link (arg)
  9099. "Insert the last link stored in `org-stored-links'."
  9100. (interactive "p")
  9101. (org-insert-all-links arg "" "\n"))
  9102. (defun org-link-fontify-links-to-this-file ()
  9103. "Fontify links to the current file in `org-stored-links'."
  9104. (let ((f (buffer-file-name)) a b)
  9105. (setq a (mapcar (lambda(l)
  9106. (let ((ll (car l)))
  9107. (when (and (string-match "^file:\\(.+\\)::" ll)
  9108. (equal f (expand-file-name (match-string 1 ll))))
  9109. ll)))
  9110. org-stored-links))
  9111. (when (featurep 'org-id)
  9112. (setq b (mapcar (lambda(l)
  9113. (let ((ll (car l)))
  9114. (when (and (string-match "^id:\\(.+\\)$" ll)
  9115. (equal f (expand-file-name
  9116. (or (org-id-find-id-file
  9117. (match-string 1 ll)) ""))))
  9118. ll)))
  9119. org-stored-links)))
  9120. (mapcar (lambda(l)
  9121. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9122. (delq nil (append a b)))))
  9123. (defvar org--links-history nil)
  9124. (defun org-insert-link (&optional complete-file link-location default-description)
  9125. "Insert a link. At the prompt, enter the link.
  9126. Completion can be used to insert any of the link protocol prefixes like
  9127. http or ftp in use.
  9128. The history can be used to select a link previously stored with
  9129. `org-store-link'. When the empty string is entered (i.e. if you just
  9130. press RET at the prompt), the link defaults to the most recently
  9131. stored link. As SPC triggers completion in the minibuffer, you need to
  9132. use M-SPC or C-q SPC to force the insertion of a space character.
  9133. You will also be prompted for a description, and if one is given, it will
  9134. be displayed in the buffer instead of the link.
  9135. If there is already a link at point, this command will allow you to edit link
  9136. and description parts.
  9137. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9138. be selected using completion. The path to the file will be relative to the
  9139. current directory if the file is in the current directory or a subdirectory.
  9140. Otherwise, the link will be the absolute path as completed in the minibuffer
  9141. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9142. option `org-link-file-path-type'.
  9143. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9144. the current directory or below.
  9145. With three \\[universal-argument] prefixes, negate the meaning of
  9146. `org-keep-stored-link-after-insertion'.
  9147. If `org-make-link-description-function' is non-nil, this function will be
  9148. called with the link target, and the result will be the default
  9149. link description.
  9150. If the LINK-LOCATION parameter is non-nil, this value will be
  9151. used as the link location instead of reading one interactively.
  9152. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9153. be used as the default description."
  9154. (interactive "P")
  9155. (let* ((wcf (current-window-configuration))
  9156. (origbuf (current-buffer))
  9157. (region (when (org-region-active-p)
  9158. (buffer-substring (region-beginning) (region-end))))
  9159. (remove (and region (list (region-beginning) (region-end))))
  9160. (desc region)
  9161. (link link-location)
  9162. (abbrevs org-link-abbrev-alist-local)
  9163. entry all-prefixes auto-desc)
  9164. (cond
  9165. (link-location) ; specified by arg, just use it.
  9166. ((org-in-regexp org-bracket-link-regexp 1)
  9167. ;; We do have a link at point, and we are going to edit it.
  9168. (setq remove (list (match-beginning 0) (match-end 0)))
  9169. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9170. (setq link (read-string "Link: "
  9171. (org-link-unescape
  9172. (org-match-string-no-properties 1)))))
  9173. ((or (org-in-regexp org-angle-link-re)
  9174. (org-in-regexp org-plain-link-re))
  9175. ;; Convert to bracket link
  9176. (setq remove (list (match-beginning 0) (match-end 0))
  9177. link (read-string "Link: "
  9178. (org-remove-angle-brackets (match-string 0)))))
  9179. ((member complete-file '((4) (16)))
  9180. ;; Completing read for file names.
  9181. (setq link (org-file-complete-link complete-file)))
  9182. (t
  9183. ;; Read link, with completion for stored links.
  9184. (org-link-fontify-links-to-this-file)
  9185. (org-switch-to-buffer-other-window "*Org Links*")
  9186. (with-current-buffer "*Org Links*"
  9187. (erase-buffer)
  9188. (insert "Insert a link.
  9189. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9190. (when org-stored-links
  9191. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9192. (insert (mapconcat 'org-link-prettify
  9193. (reverse org-stored-links) "\n")))
  9194. (goto-char (point-min)))
  9195. (let ((cw (selected-window)))
  9196. (select-window (get-buffer-window "*Org Links*" 'visible))
  9197. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9198. (unless (pos-visible-in-window-p (point-max))
  9199. (org-fit-window-to-buffer))
  9200. (and (window-live-p cw) (select-window cw)))
  9201. (setq all-prefixes (append (mapcar 'car abbrevs)
  9202. (mapcar 'car org-link-abbrev-alist)
  9203. org-link-types))
  9204. (unwind-protect
  9205. ;; Fake a link history, containing the stored links.
  9206. (let ((org--links-history
  9207. (append (mapcar #'car org-stored-links)
  9208. org-insert-link-history)))
  9209. (setq link
  9210. (org-completing-read
  9211. "Link: "
  9212. (append
  9213. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9214. (mapcar #'car org-stored-links))
  9215. nil nil nil
  9216. 'org--links-history
  9217. (caar org-stored-links)))
  9218. (unless (org-string-nw-p link) (user-error "No link selected"))
  9219. (dolist (l org-stored-links)
  9220. (when (equal link (cadr l))
  9221. (setq link (car l))
  9222. (setq auto-desc t)))
  9223. (when (or (member link all-prefixes)
  9224. (and (equal ":" (substring link -1))
  9225. (member (substring link 0 -1) all-prefixes)
  9226. (setq link (substring link 0 -1))))
  9227. (setq link (with-current-buffer origbuf
  9228. (org-link-try-special-completion link)))))
  9229. (set-window-configuration wcf)
  9230. (kill-buffer "*Org Links*"))
  9231. (setq entry (assoc link org-stored-links))
  9232. (or entry (push link org-insert-link-history))
  9233. (setq desc (or desc (nth 1 entry)))))
  9234. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9235. (not org-keep-stored-link-after-insertion))
  9236. (setq org-stored-links (delq (assoc link org-stored-links)
  9237. org-stored-links)))
  9238. (when (and (string-match org-plain-link-re link)
  9239. (not (string-match org-ts-regexp link)))
  9240. ;; URL-like link, normalize the use of angular brackets.
  9241. (setq link (org-remove-angle-brackets link)))
  9242. ;; Check if we are linking to the current file with a search
  9243. ;; option If yes, simplify the link by using only the search
  9244. ;; option.
  9245. (when (and buffer-file-name
  9246. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9247. (let* ((path (match-string 1 link))
  9248. (case-fold-search nil)
  9249. (search (match-string 2 link)))
  9250. (save-match-data
  9251. (when (equal (file-truename buffer-file-name) (file-truename path))
  9252. ;; We are linking to this same file, with a search option
  9253. (setq link search)))))
  9254. ;; Check if we can/should use a relative path. If yes, simplify the link
  9255. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9256. (let* ((type (match-string 1 link))
  9257. (path (match-string 2 link))
  9258. (origpath path)
  9259. (case-fold-search nil))
  9260. (cond
  9261. ((or (eq org-link-file-path-type 'absolute)
  9262. (equal complete-file '(16)))
  9263. (setq path (abbreviate-file-name (expand-file-name path))))
  9264. ((eq org-link-file-path-type 'noabbrev)
  9265. (setq path (expand-file-name path)))
  9266. ((eq org-link-file-path-type 'relative)
  9267. (setq path (file-relative-name path)))
  9268. (t
  9269. (save-match-data
  9270. (if (string-match (concat "^" (regexp-quote
  9271. (expand-file-name
  9272. (file-name-as-directory
  9273. default-directory))))
  9274. (expand-file-name path))
  9275. ;; We are linking a file with relative path name.
  9276. (setq path (substring (expand-file-name path)
  9277. (match-end 0)))
  9278. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9279. (setq link (concat type path))
  9280. (when (equal desc origpath)
  9281. (setq desc path))))
  9282. (if org-make-link-description-function
  9283. (setq desc
  9284. (or (condition-case nil
  9285. (funcall org-make-link-description-function link desc)
  9286. (error (progn (message "Can't get link description from `%s'"
  9287. (symbol-name org-make-link-description-function))
  9288. (sit-for 2) nil)))
  9289. (read-string "Description: " default-description)))
  9290. (if default-description (setq desc default-description)
  9291. (setq desc (or (and auto-desc desc)
  9292. (read-string "Description: " desc)))))
  9293. (unless (string-match "\\S-" desc) (setq desc nil))
  9294. (when remove (apply 'delete-region remove))
  9295. (insert (org-make-link-string link desc))
  9296. ;; Redisplay so as the new link has proper invisible characters.
  9297. (sit-for 0)))
  9298. (defun org-link-try-special-completion (type)
  9299. "If there is completion support for link type TYPE, offer it."
  9300. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9301. (if (functionp fun)
  9302. (funcall fun)
  9303. (read-string "Link (no completion support): " (concat type ":")))))
  9304. (defun org-file-complete-link (&optional arg)
  9305. "Create a file link using completion."
  9306. (let ((file (read-file-name "File: "))
  9307. (pwd (file-name-as-directory (expand-file-name ".")))
  9308. (pwd1 (file-name-as-directory (abbreviate-file-name
  9309. (expand-file-name ".")))))
  9310. (cond ((equal arg '(16))
  9311. (concat "file:"
  9312. (abbreviate-file-name (expand-file-name file))))
  9313. ((string-match
  9314. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9315. (concat "file:" (match-string 1 file)))
  9316. ((string-match
  9317. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9318. (expand-file-name file))
  9319. (concat "file:"
  9320. (match-string 1 (expand-file-name file))))
  9321. (t (concat "file:" file)))))
  9322. (defun org-completing-read (&rest args)
  9323. "Completing-read with SPACE being a normal character."
  9324. (let ((enable-recursive-minibuffers t)
  9325. (minibuffer-local-completion-map
  9326. (copy-keymap minibuffer-local-completion-map)))
  9327. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9328. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9329. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9330. 'org-time-stamp-inactive)
  9331. (apply #'completing-read args)))
  9332. (define-obsolete-function-alias
  9333. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9334. (define-obsolete-function-alias
  9335. 'org-icompleting-read 'completing-read "Org 9.0")
  9336. (define-obsolete-function-alias
  9337. 'org-iread-file-name 'read-file-name "Org 9.0")
  9338. ;;; Opening/following a link
  9339. (defvar org-link-search-failed nil)
  9340. (defvar org-open-link-functions nil
  9341. "Hook for functions finding a plain text link.
  9342. These functions must take a single argument, the link content.
  9343. They will be called for links that look like [[link text][description]]
  9344. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9345. like a filename (e.g. \"./blue.png\").
  9346. These functions will be called *before* Org attempts to resolve the
  9347. link by doing text searches in the current buffer - so if you want a
  9348. link \"[[target]]\" to still find \"<<target>>\", your function should
  9349. handle this as a special case.
  9350. When the function does handle the link, it must return a non-nil value.
  9351. If it decides that it is not responsible for this link, it must return
  9352. nil to indicate that that Org-mode can continue with other options
  9353. like exact and fuzzy text search.")
  9354. (defun org-next-link (&optional search-backward)
  9355. "Move forward to the next link.
  9356. If the link is in hidden text, expose it."
  9357. (interactive "P")
  9358. (when (and org-link-search-failed (eq this-command last-command))
  9359. (goto-char (point-min))
  9360. (message "Link search wrapped back to beginning of buffer"))
  9361. (setq org-link-search-failed nil)
  9362. (let* ((pos (point))
  9363. (ct (org-context))
  9364. (a (assoc :link ct))
  9365. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9366. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9367. ((looking-at org-any-link-re)
  9368. ;; Don't stay stuck at link without an org-link face
  9369. (forward-char (if search-backward -1 1))))
  9370. (if (funcall srch-fun org-any-link-re nil t)
  9371. (progn
  9372. (goto-char (match-beginning 0))
  9373. (when (outline-invisible-p) (org-show-context)))
  9374. (goto-char pos)
  9375. (setq org-link-search-failed t)
  9376. (message "No further link found"))))
  9377. (defun org-previous-link ()
  9378. "Move backward to the previous link.
  9379. If the link is in hidden text, expose it."
  9380. (interactive)
  9381. (funcall 'org-next-link t))
  9382. (defun org-translate-link (s)
  9383. "Translate a link string if a translation function has been defined."
  9384. (with-temp-buffer
  9385. (insert (org-trim s))
  9386. (org-trim (org-element-interpret-data (org-element-context)))))
  9387. (defun org-translate-link-from-planner (type path)
  9388. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9389. This is still an experimental function, your mileage may vary."
  9390. (cond
  9391. ((member type '("http" "https" "news" "ftp"))
  9392. ;; standard Internet links are the same.
  9393. nil)
  9394. ((and (equal type "irc") (string-match "^//" path))
  9395. ;; Planner has two / at the beginning of an irc link, we have 1.
  9396. ;; We should have zero, actually....
  9397. (setq path (substring path 1)))
  9398. ((and (equal type "lisp") (string-match "^/" path))
  9399. ;; Planner has a slash, we do not.
  9400. (setq type "elisp" path (substring path 1)))
  9401. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9402. ;; A typical message link. Planner has the id after the final slash,
  9403. ;; we separate it with a hash mark
  9404. (setq path (concat (match-string 1 path) "#"
  9405. (org-remove-angle-brackets (match-string 2 path))))))
  9406. (cons type path))
  9407. (defun org-find-file-at-mouse (ev)
  9408. "Open file link or URL at mouse."
  9409. (interactive "e")
  9410. (mouse-set-point ev)
  9411. (org-open-at-point 'in-emacs))
  9412. (defun org-open-at-mouse (ev)
  9413. "Open file link or URL at mouse.
  9414. See the docstring of `org-open-file' for details."
  9415. (interactive "e")
  9416. (mouse-set-point ev)
  9417. (when (eq major-mode 'org-agenda-mode)
  9418. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9419. (org-open-at-point))
  9420. (defvar org-window-config-before-follow-link nil
  9421. "The window configuration before following a link.
  9422. This is saved in case the need arises to restore it.")
  9423. ;;;###autoload
  9424. (defun org-open-at-point-global ()
  9425. "Follow a link like Org-mode does.
  9426. This command can be called in any mode to follow a link that has
  9427. Org-mode syntax."
  9428. (interactive)
  9429. (org-run-like-in-org-mode 'org-open-at-point))
  9430. ;;;###autoload
  9431. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9432. "Open a link in the string S, as if it was in Org-mode."
  9433. (interactive "sLink: \nP")
  9434. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9435. (with-temp-buffer
  9436. (let ((org-inhibit-startup (not reference-buffer)))
  9437. (org-mode)
  9438. (insert s)
  9439. (goto-char (point-min))
  9440. (when reference-buffer
  9441. (setq org-link-abbrev-alist-local
  9442. (with-current-buffer reference-buffer
  9443. org-link-abbrev-alist-local)))
  9444. (org-open-at-point arg reference-buffer)))))
  9445. (defvar org-open-at-point-functions nil
  9446. "Hook that is run when following a link at point.
  9447. Functions in this hook must return t if they identify and follow
  9448. a link at point. If they don't find anything interesting at point,
  9449. they must return nil.")
  9450. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9451. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9452. (defun org-open-at-point (&optional arg reference-buffer)
  9453. "Open link, timestamp, footnote or tags at point.
  9454. When point is on a link, follow it. Normally, files will be
  9455. opened by an appropriate application. If the optional prefix
  9456. argument ARG is non-nil, Emacs will visit the file. With
  9457. a double prefix argument, try to open outside of Emacs, in the
  9458. application the system uses for this file type.
  9459. When point is on a timestamp, open the agenda at the day
  9460. specified.
  9461. When point is a footnote definition, move to the first reference
  9462. found. If it is on a reference, move to the associated
  9463. definition.
  9464. When point is on a headline, display a list of every link in the
  9465. entry, so it is possible to pick one, or all, of them. If point
  9466. is on a tag, call `org-tags-view' instead.
  9467. When optional argument REFERENCE-BUFFER is non-nil, it should
  9468. specify a buffer from where the link search should happen. This
  9469. is used internally by `org-open-link-from-string'.
  9470. On top of syntactically correct links, this function will open
  9471. the link at point in comments or comment blocks and the first
  9472. link in a property drawer line."
  9473. (interactive "P")
  9474. ;; On a code block, open block's results.
  9475. (unless (call-interactively 'org-babel-open-src-block-result)
  9476. (org-load-modules-maybe)
  9477. (setq org-window-config-before-follow-link (current-window-configuration))
  9478. (org-remove-occur-highlights nil nil t)
  9479. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9480. (let* ((context
  9481. ;; Only consider supported types, even if they are not
  9482. ;; the closest one.
  9483. (org-element-lineage
  9484. (org-element-context)
  9485. '(clock comment comment-block footnote-definition
  9486. footnote-reference headline inlinetask keyword link
  9487. node-property timestamp)
  9488. t))
  9489. (type (org-element-type context))
  9490. (value (org-element-property :value context)))
  9491. (cond
  9492. ((not context) (user-error "No link found"))
  9493. ;; Exception: open timestamps and links in properties
  9494. ;; drawers, keywords and comments.
  9495. ((memq type '(comment comment-block keyword node-property))
  9496. (cond ((org-in-regexp org-any-link-re)
  9497. (org-open-link-from-string (match-string-no-properties 0)))
  9498. ((or (org-in-regexp org-ts-regexp-both nil t)
  9499. (org-in-regexp org-tsr-regexp-both nil t))
  9500. (org-follow-timestamp-link))
  9501. (t (user-error "No link found"))))
  9502. ;; On a headline or an inlinetask, but not on a timestamp,
  9503. ;; a link, a footnote reference or on tags.
  9504. ((and (memq type '(headline inlinetask))
  9505. ;; Not on tags.
  9506. (progn (save-excursion (beginning-of-line)
  9507. (looking-at org-complex-heading-regexp))
  9508. (or (not (match-beginning 5))
  9509. (< (point) (match-beginning 5)))))
  9510. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9511. (links (car data))
  9512. (links-end (cdr data)))
  9513. (if links
  9514. (dolist (link (if (stringp links) (list links) links))
  9515. (search-forward link nil links-end)
  9516. (goto-char (match-beginning 0))
  9517. (org-open-at-point))
  9518. (require 'org-attach)
  9519. (org-attach-reveal 'if-exists))))
  9520. ;; On a clock line, make sure point is on the timestamp
  9521. ;; before opening it.
  9522. ((and (eq type 'clock)
  9523. value
  9524. (>= (point) (org-element-property :begin value))
  9525. (<= (point) (org-element-property :end value)))
  9526. (org-follow-timestamp-link))
  9527. ;; Do nothing on white spaces after an object.
  9528. ((>= (point)
  9529. (save-excursion
  9530. (goto-char (org-element-property :end context))
  9531. (skip-chars-backward " \t")
  9532. (point)))
  9533. (user-error "No link found"))
  9534. ((eq type 'timestamp) (org-follow-timestamp-link))
  9535. ;; On tags within a headline or an inlinetask.
  9536. ((and (memq type '(headline inlinetask))
  9537. (progn (save-excursion (beginning-of-line)
  9538. (looking-at org-complex-heading-regexp))
  9539. (and (match-beginning 5)
  9540. (>= (point) (match-beginning 5)))))
  9541. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9542. ((eq type 'link)
  9543. ;; When link is located within the description of another
  9544. ;; link (e.g., an inline image), always open the parent
  9545. ;; link.
  9546. (let*((link (let ((up (org-element-property :parent context)))
  9547. (if (eq (org-element-type up) 'link) up context)))
  9548. (type (org-element-property :type link))
  9549. (path (org-link-unescape (org-element-property :path link))))
  9550. ;; Switch back to REFERENCE-BUFFER needed when called in
  9551. ;; a temporary buffer through `org-open-link-from-string'.
  9552. (with-current-buffer (or reference-buffer (current-buffer))
  9553. (cond
  9554. ((equal type "file")
  9555. (if (string-match "[*?{]" (file-name-nondirectory path))
  9556. (dired path)
  9557. ;; Look into `org-link-protocols' in order to find
  9558. ;; a DEDICATED-FUNCTION to open file. The function
  9559. ;; will be applied on raw link instead of parsed
  9560. ;; link due to the limitation in `org-add-link-type'
  9561. ;; ("open" function called with a single argument).
  9562. ;; If no such function is found, fallback to
  9563. ;; `org-open-file'.
  9564. ;;
  9565. ;; Note : "file+emacs" and "file+sys" types are
  9566. ;; hard-coded in order to escape the previous
  9567. ;; limitation.
  9568. (let* ((option (org-element-property :search-option link))
  9569. (app (org-element-property :application link))
  9570. (dedicated-function
  9571. (nth 1 (assoc app org-link-protocols))))
  9572. (if dedicated-function
  9573. (funcall dedicated-function
  9574. (concat path
  9575. (and option (concat "::" option))))
  9576. (apply #'org-open-file
  9577. path
  9578. (cond (arg)
  9579. ((equal app "emacs") 'emacs)
  9580. ((equal app "sys") 'system))
  9581. (cond ((not option) nil)
  9582. ((org-string-match-p "\\`[0-9]+\\'" option)
  9583. (list (string-to-number option)))
  9584. (t (list nil
  9585. (org-link-unescape option)))))))))
  9586. ((assoc type org-link-protocols)
  9587. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9588. ((equal type "help")
  9589. (let ((f-or-v (intern path)))
  9590. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9591. ((boundp f-or-v) (describe-variable f-or-v))
  9592. (t (error "Not a known function or variable")))))
  9593. ((member type '("http" "https" "ftp" "mailto" "news"))
  9594. (browse-url (org-link-escape-browser (concat type ":" path))))
  9595. ((equal type "doi")
  9596. (browse-url
  9597. (org-link-escape-browser (concat org-doi-server-url path))))
  9598. ((equal type "message") (browse-url (concat type ":" path)))
  9599. ((equal type "shell")
  9600. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9601. (cmd path))
  9602. (if (or (and (org-string-nw-p
  9603. org-confirm-shell-link-not-regexp)
  9604. (string-match
  9605. org-confirm-shell-link-not-regexp cmd))
  9606. (not org-confirm-shell-link-function)
  9607. (funcall org-confirm-shell-link-function
  9608. (format "Execute \"%s\" in shell? "
  9609. (org-add-props cmd nil
  9610. 'face 'org-warning))))
  9611. (progn
  9612. (message "Executing %s" cmd)
  9613. (shell-command cmd buf)
  9614. (when (featurep 'midnight)
  9615. (setq clean-buffer-list-kill-buffer-names
  9616. (cons (buffer-name buf)
  9617. clean-buffer-list-kill-buffer-names))))
  9618. (user-error "Abort"))))
  9619. ((equal type "elisp")
  9620. (let ((cmd path))
  9621. (if (or (and (org-string-nw-p
  9622. org-confirm-elisp-link-not-regexp)
  9623. (org-string-match-p
  9624. org-confirm-elisp-link-not-regexp cmd))
  9625. (not org-confirm-elisp-link-function)
  9626. (funcall org-confirm-elisp-link-function
  9627. (format "Execute \"%s\" as elisp? "
  9628. (org-add-props cmd nil
  9629. 'face 'org-warning))))
  9630. (message "%s => %s" cmd
  9631. (if (eq (string-to-char cmd) ?\()
  9632. (eval (read cmd))
  9633. (call-interactively (read cmd))))
  9634. (user-error "Abort"))))
  9635. ((equal type "id")
  9636. (require 'org-id)
  9637. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9638. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9639. (unless (run-hook-with-args-until-success
  9640. 'org-open-link-functions path)
  9641. (if (not arg) (org-mark-ring-push)
  9642. (switch-to-buffer-other-window
  9643. (org-get-buffer-for-internal-link (current-buffer))))
  9644. (let ((destination
  9645. (org-with-wide-buffer
  9646. (if (equal type "radio")
  9647. (org-search-radio-target
  9648. (org-element-property :path link))
  9649. (org-link-search
  9650. (if (member type '("custom-id" "coderef"))
  9651. (org-element-property :raw-link link)
  9652. path)
  9653. ;; Prevent fuzzy links from matching
  9654. ;; themselves.
  9655. (and (equal type "fuzzy")
  9656. (+ 2 (org-element-property :begin link)))))
  9657. (point))))
  9658. (unless (and (<= (point-min) destination)
  9659. (>= (point-max) destination))
  9660. (widen))
  9661. (goto-char destination))))
  9662. (t (browse-url-at-point))))))
  9663. ;; On a footnote reference or at a footnote definition's label.
  9664. ((or (eq type 'footnote-reference)
  9665. (and (eq type 'footnote-definition)
  9666. (save-excursion
  9667. ;; Do not validate action when point is on the
  9668. ;; spaces right after the footnote label, in
  9669. ;; order to be on par with behaviour on links.
  9670. (skip-chars-forward " \t")
  9671. (let ((begin
  9672. (org-element-property :contents-begin context)))
  9673. (if begin (< (point) begin)
  9674. (= (org-element-property :post-affiliated context)
  9675. (line-beginning-position)))))))
  9676. (org-footnote-action))
  9677. (t (user-error "No link found")))))
  9678. (run-hook-with-args 'org-follow-link-hook)))
  9679. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9680. "Offer links in the current entry and return the selected link.
  9681. If there is only one link, return it.
  9682. If NTH is an integer, return the NTH link found.
  9683. If ZERO is a string, check also this string for a link, and if
  9684. there is one, return it."
  9685. (with-current-buffer buffer
  9686. (save-excursion
  9687. (save-restriction
  9688. (widen)
  9689. (goto-char marker)
  9690. (let ((cnt ?0)
  9691. have-zero end links link c)
  9692. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9693. (push (match-string 0 zero) links)
  9694. (setq cnt (1- cnt) have-zero t))
  9695. (save-excursion
  9696. (org-back-to-heading t)
  9697. (setq end (save-excursion (outline-next-heading) (point)))
  9698. (while (re-search-forward org-any-link-re end t)
  9699. (push (match-string 0) links))
  9700. (setq links (org-uniquify (reverse links))))
  9701. (cond
  9702. ((null links)
  9703. (message "No links"))
  9704. ((equal (length links) 1)
  9705. (setq link (car links)))
  9706. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9707. (setq link (nth (if have-zero nth (1- nth)) links)))
  9708. (t ; we have to select a link
  9709. (save-excursion
  9710. (save-window-excursion
  9711. (delete-other-windows)
  9712. (with-output-to-temp-buffer "*Select Link*"
  9713. (dolist (l links)
  9714. (cond
  9715. ((not (string-match org-bracket-link-regexp l))
  9716. (princ (format "[%c] %s\n" (cl-incf cnt)
  9717. (org-remove-angle-brackets l))))
  9718. ((match-end 3)
  9719. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9720. (match-string 3 l) (match-string 1 l))))
  9721. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9722. (match-string 1 l)))))))
  9723. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9724. (message "Select link to open, RET to open all:")
  9725. (setq c (read-char-exclusive))
  9726. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9727. (when (equal c ?q) (user-error "Abort"))
  9728. (if (equal c ?\C-m)
  9729. (setq link links)
  9730. (setq nth (- c ?0))
  9731. (when have-zero (setq nth (1+ nth)))
  9732. (unless (and (integerp nth) (>= (length links) nth))
  9733. (user-error "Invalid link selection"))
  9734. (setq link (nth (1- nth) links)))))
  9735. (cons link end))))))
  9736. ;; TODO: These functions are deprecated since `org-open-at-point'
  9737. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9738. (defun org-open-file-with-system (path)
  9739. "Open file at PATH using the system way of opening it."
  9740. (org-open-file path 'system))
  9741. (defun org-open-file-with-emacs (path)
  9742. "Open file at PATH in Emacs."
  9743. (org-open-file path 'emacs))
  9744. ;;; File search
  9745. (defvar org-create-file-search-functions nil
  9746. "List of functions to construct the right search string for a file link.
  9747. These functions are called in turn with point at the location to
  9748. which the link should point.
  9749. A function in the hook should first test if it would like to
  9750. handle this file type, for example by checking the `major-mode'
  9751. or the file extension. If it decides not to handle this file, it
  9752. should just return nil to give other functions a chance. If it
  9753. does handle the file, it must return the search string to be used
  9754. when following the link. The search string will be part of the
  9755. file link, given after a double colon, and `org-open-at-point'
  9756. will automatically search for it. If special measures must be
  9757. taken to make the search successful, another function should be
  9758. added to the companion hook `org-execute-file-search-functions',
  9759. which see.
  9760. A function in this hook may also use `setq' to set the variable
  9761. `description' to provide a suggestion for the descriptive text to
  9762. be used for this link when it gets inserted into an Org-mode
  9763. buffer with \\[org-insert-link].")
  9764. (defvar org-execute-file-search-functions nil
  9765. "List of functions to execute a file search triggered by a link.
  9766. Functions added to this hook must accept a single argument, the
  9767. search string that was part of the file link, the part after the
  9768. double colon. The function must first check if it would like to
  9769. handle this search, for example by checking the `major-mode' or
  9770. the file extension. If it decides not to handle this search, it
  9771. should just return nil to give other functions a chance. If it
  9772. does handle the search, it must return a non-nil value to keep
  9773. other functions from trying.
  9774. Each function can access the current prefix argument through the
  9775. variable `current-prefix-arg'. Note that a single prefix is used
  9776. to force opening a link in Emacs, so it may be good to only use a
  9777. numeric or double prefix to guide the search function.
  9778. In case this is needed, a function in this hook can also restore
  9779. the window configuration before `org-open-at-point' was called using:
  9780. \(set-window-configuration org-window-config-before-follow-link)")
  9781. (defun org-search-radio-target (target)
  9782. "Search a radio target matching TARGET in current buffer.
  9783. White spaces are not significant."
  9784. (let ((re (format "<<<%s>>>"
  9785. (mapconcat #'regexp-quote
  9786. (org-split-string target "[ \t\n]+")
  9787. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9788. (origin (point)))
  9789. (goto-char (point-min))
  9790. (catch :radio-match
  9791. (while (re-search-forward re nil t)
  9792. (backward-char)
  9793. (let ((object (org-element-context)))
  9794. (when (eq (org-element-type object) 'radio-target)
  9795. (goto-char (org-element-property :begin object))
  9796. (org-show-context 'link-search)
  9797. (throw :radio-match nil))))
  9798. (goto-char origin)
  9799. (user-error "No match for radio target: %s" target))))
  9800. (defun org-link-search (s &optional avoid-pos stealth)
  9801. "Search for a search string S.
  9802. If S starts with \"#\", it triggers a custom ID search.
  9803. If S is enclosed within parenthesis, it initiates a coderef
  9804. search.
  9805. If S is surrounded by forward slashes, it is interpreted as
  9806. a regular expression. In Org mode files, this will create an
  9807. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9808. to list matches. If the current buffer is in `dired-mode', grep
  9809. will be used to search in all files.
  9810. When AVOID-POS is given, ignore matches near that position.
  9811. When optional argument STEALTH is non-nil, do not modify
  9812. visibility around point, thus ignoring `org-show-context-detail'
  9813. variable.
  9814. Search is case-insensitive and ignores white spaces. Return type
  9815. of matched result, with is either `dedicated' or `fuzzy'."
  9816. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9817. (let* ((case-fold-search t)
  9818. (origin (point))
  9819. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9820. (words (org-split-string s "[ \t\n]+"))
  9821. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9822. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9823. type)
  9824. (cond
  9825. ;; Check if there are any special search functions.
  9826. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9827. ((eq (string-to-char s) ?#)
  9828. ;; Look for a custom ID S if S starts with "#".
  9829. (let* ((id (substring normalized 1))
  9830. (match (org-find-property "CUSTOM_ID" id)))
  9831. (if match (progn (goto-char match) (setf type 'dedicated))
  9832. (error "No match for custom ID: %s" id))))
  9833. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9834. ;; Look for coderef targets if S is enclosed within parenthesis.
  9835. (let ((coderef (match-string-no-properties 1 normalized))
  9836. (re (substring s-single-re 1 -1)))
  9837. (goto-char (point-min))
  9838. (catch :coderef-match
  9839. (while (re-search-forward re nil t)
  9840. (let ((element (org-element-at-point)))
  9841. (when (and (memq (org-element-type element)
  9842. '(example-block src-block))
  9843. ;; Build proper regexp according to current
  9844. ;; block's label format.
  9845. (let ((label-fmt
  9846. (regexp-quote
  9847. (or (org-element-property :label-fmt element)
  9848. org-coderef-label-format))))
  9849. (save-excursion
  9850. (beginning-of-line)
  9851. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9852. (format label-fmt coderef))))))
  9853. (setq type 'dedicated)
  9854. (goto-char (match-beginning 1))
  9855. (throw :coderef-match nil))))
  9856. (goto-char origin)
  9857. (error "No match for coderef: %s" coderef))))
  9858. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9859. ;; Look for a regular expression.
  9860. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9861. (match-string 1 s)))
  9862. ;; Fuzzy links.
  9863. (t
  9864. (let ((starred (eq (string-to-char normalized) ?*)))
  9865. (cond
  9866. ;; Look for targets, only if not in a headline search.
  9867. ((and (not starred)
  9868. (let ((target (format "<<%s>>" s-multi-re)))
  9869. (catch :target-match
  9870. (goto-char (point-min))
  9871. (while (re-search-forward target nil t)
  9872. (backward-char)
  9873. (let ((context (org-element-context)))
  9874. (when (eq (org-element-type context) 'target)
  9875. (setq type 'dedicated)
  9876. (goto-char (org-element-property :begin context))
  9877. (throw :target-match t))))
  9878. nil))))
  9879. ;; Look for elements named after S, only if not in a headline
  9880. ;; search.
  9881. ((and (not starred)
  9882. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9883. (catch :name-match
  9884. (goto-char (point-min))
  9885. (while (re-search-forward name nil t)
  9886. (let ((element (org-element-at-point)))
  9887. (when (equal (org-split-string
  9888. (org-element-property :name element)
  9889. "[ \t]+")
  9890. words)
  9891. (setq type 'dedicated)
  9892. (beginning-of-line)
  9893. (throw :name-match t))))
  9894. nil))))
  9895. ;; Regular text search. Prefer headlines in Org mode
  9896. ;; buffers.
  9897. ((and (derived-mode-p 'org-mode)
  9898. (let* ((wspace "[ \t]")
  9899. (wspaceopt (concat wspace "*"))
  9900. (cookie (concat "\\(?:"
  9901. wspaceopt
  9902. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9903. wspaceopt
  9904. "\\)"))
  9905. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  9906. (re (concat
  9907. org-outline-regexp-bol
  9908. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9909. "\\(?:\\[#.\\][ \t]+\\)?"
  9910. "\\(?:" org-comment-string "[ \t]+\\)?"
  9911. sep "?"
  9912. (let ((title (mapconcat #'regexp-quote
  9913. words
  9914. sep)))
  9915. (if starred (substring title 1) title))
  9916. sep "?"
  9917. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9918. "[ \t]*$")))
  9919. (goto-char (point-min))
  9920. (re-search-forward re nil t)))
  9921. (goto-char (match-beginning 0))
  9922. (setq type 'dedicated))
  9923. ;; Offer to create non-existent headline depending on
  9924. ;; `org-link-search-must-match-exact-headline'.
  9925. ((and (derived-mode-p 'org-mode)
  9926. (not org-link-search-inhibit-query)
  9927. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9928. (yes-or-no-p "No match - create this as a new heading? "))
  9929. (goto-char (point-max))
  9930. (unless (bolp) (newline))
  9931. (org-insert-heading nil t t)
  9932. (insert s "\n")
  9933. (beginning-of-line 0))
  9934. ;; Only headlines are looked after. No need to process
  9935. ;; further: throw an error.
  9936. ((and (derived-mode-p 'org-mode)
  9937. (or starred org-link-search-must-match-exact-headline))
  9938. (goto-char origin)
  9939. (error "No match for fuzzy expression: %s" normalized))
  9940. ;; Regular text search.
  9941. ((catch :fuzzy-match
  9942. (goto-char (point-min))
  9943. (while (re-search-forward s-multi-re nil t)
  9944. ;; Skip match if it contains AVOID-POS or it is included
  9945. ;; in a link with a description but outside the
  9946. ;; description.
  9947. (unless (or (and avoid-pos
  9948. (<= (match-beginning 0) avoid-pos)
  9949. (> (match-end 0) avoid-pos))
  9950. (and (save-match-data
  9951. (org-in-regexp org-bracket-link-regexp))
  9952. (match-beginning 3)
  9953. (or (> (match-beginning 3) (point))
  9954. (<= (match-end 3) (point)))
  9955. (org-element-lineage
  9956. (save-match-data (org-element-context))
  9957. '(link) t)))
  9958. (goto-char (match-beginning 0))
  9959. (setq type 'fuzzy)
  9960. (throw :fuzzy-match t)))
  9961. nil))
  9962. ;; All failed. Throw an error.
  9963. (t (goto-char origin)
  9964. (error "No match for fuzzy expression: %s" normalized))))))
  9965. ;; Disclose surroundings of match, if appropriate.
  9966. (when (and (derived-mode-p 'org-mode) (not stealth))
  9967. (org-show-context 'link-search))
  9968. type))
  9969. (defun org-get-buffer-for-internal-link (buffer)
  9970. "Return a buffer to be used for displaying the link target of internal links."
  9971. (cond
  9972. ((not org-display-internal-link-with-indirect-buffer)
  9973. buffer)
  9974. ((string-match "(Clone)$" (buffer-name buffer))
  9975. (message "Buffer is already a clone, not making another one")
  9976. ;; we also do not modify visibility in this case
  9977. buffer)
  9978. (t ; make a new indirect buffer for displaying the link
  9979. (let* ((bn (buffer-name buffer))
  9980. (ibn (concat bn "(Clone)"))
  9981. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9982. (with-current-buffer ib (org-overview))
  9983. ib))))
  9984. (defun org-do-occur (regexp &optional cleanup)
  9985. "Call the Emacs command `occur'.
  9986. If CLEANUP is non-nil, remove the printout of the regular expression
  9987. in the *Occur* buffer. This is useful if the regex is long and not useful
  9988. to read."
  9989. (occur regexp)
  9990. (when cleanup
  9991. (let ((cwin (selected-window)) win beg end)
  9992. (when (setq win (get-buffer-window "*Occur*"))
  9993. (select-window win))
  9994. (goto-char (point-min))
  9995. (when (re-search-forward "match[a-z]+" nil t)
  9996. (setq beg (match-end 0))
  9997. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  9998. (setq end (1- (match-beginning 0)))))
  9999. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10000. (goto-char (point-min))
  10001. (select-window cwin))))
  10002. ;;; The mark ring for links jumps
  10003. (defvar org-mark-ring nil
  10004. "Mark ring for positions before jumps in Org-mode.")
  10005. (defvar org-mark-ring-last-goto nil
  10006. "Last position in the mark ring used to go back.")
  10007. ;; Fill and close the ring
  10008. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10009. (dotimes (_ org-mark-ring-length)
  10010. (push (make-marker) org-mark-ring))
  10011. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10012. org-mark-ring)
  10013. (defun org-mark-ring-push (&optional pos buffer)
  10014. "Put the current position or POS into the mark ring and rotate it."
  10015. (interactive)
  10016. (setq pos (or pos (point)))
  10017. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10018. (move-marker (car org-mark-ring)
  10019. (or pos (point))
  10020. (or buffer (current-buffer)))
  10021. (message "%s"
  10022. (substitute-command-keys
  10023. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10024. (defun org-mark-ring-goto (&optional n)
  10025. "Jump to the previous position in the mark ring.
  10026. With prefix arg N, jump back that many stored positions. When
  10027. called several times in succession, walk through the entire ring.
  10028. Org-mode commands jumping to a different position in the current file,
  10029. or to another Org-mode file, automatically push the old position
  10030. onto the ring."
  10031. (interactive "p")
  10032. (let (p m)
  10033. (if (eq last-command this-command)
  10034. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10035. (setq p org-mark-ring))
  10036. (setq org-mark-ring-last-goto p)
  10037. (setq m (car p))
  10038. (org-pop-to-buffer-same-window (marker-buffer m))
  10039. (goto-char m)
  10040. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10041. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10042. ;;; Emacs 24.4+
  10043. (defun org-remove-angle-brackets (s)
  10044. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10045. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10046. s)
  10047. (defun org-add-angle-brackets (s)
  10048. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10049. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10050. s)
  10051. (defun org-remove-double-quotes (s)
  10052. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10053. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10054. s)
  10055. ;;; Following specific links
  10056. (defvar org-agenda-buffer-tmp-name)
  10057. (defvar org-agenda-start-on-weekday)
  10058. (defun org-follow-timestamp-link ()
  10059. "Open an agenda view for the time-stamp date/range at point."
  10060. (cond
  10061. ((org-at-date-range-p t)
  10062. (let ((org-agenda-start-on-weekday)
  10063. (t1 (match-string 1))
  10064. (t2 (match-string 2)) tt1 tt2)
  10065. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10066. tt2 (time-to-days (org-time-string-to-time t2)))
  10067. (let ((org-agenda-buffer-tmp-name
  10068. (format "*Org Agenda(a:%s)"
  10069. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10070. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10071. ((org-at-timestamp-p t)
  10072. (let ((org-agenda-buffer-tmp-name
  10073. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10074. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10075. (substring (match-string 1) 0 10)))
  10076. 1)))
  10077. (t (error "This should not happen"))))
  10078. ;;; Following file links
  10079. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10080. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10081. (declare-function mailcap-mime-info
  10082. "mailcap" (string &optional request no-decode))
  10083. (defvar org-wait nil)
  10084. (defun org-open-file (path &optional in-emacs line search)
  10085. "Open the file at PATH.
  10086. First, this expands any special file name abbreviations. Then the
  10087. configuration variable `org-file-apps' is checked if it contains an
  10088. entry for this file type, and if yes, the corresponding command is launched.
  10089. If no application is found, Emacs simply visits the file.
  10090. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10091. With a double \\[universal-argument] \\[universal-argument] \
  10092. prefix arg, Org tries to avoid opening in Emacs
  10093. and to use an external application to visit the file.
  10094. Optional LINE specifies a line to go to, optional SEARCH a string
  10095. to search for. If LINE or SEARCH is given, the file will be
  10096. opened in Emacs, unless an entry from org-file-apps that makes
  10097. use of groups in a regexp matches.
  10098. If you want to change the way frames are used when following a
  10099. link, please customize `org-link-frame-setup'.
  10100. If the file does not exist, an error is thrown."
  10101. (let* ((file (if (equal path "")
  10102. buffer-file-name
  10103. (substitute-in-file-name (expand-file-name path))))
  10104. (file-apps (append org-file-apps (org-default-apps)))
  10105. (apps (cl-remove-if
  10106. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10107. (apps-dlink (cl-remove-if-not
  10108. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10109. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10110. (dirp (unless remp (file-directory-p file)))
  10111. (file (if (and dirp org-open-directory-means-index-dot-org)
  10112. (concat (file-name-as-directory file) "index.org")
  10113. file))
  10114. (a-m-a-p (assq 'auto-mode apps))
  10115. (dfile (downcase file))
  10116. ;; Reconstruct the original link from the PATH, LINE and
  10117. ;; SEARCH args.
  10118. (link (cond (line (concat file "::" (number-to-string line)))
  10119. (search (concat file "::" search))
  10120. (t file)))
  10121. (dlink (downcase link))
  10122. (old-buffer (current-buffer))
  10123. (old-pos (point))
  10124. (old-mode major-mode)
  10125. (ext
  10126. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10127. (match-string 1 dfile)))
  10128. cmd link-match-data)
  10129. (cond
  10130. ((member in-emacs '((16) system))
  10131. (setq cmd (cdr (assq 'system apps))))
  10132. (in-emacs (setq cmd 'emacs))
  10133. (t
  10134. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10135. (and dirp (cdr (assq 'directory apps)))
  10136. ;; First, try matching against apps-dlink if we
  10137. ;; get a match here, store the match data for
  10138. ;; later.
  10139. (let ((match (assoc-default dlink apps-dlink
  10140. 'string-match)))
  10141. (if match
  10142. (progn (setq link-match-data (match-data))
  10143. match)
  10144. (progn (setq in-emacs (or in-emacs line search))
  10145. nil))) ; if we have no match in apps-dlink,
  10146. ; always open the file in emacs if line or search
  10147. ; is given (for backwards compatibility)
  10148. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10149. 'string-match)
  10150. (cdr (assoc ext apps))
  10151. (cdr (assq t apps))))))
  10152. (when (eq cmd 'system)
  10153. (setq cmd (cdr (assoc 'system apps))))
  10154. (when (eq cmd 'default)
  10155. (setq cmd (cdr (assoc t apps))))
  10156. (when (eq cmd 'mailcap)
  10157. (require 'mailcap)
  10158. (mailcap-parse-mailcaps)
  10159. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10160. (command (mailcap-mime-info mime-type)))
  10161. (if (stringp command)
  10162. (setq cmd command)
  10163. (setq cmd 'emacs))))
  10164. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10165. (not (file-exists-p file))
  10166. (not org-open-non-existing-files))
  10167. (user-error "No such file: %s" file))
  10168. (cond
  10169. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10170. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10171. (while (string-match "['\"]%s['\"]" cmd)
  10172. (setq cmd (replace-match "%s" t t cmd)))
  10173. (while (string-match "%s" cmd)
  10174. (setq cmd (replace-match
  10175. (save-match-data
  10176. (shell-quote-argument
  10177. (convert-standard-filename file)))
  10178. t t cmd)))
  10179. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10180. (save-match-data
  10181. (let ((match-index 1)
  10182. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10183. (set-match-data link-match-data)
  10184. (while (<= match-index number-of-groups)
  10185. (let ((regex (concat "%" (number-to-string match-index)))
  10186. (replace-with (match-string match-index dlink)))
  10187. (while (string-match regex cmd)
  10188. (setq cmd (replace-match replace-with t t cmd))))
  10189. (setq match-index (+ match-index 1)))))
  10190. (save-window-excursion
  10191. (message "Running %s...done" cmd)
  10192. (start-process-shell-command cmd nil cmd)
  10193. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10194. ((or (stringp cmd)
  10195. (eq cmd 'emacs))
  10196. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10197. (widen)
  10198. (cond (line (org-goto-line line)
  10199. (when (derived-mode-p 'org-mode) (org-reveal)))
  10200. (search (org-link-search search))))
  10201. ((functionp cmd)
  10202. (save-match-data
  10203. (set-match-data link-match-data)
  10204. (funcall cmd file link)))
  10205. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10206. (and (derived-mode-p 'org-mode)
  10207. (eq old-mode 'org-mode)
  10208. (or (not (eq old-buffer (current-buffer)))
  10209. (not (eq old-pos (point))))
  10210. (org-mark-ring-push old-pos old-buffer))))
  10211. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10212. "This function returns non-nil if `entry' uses a regular
  10213. expression which should be matched against the whole link by
  10214. org-open-file.
  10215. It assumes that is the case when the entry uses a regular
  10216. expression which has at least one grouping construct and the
  10217. action is either a lisp form or a command string containing
  10218. `%1', i.e. using at least one subexpression match as a
  10219. parameter."
  10220. (let ((selector (car entry))
  10221. (action (cdr entry)))
  10222. (if (stringp selector)
  10223. (and (> (regexp-opt-depth selector) 0)
  10224. (or (and (stringp action)
  10225. (string-match "%[0-9]" action))
  10226. (consp action)))
  10227. nil)))
  10228. (defun org-default-apps ()
  10229. "Return the default applications for this operating system."
  10230. (cond
  10231. ((eq system-type 'darwin)
  10232. org-file-apps-defaults-macosx)
  10233. ((eq system-type 'windows-nt)
  10234. org-file-apps-defaults-windowsnt)
  10235. (t org-file-apps-defaults-gnu)))
  10236. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10237. "Convert extensions to regular expressions in the cars of LIST.
  10238. Also, weed out any non-string entries, because the return value is used
  10239. only for regexp matching.
  10240. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10241. point to the symbol `emacs', indicating that the file should
  10242. be opened in Emacs."
  10243. (append
  10244. (delq nil
  10245. (mapcar (lambda (x)
  10246. (unless (not (stringp (car x)))
  10247. (if (string-match "\\W" (car x))
  10248. x
  10249. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10250. list))
  10251. (when add-auto-mode
  10252. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10253. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10254. (defun org-file-remote-p (file)
  10255. "Test whether FILE specifies a location on a remote system.
  10256. Return non-nil if the location is indeed remote.
  10257. For example, the filename \"/user@host:/foo\" specifies a location
  10258. on the system \"/user@host:\"."
  10259. (cond ((fboundp 'file-remote-p)
  10260. (file-remote-p file))
  10261. ((fboundp 'tramp-handle-file-remote-p)
  10262. (tramp-handle-file-remote-p file))
  10263. ((and (boundp 'ange-ftp-name-format)
  10264. (string-match (car ange-ftp-name-format) file))
  10265. t)))
  10266. ;;;; Refiling
  10267. (defun org-get-org-file ()
  10268. "Read a filename, with default directory `org-directory'."
  10269. (let ((default (or org-default-notes-file remember-data-file)))
  10270. (read-file-name (format "File name [%s]: " default)
  10271. (file-name-as-directory org-directory)
  10272. default)))
  10273. (defun org-notes-order-reversed-p ()
  10274. "Check if the current file should receive notes in reversed order."
  10275. (cond
  10276. ((not org-reverse-note-order) nil)
  10277. ((eq t org-reverse-note-order) t)
  10278. ((not (listp org-reverse-note-order)) nil)
  10279. (t (catch 'exit
  10280. (dolist (entry org-reverse-note-order)
  10281. (when (string-match (car entry) buffer-file-name)
  10282. (throw 'exit (cdr entry))))))))
  10283. (defvar org-refile-target-table nil
  10284. "The list of refile targets, created by `org-refile'.")
  10285. (defvar org-agenda-new-buffers nil
  10286. "Buffers created to visit agenda files.")
  10287. (defvar org-refile-cache nil
  10288. "Cache for refile targets.")
  10289. (defvar org-refile-markers nil
  10290. "All the markers used for caching refile locations.")
  10291. (defun org-refile-marker (pos)
  10292. "Get a new refile marker, but only if caching is in use."
  10293. (if (not org-refile-use-cache)
  10294. pos
  10295. (let ((m (make-marker)))
  10296. (move-marker m pos)
  10297. (push m org-refile-markers)
  10298. m)))
  10299. (defun org-refile-cache-clear ()
  10300. "Clear the refile cache and disable all the markers."
  10301. (dolist (m org-refile-markers) (move-marker m nil))
  10302. (setq org-refile-markers nil)
  10303. (setq org-refile-cache nil)
  10304. (message "Refile cache has been cleared"))
  10305. (defun org-refile-cache-check-set (set)
  10306. "Check if all the markers in the cache still have live buffers."
  10307. (let (marker)
  10308. (catch 'exit
  10309. (while (and set (setq marker (nth 3 (pop set))))
  10310. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10311. (when (and marker (null (marker-buffer marker)))
  10312. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10313. (sit-for 3)
  10314. (throw 'exit nil)))
  10315. t)))
  10316. (defun org-refile-cache-put (set &rest identifiers)
  10317. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10318. (let* ((key (sha1 (prin1-to-string identifiers)))
  10319. (entry (assoc key org-refile-cache)))
  10320. (if entry
  10321. (setcdr entry set)
  10322. (push (cons key set) org-refile-cache))))
  10323. (defun org-refile-cache-get (&rest identifiers)
  10324. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10325. (cond
  10326. ((not org-refile-cache) nil)
  10327. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10328. (t
  10329. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10330. org-refile-cache))))
  10331. (and set (org-refile-cache-check-set set) set)))))
  10332. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10333. "Produce a table with refile targets."
  10334. (let ((case-fold-search nil)
  10335. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10336. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10337. targets tgs txt re files desc descre fast-path-p level pos0)
  10338. (message "Getting targets...")
  10339. (with-current-buffer (or default-buffer (current-buffer))
  10340. (dolist (entry entries)
  10341. (setq files (car entry) desc (cdr entry))
  10342. (setq fast-path-p nil)
  10343. (cond
  10344. ((null files) (setq files (list (current-buffer))))
  10345. ((eq files 'org-agenda-files)
  10346. (setq files (org-agenda-files 'unrestricted)))
  10347. ((and (symbolp files) (fboundp files))
  10348. (setq files (funcall files)))
  10349. ((and (symbolp files) (boundp files))
  10350. (setq files (symbol-value files))))
  10351. (when (stringp files) (setq files (list files)))
  10352. (cond
  10353. ((eq (car desc) :tag)
  10354. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10355. ((eq (car desc) :todo)
  10356. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10357. ((eq (car desc) :regexp)
  10358. (setq descre (cdr desc)))
  10359. ((eq (car desc) :level)
  10360. (setq descre (concat "^\\*\\{" (number-to-string
  10361. (if org-odd-levels-only
  10362. (1- (* 2 (cdr desc)))
  10363. (cdr desc)))
  10364. "\\}[ \t]")))
  10365. ((eq (car desc) :maxlevel)
  10366. (setq fast-path-p t)
  10367. (setq descre (concat "^\\*\\{1," (number-to-string
  10368. (if org-odd-levels-only
  10369. (1- (* 2 (cdr desc)))
  10370. (cdr desc)))
  10371. "\\}[ \t]")))
  10372. (t (error "Bad refiling target description %s" desc)))
  10373. (dolist (f files)
  10374. (with-current-buffer
  10375. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10376. (or
  10377. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10378. (progn
  10379. (when (bufferp f) (setq f (buffer-file-name
  10380. (buffer-base-buffer f))))
  10381. (setq f (and f (expand-file-name f)))
  10382. (when (eq org-refile-use-outline-path 'file)
  10383. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10384. (save-excursion
  10385. (save-restriction
  10386. (widen)
  10387. (goto-char (point-min))
  10388. (while (re-search-forward descre nil t)
  10389. (goto-char (setq pos0 (point-at-bol)))
  10390. (catch 'next
  10391. (when org-refile-target-verify-function
  10392. (save-match-data
  10393. (or (funcall org-refile-target-verify-function)
  10394. (throw 'next t))))
  10395. (when (and (looking-at org-complex-heading-regexp)
  10396. (not (member (match-string 4) excluded-entries))
  10397. (match-string 4))
  10398. (setq level (org-reduced-level
  10399. (- (match-end 1) (match-beginning 1)))
  10400. txt (org-link-display-format (match-string 4))
  10401. txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
  10402. re (format org-complex-heading-regexp-format
  10403. (regexp-quote (match-string 4))))
  10404. (when org-refile-use-outline-path
  10405. (setq txt (mapconcat
  10406. 'org-protect-slash
  10407. (append
  10408. (if (eq org-refile-use-outline-path
  10409. 'file)
  10410. (list (file-name-nondirectory
  10411. (buffer-file-name
  10412. (buffer-base-buffer))))
  10413. (when (eq org-refile-use-outline-path
  10414. 'full-file-path)
  10415. (list (buffer-file-name
  10416. (buffer-base-buffer)))))
  10417. (org-get-outline-path fast-path-p
  10418. level txt)
  10419. (list txt))
  10420. "/")))
  10421. (push (list txt f re (org-refile-marker (point)))
  10422. tgs)))
  10423. (when (= (point) pos0)
  10424. ;; verification function has not moved point
  10425. (goto-char (point-at-eol))))))))
  10426. (when org-refile-use-cache
  10427. (org-refile-cache-put tgs (buffer-file-name) descre))
  10428. (setq targets (append tgs targets))))))
  10429. (message "Getting targets...done")
  10430. (nreverse targets)))
  10431. (defun org-protect-slash (s)
  10432. (while (string-match "/" s)
  10433. (setq s (replace-match "\\" t t s)))
  10434. s)
  10435. (defvar org-olpa (make-vector 20 nil))
  10436. (defun org-get-outline-path (&optional fastp level heading)
  10437. "Return the outline path to the current entry, as a list.
  10438. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10439. routine which makes outline path derivations for an entire file,
  10440. avoiding backtracing. Refile target collection makes use of that."
  10441. (if fastp
  10442. (progn
  10443. (when (> level 19)
  10444. (error "Outline path failure, more than 19 levels"))
  10445. (cl-loop for i from level upto 19 do
  10446. (aset org-olpa i nil))
  10447. (prog1
  10448. (delq nil (append org-olpa nil))
  10449. (aset org-olpa level heading)))
  10450. (let (rtn case-fold-search)
  10451. (save-excursion
  10452. (save-restriction
  10453. (widen)
  10454. (while (org-up-heading-safe)
  10455. (when (looking-at org-complex-heading-regexp)
  10456. (push (org-trim
  10457. (replace-regexp-in-string
  10458. ;; Remove statistical/checkboxes cookies
  10459. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10460. (org-match-string-no-properties 4)))
  10461. rtn)))
  10462. rtn)))))
  10463. (defun org-format-outline-path (path &optional width prefix separator)
  10464. "Format the outline path PATH for display.
  10465. WIDTH is the maximum number of characters that is available.
  10466. PREFIX is a prefix to be included in the returned string,
  10467. such as the file name.
  10468. SEPARATOR is inserted between the different parts of the path,
  10469. the default is \"/\"."
  10470. (setq width (or width 79))
  10471. (setq path (delq nil path))
  10472. (unless (> width 0)
  10473. (user-error "Argument `width' must be positive"))
  10474. (setq separator (or separator "/"))
  10475. (let* ((org-odd-levels-only nil)
  10476. (fpath (concat
  10477. prefix (and prefix path separator)
  10478. (mapconcat
  10479. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10480. (cl-loop for head in path
  10481. for n from 0
  10482. collect (org-add-props
  10483. head nil 'face
  10484. (nth (% n org-n-level-faces) org-level-faces)))
  10485. separator))))
  10486. (when (> (length fpath) width)
  10487. (if (< width 7)
  10488. ;; It's unlikely that `width' will be this small, but don't
  10489. ;; waste characters by adding ".." if it is.
  10490. (setq fpath (substring fpath 0 width))
  10491. (setf (substring fpath (- width 2)) "..")))
  10492. fpath))
  10493. (defun org-display-outline-path (&optional file current separator just-return-string)
  10494. "Display the current outline path in the echo area.
  10495. If FILE is non-nil, prepend the output with the file name.
  10496. If CURRENT is non-nil, append the current heading to the output.
  10497. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10498. the different parts of the path and defaults to \"/\".
  10499. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10500. (interactive "P")
  10501. (let* (case-fold-search
  10502. (bfn (buffer-file-name (buffer-base-buffer)))
  10503. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10504. res)
  10505. (when current (setq path (append path
  10506. (save-excursion
  10507. (org-back-to-heading t)
  10508. (when (looking-at org-complex-heading-regexp)
  10509. (list (match-string 4)))))))
  10510. (setq res
  10511. (org-format-outline-path
  10512. path
  10513. (1- (frame-width))
  10514. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10515. separator))
  10516. (if just-return-string
  10517. (org-no-properties res)
  10518. (org-unlogged-message "%s" res))))
  10519. (defvar org-refile-history nil
  10520. "History for refiling operations.")
  10521. (defvar org-after-refile-insert-hook nil
  10522. "Hook run after `org-refile' has inserted its stuff at the new location.
  10523. Note that this is still *before* the stuff will be removed from
  10524. the *old* location.")
  10525. (defvar org-capture-last-stored-marker)
  10526. (defvar org-refile-keep nil
  10527. "Non-nil means `org-refile' will copy instead of refile.")
  10528. (defun org-copy ()
  10529. "Like `org-refile', but copy."
  10530. (interactive)
  10531. (let ((org-refile-keep t))
  10532. (funcall 'org-refile nil nil nil "Copy")))
  10533. (defun org-refile (&optional arg default-buffer rfloc msg)
  10534. "Move the entry or entries at point to another heading.
  10535. The list of target headings is compiled using the information in
  10536. `org-refile-targets', which see.
  10537. At the target location, the entry is filed as a subitem of the
  10538. target heading. Depending on `org-reverse-note-order', the new
  10539. subitem will either be the first or the last subitem.
  10540. If there is an active region, all entries in that region will be
  10541. refiled. However, the region must fulfill the requirement that
  10542. the first heading sets the top-level of the moved text.
  10543. With prefix arg ARG, the command will only visit the target
  10544. location and not actually move anything.
  10545. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10546. refiling operation has put the subtree.
  10547. With a numeric prefix argument of `2', refile to the running clock.
  10548. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10549. being set to t and copy to the target location, don't move it.
  10550. Beware that keeping refiled entries may result in duplicated ID
  10551. properties.
  10552. RFLOC can be a refile location obtained in a different way.
  10553. MSG is a string to replace \"Refile\" in the default prompt with
  10554. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10555. See also `org-refile-use-outline-path'.
  10556. If you are using target caching (see `org-refile-use-cache'), you
  10557. have to clear the target cache in order to find new targets.
  10558. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10559. prefix argument (`C-u C-u C-u C-c C-w')."
  10560. (interactive "P")
  10561. (if (member arg '(0 (64)))
  10562. (org-refile-cache-clear)
  10563. (let* ((actionmsg (cond (msg msg)
  10564. ((equal arg 3) "Refile (and keep)")
  10565. (t "Refile")))
  10566. (regionp (org-region-active-p))
  10567. (region-start (and regionp (region-beginning)))
  10568. (region-end (and regionp (region-end)))
  10569. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10570. pos it nbuf file level reversed)
  10571. (setq last-command nil)
  10572. (when regionp
  10573. (goto-char region-start)
  10574. (or (bolp) (goto-char (point-at-bol)))
  10575. (setq region-start (point))
  10576. (unless (or (org-kill-is-subtree-p
  10577. (buffer-substring region-start region-end))
  10578. (prog1 org-refile-active-region-within-subtree
  10579. (let ((s (point-at-eol)))
  10580. (org-toggle-heading)
  10581. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10582. (user-error "The region is not a (sequence of) subtree(s)")))
  10583. (if (equal arg '(16))
  10584. (org-refile-goto-last-stored)
  10585. (when (or
  10586. (and (equal arg 2)
  10587. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10588. (prog1
  10589. (setq it (list (or org-clock-heading "running clock")
  10590. (buffer-file-name
  10591. (marker-buffer org-clock-hd-marker))
  10592. ""
  10593. (marker-position org-clock-hd-marker)))
  10594. (setq arg nil)))
  10595. (setq it (or rfloc
  10596. (let (heading-text)
  10597. (save-excursion
  10598. (unless (and arg (listp arg))
  10599. (org-back-to-heading t)
  10600. (setq heading-text
  10601. (replace-regexp-in-string
  10602. org-bracket-link-regexp
  10603. "\\3"
  10604. (nth 4 (org-heading-components)))))
  10605. (org-refile-get-location
  10606. (cond ((and arg (listp arg)) "Goto")
  10607. (regionp (concat actionmsg " region to"))
  10608. (t (concat actionmsg " subtree \""
  10609. heading-text "\" to")))
  10610. default-buffer
  10611. (and (not (equal '(4) arg))
  10612. org-refile-allow-creating-parent-nodes)
  10613. arg))))))
  10614. (setq file (nth 1 it)
  10615. pos (nth 3 it))
  10616. (when (and (not arg)
  10617. pos
  10618. (equal (buffer-file-name) file)
  10619. (if regionp
  10620. (and (>= pos region-start)
  10621. (<= pos region-end))
  10622. (and (>= pos (point))
  10623. (< pos (save-excursion
  10624. (org-end-of-subtree t t))))))
  10625. (error "Cannot refile to position inside the tree or region"))
  10626. (setq nbuf (or (find-buffer-visiting file)
  10627. (find-file-noselect file)))
  10628. (if (and arg (not (equal arg 3)))
  10629. (progn
  10630. (org-pop-to-buffer-same-window nbuf)
  10631. (goto-char pos)
  10632. (org-show-context 'org-goto))
  10633. (if regionp
  10634. (progn
  10635. (org-kill-new (buffer-substring region-start region-end))
  10636. (org-save-markers-in-region region-start region-end))
  10637. (org-copy-subtree 1 nil t))
  10638. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10639. (find-file-noselect file)))
  10640. (setq reversed (org-notes-order-reversed-p))
  10641. (save-excursion
  10642. (save-restriction
  10643. (widen)
  10644. (if pos
  10645. (progn
  10646. (goto-char pos)
  10647. (looking-at org-outline-regexp)
  10648. (setq level (org-get-valid-level (funcall outline-level) 1))
  10649. (goto-char
  10650. (if reversed
  10651. (or (outline-next-heading) (point-max))
  10652. (or (save-excursion (org-get-next-sibling))
  10653. (org-end-of-subtree t t)
  10654. (point-max)))))
  10655. (setq level 1)
  10656. (if (not reversed)
  10657. (goto-char (point-max))
  10658. (goto-char (point-min))
  10659. (or (outline-next-heading) (goto-char (point-max)))))
  10660. (unless (bolp) (newline))
  10661. (org-paste-subtree level nil nil t)
  10662. (when org-log-refile
  10663. (org-add-log-setup 'refile nil nil org-log-refile)
  10664. (unless (eq org-log-refile 'note)
  10665. (save-excursion (org-add-log-note))))
  10666. (and org-auto-align-tags
  10667. (let ((org-loop-over-headlines-in-active-region nil))
  10668. (org-set-tags nil t)))
  10669. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10670. :last-refile)))
  10671. (when bookmark-name
  10672. (with-demoted-errors
  10673. (bookmark-set bookmark-name))))
  10674. ;; If we are refiling for capture, make sure that the
  10675. ;; last-capture pointers point here
  10676. (when (org-bound-and-true-p org-capture-is-refiling)
  10677. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10678. :last-capture-marker)))
  10679. (when bookmark-name
  10680. (with-demoted-errors
  10681. (bookmark-set bookmark-name))))
  10682. (move-marker org-capture-last-stored-marker (point)))
  10683. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10684. (run-hooks 'org-after-refile-insert-hook))))
  10685. (unless org-refile-keep
  10686. (if regionp
  10687. (delete-region (point) (+ (point) (- region-end region-start)))
  10688. (delete-region
  10689. (and (org-back-to-heading t) (point))
  10690. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10691. (when (featurep 'org-inlinetask)
  10692. (org-inlinetask-remove-END-maybe))
  10693. (setq org-markers-to-move nil)
  10694. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10695. (defun org-refile-goto-last-stored ()
  10696. "Go to the location where the last refile was stored."
  10697. (interactive)
  10698. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10699. (message "This is the location of the last refile"))
  10700. (defun org-refile--get-location (refloc tbl)
  10701. "When user refile to REFLOC, find the associated target in TBL.
  10702. Also check `org-refile-target-table'."
  10703. (car (delq
  10704. nil
  10705. (mapcar
  10706. (lambda (r) (or (assoc r tbl)
  10707. (assoc r org-refile-target-table)))
  10708. (list (replace-regexp-in-string "/$" "" refloc)
  10709. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10710. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10711. no-exclude)
  10712. "Prompt the user for a refile location, using PROMPT.
  10713. PROMPT should not be suffixed with a colon and a space, because
  10714. this function appends the default value from
  10715. `org-refile-history' automatically, if that is not empty.
  10716. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10717. this is used for the GOTO interface."
  10718. (let ((org-refile-targets org-refile-targets)
  10719. (org-refile-use-outline-path org-refile-use-outline-path)
  10720. excluded-entries)
  10721. (when (and (derived-mode-p 'org-mode)
  10722. (not org-refile-use-cache)
  10723. (not no-exclude))
  10724. (org-map-tree
  10725. (lambda()
  10726. (setq excluded-entries
  10727. (append excluded-entries (list (org-get-heading t t)))))))
  10728. (setq org-refile-target-table
  10729. (org-refile-get-targets default-buffer excluded-entries)))
  10730. (unless org-refile-target-table
  10731. (user-error "No refile targets"))
  10732. (let* ((cbuf (current-buffer))
  10733. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10734. (cfunc (if (and org-refile-use-outline-path
  10735. org-outline-path-complete-in-steps)
  10736. #'org-olpath-completing-read
  10737. #'completing-read))
  10738. (extra (if org-refile-use-outline-path "/" ""))
  10739. (cbnex (concat (buffer-name) extra))
  10740. (filename (and cfn (expand-file-name cfn)))
  10741. (tbl (mapcar
  10742. (lambda (x)
  10743. (if (and (not (member org-refile-use-outline-path
  10744. '(file full-file-path)))
  10745. (not (equal filename (nth 1 x))))
  10746. (cons (concat (car x) extra " ("
  10747. (file-name-nondirectory (nth 1 x)) ")")
  10748. (cdr x))
  10749. (cons (concat (car x) extra) (cdr x))))
  10750. org-refile-target-table))
  10751. (completion-ignore-case t)
  10752. cdef
  10753. (prompt (concat prompt
  10754. (or (and (car org-refile-history)
  10755. (concat " (default " (car org-refile-history) ")"))
  10756. (and (assoc cbnex tbl) (setq cdef cbnex)
  10757. (concat " (default " cbnex ")"))) ": "))
  10758. pa answ parent-target child parent old-hist)
  10759. (setq old-hist org-refile-history)
  10760. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10761. nil 'org-refile-history (or cdef (car org-refile-history))))
  10762. (if (setq pa (org-refile--get-location answ tbl))
  10763. (progn
  10764. (org-refile-check-position pa)
  10765. (when (or (not org-refile-history)
  10766. (not (eq old-hist org-refile-history))
  10767. (not (equal (car pa) (car org-refile-history))))
  10768. (setq org-refile-history
  10769. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10770. org-refile-history
  10771. (cdr org-refile-history))))
  10772. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10773. (pop org-refile-history)))
  10774. pa)
  10775. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10776. (progn
  10777. (setq parent (match-string 1 answ)
  10778. child (match-string 2 answ))
  10779. (setq parent-target (org-refile--get-location parent tbl))
  10780. (when (and parent-target
  10781. (or (eq new-nodes t)
  10782. (and (eq new-nodes 'confirm)
  10783. (y-or-n-p (format "Create new node \"%s\"? "
  10784. child)))))
  10785. (org-refile-new-child parent-target child)))
  10786. (user-error "Invalid target location")))))
  10787. (declare-function org-string-nw-p "org-macs" (s))
  10788. (defun org-refile-check-position (refile-pointer)
  10789. "Check if the refile pointer matches the headline to which it points."
  10790. (let* ((file (nth 1 refile-pointer))
  10791. (re (nth 2 refile-pointer))
  10792. (pos (nth 3 refile-pointer))
  10793. buffer)
  10794. (if (and (not (markerp pos)) (not file))
  10795. (user-error "Please indicate a target file in the refile path")
  10796. (when (org-string-nw-p re)
  10797. (setq buffer (if (markerp pos)
  10798. (marker-buffer pos)
  10799. (or (find-buffer-visiting file)
  10800. (find-file-noselect file))))
  10801. (with-current-buffer buffer
  10802. (save-excursion
  10803. (save-restriction
  10804. (widen)
  10805. (goto-char pos)
  10806. (beginning-of-line 1)
  10807. (unless (org-looking-at-p re)
  10808. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10809. (defun org-refile-new-child (parent-target child)
  10810. "Use refile target PARENT-TARGET to add new CHILD below it."
  10811. (unless parent-target
  10812. (error "Cannot find parent for new node"))
  10813. (let ((file (nth 1 parent-target))
  10814. (pos (nth 3 parent-target))
  10815. level)
  10816. (with-current-buffer (or (find-buffer-visiting file)
  10817. (find-file-noselect file))
  10818. (save-excursion
  10819. (save-restriction
  10820. (widen)
  10821. (if pos
  10822. (goto-char pos)
  10823. (goto-char (point-max))
  10824. (unless (bolp) (newline)))
  10825. (when (looking-at org-outline-regexp)
  10826. (setq level (funcall outline-level))
  10827. (org-end-of-subtree t t))
  10828. (org-back-over-empty-lines)
  10829. (insert "\n" (make-string
  10830. (if pos (org-get-valid-level level 1) 1) ?*)
  10831. " " child "\n")
  10832. (beginning-of-line 0)
  10833. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10834. (defun org-olpath-completing-read (prompt collection &rest args)
  10835. "Read an outline path like a file name."
  10836. (let ((thetable collection))
  10837. (apply #'completing-read
  10838. prompt
  10839. (lambda (string predicate &optional flag)
  10840. (cond
  10841. ((eq flag nil) (try-completion string thetable))
  10842. ((eq flag t)
  10843. (let ((l (length string)))
  10844. (mapcar (lambda (x)
  10845. (let ((r (substring x l))
  10846. (f (if (string-match " ([^)]*)$" x)
  10847. (match-string 0 x)
  10848. "")))
  10849. (if (string-match "/" r)
  10850. (concat string (substring r 0 (match-end 0)) f)
  10851. x)))
  10852. (all-completions string thetable predicate))))
  10853. ;; Exact match?
  10854. ((eq flag 'lambda) (assoc string thetable))))
  10855. args)))
  10856. ;;;; Dynamic blocks
  10857. (defun org-find-dblock (name)
  10858. "Find the first dynamic block with name NAME in the buffer.
  10859. If not found, stay at current position and return nil."
  10860. (let ((case-fold-search t) pos)
  10861. (save-excursion
  10862. (goto-char (point-min))
  10863. (setq pos (and (re-search-forward
  10864. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10865. (match-beginning 0))))
  10866. (when pos (goto-char pos))
  10867. pos))
  10868. (defun org-create-dblock (plist)
  10869. "Create a dynamic block section, with parameters taken from PLIST.
  10870. PLIST must contain a :name entry which is used as the name of the block."
  10871. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10872. (end-of-line 1)
  10873. (newline))
  10874. (let ((col (current-column))
  10875. (name (plist-get plist :name)))
  10876. (insert "#+BEGIN: " name)
  10877. (while plist
  10878. (if (eq (car plist) :name)
  10879. (setq plist (cddr plist))
  10880. (insert " " (prin1-to-string (pop plist)))))
  10881. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10882. (beginning-of-line -2)))
  10883. (defun org-prepare-dblock ()
  10884. "Prepare dynamic block for refresh.
  10885. This empties the block, puts the cursor at the insert position and returns
  10886. the property list including an extra property :name with the block name."
  10887. (unless (looking-at org-dblock-start-re)
  10888. (user-error "Not at a dynamic block"))
  10889. (let* ((begdel (1+ (match-end 0)))
  10890. (name (org-no-properties (match-string 1)))
  10891. (params (append (list :name name)
  10892. (read (concat "(" (match-string 3) ")")))))
  10893. (save-excursion
  10894. (beginning-of-line 1)
  10895. (skip-chars-forward " \t")
  10896. (setq params (plist-put params :indentation-column (current-column))))
  10897. (unless (re-search-forward org-dblock-end-re nil t)
  10898. (error "Dynamic block not terminated"))
  10899. (setq params
  10900. (append params
  10901. (list :content (buffer-substring
  10902. begdel (match-beginning 0)))))
  10903. (delete-region begdel (match-beginning 0))
  10904. (goto-char begdel)
  10905. (open-line 1)
  10906. params))
  10907. (defun org-map-dblocks (&optional command)
  10908. "Apply COMMAND to all dynamic blocks in the current buffer.
  10909. If COMMAND is not given, use `org-update-dblock'."
  10910. (let ((cmd (or command 'org-update-dblock)))
  10911. (save-excursion
  10912. (goto-char (point-min))
  10913. (while (re-search-forward org-dblock-start-re nil t)
  10914. (goto-char (match-beginning 0))
  10915. (save-excursion
  10916. (condition-case nil
  10917. (funcall cmd)
  10918. (error (message "Error during update of dynamic block"))))
  10919. (unless (re-search-forward org-dblock-end-re nil t)
  10920. (error "Dynamic block not terminated"))))))
  10921. (defun org-dblock-update (&optional arg)
  10922. "User command for updating dynamic blocks.
  10923. Update the dynamic block at point. With prefix ARG, update all dynamic
  10924. blocks in the buffer."
  10925. (interactive "P")
  10926. (if arg
  10927. (org-update-all-dblocks)
  10928. (or (looking-at org-dblock-start-re)
  10929. (org-beginning-of-dblock))
  10930. (org-update-dblock)))
  10931. (defun org-update-dblock ()
  10932. "Update the dynamic block at point.
  10933. This means to empty the block, parse for parameters and then call
  10934. the correct writing function."
  10935. (interactive)
  10936. (save-excursion
  10937. (let* ((win (selected-window))
  10938. (pos (point))
  10939. (line (org-current-line))
  10940. (params (org-prepare-dblock))
  10941. (name (plist-get params :name))
  10942. (indent (plist-get params :indentation-column))
  10943. (cmd (intern (concat "org-dblock-write:" name))))
  10944. (message "Updating dynamic block `%s' at line %d..." name line)
  10945. (funcall cmd params)
  10946. (message "Updating dynamic block `%s' at line %d...done" name line)
  10947. (goto-char pos)
  10948. (when (and indent (> indent 0))
  10949. (setq indent (make-string indent ?\ ))
  10950. (save-excursion
  10951. (select-window win)
  10952. (org-beginning-of-dblock)
  10953. (forward-line 1)
  10954. (while (not (looking-at org-dblock-end-re))
  10955. (insert indent)
  10956. (beginning-of-line 2))
  10957. (when (looking-at org-dblock-end-re)
  10958. (and (looking-at "[ \t]+")
  10959. (replace-match ""))
  10960. (insert indent)))))))
  10961. (defun org-beginning-of-dblock ()
  10962. "Find the beginning of the dynamic block at point.
  10963. Error if there is no such block at point."
  10964. (let ((pos (point))
  10965. beg)
  10966. (end-of-line 1)
  10967. (if (and (re-search-backward org-dblock-start-re nil t)
  10968. (setq beg (match-beginning 0))
  10969. (re-search-forward org-dblock-end-re nil t)
  10970. (> (match-end 0) pos))
  10971. (goto-char beg)
  10972. (goto-char pos)
  10973. (error "Not in a dynamic block"))))
  10974. (defun org-update-all-dblocks ()
  10975. "Update all dynamic blocks in the buffer.
  10976. This function can be used in a hook."
  10977. (interactive)
  10978. (when (derived-mode-p 'org-mode)
  10979. (org-map-dblocks 'org-update-dblock)))
  10980. ;;;; Completion
  10981. (declare-function org-export-backend-name "org-export" (cl-x))
  10982. (declare-function org-export-backend-options "org-export" (cl-x))
  10983. (defun org-get-export-keywords ()
  10984. "Return a list of all currently understood export keywords.
  10985. Export keywords include options, block names, attributes and
  10986. keywords relative to each registered export back-end."
  10987. (let (keywords)
  10988. (dolist (backend
  10989. (org-bound-and-true-p org-export-registered-backends)
  10990. (delq nil keywords))
  10991. ;; Back-end name (for keywords, like #+LATEX:)
  10992. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10993. (dolist (option-entry (org-export-backend-options backend))
  10994. ;; Back-end options.
  10995. (push (nth 1 option-entry) keywords)))))
  10996. (defconst org-options-keywords
  10997. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10998. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10999. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11000. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11001. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11002. (defcustom org-structure-template-alist
  11003. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11004. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11005. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11006. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11007. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11008. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11009. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11010. ("L" "#+LaTeX: ")
  11011. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11012. ("H" "#+HTML: ")
  11013. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11014. ("A" "#+ASCII: ")
  11015. ("i" "#+INDEX: ?")
  11016. ("I" "#+INCLUDE: %file ?"))
  11017. "Structure completion elements.
  11018. This is a list of abbreviation keys and values. The value gets inserted
  11019. if you type `<' followed by the key and then press the completion key,
  11020. usually `TAB'. %file will be replaced by a file name after prompting
  11021. for the file using completion. The cursor will be placed at the position
  11022. of the `?' in the template.
  11023. There are two templates for each key, the first uses the original Org syntax,
  11024. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11025. the default when the /org-mtags.el/ module has been loaded. See also the
  11026. variable `org-mtags-prefer-muse-templates'."
  11027. :group 'org-completion
  11028. :type '(repeat
  11029. (list
  11030. (string :tag "Key")
  11031. (string :tag "Template")))
  11032. :version "25.1"
  11033. :package-version '(Org . "8.3"))
  11034. (defun org-try-structure-completion ()
  11035. "Try to complete a structure template before point.
  11036. This looks for strings like \"<e\" on an otherwise empty line and
  11037. expands them."
  11038. (let ((l (buffer-substring (point-at-bol) (point)))
  11039. a)
  11040. (when (and (looking-at "[ \t]*$")
  11041. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11042. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11043. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11044. (match-beginning 1)) a)
  11045. t)))
  11046. (defun org-complete-expand-structure-template (start cell)
  11047. "Expand a structure template."
  11048. (let ((rpl (nth 1 cell))
  11049. (ind ""))
  11050. (delete-region start (point))
  11051. (when (string-match "\\`[ \t]*#\\+" rpl)
  11052. (cond
  11053. ((bolp))
  11054. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11055. (setq ind (buffer-substring (point-at-bol) (point))))
  11056. (t (newline))))
  11057. (setq start (point))
  11058. (when (string-match "%file" rpl)
  11059. (setq rpl (replace-match
  11060. (concat
  11061. "\""
  11062. (save-match-data
  11063. (abbreviate-file-name (read-file-name "Include file: ")))
  11064. "\"")
  11065. t t rpl)))
  11066. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11067. (concat "\n" ind)))
  11068. (insert rpl)
  11069. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11070. ;;;; TODO, DEADLINE, Comments
  11071. (defun org-toggle-comment ()
  11072. "Change the COMMENT state of an entry."
  11073. (interactive)
  11074. (save-excursion
  11075. (org-back-to-heading)
  11076. (looking-at org-complex-heading-regexp)
  11077. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11078. (skip-chars-forward " \t")
  11079. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11080. (if (org-in-commented-heading-p t)
  11081. (delete-region (point)
  11082. (progn (search-forward " " (line-end-position) 'move)
  11083. (skip-chars-forward " \t")
  11084. (point)))
  11085. (insert org-comment-string)
  11086. (unless (eolp) (insert " ")))))
  11087. (defvar org-last-todo-state-is-todo nil
  11088. "This is non-nil when the last TODO state change led to a TODO state.
  11089. If the last change removed the TODO tag or switched to DONE, then
  11090. this is nil.")
  11091. (defvar org-setting-tags nil) ; dynamically skipped
  11092. (defvar org-todo-setup-filter-hook nil
  11093. "Hook for functions that pre-filter todo specs.
  11094. Each function takes a todo spec and returns either nil or the spec
  11095. transformed into canonical form." )
  11096. (defvar org-todo-get-default-hook nil
  11097. "Hook for functions that get a default item for todo.
  11098. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11099. nil or a string to be used for the todo mark." )
  11100. (defvar org-agenda-headline-snapshot-before-repeat)
  11101. (defun org-current-effective-time ()
  11102. "Return current time adjusted for `org-extend-today-until' variable."
  11103. (let* ((ct (org-current-time))
  11104. (dct (decode-time ct))
  11105. (ct1
  11106. (cond
  11107. (org-use-last-clock-out-time-as-effective-time
  11108. (or (org-clock-get-last-clock-out-time) ct))
  11109. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11110. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11111. (t ct))))
  11112. ct1))
  11113. (defun org-todo-yesterday (&optional arg)
  11114. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11115. (interactive "P")
  11116. (if (eq major-mode 'org-agenda-mode)
  11117. (apply 'org-agenda-todo-yesterday arg)
  11118. (let* ((org-use-effective-time t)
  11119. (hour (nth 2 (decode-time (org-current-time))))
  11120. (org-extend-today-until (1+ hour)))
  11121. (org-todo arg))))
  11122. (defvar org-block-entry-blocking ""
  11123. "First entry preventing the TODO state change.")
  11124. (defun org-cancel-repeater ()
  11125. "Cancel a repeater by setting its numeric value to zero."
  11126. (interactive)
  11127. (save-excursion
  11128. (org-back-to-heading t)
  11129. (let ((bound1 (point))
  11130. (bound0 (save-excursion (outline-next-heading) (point))))
  11131. (when (and (re-search-forward
  11132. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11133. org-deadline-time-regexp "\\)\\|\\("
  11134. org-ts-regexp "\\)")
  11135. bound0 t)
  11136. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11137. bound1 t))
  11138. (replace-match "0" t nil nil 1)))))
  11139. (defvar org-state) ;; dynamically scoped into this function
  11140. (defun org-todo (&optional arg)
  11141. "Change the TODO state of an item.
  11142. The state of an item is given by a keyword at the start of the heading,
  11143. like
  11144. *** TODO Write paper
  11145. *** DONE Call mom
  11146. The different keywords are specified in the variable `org-todo-keywords'.
  11147. By default the available states are \"TODO\" and \"DONE\".
  11148. So for this example: when the item starts with TODO, it is changed to DONE.
  11149. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11150. DONE are present, add TODO at the beginning of the heading.
  11151. With \\[universal-argument] prefix arg, use completion to determine the new \
  11152. state.
  11153. With numeric prefix arg, switch to that state.
  11154. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11155. keywords (nextset).
  11156. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11157. With a numeric prefix arg of 0, inhibit note taking for the change.
  11158. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11159. When called through ELisp, arg is also interpreted in the following way:
  11160. `none' -> empty state
  11161. \"\"(empty string) -> switch to empty state
  11162. `done' -> switch to DONE
  11163. `nextset' -> switch to the next set of keywords
  11164. `previousset' -> switch to the previous set of keywords
  11165. \"WAITING\" -> switch to the specified keyword, but only if it
  11166. really is a member of `org-todo-keywords'."
  11167. (interactive "P")
  11168. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11169. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11170. 'region-start-level 'region))
  11171. org-loop-over-headlines-in-active-region)
  11172. (org-map-entries
  11173. `(org-todo ,arg)
  11174. org-loop-over-headlines-in-active-region
  11175. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11176. (when (equal arg '(16)) (setq arg 'nextset))
  11177. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11178. (let ((org-blocker-hook org-blocker-hook)
  11179. commentp
  11180. case-fold-search)
  11181. (when (equal arg '(64))
  11182. (setq arg nil org-blocker-hook nil))
  11183. (when (and org-blocker-hook
  11184. (or org-inhibit-blocking
  11185. (org-entry-get nil "NOBLOCKING")))
  11186. (setq org-blocker-hook nil))
  11187. (save-excursion
  11188. (catch 'exit
  11189. (org-back-to-heading t)
  11190. (when (org-in-commented-heading-p t)
  11191. (org-toggle-comment)
  11192. (setq commentp t))
  11193. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11194. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11195. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11196. (let* ((match-data (match-data))
  11197. (startpos (point-at-bol))
  11198. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11199. (org-log-done org-log-done)
  11200. (org-log-repeat org-log-repeat)
  11201. (org-todo-log-states org-todo-log-states)
  11202. (org-inhibit-logging
  11203. (if (equal arg 0)
  11204. (progn (setq arg nil) 'note) org-inhibit-logging))
  11205. (this (match-string 1))
  11206. (hl-pos (match-beginning 0))
  11207. (head (org-get-todo-sequence-head this))
  11208. (ass (assoc head org-todo-kwd-alist))
  11209. (interpret (nth 1 ass))
  11210. (done-word (nth 3 ass))
  11211. (final-done-word (nth 4 ass))
  11212. (org-last-state (or this ""))
  11213. (completion-ignore-case t)
  11214. (member (member this org-todo-keywords-1))
  11215. (tail (cdr member))
  11216. (org-state (cond
  11217. ((and org-todo-key-trigger
  11218. (or (and (equal arg '(4))
  11219. (eq org-use-fast-todo-selection 'prefix))
  11220. (and (not arg) org-use-fast-todo-selection
  11221. (not (eq org-use-fast-todo-selection
  11222. 'prefix)))))
  11223. ;; Use fast selection
  11224. (org-fast-todo-selection))
  11225. ((and (equal arg '(4))
  11226. (or (not org-use-fast-todo-selection)
  11227. (not org-todo-key-trigger)))
  11228. ;; Read a state with completion
  11229. (completing-read
  11230. "State: " (mapcar #'list org-todo-keywords-1)
  11231. nil t))
  11232. ((eq arg 'right)
  11233. (if this
  11234. (if tail (car tail) nil)
  11235. (car org-todo-keywords-1)))
  11236. ((eq arg 'left)
  11237. (unless (equal member org-todo-keywords-1)
  11238. (if this
  11239. (nth (- (length org-todo-keywords-1)
  11240. (length tail) 2)
  11241. org-todo-keywords-1)
  11242. (org-last org-todo-keywords-1))))
  11243. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11244. (setq arg nil))) ; hack to fall back to cycling
  11245. (arg
  11246. ;; user or caller requests a specific state
  11247. (cond
  11248. ((equal arg "") nil)
  11249. ((eq arg 'none) nil)
  11250. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11251. ((eq arg 'nextset)
  11252. (or (car (cdr (member head org-todo-heads)))
  11253. (car org-todo-heads)))
  11254. ((eq arg 'previousset)
  11255. (let ((org-todo-heads (reverse org-todo-heads)))
  11256. (or (car (cdr (member head org-todo-heads)))
  11257. (car org-todo-heads))))
  11258. ((car (member arg org-todo-keywords-1)))
  11259. ((stringp arg)
  11260. (user-error "State `%s' not valid in this file" arg))
  11261. ((nth (1- (prefix-numeric-value arg))
  11262. org-todo-keywords-1))))
  11263. ((null member) (or head (car org-todo-keywords-1)))
  11264. ((equal this final-done-word) nil) ;; -> make empty
  11265. ((null tail) nil) ;; -> first entry
  11266. ((memq interpret '(type priority))
  11267. (if (eq this-command last-command)
  11268. (car tail)
  11269. (if (> (length tail) 0)
  11270. (or done-word (car org-done-keywords))
  11271. nil)))
  11272. (t
  11273. (car tail))))
  11274. (org-state (or
  11275. (run-hook-with-args-until-success
  11276. 'org-todo-get-default-hook org-state org-last-state)
  11277. org-state))
  11278. (next (if org-state (concat " " org-state " ") " "))
  11279. (change-plist (list :type 'todo-state-change :from this :to org-state
  11280. :position startpos))
  11281. dolog now-done-p)
  11282. (when org-blocker-hook
  11283. (setq org-last-todo-state-is-todo
  11284. (not (member this org-done-keywords)))
  11285. (unless (save-excursion
  11286. (save-match-data
  11287. (org-with-wide-buffer
  11288. (run-hook-with-args-until-failure
  11289. 'org-blocker-hook change-plist))))
  11290. (if (org-called-interactively-p 'interactive)
  11291. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11292. this org-state org-block-entry-blocking)
  11293. ;; fail silently
  11294. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11295. this org-state org-block-entry-blocking)
  11296. (throw 'exit nil))))
  11297. (store-match-data match-data)
  11298. (replace-match next t t)
  11299. (cond ((equal this org-state)
  11300. (message "TODO state was already %s" (org-trim next)))
  11301. ((pos-visible-in-window-p hl-pos)
  11302. (message "TODO state changed to %s" (org-trim next))))
  11303. (unless head
  11304. (setq head (org-get-todo-sequence-head org-state)
  11305. ass (assoc head org-todo-kwd-alist)
  11306. interpret (nth 1 ass)
  11307. done-word (nth 3 ass)
  11308. final-done-word (nth 4 ass)))
  11309. (when (memq arg '(nextset previousset))
  11310. (message "Keyword-Set %d/%d: %s"
  11311. (- (length org-todo-sets) -1
  11312. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11313. (length org-todo-sets)
  11314. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11315. (setq org-last-todo-state-is-todo
  11316. (not (member org-state org-done-keywords)))
  11317. (setq now-done-p (and (member org-state org-done-keywords)
  11318. (not (member this org-done-keywords))))
  11319. (and logging (org-local-logging logging))
  11320. (when (and (or org-todo-log-states org-log-done)
  11321. (not (eq org-inhibit-logging t))
  11322. (not (memq arg '(nextset previousset))))
  11323. ;; we need to look at recording a time and note
  11324. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11325. (nth 2 (assoc this org-todo-log-states))))
  11326. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11327. (setq dolog 'time))
  11328. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11329. (and org-state
  11330. (member org-state org-not-done-keywords)
  11331. (not (member this org-not-done-keywords))))
  11332. ;; This is now a todo state and was not one before
  11333. ;; If there was a CLOSED time stamp, get rid of it.
  11334. (org-add-planning-info nil nil 'closed))
  11335. (when (and now-done-p org-log-done)
  11336. ;; It is now done, and it was not done before
  11337. (org-add-planning-info 'closed (org-current-effective-time))
  11338. (when (and (not dolog) (eq 'note org-log-done))
  11339. (org-add-log-setup 'done org-state this 'note)))
  11340. (when (and org-state dolog)
  11341. ;; This is a non-nil state, and we need to log it
  11342. (org-add-log-setup 'state org-state this dolog)))
  11343. ;; Fixup tag positioning
  11344. (org-todo-trigger-tag-changes org-state)
  11345. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11346. (when org-provide-todo-statistics
  11347. (org-update-parent-todo-statistics))
  11348. (run-hooks 'org-after-todo-state-change-hook)
  11349. (when (and arg (not (member org-state org-done-keywords)))
  11350. (setq head (org-get-todo-sequence-head org-state)))
  11351. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11352. ;; Do we need to trigger a repeat?
  11353. (when now-done-p
  11354. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11355. ;; This is for the agenda, take a snapshot of the headline.
  11356. (save-match-data
  11357. (setq org-agenda-headline-snapshot-before-repeat
  11358. (org-get-heading))))
  11359. (org-auto-repeat-maybe org-state))
  11360. ;; Fixup cursor location if close to the keyword
  11361. (when (and (outline-on-heading-p)
  11362. (not (bolp))
  11363. (save-excursion (beginning-of-line 1)
  11364. (looking-at org-todo-line-regexp))
  11365. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11366. (goto-char (or (match-end 2) (match-end 1)))
  11367. (and (looking-at " ") (just-one-space)))
  11368. (when org-trigger-hook
  11369. (save-excursion
  11370. (run-hook-with-args 'org-trigger-hook change-plist)))
  11371. (when commentp (org-toggle-comment))))))))
  11372. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11373. "Block turning an entry into a TODO, using the hierarchy.
  11374. This checks whether the current task should be blocked from state
  11375. changes. Such blocking occurs when:
  11376. 1. The task has children which are not all in a completed state.
  11377. 2. A task has a parent with the property :ORDERED:, and there
  11378. are siblings prior to the current task with incomplete
  11379. status.
  11380. 3. The parent of the task is blocked because it has siblings that should
  11381. be done first, or is child of a block grandparent TODO entry."
  11382. (if (not org-enforce-todo-dependencies)
  11383. t ; if locally turned off don't block
  11384. (catch 'dont-block
  11385. ;; If this is not a todo state change, or if this entry is already DONE,
  11386. ;; do not block
  11387. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11388. (member (plist-get change-plist :from)
  11389. (cons 'done org-done-keywords))
  11390. (member (plist-get change-plist :to)
  11391. (cons 'todo org-not-done-keywords))
  11392. (not (plist-get change-plist :to)))
  11393. (throw 'dont-block t))
  11394. ;; If this task has children, and any are undone, it's blocked
  11395. (save-excursion
  11396. (org-back-to-heading t)
  11397. (let ((this-level (funcall outline-level)))
  11398. (outline-next-heading)
  11399. (let ((child-level (funcall outline-level)))
  11400. (while (and (not (eobp))
  11401. (> child-level this-level))
  11402. ;; this todo has children, check whether they are all
  11403. ;; completed
  11404. (when (and (not (org-entry-is-done-p))
  11405. (org-entry-is-todo-p))
  11406. (setq org-block-entry-blocking (org-get-heading))
  11407. (throw 'dont-block nil))
  11408. (outline-next-heading)
  11409. (setq child-level (funcall outline-level))))))
  11410. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11411. ;; any previous siblings are undone, it's blocked
  11412. (save-excursion
  11413. (org-back-to-heading t)
  11414. (let* ((pos (point))
  11415. (parent-pos (and (org-up-heading-safe) (point))))
  11416. (unless parent-pos (throw 'dont-block t)) ; no parent
  11417. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11418. (forward-line 1)
  11419. (re-search-forward org-not-done-heading-regexp pos t))
  11420. (setq org-block-entry-blocking (match-string 0))
  11421. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11422. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11423. (while t
  11424. (goto-char parent-pos)
  11425. (unless (looking-at org-not-done-heading-regexp)
  11426. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11427. (setq pos (point))
  11428. (setq parent-pos (and (org-up-heading-safe) (point)))
  11429. (unless parent-pos (throw 'dont-block t)) ; no parent
  11430. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11431. (forward-line 1)
  11432. (re-search-forward org-not-done-heading-regexp pos t)
  11433. (setq org-block-entry-blocking (org-get-heading)))
  11434. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11435. (defcustom org-track-ordered-property-with-tag nil
  11436. "Should the ORDERED property also be shown as a tag?
  11437. The ORDERED property decides if an entry should require subtasks to be
  11438. completed in sequence. Since a property is not very visible, setting
  11439. this option means that toggling the ORDERED property with the command
  11440. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11441. not relevant for the behavior, but it makes things more visible.
  11442. Note that toggling the tag with tags commands will not change the property
  11443. and therefore not influence behavior!
  11444. This can be t, meaning the tag ORDERED should be used, It can also be a
  11445. string to select a different tag for this task."
  11446. :group 'org-todo
  11447. :type '(choice
  11448. (const :tag "No tracking" nil)
  11449. (const :tag "Track with ORDERED tag" t)
  11450. (string :tag "Use other tag")))
  11451. (defun org-toggle-ordered-property ()
  11452. "Toggle the ORDERED property of the current entry.
  11453. For better visibility, you can track the value of this property with a tag.
  11454. See variable `org-track-ordered-property-with-tag'."
  11455. (interactive)
  11456. (let* ((t1 org-track-ordered-property-with-tag)
  11457. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11458. (save-excursion
  11459. (org-back-to-heading)
  11460. (if (org-entry-get nil "ORDERED")
  11461. (progn
  11462. (org-delete-property "ORDERED")
  11463. (and tag (org-toggle-tag tag 'off))
  11464. (message "Subtasks can be completed in arbitrary order"))
  11465. (org-entry-put nil "ORDERED" "t")
  11466. (and tag (org-toggle-tag tag 'on))
  11467. (message "Subtasks must be completed in sequence")))))
  11468. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11469. (defun org-block-todo-from-checkboxes (change-plist)
  11470. "Block turning an entry into a TODO, using checkboxes.
  11471. This checks whether the current task should be blocked from state
  11472. changes because there are unchecked boxes in this entry."
  11473. (if (not org-enforce-todo-checkbox-dependencies)
  11474. t ; if locally turned off don't block
  11475. (catch 'dont-block
  11476. ;; If this is not a todo state change, or if this entry is already DONE,
  11477. ;; do not block
  11478. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11479. (member (plist-get change-plist :from)
  11480. (cons 'done org-done-keywords))
  11481. (member (plist-get change-plist :to)
  11482. (cons 'todo org-not-done-keywords))
  11483. (not (plist-get change-plist :to)))
  11484. (throw 'dont-block t))
  11485. ;; If this task has checkboxes that are not checked, it's blocked
  11486. (save-excursion
  11487. (org-back-to-heading t)
  11488. (let ((beg (point)) end)
  11489. (outline-next-heading)
  11490. (setq end (point))
  11491. (goto-char beg)
  11492. (when (org-list-search-forward
  11493. (concat (org-item-beginning-re)
  11494. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11495. "\\[[- ]\\]")
  11496. end t)
  11497. (when (boundp 'org-blocked-by-checkboxes)
  11498. (setq org-blocked-by-checkboxes t))
  11499. (throw 'dont-block nil))))
  11500. t))) ; do not block
  11501. (defun org-entry-blocked-p ()
  11502. "Non-nil if entry at point is blocked."
  11503. (and (not (org-entry-get nil "NOBLOCKING"))
  11504. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11505. (not (run-hook-with-args-until-failure
  11506. 'org-blocker-hook
  11507. (list :type 'todo-state-change
  11508. :position (point)
  11509. :from 'todo
  11510. :to 'done)))))
  11511. (defun org-update-statistics-cookies (all)
  11512. "Update the statistics cookie, either from TODO or from checkboxes.
  11513. This should be called with the cursor in a line with a statistics cookie."
  11514. (interactive "P")
  11515. (if all
  11516. (progn
  11517. (org-update-checkbox-count 'all)
  11518. (org-map-entries 'org-update-parent-todo-statistics))
  11519. (if (not (org-at-heading-p))
  11520. (org-update-checkbox-count)
  11521. (let ((pos (point-marker))
  11522. end l1 l2)
  11523. (ignore-errors (org-back-to-heading t))
  11524. (if (not (org-at-heading-p))
  11525. (org-update-checkbox-count)
  11526. (setq l1 (org-outline-level))
  11527. (setq end (save-excursion
  11528. (outline-next-heading)
  11529. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11530. (point)))
  11531. (if (and (save-excursion
  11532. (re-search-forward
  11533. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11534. (not (save-excursion (re-search-forward
  11535. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11536. (org-update-checkbox-count)
  11537. (if (and l2 (> l2 l1))
  11538. (progn
  11539. (goto-char end)
  11540. (org-update-parent-todo-statistics))
  11541. (goto-char pos)
  11542. (beginning-of-line 1)
  11543. (while (re-search-forward
  11544. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11545. (point-at-eol) t)
  11546. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11547. (goto-char pos)
  11548. (move-marker pos nil)))))
  11549. (defvar org-entry-property-inherited-from) ;; defined below
  11550. (defun org-update-parent-todo-statistics ()
  11551. "Update any statistics cookie in the parent of the current headline.
  11552. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11553. the entire subtree and this will travel up the hierarchy and update
  11554. statistics everywhere."
  11555. (let* ((prop (save-excursion (org-up-heading-safe)
  11556. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11557. (recursive (or (not org-hierarchical-todo-statistics)
  11558. (and prop (string-match "\\<recursive\\>" prop))))
  11559. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11560. 0))
  11561. (first t)
  11562. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11563. level ltoggle l1 new ndel
  11564. (cnt-all 0) (cnt-done 0) is-percent kwd
  11565. checkbox-beg ov ovs ove cookie-present)
  11566. (catch 'exit
  11567. (save-excursion
  11568. (beginning-of-line 1)
  11569. (setq ltoggle (funcall outline-level))
  11570. ;; Three situations are to consider:
  11571. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11572. ;; to the top-level ancestor on the headline;
  11573. ;; 2. If parent has "recursive" property, repeat up to the
  11574. ;; headline setting that property, taking inheritance into
  11575. ;; account;
  11576. ;; 3. Else, move up to direct parent and proceed only once.
  11577. (while (and (setq level (org-up-heading-safe))
  11578. (or recursive first)
  11579. (>= (point) lim))
  11580. (setq first nil cookie-present nil)
  11581. (unless (and level
  11582. (not (string-match
  11583. "\\<checkbox\\>"
  11584. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11585. "")))))
  11586. (throw 'exit nil))
  11587. (while (re-search-forward box-re (point-at-eol) t)
  11588. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11589. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11590. (save-match-data
  11591. (unless (outline-next-heading) (throw 'exit nil))
  11592. (while (and (looking-at org-complex-heading-regexp)
  11593. (> (setq l1 (length (match-string 1))) level))
  11594. (setq kwd (and (or recursive (= l1 ltoggle))
  11595. (match-string 2)))
  11596. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11597. (and (eq org-provide-todo-statistics t)
  11598. (or (member kwd org-done-keywords)))
  11599. (and (listp org-provide-todo-statistics)
  11600. (stringp (car org-provide-todo-statistics))
  11601. (or (member kwd org-provide-todo-statistics)
  11602. (member kwd org-done-keywords)))
  11603. (and (listp org-provide-todo-statistics)
  11604. (listp (car org-provide-todo-statistics))
  11605. (or (member kwd (car org-provide-todo-statistics))
  11606. (and (member kwd org-done-keywords)
  11607. (member kwd (cadr org-provide-todo-statistics))))))
  11608. (setq cnt-all (1+ cnt-all))
  11609. (and (eq org-provide-todo-statistics t)
  11610. kwd
  11611. (setq cnt-all (1+ cnt-all))))
  11612. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11613. (member kwd org-done-keywords))
  11614. (and (listp org-provide-todo-statistics)
  11615. (listp (car org-provide-todo-statistics))
  11616. (member kwd org-done-keywords)
  11617. (member kwd (cadr org-provide-todo-statistics)))
  11618. (and (listp org-provide-todo-statistics)
  11619. (stringp (car org-provide-todo-statistics))
  11620. (member kwd org-done-keywords)))
  11621. (setq cnt-done (1+ cnt-done)))
  11622. (outline-next-heading)))
  11623. (setq new
  11624. (if is-percent
  11625. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11626. (max 1 cnt-all)))
  11627. (format "[%d/%d]" cnt-done cnt-all))
  11628. ndel (- (match-end 0) checkbox-beg))
  11629. ;; handle overlays when updating cookie from column view
  11630. (when (setq ov (car (overlays-at checkbox-beg)))
  11631. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11632. (delete-overlay ov))
  11633. (goto-char checkbox-beg)
  11634. (insert new)
  11635. (delete-region (point) (+ (point) ndel))
  11636. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11637. (when ov (move-overlay ov ovs ove)))
  11638. (when cookie-present
  11639. (run-hook-with-args 'org-after-todo-statistics-hook
  11640. cnt-done (- cnt-all cnt-done))))))
  11641. (run-hooks 'org-todo-statistics-hook)))
  11642. (defvar org-after-todo-statistics-hook nil
  11643. "Hook that is called after a TODO statistics cookie has been updated.
  11644. Each function is called with two arguments: the number of not-done entries
  11645. and the number of done entries.
  11646. For example, the following function, when added to this hook, will switch
  11647. an entry to DONE when all children are done, and back to TODO when new
  11648. entries are set to a TODO status. Note that this hook is only called
  11649. when there is a statistics cookie in the headline!
  11650. \(defun org-summary-todo (n-done n-not-done)
  11651. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11652. \(let (org-log-done org-log-states) ; turn off logging
  11653. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11654. ")
  11655. (defvar org-todo-statistics-hook nil
  11656. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11657. This hook runs even if there is no statistics cookie present, in which case
  11658. `org-after-todo-statistics-hook' would not run.")
  11659. (defun org-todo-trigger-tag-changes (state)
  11660. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11661. (let ((l org-todo-state-tags-triggers)
  11662. changes)
  11663. (when (or (not state) (equal state ""))
  11664. (setq changes (append changes (cdr (assoc "" l)))))
  11665. (when (and (stringp state) (> (length state) 0))
  11666. (setq changes (append changes (cdr (assoc state l)))))
  11667. (when (member state org-not-done-keywords)
  11668. (setq changes (append changes (cdr (assoc 'todo l)))))
  11669. (when (member state org-done-keywords)
  11670. (setq changes (append changes (cdr (assoc 'done l)))))
  11671. (dolist (c changes)
  11672. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11673. (defun org-local-logging (value)
  11674. "Get logging settings from a property VALUE."
  11675. ;; Directly set the variables, they are already local.
  11676. (setq org-log-done nil
  11677. org-log-repeat nil
  11678. org-todo-log-states nil)
  11679. (dolist (w (org-split-string value))
  11680. (let (a)
  11681. (cond
  11682. ((setq a (assoc w org-startup-options))
  11683. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11684. (set (nth 1 a) (nth 2 a))))
  11685. ((setq a (org-extract-log-state-settings w))
  11686. (and (member (car a) org-todo-keywords-1)
  11687. (push a org-todo-log-states)))))))
  11688. (defun org-get-todo-sequence-head (kwd)
  11689. "Return the head of the TODO sequence to which KWD belongs.
  11690. If KWD is not set, check if there is a text property remembering the
  11691. right sequence."
  11692. (let (p)
  11693. (cond
  11694. ((not kwd)
  11695. (or (get-text-property (point-at-bol) 'org-todo-head)
  11696. (progn
  11697. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11698. nil (point-at-eol)))
  11699. (get-text-property p 'org-todo-head))))
  11700. ((not (member kwd org-todo-keywords-1))
  11701. (car org-todo-keywords-1))
  11702. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11703. (defun org-fast-todo-selection ()
  11704. "Fast TODO keyword selection with single keys.
  11705. Returns the new TODO keyword, or nil if no state change should occur."
  11706. (let* ((fulltable org-todo-key-alist)
  11707. (done-keywords org-done-keywords) ;; needed for the faces.
  11708. (maxlen (apply 'max (mapcar
  11709. (lambda (x)
  11710. (if (stringp (car x)) (string-width (car x)) 0))
  11711. fulltable)))
  11712. (expert nil)
  11713. (fwidth (+ maxlen 3 1 3))
  11714. (ncol (/ (- (window-width) 4) fwidth))
  11715. tg cnt e c tbl
  11716. groups ingroup)
  11717. (save-excursion
  11718. (save-window-excursion
  11719. (if expert
  11720. (set-buffer (get-buffer-create " *Org todo*"))
  11721. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11722. (erase-buffer)
  11723. (setq-local org-done-keywords done-keywords)
  11724. (setq tbl fulltable cnt 0)
  11725. (while (setq e (pop tbl))
  11726. (cond
  11727. ((equal e '(:startgroup))
  11728. (push '() groups) (setq ingroup t)
  11729. (unless (= cnt 0)
  11730. (setq cnt 0)
  11731. (insert "\n"))
  11732. (insert "{ "))
  11733. ((equal e '(:endgroup))
  11734. (setq ingroup nil cnt 0)
  11735. (insert "}\n"))
  11736. ((equal e '(:newline))
  11737. (unless (= cnt 0)
  11738. (setq cnt 0)
  11739. (insert "\n")
  11740. (setq e (car tbl))
  11741. (while (equal (car tbl) '(:newline))
  11742. (insert "\n")
  11743. (setq tbl (cdr tbl)))))
  11744. (t
  11745. (setq tg (car e) c (cdr e))
  11746. (when ingroup (push tg (car groups)))
  11747. (setq tg (org-add-props tg nil 'face
  11748. (org-get-todo-face tg)))
  11749. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11750. (insert "[" c "] " tg (make-string
  11751. (- fwidth 4 (length tg)) ?\ ))
  11752. (when (= (setq cnt (1+ cnt)) ncol)
  11753. (insert "\n")
  11754. (when ingroup (insert " "))
  11755. (setq cnt 0)))))
  11756. (insert "\n")
  11757. (goto-char (point-min))
  11758. (unless expert (org-fit-window-to-buffer))
  11759. (message "[a-z..]:Set [SPC]:clear")
  11760. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11761. (cond
  11762. ((or (= c ?\C-g)
  11763. (and (= c ?q) (not (rassoc c fulltable))))
  11764. (setq quit-flag t))
  11765. ((= c ?\ ) nil)
  11766. ((setq e (rassoc c fulltable) tg (car e))
  11767. tg)
  11768. (t (setq quit-flag t)))))))
  11769. (defun org-entry-is-todo-p ()
  11770. (member (org-get-todo-state) org-not-done-keywords))
  11771. (defun org-entry-is-done-p ()
  11772. (member (org-get-todo-state) org-done-keywords))
  11773. (defun org-get-todo-state ()
  11774. "Return the TODO keyword of the current subtree."
  11775. (save-excursion
  11776. (org-back-to-heading t)
  11777. (and (looking-at org-todo-line-regexp)
  11778. (match-end 2)
  11779. (match-string 2))))
  11780. (defun org-at-date-range-p (&optional inactive-ok)
  11781. "Non-nil if point is inside a date range.
  11782. When optional argument INACTIVE-OK is non-nil, also consider
  11783. inactive time ranges.
  11784. When this function returns a non-nil value, match data is set
  11785. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11786. on INACTIVE-OK."
  11787. (interactive)
  11788. (save-excursion
  11789. (catch 'exit
  11790. (let ((pos (point)))
  11791. (skip-chars-backward "^[<\r\n")
  11792. (skip-chars-backward "<[")
  11793. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11794. (>= (match-end 0) pos)
  11795. (throw 'exit t))
  11796. (skip-chars-backward "^<[\r\n")
  11797. (skip-chars-backward "<[")
  11798. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11799. (>= (match-end 0) pos)
  11800. (throw 'exit t)))
  11801. nil)))
  11802. (defun org-get-repeat (&optional tagline)
  11803. "Check if there is a deadline/schedule with repeater in this entry."
  11804. (save-match-data
  11805. (save-excursion
  11806. (org-back-to-heading t)
  11807. (and (re-search-forward (if tagline
  11808. (concat tagline "\\s-*" org-repeat-re)
  11809. org-repeat-re)
  11810. (org-entry-end-position) t)
  11811. (match-string-no-properties 1)))))
  11812. (defvar org-last-changed-timestamp)
  11813. (defvar org-last-inserted-timestamp)
  11814. (defvar org-log-post-message)
  11815. (defvar org-log-note-purpose)
  11816. (defvar org-log-note-how nil)
  11817. (defvar org-log-note-extra)
  11818. (defun org-auto-repeat-maybe (done-word)
  11819. "Check if the current headline contains a repeated time-stamp.
  11820. If yes, set TODO state back to what it was and change the base date
  11821. of repeating deadline/scheduled time stamps to new date.
  11822. This function is run automatically after each state change to a DONE state."
  11823. (let* ((repeat (org-get-repeat))
  11824. (aa (assoc org-last-state org-todo-kwd-alist))
  11825. (interpret (nth 1 aa))
  11826. (head (nth 2 aa))
  11827. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11828. (msg "Entry repeats: ")
  11829. (org-log-done nil)
  11830. (org-todo-log-states nil))
  11831. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11832. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11833. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11834. org-todo-repeat-to-state)))
  11835. (unless (and to-state (member to-state org-todo-keywords-1))
  11836. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11837. (org-todo to-state))
  11838. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11839. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11840. (org-time-stamp-format t t))))
  11841. (when org-log-repeat
  11842. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11843. (memq 'org-add-log-note post-command-hook))
  11844. ;; We are already setup for some record.
  11845. (when (eq org-log-repeat 'note)
  11846. ;; Make sure we take a note, not only a time stamp.
  11847. (setq org-log-note-how 'note))
  11848. ;; Set up for taking a record.
  11849. (org-add-log-setup 'state
  11850. (or done-word (car org-done-keywords))
  11851. org-last-state
  11852. org-log-repeat)))
  11853. (org-back-to-heading t)
  11854. (org-add-planning-info nil nil 'closed)
  11855. (let ((end (save-excursion (outline-next-heading) (point))))
  11856. (while (re-search-forward org-ts-regexp end t)
  11857. (when (save-match-data
  11858. (or (org-at-planning-p)
  11859. (org-at-property-p)
  11860. (eq (org-element-type (save-excursion
  11861. (backward-char)
  11862. (org-element-context)))
  11863. 'timestamp)))
  11864. (let ((type (cond ((match-end 1) org-scheduled-string)
  11865. ((match-end 3) org-deadline-string)
  11866. (t "Plain:")))
  11867. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  11868. (cond
  11869. ((not
  11870. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11871. ;; Time-stamps without a repeater are usually skipped.
  11872. ;; However, a SCHEDULED time-stamp without one is
  11873. ;; removed, as it is considered as no longer relevant.
  11874. (when (equal type org-scheduled-string)
  11875. (org-remove-timestamp-with-keyword type)))
  11876. (t
  11877. (let ((n (string-to-number (match-string 2 ts)))
  11878. (what (match-string 3 ts)))
  11879. (when (equal what "w") (setq n (* n 7) what "d"))
  11880. (when (and (equal what "h")
  11881. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11882. ts)))
  11883. (user-error
  11884. "Cannot repeat in Repeat in %d hour(s) because no hour \
  11885. has been set"
  11886. n))
  11887. ;; Preparation, see if we need to modify the start
  11888. ;; date for the change.
  11889. (when (match-end 1)
  11890. (let ((time (save-match-data (org-time-string-to-time ts))))
  11891. (cond
  11892. ((equal (match-string 1 ts) ".")
  11893. ;; Shift starting date to today
  11894. (org-timestamp-change
  11895. (- (org-today) (time-to-days time))
  11896. 'day))
  11897. ((equal (match-string 1 ts) "+")
  11898. (let ((nshiftmax 10)
  11899. (nshift 0))
  11900. (while (or (= nshift 0)
  11901. (<= (time-to-days time)
  11902. (time-to-days (current-time))))
  11903. (when (= (cl-incf nshift) nshiftmax)
  11904. (or (y-or-n-p
  11905. (format "%d repeater intervals were not \
  11906. enough to shift date past today. Continue? "
  11907. nshift))
  11908. (user-error "Abort")))
  11909. (org-timestamp-change n (cdr (assoc what whata)))
  11910. (org-at-timestamp-p t)
  11911. (setq ts (match-string 1))
  11912. (setq time
  11913. (save-match-data
  11914. (org-time-string-to-time ts)))))
  11915. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11916. ;; Rematch, so that we have everything in
  11917. ;; place for the real shift.
  11918. (org-at-timestamp-p t)
  11919. (setq ts (match-string 1))
  11920. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11921. ts)))))
  11922. (save-excursion
  11923. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11924. (setq msg
  11925. (concat
  11926. msg type " " org-last-changed-timestamp " ")))))))))
  11927. (setq org-log-post-message msg)
  11928. (message "%s" msg))))
  11929. (defun org-show-todo-tree (arg)
  11930. "Make a compact tree which shows all headlines marked with TODO.
  11931. The tree will show the lines where the regexp matches, and all higher
  11932. headlines above the match.
  11933. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11934. With a numeric prefix N, construct a sparse tree for the Nth element
  11935. of `org-todo-keywords-1'."
  11936. (interactive "P")
  11937. (let ((case-fold-search nil)
  11938. (kwd-re
  11939. (cond ((null arg) org-not-done-regexp)
  11940. ((equal arg '(4))
  11941. (let ((kwd
  11942. (completing-read "Keyword (or KWD1|KWD2|...): "
  11943. (mapcar #'list org-todo-keywords-1))))
  11944. (concat "\\("
  11945. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11946. "\\)\\>")))
  11947. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11948. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11949. org-todo-keywords-1)))
  11950. (t (user-error "Invalid prefix argument: %s" arg)))))
  11951. (message "%d TODO entries found"
  11952. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11953. (defun org-deadline (arg &optional time)
  11954. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11955. With one universal prefix argument, remove any deadline from the item.
  11956. With two universal prefix arguments, prompt for a warning delay.
  11957. With argument TIME, set the deadline at the corresponding date. TIME
  11958. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11959. (interactive "P")
  11960. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11961. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11962. 'region-start-level 'region))
  11963. org-loop-over-headlines-in-active-region)
  11964. (org-map-entries
  11965. `(org-deadline ',arg ,time)
  11966. org-loop-over-headlines-in-active-region
  11967. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11968. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11969. (old-date-time (when old-date (org-time-string-to-time old-date)))
  11970. (repeater (and old-date
  11971. (string-match
  11972. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11973. old-date)
  11974. (match-string 1 old-date))))
  11975. (cond
  11976. ((equal arg '(4))
  11977. (when (and old-date org-log-redeadline)
  11978. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  11979. (org-remove-timestamp-with-keyword org-deadline-string)
  11980. (message "Item no longer has a deadline."))
  11981. ((equal arg '(16))
  11982. (save-excursion
  11983. (org-back-to-heading t)
  11984. (if (re-search-forward
  11985. org-deadline-time-regexp
  11986. (save-excursion (outline-next-heading) (point)) t)
  11987. (let* ((rpl0 (match-string 1))
  11988. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11989. (replace-match
  11990. (concat org-deadline-string
  11991. " <" rpl
  11992. (format " -%dd"
  11993. (abs
  11994. (- (time-to-days
  11995. (save-match-data
  11996. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11997. (time-to-days old-date-time))))
  11998. ">") t t))
  11999. (user-error "No deadline information to update"))))
  12000. (t
  12001. (org-add-planning-info 'deadline time 'closed)
  12002. (when (and old-date
  12003. org-log-redeadline
  12004. (not (equal old-date org-last-inserted-timestamp)))
  12005. (org-add-log-setup
  12006. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12007. (when repeater
  12008. (save-excursion
  12009. (org-back-to-heading t)
  12010. (when (re-search-forward (concat org-deadline-string " "
  12011. org-last-inserted-timestamp)
  12012. (save-excursion
  12013. (outline-next-heading) (point)) t)
  12014. (goto-char (1- (match-end 0)))
  12015. (insert " " repeater)
  12016. (setq org-last-inserted-timestamp
  12017. (concat (substring org-last-inserted-timestamp 0 -1)
  12018. " " repeater
  12019. (substring org-last-inserted-timestamp -1))))))
  12020. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12021. (defun org-schedule (arg &optional time)
  12022. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12023. With one universal prefix argument, remove any scheduling date from the item.
  12024. With two universal prefix arguments, prompt for a delay cookie.
  12025. With argument TIME, scheduled at the corresponding date. TIME can
  12026. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12027. (interactive "P")
  12028. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12029. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12030. 'region-start-level 'region))
  12031. org-loop-over-headlines-in-active-region)
  12032. (org-map-entries
  12033. `(org-schedule ',arg ,time)
  12034. org-loop-over-headlines-in-active-region
  12035. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12036. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12037. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12038. (repeater (and old-date
  12039. (string-match
  12040. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12041. old-date)
  12042. (match-string 1 old-date))))
  12043. (cond
  12044. ((equal arg '(4))
  12045. (progn
  12046. (when (and old-date org-log-reschedule)
  12047. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12048. (org-remove-timestamp-with-keyword org-scheduled-string)
  12049. (message "Item is no longer scheduled.")))
  12050. ((equal arg '(16))
  12051. (save-excursion
  12052. (org-back-to-heading t)
  12053. (if (re-search-forward
  12054. org-scheduled-time-regexp
  12055. (save-excursion (outline-next-heading) (point)) t)
  12056. (let* ((rpl0 (match-string 1))
  12057. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12058. (replace-match
  12059. (concat org-scheduled-string
  12060. " <" rpl
  12061. (format " -%dd"
  12062. (abs
  12063. (- (time-to-days
  12064. (save-match-data
  12065. (org-read-date nil t nil "Delay until" old-date-time)))
  12066. (time-to-days old-date-time))))
  12067. ">") t t))
  12068. (user-error "No scheduled information to update"))))
  12069. (t
  12070. (org-add-planning-info 'scheduled time 'closed)
  12071. (when (and old-date
  12072. org-log-reschedule
  12073. (not (equal old-date org-last-inserted-timestamp)))
  12074. (org-add-log-setup
  12075. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12076. (when repeater
  12077. (save-excursion
  12078. (org-back-to-heading t)
  12079. (when (re-search-forward (concat org-scheduled-string " "
  12080. org-last-inserted-timestamp)
  12081. (save-excursion
  12082. (outline-next-heading) (point)) t)
  12083. (goto-char (1- (match-end 0)))
  12084. (insert " " repeater)
  12085. (setq org-last-inserted-timestamp
  12086. (concat (substring org-last-inserted-timestamp 0 -1)
  12087. " " repeater
  12088. (substring org-last-inserted-timestamp -1))))))
  12089. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12090. (defun org-get-scheduled-time (pom &optional inherit)
  12091. "Get the scheduled time as a time tuple, of a format suitable
  12092. for calling org-schedule with, or if there is no scheduling,
  12093. returns nil."
  12094. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12095. (when time
  12096. (apply 'encode-time (org-parse-time-string time)))))
  12097. (defun org-get-deadline-time (pom &optional inherit)
  12098. "Get the deadline as a time tuple, of a format suitable for
  12099. calling org-deadline with, or if there is no scheduling, returns
  12100. nil."
  12101. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12102. (when time
  12103. (apply 'encode-time (org-parse-time-string time)))))
  12104. (defun org-remove-timestamp-with-keyword (keyword)
  12105. "Remove all time stamps with KEYWORD in the current entry."
  12106. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12107. beg)
  12108. (save-excursion
  12109. (org-back-to-heading t)
  12110. (setq beg (point))
  12111. (outline-next-heading)
  12112. (while (re-search-backward re beg t)
  12113. (replace-match "")
  12114. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12115. (equal (char-before) ?\ ))
  12116. (backward-delete-char 1)
  12117. (when (string-match "^[ \t]*$" (buffer-substring
  12118. (point-at-bol) (point-at-eol)))
  12119. (delete-region (point-at-bol)
  12120. (min (point-max) (1+ (point-at-eol))))))))))
  12121. (defvar org-time-was-given) ; dynamically scoped parameter
  12122. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12123. (defun org-at-planning-p ()
  12124. "Non-nil when point is on a planning info line."
  12125. ;; This is as accurate and faster than `org-element-at-point' since
  12126. ;; planning info location is fixed in the section.
  12127. (org-with-wide-buffer
  12128. (beginning-of-line)
  12129. (and (org-looking-at-p org-planning-line-re)
  12130. (eq (point)
  12131. (ignore-errors
  12132. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12133. (org-back-to-heading t)
  12134. (org-with-limited-levels (org-back-to-heading t)))
  12135. (line-beginning-position 2))))))
  12136. (defun org-add-planning-info (what &optional time &rest remove)
  12137. "Insert new timestamp with keyword in the planning line.
  12138. WHAT indicates what kind of time stamp to add. It is a symbol
  12139. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12140. the time to use. If none is given, the user is prompted for
  12141. a date. REMOVE indicates what kind of entries to remove. An old
  12142. WHAT entry will also be removed."
  12143. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12144. (catch 'exit
  12145. (when (and (memq what '(scheduled deadline))
  12146. (or (not time)
  12147. (and (stringp time)
  12148. (string-match "^[-+]+[0-9]" time))))
  12149. ;; Try to get a default date/time from existing timestamp
  12150. (save-excursion
  12151. (org-back-to-heading t)
  12152. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12153. (when (re-search-forward (if (eq what 'scheduled)
  12154. org-scheduled-time-regexp
  12155. org-deadline-time-regexp)
  12156. end t)
  12157. (setq ts (match-string 1)
  12158. default-time (apply 'encode-time (org-parse-time-string ts))
  12159. default-input (and ts (org-get-compact-tod ts)))))))
  12160. (when what
  12161. (setq time
  12162. (if (stringp time)
  12163. ;; This is a string (relative or absolute), set
  12164. ;; proper date.
  12165. (apply #'encode-time
  12166. (org-read-date-analyze
  12167. time default-time (decode-time default-time)))
  12168. ;; If necessary, get the time from the user
  12169. (or time (org-read-date nil 'to-time nil nil
  12170. default-time default-input)))))
  12171. (org-with-wide-buffer
  12172. (org-back-to-heading t)
  12173. (forward-line)
  12174. (unless (bolp) (insert "\n"))
  12175. (cond ((org-looking-at-p org-planning-line-re)
  12176. ;; Move to current indentation.
  12177. (skip-chars-forward " \t")
  12178. ;; Check if we have to remove something.
  12179. (dolist (type (if what (cons what remove) remove))
  12180. (save-excursion
  12181. (when (re-search-forward
  12182. (cl-case type
  12183. (closed org-closed-time-regexp)
  12184. (deadline org-deadline-time-regexp)
  12185. (scheduled org-scheduled-time-regexp)
  12186. (otherwise
  12187. (error "Invalid planning type: %s" type)))
  12188. (line-end-position) t)
  12189. ;; Delete until next keyword or end of line.
  12190. (delete-region
  12191. (match-beginning 0)
  12192. (if (re-search-forward org-keyword-time-not-clock-regexp
  12193. (line-end-position)
  12194. t)
  12195. (match-beginning 0)
  12196. (line-end-position))))))
  12197. ;; If there is nothing more to add and no more keyword
  12198. ;; is left, remove the line completely.
  12199. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12200. (delete-region (line-beginning-position)
  12201. (line-beginning-position 2))
  12202. ;; If we removed last keyword, do not leave trailing
  12203. ;; white space at the end of line.
  12204. (let ((p (point)))
  12205. (save-excursion
  12206. (end-of-line)
  12207. (unless (= (skip-chars-backward " \t" p) 0)
  12208. (delete-region (point) (line-end-position)))))))
  12209. ((not what) (throw 'exit nil)) ; Nothing to do.
  12210. (t (insert-before-markers "\n")
  12211. (backward-char 1)
  12212. (when org-adapt-indentation
  12213. (org-indent-to-column (1+ (org-outline-level))))))
  12214. (when what
  12215. ;; Insert planning keyword.
  12216. (insert (cl-case what
  12217. (closed org-closed-string)
  12218. (deadline org-deadline-string)
  12219. (scheduled org-scheduled-string)
  12220. (otherwise (error "Invalid planning type: %s" what)))
  12221. " ")
  12222. ;; Insert associated timestamp.
  12223. (let ((ts (org-insert-time-stamp
  12224. time
  12225. (or org-time-was-given
  12226. (and (eq what 'closed) org-log-done-with-time))
  12227. (eq what 'closed)
  12228. nil nil (list org-end-time-was-given))))
  12229. (unless (eolp) (insert " "))
  12230. ts))))))
  12231. (defvar org-log-note-marker (make-marker)
  12232. "Marker pointing at the entry where the note is to be inserted.")
  12233. (defvar org-log-note-purpose nil)
  12234. (defvar org-log-note-state nil)
  12235. (defvar org-log-note-previous-state nil)
  12236. (defvar org-log-note-extra nil)
  12237. (defvar org-log-note-window-configuration nil)
  12238. (defvar org-log-note-return-to (make-marker))
  12239. (defvar org-log-note-effective-time nil
  12240. "Remembered current time so that dynamically scoped
  12241. `org-extend-today-until' affects timestamps in state change log")
  12242. (defvar org-log-post-message nil
  12243. "Message to be displayed after a log note has been stored.
  12244. The auto-repeater uses this.")
  12245. (defun org-add-note ()
  12246. "Add a note to the current entry.
  12247. This is done in the same way as adding a state change note."
  12248. (interactive)
  12249. (org-add-log-setup 'note))
  12250. (defun org-log-beginning (&optional create)
  12251. "Return expected start of log notes in current entry.
  12252. When optional argument CREATE is non-nil, the function creates
  12253. a drawer to store notes, if necessary. Returned position ignores
  12254. narrowing."
  12255. (org-with-wide-buffer
  12256. (let ((drawer (org-log-into-drawer)))
  12257. (cond
  12258. (drawer
  12259. (org-end-of-meta-data)
  12260. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12261. (end (if (org-at-heading-p) (point)
  12262. (save-excursion (outline-next-heading) (point))))
  12263. (case-fold-search t))
  12264. (catch 'exit
  12265. ;; Try to find existing drawer.
  12266. (while (re-search-forward regexp end t)
  12267. (let ((element (org-element-at-point)))
  12268. (when (eq (org-element-type element) 'drawer)
  12269. (let ((cend (org-element-property :contents-end element)))
  12270. (when (and (not org-log-states-order-reversed) cend)
  12271. (goto-char cend)))
  12272. (throw 'exit nil))))
  12273. ;; No drawer found. Create one, if permitted.
  12274. (when create
  12275. (unless (bolp) (insert "\n"))
  12276. (let ((beg (point)))
  12277. (insert ":" drawer ":\n:END:\n")
  12278. (org-indent-region beg (point)))
  12279. (end-of-line -1)))))
  12280. (t
  12281. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12282. (skip-chars-forward " \t\n")
  12283. (beginning-of-line)
  12284. (unless org-log-states-order-reversed
  12285. (org-skip-over-state-notes)
  12286. (skip-chars-backward " \t\n")
  12287. (forward-line)))))
  12288. (if (bolp) (point) (line-beginning-position 2))))
  12289. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12290. "Set up the post command hook to take a note.
  12291. If this is about to TODO state change, the new state is expected in STATE.
  12292. HOW is an indicator what kind of note should be created.
  12293. EXTRA is additional text that will be inserted into the notes buffer."
  12294. (move-marker org-log-note-marker (point))
  12295. (setq org-log-note-purpose purpose
  12296. org-log-note-state state
  12297. org-log-note-previous-state prev-state
  12298. org-log-note-how how
  12299. org-log-note-extra extra
  12300. org-log-note-effective-time (org-current-effective-time))
  12301. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12302. (defun org-skip-over-state-notes ()
  12303. "Skip past the list of State notes in an entry."
  12304. (when (ignore-errors (goto-char (org-in-item-p)))
  12305. (let* ((struct (org-list-struct))
  12306. (prevs (org-list-prevs-alist struct))
  12307. (regexp
  12308. (concat "[ \t]*- +"
  12309. (replace-regexp-in-string
  12310. " +" " +"
  12311. (org-replace-escapes
  12312. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12313. `(("%d" . ,org-ts-regexp-inactive)
  12314. ("%D" . ,org-ts-regexp)
  12315. ("%s" . "\"\\S-+\"")
  12316. ("%S" . "\"\\S-+\"")
  12317. ("%t" . ,org-ts-regexp-inactive)
  12318. ("%T" . ,org-ts-regexp)
  12319. ("%u" . ".*?")
  12320. ("%U" . ".*?")))))))
  12321. (while (org-looking-at-p regexp)
  12322. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12323. (org-list-get-item-end (point) struct)))))))
  12324. (defun org-add-log-note (&optional _purpose)
  12325. "Pop up a window for taking a note, and add this note later."
  12326. (remove-hook 'post-command-hook 'org-add-log-note)
  12327. (setq org-log-note-window-configuration (current-window-configuration))
  12328. (delete-other-windows)
  12329. (move-marker org-log-note-return-to (point))
  12330. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12331. (goto-char org-log-note-marker)
  12332. (org-switch-to-buffer-other-window "*Org Note*")
  12333. (erase-buffer)
  12334. (if (memq org-log-note-how '(time state))
  12335. (let (current-prefix-arg) (org-store-log-note))
  12336. (let ((org-inhibit-startup t)) (org-mode))
  12337. (insert (format "# Insert note for %s.
  12338. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12339. (cond
  12340. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12341. ((eq org-log-note-purpose 'done) "closed todo item")
  12342. ((eq org-log-note-purpose 'state)
  12343. (format "state change from \"%s\" to \"%s\""
  12344. (or org-log-note-previous-state "")
  12345. (or org-log-note-state "")))
  12346. ((eq org-log-note-purpose 'reschedule)
  12347. "rescheduling")
  12348. ((eq org-log-note-purpose 'delschedule)
  12349. "no longer scheduled")
  12350. ((eq org-log-note-purpose 'redeadline)
  12351. "changing deadline")
  12352. ((eq org-log-note-purpose 'deldeadline)
  12353. "removing deadline")
  12354. ((eq org-log-note-purpose 'refile)
  12355. "refiling")
  12356. ((eq org-log-note-purpose 'note)
  12357. "this entry")
  12358. (t (error "This should not happen")))))
  12359. (when org-log-note-extra (insert org-log-note-extra))
  12360. (setq-local org-finish-function 'org-store-log-note)
  12361. (run-hooks 'org-log-buffer-setup-hook)))
  12362. (defvar org-note-abort nil) ; dynamically scoped
  12363. (defun org-store-log-note ()
  12364. "Finish taking a log note, and insert it to where it belongs."
  12365. (let ((txt (prog1 (buffer-string)
  12366. (kill-buffer)))
  12367. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12368. lines)
  12369. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12370. (setq txt (replace-match "" t t txt)))
  12371. (when (string-match "\\s-+\\'" txt)
  12372. (setq txt (replace-match "" t t txt)))
  12373. (setq lines (org-split-string txt "\n"))
  12374. (when (org-string-nw-p note)
  12375. (setq note
  12376. (org-replace-escapes
  12377. note
  12378. (list (cons "%u" (user-login-name))
  12379. (cons "%U" user-full-name)
  12380. (cons "%t" (format-time-string
  12381. (org-time-stamp-format 'long 'inactive)
  12382. org-log-note-effective-time))
  12383. (cons "%T" (format-time-string
  12384. (org-time-stamp-format 'long nil)
  12385. org-log-note-effective-time))
  12386. (cons "%d" (format-time-string
  12387. (org-time-stamp-format nil 'inactive)
  12388. org-log-note-effective-time))
  12389. (cons "%D" (format-time-string
  12390. (org-time-stamp-format nil nil)
  12391. org-log-note-effective-time))
  12392. (cons "%s" (cond
  12393. ((not org-log-note-state) "")
  12394. ((org-string-match-p org-ts-regexp
  12395. org-log-note-state)
  12396. (format "\"[%s]\""
  12397. (substring org-log-note-state 1 -1)))
  12398. (t (format "\"%s\"" org-log-note-state))))
  12399. (cons "%S"
  12400. (cond
  12401. ((not org-log-note-previous-state) "")
  12402. ((org-string-match-p org-ts-regexp
  12403. org-log-note-previous-state)
  12404. (format "\"[%s]\""
  12405. (substring
  12406. org-log-note-previous-state 1 -1)))
  12407. (t (format "\"%s\""
  12408. org-log-note-previous-state)))))))
  12409. (when lines (setq note (concat note " \\\\")))
  12410. (push note lines))
  12411. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12412. (with-current-buffer (marker-buffer org-log-note-marker)
  12413. (org-with-wide-buffer
  12414. ;; Find location for the new note.
  12415. (goto-char org-log-note-marker)
  12416. (set-marker org-log-note-marker nil)
  12417. (goto-char (org-log-beginning t))
  12418. ;; Make sure point is at the beginning of an empty line.
  12419. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12420. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12421. ;; In an existing list, add a new item at the top level.
  12422. ;; Otherwise, indent line like a regular one.
  12423. (let ((itemp (org-in-item-p)))
  12424. (if itemp
  12425. (org-indent-line-to
  12426. (let ((struct (save-excursion
  12427. (goto-char itemp) (org-list-struct))))
  12428. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12429. (org-indent-line)))
  12430. (insert (org-list-bullet-string "-") (pop lines))
  12431. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12432. (dolist (line lines)
  12433. (insert "\n")
  12434. (org-indent-line-to ind)
  12435. (insert line)))
  12436. (message "Note stored")
  12437. (org-back-to-heading t)
  12438. (org-cycle-hide-drawers 'children))
  12439. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12440. ;; from within `org-add-log-note' because `buffer-undo-list'
  12441. ;; is then modified outside of `org-with-remote-undo'.
  12442. (when (eq this-command 'org-agenda-todo)
  12443. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12444. ;; Don't add undo information when called from `org-agenda-todo'.
  12445. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12446. (set-window-configuration org-log-note-window-configuration)
  12447. (with-current-buffer (marker-buffer org-log-note-return-to)
  12448. (goto-char org-log-note-return-to))
  12449. (move-marker org-log-note-return-to nil)
  12450. (when org-log-post-message (message "%s" org-log-post-message))))
  12451. (defun org-remove-empty-drawer-at (pos)
  12452. "Remove an empty drawer at position POS.
  12453. POS may also be a marker."
  12454. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12455. (org-with-wide-buffer
  12456. (goto-char pos)
  12457. (let ((drawer (org-element-at-point)))
  12458. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12459. (not (org-element-property :contents-begin drawer)))
  12460. (delete-region (org-element-property :begin drawer)
  12461. (progn (goto-char (org-element-property :end drawer))
  12462. (skip-chars-backward " \r\t\n")
  12463. (forward-line)
  12464. (point))))))))
  12465. (defvar org-ts-type nil)
  12466. (defun org-sparse-tree (&optional arg type)
  12467. "Create a sparse tree, prompt for the details.
  12468. This command can create sparse trees. You first need to select the type
  12469. of match used to create the tree:
  12470. t Show all TODO entries.
  12471. T Show entries with a specific TODO keyword.
  12472. m Show entries selected by a tags/property match.
  12473. p Enter a property name and its value (both with completion on existing
  12474. names/values) and show entries with that property.
  12475. r Show entries matching a regular expression (`/' can be used as well).
  12476. b Show deadlines and scheduled items before a date.
  12477. a Show deadlines and scheduled items after a date.
  12478. d Show deadlines due within `org-deadline-warning-days'.
  12479. D Show deadlines and scheduled items between a date range."
  12480. (interactive "P")
  12481. (setq type (or type org-sparse-tree-default-date-type))
  12482. (setq org-ts-type type)
  12483. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12484. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12485. \[c]ycle through date types: %s"
  12486. (cl-case type
  12487. (all "all timestamps")
  12488. (scheduled "only scheduled")
  12489. (deadline "only deadline")
  12490. (active "only active timestamps")
  12491. (inactive "only inactive timestamps")
  12492. (closed "with a closed time-stamp")
  12493. (otherwise "scheduled/deadline")))
  12494. (let ((answer (read-char-exclusive)))
  12495. (cl-case answer
  12496. (?c
  12497. (org-sparse-tree
  12498. arg
  12499. (cadr
  12500. (memq type '(nil all scheduled deadline active inactive closed)))))
  12501. (?d (call-interactively 'org-check-deadlines))
  12502. (?b (call-interactively 'org-check-before-date))
  12503. (?a (call-interactively 'org-check-after-date))
  12504. (?D (call-interactively 'org-check-dates-range))
  12505. (?t (call-interactively 'org-show-todo-tree))
  12506. (?T (org-show-todo-tree '(4)))
  12507. (?m (call-interactively 'org-match-sparse-tree))
  12508. ((?p ?P)
  12509. (let* ((kwd (completing-read
  12510. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12511. (value (completing-read
  12512. "Value: " (mapcar #'list (org-property-values kwd)))))
  12513. (unless (string-match "\\`{.*}\\'" value)
  12514. (setq value (concat "\"" value "\"")))
  12515. (org-match-sparse-tree arg (concat kwd "=" value))))
  12516. ((?r ?R ?/) (call-interactively 'org-occur))
  12517. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12518. (defvar-local org-occur-highlights nil
  12519. "List of overlays used for occur matches.")
  12520. (defvar-local org-occur-parameters nil
  12521. "Parameters of the active org-occur calls.
  12522. This is a list, each call to org-occur pushes as cons cell,
  12523. containing the regular expression and the callback, onto the list.
  12524. The list can contain several entries if `org-occur' has been called
  12525. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12526. will only contain one set of parameters. When the highlights are
  12527. removed (for example with `C-c C-c', or with the next edit (depending
  12528. on `org-remove-highlights-with-change'), this variable is emptied
  12529. as well.")
  12530. (defun org-occur (regexp &optional keep-previous callback)
  12531. "Make a compact tree which shows all matches of REGEXP.
  12532. The tree will show the lines where the regexp matches, and all higher
  12533. headlines above the match. It will also show the heading after the match,
  12534. to make sure editing the matching entry is easy.
  12535. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12536. call to `org-occur' will be kept, to allow stacking of calls to this
  12537. command.
  12538. If CALLBACK is non-nil, it is a function which is called to confirm
  12539. that the match should indeed be shown."
  12540. (interactive "sRegexp: \nP")
  12541. (when (equal regexp "")
  12542. (user-error "Regexp cannot be empty"))
  12543. (unless keep-previous
  12544. (org-remove-occur-highlights nil nil t))
  12545. (push (cons regexp callback) org-occur-parameters)
  12546. (let ((cnt 0))
  12547. (save-excursion
  12548. (goto-char (point-min))
  12549. (when (or (not keep-previous) ; do not want to keep
  12550. (not org-occur-highlights)) ; no previous matches
  12551. ;; hide everything
  12552. (org-overview))
  12553. (while (re-search-forward regexp nil t)
  12554. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12555. (when (or (not callback)
  12556. (save-match-data (funcall callback)))
  12557. (setq cnt (1+ cnt))
  12558. (when org-highlight-sparse-tree-matches
  12559. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12560. (org-show-context 'occur-tree))))
  12561. (when org-remove-highlights-with-change
  12562. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12563. nil 'local))
  12564. (unless org-sparse-tree-open-archived-trees
  12565. (org-hide-archived-subtrees (point-min) (point-max)))
  12566. (run-hooks 'org-occur-hook)
  12567. (when (org-called-interactively-p 'interactive)
  12568. (message "%d match(es) for regexp %s" cnt regexp))
  12569. cnt))
  12570. (defun org-occur-next-match (&optional n _reset)
  12571. "Function for `next-error-function' to find sparse tree matches.
  12572. N is the number of matches to move, when negative move backwards.
  12573. This function always goes back to the starting point when no
  12574. match is found."
  12575. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12576. (search-func (if (< n 0)
  12577. 'previous-single-char-property-change
  12578. 'next-single-char-property-change))
  12579. (n (abs n))
  12580. (pos (point))
  12581. p1)
  12582. (catch 'exit
  12583. (while (setq p1 (funcall search-func (point) 'org-type))
  12584. (when (equal p1 limit)
  12585. (goto-char pos)
  12586. (user-error "No more matches"))
  12587. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12588. (setq n (1- n))
  12589. (when (= n 0)
  12590. (goto-char p1)
  12591. (throw 'exit (point))))
  12592. (goto-char p1))
  12593. (goto-char p1)
  12594. (user-error "No more matches"))))
  12595. (defun org-show-context (&optional key)
  12596. "Make sure point and context are visible.
  12597. Optional argument KEY, when non-nil, is a symbol. See
  12598. `org-show-context-detail' for allowed values and how much is to
  12599. be shown."
  12600. (org-show-set-visibility
  12601. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12602. ((cdr (assq key org-show-context-detail)))
  12603. (t (cdr (assq 'default org-show-context-detail))))))
  12604. (defun org-show-set-visibility (detail)
  12605. "Set visibility around point according to DETAIL.
  12606. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12607. `tree', `canonical' or t. See `org-show-context-detail' for more
  12608. information."
  12609. (unless (org-before-first-heading-p)
  12610. ;; Show current heading and possibly its entry, following headline
  12611. ;; or all children.
  12612. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12613. (org-flag-heading nil)
  12614. (org-show-entry)
  12615. (org-with-limited-levels
  12616. (cl-case detail
  12617. ((tree canonical t) (org-show-children))
  12618. ((nil minimal ancestors))
  12619. (t (save-excursion
  12620. (outline-next-heading)
  12621. (org-flag-heading nil))))))
  12622. ;; Show all siblings.
  12623. (when (eq detail 'lineage) (org-show-siblings))
  12624. ;; Show ancestors, possibly with their children.
  12625. (when (memq detail '(ancestors lineage tree canonical t))
  12626. (save-excursion
  12627. (while (org-up-heading-safe)
  12628. (org-flag-heading nil)
  12629. (when (memq detail '(canonical t)) (org-show-entry))
  12630. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12631. (defvar org-reveal-start-hook nil
  12632. "Hook run before revealing a location.")
  12633. (defun org-reveal (&optional siblings)
  12634. "Show current entry, hierarchy above it, and the following headline.
  12635. This can be used to show a consistent set of context around
  12636. locations exposed with `org-show-context'.
  12637. With optional argument SIBLINGS, on each level of the hierarchy all
  12638. siblings are shown. This repairs the tree structure to what it would
  12639. look like when opened with hierarchical calls to `org-cycle'.
  12640. With double optional argument \\[universal-argument] \\[universal-argument], \
  12641. go to the parent and show the
  12642. entire tree."
  12643. (interactive "P")
  12644. (run-hooks 'org-reveal-start-hook)
  12645. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12646. ((equal siblings '(16))
  12647. (save-excursion
  12648. (when (org-up-heading-safe)
  12649. (org-show-subtree)
  12650. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12651. (t (org-show-set-visibility 'lineage))))
  12652. (defun org-highlight-new-match (beg end)
  12653. "Highlight from BEG to END and mark the highlight is an occur headline."
  12654. (let ((ov (make-overlay beg end)))
  12655. (overlay-put ov 'face 'secondary-selection)
  12656. (overlay-put ov 'org-type 'org-occur)
  12657. (push ov org-occur-highlights)))
  12658. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12659. "Remove the occur highlights from the buffer.
  12660. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12661. from the `before-change-functions' in the current buffer."
  12662. (interactive)
  12663. (unless org-inhibit-highlight-removal
  12664. (mapc #'delete-overlay org-occur-highlights)
  12665. (setq org-occur-highlights nil)
  12666. (setq org-occur-parameters nil)
  12667. (unless noremove
  12668. (remove-hook 'before-change-functions
  12669. 'org-remove-occur-highlights 'local))))
  12670. ;;;; Priorities
  12671. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12672. "Regular expression matching the priority indicator.")
  12673. (defvar org-remove-priority-next-time nil)
  12674. (defun org-priority-up ()
  12675. "Increase the priority of the current item."
  12676. (interactive)
  12677. (org-priority 'up))
  12678. (defun org-priority-down ()
  12679. "Decrease the priority of the current item."
  12680. (interactive)
  12681. (org-priority 'down))
  12682. (defun org-priority (&optional action _show)
  12683. "Change the priority of an item.
  12684. ACTION can be `set', `up', `down', or a character."
  12685. (interactive "P")
  12686. (if (equal action '(4))
  12687. (org-show-priority)
  12688. (unless org-enable-priority-commands
  12689. (user-error "Priority commands are disabled"))
  12690. (setq action (or action 'set))
  12691. (let (current new news have remove)
  12692. (save-excursion
  12693. (org-back-to-heading t)
  12694. (when (looking-at org-priority-regexp)
  12695. (setq current (string-to-char (match-string 2))
  12696. have t))
  12697. (cond
  12698. ((eq action 'remove)
  12699. (setq remove t new ?\ ))
  12700. ((or (eq action 'set)
  12701. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12702. (if (not (eq action 'set))
  12703. (setq new action)
  12704. (message "Priority %c-%c, SPC to remove: "
  12705. org-highest-priority org-lowest-priority)
  12706. (save-match-data
  12707. (setq new (read-char-exclusive))))
  12708. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12709. (= (upcase org-lowest-priority) org-lowest-priority))
  12710. (setq new (upcase new)))
  12711. (cond ((equal new ?\ ) (setq remove t))
  12712. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12713. (user-error "Priority must be between `%c' and `%c'"
  12714. org-highest-priority org-lowest-priority))))
  12715. ((eq action 'up)
  12716. (setq new (if have
  12717. (1- current) ; normal cycling
  12718. ;; last priority was empty
  12719. (if (eq last-command this-command)
  12720. org-lowest-priority ; wrap around empty to lowest
  12721. ;; default
  12722. (if org-priority-start-cycle-with-default
  12723. org-default-priority
  12724. (1- org-default-priority))))))
  12725. ((eq action 'down)
  12726. (setq new (if have
  12727. (1+ current) ; normal cycling
  12728. ;; last priority was empty
  12729. (if (eq last-command this-command)
  12730. org-highest-priority ; wrap around empty to highest
  12731. ;; default
  12732. (if org-priority-start-cycle-with-default
  12733. org-default-priority
  12734. (1+ org-default-priority))))))
  12735. (t (user-error "Invalid action")))
  12736. (when (or (< (upcase new) org-highest-priority)
  12737. (> (upcase new) org-lowest-priority))
  12738. (if (and (memq action '(up down))
  12739. (not have) (not (eq last-command this-command)))
  12740. ;; `new' is from default priority
  12741. (error
  12742. "The default can not be set, see `org-default-priority' why")
  12743. ;; normal cycling: `new' is beyond highest/lowest priority
  12744. ;; and is wrapped around to the empty priority
  12745. (setq remove t)))
  12746. (setq news (format "%c" new))
  12747. (if have
  12748. (if remove
  12749. (replace-match "" t t nil 1)
  12750. (replace-match news t t nil 2))
  12751. (if remove
  12752. (user-error "No priority cookie found in line")
  12753. (let ((case-fold-search nil))
  12754. (looking-at org-todo-line-regexp))
  12755. (if (match-end 2)
  12756. (progn
  12757. (goto-char (match-end 2))
  12758. (insert " [#" news "]"))
  12759. (goto-char (match-beginning 3))
  12760. (insert "[#" news "] "))))
  12761. (org-set-tags nil 'align))
  12762. (if remove
  12763. (message "Priority removed")
  12764. (message "Priority of current item set to %s" news)))))
  12765. (defun org-show-priority ()
  12766. "Show the priority of the current item.
  12767. This priority is composed of the main priority given with the [#A] cookies,
  12768. and by additional input from the age of a schedules or deadline entry."
  12769. (interactive)
  12770. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12771. (org-get-at-bol 'priority)
  12772. (save-excursion
  12773. (save-match-data
  12774. (beginning-of-line)
  12775. (and (looking-at org-heading-regexp)
  12776. (org-get-priority (match-string 0))))))))
  12777. (message "Priority is %d" (if pri pri -1000))))
  12778. (defun org-get-priority (s)
  12779. "Find priority cookie and return priority."
  12780. (save-match-data
  12781. (if (functionp org-get-priority-function)
  12782. (funcall org-get-priority-function)
  12783. (if (not (string-match org-priority-regexp s))
  12784. (* 1000 (- org-lowest-priority org-default-priority))
  12785. (* 1000 (- org-lowest-priority
  12786. (string-to-char (match-string 2 s))))))))
  12787. ;;;; Tags
  12788. (defvar org-agenda-archives-mode)
  12789. (defvar org-map-continue-from nil
  12790. "Position from where mapping should continue.
  12791. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12792. (defvar org-scanner-tags nil
  12793. "The current tag list while the tags scanner is running.")
  12794. (defvar org-trust-scanner-tags nil
  12795. "Should `org-get-tags-at' use the tags for the scanner.
  12796. This is for internal dynamical scoping only.
  12797. When this is non-nil, the function `org-get-tags-at' will return the value
  12798. of `org-scanner-tags' instead of building the list by itself. This
  12799. can lead to large speed-ups when the tags scanner is used in a file with
  12800. many entries, and when the list of tags is retrieved, for example to
  12801. obtain a list of properties. Building the tags list for each entry in such
  12802. a file becomes an N^2 operation - but with this variable set, it scales
  12803. as N.")
  12804. (defvar org--matcher-tags-todo-only nil)
  12805. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12806. "Scan headline tags with inheritance and produce output ACTION.
  12807. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12808. or `agenda' to produce an entry list for an agenda view. It can also be
  12809. a Lisp form or a function that should be called at each matched headline, in
  12810. this case the return value is a list of all return values from these calls.
  12811. MATCHER is a function accepting three arguments, returning
  12812. a non-nil value whenever a given set of tags qualifies a headline
  12813. for inclusion. See `org-make-tags-matcher' for more information.
  12814. As a special case, it can also be set to t (respectively nil) in
  12815. order to match all (respectively none) headline.
  12816. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12817. keyword are included in the output.
  12818. START-LEVEL can be a string with asterisks, reducing the scope to
  12819. headlines matching this string."
  12820. (require 'org-agenda)
  12821. (let* ((re (concat "^"
  12822. (if start-level
  12823. ;; Get the correct level to match
  12824. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12825. org-outline-regexp)
  12826. " *\\(\\<\\("
  12827. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12828. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12829. (props (list 'face 'default
  12830. 'done-face 'org-agenda-done
  12831. 'undone-face 'default
  12832. 'mouse-face 'highlight
  12833. 'org-not-done-regexp org-not-done-regexp
  12834. 'org-todo-regexp org-todo-regexp
  12835. 'org-complex-heading-regexp org-complex-heading-regexp
  12836. 'help-echo
  12837. (format "mouse-2 or RET jump to org file %s"
  12838. (abbreviate-file-name
  12839. (or (buffer-file-name (buffer-base-buffer))
  12840. (buffer-name (buffer-base-buffer)))))))
  12841. (org-map-continue-from nil)
  12842. lspos tags tags-list
  12843. (tags-alist (list (cons 0 org-file-tags)))
  12844. (llast 0) rtn rtn1 level category i txt
  12845. todo marker entry priority
  12846. ts-date ts-date-type ts-date-pair)
  12847. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12848. (setq action (list 'lambda nil action)))
  12849. (save-excursion
  12850. (goto-char (point-min))
  12851. (when (eq action 'sparse-tree)
  12852. (org-overview)
  12853. (org-remove-occur-highlights))
  12854. (while (let (case-fold-search)
  12855. (re-search-forward re nil t))
  12856. (setq org-map-continue-from nil)
  12857. (catch :skip
  12858. (setq todo
  12859. ;; TODO: is the 1-2 difference a bug?
  12860. (when (match-end 1) (org-match-string-no-properties 2))
  12861. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12862. (goto-char (setq lspos (match-beginning 0)))
  12863. (setq level (org-reduced-level (org-outline-level))
  12864. category (org-get-category))
  12865. (when (eq action 'agenda)
  12866. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12867. ts-date (car ts-date-pair)
  12868. ts-date-type (cdr ts-date-pair)))
  12869. (setq i llast llast level)
  12870. ;; remove tag lists from same and sublevels
  12871. (while (>= i level)
  12872. (when (setq entry (assoc i tags-alist))
  12873. (setq tags-alist (delete entry tags-alist)))
  12874. (setq i (1- i)))
  12875. ;; add the next tags
  12876. (when tags
  12877. (setq tags (org-split-string tags ":")
  12878. tags-alist
  12879. (cons (cons level tags) tags-alist)))
  12880. ;; compile tags for current headline
  12881. (setq tags-list
  12882. (if org-use-tag-inheritance
  12883. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12884. tags)
  12885. org-scanner-tags tags-list)
  12886. (when org-use-tag-inheritance
  12887. (setcdr (car tags-alist)
  12888. (mapcar (lambda (x)
  12889. (setq x (copy-sequence x))
  12890. (org-add-prop-inherited x))
  12891. (cdar tags-alist))))
  12892. (when (and tags org-use-tag-inheritance
  12893. (or (not (eq t org-use-tag-inheritance))
  12894. org-tags-exclude-from-inheritance))
  12895. ;; Selective inheritance, remove uninherited ones.
  12896. (setcdr (car tags-alist)
  12897. (org-remove-uninherited-tags (cdar tags-alist))))
  12898. (when (and
  12899. ;; eval matcher only when the todo condition is OK
  12900. (and (or (not todo-only) (member todo org-not-done-keywords))
  12901. (if (functionp matcher)
  12902. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12903. (funcall matcher todo tags-list level))
  12904. matcher))
  12905. ;; Call the skipper, but return t if it does not
  12906. ;; skip, so that the `and' form continues evaluating.
  12907. (progn
  12908. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12909. t)
  12910. ;; Check if timestamps are deselecting this entry
  12911. (or (not todo-only)
  12912. (and (member todo org-not-done-keywords)
  12913. (or (not org-agenda-tags-todo-honor-ignore-options)
  12914. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12915. ;; select this headline
  12916. (cond
  12917. ((eq action 'sparse-tree)
  12918. (and org-highlight-sparse-tree-matches
  12919. (org-get-heading) (match-end 0)
  12920. (org-highlight-new-match
  12921. (match-beginning 1) (match-end 1)))
  12922. (org-show-context 'tags-tree))
  12923. ((eq action 'agenda)
  12924. (setq txt (org-agenda-format-item
  12925. ""
  12926. (concat
  12927. (if (eq org-tags-match-list-sublevels 'indented)
  12928. (make-string (1- level) ?.) "")
  12929. (org-get-heading))
  12930. (make-string level ?\s)
  12931. category
  12932. tags-list)
  12933. priority (org-get-priority txt))
  12934. (goto-char lspos)
  12935. (setq marker (org-agenda-new-marker))
  12936. (org-add-props txt props
  12937. 'org-marker marker 'org-hd-marker marker 'org-category category
  12938. 'todo-state todo
  12939. 'ts-date ts-date
  12940. 'priority priority
  12941. 'type (concat "tagsmatch" ts-date-type))
  12942. (push txt rtn))
  12943. ((functionp action)
  12944. (setq org-map-continue-from nil)
  12945. (save-excursion
  12946. (setq rtn1 (funcall action))
  12947. (push rtn1 rtn)))
  12948. (t (user-error "Invalid action")))
  12949. ;; if we are to skip sublevels, jump to end of subtree
  12950. (unless org-tags-match-list-sublevels
  12951. (org-end-of-subtree t)
  12952. (backward-char 1))))
  12953. ;; Get the correct position from where to continue
  12954. (if org-map-continue-from
  12955. (goto-char org-map-continue-from)
  12956. (and (= (point) lspos) (end-of-line 1)))))
  12957. (when (and (eq action 'sparse-tree)
  12958. (not org-sparse-tree-open-archived-trees))
  12959. (org-hide-archived-subtrees (point-min) (point-max)))
  12960. (nreverse rtn)))
  12961. (defun org-remove-uninherited-tags (tags)
  12962. "Remove all tags that are not inherited from the list TAGS."
  12963. (cond
  12964. ((eq org-use-tag-inheritance t)
  12965. (if org-tags-exclude-from-inheritance
  12966. (org-delete-all org-tags-exclude-from-inheritance tags)
  12967. tags))
  12968. ((not org-use-tag-inheritance) nil)
  12969. ((stringp org-use-tag-inheritance)
  12970. (delq nil (mapcar
  12971. (lambda (x)
  12972. (if (and (string-match org-use-tag-inheritance x)
  12973. (not (member x org-tags-exclude-from-inheritance)))
  12974. x nil))
  12975. tags)))
  12976. ((listp org-use-tag-inheritance)
  12977. (delq nil (mapcar
  12978. (lambda (x)
  12979. (if (member x org-use-tag-inheritance) x nil))
  12980. tags)))))
  12981. (defun org-match-sparse-tree (&optional todo-only match)
  12982. "Create a sparse tree according to tags string MATCH.
  12983. MATCH can contain positive and negative selection of tags, like
  12984. \"+WORK+URGENT-WITHBOSS\".
  12985. If optional argument TODO-ONLY is non-nil, only select lines that are
  12986. also TODO lines."
  12987. (interactive "P")
  12988. (org-agenda-prepare-buffers (list (current-buffer)))
  12989. (let ((org--matcher-tags-todo-only todo-only))
  12990. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  12991. org--matcher-tags-todo-only)))
  12992. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12993. (defvar org-cached-props nil)
  12994. (defun org-cached-entry-get (pom property)
  12995. (if (or (eq t org-use-property-inheritance)
  12996. (and (stringp org-use-property-inheritance)
  12997. (let ((case-fold-search t))
  12998. (org-string-match-p org-use-property-inheritance property)))
  12999. (and (listp org-use-property-inheritance)
  13000. (member-ignore-case property org-use-property-inheritance)))
  13001. ;; Caching is not possible, check it directly.
  13002. (org-entry-get pom property 'inherit)
  13003. ;; Get all properties, so we can do complicated checks easily.
  13004. (cdr (assoc-string property
  13005. (or org-cached-props
  13006. (setq org-cached-props (org-entry-properties pom)))
  13007. t))))
  13008. (defun org-global-tags-completion-table (&optional files)
  13009. "Return the list of all tags in all agenda buffer/files.
  13010. Optional FILES argument is a list of files which can be used
  13011. instead of the agenda files."
  13012. (save-excursion
  13013. (org-uniquify
  13014. (delq nil
  13015. (apply 'append
  13016. (mapcar
  13017. (lambda (file)
  13018. (set-buffer (find-file-noselect file))
  13019. (append (org-get-buffer-tags)
  13020. (mapcar (lambda (x) (if (stringp (car-safe x))
  13021. (list (car-safe x)) nil))
  13022. org-tag-alist)))
  13023. (if (and files (car files))
  13024. files
  13025. (org-agenda-files))))))))
  13026. (defun org-make-tags-matcher (match)
  13027. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13028. Returns a cons of the selection string MATCH and a function
  13029. implementing the matcher.
  13030. The matcher is to be called at an Org entry, with point on the
  13031. headline, and returns non-nil if the entry matches the selection
  13032. string MATCH. It must be called with three arguments: the TODO
  13033. keyword at the entry (or nil if none), the list of all tags at
  13034. the entry including inherited ones and the reduced level of the
  13035. headline. Additionally, the category of the entry, if any, must
  13036. be specified as the text property `org-category' on the headline.
  13037. This function sets the variable `org--matcher-tags-todo-only' to
  13038. a non-nil value if the matcher restricts matching to TODO
  13039. entries, otherwise it is not touched.
  13040. See also `org-scan-tags'."
  13041. (unless match
  13042. ;; Get a new match request, with completion against the global
  13043. ;; tags table and the local tags in current buffer.
  13044. (let ((org-last-tags-completion-table
  13045. (org-uniquify
  13046. (delq nil (append (org-get-buffer-tags)
  13047. (org-global-tags-completion-table))))))
  13048. (setq match
  13049. (completing-read
  13050. "Match: "
  13051. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13052. (let ((match0 match)
  13053. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13054. (start 0)
  13055. tagsmatch todomatch tagsmatcher todomatcher)
  13056. ;; Expand group tags.
  13057. (setq match (org-tags-expand match))
  13058. ;; Check if there is a TODO part of this match, which would be the
  13059. ;; part after a "/". To make sure that this slash is not part of
  13060. ;; a property value to be matched against, we also check that
  13061. ;; there is no / after that slash. First, find the last slash.
  13062. (let ((s 0))
  13063. (while (string-match "/+" match s)
  13064. (setq start (match-beginning 0))
  13065. (setq s (match-end 0))))
  13066. (if (and (string-match "/+" match start)
  13067. (not (string-match-p "\"" match start)))
  13068. ;; Match contains also a TODO-matching request.
  13069. (progn
  13070. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13071. (setq todomatch (substring match (match-end 0)))
  13072. (when (string-match "\\`!" todomatch)
  13073. (setq org--matcher-tags-todo-only t)
  13074. (setq todomatch (substring todomatch 1)))
  13075. (when (string-match "\\`\\s-*\\'" todomatch)
  13076. (setq todomatch nil)))
  13077. ;; Only matching tags.
  13078. (setq tagsmatch match)
  13079. (setq todomatch nil))
  13080. ;; Make the tags matcher.
  13081. (when (org-string-nw-p tagsmatch)
  13082. (let ((orlist nil)
  13083. (orterms (org-split-string tagsmatch "|"))
  13084. term)
  13085. (while (setq term (pop orterms))
  13086. (while (and (equal (substring term -1) "\\") orterms)
  13087. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13088. (while (string-match re term)
  13089. (let* ((rest (substring term (match-end 0)))
  13090. (minus (and (match-end 1)
  13091. (equal (match-string 1 term) "-")))
  13092. (tag (save-match-data
  13093. (replace-regexp-in-string
  13094. "\\\\-" "-" (match-string 2 term))))
  13095. (regexp (eq (string-to-char tag) ?{))
  13096. (levelp (match-end 4))
  13097. (propp (match-end 5))
  13098. (mm
  13099. (cond
  13100. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13101. (levelp
  13102. `(,(org-op-to-function (match-string 3 term))
  13103. level
  13104. ,(string-to-number (match-string 4 term))))
  13105. (propp
  13106. (let* ((gv (pcase (upcase (match-string 5 term))
  13107. ("CATEGORY"
  13108. '(get-text-property (point) 'org-category))
  13109. ("TODO" 'todo)
  13110. (p `(org-cached-entry-get nil ,p))))
  13111. (pv (match-string 7 term))
  13112. (regexp (eq (string-to-char pv) ?{))
  13113. (strp (eq (string-to-char pv) ?\"))
  13114. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13115. (po (org-op-to-function (match-string 6 term)
  13116. (if timep 'time strp))))
  13117. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13118. (when timep (setq pv (org-matcher-time pv)))
  13119. (cond ((and regexp (eq po 'org<>))
  13120. `(not (string-match ,pv (or ,gv ""))))
  13121. (regexp `(string-match ,pv (or ,gv "")))
  13122. (strp `(,po (or ,gv "") ,pv))
  13123. (t
  13124. `(,po
  13125. (string-to-number (or ,gv ""))
  13126. ,(string-to-number pv))))))
  13127. (t `(member ,tag tags-list)))))
  13128. (push (if minus `(not ,mm) mm) tagsmatcher)
  13129. (setq term rest)))
  13130. (push `(and ,@tagsmatcher) orlist)
  13131. (setq tagsmatcher nil))
  13132. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13133. ;; Make the TODO matcher.
  13134. (when (org-string-nw-p todomatch)
  13135. (let ((orlist nil))
  13136. (dolist (term (org-split-string todomatch "|"))
  13137. (while (string-match re term)
  13138. (let* ((minus (and (match-end 1)
  13139. (equal (match-string 1 term) "-")))
  13140. (kwd (match-string 2 term))
  13141. (regexp (eq (string-to-char kwd) ?{))
  13142. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13143. `(equal todo ,kwd))))
  13144. (push (if minus `(not ,mm) mm) todomatcher))
  13145. (setq term (substring term (match-end 0))))
  13146. (push (if (> (length todomatcher) 1)
  13147. (cons 'and todomatcher)
  13148. (car todomatcher))
  13149. orlist)
  13150. (setq todomatcher nil))
  13151. (setq todomatcher (cons 'or orlist))))
  13152. ;; Return the string and function of the matcher. If no
  13153. ;; tags-specific or todo-specific matcher exists, match
  13154. ;; everything.
  13155. (let ((matcher (if (and tagsmatcher todomatcher)
  13156. `(and ,tagsmatcher ,todomatcher)
  13157. (or tagsmatcher todomatcher t))))
  13158. (when org--matcher-tags-todo-only
  13159. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13160. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13161. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13162. "Expand group tags in MATCH.
  13163. This replaces every group tag in MATCH with a regexp tag search.
  13164. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13165. will be replaced like this:
  13166. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13167. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13168. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13169. Replacing by a regexp preserves the structure of the match.
  13170. E.g., this expansion
  13171. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13172. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13173. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13174. A group tag in MATCH can contain regular expressions of its own.
  13175. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13176. will be replaced like this:
  13177. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13178. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13179. assumed to be a single group tag, and the function will return
  13180. the list of tags in this group.
  13181. When DOWNCASE is non-nil, expand downcased TAGS."
  13182. (if org-group-tags
  13183. (let* ((case-fold-search t)
  13184. (stable org-mode-syntax-table)
  13185. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13186. (taggroups (if downcased
  13187. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13188. taggroups)
  13189. taggroups))
  13190. (taggroups-keys (mapcar #'car taggroups))
  13191. (return-match (if downcased (downcase match) match))
  13192. (count 0)
  13193. (work-already-expanded tags-already-expanded)
  13194. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13195. ;; @ and _ are allowed as word-components in tags.
  13196. (modify-syntax-entry ?@ "w" stable)
  13197. (modify-syntax-entry ?_ "w" stable)
  13198. ;; Temporarily replace regexp-expressions in the match-expression.
  13199. (while (string-match "{.+?}" return-match)
  13200. (cl-incf count)
  13201. (push (match-string 0 return-match) regexps-in-match)
  13202. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13203. (while (and taggroups-keys
  13204. (with-syntax-table stable
  13205. (string-match
  13206. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13207. (regexp-opt taggroups-keys) "\\>\\)")
  13208. return-match)))
  13209. (let* ((dir (match-string 1 return-match))
  13210. (tag (match-string 2 return-match))
  13211. (tag (if downcased (downcase tag) tag)))
  13212. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13213. (member tag work-already-expanded))
  13214. (setq tags-in-group (assoc tag taggroups))
  13215. (push tag work-already-expanded)
  13216. ;; Recursively expand each tag in the group, if the tag hasn't
  13217. ;; already been expanded. Restore the match-data after all recursive calls.
  13218. (save-match-data
  13219. (let (tags-expanded)
  13220. (dolist (x (cdr tags-in-group))
  13221. (if (and (member x taggroups-keys)
  13222. (not (member x work-already-expanded)))
  13223. (setq tags-expanded
  13224. (delete-dups
  13225. (append
  13226. (org-tags-expand x t downcased
  13227. work-already-expanded)
  13228. tags-expanded)))
  13229. (setq tags-expanded
  13230. (append (list x) tags-expanded)))
  13231. (setq work-already-expanded
  13232. (delete-dups
  13233. (append tags-expanded
  13234. work-already-expanded))))
  13235. (setq tags-in-group
  13236. (delete-dups (cons (car tags-in-group)
  13237. tags-expanded)))))
  13238. ;; Filter tag-regexps from tags.
  13239. (setq regexp-in-group-escaped
  13240. (delq nil (mapcar (lambda (x)
  13241. (if (stringp x)
  13242. (and (equal "{" (substring x 0 1))
  13243. (equal "}" (substring x -1))
  13244. x)
  13245. x))
  13246. tags-in-group))
  13247. regexp-in-group
  13248. (mapcar (lambda (x)
  13249. (substring x 1 -1))
  13250. regexp-in-group-escaped)
  13251. tags-in-group
  13252. (delq nil (mapcar (lambda (x)
  13253. (if (stringp x)
  13254. (and (not (equal "{" (substring x 0 1)))
  13255. (not (equal "}" (substring x -1)))
  13256. x)
  13257. x))
  13258. tags-in-group)))
  13259. ;; If single-as-list, do no more in the while-loop.
  13260. (if (not single-as-list)
  13261. (progn
  13262. (when regexp-in-group
  13263. (setq regexp-in-group
  13264. (concat "\\|"
  13265. (mapconcat 'identity regexp-in-group
  13266. "\\|"))))
  13267. (setq tags-in-group
  13268. (concat dir
  13269. "{\\<"
  13270. (regexp-opt tags-in-group)
  13271. "\\>"
  13272. regexp-in-group
  13273. "}"))
  13274. (when (stringp tags-in-group)
  13275. (org-add-props tags-in-group '(grouptag t)))
  13276. (setq return-match
  13277. (replace-match tags-in-group t t return-match)))
  13278. (setq tags-in-group
  13279. (append regexp-in-group-escaped tags-in-group))))
  13280. (setq taggroups-keys (delete tag taggroups-keys))))
  13281. ;; Add the regular expressions back into the match-expression again.
  13282. (while regexps-in-match
  13283. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13284. (pop regexps-in-match)
  13285. return-match t t))
  13286. (cl-decf count))
  13287. (if single-as-list
  13288. (if tags-in-group tags-in-group (list return-match))
  13289. return-match))
  13290. (if single-as-list
  13291. (list (if downcased (downcase match) match))
  13292. match)))
  13293. (defun org-op-to-function (op &optional stringp)
  13294. "Turn an operator into the appropriate function."
  13295. (setq op
  13296. (cond
  13297. ((equal op "<" ) '(< string< org-time<))
  13298. ((equal op ">" ) '(> org-string> org-time>))
  13299. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13300. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13301. ((member op '("=" "==")) '(= string= org-time=))
  13302. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13303. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13304. (defun org<> (a b) (not (= a b)))
  13305. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13306. (defun org-string>= (a b) (not (string< a b)))
  13307. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13308. (defun org-string<> (a b) (not (string= a b)))
  13309. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13310. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13311. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13312. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13313. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13314. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13315. (defun org-2ft (s)
  13316. "Convert S to a floating point time.
  13317. If S is already a number, just return it. If it is a string, parse
  13318. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13319. (cond
  13320. ((numberp s) s)
  13321. ((stringp s)
  13322. (condition-case nil
  13323. (float-time (apply 'encode-time (org-parse-time-string s)))
  13324. (error 0.)))
  13325. (t 0.)))
  13326. (defun org-time-today ()
  13327. "Time in seconds today at 0:00.
  13328. Returns the float number of seconds since the beginning of the
  13329. epoch to the beginning of today (00:00)."
  13330. (float-time (apply 'encode-time
  13331. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13332. (defun org-matcher-time (s)
  13333. "Interpret a time comparison value."
  13334. (save-match-data
  13335. (cond
  13336. ((string= s "<now>") (float-time))
  13337. ((string= s "<today>") (org-time-today))
  13338. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13339. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13340. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13341. (+ (org-time-today)
  13342. (* (string-to-number (match-string 1 s))
  13343. (cdr (assoc (match-string 2 s)
  13344. '(("d" . 86400.0) ("w" . 604800.0)
  13345. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13346. (t (org-2ft s)))))
  13347. (defun org-match-any-p (re list)
  13348. "Does re match any element of list?"
  13349. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13350. (delq nil list))
  13351. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13352. (defvar org-tags-overlay (make-overlay 1 1))
  13353. (org-detach-overlay org-tags-overlay)
  13354. (defun org-get-local-tags-at (&optional pos)
  13355. "Get a list of tags defined in the current headline."
  13356. (org-get-tags-at pos 'local))
  13357. (defun org-get-local-tags ()
  13358. "Get a list of tags defined in the current headline."
  13359. (org-get-tags-at nil 'local))
  13360. (defun org-get-tags-at (&optional pos local)
  13361. "Get a list of all headline tags applicable at POS.
  13362. POS defaults to point. If tags are inherited, the list contains
  13363. the targets in the same sequence as the headlines appear, i.e.
  13364. the tags of the current headline come last.
  13365. When LOCAL is non-nil, only return tags from the current headline,
  13366. ignore inherited ones."
  13367. (interactive)
  13368. (if (and org-trust-scanner-tags
  13369. (or (not pos) (equal pos (point)))
  13370. (not local))
  13371. org-scanner-tags
  13372. (let (tags ltags lastpos parent)
  13373. (save-excursion
  13374. (save-restriction
  13375. (widen)
  13376. (goto-char (or pos (point)))
  13377. (save-match-data
  13378. (catch 'done
  13379. (condition-case nil
  13380. (progn
  13381. (org-back-to-heading t)
  13382. (while (not (equal lastpos (point)))
  13383. (setq lastpos (point))
  13384. (when (looking-at
  13385. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13386. (setq ltags (org-split-string
  13387. (org-match-string-no-properties 1) ":"))
  13388. (when parent
  13389. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13390. (setq tags (append
  13391. (if parent
  13392. (org-remove-uninherited-tags ltags)
  13393. ltags)
  13394. tags)))
  13395. (or org-use-tag-inheritance (throw 'done t))
  13396. (when local (throw 'done t))
  13397. (or (org-up-heading-safe) (error nil))
  13398. (setq parent t)))
  13399. (error nil)))))
  13400. (if local
  13401. tags
  13402. (reverse (delete-dups
  13403. (reverse (append
  13404. (org-remove-uninherited-tags
  13405. org-file-tags)
  13406. tags)))))))))
  13407. (defun org-add-prop-inherited (s)
  13408. (add-text-properties 0 (length s) '(inherited t) s)
  13409. s)
  13410. (defun org-toggle-tag (tag &optional onoff)
  13411. "Toggle the tag TAG for the current line.
  13412. If ONOFF is `on' or `off', don't toggle but set to this state."
  13413. (let (res current)
  13414. (save-excursion
  13415. (org-back-to-heading t)
  13416. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13417. (point-at-eol) t)
  13418. (progn
  13419. (setq current (match-string 1))
  13420. (replace-match ""))
  13421. (setq current ""))
  13422. (setq current (nreverse (org-split-string current ":")))
  13423. (cond
  13424. ((eq onoff 'on)
  13425. (setq res t)
  13426. (or (member tag current) (push tag current)))
  13427. ((eq onoff 'off)
  13428. (or (not (member tag current)) (setq current (delete tag current))))
  13429. (t (if (member tag current)
  13430. (setq current (delete tag current))
  13431. (setq res t)
  13432. (push tag current))))
  13433. (end-of-line 1)
  13434. (if current
  13435. (progn
  13436. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13437. (org-set-tags nil t))
  13438. (delete-horizontal-space))
  13439. (run-hooks 'org-after-tags-change-hook))
  13440. res))
  13441. (defun org-align-tags-here (to-col)
  13442. ;; Assumes that this is a headline
  13443. "Align tags on the current headline to TO-COL."
  13444. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13445. (beginning-of-line 1)
  13446. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13447. (< pos (match-beginning 2)))
  13448. (progn
  13449. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13450. (goto-char (match-beginning 1))
  13451. (insert " ")
  13452. (delete-region (point) (1+ (match-beginning 2)))
  13453. (setq ncol (max (current-column)
  13454. (1+ col)
  13455. (if (> to-col 0)
  13456. to-col
  13457. (- (abs to-col) tags-l))))
  13458. (setq p (point))
  13459. (insert (make-string (- ncol (current-column)) ?\ ))
  13460. (setq ncol (current-column))
  13461. (when indent-tabs-mode (tabify p (point-at-eol)))
  13462. (org-move-to-column (min ncol col)))
  13463. (goto-char pos))))
  13464. (defun org-set-tags-command (&optional arg just-align)
  13465. "Call the set-tags command for the current entry."
  13466. (interactive "P")
  13467. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13468. (org-set-tags arg just-align)
  13469. (save-excursion
  13470. (unless (and (org-region-active-p)
  13471. org-loop-over-headlines-in-active-region)
  13472. (org-back-to-heading t))
  13473. (org-set-tags arg just-align))))
  13474. (defun org-set-tags-to (data)
  13475. "Set the tags of the current entry to DATA, replacing the current tags.
  13476. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13477. If DATA is nil or the empty string, any tags will be removed."
  13478. (interactive "sTags: ")
  13479. (setq data
  13480. (cond
  13481. ((eq data nil) "")
  13482. ((equal data "") "")
  13483. ((stringp data)
  13484. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13485. ":"))
  13486. ((listp data)
  13487. (concat ":" (mapconcat 'identity data ":") ":"))))
  13488. (when data
  13489. (save-excursion
  13490. (org-back-to-heading t)
  13491. (when (looking-at org-complex-heading-regexp)
  13492. (if (match-end 5)
  13493. (progn
  13494. (goto-char (match-beginning 5))
  13495. (insert data)
  13496. (delete-region (point) (point-at-eol))
  13497. (org-set-tags nil 'align))
  13498. (goto-char (point-at-eol))
  13499. (insert " " data)
  13500. (org-set-tags nil 'align)))
  13501. (beginning-of-line 1)
  13502. (when (looking-at ".*?\\([ \t]+\\)$")
  13503. (delete-region (match-beginning 1) (match-end 1))))))
  13504. (defun org-align-all-tags ()
  13505. "Align the tags i all headings."
  13506. (interactive)
  13507. (save-excursion
  13508. (or (ignore-errors (org-back-to-heading t))
  13509. (outline-next-heading))
  13510. (if (org-at-heading-p)
  13511. (org-set-tags t)
  13512. (message "No headings"))))
  13513. (defvar org-indent-indentation-per-level)
  13514. (defun org-set-tags (&optional arg just-align)
  13515. "Set the tags for the current headline.
  13516. With prefix ARG, realign all tags in headings in the current buffer.
  13517. When JUST-ALIGN is non-nil, only align tags."
  13518. (interactive "P")
  13519. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13520. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13521. 'region-start-level
  13522. 'region))
  13523. org-loop-over-headlines-in-active-region)
  13524. (org-map-entries
  13525. ;; We don't use ARG and JUST-ALIGN here because these args
  13526. ;; are not useful when looping over headlines.
  13527. #'org-set-tags
  13528. org-loop-over-headlines-in-active-region
  13529. cl
  13530. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13531. (let ((org-setting-tags t))
  13532. (if arg
  13533. (save-excursion
  13534. (goto-char (point-min))
  13535. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13536. (while (re-search-forward org-outline-regexp-bol nil t)
  13537. (org-set-tags nil t)
  13538. (end-of-line)))
  13539. (message "All tags realigned to column %d" org-tags-column))
  13540. (let* ((current (org-get-tags-string))
  13541. (col (current-column))
  13542. (tags
  13543. (if just-align current
  13544. ;; Get a new set of tags from the user.
  13545. (save-excursion
  13546. (let* ((table
  13547. (setq
  13548. org-last-tags-completion-table
  13549. (append
  13550. org-tag-persistent-alist
  13551. (or org-tag-alist (org-get-buffer-tags))
  13552. (and
  13553. org-complete-tags-always-offer-all-agenda-tags
  13554. (org-global-tags-completion-table
  13555. (org-agenda-files))))))
  13556. (current-tags (org-split-string current ":"))
  13557. (inherited-tags
  13558. (nreverse (nthcdr (length current-tags)
  13559. (nreverse (org-get-tags-at))))))
  13560. (replace-regexp-in-string
  13561. "\\([-+&]+\\|,\\)"
  13562. ":"
  13563. (if (or (eq t org-use-fast-tag-selection)
  13564. (and org-use-fast-tag-selection
  13565. (delq nil (mapcar #'cdr table))))
  13566. (org-fast-tag-selection
  13567. current-tags inherited-tags table
  13568. (and org-fast-tag-selection-include-todo
  13569. org-todo-key-alist))
  13570. (let ((org-add-colon-after-tag-completion
  13571. (< 1 (length table))))
  13572. (org-trim
  13573. (completing-read
  13574. "Tags: "
  13575. #'org-tags-completion-function
  13576. nil nil current 'org-tags-history))))))))))
  13577. (when org-tags-sort-function
  13578. (setq tags
  13579. (mapconcat
  13580. #'identity
  13581. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13582. org-tags-sort-function)
  13583. ":")))
  13584. (if (not (org-string-nw-p tags)) (setq tags "")
  13585. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13586. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13587. ;; Insert new tags at the correct column
  13588. (beginning-of-line)
  13589. (let ((level (if (looking-at org-outline-regexp)
  13590. (- (match-end 0) (point) 1)
  13591. 1)))
  13592. (cond
  13593. ((and (equal current "") (equal tags "")))
  13594. ((re-search-forward
  13595. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13596. (line-end-position)
  13597. t)
  13598. (if (equal tags "") (replace-match "" t t)
  13599. (goto-char (match-beginning 0))
  13600. (let* ((c0 (current-column))
  13601. ;; Compute offset for the case of org-indent-mode
  13602. ;; active.
  13603. (di (if (org-bound-and-true-p org-indent-mode)
  13604. (* (1- org-indent-indentation-per-level)
  13605. (1- level))
  13606. 0))
  13607. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13608. (tc (+ org-tags-column
  13609. (if (> org-tags-column 0) (- di) di)))
  13610. (c1 (max (1+ c0)
  13611. (if (> tc 0) tc
  13612. (- (- tc) (string-width tags)))))
  13613. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13614. (replace-match rpl t t)
  13615. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13616. (tabify p0 (point))))))
  13617. (t (error "Tags alignment failed"))))
  13618. (org-move-to-column col))
  13619. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13620. (defun org-change-tag-in-region (beg end tag off)
  13621. "Add or remove TAG for each entry in the region.
  13622. This works in the agenda, and also in an org-mode buffer."
  13623. (interactive
  13624. (list (region-beginning) (region-end)
  13625. (let ((org-last-tags-completion-table
  13626. (if (derived-mode-p 'org-mode)
  13627. (org-uniquify
  13628. (delq nil (append (org-get-buffer-tags)
  13629. (org-global-tags-completion-table))))
  13630. (org-global-tags-completion-table))))
  13631. (completing-read
  13632. "Tag: " 'org-tags-completion-function nil nil nil
  13633. 'org-tags-history))
  13634. (progn
  13635. (message "[s]et or [r]emove? ")
  13636. (equal (read-char-exclusive) ?r))))
  13637. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13638. (let ((agendap (equal major-mode 'org-agenda-mode))
  13639. l1 l2 m buf pos newhead (cnt 0))
  13640. (goto-char end)
  13641. (setq l2 (1- (org-current-line)))
  13642. (goto-char beg)
  13643. (setq l1 (org-current-line))
  13644. (cl-loop for l from l1 to l2 do
  13645. (org-goto-line l)
  13646. (setq m (get-text-property (point) 'org-hd-marker))
  13647. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13648. (and agendap m))
  13649. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13650. pos (if agendap m (point)))
  13651. (with-current-buffer buf
  13652. (save-excursion
  13653. (save-restriction
  13654. (goto-char pos)
  13655. (setq cnt (1+ cnt))
  13656. (org-toggle-tag tag (if off 'off 'on))
  13657. (setq newhead (org-get-heading)))))
  13658. (and agendap (org-agenda-change-all-lines newhead m))))
  13659. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13660. (defun org-tags-completion-function (string _predicate &optional flag)
  13661. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13662. (confirm (lambda (x) (stringp (car x)))))
  13663. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13664. (setq s1 (match-string 1 string)
  13665. s2 (match-string 2 string))
  13666. (setq s1 "" s2 string))
  13667. (cond
  13668. ((eq flag nil)
  13669. ;; try completion
  13670. (setq rtn (try-completion s2 ctable confirm))
  13671. (when (stringp rtn)
  13672. (setq rtn
  13673. (concat s1 s2 (substring rtn (length s2))
  13674. (if (and org-add-colon-after-tag-completion
  13675. (assoc rtn ctable))
  13676. ":" ""))))
  13677. rtn)
  13678. ((eq flag t)
  13679. ;; all-completions
  13680. (all-completions s2 ctable confirm))
  13681. ((eq flag 'lambda)
  13682. ;; exact match?
  13683. (assoc s2 ctable)))))
  13684. (defun org-fast-tag-insert (kwd tags face &optional end)
  13685. "Insert KDW, and the TAGS, the latter with face FACE.
  13686. Also insert END."
  13687. (insert (format "%-12s" (concat kwd ":"))
  13688. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13689. (or end "")))
  13690. (defun org-fast-tag-show-exit (flag)
  13691. (save-excursion
  13692. (org-goto-line 3)
  13693. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13694. (replace-match ""))
  13695. (when flag
  13696. (end-of-line 1)
  13697. (org-move-to-column (- (window-width) 19) t)
  13698. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13699. (defun org-set-current-tags-overlay (current prefix)
  13700. "Add an overlay to CURRENT tag with PREFIX."
  13701. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13702. (if (featurep 'xemacs)
  13703. (org-overlay-display org-tags-overlay (concat prefix s)
  13704. 'secondary-selection)
  13705. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13706. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13707. (defvar org-last-tag-selection-key nil)
  13708. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13709. "Fast tag selection with single keys.
  13710. CURRENT is the current list of tags in the headline, INHERITED is the
  13711. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13712. possibly with grouping information. TODO-TABLE is a similar table with
  13713. TODO keywords, should these have keys assigned to them.
  13714. If the keys are nil, a-z are automatically assigned.
  13715. Returns the new tags string, or nil to not change the current settings."
  13716. (let* ((fulltable (append table todo-table))
  13717. (maxlen (apply 'max (mapcar
  13718. (lambda (x)
  13719. (if (stringp (car x)) (string-width (car x)) 0))
  13720. fulltable)))
  13721. (buf (current-buffer))
  13722. (expert (eq org-fast-tag-selection-single-key 'expert))
  13723. (buffer-tags nil)
  13724. (fwidth (+ maxlen 3 1 3))
  13725. (ncol (/ (- (window-width) 4) fwidth))
  13726. (i-face 'org-done)
  13727. (c-face 'org-todo)
  13728. tg cnt e c char c1 c2 ntable tbl rtn
  13729. ov-start ov-end ov-prefix
  13730. (exit-after-next org-fast-tag-selection-single-key)
  13731. (done-keywords org-done-keywords)
  13732. groups ingroup intaggroup)
  13733. (save-excursion
  13734. (beginning-of-line 1)
  13735. (if (looking-at
  13736. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13737. (setq ov-start (match-beginning 1)
  13738. ov-end (match-end 1)
  13739. ov-prefix "")
  13740. (setq ov-start (1- (point-at-eol))
  13741. ov-end (1+ ov-start))
  13742. (skip-chars-forward "^\n\r")
  13743. (setq ov-prefix
  13744. (concat
  13745. (buffer-substring (1- (point)) (point))
  13746. (if (> (current-column) org-tags-column)
  13747. " "
  13748. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13749. (move-overlay org-tags-overlay ov-start ov-end)
  13750. (save-window-excursion
  13751. (if expert
  13752. (set-buffer (get-buffer-create " *Org tags*"))
  13753. (delete-other-windows)
  13754. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13755. (org-switch-to-buffer-other-window " *Org tags*"))
  13756. (erase-buffer)
  13757. (setq-local org-done-keywords done-keywords)
  13758. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13759. (org-fast-tag-insert "Current" current c-face "\n\n")
  13760. (org-fast-tag-show-exit exit-after-next)
  13761. (org-set-current-tags-overlay current ov-prefix)
  13762. (setq tbl fulltable char ?a cnt 0)
  13763. (while (setq e (pop tbl))
  13764. (cond
  13765. ((eq (car e) :startgroup)
  13766. (push '() groups) (setq ingroup t)
  13767. (unless (zerop cnt)
  13768. (setq cnt 0)
  13769. (insert "\n"))
  13770. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13771. ((eq (car e) :endgroup)
  13772. (setq ingroup nil cnt 0)
  13773. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13774. ((eq (car e) :startgrouptag)
  13775. (setq intaggroup t)
  13776. (unless (zerop cnt)
  13777. (setq cnt 0)
  13778. (insert "\n"))
  13779. (insert "[ "))
  13780. ((eq (car e) :endgrouptag)
  13781. (setq intaggroup nil cnt 0)
  13782. (insert "]\n"))
  13783. ((equal e '(:newline))
  13784. (unless (zerop cnt)
  13785. (setq cnt 0)
  13786. (insert "\n")
  13787. (setq e (car tbl))
  13788. (while (equal (car tbl) '(:newline))
  13789. (insert "\n")
  13790. (setq tbl (cdr tbl)))))
  13791. ((equal e '(:grouptags)) (insert " : "))
  13792. (t
  13793. (setq tg (copy-sequence (car e)) c2 nil)
  13794. (if (cdr e)
  13795. (setq c (cdr e))
  13796. ;; automatically assign a character.
  13797. (setq c1 (string-to-char
  13798. (downcase (substring
  13799. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13800. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13801. (while (or (rassoc char ntable) (rassoc char table))
  13802. (setq char (1+ char)))
  13803. (setq c2 c1))
  13804. (setq c (or c2 char)))
  13805. (when ingroup (push tg (car groups)))
  13806. (setq tg (org-add-props tg nil 'face
  13807. (cond
  13808. ((not (assoc tg table))
  13809. (org-get-todo-face tg))
  13810. ((member tg current) c-face)
  13811. ((member tg inherited) i-face))))
  13812. (when (equal (caar tbl) :grouptags)
  13813. (org-add-props tg nil 'face 'org-tag-group))
  13814. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13815. (insert "[" c "] " tg (make-string
  13816. (- fwidth 4 (length tg)) ?\ ))
  13817. (push (cons tg c) ntable)
  13818. (when (= (cl-incf cnt) ncol)
  13819. (insert "\n")
  13820. (when (or ingroup intaggroup) (insert " "))
  13821. (setq cnt 0)))))
  13822. (setq ntable (nreverse ntable))
  13823. (insert "\n")
  13824. (goto-char (point-min))
  13825. (unless expert (org-fit-window-to-buffer))
  13826. (setq rtn
  13827. (catch 'exit
  13828. (while t
  13829. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13830. (if (not groups) "no " "")
  13831. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13832. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13833. (setq org-last-tag-selection-key c)
  13834. (cond
  13835. ((= c ?\r) (throw 'exit t))
  13836. ((= c ?!)
  13837. (setq groups (not groups))
  13838. (goto-char (point-min))
  13839. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13840. ((= c ?\C-c)
  13841. (if (not expert)
  13842. (org-fast-tag-show-exit
  13843. (setq exit-after-next (not exit-after-next)))
  13844. (setq expert nil)
  13845. (delete-other-windows)
  13846. (set-window-buffer (split-window-vertically) " *Org tags*")
  13847. (org-switch-to-buffer-other-window " *Org tags*")
  13848. (org-fit-window-to-buffer)))
  13849. ((or (= c ?\C-g)
  13850. (and (= c ?q) (not (rassoc c ntable))))
  13851. (org-detach-overlay org-tags-overlay)
  13852. (setq quit-flag t))
  13853. ((= c ?\ )
  13854. (setq current nil)
  13855. (when exit-after-next (setq exit-after-next 'now)))
  13856. ((= c ?\t)
  13857. (condition-case nil
  13858. (setq tg (completing-read
  13859. "Tag: "
  13860. (or buffer-tags
  13861. (with-current-buffer buf
  13862. (setq buffer-tags
  13863. (org-get-buffer-tags))))))
  13864. (quit (setq tg "")))
  13865. (when (string-match "\\S-" tg)
  13866. (cl-pushnew (list tg) buffer-tags :test #'equal)
  13867. (if (member tg current)
  13868. (setq current (delete tg current))
  13869. (push tg current)))
  13870. (when exit-after-next (setq exit-after-next 'now)))
  13871. ((setq e (rassoc c todo-table) tg (car e))
  13872. (with-current-buffer buf
  13873. (save-excursion (org-todo tg)))
  13874. (when exit-after-next (setq exit-after-next 'now)))
  13875. ((setq e (rassoc c ntable) tg (car e))
  13876. (if (member tg current)
  13877. (setq current (delete tg current))
  13878. (cl-loop for g in groups do
  13879. (when (member tg g)
  13880. (dolist (x g) (setq current (delete x current)))))
  13881. (push tg current))
  13882. (when exit-after-next (setq exit-after-next 'now))))
  13883. ;; Create a sorted list
  13884. (setq current
  13885. (sort current
  13886. (lambda (a b)
  13887. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13888. (when (eq exit-after-next 'now) (throw 'exit t))
  13889. (goto-char (point-min))
  13890. (beginning-of-line 2)
  13891. (delete-region (point) (point-at-eol))
  13892. (org-fast-tag-insert "Current" current c-face)
  13893. (org-set-current-tags-overlay current ov-prefix)
  13894. (while (re-search-forward
  13895. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13896. (setq tg (match-string 1))
  13897. (add-text-properties
  13898. (match-beginning 1) (match-end 1)
  13899. (list 'face
  13900. (cond
  13901. ((member tg current) c-face)
  13902. ((member tg inherited) i-face)
  13903. (t (get-text-property (match-beginning 1) 'face))))))
  13904. (goto-char (point-min)))))
  13905. (org-detach-overlay org-tags-overlay)
  13906. (if rtn
  13907. (mapconcat 'identity current ":")
  13908. nil))))
  13909. (defun org-get-tags-string ()
  13910. "Get the TAGS string in the current headline."
  13911. (unless (org-at-heading-p t)
  13912. (user-error "Not on a heading"))
  13913. (save-excursion
  13914. (beginning-of-line 1)
  13915. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13916. (org-match-string-no-properties 1)
  13917. "")))
  13918. (defun org-get-tags ()
  13919. "Get the list of tags specified in the current headline."
  13920. (org-split-string (org-get-tags-string) ":"))
  13921. (defun org-get-buffer-tags ()
  13922. "Get a table of all tags used in the buffer, for completion."
  13923. (org-with-wide-buffer
  13924. (goto-char (point-min))
  13925. (let ((tag-re (concat org-outline-regexp-bol
  13926. "\\(?:.*?[ \t]\\)?"
  13927. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13928. tags)
  13929. (while (re-search-forward tag-re nil t)
  13930. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13931. (push tag tags)))
  13932. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13933. ;;;; The mapping API
  13934. (defvar org-agenda-skip-comment-trees)
  13935. (defvar org-agenda-skip-function)
  13936. (defun org-map-entries (func &optional match scope &rest skip)
  13937. "Call FUNC at each headline selected by MATCH in SCOPE.
  13938. FUNC is a function or a lisp form. The function will be called without
  13939. arguments, with the cursor positioned at the beginning of the headline.
  13940. The return values of all calls to the function will be collected and
  13941. returned as a list.
  13942. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13943. does not need to preserve point. After evaluation, the cursor will be
  13944. moved to the end of the line (presumably of the headline of the
  13945. processed entry) and search continues from there. Under some
  13946. circumstances, this may not produce the wanted results. For example,
  13947. if you have removed (e.g. archived) the current (sub)tree it could
  13948. mean that the next entry will be skipped entirely. In such cases, you
  13949. can specify the position from where search should continue by making
  13950. FUNC set the variable `org-map-continue-from' to the desired buffer
  13951. position.
  13952. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13953. Only headlines that are matched by this query will be considered during
  13954. the iteration. When MATCH is nil or t, all headlines will be
  13955. visited by the iteration.
  13956. SCOPE determines the scope of this command. It can be any of:
  13957. nil The current buffer, respecting the restriction if any
  13958. tree The subtree started with the entry at point
  13959. region The entries within the active region, if any
  13960. region-start-level
  13961. The entries within the active region, but only those at
  13962. the same level than the first one.
  13963. file The current buffer, without restriction
  13964. file-with-archives
  13965. The current buffer, and any archives associated with it
  13966. agenda All agenda files
  13967. agenda-with-archives
  13968. All agenda files with any archive files associated with them
  13969. \(file1 file2 ...)
  13970. If this is a list, all files in the list will be scanned
  13971. The remaining args are treated as settings for the skipping facilities of
  13972. the scanner. The following items can be given here:
  13973. archive skip trees with the archive tag
  13974. comment skip trees with the COMMENT keyword
  13975. function or Emacs Lisp form:
  13976. will be used as value for `org-agenda-skip-function', so
  13977. whenever the function returns a position, FUNC will not be
  13978. called for that entry and search will continue from the
  13979. position returned
  13980. If your function needs to retrieve the tags including inherited tags
  13981. at the *current* entry, you can use the value of the variable
  13982. `org-scanner-tags' which will be much faster than getting the value
  13983. with `org-get-tags-at'. If your function gets properties with
  13984. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13985. to t around the call to `org-entry-properties' to get the same speedup.
  13986. Note that if your function moves around to retrieve tags and properties at
  13987. a *different* entry, you cannot use these techniques."
  13988. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13989. (not (org-region-active-p)))
  13990. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13991. (org-agenda-skip-archived-trees (memq 'archive skip))
  13992. (org-agenda-skip-comment-trees (memq 'comment skip))
  13993. (org-agenda-skip-function
  13994. (car (org-delete-all '(comment archive) skip)))
  13995. (org-tags-match-list-sublevels t)
  13996. (start-level (eq scope 'region-start-level))
  13997. matcher res
  13998. org-todo-keywords-for-agenda
  13999. org-done-keywords-for-agenda
  14000. org-todo-keyword-alist-for-agenda
  14001. org-tag-alist-for-agenda
  14002. org--matcher-tags-todo-only)
  14003. (cond
  14004. ((eq match t) (setq matcher t))
  14005. ((eq match nil) (setq matcher t))
  14006. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14007. (save-excursion
  14008. (save-restriction
  14009. (cond ((eq scope 'tree)
  14010. (org-back-to-heading t)
  14011. (org-narrow-to-subtree)
  14012. (setq scope nil))
  14013. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14014. (org-region-active-p))
  14015. ;; If needed, set start-level to a string like "2"
  14016. (when start-level
  14017. (save-excursion
  14018. (goto-char (region-beginning))
  14019. (unless (org-at-heading-p) (outline-next-heading))
  14020. (setq start-level (org-current-level))))
  14021. (narrow-to-region (region-beginning)
  14022. (save-excursion
  14023. (goto-char (region-end))
  14024. (unless (and (bolp) (org-at-heading-p))
  14025. (outline-next-heading))
  14026. (point)))
  14027. (setq scope nil)))
  14028. (if (not scope)
  14029. (progn
  14030. (org-agenda-prepare-buffers
  14031. (and buffer-file-name (list buffer-file-name)))
  14032. (setq res
  14033. (org-scan-tags
  14034. func matcher org--matcher-tags-todo-only start-level)))
  14035. ;; Get the right scope
  14036. (cond
  14037. ((and scope (listp scope) (symbolp (car scope)))
  14038. (setq scope (eval scope)))
  14039. ((eq scope 'agenda)
  14040. (setq scope (org-agenda-files t)))
  14041. ((eq scope 'agenda-with-archives)
  14042. (setq scope (org-agenda-files t))
  14043. (setq scope (org-add-archive-files scope)))
  14044. ((eq scope 'file)
  14045. (setq scope (and buffer-file-name (list buffer-file-name))))
  14046. ((eq scope 'file-with-archives)
  14047. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14048. (org-agenda-prepare-buffers scope)
  14049. (dolist (file scope)
  14050. (with-current-buffer (org-find-base-buffer-visiting file)
  14051. (save-excursion
  14052. (save-restriction
  14053. (widen)
  14054. (goto-char (point-min))
  14055. (setq res
  14056. (append
  14057. res
  14058. (org-scan-tags
  14059. func matcher org--matcher-tags-todo-only))))))))))
  14060. res)))
  14061. ;;; Properties API
  14062. (defconst org-special-properties
  14063. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14064. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14065. "The special properties valid in Org mode.
  14066. These are properties that are not defined in the property drawer,
  14067. but in some other way.")
  14068. (defconst org-default-properties
  14069. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14070. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14071. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14072. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14073. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14074. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14075. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14076. "Some properties that are used by Org mode for various purposes.
  14077. Being in this list makes sure that they are offered for completion.")
  14078. (defun org--valid-property-p (property)
  14079. "Non nil when string PROPERTY is a valid property name."
  14080. (not
  14081. (or (equal property "")
  14082. (org-string-match-p "\\s-" property))))
  14083. (defun org--update-property-plist (key val props)
  14084. "Associate KEY to VAL in alist PROPS.
  14085. Modifications are made by side-effect. Return new alist."
  14086. (let* ((appending (string= (substring key -1) "+"))
  14087. (key (if appending (substring key 0 -1) key))
  14088. (old (assoc-string key props t)))
  14089. (if (not old) (cons (cons key val) props)
  14090. (setcdr old (if appending (concat (cdr old) " " val) val))
  14091. props)))
  14092. (defun org-get-property-block (&optional beg force)
  14093. "Return the (beg . end) range of the body of the property drawer.
  14094. BEG is the beginning of the current subtree, or of the part
  14095. before the first headline. If it is not given, it will be found.
  14096. If the drawer does not exist, create it if FORCE is non-nil, or
  14097. return nil."
  14098. (org-with-wide-buffer
  14099. (when beg (goto-char beg))
  14100. (unless (org-before-first-heading-p)
  14101. (let ((beg (cond (beg)
  14102. ((or (not (featurep 'org-inlinetask))
  14103. (org-inlinetask-in-task-p))
  14104. (org-back-to-heading t))
  14105. (t (org-with-limited-levels (org-back-to-heading t))))))
  14106. (forward-line)
  14107. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14108. (cond ((looking-at org-property-drawer-re)
  14109. (forward-line)
  14110. (cons (point) (progn (goto-char (match-end 0))
  14111. (line-beginning-position))))
  14112. (force
  14113. (goto-char beg)
  14114. (org-insert-property-drawer)
  14115. (let ((pos (save-excursion (search-forward ":END:")
  14116. (line-beginning-position))))
  14117. (cons pos pos))))))))
  14118. (defun org-at-property-p ()
  14119. "Non-nil when point is inside a property drawer.
  14120. See `org-property-re' for match data, if applicable."
  14121. (save-excursion
  14122. (beginning-of-line)
  14123. (and (looking-at org-property-re)
  14124. (let ((property-drawer (save-match-data (org-get-property-block))))
  14125. (and property-drawer
  14126. (>= (point) (car property-drawer))
  14127. (< (point) (cdr property-drawer)))))))
  14128. (defun org-property-action ()
  14129. "Do an action on properties."
  14130. (interactive)
  14131. (unless (org-at-property-p) (user-error "Not at a property"))
  14132. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14133. (let ((c (read-char-exclusive)))
  14134. (cl-case c
  14135. (?s (call-interactively #'org-set-property))
  14136. (?d (call-interactively #'org-delete-property))
  14137. (?D (call-interactively #'org-delete-property-globally))
  14138. (?c (call-interactively #'org-compute-property-at-point))
  14139. (otherwise (user-error "No such property action %c" c)))))
  14140. (defun org-inc-effort ()
  14141. "Increment the value of the effort property in the current entry."
  14142. (interactive)
  14143. (org-set-effort nil t))
  14144. (defvar org-clock-effort) ; Defined in org-clock.el.
  14145. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14146. (defun org-set-effort (&optional value increment)
  14147. "Set the effort property of the current entry.
  14148. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14149. 10th allowed value.
  14150. When INCREMENT is non-nil, set the property to the next allowed value."
  14151. (interactive "P")
  14152. (when (equal value 0) (setq value 10))
  14153. (let* ((completion-ignore-case t)
  14154. (prop org-effort-property)
  14155. (cur (org-entry-get nil prop))
  14156. (allowed (org-property-get-allowed-values nil prop 'table))
  14157. (existing (mapcar 'list (org-property-values prop)))
  14158. (heading (nth 4 (org-heading-components)))
  14159. rpl
  14160. (val (cond
  14161. ((stringp value) value)
  14162. ((and allowed (integerp value))
  14163. (or (car (nth (1- value) allowed))
  14164. (car (org-last allowed))))
  14165. ((and allowed increment)
  14166. (or (cl-caadr (member (list cur) allowed))
  14167. (user-error "Allowed effort values are not set")))
  14168. (allowed
  14169. (message "Select 1-9,0, [RET%s]: %s"
  14170. (if cur (concat "=" cur) "")
  14171. (mapconcat 'car allowed " "))
  14172. (setq rpl (read-char-exclusive))
  14173. (if (equal rpl ?\r)
  14174. cur
  14175. (setq rpl (- rpl ?0))
  14176. (when (equal rpl 0) (setq rpl 10))
  14177. (if (and (> rpl 0) (<= rpl (length allowed)))
  14178. (car (nth (1- rpl) allowed))
  14179. (org-completing-read "Effort: " allowed nil))))
  14180. (t
  14181. (org-completing-read
  14182. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14183. (concat "[" cur "]") "")
  14184. ": ")
  14185. existing nil nil "" nil cur)))))
  14186. (unless (equal (org-entry-get nil prop) val)
  14187. (org-entry-put nil prop val))
  14188. (org-refresh-property
  14189. '((effort . identity)
  14190. (effort-minutes . org-duration-string-to-minutes))
  14191. val)
  14192. (when (string= heading org-clock-current-task)
  14193. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14194. (org-clock-update-mode-line))
  14195. (message "%s is now %s" prop val)))
  14196. (defun org-entry-properties (&optional pom which)
  14197. "Get all properties of the current entry.
  14198. When POM is a buffer position, get all properties from the entry
  14199. there instead.
  14200. This includes the TODO keyword, the tags, time strings for
  14201. deadline, scheduled, and clocking, and any additional properties
  14202. defined in the entry.
  14203. If WHICH is nil or `all', get all properties. If WHICH is
  14204. `special' or `standard', only get that subclass. If WHICH is
  14205. a string, only get that property.
  14206. Return value is an alist. Keys are properties, as upcased
  14207. strings."
  14208. (org-with-point-at pom
  14209. (when (and (derived-mode-p 'org-mode)
  14210. (ignore-errors (org-back-to-heading t)))
  14211. (catch 'exit
  14212. (let* ((beg (point))
  14213. (specific (and (stringp which) (upcase which)))
  14214. (which (cond ((not specific) which)
  14215. ((member specific org-special-properties) 'special)
  14216. (t 'standard)))
  14217. props)
  14218. ;; Get the special properties, like TODO and TAGS.
  14219. (when (memq which '(nil all special))
  14220. (when (or (not specific) (string= specific "CLOCKSUM"))
  14221. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14222. (when clocksum
  14223. (push (cons "CLOCKSUM"
  14224. (org-columns-number-to-string
  14225. (/ (float clocksum) 60.) 'add_times))
  14226. props)))
  14227. (when specific (throw 'exit props)))
  14228. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14229. (let ((clocksumt (get-text-property (point)
  14230. :org-clock-minutes-today)))
  14231. (when clocksumt
  14232. (push (cons "CLOCKSUM_T"
  14233. (org-columns-number-to-string
  14234. (/ (float clocksumt) 60.) 'add_times))
  14235. props)))
  14236. (when specific (throw 'exit props)))
  14237. (when (or (not specific) (string= specific "ITEM"))
  14238. (when (looking-at org-complex-heading-regexp)
  14239. (push (cons "ITEM"
  14240. (concat
  14241. (org-match-string-no-properties 1)
  14242. (let ((title (org-match-string-no-properties 4)))
  14243. (when (org-string-nw-p title)
  14244. (concat " " (org-remove-tabs title))))))
  14245. props))
  14246. (when specific (throw 'exit props)))
  14247. (when (or (not specific) (string= specific "TODO"))
  14248. (let ((case-fold-search nil))
  14249. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14250. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14251. (when specific (throw 'exit props)))
  14252. (when (or (not specific) (string= specific "PRIORITY"))
  14253. (push (cons "PRIORITY"
  14254. (if (looking-at org-priority-regexp)
  14255. (org-match-string-no-properties 2)
  14256. (char-to-string org-default-priority)))
  14257. props)
  14258. (when specific (throw 'exit props)))
  14259. (when (or (not specific) (string= specific "FILE"))
  14260. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14261. props)
  14262. (when specific (throw 'exit props)))
  14263. (when (or (not specific) (string= specific "TAGS"))
  14264. (let ((value (org-string-nw-p (org-get-tags-string))))
  14265. (when value (push (cons "TAGS" value) props)))
  14266. (when specific (throw 'exit props)))
  14267. (when (or (not specific) (string= specific "ALLTAGS"))
  14268. (let ((value (org-get-tags-at)))
  14269. (when value
  14270. (push (cons "ALLTAGS"
  14271. (format ":%s:" (mapconcat #'identity value ":")))
  14272. props)))
  14273. (when specific (throw 'exit props)))
  14274. (when (or (not specific) (string= specific "BLOCKED"))
  14275. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14276. (when specific (throw 'exit props)))
  14277. (when (or (not specific)
  14278. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14279. (forward-line)
  14280. (when (org-looking-at-p org-planning-line-re)
  14281. (end-of-line)
  14282. (let ((bol (line-beginning-position))
  14283. ;; Backward compatibility: time keywords used to
  14284. ;; be configurable (before 8.3). Make sure we
  14285. ;; get the correct keyword.
  14286. (key-assoc `(("CLOSED" . ,org-closed-string)
  14287. ("DEADLINE" . ,org-deadline-string)
  14288. ("SCHEDULED" . ,org-scheduled-string))))
  14289. (dolist (pair (if specific (list (assoc specific key-assoc))
  14290. key-assoc))
  14291. (save-excursion
  14292. (when (search-backward (cdr pair) bol t)
  14293. (goto-char (match-end 0))
  14294. (skip-chars-forward " \t")
  14295. (and (looking-at org-ts-regexp-both)
  14296. (push (cons (car pair)
  14297. (org-match-string-no-properties 0))
  14298. props)))))))
  14299. (when specific (throw 'exit props)))
  14300. (when (or (not specific)
  14301. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14302. (let ((find-ts
  14303. (lambda (end ts)
  14304. ;; Fix next time-stamp before END. TS is the
  14305. ;; list of time-stamps found so far.
  14306. (let ((ts ts)
  14307. (regexp (cond
  14308. ((string= specific "TIMESTAMP")
  14309. org-ts-regexp)
  14310. ((string= specific "TIMESTAMP_IA")
  14311. org-ts-regexp-inactive)
  14312. ((assoc "TIMESTAMP_IA" ts)
  14313. org-ts-regexp)
  14314. ((assoc "TIMESTAMP" ts)
  14315. org-ts-regexp-inactive)
  14316. (t org-ts-regexp-both))))
  14317. (catch 'next
  14318. (while (re-search-forward regexp end t)
  14319. (backward-char)
  14320. (let ((object (org-element-context)))
  14321. ;; Accept to match timestamps in node
  14322. ;; properties, too.
  14323. (when (memq (org-element-type object)
  14324. '(node-property timestamp))
  14325. (let ((type
  14326. (org-element-property :type object)))
  14327. (cond
  14328. ((and (memq type '(active active-range))
  14329. (not (equal specific "TIMESTAMP_IA")))
  14330. (unless (assoc "TIMESTAMP" ts)
  14331. (push (cons "TIMESTAMP"
  14332. (org-element-property
  14333. :raw-value object))
  14334. ts)
  14335. (when specific (throw 'exit ts))))
  14336. ((and (memq type '(inactive inactive-range))
  14337. (not (string= specific "TIMESTAMP")))
  14338. (unless (assoc "TIMESTAMP_IA" ts)
  14339. (push (cons "TIMESTAMP_IA"
  14340. (org-element-property
  14341. :raw-value object))
  14342. ts)
  14343. (when specific (throw 'exit ts))))))
  14344. ;; Both timestamp types are found,
  14345. ;; move to next part.
  14346. (when (= (length ts) 2) (throw 'next ts)))))
  14347. ts)))))
  14348. (goto-char beg)
  14349. ;; First look for timestamps within headline.
  14350. (let ((ts (funcall find-ts (line-end-position) nil)))
  14351. (if (= (length ts) 2) (setq props (nconc ts props))
  14352. (forward-line)
  14353. ;; Then find timestamps in the section, skipping
  14354. ;; planning line.
  14355. (when (org-looking-at-p org-planning-line-re)
  14356. (forward-line))
  14357. (let ((end (save-excursion (outline-next-heading))))
  14358. (setq props (nconc (funcall find-ts end ts) props))))))))
  14359. ;; Get the standard properties, like :PROP:.
  14360. (when (memq which '(nil all standard))
  14361. ;; If we are looking after a specific property, delegate
  14362. ;; to `org-entry-get', which is faster. However, make an
  14363. ;; exception for "CATEGORY", since it can be also set
  14364. ;; through keywords (i.e. #+CATEGORY).
  14365. (if (and specific (not (equal specific "CATEGORY")))
  14366. (let ((value (org-entry-get beg specific nil t)))
  14367. (throw 'exit (and value (list (cons specific value)))))
  14368. (let ((range (org-get-property-block beg)))
  14369. (when range
  14370. (let ((end (cdr range)) seen-base)
  14371. (goto-char (car range))
  14372. ;; Unlike to `org--update-property-plist', we
  14373. ;; handle the case where base values is found
  14374. ;; after its extension. We also forbid standard
  14375. ;; properties to be named as special properties.
  14376. (while (re-search-forward org-property-re end t)
  14377. (let* ((key (upcase (org-match-string-no-properties 2)))
  14378. (extendp (org-string-match-p "\\+\\'" key))
  14379. (key-base (if extendp (substring key 0 -1) key))
  14380. (value (org-match-string-no-properties 3)))
  14381. (cond
  14382. ((member-ignore-case key-base org-special-properties))
  14383. (extendp
  14384. (setq props
  14385. (org--update-property-plist key value props)))
  14386. ((member key seen-base))
  14387. (t (push key seen-base)
  14388. (let ((p (assoc-string key props t)))
  14389. (if p (setcdr p (concat value " " (cdr p)))
  14390. (push (cons key value) props))))))))))))
  14391. (unless (assoc "CATEGORY" props)
  14392. (push (cons "CATEGORY" (org-get-category beg)) props)
  14393. (when (string= specific "CATEGORY") (throw 'exit props)))
  14394. ;; Return value.
  14395. props)))))
  14396. (defun org-property--local-values (property literal-nil)
  14397. "Return value for PROPERTY in current entry.
  14398. Value is a list whose care is the base value for PROPERTY and cdr
  14399. a list of accumulated values. Return nil if neither is found in
  14400. the entry. Also return nil when PROPERTY is set to \"nil\",
  14401. unless LITERAL-NIL is non-nil."
  14402. (let ((range (org-get-property-block)))
  14403. (when range
  14404. (goto-char (car range))
  14405. (let* ((case-fold-search t)
  14406. (end (cdr range))
  14407. (value
  14408. ;; Base value.
  14409. (save-excursion
  14410. (let ((v (and (re-search-forward
  14411. (org-re-property property nil t) end t)
  14412. (org-match-string-no-properties 3))))
  14413. (list (if literal-nil v (org-not-nil v)))))))
  14414. ;; Find additional values.
  14415. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14416. (while (re-search-forward property+ end t)
  14417. (push (org-match-string-no-properties 3) value)))
  14418. ;; Return final values.
  14419. (and (not (equal value '(nil))) (nreverse value))))))
  14420. (defun org-entry-get (pom property &optional inherit literal-nil)
  14421. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14422. If INHERIT is non-nil and the entry does not have the property,
  14423. then also check higher levels of the hierarchy. If INHERIT is
  14424. the symbol `selective', use inheritance only if the setting in
  14425. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14426. If the property is present but empty, the return value is the
  14427. empty string. If the property is not present at all, nil is
  14428. returned. In any other case, return the value as a string.
  14429. Search is case-insensitive.
  14430. If LITERAL-NIL is set, return the string value \"nil\" as
  14431. a string, do not interpret it as the list atom nil. This is used
  14432. for inheritance when a \"nil\" value can supersede a non-nil
  14433. value higher up the hierarchy."
  14434. (org-with-point-at pom
  14435. (cond
  14436. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14437. ;; We need a special property. Use `org-entry-properties' to
  14438. ;; retrieve it, but specify the wanted property.
  14439. (cdr (assoc-string property (org-entry-properties nil property))))
  14440. ((and inherit
  14441. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14442. (org-entry-get-with-inheritance property literal-nil))
  14443. (t
  14444. (let* ((local (org-property--local-values property literal-nil))
  14445. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14446. (if literal-nil value (org-not-nil value)))))))
  14447. (defun org-property-or-variable-value (var &optional inherit)
  14448. "Check if there is a property fixing the value of VAR.
  14449. If yes, return this value. If not, return the current value of the variable."
  14450. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14451. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14452. (read prop)
  14453. (symbol-value var))))
  14454. (defun org-entry-delete (pom property)
  14455. "Delete PROPERTY from entry at point-or-marker POM.
  14456. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14457. non-nil when a property was removed."
  14458. (unless (member property org-special-properties)
  14459. (org-with-point-at pom
  14460. (let ((range (org-get-property-block)))
  14461. (when range
  14462. (let* ((begin (car range))
  14463. (origin (cdr range))
  14464. (end (copy-marker origin))
  14465. (re (org-re-property
  14466. (concat (regexp-quote property) "\\+?") t t)))
  14467. (goto-char begin)
  14468. (while (re-search-forward re end t)
  14469. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14470. ;; If drawer is empty, remove it altogether.
  14471. (when (= begin end)
  14472. (delete-region (line-beginning-position 0)
  14473. (line-beginning-position 2)))
  14474. ;; Return non-nil if some property was removed.
  14475. (prog1 (/= end origin) (set-marker end nil))))))))
  14476. ;; Multi-values properties are properties that contain multiple values
  14477. ;; These values are assumed to be single words, separated by whitespace.
  14478. (defun org-entry-add-to-multivalued-property (pom property value)
  14479. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14480. (let* ((old (org-entry-get pom property))
  14481. (values (and old (org-split-string old "[ \t]"))))
  14482. (setq value (org-entry-protect-space value))
  14483. (unless (member value values)
  14484. (setq values (append values (list value)))
  14485. (org-entry-put pom property
  14486. (mapconcat 'identity values " ")))))
  14487. (defun org-entry-remove-from-multivalued-property (pom property value)
  14488. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14489. (let* ((old (org-entry-get pom property))
  14490. (values (and old (org-split-string old "[ \t]"))))
  14491. (setq value (org-entry-protect-space value))
  14492. (when (member value values)
  14493. (setq values (delete value values))
  14494. (org-entry-put pom property
  14495. (mapconcat 'identity values " ")))))
  14496. (defun org-entry-member-in-multivalued-property (pom property value)
  14497. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14498. (let* ((old (org-entry-get pom property))
  14499. (values (and old (org-split-string old "[ \t]"))))
  14500. (setq value (org-entry-protect-space value))
  14501. (member value values)))
  14502. (defun org-entry-get-multivalued-property (pom property)
  14503. "Return a list of values in a multivalued property."
  14504. (let* ((value (org-entry-get pom property))
  14505. (values (and value (org-split-string value "[ \t]"))))
  14506. (mapcar 'org-entry-restore-space values)))
  14507. (defun org-entry-put-multivalued-property (pom property &rest values)
  14508. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14509. VALUES should be a list of strings. Spaces will be protected."
  14510. (org-entry-put pom property
  14511. (mapconcat 'org-entry-protect-space values " "))
  14512. (let* ((value (org-entry-get pom property))
  14513. (values (and value (org-split-string value "[ \t]"))))
  14514. (mapcar 'org-entry-restore-space values)))
  14515. (defun org-entry-protect-space (s)
  14516. "Protect spaces and newline in string S."
  14517. (while (string-match " " s)
  14518. (setq s (replace-match "%20" t t s)))
  14519. (while (string-match "\n" s)
  14520. (setq s (replace-match "%0A" t t s)))
  14521. s)
  14522. (defun org-entry-restore-space (s)
  14523. "Restore spaces and newline in string S."
  14524. (while (string-match "%20" s)
  14525. (setq s (replace-match " " t t s)))
  14526. (while (string-match "%0A" s)
  14527. (setq s (replace-match "\n" t t s)))
  14528. s)
  14529. (defvar org-entry-property-inherited-from (make-marker)
  14530. "Marker pointing to the entry from where a property was inherited.
  14531. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14532. location of the entry where the inheritance search matched. If there was
  14533. no match, the marker will point nowhere.
  14534. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14535. is set.")
  14536. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14537. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14538. The search will stop at the first ancestor which has the property defined.
  14539. If the value found is \"nil\", return nil to show that the property
  14540. should be considered as undefined (this is the meaning of nil here).
  14541. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14542. (move-marker org-entry-property-inherited-from nil)
  14543. (org-with-wide-buffer
  14544. (let (value)
  14545. (catch 'exit
  14546. (while t
  14547. (let ((v (org-property--local-values property literal-nil)))
  14548. (when v
  14549. (setq value
  14550. (concat (mapconcat #'identity (delq nil v) " ")
  14551. (and value " ")
  14552. value)))
  14553. (cond
  14554. ((car v)
  14555. (org-back-to-heading t)
  14556. (move-marker org-entry-property-inherited-from (point))
  14557. (throw 'exit nil))
  14558. ((org-up-heading-safe))
  14559. (t
  14560. (let ((global
  14561. (cdr (or (assoc-string property org-file-properties t)
  14562. (assoc-string property org-global-properties t)
  14563. (assoc-string property org-global-properties-fixed t)))))
  14564. (cond ((not global))
  14565. (value (setq value (concat global " " value)))
  14566. (t (setq value global))))
  14567. (throw 'exit nil))))))
  14568. (if literal-nil value (org-not-nil value)))))
  14569. (defvar org-property-changed-functions nil
  14570. "Hook called when the value of a property has changed.
  14571. Each hook function should accept two arguments, the name of the property
  14572. and the new value.")
  14573. (defun org-entry-put (pom property value)
  14574. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14575. If the value is nil, it is converted to the empty string. If it
  14576. is not a string, an error is raised. Also raise an error on
  14577. invalid property names.
  14578. PROPERTY can be any regular property (see
  14579. `org-special-properties'). It can also be \"TODO\",
  14580. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14581. For the last two properties, VALUE may have any of the special
  14582. values \"earlier\" and \"later\". The function then increases or
  14583. decreases scheduled or deadline date by one day."
  14584. (cond ((null value) (setq value ""))
  14585. ((not (stringp value)) (error "Properties values should be strings"))
  14586. ((not (org--valid-property-p property))
  14587. (user-error "Invalid property name: \"%s\"" property)))
  14588. (org-with-point-at pom
  14589. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14590. (org-back-to-heading t)
  14591. (org-with-limited-levels (org-back-to-heading t)))
  14592. (let ((beg (point)))
  14593. (cond
  14594. ((equal property "TODO")
  14595. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14596. ((not (member value org-todo-keywords-1))
  14597. (user-error "\"%s\" is not a valid TODO state" value)))
  14598. (org-todo value)
  14599. (org-set-tags nil 'align))
  14600. ((equal property "PRIORITY")
  14601. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14602. (org-set-tags nil 'align))
  14603. ((equal property "SCHEDULED")
  14604. (forward-line)
  14605. (if (and (org-looking-at-p org-planning-line-re)
  14606. (re-search-forward
  14607. org-scheduled-time-regexp (line-end-position) t))
  14608. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14609. ((string= value "later") (org-timestamp-change 1 'day))
  14610. ((string= value "") (org-schedule '(4)))
  14611. (t (org-schedule nil value)))
  14612. (if (member value '("earlier" "later" ""))
  14613. (call-interactively #'org-schedule)
  14614. (org-schedule nil value))))
  14615. ((equal property "DEADLINE")
  14616. (forward-line)
  14617. (if (and (org-looking-at-p org-planning-line-re)
  14618. (re-search-forward
  14619. org-deadline-time-regexp (line-end-position) t))
  14620. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14621. ((string= value "later") (org-timestamp-change 1 'day))
  14622. ((string= value "") (org-deadline '(4)))
  14623. (t (org-deadline nil value)))
  14624. (if (member value '("earlier" "later" ""))
  14625. (call-interactively #'org-deadline)
  14626. (org-deadline nil value))))
  14627. ((member property org-special-properties)
  14628. (error "The %s property cannot be set with `org-entry-put'" property))
  14629. (t
  14630. (let* ((range (org-get-property-block beg 'force))
  14631. (end (cdr range))
  14632. (case-fold-search t))
  14633. (goto-char (car range))
  14634. (if (re-search-forward (org-re-property property nil t) end t)
  14635. (progn (delete-region (match-beginning 0) (match-end 0))
  14636. (goto-char (match-beginning 0)))
  14637. (goto-char end)
  14638. (insert "\n")
  14639. (backward-char))
  14640. (insert ":" property ":")
  14641. (when value (insert " " value))
  14642. (org-indent-line)))))
  14643. (run-hook-with-args 'org-property-changed-functions property value)))
  14644. (defun org-buffer-property-keys
  14645. (&optional specials defaults columns ignore-malformed)
  14646. "Get all property keys in the current buffer.
  14647. When SPECIALS is non-nil, also list the special properties that
  14648. reflect things like tags and TODO state.
  14649. When DEFAULTS is non-nil, also include properties that has
  14650. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14651. DESCRIPTION, LOCATION, and LOGGING and others.
  14652. When COLUMNS in non-nil, also include property names given in
  14653. COLUMN formats in the current buffer.
  14654. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14655. automatically performed, such drawers will be silently ignored."
  14656. (let ((case-fold-search t)
  14657. (props (append
  14658. (and specials org-special-properties)
  14659. (and defaults (cons org-effort-property org-default-properties))
  14660. nil)))
  14661. (org-with-wide-buffer
  14662. (goto-char (point-min))
  14663. (while (re-search-forward org-property-start-re nil t)
  14664. (let ((range (org-get-property-block)))
  14665. (catch 'skip
  14666. (unless range
  14667. (when (and (not ignore-malformed)
  14668. (not (org-before-first-heading-p))
  14669. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14670. (line-beginning-position))))
  14671. (org-get-property-block nil t))
  14672. (throw 'skip nil))
  14673. (goto-char (car range))
  14674. (let ((begin (car range))
  14675. (end (cdr range)))
  14676. ;; Make sure that found property block is not located
  14677. ;; before current point, as it would generate an infloop.
  14678. ;; It can happen, for example, in the following
  14679. ;; situation:
  14680. ;;
  14681. ;; * Headline
  14682. ;; :PROPERTIES:
  14683. ;; ...
  14684. ;; :END:
  14685. ;; *************** Inlinetask
  14686. ;; #+BEGIN_EXAMPLE
  14687. ;; :PROPERTIES:
  14688. ;; #+END_EXAMPLE
  14689. ;;
  14690. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14691. (while (< (point) end)
  14692. (let ((p (progn (looking-at org-property-re)
  14693. (org-match-string-no-properties 2))))
  14694. ;; Only add true property name, not extension symbol.
  14695. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14696. (substring p 0 -1))
  14697. props))
  14698. (forward-line))))
  14699. (outline-next-heading)))
  14700. (when columns
  14701. (goto-char (point-min))
  14702. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14703. (let ((element (org-element-at-point)))
  14704. (when (memq (org-element-type element) '(keyword node-property))
  14705. (let ((value (org-element-property :value element))
  14706. (start 0))
  14707. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14708. (setq start (match-end 0))
  14709. (let ((p (org-match-string-no-properties 1 value)))
  14710. (unless (member-ignore-case p org-special-properties)
  14711. (push p props))))))))))
  14712. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14713. (defun org-property-values (key)
  14714. "List all non-nil values of property KEY in current buffer."
  14715. (org-with-wide-buffer
  14716. (goto-char (point-min))
  14717. (let ((case-fold-search t)
  14718. (re (org-re-property key))
  14719. values)
  14720. (while (re-search-forward re nil t)
  14721. (push (org-entry-get (point) key) values))
  14722. (delete-dups values))))
  14723. (defun org-insert-property-drawer ()
  14724. "Insert a property drawer into the current entry."
  14725. (org-with-wide-buffer
  14726. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14727. (org-back-to-heading t)
  14728. (org-with-limited-levels (org-back-to-heading t)))
  14729. (forward-line)
  14730. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14731. (unless (org-looking-at-p org-property-drawer-re)
  14732. ;; Make sure we start editing a line from current entry, not from
  14733. ;; next one. It prevents extending text properties or overlays
  14734. ;; belonging to the latter.
  14735. (when (bolp) (backward-char))
  14736. (let ((begin (1+ (point)))
  14737. (inhibit-read-only t))
  14738. (insert "\n:PROPERTIES:\n:END:")
  14739. (when (eobp) (insert "\n"))
  14740. (org-indent-region begin (point))))))
  14741. (defun org-insert-drawer (&optional arg drawer)
  14742. "Insert a drawer at point.
  14743. When optional argument ARG is non-nil, insert a property drawer.
  14744. Optional argument DRAWER, when non-nil, is a string representing
  14745. drawer's name. Otherwise, the user is prompted for a name.
  14746. If a region is active, insert the drawer around that region
  14747. instead.
  14748. Point is left between drawer's boundaries."
  14749. (interactive "P")
  14750. (let* ((drawer (if arg "PROPERTIES"
  14751. (or drawer (read-from-minibuffer "Drawer: ")))))
  14752. (cond
  14753. ;; With C-u, fall back on `org-insert-property-drawer'
  14754. (arg (org-insert-property-drawer))
  14755. ;; Check validity of suggested drawer's name.
  14756. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14757. (user-error "Invalid drawer name"))
  14758. ;; With an active region, insert a drawer at point.
  14759. ((not (org-region-active-p))
  14760. (progn
  14761. (unless (bolp) (insert "\n"))
  14762. (insert (format ":%s:\n\n:END:\n" drawer))
  14763. (forward-line -2)))
  14764. ;; Otherwise, insert the drawer at point
  14765. (t
  14766. (let ((rbeg (region-beginning))
  14767. (rend (copy-marker (region-end))))
  14768. (unwind-protect
  14769. (progn
  14770. (goto-char rbeg)
  14771. (beginning-of-line)
  14772. (when (save-excursion
  14773. (re-search-forward org-outline-regexp-bol rend t))
  14774. (user-error "Drawers cannot contain headlines"))
  14775. ;; Position point at the beginning of the first
  14776. ;; non-blank line in region. Insert drawer's opening
  14777. ;; there, then indent it.
  14778. (org-skip-whitespace)
  14779. (beginning-of-line)
  14780. (insert ":" drawer ":\n")
  14781. (forward-line -1)
  14782. (indent-for-tab-command)
  14783. ;; Move point to the beginning of the first blank line
  14784. ;; after the last non-blank line in region. Insert
  14785. ;; drawer's closing, then indent it.
  14786. (goto-char rend)
  14787. (skip-chars-backward " \r\t\n")
  14788. (insert "\n:END:")
  14789. (deactivate-mark t)
  14790. (indent-for-tab-command)
  14791. (unless (eolp) (insert "\n")))
  14792. ;; Clear marker, whatever the outcome of insertion is.
  14793. (set-marker rend nil)))))))
  14794. (defvar org-property-set-functions-alist nil
  14795. "Property set function alist.
  14796. Each entry should have the following format:
  14797. (PROPERTY . READ-FUNCTION)
  14798. The read function will be called with the same argument as
  14799. `org-completing-read'.")
  14800. (defun org-set-property-function (property)
  14801. "Get the function that should be used to set PROPERTY.
  14802. This is computed according to `org-property-set-functions-alist'."
  14803. (or (cdr (assoc property org-property-set-functions-alist))
  14804. 'org-completing-read))
  14805. (defun org-read-property-value (property)
  14806. "Read PROPERTY value from user."
  14807. (let* ((completion-ignore-case t)
  14808. (allowed (org-property-get-allowed-values nil property 'table))
  14809. (cur (org-entry-get nil property))
  14810. (prompt (concat property " value"
  14811. (if (and cur (string-match "\\S-" cur))
  14812. (concat " [" cur "]") "") ": "))
  14813. (set-function (org-set-property-function property))
  14814. (val (if allowed
  14815. (funcall set-function prompt allowed nil
  14816. (not (get-text-property 0 'org-unrestricted
  14817. (caar allowed))))
  14818. (funcall set-function prompt
  14819. (mapcar 'list (org-property-values property))
  14820. nil nil "" nil cur))))
  14821. (org-trim val)))
  14822. (defvar org-last-set-property nil)
  14823. (defvar org-last-set-property-value nil)
  14824. (defun org-read-property-name ()
  14825. "Read a property name."
  14826. (let ((completion-ignore-case t)
  14827. (default-prop (or (and (org-at-property-p)
  14828. (org-match-string-no-properties 2))
  14829. org-last-set-property)))
  14830. (org-completing-read
  14831. (concat "Property"
  14832. (if default-prop (concat " [" default-prop "]") "")
  14833. ": ")
  14834. (mapcar #'list (org-buffer-property-keys nil t t))
  14835. nil nil nil nil default-prop)))
  14836. (defun org-set-property-and-value (use-last)
  14837. "Allow to set [PROPERTY]: [value] direction from prompt.
  14838. When use-default, don't even ask, just use the last
  14839. \"[PROPERTY]: [value]\" string from the history."
  14840. (interactive "P")
  14841. (let* ((completion-ignore-case t)
  14842. (pv (or (and use-last org-last-set-property-value)
  14843. (org-completing-read
  14844. "Enter a \"[Property]: [value]\" pair: "
  14845. nil nil nil nil nil
  14846. org-last-set-property-value)))
  14847. prop val)
  14848. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14849. (setq prop (match-string 1 pv)
  14850. val (match-string 2 pv))
  14851. (org-set-property prop val))))
  14852. (defun org-set-property (property value)
  14853. "In the current entry, set PROPERTY to VALUE.
  14854. When called interactively, this will prompt for a property name, offering
  14855. completion on existing and default properties. And then it will prompt
  14856. for a value, offering completion either on allowed values (via an inherited
  14857. xxx_ALL property) or on existing values in other instances of this property
  14858. in the current file.
  14859. Throw an error when trying to set a property with an invalid name."
  14860. (interactive (list nil nil))
  14861. (let ((property (or property (org-read-property-name))))
  14862. ;; `org-entry-put' also makes the following check, but this one
  14863. ;; avoids polluting `org-last-set-property' and
  14864. ;; `org-last-set-property-value' needlessly.
  14865. (unless (org--valid-property-p property)
  14866. (user-error "Invalid property name: \"%s\"" property))
  14867. (let ((value (or value (org-read-property-value property)))
  14868. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14869. (setq org-last-set-property property)
  14870. (setq org-last-set-property-value (concat property ": " value))
  14871. ;; Possibly postprocess the inserted value:
  14872. (when fn (setq value (funcall fn value)))
  14873. (unless (equal (org-entry-get nil property) value)
  14874. (org-entry-put nil property value)))))
  14875. (defun org-find-property (property &optional value)
  14876. "Find first entry in buffer that sets PROPERTY.
  14877. When optional argument VALUE is non-nil, only consider an entry
  14878. if it contains PROPERTY set to this value. If PROPERTY should be
  14879. explicitly set to nil, use string \"nil\" for VALUE.
  14880. Return position where the entry begins, or nil if there is no
  14881. such entry. If narrowing is in effect, only search the visible
  14882. part of the buffer."
  14883. (save-excursion
  14884. (goto-char (point-min))
  14885. (let ((case-fold-search t)
  14886. (re (org-re-property property nil (not value) value)))
  14887. (catch 'exit
  14888. (while (re-search-forward re nil t)
  14889. (when (if value (org-at-property-p)
  14890. (org-entry-get (point) property nil t))
  14891. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14892. (defun org-delete-property (property)
  14893. "In the current entry, delete PROPERTY."
  14894. (interactive
  14895. (let* ((completion-ignore-case t)
  14896. (cat (org-entry-get (point) "CATEGORY"))
  14897. (props0 (org-entry-properties nil 'standard))
  14898. (props (if cat props0
  14899. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14900. (prop (if (< 1 (length props))
  14901. (completing-read "Property: " props nil t)
  14902. (caar props))))
  14903. (list prop)))
  14904. (if (not property)
  14905. (message "No property to delete in this entry")
  14906. (org-entry-delete nil property)
  14907. (message "Property \"%s\" deleted" property)))
  14908. (defun org-delete-property-globally (property)
  14909. "Remove PROPERTY globally, from all entries.
  14910. This function ignores narrowing, if any."
  14911. (interactive
  14912. (let* ((completion-ignore-case t)
  14913. (prop (completing-read
  14914. "Globally remove property: "
  14915. (mapcar #'list (org-buffer-property-keys)))))
  14916. (list prop)))
  14917. (org-with-wide-buffer
  14918. (goto-char (point-min))
  14919. (let ((count 0)
  14920. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14921. (while (re-search-forward re nil t)
  14922. (when (org-entry-delete (point) property) (cl-incf count)))
  14923. (message "Property \"%s\" removed from %d entries" property count))))
  14924. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14925. (defun org-compute-property-at-point ()
  14926. "Compute the property at point.
  14927. This looks for an enclosing column format, extracts the operator and
  14928. then applies it to the property in the column format's scope."
  14929. (interactive)
  14930. (unless (org-at-property-p)
  14931. (user-error "Not at a property"))
  14932. (let ((prop (org-match-string-no-properties 2)))
  14933. (org-columns-get-format-and-top-level)
  14934. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14935. (user-error "No operator defined for property %s" prop))
  14936. (org-columns-compute prop)))
  14937. (defvar org-property-allowed-value-functions nil
  14938. "Hook for functions supplying allowed values for a specific property.
  14939. The functions must take a single argument, the name of the property, and
  14940. return a flat list of allowed values. If \":ETC\" is one of
  14941. the values, this means that these values are intended as defaults for
  14942. completion, but that other values should be allowed too.
  14943. The functions must return nil if they are not responsible for this
  14944. property.")
  14945. (defun org-property-get-allowed-values (pom property &optional table)
  14946. "Get allowed values for the property PROPERTY.
  14947. When TABLE is non-nil, return an alist that can directly be used for
  14948. completion."
  14949. (let (vals)
  14950. (cond
  14951. ((equal property "TODO")
  14952. (setq vals (org-with-point-at pom
  14953. (append org-todo-keywords-1 '("")))))
  14954. ((equal property "PRIORITY")
  14955. (let ((n org-lowest-priority))
  14956. (while (>= n org-highest-priority)
  14957. (push (char-to-string n) vals)
  14958. (setq n (1- n)))))
  14959. ((equal property "CATEGORY"))
  14960. ((member property org-special-properties))
  14961. ((setq vals (run-hook-with-args-until-success
  14962. 'org-property-allowed-value-functions property)))
  14963. (t
  14964. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14965. (when (and vals (string-match "\\S-" vals))
  14966. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14967. (setq vals (mapcar (lambda (x)
  14968. (cond ((stringp x) x)
  14969. ((numberp x) (number-to-string x))
  14970. ((symbolp x) (symbol-name x))
  14971. (t "???")))
  14972. vals)))))
  14973. (when (member ":ETC" vals)
  14974. (setq vals (remove ":ETC" vals))
  14975. (org-add-props (car vals) '(org-unrestricted t)))
  14976. (if table (mapcar 'list vals) vals)))
  14977. (defun org-property-previous-allowed-value (&optional _previous)
  14978. "Switch to the next allowed value for this property."
  14979. (interactive)
  14980. (org-property-next-allowed-value t))
  14981. (defun org-property-next-allowed-value (&optional previous)
  14982. "Switch to the next allowed value for this property."
  14983. (interactive)
  14984. (unless (org-at-property-p)
  14985. (user-error "Not at a property"))
  14986. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14987. (key (match-string 2))
  14988. (value (match-string 3))
  14989. (allowed (or (org-property-get-allowed-values (point) key)
  14990. (and (member value '("[ ]" "[-]" "[X]"))
  14991. '("[ ]" "[X]"))))
  14992. (heading (save-match-data (nth 4 (org-heading-components))))
  14993. nval)
  14994. (unless allowed
  14995. (user-error "Allowed values for this property have not been defined"))
  14996. (when previous (setq allowed (reverse allowed)))
  14997. (when (member value allowed)
  14998. (setq nval (car (cdr (member value allowed)))))
  14999. (setq nval (or nval (car allowed)))
  15000. (when (equal nval value)
  15001. (user-error "Only one allowed value for this property"))
  15002. (org-at-property-p)
  15003. (replace-match (concat " :" key ": " nval) t t)
  15004. (org-indent-line)
  15005. (beginning-of-line 1)
  15006. (skip-chars-forward " \t")
  15007. (when (equal prop org-effort-property)
  15008. (org-refresh-property
  15009. '((effort . identity)
  15010. (effort-minutes . org-duration-string-to-minutes))
  15011. nval)
  15012. (when (string= org-clock-current-task heading)
  15013. (setq org-clock-effort nval)
  15014. (org-clock-update-mode-line)))
  15015. (run-hook-with-args 'org-property-changed-functions key nval)))
  15016. (defun org-find-olp (path &optional this-buffer)
  15017. "Return a marker pointing to the entry at outline path OLP.
  15018. If anything goes wrong, throw an error.
  15019. You can wrap this call to catch the error like this:
  15020. \(condition-case msg
  15021. \(org-mobile-locate-entry (match-string 4))
  15022. \(error (nth 1 msg)))
  15023. The return value will then be either a string with the error message,
  15024. or a marker if everything is OK.
  15025. If THIS-BUFFER is set, the outline path does not contain a file,
  15026. only headings."
  15027. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15028. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15029. (level 1)
  15030. (lmin 1)
  15031. (lmax 1)
  15032. end found flevel)
  15033. (unless buffer (error "File not found :%s" file))
  15034. (with-current-buffer buffer
  15035. (org-with-wide-buffer
  15036. (goto-char (point-min))
  15037. (dolist (heading path)
  15038. (let ((re (format org-complex-heading-regexp-format
  15039. (regexp-quote heading)))
  15040. (cnt 0))
  15041. (while (re-search-forward re end t)
  15042. (setq level (- (match-end 1) (match-beginning 1)))
  15043. (when (and (>= level lmin) (<= level lmax))
  15044. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15045. (when (= cnt 0)
  15046. (error "Heading not found on level %d: %s" lmax heading))
  15047. (when (> cnt 1)
  15048. (error "Heading not unique on level %d: %s" lmax heading))
  15049. (goto-char found)
  15050. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15051. (setq end (save-excursion (org-end-of-subtree t t)))))
  15052. (when (org-at-heading-p)
  15053. (point-marker))))))
  15054. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15055. "Find node HEADING in BUFFER.
  15056. Return a marker to the heading if it was found, or nil if not.
  15057. If POS-ONLY is set, return just the position instead of a marker.
  15058. The heading text must match exact, but it may have a TODO keyword,
  15059. a priority cookie and tags in the standard locations."
  15060. (with-current-buffer (or buffer (current-buffer))
  15061. (save-excursion
  15062. (save-restriction
  15063. (widen)
  15064. (goto-char (point-min))
  15065. (let (case-fold-search)
  15066. (when (re-search-forward
  15067. (format org-complex-heading-regexp-format
  15068. (regexp-quote heading)) nil t)
  15069. (if pos-only
  15070. (match-beginning 0)
  15071. (move-marker (make-marker) (match-beginning 0)))))))))
  15072. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15073. "Find Org node headline HEADING in all .org files in directory DIR.
  15074. When the target headline is found, return a marker to this location."
  15075. (let ((files (directory-files (or dir default-directory)
  15076. t "\\`[^.#].*\\.org\\'"))
  15077. visiting m buffer)
  15078. (catch 'found
  15079. (dolist (file files)
  15080. (message "trying %s" file)
  15081. (setq visiting (org-find-base-buffer-visiting file))
  15082. (setq buffer (or visiting (find-file-noselect file)))
  15083. (setq m (org-find-exact-headline-in-buffer
  15084. heading buffer))
  15085. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15086. (and m (throw 'found m))))))
  15087. (defun org-find-entry-with-id (ident)
  15088. "Locate the entry that contains the ID property with exact value IDENT.
  15089. IDENT can be a string, a symbol or a number, this function will search for
  15090. the string representation of it.
  15091. Return the position where this entry starts, or nil if there is no such entry."
  15092. (interactive "sID: ")
  15093. (let ((id (cond
  15094. ((stringp ident) ident)
  15095. ((symbolp ident) (symbol-name ident))
  15096. ((numberp ident) (number-to-string ident))
  15097. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15098. (org-with-wide-buffer (org-find-property "ID" id))))
  15099. ;;;; Timestamps
  15100. (defvar org-last-changed-timestamp nil)
  15101. (defvar org-last-inserted-timestamp nil
  15102. "The last time stamp inserted with `org-insert-time-stamp'.")
  15103. (defvar org-ts-what) ; dynamically scoped parameter
  15104. (defun org-time-stamp (arg &optional inactive)
  15105. "Prompt for a date/time and insert a time stamp.
  15106. If the user specifies a time like HH:MM or if this command is
  15107. called with at least one prefix argument, the time stamp contains
  15108. the date and the time. Otherwise, only the date is included.
  15109. All parts of a date not specified by the user are filled in from
  15110. the timestamp at point, if any, or the current date/time
  15111. otherwise.
  15112. If there is already a timestamp at the cursor, it is replaced.
  15113. With two universal prefix arguments, insert an active timestamp
  15114. with the current time without prompting the user.
  15115. When called from lisp, the timestamp is inactive if INACTIVE is
  15116. non-nil."
  15117. (interactive "P")
  15118. (let* ((ts (cond
  15119. ((org-at-date-range-p t)
  15120. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15121. ((org-at-timestamp-p t) (match-string 0))))
  15122. ;; Default time is either the timestamp at point or today.
  15123. ;; When entering a range, only the range start is considered.
  15124. (default-time (if (not ts) (current-time)
  15125. (apply #'encode-time (org-parse-time-string ts))))
  15126. (default-input (and ts (org-get-compact-tod ts)))
  15127. (repeater (and ts
  15128. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15129. (match-string 0 ts)))
  15130. org-time-was-given
  15131. org-end-time-was-given
  15132. (time
  15133. (and (if (equal arg '(16)) (current-time)
  15134. ;; Preserve `this-command' and `last-command'.
  15135. (let ((this-command this-command)
  15136. (last-command last-command))
  15137. (org-read-date
  15138. arg 'totime nil nil default-time default-input
  15139. inactive))))))
  15140. (cond
  15141. ((and ts
  15142. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15143. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15144. (insert "--")
  15145. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15146. (ts
  15147. ;; Make sure we're on a timestamp. When in the middle of a date
  15148. ;; range, move arbitrarily to range end.
  15149. (unless (org-at-timestamp-p t)
  15150. (skip-chars-forward "-")
  15151. (org-at-timestamp-p t))
  15152. (replace-match "")
  15153. (setq org-last-changed-timestamp
  15154. (org-insert-time-stamp
  15155. time (or org-time-was-given arg)
  15156. inactive nil nil (list org-end-time-was-given)))
  15157. (when repeater
  15158. (backward-char)
  15159. (insert " " repeater)
  15160. (setq org-last-changed-timestamp
  15161. (concat (substring org-last-inserted-timestamp 0 -1)
  15162. " " repeater ">")))
  15163. (message "Timestamp updated"))
  15164. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15165. (t (org-insert-time-stamp
  15166. time (or org-time-was-given arg) inactive nil nil
  15167. (list org-end-time-was-given))))))
  15168. ;; FIXME: can we use this for something else, like computing time differences?
  15169. (defun org-get-compact-tod (s)
  15170. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15171. (let* ((t1 (match-string 1 s))
  15172. (h1 (string-to-number (match-string 2 s)))
  15173. (m1 (string-to-number (match-string 3 s)))
  15174. (t2 (and (match-end 4) (match-string 5 s)))
  15175. (h2 (and t2 (string-to-number (match-string 6 s))))
  15176. (m2 (and t2 (string-to-number (match-string 7 s))))
  15177. dh dm)
  15178. (if (not t2)
  15179. t1
  15180. (setq dh (- h2 h1) dm (- m2 m1))
  15181. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15182. (concat t1 "+" (number-to-string dh)
  15183. (and (/= 0 dm) (format ":%02d" dm)))))))
  15184. (defun org-time-stamp-inactive (&optional arg)
  15185. "Insert an inactive time stamp.
  15186. An inactive time stamp is enclosed in square brackets instead of angle
  15187. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15188. does not link to the calendar and cannot be changed with the S-cursor keys.
  15189. So these are more for recording a certain time/date."
  15190. (interactive "P")
  15191. (org-time-stamp arg 'inactive))
  15192. (defvar org-date-ovl (make-overlay 1 1))
  15193. (overlay-put org-date-ovl 'face 'org-date-selected)
  15194. (org-detach-overlay org-date-ovl)
  15195. (defvar org-ans1) ; dynamically scoped parameter
  15196. (defvar org-ans2) ; dynamically scoped parameter
  15197. (defvar org-plain-time-of-day-regexp) ; defined below
  15198. (defvar org-overriding-default-time nil) ; dynamically scoped
  15199. (defvar org-read-date-overlay nil)
  15200. (defvar org-dcst nil) ; dynamically scoped
  15201. (defvar org-read-date-history nil)
  15202. (defvar org-read-date-final-answer nil)
  15203. (defvar org-read-date-analyze-futurep nil)
  15204. (defvar org-read-date-analyze-forced-year nil)
  15205. (defvar org-read-date-inactive)
  15206. (defvar org-read-date-minibuffer-local-map
  15207. (let* ((map (make-sparse-keymap)))
  15208. (set-keymap-parent map minibuffer-local-map)
  15209. (org-defkey map (kbd ".")
  15210. (lambda () (interactive)
  15211. ;; Are we at the beginning of the prompt?
  15212. (if (org-looking-back "^[^:]+: "
  15213. (let ((inhibit-field-text-motion t))
  15214. (line-beginning-position)))
  15215. (org-eval-in-calendar '(calendar-goto-today))
  15216. (insert "."))))
  15217. (org-defkey map (kbd "C-.")
  15218. (lambda () (interactive)
  15219. (org-eval-in-calendar '(calendar-goto-today))))
  15220. (org-defkey map [(meta shift left)]
  15221. (lambda () (interactive)
  15222. (org-eval-in-calendar '(calendar-backward-month 1))))
  15223. (org-defkey map [(meta shift right)]
  15224. (lambda () (interactive)
  15225. (org-eval-in-calendar '(calendar-forward-month 1))))
  15226. (org-defkey map [(meta shift up)]
  15227. (lambda () (interactive)
  15228. (org-eval-in-calendar '(calendar-backward-year 1))))
  15229. (org-defkey map [(meta shift down)]
  15230. (lambda () (interactive)
  15231. (org-eval-in-calendar '(calendar-forward-year 1))))
  15232. (org-defkey map [?\e (shift left)]
  15233. (lambda () (interactive)
  15234. (org-eval-in-calendar '(calendar-backward-month 1))))
  15235. (org-defkey map [?\e (shift right)]
  15236. (lambda () (interactive)
  15237. (org-eval-in-calendar '(calendar-forward-month 1))))
  15238. (org-defkey map [?\e (shift up)]
  15239. (lambda () (interactive)
  15240. (org-eval-in-calendar '(calendar-backward-year 1))))
  15241. (org-defkey map [?\e (shift down)]
  15242. (lambda () (interactive)
  15243. (org-eval-in-calendar '(calendar-forward-year 1))))
  15244. (org-defkey map [(shift up)]
  15245. (lambda () (interactive)
  15246. (org-eval-in-calendar '(calendar-backward-week 1))))
  15247. (org-defkey map [(shift down)]
  15248. (lambda () (interactive)
  15249. (org-eval-in-calendar '(calendar-forward-week 1))))
  15250. (org-defkey map [(shift left)]
  15251. (lambda () (interactive)
  15252. (org-eval-in-calendar '(calendar-backward-day 1))))
  15253. (org-defkey map [(shift right)]
  15254. (lambda () (interactive)
  15255. (org-eval-in-calendar '(calendar-forward-day 1))))
  15256. (org-defkey map "!"
  15257. (lambda () (interactive)
  15258. (org-eval-in-calendar '(diary-view-entries))
  15259. (message "")))
  15260. (org-defkey map ">"
  15261. (lambda () (interactive)
  15262. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15263. (org-defkey map "<"
  15264. (lambda () (interactive)
  15265. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15266. (org-defkey map "\C-v"
  15267. (lambda () (interactive)
  15268. (org-eval-in-calendar
  15269. '(calendar-scroll-left-three-months 1))))
  15270. (org-defkey map "\M-v"
  15271. (lambda () (interactive)
  15272. (org-eval-in-calendar
  15273. '(calendar-scroll-right-three-months 1))))
  15274. map)
  15275. "Keymap for minibuffer commands when using `org-read-date'.")
  15276. (defvar org-def)
  15277. (defvar org-defdecode)
  15278. (defvar org-with-time)
  15279. (defvar calendar-setup) ; Dynamically scoped.
  15280. (defun org-read-date (&optional with-time to-time from-string prompt
  15281. default-time default-input inactive)
  15282. "Read a date, possibly a time, and make things smooth for the user.
  15283. The prompt will suggest to enter an ISO date, but you can also enter anything
  15284. which will at least partially be understood by `parse-time-string'.
  15285. Unrecognized parts of the date will default to the current day, month, year,
  15286. hour and minute. If this command is called to replace a timestamp at point,
  15287. or to enter the second timestamp of a range, the default time is taken
  15288. from the existing stamp. Furthermore, the command prefers the future,
  15289. so if you are giving a date where the year is not given, and the day-month
  15290. combination is already past in the current year, it will assume you
  15291. mean next year. For details, see the manual. A few examples:
  15292. 3-2-5 --> 2003-02-05
  15293. feb 15 --> currentyear-02-15
  15294. 2/15 --> currentyear-02-15
  15295. sep 12 9 --> 2009-09-12
  15296. 12:45 --> today 12:45
  15297. 22 sept 0:34 --> currentyear-09-22 0:34
  15298. 12 --> currentyear-currentmonth-12
  15299. Fri --> nearest Friday after today
  15300. -Tue --> last Tuesday
  15301. etc.
  15302. Furthermore you can specify a relative date by giving, as the *first* thing
  15303. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15304. change in days weeks, months, years.
  15305. With a single plus or minus, the date is relative to today. With a double
  15306. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15307. +4d --> four days from today
  15308. +4 --> same as above
  15309. +2w --> two weeks from today
  15310. ++5 --> five days from default date
  15311. The function understands only English month and weekday abbreviations.
  15312. While prompting, a calendar is popped up - you can also select the
  15313. date with the mouse (button 1). The calendar shows a period of three
  15314. months. To scroll it to other months, use the keys `>' and `<'.
  15315. If you don't like the calendar, turn it off with
  15316. (setq org-read-date-popup-calendar nil)
  15317. With optional argument TO-TIME, the date will immediately be converted
  15318. to an internal time.
  15319. With an optional argument WITH-TIME, the prompt will suggest to
  15320. also insert a time. Note that when WITH-TIME is not set, you can
  15321. still enter a time, and this function will inform the calling routine
  15322. about this change. The calling routine may then choose to change the
  15323. format used to insert the time stamp into the buffer to include the time.
  15324. With optional argument FROM-STRING, read from this string instead from
  15325. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15326. the time/date that is used for everything that is not specified by the
  15327. user."
  15328. (require 'parse-time)
  15329. (let* ((org-with-time with-time)
  15330. (org-time-stamp-rounding-minutes
  15331. (if (equal org-with-time '(16))
  15332. '(0 0)
  15333. org-time-stamp-rounding-minutes))
  15334. (org-dcst org-display-custom-times)
  15335. (ct (org-current-time))
  15336. (org-def (or org-overriding-default-time default-time ct))
  15337. (org-defdecode (decode-time org-def))
  15338. (cur-frame (selected-frame))
  15339. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15340. (calendar-setup
  15341. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15342. (calendar-move-hook nil)
  15343. (calendar-view-diary-initially-flag nil)
  15344. (calendar-view-holidays-initially-flag nil)
  15345. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15346. ;; Rationalize `org-def' and `org-defdecode', if required.
  15347. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15348. (setf (nth 2 org-defdecode) -1)
  15349. (setf (nth 1 org-defdecode) 59)
  15350. (setq org-def (apply #'encode-time org-defdecode))
  15351. (setq org-defdecode (decode-time org-def)))
  15352. (let* ((timestr (format-time-string
  15353. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15354. org-def))
  15355. (prompt (concat (if prompt (concat prompt " ") "")
  15356. (format "Date+time [%s]: " timestr))))
  15357. (cond
  15358. (from-string (setq ans from-string))
  15359. (org-read-date-popup-calendar
  15360. (save-excursion
  15361. (save-window-excursion
  15362. (calendar)
  15363. (when (eq calendar-setup 'calendar-only)
  15364. (setq cal-frame
  15365. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15366. (select-frame cal-frame))
  15367. (org-eval-in-calendar '(setq cursor-type nil) t)
  15368. (unwind-protect
  15369. (progn
  15370. (calendar-forward-day (- (time-to-days org-def)
  15371. (calendar-absolute-from-gregorian
  15372. (calendar-current-date))))
  15373. (org-eval-in-calendar nil t)
  15374. (let* ((old-map (current-local-map))
  15375. (map (copy-keymap calendar-mode-map))
  15376. (minibuffer-local-map
  15377. (copy-keymap org-read-date-minibuffer-local-map)))
  15378. (org-defkey map (kbd "RET") 'org-calendar-select)
  15379. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15380. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15381. (unwind-protect
  15382. (progn
  15383. (use-local-map map)
  15384. (setq org-read-date-inactive inactive)
  15385. (add-hook 'post-command-hook 'org-read-date-display)
  15386. (setq org-ans0
  15387. (read-string prompt
  15388. default-input
  15389. 'org-read-date-history
  15390. nil))
  15391. ;; org-ans0: from prompt
  15392. ;; org-ans1: from mouse click
  15393. ;; org-ans2: from calendar motion
  15394. (setq ans
  15395. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15396. (remove-hook 'post-command-hook 'org-read-date-display)
  15397. (use-local-map old-map)
  15398. (when org-read-date-overlay
  15399. (delete-overlay org-read-date-overlay)
  15400. (setq org-read-date-overlay nil)))))
  15401. (bury-buffer "*Calendar*")
  15402. (when cal-frame
  15403. (delete-frame cal-frame)
  15404. (select-frame-set-input-focus cur-frame))))))
  15405. (t ; Naked prompt only
  15406. (unwind-protect
  15407. (setq ans (read-string prompt default-input
  15408. 'org-read-date-history timestr))
  15409. (when org-read-date-overlay
  15410. (delete-overlay org-read-date-overlay)
  15411. (setq org-read-date-overlay nil))))))
  15412. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15413. (when org-read-date-analyze-forced-year
  15414. (message "Year was forced into %s"
  15415. (if org-read-date-force-compatible-dates
  15416. "compatible range (1970-2037)"
  15417. "range representable on this machine"))
  15418. (ding))
  15419. ;; One round trip to get rid of 34th of August and stuff like that....
  15420. (setq final (decode-time (apply 'encode-time final)))
  15421. (setq org-read-date-final-answer ans)
  15422. (if to-time
  15423. (apply 'encode-time final)
  15424. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15425. (format "%04d-%02d-%02d %02d:%02d"
  15426. (nth 5 final) (nth 4 final) (nth 3 final)
  15427. (nth 2 final) (nth 1 final))
  15428. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15429. (defun org-read-date-display ()
  15430. "Display the current date prompt interpretation in the minibuffer."
  15431. (when org-read-date-display-live
  15432. (when org-read-date-overlay
  15433. (delete-overlay org-read-date-overlay))
  15434. (when (minibufferp (current-buffer))
  15435. (save-excursion
  15436. (end-of-line 1)
  15437. (while (not (equal (buffer-substring
  15438. (max (point-min) (- (point) 4)) (point))
  15439. " "))
  15440. (insert " ")))
  15441. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15442. " " (or org-ans1 org-ans2)))
  15443. (org-end-time-was-given nil)
  15444. (f (org-read-date-analyze ans org-def org-defdecode))
  15445. (fmts (if org-dcst
  15446. org-time-stamp-custom-formats
  15447. org-time-stamp-formats))
  15448. (fmt (if (or org-with-time
  15449. (and (boundp 'org-time-was-given) org-time-was-given))
  15450. (cdr fmts)
  15451. (car fmts)))
  15452. (txt (format-time-string fmt (apply 'encode-time f)))
  15453. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15454. (txt (concat "=> " txt)))
  15455. (when (and org-end-time-was-given
  15456. (string-match org-plain-time-of-day-regexp txt))
  15457. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15458. org-end-time-was-given
  15459. (substring txt (match-end 0)))))
  15460. (when org-read-date-analyze-futurep
  15461. (setq txt (concat txt " (=>F)")))
  15462. (setq org-read-date-overlay
  15463. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15464. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15465. (defun org-read-date-analyze (ans def defdecode)
  15466. "Analyze the combined answer of the date prompt."
  15467. ;; FIXME: cleanup and comment
  15468. ;; Pass `current-time' result to `decode-time' (instead of calling
  15469. ;; without arguments) so that only `current-time' has to be
  15470. ;; overriden in tests.
  15471. (let ((org-def def)
  15472. (org-defdecode defdecode)
  15473. (nowdecode (decode-time (current-time)))
  15474. delta deltan deltaw deltadef year month day
  15475. hour minute second wday pm h2 m2 tl wday1
  15476. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15477. (setq org-read-date-analyze-futurep nil
  15478. org-read-date-analyze-forced-year nil)
  15479. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15480. (setq ans "+0"))
  15481. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15482. (setq ans (replace-match "" t t ans)
  15483. deltan (car delta)
  15484. deltaw (nth 1 delta)
  15485. deltadef (nth 2 delta)))
  15486. ;; Check if there is an iso week date in there. If yes, store the
  15487. ;; info and postpone interpreting it until the rest of the parsing
  15488. ;; is done.
  15489. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15490. (setq iso-year (when (match-end 1)
  15491. (org-small-year-to-year
  15492. (string-to-number (match-string 1 ans))))
  15493. iso-weekday (when (match-end 3)
  15494. (string-to-number (match-string 3 ans)))
  15495. iso-week (string-to-number (match-string 2 ans)))
  15496. (setq ans (replace-match "" t t ans)))
  15497. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15498. (when (string-match
  15499. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15500. (setq year (if (match-end 2)
  15501. (string-to-number (match-string 2 ans))
  15502. (progn (setq kill-year t)
  15503. (string-to-number (format-time-string "%Y"))))
  15504. month (string-to-number (match-string 3 ans))
  15505. day (string-to-number (match-string 4 ans)))
  15506. (when (< year 100) (setq year (+ 2000 year)))
  15507. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15508. t nil ans)))
  15509. ;; Help matching dotted european dates
  15510. (when (string-match
  15511. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15512. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15513. (setq kill-year t)
  15514. (string-to-number (format-time-string "%Y")))
  15515. day (string-to-number (match-string 1 ans))
  15516. month (string-to-number (match-string 2 ans))
  15517. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15518. t nil ans)))
  15519. ;; Help matching american dates, like 5/30 or 5/30/7
  15520. (when (string-match
  15521. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15522. (setq year (if (match-end 4)
  15523. (string-to-number (match-string 4 ans))
  15524. (progn (setq kill-year t)
  15525. (string-to-number (format-time-string "%Y"))))
  15526. month (string-to-number (match-string 1 ans))
  15527. day (string-to-number (match-string 2 ans)))
  15528. (when (< year 100) (setq year (+ 2000 year)))
  15529. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15530. t nil ans)))
  15531. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15532. ;; If there is a time with am/pm, and *no* time without it, we convert
  15533. ;; so that matching will be successful.
  15534. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15535. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15536. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15537. (setq hour (string-to-number (match-string 1 ans))
  15538. minute (if (match-end 3)
  15539. (string-to-number (match-string 3 ans))
  15540. 0)
  15541. pm (equal ?p
  15542. (string-to-char (downcase (match-string 4 ans)))))
  15543. (if (and (= hour 12) (not pm))
  15544. (setq hour 0)
  15545. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15546. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15547. t t ans))))
  15548. ;; Check if a time range is given as a duration
  15549. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15550. (setq hour (string-to-number (match-string 1 ans))
  15551. h2 (+ hour (string-to-number (match-string 3 ans)))
  15552. minute (string-to-number (match-string 2 ans))
  15553. m2 (+ minute (if (match-end 5) (string-to-number
  15554. (match-string 5 ans))0)))
  15555. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15556. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15557. t t ans)))
  15558. ;; Check if there is a time range
  15559. (when (boundp 'org-end-time-was-given)
  15560. (setq org-time-was-given nil)
  15561. (when (and (string-match org-plain-time-of-day-regexp ans)
  15562. (match-end 8))
  15563. (setq org-end-time-was-given (match-string 8 ans))
  15564. (setq ans (concat (substring ans 0 (match-beginning 7))
  15565. (substring ans (match-end 7))))))
  15566. (setq tl (parse-time-string ans)
  15567. day (or (nth 3 tl) (nth 3 org-defdecode))
  15568. month
  15569. (cond ((nth 4 tl))
  15570. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15571. ;; Day was specified. Make sure DAY+MONTH
  15572. ;; combination happens in the future.
  15573. ((nth 3 tl)
  15574. (setq futurep t)
  15575. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15576. (nth 4 nowdecode)))
  15577. (t (nth 4 org-defdecode)))
  15578. year
  15579. (cond ((and (not kill-year) (nth 5 tl)))
  15580. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15581. ;; Month was guessed in the future and is at least
  15582. ;; equal to NOWDECODE's. Fix year accordingly.
  15583. (futurep
  15584. (if (or (> month (nth 4 nowdecode))
  15585. (>= day (nth 3 nowdecode)))
  15586. (nth 5 nowdecode)
  15587. (1+ (nth 5 nowdecode))))
  15588. ;; Month was specified. Make sure MONTH+YEAR
  15589. ;; combination happens in the future.
  15590. ((nth 4 tl)
  15591. (setq futurep t)
  15592. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15593. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15594. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15595. (t (nth 5 nowdecode))))
  15596. (t (nth 5 org-defdecode)))
  15597. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15598. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15599. second (or (nth 0 tl) 0)
  15600. wday (nth 6 tl))
  15601. (when (and (eq org-read-date-prefer-future 'time)
  15602. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15603. (equal day (nth 3 nowdecode))
  15604. (equal month (nth 4 nowdecode))
  15605. (equal year (nth 5 nowdecode))
  15606. (nth 2 tl)
  15607. (or (< (nth 2 tl) (nth 2 nowdecode))
  15608. (and (= (nth 2 tl) (nth 2 nowdecode))
  15609. (nth 1 tl)
  15610. (< (nth 1 tl) (nth 1 nowdecode)))))
  15611. (setq day (1+ day)
  15612. futurep t))
  15613. ;; Special date definitions below
  15614. (cond
  15615. (iso-week
  15616. ;; There was an iso week
  15617. (require 'cal-iso)
  15618. (setq futurep nil)
  15619. (setq year (or iso-year year)
  15620. day (or iso-weekday wday 1)
  15621. wday nil ; to make sure that the trigger below does not match
  15622. iso-date (calendar-gregorian-from-absolute
  15623. (calendar-iso-to-absolute
  15624. (list iso-week day year))))
  15625. ; FIXME: Should we also push ISO weeks into the future?
  15626. ; (when (and org-read-date-prefer-future
  15627. ; (not iso-year)
  15628. ; (< (calendar-absolute-from-gregorian iso-date)
  15629. ; (time-to-days (current-time))))
  15630. ; (setq year (1+ year)
  15631. ; iso-date (calendar-gregorian-from-absolute
  15632. ; (calendar-iso-to-absolute
  15633. ; (list iso-week day year)))))
  15634. (setq month (car iso-date)
  15635. year (nth 2 iso-date)
  15636. day (nth 1 iso-date)))
  15637. (deltan
  15638. (setq futurep nil)
  15639. (unless deltadef
  15640. ;; Pass `current-time' result to `decode-time' (instead of
  15641. ;; calling without arguments) so that only `current-time' has
  15642. ;; to be overriden in tests.
  15643. (let ((now (decode-time (current-time))))
  15644. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15645. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15646. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15647. ((equal deltaw "m") (setq month (+ month deltan)))
  15648. ((equal deltaw "y") (setq year (+ year deltan)))))
  15649. ((and wday (not (nth 3 tl)))
  15650. ;; Weekday was given, but no day, so pick that day in the week
  15651. ;; on or after the derived date.
  15652. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15653. (unless (equal wday wday1)
  15654. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15655. (when (and (boundp 'org-time-was-given)
  15656. (nth 2 tl))
  15657. (setq org-time-was-given t))
  15658. (when (< year 100) (setq year (+ 2000 year)))
  15659. ;; Check of the date is representable
  15660. (if org-read-date-force-compatible-dates
  15661. (progn
  15662. (when (< year 1970)
  15663. (setq year 1970 org-read-date-analyze-forced-year t))
  15664. (when (> year 2037)
  15665. (setq year 2037 org-read-date-analyze-forced-year t)))
  15666. (condition-case nil
  15667. (ignore (encode-time second minute hour day month year))
  15668. (error
  15669. (setq year (nth 5 org-defdecode))
  15670. (setq org-read-date-analyze-forced-year t))))
  15671. (setq org-read-date-analyze-futurep futurep)
  15672. (list second minute hour day month year)))
  15673. (defvar parse-time-weekdays)
  15674. (defun org-read-date-get-relative (s today default)
  15675. "Check string S for special relative date string.
  15676. TODAY and DEFAULT are internal times, for today and for a default.
  15677. Return shift list (N what def-flag)
  15678. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15679. N is the number of WHATs to shift.
  15680. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15681. the DEFAULT date rather than TODAY."
  15682. (require 'parse-time)
  15683. (when (and
  15684. (string-match
  15685. (concat
  15686. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15687. "\\([0-9]+\\)?"
  15688. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15689. "\\([ \t]\\|$\\)") s)
  15690. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15691. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15692. (string-to-char (substring (match-string 1 s) -1))
  15693. ?+))
  15694. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15695. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15696. (what (if (match-end 3) (match-string 3 s) "d"))
  15697. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15698. (date (if rel default today))
  15699. (wday (nth 6 (decode-time date)))
  15700. delta)
  15701. (if wday1
  15702. (progn
  15703. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15704. (when (= delta 0) (setq delta 7))
  15705. (when (= dir ?-)
  15706. (setq delta (- delta 7))
  15707. (when (= delta 0) (setq delta -7)))
  15708. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15709. (list delta "d" rel))
  15710. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15711. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15712. "Turn a user-specified date into the internal representation.
  15713. The internal representation needed by the calendar is (month day year).
  15714. This is a wrapper to handle the brain-dead convention in calendar that
  15715. user function argument order change dependent on argument order."
  15716. (if (boundp 'calendar-date-style)
  15717. (cond
  15718. ((eq calendar-date-style 'american)
  15719. (list arg1 arg2 arg3))
  15720. ((eq calendar-date-style 'european)
  15721. (list arg2 arg1 arg3))
  15722. ((eq calendar-date-style 'iso)
  15723. (list arg2 arg3 arg1)))
  15724. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15725. (if (org-bound-and-true-p european-calendar-style)
  15726. (list arg2 arg1 arg3)
  15727. (list arg1 arg2 arg3)))))
  15728. (defun org-eval-in-calendar (form &optional keepdate)
  15729. "Eval FORM in the calendar window and return to current window.
  15730. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15731. otherwise stick to the current value of `org-ans2'."
  15732. (let ((sf (selected-frame))
  15733. (sw (selected-window)))
  15734. (select-window (get-buffer-window "*Calendar*" t))
  15735. (eval form)
  15736. (when (and (not keepdate) (calendar-cursor-to-date))
  15737. (let* ((date (calendar-cursor-to-date))
  15738. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15739. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15740. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15741. (select-window sw)
  15742. (org-select-frame-set-input-focus sf)))
  15743. (defun org-calendar-select ()
  15744. "Return to `org-read-date' with the date currently selected.
  15745. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15746. (interactive)
  15747. (when (calendar-cursor-to-date)
  15748. (let* ((date (calendar-cursor-to-date))
  15749. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15750. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15751. (when (active-minibuffer-window) (exit-minibuffer))))
  15752. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15753. "Insert a date stamp for the date given by the internal TIME.
  15754. See `format-time-string' for the format of TIME.
  15755. WITH-HM means use the stamp format that includes the time of the day.
  15756. INACTIVE means use square brackets instead of angular ones, so that the
  15757. stamp will not contribute to the agenda.
  15758. PRE and POST are optional strings to be inserted before and after the
  15759. stamp.
  15760. The command returns the inserted time stamp."
  15761. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15762. stamp)
  15763. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15764. (insert-before-markers (or pre ""))
  15765. (when (listp extra)
  15766. (setq extra (car extra))
  15767. (if (and (stringp extra)
  15768. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15769. (setq extra (format "-%02d:%02d"
  15770. (string-to-number (match-string 1 extra))
  15771. (string-to-number (match-string 2 extra))))
  15772. (setq extra nil)))
  15773. (when extra
  15774. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15775. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15776. (insert-before-markers (or post ""))
  15777. (setq org-last-inserted-timestamp stamp)))
  15778. (defun org-toggle-time-stamp-overlays ()
  15779. "Toggle the use of custom time stamp formats."
  15780. (interactive)
  15781. (setq org-display-custom-times (not org-display-custom-times))
  15782. (unless org-display-custom-times
  15783. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15784. (while (setq p (next-single-property-change p 'display))
  15785. (when (and (get-text-property p 'display)
  15786. (eq (get-text-property p 'face) 'org-date))
  15787. (remove-text-properties
  15788. p (setq p (next-single-property-change p 'display))
  15789. '(display t))))
  15790. (set-buffer-modified-p bmp)))
  15791. (when (featurep 'xemacs)
  15792. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15793. (org-restart-font-lock)
  15794. (setq org-table-may-need-update t)
  15795. (if org-display-custom-times
  15796. (message "Time stamps are overlaid with custom format")
  15797. (message "Time stamp overlays removed")))
  15798. (defun org-display-custom-time (beg end)
  15799. "Overlay modified time stamp format over timestamp between BEG and END."
  15800. (let* ((ts (buffer-substring beg end))
  15801. t1 w1 with-hm tf time str w2 (off 0))
  15802. (save-match-data
  15803. (setq t1 (org-parse-time-string ts t))
  15804. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15805. (setq off (- (match-end 0) (match-beginning 0)))))
  15806. (setq end (- end off))
  15807. (setq w1 (- end beg)
  15808. with-hm (and (nth 1 t1) (nth 2 t1))
  15809. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15810. time (org-fix-decoded-time t1)
  15811. str (org-add-props
  15812. (format-time-string
  15813. (substring tf 1 -1) (apply 'encode-time time))
  15814. nil 'mouse-face 'highlight)
  15815. w2 (length str))
  15816. (unless (= w2 w1)
  15817. (add-text-properties (1+ beg) (+ 2 beg)
  15818. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15819. (if (featurep 'xemacs)
  15820. (progn
  15821. (put-text-property beg end 'invisible t)
  15822. (put-text-property beg end 'end-glyph (make-glyph str)))
  15823. (put-text-property beg end 'display str))))
  15824. (defun org-fix-decoded-time (time)
  15825. "Set 0 instead of nil for the first 6 elements of time.
  15826. Don't touch the rest."
  15827. (let ((n 0))
  15828. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15829. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15830. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15831. "Difference between TIMESTAMP-STRING and now in days.
  15832. If SECONDS is non-nil, return the difference in seconds."
  15833. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15834. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15835. (funcall fdiff (current-time)))))
  15836. (defun org-deadline-close (timestamp-string &optional ndays)
  15837. "Is the time in TIMESTAMP-STRING close to the current date?"
  15838. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15839. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15840. (not (org-entry-is-done-p))))
  15841. (defun org-get-wdays (ts &optional delay zero-delay)
  15842. "Get the deadline lead time appropriate for timestring TS.
  15843. When DELAY is non-nil, get the delay time for scheduled items
  15844. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15845. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15846. don't try to find the delay cookie in the scheduled timestamp."
  15847. (let ((tv (if delay org-scheduled-delay-days
  15848. org-deadline-warning-days)))
  15849. (cond
  15850. ((or (and delay (< tv 0))
  15851. (and delay zero-delay (<= tv 0))
  15852. (and (not delay) (<= tv 0)))
  15853. ;; Enforce this value no matter what
  15854. (- tv))
  15855. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15856. ;; lead time is specified.
  15857. (floor (* (string-to-number (match-string 1 ts))
  15858. (cdr (assoc (match-string 2 ts)
  15859. '(("d" . 1) ("w" . 7)
  15860. ("m" . 30.4) ("y" . 365.25)
  15861. ("h" . 0.041667)))))))
  15862. ;; go for the default.
  15863. (t tv))))
  15864. (defun org-calendar-select-mouse (ev)
  15865. "Return to `org-read-date' with the date currently selected.
  15866. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15867. (interactive "e")
  15868. (mouse-set-point ev)
  15869. (when (calendar-cursor-to-date)
  15870. (let* ((date (calendar-cursor-to-date))
  15871. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15872. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15873. (when (active-minibuffer-window) (exit-minibuffer))))
  15874. (defun org-check-deadlines (ndays)
  15875. "Check if there are any deadlines due or past due.
  15876. A deadline is considered due if it happens within `org-deadline-warning-days'
  15877. days from today's date. If the deadline appears in an entry marked DONE,
  15878. it is not shown. The prefix arg NDAYS can be used to test that many
  15879. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15880. (interactive "P")
  15881. (let* ((org-warn-days
  15882. (cond
  15883. ((equal ndays '(4)) 100000)
  15884. (ndays (prefix-numeric-value ndays))
  15885. (t (abs org-deadline-warning-days))))
  15886. (case-fold-search nil)
  15887. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15888. (callback
  15889. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15890. (message "%d deadlines past-due or due within %d days"
  15891. (org-occur regexp nil callback)
  15892. org-warn-days)))
  15893. (defsubst org-re-timestamp (type)
  15894. "Return a regexp for timestamp TYPE.
  15895. Allowed values for TYPE are:
  15896. all: all timestamps
  15897. active: only active timestamps (<...>)
  15898. inactive: only inactive timestamps ([...])
  15899. scheduled: only scheduled timestamps
  15900. deadline: only deadline timestamps
  15901. closed: only closed time-stamps
  15902. When TYPE is nil, fall back on returning a regexp that matches
  15903. both scheduled and deadline timestamps."
  15904. (cl-case type
  15905. (all org-ts-regexp-both)
  15906. (active org-ts-regexp)
  15907. (inactive org-ts-regexp-inactive)
  15908. (scheduled org-scheduled-time-regexp)
  15909. (deadline org-deadline-time-regexp)
  15910. (closed org-closed-time-regexp)
  15911. (otherwise
  15912. (concat "\\<"
  15913. (regexp-opt (list org-deadline-string org-scheduled-string))
  15914. " *<\\([^>]+\\)>"))))
  15915. (defun org-check-before-date (d)
  15916. "Check if there are deadlines or scheduled entries before date D."
  15917. (interactive (list (org-read-date)))
  15918. (let ((case-fold-search nil)
  15919. (regexp (org-re-timestamp org-ts-type))
  15920. (callback
  15921. `(lambda ()
  15922. (let ((match (match-string 1)))
  15923. (and ,(if (memq org-ts-type '(active inactive all))
  15924. '(eq (org-element-type (org-element-context)) 'timestamp)
  15925. '(org-at-planning-p))
  15926. (time-less-p
  15927. (org-time-string-to-time match)
  15928. (org-time-string-to-time d)))))))
  15929. (message "%d entries before %s"
  15930. (org-occur regexp nil callback)
  15931. d)))
  15932. (defun org-check-after-date (d)
  15933. "Check if there are deadlines or scheduled entries after date D."
  15934. (interactive (list (org-read-date)))
  15935. (let ((case-fold-search nil)
  15936. (regexp (org-re-timestamp org-ts-type))
  15937. (callback
  15938. `(lambda ()
  15939. (let ((match (match-string 1)))
  15940. (and ,(if (memq org-ts-type '(active inactive all))
  15941. '(eq (org-element-type (org-element-context)) 'timestamp)
  15942. '(org-at-planning-p))
  15943. (not (time-less-p
  15944. (org-time-string-to-time match)
  15945. (org-time-string-to-time d))))))))
  15946. (message "%d entries after %s"
  15947. (org-occur regexp nil callback)
  15948. d)))
  15949. (defun org-check-dates-range (start-date end-date)
  15950. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15951. (interactive (list (org-read-date nil nil nil "Range starts")
  15952. (org-read-date nil nil nil "Range end")))
  15953. (let ((case-fold-search nil)
  15954. (regexp (org-re-timestamp org-ts-type))
  15955. (callback
  15956. `(lambda ()
  15957. (let ((match (match-string 1)))
  15958. (and
  15959. ,(if (memq org-ts-type '(active inactive all))
  15960. '(eq (org-element-type (org-element-context)) 'timestamp)
  15961. '(org-at-planning-p))
  15962. (not (time-less-p
  15963. (org-time-string-to-time match)
  15964. (org-time-string-to-time start-date)))
  15965. (time-less-p
  15966. (org-time-string-to-time match)
  15967. (org-time-string-to-time end-date)))))))
  15968. (message "%d entries between %s and %s"
  15969. (org-occur regexp nil callback) start-date end-date)))
  15970. (defun org-evaluate-time-range (&optional to-buffer)
  15971. "Evaluate a time range by computing the difference between start and end.
  15972. Normally the result is just printed in the echo area, but with prefix arg
  15973. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15974. If the time range is actually in a table, the result is inserted into the
  15975. next column.
  15976. For time difference computation, a year is assumed to be exactly 365
  15977. days in order to avoid rounding problems."
  15978. (interactive "P")
  15979. (or
  15980. (org-clock-update-time-maybe)
  15981. (save-excursion
  15982. (unless (org-at-date-range-p t)
  15983. (goto-char (point-at-bol))
  15984. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15985. (unless (org-at-date-range-p t)
  15986. (user-error "Not at a time-stamp range, and none found in current line")))
  15987. (let* ((ts1 (match-string 1))
  15988. (ts2 (match-string 2))
  15989. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15990. (match-end (match-end 0))
  15991. (time1 (org-time-string-to-time ts1))
  15992. (time2 (org-time-string-to-time ts2))
  15993. (t1 (org-float-time time1))
  15994. (t2 (org-float-time time2))
  15995. (diff (abs (- t2 t1)))
  15996. (negative (< (- t2 t1) 0))
  15997. ;; (ys (floor (* 365 24 60 60)))
  15998. (ds (* 24 60 60))
  15999. (hs (* 60 60))
  16000. (fy "%dy %dd %02d:%02d")
  16001. (fy1 "%dy %dd")
  16002. (fd "%dd %02d:%02d")
  16003. (fd1 "%dd")
  16004. (fh "%02d:%02d")
  16005. y d h m align)
  16006. (if havetime
  16007. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16008. y 0
  16009. d (floor (/ diff ds)) diff (mod diff ds)
  16010. h (floor (/ diff hs)) diff (mod diff hs)
  16011. m (floor (/ diff 60)))
  16012. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16013. y 0
  16014. d (floor (+ (/ diff ds) 0.5))
  16015. h 0 m 0))
  16016. (if (not to-buffer)
  16017. (message "%s" (org-make-tdiff-string y d h m))
  16018. (if (org-at-table-p)
  16019. (progn
  16020. (goto-char match-end)
  16021. (setq align t)
  16022. (and (looking-at " *|") (goto-char (match-end 0))))
  16023. (goto-char match-end))
  16024. (when (looking-at
  16025. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16026. (replace-match ""))
  16027. (when negative (insert " -"))
  16028. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16029. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16030. (insert " " (format fh h m))))
  16031. (when align (org-table-align))
  16032. (message "Time difference inserted")))))
  16033. (defun org-make-tdiff-string (y d h m)
  16034. (let ((fmt "")
  16035. (l nil))
  16036. (when (> y 0)
  16037. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16038. (push y l))
  16039. (when (> d 0)
  16040. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16041. (push d l))
  16042. (when (> h 0)
  16043. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16044. (push h l))
  16045. (when (> m 0)
  16046. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16047. (push m l))
  16048. (apply 'format fmt (nreverse l))))
  16049. (defun org-time-string-to-time (s &optional buffer pos)
  16050. "Convert a timestamp string into internal time."
  16051. (condition-case errdata
  16052. (apply 'encode-time (org-parse-time-string s))
  16053. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16054. s (if (not (and buffer pos))
  16055. ""
  16056. (format-message " at %d in buffer `%s'" pos buffer))
  16057. (cdr errdata)))))
  16058. (defun org-time-string-to-seconds (s)
  16059. "Convert a timestamp string to a number of seconds."
  16060. (org-float-time (org-time-string-to-time s)))
  16061. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16062. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16063. "Convert time stamp S to an absolute day number.
  16064. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16065. stamp, get the closest date to DAYNR. If PREFER is
  16066. `past' (respectively `future') return a date past (respectively
  16067. after) or equal to DAYNR.
  16068. POS is the location of time stamp S, as a buffer position in
  16069. BUFFER.
  16070. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16071. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16072. signalled."
  16073. (cond
  16074. ((string-match "\\`%%\\((.*)\\)" s)
  16075. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16076. ;; only able to match individual dates. If it fails, raise an
  16077. ;; error.
  16078. (if (and daynr
  16079. (org-diary-sexp-entry
  16080. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16081. daynr
  16082. (signal 'org-diary-sexp-no-match (list s))))
  16083. ((and daynr show-all) (org-closest-date s daynr prefer))
  16084. (t (time-to-days
  16085. (condition-case errdata
  16086. (apply #'encode-time (org-parse-time-string s))
  16087. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16088. s
  16089. (if (not (and buffer pos)) ""
  16090. (format-message " at %d in buffer `%s'" pos buffer))
  16091. (cdr errdata))))))))
  16092. (defun org-days-to-iso-week (days)
  16093. "Return the iso week number."
  16094. (require 'cal-iso)
  16095. (car (calendar-iso-from-absolute days)))
  16096. (defun org-small-year-to-year (year)
  16097. "Convert 2-digit years into 4-digit years.
  16098. YEAR is expanded into one of the 30 next years, if possible, or
  16099. into a past one. Any year larger than 99 is returned unchanged."
  16100. (if (>= year 100) year
  16101. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16102. (century (/ current 100))
  16103. (offset (- year (% current 100))))
  16104. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16105. ((> offset -70) (+ (* century 100) year))
  16106. (t (+ (* (1+ century) 100) year))))))
  16107. (defun org-time-from-absolute (d)
  16108. "Return the time corresponding to date D.
  16109. D may be an absolute day number, or a calendar-type list (month day year)."
  16110. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16111. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16112. (defun org-calendar-holiday ()
  16113. "List of holidays, for Diary display in Org mode."
  16114. (declare (special date))
  16115. (require 'holidays)
  16116. (let ((hl (calendar-check-holidays date)))
  16117. (and hl (mapconcat #'identity hl "; "))))
  16118. (defun org-diary-sexp-entry (sexp entry d)
  16119. "Process a SEXP diary ENTRY for date D."
  16120. (require 'diary-lib)
  16121. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16122. ;; dynamically.
  16123. (let* ((sexp `(let ((entry ,entry)
  16124. (date ',d))
  16125. ,(car (read-from-string sexp))))
  16126. (result (if calendar-debug-sexp (eval sexp)
  16127. (condition-case nil
  16128. (eval sexp)
  16129. (error
  16130. (beep)
  16131. (message "Bad sexp at line %d in %s: %s"
  16132. (org-current-line)
  16133. (buffer-file-name) sexp)
  16134. (sleep-for 2))))))
  16135. (cond ((stringp result) (split-string result "; "))
  16136. ((and (consp result)
  16137. (not (consp (cdr result)))
  16138. (stringp (cdr result))) (cdr result))
  16139. ((and (consp result)
  16140. (stringp (car result))) result)
  16141. (result entry))))
  16142. (defun org-diary-to-ical-string (frombuf)
  16143. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16144. This uses the icalendar.el library."
  16145. (let* ((tmpdir (if (featurep 'xemacs)
  16146. (temp-directory)
  16147. temporary-file-directory))
  16148. (tmpfile (make-temp-name
  16149. (expand-file-name "orgics" tmpdir)))
  16150. buf rtn b e)
  16151. (with-current-buffer frombuf
  16152. (icalendar-export-region (point-min) (point-max) tmpfile)
  16153. (setq buf (find-buffer-visiting tmpfile))
  16154. (set-buffer buf)
  16155. (goto-char (point-min))
  16156. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16157. (setq b (match-beginning 0)))
  16158. (goto-char (point-max))
  16159. (when (re-search-backward "^END:VEVENT" nil t)
  16160. (setq e (match-end 0)))
  16161. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16162. (kill-buffer buf)
  16163. (delete-file tmpfile)
  16164. rtn))
  16165. (defun org-closest-date (start current prefer)
  16166. "Return closest date to CURRENT starting from START.
  16167. CURRENT and START are both time stamps.
  16168. When PREFER is `past', return a date that is either CURRENT or
  16169. past. When PREFER is `future', return a date that is either
  16170. CURRENT or future.
  16171. Only time stamps with a repeater are modified. Any other time
  16172. stamp stay unchanged. In any case, return value is an absolute
  16173. day number."
  16174. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16175. ;; No repeater. Do not shift time stamp.
  16176. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16177. (let ((value (string-to-number (match-string 1 start)))
  16178. (type (match-string 2 start)))
  16179. (if (= 0 value)
  16180. ;; Repeater with a 0-value is considered as void.
  16181. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16182. (let* ((base (org-date-to-gregorian start))
  16183. (target (org-date-to-gregorian current))
  16184. (sday (calendar-absolute-from-gregorian base))
  16185. (cday (calendar-absolute-from-gregorian target))
  16186. n1 n2)
  16187. ;; If START is already past CURRENT, just return START.
  16188. (if (<= cday sday) sday
  16189. ;; Compute closest date before (N1) and closest date past
  16190. ;; (N2) CURRENT.
  16191. (pcase type
  16192. ("h"
  16193. (let ((missing-hours
  16194. (mod (+ (- (* 24 (- cday sday))
  16195. (nth 2 (org-parse-time-string start)))
  16196. org-extend-today-until)
  16197. value)))
  16198. (setf n1 (if (= missing-hours 0) cday
  16199. (- cday (1+ (/ missing-hours 24)))))
  16200. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16201. ((or "d" "w")
  16202. (let ((value (if (equal type "w") (* 7 value) value)))
  16203. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16204. (setf n2 (+ n1 value))))
  16205. ("m"
  16206. (let* ((add-months
  16207. (lambda (d n)
  16208. ;; Add N months to gregorian date D, i.e.,
  16209. ;; a list (MONTH DAY YEAR). Return a valid
  16210. ;; gregorian date.
  16211. (let ((m (+ (nth 0 d) n)))
  16212. (list (mod m 12)
  16213. (nth 1 d)
  16214. (+ (/ m 12) (nth 2 d))))))
  16215. (months ; Complete months to TARGET.
  16216. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16217. (- (nth 0 target) (nth 0 base))
  16218. ;; If START's day is greater than
  16219. ;; TARGET's, remove incomplete month.
  16220. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16221. value)
  16222. value))
  16223. (before (funcall add-months base months)))
  16224. (setf n1 (calendar-absolute-from-gregorian before))
  16225. (setf n2
  16226. (calendar-absolute-from-gregorian
  16227. (funcall add-months before value)))))
  16228. (_
  16229. (let* ((d (nth 1 base))
  16230. (m (nth 0 base))
  16231. (y (nth 2 base))
  16232. (years ; Complete years to TARGET.
  16233. (* (/ (- (nth 2 target)
  16234. y
  16235. ;; If START's month and day are
  16236. ;; greater than TARGET's, remove
  16237. ;; incomplete year.
  16238. (if (or (> (nth 0 target) m)
  16239. (and (= (nth 0 target) m)
  16240. (> (nth 1 target) d)))
  16241. 0
  16242. 1))
  16243. value)
  16244. value))
  16245. (before (list m d (+ y years))))
  16246. (setf n1 (calendar-absolute-from-gregorian before))
  16247. (setf n2 (calendar-absolute-from-gregorian
  16248. (list m d (+ (nth 2 before) value)))))))
  16249. ;; Handle PREFER parameter, if any.
  16250. (cond
  16251. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16252. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16253. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16254. (defun org-date-to-gregorian (d)
  16255. "Turn any specification of date D into a Gregorian date for the calendar."
  16256. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16257. ((and (listp d) (= (length d) 3)) d)
  16258. ((stringp d)
  16259. (let ((d (org-parse-time-string d)))
  16260. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16261. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16262. (defun org-parse-time-string (s &optional nodefault)
  16263. "Parse the standard Org-mode time string.
  16264. This should be a lot faster than the normal `parse-time-string'.
  16265. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16266. hour and minute fields will be nil if not given."
  16267. (cond ((string-match org-ts-regexp0 s)
  16268. (list 0
  16269. (when (or (match-beginning 8) (not nodefault))
  16270. (string-to-number (or (match-string 8 s) "0")))
  16271. (when (or (match-beginning 7) (not nodefault))
  16272. (string-to-number (or (match-string 7 s) "0")))
  16273. (string-to-number (match-string 4 s))
  16274. (string-to-number (match-string 3 s))
  16275. (string-to-number (match-string 2 s))
  16276. nil nil nil))
  16277. ((string-match "^<[^>]+>$" s)
  16278. (decode-time (seconds-to-time (org-matcher-time s))))
  16279. (t (error "Not a standard Org-mode time string: %s" s))))
  16280. (defun org-timestamp-up (&optional arg)
  16281. "Increase the date item at the cursor by one.
  16282. If the cursor is on the year, change the year. If it is on the month,
  16283. the day or the time, change that.
  16284. With prefix ARG, change by that many units."
  16285. (interactive "p")
  16286. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16287. (defun org-timestamp-down (&optional arg)
  16288. "Decrease the date item at the cursor by one.
  16289. If the cursor is on the year, change the year. If it is on the month,
  16290. the day or the time, change that.
  16291. With prefix ARG, change by that many units."
  16292. (interactive "p")
  16293. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16294. (defun org-timestamp-up-day (&optional arg)
  16295. "Increase the date in the time stamp by one day.
  16296. With prefix ARG, change that many days."
  16297. (interactive "p")
  16298. (if (and (not (org-at-timestamp-p t))
  16299. (org-at-heading-p))
  16300. (org-todo 'up)
  16301. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16302. (defun org-timestamp-down-day (&optional arg)
  16303. "Decrease the date in the time stamp by one day.
  16304. With prefix ARG, change that many days."
  16305. (interactive "p")
  16306. (if (and (not (org-at-timestamp-p t))
  16307. (org-at-heading-p))
  16308. (org-todo 'down)
  16309. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16310. (defun org-at-timestamp-p (&optional inactive-ok)
  16311. "Non-nil if point is inside a timestamp.
  16312. When optional argument INACTIVE-OK is non-nil, also consider
  16313. inactive timestamps.
  16314. When this function returns a non-nil value, match data is set
  16315. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16316. INACTIVE-OK."
  16317. (interactive)
  16318. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16319. (pos (point))
  16320. (ans (or (looking-at tsr)
  16321. (save-excursion
  16322. (skip-chars-backward "^[<\n\r\t")
  16323. (when (> (point) (point-min)) (backward-char 1))
  16324. (and (looking-at tsr)
  16325. (> (- (match-end 0) pos) -1))))))
  16326. (and ans
  16327. (boundp 'org-ts-what)
  16328. (setq org-ts-what
  16329. (cond
  16330. ((= pos (match-beginning 0)) 'bracket)
  16331. ;; Point is considered to be "on the bracket" whether
  16332. ;; it's really on it or right after it.
  16333. ((= pos (1- (match-end 0))) 'bracket)
  16334. ((= pos (match-end 0)) 'after)
  16335. ((org-pos-in-match-range pos 2) 'year)
  16336. ((org-pos-in-match-range pos 3) 'month)
  16337. ((org-pos-in-match-range pos 7) 'hour)
  16338. ((org-pos-in-match-range pos 8) 'minute)
  16339. ((or (org-pos-in-match-range pos 4)
  16340. (org-pos-in-match-range pos 5)) 'day)
  16341. ((and (> pos (or (match-end 8) (match-end 5)))
  16342. (< pos (match-end 0)))
  16343. (- pos (or (match-end 8) (match-end 5))))
  16344. (t 'day))))
  16345. ans))
  16346. (defun org-toggle-timestamp-type ()
  16347. "Toggle the type (<active> or [inactive]) of a time stamp."
  16348. (interactive)
  16349. (when (org-at-timestamp-p t)
  16350. (let ((beg (match-beginning 0)) (end (match-end 0))
  16351. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16352. (save-excursion
  16353. (goto-char beg)
  16354. (while (re-search-forward "[][<>]" end t)
  16355. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16356. t t)))
  16357. (message "Timestamp is now %sactive"
  16358. (if (equal (char-after beg) ?<) "" "in")))))
  16359. (defun org-at-clock-log-p nil
  16360. "Is the cursor on the clock log line?"
  16361. (save-excursion
  16362. (move-beginning-of-line 1)
  16363. (looking-at org-clock-line-re)))
  16364. (defvar org-clock-history) ; defined in org-clock.el
  16365. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16366. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16367. "Change the date in the time stamp at point.
  16368. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16369. `year', `minute', `second'. If WHAT is not given, the cursor position
  16370. in the timestamp determines what will be changed.
  16371. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16372. (let ((origin (point)) origin-cat
  16373. with-hm inactive
  16374. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16375. org-ts-what
  16376. extra rem
  16377. ts time time0 fixnext clrgx)
  16378. (unless (org-at-timestamp-p t)
  16379. (user-error "Not at a timestamp"))
  16380. (if (and (not what) (eq org-ts-what 'bracket))
  16381. (org-toggle-timestamp-type)
  16382. ;; Point isn't on brackets. Remember the part of the time-stamp
  16383. ;; the point was in. Indeed, size of time-stamps may change,
  16384. ;; but point must be kept in the same category nonetheless.
  16385. (setq origin-cat org-ts-what)
  16386. (when (and (not what) (not (eq org-ts-what 'day))
  16387. org-display-custom-times
  16388. (get-text-property (point) 'display)
  16389. (not (get-text-property (1- (point)) 'display)))
  16390. (setq org-ts-what 'day))
  16391. (setq org-ts-what (or what org-ts-what)
  16392. inactive (= (char-after (match-beginning 0)) ?\[)
  16393. ts (match-string 0))
  16394. (replace-match "")
  16395. (when (string-match
  16396. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16397. ts)
  16398. (setq extra (match-string 1 ts))
  16399. (when suppress-tmp-delay
  16400. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16401. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16402. (setq with-hm t))
  16403. (setq time0 (org-parse-time-string ts))
  16404. (when (and updown
  16405. (eq org-ts-what 'minute)
  16406. (not current-prefix-arg))
  16407. ;; This looks like s-up and s-down. Change by one rounding step.
  16408. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16409. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16410. (setcar (cdr time0) (+ (nth 1 time0)
  16411. (if (> n 0) (- rem) (- dm rem))))))
  16412. (setq time
  16413. (encode-time (or (car time0) 0)
  16414. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16415. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16416. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16417. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16418. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16419. (nthcdr 6 time0)))
  16420. (when (and (member org-ts-what '(hour minute))
  16421. extra
  16422. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16423. (setq extra (org-modify-ts-extra
  16424. extra
  16425. (if (eq org-ts-what 'hour) 2 5)
  16426. n dm)))
  16427. (when (integerp org-ts-what)
  16428. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16429. (when (eq what 'calendar)
  16430. (let ((cal-date (org-get-date-from-calendar)))
  16431. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16432. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16433. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16434. (setcar time0 (or (car time0) 0))
  16435. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16436. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16437. (setq time (apply 'encode-time time0))))
  16438. ;; Insert the new time-stamp, and ensure point stays in the same
  16439. ;; category as before (i.e. not after the last position in that
  16440. ;; category).
  16441. (let ((pos (point)))
  16442. ;; Stay before inserted string. `save-excursion' is of no use.
  16443. (setq org-last-changed-timestamp
  16444. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16445. (goto-char pos))
  16446. (save-match-data
  16447. (looking-at org-ts-regexp3)
  16448. (goto-char (cond
  16449. ;; `day' category ends before `hour' if any, or at
  16450. ;; the end of the day name.
  16451. ((eq origin-cat 'day)
  16452. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16453. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16454. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16455. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16456. ;; `year' and `month' have both fixed size: point
  16457. ;; couldn't have moved into another part.
  16458. (t origin))))
  16459. ;; Update clock if on a CLOCK line.
  16460. (org-clock-update-time-maybe)
  16461. ;; Maybe adjust the closest clock in `org-clock-history'
  16462. (when org-clock-adjust-closest
  16463. (if (not (and (org-at-clock-log-p)
  16464. (< 1 (length (delq nil (mapcar 'marker-position
  16465. org-clock-history))))))
  16466. (message "No clock to adjust")
  16467. (cond ((save-excursion ; fix previous clock?
  16468. (re-search-backward org-ts-regexp0 nil t)
  16469. (org-looking-back (concat org-clock-string " \\[")
  16470. (line-beginning-position)))
  16471. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16472. ((save-excursion ; fix next clock?
  16473. (re-search-backward org-ts-regexp0 nil t)
  16474. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16475. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16476. (save-window-excursion
  16477. ;; Find closest clock to point, adjust the previous/next one in history
  16478. (let* ((p (save-excursion (org-back-to-heading t)))
  16479. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16480. (clfixnth
  16481. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16482. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16483. (if (not clfixpos)
  16484. (message "No clock to adjust")
  16485. (save-excursion
  16486. (org-goto-marker-or-bmk clfixpos)
  16487. (org-show-subtree)
  16488. (when (re-search-forward clrgx nil t)
  16489. (goto-char (match-beginning 1))
  16490. (let (org-clock-adjust-closest)
  16491. (org-timestamp-change n org-ts-what updown))
  16492. (message "Clock adjusted in %s for heading: %s"
  16493. (file-name-nondirectory (buffer-file-name))
  16494. (org-get-heading t t)))))))))
  16495. ;; Try to recenter the calendar window, if any.
  16496. (when (and org-calendar-follow-timestamp-change
  16497. (get-buffer-window "*Calendar*" t)
  16498. (memq org-ts-what '(day month year)))
  16499. (org-recenter-calendar (time-to-days time))))))
  16500. (defun org-modify-ts-extra (s pos n dm)
  16501. "Change the different parts of the lead-time and repeat fields in timestamp."
  16502. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16503. ng h m new rem)
  16504. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16505. (cond
  16506. ((or (org-pos-in-match-range pos 2)
  16507. (org-pos-in-match-range pos 3))
  16508. (setq m (string-to-number (match-string 3 s))
  16509. h (string-to-number (match-string 2 s)))
  16510. (if (org-pos-in-match-range pos 2)
  16511. (setq h (+ h n))
  16512. (setq n (* dm (org-no-warnings (signum n))))
  16513. (unless (= 0 (setq rem (% m dm)))
  16514. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16515. (setq m (+ m n)))
  16516. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16517. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16518. (setq h (mod h 24))
  16519. (setq ng 1 new (format "-%02d:%02d" h m)))
  16520. ((org-pos-in-match-range pos 6)
  16521. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16522. ((org-pos-in-match-range pos 5)
  16523. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16524. ((org-pos-in-match-range pos 9)
  16525. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16526. ((org-pos-in-match-range pos 8)
  16527. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16528. (when ng
  16529. (setq s (concat
  16530. (substring s 0 (match-beginning ng))
  16531. new
  16532. (substring s (match-end ng))))))
  16533. s))
  16534. (defun org-recenter-calendar (d)
  16535. "If the calendar is visible, recenter it to date D."
  16536. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16537. (when cwin
  16538. (let ((calendar-move-hook nil))
  16539. (with-selected-window cwin
  16540. (calendar-goto-date
  16541. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16542. (defun org-goto-calendar (&optional arg)
  16543. "Go to the Emacs calendar at the current date.
  16544. If there is a time stamp in the current line, go to that date.
  16545. A prefix ARG can be used to force the current date."
  16546. (interactive "P")
  16547. (let ((tsr org-ts-regexp) diff
  16548. (calendar-move-hook nil)
  16549. (calendar-view-holidays-initially-flag nil)
  16550. (calendar-view-diary-initially-flag nil))
  16551. (when (or (org-at-timestamp-p)
  16552. (save-excursion
  16553. (beginning-of-line 1)
  16554. (looking-at (concat ".*" tsr))))
  16555. (let ((d1 (time-to-days (current-time)))
  16556. (d2 (time-to-days
  16557. (org-time-string-to-time (match-string 1)))))
  16558. (setq diff (- d2 d1))))
  16559. (calendar)
  16560. (calendar-goto-today)
  16561. (when (and diff (not arg)) (calendar-forward-day diff))))
  16562. (defun org-get-date-from-calendar ()
  16563. "Return a list (month day year) of date at point in calendar."
  16564. (with-current-buffer "*Calendar*"
  16565. (save-match-data
  16566. (calendar-cursor-to-date))))
  16567. (defun org-date-from-calendar ()
  16568. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16569. If there is already a time stamp at the cursor position, update it."
  16570. (interactive)
  16571. (if (org-at-timestamp-p t)
  16572. (org-timestamp-change 0 'calendar)
  16573. (let ((cal-date (org-get-date-from-calendar)))
  16574. (org-insert-time-stamp
  16575. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16576. (defcustom org-effort-durations
  16577. `(("min" . 1)
  16578. ("h" . 60)
  16579. ("d" . ,(* 60 8))
  16580. ("w" . ,(* 60 8 5))
  16581. ("m" . ,(* 60 8 5 4))
  16582. ("y" . ,(* 60 8 5 40)))
  16583. "Conversion factor to minutes for an effort modifier.
  16584. Each entry has the form (MODIFIER . MINUTES).
  16585. In an effort string, a number followed by MODIFIER is multiplied
  16586. by the specified number of MINUTES to obtain an effort in
  16587. minutes.
  16588. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16589. effort string \"2hours\" is equivalent to 120 minutes."
  16590. :group 'org-agenda
  16591. :version "25.1"
  16592. :package-version '(Org . "8.3")
  16593. :type '(alist :key-type (string :tag "Modifier")
  16594. :value-type (number :tag "Minutes")))
  16595. (defun org-minutes-to-clocksum-string (m)
  16596. "Format number of minutes as a clocksum string.
  16597. The format is determined by `org-time-clocksum-format',
  16598. `org-time-clocksum-use-fractional' and
  16599. `org-time-clocksum-fractional-format' and
  16600. `org-time-clocksum-use-effort-durations'."
  16601. (let ((clocksum "")
  16602. (m (round m)) ; Don't allow fractions of minutes
  16603. h d w mo y fmt n)
  16604. (setq h (if org-time-clocksum-use-effort-durations
  16605. (cdr (assoc "h" org-effort-durations)) 60)
  16606. d (if org-time-clocksum-use-effort-durations
  16607. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16608. w (if org-time-clocksum-use-effort-durations
  16609. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16610. mo (if org-time-clocksum-use-effort-durations
  16611. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16612. y (if org-time-clocksum-use-effort-durations
  16613. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16614. ;; fractional format
  16615. (if org-time-clocksum-use-fractional
  16616. (cond
  16617. ;; single format string
  16618. ((stringp org-time-clocksum-fractional-format)
  16619. (format org-time-clocksum-fractional-format (/ m (float h))))
  16620. ;; choice of fractional formats for different time units
  16621. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16622. (> (/ (truncate m) (* y d h)) 0))
  16623. (format fmt (/ m (* y d (float h)))))
  16624. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16625. (> (/ (truncate m) (* mo d h)) 0))
  16626. (format fmt (/ m (* mo d (float h)))))
  16627. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16628. (> (/ (truncate m) (* w d h)) 0))
  16629. (format fmt (/ m (* w d (float h)))))
  16630. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16631. (> (/ (truncate m) (* d h)) 0))
  16632. (format fmt (/ m (* d (float h)))))
  16633. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16634. (> (/ (truncate m) h) 0))
  16635. (format fmt (/ m (float h))))
  16636. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16637. (format fmt m))
  16638. ;; fall back to smallest time unit with a format
  16639. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16640. (format fmt (/ m (float h))))
  16641. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16642. (format fmt (/ m (* d (float h)))))
  16643. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16644. (format fmt (/ m (* w d (float h)))))
  16645. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16646. (format fmt (/ m (* mo d (float h)))))
  16647. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16648. (format fmt (/ m (* y d (float h))))))
  16649. ;; standard (non-fractional) format, with single format string
  16650. (if (stringp org-time-clocksum-format)
  16651. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16652. ;; separate formats components
  16653. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16654. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16655. (plist-get org-time-clocksum-format :require-years))
  16656. (setq clocksum (concat clocksum (format fmt n))
  16657. m (- m (* n y d h))))
  16658. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16659. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16660. (plist-get org-time-clocksum-format :require-months))
  16661. (setq clocksum (concat clocksum (format fmt n))
  16662. m (- m (* n mo d h))))
  16663. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16664. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16665. (plist-get org-time-clocksum-format :require-weeks))
  16666. (setq clocksum (concat clocksum (format fmt n))
  16667. m (- m (* n w d h))))
  16668. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16669. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16670. (plist-get org-time-clocksum-format :require-days))
  16671. (setq clocksum (concat clocksum (format fmt n))
  16672. m (- m (* n d h))))
  16673. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16674. (or (> (setq n (/ (truncate m) h)) 0)
  16675. (plist-get org-time-clocksum-format :require-hours))
  16676. (setq clocksum (concat clocksum (format fmt n))
  16677. m (- m (* n h))))
  16678. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16679. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16680. (setq clocksum (concat clocksum (format fmt m))))
  16681. ;; return formatted time duration
  16682. clocksum))))
  16683. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16684. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16685. "Org mode version 8.0")
  16686. (defun org-hours-to-clocksum-string (n)
  16687. (org-minutes-to-clocksum-string (* n 60)))
  16688. (defun org-hh:mm-string-to-minutes (s)
  16689. "Convert a string H:MM to a number of minutes.
  16690. If the string is just a number, interpret it as minutes.
  16691. In fact, the first hh:mm or number in the string will be taken,
  16692. there can be extra stuff in the string.
  16693. If no number is found, the return value is 0."
  16694. (cond
  16695. ((integerp s) s)
  16696. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16697. (+ (* (string-to-number (match-string 1 s)) 60)
  16698. (string-to-number (match-string 2 s))))
  16699. ((string-match "\\([0-9]+\\)" s)
  16700. (string-to-number (match-string 1 s)))
  16701. (t 0)))
  16702. (defcustom org-image-actual-width t
  16703. "Should we use the actual width of images when inlining them?
  16704. When set to t, always use the image width.
  16705. When set to a number, use imagemagick (when available) to set
  16706. the image's width to this value.
  16707. When set to a number in a list, try to get the width from any
  16708. #+ATTR.* keyword if it matches a width specification like
  16709. #+ATTR_HTML: :width 300px
  16710. and fall back on that number if none is found.
  16711. When set to nil, try to get the width from an #+ATTR.* keyword
  16712. and fall back on the original width if none is found.
  16713. This requires Emacs >= 24.1, build with imagemagick support."
  16714. :group 'org-appearance
  16715. :version "24.4"
  16716. :package-version '(Org . "8.0")
  16717. :type '(choice
  16718. (const :tag "Use the image width" t)
  16719. (integer :tag "Use a number of pixels")
  16720. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16721. (const :tag "Use #+ATTR* or don't resize" nil)))
  16722. (defcustom org-agenda-inhibit-startup nil
  16723. "Inhibit startup when preparing agenda buffers.
  16724. When this variable is t, the initialization of the Org agenda
  16725. buffers is inhibited: e.g. the visibility state is not set, the
  16726. tables are not re-aligned, etc."
  16727. :type 'boolean
  16728. :version "24.3"
  16729. :group 'org-agenda)
  16730. (define-obsolete-variable-alias
  16731. 'org-agenda-ignore-drawer-properties
  16732. 'org-agenda-ignore-properties "25.1")
  16733. (defcustom org-agenda-ignore-properties nil
  16734. "Avoid updating text properties when building the agenda.
  16735. Properties are used to prepare buffers for effort estimates,
  16736. appointments, statistics and subtree-local categories.
  16737. If you don't use these in the agenda, you can add them to this
  16738. list and agenda building will be a bit faster.
  16739. The value is a list, with zero or more of the symbols `effort', `appt',
  16740. `stats' or `category'."
  16741. :type '(set :greedy t
  16742. (const effort)
  16743. (const appt)
  16744. (const stats)
  16745. (const category))
  16746. :version "25.1"
  16747. :package-version '(Org . "8.3")
  16748. :group 'org-agenda)
  16749. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16750. "Convert a duration string S to minutes.
  16751. A bare number is interpreted as minutes, modifiers can be set by
  16752. customizing `org-effort-durations' (which see).
  16753. Entries containing a colon are interpreted as H:MM by
  16754. `org-hh:mm-string-to-minutes'."
  16755. (let ((result 0)
  16756. (re (concat "\\([0-9.]+\\) *\\("
  16757. (regexp-opt (mapcar 'car org-effort-durations))
  16758. "\\)")))
  16759. (while (string-match re s)
  16760. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16761. (string-to-number (match-string 1 s))))
  16762. (setq s (replace-match "" nil t s)))
  16763. (setq result (floor result))
  16764. (cl-incf result (org-hh:mm-string-to-minutes s))
  16765. (if output-to-string (number-to-string result) result)))
  16766. ;;;; Files
  16767. (defun org-save-all-org-buffers ()
  16768. "Save all Org-mode buffers without user confirmation."
  16769. (interactive)
  16770. (message "Saving all Org-mode buffers...")
  16771. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16772. (when (featurep 'org-id) (org-id-locations-save))
  16773. (message "Saving all Org-mode buffers... done"))
  16774. (defun org-revert-all-org-buffers ()
  16775. "Revert all Org-mode buffers.
  16776. Prompt for confirmation when there are unsaved changes.
  16777. Be sure you know what you are doing before letting this function
  16778. overwrite your changes.
  16779. This function is useful in a setup where one tracks org files
  16780. with a version control system, to revert on one machine after pulling
  16781. changes from another. I believe the procedure must be like this:
  16782. 1. M-x org-save-all-org-buffers
  16783. 2. Pull changes from the other machine, resolve conflicts
  16784. 3. M-x org-revert-all-org-buffers"
  16785. (interactive)
  16786. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16787. (user-error "Abort"))
  16788. (save-excursion
  16789. (save-window-excursion
  16790. (dolist (b (buffer-list))
  16791. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16792. (with-current-buffer b buffer-file-name))
  16793. (org-pop-to-buffer-same-window b)
  16794. (revert-buffer t 'no-confirm)))
  16795. (when (and (featurep 'org-id) org-id-track-globally)
  16796. (org-id-locations-load)))))
  16797. ;;;; Agenda files
  16798. ;;;###autoload
  16799. (defun org-switchb (&optional arg)
  16800. "Switch between Org buffers.
  16801. With \\[universal-argument] prefix, restrict available buffers to files.
  16802. With \\[universal-argument] \\[universal-argument] \
  16803. prefix, restrict available buffers to agenda files."
  16804. (interactive "P")
  16805. (let ((blist (org-buffer-list
  16806. (cond ((equal arg '(4)) 'files)
  16807. ((equal arg '(16)) 'agenda)))))
  16808. (org-pop-to-buffer-same-window
  16809. (completing-read "Org buffer: "
  16810. (mapcar #'list (mapcar #'buffer-name blist))
  16811. nil t))))
  16812. (defun org-buffer-list (&optional predicate exclude-tmp)
  16813. "Return a list of Org buffers.
  16814. PREDICATE can be `export', `files' or `agenda'.
  16815. export restrict the list to Export buffers.
  16816. files restrict the list to buffers visiting Org files.
  16817. agenda restrict the list to buffers visiting agenda files.
  16818. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16819. (let* ((bfn nil)
  16820. (agenda-files (and (eq predicate 'agenda)
  16821. (mapcar 'file-truename (org-agenda-files t))))
  16822. (filter
  16823. (cond
  16824. ((eq predicate 'files)
  16825. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16826. ((eq predicate 'export)
  16827. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16828. ((eq predicate 'agenda)
  16829. (lambda (b)
  16830. (with-current-buffer b
  16831. (and (derived-mode-p 'org-mode)
  16832. (setq bfn (buffer-file-name b))
  16833. (member (file-truename bfn) agenda-files)))))
  16834. (t (lambda (b) (with-current-buffer b
  16835. (or (derived-mode-p 'org-mode)
  16836. (string-match "\\*Org .*Export"
  16837. (buffer-name b)))))))))
  16838. (delq nil
  16839. (mapcar
  16840. (lambda(b)
  16841. (if (and (funcall filter b)
  16842. (or (not exclude-tmp)
  16843. (not (string-match "tmp" (buffer-name b)))))
  16844. b
  16845. nil))
  16846. (buffer-list)))))
  16847. (defun org-agenda-files (&optional unrestricted archives)
  16848. "Get the list of agenda files.
  16849. Optional UNRESTRICTED means return the full list even if a restriction
  16850. is currently in place.
  16851. When ARCHIVES is t, include all archive files that are really being
  16852. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16853. `org-agenda-archives-mode' is t."
  16854. (let ((files
  16855. (cond
  16856. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16857. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16858. ((listp org-agenda-files) org-agenda-files)
  16859. (t (error "Invalid value of `org-agenda-files'")))))
  16860. (setq files (apply 'append
  16861. (mapcar (lambda (f)
  16862. (if (file-directory-p f)
  16863. (directory-files
  16864. f t org-agenda-file-regexp)
  16865. (list f)))
  16866. files)))
  16867. (when org-agenda-skip-unavailable-files
  16868. (setq files (delq nil
  16869. (mapcar (function
  16870. (lambda (file)
  16871. (and (file-readable-p file) file)))
  16872. files))))
  16873. (when (or (eq archives t)
  16874. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16875. (setq files (org-add-archive-files files)))
  16876. files))
  16877. (defun org-agenda-file-p (&optional file)
  16878. "Return non-nil, if FILE is an agenda file.
  16879. If FILE is omitted, use the file associated with the current
  16880. buffer."
  16881. (let ((fname (or file (buffer-file-name))))
  16882. (and fname
  16883. (member (file-truename fname)
  16884. (mapcar #'file-truename (org-agenda-files t))))))
  16885. (defun org-edit-agenda-file-list ()
  16886. "Edit the list of agenda files.
  16887. Depending on setup, this either uses customize to edit the variable
  16888. `org-agenda-files', or it visits the file that is holding the list. In the
  16889. latter case, the buffer is set up in a way that saving it automatically kills
  16890. the buffer and restores the previous window configuration."
  16891. (interactive)
  16892. (if (stringp org-agenda-files)
  16893. (let ((cw (current-window-configuration)))
  16894. (find-file org-agenda-files)
  16895. (setq-local org-window-configuration cw)
  16896. (org-add-hook 'after-save-hook
  16897. (lambda ()
  16898. (set-window-configuration
  16899. (prog1 org-window-configuration
  16900. (kill-buffer (current-buffer))))
  16901. (org-install-agenda-files-menu)
  16902. (message "New agenda file list installed"))
  16903. nil 'local)
  16904. (message "%s" (substitute-command-keys
  16905. "Edit list and finish with \\[save-buffer]")))
  16906. (customize-variable 'org-agenda-files)))
  16907. (defun org-store-new-agenda-file-list (list)
  16908. "Set new value for the agenda file list and save it correctly."
  16909. (if (stringp org-agenda-files)
  16910. (let ((fe (org-read-agenda-file-list t)) b u)
  16911. (while (setq b (find-buffer-visiting org-agenda-files))
  16912. (kill-buffer b))
  16913. (with-temp-file org-agenda-files
  16914. (insert
  16915. (mapconcat
  16916. (lambda (f) ;; Keep un-expanded entries.
  16917. (if (setq u (assoc f fe))
  16918. (cdr u)
  16919. f))
  16920. list "\n")
  16921. "\n")))
  16922. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16923. (org-insert-mode-line-in-empty-file nil))
  16924. (setq org-agenda-files list)
  16925. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16926. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16927. "Read the list of agenda files from a file.
  16928. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16929. filenames, used by `org-store-new-agenda-file-list' to write back
  16930. un-expanded file names."
  16931. (when (file-directory-p org-agenda-files)
  16932. (error "`org-agenda-files' cannot be a single directory"))
  16933. (when (stringp org-agenda-files)
  16934. (with-temp-buffer
  16935. (insert-file-contents org-agenda-files)
  16936. (mapcar
  16937. (lambda (f)
  16938. (let ((e (expand-file-name (substitute-in-file-name f)
  16939. org-directory)))
  16940. (if pair-with-expansion
  16941. (cons e f)
  16942. e)))
  16943. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16944. ;;;###autoload
  16945. (defun org-cycle-agenda-files ()
  16946. "Cycle through the files in `org-agenda-files'.
  16947. If the current buffer visits an agenda file, find the next one in the list.
  16948. If the current buffer does not, find the first agenda file."
  16949. (interactive)
  16950. (let* ((fs (or (org-agenda-files t)
  16951. (user-error "No agenda files")))
  16952. (files (copy-sequence fs))
  16953. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  16954. file)
  16955. (when tcf
  16956. (while (and (setq file (pop files))
  16957. (not (equal (file-truename file) tcf)))))
  16958. (find-file (car (or files fs)))
  16959. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16960. (defun org-agenda-file-to-front (&optional to-end)
  16961. "Move/add the current file to the top of the agenda file list.
  16962. If the file is not present in the list, it is added to the front. If it is
  16963. present, it is moved there. With optional argument TO-END, add/move to the
  16964. end of the list."
  16965. (interactive "P")
  16966. (let ((org-agenda-skip-unavailable-files nil)
  16967. (file-alist (mapcar (lambda (x)
  16968. (cons (file-truename x) x))
  16969. (org-agenda-files t)))
  16970. (ctf (file-truename
  16971. (or buffer-file-name
  16972. (user-error "Please save the current buffer to a file"))))
  16973. x had)
  16974. (setq x (assoc ctf file-alist) had x)
  16975. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16976. (if to-end
  16977. (setq file-alist (append (delq x file-alist) (list x)))
  16978. (setq file-alist (cons x (delq x file-alist))))
  16979. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16980. (org-install-agenda-files-menu)
  16981. (message "File %s to %s of agenda file list"
  16982. (if had "moved" "added") (if to-end "end" "front"))))
  16983. (defun org-remove-file (&optional file)
  16984. "Remove current file from the list of files in variable `org-agenda-files'.
  16985. These are the files which are being checked for agenda entries.
  16986. Optional argument FILE means use this file instead of the current."
  16987. (interactive)
  16988. (let* ((org-agenda-skip-unavailable-files nil)
  16989. (file (or file buffer-file-name
  16990. (user-error "Current buffer does not visit a file")))
  16991. (true-file (file-truename file))
  16992. (afile (abbreviate-file-name file))
  16993. (files (delq nil (mapcar
  16994. (lambda (x)
  16995. (unless (equal true-file
  16996. (file-truename x))
  16997. x))
  16998. (org-agenda-files t)))))
  16999. (if (not (= (length files) (length (org-agenda-files t))))
  17000. (progn
  17001. (org-store-new-agenda-file-list files)
  17002. (org-install-agenda-files-menu)
  17003. (message "Removed from Org Agenda list: %s" afile))
  17004. (message "File was not in list: %s (not removed)" afile))))
  17005. (defun org-file-menu-entry (file)
  17006. (vector file (list 'find-file file) t))
  17007. (defun org-check-agenda-file (file)
  17008. "Make sure FILE exists. If not, ask user what to do."
  17009. (unless (file-exists-p file)
  17010. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17011. (abbreviate-file-name file))
  17012. (let ((r (downcase (read-char-exclusive))))
  17013. (cond
  17014. ((equal r ?r)
  17015. (org-remove-file file)
  17016. (throw 'nextfile t))
  17017. (t (user-error "Abort"))))))
  17018. (defun org-get-agenda-file-buffer (file)
  17019. "Get an agenda buffer visiting FILE.
  17020. If the buffer needs to be created, add it to the list of buffers
  17021. which might be released later."
  17022. (let ((buf (org-find-base-buffer-visiting file)))
  17023. (if buf
  17024. buf ; just return it
  17025. ;; Make a new buffer and remember it
  17026. (setq buf (find-file-noselect file))
  17027. (when buf (push buf org-agenda-new-buffers))
  17028. buf)))
  17029. (defun org-release-buffers (blist)
  17030. "Release all buffers in list, asking the user for confirmation when needed.
  17031. When a buffer is unmodified, it is just killed. When modified, it is saved
  17032. \(if the user agrees) and then killed."
  17033. (let (file)
  17034. (dolist (buf blist)
  17035. (setq file (buffer-file-name buf))
  17036. (when (and (buffer-modified-p buf)
  17037. file
  17038. (y-or-n-p (format "Save file %s? " file)))
  17039. (with-current-buffer buf (save-buffer)))
  17040. (kill-buffer buf))))
  17041. (defun org-agenda-prepare-buffers (files)
  17042. "Create buffers for all agenda files, protect archived trees and comments."
  17043. (interactive)
  17044. (let ((pa '(:org-archived t))
  17045. (pc '(:org-comment t))
  17046. (pall '(:org-archived t :org-comment t))
  17047. (inhibit-read-only t)
  17048. (org-inhibit-startup org-agenda-inhibit-startup)
  17049. (rea (concat ":" org-archive-tag ":"))
  17050. re pos)
  17051. (setq org-tag-alist-for-agenda nil
  17052. org-tag-groups-alist-for-agenda nil)
  17053. (save-excursion
  17054. (save-restriction
  17055. (dolist (file files)
  17056. (catch 'nextfile
  17057. (if (bufferp file)
  17058. (set-buffer file)
  17059. (org-check-agenda-file file)
  17060. (set-buffer (org-get-agenda-file-buffer file)))
  17061. (widen)
  17062. (org-set-regexps-and-options 'tags-only)
  17063. (setq pos (point))
  17064. (or (memq 'category org-agenda-ignore-properties)
  17065. (org-refresh-category-properties))
  17066. (or (memq 'stats org-agenda-ignore-properties)
  17067. (org-refresh-stats-properties))
  17068. (or (memq 'effort org-agenda-ignore-properties)
  17069. (org-refresh-effort-properties))
  17070. (or (memq 'appt org-agenda-ignore-properties)
  17071. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17072. (setq org-todo-keywords-for-agenda
  17073. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17074. (setq org-done-keywords-for-agenda
  17075. (append org-done-keywords-for-agenda org-done-keywords))
  17076. (setq org-todo-keyword-alist-for-agenda
  17077. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17078. (setq org-tag-alist-for-agenda
  17079. (org-uniquify
  17080. (append org-tag-alist-for-agenda
  17081. org-tag-alist
  17082. org-tag-persistent-alist)))
  17083. ;; Merge current file's tag groups into global
  17084. ;; `org-tag-groups-alist-for-agenda'.
  17085. (when org-group-tags
  17086. (dolist (alist org-tag-groups-alist)
  17087. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17088. (if old
  17089. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17090. (push alist org-tag-groups-alist-for-agenda)))))
  17091. (org-with-silent-modifications
  17092. (save-excursion
  17093. (remove-text-properties (point-min) (point-max) pall)
  17094. (when org-agenda-skip-archived-trees
  17095. (goto-char (point-min))
  17096. (while (re-search-forward rea nil t)
  17097. (when (org-at-heading-p t)
  17098. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17099. (goto-char (point-min))
  17100. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17101. (while (re-search-forward re nil t)
  17102. (when (save-match-data (org-in-commented-heading-p t))
  17103. (add-text-properties
  17104. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17105. (goto-char pos)))))
  17106. (setq org-todo-keywords-for-agenda
  17107. (org-uniquify org-todo-keywords-for-agenda))
  17108. (setq org-todo-keyword-alist-for-agenda
  17109. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17110. ;;;; CDLaTeX minor mode
  17111. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17112. "Keymap for the minor `org-cdlatex-mode'.")
  17113. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17114. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17115. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17116. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17117. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17118. (defvar org-cdlatex-texmathp-advice-is-done nil
  17119. "Flag remembering if we have applied the advice to texmathp already.")
  17120. (define-minor-mode org-cdlatex-mode
  17121. "Toggle the minor `org-cdlatex-mode'.
  17122. This mode supports entering LaTeX environment and math in LaTeX fragments
  17123. in Org-mode.
  17124. \\{org-cdlatex-mode-map}"
  17125. nil " OCDL" nil
  17126. (when org-cdlatex-mode
  17127. (require 'cdlatex)
  17128. (run-hooks 'cdlatex-mode-hook)
  17129. (cdlatex-compute-tables))
  17130. (unless org-cdlatex-texmathp-advice-is-done
  17131. (setq org-cdlatex-texmathp-advice-is-done t)
  17132. (defadvice texmathp (around org-math-always-on activate)
  17133. "Always return t in org-mode buffers.
  17134. This is because we want to insert math symbols without dollars even outside
  17135. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17136. an embedded LaTeX fragment, let texmathp do its job.
  17137. \\[org-cdlatex-mode-map]"
  17138. (interactive)
  17139. (let (p)
  17140. (cond
  17141. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17142. ((eq this-command 'cdlatex-math-symbol)
  17143. (setq ad-return-value t
  17144. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17145. (t
  17146. (let ((p (org-inside-LaTeX-fragment-p)))
  17147. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17148. (setq ad-return-value t
  17149. texmathp-why '("Org-mode embedded math" . 0))
  17150. (when p ad-do-it)))))))))
  17151. (defun turn-on-org-cdlatex ()
  17152. "Unconditionally turn on `org-cdlatex-mode'."
  17153. (org-cdlatex-mode 1))
  17154. (defun org-try-cdlatex-tab ()
  17155. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17156. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17157. - inside a LaTeX fragment, or
  17158. - after the first word in a line, where an abbreviation expansion could
  17159. insert a LaTeX environment."
  17160. (when org-cdlatex-mode
  17161. (cond
  17162. ;; Before any word on the line: No expansion possible.
  17163. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17164. ;; Just after first word on the line: Expand it. Make sure it
  17165. ;; cannot happen on headlines, though.
  17166. ((save-excursion
  17167. (skip-chars-backward "a-zA-Z0-9*")
  17168. (skip-chars-backward " \t")
  17169. (and (bolp) (not (org-at-heading-p))))
  17170. (cdlatex-tab) t)
  17171. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17172. (defun org-cdlatex-underscore-caret (&optional _arg)
  17173. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17174. Revert to the normal definition outside of these fragments."
  17175. (interactive "P")
  17176. (if (org-inside-LaTeX-fragment-p)
  17177. (call-interactively 'cdlatex-sub-superscript)
  17178. (let (org-cdlatex-mode)
  17179. (call-interactively (key-binding (vector last-input-event))))))
  17180. (defun org-cdlatex-math-modify (&optional _arg)
  17181. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17182. Revert to the normal definition outside of these fragments."
  17183. (interactive "P")
  17184. (if (org-inside-LaTeX-fragment-p)
  17185. (call-interactively 'cdlatex-math-modify)
  17186. (let (org-cdlatex-mode)
  17187. (call-interactively (key-binding (vector last-input-event))))))
  17188. (defun org-cdlatex-environment-indent (&optional environment item)
  17189. "Execute `cdlatex-environment' and indent the inserted environment.
  17190. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17191. The inserted environment is indented to current indentation
  17192. unless point is at the beginning of the line, in which the
  17193. environment remains unintended."
  17194. (interactive)
  17195. ;; cdlatex-environment always return nil. Therefore, capture output
  17196. ;; first and determine if an environment was selected.
  17197. (let* ((beg (point-marker))
  17198. (end (copy-marker (point) t))
  17199. (inserted (progn
  17200. (ignore-errors (cdlatex-environment environment item))
  17201. (< beg end)))
  17202. ;; Figure out how many lines to move forward after the
  17203. ;; environment has been inserted.
  17204. (lines (when inserted
  17205. (save-excursion
  17206. (- (cl-loop while (< beg (point))
  17207. with x = 0
  17208. do (forward-line -1)
  17209. (cl-incf x)
  17210. finally return x)
  17211. (if (progn (goto-char beg)
  17212. (and (progn (skip-chars-forward " \t") (eolp))
  17213. (progn (skip-chars-backward " \t") (bolp))))
  17214. 1 0)))))
  17215. (env (org-trim (delete-and-extract-region beg end))))
  17216. (when inserted
  17217. ;; Get indentation of next line unless at column 0.
  17218. (let ((ind (if (bolp) 0
  17219. (save-excursion
  17220. (org-return-indent)
  17221. (prog1 (org-get-indentation)
  17222. (when (progn (skip-chars-forward " \t") (eolp))
  17223. (delete-region beg (point)))))))
  17224. (bol (progn (skip-chars-backward " \t") (bolp))))
  17225. ;; Insert a newline before environment unless at column zero
  17226. ;; to "escape" the current line. Insert a newline if
  17227. ;; something is one the same line as \end{ENVIRONMENT}.
  17228. (insert
  17229. (concat (unless bol "\n") env
  17230. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17231. (unless (zerop ind)
  17232. (save-excursion
  17233. (goto-char beg)
  17234. (while (< (point) end)
  17235. (unless (eolp) (org-indent-line-to ind))
  17236. (forward-line))))
  17237. (goto-char beg)
  17238. (forward-line lines)
  17239. (org-indent-line-to ind)))
  17240. (set-marker beg nil)
  17241. (set-marker end nil)))
  17242. ;;;; LaTeX fragments
  17243. (defun org-inside-LaTeX-fragment-p ()
  17244. "Test if point is inside a LaTeX fragment.
  17245. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17246. sequence appearing also before point.
  17247. Even though the matchers for math are configurable, this function assumes
  17248. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17249. delimiters are skipped when they have been removed by customization.
  17250. The return value is nil, or a cons cell with the delimiter and the
  17251. position of this delimiter.
  17252. This function does a reasonably good job, but can locally be fooled by
  17253. for example currency specifications. For example it will assume being in
  17254. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17255. fragments that are properly closed, but during editing, we have to live
  17256. with the uncertainty caused by missing closing delimiters. This function
  17257. looks only before point, not after."
  17258. (catch 'exit
  17259. (let ((pos (point))
  17260. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17261. (lim (progn
  17262. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17263. (point)))
  17264. dd-on str (start 0) m re)
  17265. (goto-char pos)
  17266. (when dodollar
  17267. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17268. re (nth 1 (assoc "$" org-latex-regexps)))
  17269. (while (string-match re str start)
  17270. (cond
  17271. ((= (match-end 0) (length str))
  17272. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17273. ((= (match-end 0) (- (length str) 5))
  17274. (throw 'exit nil))
  17275. (t (setq start (match-end 0))))))
  17276. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17277. (goto-char pos)
  17278. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17279. (and (match-beginning 2) (throw 'exit nil))
  17280. ;; count $$
  17281. (while (re-search-backward "\\$\\$" lim t)
  17282. (setq dd-on (not dd-on)))
  17283. (goto-char pos)
  17284. (when dd-on (cons "$$" m))))))
  17285. (defun org-inside-latex-macro-p ()
  17286. "Is point inside a LaTeX macro or its arguments?"
  17287. (save-match-data
  17288. (org-in-regexp
  17289. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17290. (defvar-local org-latex-fragment-image-overlays nil
  17291. "List of overlays carrying the images of latex fragments.")
  17292. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17293. "Remove all overlays with LaTeX fragment images in current buffer.
  17294. When optional arguments BEG and END are non-nil, remove all
  17295. overlays between them instead. Return t when some overlays were
  17296. removed, nil otherwise."
  17297. (let (removedp)
  17298. (setq org-latex-fragment-image-overlays
  17299. (let ((beg (or beg (point-min)))
  17300. (end (or end (point-max))))
  17301. (cl-remove-if
  17302. (lambda (o)
  17303. (cond ((not (overlay-buffer o)) (delete-overlay o) t)
  17304. ((and (>= (overlay-start o) beg)
  17305. (<= (overlay-end o) end))
  17306. (delete-overlay o)
  17307. (unless removedp (setq removedp t)))
  17308. (t nil)))
  17309. org-latex-fragment-image-overlays)))
  17310. removedp))
  17311. (define-obsolete-function-alias
  17312. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17313. (defun org-toggle-latex-fragment (&optional arg)
  17314. "Preview the LaTeX fragment at point, or all locally or globally.
  17315. If the cursor is on a LaTeX fragment, create the image and overlay
  17316. it over the source code, if there is none. Remove it otherwise.
  17317. If there is no fragment at point, display all fragments in the
  17318. current section.
  17319. With prefix ARG, preview or clear image for all fragments in the
  17320. current subtree or in the whole buffer when used before the first
  17321. headline. With a double prefix ARG \\[universal-argument] \
  17322. \\[universal-argument] preview or clear images
  17323. for all fragments in the buffer."
  17324. (interactive "P")
  17325. (unless (buffer-file-name (buffer-base-buffer))
  17326. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17327. (when (display-graphic-p)
  17328. (catch 'exit
  17329. (save-excursion
  17330. (let ((window-start (window-start)) msg)
  17331. (save-restriction
  17332. (cond
  17333. ((or (equal arg '(16))
  17334. (and (equal arg '(4))
  17335. (org-with-limited-levels (org-before-first-heading-p))))
  17336. (if (org-remove-latex-fragment-image-overlays)
  17337. (progn (message "LaTeX fragments images removed from buffer")
  17338. (throw 'exit nil))
  17339. (setq msg "Creating images for buffer...")))
  17340. ((equal arg '(4))
  17341. (org-with-limited-levels (org-back-to-heading t))
  17342. (let ((beg (point))
  17343. (end (progn (org-end-of-subtree t) (point))))
  17344. (if (org-remove-latex-fragment-image-overlays beg end)
  17345. (progn
  17346. (message "LaTeX fragment images removed from subtree")
  17347. (throw 'exit nil))
  17348. (setq msg "Creating images for subtree...")
  17349. (narrow-to-region beg end))))
  17350. ((let ((datum (org-element-context)))
  17351. (when (memq (org-element-type datum)
  17352. '(latex-environment latex-fragment))
  17353. (let* ((beg (org-element-property :begin datum))
  17354. (end (org-element-property :end datum)))
  17355. (if (org-remove-latex-fragment-image-overlays beg end)
  17356. (progn (message "LaTeX fragment image removed")
  17357. (throw 'exit nil))
  17358. (narrow-to-region beg end)
  17359. (setq msg "Creating image..."))))))
  17360. (t
  17361. (org-with-limited-levels
  17362. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17363. (outline-previous-heading)
  17364. (point)))
  17365. (end (progn (outline-next-heading) (point))))
  17366. (if (org-remove-latex-fragment-image-overlays beg end)
  17367. (progn
  17368. (message "LaTeX fragment images removed from section")
  17369. (throw 'exit nil))
  17370. (setq msg "Creating images for section...")
  17371. (narrow-to-region beg end))))))
  17372. (let ((file (buffer-file-name (buffer-base-buffer))))
  17373. (org-format-latex
  17374. (concat org-latex-preview-ltxpng-directory
  17375. (file-name-sans-extension (file-name-nondirectory file)))
  17376. ;; Emacs cannot overlay images from remote hosts.
  17377. ;; Create it in `temporary-file-directory' instead.
  17378. (if (file-remote-p file) temporary-file-directory
  17379. default-directory)
  17380. 'overlays msg 'forbuffer
  17381. org-latex-create-formula-image-program)))
  17382. ;; Work around a bug that doesn't restore window's start
  17383. ;; when widening back the buffer.
  17384. (set-window-start nil window-start)
  17385. (message (concat msg "done")))))))
  17386. (defun org-format-latex
  17387. (prefix &optional dir overlays msg forbuffer processing-type)
  17388. "Replace LaTeX fragments with links to an image, and produce images.
  17389. When optional argument OVERLAYS is non-nil, display the image on
  17390. top of the fragment instead of replacing it.
  17391. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17392. Some of the options can be changed using the variable
  17393. `org-format-latex-options', which see."
  17394. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17395. (unless (eq processing-type 'verbatim)
  17396. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17397. (cnt 0)
  17398. checkdir-flag)
  17399. (goto-char (point-min))
  17400. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17401. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17402. (overlay-recenter (point-max)))
  17403. (while (re-search-forward math-regexp nil t)
  17404. (unless (and overlays
  17405. (eq (get-char-property (point) 'org-overlay-type)
  17406. 'org-latex-overlay))
  17407. (let* ((context (org-element-context))
  17408. (type (org-element-type context)))
  17409. (when (memq type '(latex-environment latex-fragment))
  17410. (let ((block-type (eq type 'latex-environment))
  17411. (value (org-element-property :value context))
  17412. (beg (org-element-property :begin context))
  17413. (end (save-excursion
  17414. (goto-char (org-element-property :end context))
  17415. (skip-chars-backward " \r\t\n")
  17416. (point))))
  17417. (cl-case processing-type
  17418. (mathjax
  17419. ;; Prepare for MathJax processing.
  17420. (if (not (string-match "\\`\\$\\$?" value))
  17421. (goto-char end)
  17422. (delete-region beg end)
  17423. (if (string= (match-string 0 value) "$$")
  17424. (insert "\\[" (substring value 2 -2) "\\]")
  17425. (insert "\\(" (substring value 1 -1) "\\)"))))
  17426. ((dvipng imagemagick)
  17427. ;; Process to an image.
  17428. (cl-incf cnt)
  17429. (goto-char beg)
  17430. (let* ((face (face-at-point))
  17431. ;; Get the colors from the face at point.
  17432. (fg
  17433. (let ((color (plist-get org-format-latex-options
  17434. :foreground)))
  17435. (if (and forbuffer (eq color 'auto))
  17436. (face-attribute face :foreground nil 'default)
  17437. color)))
  17438. (bg
  17439. (let ((color (plist-get org-format-latex-options
  17440. :background)))
  17441. (if (and forbuffer (eq color 'auto))
  17442. (face-attribute face :background nil 'default)
  17443. color)))
  17444. (hash (sha1 (prin1-to-string
  17445. (list org-format-latex-header
  17446. org-latex-default-packages-alist
  17447. org-latex-packages-alist
  17448. org-format-latex-options
  17449. forbuffer value fg bg))))
  17450. (absprefix (expand-file-name prefix dir))
  17451. (linkfile (format "%s_%s.png" prefix hash))
  17452. (movefile (format "%s_%s.png" absprefix hash))
  17453. (sep (and block-type "\n\n"))
  17454. (link (concat sep "[[file:" linkfile "]]" sep))
  17455. (options
  17456. (org-combine-plists
  17457. org-format-latex-options
  17458. `(:foreground ,fg :background ,bg))))
  17459. (when msg (message msg cnt))
  17460. (unless checkdir-flag ; Ensure the directory exists.
  17461. (setq checkdir-flag t)
  17462. (let ((todir (file-name-directory absprefix)))
  17463. (unless (file-directory-p todir)
  17464. (make-directory todir t))))
  17465. (unless (file-exists-p movefile)
  17466. (org-create-formula-image
  17467. value movefile options forbuffer processing-type))
  17468. (if overlays
  17469. (progn
  17470. (dolist (o (overlays-in beg end))
  17471. (when (eq (overlay-get o 'org-overlay-type)
  17472. 'org-latex-overlay)
  17473. (delete-overlay o)))
  17474. (let ((ov (make-overlay beg end)))
  17475. (overlay-put ov
  17476. 'org-overlay-type
  17477. 'org-latex-overlay)
  17478. (overlay-put ov 'evaporate t)
  17479. (if (featurep 'xemacs)
  17480. (progn
  17481. (overlay-put ov 'invisible t)
  17482. (overlay-put
  17483. ov 'end-glyph
  17484. (make-glyph
  17485. (vector 'png :file movefile))))
  17486. (overlay-put
  17487. ov 'display
  17488. (list 'image
  17489. :type 'png
  17490. :file movefile
  17491. :ascent 'center)))
  17492. (push ov org-latex-fragment-image-overlays))
  17493. (goto-char end))
  17494. (delete-region beg end)
  17495. (insert
  17496. (org-add-props link
  17497. (list 'org-latex-src
  17498. (replace-regexp-in-string "\"" "" value)
  17499. 'org-latex-src-embed-type
  17500. (if block-type 'paragraph 'character)))))))
  17501. (mathml
  17502. ;; Process to MathML.
  17503. (unless (org-format-latex-mathml-available-p)
  17504. (user-error "LaTeX to MathML converter not configured"))
  17505. (cl-incf cnt)
  17506. (when msg (message msg cnt))
  17507. (goto-char beg)
  17508. (delete-region beg end)
  17509. (insert (org-format-latex-as-mathml
  17510. value block-type prefix dir)))
  17511. (otherwise
  17512. (error "Unknown conversion type %s for LaTeX fragments"
  17513. processing-type)))))))))))
  17514. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17515. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17516. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17517. sucessful, return the portion between \"<math...> </math>\"
  17518. elements otherwise return nil. When MATHML-FILE is specified,
  17519. write the results in to that file. When invoked as an
  17520. interactive command, prompt for LATEX-FRAG, with initial value
  17521. set to the current active region and echo the results for user
  17522. inspection."
  17523. (interactive (list (let ((frag (when (org-region-active-p)
  17524. (buffer-substring-no-properties
  17525. (region-beginning) (region-end)))))
  17526. (read-string "LaTeX Fragment: " frag nil frag))))
  17527. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17528. (let* ((tmp-in-file
  17529. (let ((file (file-relative-name
  17530. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17531. (write-region latex-frag nil file)
  17532. file))
  17533. (tmp-out-file (file-relative-name
  17534. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17535. (cmd (format-spec
  17536. org-latex-to-mathml-convert-command
  17537. `((?j . ,(and org-latex-to-mathml-jar-file
  17538. (shell-quote-argument
  17539. (expand-file-name
  17540. org-latex-to-mathml-jar-file))))
  17541. (?I . ,(shell-quote-argument tmp-in-file))
  17542. (?i . ,latex-frag)
  17543. (?o . ,(shell-quote-argument tmp-out-file)))))
  17544. mathml shell-command-output)
  17545. (when (org-called-interactively-p 'any)
  17546. (unless (org-format-latex-mathml-available-p)
  17547. (user-error "LaTeX to MathML converter not configured")))
  17548. (message "Running %s" cmd)
  17549. (setq shell-command-output (shell-command-to-string cmd))
  17550. (setq mathml
  17551. (when (file-readable-p tmp-out-file)
  17552. (with-current-buffer (find-file-noselect tmp-out-file t)
  17553. (goto-char (point-min))
  17554. (when (re-search-forward
  17555. (concat
  17556. (regexp-quote
  17557. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17558. "[^>]*?>"
  17559. "\\(.\\|\n\\)*"
  17560. "</math>")
  17561. nil t)
  17562. (prog1 (match-string 0) (kill-buffer))))))
  17563. (cond
  17564. (mathml
  17565. (setq mathml
  17566. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17567. (when mathml-file
  17568. (write-region mathml nil mathml-file))
  17569. (when (org-called-interactively-p 'any)
  17570. (message mathml)))
  17571. ((message "LaTeX to MathML conversion failed")
  17572. (message shell-command-output)))
  17573. (delete-file tmp-in-file)
  17574. (when (file-exists-p tmp-out-file)
  17575. (delete-file tmp-out-file))
  17576. mathml))
  17577. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17578. prefix &optional dir)
  17579. "Use `org-create-math-formula' but check local cache first."
  17580. (let* ((absprefix (expand-file-name prefix dir))
  17581. (print-length nil) (print-level nil)
  17582. (formula-id (concat
  17583. "formula-"
  17584. (sha1
  17585. (prin1-to-string
  17586. (list latex-frag
  17587. org-latex-to-mathml-convert-command)))))
  17588. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17589. (formula-cache-dir (file-name-directory formula-cache)))
  17590. (unless (file-directory-p formula-cache-dir)
  17591. (make-directory formula-cache-dir t))
  17592. (unless (file-exists-p formula-cache)
  17593. (org-create-math-formula latex-frag formula-cache))
  17594. (if (file-exists-p formula-cache)
  17595. ;; Successful conversion. Return the link to MathML file.
  17596. (org-add-props
  17597. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17598. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17599. 'org-latex-src-embed-type (if latex-frag-type
  17600. 'paragraph 'character)))
  17601. ;; Failed conversion. Return the LaTeX fragment verbatim
  17602. latex-frag)))
  17603. (defun org-create-formula-image (string tofile options buffer &optional type)
  17604. "Create an image from LaTeX source using dvipng or convert.
  17605. This function calls either `org-create-formula-image-with-dvipng'
  17606. or `org-create-formula-image-with-imagemagick' depending on the
  17607. value of `org-latex-create-formula-image-program' or on the value
  17608. of the optional TYPE variable.
  17609. Note: ultimately these two function should be combined as they
  17610. share a good deal of logic."
  17611. (org-check-external-command
  17612. "latex" "needed to convert LaTeX fragments to images")
  17613. (funcall
  17614. (cl-case (or type org-latex-create-formula-image-program)
  17615. (dvipng
  17616. (org-check-external-command
  17617. "dvipng" "needed to convert LaTeX fragments to images")
  17618. #'org-create-formula-image-with-dvipng)
  17619. (imagemagick
  17620. (org-check-external-command
  17621. "convert" "you need to install imagemagick")
  17622. #'org-create-formula-image-with-imagemagick)
  17623. (t (error
  17624. "Invalid value of `org-latex-create-formula-image-program'")))
  17625. string tofile options buffer))
  17626. (declare-function org-export-get-backend "ox" (name))
  17627. (declare-function org-export--get-global-options "ox" (&optional backend))
  17628. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17629. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17630. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17631. (defun org-create-formula--latex-header ()
  17632. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17633. (let ((info (org-combine-plists (org-export--get-global-options
  17634. (org-export-get-backend 'latex))
  17635. (org-export--get-inbuffer-options
  17636. (org-export-get-backend 'latex)))))
  17637. (org-latex-guess-babel-language
  17638. (org-latex-guess-inputenc
  17639. (org-splice-latex-header
  17640. org-format-latex-header
  17641. org-latex-default-packages-alist
  17642. org-latex-packages-alist t
  17643. (plist-get info :latex-header)))
  17644. info)))
  17645. (defun org--get-display-dpi ()
  17646. "Get the DPI of the display.
  17647. Assumes that the display has the same pixel width in the
  17648. horizontal and vertical directions."
  17649. (if (display-graphic-p)
  17650. (round (/ (display-pixel-height)
  17651. (/ (display-mm-height) 25.4)))
  17652. (error "Attempt to calculate the dpi of a non-graphic display")))
  17653. ;; This function borrows from Ganesh Swami's latex2png.el
  17654. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17655. "This calls dvipng."
  17656. (require 'ox-latex)
  17657. (let* ((tmpdir (if (featurep 'xemacs)
  17658. (temp-directory)
  17659. temporary-file-directory))
  17660. (texfilebase (make-temp-name
  17661. (expand-file-name "orgtex" tmpdir)))
  17662. (texfile (concat texfilebase ".tex"))
  17663. (dvifile (concat texfilebase ".dvi"))
  17664. (pngfile (concat texfilebase ".png"))
  17665. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17666. ;; This assumes that the display has the same pixel width in
  17667. ;; the horizontal and vertical directions
  17668. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17669. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17670. "Black"))
  17671. (bg (or (plist-get options (if buffer :background :html-background))
  17672. "Transparent")))
  17673. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17674. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17675. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17676. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17677. (let ((latex-header (org-create-formula--latex-header)))
  17678. (with-temp-file texfile
  17679. (insert latex-header)
  17680. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17681. (let ((dir default-directory))
  17682. (ignore-errors
  17683. (cd tmpdir)
  17684. (call-process "latex" nil nil nil texfile))
  17685. (cd dir))
  17686. (if (not (file-exists-p dvifile))
  17687. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17688. (ignore-errors
  17689. (if (featurep 'xemacs)
  17690. (call-process "dvipng" nil nil nil
  17691. "-fg" fg "-bg" bg
  17692. "-T" "tight"
  17693. "-o" pngfile
  17694. dvifile)
  17695. (call-process "dvipng" nil nil nil
  17696. "-fg" fg "-bg" bg
  17697. "-D" dpi
  17698. ;;"-x" scale "-y" scale
  17699. "-T" "tight"
  17700. "-o" pngfile
  17701. dvifile)))
  17702. (if (not (file-exists-p pngfile))
  17703. (if org-format-latex-signal-error
  17704. (error "Failed to create png file from %s" texfile)
  17705. (message "Failed to create png file from %s" texfile)
  17706. nil)
  17707. ;; Use the requested file name and clean up
  17708. (copy-file pngfile tofile 'replace)
  17709. (dolist (e '(".dvi" ".tex" ".aux" ".log" ".png" ".out"))
  17710. (when (file-exists-p (concat texfilebase e))
  17711. (delete-file (concat texfilebase e))))
  17712. pngfile))))
  17713. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17714. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17715. "This calls convert, which is included into imagemagick."
  17716. (require 'ox-latex)
  17717. (let* ((tmpdir (if (featurep 'xemacs)
  17718. (temp-directory)
  17719. temporary-file-directory))
  17720. (texfilebase (make-temp-name
  17721. (expand-file-name "orgtex" tmpdir)))
  17722. (texfile (concat texfilebase ".tex"))
  17723. (pdffile (concat texfilebase ".pdf"))
  17724. (pngfile (concat texfilebase ".png"))
  17725. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17726. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17727. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17728. "black"))
  17729. (bg (or (plist-get options (if buffer :background :html-background))
  17730. "white")))
  17731. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17732. (setq fg (org-latex-color-format fg)))
  17733. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17734. (setq bg (org-latex-color-format
  17735. (if (string= bg "Transparent") "white" bg))))
  17736. (let ((latex-header (org-create-formula--latex-header)))
  17737. (with-temp-file texfile
  17738. (insert latex-header)
  17739. (insert "\n\\begin{document}\n"
  17740. "\\definecolor{fg}{rgb}{" fg "}\n"
  17741. "\\definecolor{bg}{rgb}{" bg "}\n"
  17742. "\n\\pagecolor{bg}\n"
  17743. "\n{\\color{fg}\n"
  17744. string
  17745. "\n}\n"
  17746. "\n\\end{document}\n")))
  17747. (org-latex-compile texfile t)
  17748. (if (not (file-exists-p pdffile))
  17749. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17750. (ignore-errors
  17751. (if (featurep 'xemacs)
  17752. (call-process "convert" nil nil nil
  17753. "-density" "96"
  17754. "-trim"
  17755. "-antialias"
  17756. pdffile
  17757. "-quality" "100"
  17758. ;; "-sharpen" "0x1.0"
  17759. pngfile)
  17760. (call-process "convert" nil nil nil
  17761. "-density" dpi
  17762. "-trim"
  17763. "-antialias"
  17764. pdffile
  17765. "-quality" "100"
  17766. ;; "-sharpen" "0x1.0"
  17767. pngfile)))
  17768. (if (not (file-exists-p pngfile))
  17769. (if org-format-latex-signal-error
  17770. (error "Failed to create png file from %s" texfile)
  17771. (message "Failed to create png file from %s" texfile)
  17772. nil)
  17773. ;; Use the requested file name and clean up
  17774. (copy-file pngfile tofile 'replace)
  17775. (dolist (e '(".pdf" ".tex" ".aux" ".log" ".png"))
  17776. (when (file-exists-p (concat texfilebase e))
  17777. (delete-file (concat texfilebase e))))
  17778. pngfile))))
  17779. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17780. "Fill a LaTeX header template TPL.
  17781. In the template, the following place holders will be recognized:
  17782. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17783. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17784. [PACKAGES] \\usepackage statements for PKG
  17785. [NO-PACKAGES] do not include PKG
  17786. [EXTRA] the string EXTRA
  17787. [NO-EXTRA] do not include EXTRA
  17788. For backward compatibility, if both the positive and the negative place
  17789. holder is missing, the positive one (without the \"NO-\") will be
  17790. assumed to be present at the end of the template.
  17791. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17792. EXTRA is a string.
  17793. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17794. (let (rpl (end ""))
  17795. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17796. (setq rpl (if (or (match-end 1) (not def-pkg))
  17797. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17798. tpl (replace-match rpl t t tpl))
  17799. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17800. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17801. (setq rpl (if (or (match-end 1) (not pkg))
  17802. "" (org-latex-packages-to-string pkg snippets-p t))
  17803. tpl (replace-match rpl t t tpl))
  17804. (when pkg (setq end
  17805. (concat end "\n"
  17806. (org-latex-packages-to-string pkg snippets-p)))))
  17807. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17808. (setq rpl (if (or (match-end 1) (not extra))
  17809. "" (concat extra "\n"))
  17810. tpl (replace-match rpl t t tpl))
  17811. (when (and extra (string-match "\\S-" extra))
  17812. (setq end (concat end "\n" extra))))
  17813. (if (string-match "\\S-" end)
  17814. (concat tpl "\n" end)
  17815. tpl)))
  17816. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17817. "Turn an alist of packages into a string with the \\usepackage macros."
  17818. (setq pkg (mapconcat (lambda(p)
  17819. (cond
  17820. ((stringp p) p)
  17821. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17822. (format "%% Package %s omitted" (cadr p)))
  17823. ((equal "" (car p))
  17824. (format "\\usepackage{%s}" (cadr p)))
  17825. (t
  17826. (format "\\usepackage[%s]{%s}"
  17827. (car p) (cadr p)))))
  17828. pkg
  17829. "\n"))
  17830. (if newline (concat pkg "\n") pkg))
  17831. (defun org-dvipng-color (attr)
  17832. "Return a RGB color specification for dvipng."
  17833. (apply 'format "rgb %s %s %s"
  17834. (mapcar 'org-normalize-color
  17835. (if (featurep 'xemacs)
  17836. (color-rgb-components
  17837. (face-property 'default
  17838. (cond ((eq attr :foreground) 'foreground)
  17839. ((eq attr :background) 'background))))
  17840. (color-values (face-attribute 'default attr nil))))))
  17841. (defun org-dvipng-color-format (color-name)
  17842. "Convert COLOR-NAME to a RGB color value for dvipng."
  17843. (apply 'format "rgb %s %s %s"
  17844. (mapcar 'org-normalize-color
  17845. (color-values color-name))))
  17846. (defun org-latex-color (attr)
  17847. "Return a RGB color for the LaTeX color package."
  17848. (apply 'format "%s,%s,%s"
  17849. (mapcar 'org-normalize-color
  17850. (if (featurep 'xemacs)
  17851. (color-rgb-components
  17852. (face-property 'default
  17853. (cond ((eq attr :foreground) 'foreground)
  17854. ((eq attr :background) 'background))))
  17855. (color-values (face-attribute 'default attr nil))))))
  17856. (defun org-latex-color-format (color-name)
  17857. "Convert COLOR-NAME to a RGB color value."
  17858. (apply 'format "%s,%s,%s"
  17859. (mapcar 'org-normalize-color
  17860. (color-values color-name))))
  17861. (defun org-normalize-color (value)
  17862. "Return string to be used as color value for an RGB component."
  17863. (format "%g" (/ value 65535.0)))
  17864. ;; Image display
  17865. (defvar-local org-inline-image-overlays nil)
  17866. (defun org-toggle-inline-images (&optional include-linked)
  17867. "Toggle the display of inline images.
  17868. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17869. (interactive "P")
  17870. (if org-inline-image-overlays
  17871. (progn
  17872. (org-remove-inline-images)
  17873. (when (org-called-interactively-p 'interactive)
  17874. (message "Inline image display turned off")))
  17875. (org-display-inline-images include-linked)
  17876. (when (org-called-interactively-p 'interactive)
  17877. (message (if org-inline-image-overlays
  17878. (format "%d images displayed inline"
  17879. (length org-inline-image-overlays))
  17880. "No images to display inline")))))
  17881. (defun org-redisplay-inline-images ()
  17882. "Refresh the display of inline images."
  17883. (interactive)
  17884. (if (not org-inline-image-overlays)
  17885. (org-toggle-inline-images)
  17886. (org-toggle-inline-images)
  17887. (org-toggle-inline-images)))
  17888. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17889. "Display inline images.
  17890. An inline image is a link which follows either of these
  17891. conventions:
  17892. 1. Its path is a file with an extension matching return value
  17893. from `image-file-name-regexp' and it has no contents.
  17894. 2. Its description consists in a single link of the previous
  17895. type.
  17896. When optional argument INCLUDE-LINKED is non-nil, also links with
  17897. a text description part will be inlined. This can be nice for
  17898. a quick look at those images, but it does not reflect what
  17899. exported files will look like.
  17900. When optional argument REFRESH is non-nil, refresh existing
  17901. images between BEG and END. This will create new image displays
  17902. only if necessary. BEG and END default to the buffer
  17903. boundaries."
  17904. (interactive "P")
  17905. (when (display-graphic-p)
  17906. (unless refresh
  17907. (org-remove-inline-images)
  17908. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17909. (org-with-wide-buffer
  17910. (goto-char (or beg (point-min)))
  17911. (let ((case-fold-search t)
  17912. (file-extension-re (image-file-name-regexp)))
  17913. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17914. (let ((link (save-match-data (org-element-context))))
  17915. ;; Check if we're at an inline image.
  17916. (when (and (equal (org-element-property :type link) "file")
  17917. (or include-linked
  17918. (not (org-element-property :contents-begin link)))
  17919. (let ((parent (org-element-property :parent link)))
  17920. (or (not (eq (org-element-type parent) 'link))
  17921. (not (cdr (org-element-contents parent)))))
  17922. (org-string-match-p file-extension-re
  17923. (org-element-property :path link)))
  17924. (let ((file (expand-file-name
  17925. (org-link-unescape
  17926. (org-element-property :path link)))))
  17927. (when (file-exists-p file)
  17928. (let ((width
  17929. ;; Apply `org-image-actual-width' specifications.
  17930. (cond
  17931. ((not (image-type-available-p 'imagemagick)) nil)
  17932. ((eq org-image-actual-width t) nil)
  17933. ((listp org-image-actual-width)
  17934. (or
  17935. ;; First try to find a width among
  17936. ;; attributes associated to the paragraph
  17937. ;; containing link.
  17938. (let ((paragraph
  17939. (let ((e link))
  17940. (while (and (setq e (org-element-property
  17941. :parent e))
  17942. (not (eq (org-element-type e)
  17943. 'paragraph))))
  17944. e)))
  17945. (when paragraph
  17946. (save-excursion
  17947. (goto-char (org-element-property :begin paragraph))
  17948. (when
  17949. (re-search-forward
  17950. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17951. (org-element-property
  17952. :post-affiliated paragraph)
  17953. t)
  17954. (string-to-number (match-string 1))))))
  17955. ;; Otherwise, fall-back to provided number.
  17956. (car org-image-actual-width)))
  17957. ((numberp org-image-actual-width)
  17958. org-image-actual-width)))
  17959. (old (get-char-property-and-overlay
  17960. (org-element-property :begin link)
  17961. 'org-image-overlay)))
  17962. (if (and (car-safe old) refresh)
  17963. (image-refresh (overlay-get (cdr old) 'display))
  17964. (let ((image (create-image file
  17965. (and width 'imagemagick)
  17966. nil
  17967. :width width)))
  17968. (when image
  17969. (let* ((link
  17970. ;; If inline image is the description
  17971. ;; of another link, be sure to
  17972. ;; consider the latter as the one to
  17973. ;; apply the overlay on.
  17974. (let ((parent
  17975. (org-element-property :parent link)))
  17976. (if (eq (org-element-type parent) 'link)
  17977. parent
  17978. link)))
  17979. (ov (make-overlay
  17980. (org-element-property :begin link)
  17981. (progn
  17982. (goto-char
  17983. (org-element-property :end link))
  17984. (skip-chars-backward " \t")
  17985. (point)))))
  17986. (overlay-put ov 'display image)
  17987. (overlay-put ov 'face 'default)
  17988. (overlay-put ov 'org-image-overlay t)
  17989. (overlay-put
  17990. ov 'modification-hooks
  17991. (list 'org-display-inline-remove-overlay))
  17992. (push ov org-inline-image-overlays)))))))))))))))
  17993. (define-obsolete-function-alias
  17994. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17995. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  17996. "Remove inline-display overlay if a corresponding region is modified."
  17997. (let ((inhibit-modification-hooks t))
  17998. (when (and ov after)
  17999. (delete ov org-inline-image-overlays)
  18000. (delete-overlay ov))))
  18001. (defun org-remove-inline-images ()
  18002. "Remove inline display of images."
  18003. (interactive)
  18004. (mapc #'delete-overlay org-inline-image-overlays)
  18005. (setq org-inline-image-overlays nil))
  18006. ;;;; Key bindings
  18007. ;; Outline functions from `outline-mode-prefix-map'
  18008. ;; that can be remapped in Org:
  18009. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18010. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18011. (define-key org-mode-map [remap outline-forward-same-level]
  18012. 'org-forward-heading-same-level)
  18013. (define-key org-mode-map [remap outline-backward-same-level]
  18014. 'org-backward-heading-same-level)
  18015. (define-key org-mode-map [remap outline-show-branches]
  18016. 'org-kill-note-or-show-branches)
  18017. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18018. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18019. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18020. (define-key org-mode-map [remap outline-next-visible-heading]
  18021. 'org-next-visible-heading)
  18022. (define-key org-mode-map [remap outline-previous-visible-heading]
  18023. 'org-previous-visible-heading)
  18024. (define-key org-mode-map [remap show-children] 'org-show-children)
  18025. ;; Outline functions from `outline-mode-prefix-map' that can not
  18026. ;; be remapped in Org:
  18027. ;; - the column "key binding" shows whether the Outline function is still
  18028. ;; available in Org mode on the same key that it has been bound to in
  18029. ;; Outline mode:
  18030. ;; - "overridden": key used for a different functionality in Org mode
  18031. ;; - else: key still bound to the same Outline function in Org mode
  18032. ;; | Outline function | key binding | Org replacement |
  18033. ;; |------------------------------------+-------------+--------------------------|
  18034. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18035. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18036. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18037. ;; | `show-entry' | overridden | no replacement |
  18038. ;; | `show-branches' | `C-c C-k' | still same function |
  18039. ;; | `show-subtree' | overridden | visibility cycling |
  18040. ;; | `show-all' | overridden | no replacement |
  18041. ;; | `hide-subtree' | overridden | visibility cycling |
  18042. ;; | `hide-body' | overridden | no replacement |
  18043. ;; | `hide-entry' | overridden | visibility cycling |
  18044. ;; | `hide-leaves' | overridden | no replacement |
  18045. ;; | `hide-sublevels' | overridden | no replacement |
  18046. ;; | `hide-other' | overridden | no replacement |
  18047. ;; Make `C-c C-x' a prefix key
  18048. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18049. ;; TAB key with modifiers
  18050. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18051. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18052. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18053. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18054. ;; The following line is necessary under Suse GNU/Linux
  18055. (unless (featurep 'xemacs)
  18056. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18057. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18058. (define-key org-mode-map [backtab] 'org-shifttab)
  18059. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18060. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18061. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18062. ;; Cursor keys with modifiers
  18063. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18064. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18065. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18066. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18067. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18068. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18069. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18070. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18071. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18072. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18073. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18074. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18075. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18076. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18077. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18078. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18079. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18080. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18081. ;; Babel keys
  18082. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18083. (dolist (pair org-babel-key-bindings)
  18084. (define-key org-babel-map (car pair) (cdr pair)))
  18085. ;;; Extra keys for tty access.
  18086. ;; We only set them when really needed because otherwise the
  18087. ;; menus don't show the simple keys
  18088. (when (or org-use-extra-keys
  18089. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18090. (not window-system))
  18091. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18092. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18093. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18094. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18095. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18096. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18097. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18098. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18099. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18100. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18101. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18102. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18103. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18104. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18105. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18106. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18107. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18108. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18109. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18110. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18111. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18112. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18113. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18114. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18115. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18116. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18117. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18118. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18119. ;; All the other keys
  18120. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18121. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18122. (if (boundp 'narrow-map)
  18123. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18124. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18125. (if (boundp 'narrow-map)
  18126. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18127. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18128. (if (boundp 'narrow-map)
  18129. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18130. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18131. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18132. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18133. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18134. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18135. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18136. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18137. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18138. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18139. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18140. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18141. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18142. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18143. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18144. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18145. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18146. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18147. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18148. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18149. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18150. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18151. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18152. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18153. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18154. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18155. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18156. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18157. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18158. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18159. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18160. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18161. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18162. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18163. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18164. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18165. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18166. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18167. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18168. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18169. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18170. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18171. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18172. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18173. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18174. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18175. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18176. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18177. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18178. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18179. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18180. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18181. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18182. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18183. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18184. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18185. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18186. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18187. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18188. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18189. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18190. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18191. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18192. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18193. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18194. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18195. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18196. (org-defkey org-mode-map "\C-m" 'org-return)
  18197. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18198. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18199. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18200. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18201. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18202. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18203. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18204. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18205. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18206. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18207. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18208. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18209. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18210. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18211. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18212. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18213. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18214. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18215. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18216. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18217. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18218. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18219. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18220. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18221. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18222. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18223. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18224. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18225. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18226. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18227. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18228. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18229. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18230. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18231. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18232. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18233. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18234. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18235. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18236. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18237. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18238. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18239. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18240. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18241. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18242. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18243. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18244. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18245. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18246. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18247. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18248. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18249. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18250. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18251. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18252. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18253. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18254. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18255. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18256. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18257. (when (featurep 'xemacs)
  18258. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18259. (defconst org-speed-commands-default
  18260. '(
  18261. ("Outline Navigation")
  18262. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18263. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18264. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18265. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18266. ("F" . org-next-block)
  18267. ("B" . org-previous-block)
  18268. ("u" . (org-speed-move-safe 'outline-up-heading))
  18269. ("j" . org-goto)
  18270. ("g" . (org-refile t))
  18271. ("Outline Visibility")
  18272. ("c" . org-cycle)
  18273. ("C" . org-shifttab)
  18274. (" " . org-display-outline-path)
  18275. ("s" . org-narrow-to-subtree)
  18276. ("=" . org-columns)
  18277. ("Outline Structure Editing")
  18278. ("U" . org-metaup)
  18279. ("D" . org-metadown)
  18280. ("r" . org-metaright)
  18281. ("l" . org-metaleft)
  18282. ("R" . org-shiftmetaright)
  18283. ("L" . org-shiftmetaleft)
  18284. ("i" . (progn (forward-char 1) (call-interactively
  18285. 'org-insert-heading-respect-content)))
  18286. ("^" . org-sort)
  18287. ("w" . org-refile)
  18288. ("a" . org-archive-subtree-default-with-confirmation)
  18289. ("@" . org-mark-subtree)
  18290. ("#" . org-toggle-comment)
  18291. ("Clock Commands")
  18292. ("I" . org-clock-in)
  18293. ("O" . org-clock-out)
  18294. ("Meta Data Editing")
  18295. ("t" . org-todo)
  18296. ("," . (org-priority))
  18297. ("0" . (org-priority ?\ ))
  18298. ("1" . (org-priority ?A))
  18299. ("2" . (org-priority ?B))
  18300. ("3" . (org-priority ?C))
  18301. (":" . org-set-tags-command)
  18302. ("e" . org-set-effort)
  18303. ("E" . org-inc-effort)
  18304. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18305. (org-entry-put (point) "APPT_WARNTIME" m)))
  18306. ("Agenda Views etc")
  18307. ("v" . org-agenda)
  18308. ("/" . org-sparse-tree)
  18309. ("Misc")
  18310. ("o" . org-open-at-point)
  18311. ("?" . org-speed-command-help)
  18312. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18313. (">" . (org-agenda-remove-restriction-lock))
  18314. )
  18315. "The default speed commands.")
  18316. (defun org-print-speed-command (e)
  18317. (if (> (length (car e)) 1)
  18318. (progn
  18319. (princ "\n")
  18320. (princ (car e))
  18321. (princ "\n")
  18322. (princ (make-string (length (car e)) ?-))
  18323. (princ "\n"))
  18324. (princ (car e))
  18325. (princ " ")
  18326. (if (symbolp (cdr e))
  18327. (princ (symbol-name (cdr e)))
  18328. (prin1 (cdr e)))
  18329. (princ "\n")))
  18330. (defun org-speed-command-help ()
  18331. "Show the available speed commands."
  18332. (interactive)
  18333. (if (not org-use-speed-commands)
  18334. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18335. (with-output-to-temp-buffer "*Help*"
  18336. (princ "User-defined Speed commands\n===========================\n")
  18337. (mapc #'org-print-speed-command org-speed-commands-user)
  18338. (princ "\n")
  18339. (princ "Built-in Speed commands\n=======================\n")
  18340. (mapc #'org-print-speed-command org-speed-commands-default))
  18341. (with-current-buffer "*Help*"
  18342. (setq truncate-lines t))))
  18343. (defun org-speed-move-safe (cmd)
  18344. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18345. If not, return to the original position and throw an error."
  18346. (interactive)
  18347. (let ((pos (point)))
  18348. (call-interactively cmd)
  18349. (unless (and (bolp) (org-at-heading-p))
  18350. (goto-char pos)
  18351. (error "Boundary reached while executing %s" cmd))))
  18352. (defvar org-self-insert-command-undo-counter 0)
  18353. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18354. (defvar org-speed-command nil)
  18355. (define-obsolete-function-alias
  18356. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18357. (defun org-speed-command-activate (keys)
  18358. "Hook for activating single-letter speed commands.
  18359. `org-speed-commands-default' specifies a minimal command set.
  18360. Use `org-speed-commands-user' for further customization."
  18361. (when (or (and (bolp) (looking-at org-outline-regexp))
  18362. (and (functionp org-use-speed-commands)
  18363. (funcall org-use-speed-commands)))
  18364. (cdr (assoc keys (append org-speed-commands-user
  18365. org-speed-commands-default)))))
  18366. (define-obsolete-function-alias
  18367. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18368. (defun org-babel-speed-command-activate (keys)
  18369. "Hook for activating single-letter code block commands."
  18370. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18371. (cdr (assoc keys org-babel-key-bindings))))
  18372. (defcustom org-speed-command-hook
  18373. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18374. "Hook for activating speed commands at strategic locations.
  18375. Hook functions are called in sequence until a valid handler is
  18376. found.
  18377. Each hook takes a single argument, a user-pressed command key
  18378. which is also a `self-insert-command' from the global map.
  18379. Within the hook, examine the cursor position and the command key
  18380. and return nil or a valid handler as appropriate. Handler could
  18381. be one of an interactive command, a function, or a form.
  18382. Set `org-use-speed-commands' to non-nil value to enable this
  18383. hook. The default setting is `org-speed-command-activate'."
  18384. :group 'org-structure
  18385. :version "24.1"
  18386. :type 'hook)
  18387. (defun org-self-insert-command (N)
  18388. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18389. If the cursor is in a table looking at whitespace, the whitespace is
  18390. overwritten, and the table is not marked as requiring realignment."
  18391. (interactive "p")
  18392. (org-check-before-invisible-edit 'insert)
  18393. (cond
  18394. ((and org-use-speed-commands
  18395. (let ((kv (this-command-keys-vector)))
  18396. (setq org-speed-command
  18397. (run-hook-with-args-until-success
  18398. 'org-speed-command-hook
  18399. (make-string 1 (aref kv (1- (length kv))))))))
  18400. (cond
  18401. ((commandp org-speed-command)
  18402. (setq this-command org-speed-command)
  18403. (call-interactively org-speed-command))
  18404. ((functionp org-speed-command)
  18405. (funcall org-speed-command))
  18406. ((and org-speed-command (listp org-speed-command))
  18407. (eval org-speed-command))
  18408. (t (let (org-use-speed-commands)
  18409. (call-interactively 'org-self-insert-command)))))
  18410. ((and
  18411. (org-at-table-p)
  18412. (progn
  18413. ;; Check if we blank the field, and if that triggers align.
  18414. (and (featurep 'org-table) org-table-auto-blank-field
  18415. (memq last-command
  18416. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18417. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18418. ;; Got extra space, this field does not determine
  18419. ;; column width.
  18420. (let (org-table-may-need-update) (org-table-blank-field))
  18421. ;; No extra space, this field may determine column
  18422. ;; width.
  18423. (org-table-blank-field)))
  18424. t)
  18425. (eq N 1)
  18426. (looking-at "[^|\n]* \\( \\)|"))
  18427. ;; There is room for insertion without re-aligning the table.
  18428. (delete-region (match-beginning 1) (match-end 1))
  18429. (self-insert-command N))
  18430. (t
  18431. (setq org-table-may-need-update t)
  18432. (self-insert-command N)
  18433. (org-fix-tags-on-the-fly)
  18434. (when org-self-insert-cluster-for-undo
  18435. (if (not (eq last-command 'org-self-insert-command))
  18436. (setq org-self-insert-command-undo-counter 1)
  18437. (if (>= org-self-insert-command-undo-counter 20)
  18438. (setq org-self-insert-command-undo-counter 1)
  18439. (and (> org-self-insert-command-undo-counter 0)
  18440. buffer-undo-list (listp buffer-undo-list)
  18441. (not (cadr buffer-undo-list)) ; remove nil entry
  18442. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18443. (setq org-self-insert-command-undo-counter
  18444. (1+ org-self-insert-command-undo-counter))))))))
  18445. (defun org-check-before-invisible-edit (kind)
  18446. "Check is editing if kind KIND would be dangerous with invisible text around.
  18447. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18448. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18449. (when (and org-catch-invisible-edits
  18450. (or (not (boundp 'visible-mode)) (not visible-mode))
  18451. (or (get-char-property (point) 'invisible)
  18452. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18453. ;; OK, we need to take a closer look
  18454. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18455. (invisible-before-point (unless (bobp) (get-char-property
  18456. (1- (point)) 'invisible)))
  18457. (border-and-ok-direction
  18458. (or
  18459. ;; Check if we are acting predictably before invisible text
  18460. (and invisible-at-point (not invisible-before-point)
  18461. (memq kind '(insert delete-backward)))
  18462. ;; Check if we are acting predictably after invisible text
  18463. ;; This works not well, and I have turned it off. It seems
  18464. ;; better to always show and stop after invisible text.
  18465. ;; (and (not invisible-at-point) invisible-before-point
  18466. ;; (memq kind '(insert delete)))
  18467. )))
  18468. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18469. (memq invisible-before-point '(outline org-hide-block t)))
  18470. (when (eq org-catch-invisible-edits 'error)
  18471. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18472. (if (and org-custom-properties-overlays
  18473. (y-or-n-p "Display invisible properties in this buffer? "))
  18474. (org-toggle-custom-properties-visibility)
  18475. ;; Make the area visible
  18476. (save-excursion
  18477. (when invisible-before-point
  18478. (goto-char (previous-single-char-property-change
  18479. (point) 'invisible)))
  18480. (outline-show-subtree))
  18481. (cond
  18482. ((eq org-catch-invisible-edits 'show)
  18483. ;; That's it, we do the edit after showing
  18484. (message
  18485. "Unfolding invisible region around point before editing")
  18486. (sit-for 1))
  18487. ((and (eq org-catch-invisible-edits 'smart)
  18488. border-and-ok-direction)
  18489. (message "Unfolding invisible region around point before editing"))
  18490. (t
  18491. ;; Don't do the edit, make the user repeat it in full visibility
  18492. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18493. (defun org-fix-tags-on-the-fly ()
  18494. (when (and (equal (char-after (point-at-bol)) ?*)
  18495. (org-at-heading-p))
  18496. (org-align-tags-here org-tags-column)))
  18497. (defun org-delete-backward-char (N)
  18498. "Like `delete-backward-char', insert whitespace at field end in tables.
  18499. When deleting backwards, in tables this function will insert whitespace in
  18500. front of the next \"|\" separator, to keep the table aligned. The table will
  18501. still be marked for re-alignment if the field did fill the entire column,
  18502. because, in this case the deletion might narrow the column."
  18503. (interactive "p")
  18504. (save-match-data
  18505. (org-check-before-invisible-edit 'delete-backward)
  18506. (if (and (org-at-table-p)
  18507. (eq N 1)
  18508. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18509. (looking-at ".*?|"))
  18510. (let ((pos (point))
  18511. (noalign (looking-at "[^|\n\r]* |"))
  18512. (c org-table-may-need-update))
  18513. (backward-delete-char N)
  18514. (unless overwrite-mode
  18515. (skip-chars-forward "^|")
  18516. (insert " ")
  18517. (goto-char (1- pos)))
  18518. ;; noalign: if there were two spaces at the end, this field
  18519. ;; does not determine the width of the column.
  18520. (when noalign (setq org-table-may-need-update c)))
  18521. (backward-delete-char N)
  18522. (org-fix-tags-on-the-fly))))
  18523. (defun org-delete-char (N)
  18524. "Like `delete-char', but insert whitespace at field end in tables.
  18525. When deleting characters, in tables this function will insert whitespace in
  18526. front of the next \"|\" separator, to keep the table aligned. The table will
  18527. still be marked for re-alignment if the field did fill the entire column,
  18528. because, in this case the deletion might narrow the column."
  18529. (interactive "p")
  18530. (save-match-data
  18531. (org-check-before-invisible-edit 'delete)
  18532. (if (and (org-at-table-p)
  18533. (not (bolp))
  18534. (not (= (char-after) ?|))
  18535. (eq N 1))
  18536. (if (looking-at ".*?|")
  18537. (let ((pos (point))
  18538. (noalign (looking-at "[^|\n\r]* |"))
  18539. (c org-table-may-need-update))
  18540. (replace-match
  18541. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18542. (goto-char pos)
  18543. ;; noalign: if there were two spaces at the end, this field
  18544. ;; does not determine the width of the column.
  18545. (when noalign (setq org-table-may-need-update c)))
  18546. (delete-char N))
  18547. (delete-char N)
  18548. (org-fix-tags-on-the-fly))))
  18549. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18550. (put 'org-self-insert-command 'delete-selection
  18551. (lambda ()
  18552. (not (run-hook-with-args-until-success
  18553. 'self-insert-uses-region-functions))))
  18554. (put 'orgtbl-self-insert-command 'delete-selection
  18555. (lambda ()
  18556. (not (run-hook-with-args-until-success
  18557. 'self-insert-uses-region-functions))))
  18558. (put 'org-delete-char 'delete-selection 'supersede)
  18559. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18560. (put 'org-yank 'delete-selection 'yank)
  18561. ;; Make `flyspell-mode' delay after some commands
  18562. (put 'org-self-insert-command 'flyspell-delayed t)
  18563. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18564. (put 'org-delete-char 'flyspell-delayed t)
  18565. (put 'org-delete-backward-char 'flyspell-delayed t)
  18566. ;; Make pabbrev-mode expand after org-mode commands
  18567. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18568. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18569. (defun org-remap (map &rest commands)
  18570. "In MAP, remap the functions given in COMMANDS.
  18571. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18572. (let (new old)
  18573. (while commands
  18574. (setq old (pop commands) new (pop commands))
  18575. (if (fboundp 'command-remapping)
  18576. (org-defkey map (vector 'remap old) new)
  18577. (substitute-key-definition old new map global-map)))))
  18578. (defun org-transpose-words ()
  18579. "Transpose words for Org.
  18580. This uses the `org-mode-transpose-word-syntax-table' syntax
  18581. table, which interprets characters in `org-emphasis-alist' as
  18582. word constituents."
  18583. (interactive)
  18584. (with-syntax-table org-mode-transpose-word-syntax-table
  18585. (call-interactively 'transpose-words)))
  18586. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18587. (when (eq org-enable-table-editor 'optimized)
  18588. ;; If the user wants maximum table support, we need to hijack
  18589. ;; some standard editing functions
  18590. (org-remap org-mode-map
  18591. 'self-insert-command 'org-self-insert-command
  18592. 'delete-char 'org-delete-char
  18593. 'delete-backward-char 'org-delete-backward-char)
  18594. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18595. (defvar org-ctrl-c-ctrl-c-hook nil
  18596. "Hook for functions attaching themselves to `C-c C-c'.
  18597. This can be used to add additional functionality to the C-c C-c
  18598. key which executes context-dependent commands. This hook is run
  18599. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18600. run after the last test.
  18601. Each function will be called with no arguments. The function
  18602. must check if the context is appropriate for it to act. If yes,
  18603. it should do its thing and then return a non-nil value. If the
  18604. context is wrong, just do nothing and return nil.")
  18605. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18606. "Hook for functions attaching themselves to `C-c C-c'.
  18607. This can be used to add additional functionality to the C-c C-c
  18608. key which executes context-dependent commands. This hook is run
  18609. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18610. before the first test.
  18611. Each function will be called with no arguments. The function
  18612. must check if the context is appropriate for it to act. If yes,
  18613. it should do its thing and then return a non-nil value. If the
  18614. context is wrong, just do nothing and return nil.")
  18615. (defvar org-tab-first-hook nil
  18616. "Hook for functions to attach themselves to TAB.
  18617. See `org-ctrl-c-ctrl-c-hook' for more information.
  18618. This hook runs as the first action when TAB is pressed, even before
  18619. `org-cycle' messes around with the `outline-regexp' to cater for
  18620. inline tasks and plain list item folding.
  18621. If any function in this hook returns t, any other actions that
  18622. would have been caused by TAB (such as table field motion or visibility
  18623. cycling) will not occur.")
  18624. (defvar org-tab-after-check-for-table-hook nil
  18625. "Hook for functions to attach themselves to TAB.
  18626. See `org-ctrl-c-ctrl-c-hook' for more information.
  18627. This hook runs after it has been established that the cursor is not in a
  18628. table, but before checking if the cursor is in a headline or if global cycling
  18629. should be done.
  18630. If any function in this hook returns t, not other actions like visibility
  18631. cycling will be done.")
  18632. (defvar org-tab-after-check-for-cycling-hook nil
  18633. "Hook for functions to attach themselves to TAB.
  18634. See `org-ctrl-c-ctrl-c-hook' for more information.
  18635. This hook runs after it has been established that not table field motion and
  18636. not visibility should be done because of current context. This is probably
  18637. the place where a package like yasnippets can hook in.")
  18638. (defvar org-tab-before-tab-emulation-hook nil
  18639. "Hook for functions to attach themselves to TAB.
  18640. See `org-ctrl-c-ctrl-c-hook' for more information.
  18641. This hook runs after every other options for TAB have been exhausted, but
  18642. before indentation and \t insertion takes place.")
  18643. (defvar org-metaleft-hook nil
  18644. "Hook for functions attaching themselves to `M-left'.
  18645. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18646. (defvar org-metaright-hook nil
  18647. "Hook for functions attaching themselves to `M-right'.
  18648. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18649. (defvar org-metaup-hook nil
  18650. "Hook for functions attaching themselves to `M-up'.
  18651. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18652. (defvar org-metadown-hook nil
  18653. "Hook for functions attaching themselves to `M-down'.
  18654. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18655. (defvar org-shiftmetaleft-hook nil
  18656. "Hook for functions attaching themselves to `M-S-left'.
  18657. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18658. (defvar org-shiftmetaright-hook nil
  18659. "Hook for functions attaching themselves to `M-S-right'.
  18660. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18661. (defvar org-shiftmetaup-hook nil
  18662. "Hook for functions attaching themselves to `M-S-up'.
  18663. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18664. (defvar org-shiftmetadown-hook nil
  18665. "Hook for functions attaching themselves to `M-S-down'.
  18666. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18667. (defvar org-metareturn-hook nil
  18668. "Hook for functions attaching themselves to `M-RET'.
  18669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18670. (defvar org-shiftup-hook nil
  18671. "Hook for functions attaching themselves to `S-up'.
  18672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18673. (defvar org-shiftup-final-hook nil
  18674. "Hook for functions attaching themselves to `S-up'.
  18675. This one runs after all other options except shift-select have been excluded.
  18676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18677. (defvar org-shiftdown-hook nil
  18678. "Hook for functions attaching themselves to `S-down'.
  18679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18680. (defvar org-shiftdown-final-hook nil
  18681. "Hook for functions attaching themselves to `S-down'.
  18682. This one runs after all other options except shift-select have been excluded.
  18683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18684. (defvar org-shiftleft-hook nil
  18685. "Hook for functions attaching themselves to `S-left'.
  18686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18687. (defvar org-shiftleft-final-hook nil
  18688. "Hook for functions attaching themselves to `S-left'.
  18689. This one runs after all other options except shift-select have been excluded.
  18690. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18691. (defvar org-shiftright-hook nil
  18692. "Hook for functions attaching themselves to `S-right'.
  18693. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18694. (defvar org-shiftright-final-hook nil
  18695. "Hook for functions attaching themselves to `S-right'.
  18696. This one runs after all other options except shift-select have been excluded.
  18697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18698. (defun org-modifier-cursor-error ()
  18699. "Throw an error, a modified cursor command was applied in wrong context."
  18700. (user-error "This command is active in special context like tables, headlines or items"))
  18701. (defun org-shiftselect-error ()
  18702. "Throw an error because Shift-Cursor command was applied in wrong context."
  18703. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18704. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18705. (user-error "This command works only in special context like headlines or timestamps")))
  18706. (defun org-call-for-shift-select (cmd)
  18707. (let ((this-command-keys-shift-translated t))
  18708. (call-interactively cmd)))
  18709. (defun org-shifttab (&optional arg)
  18710. "Global visibility cycling or move to previous table field.
  18711. Call `org-table-previous-field' within a table.
  18712. When ARG is nil, cycle globally through visibility states.
  18713. When ARG is a numeric prefix, show contents of this level."
  18714. (interactive "P")
  18715. (cond
  18716. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18717. ((integerp arg)
  18718. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18719. (message "Content view to level: %d" arg)
  18720. (org-content (prefix-numeric-value arg2))
  18721. (org-cycle-show-empty-lines t)
  18722. (setq org-cycle-global-status 'overview)))
  18723. (t (call-interactively 'org-global-cycle))))
  18724. (defun org-shiftmetaleft ()
  18725. "Promote subtree or delete table column.
  18726. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18727. `org-table-delete-column', depending on context. See the
  18728. individual commands for more information."
  18729. (interactive)
  18730. (cond
  18731. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18732. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18733. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18734. ((if (not (org-region-active-p)) (org-at-item-p)
  18735. (save-excursion (goto-char (region-beginning))
  18736. (org-at-item-p)))
  18737. (call-interactively 'org-outdent-item-tree))
  18738. (t (org-modifier-cursor-error))))
  18739. (defun org-shiftmetaright ()
  18740. "Demote subtree or insert table column.
  18741. Calls `org-demote-subtree', `org-indent-item-tree', or
  18742. `org-table-insert-column', depending on context. See the
  18743. individual commands for more information."
  18744. (interactive)
  18745. (cond
  18746. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18747. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18748. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18749. ((if (not (org-region-active-p)) (org-at-item-p)
  18750. (save-excursion (goto-char (region-beginning))
  18751. (org-at-item-p)))
  18752. (call-interactively 'org-indent-item-tree))
  18753. (t (org-modifier-cursor-error))))
  18754. (defun org-shiftmetaup (&optional _arg)
  18755. "Drag the line at point up.
  18756. In a table, kill the current row.
  18757. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18758. but also adjust the previous clocked item in the clock history.
  18759. Everywhere else, drag the line at point up."
  18760. (interactive "P")
  18761. (cond
  18762. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18763. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18764. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18765. (call-interactively 'org-timestamp-up)))
  18766. (t (call-interactively 'org-drag-line-backward))))
  18767. (defun org-shiftmetadown (&optional _arg)
  18768. "Drag the line at point down.
  18769. In a table, insert an empty row at the current line.
  18770. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18771. but also adjust the previous clocked item in the clock history.
  18772. Everywhere else, drag the line at point down."
  18773. (interactive "P")
  18774. (cond
  18775. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18776. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18777. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18778. (call-interactively 'org-timestamp-down)))
  18779. (t (call-interactively 'org-drag-line-forward))))
  18780. (defsubst org-hidden-tree-error ()
  18781. (user-error
  18782. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18783. (defun org-metaleft (&optional _arg)
  18784. "Promote heading, list item at point or move table column left.
  18785. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18786. depending on context. With no specific context, calls the Emacs
  18787. default `backward-word'. See the individual commands for more
  18788. information.
  18789. This function runs the hook `org-metaleft-hook' as a first step,
  18790. and returns at first non-nil value."
  18791. (interactive "P")
  18792. (cond
  18793. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18794. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18795. ((org-with-limited-levels
  18796. (or (org-at-heading-p)
  18797. (and (org-region-active-p)
  18798. (save-excursion
  18799. (goto-char (region-beginning))
  18800. (org-at-heading-p)))))
  18801. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18802. (call-interactively 'org-do-promote))
  18803. ;; At an inline task.
  18804. ((org-at-heading-p)
  18805. (call-interactively 'org-inlinetask-promote))
  18806. ((or (org-at-item-p)
  18807. (and (org-region-active-p)
  18808. (save-excursion
  18809. (goto-char (region-beginning))
  18810. (org-at-item-p))))
  18811. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18812. (call-interactively 'org-outdent-item))
  18813. (t (call-interactively 'backward-word))))
  18814. (defun org-metaright (&optional _arg)
  18815. "Demote heading, list item at point or move table column right.
  18816. In front of a drawer or a block keyword, indent it correctly.
  18817. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18818. `org-indnet-drawer' or `org-indent-block' depending on context.
  18819. With no specific context, calls the Emacs default `forward-word'.
  18820. See the individual commands for more information.
  18821. This function runs the hook `org-metaright-hook' as a first step,
  18822. and returns at first non-nil value."
  18823. (interactive "P")
  18824. (cond
  18825. ((run-hook-with-args-until-success 'org-metaright-hook))
  18826. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18827. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18828. ((org-at-block-p) (call-interactively 'org-indent-block))
  18829. ((org-with-limited-levels
  18830. (or (org-at-heading-p)
  18831. (and (org-region-active-p)
  18832. (save-excursion
  18833. (goto-char (region-beginning))
  18834. (org-at-heading-p)))))
  18835. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18836. (call-interactively 'org-do-demote))
  18837. ;; At an inline task.
  18838. ((org-at-heading-p)
  18839. (call-interactively 'org-inlinetask-demote))
  18840. ((or (org-at-item-p)
  18841. (and (org-region-active-p)
  18842. (save-excursion
  18843. (goto-char (region-beginning))
  18844. (org-at-item-p))))
  18845. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18846. (call-interactively 'org-indent-item))
  18847. (t (call-interactively 'forward-word))))
  18848. (defun org-check-for-hidden (what)
  18849. "Check if there are hidden headlines/items in the current visual line.
  18850. WHAT can be either `headlines' or `items'. If the current line is
  18851. an outline or item heading and it has a folded subtree below it,
  18852. this function returns t, nil otherwise."
  18853. (let ((re (cond
  18854. ((eq what 'headlines) org-outline-regexp-bol)
  18855. ((eq what 'items) (org-item-beginning-re))
  18856. (t (error "This should not happen"))))
  18857. beg end)
  18858. (save-excursion
  18859. (catch 'exit
  18860. (unless (org-region-active-p)
  18861. (setq beg (point-at-bol))
  18862. (beginning-of-line 2)
  18863. (while (and (not (eobp)) ;; this is like `next-line'
  18864. (get-char-property (1- (point)) 'invisible))
  18865. (beginning-of-line 2))
  18866. (setq end (point))
  18867. (goto-char beg)
  18868. (goto-char (point-at-eol))
  18869. (setq end (max end (point)))
  18870. (while (re-search-forward re end t)
  18871. (when (get-char-property (match-beginning 0) 'invisible)
  18872. (throw 'exit t))))
  18873. nil))))
  18874. (defun org-metaup (&optional _arg)
  18875. "Move subtree up or move table row up.
  18876. Calls `org-move-subtree-up' or `org-table-move-row' or
  18877. `org-move-item-up', depending on context. See the individual commands
  18878. for more information."
  18879. (interactive "P")
  18880. (cond
  18881. ((run-hook-with-args-until-success 'org-metaup-hook))
  18882. ((org-region-active-p)
  18883. (let* ((a (min (region-beginning) (region-end)))
  18884. (b (1- (max (region-beginning) (region-end))))
  18885. (c (save-excursion (goto-char a)
  18886. (move-beginning-of-line 0)))
  18887. (d (save-excursion (goto-char a)
  18888. (move-end-of-line 0) (point))))
  18889. (transpose-regions a b c d)
  18890. (goto-char c)))
  18891. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18892. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18893. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18894. (t (org-drag-element-backward))))
  18895. (defun org-metadown (&optional _arg)
  18896. "Move subtree down or move table row down.
  18897. Calls `org-move-subtree-down' or `org-table-move-row' or
  18898. `org-move-item-down', depending on context. See the individual
  18899. commands for more information."
  18900. (interactive "P")
  18901. (cond
  18902. ((run-hook-with-args-until-success 'org-metadown-hook))
  18903. ((org-region-active-p)
  18904. (let* ((a (min (region-beginning) (region-end)))
  18905. (b (max (region-beginning) (region-end)))
  18906. (c (save-excursion (goto-char b)
  18907. (move-beginning-of-line 1)))
  18908. (d (save-excursion (goto-char b)
  18909. (move-end-of-line 1) (1+ (point)))))
  18910. (transpose-regions a b c d)
  18911. (goto-char d)))
  18912. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18913. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18914. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18915. (t (org-drag-element-forward))))
  18916. (defun org-shiftup (&optional arg)
  18917. "Increase item in timestamp or increase priority of current headline.
  18918. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18919. depending on context. See the individual commands for more information."
  18920. (interactive "P")
  18921. (cond
  18922. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18923. ((and org-support-shift-select (org-region-active-p))
  18924. (org-call-for-shift-select 'previous-line))
  18925. ((org-at-timestamp-p t)
  18926. (call-interactively (if org-edit-timestamp-down-means-later
  18927. 'org-timestamp-down 'org-timestamp-up)))
  18928. ((and (not (eq org-support-shift-select 'always))
  18929. org-enable-priority-commands
  18930. (org-at-heading-p))
  18931. (call-interactively 'org-priority-up))
  18932. ((and (not org-support-shift-select) (org-at-item-p))
  18933. (call-interactively 'org-previous-item))
  18934. ((org-clocktable-try-shift 'up arg))
  18935. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18936. (org-support-shift-select
  18937. (org-call-for-shift-select 'previous-line))
  18938. (t (org-shiftselect-error))))
  18939. (defun org-shiftdown (&optional arg)
  18940. "Decrease item in timestamp or decrease priority of current headline.
  18941. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18942. depending on context. See the individual commands for more information."
  18943. (interactive "P")
  18944. (cond
  18945. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18946. ((and org-support-shift-select (org-region-active-p))
  18947. (org-call-for-shift-select 'next-line))
  18948. ((org-at-timestamp-p t)
  18949. (call-interactively (if org-edit-timestamp-down-means-later
  18950. 'org-timestamp-up 'org-timestamp-down)))
  18951. ((and (not (eq org-support-shift-select 'always))
  18952. org-enable-priority-commands
  18953. (org-at-heading-p))
  18954. (call-interactively 'org-priority-down))
  18955. ((and (not org-support-shift-select) (org-at-item-p))
  18956. (call-interactively 'org-next-item))
  18957. ((org-clocktable-try-shift 'down arg))
  18958. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18959. (org-support-shift-select
  18960. (org-call-for-shift-select 'next-line))
  18961. (t (org-shiftselect-error))))
  18962. (defun org-shiftright (&optional arg)
  18963. "Cycle the thing at point or in the current line, depending on context.
  18964. Depending on context, this does one of the following:
  18965. - switch a timestamp at point one day into the future
  18966. - on a headline, switch to the next TODO keyword.
  18967. - on an item, switch entire list to the next bullet type
  18968. - on a property line, switch to the next allowed value
  18969. - on a clocktable definition line, move time block into the future"
  18970. (interactive "P")
  18971. (cond
  18972. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18973. ((and org-support-shift-select (org-region-active-p))
  18974. (org-call-for-shift-select 'forward-char))
  18975. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18976. ((and (not (eq org-support-shift-select 'always))
  18977. (org-at-heading-p))
  18978. (let ((org-inhibit-logging
  18979. (not org-treat-S-cursor-todo-selection-as-state-change))
  18980. (org-inhibit-blocking
  18981. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18982. (org-call-with-arg 'org-todo 'right)))
  18983. ((or (and org-support-shift-select
  18984. (not (eq org-support-shift-select 'always))
  18985. (org-at-item-bullet-p))
  18986. (and (not org-support-shift-select) (org-at-item-p)))
  18987. (org-call-with-arg 'org-cycle-list-bullet nil))
  18988. ((and (not (eq org-support-shift-select 'always))
  18989. (org-at-property-p))
  18990. (call-interactively 'org-property-next-allowed-value))
  18991. ((org-clocktable-try-shift 'right arg))
  18992. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18993. (org-support-shift-select
  18994. (org-call-for-shift-select 'forward-char))
  18995. (t (org-shiftselect-error))))
  18996. (defun org-shiftleft (&optional arg)
  18997. "Cycle the thing at point or in the current line, depending on context.
  18998. Depending on context, this does one of the following:
  18999. - switch a timestamp at point one day into the past
  19000. - on a headline, switch to the previous TODO keyword.
  19001. - on an item, switch entire list to the previous bullet type
  19002. - on a property line, switch to the previous allowed value
  19003. - on a clocktable definition line, move time block into the past"
  19004. (interactive "P")
  19005. (cond
  19006. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19007. ((and org-support-shift-select (org-region-active-p))
  19008. (org-call-for-shift-select 'backward-char))
  19009. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19010. ((and (not (eq org-support-shift-select 'always))
  19011. (org-at-heading-p))
  19012. (let ((org-inhibit-logging
  19013. (not org-treat-S-cursor-todo-selection-as-state-change))
  19014. (org-inhibit-blocking
  19015. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19016. (org-call-with-arg 'org-todo 'left)))
  19017. ((or (and org-support-shift-select
  19018. (not (eq org-support-shift-select 'always))
  19019. (org-at-item-bullet-p))
  19020. (and (not org-support-shift-select) (org-at-item-p)))
  19021. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19022. ((and (not (eq org-support-shift-select 'always))
  19023. (org-at-property-p))
  19024. (call-interactively 'org-property-previous-allowed-value))
  19025. ((org-clocktable-try-shift 'left arg))
  19026. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19027. (org-support-shift-select
  19028. (org-call-for-shift-select 'backward-char))
  19029. (t (org-shiftselect-error))))
  19030. (defun org-shiftcontrolright ()
  19031. "Switch to next TODO set."
  19032. (interactive)
  19033. (cond
  19034. ((and org-support-shift-select (org-region-active-p))
  19035. (org-call-for-shift-select 'forward-word))
  19036. ((and (not (eq org-support-shift-select 'always))
  19037. (org-at-heading-p))
  19038. (org-call-with-arg 'org-todo 'nextset))
  19039. (org-support-shift-select
  19040. (org-call-for-shift-select 'forward-word))
  19041. (t (org-shiftselect-error))))
  19042. (defun org-shiftcontrolleft ()
  19043. "Switch to previous TODO set."
  19044. (interactive)
  19045. (cond
  19046. ((and org-support-shift-select (org-region-active-p))
  19047. (org-call-for-shift-select 'backward-word))
  19048. ((and (not (eq org-support-shift-select 'always))
  19049. (org-at-heading-p))
  19050. (org-call-with-arg 'org-todo 'previousset))
  19051. (org-support-shift-select
  19052. (org-call-for-shift-select 'backward-word))
  19053. (t (org-shiftselect-error))))
  19054. (defun org-shiftcontrolup (&optional n)
  19055. "Change timestamps synchronously up in CLOCK log lines.
  19056. Optional argument N tells to change by that many units."
  19057. (interactive "P")
  19058. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19059. (let (org-support-shift-select)
  19060. (org-clock-timestamps-up n))
  19061. (user-error "Not at a clock log")))
  19062. (defun org-shiftcontroldown (&optional n)
  19063. "Change timestamps synchronously down in CLOCK log lines.
  19064. Optional argument N tells to change by that many units."
  19065. (interactive "P")
  19066. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19067. (let (org-support-shift-select)
  19068. (org-clock-timestamps-down n))
  19069. (user-error "Not at a clock log")))
  19070. (defun org-increase-number-at-point (&optional inc)
  19071. "Increment the number at point.
  19072. With an optional prefix numeric argument INC, increment using
  19073. this numeric value."
  19074. (interactive "p")
  19075. (if (not (number-at-point))
  19076. (user-error "Not on a number")
  19077. (unless inc (setq inc 1))
  19078. (let ((pos (point))
  19079. (beg (skip-chars-backward "-+^/*0-9eE."))
  19080. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19081. (setq nap (buffer-substring-no-properties
  19082. (+ pos beg) (+ pos beg end)))
  19083. (delete-region (+ pos beg) (+ pos beg end))
  19084. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19085. (when (org-at-table-p)
  19086. (org-table-align)
  19087. (org-table-end-of-field 1))))
  19088. (defun org-decrease-number-at-point (&optional inc)
  19089. "Decrement the number at point.
  19090. With an optional prefix numeric argument INC, decrement using
  19091. this numeric value."
  19092. (interactive "p")
  19093. (org-increase-number-at-point (- (or inc 1))))
  19094. (defun org-ctrl-c-ret ()
  19095. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19096. (interactive)
  19097. (cond
  19098. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19099. (t (call-interactively 'org-insert-heading))))
  19100. (defun org-find-visible ()
  19101. (let ((s (point)))
  19102. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19103. (get-char-property s 'invisible)))
  19104. s))
  19105. (defun org-find-invisible ()
  19106. (let ((s (point)))
  19107. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19108. (not (get-char-property s 'invisible))))
  19109. s))
  19110. (defun org-copy-visible (beg end)
  19111. "Copy the visible parts of the region."
  19112. (interactive "r")
  19113. (let (snippets s)
  19114. (save-excursion
  19115. (save-restriction
  19116. (narrow-to-region beg end)
  19117. (setq s (goto-char (point-min)))
  19118. (while (not (= (point) (point-max)))
  19119. (goto-char (org-find-invisible))
  19120. (push (buffer-substring s (point)) snippets)
  19121. (setq s (goto-char (org-find-visible))))))
  19122. (kill-new (apply 'concat (nreverse snippets)))))
  19123. (defun org-copy-special ()
  19124. "Copy region in table or copy current subtree.
  19125. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19126. See the individual commands for more information."
  19127. (interactive)
  19128. (call-interactively
  19129. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19130. (defun org-cut-special ()
  19131. "Cut region in table or cut current subtree.
  19132. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19133. See the individual commands for more information."
  19134. (interactive)
  19135. (call-interactively
  19136. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19137. (defun org-paste-special (arg)
  19138. "Paste rectangular region into table, or past subtree relative to level.
  19139. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19140. See the individual commands for more information."
  19141. (interactive "P")
  19142. (if (org-at-table-p)
  19143. (org-table-paste-rectangle)
  19144. (org-paste-subtree arg)))
  19145. (defsubst org-in-fixed-width-region-p ()
  19146. "Is point in a fixed-width region?"
  19147. (save-match-data
  19148. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19149. (defun org-edit-special (&optional arg)
  19150. "Call a special editor for the element at point.
  19151. When at a table, call the formula editor with `org-table-edit-formulas'.
  19152. When in a source code block, call `org-edit-src-code'.
  19153. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19154. When in an export block, call `org-edit-export-block'.
  19155. When at an #+INCLUDE keyword, visit the included file.
  19156. When at a footnote reference, call `org-edit-footnote-reference'
  19157. On a link, call `ffap' to visit the link at point.
  19158. Otherwise, return a user error."
  19159. (interactive "P")
  19160. (let ((element (org-element-at-point)))
  19161. (cl-assert (not buffer-read-only) nil
  19162. "Buffer is read-only: %s" (buffer-name))
  19163. (pcase (org-element-type element)
  19164. (`src-block
  19165. (if (not arg) (org-edit-src-code)
  19166. (let* ((info (org-babel-get-src-block-info))
  19167. (lang (nth 0 info))
  19168. (params (nth 2 info))
  19169. (session (cdr (assq :session params))))
  19170. (if (not session) (org-edit-src-code)
  19171. ;; At a src-block with a session and function called with
  19172. ;; an ARG: switch to the buffer related to the inferior
  19173. ;; process.
  19174. (switch-to-buffer
  19175. (funcall (intern (concat "org-babel-prep-session:" lang))
  19176. session params))))))
  19177. (`keyword
  19178. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19179. (org-open-link-from-string
  19180. (format "[[%s]]"
  19181. (expand-file-name
  19182. (let ((value (org-element-property :value element)))
  19183. (cond ((not (org-string-nw-p value))
  19184. (user-error "No file to edit"))
  19185. ((string-match "\\`\"\\(.*?\\)\"" value)
  19186. (match-string 1 value))
  19187. ((string-match "\\`[^ \t\"]\\S-*" value)
  19188. (match-string 0 value))
  19189. (t (user-error "No valid file specified")))))))
  19190. (user-error "No special environment to edit here")))
  19191. (`table
  19192. (if (eq (org-element-property :type element) 'table.el)
  19193. (org-edit-table.el)
  19194. (call-interactively 'org-table-edit-formulas)))
  19195. ;; Only Org tables contain `table-row' type elements.
  19196. (`table-row (call-interactively 'org-table-edit-formulas))
  19197. (`example-block (org-edit-src-code))
  19198. (`export-block (org-edit-export-block))
  19199. (`fixed-width (org-edit-fixed-width-region))
  19200. (_
  19201. ;; No notable element at point. Though, we may be at a link or
  19202. ;; a footnote reference, which are objects. Thus, scan deeper.
  19203. (let ((context (org-element-context element)))
  19204. (pcase (org-element-type context)
  19205. (`footnote-reference (org-edit-footnote-reference))
  19206. (`inline-src-block (org-edit-inline-src-code))
  19207. (`link (call-interactively #'ffap))
  19208. (_ (user-error "No special environment to edit here"))))))))
  19209. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19210. (defun org-ctrl-c-ctrl-c (&optional arg)
  19211. "Set tags in headline, or update according to changed information at point.
  19212. This command does many different things, depending on context:
  19213. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19214. this is what we do.
  19215. - If the cursor is on a statistics cookie, update it.
  19216. - If the cursor is in a headline, prompt for tags and insert them
  19217. into the current line, aligned to `org-tags-column'. When called
  19218. with prefix arg, realign all tags in the current buffer.
  19219. - If the cursor is in one of the special #+KEYWORD lines, this
  19220. triggers scanning the buffer for these lines and updating the
  19221. information.
  19222. - If the cursor is inside a table, realign the table. This command
  19223. works even if the automatic table editor has been turned off.
  19224. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19225. the entire table.
  19226. - If the cursor is at a footnote reference or definition, jump to
  19227. the corresponding definition or references, respectively.
  19228. - If the cursor is a the beginning of a dynamic block, update it.
  19229. - If the current buffer is a capture buffer, close note and file it.
  19230. - If the cursor is on a <<<target>>>, update radio targets and
  19231. corresponding links in this buffer.
  19232. - If the cursor is on a numbered item in a plain list, renumber the
  19233. ordered list.
  19234. - If the cursor is on a checkbox, toggle it.
  19235. - If the cursor is on a code block, evaluate it. The variable
  19236. `org-confirm-babel-evaluate' can be used to control prompting
  19237. before code block evaluation, by default every code block
  19238. evaluation requires confirmation. Code block evaluation can be
  19239. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19240. (interactive "P")
  19241. (cond
  19242. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19243. org-occur-highlights)
  19244. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19245. (org-remove-occur-highlights)
  19246. (message "Temporary highlights/overlays removed from current buffer"))
  19247. ((and (local-variable-p 'org-finish-function (current-buffer))
  19248. (fboundp org-finish-function))
  19249. (funcall org-finish-function))
  19250. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19251. (t
  19252. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19253. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19254. (user-error "C-c C-c can do nothing useful at this location"))
  19255. (let* ((context (org-element-context))
  19256. (type (org-element-type context)))
  19257. (cl-case type
  19258. ;; When at a link, act according to the parent instead.
  19259. (link (setq context (org-element-property :parent context))
  19260. (setq type (org-element-type context)))
  19261. ;; Unsupported object types: refer to the first supported
  19262. ;; element or object containing it.
  19263. ((bold code entity export-snippet inline-babel-call inline-src-block
  19264. italic latex-fragment line-break macro strike-through subscript
  19265. superscript underline verbatim)
  19266. (setq context
  19267. (org-element-lineage
  19268. context '(radio-target paragraph verse-block table-cell)))))
  19269. ;; For convenience: at the first line of a paragraph on the
  19270. ;; same line as an item, apply function on that item instead.
  19271. (when (eq type 'paragraph)
  19272. (let ((parent (org-element-property :parent context)))
  19273. (when (and (eq (org-element-type parent) 'item)
  19274. (= (line-beginning-position)
  19275. (org-element-property :begin parent)))
  19276. (setq context parent type 'item))))
  19277. ;; Act according to type of element or object at point.
  19278. (cl-case type
  19279. (clock (org-clock-update-time-maybe))
  19280. (dynamic-block
  19281. (save-excursion
  19282. (goto-char (org-element-property :post-affiliated context))
  19283. (org-update-dblock)))
  19284. (footnote-definition
  19285. (goto-char (org-element-property :post-affiliated context))
  19286. (call-interactively 'org-footnote-action))
  19287. (footnote-reference (call-interactively 'org-footnote-action))
  19288. ((headline inlinetask)
  19289. (save-excursion (goto-char (org-element-property :begin context))
  19290. (call-interactively 'org-set-tags)))
  19291. (item
  19292. ;; At an item: a double C-u set checkbox to "[-]"
  19293. ;; unconditionally, whereas a single one will toggle its
  19294. ;; presence. Without a universal argument, if the item
  19295. ;; has a checkbox, toggle it. Otherwise repair the list.
  19296. (let* ((box (org-element-property :checkbox context))
  19297. (struct (org-element-property :structure context))
  19298. (old-struct (copy-tree struct))
  19299. (parents (org-list-parents-alist struct))
  19300. (prevs (org-list-prevs-alist struct))
  19301. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19302. (org-list-set-checkbox
  19303. (org-element-property :begin context) struct
  19304. (cond ((equal arg '(16)) "[-]")
  19305. ((and (not box) (equal arg '(4))) "[ ]")
  19306. ((or (not box) (equal arg '(4))) nil)
  19307. ((eq box 'on) "[ ]")
  19308. (t "[X]")))
  19309. ;; Mimic `org-list-write-struct' but with grabbing
  19310. ;; a return value from `org-list-struct-fix-box'.
  19311. (org-list-struct-fix-ind struct parents 2)
  19312. (org-list-struct-fix-item-end struct)
  19313. (org-list-struct-fix-bul struct prevs)
  19314. (org-list-struct-fix-ind struct parents)
  19315. (let ((block-item
  19316. (org-list-struct-fix-box struct parents prevs orderedp)))
  19317. (if (and box (equal struct old-struct))
  19318. (if (equal arg '(16))
  19319. (message "Checkboxes already reset")
  19320. (user-error "Cannot toggle this checkbox: %s"
  19321. (if (eq box 'on)
  19322. "all subitems checked"
  19323. "unchecked subitems")))
  19324. (org-list-struct-apply-struct struct old-struct)
  19325. (org-update-checkbox-count-maybe))
  19326. (when block-item
  19327. (message "Checkboxes were removed due to empty box at line %d"
  19328. (org-current-line block-item))))))
  19329. (keyword
  19330. (let ((org-inhibit-startup-visibility-stuff t)
  19331. (org-startup-align-all-tables nil))
  19332. (when (boundp 'org-table-coordinate-overlays)
  19333. (mapc #'delete-overlay org-table-coordinate-overlays)
  19334. (setq org-table-coordinate-overlays nil))
  19335. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19336. (message "Local setup has been refreshed"))
  19337. (plain-list
  19338. ;; At a plain list, with a double C-u argument, set
  19339. ;; checkboxes of each item to "[-]", whereas a single one
  19340. ;; will toggle their presence according to the state of the
  19341. ;; first item in the list. Without an argument, repair the
  19342. ;; list.
  19343. (let* ((begin (org-element-property :contents-begin context))
  19344. (beginm (move-marker (make-marker) begin))
  19345. (struct (org-element-property :structure context))
  19346. (old-struct (copy-tree struct))
  19347. (first-box (save-excursion
  19348. (goto-char begin)
  19349. (looking-at org-list-full-item-re)
  19350. (match-string-no-properties 3)))
  19351. (new-box (cond ((equal arg '(16)) "[-]")
  19352. ((equal arg '(4)) (unless first-box "[ ]"))
  19353. ((equal first-box "[X]") "[ ]")
  19354. (t "[X]"))))
  19355. (cond
  19356. (arg
  19357. (dolist (pos
  19358. (org-list-get-all-items
  19359. begin struct (org-list-prevs-alist struct)))
  19360. (org-list-set-checkbox pos struct new-box)))
  19361. ((and first-box (eq (point) begin))
  19362. ;; For convenience, when point is at bol on the first
  19363. ;; item of the list and no argument is provided, simply
  19364. ;; toggle checkbox of that item, if any.
  19365. (org-list-set-checkbox begin struct new-box)))
  19366. (org-list-write-struct
  19367. struct (org-list-parents-alist struct) old-struct)
  19368. (org-update-checkbox-count-maybe)
  19369. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19370. ((property-drawer node-property)
  19371. (call-interactively 'org-property-action))
  19372. ((radio-target target)
  19373. (call-interactively 'org-update-radio-target-regexp))
  19374. (statistics-cookie
  19375. (call-interactively 'org-update-statistics-cookies))
  19376. ((table table-cell table-row)
  19377. ;; At a table, recalculate every field and align it. Also
  19378. ;; send the table if necessary. If the table has
  19379. ;; a `table.el' type, just give up. At a table row or
  19380. ;; cell, maybe recalculate line but always align table.
  19381. (if (eq (org-element-property :type context) 'table.el)
  19382. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19383. Use \\[org-edit-special] to edit table.el tables"))
  19384. (let ((org-enable-table-editor t))
  19385. (if (or (eq type 'table)
  19386. ;; Check if point is at a TBLFM line.
  19387. (and (eq type 'table-row)
  19388. (= (point) (org-element-property :end context))))
  19389. (save-excursion
  19390. (if (org-at-TBLFM-p)
  19391. (progn (require 'org-table)
  19392. (org-table-calc-current-TBLFM))
  19393. (goto-char (org-element-property :contents-begin context))
  19394. (org-call-with-arg 'org-table-recalculate (or arg t))
  19395. (orgtbl-send-table 'maybe)))
  19396. (org-table-maybe-eval-formula)
  19397. (cond (arg (call-interactively 'org-table-recalculate))
  19398. ((org-table-maybe-recalculate-line))
  19399. (t (org-table-align)))))))
  19400. (timestamp (org-timestamp-change 0 'day))
  19401. (otherwise
  19402. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19403. (user-error
  19404. "C-c C-c can do nothing useful at this location")))))))))
  19405. (defun org-mode-restart ()
  19406. (interactive)
  19407. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19408. (funcall major-mode)
  19409. (hack-local-variables)
  19410. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19411. (org-indent-mode -1)))
  19412. (message "%s restarted" major-mode))
  19413. (defun org-kill-note-or-show-branches ()
  19414. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19415. (interactive)
  19416. (if (not org-finish-function)
  19417. (progn
  19418. (outline-hide-subtree)
  19419. (call-interactively 'outline-show-branches))
  19420. (let ((org-note-abort t))
  19421. (funcall org-finish-function))))
  19422. (defun org-delete-indentation (&optional ARG)
  19423. "Join current line to previous and fix whitespace at join.
  19424. If previous line is a headline add to headline title. Otherwise
  19425. the function calls `delete-indentation'.
  19426. With argument, join this line to following line."
  19427. (interactive "*P")
  19428. (if (save-excursion
  19429. (if ARG (beginning-of-line)
  19430. (forward-line -1))
  19431. (looking-at org-complex-heading-regexp))
  19432. ;; At headline.
  19433. (let ((tags-column (when (match-beginning 5)
  19434. (save-excursion (goto-char (match-beginning 5))
  19435. (current-column))))
  19436. (string (concat " " (progn (when ARG (forward-line 1))
  19437. (org-trim (delete-and-extract-region
  19438. (line-beginning-position)
  19439. (line-end-position)))))))
  19440. (unless (bobp) (delete-region (point) (1- (point))))
  19441. (goto-char (or (match-end 4)
  19442. (match-beginning 5)
  19443. (match-end 0)))
  19444. (skip-chars-backward " \t")
  19445. (save-excursion (insert string))
  19446. ;; Adjust alignment of tags.
  19447. (when tags-column
  19448. (org-align-tags-here (if org-auto-align-tags
  19449. org-tags-column
  19450. tags-column))))
  19451. (delete-indentation ARG)))
  19452. (defun org-open-line (n)
  19453. "Insert a new row in tables, call `open-line' elsewhere.
  19454. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19455. (interactive "*p")
  19456. (cond
  19457. ((not org-special-ctrl-o)
  19458. (open-line n))
  19459. ((org-at-table-p)
  19460. (org-table-insert-row))
  19461. (t
  19462. (open-line n))))
  19463. (defun org-return (&optional indent)
  19464. "Goto next table row or insert a newline.
  19465. Calls `org-table-next-row' or `newline', depending on context.
  19466. When optional INDENT argument is non-nil, call
  19467. `newline-and-indent' instead of `newline'.
  19468. When `org-return-follows-link' is non-nil and point is on
  19469. a timestamp or a link, call `org-open-at-point'. However, it
  19470. will not happen if point is in a table or on a \"dead\"
  19471. object (e.g., within a comment). In these case, you need to use
  19472. `org-open-at-point' directly."
  19473. (interactive)
  19474. (let ((context (if org-return-follows-link (org-element-context)
  19475. (org-element-at-point))))
  19476. (cond
  19477. ;; In a table, call `org-table-next-row'.
  19478. ((or (and (eq (org-element-type context) 'table)
  19479. (>= (point) (org-element-property :contents-begin context))
  19480. (< (point) (org-element-property :contents-end context)))
  19481. (org-element-lineage context '(table-row table-cell) t))
  19482. (org-table-justify-field-maybe)
  19483. (call-interactively #'org-table-next-row))
  19484. ;; On a link or a timestamp, call `org-open-at-point' if
  19485. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19486. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19487. ((and org-return-follows-link
  19488. (or (org-in-regexp org-ts-regexp-both nil t)
  19489. (org-in-regexp org-tsr-regexp-both nil t)
  19490. (org-in-regexp org-any-link-re nil t)))
  19491. (call-interactively #'org-open-at-point))
  19492. ;; Insert newline in heading, but preserve tags.
  19493. ((and (not (bolp))
  19494. (save-excursion (beginning-of-line)
  19495. (looking-at org-complex-heading-regexp)))
  19496. ;; At headline. Split line. However, if point is on keyword,
  19497. ;; priority cookie or tags, do not break any of them: add
  19498. ;; a newline after the headline instead.
  19499. (let ((tags-column (and (match-beginning 5)
  19500. (save-excursion (goto-char (match-beginning 5))
  19501. (current-column))))
  19502. (string
  19503. (when (and (match-end 4)
  19504. (>= (point)
  19505. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19506. (<= (point) (match-end 4)))
  19507. (delete-and-extract-region (point) (match-end 4)))))
  19508. (when (and tags-column string) ; Adjust tag alignment.
  19509. (org-align-tags-here
  19510. (if org-auto-align-tags org-tags-column tags-column)))
  19511. (end-of-line)
  19512. (org-show-entry)
  19513. (if indent (newline-and-indent) (newline))
  19514. (when string (save-excursion (insert (org-trim string))))))
  19515. ;; In a list, make sure indenting keeps trailing text within.
  19516. ((and indent
  19517. (not (eolp))
  19518. (org-element-lineage context '(item)))
  19519. (let ((trailing-data
  19520. (delete-and-extract-region (point) (line-end-position))))
  19521. (newline-and-indent)
  19522. (save-excursion (insert trailing-data))))
  19523. (t (if indent (newline-and-indent) (newline))))))
  19524. (defun org-return-indent ()
  19525. "Goto next table row or insert a newline and indent.
  19526. Calls `org-table-next-row' or `newline-and-indent', depending on
  19527. context. See the individual commands for more information."
  19528. (interactive)
  19529. (org-return t))
  19530. (defun org-ctrl-c-star ()
  19531. "Compute table, or change heading status of lines.
  19532. Calls `org-table-recalculate' or `org-toggle-heading',
  19533. depending on context."
  19534. (interactive)
  19535. (cond
  19536. ((org-at-table-p)
  19537. (call-interactively 'org-table-recalculate))
  19538. (t
  19539. ;; Convert all lines in region to list items
  19540. (call-interactively 'org-toggle-heading))))
  19541. (defun org-ctrl-c-minus ()
  19542. "Insert separator line in table or modify bullet status of line.
  19543. Also turns a plain line or a region of lines into list items.
  19544. Calls `org-table-insert-hline', `org-toggle-item', or
  19545. `org-cycle-list-bullet', depending on context."
  19546. (interactive)
  19547. (cond
  19548. ((org-at-table-p)
  19549. (call-interactively 'org-table-insert-hline))
  19550. ((org-region-active-p)
  19551. (call-interactively 'org-toggle-item))
  19552. ((org-in-item-p)
  19553. (call-interactively 'org-cycle-list-bullet))
  19554. (t
  19555. (call-interactively 'org-toggle-item))))
  19556. (defun org-toggle-item (arg)
  19557. "Convert headings or normal lines to items, items to normal lines.
  19558. If there is no active region, only the current line is considered.
  19559. If the first non blank line in the region is a headline, convert
  19560. all headlines to items, shifting text accordingly.
  19561. If it is an item, convert all items to normal lines.
  19562. If it is normal text, change region into a list of items.
  19563. With a prefix argument ARG, change the region in a single item."
  19564. (interactive "P")
  19565. (let ((shift-text
  19566. (function
  19567. ;; Shift text in current section to IND, from point to END.
  19568. ;; The function leaves point to END line.
  19569. (lambda (ind end)
  19570. (let ((min-i 1000) (end (copy-marker end)))
  19571. ;; First determine the minimum indentation (MIN-I) of
  19572. ;; the text.
  19573. (save-excursion
  19574. (catch 'exit
  19575. (while (< (point) end)
  19576. (let ((i (org-get-indentation)))
  19577. (cond
  19578. ;; Skip blank lines and inline tasks.
  19579. ((looking-at "^[ \t]*$"))
  19580. ((looking-at org-outline-regexp-bol))
  19581. ;; We can't find less than 0 indentation.
  19582. ((zerop i) (throw 'exit (setq min-i 0)))
  19583. ((< i min-i) (setq min-i i))))
  19584. (forward-line))))
  19585. ;; Then indent each line so that a line indented to
  19586. ;; MIN-I becomes indented to IND. Ignore blank lines
  19587. ;; and inline tasks in the process.
  19588. (let ((delta (- ind min-i)))
  19589. (while (< (point) end)
  19590. (unless (or (looking-at "^[ \t]*$")
  19591. (looking-at org-outline-regexp-bol))
  19592. (org-indent-line-to (+ (org-get-indentation) delta)))
  19593. (forward-line)))))))
  19594. (skip-blanks
  19595. (function
  19596. ;; Return beginning of first non-blank line, starting from
  19597. ;; line at POS.
  19598. (lambda (pos)
  19599. (save-excursion
  19600. (goto-char pos)
  19601. (skip-chars-forward " \r\t\n")
  19602. (point-at-bol)))))
  19603. beg end)
  19604. ;; Determine boundaries of changes.
  19605. (if (org-region-active-p)
  19606. (setq beg (funcall skip-blanks (region-beginning))
  19607. end (copy-marker (region-end)))
  19608. (setq beg (funcall skip-blanks (point-at-bol))
  19609. end (copy-marker (point-at-eol))))
  19610. ;; Depending on the starting line, choose an action on the text
  19611. ;; between BEG and END.
  19612. (org-with-limited-levels
  19613. (save-excursion
  19614. (goto-char beg)
  19615. (cond
  19616. ;; Case 1. Start at an item: de-itemize. Note that it only
  19617. ;; happens when a region is active: `org-ctrl-c-minus'
  19618. ;; would call `org-cycle-list-bullet' otherwise.
  19619. ((org-at-item-p)
  19620. (while (< (point) end)
  19621. (when (org-at-item-p)
  19622. (skip-chars-forward " \t")
  19623. (delete-region (point) (match-end 0)))
  19624. (forward-line)))
  19625. ;; Case 2. Start at an heading: convert to items.
  19626. ((org-at-heading-p)
  19627. (let* ((bul (org-list-bullet-string "-"))
  19628. (bul-len (length bul))
  19629. (done (org-entry-is-done-p))
  19630. (todo (org-entry-is-todo-p))
  19631. ;; Indentation of the first heading. It should be
  19632. ;; relative to the indentation of its parent, if any.
  19633. (start-ind (save-excursion
  19634. (cond
  19635. ((not org-adapt-indentation) 0)
  19636. ((not (outline-previous-heading)) 0)
  19637. (t (length (match-string 0))))))
  19638. ;; Level of first heading. Further headings will be
  19639. ;; compared to it to determine hierarchy in the list.
  19640. (ref-level (org-reduced-level (org-outline-level))))
  19641. (when (or done todo) (org-todo ""))
  19642. (while (< (point) end)
  19643. (let* ((level (org-reduced-level (org-outline-level)))
  19644. (delta (max 0 (- level ref-level))))
  19645. ;; If current headline is less indented than the first
  19646. ;; one, set it as reference, in order to preserve
  19647. ;; subtrees.
  19648. (when (< level ref-level) (setq ref-level level))
  19649. (replace-match bul t t)
  19650. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19651. (when (or done todo)
  19652. (let* ((struct (org-list-struct))
  19653. (old (copy-tree struct)))
  19654. (org-list-set-checkbox (line-beginning-position)
  19655. struct
  19656. (if done "[X]" "[ ]"))
  19657. (org-list-write-struct struct
  19658. (org-list-parents-alist struct)
  19659. old)))
  19660. ;; Ensure all text down to END (or SECTION-END) belongs
  19661. ;; to the newly created item.
  19662. (let ((section-end (save-excursion
  19663. (or (outline-next-heading) (point)))))
  19664. (forward-line)
  19665. (funcall shift-text
  19666. (+ start-ind (* (1+ delta) bul-len))
  19667. (min end section-end)))))))
  19668. ;; Case 3. Normal line with ARG: make the first line of region
  19669. ;; an item, and shift indentation of others lines to
  19670. ;; set them as item's body.
  19671. (arg (let* ((bul (org-list-bullet-string "-"))
  19672. (bul-len (length bul))
  19673. (ref-ind (org-get-indentation)))
  19674. (skip-chars-forward " \t")
  19675. (insert bul)
  19676. (forward-line)
  19677. (while (< (point) end)
  19678. ;; Ensure that lines less indented than first one
  19679. ;; still get included in item body.
  19680. (funcall shift-text
  19681. (+ ref-ind bul-len)
  19682. (min end (save-excursion (or (outline-next-heading)
  19683. (point)))))
  19684. (forward-line))))
  19685. ;; Case 4. Normal line without ARG: turn each non-item line
  19686. ;; into an item.
  19687. (t
  19688. (while (< (point) end)
  19689. (unless (or (org-at-heading-p) (org-at-item-p))
  19690. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19691. (replace-match
  19692. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19693. (forward-line))))))))
  19694. (defun org-toggle-heading (&optional nstars)
  19695. "Convert headings to normal text, or items or text to headings.
  19696. If there is no active region, only convert the current line.
  19697. With a \\[universal-argument] prefix, convert the whole list at
  19698. point into heading.
  19699. In a region:
  19700. - If the first non blank line is a headline, remove the stars
  19701. from all headlines in the region.
  19702. - If it is a normal line, turn each and every normal line (i.e.,
  19703. not an heading or an item) in the region into headings. If you
  19704. want to convert only the first line of this region, use one
  19705. universal prefix argument.
  19706. - If it is a plain list item, turn all plain list items into headings.
  19707. When converting a line into a heading, the number of stars is chosen
  19708. such that the lines become children of the current entry. However,
  19709. when a numeric prefix argument is given, its value determines the
  19710. number of stars to add."
  19711. (interactive "P")
  19712. (let ((skip-blanks
  19713. (function
  19714. ;; Return beginning of first non-blank line, starting from
  19715. ;; line at POS.
  19716. (lambda (pos)
  19717. (save-excursion
  19718. (goto-char pos)
  19719. (while (org-at-comment-p) (forward-line))
  19720. (skip-chars-forward " \r\t\n")
  19721. (point-at-bol)))))
  19722. beg end toggled)
  19723. ;; Determine boundaries of changes. If a universal prefix has
  19724. ;; been given, put the list in a region. If region ends at a bol,
  19725. ;; do not consider the last line to be in the region.
  19726. (when (and current-prefix-arg (org-at-item-p))
  19727. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19728. (org-mark-element))
  19729. (if (org-region-active-p)
  19730. (setq beg (funcall skip-blanks (region-beginning))
  19731. end (copy-marker (save-excursion
  19732. (goto-char (region-end))
  19733. (if (bolp) (point) (point-at-eol)))))
  19734. (setq beg (funcall skip-blanks (point-at-bol))
  19735. end (copy-marker (point-at-eol))))
  19736. ;; Ensure inline tasks don't count as headings.
  19737. (org-with-limited-levels
  19738. (save-excursion
  19739. (goto-char beg)
  19740. (cond
  19741. ;; Case 1. Started at an heading: de-star headings.
  19742. ((org-at-heading-p)
  19743. (while (< (point) end)
  19744. (when (org-at-heading-p t)
  19745. (looking-at org-outline-regexp) (replace-match "")
  19746. (setq toggled t))
  19747. (forward-line)))
  19748. ;; Case 2. Started at an item: change items into headlines.
  19749. ;; One star will be added by `org-list-to-subtree'.
  19750. ((org-at-item-p)
  19751. (while (< (point) end)
  19752. (when (org-at-item-p)
  19753. ;; Pay attention to cases when region ends before list.
  19754. (let* ((struct (org-list-struct))
  19755. (list-end
  19756. (min (org-list-get-bottom-point struct) (1+ end))))
  19757. (save-restriction
  19758. (narrow-to-region (point) list-end)
  19759. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19760. (setq toggled t))
  19761. (forward-line)))
  19762. ;; Case 3. Started at normal text: make every line an heading,
  19763. ;; skipping headlines and items.
  19764. (t (let* ((stars
  19765. (make-string
  19766. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19767. (add-stars
  19768. (cond (nstars "") ; stars from prefix only
  19769. ((equal stars "") "*") ; before first heading
  19770. (org-odd-levels-only "**") ; inside heading, odd
  19771. (t "*"))) ; inside heading, oddeven
  19772. (rpl (concat stars add-stars " "))
  19773. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19774. (while (< (point) (if (equal nstars '(4)) lend end))
  19775. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19776. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19777. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19778. (forward-line)))))))
  19779. (unless toggled (message "Cannot toggle heading from here"))))
  19780. (defun org-meta-return (&optional _arg)
  19781. "Insert a new heading or wrap a region in a table.
  19782. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19783. on context. See the individual commands for more information."
  19784. (interactive)
  19785. (org-check-before-invisible-edit 'insert)
  19786. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19787. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19788. #'org-insert-heading))))
  19789. ;;; Menu entries
  19790. (defsubst org-in-subtree-not-table-p ()
  19791. "Are we in a subtree and not in a table?"
  19792. (and (not (org-before-first-heading-p))
  19793. (not (org-at-table-p))))
  19794. ;; Define the Org-mode menus
  19795. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19796. '("Tbl"
  19797. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19798. ["Next Field" org-cycle (org-at-table-p)]
  19799. ["Previous Field" org-shifttab (org-at-table-p)]
  19800. ["Next Row" org-return (org-at-table-p)]
  19801. "--"
  19802. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19803. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19804. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19805. "--"
  19806. ("Column"
  19807. ["Move Column Left" org-metaleft (org-at-table-p)]
  19808. ["Move Column Right" org-metaright (org-at-table-p)]
  19809. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19810. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19811. ("Row"
  19812. ["Move Row Up" org-metaup (org-at-table-p)]
  19813. ["Move Row Down" org-metadown (org-at-table-p)]
  19814. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19815. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19816. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19817. "--"
  19818. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19819. ("Rectangle"
  19820. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19821. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19822. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19823. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19824. "--"
  19825. ("Calculate"
  19826. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19827. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19828. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19829. "--"
  19830. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19831. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19832. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19833. "--"
  19834. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19835. "--"
  19836. ["Sum Column/Rectangle" org-table-sum
  19837. (or (org-at-table-p) (org-region-active-p))]
  19838. ["Which Column?" org-table-current-column (org-at-table-p)])
  19839. ["Debug Formulas"
  19840. org-table-toggle-formula-debugger
  19841. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19842. ["Show Col/Row Numbers"
  19843. org-table-toggle-coordinate-overlays
  19844. :style toggle
  19845. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19846. "--"
  19847. ["Create" org-table-create (and (not (org-at-table-p))
  19848. org-enable-table-editor)]
  19849. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19850. ["Import from File" org-table-import (not (org-at-table-p))]
  19851. ["Export to File" org-table-export (org-at-table-p)]
  19852. "--"
  19853. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19854. "--"
  19855. ("Plot"
  19856. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19857. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19858. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19859. '("Org"
  19860. ("Show/Hide"
  19861. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19862. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19863. ["Sparse Tree..." org-sparse-tree t]
  19864. ["Reveal Context" org-reveal t]
  19865. ["Show All" outline-show-all t]
  19866. "--"
  19867. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19868. "--"
  19869. ["New Heading" org-insert-heading t]
  19870. ("Navigate Headings"
  19871. ["Up" outline-up-heading t]
  19872. ["Next" outline-next-visible-heading t]
  19873. ["Previous" outline-previous-visible-heading t]
  19874. ["Next Same Level" outline-forward-same-level t]
  19875. ["Previous Same Level" outline-backward-same-level t]
  19876. "--"
  19877. ["Jump" org-goto t])
  19878. ("Edit Structure"
  19879. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19880. "--"
  19881. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19882. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19883. "--"
  19884. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19885. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19886. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19887. "--"
  19888. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19889. "--"
  19890. ["Copy visible text" org-copy-visible t]
  19891. "--"
  19892. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19893. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19894. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19895. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19896. "--"
  19897. ["Sort Region/Children" org-sort t]
  19898. "--"
  19899. ["Convert to odd levels" org-convert-to-odd-levels t]
  19900. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19901. ("Editing"
  19902. ["Emphasis..." org-emphasize t]
  19903. ["Edit Source Example" org-edit-special t]
  19904. "--"
  19905. ["Footnote new/jump" org-footnote-action t]
  19906. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19907. ("Archive"
  19908. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19909. "--"
  19910. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19911. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19912. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19913. )
  19914. "--"
  19915. ("Hyperlinks"
  19916. ["Store Link (Global)" org-store-link t]
  19917. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19918. ["Insert Link" org-insert-link t]
  19919. ["Follow Link" org-open-at-point t]
  19920. "--"
  19921. ["Next link" org-next-link t]
  19922. ["Previous link" org-previous-link t]
  19923. "--"
  19924. ["Descriptive Links"
  19925. org-toggle-link-display
  19926. :style radio
  19927. :selected org-descriptive-links
  19928. ]
  19929. ["Literal Links"
  19930. org-toggle-link-display
  19931. :style radio
  19932. :selected (not org-descriptive-links)])
  19933. "--"
  19934. ("TODO Lists"
  19935. ["TODO/DONE/-" org-todo t]
  19936. ("Select keyword"
  19937. ["Next keyword" org-shiftright (org-at-heading-p)]
  19938. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19939. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19940. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19941. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19942. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19943. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19944. "--"
  19945. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19946. :selected org-enforce-todo-dependencies :style toggle :active t]
  19947. "Settings for tree at point"
  19948. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19949. :selected (org-entry-get nil "ORDERED")
  19950. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19951. ["Do Children parallel" org-toggle-ordered-property :style radio
  19952. :selected (not (org-entry-get nil "ORDERED"))
  19953. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19954. "--"
  19955. ["Set Priority" org-priority t]
  19956. ["Priority Up" org-shiftup t]
  19957. ["Priority Down" org-shiftdown t]
  19958. "--"
  19959. ["Get news from all feeds" org-feed-update-all t]
  19960. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19961. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19962. ("TAGS and Properties"
  19963. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19964. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19965. "--"
  19966. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19967. ["Column view of properties" org-columns t]
  19968. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19969. ("Dates and Scheduling"
  19970. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19971. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19972. ("Change Date"
  19973. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19974. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19975. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19976. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19977. ["Compute Time Range" org-evaluate-time-range t]
  19978. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19979. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19980. "--"
  19981. ["Custom time format" org-toggle-time-stamp-overlays
  19982. :style radio :selected org-display-custom-times]
  19983. "--"
  19984. ["Goto Calendar" org-goto-calendar t]
  19985. ["Date from Calendar" org-date-from-calendar t]
  19986. "--"
  19987. ["Start/Restart Timer" org-timer-start t]
  19988. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19989. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19990. ["Insert Timer String" org-timer t]
  19991. ["Insert Timer Item" org-timer-item t])
  19992. ("Logging work"
  19993. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19994. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19995. ["Clock out" org-clock-out t]
  19996. ["Clock cancel" org-clock-cancel t]
  19997. "--"
  19998. ["Mark as default task" org-clock-mark-default-task t]
  19999. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20000. ["Goto running clock" org-clock-goto t]
  20001. "--"
  20002. ["Display times" org-clock-display t]
  20003. ["Create clock table" org-clock-report t]
  20004. "--"
  20005. ["Record DONE time"
  20006. (progn (setq org-log-done (not org-log-done))
  20007. (message "Switching to %s will %s record a timestamp"
  20008. (car org-done-keywords)
  20009. (if org-log-done "automatically" "not")))
  20010. :style toggle :selected org-log-done])
  20011. "--"
  20012. ["Agenda Command..." org-agenda t]
  20013. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20014. ("File List for Agenda")
  20015. ("Special views current file"
  20016. ["TODO Tree" org-show-todo-tree t]
  20017. ["Check Deadlines" org-check-deadlines t]
  20018. ["Timeline" org-timeline t]
  20019. ["Tags/Property tree" org-match-sparse-tree t])
  20020. "--"
  20021. ["Export/Publish..." org-export-dispatch t]
  20022. ("LaTeX"
  20023. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20024. :selected org-cdlatex-mode]
  20025. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20026. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20027. ["Modify math symbol" org-cdlatex-math-modify
  20028. (org-inside-LaTeX-fragment-p)]
  20029. ["Insert citation" org-reftex-citation t]
  20030. "--"
  20031. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20032. "--"
  20033. ("MobileOrg"
  20034. ["Push Files and Views" org-mobile-push t]
  20035. ["Get Captured and Flagged" org-mobile-pull t]
  20036. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20037. "--"
  20038. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20039. "--"
  20040. ("Documentation"
  20041. ["Show Version" org-version t]
  20042. ["Info Documentation" org-info t])
  20043. ("Customize"
  20044. ["Browse Org Group" org-customize t]
  20045. "--"
  20046. ["Expand This Menu" org-create-customize-menu
  20047. (fboundp 'customize-menu-create)])
  20048. ["Send bug report" org-submit-bug-report t]
  20049. "--"
  20050. ("Refresh/Reload"
  20051. ["Refresh setup current buffer" org-mode-restart t]
  20052. ["Reload Org (after update)" org-reload t]
  20053. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20054. ))
  20055. (defun org-info (&optional node)
  20056. "Read documentation for Org-mode in the info system.
  20057. With optional NODE, go directly to that node."
  20058. (interactive)
  20059. (info (format "(org)%s" (or node ""))))
  20060. ;;;###autoload
  20061. (defun org-submit-bug-report ()
  20062. "Submit a bug report on Org-mode via mail.
  20063. Don't hesitate to report any problems or inaccurate documentation.
  20064. If you don't have setup sending mail from (X)Emacs, please copy the
  20065. output buffer into your mail program, as it gives us important
  20066. information about your Org-mode version and configuration."
  20067. (interactive)
  20068. (require 'reporter)
  20069. (defvar reporter-prompt-for-summary-p)
  20070. (org-load-modules-maybe)
  20071. (org-require-autoloaded-modules)
  20072. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20073. (reporter-submit-bug-report
  20074. "emacs-orgmode@gnu.org"
  20075. (org-version nil 'full)
  20076. (let (list)
  20077. (save-window-excursion
  20078. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20079. (delete-other-windows)
  20080. (erase-buffer)
  20081. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20082. We would like to add your full Org-mode and Outline configuration to the
  20083. bug report. This greatly simplifies the work of the maintainer and
  20084. other experts on the mailing list.
  20085. HOWEVER, some variables you have customized may contain private
  20086. information. The names of customers, colleagues, or friends, might
  20087. appear in the form of file names, tags, todo states, or search strings.
  20088. If you answer yes to the prompt, you might want to check and remove
  20089. such private information before sending the email.")
  20090. (add-text-properties (point-min) (point-max) '(face org-warning))
  20091. (when (yes-or-no-p "Include your Org-mode configuration ")
  20092. (mapatoms
  20093. (lambda (v)
  20094. (and (boundp v)
  20095. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20096. (or (and (symbol-value v)
  20097. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20098. (and
  20099. (get v 'custom-type) (get v 'standard-value)
  20100. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20101. (push v list)))))
  20102. (kill-buffer (get-buffer "*Warn about privacy*"))
  20103. list))
  20104. nil nil
  20105. "Remember to cover the basics, that is, what you expected to happen and
  20106. what in fact did happen. You don't know how to make a good report? See
  20107. http://orgmode.org/manual/Feedback.html#Feedback
  20108. Your bug report will be posted to the Org-mode mailing list.
  20109. ------------------------------------------------------------------------")
  20110. (save-excursion
  20111. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20112. (replace-match "\\1Bug: \\3 [\\2]")))))
  20113. (defun org-install-agenda-files-menu ()
  20114. (let ((bl (buffer-list)))
  20115. (save-excursion
  20116. (while bl
  20117. (set-buffer (pop bl))
  20118. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20119. (when (derived-mode-p 'org-mode)
  20120. (easy-menu-change
  20121. '("Org") "File List for Agenda"
  20122. (append
  20123. (list
  20124. ["Edit File List" (org-edit-agenda-file-list) t]
  20125. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20126. ["Remove Current File from List" org-remove-file t]
  20127. ["Cycle through agenda files" org-cycle-agenda-files t]
  20128. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20129. "--")
  20130. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20131. ;;;; Documentation
  20132. (defun org-require-autoloaded-modules ()
  20133. (interactive)
  20134. (mapc #'require
  20135. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20136. org-table org-timer)))
  20137. ;;;###autoload
  20138. (defun org-reload (&optional uncompiled)
  20139. "Reload all org lisp files.
  20140. With prefix arg UNCOMPILED, load the uncompiled versions."
  20141. (interactive "P")
  20142. (require 'loadhist)
  20143. (let* ((org-dir (org-find-library-dir "org"))
  20144. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20145. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20146. (remove-re (mapconcat 'identity
  20147. (mapcar (lambda (f) (concat "^" f "$"))
  20148. (list (if (featurep 'xemacs)
  20149. "org-colview"
  20150. "org-colview-xemacs")
  20151. "org" "org-loaddefs" "org-version"))
  20152. "\\|"))
  20153. (feats (delete-dups
  20154. (mapcar 'file-name-sans-extension
  20155. (mapcar 'file-name-nondirectory
  20156. (delq nil
  20157. (mapcar 'feature-file
  20158. features))))))
  20159. (lfeat (append
  20160. (sort
  20161. (setq feats
  20162. (delq nil (mapcar
  20163. (lambda (f)
  20164. (if (and (string-match feature-re f)
  20165. (not (string-match remove-re f)))
  20166. f nil))
  20167. feats)))
  20168. 'string-lessp)
  20169. (list "org-version" "org")))
  20170. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20171. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20172. load-uncore load-misses)
  20173. (setq load-misses
  20174. (delq 't
  20175. (mapcar (lambda (f)
  20176. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20177. (and (string= org-dir contrib-dir)
  20178. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20179. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20180. (add-to-list 'load-uncore f 'append)
  20181. 't)
  20182. f))
  20183. lfeat)))
  20184. (when load-uncore
  20185. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20186. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20187. (if load-misses
  20188. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20189. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20190. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20191. ;;;###autoload
  20192. (defun org-customize ()
  20193. "Call the customize function with org as argument."
  20194. (interactive)
  20195. (org-load-modules-maybe)
  20196. (org-require-autoloaded-modules)
  20197. (customize-browse 'org))
  20198. (defun org-create-customize-menu ()
  20199. "Create a full customization menu for Org-mode, insert it into the menu."
  20200. (interactive)
  20201. (org-load-modules-maybe)
  20202. (org-require-autoloaded-modules)
  20203. (if (fboundp 'customize-menu-create)
  20204. (progn
  20205. (easy-menu-change
  20206. '("Org") "Customize"
  20207. `(["Browse Org group" org-customize t]
  20208. "--"
  20209. ,(customize-menu-create 'org)
  20210. ["Set" Custom-set t]
  20211. ["Save" Custom-save t]
  20212. ["Reset to Current" Custom-reset-current t]
  20213. ["Reset to Saved" Custom-reset-saved t]
  20214. ["Reset to Standard Settings" Custom-reset-standard t]))
  20215. (message "\"Org\"-menu now contains full customization menu"))
  20216. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20217. ;;;; Miscellaneous stuff
  20218. ;;; Generally useful functions
  20219. (defun org-get-at-eol (property n)
  20220. "Get text property PROPERTY at the end of line less N characters."
  20221. (get-text-property (- (point-at-eol) n) property))
  20222. (defun org-find-text-property-in-string (prop s)
  20223. "Return the first non-nil value of property PROP in string S."
  20224. (or (get-text-property 0 prop s)
  20225. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20226. prop s)))
  20227. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20228. "Display the given MESSAGE as a warning."
  20229. (if (fboundp 'display-warning)
  20230. (display-warning 'org message
  20231. (if (featurep 'xemacs) 'warning :warning))
  20232. (let ((buf (get-buffer-create "*Org warnings*")))
  20233. (with-current-buffer buf
  20234. (goto-char (point-max))
  20235. (insert "Warning (Org): " message)
  20236. (unless (bolp)
  20237. (newline)))
  20238. (display-buffer buf)
  20239. (sit-for 0))))
  20240. (defun org-eval (form)
  20241. "Eval FORM and return result."
  20242. (condition-case error
  20243. (eval form)
  20244. (error (format "%%![Error: %s]" error))))
  20245. (defun org-in-clocktable-p ()
  20246. "Check if the cursor is in a clocktable."
  20247. (let ((pos (point)) start)
  20248. (save-excursion
  20249. (end-of-line 1)
  20250. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20251. (setq start (match-beginning 0))
  20252. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20253. (>= (match-end 0) pos)
  20254. start))))
  20255. (defun org-in-verbatim-emphasis ()
  20256. (save-match-data
  20257. (and (org-in-regexp org-emph-re 2)
  20258. (>= (point) (match-beginning 3))
  20259. (<= (point) (match-end 4))
  20260. (member (match-string 3) '("=" "~")))))
  20261. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20262. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20263. (if (and marker (marker-buffer marker)
  20264. (buffer-live-p (marker-buffer marker)))
  20265. (progn
  20266. (org-pop-to-buffer-same-window (marker-buffer marker))
  20267. (when (or (> marker (point-max)) (< marker (point-min)))
  20268. (widen))
  20269. (goto-char marker)
  20270. (org-show-context 'org-goto))
  20271. (if bookmark
  20272. (bookmark-jump bookmark)
  20273. (error "Cannot find location"))))
  20274. (defun org-quote-csv-field (s)
  20275. "Quote field for inclusion in CSV material."
  20276. (if (string-match "[\",]" s)
  20277. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20278. s))
  20279. (defun org-force-self-insert (N)
  20280. "Needed to enforce self-insert under remapping."
  20281. (interactive "p")
  20282. (self-insert-command N))
  20283. (defun org-string-width (s)
  20284. "Compute width of string, ignoring invisible characters.
  20285. This ignores character with invisibility property `org-link', and also
  20286. characters with property `org-cwidth', because these will become invisible
  20287. upon the next fontification round."
  20288. (let (b l)
  20289. (when (or (eq t buffer-invisibility-spec)
  20290. (assq 'org-link buffer-invisibility-spec))
  20291. (while (setq b (text-property-any 0 (length s)
  20292. 'invisible 'org-link s))
  20293. (setq s (concat (substring s 0 b)
  20294. (substring s (or (next-single-property-change
  20295. b 'invisible s)
  20296. (length s)))))))
  20297. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20298. (setq s (concat (substring s 0 b)
  20299. (substring s (or (next-single-property-change
  20300. b 'org-cwidth s)
  20301. (length s))))))
  20302. (setq l (string-width s) b -1)
  20303. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20304. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20305. l))
  20306. (defun org-shorten-string (s maxlength)
  20307. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20308. If the string is shorter or has length MAXLENGTH, just return the
  20309. original string. If it is longer, the functions finds a space in the
  20310. string, breaks this string off at that locations and adds three dots
  20311. as ellipsis. Including the ellipsis, the string will not be longer
  20312. than MAXLENGTH. If finding a good breaking point in the string does
  20313. not work, the string is just chopped off in the middle of a word
  20314. if necessary."
  20315. (if (<= (length s) maxlength)
  20316. s
  20317. (let* ((n (max (- maxlength 4) 1))
  20318. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20319. (if (string-match re s)
  20320. (concat (match-string 1 s) "...")
  20321. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20322. (defun org-get-indentation (&optional line)
  20323. "Get the indentation of the current line, interpreting tabs.
  20324. When LINE is given, assume it represents a line and compute its indentation."
  20325. (if line
  20326. (when (string-match "^ *" (org-remove-tabs line))
  20327. (match-end 0))
  20328. (save-excursion
  20329. (beginning-of-line 1)
  20330. (skip-chars-forward " \t")
  20331. (current-column))))
  20332. (defun org-get-string-indentation (s)
  20333. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20334. (let ((n -1) (i 0) (w tab-width) c)
  20335. (catch 'exit
  20336. (while (< (setq n (1+ n)) (length s))
  20337. (setq c (aref s n))
  20338. (cond ((= c ?\ ) (setq i (1+ i)))
  20339. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20340. (t (throw 'exit t)))))
  20341. i))
  20342. (defun org-remove-tabs (s &optional width)
  20343. "Replace tabulators in S with spaces.
  20344. Assumes that s is a single line, starting in column 0."
  20345. (setq width (or width tab-width))
  20346. (while (string-match "\t" s)
  20347. (setq s (replace-match
  20348. (make-string
  20349. (- (* width (/ (+ (match-beginning 0) width) width))
  20350. (match-beginning 0)) ?\ )
  20351. t t s)))
  20352. s)
  20353. (defun org-fix-indentation (line ind)
  20354. "Fix indentation in LINE.
  20355. IND is a cons cell with target and minimum indentation.
  20356. If the current indentation in LINE is smaller than the minimum,
  20357. leave it alone. If it is larger than ind, set it to the target."
  20358. (let* ((l (org-remove-tabs line))
  20359. (i (org-get-indentation l))
  20360. (i1 (car ind)) (i2 (cdr ind)))
  20361. (when (>= i i2) (setq l (substring line i2)))
  20362. (if (> i1 0)
  20363. (concat (make-string i1 ?\ ) l)
  20364. l)))
  20365. (defun org-remove-indentation (code &optional n)
  20366. "Remove maximum common indentation in string CODE and return it.
  20367. N may optionally be the number of columns to remove. Return CODE
  20368. as-is if removal failed."
  20369. (with-temp-buffer
  20370. (insert code)
  20371. (if (org-do-remove-indentation n) (buffer-string) code)))
  20372. (defun org-do-remove-indentation (&optional n)
  20373. "Remove the maximum common indentation from the buffer.
  20374. When optional argument N is a positive integer, remove exactly
  20375. that much characters from indentation, if possible. Return nil
  20376. if it fails."
  20377. (catch :exit
  20378. (goto-char (point-min))
  20379. ;; Find maximum common indentation, if not specified.
  20380. (let ((n (or n
  20381. (let ((min-ind (point-max)))
  20382. (save-excursion
  20383. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20384. (let ((ind (1- (current-column))))
  20385. (if (zerop ind) (throw :exit nil)
  20386. (setq min-ind (min min-ind ind))))))
  20387. min-ind))))
  20388. (if (zerop n) (throw :exit nil)
  20389. ;; Remove exactly N indentation, but give up if not possible.
  20390. (while (not (eobp))
  20391. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20392. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20393. ((< ind n) (throw :exit nil))
  20394. (t (org-indent-line-to (- ind n))))
  20395. (forward-line)))
  20396. ;; Signal success.
  20397. t))))
  20398. (defun org-fill-template (template alist)
  20399. "Find each %key of ALIST in TEMPLATE and replace it."
  20400. (let ((case-fold-search nil))
  20401. (dolist (entry (sort (copy-sequence alist)
  20402. (lambda (a b) (< (length (car a)) (length (car b))))))
  20403. (setq template
  20404. (replace-regexp-in-string
  20405. (concat "%" (regexp-quote (car entry)))
  20406. (or (cdr entry) "") template t t)))
  20407. template))
  20408. (defun org-base-buffer (buffer)
  20409. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20410. (if (not buffer)
  20411. buffer
  20412. (or (buffer-base-buffer buffer)
  20413. buffer)))
  20414. (defun org-wrap (string &optional width lines)
  20415. "Wrap string to either a number of lines, or a width in characters.
  20416. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20417. that costs. If there is a word longer than WIDTH, the text is actually
  20418. wrapped to the length of that word.
  20419. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20420. many lines, whatever width that takes.
  20421. The return value is a list of lines, without newlines at the end."
  20422. (let* ((words (org-split-string string "[ \t\n]+"))
  20423. (maxword (apply 'max (mapcar 'org-string-width words)))
  20424. w ll)
  20425. (cond (width
  20426. (org-do-wrap words (max maxword width)))
  20427. (lines
  20428. (setq w maxword)
  20429. (setq ll (org-do-wrap words maxword))
  20430. (if (<= (length ll) lines)
  20431. ll
  20432. (setq ll words)
  20433. (while (> (length ll) lines)
  20434. (setq w (1+ w))
  20435. (setq ll (org-do-wrap words w)))
  20436. ll))
  20437. (t (error "Cannot wrap this")))))
  20438. (defun org-do-wrap (words width)
  20439. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20440. (let (lines line)
  20441. (while words
  20442. (setq line (pop words))
  20443. (while (and words (< (+ (length line) (length (car words))) width))
  20444. (setq line (concat line " " (pop words))))
  20445. (setq lines (push line lines)))
  20446. (nreverse lines)))
  20447. (defun org-split-string (string &optional separators)
  20448. "Splits STRING into substrings at SEPARATORS.
  20449. SEPARATORS is a regular expression.
  20450. No empty strings are returned if there are matches at the beginning
  20451. and end of string."
  20452. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20453. (let ((start 0) notfirst list)
  20454. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20455. (if (and notfirst
  20456. (= start (match-beginning 0))
  20457. (< start (length string)))
  20458. (1+ start) start))
  20459. (< (match-beginning 0) (length string)))
  20460. (setq notfirst t)
  20461. (or (eq (match-beginning 0) 0)
  20462. (and (eq (match-beginning 0) (match-end 0))
  20463. (eq (match-beginning 0) start))
  20464. (push (substring string start (match-beginning 0)) list))
  20465. (setq start (match-end 0)))
  20466. (or (eq start (length string))
  20467. (push (substring string start) list))
  20468. (nreverse list)))
  20469. (defun org-quote-vert (s)
  20470. "Replace \"|\" with \"\\vert\"."
  20471. (while (string-match "|" s)
  20472. (setq s (replace-match "\\vert" t t s)))
  20473. s)
  20474. (defun org-uuidgen-p (s)
  20475. "Is S an ID created by UUIDGEN?"
  20476. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20477. (defun org-in-src-block-p (&optional inside)
  20478. "Whether point is in a code source block.
  20479. When INSIDE is non-nil, don't consider we are within a src block
  20480. when point is at #+BEGIN_SRC or #+END_SRC."
  20481. (let ((case-fold-search t))
  20482. (or (and (eq (get-char-property (point) 'src-block) t))
  20483. (and (not inside)
  20484. (save-match-data
  20485. (save-excursion
  20486. (beginning-of-line)
  20487. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20488. (defun org-context ()
  20489. "Return a list of contexts of the current cursor position.
  20490. If several contexts apply, all are returned.
  20491. Each context entry is a list with a symbol naming the context, and
  20492. two positions indicating start and end of the context. Possible
  20493. contexts are:
  20494. :headline anywhere in a headline
  20495. :headline-stars on the leading stars in a headline
  20496. :todo-keyword on a TODO keyword (including DONE) in a headline
  20497. :tags on the TAGS in a headline
  20498. :priority on the priority cookie in a headline
  20499. :item on the first line of a plain list item
  20500. :item-bullet on the bullet/number of a plain list item
  20501. :checkbox on the checkbox in a plain list item
  20502. :table in an org-mode table
  20503. :table-special on a special filed in a table
  20504. :table-table in a table.el table
  20505. :clocktable in a clocktable
  20506. :src-block in a source block
  20507. :link on a hyperlink
  20508. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20509. :target on a <<target>>
  20510. :radio-target on a <<<radio-target>>>
  20511. :latex-fragment on a LaTeX fragment
  20512. :latex-preview on a LaTeX fragment with overlaid preview image
  20513. This function expects the position to be visible because it uses font-lock
  20514. faces as a help to recognize the following contexts: :table-special, :link,
  20515. and :keyword."
  20516. (let* ((f (get-text-property (point) 'face))
  20517. (faces (if (listp f) f (list f)))
  20518. (case-fold-search t)
  20519. (p (point)) clist o)
  20520. ;; First the large context
  20521. (cond
  20522. ((org-at-heading-p t)
  20523. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20524. (when (progn
  20525. (beginning-of-line 1)
  20526. (looking-at org-todo-line-tags-regexp))
  20527. (push (org-point-in-group p 1 :headline-stars) clist)
  20528. (push (org-point-in-group p 2 :todo-keyword) clist)
  20529. (push (org-point-in-group p 4 :tags) clist))
  20530. (goto-char p)
  20531. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20532. (when (looking-at "\\[#[A-Z0-9]\\]")
  20533. (push (org-point-in-group p 0 :priority) clist)))
  20534. ((org-at-item-p)
  20535. (push (org-point-in-group p 2 :item-bullet) clist)
  20536. (push (list :item (point-at-bol)
  20537. (save-excursion (org-end-of-item) (point)))
  20538. clist)
  20539. (and (org-at-item-checkbox-p)
  20540. (push (org-point-in-group p 0 :checkbox) clist)))
  20541. ((org-at-table-p)
  20542. (push (list :table (org-table-begin) (org-table-end)) clist)
  20543. (when (memq 'org-formula faces)
  20544. (push (list :table-special
  20545. (previous-single-property-change p 'face)
  20546. (next-single-property-change p 'face)) clist)))
  20547. ((org-at-table-p 'any)
  20548. (push (list :table-table) clist)))
  20549. (goto-char p)
  20550. (let ((case-fold-search t))
  20551. ;; New the "medium" contexts: clocktables, source blocks
  20552. (cond ((org-in-clocktable-p)
  20553. (push (list :clocktable
  20554. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20555. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20556. (match-beginning 1))
  20557. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20558. (match-end 0))) clist))
  20559. ((org-in-src-block-p)
  20560. (push (list :src-block
  20561. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20562. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20563. (match-beginning 1))
  20564. (and (search-forward "#+END_SRC" nil t)
  20565. (match-beginning 0))) clist))))
  20566. (goto-char p)
  20567. ;; Now the small context
  20568. (cond
  20569. ((org-at-timestamp-p)
  20570. (push (org-point-in-group p 0 :timestamp) clist))
  20571. ((memq 'org-link faces)
  20572. (push (list :link
  20573. (previous-single-property-change p 'face)
  20574. (next-single-property-change p 'face)) clist))
  20575. ((memq 'org-special-keyword faces)
  20576. (push (list :keyword
  20577. (previous-single-property-change p 'face)
  20578. (next-single-property-change p 'face)) clist))
  20579. ((org-at-target-p)
  20580. (push (org-point-in-group p 0 :target) clist)
  20581. (goto-char (1- (match-beginning 0)))
  20582. (when (looking-at org-radio-target-regexp)
  20583. (push (org-point-in-group p 0 :radio-target) clist))
  20584. (goto-char p))
  20585. ((setq o (car (delq nil
  20586. (mapcar
  20587. (lambda (x)
  20588. (when (memq x org-latex-fragment-image-overlays) x))
  20589. (overlays-at (point))))))
  20590. (push (list :latex-fragment
  20591. (overlay-start o) (overlay-end o)) clist)
  20592. (push (list :latex-preview
  20593. (overlay-start o) (overlay-end o)) clist))
  20594. ((org-inside-LaTeX-fragment-p)
  20595. ;; FIXME: positions wrong.
  20596. (push (list :latex-fragment (point) (point)) clist)))
  20597. (setq clist (nreverse (delq nil clist)))
  20598. clist))
  20599. (defun org-in-regexp (regexp &optional nlines visually)
  20600. "Check if point is inside a match of REGEXP.
  20601. Normally only the current line is checked, but you can include
  20602. NLINES extra lines around point into the search. If VISUALLY is
  20603. set, require that the cursor is not after the match but really
  20604. on, so that the block visually is on the match.
  20605. Return nil or a cons cell (BEG . END) where BEG and END are,
  20606. respectively, the positions at the beginning and the end of the
  20607. match."
  20608. (catch :exit
  20609. (let ((pos (point))
  20610. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20611. (save-excursion
  20612. (beginning-of-line (- 1 (or nlines 0)))
  20613. (while (and (re-search-forward regexp eol t)
  20614. (<= (match-beginning 0) pos))
  20615. (let ((end (match-end 0)))
  20616. (when (or (> end pos) (and (= end pos) (not visually)))
  20617. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20618. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20619. "Org mode 8.3")
  20620. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20621. "Non-nil when point is between matches of START-RE and END-RE.
  20622. Also return a non-nil value when point is on one of the matches.
  20623. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20624. buffer positions. Default values are the positions of headlines
  20625. surrounding the point.
  20626. The functions returns a cons cell whose car (resp. cdr) is the
  20627. position before START-RE (resp. after END-RE)."
  20628. (save-match-data
  20629. (let ((pos (point))
  20630. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20631. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20632. beg end)
  20633. (save-excursion
  20634. ;; Point is on a block when on START-RE or if START-RE can be
  20635. ;; found before it...
  20636. (and (or (org-in-regexp start-re)
  20637. (re-search-backward start-re limit-up t))
  20638. (setq beg (match-beginning 0))
  20639. ;; ... and END-RE after it...
  20640. (goto-char (match-end 0))
  20641. (re-search-forward end-re limit-down t)
  20642. (> (setq end (match-end 0)) pos)
  20643. ;; ... without another START-RE in-between.
  20644. (goto-char (match-beginning 0))
  20645. (not (re-search-backward start-re (1+ beg) t))
  20646. ;; Return value.
  20647. (cons beg end))))))
  20648. (defun org-in-block-p (names)
  20649. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20650. NAMES is a list of strings containing names of blocks.
  20651. Return first block name matched, or nil. Beware that in case of
  20652. nested blocks, the returned name may not belong to the closest
  20653. block from point."
  20654. (save-match-data
  20655. (catch 'exit
  20656. (let ((case-fold-search t)
  20657. (lim-up (save-excursion (outline-previous-heading)))
  20658. (lim-down (save-excursion (outline-next-heading))))
  20659. (dolist (name names)
  20660. (let ((n (regexp-quote name)))
  20661. (when (org-between-regexps-p
  20662. (concat "^[ \t]*#\\+begin_" n)
  20663. (concat "^[ \t]*#\\+end_" n)
  20664. lim-up lim-down)
  20665. (throw 'exit n)))))
  20666. nil)))
  20667. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20668. "Call `multi-occur' with buffers for all agenda files."
  20669. (interactive "sOrg-files matching: ")
  20670. (let* ((files (org-agenda-files))
  20671. (tnames (mapcar #'file-truename files))
  20672. (extra org-agenda-text-search-extra-files))
  20673. (when (eq (car extra) 'agenda-archives)
  20674. (setq extra (cdr extra))
  20675. (setq files (org-add-archive-files files)))
  20676. (dolist (f extra)
  20677. (unless (member (file-truename f) tnames)
  20678. (unless (member f files) (setq files (append files (list f))))
  20679. (setq tnames (append tnames (list (file-truename f))))))
  20680. (multi-occur
  20681. (mapcar (lambda (x)
  20682. (with-current-buffer
  20683. ;; FIXME: Why not just (find-file-noselect x)?
  20684. ;; Is it to avoid the "revert buffer" prompt?
  20685. (or (get-file-buffer x) (find-file-noselect x))
  20686. (widen)
  20687. (current-buffer)))
  20688. files)
  20689. regexp)))
  20690. (if (boundp 'occur-mode-find-occurrence-hook)
  20691. ;; Emacs 23
  20692. (add-hook 'occur-mode-find-occurrence-hook
  20693. (lambda ()
  20694. (when (derived-mode-p 'org-mode)
  20695. (org-reveal))))
  20696. ;; Emacs 22
  20697. (defadvice occur-mode-goto-occurrence
  20698. (after org-occur-reveal activate)
  20699. (and (derived-mode-p 'org-mode) (org-reveal)))
  20700. (defadvice occur-mode-goto-occurrence-other-window
  20701. (after org-occur-reveal activate)
  20702. (and (derived-mode-p 'org-mode) (org-reveal)))
  20703. (defadvice occur-mode-display-occurrence
  20704. (after org-occur-reveal activate)
  20705. (when (derived-mode-p 'org-mode)
  20706. (let ((pos (occur-mode-find-occurrence)))
  20707. (with-current-buffer (marker-buffer pos)
  20708. (save-excursion
  20709. (goto-char pos)
  20710. (org-reveal)))))))
  20711. (defun org-occur-link-in-agenda-files ()
  20712. "Create a link and search for it in the agendas.
  20713. The link is not stored in `org-stored-links', it is just created
  20714. for the search purpose."
  20715. (interactive)
  20716. (let ((link (condition-case nil
  20717. (org-store-link nil)
  20718. (error "Unable to create a link to here"))))
  20719. (org-occur-in-agenda-files (regexp-quote link))))
  20720. (defun org-reverse-string (string)
  20721. "Return the reverse of STRING."
  20722. (apply 'string (reverse (string-to-list string))))
  20723. ;; defsubst org-uniquify must be defined before first use
  20724. (defun org-uniquify-alist (alist)
  20725. "Merge elements of ALIST with the same key.
  20726. For example, in this alist:
  20727. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20728. => \\='((a 1 3) (b 2))
  20729. merge (a 1) and (a 3) into (a 1 3).
  20730. The function returns the new ALIST."
  20731. (let (rtn)
  20732. (dolist (e alist rtn)
  20733. (let (n)
  20734. (if (not (assoc (car e) rtn))
  20735. (push e rtn)
  20736. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20737. (setq rtn (assq-delete-all (car e) rtn))
  20738. (push n rtn))))))
  20739. (defun org-delete-all (elts list)
  20740. "Remove all elements in ELTS from LIST."
  20741. (while elts
  20742. (setq list (delete (pop elts) list)))
  20743. list)
  20744. (defun org-back-over-empty-lines ()
  20745. "Move backwards over whitespace, to the beginning of the first empty line.
  20746. Returns the number of empty lines passed."
  20747. (let ((pos (point)))
  20748. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20749. (skip-chars-backward " \t\n\r")
  20750. (unless (eobp)
  20751. (forward-line -1)))
  20752. (beginning-of-line 2)
  20753. (goto-char (min (point) pos))
  20754. (count-lines (point) pos)))
  20755. (defun org-skip-whitespace ()
  20756. (skip-chars-forward " \t\n\r"))
  20757. (defun org-point-in-group (point group &optional context)
  20758. "Check if POINT is in match-group GROUP.
  20759. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20760. match. If the match group does not exist or point is not inside it,
  20761. return nil."
  20762. (and (match-beginning group)
  20763. (>= point (match-beginning group))
  20764. (<= point (match-end group))
  20765. (if context
  20766. (list context (match-beginning group) (match-end group))
  20767. t)))
  20768. (defun org-switch-to-buffer-other-window (&rest args)
  20769. "Switch to buffer in a second window on the current frame.
  20770. In particular, do not allow pop-up frames.
  20771. Returns the newly created buffer."
  20772. (org-no-popups
  20773. (apply 'switch-to-buffer-other-window args)))
  20774. (defun org-combine-plists (&rest plists)
  20775. "Create a single property list from all plists in PLISTS.
  20776. The process starts by copying the first list, and then setting properties
  20777. from the other lists. Settings in the last list are the most significant
  20778. ones and overrule settings in the other lists."
  20779. (let ((rtn (copy-sequence (pop plists)))
  20780. p v ls)
  20781. (while plists
  20782. (setq ls (pop plists))
  20783. (while ls
  20784. (setq p (pop ls) v (pop ls))
  20785. (setq rtn (plist-put rtn p v))))
  20786. rtn))
  20787. (defun org-replace-escapes (string table)
  20788. "Replace %-escapes in STRING with values in TABLE.
  20789. TABLE is an association list with keys like \"%a\" and string values.
  20790. The sequences in STRING may contain normal field width and padding information,
  20791. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20792. so values can contain further %-escapes if they are define later in TABLE."
  20793. (let ((tbl (copy-alist table))
  20794. (case-fold-search nil)
  20795. (pchg 0)
  20796. re rpl)
  20797. (dolist (e tbl)
  20798. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20799. (when (and (cdr e) (string-match re (cdr e)))
  20800. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20801. (safe "SREF"))
  20802. (add-text-properties 0 3 (list 'sref sref) safe)
  20803. (setcdr e (replace-match safe t t (cdr e)))))
  20804. (while (string-match re string)
  20805. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20806. (cdr e)))
  20807. (setq string (replace-match rpl t t string))))
  20808. (while (setq pchg (next-property-change pchg string))
  20809. (let ((sref (get-text-property pchg 'sref string)))
  20810. (when (and sref (string-match "SREF" string pchg))
  20811. (setq string (replace-match sref t t string)))))
  20812. string))
  20813. (defun org-sublist (list start end)
  20814. "Return a section of LIST, from START to END.
  20815. Counting starts at 1."
  20816. (cl-subseq list (1- start) end))
  20817. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20818. (defun org-find-base-buffer-visiting (file)
  20819. "Like `find-buffer-visiting' but always return the base buffer and
  20820. not an indirect buffer."
  20821. (let ((buf (or (get-file-buffer file)
  20822. (find-buffer-visiting file))))
  20823. (if buf
  20824. (or (buffer-base-buffer buf) buf)
  20825. nil)))
  20826. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20827. "Org 9.0")
  20828. ;;; TODO: Only called once, from ox-odt which should probably use
  20829. ;;; org-export-inline-image-p or something.
  20830. (defun org-file-image-p (file)
  20831. "Return non-nil if FILE is an image."
  20832. (save-match-data
  20833. (string-match (image-file-name-regexp) file)))
  20834. (defun org-get-cursor-date (&optional with-time)
  20835. "Return the date at cursor in as a time.
  20836. This works in the calendar and in the agenda, anywhere else it just
  20837. returns the current time.
  20838. If WITH-TIME is non-nil, returns the time of the event at point (in
  20839. the agenda) or the current time of the day."
  20840. (let (date day defd tp hod mod)
  20841. (when with-time
  20842. (setq tp (get-text-property (point) 'time))
  20843. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20844. (setq hod (string-to-number (match-string 1 tp))
  20845. mod (string-to-number (match-string 2 tp))))
  20846. (or tp (let ((now (decode-time)))
  20847. (setq hod (nth 2 now)
  20848. mod (nth 1 now)))))
  20849. (cond
  20850. ((eq major-mode 'calendar-mode)
  20851. (setq date (calendar-cursor-to-date)
  20852. defd (encode-time 0 (or mod 0) (or hod 0)
  20853. (nth 1 date) (nth 0 date) (nth 2 date))))
  20854. ((eq major-mode 'org-agenda-mode)
  20855. (setq day (get-text-property (point) 'day))
  20856. (when day
  20857. (setq date (calendar-gregorian-from-absolute day)
  20858. defd (encode-time 0 (or mod 0) (or hod 0)
  20859. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20860. (or defd (current-time))))
  20861. (defun org-mark-subtree (&optional up)
  20862. "Mark the current subtree.
  20863. This puts point at the start of the current subtree, and mark at
  20864. the end. If a numeric prefix UP is given, move up into the
  20865. hierarchy of headlines by UP levels before marking the subtree."
  20866. (interactive "P")
  20867. (org-with-limited-levels
  20868. (cond ((org-at-heading-p) (beginning-of-line))
  20869. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20870. (t (outline-previous-visible-heading 1))))
  20871. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20872. (if (org-called-interactively-p 'any)
  20873. (call-interactively 'org-mark-element)
  20874. (org-mark-element)))
  20875. ;;; Indentation
  20876. (defun org--get-expected-indentation (element contentsp)
  20877. "Expected indentation column for current line, according to ELEMENT.
  20878. ELEMENT is an element containing point. CONTENTSP is non-nil
  20879. when indentation is to be computed according to contents of
  20880. ELEMENT."
  20881. (let ((type (org-element-type element))
  20882. (start (org-element-property :begin element))
  20883. (post-affiliated (org-element-property :post-affiliated element)))
  20884. (org-with-wide-buffer
  20885. (cond
  20886. (contentsp
  20887. (cl-case type
  20888. ((diary-sexp footnote-definition) 0)
  20889. ((headline inlinetask nil)
  20890. (if (not org-adapt-indentation) 0
  20891. (let ((level (org-current-level)))
  20892. (if level (1+ level) 0))))
  20893. ((item plain-list) (org-list-item-body-column post-affiliated))
  20894. (t
  20895. (goto-char start)
  20896. (org-get-indentation))))
  20897. ((memq type '(headline inlinetask nil))
  20898. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20899. (org--get-expected-indentation element t)
  20900. 0))
  20901. ((memq type '(diary-sexp footnote-definition)) 0)
  20902. ;; First paragraph of a footnote definition or an item.
  20903. ;; Indent like parent.
  20904. ((< (line-beginning-position) start)
  20905. (org--get-expected-indentation
  20906. (org-element-property :parent element) t))
  20907. ;; At first line: indent according to previous sibling, if any,
  20908. ;; ignoring footnote definitions and inline tasks, or parent's
  20909. ;; contents.
  20910. ((= (line-beginning-position) start)
  20911. (catch 'exit
  20912. (while t
  20913. (if (= (point-min) start) (throw 'exit 0)
  20914. (goto-char (1- start))
  20915. (let* ((previous (org-element-at-point))
  20916. (parent previous))
  20917. (while (and parent (<= (org-element-property :end parent) start))
  20918. (setq previous parent
  20919. parent (org-element-property :parent parent)))
  20920. (cond
  20921. ((not previous) (throw 'exit 0))
  20922. ((> (org-element-property :end previous) start)
  20923. (throw 'exit (org--get-expected-indentation previous t)))
  20924. ((memq (org-element-type previous)
  20925. '(footnote-definition inlinetask))
  20926. (setq start (org-element-property :begin previous)))
  20927. (t (goto-char (org-element-property :begin previous))
  20928. (throw 'exit
  20929. (if (bolp) (org-get-indentation)
  20930. ;; At first paragraph in an item or
  20931. ;; a footnote definition.
  20932. (org--get-expected-indentation
  20933. (org-element-property :parent previous) t))))))))))
  20934. ;; Otherwise, move to the first non-blank line above.
  20935. (t
  20936. (beginning-of-line)
  20937. (let ((pos (point)))
  20938. (skip-chars-backward " \r\t\n")
  20939. (cond
  20940. ;; Two blank lines end a footnote definition or a plain
  20941. ;; list. When we indent an empty line after them, the
  20942. ;; containing list or footnote definition is over, so it
  20943. ;; qualifies as a previous sibling. Therefore, we indent
  20944. ;; like its first line.
  20945. ((and (memq type '(footnote-definition plain-list))
  20946. (> (count-lines (point) pos) 2))
  20947. (goto-char start)
  20948. (org-get-indentation))
  20949. ;; Line above is the first one of a paragraph at the
  20950. ;; beginning of an item or a footnote definition. Indent
  20951. ;; like parent.
  20952. ((< (line-beginning-position) start)
  20953. (org--get-expected-indentation
  20954. (org-element-property :parent element) t))
  20955. ;; Line above is the beginning of an element, i.e., point
  20956. ;; was originally on the blank lines between element's start
  20957. ;; and contents.
  20958. ((= (line-beginning-position) post-affiliated)
  20959. (org--get-expected-indentation element t))
  20960. ;; POS is after contents in a greater element. Indent like
  20961. ;; the beginning of the element.
  20962. ;;
  20963. ;; As a special case, if point is at the end of a footnote
  20964. ;; definition or an item, indent like the very last element
  20965. ;; within.
  20966. ((and (not (eq type 'paragraph))
  20967. (let ((cend (org-element-property :contents-end element)))
  20968. (and cend (<= cend pos))))
  20969. (if (memq type '(footnote-definition item plain-list))
  20970. (let ((last (org-element-at-point)))
  20971. (org--get-expected-indentation
  20972. last
  20973. (memq (org-element-type last)
  20974. '(footnote-definition item plain-list))))
  20975. (goto-char start)
  20976. (org-get-indentation)))
  20977. ;; In any other case, indent like the current line.
  20978. (t (org-get-indentation)))))))))
  20979. (defun org--align-node-property ()
  20980. "Align node property at point.
  20981. Alignment is done according to `org-property-format', which see."
  20982. (when (save-excursion
  20983. (beginning-of-line)
  20984. (looking-at org-property-re))
  20985. (replace-match
  20986. (concat (match-string 4)
  20987. (org-trim
  20988. (format org-property-format (match-string 1) (match-string 3))))
  20989. t t)))
  20990. (defun org-indent-line ()
  20991. "Indent line depending on context.
  20992. Indentation is done according to the following rules:
  20993. - Footnote definitions, diary sexps, headlines and inline tasks
  20994. have to start at column 0.
  20995. - On the very first line of an element, consider, in order, the
  20996. next rules until one matches:
  20997. 1. If there's a sibling element before, ignoring footnote
  20998. definitions and inline tasks, indent like its first line.
  20999. 2. If element has a parent, indent like its contents. More
  21000. precisely, if parent is an item, indent after the
  21001. description part, if any, or the bullet (see
  21002. `org-list-description-max-indent'). Else, indent like
  21003. parent's first line.
  21004. 3. Otherwise, indent relatively to current level, if
  21005. `org-adapt-indentation' is non-nil, or to left margin.
  21006. - On a blank line at the end of an element, indent according to
  21007. the type of the element. More precisely
  21008. 1. If element is a plain list, an item, or a footnote
  21009. definition, indent like the very last element within.
  21010. 2. If element is a paragraph, indent like its last non blank
  21011. line.
  21012. 3. Otherwise, indent like its very first line.
  21013. - In the code part of a source block, use language major mode
  21014. to indent current line if `org-src-tab-acts-natively' is
  21015. non-nil. If it is nil, do nothing.
  21016. - Otherwise, indent like the first non-blank line above.
  21017. The function doesn't indent an item as it could break the whole
  21018. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21019. \\[org-shiftmetaright].
  21020. Also align node properties according to `org-property-format'."
  21021. (interactive)
  21022. (cond
  21023. (orgstruct-is-++
  21024. (let ((indent-line-function
  21025. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21026. (indent-according-to-mode)))
  21027. ((org-at-heading-p) 'noindent)
  21028. (t
  21029. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21030. (type (org-element-type element)))
  21031. (cond ((and (memq type '(plain-list item))
  21032. (= (line-beginning-position)
  21033. (org-element-property :post-affiliated element)))
  21034. 'noindent)
  21035. ((and (eq type 'src-block)
  21036. org-src-tab-acts-natively
  21037. (> (line-beginning-position)
  21038. (org-element-property :post-affiliated element))
  21039. (< (line-beginning-position)
  21040. (org-with-wide-buffer
  21041. (goto-char (org-element-property :end element))
  21042. (skip-chars-backward " \r\t\n")
  21043. (line-beginning-position))))
  21044. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21045. (t
  21046. (let ((column (org--get-expected-indentation element nil)))
  21047. ;; Preserve current column.
  21048. (if (<= (current-column) (current-indentation))
  21049. (org-indent-line-to column)
  21050. (save-excursion (org-indent-line-to column))))
  21051. ;; Align node property. Also preserve current column.
  21052. (when (eq type 'node-property)
  21053. (let ((column (current-column)))
  21054. (org--align-node-property)
  21055. (org-move-to-column column)))))))))
  21056. (defun org-indent-region (start end)
  21057. "Indent each non-blank line in the region.
  21058. Called from a program, START and END specify the region to
  21059. indent. The function will not indent contents of example blocks,
  21060. verse blocks and export blocks as leading white spaces are
  21061. assumed to be significant there."
  21062. (interactive "r")
  21063. (save-excursion
  21064. (goto-char start)
  21065. (skip-chars-forward " \r\t\n")
  21066. (unless (eobp) (beginning-of-line))
  21067. (let ((indent-to
  21068. (lambda (ind pos)
  21069. ;; Set IND as indentation for all lines between point and
  21070. ;; POS. Blank lines are ignored. Leave point after POS
  21071. ;; once done.
  21072. (let ((limit (copy-marker pos)))
  21073. (while (< (point) limit)
  21074. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21075. (forward-line))
  21076. (set-marker limit nil))))
  21077. (end (copy-marker end)))
  21078. (while (< (point) end)
  21079. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21080. (let* ((element (org-element-at-point))
  21081. (type (org-element-type element))
  21082. (element-end (copy-marker (org-element-property :end element)))
  21083. (ind (org--get-expected-indentation element nil)))
  21084. (cond
  21085. ((or (memq type '(paragraph table table-row))
  21086. (not (or (org-element-property :contents-begin element)
  21087. (memq type
  21088. '(example-block export-block src-block)))))
  21089. ;; Elements here are indented as a single block. Also
  21090. ;; align node properties.
  21091. (when (eq type 'node-property)
  21092. (org--align-node-property)
  21093. (beginning-of-line))
  21094. (funcall indent-to ind (min element-end end)))
  21095. (t
  21096. ;; Elements in this category consist of three parts:
  21097. ;; before the contents, the contents, and after the
  21098. ;; contents. The contents are treated specially,
  21099. ;; according to the element type, or not indented at
  21100. ;; all. Other parts are indented as a single block.
  21101. (let* ((post (copy-marker
  21102. (org-element-property :post-affiliated element)))
  21103. (cbeg
  21104. (copy-marker
  21105. (cond
  21106. ((not (org-element-property :contents-begin element))
  21107. ;; Fake contents for source blocks.
  21108. (org-with-wide-buffer
  21109. (goto-char post)
  21110. (forward-line)
  21111. (point)))
  21112. ((memq type '(footnote-definition item plain-list))
  21113. ;; Contents in these elements could start on
  21114. ;; the same line as the beginning of the
  21115. ;; element. Make sure we start indenting
  21116. ;; from the second line.
  21117. (org-with-wide-buffer
  21118. (goto-char post)
  21119. (end-of-line)
  21120. (skip-chars-forward " \r\t\n")
  21121. (if (eobp) (point) (line-beginning-position))))
  21122. (t (org-element-property :contents-begin element)))))
  21123. (cend (copy-marker
  21124. (or (org-element-property :contents-end element)
  21125. ;; Fake contents for source blocks.
  21126. (org-with-wide-buffer
  21127. (goto-char element-end)
  21128. (skip-chars-backward " \r\t\n")
  21129. (line-beginning-position)))
  21130. t)))
  21131. ;; Do not change items indentation individually as it
  21132. ;; might break the list as a whole. On the other
  21133. ;; hand, when at a plain list, indent it as a whole.
  21134. (cond ((eq type 'plain-list)
  21135. (let ((offset (- ind (org-get-indentation))))
  21136. (unless (zerop offset)
  21137. (indent-rigidly (org-element-property :begin element)
  21138. (org-element-property :end element)
  21139. offset))
  21140. (goto-char cbeg)))
  21141. ((eq type 'item) (goto-char cbeg))
  21142. (t (funcall indent-to ind (min cbeg end))))
  21143. (when (< (point) end)
  21144. (cl-case type
  21145. ((example-block export-block verse-block))
  21146. (src-block
  21147. ;; In a source block, indent source code
  21148. ;; according to language major mode, but only if
  21149. ;; `org-src-tab-acts-natively' is non-nil.
  21150. (when (and (< (point) end) org-src-tab-acts-natively)
  21151. (ignore-errors
  21152. (org-babel-do-in-edit-buffer
  21153. (indent-region (point-min) (point-max))))))
  21154. (t (org-indent-region (point) (min cend end))))
  21155. (goto-char (min cend end))
  21156. (when (< (point) end)
  21157. (funcall indent-to ind (min element-end end))))
  21158. (set-marker post nil)
  21159. (set-marker cbeg nil)
  21160. (set-marker cend nil))))
  21161. (set-marker element-end nil))))
  21162. (set-marker end nil))))
  21163. (defun org-indent-drawer ()
  21164. "Indent the drawer at point."
  21165. (interactive)
  21166. (unless (save-excursion
  21167. (beginning-of-line)
  21168. (org-looking-at-p org-drawer-regexp))
  21169. (user-error "Not at a drawer"))
  21170. (let ((element (org-element-at-point)))
  21171. (unless (memq (org-element-type element) '(drawer property-drawer))
  21172. (user-error "Not at a drawer"))
  21173. (org-with-wide-buffer
  21174. (org-indent-region (org-element-property :begin element)
  21175. (org-element-property :end element))))
  21176. (message "Drawer at point indented"))
  21177. (defun org-indent-block ()
  21178. "Indent the block at point."
  21179. (interactive)
  21180. (unless (save-excursion
  21181. (beginning-of-line)
  21182. (let ((case-fold-search t))
  21183. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21184. (user-error "Not at a block"))
  21185. (let ((element (org-element-at-point)))
  21186. (unless (memq (org-element-type element)
  21187. '(comment-block center-block dynamic-block example-block
  21188. export-block quote-block special-block
  21189. src-block verse-block))
  21190. (user-error "Not at a block"))
  21191. (org-with-wide-buffer
  21192. (org-indent-region (org-element-property :begin element)
  21193. (org-element-property :end element))))
  21194. (message "Block at point indented"))
  21195. ;;; Filling
  21196. ;; We use our own fill-paragraph and auto-fill functions.
  21197. ;; `org-fill-paragraph' relies on adaptive filling and context
  21198. ;; checking. Appropriate `fill-prefix' is computed with
  21199. ;; `org-adaptive-fill-function'.
  21200. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21201. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21202. ;; `org-adaptive-fill-function' value. Internally,
  21203. ;; `org-comment-line-break-function' breaks the line.
  21204. ;; `org-setup-filling' installs filling and auto-filling related
  21205. ;; variables during `org-mode' initialization.
  21206. (defvar org-element-paragraph-separate) ; org-element.el
  21207. (defun org-setup-filling ()
  21208. (require 'org-element)
  21209. ;; Prevent auto-fill from inserting unwanted new items.
  21210. (when (boundp 'fill-nobreak-predicate)
  21211. (setq-local
  21212. fill-nobreak-predicate
  21213. (org-uniquify
  21214. (append fill-nobreak-predicate
  21215. '(org-fill-line-break-nobreak-p
  21216. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21217. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21218. (setq-local paragraph-start paragraph-ending)
  21219. (setq-local paragraph-separate paragraph-ending))
  21220. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21221. (setq-local auto-fill-inhibit-regexp nil)
  21222. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21223. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21224. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21225. (defun org-fill-line-break-nobreak-p ()
  21226. "Non-nil when a new line at point would create an Org line break."
  21227. (save-excursion
  21228. (skip-chars-backward "[ \t]")
  21229. (skip-chars-backward "\\\\")
  21230. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21231. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21232. "Non-nil when a new line at point would split a timestamp."
  21233. (and (org-at-timestamp-p t)
  21234. (not (looking-at org-ts-regexp-both))))
  21235. (declare-function message-in-body-p "message" ())
  21236. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21237. (defun org-adaptive-fill-function ()
  21238. "Compute a fill prefix for the current line.
  21239. Return fill prefix, as a string, or nil if current line isn't
  21240. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21241. matches in paragraphs or comments, use it."
  21242. (catch 'exit
  21243. (when (derived-mode-p 'message-mode)
  21244. (save-excursion
  21245. (beginning-of-line)
  21246. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21247. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21248. ((looking-at message-cite-prefix-regexp)
  21249. (throw 'exit (match-string-no-properties 0)))
  21250. ((looking-at org-outline-regexp)
  21251. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21252. (org-with-wide-buffer
  21253. (unless (org-at-heading-p)
  21254. (let* ((p (line-beginning-position))
  21255. (element (save-excursion
  21256. (beginning-of-line)
  21257. (org-element-at-point)))
  21258. (type (org-element-type element))
  21259. (post-affiliated (org-element-property :post-affiliated element)))
  21260. (unless (< p post-affiliated)
  21261. (cl-case type
  21262. (comment
  21263. (save-excursion
  21264. (beginning-of-line)
  21265. (looking-at "[ \t]*")
  21266. (concat (match-string 0) "# ")))
  21267. (footnote-definition "")
  21268. ((item plain-list)
  21269. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21270. (paragraph
  21271. ;; Fill prefix is usually the same as the current line,
  21272. ;; unless the paragraph is at the beginning of an item.
  21273. (let ((parent (org-element-property :parent element)))
  21274. (save-excursion
  21275. (beginning-of-line)
  21276. (cond ((eq (org-element-type parent) 'item)
  21277. (make-string (org-list-item-body-column
  21278. (org-element-property :begin parent))
  21279. ?\s))
  21280. ((and adaptive-fill-regexp
  21281. ;; Locally disable
  21282. ;; `adaptive-fill-function' to let
  21283. ;; `fill-context-prefix' handle
  21284. ;; `adaptive-fill-regexp' variable.
  21285. (let (adaptive-fill-function)
  21286. (fill-context-prefix
  21287. post-affiliated
  21288. (org-element-property :end element)))))
  21289. ((looking-at "[ \t]+") (match-string 0))
  21290. (t "")))))
  21291. (comment-block
  21292. ;; Only fill contents if P is within block boundaries.
  21293. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21294. (forward-line)
  21295. (point)))
  21296. (cend (save-excursion
  21297. (goto-char (org-element-property :end element))
  21298. (skip-chars-backward " \r\t\n")
  21299. (line-beginning-position))))
  21300. (when (and (>= p cbeg) (< p cend))
  21301. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21302. (match-string 0)
  21303. "")))))))))))
  21304. (declare-function message-goto-body "message" ())
  21305. (defvar message-cite-prefix-regexp) ; From message.el
  21306. (defun org-fill-paragraph (&optional justify)
  21307. "Fill element at point, when applicable.
  21308. This function only applies to comment blocks, comments, example
  21309. blocks and paragraphs. Also, as a special case, re-align table
  21310. when point is at one.
  21311. If JUSTIFY is non-nil (interactively, with prefix argument),
  21312. justify as well. If `sentence-end-double-space' is non-nil, then
  21313. period followed by one space does not end a sentence, so don't
  21314. break a line there. The variable `fill-column' controls the
  21315. width for filling.
  21316. For convenience, when point is at a plain list, an item or
  21317. a footnote definition, try to fill the first paragraph within."
  21318. (interactive)
  21319. (if (and (derived-mode-p 'message-mode)
  21320. (or (not (message-in-body-p))
  21321. (save-excursion (move-beginning-of-line 1)
  21322. (looking-at message-cite-prefix-regexp))))
  21323. ;; First ensure filling is correct in message-mode.
  21324. (let ((fill-paragraph-function
  21325. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21326. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21327. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21328. (paragraph-separate
  21329. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21330. (fill-paragraph nil))
  21331. (with-syntax-table org-mode-transpose-word-syntax-table
  21332. ;; Move to end of line in order to get the first paragraph
  21333. ;; within a plain list or a footnote definition.
  21334. (let ((element (save-excursion
  21335. (end-of-line)
  21336. (or (ignore-errors (org-element-at-point))
  21337. (user-error "An element cannot be parsed line %d"
  21338. (line-number-at-pos (point)))))))
  21339. ;; First check if point is in a blank line at the beginning of
  21340. ;; the buffer. In that case, ignore filling.
  21341. (cl-case (org-element-type element)
  21342. ;; Use major mode filling function is src blocks.
  21343. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21344. ;; Align Org tables, leave table.el tables as-is.
  21345. (table-row (org-table-align) t)
  21346. (table
  21347. (when (eq (org-element-property :type element) 'org)
  21348. (save-excursion
  21349. (goto-char (org-element-property :post-affiliated element))
  21350. (org-table-align)))
  21351. t)
  21352. (paragraph
  21353. ;; Paragraphs may contain `line-break' type objects.
  21354. (let ((beg (max (point-min)
  21355. (org-element-property :contents-begin element)))
  21356. (end (min (point-max)
  21357. (org-element-property :contents-end element))))
  21358. ;; Do nothing if point is at an affiliated keyword.
  21359. (if (< (line-end-position) beg) t
  21360. (when (derived-mode-p 'message-mode)
  21361. ;; In `message-mode', do not fill following citation
  21362. ;; in current paragraph nor text before message body.
  21363. (let ((body-start (save-excursion (message-goto-body))))
  21364. (when body-start (setq beg (max body-start beg))))
  21365. (when (save-excursion
  21366. (re-search-forward
  21367. (concat "^" message-cite-prefix-regexp) end t))
  21368. (setq end (match-beginning 0))))
  21369. ;; Fill paragraph, taking line breaks into account.
  21370. (save-excursion
  21371. (goto-char beg)
  21372. (let ((cuts (list beg)))
  21373. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21374. (when (eq 'line-break
  21375. (org-element-type
  21376. (save-excursion (backward-char)
  21377. (org-element-context))))
  21378. (push (point) cuts)))
  21379. (dolist (c (delq end cuts))
  21380. (fill-region-as-paragraph c end justify)
  21381. (setq end c))))
  21382. t)))
  21383. ;; Contents of `comment-block' type elements should be
  21384. ;; filled as plain text, but only if point is within block
  21385. ;; markers.
  21386. (comment-block
  21387. (let* ((case-fold-search t)
  21388. (beg (save-excursion
  21389. (goto-char (org-element-property :begin element))
  21390. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21391. (forward-line)
  21392. (point)))
  21393. (end (save-excursion
  21394. (goto-char (org-element-property :end element))
  21395. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21396. (line-beginning-position))))
  21397. (if (or (< (point) beg) (> (point) end)) t
  21398. (fill-region-as-paragraph
  21399. (save-excursion (end-of-line)
  21400. (re-search-backward "^[ \t]*$" beg 'move)
  21401. (line-beginning-position))
  21402. (save-excursion (beginning-of-line)
  21403. (re-search-forward "^[ \t]*$" end 'move)
  21404. (line-beginning-position))
  21405. justify))))
  21406. ;; Fill comments.
  21407. (comment
  21408. (let ((begin (org-element-property :post-affiliated element))
  21409. (end (org-element-property :end element)))
  21410. (when (and (>= (point) begin) (<= (point) end))
  21411. (let ((begin (save-excursion
  21412. (end-of-line)
  21413. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21414. (progn (forward-line) (point))
  21415. begin)))
  21416. (end (save-excursion
  21417. (end-of-line)
  21418. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21419. (1- (line-beginning-position))
  21420. (skip-chars-backward " \r\t\n")
  21421. (line-end-position)))))
  21422. ;; Do not fill comments when at a blank line.
  21423. (when (> end begin)
  21424. (let ((fill-prefix
  21425. (save-excursion
  21426. (beginning-of-line)
  21427. (looking-at "[ \t]*#")
  21428. (let ((comment-prefix (match-string 0)))
  21429. (goto-char (match-end 0))
  21430. (if (looking-at adaptive-fill-regexp)
  21431. (concat comment-prefix (match-string 0))
  21432. (concat comment-prefix " "))))))
  21433. (save-excursion
  21434. (fill-region-as-paragraph begin end justify))))))
  21435. t))
  21436. ;; Ignore every other element.
  21437. (otherwise t))))))
  21438. (defun org-auto-fill-function ()
  21439. "Auto-fill function."
  21440. ;; Check if auto-filling is meaningful.
  21441. (let ((fc (current-fill-column)))
  21442. (when (and fc (> (current-column) fc))
  21443. (let* ((fill-prefix (org-adaptive-fill-function))
  21444. ;; Enforce empty fill prefix, if required. Otherwise, it
  21445. ;; will be computed again.
  21446. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21447. (when fill-prefix (do-auto-fill))))))
  21448. (defun org-comment-line-break-function (&optional soft)
  21449. "Break line at point and indent, continuing comment if within one.
  21450. The inserted newline is marked hard if variable
  21451. `use-hard-newlines' is true, unless optional argument SOFT is
  21452. non-nil."
  21453. (if soft (insert-and-inherit ?\n) (newline 1))
  21454. (save-excursion (forward-char -1) (delete-horizontal-space))
  21455. (delete-horizontal-space)
  21456. (indent-to-left-margin)
  21457. (insert-before-markers-and-inherit fill-prefix))
  21458. ;;; Fixed Width Areas
  21459. (defun org-toggle-fixed-width ()
  21460. "Toggle fixed-width markup.
  21461. Add or remove fixed-width markup on current line, whenever it
  21462. makes sense. Return an error otherwise.
  21463. If a region is active and if it contains only fixed-width areas
  21464. or blank lines, remove all fixed-width markup in it. If the
  21465. region contains anything else, convert all non-fixed-width lines
  21466. to fixed-width ones.
  21467. Blank lines at the end of the region are ignored unless the
  21468. region only contains such lines."
  21469. (interactive)
  21470. (if (not (org-region-active-p))
  21471. ;; No region:
  21472. ;;
  21473. ;; Remove fixed width marker only in a fixed-with element.
  21474. ;;
  21475. ;; Add fixed width maker in paragraphs, in blank lines after
  21476. ;; elements or at the beginning of a headline or an inlinetask,
  21477. ;; and before any one-line elements (e.g., a clock).
  21478. (progn
  21479. (beginning-of-line)
  21480. (let* ((element (org-element-at-point))
  21481. (type (org-element-type element)))
  21482. (cond
  21483. ((and (eq type 'fixed-width)
  21484. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21485. (replace-match
  21486. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21487. ((and (memq type '(babel-call clock comment diary-sexp headline
  21488. horizontal-rule keyword paragraph
  21489. planning))
  21490. (<= (org-element-property :post-affiliated element) (point)))
  21491. (skip-chars-forward " \t")
  21492. (insert ": "))
  21493. ((and (org-looking-at-p "[ \t]*$")
  21494. (or (eq type 'inlinetask)
  21495. (save-excursion
  21496. (skip-chars-forward " \r\t\n")
  21497. (<= (org-element-property :end element) (point)))))
  21498. (delete-region (point) (line-end-position))
  21499. (org-indent-line)
  21500. (insert ": "))
  21501. (t (user-error "Cannot insert a fixed-width line here")))))
  21502. ;; Region active.
  21503. (let* ((begin (save-excursion
  21504. (goto-char (region-beginning))
  21505. (line-beginning-position)))
  21506. (end (copy-marker
  21507. (save-excursion
  21508. (goto-char (region-end))
  21509. (unless (eolp) (beginning-of-line))
  21510. (if (save-excursion (re-search-backward "\\S-" begin t))
  21511. (progn (skip-chars-backward " \r\t\n") (point))
  21512. (point)))))
  21513. (all-fixed-width-p
  21514. (catch 'not-all-p
  21515. (save-excursion
  21516. (goto-char begin)
  21517. (skip-chars-forward " \r\t\n")
  21518. (when (eobp) (throw 'not-all-p nil))
  21519. (while (< (point) end)
  21520. (let ((element (org-element-at-point)))
  21521. (if (eq (org-element-type element) 'fixed-width)
  21522. (goto-char (org-element-property :end element))
  21523. (throw 'not-all-p nil))))
  21524. t))))
  21525. (if all-fixed-width-p
  21526. (save-excursion
  21527. (goto-char begin)
  21528. (while (< (point) end)
  21529. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21530. (replace-match
  21531. "" nil nil nil
  21532. (if (= (line-end-position) (match-end 0)) 0 1)))
  21533. (forward-line)))
  21534. (let ((min-ind (point-max)))
  21535. ;; Find minimum indentation across all lines.
  21536. (save-excursion
  21537. (goto-char begin)
  21538. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21539. (setq min-ind 0)
  21540. (catch 'zerop
  21541. (while (< (point) end)
  21542. (unless (org-looking-at-p "[ \t]*$")
  21543. (let ((ind (org-get-indentation)))
  21544. (setq min-ind (min min-ind ind))
  21545. (when (zerop ind) (throw 'zerop t))))
  21546. (forward-line)))))
  21547. ;; Loop over all lines and add fixed-width markup everywhere
  21548. ;; but in fixed-width lines.
  21549. (save-excursion
  21550. (goto-char begin)
  21551. (while (< (point) end)
  21552. (cond
  21553. ((org-at-heading-p)
  21554. (insert ": ")
  21555. (forward-line)
  21556. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21557. (insert ":")
  21558. (forward-line)))
  21559. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21560. (let* ((element (org-element-at-point))
  21561. (element-end (org-element-property :end element)))
  21562. (if (eq (org-element-type element) 'fixed-width)
  21563. (progn (goto-char element-end)
  21564. (skip-chars-backward " \r\t\n")
  21565. (forward-line))
  21566. (let ((limit (min end element-end)))
  21567. (while (< (point) limit)
  21568. (org-move-to-column min-ind t)
  21569. (insert ": ")
  21570. (forward-line))))))
  21571. (t
  21572. (org-move-to-column min-ind t)
  21573. (insert ": ")
  21574. (forward-line)))))))
  21575. (set-marker end nil))))
  21576. ;;; Comments
  21577. ;; Org comments syntax is quite complex. It requires the entire line
  21578. ;; to be just a comment. Also, even with the right syntax at the
  21579. ;; beginning of line, some some elements (i.e. verse-block or
  21580. ;; example-block) don't accept comments. Usual Emacs comment commands
  21581. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21582. ;; Org still relies on `comment-dwim', but cannot trust
  21583. ;; `comment-only-p'. So, `comment-region-function' and
  21584. ;; `uncomment-region-function' both point
  21585. ;; to`org-comment-or-uncomment-region'. Eventually,
  21586. ;; `org-insert-comment' takes care of insertion of comments at the
  21587. ;; beginning of line.
  21588. ;; `org-setup-comments-handling' install comments related variables
  21589. ;; during `org-mode' initialization.
  21590. (defun org-setup-comments-handling ()
  21591. (interactive)
  21592. (setq-local comment-use-syntax nil)
  21593. (setq-local comment-start "# ")
  21594. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21595. (setq-local comment-insert-comment-function 'org-insert-comment)
  21596. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21597. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21598. (defun org-insert-comment ()
  21599. "Insert an empty comment above current line.
  21600. If the line is empty, insert comment at its beginning. When
  21601. point is within a source block, comment according to the related
  21602. major mode."
  21603. (if (let ((element (org-element-at-point)))
  21604. (and (eq (org-element-type element) 'src-block)
  21605. (< (save-excursion
  21606. (goto-char (org-element-property :post-affiliated element))
  21607. (line-end-position))
  21608. (point))
  21609. (> (save-excursion
  21610. (goto-char (org-element-property :end element))
  21611. (skip-chars-backward " \r\t\n")
  21612. (line-beginning-position))
  21613. (point))))
  21614. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21615. (beginning-of-line)
  21616. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21617. (open-line 1))
  21618. (org-indent-line)
  21619. (insert "# ")))
  21620. (defvar comment-empty-lines) ; From newcomment.el.
  21621. (defun org-comment-or-uncomment-region (beg end &rest _)
  21622. "Comment or uncomment each non-blank line in the region.
  21623. Uncomment each non-blank line between BEG and END if it only
  21624. contains commented lines. Otherwise, comment them. If region is
  21625. strictly within a source block, use appropriate comment syntax."
  21626. (if (let ((element (org-element-at-point)))
  21627. (and (eq (org-element-type element) 'src-block)
  21628. (< (save-excursion
  21629. (goto-char (org-element-property :post-affiliated element))
  21630. (line-end-position))
  21631. beg)
  21632. (>= (save-excursion
  21633. (goto-char (org-element-property :end element))
  21634. (skip-chars-backward " \r\t\n")
  21635. (line-beginning-position))
  21636. end)))
  21637. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21638. (save-restriction
  21639. ;; Restrict region
  21640. (narrow-to-region (save-excursion (goto-char beg)
  21641. (skip-chars-forward " \r\t\n" end)
  21642. (line-beginning-position))
  21643. (save-excursion (goto-char end)
  21644. (skip-chars-backward " \r\t\n" beg)
  21645. (line-end-position)))
  21646. (let ((uncommentp
  21647. ;; UNCOMMENTP is non-nil when every non blank line between
  21648. ;; BEG and END is a comment.
  21649. (save-excursion
  21650. (goto-char (point-min))
  21651. (while (and (not (eobp))
  21652. (let ((element (org-element-at-point)))
  21653. (and (eq (org-element-type element) 'comment)
  21654. (goto-char (min (point-max)
  21655. (org-element-property
  21656. :end element)))))))
  21657. (eobp))))
  21658. (if uncommentp
  21659. ;; Only blank lines and comments in region: uncomment it.
  21660. (save-excursion
  21661. (goto-char (point-min))
  21662. (while (not (eobp))
  21663. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21664. (replace-match "" nil nil nil 1))
  21665. (forward-line)))
  21666. ;; Comment each line in region.
  21667. (let ((min-indent (point-max)))
  21668. ;; First find the minimum indentation across all lines.
  21669. (save-excursion
  21670. (goto-char (point-min))
  21671. (while (and (not (eobp)) (not (zerop min-indent)))
  21672. (unless (looking-at "[ \t]*$")
  21673. (setq min-indent (min min-indent (current-indentation))))
  21674. (forward-line)))
  21675. ;; Then loop over all lines.
  21676. (save-excursion
  21677. (goto-char (point-min))
  21678. (while (not (eobp))
  21679. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21680. ;; Don't get fooled by invisible text (e.g. link path)
  21681. ;; when moving to column MIN-INDENT.
  21682. (let ((buffer-invisibility-spec nil))
  21683. (org-move-to-column min-indent t))
  21684. (insert comment-start))
  21685. (forward-line)))))))))
  21686. (defun org-comment-dwim (_arg)
  21687. "Call `comment-dwim' within a source edit buffer if needed."
  21688. (interactive "*P")
  21689. (if (org-in-src-block-p)
  21690. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21691. (call-interactively 'comment-dwim)))
  21692. ;;; Timestamps API
  21693. ;; This section contains tools to operate on timestamp objects, as
  21694. ;; returned by, e.g. `org-element-context'.
  21695. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21696. "Encode TIMESTAMP object into Emacs internal time.
  21697. Use end of date range or time range when END is non-nil."
  21698. (apply #'encode-time
  21699. (cons 0
  21700. (mapcar
  21701. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21702. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21703. '(:minute-start :hour-start :day-start :month-start
  21704. :year-start))))))
  21705. (defun org-timestamp-has-time-p (timestamp)
  21706. "Non-nil when TIMESTAMP has a time specified."
  21707. (org-element-property :hour-start timestamp))
  21708. (defun org-timestamp-format (timestamp format &optional end utc)
  21709. "Format a TIMESTAMP object into a string.
  21710. FORMAT is a format specifier to be passed to
  21711. `format-time-string'.
  21712. When optional argument END is non-nil, use end of date-range or
  21713. time-range, if possible.
  21714. When optional argument UTC is non-nil, time will be expressed as
  21715. Universal Time."
  21716. (format-time-string
  21717. format (org-timestamp--to-internal-time timestamp end) utc))
  21718. (defun org-timestamp-split-range (timestamp &optional end)
  21719. "Extract a TIMESTAMP object from a date or time range.
  21720. END, when non-nil, means extract the end of the range.
  21721. Otherwise, extract its start.
  21722. Return a new timestamp object."
  21723. (let ((type (org-element-property :type timestamp)))
  21724. (if (memq type '(active inactive diary)) timestamp
  21725. (let ((split-ts (org-element-copy timestamp)))
  21726. ;; Set new type.
  21727. (org-element-put-property
  21728. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21729. ;; Copy start properties over end properties if END is
  21730. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21731. (let ((p-alist '((:minute-start . :minute-end)
  21732. (:hour-start . :hour-end)
  21733. (:day-start . :day-end)
  21734. (:month-start . :month-end)
  21735. (:year-start . :year-end))))
  21736. (dolist (p-cell p-alist)
  21737. (org-element-put-property
  21738. split-ts
  21739. (funcall (if end #'car #'cdr) p-cell)
  21740. (org-element-property
  21741. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21742. ;; Eventually refresh `:raw-value'.
  21743. (org-element-put-property split-ts :raw-value nil)
  21744. (org-element-put-property
  21745. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21746. (defun org-timestamp-translate (timestamp &optional boundary)
  21747. "Translate TIMESTAMP object to custom format.
  21748. Format string is defined in `org-time-stamp-custom-formats',
  21749. which see.
  21750. When optional argument BOUNDARY is non-nil, it is either the
  21751. symbol `start' or `end'. In this case, only translate the
  21752. starting or ending part of TIMESTAMP if it is a date or time
  21753. range. Otherwise, translate both parts.
  21754. Return timestamp as-is if `org-display-custom-times' is nil or if
  21755. it has a `diary' type."
  21756. (let ((type (org-element-property :type timestamp)))
  21757. (if (or (not org-display-custom-times) (eq type 'diary))
  21758. (org-element-interpret-data timestamp)
  21759. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21760. org-time-stamp-custom-formats)))
  21761. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21762. (concat (org-timestamp-format timestamp fmt)
  21763. "--"
  21764. (org-timestamp-format timestamp fmt t))
  21765. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21766. ;;; Other stuff.
  21767. (defvar reftex-docstruct-symbol)
  21768. (defvar reftex-cite-format)
  21769. (defvar org--rds)
  21770. (defun org-reftex-citation ()
  21771. "Use reftex-citation to insert a citation into the buffer.
  21772. This looks for a line like
  21773. #+BIBLIOGRAPHY: foo plain option:-d
  21774. and derives from it that foo.bib is the bibliography file relevant
  21775. for this document. It then installs the necessary environment for RefTeX
  21776. to work in this buffer and calls `reftex-citation' to insert a citation
  21777. into the buffer.
  21778. Export of such citations to both LaTeX and HTML is handled by the contributed
  21779. package ox-bibtex by Taru Karttunen."
  21780. (interactive)
  21781. (let ((reftex-docstruct-symbol 'org--rds)
  21782. (reftex-cite-format "\\cite{%l}")
  21783. org--rds bib)
  21784. (save-excursion
  21785. (save-restriction
  21786. (widen)
  21787. (let ((case-fold-search t)
  21788. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21789. (if (not (save-excursion
  21790. (or (re-search-forward re nil t)
  21791. (re-search-backward re nil t))))
  21792. (user-error "No bibliography defined in file")
  21793. (setq bib (concat (match-string 1) ".bib")
  21794. org--rds (list (list 'bib bib)))))))
  21795. (call-interactively 'reftex-citation)))
  21796. ;;;; Functions extending outline functionality
  21797. (defun org-beginning-of-line (&optional arg)
  21798. "Go to the beginning of the current line. If that is invisible, continue
  21799. to a visible line beginning. This makes the function of C-a more intuitive.
  21800. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21801. first attempt, and only move to after the tags when the cursor is already
  21802. beyond the end of the headline."
  21803. (interactive "P")
  21804. (let ((pos (point))
  21805. (special (if (consp org-special-ctrl-a/e)
  21806. (car org-special-ctrl-a/e)
  21807. org-special-ctrl-a/e))
  21808. deactivate-mark refpos)
  21809. (if (org-bound-and-true-p visual-line-mode)
  21810. (beginning-of-visual-line 1)
  21811. (beginning-of-line 1))
  21812. (if (and arg (fboundp 'move-beginning-of-line))
  21813. (call-interactively 'move-beginning-of-line)
  21814. (unless (bobp)
  21815. (backward-char 1)
  21816. (if (org-truely-invisible-p)
  21817. (while (and (not (bobp)) (org-truely-invisible-p))
  21818. (backward-char 1)
  21819. (beginning-of-line 1))
  21820. (forward-char 1))))
  21821. (when special
  21822. (cond
  21823. ((and (looking-at org-complex-heading-regexp)
  21824. (eq (char-after (match-end 1)) ?\s))
  21825. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21826. (point-at-eol)))
  21827. (goto-char
  21828. (if (eq special t)
  21829. (cond ((> pos refpos) refpos)
  21830. ((= pos (point)) refpos)
  21831. (t (point)))
  21832. (cond ((> pos (point)) (point))
  21833. ((not (eq last-command this-command)) (point))
  21834. (t refpos)))))
  21835. ((org-at-item-p)
  21836. ;; Being at an item and not looking at an the item means point
  21837. ;; was previously moved to beginning of a visual line, which
  21838. ;; doesn't contain the item. Therefore, do nothing special,
  21839. ;; just stay here.
  21840. (when (looking-at org-list-full-item-re)
  21841. ;; Set special position at first white space character after
  21842. ;; bullet, and check-box, if any.
  21843. (let ((after-bullet
  21844. (let ((box (match-end 3)))
  21845. (if (not box) (match-end 1)
  21846. (let ((after (char-after box)))
  21847. (if (and after (= after ? )) (1+ box) box))))))
  21848. ;; Special case: Move point to special position when
  21849. ;; currently after it or at beginning of line.
  21850. (if (eq special t)
  21851. (when (or (> pos after-bullet) (= (point) pos))
  21852. (goto-char after-bullet))
  21853. ;; Reversed case: Move point to special position when
  21854. ;; point was already at beginning of line and command is
  21855. ;; repeated.
  21856. (when (and (= (point) pos) (eq last-command this-command))
  21857. (goto-char after-bullet))))))))
  21858. (org-no-warnings
  21859. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21860. (setq disable-point-adjustment
  21861. (or (not (invisible-p (point)))
  21862. (not (invisible-p (max (point-min) (1- (point))))))))
  21863. (defun org-end-of-line (&optional arg)
  21864. "Go to the end of the line.
  21865. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21866. tags on the first attempt, and only move to after the tags when
  21867. the cursor is already beyond the end of the headline."
  21868. (interactive "P")
  21869. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21870. org-special-ctrl-a/e))
  21871. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21872. 'end-of-visual-line)
  21873. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21874. (t 'end-of-line)))
  21875. deactivate-mark)
  21876. (if (or (not special) arg) (call-interactively move-fun)
  21877. (let* ((element (save-excursion (beginning-of-line)
  21878. (org-element-at-point)))
  21879. (type (org-element-type element)))
  21880. (cond
  21881. ((memq type '(headline inlinetask))
  21882. (let ((pos (point)))
  21883. (beginning-of-line 1)
  21884. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21885. (if (eq special t)
  21886. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21887. (goto-char (match-beginning 1))
  21888. (goto-char (match-end 0)))
  21889. (if (or (< pos (match-end 0))
  21890. (not (eq this-command last-command)))
  21891. (goto-char (match-end 0))
  21892. (goto-char (match-beginning 1))))
  21893. (call-interactively move-fun))))
  21894. ((outline-invisible-p (line-end-position))
  21895. ;; If element is hidden, `move-end-of-line' would put point
  21896. ;; after it. Use `end-of-line' to stay on current line.
  21897. (call-interactively 'end-of-line))
  21898. (t (call-interactively move-fun)))))
  21899. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21900. (setq disable-point-adjustment
  21901. (or (not (invisible-p (point)))
  21902. (not (invisible-p (max (point-min) (1- (point))))))))
  21903. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21904. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21905. (defun org-backward-sentence (&optional _arg)
  21906. "Go to beginning of sentence, or beginning of table field.
  21907. This will call `backward-sentence' or `org-table-beginning-of-field',
  21908. depending on context."
  21909. (interactive)
  21910. (let* ((element (org-element-at-point))
  21911. (contents-begin (org-element-property :contents-begin element))
  21912. (table (org-element-lineage element '(table) t)))
  21913. (if (and table
  21914. (> (point) contents-begin)
  21915. (<= (point) (org-element-property :contents-end table)))
  21916. (call-interactively #'org-table-beginning-of-field)
  21917. (save-restriction
  21918. (when (and contents-begin
  21919. (< (point-min) contents-begin)
  21920. (> (point) contents-begin))
  21921. (narrow-to-region contents-begin
  21922. (org-element-property :contents-end element)))
  21923. (call-interactively #'backward-sentence)))))
  21924. (defun org-forward-sentence (&optional _arg)
  21925. "Go to end of sentence, or end of table field.
  21926. This will call `forward-sentence' or `org-table-end-of-field',
  21927. depending on context."
  21928. (interactive)
  21929. (let* ((element (org-element-at-point))
  21930. (contents-end (org-element-property :contents-end element))
  21931. (table (org-element-lineage element '(table) t)))
  21932. (if (and table
  21933. (>= (point) (org-element-property :contents-begin table))
  21934. (< (point) contents-end))
  21935. (call-interactively #'org-table-end-of-field)
  21936. (save-restriction
  21937. (when (and contents-end
  21938. (> (point-max) contents-end)
  21939. ;; Skip blank lines between elements.
  21940. (< (org-element-property :end element)
  21941. (save-excursion (goto-char contents-end)
  21942. (skip-chars-forward " \r\t\n"))))
  21943. (narrow-to-region (org-element-property :contents-begin element)
  21944. contents-end))
  21945. (call-interactively #'forward-sentence)))))
  21946. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21947. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21948. (defun org-kill-line (&optional _arg)
  21949. "Kill line, to tags or end of line."
  21950. (interactive)
  21951. (cond
  21952. ((or (not org-special-ctrl-k)
  21953. (bolp)
  21954. (not (org-at-heading-p)))
  21955. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21956. org-ctrl-k-protect-subtree
  21957. (or (eq org-ctrl-k-protect-subtree 'error)
  21958. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  21959. (user-error "C-k aborted as it would kill a hidden subtree"))
  21960. (call-interactively
  21961. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21962. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21963. (kill-region (point) (match-beginning 1))
  21964. (org-set-tags nil t))
  21965. (t (kill-region (point) (point-at-eol)))))
  21966. (define-key org-mode-map "\C-k" 'org-kill-line)
  21967. (defun org-yank (&optional arg)
  21968. "Yank. If the kill is a subtree, treat it specially.
  21969. This command will look at the current kill and check if is a single
  21970. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21971. cursor is at the beginning of a line or after the stars of a currently
  21972. empty headline, then the yank is handled specially. How exactly depends
  21973. on the value of the following variables, both set by default.
  21974. `org-yank-folded-subtrees'
  21975. When set, the subtree(s) will be folded after insertion, but only
  21976. if doing so would now swallow text after the yanked text.
  21977. `org-yank-adjusted-subtrees'
  21978. When set, the subtree will be promoted or demoted in order to
  21979. fit into the local outline tree structure, which means that the
  21980. level will be adjusted so that it becomes the smaller one of the
  21981. two *visible* surrounding headings.
  21982. Any prefix to this command will cause `yank' to be called directly with
  21983. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21984. will just
  21985. plainly yank the text as it is.
  21986. \[1] The test checks if the first non-white line is a heading
  21987. and if there are no other headings with fewer stars."
  21988. (interactive "P")
  21989. (org-yank-generic 'yank arg))
  21990. (defun org-yank-generic (command arg)
  21991. "Perform some yank-like command.
  21992. This function implements the behavior described in the `org-yank'
  21993. documentation. However, it has been generalized to work for any
  21994. interactive command with similar behavior."
  21995. ;; pretend to be command COMMAND
  21996. (setq this-command command)
  21997. (if arg
  21998. (call-interactively command)
  21999. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22000. (and (org-kill-is-subtree-p)
  22001. (or (bolp)
  22002. (and (looking-at "[ \t]*$")
  22003. (string-match
  22004. "\\`\\*+\\'"
  22005. (buffer-substring (point-at-bol) (point)))))))
  22006. swallowp)
  22007. (cond
  22008. ((and subtreep org-yank-folded-subtrees)
  22009. (let ((beg (point))
  22010. end)
  22011. (if (and subtreep org-yank-adjusted-subtrees)
  22012. (org-paste-subtree nil nil 'for-yank)
  22013. (call-interactively command))
  22014. (setq end (point))
  22015. (goto-char beg)
  22016. (when (and (bolp) subtreep
  22017. (not (setq swallowp
  22018. (org-yank-folding-would-swallow-text beg end))))
  22019. (org-with-limited-levels
  22020. (or (looking-at org-outline-regexp)
  22021. (re-search-forward org-outline-regexp-bol end t))
  22022. (while (and (< (point) end) (looking-at org-outline-regexp))
  22023. (outline-hide-subtree)
  22024. (org-cycle-show-empty-lines 'folded)
  22025. (condition-case nil
  22026. (outline-forward-same-level 1)
  22027. (error (goto-char end))))))
  22028. (when swallowp
  22029. (message
  22030. "Inserted text not folded because that would swallow text"))
  22031. (goto-char end)
  22032. (skip-chars-forward " \t\n\r")
  22033. (beginning-of-line 1)
  22034. (push-mark beg 'nomsg)))
  22035. ((and subtreep org-yank-adjusted-subtrees)
  22036. (let ((beg (point-at-bol)))
  22037. (org-paste-subtree nil nil 'for-yank)
  22038. (push-mark beg 'nomsg)))
  22039. (t
  22040. (call-interactively command))))))
  22041. (defun org-yank-folding-would-swallow-text (beg end)
  22042. "Would hide-subtree at BEG swallow any text after END?"
  22043. (let (level)
  22044. (org-with-limited-levels
  22045. (save-excursion
  22046. (goto-char beg)
  22047. (when (or (looking-at org-outline-regexp)
  22048. (re-search-forward org-outline-regexp-bol end t))
  22049. (setq level (org-outline-level)))
  22050. (goto-char end)
  22051. (skip-chars-forward " \t\r\n\v\f")
  22052. (not (or (eobp)
  22053. (and (bolp) (looking-at-p org-outline-regexp)
  22054. (<= (org-outline-level) level))))))))
  22055. (define-key org-mode-map "\C-y" 'org-yank)
  22056. (defun org-truely-invisible-p ()
  22057. "Check if point is at a character currently not visible.
  22058. This version does not only check the character property, but also
  22059. `visible-mode'."
  22060. ;; Early versions of noutline don't have `outline-invisible-p'.
  22061. (unless (org-bound-and-true-p visible-mode)
  22062. (outline-invisible-p)))
  22063. (defun org-invisible-p2 ()
  22064. "Check if point is at a character currently not visible."
  22065. (save-excursion
  22066. (when (and (eolp) (not (bobp))) (backward-char 1))
  22067. ;; Early versions of noutline don't have `outline-invisible-p'.
  22068. (outline-invisible-p)))
  22069. (defun org-back-to-heading (&optional invisible-ok)
  22070. "Call `outline-back-to-heading', but provide a better error message."
  22071. (condition-case nil
  22072. (outline-back-to-heading invisible-ok)
  22073. (error (error "Before first headline at position %d in buffer %s"
  22074. (point) (current-buffer)))))
  22075. (defun org-before-first-heading-p ()
  22076. "Before first heading?"
  22077. (save-excursion
  22078. (end-of-line)
  22079. (null (re-search-backward org-outline-regexp-bol nil t))))
  22080. (defun org-at-heading-p (&optional ignored)
  22081. (outline-on-heading-p t))
  22082. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22083. ;;; warning was only added in Org 9.0, which should be taken into
  22084. ;;; account when deciding when to remove the alias.
  22085. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22086. (defun org-in-commented-heading-p (&optional no-inheritance)
  22087. "Non-nil if point is under a commented heading.
  22088. This function also checks ancestors of the current headline,
  22089. unless optional argument NO-INHERITANCE is non-nil."
  22090. (cond
  22091. ((org-before-first-heading-p) nil)
  22092. ((let ((headline (nth 4 (org-heading-components))))
  22093. (and headline
  22094. (let ((case-fold-search nil))
  22095. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22096. headline)))))
  22097. (no-inheritance nil)
  22098. (t
  22099. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22100. (defun org-at-comment-p nil
  22101. "Is cursor in a commented line?"
  22102. (save-excursion
  22103. (save-match-data
  22104. (beginning-of-line)
  22105. (looking-at "^[ \t]*# "))))
  22106. (defun org-at-drawer-p nil
  22107. "Is cursor at a drawer keyword?"
  22108. (save-excursion
  22109. (move-beginning-of-line 1)
  22110. (looking-at org-drawer-regexp)))
  22111. (defun org-at-block-p nil
  22112. "Is cursor at a block keyword?"
  22113. (save-excursion
  22114. (move-beginning-of-line 1)
  22115. (looking-at org-block-regexp)))
  22116. (defun org-point-at-end-of-empty-headline ()
  22117. "If point is at the end of an empty headline, return t, else nil.
  22118. If the heading only contains a TODO keyword, it is still still considered
  22119. empty."
  22120. (and (looking-at "[ \t]*$")
  22121. (when org-todo-line-regexp
  22122. (save-excursion
  22123. (beginning-of-line 1)
  22124. (let ((case-fold-search nil))
  22125. (looking-at org-todo-line-regexp)
  22126. (string= (match-string 3) ""))))))
  22127. (defun org-at-heading-or-item-p ()
  22128. (or (org-at-heading-p) (org-at-item-p)))
  22129. (defun org-at-target-p ()
  22130. (or (org-in-regexp org-radio-target-regexp)
  22131. (org-in-regexp org-target-regexp)))
  22132. ;; Compatibility alias with Org versions < 7.8.03
  22133. (defalias 'org-on-target-p 'org-at-target-p)
  22134. (defun org-up-heading-all (arg)
  22135. "Move to the heading line of which the present line is a subheading.
  22136. This function considers both visible and invisible heading lines.
  22137. With argument, move up ARG levels."
  22138. (if (fboundp 'outline-up-heading-all)
  22139. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22140. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22141. (defun org-up-heading-safe ()
  22142. "Move to the heading line of which the present line is a subheading.
  22143. This version will not throw an error. It will return the level of the
  22144. headline found, or nil if no higher level is found.
  22145. Also, this function will be a lot faster than `outline-up-heading',
  22146. because it relies on stars being the outline starters. This can really
  22147. make a significant difference in outlines with very many siblings."
  22148. (when (ignore-errors (org-back-to-heading t))
  22149. (let ((level-up (1- (funcall outline-level))))
  22150. (and (> level-up 0)
  22151. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22152. (funcall outline-level)))))
  22153. (defun org-first-sibling-p ()
  22154. "Is this heading the first child of its parents?"
  22155. (interactive)
  22156. (let ((re org-outline-regexp-bol)
  22157. level l)
  22158. (unless (org-at-heading-p t)
  22159. (user-error "Not at a heading"))
  22160. (setq level (funcall outline-level))
  22161. (save-excursion
  22162. (if (not (re-search-backward re nil t))
  22163. t
  22164. (setq l (funcall outline-level))
  22165. (< l level)))))
  22166. (defun org-goto-sibling (&optional previous)
  22167. "Goto the next sibling, even if it is invisible.
  22168. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22169. when a sibling was found. When none is found, return nil and don't
  22170. move point."
  22171. (let ((fun (if previous 're-search-backward 're-search-forward))
  22172. (pos (point))
  22173. (re org-outline-regexp-bol)
  22174. level l)
  22175. (when (ignore-errors (org-back-to-heading t))
  22176. (setq level (funcall outline-level))
  22177. (catch 'exit
  22178. (or previous (forward-char 1))
  22179. (while (funcall fun re nil t)
  22180. (setq l (funcall outline-level))
  22181. (when (< l level) (goto-char pos) (throw 'exit nil))
  22182. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22183. (goto-char pos)
  22184. nil))))
  22185. (defun org-show-siblings ()
  22186. "Show all siblings of the current headline."
  22187. (save-excursion
  22188. (while (org-goto-sibling) (org-flag-heading nil)))
  22189. (save-excursion
  22190. (while (org-goto-sibling 'previous)
  22191. (org-flag-heading nil))))
  22192. (defun org-goto-first-child ()
  22193. "Goto the first child, even if it is invisible.
  22194. Return t when a child was found. Otherwise don't move point and
  22195. return nil."
  22196. (let (level (pos (point)) (re org-outline-regexp-bol))
  22197. (when (ignore-errors (org-back-to-heading t))
  22198. (setq level (outline-level))
  22199. (forward-char 1)
  22200. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22201. (progn (goto-char (match-beginning 0)) t)
  22202. (goto-char pos) nil))))
  22203. (defun org-show-hidden-entry ()
  22204. "Show an entry where even the heading is hidden."
  22205. (save-excursion
  22206. (org-show-entry)))
  22207. (defun org-flag-heading (flag &optional entry)
  22208. "Flag the current heading. FLAG non-nil means make invisible.
  22209. When ENTRY is non-nil, show the entire entry."
  22210. (save-excursion
  22211. (org-back-to-heading t)
  22212. ;; Check if we should show the entire entry
  22213. (if entry
  22214. (progn
  22215. (org-show-entry)
  22216. (save-excursion
  22217. (and (outline-next-heading)
  22218. (org-flag-heading nil))))
  22219. (outline-flag-region (max (point-min) (1- (point)))
  22220. (save-excursion (outline-end-of-heading) (point))
  22221. flag))))
  22222. (defun org-get-next-sibling ()
  22223. "Move to next heading of the same level, and return point.
  22224. If there is no such heading, return nil.
  22225. This is like outline-next-sibling, but invisible headings are ok."
  22226. (let ((level (funcall outline-level)))
  22227. (outline-next-heading)
  22228. (while (and (not (eobp)) (> (funcall outline-level) level))
  22229. (outline-next-heading))
  22230. (unless (or (eobp) (< (funcall outline-level) level))
  22231. (point))))
  22232. (defun org-get-last-sibling ()
  22233. "Move to previous heading of the same level, and return point.
  22234. If there is no such heading, return nil."
  22235. (let ((opoint (point))
  22236. (level (funcall outline-level)))
  22237. (outline-previous-heading)
  22238. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22239. (while (and (> (funcall outline-level) level)
  22240. (not (bobp)))
  22241. (outline-previous-heading))
  22242. (unless (< (funcall outline-level) level)
  22243. (point)))))
  22244. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22245. "Goto to the end of a subtree."
  22246. ;; This contains an exact copy of the original function, but it uses
  22247. ;; `org-back-to-heading', to make it work also in invisible
  22248. ;; trees. And is uses an invisible-ok argument.
  22249. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22250. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22251. ;; with many children and grandchildren etc, this can be much faster
  22252. ;; than the outline version.
  22253. (org-back-to-heading invisible-ok)
  22254. (let ((first t)
  22255. (level (funcall outline-level)))
  22256. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22257. ;; A true heading (not a plain list item), in Org-mode
  22258. ;; This means we can easily find the end by looking
  22259. ;; only for the right number of stars. Using a regexp to do
  22260. ;; this is so much faster than using a Lisp loop.
  22261. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22262. (forward-char 1)
  22263. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22264. ;; something else, do it the slow way
  22265. (while (and (not (eobp))
  22266. (or first (> (funcall outline-level) level)))
  22267. (setq first nil)
  22268. (outline-next-heading)))
  22269. (unless to-heading
  22270. (when (memq (preceding-char) '(?\n ?\^M))
  22271. ;; Go to end of line before heading
  22272. (forward-char -1)
  22273. (when (memq (preceding-char) '(?\n ?\^M))
  22274. ;; leave blank line before heading
  22275. (forward-char -1)))))
  22276. (point))
  22277. (defun org-end-of-meta-data (&optional full)
  22278. "Skip planning line and properties drawer in current entry.
  22279. When optional argument FULL is non-nil, also skip empty lines,
  22280. clocking lines and regular drawers at the beginning of the
  22281. entry."
  22282. (org-back-to-heading t)
  22283. (forward-line)
  22284. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22285. (when (looking-at org-property-drawer-re)
  22286. (goto-char (match-end 0))
  22287. (forward-line))
  22288. (when (and full (not (org-at-heading-p)))
  22289. (catch 'exit
  22290. (let ((end (save-excursion (outline-next-heading) (point)))
  22291. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22292. (while (not (eobp))
  22293. (cond ((org-looking-at-p org-drawer-regexp)
  22294. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22295. (forward-line)
  22296. (throw 'exit t)))
  22297. ((org-looking-at-p re) (forward-line))
  22298. (t (throw 'exit t))))))))
  22299. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22300. "Move forward to the ARG'th subheading at same level as this one.
  22301. Stop at the first and last subheadings of a superior heading.
  22302. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22303. non-nil it will also look at invisible ones."
  22304. (interactive "p")
  22305. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22306. (if (and arg (< arg 0))
  22307. (goto-char (point-min))
  22308. (outline-next-heading))
  22309. (org-at-heading-p)
  22310. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22311. (f (if (and arg (< arg 0))
  22312. 're-search-backward
  22313. 're-search-forward))
  22314. (count (if arg (abs arg) 1))
  22315. (result (point)))
  22316. (while (and (prog1 (> count 0)
  22317. (forward-char (if (and arg (< arg 0)) -1 1)))
  22318. (funcall f org-outline-regexp-bol nil 'move))
  22319. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22320. (cond ((< l level) (setq count 0))
  22321. ((and (= l level)
  22322. (or invisible-ok
  22323. (progn
  22324. (goto-char (line-beginning-position))
  22325. (not (outline-invisible-p)))))
  22326. (setq count (1- count))
  22327. (when (eq l level)
  22328. (setq result (point)))))))
  22329. (goto-char result))
  22330. (beginning-of-line 1)))
  22331. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22332. "Move backward to the ARG'th subheading at same level as this one.
  22333. Stop at the first and last subheadings of a superior heading."
  22334. (interactive "p")
  22335. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22336. (defun org-next-visible-heading (arg)
  22337. "Move to the next visible heading.
  22338. This function wraps `outline-next-visible-heading' with
  22339. `org-with-limited-levels' in order to skip over inline tasks and
  22340. respect customization of `org-odd-levels-only'."
  22341. (interactive "p")
  22342. (org-with-limited-levels
  22343. (outline-next-visible-heading arg)))
  22344. (defun org-previous-visible-heading (arg)
  22345. "Move to the next visible heading.
  22346. This function wraps `outline-previous-visible-heading' with
  22347. `org-with-limited-levels' in order to skip over inline tasks and
  22348. respect customization of `org-odd-levels-only'."
  22349. (interactive "p")
  22350. (org-with-limited-levels
  22351. (outline-previous-visible-heading arg)))
  22352. (defun org-next-block (arg &optional backward block-regexp)
  22353. "Jump to the next block.
  22354. With a prefix argument ARG, jump forward ARG many blocks.
  22355. When BACKWARD is non-nil, jump to the previous block.
  22356. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22357. Match data is set according to this regexp when the function
  22358. returns.
  22359. Return point at beginning of the opening line of found block.
  22360. Throw an error if no block is found."
  22361. (interactive "p")
  22362. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22363. (case-fold-search t)
  22364. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22365. (count (or arg 1))
  22366. (origin (point))
  22367. last-element)
  22368. (if backward (beginning-of-line) (end-of-line))
  22369. (while (and (> count 0) (funcall search-fn re nil t))
  22370. (let ((element (save-excursion
  22371. (goto-char (match-beginning 0))
  22372. (save-match-data (org-element-at-point)))))
  22373. (when (and (memq (org-element-type element)
  22374. '(center-block comment-block dynamic-block
  22375. example-block export-block quote-block
  22376. special-block src-block verse-block))
  22377. (<= (match-beginning 0)
  22378. (org-element-property :post-affiliated element)))
  22379. (setq last-element element)
  22380. (cl-decf count))))
  22381. (if (= count 0)
  22382. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22383. (save-match-data (org-show-context)))
  22384. (goto-char origin)
  22385. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22386. (defun org-previous-block (arg &optional block-regexp)
  22387. "Jump to the previous block.
  22388. With a prefix argument ARG, jump backward ARG many source blocks.
  22389. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22390. (interactive "p")
  22391. (org-next-block arg t block-regexp))
  22392. (defun org-forward-paragraph ()
  22393. "Move forward to beginning of next paragraph or equivalent.
  22394. The function moves point to the beginning of the next visible
  22395. structural element, which can be a paragraph, a table, a list
  22396. item, etc. It also provides some special moves for convenience:
  22397. - On an affiliated keyword, jump to the beginning of the
  22398. relative element.
  22399. - On an item or a footnote definition, move to the second
  22400. element inside, if any.
  22401. - On a table or a property drawer, jump after it.
  22402. - On a verse or source block, stop after blank lines."
  22403. (interactive)
  22404. (when (eobp) (user-error "Cannot move further down"))
  22405. (let* ((deactivate-mark nil)
  22406. (element (org-element-at-point))
  22407. (type (org-element-type element))
  22408. (post-affiliated (org-element-property :post-affiliated element))
  22409. (contents-begin (org-element-property :contents-begin element))
  22410. (contents-end (org-element-property :contents-end element))
  22411. (end (let ((end (org-element-property :end element)) (parent element))
  22412. (while (and (setq parent (org-element-property :parent parent))
  22413. (= (org-element-property :contents-end parent) end))
  22414. (setq end (org-element-property :end parent)))
  22415. end)))
  22416. (cond ((not element)
  22417. (skip-chars-forward " \r\t\n")
  22418. (or (eobp) (beginning-of-line)))
  22419. ;; On affiliated keywords, move to element's beginning.
  22420. ((< (point) post-affiliated)
  22421. (goto-char post-affiliated))
  22422. ;; At a table row, move to the end of the table. Similarly,
  22423. ;; at a node property, move to the end of the property
  22424. ;; drawer.
  22425. ((memq type '(node-property table-row))
  22426. (goto-char (org-element-property
  22427. :end (org-element-property :parent element))))
  22428. ((memq type '(property-drawer table)) (goto-char end))
  22429. ;; Consider blank lines as separators in verse and source
  22430. ;; blocks to ease editing.
  22431. ((memq type '(src-block verse-block))
  22432. (when (eq type 'src-block)
  22433. (setq contents-end
  22434. (save-excursion (goto-char end)
  22435. (skip-chars-backward " \r\t\n")
  22436. (line-beginning-position))))
  22437. (beginning-of-line)
  22438. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22439. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22440. (goto-char end)
  22441. (skip-chars-forward " \r\t\n")
  22442. (if (= (point) contents-end) (goto-char end)
  22443. (beginning-of-line))))
  22444. ;; With no contents, just skip element.
  22445. ((not contents-begin) (goto-char end))
  22446. ;; If contents are invisible, skip the element altogether.
  22447. ((outline-invisible-p (line-end-position))
  22448. (cl-case type
  22449. (headline
  22450. (org-with-limited-levels (outline-next-visible-heading 1)))
  22451. ;; At a plain list, make sure we move to the next item
  22452. ;; instead of skipping the whole list.
  22453. (plain-list (forward-char)
  22454. (org-forward-paragraph))
  22455. (otherwise (goto-char end))))
  22456. ((>= (point) contents-end) (goto-char end))
  22457. ((>= (point) contents-begin)
  22458. ;; This can only happen on paragraphs and plain lists.
  22459. (cl-case type
  22460. (paragraph (goto-char end))
  22461. ;; At a plain list, try to move to second element in
  22462. ;; first item, if possible.
  22463. (plain-list (end-of-line)
  22464. (org-forward-paragraph))))
  22465. ;; When contents start on the middle of a line (e.g. in
  22466. ;; items and footnote definitions), try to reach first
  22467. ;; element starting after current line.
  22468. ((> (line-end-position) contents-begin)
  22469. (end-of-line)
  22470. (org-forward-paragraph))
  22471. (t (goto-char contents-begin)))))
  22472. (defun org-backward-paragraph ()
  22473. "Move backward to start of previous paragraph or equivalent.
  22474. The function moves point to the beginning of the current
  22475. structural element, which can be a paragraph, a table, a list
  22476. item, etc., or to the beginning of the previous visible one if
  22477. point is already there. It also provides some special moves for
  22478. convenience:
  22479. - On an affiliated keyword, jump to the first one.
  22480. - On a table or a property drawer, move to its beginning.
  22481. - On a verse or source block, stop before blank lines."
  22482. (interactive)
  22483. (when (bobp) (user-error "Cannot move further up"))
  22484. (let* ((deactivate-mark nil)
  22485. (element (org-element-at-point))
  22486. (type (org-element-type element))
  22487. (contents-begin (org-element-property :contents-begin element))
  22488. (contents-end (org-element-property :contents-end element))
  22489. (post-affiliated (org-element-property :post-affiliated element))
  22490. (begin (org-element-property :begin element)))
  22491. (cond
  22492. ((not element) (goto-char (point-min)))
  22493. ((= (point) begin)
  22494. (backward-char)
  22495. (org-backward-paragraph))
  22496. ((<= (point) post-affiliated) (goto-char begin))
  22497. ((memq type '(node-property table-row))
  22498. (goto-char (org-element-property
  22499. :post-affiliated (org-element-property :parent element))))
  22500. ((memq type '(property-drawer table)) (goto-char begin))
  22501. ((memq type '(src-block verse-block))
  22502. (when (eq type 'src-block)
  22503. (setq contents-begin
  22504. (save-excursion (goto-char begin) (forward-line) (point))))
  22505. (if (= (point) contents-begin) (goto-char post-affiliated)
  22506. ;; Inside a verse block, see blank lines as paragraph
  22507. ;; separators.
  22508. (let ((origin (point)))
  22509. (skip-chars-backward " \r\t\n" contents-begin)
  22510. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22511. (skip-chars-forward " \r\t\n" origin)
  22512. (if (= (point) origin) (goto-char contents-begin)
  22513. (beginning-of-line))))))
  22514. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22515. ((eq type 'paragraph)
  22516. (goto-char contents-begin)
  22517. ;; When at first paragraph in an item or a footnote definition,
  22518. ;; move directly to beginning of line.
  22519. (let ((parent-contents
  22520. (org-element-property
  22521. :contents-begin (org-element-property :parent element))))
  22522. (when (and parent-contents (= parent-contents contents-begin))
  22523. (beginning-of-line))))
  22524. ;; At the end of a greater element, move to the beginning of the
  22525. ;; last element within.
  22526. ((>= (point) contents-end)
  22527. (goto-char (1- contents-end))
  22528. (org-backward-paragraph))
  22529. (t (goto-char (or post-affiliated begin))))
  22530. ;; Ensure we never leave point invisible.
  22531. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22532. (defun org-forward-element ()
  22533. "Move forward by one element.
  22534. Move to the next element at the same level, when possible."
  22535. (interactive)
  22536. (cond ((eobp) (user-error "Cannot move further down"))
  22537. ((org-with-limited-levels (org-at-heading-p))
  22538. (let ((origin (point)))
  22539. (goto-char (org-end-of-subtree nil t))
  22540. (unless (org-with-limited-levels (org-at-heading-p))
  22541. (goto-char origin)
  22542. (user-error "Cannot move further down"))))
  22543. (t
  22544. (let* ((elem (org-element-at-point))
  22545. (end (org-element-property :end elem))
  22546. (parent (org-element-property :parent elem)))
  22547. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22548. (goto-char (org-element-property :end parent)))
  22549. ((integer-or-marker-p end) (goto-char end))
  22550. (t (message "No element at point")))))))
  22551. (defun org-backward-element ()
  22552. "Move backward by one element.
  22553. Move to the previous element at the same level, when possible."
  22554. (interactive)
  22555. (cond ((bobp) (user-error "Cannot move further up"))
  22556. ((org-with-limited-levels (org-at-heading-p))
  22557. ;; At a headline, move to the previous one, if any, or stay
  22558. ;; here.
  22559. (let ((origin (point)))
  22560. (org-with-limited-levels (org-backward-heading-same-level 1))
  22561. ;; When current headline has no sibling above, move to its
  22562. ;; parent.
  22563. (when (= (point) origin)
  22564. (or (org-with-limited-levels (org-up-heading-safe))
  22565. (progn (goto-char origin)
  22566. (user-error "Cannot move further up"))))))
  22567. (t
  22568. (let* ((elem (org-element-at-point))
  22569. (beg (org-element-property :begin elem)))
  22570. (cond
  22571. ;; Move to beginning of current element if point isn't
  22572. ;; there already.
  22573. ((null beg) (message "No element at point"))
  22574. ((/= (point) beg) (goto-char beg))
  22575. (t (goto-char beg)
  22576. (skip-chars-backward " \r\t\n")
  22577. (unless (bobp)
  22578. (let ((prev (org-element-at-point)))
  22579. (goto-char (org-element-property :begin prev))
  22580. (while (and (setq prev (org-element-property :parent prev))
  22581. (<= (org-element-property :end prev) beg))
  22582. (goto-char (org-element-property :begin prev)))))))))))
  22583. (defun org-up-element ()
  22584. "Move to upper element."
  22585. (interactive)
  22586. (if (org-with-limited-levels (org-at-heading-p))
  22587. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22588. (let* ((elem (org-element-at-point))
  22589. (parent (org-element-property :parent elem)))
  22590. (if parent (goto-char (org-element-property :begin parent))
  22591. (if (org-with-limited-levels (org-before-first-heading-p))
  22592. (user-error "No surrounding element")
  22593. (org-with-limited-levels (org-back-to-heading)))))))
  22594. (defvar org-element-greater-elements)
  22595. (defun org-down-element ()
  22596. "Move to inner element."
  22597. (interactive)
  22598. (let ((element (org-element-at-point)))
  22599. (cond
  22600. ((memq (org-element-type element) '(plain-list table))
  22601. (goto-char (org-element-property :contents-begin element))
  22602. (forward-char))
  22603. ((memq (org-element-type element) org-element-greater-elements)
  22604. ;; If contents are hidden, first disclose them.
  22605. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22606. (goto-char (or (org-element-property :contents-begin element)
  22607. (user-error "No content for this element"))))
  22608. (t (user-error "No inner element")))))
  22609. (defun org-drag-element-backward ()
  22610. "Move backward element at point."
  22611. (interactive)
  22612. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22613. (let* ((elem (org-element-at-point))
  22614. (prev-elem
  22615. (save-excursion
  22616. (goto-char (org-element-property :begin elem))
  22617. (skip-chars-backward " \r\t\n")
  22618. (unless (bobp)
  22619. (let* ((beg (org-element-property :begin elem))
  22620. (prev (org-element-at-point))
  22621. (up prev))
  22622. (while (and (setq up (org-element-property :parent up))
  22623. (<= (org-element-property :end up) beg))
  22624. (setq prev up))
  22625. prev)))))
  22626. ;; Error out if no previous element or previous element is
  22627. ;; a parent of the current one.
  22628. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22629. (user-error "Cannot drag element backward")
  22630. (let ((pos (point)))
  22631. (org-element-swap-A-B prev-elem elem)
  22632. (goto-char (+ (org-element-property :begin prev-elem)
  22633. (- pos (org-element-property :begin elem)))))))))
  22634. (defun org-drag-element-forward ()
  22635. "Move forward element at point."
  22636. (interactive)
  22637. (let* ((pos (point))
  22638. (elem (org-element-at-point)))
  22639. (when (= (point-max) (org-element-property :end elem))
  22640. (user-error "Cannot drag element forward"))
  22641. (goto-char (org-element-property :end elem))
  22642. (let ((next-elem (org-element-at-point)))
  22643. (when (or (org-element-nested-p elem next-elem)
  22644. (and (eq (org-element-type next-elem) 'headline)
  22645. (not (eq (org-element-type elem) 'headline))))
  22646. (goto-char pos)
  22647. (user-error "Cannot drag element forward"))
  22648. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22649. ;; body's length (without final blanks) and by the length of
  22650. ;; blanks between ELEM and NEXT-ELEM.
  22651. (let ((size-next (- (save-excursion
  22652. (goto-char (org-element-property :end next-elem))
  22653. (skip-chars-backward " \r\t\n")
  22654. (forward-line)
  22655. ;; Small correction if buffer doesn't end
  22656. ;; with a newline character.
  22657. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22658. (org-element-property :begin next-elem)))
  22659. (size-blank (- (org-element-property :end elem)
  22660. (save-excursion
  22661. (goto-char (org-element-property :end elem))
  22662. (skip-chars-backward " \r\t\n")
  22663. (forward-line)
  22664. (point)))))
  22665. (org-element-swap-A-B elem next-elem)
  22666. (goto-char (+ pos size-next size-blank))))))
  22667. (defun org-drag-line-forward (arg)
  22668. "Drag the line at point ARG lines forward."
  22669. (interactive "p")
  22670. (dotimes (_ (abs arg))
  22671. (let ((c (current-column)))
  22672. (if (< 0 arg)
  22673. (progn
  22674. (beginning-of-line 2)
  22675. (transpose-lines 1)
  22676. (beginning-of-line 0))
  22677. (transpose-lines 1)
  22678. (beginning-of-line -1))
  22679. (org-move-to-column c))))
  22680. (defun org-drag-line-backward (arg)
  22681. "Drag the line at point ARG lines backward."
  22682. (interactive "p")
  22683. (org-drag-line-forward (- arg)))
  22684. (defun org-mark-element ()
  22685. "Put point at beginning of this element, mark at end.
  22686. Interactively, if this command is repeated or (in Transient Mark
  22687. mode) if the mark is active, it marks the next element after the
  22688. ones already marked."
  22689. (interactive)
  22690. (let (deactivate-mark)
  22691. (if (and (org-called-interactively-p 'any)
  22692. (or (and (eq last-command this-command) (mark t))
  22693. (and transient-mark-mode mark-active)))
  22694. (set-mark
  22695. (save-excursion
  22696. (goto-char (mark))
  22697. (goto-char (org-element-property :end (org-element-at-point)))))
  22698. (let ((element (org-element-at-point)))
  22699. (end-of-line)
  22700. (push-mark (org-element-property :end element) t t)
  22701. (goto-char (org-element-property :begin element))))))
  22702. (defun org-narrow-to-element ()
  22703. "Narrow buffer to current element."
  22704. (interactive)
  22705. (let ((elem (org-element-at-point)))
  22706. (cond
  22707. ((eq (car elem) 'headline)
  22708. (narrow-to-region
  22709. (org-element-property :begin elem)
  22710. (org-element-property :end elem)))
  22711. ((memq (car elem) org-element-greater-elements)
  22712. (narrow-to-region
  22713. (org-element-property :contents-begin elem)
  22714. (org-element-property :contents-end elem)))
  22715. (t
  22716. (narrow-to-region
  22717. (org-element-property :begin elem)
  22718. (org-element-property :end elem))))))
  22719. (defun org-transpose-element ()
  22720. "Transpose current and previous elements, keeping blank lines between.
  22721. Point is moved after both elements."
  22722. (interactive)
  22723. (org-skip-whitespace)
  22724. (let ((end (org-element-property :end (org-element-at-point))))
  22725. (org-drag-element-backward)
  22726. (goto-char end)))
  22727. (defun org-unindent-buffer ()
  22728. "Un-indent the visible part of the buffer.
  22729. Relative indentation (between items, inside blocks, etc.) isn't
  22730. modified."
  22731. (interactive)
  22732. (unless (eq major-mode 'org-mode)
  22733. (user-error "Cannot un-indent a buffer not in Org mode"))
  22734. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22735. (unindent-tree
  22736. (lambda (contents)
  22737. (dolist (element (reverse contents))
  22738. (if (memq (org-element-type element) '(headline section))
  22739. (funcall unindent-tree (org-element-contents element))
  22740. (save-excursion
  22741. (save-restriction
  22742. (narrow-to-region
  22743. (org-element-property :begin element)
  22744. (org-element-property :end element))
  22745. (org-do-remove-indentation))))))))
  22746. (funcall unindent-tree (org-element-contents parse-tree))))
  22747. (defun org-show-children (&optional level)
  22748. "Show all direct subheadings of this heading.
  22749. Prefix arg LEVEL is how many levels below the current level
  22750. should be shown. Default is enough to cause the following
  22751. heading to appear."
  22752. (interactive "p")
  22753. ;; If `orgstruct-mode' is active, use the slower version.
  22754. (if orgstruct-mode (call-interactively #'outline-show-children)
  22755. (save-excursion
  22756. (org-back-to-heading t)
  22757. (let* ((current-level (funcall outline-level))
  22758. (max-level (org-get-valid-level
  22759. current-level
  22760. (if level (prefix-numeric-value level) 1)))
  22761. (end (save-excursion (org-end-of-subtree t t)))
  22762. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22763. (past-first-child nil)
  22764. ;; Make sure to skip inlinetasks.
  22765. (re (format regexp-fmt
  22766. current-level
  22767. (cond
  22768. ((not (featurep 'org-inlinetask)) "")
  22769. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22770. 3))
  22771. (t (1- org-inlinetask-min-level))))))
  22772. ;; Display parent heading.
  22773. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22774. (forward-line)
  22775. ;; Display children. First child may be deeper than expected
  22776. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22777. ;; MAX-LEVEL accordingly.
  22778. (while (re-search-forward re end t)
  22779. (unless past-first-child
  22780. (setq re (format regexp-fmt
  22781. current-level
  22782. (max (funcall outline-level) max-level)))
  22783. (setq past-first-child t))
  22784. (outline-flag-region
  22785. (line-end-position 0) (line-end-position) nil))))))
  22786. (defun org-show-subtree ()
  22787. "Show everything after this heading at deeper levels."
  22788. (interactive)
  22789. (outline-flag-region
  22790. (point)
  22791. (save-excursion
  22792. (org-end-of-subtree t t))
  22793. nil))
  22794. (defun org-show-entry ()
  22795. "Show the body directly following this heading.
  22796. Show the heading too, if it is currently invisible."
  22797. (interactive)
  22798. (save-excursion
  22799. (ignore-errors
  22800. (org-back-to-heading t)
  22801. (outline-flag-region
  22802. (max (point-min) (1- (point)))
  22803. (save-excursion
  22804. (if (re-search-forward
  22805. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22806. (match-beginning 1)
  22807. (point-max)))
  22808. nil)
  22809. (org-cycle-hide-drawers 'children))))
  22810. (defun org-make-options-regexp (kwds &optional extra)
  22811. "Make a regular expression for keyword lines.
  22812. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22813. when non-nil, is a regexp matching keywords names."
  22814. (concat "^[ \t]*#\\+\\("
  22815. (regexp-opt kwds)
  22816. (and extra (concat (and kwds "\\|") extra))
  22817. "\\):[ \t]*\\(.*\\)"))
  22818. ;;;; Integration with and fixes for other packages
  22819. ;;; Imenu support
  22820. (defvar-local org-imenu-markers nil
  22821. "All markers currently used by Imenu.")
  22822. (defun org-imenu-new-marker (&optional pos)
  22823. "Return a new marker for use by Imenu, and remember the marker."
  22824. (let ((m (make-marker)))
  22825. (move-marker m (or pos (point)))
  22826. (push m org-imenu-markers)
  22827. m))
  22828. (defun org-imenu-get-tree ()
  22829. "Produce the index for Imenu."
  22830. (dolist (x org-imenu-markers) (move-marker x nil))
  22831. (setq org-imenu-markers nil)
  22832. (let* ((n org-imenu-depth)
  22833. (re (concat "^" (org-get-limited-outline-regexp)))
  22834. (subs (make-vector (1+ n) nil))
  22835. (last-level 0)
  22836. m level head0 head)
  22837. (save-excursion
  22838. (save-restriction
  22839. (widen)
  22840. (goto-char (point-max))
  22841. (while (re-search-backward re nil t)
  22842. (setq level (org-reduced-level (funcall outline-level)))
  22843. (when (and (<= level n)
  22844. (looking-at org-complex-heading-regexp)
  22845. (setq head0 (org-match-string-no-properties 4)))
  22846. (setq head (org-link-display-format head0)
  22847. m (org-imenu-new-marker))
  22848. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22849. (if (>= level last-level)
  22850. (push (cons head m) (aref subs level))
  22851. (push (cons head (aref subs (1+ level))) (aref subs level))
  22852. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22853. (setq last-level level)))))
  22854. (aref subs 1)))
  22855. (eval-after-load "imenu"
  22856. '(progn
  22857. (add-hook 'imenu-after-jump-hook
  22858. (lambda ()
  22859. (when (derived-mode-p 'org-mode)
  22860. (org-show-context 'org-goto))))))
  22861. (defun org-link-display-format (link)
  22862. "Replace a link with its the description.
  22863. If there is no description, use the link target."
  22864. (save-match-data
  22865. (if (string-match org-bracket-link-analytic-regexp link)
  22866. (replace-match (if (match-end 5)
  22867. (match-string 5 link)
  22868. (concat (match-string 1 link)
  22869. (match-string 3 link)))
  22870. nil t link)
  22871. link)))
  22872. (defun org-toggle-link-display ()
  22873. "Toggle the literal or descriptive display of links."
  22874. (interactive)
  22875. (if org-descriptive-links
  22876. (progn (org-remove-from-invisibility-spec '(org-link))
  22877. (org-restart-font-lock)
  22878. (setq org-descriptive-links nil))
  22879. (progn (add-to-invisibility-spec '(org-link))
  22880. (org-restart-font-lock)
  22881. (setq org-descriptive-links t))))
  22882. ;; Speedbar support
  22883. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22884. "Overlay marking the agenda restriction line in speedbar.")
  22885. (overlay-put org-speedbar-restriction-lock-overlay
  22886. 'face 'org-agenda-restriction-lock)
  22887. (overlay-put org-speedbar-restriction-lock-overlay
  22888. 'help-echo "Agendas are currently limited to this item.")
  22889. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22890. (defun org-speedbar-set-agenda-restriction ()
  22891. "Restrict future agenda commands to the location at point in speedbar.
  22892. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22893. (interactive)
  22894. (require 'org-agenda)
  22895. (let (p m tp np dir txt)
  22896. (cond
  22897. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22898. 'org-imenu t))
  22899. (setq m (get-text-property p 'org-imenu-marker))
  22900. (with-current-buffer (marker-buffer m)
  22901. (goto-char m)
  22902. (org-agenda-set-restriction-lock 'subtree)))
  22903. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22904. 'speedbar-function 'speedbar-find-file))
  22905. (setq tp (previous-single-property-change
  22906. (1+ p) 'speedbar-function)
  22907. np (next-single-property-change
  22908. tp 'speedbar-function)
  22909. dir (speedbar-line-directory)
  22910. txt (buffer-substring-no-properties (or tp (point-min))
  22911. (or np (point-max))))
  22912. (with-current-buffer (find-file-noselect
  22913. (let ((default-directory dir))
  22914. (expand-file-name txt)))
  22915. (unless (derived-mode-p 'org-mode)
  22916. (user-error "Cannot restrict to non-Org-mode file"))
  22917. (org-agenda-set-restriction-lock 'file)))
  22918. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22919. (move-overlay org-speedbar-restriction-lock-overlay
  22920. (point-at-bol) (point-at-eol))
  22921. (setq current-prefix-arg nil)
  22922. (org-agenda-maybe-redo)))
  22923. (defvar speedbar-file-key-map)
  22924. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22925. (eval-after-load "speedbar"
  22926. '(progn
  22927. (speedbar-add-supported-extension ".org")
  22928. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22929. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22930. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22931. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22932. (add-hook 'speedbar-visiting-tag-hook
  22933. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22934. ;;; Fixes and Hacks for problems with other packages
  22935. (defun org--flyspell-object-check-p (element)
  22936. "Non-nil when Flyspell can check object at point.
  22937. ELEMENT is the element at point."
  22938. (let ((object (save-excursion
  22939. (when (org-looking-at-p "\\>") (backward-char))
  22940. (org-element-context element))))
  22941. (cl-case (org-element-type object)
  22942. ;; Prevent checks in links due to keybinding conflict with
  22943. ;; Flyspell.
  22944. ((code entity export-snippet inline-babel-call
  22945. inline-src-block line-break latex-fragment link macro
  22946. statistics-cookie target timestamp verbatim)
  22947. nil)
  22948. (footnote-reference
  22949. ;; Only in inline footnotes, within the definition.
  22950. (and (eq (org-element-property :type object) 'inline)
  22951. (< (save-excursion
  22952. (goto-char (org-element-property :begin object))
  22953. (search-forward ":" nil t 2))
  22954. (point))))
  22955. (otherwise t))))
  22956. (defun org-mode-flyspell-verify ()
  22957. "Function used for `flyspell-generic-check-word-predicate'."
  22958. (if (org-at-heading-p)
  22959. ;; At a headline or an inlinetask, check title only. This is
  22960. ;; faster than relying on `org-element-at-point'.
  22961. (and (save-excursion (beginning-of-line)
  22962. (and (let ((case-fold-search t))
  22963. (not (looking-at "\\*+ END[ \t]*$")))
  22964. (looking-at org-complex-heading-regexp)))
  22965. (match-beginning 4)
  22966. (>= (point) (match-beginning 4))
  22967. (or (not (match-beginning 5))
  22968. (< (point) (match-beginning 5))))
  22969. (let* ((element (org-element-at-point))
  22970. (post-affiliated (org-element-property :post-affiliated element)))
  22971. (cond
  22972. ;; Ignore checks in all affiliated keywords but captions.
  22973. ((< (point) post-affiliated)
  22974. (and (save-excursion
  22975. (beginning-of-line)
  22976. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22977. (> (point) (match-end 0))
  22978. (org--flyspell-object-check-p element)))
  22979. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22980. ((let ((log (org-log-into-drawer)))
  22981. (and log
  22982. (let ((drawer (org-element-lineage element '(drawer))))
  22983. (and drawer
  22984. (eq (compare-strings
  22985. log nil nil
  22986. (org-element-property :drawer-name drawer) nil nil t)
  22987. t)))))
  22988. nil)
  22989. (t
  22990. (cl-case (org-element-type element)
  22991. ((comment quote-section) t)
  22992. (comment-block
  22993. ;; Allow checks between block markers, not on them.
  22994. (and (> (line-beginning-position) post-affiliated)
  22995. (save-excursion
  22996. (end-of-line)
  22997. (skip-chars-forward " \r\t\n")
  22998. (< (point) (org-element-property :end element)))))
  22999. ;; Arbitrary list of keywords where checks are meaningful.
  23000. ;; Make sure point is on the value part of the element.
  23001. (keyword
  23002. (and (member (org-element-property :key element)
  23003. '("DESCRIPTION" "TITLE"))
  23004. (save-excursion
  23005. (search-backward ":" (line-beginning-position) t))))
  23006. ;; Check is globally allowed in paragraphs verse blocks and
  23007. ;; table rows (after affiliated keywords) but some objects
  23008. ;; must not be affected.
  23009. ((paragraph table-row verse-block)
  23010. (let ((cbeg (org-element-property :contents-begin element))
  23011. (cend (org-element-property :contents-end element)))
  23012. (and cbeg (>= (point) cbeg) (< (point) cend)
  23013. (org--flyspell-object-check-p element))))))))))
  23014. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23015. (defun org-remove-flyspell-overlays-in (beg end)
  23016. "Remove flyspell overlays in region."
  23017. (and (org-bound-and-true-p flyspell-mode)
  23018. (fboundp 'flyspell-delete-region-overlays)
  23019. (flyspell-delete-region-overlays beg end)))
  23020. (defvar flyspell-delayed-commands)
  23021. (eval-after-load "flyspell"
  23022. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23023. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23024. (eval-after-load "bookmark"
  23025. '(if (boundp 'bookmark-after-jump-hook)
  23026. ;; We can use the hook
  23027. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23028. ;; Hook not available, use advice
  23029. (defadvice bookmark-jump (after org-make-visible activate)
  23030. "Make the position visible."
  23031. (org-bookmark-jump-unhide))))
  23032. ;; Make sure saveplace shows the location if it was hidden
  23033. (eval-after-load "saveplace"
  23034. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23035. "Make the position visible."
  23036. (org-bookmark-jump-unhide)))
  23037. ;; Make sure ecb shows the location if it was hidden
  23038. (eval-after-load "ecb"
  23039. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23040. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23041. (when (derived-mode-p 'org-mode)
  23042. (org-show-context))))
  23043. (defun org-bookmark-jump-unhide ()
  23044. "Unhide the current position, to show the bookmark location."
  23045. (and (derived-mode-p 'org-mode)
  23046. (or (outline-invisible-p)
  23047. (save-excursion (goto-char (max (point-min) (1- (point))))
  23048. (outline-invisible-p)))
  23049. (org-show-context 'bookmark-jump)))
  23050. (defun org-mark-jump-unhide ()
  23051. "Make the point visible with `org-show-context' after jumping to the mark."
  23052. (when (and (derived-mode-p 'org-mode)
  23053. (outline-invisible-p))
  23054. (org-show-context 'mark-goto)))
  23055. (eval-after-load "simple"
  23056. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23057. "Make the point visible with `org-show-context'."
  23058. (org-mark-jump-unhide)))
  23059. (eval-after-load "simple"
  23060. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23061. "Make the point visible with `org-show-context'."
  23062. (org-mark-jump-unhide)))
  23063. (eval-after-load "simple"
  23064. '(defadvice pop-global-mark (after org-make-visible activate)
  23065. "Make the point visible with `org-show-context'."
  23066. (org-mark-jump-unhide)))
  23067. ;; Make session.el ignore our circular variable
  23068. (defvar session-globals-exclude)
  23069. (eval-after-load "session"
  23070. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23071. ;;;; Finish up
  23072. (provide 'org)
  23073. (run-hooks 'org-load-hook)
  23074. ;;; org.el ends here