org.el 940 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891
  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-2018 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 <https://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org 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
  29. ;; contain information about projects as plain text. Org mode is
  30. ;; implemented on top of outline-mode, which makes it possible to keep
  31. ;; the content of large files well structured. Visibility cycling and
  32. ;; structure editing help to work with the tree. Tables are easily
  33. ;; created with a built-in table editor. Org mode supports ToDo
  34. ;; items, deadlines, time stamps, and scheduling. It dynamically
  35. ;; compiles entries into an agenda that utilizes and smoothly
  36. ;; integrates much of the Emacs calendar and diary. Plain text
  37. ;; URL-like links connect to websites, emails, Usenet messages, BBDB
  38. ;; entries, and any files related to the projects. For printing and
  39. ;; sharing of notes, an Org file can be exported as a structured ASCII
  40. ;; file, as HTML, or (todo and agenda items only) as an iCalendar
  41. ;; file. It can also serve as a publishing tool for a set of linked
  42. ;; webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the doc/ directory.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar-local org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. ;;;; Require other packages
  66. (require 'cl-lib)
  67. (eval-when-compile (require 'gnus-sum))
  68. (require 'calendar)
  69. (require 'find-func)
  70. (require 'format-spec)
  71. (or (eq this-command 'eval-buffer)
  72. (condition-case nil
  73. (load (concat (file-name-directory load-file-name)
  74. "org-loaddefs.el")
  75. nil t t t)
  76. (error
  77. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  78. (sit-for 3)
  79. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  80. (sit-for 3))))
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  84. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  85. ;;
  86. ;; In Org buffers, the value of `outline-regexp' is that of
  87. ;; `org-outline-regexp'. The only function still directly relying on
  88. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  89. ;; job when `orgstruct-mode' is active.
  90. (defvar org-outline-regexp "\\*+ "
  91. "Regexp to match Org headlines.")
  92. (defvar org-outline-regexp-bol "^\\*+ "
  93. "Regexp to match Org headlines.
  94. This is similar to `org-outline-regexp' but additionally makes
  95. sure that we are at the beginning of the line.")
  96. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  97. "Matches a headline, putting stars and text into groups.
  98. Stars are put in group 1 and the trimmed body in group 2.")
  99. (declare-function calendar-check-holidays "holidays" (date))
  100. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  101. (declare-function isearch-no-upper-case-p "isearch" (string regexp-flag))
  102. (declare-function org-add-archive-files "org-archive" (files))
  103. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  104. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span with-hour))
  105. (declare-function org-agenda-redo "org-agenda" (&optional all))
  106. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body) t)
  107. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  108. (declare-function org-beamer-mode "ox-beamer" (&optional prefix) t)
  109. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  112. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  113. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  114. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  115. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  116. (declare-function org-clock-update-time-maybe "org-clock" ())
  117. (declare-function org-clocking-buffer "org-clock" ())
  118. (declare-function org-clocktable-shift "org-clock" (dir n))
  119. (declare-function
  120. org-duration-from-minutes "org-duration" (minutes &optional fmt canonical))
  121. (declare-function org-element-at-point "org-element" ())
  122. (declare-function org-element-cache-refresh "org-element" (pos))
  123. (declare-function org-element-cache-reset "org-element" (&optional all))
  124. (declare-function org-element-contents "org-element" (element))
  125. (declare-function org-element-context "org-element" (&optional element))
  126. (declare-function org-element-copy "org-element" (datum))
  127. (declare-function org-element-interpret-data "org-element" (data))
  128. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  129. (declare-function org-element-link-parser "org-element" ())
  130. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  131. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  132. (declare-function org-element-property "org-element" (property element))
  133. (declare-function org-element-put-property "org-element" (element property value))
  134. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  135. (declare-function org-element-type "org-element" (element))
  136. (declare-function org-element-update-syntax "org-element" ())
  137. (declare-function org-id-find-id-file "org-id" (id))
  138. (declare-function org-id-get-create "org-id" (&optional force))
  139. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  140. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  141. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  142. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  143. (declare-function org-table-align "org-table" ())
  144. (declare-function org-table-begin "org-table" (&optional table-type))
  145. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  146. (declare-function org-table-blank-field "org-table" ())
  147. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  148. (declare-function org-table-copy-region "org-table" (beg end &optional cut))
  149. (declare-function org-table-cut-region "org-table" (beg end))
  150. (declare-function org-table-edit-field "org-table" (arg))
  151. (declare-function org-table-end "org-table" (&optional table-type))
  152. (declare-function org-table-end-of-field "org-table" (&optional n))
  153. (declare-function org-table-insert-row "org-table" (&optional arg))
  154. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  155. (declare-function org-table-maybe-eval-formula "org-table" ())
  156. (declare-function org-table-maybe-recalculate-line "org-table" ())
  157. (declare-function org-table-next-row "org-table" ())
  158. (declare-function org-table-paste-rectangle "org-table" ())
  159. (declare-function org-table-recalculate "org-table" (&optional all noalign))
  160. (declare-function
  161. org-table-sort-lines "org-table"
  162. (&optional with-case sorting-type getkey-func compare-func interactive?))
  163. (declare-function org-table-wrap-region "org-table" (arg))
  164. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  165. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  166. (declare-function orgtbl-mode "org-table" (&optional arg))
  167. (declare-function org-export-get-backend "ox" (name))
  168. (declare-function org-export-get-environment "ox" (&optional backend subtreep ext-plist))
  169. (declare-function org-latex-make-preamble "ox-latex" (info &optional template snippet?))
  170. (defvar ffap-url-regexp)
  171. (defvar org-element-paragraph-separate)
  172. (defsubst org-uniquify (list)
  173. "Non-destructively remove duplicate elements from LIST."
  174. (let ((res (copy-sequence list))) (delete-dups res)))
  175. (defsubst org-get-at-bol (property)
  176. "Get text property PROPERTY at the beginning of line."
  177. (get-text-property (point-at-bol) property))
  178. (defsubst org-trim (s &optional keep-lead)
  179. "Remove whitespace at the beginning and the end of string S.
  180. When optional argument KEEP-LEAD is non-nil, removing blank lines
  181. at the beginning of the string does not affect leading indentation."
  182. (replace-regexp-in-string
  183. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  184. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  185. ;; load languages based on value of `org-babel-load-languages'
  186. (defvar org-babel-load-languages)
  187. ;;;###autoload
  188. (defun org-babel-do-load-languages (sym value)
  189. "Load the languages defined in `org-babel-load-languages'."
  190. (set-default sym value)
  191. (dolist (pair org-babel-load-languages)
  192. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  193. (if active
  194. (require (intern (concat "ob-" lang)))
  195. (funcall 'fmakunbound
  196. (intern (concat "org-babel-execute:" lang)))
  197. (funcall 'fmakunbound
  198. (intern (concat "org-babel-expand-body:" lang)))))))
  199. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  200. ;;;###autoload
  201. (defun org-babel-load-file (file &optional compile)
  202. "Load Emacs Lisp source code blocks in the Org FILE.
  203. This function exports the source code using `org-babel-tangle'
  204. and then loads the resulting file using `load-file'. With prefix
  205. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  206. file to byte-code before it is loaded."
  207. (interactive "fFile to load: \nP")
  208. (let* ((age (lambda (file)
  209. (float-time
  210. (time-subtract (current-time)
  211. (nth 5 (or (file-attributes (file-truename file))
  212. (file-attributes file)))))))
  213. (base-name (file-name-sans-extension file))
  214. (exported-file (concat base-name ".el")))
  215. ;; tangle if the Org file is newer than the elisp file
  216. (unless (and (file-exists-p exported-file)
  217. (> (funcall age file) (funcall age exported-file)))
  218. ;; Tangle-file traversal returns reversed list of tangled files
  219. ;; and we want to evaluate the first target.
  220. (setq exported-file
  221. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  222. (message "%s %s"
  223. (if compile
  224. (progn (byte-compile-file exported-file 'load)
  225. "Compiled and loaded")
  226. (progn (load-file exported-file) "Loaded"))
  227. exported-file)))
  228. (defcustom org-babel-load-languages '((emacs-lisp . t))
  229. "Languages which can be evaluated in Org buffers.
  230. This list can be used to load support for any of the languages
  231. below, note that each language will depend on a different set of
  232. system executables and/or Emacs modes. When a language is
  233. \"loaded\", then code blocks in that language can be evaluated
  234. with `org-babel-execute-src-block' bound by default to C-c
  235. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  236. be set to remove code block evaluation from the C-c C-c
  237. keybinding. By default only Emacs Lisp (which has no
  238. requirements) is loaded."
  239. :group 'org-babel
  240. :set 'org-babel-do-load-languages
  241. :version "24.1"
  242. :type '(alist :tag "Babel Languages"
  243. :key-type
  244. (choice
  245. (const :tag "Awk" awk)
  246. (const :tag "C" C)
  247. (const :tag "R" R)
  248. (const :tag "Asymptote" asymptote)
  249. (const :tag "Calc" calc)
  250. (const :tag "Clojure" clojure)
  251. (const :tag "CSS" css)
  252. (const :tag "Ditaa" ditaa)
  253. (const :tag "Dot" dot)
  254. (const :tag "Ebnf2ps" ebnf2ps)
  255. (const :tag "Emacs Lisp" emacs-lisp)
  256. (const :tag "Forth" forth)
  257. (const :tag "Fortran" fortran)
  258. (const :tag "Gnuplot" gnuplot)
  259. (const :tag "Haskell" haskell)
  260. (const :tag "hledger" hledger)
  261. (const :tag "IO" io)
  262. (const :tag "J" J)
  263. (const :tag "Java" java)
  264. (const :tag "Javascript" js)
  265. (const :tag "LaTeX" latex)
  266. (const :tag "Ledger" ledger)
  267. (const :tag "Lilypond" lilypond)
  268. (const :tag "Lisp" lisp)
  269. (const :tag "Makefile" makefile)
  270. (const :tag "Maxima" maxima)
  271. (const :tag "Matlab" matlab)
  272. (const :tag "Mscgen" mscgen)
  273. (const :tag "Ocaml" ocaml)
  274. (const :tag "Octave" octave)
  275. (const :tag "Org" org)
  276. (const :tag "Perl" perl)
  277. (const :tag "Pico Lisp" picolisp)
  278. (const :tag "PlantUML" plantuml)
  279. (const :tag "Python" python)
  280. (const :tag "Ruby" ruby)
  281. (const :tag "Sass" sass)
  282. (const :tag "Scala" scala)
  283. (const :tag "Scheme" scheme)
  284. (const :tag "Screen" screen)
  285. (const :tag "Shell Script" shell)
  286. (const :tag "Shen" shen)
  287. (const :tag "Sql" sql)
  288. (const :tag "Sqlite" sqlite)
  289. (const :tag "Stan" stan)
  290. (const :tag "Vala" vala))
  291. :value-type (boolean :tag "Activate" :value t)))
  292. ;;;; Customization variables
  293. (defcustom org-clone-delete-id nil
  294. "Remove ID property of clones of a subtree.
  295. When non-nil, clones of a subtree don't inherit the ID property.
  296. Otherwise they inherit the ID property with a new unique
  297. identifier."
  298. :type 'boolean
  299. :version "24.1"
  300. :group 'org-id)
  301. ;;; Version
  302. (org-check-version)
  303. ;;;###autoload
  304. (defun org-version (&optional here full message)
  305. "Show the Org version.
  306. Interactively, or when MESSAGE is non-nil, show it in echo area.
  307. With prefix argument, or when HERE is non-nil, insert it at point.
  308. In non-interactive uses, a reduced version string is output unless
  309. FULL is given."
  310. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  311. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  312. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  313. (load-suffixes (list ".el"))
  314. (org-install-dir
  315. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  316. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  317. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  318. (let* ((load-suffixes save-load-suffixes)
  319. (release (org-release))
  320. (git-version (org-git-version))
  321. (version (format "Org mode version %s (%s @ %s)"
  322. release
  323. git-version
  324. (if org-install-dir
  325. (if (string= org-dir org-install-dir)
  326. org-install-dir
  327. (concat "mixed installation! "
  328. org-install-dir
  329. " and "
  330. org-dir))
  331. "org-loaddefs.el can not be found!")))
  332. (version1 (if full version release)))
  333. (when here (insert version1))
  334. (when message (message "%s" version1))
  335. version1)))
  336. (defconst org-version (org-version))
  337. ;;; Syntax Constants
  338. ;;;; Block
  339. (defconst org-block-regexp
  340. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  341. "Regular expression for hiding blocks.")
  342. (defconst org-dblock-start-re
  343. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  344. "Matches the start line of a dynamic block, with parameters.")
  345. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  346. "Matches the end of a dynamic block.")
  347. ;;;; Clock and Planning
  348. (defconst org-clock-string "CLOCK:"
  349. "String used as prefix for timestamps clocking work hours on an item.")
  350. (defvar org-closed-string "CLOSED:"
  351. "String used as the prefix for timestamps logging closing a TODO entry.")
  352. (defvar org-deadline-string "DEADLINE:"
  353. "String to mark deadline entries.
  354. \\<org-mode-map>
  355. A deadline is this string, followed by a time stamp. It must be
  356. a word, terminated by a colon. You can insert a schedule keyword
  357. and a timestamp with `\\[org-deadline]'.")
  358. (defvar org-scheduled-string "SCHEDULED:"
  359. "String to mark scheduled TODO entries.
  360. \\<org-mode-map>
  361. A schedule is this string, followed by a time stamp. It must be
  362. a word, terminated by a colon. You can insert a schedule keyword
  363. and a timestamp with `\\[org-schedule]'.")
  364. (defconst org-ds-keyword-length
  365. (+ 2
  366. (apply #'max
  367. (mapcar #'length
  368. (list org-deadline-string org-scheduled-string
  369. org-clock-string org-closed-string))))
  370. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  371. (defconst org-planning-line-re
  372. (concat "^[ \t]*"
  373. (regexp-opt
  374. (list org-closed-string org-deadline-string org-scheduled-string)
  375. t))
  376. "Matches a line with planning info.
  377. Matched keyword is in group 1.")
  378. (defconst org-clock-line-re
  379. (concat "^[ \t]*" org-clock-string)
  380. "Matches a line with clock info.")
  381. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  382. "Matches the DEADLINE keyword.")
  383. (defconst org-deadline-time-regexp
  384. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  385. "Matches the DEADLINE keyword together with a time stamp.")
  386. (defconst org-deadline-time-hour-regexp
  387. (concat "\\<" org-deadline-string
  388. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  389. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  390. (defconst org-deadline-line-regexp
  391. (concat "\\<\\(" org-deadline-string "\\).*")
  392. "Matches the DEADLINE keyword and the rest of the line.")
  393. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  394. "Matches the SCHEDULED keyword.")
  395. (defconst org-scheduled-time-regexp
  396. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  397. "Matches the SCHEDULED keyword together with a time stamp.")
  398. (defconst org-scheduled-time-hour-regexp
  399. (concat "\\<" org-scheduled-string
  400. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  401. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  402. (defconst org-closed-time-regexp
  403. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  404. "Matches the CLOSED keyword together with a time stamp.")
  405. (defconst org-keyword-time-regexp
  406. (concat "\\<"
  407. (regexp-opt
  408. (list org-scheduled-string org-deadline-string org-closed-string
  409. org-clock-string)
  410. t)
  411. " *[[<]\\([^]>]+\\)[]>]")
  412. "Matches any of the 4 keywords, together with the time stamp.")
  413. (defconst org-keyword-time-not-clock-regexp
  414. (concat
  415. "\\<"
  416. (regexp-opt
  417. (list org-scheduled-string org-deadline-string org-closed-string) t)
  418. " *[[<]\\([^]>]+\\)[]>]")
  419. "Matches any of the 3 keywords, together with the time stamp.")
  420. (defconst org-maybe-keyword-time-regexp
  421. (concat "\\(\\<"
  422. (regexp-opt
  423. (list org-scheduled-string org-deadline-string org-closed-string
  424. org-clock-string)
  425. t)
  426. "\\)?"
  427. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  428. "\\|"
  429. "<%%([^\r\n>]*>\\)")
  430. "Matches a timestamp, possibly preceded by a keyword.")
  431. (defconst org-all-time-keywords
  432. (mapcar (lambda (w) (substring w 0 -1))
  433. (list org-scheduled-string org-deadline-string
  434. org-clock-string org-closed-string))
  435. "List of time keywords.")
  436. ;;;; Drawer
  437. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  438. "Matches first or last line of a hidden block.
  439. Group 1 contains drawer's name or \"END\".")
  440. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  441. "Regular expression matching the first line of a property drawer.")
  442. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  443. "Regular expression matching the last line of a property drawer.")
  444. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  445. "Regular expression matching the first line of a clock drawer.")
  446. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  447. "Regular expression matching the last line of a clock drawer.")
  448. (defconst org-property-drawer-re
  449. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  450. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  451. "[ \t]*:END:[ \t]*$")
  452. "Matches an entire property drawer.")
  453. (defconst org-clock-drawer-re
  454. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  455. org-clock-drawer-end-re "\\)\n?")
  456. "Matches an entire clock drawer.")
  457. ;;;; Headline
  458. (defconst org-heading-keyword-regexp-format
  459. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  460. "Printf format for a regexp matching a headline with some keyword.
  461. This regexp will match the headline of any node which has the
  462. exact keyword that is put into the format. The keyword isn't in
  463. any group by default, but the stars and the body are.")
  464. (defconst org-heading-keyword-maybe-regexp-format
  465. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  466. "Printf format for a regexp matching a headline, possibly with some keyword.
  467. This regexp can match any headline with the specified keyword, or
  468. without a keyword. The keyword isn't in any group by default,
  469. but the stars and the body are.")
  470. (defconst org-archive-tag "ARCHIVE"
  471. "The tag that marks a subtree as archived.
  472. An archived subtree does not open during visibility cycling, and does
  473. not contribute to the agenda listings.")
  474. (eval-and-compile
  475. (defconst org-comment-string "COMMENT"
  476. "Entries starting with this keyword will never be exported.
  477. \\<org-mode-map>
  478. An entry can be toggled between COMMENT and normal with
  479. `\\[org-toggle-comment]'."))
  480. ;;;; LaTeX Environments and Fragments
  481. (defconst org-latex-regexps
  482. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  483. ;; ("$" "\\([ \t(]\\|^\\)\\(\\(\\([$]\\)\\([^ \t\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \t\n,.$]\\)\\4\\)\\)\\([ \t.,?;:'\")]\\|$\\)" 2 nil)
  484. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  485. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \t\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
  486. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \t\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \t\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
  487. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  488. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  489. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  490. "Regular expressions for matching embedded LaTeX.")
  491. ;;;; Node Property
  492. (defconst org-effort-property "Effort"
  493. "The property that is being used to keep track of effort estimates.
  494. Effort estimates given in this property need to have the format H:MM.")
  495. ;;;; Table
  496. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  497. "Detect an org-type or table-type table.")
  498. (defconst org-table-line-regexp "^[ \t]*|"
  499. "Detect an org-type table line.")
  500. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  501. "Detect an org-type table line.")
  502. (defconst org-table-hline-regexp "^[ \t]*|-"
  503. "Detect an org-type table hline.")
  504. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  505. "Detect a table-type table hline.")
  506. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  507. "Detect the first line outside a table when searching from within it.
  508. This works for both table types.")
  509. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  510. "Detect a #+TBLFM line.")
  511. ;;;; Timestamp
  512. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  513. "Regular expression for fast time stamp matching.")
  514. (defconst org-ts-regexp-inactive
  515. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  516. "Regular expression for fast inactive time stamp matching.")
  517. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  518. "Regular expression for fast time stamp matching.")
  519. (defconst org-ts-regexp0
  520. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  521. "Regular expression matching time strings for analysis.
  522. This one does not require the space after the date, so it can be used
  523. on a string that terminates immediately after the date.")
  524. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  525. "Regular expression matching time strings for analysis.")
  526. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  527. "Regular expression matching time stamps, with groups.")
  528. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  529. "Regular expression matching time stamps (also [..]), with groups.")
  530. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  531. "Regular expression matching a time stamp range.")
  532. (defconst org-tr-regexp-both
  533. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  534. "Regular expression matching a time stamp range.")
  535. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  536. org-ts-regexp "\\)?")
  537. "Regular expression matching a time stamp or time stamp range.")
  538. (defconst org-tsr-regexp-both
  539. (concat org-ts-regexp-both "\\(--?-?"
  540. org-ts-regexp-both "\\)?")
  541. "Regular expression matching a time stamp or time stamp range.
  542. The time stamps may be either active or inactive.")
  543. (defconst org-repeat-re
  544. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  545. "Regular expression for specifying repeated events.
  546. After a match, group 1 contains the repeat expression.")
  547. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  548. "Formats for `format-time-string' which are used for time stamps.")
  549. ;;; The custom variables
  550. (defgroup org nil
  551. "Outline-based notes management and organizer."
  552. :tag "Org"
  553. :group 'outlines
  554. :group 'calendar)
  555. (defcustom org-mode-hook nil
  556. "Mode hook for Org mode, run after the mode was turned on."
  557. :group 'org
  558. :type 'hook)
  559. (defcustom org-load-hook nil
  560. "Hook that is run after org.el has been loaded."
  561. :group 'org
  562. :type 'hook)
  563. (defcustom org-log-buffer-setup-hook nil
  564. "Hook that is run after an Org log buffer is created."
  565. :group 'org
  566. :version "24.1"
  567. :type 'hook)
  568. (defvar org-modules) ; defined below
  569. (defvar org-modules-loaded nil
  570. "Have the modules been loaded already?")
  571. (defun org-load-modules-maybe (&optional force)
  572. "Load all extensions listed in `org-modules'."
  573. (when (or force (not org-modules-loaded))
  574. (dolist (ext org-modules)
  575. (condition-case nil (require ext)
  576. (error (message "Problems while trying to load feature `%s'" ext))))
  577. (setq org-modules-loaded t)))
  578. (defun org-set-modules (var value)
  579. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  580. (set var value)
  581. (when (featurep 'org)
  582. (org-load-modules-maybe 'force)
  583. (org-element-cache-reset 'all)))
  584. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  585. "Modules that should always be loaded together with org.el.
  586. If a description starts with <C>, the file is not part of Emacs
  587. and loading it will require that you have downloaded and properly
  588. installed the Org mode distribution.
  589. You can also use this system to load external packages (i.e. neither Org
  590. core modules, nor modules from the CONTRIB directory). Just add symbols
  591. to the end of the list. If the package is called org-xyz.el, then you need
  592. to add the symbol `xyz', and the package must have a call to:
  593. (provide \\='org-xyz)
  594. For export specific modules, see also `org-export-backends'."
  595. :group 'org
  596. :set 'org-set-modules
  597. :version "24.4"
  598. :package-version '(Org . "8.0")
  599. :type
  600. '(set :greedy t
  601. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  602. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  603. (const :tag " crypt: Encryption of subtrees" org-crypt)
  604. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  605. (const :tag " docview: Links to doc-view buffers" org-docview)
  606. (const :tag " eww: Store link to url of eww" org-eww)
  607. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  608. (const :tag " habit: Track your consistency with habits" org-habit)
  609. (const :tag " id: Global IDs for identifying entries" org-id)
  610. (const :tag " info: Links to Info nodes" org-info)
  611. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  612. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  613. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  614. (const :tag " mouse: Additional mouse support" org-mouse)
  615. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  616. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  617. (const :tag " w3m: Special cut/paste from w3m to Org mode." org-w3m)
  618. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  619. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  620. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  621. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  622. (const :tag "C collector: Collect properties into tables" org-collector)
  623. (const :tag "C depend: TODO dependencies for Org mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  624. (const :tag "C drill: Flashcards and spaced repetition for Org mode" org-drill)
  625. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  626. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  627. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  628. (const :tag "C eval: Include command output as text" org-eval)
  629. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  630. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  631. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  632. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  633. (const :tag "C invoice: Help manage client invoices in Org mode" org-invoice)
  634. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  635. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  636. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  637. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  638. (const :tag "C man: Support for links to manpages in Org mode" org-man)
  639. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  640. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  641. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  642. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  643. (const :tag "C registry: A registry for Org links" org-registry)
  644. (const :tag "C screen: Visit screen sessions through Org links" org-screen)
  645. (const :tag "C secretary: Team management with Org" org-secretary)
  646. (const :tag "C sqlinsert: Convert Org tables to SQL insertions" orgtbl-sqlinsert)
  647. (const :tag "C toc: Table of contents for Org buffer" org-toc)
  648. (const :tag "C track: Keep up with Org mode development" org-track)
  649. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  650. (const :tag "C vm: Links to VM folders/messages" org-vm)
  651. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  652. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  653. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  654. (defvar org-export-registered-backends) ; From ox.el.
  655. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  656. (declare-function org-export-backend-name "ox" (backend) t)
  657. (defcustom org-export-backends '(ascii html icalendar latex odt)
  658. "List of export back-ends that should be always available.
  659. If a description starts with <C>, the file is not part of Emacs
  660. and loading it will require that you have downloaded and properly
  661. installed the Org mode distribution.
  662. Unlike to `org-modules', libraries in this list will not be
  663. loaded along with Org, but only once the export framework is
  664. needed.
  665. This variable needs to be set before org.el is loaded. If you
  666. need to make a change while Emacs is running, use the customize
  667. interface or run the following code, where VAL stands for the new
  668. value of the variable, after updating it:
  669. (progn
  670. (setq org-export-registered-backends
  671. (cl-remove-if-not
  672. (lambda (backend)
  673. (let ((name (org-export-backend-name backend)))
  674. (or (memq name val)
  675. (catch \\='parentp
  676. (dolist (b val)
  677. (and (org-export-derived-backend-p b name)
  678. (throw \\='parentp t)))))))
  679. org-export-registered-backends))
  680. (let ((new-list (mapcar #\\='org-export-backend-name
  681. org-export-registered-backends)))
  682. (dolist (backend val)
  683. (cond
  684. ((not (load (format \"ox-%s\" backend) t t))
  685. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  686. backend))
  687. ((not (memq backend new-list)) (push backend new-list))))
  688. (set-default \\='org-export-backends new-list)))
  689. Adding a back-end to this list will also pull the back-end it
  690. depends on, if any."
  691. :group 'org
  692. :group 'org-export
  693. :version "26.1"
  694. :package-version '(Org . "9.0")
  695. :initialize 'custom-initialize-set
  696. :set (lambda (var val)
  697. (if (not (featurep 'ox)) (set-default var val)
  698. ;; Any back-end not required anymore (not present in VAL and not
  699. ;; a parent of any back-end in the new value) is removed from the
  700. ;; list of registered back-ends.
  701. (setq org-export-registered-backends
  702. (cl-remove-if-not
  703. (lambda (backend)
  704. (let ((name (org-export-backend-name backend)))
  705. (or (memq name val)
  706. (catch 'parentp
  707. (dolist (b val)
  708. (and (org-export-derived-backend-p b name)
  709. (throw 'parentp t)))))))
  710. org-export-registered-backends))
  711. ;; Now build NEW-LIST of both new back-ends and required
  712. ;; parents.
  713. (let ((new-list (mapcar #'org-export-backend-name
  714. org-export-registered-backends)))
  715. (dolist (backend val)
  716. (cond
  717. ((not (load (format "ox-%s" backend) t t))
  718. (message "Problems while trying to load export back-end `%s'"
  719. backend))
  720. ((not (memq backend new-list)) (push backend new-list))))
  721. ;; Set VAR to that list with fixed dependencies.
  722. (set-default var new-list))))
  723. :type '(set :greedy t
  724. (const :tag " ascii Export buffer to ASCII format" ascii)
  725. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  726. (const :tag " html Export buffer to HTML format" html)
  727. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  728. (const :tag " latex Export buffer to LaTeX format" latex)
  729. (const :tag " man Export buffer to MAN format" man)
  730. (const :tag " md Export buffer to Markdown format" md)
  731. (const :tag " odt Export buffer to ODT format" odt)
  732. (const :tag " org Export buffer to Org format" org)
  733. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  734. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  735. (const :tag "C deck Export buffer to deck.js presentations" deck)
  736. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  737. (const :tag "C groff Export buffer to Groff format" groff)
  738. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  739. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  740. (const :tag "C s5 Export buffer to s5 presentations" s5)
  741. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  742. (eval-after-load 'ox
  743. '(dolist (backend org-export-backends)
  744. (condition-case nil (require (intern (format "ox-%s" backend)))
  745. (error (message "Problems while trying to load export back-end `%s'"
  746. backend)))))
  747. (defcustom org-support-shift-select nil
  748. "Non-nil means make shift-cursor commands select text when possible.
  749. \\<org-mode-map>\
  750. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  751. start selecting a region, or enlarge regions started in this way.
  752. In Org mode, in special contexts, these same keys are used for
  753. other purposes, important enough to compete with shift selection.
  754. Org tries to balance these needs by supporting `shift-select-mode'
  755. outside these special contexts, under control of this variable.
  756. The default of this variable is nil, to avoid confusing behavior. Shifted
  757. cursor keys will then execute Org commands in the following contexts:
  758. - on a headline, changing TODO state (left/right) and priority (up/down)
  759. - on a time stamp, changing the time
  760. - in a plain list item, changing the bullet type
  761. - in a property definition line, switching between allowed values
  762. - in the BEGIN line of a clock table (changing the time block).
  763. Outside these contexts, the commands will throw an error.
  764. When this variable is t and the cursor is not in a special
  765. context, Org mode will support shift-selection for making and
  766. enlarging regions. To make this more effective, the bullet
  767. cycling will no longer happen anywhere in an item line, but only
  768. if the cursor is exactly on the bullet.
  769. If you set this variable to the symbol `always', then the keys
  770. will not be special in headlines, property lines, and item lines,
  771. to make shift selection work there as well. If this is what you
  772. want, you can use the following alternative commands:
  773. `\\[org-todo]' and `\\[org-priority]' \
  774. to change TODO state and priority,
  775. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  776. can be used to switch TODO sets,
  777. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  778. and properties can be edited by hand or in column view.
  779. However, when the cursor is on a timestamp, shift-cursor commands
  780. will still edit the time stamp - this is just too good to give up."
  781. :group 'org
  782. :type '(choice
  783. (const :tag "Never" nil)
  784. (const :tag "When outside special context" t)
  785. (const :tag "Everywhere except timestamps" always)))
  786. (defcustom org-loop-over-headlines-in-active-region nil
  787. "Shall some commands act upon headlines in the active region?
  788. When set to t, some commands will be performed in all headlines
  789. within the active region.
  790. When set to `start-level', some commands will be performed in all
  791. headlines within the active region, provided that these headlines
  792. are of the same level than the first one.
  793. When set to a string, those commands will be performed on the
  794. matching headlines within the active region. Such string must be
  795. a tags/property/todo match as it is used in the agenda tags view.
  796. The list of commands is: `org-schedule', `org-deadline',
  797. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  798. `org-archive-to-archive-sibling'. The archiving commands skip
  799. already archived entries."
  800. :type '(choice (const :tag "Don't loop" nil)
  801. (const :tag "All headlines in active region" t)
  802. (const :tag "In active region, headlines at the same level than the first one" start-level)
  803. (string :tag "Tags/Property/Todo matcher"))
  804. :version "24.1"
  805. :group 'org-todo
  806. :group 'org-archive)
  807. (defgroup org-startup nil
  808. "Options concerning startup of Org mode."
  809. :tag "Org Startup"
  810. :group 'org)
  811. (defcustom org-startup-folded t
  812. "Non-nil means entering Org mode will switch to OVERVIEW.
  813. This can also be configured on a per-file basis by adding one of
  814. the following lines anywhere in the buffer:
  815. #+STARTUP: fold (or `overview', this is equivalent)
  816. #+STARTUP: nofold (or `showall', this is equivalent)
  817. #+STARTUP: content
  818. #+STARTUP: showeverything
  819. Set `org-agenda-inhibit-startup' to a non-nil value if you want
  820. to ignore this option when Org opens agenda files for the first
  821. time."
  822. :group 'org-startup
  823. :type '(choice
  824. (const :tag "nofold: show all" nil)
  825. (const :tag "fold: overview" t)
  826. (const :tag "content: all headlines" content)
  827. (const :tag "show everything, even drawers" showeverything)))
  828. (defcustom org-startup-truncated t
  829. "Non-nil means entering Org mode will set `truncate-lines'.
  830. This is useful since some lines containing links can be very long and
  831. uninteresting. Also tables look terrible when wrapped.
  832. The variable `org-startup-truncated' allows to configure
  833. truncation for Org mode different to the other modes that use the
  834. variable `truncate-lines' and as a shortcut instead of putting
  835. the variable `truncate-lines' into the `org-mode-hook'. If one
  836. wants to configure truncation for Org mode not statically but
  837. dynamically e. g. in a hook like `ediff-prepare-buffer-hook' then
  838. the variable `truncate-lines' has to be used because in such a
  839. case it is too late to set the variable `org-startup-truncated'."
  840. :group 'org-startup
  841. :type 'boolean)
  842. (defcustom org-startup-indented nil
  843. "Non-nil means turn on `org-indent-mode' on startup.
  844. This can also be configured on a per-file basis by adding one of
  845. the following lines anywhere in the buffer:
  846. #+STARTUP: indent
  847. #+STARTUP: noindent"
  848. :group 'org-structure
  849. :type '(choice
  850. (const :tag "Not" nil)
  851. (const :tag "Globally (slow on startup in large files)" t)))
  852. (defcustom org-use-sub-superscripts t
  853. "Non-nil means interpret \"_\" and \"^\" for display.
  854. If you want to control how Org exports those characters, see
  855. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  856. used to be an alias for `org-export-with-sub-superscripts' in
  857. Org <8.0, it is not anymore.
  858. When this option is turned on, you can use TeX-like syntax for
  859. sub- and superscripts within the buffer. Several characters after
  860. \"_\" or \"^\" will be considered as a single item - so grouping
  861. with {} is normally not needed. For example, the following things
  862. will be parsed as single sub- or superscripts:
  863. 10^24 or 10^tau several digits will be considered 1 item.
  864. 10^-12 or 10^-tau a leading sign with digits or a word
  865. x^2-y^3 will be read as x^2 - y^3, because items are
  866. terminated by almost any nonword/nondigit char.
  867. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  868. Still, ambiguity is possible. So when in doubt, use {} to enclose
  869. the sub/superscript. If you set this variable to the symbol `{}',
  870. the braces are *required* in order to trigger interpretations as
  871. sub/superscript. This can be helpful in documents that need \"_\"
  872. frequently in plain text."
  873. :group 'org-startup
  874. :version "24.4"
  875. :package-version '(Org . "8.0")
  876. :type '(choice
  877. (const :tag "Always interpret" t)
  878. (const :tag "Only with braces" {})
  879. (const :tag "Never interpret" nil)))
  880. (defcustom org-startup-with-beamer-mode nil
  881. "Non-nil means turn on `org-beamer-mode' on startup.
  882. This can also be configured on a per-file basis by adding one of
  883. the following lines anywhere in the buffer:
  884. #+STARTUP: beamer"
  885. :group 'org-startup
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom org-startup-align-all-tables nil
  889. "Non-nil means align all tables when visiting a file.
  890. This is useful when the column width in tables is forced with <N> cookies
  891. in table fields. Such tables will look correct only after the first re-align.
  892. This can also be configured on a per-file basis by adding one of
  893. the following lines anywhere in the buffer:
  894. #+STARTUP: align
  895. #+STARTUP: noalign"
  896. :group 'org-startup
  897. :type 'boolean)
  898. (defcustom org-startup-with-inline-images nil
  899. "Non-nil means show inline images when loading a new Org file.
  900. This can also be configured on a per-file basis by adding one of
  901. the following lines anywhere in the buffer:
  902. #+STARTUP: inlineimages
  903. #+STARTUP: noinlineimages"
  904. :group 'org-startup
  905. :version "24.1"
  906. :type 'boolean)
  907. (defcustom org-startup-with-latex-preview nil
  908. "Non-nil means preview LaTeX fragments when loading a new Org file.
  909. This can also be configured on a per-file basis by adding one of
  910. the following lines anywhere in the buffer:
  911. #+STARTUP: latexpreview
  912. #+STARTUP: nolatexpreview"
  913. :group 'org-startup
  914. :version "24.4"
  915. :package-version '(Org . "8.0")
  916. :type 'boolean)
  917. (defcustom org-insert-mode-line-in-empty-file nil
  918. "Non-nil means insert the first line setting Org mode in empty files.
  919. When the function `org-mode' is called interactively in an empty file, this
  920. normally means that the file name does not automatically trigger Org mode.
  921. To ensure that the file will always be in Org mode in the future, a
  922. line enforcing Org mode will be inserted into the buffer, if this option
  923. has been set."
  924. :group 'org-startup
  925. :type 'boolean)
  926. (defcustom org-replace-disputed-keys nil
  927. "Non-nil means use alternative key bindings for some keys.
  928. Org mode uses S-<cursor> keys for changing timestamps and priorities.
  929. These keys are also used by other packages like shift-selection-mode'
  930. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  931. If you want to use Org mode together with one of these other modes,
  932. or more generally if you would like to move some Org mode commands to
  933. other keys, set this variable and configure the keys with the variable
  934. `org-disputed-keys'.
  935. This option is only relevant at load-time of Org mode, and must be set
  936. *before* org.el is loaded. Changing it requires a restart of Emacs to
  937. become effective."
  938. :group 'org-startup
  939. :type 'boolean)
  940. (defcustom org-use-extra-keys nil
  941. "Non-nil means use extra key sequence definitions for certain commands.
  942. This happens automatically if `window-system' is nil. This
  943. variable lets you do the same manually. You must set it before
  944. loading Org."
  945. :group 'org-startup
  946. :type 'boolean)
  947. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  948. (defcustom org-disputed-keys
  949. '(([(shift up)] . [(meta p)])
  950. ([(shift down)] . [(meta n)])
  951. ([(shift left)] . [(meta -)])
  952. ([(shift right)] . [(meta +)])
  953. ([(control shift right)] . [(meta shift +)])
  954. ([(control shift left)] . [(meta shift -)]))
  955. "Keys for which Org mode and other modes compete.
  956. This is an alist, cars are the default keys, second element specifies
  957. the alternative to use when `org-replace-disputed-keys' is t.
  958. Keys can be specified in any syntax supported by `define-key'.
  959. The value of this option takes effect only at Org mode startup,
  960. therefore you'll have to restart Emacs to apply it after changing."
  961. :group 'org-startup
  962. :type 'alist)
  963. (defun org-key (key)
  964. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  965. Or return the original if not disputed."
  966. (when org-replace-disputed-keys
  967. (let* ((nkey (key-description key))
  968. (x (cl-find-if (lambda (x) (equal (key-description (car x)) nkey))
  969. org-disputed-keys)))
  970. (setq key (if x (cdr x) key))))
  971. key)
  972. (defun org-defkey (keymap key def)
  973. "Define a key, possibly translated, as returned by `org-key'."
  974. (define-key keymap (org-key key) def))
  975. (defcustom org-ellipsis nil
  976. "The ellipsis to use in the Org mode outline.
  977. When nil, just use the standard three dots. When a non-empty string,
  978. use that string instead.
  979. The change affects only Org mode (which will then use its own display table).
  980. Changing this requires executing `\\[org-mode]' in a buffer to become
  981. effective."
  982. :group 'org-startup
  983. :type '(choice (const :tag "Default" nil)
  984. (string :tag "String" :value "...#"))
  985. :safe (lambda (v) (and (string-or-null-p v) (not (equal "" v)))))
  986. (defvar org-display-table nil
  987. "The display table for Org mode, in case `org-ellipsis' is non-nil.")
  988. (defgroup org-keywords nil
  989. "Keywords in Org mode."
  990. :tag "Org Keywords"
  991. :group 'org)
  992. (defcustom org-closed-keep-when-no-todo nil
  993. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  994. :group 'org-todo
  995. :group 'org-keywords
  996. :version "24.4"
  997. :package-version '(Org . "8.0")
  998. :type 'boolean)
  999. (defgroup org-structure nil
  1000. "Options concerning the general structure of Org files."
  1001. :tag "Org Structure"
  1002. :group 'org)
  1003. (defgroup org-reveal-location nil
  1004. "Options about how to make context of a location visible."
  1005. :tag "Org Reveal Location"
  1006. :group 'org-structure)
  1007. (defcustom org-show-context-detail '((agenda . local)
  1008. (bookmark-jump . lineage)
  1009. (isearch . lineage)
  1010. (default . ancestors))
  1011. "Alist between context and visibility span when revealing a location.
  1012. \\<org-mode-map>Some actions may move point into invisible
  1013. locations. As a consequence, Org always expose a neighborhood
  1014. around point. How much is shown depends on the initial action,
  1015. or context. Valid contexts are
  1016. agenda when exposing an entry from the agenda
  1017. org-goto when using the command `org-goto' (`\\[org-goto]')
  1018. occur-tree when using the command `org-occur' (`\\[org-sparse-tree] /')
  1019. tags-tree when constructing a sparse tree based on tags matches
  1020. link-search when exposing search matches associated with a link
  1021. mark-goto when exposing the jump goal of a mark
  1022. bookmark-jump when exposing a bookmark location
  1023. isearch when exiting from an incremental search
  1024. default default for all contexts not set explicitly
  1025. Allowed visibility spans are
  1026. minimal show current headline; if point is not on headline,
  1027. also show entry
  1028. local show current headline, entry and next headline
  1029. ancestors show current headline and its direct ancestors; if
  1030. point is not on headline, also show entry
  1031. lineage show current headline, its direct ancestors and all
  1032. their children; if point is not on headline, also show
  1033. entry and first child
  1034. tree show current headline, its direct ancestors and all
  1035. their children; if point is not on headline, also show
  1036. entry and all children
  1037. canonical show current headline, its direct ancestors along with
  1038. their entries and children; if point is not located on
  1039. the headline, also show current entry and all children
  1040. As special cases, a nil or t value means show all contexts in
  1041. `minimal' or `canonical' view, respectively.
  1042. Some views can make displayed information very compact, but also
  1043. make it harder to edit the location of the match. In such
  1044. a case, use the command `org-reveal' (`\\[org-reveal]') to show
  1045. more context."
  1046. :group 'org-reveal-location
  1047. :version "26.1"
  1048. :package-version '(Org . "9.0")
  1049. :type '(choice
  1050. (const :tag "Canonical" t)
  1051. (const :tag "Minimal" nil)
  1052. (repeat :greedy t :tag "Individual contexts"
  1053. (cons
  1054. (choice :tag "Context"
  1055. (const agenda)
  1056. (const org-goto)
  1057. (const occur-tree)
  1058. (const tags-tree)
  1059. (const link-search)
  1060. (const mark-goto)
  1061. (const bookmark-jump)
  1062. (const isearch)
  1063. (const default))
  1064. (choice :tag "Detail level"
  1065. (const minimal)
  1066. (const local)
  1067. (const ancestors)
  1068. (const lineage)
  1069. (const tree)
  1070. (const canonical))))))
  1071. (defcustom org-indirect-buffer-display 'other-window
  1072. "How should indirect tree buffers be displayed?
  1073. This applies to indirect buffers created with the commands
  1074. `org-tree-to-indirect-buffer' and `org-agenda-tree-to-indirect-buffer'.
  1075. Valid values are:
  1076. current-window Display in the current window
  1077. other-window Just display in another window.
  1078. dedicated-frame Create one new frame, and re-use it each time.
  1079. new-frame Make a new frame each time. Note that in this case
  1080. previously-made indirect buffers are kept, and you need to
  1081. kill these buffers yourself."
  1082. :group 'org-structure
  1083. :group 'org-agenda-windows
  1084. :type '(choice
  1085. (const :tag "In current window" current-window)
  1086. (const :tag "In current frame, other window" other-window)
  1087. (const :tag "Each time a new frame" new-frame)
  1088. (const :tag "One dedicated frame" dedicated-frame)))
  1089. (defcustom org-use-speed-commands nil
  1090. "Non-nil means activate single letter commands at beginning of a headline.
  1091. This may also be a function to test for appropriate locations where speed
  1092. commands should be active.
  1093. For example, to activate speed commands when the point is on any
  1094. star at the beginning of the headline, you can do this:
  1095. (setq org-use-speed-commands
  1096. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1097. :group 'org-structure
  1098. :type '(choice
  1099. (const :tag "Never" nil)
  1100. (const :tag "At beginning of headline stars" t)
  1101. (function)))
  1102. (defcustom org-speed-commands-user nil
  1103. "Alist of additional speed commands.
  1104. This list will be checked before `org-speed-commands-default'
  1105. when the variable `org-use-speed-commands' is non-nil
  1106. and when the cursor is at the beginning of a headline.
  1107. The car of each entry is a string with a single letter, which must
  1108. be assigned to `self-insert-command' in the global map.
  1109. The cdr is either a command to be called interactively, a function
  1110. to be called, or a form to be evaluated.
  1111. An entry that is just a list with a single string will be interpreted
  1112. as a descriptive headline that will be added when listing the speed
  1113. commands in the Help buffer using the `?' speed command."
  1114. :group 'org-structure
  1115. :type '(repeat :value ("k" . ignore)
  1116. (choice :value ("k" . ignore)
  1117. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1118. (cons :tag "Letter and Command"
  1119. (string :tag "Command letter")
  1120. (choice
  1121. (function)
  1122. (sexp))))))
  1123. (defcustom org-bookmark-names-plist
  1124. '(:last-capture "org-capture-last-stored"
  1125. :last-refile "org-refile-last-stored"
  1126. :last-capture-marker "org-capture-last-stored-marker")
  1127. "Names for bookmarks automatically set by some Org commands.
  1128. This can provide strings as names for a number of bookmarks Org sets
  1129. automatically. The following keys are currently implemented:
  1130. :last-capture
  1131. :last-capture-marker
  1132. :last-refile
  1133. When a key does not show up in the property list, the corresponding bookmark
  1134. is not set."
  1135. :group 'org-structure
  1136. :type 'plist)
  1137. (defgroup org-cycle nil
  1138. "Options concerning visibility cycling in Org mode."
  1139. :tag "Org Cycle"
  1140. :group 'org-structure)
  1141. (defcustom org-cycle-skip-children-state-if-no-children t
  1142. "Non-nil means skip CHILDREN state in entries that don't have any."
  1143. :group 'org-cycle
  1144. :type 'boolean)
  1145. (defcustom org-cycle-max-level nil
  1146. "Maximum level which should still be subject to visibility cycling.
  1147. Levels higher than this will, for cycling, be treated as text, not a headline.
  1148. When `org-odd-levels-only' is set, a value of N in this variable actually
  1149. means 2N-1 stars as the limiting headline.
  1150. When nil, cycle all levels.
  1151. Note that the limiting level of cycling is also influenced by
  1152. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1153. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1154. than its value."
  1155. :group 'org-cycle
  1156. :type '(choice
  1157. (const :tag "No limit" nil)
  1158. (integer :tag "Maximum level")))
  1159. (defcustom org-hide-block-startup nil
  1160. "Non-nil means entering Org mode will fold all blocks.
  1161. This can also be set in on a per-file basis with
  1162. #+STARTUP: hideblocks
  1163. #+STARTUP: showblocks"
  1164. :group 'org-startup
  1165. :group 'org-cycle
  1166. :type 'boolean)
  1167. (defcustom org-cycle-global-at-bob nil
  1168. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1169. This makes it possible to do global cycling without having to use `S-TAB'
  1170. or `\\[universal-argument] TAB'. For this special case to work, the first \
  1171. line of the buffer
  1172. must not be a headline -- it may be empty or some other text.
  1173. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1174. sure the cursor stays at the beginning of the buffer.
  1175. When this option is nil, don't do anything special at the beginning of
  1176. the buffer."
  1177. :group 'org-cycle
  1178. :type 'boolean)
  1179. (defcustom org-cycle-level-after-item/entry-creation t
  1180. "Non-nil means cycle entry level or item indentation in new empty entries.
  1181. When the cursor is at the end of an empty headline, i.e., with only stars
  1182. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1183. and then all possible ancestor states, before returning to the original state.
  1184. This makes data entry extremely fast: M-RET to create a new headline,
  1185. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1186. When the cursor is at the end of an empty plain list item, one TAB will
  1187. make it a subitem, two or more tabs will back up to make this an item
  1188. higher up in the item hierarchy."
  1189. :group 'org-cycle
  1190. :type 'boolean)
  1191. (defcustom org-cycle-emulate-tab t
  1192. "Where should `org-cycle' emulate TAB.
  1193. nil Never
  1194. white Only in completely white lines
  1195. whitestart Only at the beginning of lines, before the first non-white char
  1196. t Everywhere except in headlines
  1197. exc-hl-bol Everywhere except at the start of a headline
  1198. If TAB is used in a place where it does not emulate TAB, the current subtree
  1199. visibility is cycled."
  1200. :group 'org-cycle
  1201. :type '(choice (const :tag "Never" nil)
  1202. (const :tag "Only in completely white lines" white)
  1203. (const :tag "Before first char in a line" whitestart)
  1204. (const :tag "Everywhere except in headlines" t)
  1205. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1206. (defcustom org-cycle-separator-lines 2
  1207. "Number of empty lines needed to keep an empty line between collapsed trees.
  1208. If you leave an empty line between the end of a subtree and the following
  1209. headline, this empty line is hidden when the subtree is folded.
  1210. Org mode will leave (exactly) one empty line visible if the number of
  1211. empty lines is equal or larger to the number given in this variable.
  1212. So the default 2 means at least 2 empty lines after the end of a subtree
  1213. are needed to produce free space between a collapsed subtree and the
  1214. following headline.
  1215. If the number is negative, and the number of empty lines is at least -N,
  1216. all empty lines are shown.
  1217. Special case: when 0, never leave empty lines in collapsed view."
  1218. :group 'org-cycle
  1219. :type 'integer)
  1220. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1221. (defcustom org-pre-cycle-hook nil
  1222. "Hook that is run before visibility cycling is happening.
  1223. The function(s) in this hook must accept a single argument which indicates
  1224. the new state that will be set right after running this hook. The
  1225. argument is a symbol. Before a global state change, it can have the values
  1226. `overview', `content', or `all'. Before a local state change, it can have
  1227. the values `folded', `children', or `subtree'."
  1228. :group 'org-cycle
  1229. :type 'hook)
  1230. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1231. org-cycle-hide-drawers
  1232. org-cycle-show-empty-lines
  1233. org-optimize-window-after-visibility-change)
  1234. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1235. The function(s) in this hook must accept a single argument which indicates
  1236. the new state that was set by the most recent `org-cycle' command. The
  1237. argument is a symbol. After a global state change, it can have the values
  1238. `overview', `contents', or `all'. After a local state change, it can have
  1239. the values `folded', `children', or `subtree'."
  1240. :group 'org-cycle
  1241. :type 'hook
  1242. :version "26.1"
  1243. :package-version '(Org . "8.3"))
  1244. (defgroup org-edit-structure nil
  1245. "Options concerning structure editing in Org mode."
  1246. :tag "Org Edit Structure"
  1247. :group 'org-structure)
  1248. (defcustom org-odd-levels-only nil
  1249. "Non-nil means skip even levels and only use odd levels for the outline.
  1250. This has the effect that two stars are being added/taken away in
  1251. promotion/demotion commands. It also influences how levels are
  1252. handled by the exporters.
  1253. Changing it requires restart of `font-lock-mode' to become effective
  1254. for fontification also in regions already fontified.
  1255. You may also set this on a per-file basis by adding one of the following
  1256. lines to the buffer:
  1257. #+STARTUP: odd
  1258. #+STARTUP: oddeven"
  1259. :group 'org-edit-structure
  1260. :group 'org-appearance
  1261. :type 'boolean)
  1262. (defcustom org-adapt-indentation t
  1263. "Non-nil means adapt indentation to outline node level.
  1264. When this variable is set, Org assumes that you write outlines by
  1265. indenting text in each node to align with the headline (after the
  1266. stars). The following issues are influenced by this variable:
  1267. - The indentation is increased by one space in a demotion
  1268. command, and decreased by one in a promotion command. However,
  1269. in the latter case, if shifting some line in the entry body
  1270. would alter document structure (e.g., insert a new headline),
  1271. indentation is not changed at all.
  1272. - Property drawers and planning information is inserted indented
  1273. when this variable is set. When nil, they will not be indented.
  1274. - TAB indents a line relative to current level. The lines below
  1275. a headline will be indented when this variable is set.
  1276. Note that this is all about true indentation, by adding and
  1277. removing space characters. See also `org-indent.el' which does
  1278. level-dependent indentation in a virtual way, i.e. at display
  1279. time in Emacs."
  1280. :group 'org-edit-structure
  1281. :type 'boolean)
  1282. (defcustom org-special-ctrl-a/e nil
  1283. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1284. When t, `C-a' will bring back the cursor to the beginning of the
  1285. headline text, i.e. after the stars and after a possible TODO
  1286. keyword. In an item, this will be the position after bullet and
  1287. check-box, if any. When the cursor is already at that position,
  1288. another `C-a' will bring it to the beginning of the line.
  1289. `C-e' will jump to the end of the headline, ignoring the presence
  1290. of tags in the headline. A second `C-e' will then jump to the
  1291. true end of the line, after any tags. This also means that, when
  1292. this variable is non-nil, `C-e' also will never jump beyond the
  1293. end of the heading of a folded section, i.e. not after the
  1294. ellipses.
  1295. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1296. normally, going to the true line boundary first. Only a directly
  1297. following, identical keypress will bring the cursor to the
  1298. special positions.
  1299. This may also be a cons cell where the behavior for `C-a' and
  1300. `C-e' is set separately."
  1301. :group 'org-edit-structure
  1302. :type '(choice
  1303. (const :tag "off" nil)
  1304. (const :tag "on: after stars/bullet and before tags first" t)
  1305. (const :tag "reversed: true line boundary first" reversed)
  1306. (cons :tag "Set C-a and C-e separately"
  1307. (choice :tag "Special C-a"
  1308. (const :tag "off" nil)
  1309. (const :tag "on: after stars/bullet first" t)
  1310. (const :tag "reversed: before stars/bullet first" reversed))
  1311. (choice :tag "Special C-e"
  1312. (const :tag "off" nil)
  1313. (const :tag "on: before tags first" t)
  1314. (const :tag "reversed: after tags first" reversed)))))
  1315. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1316. (defcustom org-special-ctrl-k nil
  1317. "Non-nil means `C-k' will behave specially in headlines.
  1318. When nil, `C-k' will call the default `kill-line' command.
  1319. When t, the following will happen while the cursor is in the headline:
  1320. - When the cursor is at the beginning of a headline, kill the entire
  1321. line and possible the folded subtree below the line.
  1322. - When in the middle of the headline text, kill the headline up to the tags.
  1323. - When after the headline text, kill the tags."
  1324. :group 'org-edit-structure
  1325. :type 'boolean)
  1326. (defcustom org-ctrl-k-protect-subtree nil
  1327. "Non-nil means, do not delete a hidden subtree with C-k.
  1328. When set to the symbol `error', simply throw an error when C-k is
  1329. used to kill (part-of) a headline that has hidden text behind it.
  1330. Any other non-nil value will result in a query to the user, if it is
  1331. OK to kill that hidden subtree. When nil, kill without remorse."
  1332. :group 'org-edit-structure
  1333. :version "24.1"
  1334. :type '(choice
  1335. (const :tag "Do not protect hidden subtrees" nil)
  1336. (const :tag "Protect hidden subtrees with a security query" t)
  1337. (const :tag "Never kill a hidden subtree with C-k" error)))
  1338. (defcustom org-special-ctrl-o t
  1339. "Non-nil means, make `C-o' insert a row in tables."
  1340. :group 'org-edit-structure
  1341. :type 'boolean)
  1342. (defcustom org-catch-invisible-edits nil
  1343. "Check if in invisible region before inserting or deleting a character.
  1344. Valid values are:
  1345. nil Do not check, so just do invisible edits.
  1346. error Throw an error and do nothing.
  1347. show Make point visible, and do the requested edit.
  1348. show-and-error Make point visible, then throw an error and abort the edit.
  1349. smart Make point visible, and do insertion/deletion if it is
  1350. adjacent to visible text and the change feels predictable.
  1351. Never delete a previously invisible character or add in the
  1352. middle or right after an invisible region. Basically, this
  1353. allows insertion and backward-delete right before ellipses.
  1354. FIXME: maybe in this case we should not even show?"
  1355. :group 'org-edit-structure
  1356. :version "24.1"
  1357. :type '(choice
  1358. (const :tag "Do not check" nil)
  1359. (const :tag "Throw error when trying to edit" error)
  1360. (const :tag "Unhide, but do not do the edit" show-and-error)
  1361. (const :tag "Show invisible part and do the edit" show)
  1362. (const :tag "Be smart and do the right thing" smart)))
  1363. (defcustom org-yank-folded-subtrees t
  1364. "Non-nil means when yanking subtrees, fold them.
  1365. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1366. it starts with a heading and all other headings in it are either children
  1367. or siblings, then fold all the subtrees. However, do this only if no
  1368. text after the yank would be swallowed into a folded tree by this action."
  1369. :group 'org-edit-structure
  1370. :type 'boolean)
  1371. (defcustom org-yank-adjusted-subtrees nil
  1372. "Non-nil means when yanking subtrees, adjust the level.
  1373. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1374. this function for details."
  1375. :group 'org-edit-structure
  1376. :type 'boolean)
  1377. (defcustom org-M-RET-may-split-line '((default . t))
  1378. "Non-nil means M-RET will split the line at the cursor position.
  1379. When nil, it will go to the end of the line before making a
  1380. new line.
  1381. You may also set this option in a different way for different
  1382. contexts. Valid contexts are:
  1383. headline when creating a new headline
  1384. item when creating a new item
  1385. table in a table field
  1386. default the value to be used for all contexts not explicitly
  1387. customized"
  1388. :group 'org-structure
  1389. :group 'org-table
  1390. :type '(choice
  1391. (const :tag "Always" t)
  1392. (const :tag "Never" nil)
  1393. (repeat :greedy t :tag "Individual contexts"
  1394. (cons
  1395. (choice :tag "Context"
  1396. (const headline)
  1397. (const item)
  1398. (const table)
  1399. (const default))
  1400. (boolean)))))
  1401. (defcustom org-insert-heading-respect-content nil
  1402. "Non-nil means insert new headings after the current subtree.
  1403. \\<org-mode-map>
  1404. When nil, the new heading is created directly after the current line.
  1405. The commands `\\[org-insert-heading-respect-content]' and \
  1406. `\\[org-insert-todo-heading-respect-content]' turn this variable on
  1407. for the duration of the command."
  1408. :group 'org-structure
  1409. :type 'boolean)
  1410. (defcustom org-blank-before-new-entry '((heading . auto)
  1411. (plain-list-item . auto))
  1412. "Should `org-insert-heading' leave a blank line before new heading/item?
  1413. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1414. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1415. which case Org will look at the surrounding headings/items and try to
  1416. make an intelligent decision whether to insert a blank line or not."
  1417. :group 'org-edit-structure
  1418. :type '(list
  1419. (cons (const heading)
  1420. (choice (const :tag "Never" nil)
  1421. (const :tag "Always" t)
  1422. (const :tag "Auto" auto)))
  1423. (cons (const plain-list-item)
  1424. (choice (const :tag "Never" nil)
  1425. (const :tag "Always" t)
  1426. (const :tag "Auto" auto)))))
  1427. (defcustom org-insert-heading-hook nil
  1428. "Hook being run after inserting a new heading."
  1429. :group 'org-edit-structure
  1430. :type 'hook)
  1431. (defcustom org-enable-fixed-width-editor t
  1432. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1433. This currently only means they are never auto-wrapped.
  1434. When nil, such lines will be treated like ordinary lines."
  1435. :group 'org-edit-structure
  1436. :type 'boolean)
  1437. (defcustom org-goto-auto-isearch t
  1438. "Non-nil means typing characters in `org-goto' starts incremental search.
  1439. When nil, you can use these keybindings to navigate the buffer:
  1440. q Quit the org-goto interface
  1441. n Go to the next visible heading
  1442. p Go to the previous visible heading
  1443. f Go one heading forward on same level
  1444. b Go one heading backward on same level
  1445. u Go one heading up"
  1446. :group 'org-edit-structure
  1447. :type 'boolean)
  1448. (defgroup org-sparse-trees nil
  1449. "Options concerning sparse trees in Org mode."
  1450. :tag "Org Sparse Trees"
  1451. :group 'org-structure)
  1452. (defcustom org-highlight-sparse-tree-matches t
  1453. "Non-nil means highlight all matches that define a sparse tree.
  1454. The highlights will automatically disappear the next time the buffer is
  1455. changed by an edit command."
  1456. :group 'org-sparse-trees
  1457. :type 'boolean)
  1458. (defcustom org-remove-highlights-with-change t
  1459. "Non-nil means any change to the buffer will remove temporary highlights.
  1460. \\<org-mode-map>\
  1461. Such highlights are created by `org-occur' and `org-clock-display'.
  1462. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1463. to get rid of the highlights.
  1464. The highlights created by `org-toggle-latex-fragment' always need
  1465. `\\[org-toggle-latex-fragment]' to be removed."
  1466. :group 'org-sparse-trees
  1467. :group 'org-time
  1468. :type 'boolean)
  1469. (defcustom org-occur-case-fold-search t
  1470. "Non-nil means `org-occur' should be case-insensitive.
  1471. If set to `smart' the search will be case-insensitive only if it
  1472. doesn't specify any upper case character."
  1473. :group 'org-sparse-trees
  1474. :version "26.1"
  1475. :type '(choice
  1476. (const :tag "Case-sensitive" nil)
  1477. (const :tag "Case-insensitive" t)
  1478. (const :tag "Case-insensitive for lower case searches only" smart)))
  1479. (defcustom org-occur-hook '(org-first-headline-recenter)
  1480. "Hook that is run after `org-occur' has constructed a sparse tree.
  1481. This can be used to recenter the window to show as much of the structure
  1482. as possible."
  1483. :group 'org-sparse-trees
  1484. :type 'hook)
  1485. (defgroup org-imenu-and-speedbar nil
  1486. "Options concerning imenu and speedbar in Org mode."
  1487. :tag "Org Imenu and Speedbar"
  1488. :group 'org-structure)
  1489. (defcustom org-imenu-depth 2
  1490. "The maximum level for Imenu access to Org headlines.
  1491. This also applied for speedbar access."
  1492. :group 'org-imenu-and-speedbar
  1493. :type 'integer)
  1494. (defgroup org-table nil
  1495. "Options concerning tables in Org mode."
  1496. :tag "Org Table"
  1497. :group 'org)
  1498. (defcustom org-self-insert-cluster-for-undo nil
  1499. "Non-nil means cluster self-insert commands for undo when possible.
  1500. If this is set, then, like in the Emacs command loop, 20 consecutive
  1501. characters will be undone together.
  1502. This is configurable, because there is some impact on typing performance."
  1503. :group 'org-table
  1504. :type 'boolean)
  1505. (defcustom org-table-tab-recognizes-table.el t
  1506. "Non-nil means TAB will automatically notice a table.el table.
  1507. When it sees such a table, it moves point into it and - if necessary -
  1508. calls `table-recognize-table'."
  1509. :group 'org-table-editing
  1510. :type 'boolean)
  1511. (defgroup org-link nil
  1512. "Options concerning links in Org mode."
  1513. :tag "Org Link"
  1514. :group 'org)
  1515. (defvar-local org-link-abbrev-alist-local nil
  1516. "Buffer-local version of `org-link-abbrev-alist', which see.
  1517. The value of this is taken from the #+LINK lines.")
  1518. (defcustom org-link-parameters
  1519. '(("doi" :follow org--open-doi-link)
  1520. ("elisp" :follow org--open-elisp-link)
  1521. ("file" :complete org-file-complete-link)
  1522. ("ftp" :follow (lambda (path) (browse-url (concat "ftp:" path))))
  1523. ("help" :follow org--open-help-link)
  1524. ("http" :follow (lambda (path) (browse-url (concat "http:" path))))
  1525. ("https" :follow (lambda (path) (browse-url (concat "https:" path))))
  1526. ("mailto" :follow (lambda (path) (browse-url (concat "mailto:" path))))
  1527. ("news" :follow (lambda (path) (browse-url (concat "news:" path))))
  1528. ("shell" :follow org--open-shell-link))
  1529. "An alist of properties that defines all the links in Org mode.
  1530. The key in each association is a string of the link type.
  1531. Subsequent optional elements make up a p-list of link properties.
  1532. :follow - A function that takes the link path as an argument.
  1533. :export - A function that takes the link path, description and
  1534. export-backend as arguments.
  1535. :store - A function responsible for storing the link. See the
  1536. function `org-store-link-functions'.
  1537. :complete - A function that inserts a link with completion. The
  1538. function takes one optional prefix arg.
  1539. :face - A face for the link, or a function that returns a face.
  1540. The function takes one argument which is the link path. The
  1541. default face is `org-link'.
  1542. :mouse-face - The mouse-face. The default is `highlight'.
  1543. :display - `full' will not fold the link in descriptive
  1544. display. Default is `org-link'.
  1545. :help-echo - A string or function that takes (window object position)
  1546. as arguments and returns a string.
  1547. :keymap - A keymap that is active on the link. The default is
  1548. `org-mouse-map'.
  1549. :htmlize-link - A function for the htmlize-link. Defaults
  1550. to (list :uri \"type:path\")
  1551. :activate-func - A function to run at the end of font-lock
  1552. activation. The function must accept (link-start link-end path bracketp)
  1553. as arguments."
  1554. :group 'org-link
  1555. :type '(alist :tag "Link display parameters"
  1556. :value-type plist)
  1557. :version "26.1"
  1558. :package-version '(Org . "9.1"))
  1559. (defun org-link-get-parameter (type key)
  1560. "Get TYPE link property for KEY.
  1561. TYPE is a string and KEY is a plist keyword."
  1562. (plist-get
  1563. (cdr (assoc type org-link-parameters))
  1564. key))
  1565. (defun org-link-set-parameters (type &rest parameters)
  1566. "Set link TYPE properties to PARAMETERS.
  1567. PARAMETERS should be :key val pairs."
  1568. (let ((data (assoc type org-link-parameters)))
  1569. (if data (setcdr data (org-combine-plists (cdr data) parameters))
  1570. (push (cons type parameters) org-link-parameters)
  1571. (org-make-link-regexps)
  1572. (org-element-update-syntax))))
  1573. (defun org-link-types ()
  1574. "Return a list of known link types."
  1575. (mapcar #'car org-link-parameters))
  1576. (defcustom org-link-abbrev-alist nil
  1577. "Alist of link abbreviations.
  1578. The car of each element is a string, to be replaced at the start of a link.
  1579. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1580. links in Org buffers can have an optional tag after a double colon, e.g.,
  1581. [[linkkey:tag][description]]
  1582. The `linkkey' must be a single word, starting with a letter, followed
  1583. by letters, numbers, `-' or `_'.
  1584. If REPLACE is a string, the tag will simply be appended to create the link.
  1585. If the string contains \"%s\", the tag will be inserted there. If the string
  1586. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1587. at that point (see the function `url-hexify-string'). If the string contains
  1588. the specifier \"%(my-function)\", then the custom function `my-function' will
  1589. be invoked: this function takes the tag as its only argument and must return
  1590. a string.
  1591. REPLACE may also be a function that will be called with the tag as the
  1592. only argument to create the link, which should be returned as a string.
  1593. See the manual for examples."
  1594. :group 'org-link
  1595. :type '(repeat
  1596. (cons
  1597. (string :tag "Protocol")
  1598. (choice
  1599. (string :tag "Format")
  1600. (function)))))
  1601. (defcustom org-descriptive-links t
  1602. "Non-nil means Org will display descriptive links.
  1603. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1604. \"Org Website\", hiding the link itself and just displaying its
  1605. description. When set to nil, Org will display the full links
  1606. literally.
  1607. You can interactively set the value of this variable by calling
  1608. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1609. :group 'org-link
  1610. :type 'boolean)
  1611. (defcustom org-link-file-path-type 'adaptive
  1612. "How the path name in file links should be stored.
  1613. Valid values are:
  1614. relative Relative to the current directory, i.e. the directory of the file
  1615. into which the link is being inserted.
  1616. absolute Absolute path, if possible with ~ for home directory.
  1617. noabbrev Absolute path, no abbreviation of home directory.
  1618. adaptive Use relative path for files in the current directory and sub-
  1619. directories of it. For other files, use an absolute path."
  1620. :group 'org-link
  1621. :type '(choice
  1622. (const relative)
  1623. (const absolute)
  1624. (const noabbrev)
  1625. (const adaptive)))
  1626. (defvaralias 'org-activate-links 'org-highlight-links)
  1627. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1628. "Types of links that should be highlighted in Org files.
  1629. This is a list of symbols, each one of them leading to the
  1630. highlighting of a certain link type.
  1631. You can still open links that are not highlighted.
  1632. In principle, it does not hurt to turn on highlighting for all
  1633. link types. There may be a small gain when turning off unused
  1634. link types. The types are:
  1635. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1636. angle Links in angular brackets that may contain whitespace like
  1637. <bbdb:Carsten Dominik>.
  1638. plain Plain links in normal text, no whitespace, like http://google.com.
  1639. radio Text that is matched by a radio target, see manual for details.
  1640. tag Tag settings in a headline (link to tag search).
  1641. date Time stamps (link to calendar).
  1642. footnote Footnote labels.
  1643. If you set this variable during an Emacs session, use `org-mode-restart'
  1644. in the Org buffer so that the change takes effect."
  1645. :group 'org-link
  1646. :group 'org-appearance
  1647. :type '(set :greedy t
  1648. (const :tag "Double bracket links" bracket)
  1649. (const :tag "Angular bracket links" angle)
  1650. (const :tag "Plain text links" plain)
  1651. (const :tag "Radio target matches" radio)
  1652. (const :tag "Tags" tag)
  1653. (const :tag "Timestamps" date)
  1654. (const :tag "Footnotes" footnote)))
  1655. (defcustom org-make-link-description-function nil
  1656. "Function to use for generating link descriptions from links.
  1657. This function must take two parameters: the first one is the
  1658. link, the second one is the description generated by
  1659. `org-insert-link'. The function should return the description to
  1660. use."
  1661. :group 'org-link
  1662. :type '(choice (const nil) (function)))
  1663. (defgroup org-link-store nil
  1664. "Options concerning storing links in Org mode."
  1665. :tag "Org Store Link"
  1666. :group 'org-link)
  1667. (defcustom org-url-hexify-p t
  1668. "When non-nil, hexify URL when creating a link."
  1669. :type 'boolean
  1670. :version "24.3"
  1671. :group 'org-link-store)
  1672. (defcustom org-email-link-description-format "Email %c: %.30s"
  1673. "Format of the description part of a link to an email or usenet message.
  1674. The following %-escapes will be replaced by corresponding information:
  1675. %F full \"From\" field
  1676. %f name, taken from \"From\" field, address if no name
  1677. %T full \"To\" field
  1678. %t first name in \"To\" field, address if no name
  1679. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1680. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1681. %s subject
  1682. %d date
  1683. %m message-id.
  1684. You may use normal field width specification between the % and the letter.
  1685. This is for example useful to limit the length of the subject.
  1686. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1687. :group 'org-link-store
  1688. :type 'string)
  1689. (defcustom org-from-is-user-regexp
  1690. (let (r1 r2)
  1691. (when (and user-mail-address (not (string= user-mail-address "")))
  1692. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1693. (when (and user-full-name (not (string= user-full-name "")))
  1694. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1695. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1696. "Regexp matched against the \"From:\" header of an email or usenet message.
  1697. It should match if the message is from the user him/herself."
  1698. :group 'org-link-store
  1699. :type 'regexp)
  1700. (defcustom org-context-in-file-links t
  1701. "Non-nil means file links from `org-store-link' contain context.
  1702. \\<org-mode-map>
  1703. A search string will be added to the file name with :: as separator
  1704. and used to find the context when the link is activated by the command
  1705. `org-open-at-point'. When this option is t, the entire active region
  1706. will be placed in the search string of the file link. If set to a
  1707. positive integer, only the first n lines of context will be stored.
  1708. Using a prefix arg to the command `org-store-link' (`\\[universal-argument] \
  1709. \\[org-store-link]')
  1710. negates this setting for the duration of the command."
  1711. :group 'org-link-store
  1712. :type '(choice boolean integer))
  1713. (defcustom org-keep-stored-link-after-insertion nil
  1714. "Non-nil means keep link in list for entire session.
  1715. \\<org-mode-map>
  1716. The command `org-store-link' adds a link pointing to the current
  1717. location to an internal list. These links accumulate during a session.
  1718. The command `org-insert-link' can be used to insert links into any
  1719. Org file (offering completion for all stored links).
  1720. When this option is nil, every link which has been inserted once using
  1721. `\\[org-insert-link]' will be removed from the list, to make completing the \
  1722. unused
  1723. links more efficient."
  1724. :group 'org-link-store
  1725. :type 'boolean)
  1726. (defgroup org-link-follow nil
  1727. "Options concerning following links in Org mode."
  1728. :tag "Org Follow Link"
  1729. :group 'org-link)
  1730. (defcustom org-link-translation-function nil
  1731. "Function to translate links with different syntax to Org syntax.
  1732. This can be used to translate links created for example by the Planner
  1733. or emacs-wiki packages to Org syntax.
  1734. The function must accept two parameters, a TYPE containing the link
  1735. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1736. which is everything after the link protocol. It should return a cons
  1737. with possibly modified values of type and path.
  1738. Org contains a function for this, so if you set this variable to
  1739. `org-translate-link-from-planner', you should be able follow many
  1740. links created by planner."
  1741. :group 'org-link-follow
  1742. :type '(choice (const nil) (function)))
  1743. (defcustom org-follow-link-hook nil
  1744. "Hook that is run after a link has been followed."
  1745. :group 'org-link-follow
  1746. :type 'hook)
  1747. (defcustom org-tab-follows-link nil
  1748. "Non-nil means on links TAB will follow the link.
  1749. Needs to be set before org.el is loaded.
  1750. This really should not be used, it does not make sense, and the
  1751. implementation is bad."
  1752. :group 'org-link-follow
  1753. :type 'boolean)
  1754. (defcustom org-return-follows-link nil
  1755. "Non-nil means on links RET will follow the link.
  1756. In tables, the special behavior of RET has precedence."
  1757. :group 'org-link-follow
  1758. :type 'boolean)
  1759. (defcustom org-mouse-1-follows-link
  1760. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1761. "Non-nil means mouse-1 on a link will follow the link.
  1762. A longer mouse click will still set point. Needs to be set
  1763. before org.el is loaded."
  1764. :group 'org-link-follow
  1765. :version "26.1"
  1766. :package-version '(Org . "8.3")
  1767. :type '(choice
  1768. (const :tag "A double click follows the link" double)
  1769. (const :tag "Unconditionally follow the link with mouse-1" t)
  1770. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1771. (defcustom org-mark-ring-length 4
  1772. "Number of different positions to be recorded in the ring.
  1773. Changing this requires a restart of Emacs to work correctly."
  1774. :group 'org-link-follow
  1775. :type 'integer)
  1776. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1777. "Non-nil means internal fuzzy links can only match headlines.
  1778. When nil, the a fuzzy link may point to a target or a named
  1779. construct in the document. When set to the special value
  1780. `query-to-create', offer to create a new headline when none
  1781. matched.
  1782. Spaces and statistics cookies are ignored during heading searches."
  1783. :group 'org-link-follow
  1784. :version "24.1"
  1785. :type '(choice
  1786. (const :tag "Use fuzzy text search" nil)
  1787. (const :tag "Match only exact headline" t)
  1788. (const :tag "Match exact headline or query to create it"
  1789. query-to-create))
  1790. :safe #'symbolp)
  1791. (defcustom org-link-frame-setup
  1792. '((vm . vm-visit-folder-other-frame)
  1793. (vm-imap . vm-visit-imap-folder-other-frame)
  1794. (gnus . org-gnus-no-new-news)
  1795. (file . find-file-other-window)
  1796. (wl . wl-other-frame))
  1797. "Setup the frame configuration for following links.
  1798. When following a link with Emacs, it may often be useful to display
  1799. this link in another window or frame. This variable can be used to
  1800. set this up for the different types of links.
  1801. For VM, use any of
  1802. `vm-visit-folder'
  1803. `vm-visit-folder-other-window'
  1804. `vm-visit-folder-other-frame'
  1805. For Gnus, use any of
  1806. `gnus'
  1807. `gnus-other-frame'
  1808. `org-gnus-no-new-news'
  1809. For FILE, use any of
  1810. `find-file'
  1811. `find-file-other-window'
  1812. `find-file-other-frame'
  1813. For Wanderlust use any of
  1814. `wl'
  1815. `wl-other-frame'
  1816. For the calendar, use the variable `calendar-setup'.
  1817. For BBDB, it is currently only possible to display the matches in
  1818. another window."
  1819. :group 'org-link-follow
  1820. :type '(list
  1821. (cons (const vm)
  1822. (choice
  1823. (const vm-visit-folder)
  1824. (const vm-visit-folder-other-window)
  1825. (const vm-visit-folder-other-frame)))
  1826. (cons (const vm-imap)
  1827. (choice
  1828. (const vm-visit-imap-folder)
  1829. (const vm-visit-imap-folder-other-window)
  1830. (const vm-visit-imap-folder-other-frame)))
  1831. (cons (const gnus)
  1832. (choice
  1833. (const gnus)
  1834. (const gnus-other-frame)
  1835. (const org-gnus-no-new-news)))
  1836. (cons (const file)
  1837. (choice
  1838. (const find-file)
  1839. (const find-file-other-window)
  1840. (const find-file-other-frame)))
  1841. (cons (const wl)
  1842. (choice
  1843. (const wl)
  1844. (const wl-other-frame)))))
  1845. (defcustom org-display-internal-link-with-indirect-buffer nil
  1846. "Non-nil means use indirect buffer to display infile links.
  1847. Activating internal links (from one location in a file to another location
  1848. in the same file) normally just jumps to the location. When the link is
  1849. activated with a `\\[universal-argument]' prefix (or with mouse-3), the link \
  1850. is displayed in
  1851. another window. When this option is set, the other window actually displays
  1852. an indirect buffer clone of the current buffer, to avoid any visibility
  1853. changes to the current buffer."
  1854. :group 'org-link-follow
  1855. :type 'boolean)
  1856. (defcustom org-open-non-existing-files nil
  1857. "Non-nil means `org-open-file' will open non-existing files.
  1858. When nil, an error will be generated.
  1859. This variable applies only to external applications because they
  1860. might choke on non-existing files. If the link is to a file that
  1861. will be opened in Emacs, the variable is ignored."
  1862. :group 'org-link-follow
  1863. :type 'boolean)
  1864. (defcustom org-open-directory-means-index-dot-org nil
  1865. "Non-nil means a link to a directory really means to index.org.
  1866. When nil, following a directory link will run dired or open a finder/explorer
  1867. window on that directory."
  1868. :group 'org-link-follow
  1869. :type 'boolean)
  1870. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1871. "Non-nil means ask for confirmation before executing shell links.
  1872. Shell links can be dangerous: just think about a link
  1873. [[shell:rm -rf ~/*][Google Search]]
  1874. This link would show up in your Org document as \"Google Search\",
  1875. but really it would remove your entire home directory.
  1876. Therefore we advise against setting this variable to nil.
  1877. Just change it to `y-or-n-p' if you want to confirm with a
  1878. single keystroke rather than having to type \"yes\"."
  1879. :group 'org-link-follow
  1880. :type '(choice
  1881. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1882. (const :tag "with y-or-n (faster)" y-or-n-p)
  1883. (const :tag "no confirmation (dangerous)" nil)))
  1884. (put 'org-confirm-shell-link-function
  1885. 'safe-local-variable
  1886. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1887. (defcustom org-confirm-shell-link-not-regexp ""
  1888. "A regexp to skip confirmation for shell links."
  1889. :group 'org-link-follow
  1890. :version "24.1"
  1891. :type 'regexp)
  1892. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1893. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1894. Elisp links can be dangerous: just think about a link
  1895. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1896. This link would show up in your Org document as \"Google Search\",
  1897. but really it would remove your entire home directory.
  1898. Therefore we advise against setting this variable to nil.
  1899. Just change it to `y-or-n-p' if you want to confirm with a
  1900. single keystroke rather than having to type \"yes\"."
  1901. :group 'org-link-follow
  1902. :type '(choice
  1903. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1904. (const :tag "with y-or-n (faster)" y-or-n-p)
  1905. (const :tag "no confirmation (dangerous)" nil)))
  1906. (put 'org-confirm-shell-link-function
  1907. 'safe-local-variable
  1908. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1909. (defcustom org-confirm-elisp-link-not-regexp ""
  1910. "A regexp to skip confirmation for Elisp links."
  1911. :group 'org-link-follow
  1912. :version "24.1"
  1913. :type 'regexp)
  1914. (defconst org-file-apps-defaults-gnu
  1915. '((remote . emacs)
  1916. (system . mailcap)
  1917. (t . mailcap))
  1918. "Default file applications on a UNIX or GNU/Linux system.
  1919. See `org-file-apps'.")
  1920. (defconst org-file-apps-defaults-macosx
  1921. '((remote . emacs)
  1922. (system . "open %s")
  1923. ("ps.gz" . "gv %s")
  1924. ("eps.gz" . "gv %s")
  1925. ("dvi" . "xdvi %s")
  1926. ("fig" . "xfig %s")
  1927. (t . "open %s"))
  1928. "Default file applications on a macOS system.
  1929. The system \"open\" is known as a default, but we use X11 applications
  1930. for some files for which the OS does not have a good default.
  1931. See `org-file-apps'.")
  1932. (defconst org-file-apps-defaults-windowsnt
  1933. (list '(remote . emacs)
  1934. (cons 'system (lambda (file _path)
  1935. (with-no-warnings (w32-shell-execute "open" file))))
  1936. (cons t (lambda (file _path)
  1937. (with-no-warnings (w32-shell-execute "open" file)))))
  1938. "Default file applications on a Windows NT system.
  1939. The system \"open\" is used for most files.
  1940. See `org-file-apps'.")
  1941. (defcustom org-file-apps
  1942. '((auto-mode . emacs)
  1943. ("\\.mm\\'" . default)
  1944. ("\\.x?html?\\'" . default)
  1945. ("\\.pdf\\'" . default))
  1946. "External applications for opening `file:path' items in a document.
  1947. \\<org-mode-map>\
  1948. Org mode uses system defaults for different file types, but
  1949. you can use this variable to set the application for a given file
  1950. extension. The entries in this list are cons cells where the car identifies
  1951. files and the cdr the corresponding command.
  1952. Possible values for the file identifier are:
  1953. \"string\" A string as a file identifier can be interpreted in different
  1954. ways, depending on its contents:
  1955. - Alphanumeric characters only:
  1956. Match links with this file extension.
  1957. Example: (\"pdf\" . \"evince %s\")
  1958. to open PDFs with evince.
  1959. - Regular expression: Match links where the
  1960. filename matches the regexp. If you want to
  1961. use groups here, use shy groups.
  1962. Example: (\"\\\\.x?html\\\\\\='\" . \"firefox %s\")
  1963. (\"\\\\(?:xhtml\\\\|html\\\\)\\\\\\='\" . \"firefox %s\")
  1964. to open *.html and *.xhtml with firefox.
  1965. - Regular expression which contains (non-shy) groups:
  1966. Match links where the whole link, including \"::\", and
  1967. anything after that, matches the regexp.
  1968. In a custom command string, %1, %2, etc. are replaced with
  1969. the parts of the link that were matched by the groups.
  1970. For backwards compatibility, if a command string is given
  1971. that does not use any of the group matches, this case is
  1972. handled identically to the second one (i.e. match against
  1973. file name only).
  1974. In a custom function, you can access the group matches with
  1975. (match-string n link).
  1976. Example: (\"\\\\.pdf::\\\\(\\\\d+\\\\)\\\\\\='\" . \
  1977. \"evince -p %1 %s\")
  1978. to open [[file:document.pdf::5]] with evince at page 5.
  1979. `directory' Matches a directory
  1980. `remote' Matches a remote file, accessible through tramp or efs.
  1981. Remote files most likely should be visited through Emacs
  1982. because external applications cannot handle such paths.
  1983. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1984. so all files Emacs knows how to handle. Using this with
  1985. command `emacs' will open most files in Emacs. Beware that this
  1986. will also open html files inside Emacs, unless you add
  1987. (\"html\" . default) to the list as well.
  1988. `system' The system command to open files, like `open' on Windows
  1989. and macOS, and mailcap under GNU/Linux. This is the command
  1990. that will be selected if you call `org-open-at-point' with a
  1991. double prefix argument (`\\[universal-argument] \
  1992. \\[universal-argument] \\[org-open-at-point]').
  1993. t Default for files not matched by any of the other options.
  1994. Possible values for the command are:
  1995. `emacs' The file will be visited by the current Emacs process.
  1996. `default' Use the default application for this file type, which is the
  1997. association for t in the list, most likely in the system-specific
  1998. part. This can be used to overrule an unwanted setting in the
  1999. system-specific variable.
  2000. `system' Use the system command for opening files, like \"open\".
  2001. This command is specified by the entry whose car is `system'.
  2002. Most likely, the system-specific version of this variable
  2003. does define this command, but you can overrule/replace it
  2004. here.
  2005. `mailcap' Use command specified in the mailcaps.
  2006. string A command to be executed by a shell; %s will be replaced
  2007. by the path to the file.
  2008. function A Lisp function, which will be called with two arguments:
  2009. the file path and the original link string, without the
  2010. \"file:\" prefix.
  2011. For more examples, see the system specific constants
  2012. `org-file-apps-defaults-macosx'
  2013. `org-file-apps-defaults-windowsnt'
  2014. `org-file-apps-defaults-gnu'."
  2015. :group 'org-link-follow
  2016. :type '(repeat
  2017. (cons (choice :value ""
  2018. (string :tag "Extension")
  2019. (const :tag "System command to open files" system)
  2020. (const :tag "Default for unrecognized files" t)
  2021. (const :tag "Remote file" remote)
  2022. (const :tag "Links to a directory" directory)
  2023. (const :tag "Any files that have Emacs modes"
  2024. auto-mode))
  2025. (choice :value ""
  2026. (const :tag "Visit with Emacs" emacs)
  2027. (const :tag "Use default" default)
  2028. (const :tag "Use the system command" system)
  2029. (string :tag "Command")
  2030. (function :tag "Function")))))
  2031. (defcustom org-doi-server-url "http://dx.doi.org/"
  2032. "The URL of the DOI server."
  2033. :type 'string
  2034. :version "24.3"
  2035. :group 'org-link-follow)
  2036. (defgroup org-refile nil
  2037. "Options concerning refiling entries in Org mode."
  2038. :tag "Org Refile"
  2039. :group 'org)
  2040. (defcustom org-directory "~/org"
  2041. "Directory with Org files.
  2042. This is just a default location to look for Org files. There is no need
  2043. at all to put your files into this directory. It is used in the
  2044. following situations:
  2045. 1. When a capture template specifies a target file that is not an
  2046. absolute path. The path will then be interpreted relative to
  2047. `org-directory'
  2048. 2. When the value of variable `org-agenda-files' is a single file, any
  2049. relative paths in this file will be taken as relative to
  2050. `org-directory'."
  2051. :group 'org-refile
  2052. :group 'org-capture
  2053. :type 'directory)
  2054. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2055. "Default target for storing notes.
  2056. Used as a fall back file for org-capture.el, for templates that
  2057. do not specify a target file."
  2058. :group 'org-refile
  2059. :group 'org-capture
  2060. :type 'file)
  2061. (defcustom org-goto-interface 'outline
  2062. "The default interface to be used for `org-goto'.
  2063. Allowed values are:
  2064. outline The interface shows an outline of the relevant file
  2065. and the correct heading is found by moving through
  2066. the outline or by searching with incremental search.
  2067. outline-path-completion Headlines in the current buffer are offered via
  2068. completion. This is the interface also used by
  2069. the refile command."
  2070. :group 'org-refile
  2071. :type '(choice
  2072. (const :tag "Outline" outline)
  2073. (const :tag "Outline-path-completion" outline-path-completion)))
  2074. (defcustom org-goto-max-level 5
  2075. "Maximum target level when running `org-goto' with refile interface."
  2076. :group 'org-refile
  2077. :type 'integer)
  2078. (defcustom org-reverse-note-order nil
  2079. "Non-nil means store new notes at the beginning of a file or entry.
  2080. When nil, new notes will be filed to the end of a file or entry.
  2081. This can also be a list with cons cells of regular expressions that
  2082. are matched against file names, and values."
  2083. :group 'org-capture
  2084. :group 'org-refile
  2085. :type '(choice
  2086. (const :tag "Reverse always" t)
  2087. (const :tag "Reverse never" nil)
  2088. (repeat :tag "By file name regexp"
  2089. (cons regexp boolean))))
  2090. (defcustom org-log-refile nil
  2091. "Information to record when a task is refiled.
  2092. Possible values are:
  2093. nil Don't add anything
  2094. time Add a time stamp to the task
  2095. note Prompt for a note and add it with template `org-log-note-headings'
  2096. This option can also be set with on a per-file-basis with
  2097. #+STARTUP: nologrefile
  2098. #+STARTUP: logrefile
  2099. #+STARTUP: lognoterefile
  2100. You can have local logging settings for a subtree by setting the LOGGING
  2101. property to one or more of these keywords.
  2102. When bulk-refiling from the agenda, the value `note' is forbidden and
  2103. will temporarily be changed to `time'."
  2104. :group 'org-refile
  2105. :group 'org-progress
  2106. :version "24.1"
  2107. :type '(choice
  2108. (const :tag "No logging" nil)
  2109. (const :tag "Record timestamp" time)
  2110. (const :tag "Record timestamp with note." note)))
  2111. (defcustom org-refile-targets nil
  2112. "Targets for refiling entries with `\\[org-refile]'.
  2113. This is a list of cons cells. Each cell contains:
  2114. - a specification of the files to be considered, either a list of files,
  2115. or a symbol whose function or variable value will be used to retrieve
  2116. a file name or a list of file names. If you use `org-agenda-files' for
  2117. that, all agenda files will be scanned for targets. Nil means consider
  2118. headings in the current buffer.
  2119. - A specification of how to find candidate refile targets. This may be
  2120. any of:
  2121. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2122. This tag has to be present in all target headlines, inheritance will
  2123. not be considered.
  2124. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2125. todo keyword.
  2126. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2127. headlines that are refiling targets.
  2128. - a cons cell (:level . N). Any headline of level N is considered a target.
  2129. Note that, when `org-odd-levels-only' is set, level corresponds to
  2130. order in hierarchy, not to the number of stars.
  2131. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2132. Note that, when `org-odd-levels-only' is set, level corresponds to
  2133. order in hierarchy, not to the number of stars.
  2134. Each element of this list generates a set of possible targets.
  2135. The union of these sets is presented (with completion) to
  2136. the user by `org-refile'.
  2137. You can set the variable `org-refile-target-verify-function' to a function
  2138. to verify each headline found by the simple criteria above.
  2139. When this variable is nil, all top-level headlines in the current buffer
  2140. are used, equivalent to the value `((nil . (:level . 1))'."
  2141. :group 'org-refile
  2142. :type '(repeat
  2143. (cons
  2144. (choice :value org-agenda-files
  2145. (const :tag "All agenda files" org-agenda-files)
  2146. (const :tag "Current buffer" nil)
  2147. (function) (variable) (file))
  2148. (choice :tag "Identify target headline by"
  2149. (cons :tag "Specific tag" (const :value :tag) (string))
  2150. (cons :tag "TODO keyword" (const :value :todo) (string))
  2151. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2152. (cons :tag "Level number" (const :value :level) (integer))
  2153. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2154. (defcustom org-refile-target-verify-function nil
  2155. "Function to verify if the headline at point should be a refile target.
  2156. The function will be called without arguments, with point at the
  2157. beginning of the headline. It should return t and leave point
  2158. where it is if the headline is a valid target for refiling.
  2159. If the target should not be selected, the function must return nil.
  2160. In addition to this, it may move point to a place from where the search
  2161. should be continued. For example, the function may decide that the entire
  2162. subtree of the current entry should be excluded and move point to the end
  2163. of the subtree."
  2164. :group 'org-refile
  2165. :type '(choice
  2166. (const nil)
  2167. (function)))
  2168. (defcustom org-refile-use-cache nil
  2169. "Non-nil means cache refile targets to speed up the process.
  2170. \\<org-mode-map>\
  2171. The cache for a particular file will be updated automatically when
  2172. the buffer has been killed, or when any of the marker used for flagging
  2173. refile targets no longer points at a live buffer.
  2174. If you have added new entries to a buffer that might themselves be targets,
  2175. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2176. if you find that easier, \
  2177. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2178. \\[org-refile]'."
  2179. :group 'org-refile
  2180. :version "24.1"
  2181. :type 'boolean)
  2182. (defcustom org-refile-use-outline-path nil
  2183. "Non-nil means provide refile targets as paths.
  2184. So a level 3 headline will be available as level1/level2/level3.
  2185. When the value is `file', also include the file name (without directory)
  2186. into the path. In this case, you can also stop the completion after
  2187. the file name, to get entries inserted as top level in the file.
  2188. When `full-file-path', include the full file path.
  2189. When `buffer-name', use the buffer name."
  2190. :group 'org-refile
  2191. :type '(choice
  2192. (const :tag "Not" nil)
  2193. (const :tag "Yes" t)
  2194. (const :tag "Start with file name" file)
  2195. (const :tag "Start with full file path" full-file-path)
  2196. (const :tag "Start with buffer name" buffer-name)))
  2197. (defcustom org-outline-path-complete-in-steps t
  2198. "Non-nil means complete the outline path in hierarchical steps.
  2199. When Org uses the refile interface to select an outline path (see
  2200. `org-refile-use-outline-path'), the completion of the path can be
  2201. done in a single go, or it can be done in steps down the headline
  2202. hierarchy. Going in steps is probably the best if you do not use
  2203. a special completion package like `ido' or `icicles'. However,
  2204. when using these packages, going in one step can be very fast,
  2205. while still showing the whole path to the entry."
  2206. :group 'org-refile
  2207. :type 'boolean)
  2208. (defcustom org-refile-allow-creating-parent-nodes nil
  2209. "Non-nil means allow the creation of new nodes as refile targets.
  2210. New nodes are then created by adding \"/new node name\" to the completion
  2211. of an existing node. When the value of this variable is `confirm',
  2212. new node creation must be confirmed by the user (recommended).
  2213. When nil, the completion must match an existing entry.
  2214. Note that, if the new heading is not seen by the criteria
  2215. listed in `org-refile-targets', multiple instances of the same
  2216. heading would be created by trying again to file under the new
  2217. heading."
  2218. :group 'org-refile
  2219. :type '(choice
  2220. (const :tag "Never" nil)
  2221. (const :tag "Always" t)
  2222. (const :tag "Prompt for confirmation" confirm)))
  2223. (defcustom org-refile-active-region-within-subtree nil
  2224. "Non-nil means also refile active region within a subtree.
  2225. By default `org-refile' doesn't allow refiling regions if they
  2226. don't contain a set of subtrees, but it might be convenient to
  2227. do so sometimes: in that case, the first line of the region is
  2228. converted to a headline before refiling."
  2229. :group 'org-refile
  2230. :version "24.1"
  2231. :type 'boolean)
  2232. (defgroup org-todo nil
  2233. "Options concerning TODO items in Org mode."
  2234. :tag "Org TODO"
  2235. :group 'org)
  2236. (defgroup org-progress nil
  2237. "Options concerning Progress logging in Org mode."
  2238. :tag "Org Progress"
  2239. :group 'org-time)
  2240. (defvar org-todo-interpretation-widgets
  2241. '((:tag "Sequence (cycling hits every state)" sequence)
  2242. (:tag "Type (cycling directly to DONE)" type))
  2243. "The available interpretation symbols for customizing `org-todo-keywords'.
  2244. Interested libraries should add to this list.")
  2245. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2246. "List of TODO entry keyword sequences and their interpretation.
  2247. \\<org-mode-map>This is a list of sequences.
  2248. Each sequence starts with a symbol, either `sequence' or `type',
  2249. indicating if the keywords should be interpreted as a sequence of
  2250. action steps, or as different types of TODO items. The first
  2251. keywords are states requiring action - these states will select a headline
  2252. for inclusion into the global TODO list Org produces. If one of the
  2253. \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2254. signify that no further action is necessary. If \"|\" is not found,
  2255. the last keyword is treated as the only DONE state of the sequence.
  2256. The command `\\[org-todo]' cycles an entry through these states, and one
  2257. additional state where no keyword is present. For details about this
  2258. cycling, see the manual.
  2259. TODO keywords and interpretation can also be set on a per-file basis with
  2260. the special #+SEQ_TODO and #+TYP_TODO lines.
  2261. Each keyword can optionally specify a character for fast state selection
  2262. \(in combination with the variable `org-use-fast-todo-selection')
  2263. and specifiers for state change logging, using the same syntax that
  2264. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2265. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2266. indicates to record a time stamp each time this state is selected.
  2267. Each keyword may also specify if a timestamp or a note should be
  2268. recorded when entering or leaving the state, by adding additional
  2269. characters in the parenthesis after the keyword. This looks like this:
  2270. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2271. record only the time of the state change. With X and Y being either
  2272. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2273. Y when leaving the state if and only if the *target* state does not
  2274. define X. You may omit any of the fast-selection key or X or /Y,
  2275. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2276. For backward compatibility, this variable may also be just a list
  2277. of keywords. In this case the interpretation (sequence or type) will be
  2278. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2279. :group 'org-todo
  2280. :group 'org-keywords
  2281. :type '(choice
  2282. (repeat :tag "Old syntax, just keywords"
  2283. (string :tag "Keyword"))
  2284. (repeat :tag "New syntax"
  2285. (cons
  2286. (choice
  2287. :tag "Interpretation"
  2288. ;;Quick and dirty way to see
  2289. ;;`org-todo-interpretations'. This takes the
  2290. ;;place of item arguments
  2291. :convert-widget
  2292. (lambda (widget)
  2293. (widget-put widget
  2294. :args (mapcar
  2295. (lambda (x)
  2296. (widget-convert
  2297. (cons 'const x)))
  2298. org-todo-interpretation-widgets))
  2299. widget))
  2300. (repeat
  2301. (string :tag "Keyword"))))))
  2302. (defvar-local org-todo-keywords-1 nil
  2303. "All TODO and DONE keywords active in a buffer.")
  2304. (defvar org-todo-keywords-for-agenda nil)
  2305. (defvar org-done-keywords-for-agenda nil)
  2306. (defvar org-todo-keyword-alist-for-agenda nil)
  2307. (defvar org-tag-alist-for-agenda nil
  2308. "Alist of all tags from all agenda files.")
  2309. (defvar org-tag-groups-alist-for-agenda nil
  2310. "Alist of all groups tags from all current agenda files.")
  2311. (defvar-local org-tag-groups-alist nil)
  2312. (defvar org-agenda-contributing-files nil)
  2313. (defvar-local org-current-tag-alist nil
  2314. "Alist of all tag groups in current buffer.
  2315. This variable takes into consideration `org-tag-alist',
  2316. `org-tag-persistent-alist' and TAGS keywords in the buffer.")
  2317. (defvar-local org-not-done-keywords nil)
  2318. (defvar-local org-done-keywords nil)
  2319. (defvar-local org-todo-heads nil)
  2320. (defvar-local org-todo-sets nil)
  2321. (defvar-local org-todo-log-states nil)
  2322. (defvar-local org-todo-kwd-alist nil)
  2323. (defvar-local org-todo-key-alist nil)
  2324. (defvar-local org-todo-key-trigger nil)
  2325. (defcustom org-todo-interpretation 'sequence
  2326. "Controls how TODO keywords are interpreted.
  2327. This variable is in principle obsolete and is only used for
  2328. backward compatibility, if the interpretation of todo keywords is
  2329. not given already in `org-todo-keywords'. See that variable for
  2330. more information."
  2331. :group 'org-todo
  2332. :group 'org-keywords
  2333. :type '(choice (const sequence)
  2334. (const type)))
  2335. (defcustom org-use-fast-todo-selection t
  2336. "\\<org-mode-map>\
  2337. Non-nil means use the fast todo selection scheme with `\\[org-todo]'.
  2338. This variable describes if and under what circumstances the cycling
  2339. mechanism for TODO keywords will be replaced by a single-key, direct
  2340. selection scheme.
  2341. When nil, fast selection is never used.
  2342. When the symbol `prefix', it will be used when `org-todo' is called
  2343. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2344. in an Org buffer, and
  2345. `\\[universal-argument] t' in an agenda buffer.
  2346. When t, fast selection is used by default. In this case, the prefix
  2347. argument forces cycling instead.
  2348. In all cases, the special interface is only used if access keys have
  2349. actually been assigned by the user, i.e. if keywords in the configuration
  2350. are followed by a letter in parenthesis, like TODO(t)."
  2351. :group 'org-todo
  2352. :type '(choice
  2353. (const :tag "Never" nil)
  2354. (const :tag "By default" t)
  2355. (const :tag "Only with C-u C-c C-t" prefix)))
  2356. (defcustom org-provide-todo-statistics t
  2357. "Non-nil means update todo statistics after insert and toggle.
  2358. ALL-HEADLINES means update todo statistics by including headlines
  2359. with no TODO keyword as well, counting them as not done.
  2360. A list of TODO keywords means the same, but skip keywords that are
  2361. not in this list.
  2362. When set to a list of two lists, the first list contains keywords
  2363. to consider as TODO keywords, the second list contains keywords
  2364. to consider as DONE keywords.
  2365. When this is set, todo statistics is updated in the parent of the
  2366. current entry each time a todo state is changed."
  2367. :group 'org-todo
  2368. :type '(choice
  2369. (const :tag "Yes, only for TODO entries" t)
  2370. (const :tag "Yes, including all entries" all-headlines)
  2371. (repeat :tag "Yes, for TODOs in this list"
  2372. (string :tag "TODO keyword"))
  2373. (list :tag "Yes, for TODOs and DONEs in these lists"
  2374. (repeat (string :tag "TODO keyword"))
  2375. (repeat (string :tag "DONE keyword")))
  2376. (other :tag "No TODO statistics" nil)))
  2377. (defcustom org-hierarchical-todo-statistics t
  2378. "Non-nil means TODO statistics covers just direct children.
  2379. When nil, all entries in the subtree are considered.
  2380. This has only an effect if `org-provide-todo-statistics' is set.
  2381. To set this to nil for only a single subtree, use a COOKIE_DATA
  2382. property and include the word \"recursive\" into the value."
  2383. :group 'org-todo
  2384. :type 'boolean)
  2385. (defcustom org-after-todo-state-change-hook nil
  2386. "Hook which is run after the state of a TODO item was changed.
  2387. The new state (a string with a TODO keyword, or nil) is available in the
  2388. Lisp variable `org-state'."
  2389. :group 'org-todo
  2390. :type 'hook)
  2391. (defvar org-blocker-hook nil
  2392. "Hook for functions that are allowed to block a state change.
  2393. Functions in this hook should not modify the buffer.
  2394. Each function gets as its single argument a property list,
  2395. see `org-trigger-hook' for more information about this list.
  2396. If any of the functions in this hook returns nil, the state change
  2397. is blocked.")
  2398. (defvar org-trigger-hook nil
  2399. "Hook for functions that are triggered by a state change.
  2400. Each function gets as its single argument a property list with at
  2401. least the following elements:
  2402. (:type type-of-change :position pos-at-entry-start
  2403. :from old-state :to new-state)
  2404. Depending on the type, more properties may be present.
  2405. This mechanism is currently implemented for:
  2406. TODO state changes
  2407. ------------------
  2408. :type todo-state-change
  2409. :from previous state (keyword as a string), or nil, or a symbol
  2410. `todo' or `done', to indicate the general type of state.
  2411. :to new state, like in :from")
  2412. (defcustom org-enforce-todo-dependencies nil
  2413. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2414. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2415. be blocked if any prior sibling is not yet done.
  2416. Finally, if the parent is blocked because of ordered siblings of its own,
  2417. the child will also be blocked."
  2418. :set (lambda (var val)
  2419. (set var val)
  2420. (if val
  2421. (add-hook 'org-blocker-hook
  2422. 'org-block-todo-from-children-or-siblings-or-parent)
  2423. (remove-hook 'org-blocker-hook
  2424. 'org-block-todo-from-children-or-siblings-or-parent)))
  2425. :group 'org-todo
  2426. :type 'boolean)
  2427. (defcustom org-enforce-todo-checkbox-dependencies nil
  2428. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2429. When this is nil, checkboxes have no influence on switching TODO states.
  2430. When non-nil, you first need to check off all check boxes before the TODO
  2431. entry can be switched to DONE.
  2432. This variable needs to be set before org.el is loaded, and you need to
  2433. restart Emacs after a change to make the change effective. The only way
  2434. to change is while Emacs is running is through the customize interface."
  2435. :set (lambda (var val)
  2436. (set var val)
  2437. (if val
  2438. (add-hook 'org-blocker-hook
  2439. 'org-block-todo-from-checkboxes)
  2440. (remove-hook 'org-blocker-hook
  2441. 'org-block-todo-from-checkboxes)))
  2442. :group 'org-todo
  2443. :type 'boolean)
  2444. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2445. "Non-nil means inserting a TODO heading is treated as state change.
  2446. So when the command `\\[org-insert-todo-heading]' is used, state change
  2447. logging will apply if appropriate. When nil, the new TODO item will
  2448. be inserted directly, and no logging will take place."
  2449. :group 'org-todo
  2450. :type 'boolean)
  2451. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2452. "Non-nil means switching TODO states with S-cursor counts as state change.
  2453. This is the default behavior. However, setting this to nil allows a
  2454. convenient way to select a TODO state and bypass any logging associated
  2455. with that."
  2456. :group 'org-todo
  2457. :type 'boolean)
  2458. (defcustom org-todo-state-tags-triggers nil
  2459. "Tag changes that should be triggered by TODO state changes.
  2460. This is a list. Each entry is
  2461. (state-change (tag . flag) .......)
  2462. State-change can be a string with a state, and empty string to indicate the
  2463. state that has no TODO keyword, or it can be one of the symbols `todo'
  2464. or `done', meaning any not-done or done state, respectively."
  2465. :group 'org-todo
  2466. :group 'org-tags
  2467. :type '(repeat
  2468. (cons (choice :tag "When changing to"
  2469. (const :tag "Not-done state" todo)
  2470. (const :tag "Done state" done)
  2471. (string :tag "State"))
  2472. (repeat
  2473. (cons :tag "Tag action"
  2474. (string :tag "Tag")
  2475. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2476. (defcustom org-log-done nil
  2477. "Information to record when a task moves to the DONE state.
  2478. Possible values are:
  2479. nil Don't add anything, just change the keyword
  2480. time Add a time stamp to the task
  2481. note Prompt for a note and add it with template `org-log-note-headings'
  2482. This option can also be set with on a per-file-basis with
  2483. #+STARTUP: nologdone
  2484. #+STARTUP: logdone
  2485. #+STARTUP: lognotedone
  2486. You can have local logging settings for a subtree by setting the LOGGING
  2487. property to one or more of these keywords."
  2488. :group 'org-todo
  2489. :group 'org-progress
  2490. :type '(choice
  2491. (const :tag "No logging" nil)
  2492. (const :tag "Record CLOSED timestamp" time)
  2493. (const :tag "Record CLOSED timestamp with note." note)))
  2494. ;; Normalize old uses of org-log-done.
  2495. (cond
  2496. ((eq org-log-done t) (setq org-log-done 'time))
  2497. ((and (listp org-log-done) (memq 'done org-log-done))
  2498. (setq org-log-done 'note)))
  2499. (defcustom org-log-reschedule nil
  2500. "Information to record when the scheduling date of a tasks is modified.
  2501. Possible values are:
  2502. nil Don't add anything, just change the date
  2503. time Add a time stamp to the task
  2504. note Prompt for a note and add it with template `org-log-note-headings'
  2505. This option can also be set with on a per-file-basis with
  2506. #+STARTUP: nologreschedule
  2507. #+STARTUP: logreschedule
  2508. #+STARTUP: lognotereschedule"
  2509. :group 'org-todo
  2510. :group 'org-progress
  2511. :type '(choice
  2512. (const :tag "No logging" nil)
  2513. (const :tag "Record timestamp" time)
  2514. (const :tag "Record timestamp with note." note)))
  2515. (defcustom org-log-redeadline nil
  2516. "Information to record when the deadline date of a tasks is modified.
  2517. Possible values are:
  2518. nil Don't add anything, just change the date
  2519. time Add a time stamp to the task
  2520. note Prompt for a note and add it with template `org-log-note-headings'
  2521. This option can also be set with on a per-file-basis with
  2522. #+STARTUP: nologredeadline
  2523. #+STARTUP: logredeadline
  2524. #+STARTUP: lognoteredeadline
  2525. You can have local logging settings for a subtree by setting the LOGGING
  2526. property to one or more of these keywords."
  2527. :group 'org-todo
  2528. :group 'org-progress
  2529. :type '(choice
  2530. (const :tag "No logging" nil)
  2531. (const :tag "Record timestamp" time)
  2532. (const :tag "Record timestamp with note." note)))
  2533. (defcustom org-log-note-clock-out nil
  2534. "Non-nil means record a note when clocking out of an item.
  2535. This can also be configured on a per-file basis by adding one of
  2536. the following lines anywhere in the buffer:
  2537. #+STARTUP: lognoteclock-out
  2538. #+STARTUP: nolognoteclock-out"
  2539. :group 'org-todo
  2540. :group 'org-progress
  2541. :type 'boolean)
  2542. (defcustom org-log-done-with-time t
  2543. "Non-nil means the CLOSED time stamp will contain date and time.
  2544. When nil, only the date will be recorded."
  2545. :group 'org-progress
  2546. :type 'boolean)
  2547. (defcustom org-log-note-headings
  2548. '((done . "CLOSING NOTE %t")
  2549. (state . "State %-12s from %-12S %t")
  2550. (note . "Note taken on %t")
  2551. (reschedule . "Rescheduled from %S on %t")
  2552. (delschedule . "Not scheduled, was %S on %t")
  2553. (redeadline . "New deadline from %S on %t")
  2554. (deldeadline . "Removed deadline, was %S on %t")
  2555. (refile . "Refiled on %t")
  2556. (clock-out . ""))
  2557. "Headings for notes added to entries.
  2558. The value is an alist, with the car being a symbol indicating the
  2559. note context, and the cdr is the heading to be used. The heading
  2560. may also be the empty string. The following placeholders can be
  2561. used:
  2562. %t a time stamp.
  2563. %T an active time stamp instead the default inactive one
  2564. %d a short-format time stamp.
  2565. %D an active short-format time stamp.
  2566. %s the new TODO state or time stamp (inactive), in double quotes.
  2567. %S the old TODO state or time stamp (inactive), in double quotes.
  2568. %u the user name.
  2569. %U full user name.
  2570. In fact, it is not a good idea to change the `state' entry,
  2571. because Agenda Log mode depends on the format of these entries."
  2572. :group 'org-todo
  2573. :group 'org-progress
  2574. :type '(list :greedy t
  2575. (cons (const :tag "Heading when closing an item" done) string)
  2576. (cons (const :tag
  2577. "Heading when changing todo state (todo sequence only)"
  2578. state) string)
  2579. (cons (const :tag "Heading when just taking a note" note) string)
  2580. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2581. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2582. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2583. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2584. (cons (const :tag "Heading when refiling" refile) string)
  2585. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2586. (unless (assq 'note org-log-note-headings)
  2587. (push '(note . "%t") org-log-note-headings))
  2588. (defcustom org-log-into-drawer nil
  2589. "Non-nil means insert state change notes and time stamps into a drawer.
  2590. When nil, state changes notes will be inserted after the headline and
  2591. any scheduling and clock lines, but not inside a drawer.
  2592. The value of this variable should be the name of the drawer to use.
  2593. LOGBOOK is proposed as the default drawer for this purpose, you can
  2594. also set this to a string to define the drawer of your choice.
  2595. A value of t is also allowed, representing \"LOGBOOK\".
  2596. A value of t or nil can also be set with on a per-file-basis with
  2597. #+STARTUP: logdrawer
  2598. #+STARTUP: nologdrawer
  2599. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2600. will be ignored.
  2601. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2602. a subtree.
  2603. Do not check directly this variable in a Lisp program. Call
  2604. function `org-log-into-drawer' instead."
  2605. :group 'org-todo
  2606. :group 'org-progress
  2607. :type '(choice
  2608. (const :tag "Not into a drawer" nil)
  2609. (const :tag "LOGBOOK" t)
  2610. (string :tag "Other")))
  2611. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2612. (defun org-log-into-drawer ()
  2613. "Name of the log drawer, as a string, or nil.
  2614. This is the value of `org-log-into-drawer'. However, if the
  2615. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2616. be used instead of the default value."
  2617. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2618. (cond ((equal p "nil") nil)
  2619. ((equal p "t") "LOGBOOK")
  2620. ((stringp p) p)
  2621. (p "LOGBOOK")
  2622. ((stringp org-log-into-drawer) org-log-into-drawer)
  2623. (org-log-into-drawer "LOGBOOK"))))
  2624. (defcustom org-log-state-notes-insert-after-drawers nil
  2625. "Non-nil means insert state change notes after any drawers in entry.
  2626. Only the drawers that *immediately* follow the headline and the
  2627. deadline/scheduled line are skipped.
  2628. When nil, insert notes right after the heading and perhaps the line
  2629. with deadline/scheduling if present.
  2630. This variable will have no effect if `org-log-into-drawer' is
  2631. set."
  2632. :group 'org-todo
  2633. :group 'org-progress
  2634. :type 'boolean)
  2635. (defcustom org-log-states-order-reversed t
  2636. "Non-nil means the latest state note will be directly after heading.
  2637. When nil, the state change notes will be ordered according to time.
  2638. This option can also be set with on a per-file-basis with
  2639. #+STARTUP: logstatesreversed
  2640. #+STARTUP: nologstatesreversed"
  2641. :group 'org-todo
  2642. :group 'org-progress
  2643. :type 'boolean)
  2644. (defcustom org-todo-repeat-to-state nil
  2645. "The TODO state to which a repeater should return the repeating task.
  2646. By default this is the first task in a TODO sequence, or the previous state
  2647. in a TODO_TYP set. But you can specify another task here.
  2648. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2649. :group 'org-todo
  2650. :version "24.1"
  2651. :type '(choice (const :tag "Head of sequence" nil)
  2652. (string :tag "Specific state")))
  2653. (defcustom org-log-repeat 'time
  2654. "Non-nil means record moving through the DONE state when triggering repeat.
  2655. An auto-repeating task is immediately switched back to TODO when
  2656. marked DONE. If you are not logging state changes (by adding \"@\"
  2657. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2658. record a closing note, there will be no record of the task moving
  2659. through DONE. This variable forces taking a note anyway.
  2660. nil Don't force a record
  2661. time Record a time stamp
  2662. note Prompt for a note and add it with template `org-log-note-headings'
  2663. This option can also be set with on a per-file-basis with
  2664. #+STARTUP: nologrepeat
  2665. #+STARTUP: logrepeat
  2666. #+STARTUP: lognoterepeat
  2667. You can have local logging settings for a subtree by setting the LOGGING
  2668. property to one or more of these keywords."
  2669. :group 'org-todo
  2670. :group 'org-progress
  2671. :type '(choice
  2672. (const :tag "Don't force a record" nil)
  2673. (const :tag "Force recording the DONE state" time)
  2674. (const :tag "Force recording a note with the DONE state" note)))
  2675. (defgroup org-priorities nil
  2676. "Priorities in Org mode."
  2677. :tag "Org Priorities"
  2678. :group 'org-todo)
  2679. (defcustom org-enable-priority-commands t
  2680. "Non-nil means priority commands are active.
  2681. When nil, these commands will be disabled, so that you never accidentally
  2682. set a priority."
  2683. :group 'org-priorities
  2684. :type 'boolean)
  2685. (defcustom org-highest-priority ?A
  2686. "The highest priority of TODO items. A character like ?A, ?B etc.
  2687. Must have a smaller ASCII number than `org-lowest-priority'."
  2688. :group 'org-priorities
  2689. :type 'character)
  2690. (defcustom org-lowest-priority ?C
  2691. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2692. Must have a larger ASCII number than `org-highest-priority'."
  2693. :group 'org-priorities
  2694. :type 'character)
  2695. (defcustom org-default-priority ?B
  2696. "The default priority of TODO items.
  2697. This is the priority an item gets if no explicit priority is given.
  2698. When starting to cycle on an empty priority the first step in the cycle
  2699. depends on `org-priority-start-cycle-with-default'. The resulting first
  2700. step priority must not exceed the range from `org-highest-priority' to
  2701. `org-lowest-priority' which means that `org-default-priority' has to be
  2702. in this range exclusive or inclusive the range boundaries. Else the
  2703. first step refuses to set the default and the second will fall back
  2704. to (depending on the command used) the highest or lowest priority."
  2705. :group 'org-priorities
  2706. :type 'character)
  2707. (defcustom org-priority-start-cycle-with-default t
  2708. "Non-nil means start with default priority when starting to cycle.
  2709. When this is nil, the first step in the cycle will be (depending on the
  2710. command used) one higher or lower than the default priority.
  2711. See also `org-default-priority'."
  2712. :group 'org-priorities
  2713. :type 'boolean)
  2714. (defcustom org-get-priority-function nil
  2715. "Function to extract the priority from a string.
  2716. The string is normally the headline. If this is nil Org computes the
  2717. priority from the priority cookie like [#A] in the headline. It returns
  2718. an integer, increasing by 1000 for each priority level.
  2719. The user can set a different function here, which should take a string
  2720. as an argument and return the numeric priority."
  2721. :group 'org-priorities
  2722. :version "24.1"
  2723. :type '(choice
  2724. (const nil)
  2725. (function)))
  2726. (defgroup org-time nil
  2727. "Options concerning time stamps and deadlines in Org mode."
  2728. :tag "Org Time"
  2729. :group 'org)
  2730. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2731. "Number of minutes to round time stamps to.
  2732. \\<org-mode-map>\
  2733. These are two values, the first applies when first creating a time stamp.
  2734. The second applies when changing it with the commands `S-up' and `S-down'.
  2735. When changing the time stamp, this means that it will change in steps
  2736. of N minutes, as given by the second value.
  2737. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2738. numbers should be factors of 60, so for example 5, 10, 15.
  2739. When this is larger than 1, you can still force an exact time stamp by using
  2740. a double prefix argument to a time stamp command like \
  2741. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2742. and by using a prefix arg to `S-up/down' to specify the exact number
  2743. of minutes to shift."
  2744. :group 'org-time
  2745. :get (lambda (var) ; Make sure both elements are there
  2746. (if (integerp (default-value var))
  2747. (list (default-value var) 5)
  2748. (default-value var)))
  2749. :type '(list
  2750. (integer :tag "when inserting times")
  2751. (integer :tag "when modifying times")))
  2752. ;; Normalize old customizations of this variable.
  2753. (when (integerp org-time-stamp-rounding-minutes)
  2754. (setq org-time-stamp-rounding-minutes
  2755. (list org-time-stamp-rounding-minutes
  2756. org-time-stamp-rounding-minutes)))
  2757. (defcustom org-display-custom-times nil
  2758. "Non-nil means overlay custom formats over all time stamps.
  2759. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2760. To turn this on on a per-file basis, insert anywhere in the file:
  2761. #+STARTUP: customtime"
  2762. :group 'org-time
  2763. :set 'set-default
  2764. :type 'sexp)
  2765. (make-variable-buffer-local 'org-display-custom-times)
  2766. (defcustom org-time-stamp-custom-formats
  2767. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2768. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2769. These are overlaid over the default ISO format if the variable
  2770. `org-display-custom-times' is set. Time like %H:%M should be at the
  2771. end of the second format. The custom formats are also honored by export
  2772. commands, if custom time display is turned on at the time of export."
  2773. :group 'org-time
  2774. :type 'sexp)
  2775. (defun org-time-stamp-format (&optional long inactive)
  2776. "Get the right format for a time string."
  2777. (let ((f (if long (cdr org-time-stamp-formats)
  2778. (car org-time-stamp-formats))))
  2779. (if inactive
  2780. (concat "[" (substring f 1 -1) "]")
  2781. f)))
  2782. (defcustom org-deadline-warning-days 14
  2783. "Number of days before expiration during which a deadline becomes active.
  2784. This variable governs the display in sparse trees and in the agenda.
  2785. When 0 or negative, it means use this number (the absolute value of it)
  2786. even if a deadline has a different individual lead time specified.
  2787. Custom commands can set this variable in the options section."
  2788. :group 'org-time
  2789. :group 'org-agenda-daily/weekly
  2790. :type 'integer)
  2791. (defcustom org-scheduled-delay-days 0
  2792. "Number of days before a scheduled item becomes active.
  2793. This variable governs the display in sparse trees and in the agenda.
  2794. The default value (i.e. 0) means: don't delay scheduled item.
  2795. When negative, it means use this number (the absolute value of it)
  2796. even if a scheduled item has a different individual delay time
  2797. specified.
  2798. Custom commands can set this variable in the options section."
  2799. :group 'org-time
  2800. :group 'org-agenda-daily/weekly
  2801. :version "24.4"
  2802. :package-version '(Org . "8.0")
  2803. :type 'integer)
  2804. (defcustom org-read-date-prefer-future t
  2805. "Non-nil means assume future for incomplete date input from user.
  2806. This affects the following situations:
  2807. 1. The user gives a month but not a year.
  2808. For example, if it is April and you enter \"feb 2\", this will be read
  2809. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2810. 2. The user gives a day, but no month.
  2811. For example, if today is the 15th, and you enter \"3\", Org will read
  2812. this as the third of *next* month. However, if you enter \"17\",
  2813. it will be considered as *this* month.
  2814. If you set this variable to the symbol `time', then also the following
  2815. will work:
  2816. 3. If the user gives a time.
  2817. If the time is before now, it will be interpreted as tomorrow.
  2818. Currently none of this works for ISO week specifications.
  2819. When this option is nil, the current day, month and year will always be
  2820. used as defaults.
  2821. See also `org-agenda-jump-prefer-future'."
  2822. :group 'org-time
  2823. :type '(choice
  2824. (const :tag "Never" nil)
  2825. (const :tag "Check month and day" t)
  2826. (const :tag "Check month, day, and time" time)))
  2827. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2828. "Should the agenda jump command prefer the future for incomplete dates?
  2829. The default is to do the same as configured in `org-read-date-prefer-future'.
  2830. But you can also set a deviating value here.
  2831. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2832. :group 'org-agenda
  2833. :group 'org-time
  2834. :version "24.1"
  2835. :type '(choice
  2836. (const :tag "Use org-read-date-prefer-future"
  2837. org-read-date-prefer-future)
  2838. (const :tag "Never" nil)
  2839. (const :tag "Always" t)))
  2840. (defcustom org-read-date-force-compatible-dates t
  2841. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2842. Depending on the system Emacs is running on, certain dates cannot
  2843. be represented with the type used internally to represent time.
  2844. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2845. correctly. Some systems allow for earlier dates, some for later,
  2846. some for both. One way to find out it to insert any date into an
  2847. Org buffer, putting the cursor on the year and hitting S-up and
  2848. S-down to test the range.
  2849. When this variable is set to t, the date/time prompt will not let
  2850. you specify dates outside the 1970-2037 range, so it is certain that
  2851. these dates will work in whatever version of Emacs you are
  2852. running, and also that you can move a file from one Emacs implementation
  2853. to another. WHenever Org is forcing the year for you, it will display
  2854. a message and beep.
  2855. When this variable is nil, Org will check if the date is
  2856. representable in the specific Emacs implementation you are using.
  2857. If not, it will force a year, usually the current year, and beep
  2858. to remind you. Currently this setting is not recommended because
  2859. the likelihood that you will open your Org files in an Emacs that
  2860. has limited date range is not negligible.
  2861. A workaround for this problem is to use diary sexp dates for time
  2862. stamps outside of this range."
  2863. :group 'org-time
  2864. :version "24.1"
  2865. :type 'boolean)
  2866. (defcustom org-read-date-display-live t
  2867. "Non-nil means display current interpretation of date prompt live.
  2868. This display will be in an overlay, in the minibuffer."
  2869. :group 'org-time
  2870. :type 'boolean)
  2871. (defcustom org-read-date-popup-calendar t
  2872. "Non-nil means pop up a calendar when prompting for a date.
  2873. In the calendar, the date can be selected with mouse-1. However, the
  2874. minibuffer will also be active, and you can simply enter the date as well.
  2875. When nil, only the minibuffer will be available."
  2876. :group 'org-time
  2877. :type 'boolean)
  2878. (defvaralias 'org-popup-calendar-for-date-prompt
  2879. 'org-read-date-popup-calendar)
  2880. (defcustom org-extend-today-until 0
  2881. "The hour when your day really ends. Must be an integer.
  2882. This has influence for the following applications:
  2883. - When switching the agenda to \"today\". It it is still earlier than
  2884. the time given here, the day recognized as TODAY is actually yesterday.
  2885. - When a date is read from the user and it is still before the time given
  2886. here, the current date and time will be assumed to be yesterday, 23:59.
  2887. Also, timestamps inserted in capture templates follow this rule.
  2888. IMPORTANT: This is a feature whose implementation is and likely will
  2889. remain incomplete. Really, it is only here because past midnight seems to
  2890. be the favorite working time of John Wiegley :-)"
  2891. :group 'org-time
  2892. :type 'integer)
  2893. (defcustom org-use-effective-time nil
  2894. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2895. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2896. \"effective time\" of any timestamps between midnight and 8am will be
  2897. 23:59 of the previous day."
  2898. :group 'org-time
  2899. :version "24.1"
  2900. :type 'boolean)
  2901. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2902. "When non-nil, use the last clock out time for `org-todo'.
  2903. Note that this option has precedence over the combined use of
  2904. `org-use-effective-time' and `org-extend-today-until'."
  2905. :group 'org-time
  2906. :version "24.4"
  2907. :package-version '(Org . "8.0")
  2908. :type 'boolean)
  2909. (defcustom org-edit-timestamp-down-means-later nil
  2910. "Non-nil means S-down will increase the time in a time stamp.
  2911. When nil, S-up will increase."
  2912. :group 'org-time
  2913. :type 'boolean)
  2914. (defcustom org-calendar-follow-timestamp-change t
  2915. "Non-nil means make the calendar window follow timestamp changes.
  2916. When a timestamp is modified and the calendar window is visible, it will be
  2917. moved to the new date."
  2918. :group 'org-time
  2919. :type 'boolean)
  2920. (defgroup org-tags nil
  2921. "Options concerning tags in Org mode."
  2922. :tag "Org Tags"
  2923. :group 'org)
  2924. (defcustom org-tag-alist nil
  2925. "Default tags available in Org files.
  2926. The value of this variable is an alist. Associations either:
  2927. (TAG)
  2928. (TAG . SELECT)
  2929. (SPECIAL)
  2930. where TAG is a tag as a string, SELECT is character, used to
  2931. select that tag through the fast tag selection interface, and
  2932. SPECIAL is one of the following keywords: `:startgroup',
  2933. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2934. `:newline'. These keywords are used to define a hierarchy of
  2935. tags. See manual for details.
  2936. When this variable is nil, Org mode bases tag input on what is
  2937. already in the buffer. The value can be overridden locally by
  2938. using a TAGS keyword, e.g.,
  2939. #+TAGS: tag1 tag2
  2940. See also `org-tag-persistent-alist' to sidestep this behavior."
  2941. :group 'org-tags
  2942. :type '(repeat
  2943. (choice
  2944. (cons :tag "Tag with key"
  2945. (string :tag "Tag name")
  2946. (character :tag "Access char"))
  2947. (list :tag "Tag" (string :tag "Tag name"))
  2948. (const :tag "Start radio group" (:startgroup))
  2949. (const :tag "Start tag group, non distinct" (:startgrouptag))
  2950. (const :tag "Group tags delimiter" (:grouptags))
  2951. (const :tag "End radio group" (:endgroup))
  2952. (const :tag "End tag group, non distinct" (:endgrouptag))
  2953. (const :tag "New line" (:newline)))))
  2954. (defcustom org-tag-persistent-alist nil
  2955. "Tags always available in Org files.
  2956. The value of this variable is an alist. Associations either:
  2957. (TAG)
  2958. (TAG . SELECT)
  2959. (SPECIAL)
  2960. where TAG is a tag as a string, SELECT is a character, used to
  2961. select that tag through the fast tag selection interface, and
  2962. SPECIAL is one of the following keywords: `:startgroup',
  2963. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2964. `:newline'. These keywords are used to define a hierarchy of
  2965. tags. See manual for details.
  2966. Unlike to `org-tag-alist', tags defined in this variable do not
  2967. depend on a local TAGS keyword. Instead, to disable these tags
  2968. on a per-file basis, insert anywhere in the file:
  2969. #+STARTUP: noptag"
  2970. :group 'org-tags
  2971. :type '(repeat
  2972. (choice
  2973. (cons :tag "Tag with key"
  2974. (string :tag "Tag name")
  2975. (character :tag "Access char"))
  2976. (list :tag "Tag" (string :tag "Tag name"))
  2977. (const :tag "Start radio group" (:startgroup))
  2978. (const :tag "Start tag group, non distinct" (:startgrouptag))
  2979. (const :tag "Group tags delimiter" (:grouptags))
  2980. (const :tag "End radio group" (:endgroup))
  2981. (const :tag "End tag group, non distinct" (:endgrouptag))
  2982. (const :tag "New line" (:newline)))))
  2983. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2984. "If non-nil, always offer completion for all tags of all agenda files.
  2985. Instead of customizing this variable directly, you might want to
  2986. set it locally for capture buffers, because there no list of
  2987. tags in that file can be created dynamically (there are none).
  2988. (add-hook \\='org-capture-mode-hook
  2989. (lambda ()
  2990. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  2991. :group 'org-tags
  2992. :version "24.1"
  2993. :type 'boolean)
  2994. (defvar org-file-tags nil
  2995. "List of tags that can be inherited by all entries in the file.
  2996. The tags will be inherited if the variable `org-use-tag-inheritance'
  2997. says they should be.
  2998. This variable is populated from #+FILETAGS lines.")
  2999. (defcustom org-use-fast-tag-selection 'auto
  3000. "Non-nil means use fast tag selection scheme.
  3001. This is a special interface to select and deselect tags with single keys.
  3002. When nil, fast selection is never used.
  3003. When the symbol `auto', fast selection is used if and only if selection
  3004. characters for tags have been configured, either through the variable
  3005. `org-tag-alist' or through a #+TAGS line in the buffer.
  3006. When t, fast selection is always used and selection keys are assigned
  3007. automatically if necessary."
  3008. :group 'org-tags
  3009. :type '(choice
  3010. (const :tag "Always" t)
  3011. (const :tag "Never" nil)
  3012. (const :tag "When selection characters are configured" auto)))
  3013. (defcustom org-fast-tag-selection-single-key nil
  3014. "Non-nil means fast tag selection exits after first change.
  3015. When nil, you have to press RET to exit it.
  3016. During fast tag selection, you can toggle this flag with `C-c'.
  3017. This variable can also have the value `expert'. In this case, the window
  3018. displaying the tags menu is not even shown, until you press C-c again."
  3019. :group 'org-tags
  3020. :type '(choice
  3021. (const :tag "No" nil)
  3022. (const :tag "Yes" t)
  3023. (const :tag "Expert" expert)))
  3024. (defvar org-fast-tag-selection-include-todo nil
  3025. "Non-nil means fast tags selection interface will also offer TODO states.
  3026. This is an undocumented feature, you should not rely on it.")
  3027. (defcustom org-tags-column -77
  3028. "The column to which tags should be indented in a headline.
  3029. If this number is positive, it specifies the column. If it is negative,
  3030. it means that the tags should be flushright to that column. For example,
  3031. -80 works well for a normal 80 character screen.
  3032. When 0, place tags directly after headline text, with only one space in
  3033. between."
  3034. :group 'org-tags
  3035. :type 'integer)
  3036. (defcustom org-auto-align-tags t
  3037. "Non-nil keeps tags aligned when modifying headlines.
  3038. Some operations (i.e. demoting) change the length of a headline and
  3039. therefore shift the tags around. With this option turned on, after
  3040. each such operation the tags are again aligned to `org-tags-column'."
  3041. :group 'org-tags
  3042. :type 'boolean)
  3043. (defcustom org-use-tag-inheritance t
  3044. "Non-nil means tags in levels apply also for sublevels.
  3045. When nil, only the tags directly given in a specific line apply there.
  3046. This may also be a list of tags that should be inherited, or a regexp that
  3047. matches tags that should be inherited. Additional control is possible
  3048. with the variable `org-tags-exclude-from-inheritance' which gives an
  3049. explicit list of tags to be excluded from inheritance, even if the value of
  3050. `org-use-tag-inheritance' would select it for inheritance.
  3051. If this option is t, a match early-on in a tree can lead to a large
  3052. number of matches in the subtree when constructing the agenda or creating
  3053. a sparse tree. If you only want to see the first match in a tree during
  3054. a search, check out the variable `org-tags-match-list-sublevels'."
  3055. :group 'org-tags
  3056. :type '(choice
  3057. (const :tag "Not" nil)
  3058. (const :tag "Always" t)
  3059. (repeat :tag "Specific tags" (string :tag "Tag"))
  3060. (regexp :tag "Tags matched by regexp")))
  3061. (defcustom org-tags-exclude-from-inheritance nil
  3062. "List of tags that should never be inherited.
  3063. This is a way to exclude a few tags from inheritance. For way to do
  3064. the opposite, to actively allow inheritance for selected tags,
  3065. see the variable `org-use-tag-inheritance'."
  3066. :group 'org-tags
  3067. :type '(repeat (string :tag "Tag")))
  3068. (defun org-tag-inherit-p (tag)
  3069. "Check if TAG is one that should be inherited."
  3070. (cond
  3071. ((member tag org-tags-exclude-from-inheritance) nil)
  3072. ((eq org-use-tag-inheritance t) t)
  3073. ((not org-use-tag-inheritance) nil)
  3074. ((stringp org-use-tag-inheritance)
  3075. (string-match org-use-tag-inheritance tag))
  3076. ((listp org-use-tag-inheritance)
  3077. (member tag org-use-tag-inheritance))
  3078. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3079. (defcustom org-tags-match-list-sublevels t
  3080. "Non-nil means list also sublevels of headlines matching a search.
  3081. This variable applies to tags/property searches, and also to stuck
  3082. projects because this search is based on a tags match as well.
  3083. When set to the symbol `indented', sublevels are indented with
  3084. leading dots.
  3085. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3086. the sublevels of a headline matching a tag search often also match
  3087. the same search. Listing all of them can create very long lists.
  3088. Setting this variable to nil causes subtrees of a match to be skipped.
  3089. This variable is semi-obsolete and probably should always be true. It
  3090. is better to limit inheritance to certain tags using the variables
  3091. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3092. :group 'org-tags
  3093. :type '(choice
  3094. (const :tag "No, don't list them" nil)
  3095. (const :tag "Yes, do list them" t)
  3096. (const :tag "List them, indented with leading dots" indented)))
  3097. (defcustom org-tags-sort-function nil
  3098. "When set, tags are sorted using this function as a comparator."
  3099. :group 'org-tags
  3100. :type '(choice
  3101. (const :tag "No sorting" nil)
  3102. (const :tag "Alphabetical" string<)
  3103. (const :tag "Reverse alphabetical" string>)
  3104. (function :tag "Custom function" nil)))
  3105. (defvar org-tags-history nil
  3106. "History of minibuffer reads for tags.")
  3107. (defvar org-last-tags-completion-table nil
  3108. "The last used completion table for tags.")
  3109. (defvar org-after-tags-change-hook nil
  3110. "Hook that is run after the tags in a line have changed.")
  3111. (defgroup org-properties nil
  3112. "Options concerning properties in Org mode."
  3113. :tag "Org Properties"
  3114. :group 'org)
  3115. (defcustom org-property-format "%-10s %s"
  3116. "How property key/value pairs should be formatted by `indent-line'.
  3117. When `indent-line' hits a property definition, it will format the line
  3118. according to this format, mainly to make sure that the values are
  3119. lined-up with respect to each other."
  3120. :group 'org-properties
  3121. :type 'string)
  3122. (defcustom org-properties-postprocess-alist nil
  3123. "Alist of properties and functions to adjust inserted values.
  3124. Elements of this alist must be of the form
  3125. ([string] [function])
  3126. where [string] must be a property name and [function] must be a
  3127. lambda expression: this lambda expression must take one argument,
  3128. the value to adjust, and return the new value as a string.
  3129. For example, this element will allow the property \"Remaining\"
  3130. to be updated wrt the relation between the \"Effort\" property
  3131. and the clock summary:
  3132. ((\"Remaining\" (lambda(value)
  3133. (let ((clocksum (org-clock-sum-current-item))
  3134. (effort (org-duration-to-minutes
  3135. (org-entry-get (point) \"Effort\"))))
  3136. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3137. :group 'org-properties
  3138. :version "24.1"
  3139. :type '(alist :key-type (string :tag "Property")
  3140. :value-type (function :tag "Function")))
  3141. (defcustom org-use-property-inheritance nil
  3142. "Non-nil means properties apply also for sublevels.
  3143. This setting is chiefly used during property searches. Turning it on can
  3144. cause significant overhead when doing a search, which is why it is not
  3145. on by default.
  3146. When nil, only the properties directly given in the current entry count.
  3147. When t, every property is inherited. The value may also be a list of
  3148. properties that should have inheritance, or a regular expression matching
  3149. properties that should be inherited.
  3150. However, note that some special properties use inheritance under special
  3151. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3152. and the properties ending in \"_ALL\" when they are used as descriptor
  3153. for valid values of a property.
  3154. Note for programmers:
  3155. When querying an entry with `org-entry-get', you can control if inheritance
  3156. should be used. By default, `org-entry-get' looks only at the local
  3157. properties. You can request inheritance by setting the inherit argument
  3158. to t (to force inheritance) or to `selective' (to respect the setting
  3159. in this variable)."
  3160. :group 'org-properties
  3161. :type '(choice
  3162. (const :tag "Not" nil)
  3163. (const :tag "Always" t)
  3164. (repeat :tag "Specific properties" (string :tag "Property"))
  3165. (regexp :tag "Properties matched by regexp")))
  3166. (defun org-property-inherit-p (property)
  3167. "Return a non-nil value if PROPERTY should be inherited."
  3168. (cond
  3169. ((eq org-use-property-inheritance t) t)
  3170. ((not org-use-property-inheritance) nil)
  3171. ((stringp org-use-property-inheritance)
  3172. (string-match org-use-property-inheritance property))
  3173. ((listp org-use-property-inheritance)
  3174. (member-ignore-case property org-use-property-inheritance))
  3175. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3176. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3177. "The default column format, if no other format has been defined.
  3178. This variable can be set on the per-file basis by inserting a line
  3179. #+COLUMNS: %25ITEM ....."
  3180. :group 'org-properties
  3181. :type 'string)
  3182. (defcustom org-columns-ellipses ".."
  3183. "The ellipses to be used when a field in column view is truncated.
  3184. When this is the empty string, as many characters as possible are shown,
  3185. but then there will be no visual indication that the field has been truncated.
  3186. When this is a string of length N, the last N characters of a truncated
  3187. field are replaced by this string. If the column is narrower than the
  3188. ellipses string, only part of the ellipses string will be shown."
  3189. :group 'org-properties
  3190. :type 'string)
  3191. (defconst org-global-properties-fixed
  3192. '(("VISIBILITY_ALL" . "folded children content all")
  3193. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3194. "List of property/value pairs that can be inherited by any entry.
  3195. These are fixed values, for the preset properties. The user variable
  3196. that can be used to add to this list is `org-global-properties'.
  3197. The entries in this list are cons cells where the car is a property
  3198. name and cdr is a string with the value. If the value represents
  3199. multiple items like an \"_ALL\" property, separate the items by
  3200. spaces.")
  3201. (defcustom org-global-properties nil
  3202. "List of property/value pairs that can be inherited by any entry.
  3203. This list will be combined with the constant `org-global-properties-fixed'.
  3204. The entries in this list are cons cells where the car is a property
  3205. name and cdr is a string with the value.
  3206. You can set buffer-local values for the same purpose in the variable
  3207. `org-file-properties' this by adding lines like
  3208. #+PROPERTY: NAME VALUE"
  3209. :group 'org-properties
  3210. :type '(repeat
  3211. (cons (string :tag "Property")
  3212. (string :tag "Value"))))
  3213. (defvar-local org-file-properties nil
  3214. "List of property/value pairs that can be inherited by any entry.
  3215. Valid for the current buffer.
  3216. This variable is populated from #+PROPERTY lines.")
  3217. (defgroup org-agenda nil
  3218. "Options concerning agenda views in Org mode."
  3219. :tag "Org Agenda"
  3220. :group 'org)
  3221. (defvar-local org-category nil
  3222. "Variable used by org files to set a category for agenda display.
  3223. Such files should use a file variable to set it, for example
  3224. # -*- mode: org; org-category: \"ELisp\"
  3225. or contain a special line
  3226. #+CATEGORY: ELisp
  3227. If the file does not specify a category, then file's base name
  3228. is used instead.")
  3229. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3230. (defcustom org-agenda-files nil
  3231. "The files to be used for agenda display.
  3232. If an entry is a directory, all files in that directory that are matched
  3233. by `org-agenda-file-regexp' will be part of the file list.
  3234. If the value of the variable is not a list but a single file name, then
  3235. the list of agenda files is actually stored and maintained in that file,
  3236. one agenda file per line. In this file paths can be given relative to
  3237. `org-directory'. Tilde expansion and environment variable substitution
  3238. are also made.
  3239. Entries may be added to this list with `\\[org-agenda-file-to-front]'
  3240. and removed with `\\[org-remove-file]'."
  3241. :group 'org-agenda
  3242. :type '(choice
  3243. (repeat :tag "List of files and directories" file)
  3244. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3245. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3246. "Regular expression to match files for `org-agenda-files'.
  3247. If any element in the list in that variable contains a directory instead
  3248. of a normal file, all files in that directory that are matched by this
  3249. regular expression will be included."
  3250. :group 'org-agenda
  3251. :type 'regexp)
  3252. (defcustom org-agenda-text-search-extra-files nil
  3253. "List of extra files to be searched by text search commands.
  3254. These files will be searched in addition to the agenda files by the
  3255. commands `org-search-view' (`\\[org-agenda] s') \
  3256. and `org-occur-in-agenda-files'.
  3257. Note that these files will only be searched for text search commands,
  3258. not for the other agenda views like todo lists, tag searches or the weekly
  3259. agenda. This variable is intended to list notes and possibly archive files
  3260. that should also be searched by these two commands.
  3261. In fact, if the first element in the list is the symbol `agenda-archives',
  3262. then all archive files of all agenda files will be added to the search
  3263. scope."
  3264. :group 'org-agenda
  3265. :type '(set :greedy t
  3266. (const :tag "Agenda Archives" agenda-archives)
  3267. (repeat :inline t (file))))
  3268. (defvaralias 'org-agenda-multi-occur-extra-files
  3269. 'org-agenda-text-search-extra-files)
  3270. (defcustom org-agenda-skip-unavailable-files nil
  3271. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3272. A nil value means to remove them, after a query, from the list."
  3273. :group 'org-agenda
  3274. :type 'boolean)
  3275. (defcustom org-calendar-to-agenda-key [?c]
  3276. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3277. The command `org-calendar-goto-agenda' will be bound to this key. The
  3278. default is the character `c' because then `c' can be used to switch back and
  3279. forth between agenda and calendar."
  3280. :group 'org-agenda
  3281. :type 'sexp)
  3282. (defcustom org-calendar-insert-diary-entry-key [?i]
  3283. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3284. This option is irrelevant until `org-agenda-diary-file' has been configured
  3285. to point to an Org file. When that is the case, the command
  3286. `org-agenda-diary-entry' will be bound to the key given here, by default
  3287. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3288. if you want to continue doing this, you need to change this to a different
  3289. key."
  3290. :group 'org-agenda
  3291. :type 'sexp)
  3292. (defcustom org-agenda-diary-file 'diary-file
  3293. "File to which to add new entries with the `i' key in agenda and calendar.
  3294. When this is the symbol `diary-file', the functionality in the Emacs
  3295. calendar will be used to add entries to the `diary-file'. But when this
  3296. points to a file, `org-agenda-diary-entry' will be used instead."
  3297. :group 'org-agenda
  3298. :type '(choice
  3299. (const :tag "The standard Emacs diary file" diary-file)
  3300. (file :tag "Special Org file diary entries")))
  3301. (eval-after-load "calendar"
  3302. '(progn
  3303. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3304. 'org-calendar-goto-agenda)
  3305. (add-hook 'calendar-mode-hook
  3306. (lambda ()
  3307. (unless (eq org-agenda-diary-file 'diary-file)
  3308. (define-key calendar-mode-map
  3309. org-calendar-insert-diary-entry-key
  3310. 'org-agenda-diary-entry))))))
  3311. (defgroup org-latex nil
  3312. "Options for embedding LaTeX code into Org mode."
  3313. :tag "Org LaTeX"
  3314. :group 'org)
  3315. (defcustom org-format-latex-options
  3316. '(:foreground default :background default :scale 1.0
  3317. :html-foreground "Black" :html-background "Transparent"
  3318. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3319. "Options for creating images from LaTeX fragments.
  3320. This is a property list with the following properties:
  3321. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3322. `default' means use the foreground of the default face.
  3323. `auto' means use the foreground from the text face.
  3324. :background the background color, or \"Transparent\".
  3325. `default' means use the background of the default face.
  3326. `auto' means use the background from the text face.
  3327. :scale a scaling factor for the size of the images, to get more pixels
  3328. :html-foreground, :html-background, :html-scale
  3329. the same numbers for HTML export.
  3330. :matchers a list indicating which matchers should be used to
  3331. find LaTeX fragments. Valid members of this list are:
  3332. \"begin\" find environments
  3333. \"$1\" find single characters surrounded by $.$
  3334. \"$\" find math expressions surrounded by $...$
  3335. \"$$\" find math expressions surrounded by $$....$$
  3336. \"\\(\" find math expressions surrounded by \\(...\\)
  3337. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3338. :group 'org-latex
  3339. :type 'plist)
  3340. (defcustom org-format-latex-signal-error t
  3341. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3342. When nil, just push out a message."
  3343. :group 'org-latex
  3344. :version "24.1"
  3345. :type 'boolean)
  3346. (defcustom org-latex-to-mathml-jar-file nil
  3347. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3348. Use this to specify additional executable file say a jar file.
  3349. When using MathToWeb as the converter, specify the full-path to
  3350. your mathtoweb.jar file."
  3351. :group 'org-latex
  3352. :version "24.1"
  3353. :type '(choice
  3354. (const :tag "None" nil)
  3355. (file :tag "JAR file" :must-match t)))
  3356. (defcustom org-latex-to-mathml-convert-command nil
  3357. "Command to convert LaTeX fragments to MathML.
  3358. Replace format-specifiers in the command as noted below and use
  3359. `shell-command' to convert LaTeX to MathML.
  3360. %j: Executable file in fully expanded form as specified by
  3361. `org-latex-to-mathml-jar-file'.
  3362. %I: Input LaTeX file in fully expanded form.
  3363. %i: The latex fragment to be converted.
  3364. %o: Output MathML file.
  3365. This command is used by `org-create-math-formula'.
  3366. When using MathToWeb as the converter, set this option to
  3367. \"java -jar %j -unicode -force -df %o %I\".
  3368. When using LaTeXML set this option to
  3369. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3370. :group 'org-latex
  3371. :version "24.1"
  3372. :type '(choice
  3373. (const :tag "None" nil)
  3374. (string :tag "\nShell command")))
  3375. (defcustom org-preview-latex-default-process 'dvipng
  3376. "The default process to convert LaTeX fragments to image files.
  3377. All available processes and theirs documents can be found in
  3378. `org-preview-latex-process-alist', which see."
  3379. :group 'org-latex
  3380. :version "26.1"
  3381. :package-version '(Org . "9.0")
  3382. :type 'symbol)
  3383. (defcustom org-preview-latex-process-alist
  3384. '((dvipng
  3385. :programs ("latex" "dvipng")
  3386. :description "dvi > png"
  3387. :message "you need to install the programs: latex and dvipng."
  3388. :image-input-type "dvi"
  3389. :image-output-type "png"
  3390. :image-size-adjust (1.0 . 1.0)
  3391. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3392. :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %O %f"))
  3393. (dvisvgm
  3394. :programs ("latex" "dvisvgm")
  3395. :description "dvi > svg"
  3396. :message "you need to install the programs: latex and dvisvgm."
  3397. :use-xcolor t
  3398. :image-input-type "dvi"
  3399. :image-output-type "svg"
  3400. :image-size-adjust (1.7 . 1.5)
  3401. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3402. :image-converter ("dvisvgm %f -n -b min -c %S -o %O"))
  3403. (imagemagick
  3404. :programs ("latex" "convert")
  3405. :description "pdf > png"
  3406. :message "you need to install the programs: latex and imagemagick."
  3407. :use-xcolor t
  3408. :image-input-type "pdf"
  3409. :image-output-type "png"
  3410. :image-size-adjust (1.0 . 1.0)
  3411. :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
  3412. :image-converter
  3413. ("convert -density %D -trim -antialias %f -quality 100 %O")))
  3414. "Definitions of external processes for LaTeX previewing.
  3415. Org mode can use some external commands to generate TeX snippet's images for
  3416. previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
  3417. `org-create-formula-image' how to call them.
  3418. The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
  3419. PROPERTIES accepts the following attributes:
  3420. :programs list of strings, required programs.
  3421. :description string, describe the process.
  3422. :message string, message it when required programs cannot be found.
  3423. :image-input-type string, input file type of image converter (e.g., \"dvi\").
  3424. :image-output-type string, output file type of image converter (e.g., \"png\").
  3425. :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
  3426. deal with background and foreground color of image.
  3427. Otherwise, dvipng style background and foreground color
  3428. format are generated. You may then refer to them in
  3429. command options with \"%F\" and \"%B\".
  3430. :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
  3431. image size showed in buffer and the cdr element is for
  3432. HTML file. This option is only useful for process
  3433. developers, users should use variable
  3434. `org-format-latex-options' instead.
  3435. :post-clean list of strings, files matched are to be cleaned up once
  3436. the image is generated. When nil, the files with \".dvi\",
  3437. \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
  3438. \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
  3439. be cleaned up.
  3440. :latex-header list of strings, the LaTeX header of the snippet file.
  3441. When nil, the fallback value is used instead, which is
  3442. controlled by `org-format-latex-header',
  3443. `org-latex-default-packages-alist' and
  3444. `org-latex-packages-alist', which see.
  3445. :latex-compiler list of LaTeX commands, as strings. Each of them is given
  3446. to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
  3447. replaced with values defined below.
  3448. :image-converter list of image converter commands strings. Each of them is
  3449. given to the shell and supports any of the following
  3450. place-holders defined below.
  3451. Place-holders used by `:image-converter' and `:latex-compiler':
  3452. %f input file name
  3453. %b base name of input file
  3454. %o base directory of input file
  3455. %O absolute output file name
  3456. Place-holders only used by `:image-converter':
  3457. %F foreground of image
  3458. %B background of image
  3459. %D dpi, which is used to adjust image size by some processing commands.
  3460. %S the image size scale ratio, which is used to adjust image size by some
  3461. processing commands."
  3462. :group 'org-latex
  3463. :version "26.1"
  3464. :package-version '(Org . "9.0")
  3465. :type '(alist :tag "LaTeX to image backends"
  3466. :value-type (plist)))
  3467. (defcustom org-preview-latex-image-directory "ltximg/"
  3468. "Path to store latex preview images.
  3469. A relative path here creates many directories relative to the
  3470. processed org files paths. An absolute path puts all preview
  3471. images at the same place."
  3472. :group 'org-latex
  3473. :version "26.1"
  3474. :package-version '(Org . "9.0")
  3475. :type 'string)
  3476. (defun org-format-latex-mathml-available-p ()
  3477. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3478. (save-match-data
  3479. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3480. org-latex-to-mathml-convert-command)
  3481. (let ((executable (car (split-string
  3482. org-latex-to-mathml-convert-command))))
  3483. (when (executable-find executable)
  3484. (if (string-match
  3485. "%j" org-latex-to-mathml-convert-command)
  3486. (file-readable-p org-latex-to-mathml-jar-file)
  3487. t))))))
  3488. (defcustom org-format-latex-header "\\documentclass{article}
  3489. \\usepackage[usenames]{color}
  3490. \[PACKAGES]
  3491. \[DEFAULT-PACKAGES]
  3492. \\pagestyle{empty} % do not remove
  3493. % The settings below are copied from fullpage.sty
  3494. \\setlength{\\textwidth}{\\paperwidth}
  3495. \\addtolength{\\textwidth}{-3cm}
  3496. \\setlength{\\oddsidemargin}{1.5cm}
  3497. \\addtolength{\\oddsidemargin}{-2.54cm}
  3498. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3499. \\setlength{\\textheight}{\\paperheight}
  3500. \\addtolength{\\textheight}{-\\headheight}
  3501. \\addtolength{\\textheight}{-\\headsep}
  3502. \\addtolength{\\textheight}{-\\footskip}
  3503. \\addtolength{\\textheight}{-3cm}
  3504. \\setlength{\\topmargin}{1.5cm}
  3505. \\addtolength{\\topmargin}{-2.54cm}"
  3506. "The document header used for processing LaTeX fragments.
  3507. It is imperative that this header make sure that no page number
  3508. appears on the page. The package defined in the variables
  3509. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3510. will either replace the placeholder \"[PACKAGES]\" in this
  3511. header, or they will be appended."
  3512. :group 'org-latex
  3513. :type 'string)
  3514. (defun org-set-packages-alist (var val)
  3515. "Set the packages alist and make sure it has 3 elements per entry."
  3516. (set var (mapcar (lambda (x)
  3517. (if (and (consp x) (= (length x) 2))
  3518. (list (car x) (nth 1 x) t)
  3519. x))
  3520. val)))
  3521. (defun org-get-packages-alist (var)
  3522. "Get the packages alist and make sure it has 3 elements per entry."
  3523. (mapcar (lambda (x)
  3524. (if (and (consp x) (= (length x) 2))
  3525. (list (car x) (nth 1 x) t)
  3526. x))
  3527. (default-value var)))
  3528. (defcustom org-latex-default-packages-alist
  3529. '(("AUTO" "inputenc" t ("pdflatex"))
  3530. ("T1" "fontenc" t ("pdflatex"))
  3531. ("" "graphicx" t)
  3532. ("" "grffile" t)
  3533. ("" "longtable" nil)
  3534. ("" "wrapfig" nil)
  3535. ("" "rotating" nil)
  3536. ("normalem" "ulem" t)
  3537. ("" "amsmath" t)
  3538. ("" "textcomp" t)
  3539. ("" "amssymb" t)
  3540. ("" "capt-of" nil)
  3541. ("" "hyperref" nil))
  3542. "Alist of default packages to be inserted in the header.
  3543. Change this only if one of the packages here causes an
  3544. incompatibility with another package you are using.
  3545. The packages in this list are needed by one part or another of
  3546. Org mode to function properly:
  3547. - inputenc, fontenc: for basic font and character selection
  3548. - graphicx: for including images
  3549. - grffile: allow periods and spaces in graphics file names
  3550. - longtable: For multipage tables
  3551. - wrapfig: for figure placement
  3552. - rotating: for sideways figures and tables
  3553. - ulem: for underline and strike-through
  3554. - amsmath: for subscript and superscript and math environments
  3555. - textcomp, amssymb: for various symbols used
  3556. for interpreting the entities in `org-entities'. You can skip
  3557. some of these packages if you don't use any of their symbols.
  3558. - capt-of: for captions outside of floats
  3559. - hyperref: for cross references
  3560. Therefore you should not modify this variable unless you know
  3561. what you are doing. The one reason to change it anyway is that
  3562. you might be loading some other package that conflicts with one
  3563. of the default packages. Each element is either a cell or
  3564. a string.
  3565. A cell is of the format
  3566. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3567. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3568. when compiling LaTeX snippets into images for inclusion into
  3569. non-LaTeX output. COMPILERS is a list of compilers that should
  3570. include the package, see `org-latex-compiler'. If the document
  3571. compiler is not in the list, and the list is non-nil, the package
  3572. will not be inserted in the final document.
  3573. A string will be inserted as-is in the header of the document."
  3574. :group 'org-latex
  3575. :group 'org-export-latex
  3576. :set 'org-set-packages-alist
  3577. :get 'org-get-packages-alist
  3578. :version "26.1"
  3579. :package-version '(Org . "8.3")
  3580. :type '(repeat
  3581. (choice
  3582. (list :tag "options/package pair"
  3583. (string :tag "options")
  3584. (string :tag "package")
  3585. (boolean :tag "Snippet")
  3586. (choice
  3587. (const :tag "For all compilers" nil)
  3588. (repeat :tag "Allowed compiler" string)))
  3589. (string :tag "A line of LaTeX"))))
  3590. (defcustom org-latex-packages-alist nil
  3591. "Alist of packages to be inserted in every LaTeX header.
  3592. These will be inserted after `org-latex-default-packages-alist'.
  3593. Each element is either a cell or a string.
  3594. A cell is of the format:
  3595. (\"options\" \"package\" SNIPPET-FLAG)
  3596. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3597. needed when turning LaTeX snippets into images for inclusion into
  3598. non-LaTeX output.
  3599. A string will be inserted as-is in the header of the document.
  3600. Make sure that you only list packages here which:
  3601. - you want in every file;
  3602. - do not conflict with the setup in `org-format-latex-header';
  3603. - do not conflict with the default packages in
  3604. `org-latex-default-packages-alist'."
  3605. :group 'org-latex
  3606. :group 'org-export-latex
  3607. :set 'org-set-packages-alist
  3608. :get 'org-get-packages-alist
  3609. :type '(repeat
  3610. (choice
  3611. (list :tag "options/package pair"
  3612. (string :tag "options")
  3613. (string :tag "package")
  3614. (boolean :tag "Snippet"))
  3615. (string :tag "A line of LaTeX"))))
  3616. (defgroup org-appearance nil
  3617. "Settings for Org mode appearance."
  3618. :tag "Org Appearance"
  3619. :group 'org)
  3620. (defcustom org-level-color-stars-only nil
  3621. "Non-nil means fontify only the stars in each headline.
  3622. When nil, the entire headline is fontified.
  3623. Changing it requires restart of `font-lock-mode' to become effective
  3624. also in regions already fontified."
  3625. :group 'org-appearance
  3626. :type 'boolean)
  3627. (defcustom org-hide-leading-stars nil
  3628. "Non-nil means hide the first N-1 stars in a headline.
  3629. This works by using the face `org-hide' for these stars. This
  3630. face is white for a light background, and black for a dark
  3631. background. You may have to customize the face `org-hide' to
  3632. make this work.
  3633. Changing it requires restart of `font-lock-mode' to become effective
  3634. also in regions already fontified.
  3635. You may also set this on a per-file basis by adding one of the following
  3636. lines to the buffer:
  3637. #+STARTUP: hidestars
  3638. #+STARTUP: showstars"
  3639. :group 'org-appearance
  3640. :type 'boolean)
  3641. (defcustom org-hidden-keywords nil
  3642. "List of symbols corresponding to keywords to be hidden the org buffer.
  3643. For example, a value \\='(title) for this list will make the document's title
  3644. appear in the buffer without the initial #+TITLE: keyword."
  3645. :group 'org-appearance
  3646. :version "24.1"
  3647. :type '(set (const :tag "#+AUTHOR" author)
  3648. (const :tag "#+DATE" date)
  3649. (const :tag "#+EMAIL" email)
  3650. (const :tag "#+TITLE" title)))
  3651. (defcustom org-custom-properties nil
  3652. "List of properties (as strings) with a special meaning.
  3653. The default use of these custom properties is to let the user
  3654. hide them with `org-toggle-custom-properties-visibility'."
  3655. :group 'org-properties
  3656. :group 'org-appearance
  3657. :version "24.3"
  3658. :type '(repeat (string :tag "Property Name")))
  3659. (defcustom org-fontify-done-headline nil
  3660. "Non-nil means change the face of a headline if it is marked DONE.
  3661. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3662. When this is non-nil, the headline after the keyword is set to the
  3663. `org-headline-done' as an additional indication."
  3664. :group 'org-appearance
  3665. :type 'boolean)
  3666. (defcustom org-fontify-emphasized-text t
  3667. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3668. Changing this variable requires a restart of Emacs to take effect."
  3669. :group 'org-appearance
  3670. :type 'boolean)
  3671. (defcustom org-fontify-whole-heading-line nil
  3672. "Non-nil means fontify the whole line for headings.
  3673. This is useful when setting a background color for the
  3674. org-level-* faces."
  3675. :group 'org-appearance
  3676. :type 'boolean)
  3677. (defcustom org-highlight-latex-and-related nil
  3678. "Non-nil means highlight LaTeX related syntax in the buffer.
  3679. When non nil, the value should be a list containing any of the
  3680. following symbols:
  3681. `latex' Highlight LaTeX snippets and environments.
  3682. `script' Highlight subscript and superscript.
  3683. `entities' Highlight entities."
  3684. :group 'org-appearance
  3685. :version "24.4"
  3686. :package-version '(Org . "8.0")
  3687. :type '(choice
  3688. (const :tag "No highlighting" nil)
  3689. (set :greedy t :tag "Highlight"
  3690. (const :tag "LaTeX snippets and environments" latex)
  3691. (const :tag "Subscript and superscript" script)
  3692. (const :tag "Entities" entities))))
  3693. (defcustom org-hide-emphasis-markers nil
  3694. "Non-nil mean font-lock should hide the emphasis marker characters."
  3695. :group 'org-appearance
  3696. :type 'boolean)
  3697. (defcustom org-hide-macro-markers nil
  3698. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3699. :group 'org-appearance
  3700. :type 'boolean)
  3701. (defcustom org-pretty-entities nil
  3702. "Non-nil means show entities as UTF8 characters.
  3703. When nil, the \\name form remains in the buffer."
  3704. :group 'org-appearance
  3705. :version "24.1"
  3706. :type 'boolean)
  3707. (defcustom org-pretty-entities-include-sub-superscripts t
  3708. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3709. :group 'org-appearance
  3710. :version "24.1"
  3711. :type 'boolean)
  3712. (defvar org-emph-re nil
  3713. "Regular expression for matching emphasis.
  3714. After a match, the match groups contain these elements:
  3715. 0 The match of the full regular expression, including the characters
  3716. before and after the proper match
  3717. 1 The character before the proper match, or empty at beginning of line
  3718. 2 The proper match, including the leading and trailing markers
  3719. 3 The leading marker like * or /, indicating the type of highlighting
  3720. 4 The text between the emphasis markers, not including the markers
  3721. 5 The character after the match, empty at the end of a line")
  3722. (defvar org-verbatim-re nil
  3723. "Regular expression for matching verbatim text.")
  3724. (defvar org-emphasis-regexp-components) ; defined just below
  3725. (defvar org-emphasis-alist) ; defined just below
  3726. (defun org-set-emph-re (var val)
  3727. "Set variable and compute the emphasis regular expression."
  3728. (set var val)
  3729. (when (and (boundp 'org-emphasis-alist)
  3730. (boundp 'org-emphasis-regexp-components)
  3731. org-emphasis-alist org-emphasis-regexp-components)
  3732. (pcase-let*
  3733. ((`(,pre ,post ,border ,body ,nl) org-emphasis-regexp-components)
  3734. (body (if (<= nl 0) body
  3735. (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl)))
  3736. (template
  3737. (format (concat "\\([%s]\\|^\\)" ;before markers
  3738. "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
  3739. "\\([%s]\\|$\\)") ;after markers
  3740. pre border border body border post)))
  3741. (setq org-emph-re (format template "*/_+"))
  3742. (setq org-verbatim-re (format template "=~")))))
  3743. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3744. ;; set this option proved cumbersome. See this message/thread:
  3745. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3746. (defvar org-emphasis-regexp-components
  3747. '("- \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3748. "Components used to build the regular expression for emphasis.
  3749. This is a list with five entries. Terminology: In an emphasis string
  3750. like \" *strong word* \", we call the initial space PREMATCH, the final
  3751. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3752. and \"trong wor\" is the body. The different components in this variable
  3753. specify what is allowed/forbidden in each part:
  3754. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3755. post Chars allowed as postmatch. End of line will be allowed too.
  3756. border The chars *forbidden* as border characters.
  3757. body-regexp A regexp like \".\" to match a body character. Don't use
  3758. non-shy groups here, and don't allow newline here.
  3759. newline The maximum number of newlines allowed in an emphasis exp.
  3760. You need to reload Org or to restart Emacs after customizing this.")
  3761. (defcustom org-emphasis-alist
  3762. '(("*" bold)
  3763. ("/" italic)
  3764. ("_" underline)
  3765. ("=" org-verbatim verbatim)
  3766. ("~" org-code verbatim)
  3767. ("+" (:strike-through t)))
  3768. "Alist of characters and faces to emphasize text.
  3769. Text starting and ending with a special character will be emphasized,
  3770. for example *bold*, _underlined_ and /italic/. This variable sets the
  3771. marker characters and the face to be used by font-lock for highlighting
  3772. in Org buffers.
  3773. You need to reload Org or to restart Emacs after customizing this."
  3774. :group 'org-appearance
  3775. :set 'org-set-emph-re
  3776. :version "24.4"
  3777. :package-version '(Org . "8.0")
  3778. :type '(repeat
  3779. (list
  3780. (string :tag "Marker character")
  3781. (choice
  3782. (face :tag "Font-lock-face")
  3783. (plist :tag "Face property list"))
  3784. (option (const verbatim)))))
  3785. (defvar org-protecting-blocks '("src" "example" "export")
  3786. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3787. This is needed for font-lock setup.")
  3788. ;;; Functions and variables from their packages
  3789. ;; Declared here to avoid compiler warnings
  3790. (defvar mark-active)
  3791. ;; Various packages
  3792. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3793. (declare-function calendar-forward-day "cal-move" (arg))
  3794. (declare-function calendar-goto-date "cal-move" (date))
  3795. (declare-function calendar-goto-today "cal-move" ())
  3796. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3797. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3798. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3799. (declare-function cdlatex-tab "ext:cdlatex" ())
  3800. (declare-function dired-get-filename
  3801. "dired"
  3802. (&optional localp no-error-if-not-filep))
  3803. (declare-function iswitchb-read-buffer
  3804. "iswitchb"
  3805. (prompt &optional
  3806. default require-match _predicate start matches-set))
  3807. (declare-function org-agenda-change-all-lines
  3808. "org-agenda"
  3809. (newhead hdmarker &optional fixface just-this))
  3810. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3811. "org-agenda"
  3812. (&optional end))
  3813. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3814. (declare-function org-agenda-format-item
  3815. "org-agenda"
  3816. (extra txt &optional level category tags dotime
  3817. remove-re habitp))
  3818. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3819. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3820. (declare-function org-agenda-save-markers-for-cut-and-paste
  3821. "org-agenda"
  3822. (beg end))
  3823. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3824. (declare-function org-agenda-skip "org-agenda" ())
  3825. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3826. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3827. (declare-function org-indent-mode "org-indent" (&optional arg))
  3828. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3829. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3830. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3831. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3832. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3833. (declare-function parse-time-string "parse-time" (string))
  3834. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3835. (defvar align-mode-rules-list)
  3836. (defvar calc-embedded-close-formula)
  3837. (defvar calc-embedded-open-formula)
  3838. (defvar calc-embedded-open-mode)
  3839. (defvar font-lock-unfontify-region-function)
  3840. (defvar iswitchb-temp-buflist)
  3841. (defvar org-agenda-tags-todo-honor-ignore-options)
  3842. (defvar remember-data-file)
  3843. (defvar texmathp-why)
  3844. ;;;###autoload
  3845. (defun turn-on-orgtbl ()
  3846. "Unconditionally turn on `orgtbl-mode'."
  3847. (require 'org-table)
  3848. (orgtbl-mode 1))
  3849. (defun org-at-table-p (&optional table-type)
  3850. "Non-nil if the cursor is inside an Org table.
  3851. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3852. (and (org-match-line (if table-type "[ \t]*[|+]" "[ \t]*|"))
  3853. (or (not (derived-mode-p 'org-mode))
  3854. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3855. (and e (or table-type
  3856. (eq 'org (org-element-property :type e))))))))
  3857. (defun org-at-table.el-p ()
  3858. "Non-nil when point is at a table.el table."
  3859. (and (org-match-line "[ \t]*[|+]")
  3860. (let ((element (org-element-at-point)))
  3861. (and (eq (org-element-type element) 'table)
  3862. (eq (org-element-property :type element) 'table.el)))))
  3863. (defun org-at-table-hline-p ()
  3864. "Non-nil when point is inside a hline in a table.
  3865. Assume point is already in a table."
  3866. (org-match-line org-table-hline-regexp))
  3867. (defun org-table-map-tables (function &optional quietly)
  3868. "Apply FUNCTION to the start of all tables in the buffer."
  3869. (org-with-wide-buffer
  3870. (goto-char (point-min))
  3871. (while (re-search-forward org-table-any-line-regexp nil t)
  3872. (unless quietly
  3873. (message "Mapping tables: %d%%"
  3874. (floor (* 100.0 (point)) (buffer-size))))
  3875. (beginning-of-line 1)
  3876. (when (and (looking-at org-table-line-regexp)
  3877. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3878. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3879. (save-excursion (funcall function))
  3880. (or (looking-at org-table-line-regexp)
  3881. (forward-char 1)))
  3882. (re-search-forward org-table-any-border-regexp nil 1)))
  3883. (unless quietly (message "Mapping tables: done")))
  3884. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3885. (declare-function org-clock-update-mode-line "org-clock" ())
  3886. (declare-function org-resolve-clocks "org-clock"
  3887. (&optional also-non-dangling-p prompt last-valid))
  3888. (defun org-at-TBLFM-p (&optional pos)
  3889. "Non-nil when point (or POS) is in #+TBLFM line."
  3890. (save-excursion
  3891. (goto-char (or pos (point)))
  3892. (beginning-of-line)
  3893. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3894. (eq (org-element-type (org-element-at-point)) 'table))))
  3895. (defvar org-clock-start-time)
  3896. (defvar org-clock-marker (make-marker)
  3897. "Marker recording the last clock-in.")
  3898. (defvar org-clock-hd-marker (make-marker)
  3899. "Marker recording the last clock-in, but the headline position.")
  3900. (defvar org-clock-heading ""
  3901. "The heading of the current clock entry.")
  3902. (defun org-clock-is-active ()
  3903. "Return the buffer where the clock is currently running.
  3904. Return nil if no clock is running."
  3905. (marker-buffer org-clock-marker))
  3906. (defun org-check-running-clock ()
  3907. "Check if the current buffer contains the running clock.
  3908. If yes, offer to stop it and to save the buffer with the changes."
  3909. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3910. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3911. (buffer-name))))
  3912. (org-clock-out)
  3913. (when (y-or-n-p "Save changed buffer?")
  3914. (save-buffer))))
  3915. (defun org-clocktable-try-shift (dir n)
  3916. "Check if this line starts a clock table, if yes, shift the time block."
  3917. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3918. (org-clocktable-shift dir n)))
  3919. ;;;###autoload
  3920. (defun org-clock-persistence-insinuate ()
  3921. "Set up hooks for clock persistence."
  3922. (require 'org-clock)
  3923. (add-hook 'org-mode-hook 'org-clock-load)
  3924. (add-hook 'kill-emacs-hook 'org-clock-save))
  3925. (defgroup org-archive nil
  3926. "Options concerning archiving in Org mode."
  3927. :tag "Org Archive"
  3928. :group 'org-structure)
  3929. (defcustom org-archive-location "%s_archive::"
  3930. "The location where subtrees should be archived.
  3931. The value of this variable is a string, consisting of two parts,
  3932. separated by a double-colon. The first part is a filename and
  3933. the second part is a headline.
  3934. When the filename is omitted, archiving happens in the same file.
  3935. %s in the filename will be replaced by the current file
  3936. name (without the directory part). Archiving to a different file
  3937. is useful to keep archived entries from contributing to the
  3938. Org Agenda.
  3939. The archived entries will be filed as subtrees of the specified
  3940. headline. When the headline is omitted, the subtrees are simply
  3941. filed away at the end of the file, as top-level entries. Also in
  3942. the heading you can use %s to represent the file name, this can be
  3943. useful when using the same archive for a number of different files.
  3944. Here are a few examples:
  3945. \"%s_archive::\"
  3946. If the current file is Projects.org, archive in file
  3947. Projects.org_archive, as top-level trees. This is the default.
  3948. \"::* Archived Tasks\"
  3949. Archive in the current file, under the top-level headline
  3950. \"* Archived Tasks\".
  3951. \"~/org/archive.org::\"
  3952. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3953. \"~/org/archive.org::* From %s\"
  3954. Archive in file ~/org/archive.org (absolute path), under headlines
  3955. \"From FILENAME\" where file name is the current file name.
  3956. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3957. The \"datetree/\" string is special, signifying to archive
  3958. items to the datetree. Items are placed in either the CLOSED
  3959. date of the item, or the current date if there is no CLOSED date.
  3960. The heading will be a subentry to the current date. There doesn't
  3961. need to be a heading, but there always needs to be a slash after
  3962. datetree. For example, to store archived items directly in the
  3963. datetree, use \"~/org/datetree.org::datetree/\".
  3964. \"basement::** Finished Tasks\"
  3965. Archive in file ./basement (relative path), as level 3 trees
  3966. below the level 2 heading \"** Finished Tasks\".
  3967. You may set this option on a per-file basis by adding to the buffer a
  3968. line like
  3969. #+ARCHIVE: basement::** Finished Tasks
  3970. You may also define it locally for a subtree by setting an ARCHIVE property
  3971. in the entry. If such a property is found in an entry, or anywhere up
  3972. the hierarchy, it will be used."
  3973. :group 'org-archive
  3974. :type 'string)
  3975. (defcustom org-agenda-skip-archived-trees t
  3976. "Non-nil means the agenda will skip any items located in archived trees.
  3977. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3978. variable is no longer recommended, you should leave it at the value t.
  3979. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3980. :group 'org-archive
  3981. :group 'org-agenda-skip
  3982. :type 'boolean)
  3983. (defcustom org-columns-skip-archived-trees t
  3984. "Non-nil means ignore archived trees when creating column view."
  3985. :group 'org-archive
  3986. :group 'org-properties
  3987. :type 'boolean)
  3988. (defcustom org-cycle-open-archived-trees nil
  3989. "Non-nil means `org-cycle' will open archived trees.
  3990. An archived tree is a tree marked with the tag ARCHIVE.
  3991. When nil, archived trees will stay folded. You can still open them with
  3992. normal outline commands like `show-all', but not with the cycling commands."
  3993. :group 'org-archive
  3994. :group 'org-cycle
  3995. :type 'boolean)
  3996. (defcustom org-sparse-tree-open-archived-trees nil
  3997. "Non-nil means sparse tree construction shows matches in archived trees.
  3998. When nil, matches in these trees are highlighted, but the trees are kept in
  3999. collapsed state."
  4000. :group 'org-archive
  4001. :group 'org-sparse-trees
  4002. :type 'boolean)
  4003. (defcustom org-sparse-tree-default-date-type nil
  4004. "The default date type when building a sparse tree.
  4005. When this is nil, a date is a scheduled or a deadline timestamp.
  4006. Otherwise, these types are allowed:
  4007. all: all timestamps
  4008. active: only active timestamps (<...>)
  4009. inactive: only inactive timestamps ([...])
  4010. scheduled: only scheduled timestamps
  4011. deadline: only deadline timestamps"
  4012. :type '(choice (const :tag "Scheduled or deadline" nil)
  4013. (const :tag "All timestamps" all)
  4014. (const :tag "Only active timestamps" active)
  4015. (const :tag "Only inactive timestamps" inactive)
  4016. (const :tag "Only scheduled timestamps" scheduled)
  4017. (const :tag "Only deadline timestamps" deadline)
  4018. (const :tag "Only closed timestamps" closed))
  4019. :version "26.1"
  4020. :package-version '(Org . "8.3")
  4021. :group 'org-sparse-trees)
  4022. (defun org-cycle-hide-archived-subtrees (state)
  4023. "Re-hide all archived subtrees after a visibility state change.
  4024. STATE should be one of the symbols listed in the docstring of
  4025. `org-cycle-hook'."
  4026. (when (and (not org-cycle-open-archived-trees)
  4027. (not (memq state '(overview folded))))
  4028. (save-excursion
  4029. (let* ((globalp (memq state '(contents all)))
  4030. (beg (if globalp (point-min) (point)))
  4031. (end (if globalp (point-max) (org-end-of-subtree t))))
  4032. (org-hide-archived-subtrees beg end)
  4033. (goto-char beg)
  4034. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4035. (message "%s" (substitute-command-keys
  4036. "Subtree is archived and stays closed. Use \
  4037. `\\[org-force-cycle-archived]' to cycle it anyway.")))))))
  4038. (defun org-force-cycle-archived ()
  4039. "Cycle subtree even if it is archived."
  4040. (interactive)
  4041. (setq this-command 'org-cycle)
  4042. (let ((org-cycle-open-archived-trees t))
  4043. (call-interactively 'org-cycle)))
  4044. (defun org-hide-archived-subtrees (beg end)
  4045. "Re-hide all archived subtrees after a visibility state change."
  4046. (org-with-wide-buffer
  4047. (let ((case-fold-search nil)
  4048. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4049. (goto-char beg)
  4050. ;; Include headline point is currently on.
  4051. (beginning-of-line)
  4052. (while (and (< (point) end) (re-search-forward re end t))
  4053. (when (member org-archive-tag (org-get-tags))
  4054. (org-flag-subtree t)
  4055. (org-end-of-subtree t))))))
  4056. (declare-function outline-end-of-heading "outline" ())
  4057. (declare-function outline-flag-region "outline" (from to flag))
  4058. (defun org-flag-subtree (flag)
  4059. (save-excursion
  4060. (org-back-to-heading t)
  4061. (outline-end-of-heading)
  4062. (outline-flag-region (point)
  4063. (progn (org-end-of-subtree t) (point))
  4064. flag)))
  4065. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4066. ;; Declare Column View Code
  4067. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4068. (declare-function org-columns-compute "org-colview" (property))
  4069. ;; Declare ID code
  4070. (declare-function org-id-store-link "org-id")
  4071. (declare-function org-id-locations-load "org-id")
  4072. (declare-function org-id-locations-save "org-id")
  4073. (defvar org-id-track-globally)
  4074. ;;; Variables for pre-computed regular expressions, all buffer local
  4075. (defvar-local org-todo-regexp nil
  4076. "Matches any of the TODO state keywords.
  4077. Since TODO keywords are case-sensitive, `case-fold-search' is
  4078. expected to be bound to nil when matching against this regexp.")
  4079. (defvar-local org-not-done-regexp nil
  4080. "Matches any of the TODO state keywords except the last one.
  4081. Since TODO keywords are case-sensitive, `case-fold-search' is
  4082. expected to be bound to nil when matching against this regexp.")
  4083. (defvar-local org-not-done-heading-regexp nil
  4084. "Matches a TODO headline that is not done.
  4085. Since TODO keywords are case-sensitive, `case-fold-search' is
  4086. expected to be bound to nil when matching against this regexp.")
  4087. (defvar-local org-todo-line-regexp nil
  4088. "Matches a headline and puts TODO state into group 2 if present.
  4089. Since TODO keywords are case-sensitive, `case-fold-search' is
  4090. expected to be bound to nil when matching against this regexp.")
  4091. (defvar-local org-complex-heading-regexp nil
  4092. "Matches a headline and puts everything into groups:
  4093. group 1: Stars
  4094. group 2: The TODO keyword, maybe
  4095. group 3: Priority cookie
  4096. group 4: True headline
  4097. group 5: Tags
  4098. Since TODO keywords are case-sensitive, `case-fold-search' is
  4099. expected to be bound to nil when matching against this regexp.")
  4100. (defvar-local org-complex-heading-regexp-format nil
  4101. "Printf format to make regexp to match an exact headline.
  4102. This regexp will match the headline of any node which has the
  4103. exact headline text that is put into the format, but may have any
  4104. TODO state, priority and tags.")
  4105. (defvar-local org-todo-line-tags-regexp nil
  4106. "Matches a headline and puts TODO state into group 2 if present.
  4107. Also put tags into group 4 if tags are present.")
  4108. (defconst org-plain-time-of-day-regexp
  4109. (concat
  4110. "\\(\\<[012]?[0-9]"
  4111. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4112. "\\(--?"
  4113. "\\(\\<[012]?[0-9]"
  4114. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4115. "\\)?")
  4116. "Regular expression to match a plain time or time range.
  4117. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4118. groups carry important information:
  4119. 0 the full match
  4120. 1 the first time, range or not
  4121. 8 the second time, if it is a range.")
  4122. (defconst org-plain-time-extension-regexp
  4123. (concat
  4124. "\\(\\<[012]?[0-9]"
  4125. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4126. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4127. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4128. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4129. groups carry important information:
  4130. 0 the full match
  4131. 7 hours of duration
  4132. 9 minutes of duration")
  4133. (defconst org-stamp-time-of-day-regexp
  4134. (concat
  4135. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4136. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4137. "\\(--?"
  4138. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4139. "Regular expression to match a timestamp time or time range.
  4140. After a match, the following groups carry important information:
  4141. 0 the full match
  4142. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4143. 2 the first time, range or not
  4144. 4 the second time, if it is a range.")
  4145. (defconst org-startup-options
  4146. '(("fold" org-startup-folded t)
  4147. ("overview" org-startup-folded t)
  4148. ("nofold" org-startup-folded nil)
  4149. ("showall" org-startup-folded nil)
  4150. ("showeverything" org-startup-folded showeverything)
  4151. ("content" org-startup-folded content)
  4152. ("indent" org-startup-indented t)
  4153. ("noindent" org-startup-indented nil)
  4154. ("hidestars" org-hide-leading-stars t)
  4155. ("showstars" org-hide-leading-stars nil)
  4156. ("odd" org-odd-levels-only t)
  4157. ("oddeven" org-odd-levels-only nil)
  4158. ("align" org-startup-align-all-tables t)
  4159. ("noalign" org-startup-align-all-tables nil)
  4160. ("inlineimages" org-startup-with-inline-images t)
  4161. ("noinlineimages" org-startup-with-inline-images nil)
  4162. ("latexpreview" org-startup-with-latex-preview t)
  4163. ("nolatexpreview" org-startup-with-latex-preview nil)
  4164. ("customtime" org-display-custom-times t)
  4165. ("logdone" org-log-done time)
  4166. ("lognotedone" org-log-done note)
  4167. ("nologdone" org-log-done nil)
  4168. ("lognoteclock-out" org-log-note-clock-out t)
  4169. ("nolognoteclock-out" org-log-note-clock-out nil)
  4170. ("logrepeat" org-log-repeat state)
  4171. ("lognoterepeat" org-log-repeat note)
  4172. ("logdrawer" org-log-into-drawer t)
  4173. ("nologdrawer" org-log-into-drawer nil)
  4174. ("logstatesreversed" org-log-states-order-reversed t)
  4175. ("nologstatesreversed" org-log-states-order-reversed nil)
  4176. ("nologrepeat" org-log-repeat nil)
  4177. ("logreschedule" org-log-reschedule time)
  4178. ("lognotereschedule" org-log-reschedule note)
  4179. ("nologreschedule" org-log-reschedule nil)
  4180. ("logredeadline" org-log-redeadline time)
  4181. ("lognoteredeadline" org-log-redeadline note)
  4182. ("nologredeadline" org-log-redeadline nil)
  4183. ("logrefile" org-log-refile time)
  4184. ("lognoterefile" org-log-refile note)
  4185. ("nologrefile" org-log-refile nil)
  4186. ("fninline" org-footnote-define-inline t)
  4187. ("nofninline" org-footnote-define-inline nil)
  4188. ("fnlocal" org-footnote-section nil)
  4189. ("fnauto" org-footnote-auto-label t)
  4190. ("fnprompt" org-footnote-auto-label nil)
  4191. ("fnconfirm" org-footnote-auto-label confirm)
  4192. ("fnplain" org-footnote-auto-label plain)
  4193. ("fnadjust" org-footnote-auto-adjust t)
  4194. ("nofnadjust" org-footnote-auto-adjust nil)
  4195. ("constcgs" constants-unit-system cgs)
  4196. ("constSI" constants-unit-system SI)
  4197. ("noptag" org-tag-persistent-alist nil)
  4198. ("hideblocks" org-hide-block-startup t)
  4199. ("nohideblocks" org-hide-block-startup nil)
  4200. ("beamer" org-startup-with-beamer-mode t)
  4201. ("entitiespretty" org-pretty-entities t)
  4202. ("entitiesplain" org-pretty-entities nil))
  4203. "Variable associated with STARTUP options for Org.
  4204. Each element is a list of three items: the startup options (as written
  4205. in the #+STARTUP line), the corresponding variable, and the value to set
  4206. this variable to if the option is found. An optional forth element PUSH
  4207. means to push this value onto the list in the variable.")
  4208. (defcustom org-group-tags t
  4209. "When non-nil (the default), use group tags.
  4210. This can be turned on/off through `org-toggle-tags-groups'."
  4211. :group 'org-tags
  4212. :group 'org-startup
  4213. :type 'boolean)
  4214. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4215. (defun org-toggle-tags-groups ()
  4216. "Toggle support for group tags.
  4217. Support for group tags is controlled by the option
  4218. `org-group-tags', which is non-nil by default."
  4219. (interactive)
  4220. (setq org-group-tags (not org-group-tags))
  4221. (cond ((and (derived-mode-p 'org-agenda-mode)
  4222. org-group-tags)
  4223. (org-agenda-redo))
  4224. ((derived-mode-p 'org-mode)
  4225. (let ((org-inhibit-startup t)) (org-mode))))
  4226. (message "Groups tags support has been turned %s"
  4227. (if org-group-tags "on" "off")))
  4228. (defun org-set-regexps-and-options (&optional tags-only)
  4229. "Precompute regular expressions used in the current buffer.
  4230. When optional argument TAGS-ONLY is non-nil, only compute tags
  4231. related expressions."
  4232. (when (derived-mode-p 'org-mode)
  4233. (let ((alist (org--setup-collect-keywords
  4234. (org-make-options-regexp
  4235. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4236. (and (not tags-only)
  4237. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4238. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4239. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4240. ;; Startup options. Get this early since it does change
  4241. ;; behavior for other options (e.g., tags).
  4242. (let ((startup (cdr (assq 'startup alist))))
  4243. (dolist (option startup)
  4244. (let ((entry (assoc-string option org-startup-options t)))
  4245. (when entry
  4246. (let ((var (nth 1 entry))
  4247. (val (nth 2 entry)))
  4248. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4249. (unless (listp (symbol-value var))
  4250. (set (make-local-variable var) nil))
  4251. (add-to-list var val)))))))
  4252. (setq-local org-file-tags
  4253. (mapcar #'org-add-prop-inherited
  4254. (cdr (assq 'filetags alist))))
  4255. (setq org-current-tag-alist
  4256. (append org-tag-persistent-alist
  4257. (let ((tags (cdr (assq 'tags alist))))
  4258. (if tags (org-tag-string-to-alist tags)
  4259. org-tag-alist))))
  4260. (setq org-tag-groups-alist
  4261. (org-tag-alist-to-groups org-current-tag-alist))
  4262. (unless tags-only
  4263. ;; File properties.
  4264. (setq-local org-file-properties (cdr (assq 'property alist)))
  4265. ;; Archive location.
  4266. (let ((archive (cdr (assq 'archive alist))))
  4267. (when archive (setq-local org-archive-location archive)))
  4268. ;; Category.
  4269. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4270. (when cat
  4271. (setq-local org-category (intern cat))
  4272. (setq-local org-file-properties
  4273. (org--update-property-plist
  4274. "CATEGORY" cat org-file-properties))))
  4275. ;; Columns.
  4276. (let ((column (cdr (assq 'columns alist))))
  4277. (when column (setq-local org-columns-default-format column)))
  4278. ;; Constants.
  4279. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4280. ;; Link abbreviations.
  4281. (let ((links (cdr (assq 'link alist))))
  4282. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4283. ;; Priorities.
  4284. (let ((priorities (cdr (assq 'priorities alist))))
  4285. (when priorities
  4286. (setq-local org-highest-priority (nth 0 priorities))
  4287. (setq-local org-lowest-priority (nth 1 priorities))
  4288. (setq-local org-default-priority (nth 2 priorities))))
  4289. ;; Scripts.
  4290. (let ((scripts (assq 'scripts alist)))
  4291. (when scripts
  4292. (setq-local org-use-sub-superscripts (cdr scripts))))
  4293. ;; TODO keywords.
  4294. (setq-local org-todo-kwd-alist nil)
  4295. (setq-local org-todo-key-alist nil)
  4296. (setq-local org-todo-key-trigger nil)
  4297. (setq-local org-todo-keywords-1 nil)
  4298. (setq-local org-done-keywords nil)
  4299. (setq-local org-todo-heads nil)
  4300. (setq-local org-todo-sets nil)
  4301. (setq-local org-todo-log-states nil)
  4302. (let ((todo-sequences
  4303. (or (nreverse (cdr (assq 'todo alist)))
  4304. (let ((d (default-value 'org-todo-keywords)))
  4305. (if (not (stringp (car d))) d
  4306. ;; XXX: Backward compatibility code.
  4307. (list (cons org-todo-interpretation d)))))))
  4308. (dolist (sequence todo-sequences)
  4309. (let* ((sequence (or (run-hook-with-args-until-success
  4310. 'org-todo-setup-filter-hook sequence)
  4311. sequence))
  4312. (sequence-type (car sequence))
  4313. (keywords (cdr sequence))
  4314. (sep (member "|" keywords))
  4315. names alist)
  4316. (dolist (k (remove "|" keywords))
  4317. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4318. k)
  4319. (error "Invalid TODO keyword %s" k))
  4320. (let ((name (match-string 1 k))
  4321. (key (match-string 2 k))
  4322. (log (org-extract-log-state-settings k)))
  4323. (push name names)
  4324. (push (cons name (and key (string-to-char key))) alist)
  4325. (when log (push log org-todo-log-states))))
  4326. (let* ((names (nreverse names))
  4327. (done (if sep (org-remove-keyword-keys (cdr sep))
  4328. (last names)))
  4329. (head (car names))
  4330. (tail (list sequence-type head (car done) (org-last done))))
  4331. (add-to-list 'org-todo-heads head 'append)
  4332. (push names org-todo-sets)
  4333. (setq org-done-keywords (append org-done-keywords done nil))
  4334. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4335. (setq org-todo-key-alist
  4336. (append org-todo-key-alist
  4337. (and alist
  4338. (append '((:startgroup))
  4339. (nreverse alist)
  4340. '((:endgroup))))))
  4341. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4342. (setq org-todo-sets (nreverse org-todo-sets)
  4343. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4344. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4345. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4346. ;; Compute the regular expressions and other local variables.
  4347. ;; Using `org-outline-regexp-bol' would complicate them much,
  4348. ;; because of the fixed white space at the end of that string.
  4349. (unless org-done-keywords
  4350. (setq org-done-keywords
  4351. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4352. (setq org-not-done-keywords
  4353. (org-delete-all org-done-keywords
  4354. (copy-sequence org-todo-keywords-1))
  4355. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4356. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4357. org-not-done-heading-regexp
  4358. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4359. org-todo-line-regexp
  4360. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4361. org-complex-heading-regexp
  4362. (concat "^\\(\\*+\\)"
  4363. "\\(?: +" org-todo-regexp "\\)?"
  4364. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4365. "\\(?: +\\(.*?\\)\\)??"
  4366. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
  4367. "[ \t]*$")
  4368. org-complex-heading-regexp-format
  4369. (concat "^\\(\\*+\\)"
  4370. "\\(?: +" org-todo-regexp "\\)?"
  4371. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4372. "\\(?: +"
  4373. ;; Stats cookies can be stuck to body.
  4374. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4375. "\\(%s\\)"
  4376. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4377. "\\)"
  4378. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
  4379. "[ \t]*$")
  4380. org-todo-line-tags-regexp
  4381. (concat "^\\(\\*+\\)"
  4382. "\\(?: +" org-todo-regexp "\\)?"
  4383. "\\(?: +\\(.*?\\)\\)??"
  4384. "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
  4385. "[ \t]*$"))
  4386. (org-compute-latex-and-related-regexp)))))
  4387. (defun org--setup-collect-keywords (regexp &optional files alist)
  4388. "Return setup keywords values as an alist.
  4389. REGEXP matches a subset of setup keywords. FILES is a list of
  4390. file names already visited. It is used to avoid circular setup
  4391. files. ALIST, when non-nil, is the alist computed so far.
  4392. Return value contains the following keys: `archive', `category',
  4393. `columns', `constants', `filetags', `link', `priorities',
  4394. `property', `scripts', `startup', `tags' and `todo'."
  4395. (org-with-wide-buffer
  4396. (goto-char (point-min))
  4397. (let ((case-fold-search t))
  4398. (while (re-search-forward regexp nil t)
  4399. (let ((element (org-element-at-point)))
  4400. (when (eq (org-element-type element) 'keyword)
  4401. (let ((key (org-element-property :key element))
  4402. (value (org-element-property :value element)))
  4403. (cond
  4404. ((equal key "ARCHIVE")
  4405. (when (org-string-nw-p value)
  4406. (push (cons 'archive value) alist)))
  4407. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4408. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4409. ((equal key "CONSTANTS")
  4410. (let* ((constants (assq 'constants alist))
  4411. (store (cdr constants)))
  4412. (dolist (pair (split-string value))
  4413. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4414. pair)
  4415. (let* ((name (match-string 1 pair))
  4416. (value (match-string 2 pair))
  4417. (old (assoc name store)))
  4418. (if old (setcdr old value)
  4419. (push (cons name value) store)))))
  4420. (if constants (setcdr constants store)
  4421. (push (cons 'constants store) alist))))
  4422. ((equal key "FILETAGS")
  4423. (when (org-string-nw-p value)
  4424. (let ((old (assq 'filetags alist))
  4425. (new (apply #'nconc
  4426. (mapcar (lambda (x) (org-split-string x ":"))
  4427. (split-string value)))))
  4428. (if old (setcdr old (append new (cdr old)))
  4429. (push (cons 'filetags new) alist)))))
  4430. ((equal key "LINK")
  4431. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4432. (let ((links (assq 'link alist))
  4433. (pair (cons (match-string-no-properties 1 value)
  4434. (match-string-no-properties 2 value))))
  4435. (if links (push pair (cdr links))
  4436. (push (list 'link pair) alist)))))
  4437. ((equal key "OPTIONS")
  4438. (when (and (org-string-nw-p value)
  4439. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4440. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4441. ((equal key "PRIORITIES")
  4442. (push (cons 'priorities
  4443. (let ((prio (split-string value)))
  4444. (if (< (length prio) 3) '(?A ?C ?B)
  4445. (mapcar #'string-to-char prio))))
  4446. alist))
  4447. ((equal key "PROPERTY")
  4448. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4449. (let* ((property (assq 'property alist))
  4450. (value (org--update-property-plist
  4451. (match-string-no-properties 1 value)
  4452. (match-string-no-properties 2 value)
  4453. (cdr property))))
  4454. (if property (setcdr property value)
  4455. (push (cons 'property value) alist)))))
  4456. ((equal key "STARTUP")
  4457. (let ((startup (assq 'startup alist)))
  4458. (if startup
  4459. (setcdr startup
  4460. (append (cdr startup) (split-string value)))
  4461. (push (cons 'startup (split-string value)) alist))))
  4462. ((equal key "TAGS")
  4463. (let ((tag-cell (assq 'tags alist)))
  4464. (if tag-cell
  4465. (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
  4466. (push (cons 'tags value) alist))))
  4467. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4468. (let ((todo (assq 'todo alist))
  4469. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4470. (split-string value))))
  4471. (if todo (push value (cdr todo))
  4472. (push (list 'todo value) alist))))
  4473. ((equal key "SETUPFILE")
  4474. (unless buffer-read-only ; Do not check in Gnus messages.
  4475. (let ((f (and (org-string-nw-p value)
  4476. (expand-file-name
  4477. (org-unbracket-string "\"" "\"" value)))))
  4478. (when (and f (file-readable-p f) (not (member f files)))
  4479. (with-temp-buffer
  4480. (setq default-directory (file-name-directory f))
  4481. (insert-file-contents f)
  4482. (setq alist
  4483. ;; Fake Org mode to benefit from cache
  4484. ;; without recurring needlessly.
  4485. (let ((major-mode 'org-mode))
  4486. (org--setup-collect-keywords
  4487. regexp (cons f files) alist)))))))))))))))
  4488. alist)
  4489. (defun org-tag-string-to-alist (s)
  4490. "Return tag alist associated to string S.
  4491. S is a value for TAGS keyword or produced with
  4492. `org-tag-alist-to-string'. Return value is an alist suitable for
  4493. `org-tag-alist' or `org-tag-persistent-alist'."
  4494. (let ((lines (mapcar #'split-string (split-string s "\n" t)))
  4495. (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
  4496. "\\|{.+?}\\)" ; regular expression
  4497. "\\(?:(\\(.\\))\\)?\\'"))
  4498. alist group-flag)
  4499. (dolist (tokens lines (cdr (nreverse alist)))
  4500. (push '(:newline) alist)
  4501. (while tokens
  4502. (let ((token (pop tokens)))
  4503. (pcase token
  4504. ("{"
  4505. (push '(:startgroup) alist)
  4506. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4507. ("}"
  4508. (push '(:endgroup) alist)
  4509. (setq group-flag nil))
  4510. ("["
  4511. (push '(:startgrouptag) alist)
  4512. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4513. ("]"
  4514. (push '(:endgrouptag) alist)
  4515. (setq group-flag nil))
  4516. (":"
  4517. (push '(:grouptags) alist))
  4518. ((guard (string-match tag-re token))
  4519. (let ((tag (match-string 1 token))
  4520. (key (and (match-beginning 2)
  4521. (string-to-char (match-string 2 token)))))
  4522. ;; Push all tags in groups, no matter if they already
  4523. ;; appear somewhere else in the list.
  4524. (when (or group-flag (not (assoc tag alist)))
  4525. (push (cons tag key) alist))))))))))
  4526. (defun org-tag-alist-to-string (alist &optional skip-key)
  4527. "Return tag string associated to ALIST.
  4528. ALIST is an alist, as defined in `org-tag-alist' or
  4529. `org-tag-persistent-alist', or produced with
  4530. `org-tag-string-to-alist'.
  4531. Return value is a string suitable as a value for \"TAGS\"
  4532. keyword.
  4533. When optional argument SKIP-KEY is non-nil, skip selection keys
  4534. next to tags."
  4535. (mapconcat (lambda (token)
  4536. (pcase token
  4537. (`(:startgroup) "{")
  4538. (`(:endgroup) "}")
  4539. (`(:startgrouptag) "[")
  4540. (`(:endgrouptag) "]")
  4541. (`(:grouptags) ":")
  4542. (`(:newline) "\\n")
  4543. ((and
  4544. (guard (not skip-key))
  4545. `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
  4546. (format "%s(%c)" tag key))
  4547. (`(,(and tag (pred stringp)) . ,_) tag)
  4548. (_ (user-error "Invalid tag token: %S" token))))
  4549. alist
  4550. " "))
  4551. (defun org-tag-alist-to-groups (alist)
  4552. "Return group alist from tag ALIST.
  4553. ALIST is an alist, as defined in `org-tag-alist' or
  4554. `org-tag-persistent-alist', or produced with
  4555. `org-tag-string-to-alist'. Return value is an alist following
  4556. the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
  4557. a string, summarizing TAGS, as a list of strings."
  4558. (let (groups group-status current-group)
  4559. (dolist (token alist (nreverse groups))
  4560. (pcase token
  4561. (`(,(or :startgroup :startgrouptag)) (setq group-status t))
  4562. (`(,(or :endgroup :endgrouptag))
  4563. (when (eq group-status 'append)
  4564. (push (nreverse current-group) groups))
  4565. (setq group-status nil))
  4566. (`(:grouptags) (setq group-status 'append))
  4567. ((and `(,tag . ,_) (guard group-status))
  4568. (if (eq group-status 'append) (push tag current-group)
  4569. (setq current-group (list tag))))
  4570. (_ nil)))))
  4571. (defvar org--file-cache (make-hash-table :test #'equal)
  4572. "Hash table to store contents of files referenced via a URL.
  4573. This is the cache of file URLs read using `org-file-contents'.")
  4574. (defun org-reset-file-cache ()
  4575. "Reset the cache of files downloaded by `org-file-contents'."
  4576. (clrhash org--file-cache))
  4577. (defun org-file-url-p (file)
  4578. "Non-nil if FILE is a URL."
  4579. (require 'ffap)
  4580. (string-match-p ffap-url-regexp file))
  4581. (defun org-file-contents (file &optional noerror nocache)
  4582. "Return the contents of FILE, as a string.
  4583. FILE can be a file name or URL.
  4584. If FILE is a URL, download the contents. If the URL contents are
  4585. already cached in the `org--file-cache' hash table, the download step
  4586. is skipped.
  4587. If NOERROR is non-nil, ignore the error when unable to read the FILE
  4588. from file or URL.
  4589. If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version
  4590. is available. This option applies only if FILE is a URL."
  4591. (let* ((is-url (org-file-url-p file))
  4592. (cache (and is-url
  4593. (not nocache)
  4594. (gethash file org--file-cache))))
  4595. (cond
  4596. (cache)
  4597. (is-url
  4598. (with-current-buffer (url-retrieve-synchronously file)
  4599. (goto-char (point-min))
  4600. ;; Move point to after the url-retrieve header.
  4601. (search-forward "\n\n" nil :move)
  4602. ;; Search for the success code only in the url-retrieve header.
  4603. (if (save-excursion
  4604. (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
  4605. ;; Update the cache `org--file-cache' and return contents.
  4606. (puthash file
  4607. (buffer-substring-no-properties (point) (point-max))
  4608. org--file-cache)
  4609. (funcall (if noerror #'message #'user-error)
  4610. "Unable to fetch file from %S"
  4611. file))))
  4612. (t
  4613. (with-temp-buffer
  4614. (condition-case nil
  4615. (progn
  4616. (insert-file-contents file)
  4617. (buffer-string))
  4618. (file-error
  4619. (funcall (if noerror #'message #'user-error)
  4620. "Unable to read file %S"
  4621. file))))))))
  4622. (defun org-extract-log-state-settings (x)
  4623. "Extract the log state setting from a TODO keyword string.
  4624. This will extract info from a string like \"WAIT(w@/!)\"."
  4625. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4626. (let ((kw (match-string 1 x))
  4627. (log1 (and (match-end 3) (match-string 3 x)))
  4628. (log2 (and (match-end 4) (match-string 4 x))))
  4629. (and (or log1 log2)
  4630. (list kw
  4631. (and log1 (if (equal log1 "!") 'time 'note))
  4632. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4633. (defun org-remove-keyword-keys (list)
  4634. "Remove a pair of parenthesis at the end of each string in LIST."
  4635. (mapcar (lambda (x)
  4636. (if (string-match "(.*)$" x)
  4637. (substring x 0 (match-beginning 0))
  4638. x))
  4639. list))
  4640. (defun org-assign-fast-keys (alist)
  4641. "Assign fast keys to a keyword-key alist.
  4642. Respect keys that are already there."
  4643. (let (new e (alt ?0))
  4644. (while (setq e (pop alist))
  4645. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4646. (cdr e)) ;; Key already assigned.
  4647. (push e new)
  4648. (let ((clist (string-to-list (downcase (car e))))
  4649. (used (append new alist)))
  4650. (when (= (car clist) ?@)
  4651. (pop clist))
  4652. (while (and clist (rassoc (car clist) used))
  4653. (pop clist))
  4654. (unless clist
  4655. (while (rassoc alt used)
  4656. (cl-incf alt)))
  4657. (push (cons (car e) (or (car clist) alt)) new))))
  4658. (nreverse new)))
  4659. ;;; Some variables used in various places
  4660. (defvar org-window-configuration nil
  4661. "Used in various places to store a window configuration.")
  4662. (defvar org-selected-window nil
  4663. "Used in various places to store a window configuration.")
  4664. (defvar org-finish-function nil
  4665. "Function to be called when `C-c C-c' is used.
  4666. This is for getting out of special buffers like capture.")
  4667. (defvar org-last-state)
  4668. ;; Defined somewhere in this file, but used before definition.
  4669. (defvar org-entities) ;; defined in org-entities.el
  4670. (defvar org-struct-menu)
  4671. (defvar org-org-menu)
  4672. (defvar org-tbl-menu)
  4673. ;;;; Define the Org mode
  4674. ;; We use a before-change function to check if a table might need
  4675. ;; an update.
  4676. (defvar org-table-may-need-update t
  4677. "Indicates that a table might need an update.
  4678. This variable is set by `org-before-change-function'.
  4679. `org-table-align' sets it back to nil.")
  4680. (defun org-before-change-function (_beg _end)
  4681. "Every change indicates that a table might need an update."
  4682. (setq org-table-may-need-update t))
  4683. (defvar org-mode-map)
  4684. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4685. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4686. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4688. (defvar org-table-buffer-is-an nil)
  4689. (defvar bidi-paragraph-direction)
  4690. (defvar buffer-face-mode-face)
  4691. (require 'outline)
  4692. ;; Other stuff we need.
  4693. (require 'time-date)
  4694. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4695. (require 'easymenu)
  4696. (autoload 'easy-menu-add "easymenu")
  4697. (require 'overlay)
  4698. ;; (require 'org-macs) moved higher up in the file before it is first used
  4699. (require 'org-entities)
  4700. ;; (require 'org-compat) moved higher up in the file before it is first used
  4701. (require 'org-faces)
  4702. (require 'org-list)
  4703. (require 'org-pcomplete)
  4704. (require 'org-src)
  4705. (require 'org-footnote)
  4706. (require 'org-macro)
  4707. ;; babel
  4708. (require 'ob)
  4709. ;;;###autoload
  4710. (define-derived-mode org-mode outline-mode "Org"
  4711. "Outline-based notes management and organizer, alias
  4712. \"Carsten's outline-mode for keeping track of everything.\"
  4713. Org mode develops organizational tasks around a NOTES file which
  4714. contains information about projects as plain text. Org mode is
  4715. implemented on top of Outline mode, which is ideal to keep the content
  4716. of large files well structured. It supports ToDo items, deadlines and
  4717. time stamps, which magically appear in the diary listing of the Emacs
  4718. calendar. Tables are easily created with a built-in table editor.
  4719. Plain text URL-like links connect to websites, emails (VM), Usenet
  4720. messages (Gnus), BBDB entries, and any files related to the project.
  4721. For printing and sharing of notes, an Org file (or a part of it)
  4722. can be exported as a structured ASCII or HTML file.
  4723. The following commands are available:
  4724. \\{org-mode-map}"
  4725. ;; Get rid of Outline menus, they are not needed
  4726. ;; Need to do this here because define-derived-mode sets up
  4727. ;; the keymap so late. Still, it is a waste to call this each time
  4728. ;; we switch another buffer into Org mode.
  4729. (define-key org-mode-map [menu-bar headings] 'undefined)
  4730. (define-key org-mode-map [menu-bar hide] 'undefined)
  4731. (define-key org-mode-map [menu-bar show] 'undefined)
  4732. (org-load-modules-maybe)
  4733. (org-install-agenda-files-menu)
  4734. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4735. (add-to-invisibility-spec '(org-cwidth))
  4736. (add-to-invisibility-spec '(org-hide-block . t))
  4737. (setq-local outline-regexp org-outline-regexp)
  4738. (setq-local outline-level 'org-outline-level)
  4739. (setq bidi-paragraph-direction 'left-to-right)
  4740. (when (and (stringp org-ellipsis) (not (equal "" org-ellipsis)))
  4741. (unless org-display-table
  4742. (setq org-display-table (make-display-table)))
  4743. (set-display-table-slot
  4744. org-display-table 4
  4745. (vconcat (mapcar (lambda (c) (make-glyph-code c 'org-ellipsis))
  4746. org-ellipsis)))
  4747. (setq buffer-display-table org-display-table))
  4748. (org-set-regexps-and-options)
  4749. (org-set-font-lock-defaults)
  4750. (when (and org-tag-faces (not org-tags-special-faces-re))
  4751. ;; tag faces set outside customize.... force initialization.
  4752. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4753. ;; Calc embedded
  4754. (setq-local calc-embedded-open-mode "# ")
  4755. ;; Modify a few syntax entries
  4756. (modify-syntax-entry ?@ "w")
  4757. (modify-syntax-entry ?\" "\"")
  4758. (modify-syntax-entry ?\\ "_")
  4759. (modify-syntax-entry ?~ "_")
  4760. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4761. ;; Activate before-change-function
  4762. (setq-local org-table-may-need-update t)
  4763. (add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4764. ;; Check for running clock before killing a buffer
  4765. (add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4766. ;; Initialize macros templates.
  4767. (org-macro-initialize-templates)
  4768. ;; Initialize radio targets.
  4769. (org-update-radio-target-regexp)
  4770. ;; Indentation.
  4771. (setq-local indent-line-function 'org-indent-line)
  4772. (setq-local indent-region-function 'org-indent-region)
  4773. ;; Filling and auto-filling.
  4774. (org-setup-filling)
  4775. ;; Comments.
  4776. (org-setup-comments-handling)
  4777. ;; Initialize cache.
  4778. (org-element-cache-reset)
  4779. ;; Beginning/end of defun
  4780. (setq-local beginning-of-defun-function 'org-backward-element)
  4781. (setq-local end-of-defun-function
  4782. (lambda ()
  4783. (if (not (org-at-heading-p))
  4784. (org-forward-element)
  4785. (org-forward-element)
  4786. (forward-char -1))))
  4787. ;; Next error for sparse trees
  4788. (setq-local next-error-function 'org-occur-next-match)
  4789. ;; Make sure dependence stuff works reliably, even for users who set it
  4790. ;; too late :-(
  4791. (if org-enforce-todo-dependencies
  4792. (add-hook 'org-blocker-hook
  4793. 'org-block-todo-from-children-or-siblings-or-parent)
  4794. (remove-hook 'org-blocker-hook
  4795. 'org-block-todo-from-children-or-siblings-or-parent))
  4796. (if org-enforce-todo-checkbox-dependencies
  4797. (add-hook 'org-blocker-hook
  4798. 'org-block-todo-from-checkboxes)
  4799. (remove-hook 'org-blocker-hook
  4800. 'org-block-todo-from-checkboxes))
  4801. ;; Align options lines
  4802. (setq-local
  4803. align-mode-rules-list
  4804. '((org-in-buffer-settings
  4805. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4806. (modes . '(org-mode)))))
  4807. ;; Imenu
  4808. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4809. ;; Make isearch reveal context
  4810. (setq-local outline-isearch-open-invisible-function
  4811. (lambda (&rest _) (org-show-context 'isearch)))
  4812. ;; Setup the pcomplete hooks
  4813. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4814. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4815. (setq-local pcomplete-default-completion-function 'ignore)
  4816. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4817. (setq-local pcomplete-termination-string "")
  4818. (setq-local buffer-face-mode-face 'org-default)
  4819. ;; If empty file that did not turn on Org mode automatically, make
  4820. ;; it to.
  4821. (when (and org-insert-mode-line-in-empty-file
  4822. (called-interactively-p 'any)
  4823. (= (point-min) (point-max)))
  4824. (insert "# -*- mode: org -*-\n\n"))
  4825. (unless org-inhibit-startup
  4826. (org-unmodified
  4827. (when org-startup-with-beamer-mode (org-beamer-mode))
  4828. (when org-startup-align-all-tables
  4829. (org-table-map-tables #'org-table-align t))
  4830. (when org-startup-with-inline-images (org-display-inline-images))
  4831. (when org-startup-with-latex-preview (org-toggle-latex-fragment '(16)))
  4832. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4833. (when org-startup-truncated (setq truncate-lines t))
  4834. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4835. (org-refresh-effort-properties)))
  4836. ;; Try to set `org-hide' face correctly.
  4837. (let ((foreground (org-find-invisible-foreground)))
  4838. (when foreground
  4839. (set-face-foreground 'org-hide foreground))))
  4840. ;; Update `customize-package-emacs-version-alist'
  4841. (add-to-list 'customize-package-emacs-version-alist
  4842. '(Org ("8.0" . "24.4")
  4843. ("8.1" . "24.4")
  4844. ("8.2" . "24.4")
  4845. ("8.2.7" . "24.4")
  4846. ("8.3" . "26.1")
  4847. ("9.0" . "26.1")
  4848. ("9.1" . "26.1")))
  4849. (defvar org-mode-transpose-word-syntax-table
  4850. (let ((st (make-syntax-table text-mode-syntax-table)))
  4851. (dolist (c org-emphasis-alist st)
  4852. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4853. (when (fboundp 'abbrev-table-put)
  4854. (abbrev-table-put org-mode-abbrev-table
  4855. :parents (list text-mode-abbrev-table)))
  4856. (defun org-find-invisible-foreground ()
  4857. (let ((candidates (remove
  4858. "unspecified-bg"
  4859. (nconc
  4860. (list (face-background 'default)
  4861. (face-background 'org-default))
  4862. (mapcar
  4863. (lambda (alist)
  4864. (when (boundp alist)
  4865. (cdr (assq 'background-color (symbol-value alist)))))
  4866. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4867. (list (face-foreground 'org-hide))))))
  4868. (car (remove nil candidates))))
  4869. (defun org-current-time (&optional rounding-minutes past)
  4870. "Current time, possibly rounded to ROUNDING-MINUTES.
  4871. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4872. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4873. the rounding returns a past time."
  4874. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4875. (car org-time-stamp-rounding-minutes)))
  4876. (time (decode-time)) res)
  4877. (if (< r 1)
  4878. (current-time)
  4879. (setq res
  4880. (apply 'encode-time
  4881. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4882. (nthcdr 2 time))))
  4883. (if (and past (< (float-time (time-subtract (current-time) res)) 0))
  4884. (seconds-to-time (- (float-time res) (* r 60)))
  4885. res))))
  4886. (defun org-today ()
  4887. "Return today date, considering `org-extend-today-until'."
  4888. (time-to-days
  4889. (time-subtract (current-time)
  4890. (list 0 (* 3600 org-extend-today-until) 0))))
  4891. ;;;; Font-Lock stuff, including the activators
  4892. (defvar org-mouse-map (make-sparse-keymap))
  4893. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4894. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4895. (when org-mouse-1-follows-link
  4896. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4897. (when org-tab-follows-link
  4898. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4899. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4900. (require 'font-lock)
  4901. (defconst org-non-link-chars "]\t\n\r<>")
  4902. (defvar org-link-types-re nil
  4903. "Matches a link that has a url-like prefix like \"http:\"")
  4904. (defvar org-link-re-with-space nil
  4905. "Matches a link with spaces, optional angular brackets around it.")
  4906. (defvar org-link-re-with-space2 nil
  4907. "Matches a link with spaces, optional angular brackets around it.")
  4908. (defvar org-link-re-with-space3 nil
  4909. "Matches a link with spaces, only for internal part in bracket links.")
  4910. (defvar org-angle-link-re nil
  4911. "Matches link with angular brackets, spaces are allowed.")
  4912. (defvar org-plain-link-re nil
  4913. "Matches plain link, without spaces.")
  4914. (defvar org-bracket-link-regexp nil
  4915. "Matches a link in double brackets.")
  4916. (defvar org-bracket-link-analytic-regexp nil
  4917. "Regular expression used to analyze links.
  4918. Here is what the match groups contain after a match:
  4919. 1: http:
  4920. 2: http
  4921. 3: path
  4922. 4: [desc]
  4923. 5: desc")
  4924. (defvar org-bracket-link-analytic-regexp++ nil
  4925. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4926. (defvar org-any-link-re nil
  4927. "Regular expression matching any link.")
  4928. (defconst org-match-sexp-depth 3
  4929. "Number of stacked braces for sub/superscript matching.")
  4930. (defun org-create-multibrace-regexp (left right n)
  4931. "Create a regular expression which will match a balanced sexp.
  4932. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4933. as single character strings.
  4934. The regexp returned will match the entire expression including the
  4935. delimiters. It will also define a single group which contains the
  4936. match except for the outermost delimiters. The maximum depth of
  4937. stacked delimiters is N. Escaping delimiters is not possible."
  4938. (let* ((nothing (concat "[^" left right "]*?"))
  4939. (or "\\|")
  4940. (re nothing)
  4941. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4942. (while (> n 1)
  4943. (setq n (1- n)
  4944. re (concat re or next)
  4945. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4946. (concat left "\\(" re "\\)" right)))
  4947. (defconst org-match-substring-regexp
  4948. (concat
  4949. "\\(\\S-\\)\\([_^]\\)\\("
  4950. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4951. "\\|"
  4952. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4953. "\\|"
  4954. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4955. "The regular expression matching a sub- or superscript.")
  4956. (defconst org-match-substring-with-braces-regexp
  4957. (concat
  4958. "\\(\\S-\\)\\([_^]\\)"
  4959. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4960. "The regular expression matching a sub- or superscript, forcing braces.")
  4961. (defun org-make-link-regexps ()
  4962. "Update the link regular expressions.
  4963. This should be called after the variable `org-link-parameters' has changed."
  4964. (let ((types-re (regexp-opt (org-link-types) t)))
  4965. (setq org-link-types-re
  4966. (concat "\\`" types-re ":")
  4967. org-link-re-with-space
  4968. (concat "<?" types-re ":"
  4969. "\\([^" org-non-link-chars " ]"
  4970. "[^" org-non-link-chars "]*"
  4971. "[^" org-non-link-chars " ]\\)>?")
  4972. org-link-re-with-space2
  4973. (concat "<?" types-re ":"
  4974. "\\([^" org-non-link-chars " ]"
  4975. "[^\t\n\r]*"
  4976. "[^" org-non-link-chars " ]\\)>?")
  4977. org-link-re-with-space3
  4978. (concat "<?" types-re ":"
  4979. "\\([^" org-non-link-chars " ]"
  4980. "[^\t\n\r]*\\)")
  4981. org-angle-link-re
  4982. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  4983. types-re)
  4984. org-plain-link-re
  4985. (concat
  4986. "\\<" types-re ":"
  4987. "\\([^][ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)")
  4988. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4989. org-bracket-link-regexp
  4990. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4991. org-bracket-link-analytic-regexp
  4992. (concat
  4993. "\\[\\["
  4994. "\\(" types-re ":\\)?"
  4995. "\\([^]]+\\)"
  4996. "\\]"
  4997. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4998. "\\]")
  4999. org-bracket-link-analytic-regexp++
  5000. (concat
  5001. "\\[\\["
  5002. "\\(" (regexp-opt (cons "coderef" (org-link-types)) t) ":\\)?"
  5003. "\\([^]]+\\)"
  5004. "\\]"
  5005. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5006. "\\]")
  5007. org-any-link-re
  5008. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5009. org-angle-link-re "\\)\\|\\("
  5010. org-plain-link-re "\\)"))))
  5011. (org-make-link-regexps)
  5012. (defvar org-emph-face nil)
  5013. (defun org-do-emphasis-faces (limit)
  5014. "Run through the buffer and emphasize strings."
  5015. (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)"
  5016. (car org-emphasis-regexp-components))))
  5017. (catch :exit
  5018. (while (re-search-forward quick-re limit t)
  5019. (let* ((marker (match-string 2))
  5020. (verbatim? (member marker '("~" "="))))
  5021. (when (save-excursion
  5022. (goto-char (match-beginning 0))
  5023. (and
  5024. ;; Do not match table hlines.
  5025. (not (and (equal marker "+")
  5026. (org-match-line
  5027. "[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$")))
  5028. ;; Do not match headline stars. Do not consider
  5029. ;; stars of a headline as closing marker for bold
  5030. ;; markup either.
  5031. (not (and (equal marker "*")
  5032. (save-excursion
  5033. (forward-char)
  5034. (skip-chars-backward "*")
  5035. (looking-at-p org-outline-regexp-bol))))
  5036. ;; Match full emphasis markup regexp.
  5037. (looking-at (if verbatim? org-verbatim-re org-emph-re))
  5038. ;; Do not span over paragraph boundaries.
  5039. (not (string-match-p org-element-paragraph-separate
  5040. (match-string 2)))
  5041. ;; Do not span over cells in table rows.
  5042. (not (and (save-match-data (org-match-line "[ \t]*|"))
  5043. (string-match-p "|" (match-string 4))))))
  5044. (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)))
  5045. (font-lock-prepend-text-property
  5046. (match-beginning 2) (match-end 2) 'face face)
  5047. (when verbatim?
  5048. (org-remove-flyspell-overlays-in
  5049. (match-beginning 0) (match-end 0)))
  5050. (add-text-properties (match-beginning 2) (match-end 2)
  5051. '(font-lock-multiline t org-emphasis t))
  5052. (when org-hide-emphasis-markers
  5053. (add-text-properties (match-end 4) (match-beginning 5)
  5054. '(invisible org-link))
  5055. (add-text-properties (match-beginning 3) (match-end 3)
  5056. '(invisible org-link)))
  5057. (throw :exit t))))))))
  5058. (defun org-emphasize (&optional char)
  5059. "Insert or change an emphasis, i.e. a font like bold or italic.
  5060. If there is an active region, change that region to a new emphasis.
  5061. If there is no region, just insert the marker characters and position
  5062. the cursor between them.
  5063. CHAR should be the marker character. If it is a space, it means to
  5064. remove the emphasis of the selected region.
  5065. If CHAR is not given (for example in an interactive call) it will be
  5066. prompted for."
  5067. (interactive)
  5068. (let ((erc org-emphasis-regexp-components)
  5069. (string "") beg end move s)
  5070. (if (org-region-active-p)
  5071. (setq beg (region-beginning)
  5072. end (region-end)
  5073. string (buffer-substring beg end))
  5074. (setq move t))
  5075. (unless char
  5076. (message "Emphasis marker or tag: [%s]"
  5077. (mapconcat #'car org-emphasis-alist ""))
  5078. (setq char (read-char-exclusive)))
  5079. (if (equal char ?\s)
  5080. (setq s ""
  5081. move nil)
  5082. (unless (assoc (char-to-string char) org-emphasis-alist)
  5083. (user-error "No such emphasis marker: \"%c\"" char))
  5084. (setq s (char-to-string char)))
  5085. (while (and (> (length string) 1)
  5086. (equal (substring string 0 1) (substring string -1))
  5087. (assoc (substring string 0 1) org-emphasis-alist))
  5088. (setq string (substring string 1 -1)))
  5089. (setq string (concat s string s))
  5090. (when beg (delete-region beg end))
  5091. (unless (or (bolp)
  5092. (string-match (concat "[" (nth 0 erc) "\n]")
  5093. (char-to-string (char-before (point)))))
  5094. (insert " "))
  5095. (unless (or (eobp)
  5096. (string-match (concat "[" (nth 1 erc) "\n]")
  5097. (char-to-string (char-after (point)))))
  5098. (insert " ") (backward-char 1))
  5099. (insert string)
  5100. (and move (backward-char 1))))
  5101. (defconst org-nonsticky-props
  5102. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5103. (defsubst org-rear-nonsticky-at (pos)
  5104. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5105. (defun org-activate-links (limit)
  5106. "Add link properties to links.
  5107. This includes angle, plain, and bracket links."
  5108. (catch :exit
  5109. (while (re-search-forward org-any-link-re limit t)
  5110. (let* ((start (match-beginning 0))
  5111. (end (match-end 0))
  5112. (style (cond ((eq ?< (char-after start)) 'angle)
  5113. ((eq ?\[ (char-after (1+ start))) 'bracket)
  5114. (t 'plain))))
  5115. (when (and (memq style org-highlight-links)
  5116. ;; Do not confuse plain links with tags.
  5117. (not (and (eq style 'plain)
  5118. (let ((face (get-text-property
  5119. (max (1- start) (point-min)) 'face)))
  5120. (if (consp face) (memq 'org-tag face)
  5121. (eq 'org-tag face))))))
  5122. (let* ((link-object (save-excursion
  5123. (goto-char start)
  5124. (save-match-data (org-element-link-parser))))
  5125. (link (org-element-property :raw-link link-object))
  5126. (type (org-element-property :type link-object))
  5127. (path (org-element-property :path link-object))
  5128. (properties ;for link's visible part
  5129. (list
  5130. 'face (pcase (org-link-get-parameter type :face)
  5131. ((and (pred functionp) face) (funcall face path))
  5132. ((and (pred facep) face) face)
  5133. ((and (pred consp) face) face) ;anonymous
  5134. (_ 'org-link))
  5135. 'mouse-face (or (org-link-get-parameter type :mouse-face)
  5136. 'highlight)
  5137. 'keymap (or (org-link-get-parameter type :keymap)
  5138. org-mouse-map)
  5139. 'help-echo (pcase (org-link-get-parameter type :help-echo)
  5140. ((and (pred stringp) echo) echo)
  5141. ((and (pred functionp) echo) echo)
  5142. (_ (concat "LINK: " link)))
  5143. 'htmlize-link (pcase (org-link-get-parameter type
  5144. :htmlize-link)
  5145. ((and (pred functionp) f) (funcall f))
  5146. (_ `(:uri ,link)))
  5147. 'font-lock-multiline t)))
  5148. (org-remove-flyspell-overlays-in start end)
  5149. (org-rear-nonsticky-at end)
  5150. (if (not (eq 'bracket style))
  5151. (add-text-properties start end properties)
  5152. ;; Handle invisible parts in bracket links.
  5153. (remove-text-properties start end '(invisible nil))
  5154. (let ((hidden
  5155. (append `(invisible
  5156. ,(or (org-link-get-parameter type :display)
  5157. 'org-link))
  5158. properties))
  5159. (visible-start (or (match-beginning 4) (match-beginning 2)))
  5160. (visible-end (or (match-end 4) (match-end 2))))
  5161. (add-text-properties start visible-start hidden)
  5162. (add-text-properties visible-start visible-end properties)
  5163. (add-text-properties visible-end end hidden)
  5164. (org-rear-nonsticky-at visible-start)
  5165. (org-rear-nonsticky-at visible-end)))
  5166. (let ((f (org-link-get-parameter type :activate-func)))
  5167. (when (functionp f)
  5168. (funcall f start end path (eq style 'bracket))))
  5169. (throw :exit t))))) ;signal success
  5170. nil))
  5171. (defun org-activate-code (limit)
  5172. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5173. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5174. (remove-text-properties (match-beginning 0) (match-end 0)
  5175. '(display t invisible t intangible t))
  5176. t))
  5177. (defcustom org-src-fontify-natively t
  5178. "When non-nil, fontify code in code blocks.
  5179. See also the `org-block' face."
  5180. :type 'boolean
  5181. :version "26.1"
  5182. :package-version '(Org . "8.3")
  5183. :group 'org-appearance
  5184. :group 'org-babel)
  5185. (defcustom org-allow-promoting-top-level-subtree nil
  5186. "When non-nil, allow promoting a top level subtree.
  5187. The leading star of the top level headline will be replaced
  5188. by a #."
  5189. :type 'boolean
  5190. :version "24.1"
  5191. :group 'org-appearance)
  5192. (defun org-fontify-meta-lines-and-blocks (limit)
  5193. (condition-case nil
  5194. (org-fontify-meta-lines-and-blocks-1 limit)
  5195. (error (message "Org mode fontification error in %S at %d"
  5196. (current-buffer)
  5197. (line-number-at-pos)))))
  5198. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5199. "Fontify #+ lines and blocks."
  5200. (let ((case-fold-search t))
  5201. (when (re-search-forward
  5202. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5203. limit t)
  5204. (let ((beg (match-beginning 0))
  5205. (block-start (match-end 0))
  5206. (block-end nil)
  5207. (lang (match-string 7))
  5208. (beg1 (line-beginning-position 2))
  5209. (dc1 (downcase (match-string 2)))
  5210. (dc3 (downcase (match-string 3)))
  5211. end end1 quoting block-type)
  5212. (cond
  5213. ((and (match-end 4) (equal dc3 "+begin"))
  5214. ;; Truly a block
  5215. (setq block-type (downcase (match-string 5))
  5216. quoting (member block-type org-protecting-blocks))
  5217. (when (re-search-forward
  5218. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5219. nil t) ;; on purpose, we look further than LIMIT
  5220. (setq end (min (point-max) (match-end 0))
  5221. end1 (min (point-max) (1- (match-beginning 0))))
  5222. (setq block-end (match-beginning 0))
  5223. (when quoting
  5224. (org-remove-flyspell-overlays-in beg1 end1)
  5225. (remove-text-properties beg end
  5226. '(display t invisible t intangible t)))
  5227. (add-text-properties
  5228. beg end '(font-lock-fontified t font-lock-multiline t))
  5229. (add-text-properties beg beg1 '(face org-meta-line))
  5230. (org-remove-flyspell-overlays-in beg beg1)
  5231. (add-text-properties ; For end_src
  5232. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5233. (org-remove-flyspell-overlays-in end1 end)
  5234. (cond
  5235. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5236. (org-src-font-lock-fontify-block lang block-start block-end)
  5237. (add-text-properties beg1 block-end '(src-block t)))
  5238. (quoting
  5239. (add-text-properties beg1 (min (point-max) (1+ end1))
  5240. (list 'face
  5241. (list :inherit
  5242. (let ((face-name
  5243. (intern (format "org-block-%s" lang))))
  5244. (append (and (facep face-name) (list face-name))
  5245. '(org-block))))))) ; end of source block
  5246. ((not org-fontify-quote-and-verse-blocks))
  5247. ((string= block-type "quote")
  5248. (add-face-text-property
  5249. beg1 (min (point-max) (1+ end1)) 'org-quote t))
  5250. ((string= block-type "verse")
  5251. (add-face-text-property
  5252. beg1 (min (point-max) (1+ end1)) 'org-verse t)))
  5253. (add-text-properties beg beg1 '(face org-block-begin-line))
  5254. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5255. '(face org-block-end-line))
  5256. t))
  5257. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5258. (org-remove-flyspell-overlays-in
  5259. (match-beginning 0)
  5260. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5261. (add-text-properties
  5262. beg (match-end 3)
  5263. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5264. '(font-lock-fontified t invisible t)
  5265. '(font-lock-fontified t face org-document-info-keyword)))
  5266. (add-text-properties
  5267. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5268. (if (string-equal dc1 "+title:")
  5269. '(font-lock-fontified t face org-document-title)
  5270. '(font-lock-fontified t face org-document-info))))
  5271. ((string-prefix-p "+caption" dc1)
  5272. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5273. (remove-text-properties (match-beginning 0) (match-end 0)
  5274. '(display t invisible t intangible t))
  5275. ;; Handle short captions.
  5276. (save-excursion
  5277. (beginning-of-line)
  5278. (looking-at "\\([ \t]*#\\+caption\\(?:\\[.*\\]\\)?:\\)[ \t]*"))
  5279. (add-text-properties (line-beginning-position) (match-end 1)
  5280. '(font-lock-fontified t face org-meta-line))
  5281. (add-text-properties (match-end 0) (line-end-position)
  5282. '(font-lock-fontified t face org-block))
  5283. t)
  5284. ((member dc3 '(" " ""))
  5285. (org-remove-flyspell-overlays-in beg (match-end 0))
  5286. (add-text-properties
  5287. beg (match-end 0)
  5288. '(font-lock-fontified t face font-lock-comment-face)))
  5289. (t ;; just any other in-buffer setting, but not indented
  5290. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5291. (remove-text-properties (match-beginning 0) (match-end 0)
  5292. '(display t invisible t intangible t))
  5293. (add-text-properties beg (match-end 0)
  5294. '(font-lock-fontified t face org-meta-line))
  5295. t))))))
  5296. (defun org-fontify-drawers (limit)
  5297. "Fontify drawers."
  5298. (when (re-search-forward org-drawer-regexp limit t)
  5299. (add-text-properties
  5300. (match-beginning 0) (match-end 0)
  5301. '(font-lock-fontified t face org-special-keyword))
  5302. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5303. t))
  5304. (defun org-fontify-macros (limit)
  5305. "Fontify macros."
  5306. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5307. (add-text-properties
  5308. (match-beginning 0) (match-end 0)
  5309. '(font-lock-fontified t face org-macro))
  5310. (when org-hide-macro-markers
  5311. (add-text-properties (match-end 2) (match-beginning 2)
  5312. '(invisible t))
  5313. (add-text-properties (match-beginning 1) (match-end 1)
  5314. '(invisible t)))
  5315. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5316. t))
  5317. (defun org-activate-footnote-links (limit)
  5318. "Add text properties for footnotes."
  5319. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5320. (when fn
  5321. (let* ((beg (nth 1 fn))
  5322. (end (nth 2 fn))
  5323. (label (car fn))
  5324. (referencep (/= (line-beginning-position) beg)))
  5325. (when (and referencep (nth 3 fn))
  5326. (save-excursion
  5327. (goto-char beg)
  5328. (search-forward (or label "fn:"))
  5329. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5330. (add-text-properties beg end
  5331. (list 'mouse-face 'highlight
  5332. 'keymap org-mouse-map
  5333. 'help-echo
  5334. (if referencep "Footnote reference"
  5335. "Footnote definition")
  5336. 'font-lock-fontified t
  5337. 'font-lock-multiline t
  5338. 'face 'org-footnote))))))
  5339. (defun org-activate-dates (limit)
  5340. "Add text properties for dates."
  5341. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5342. (not (equal (char-before (match-beginning 0)) 91)))
  5343. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5344. (add-text-properties (match-beginning 0) (match-end 0)
  5345. (list 'mouse-face 'highlight
  5346. 'keymap org-mouse-map))
  5347. (org-rear-nonsticky-at (match-end 0))
  5348. (when org-display-custom-times
  5349. ;; If it's a date range, activate custom time for second date.
  5350. (when (match-end 3)
  5351. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5352. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5353. t))
  5354. (defvar-local org-target-link-regexp nil
  5355. "Regular expression matching radio targets in plain text.")
  5356. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5357. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5358. border border border))
  5359. "Regular expression matching a link target.")
  5360. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5361. "Regular expression matching a radio target.")
  5362. (defconst org-any-target-regexp
  5363. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5364. "Regular expression matching any target.")
  5365. (defun org-activate-target-links (limit)
  5366. "Add text properties for target matches."
  5367. (when org-target-link-regexp
  5368. (let ((case-fold-search t))
  5369. (when (re-search-forward org-target-link-regexp limit t)
  5370. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5371. (add-text-properties (match-beginning 1) (match-end 1)
  5372. (list 'mouse-face 'highlight
  5373. 'keymap org-mouse-map
  5374. 'help-echo "Radio target link"
  5375. 'org-linked-text t))
  5376. (org-rear-nonsticky-at (match-end 1))
  5377. t))))
  5378. (defun org-update-radio-target-regexp ()
  5379. "Find all radio targets in this file and update the regular expression.
  5380. Also refresh fontification if needed."
  5381. (interactive)
  5382. (let ((old-regexp org-target-link-regexp)
  5383. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5384. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5385. (targets
  5386. (org-with-wide-buffer
  5387. (goto-char (point-min))
  5388. (let (rtn)
  5389. (while (re-search-forward org-radio-target-regexp nil t)
  5390. ;; Make sure point is really within the object.
  5391. (backward-char)
  5392. (let ((obj (org-element-context)))
  5393. (when (eq (org-element-type obj) 'radio-target)
  5394. (cl-pushnew (org-element-property :value obj) rtn
  5395. :test #'equal))))
  5396. rtn))))
  5397. (setq org-target-link-regexp
  5398. (and targets
  5399. (concat before-re
  5400. (mapconcat
  5401. (lambda (x)
  5402. (replace-regexp-in-string
  5403. " +" "\\s-+" (regexp-quote x) t t))
  5404. targets
  5405. "\\|")
  5406. after-re)))
  5407. (unless (equal old-regexp org-target-link-regexp)
  5408. ;; Clean-up cache.
  5409. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5410. ((not org-target-link-regexp) old-regexp)
  5411. (t
  5412. (concat before-re
  5413. (mapconcat
  5414. (lambda (re)
  5415. (substring re (length before-re)
  5416. (- (length after-re))))
  5417. (list old-regexp org-target-link-regexp)
  5418. "\\|")
  5419. after-re)))))
  5420. (org-with-wide-buffer
  5421. (goto-char (point-min))
  5422. (while (re-search-forward regexp nil t)
  5423. (org-element-cache-refresh (match-beginning 1)))))
  5424. ;; Re fontify buffer.
  5425. (when (memq 'radio org-highlight-links)
  5426. (org-restart-font-lock)))))
  5427. (defun org-hide-wide-columns (limit)
  5428. (let (s e)
  5429. (setq s (text-property-any (point) (or limit (point-max))
  5430. 'org-cwidth t))
  5431. (when s
  5432. (setq e (next-single-property-change s 'org-cwidth))
  5433. (add-text-properties s e '(invisible org-cwidth))
  5434. (goto-char e)
  5435. t)))
  5436. (defvar org-latex-and-related-regexp nil
  5437. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5438. (defun org-compute-latex-and-related-regexp ()
  5439. "Compute regular expression for LaTeX, entities and sub/superscript.
  5440. Result depends on variable `org-highlight-latex-and-related'."
  5441. (setq-local
  5442. org-latex-and-related-regexp
  5443. (let* ((re-sub
  5444. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5445. ((eq org-use-sub-superscripts '{})
  5446. (list org-match-substring-with-braces-regexp))
  5447. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5448. (re-latex
  5449. (when (memq 'latex org-highlight-latex-and-related)
  5450. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5451. (delq nil
  5452. (mapcar (lambda (x)
  5453. (and (member (car x) matchers) (nth 1 x)))
  5454. org-latex-regexps)))))
  5455. (re-entities
  5456. (when (memq 'entities org-highlight-latex-and-related)
  5457. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5458. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5459. (defun org-do-latex-and-related (limit)
  5460. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5461. LIMIT bounds the search for syntax to highlight. Stop at first
  5462. highlighted object, if any. Return t if some highlighting was
  5463. done, nil otherwise."
  5464. (when (org-string-nw-p org-latex-and-related-regexp)
  5465. (catch 'found
  5466. (while (re-search-forward org-latex-and-related-regexp limit t)
  5467. (unless
  5468. (cl-some
  5469. (lambda (f)
  5470. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5471. (save-excursion
  5472. (goto-char (1+ (match-beginning 0)))
  5473. (face-at-point nil t)))
  5474. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5475. '(?_ ?^))
  5476. 1
  5477. 0)))
  5478. (font-lock-prepend-text-property
  5479. (+ offset (match-beginning 0)) (match-end 0)
  5480. 'face 'org-latex-and-related)
  5481. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5482. '(font-lock-multiline t)))
  5483. (throw 'found t)))
  5484. nil)))
  5485. (defun org-restart-font-lock ()
  5486. "Restart `font-lock-mode', to force refontification."
  5487. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5488. (font-lock-mode -1)
  5489. (font-lock-mode 1)))
  5490. (defun org-activate-tags (limit)
  5491. (when (re-search-forward
  5492. "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" limit t)
  5493. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5494. (add-text-properties (match-beginning 1) (match-end 1)
  5495. (list 'mouse-face 'highlight
  5496. 'keymap org-mouse-map))
  5497. (org-rear-nonsticky-at (match-end 1))
  5498. t))
  5499. (defun org-outline-level ()
  5500. "Compute the outline level of the heading at point.
  5501. If this is called at a normal headline, the level is the number
  5502. of stars. Use `org-reduced-level' to remove the effect of
  5503. `org-odd-levels'. Unlike to `org-current-level', this function
  5504. takes into consideration inlinetasks."
  5505. (org-with-wide-buffer
  5506. (end-of-line)
  5507. (if (re-search-backward org-outline-regexp-bol nil t)
  5508. (1- (- (match-end 0) (match-beginning 0)))
  5509. 0)))
  5510. (defvar org-font-lock-keywords nil)
  5511. (defsubst org-re-property (property &optional literal allow-null value)
  5512. "Return a regexp matching a PROPERTY line.
  5513. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5514. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5515. non-nil, match properties even without a value.
  5516. Match group 3 is set to the value when it exists. If there is no
  5517. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5518. With optional argument VALUE, match only property lines with
  5519. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5520. unless LITERAL is non-nil."
  5521. (concat
  5522. "^\\(?4:[ \t]*\\)"
  5523. (format "\\(?1::\\(?2:%s\\):\\)"
  5524. (if literal property (regexp-quote property)))
  5525. (cond (value
  5526. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5527. (if literal value (regexp-quote value))))
  5528. (allow-null
  5529. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5530. (t
  5531. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5532. (defconst org-property-re
  5533. (org-re-property "\\S-+" 'literal t)
  5534. "Regular expression matching a property line.
  5535. There are four matching groups:
  5536. 1: :PROPKEY: including the leading and trailing colon,
  5537. 2: PROPKEY without the leading and trailing colon,
  5538. 3: PROPVAL without leading or trailing spaces,
  5539. 4: the indentation of the current line,
  5540. 5: trailing whitespace.")
  5541. (defvar org-font-lock-hook nil
  5542. "Functions to be called for special font lock stuff.")
  5543. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5544. (defvar org-font-lock-set-keywords-hook nil
  5545. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5546. This is called after `org-font-lock-extra-keywords' is defined, but before
  5547. it is installed to be used by font lock. This can be useful if something
  5548. needs to be inserted at a specific position in the font-lock sequence.")
  5549. (defun org-font-lock-hook (limit)
  5550. "Run `org-font-lock-hook' within LIMIT."
  5551. (run-hook-with-args 'org-font-lock-hook limit))
  5552. (defun org-set-font-lock-defaults ()
  5553. "Set font lock defaults for the current buffer."
  5554. (let* ((em org-fontify-emphasized-text)
  5555. (lk org-highlight-links)
  5556. (org-font-lock-extra-keywords
  5557. (list
  5558. ;; Call the hook
  5559. '(org-font-lock-hook)
  5560. ;; Headlines
  5561. `(,(if org-fontify-whole-heading-line
  5562. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5563. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5564. (1 (org-get-level-face 1))
  5565. (2 (org-get-level-face 2))
  5566. (3 (org-get-level-face 3)))
  5567. ;; Table lines
  5568. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5569. (1 'org-table t))
  5570. ;; Table internals
  5571. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5572. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5573. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5574. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5575. ;; Drawers
  5576. '(org-fontify-drawers)
  5577. ;; Properties
  5578. (list org-property-re
  5579. '(1 'org-special-keyword t)
  5580. '(3 'org-property-value t))
  5581. ;; Link related fontification.
  5582. '(org-activate-links)
  5583. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5584. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5585. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5586. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5587. ;; Targets.
  5588. (list org-any-target-regexp '(0 'org-target t))
  5589. ;; Diary sexps.
  5590. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5591. ;; Macro
  5592. '(org-fontify-macros)
  5593. '(org-hide-wide-columns (0 nil append))
  5594. ;; TODO keyword
  5595. (list (format org-heading-keyword-regexp-format
  5596. org-todo-regexp)
  5597. '(2 (org-get-todo-face 2) t))
  5598. ;; DONE
  5599. (if org-fontify-done-headline
  5600. (list (format org-heading-keyword-regexp-format
  5601. (concat
  5602. "\\(?:"
  5603. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5604. "\\)"))
  5605. '(2 'org-headline-done t))
  5606. nil)
  5607. ;; Priorities
  5608. '(org-font-lock-add-priority-faces)
  5609. ;; Tags
  5610. '(org-font-lock-add-tag-faces)
  5611. ;; Tags groups
  5612. (when (and org-group-tags org-tag-groups-alist)
  5613. (list (concat org-outline-regexp-bol ".+\\(:"
  5614. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5615. ":\\).*$")
  5616. '(1 'org-tag-group prepend)))
  5617. ;; Special keywords
  5618. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5619. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5620. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5621. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5622. ;; Emphasis
  5623. (when em '(org-do-emphasis-faces))
  5624. ;; Checkboxes
  5625. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5626. 1 'org-checkbox prepend)
  5627. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5628. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5629. (0 (org-get-checkbox-statistics-face) t)))
  5630. ;; Description list items
  5631. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5632. 1 'org-list-dt prepend)
  5633. ;; ARCHIVEd headings
  5634. (list (concat
  5635. org-outline-regexp-bol
  5636. "\\(.*:" org-archive-tag ":.*\\)")
  5637. '(1 'org-archived prepend))
  5638. ;; Specials
  5639. '(org-do-latex-and-related)
  5640. '(org-fontify-entities)
  5641. '(org-raise-scripts)
  5642. ;; Code
  5643. '(org-activate-code (1 'org-code t))
  5644. ;; COMMENT
  5645. (list (format
  5646. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5647. org-todo-regexp
  5648. org-comment-string)
  5649. '(9 'org-special-keyword t))
  5650. ;; Blocks and meta lines
  5651. '(org-fontify-meta-lines-and-blocks))))
  5652. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5653. (run-hooks 'org-font-lock-set-keywords-hook)
  5654. ;; Now set the full font-lock-keywords
  5655. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5656. (setq-local font-lock-defaults
  5657. '(org-font-lock-keywords t nil nil backward-paragraph))
  5658. (kill-local-variable 'font-lock-keywords)
  5659. nil))
  5660. (defun org-toggle-pretty-entities ()
  5661. "Toggle the composition display of entities as UTF8 characters."
  5662. (interactive)
  5663. (setq-local org-pretty-entities (not org-pretty-entities))
  5664. (org-restart-font-lock)
  5665. (if org-pretty-entities
  5666. (message "Entities are now displayed as UTF8 characters")
  5667. (save-restriction
  5668. (widen)
  5669. (decompose-region (point-min) (point-max))
  5670. (message "Entities are now displayed as plain text"))))
  5671. (defvar-local org-custom-properties-overlays nil
  5672. "List of overlays used for custom properties.")
  5673. (defun org-toggle-custom-properties-visibility ()
  5674. "Display or hide properties in `org-custom-properties'."
  5675. (interactive)
  5676. (if org-custom-properties-overlays
  5677. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5678. (setq org-custom-properties-overlays nil))
  5679. (when org-custom-properties
  5680. (org-with-wide-buffer
  5681. (goto-char (point-min))
  5682. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5683. (while (re-search-forward regexp nil t)
  5684. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5685. (when (and end (< (point) end))
  5686. ;; Hide first custom property in current drawer.
  5687. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5688. (overlay-put o 'invisible t)
  5689. (overlay-put o 'org-custom-property t)
  5690. (push o org-custom-properties-overlays))
  5691. ;; Hide additional custom properties in the same drawer.
  5692. (while (re-search-forward regexp end t)
  5693. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5694. (overlay-put o 'invisible t)
  5695. (overlay-put o 'org-custom-property t)
  5696. (push o org-custom-properties-overlays)))))
  5697. ;; Each entry is limited to a single property drawer.
  5698. (outline-next-heading)))))))
  5699. (defun org-fontify-entities (limit)
  5700. "Find an entity to fontify."
  5701. (let (ee)
  5702. (when org-pretty-entities
  5703. (catch 'match
  5704. ;; "\_ "-family is left out on purpose. Only the first one,
  5705. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5706. ;; confusing when adding a second white space character.
  5707. (while (re-search-forward
  5708. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5709. limit t)
  5710. (when (and (not (org-at-comment-p))
  5711. (setq ee (org-entity-get (match-string 1)))
  5712. (= (length (nth 6 ee)) 1))
  5713. (let* ((end (if (equal (match-string 2) "{}")
  5714. (match-end 2)
  5715. (match-end 1))))
  5716. (add-text-properties
  5717. (match-beginning 0) end
  5718. (list 'font-lock-fontified t))
  5719. (compose-region (match-beginning 0) end
  5720. (nth 6 ee) nil)
  5721. (backward-char 1)
  5722. (throw 'match t))))
  5723. nil))))
  5724. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5725. "Fontify string S like in Org mode."
  5726. (with-temp-buffer
  5727. (insert s)
  5728. (let ((org-odd-levels-only odd-levels))
  5729. (org-mode)
  5730. (org-font-lock-ensure)
  5731. (buffer-string))))
  5732. (defvar org-m nil)
  5733. (defvar org-l nil)
  5734. (defvar org-f nil)
  5735. (defun org-get-level-face (n)
  5736. "Get the right face for match N in font-lock matching of headlines."
  5737. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5738. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5739. (if org-cycle-level-faces
  5740. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5741. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5742. (cond
  5743. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5744. ((eq n 2) org-f)
  5745. (t (unless org-level-color-stars-only org-f))))
  5746. (defun org-face-from-face-or-color (context inherit face-or-color)
  5747. "Create a face list that inherits INHERIT, but sets the foreground color.
  5748. When FACE-OR-COLOR is not a string, just return it."
  5749. (if (stringp face-or-color)
  5750. (list :inherit inherit
  5751. (cdr (assoc context org-faces-easy-properties))
  5752. face-or-color)
  5753. face-or-color))
  5754. (defun org-get-todo-face (kwd)
  5755. "Get the right face for a TODO keyword KWD.
  5756. If KWD is a number, get the corresponding match group."
  5757. (when (numberp kwd) (setq kwd (match-string kwd)))
  5758. (or (org-face-from-face-or-color
  5759. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5760. (and (member kwd org-done-keywords) 'org-done)
  5761. 'org-todo))
  5762. (defun org-get-priority-face (priority)
  5763. "Get the right face for PRIORITY.
  5764. PRIORITY is a character."
  5765. (or (org-face-from-face-or-color
  5766. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5767. 'org-priority))
  5768. (defun org-get-tag-face (tag)
  5769. "Get the right face for TAG.
  5770. If TAG is a number, get the corresponding match group."
  5771. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5772. (or (org-face-from-face-or-color
  5773. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5774. 'org-tag)))
  5775. (defun org-font-lock-add-priority-faces (limit)
  5776. "Add the special priority faces."
  5777. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5778. (add-text-properties
  5779. (match-beginning 1) (match-end 1)
  5780. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5781. 'font-lock-fontified t))))
  5782. (defun org-font-lock-add-tag-faces (limit)
  5783. "Add the special tag faces."
  5784. (when (and org-tag-faces org-tags-special-faces-re)
  5785. (while (re-search-forward org-tags-special-faces-re limit t)
  5786. (add-text-properties (match-beginning 1) (match-end 1)
  5787. (list 'face (org-get-tag-face 1)
  5788. 'font-lock-fontified t))
  5789. (backward-char 1))))
  5790. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5791. "Remove fontification and activation overlays from links."
  5792. (font-lock-default-unfontify-region beg end)
  5793. (let* ((buffer-undo-list t)
  5794. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5795. (inhibit-modification-hooks t)
  5796. deactivate-mark buffer-file-name buffer-file-truename)
  5797. (decompose-region beg end)
  5798. (remove-text-properties beg end
  5799. '(mouse-face t keymap t org-linked-text t
  5800. invisible t intangible t
  5801. org-emphasis t))
  5802. (org-remove-font-lock-display-properties beg end)))
  5803. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5804. ((raise 0.3) (height 0.7))
  5805. ((raise -0.5))
  5806. ((raise 0.5)))
  5807. "Display properties for showing superscripts and subscripts.")
  5808. (defun org-remove-font-lock-display-properties (beg end)
  5809. "Remove specific display properties that have been added by font lock.
  5810. The will remove the raise properties that are used to show superscripts
  5811. and subscripts."
  5812. (let (next prop)
  5813. (while (< beg end)
  5814. (setq next (next-single-property-change beg 'display nil end)
  5815. prop (get-text-property beg 'display))
  5816. (when (member prop org-script-display)
  5817. (put-text-property beg next 'display nil))
  5818. (setq beg next))))
  5819. (defun org-raise-scripts (limit)
  5820. "Add raise properties to sub/superscripts."
  5821. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5822. (re-search-forward
  5823. (if (eq org-use-sub-superscripts t)
  5824. org-match-substring-regexp
  5825. org-match-substring-with-braces-regexp)
  5826. limit t))
  5827. (let* ((pos (point)) table-p comment-p
  5828. (mpos (match-beginning 3))
  5829. (emph-p (get-text-property mpos 'org-emphasis))
  5830. (link-p (get-text-property mpos 'mouse-face))
  5831. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5832. (goto-char (point-at-bol))
  5833. (setq table-p (looking-at-p org-table-dataline-regexp)
  5834. comment-p (looking-at-p "^[ \t]*#[ +]"))
  5835. (goto-char pos)
  5836. ;; Handle a_b^c
  5837. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5838. (unless (or comment-p emph-p link-p keyw-p)
  5839. (put-text-property (match-beginning 3) (match-end 0)
  5840. 'display
  5841. (if (equal (char-after (match-beginning 2)) ?^)
  5842. (nth (if table-p 3 1) org-script-display)
  5843. (nth (if table-p 2 0) org-script-display)))
  5844. (add-text-properties (match-beginning 2) (match-end 2)
  5845. (list 'invisible t))
  5846. (when (and (eq (char-after (match-beginning 3)) ?{)
  5847. (eq (char-before (match-end 3)) ?}))
  5848. (add-text-properties (match-beginning 3) (1+ (match-beginning 3))
  5849. (list 'invisible t))
  5850. (add-text-properties (1- (match-end 3)) (match-end 3)
  5851. (list 'invisible t))))
  5852. t)))
  5853. ;;;; Visibility cycling, including org-goto and indirect buffer
  5854. ;;; Cycling
  5855. (defvar-local org-cycle-global-status nil)
  5856. (put 'org-cycle-global-status 'org-state t)
  5857. (defvar-local org-cycle-subtree-status nil)
  5858. (put 'org-cycle-subtree-status 'org-state t)
  5859. (defvar org-inlinetask-min-level)
  5860. (defun org-unlogged-message (&rest args)
  5861. "Display a message, but avoid logging it in the *Messages* buffer."
  5862. (let ((message-log-max nil))
  5863. (apply 'message args)))
  5864. ;;;###autoload
  5865. (defun org-cycle (&optional arg)
  5866. "TAB-action and visibility cycling for Org mode.
  5867. This is the command invoked in Org mode by the `TAB' key. Its main
  5868. purpose is outline visibility cycling, but it also invokes other actions
  5869. in special contexts.
  5870. When this function is called with a `\\[universal-argument]' prefix, rotate \
  5871. the entire
  5872. buffer through 3 states (global cycling)
  5873. 1. OVERVIEW: Show only top-level headlines.
  5874. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5875. 3. SHOW ALL: Show everything.
  5876. With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
  5877. switch to the startup visibility,
  5878. determined by the variable `org-startup-folded', and by any VISIBILITY
  5879. properties in the buffer.
  5880. With a `\\[universal-argument] \\[universal-argument] \
  5881. \\[universal-argument]' prefix argument, show the entire buffer, including
  5882. any drawers.
  5883. When inside a table, re-align the table and move to the next field.
  5884. When point is at the beginning of a headline, rotate the subtree started
  5885. by this line through 3 different states (local cycling)
  5886. 1. FOLDED: Only the main headline is shown.
  5887. 2. CHILDREN: The main headline and the direct children are shown.
  5888. From this state, you can move to one of the children
  5889. and zoom in further.
  5890. 3. SUBTREE: Show the entire subtree, including body text.
  5891. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5892. When point is at the beginning of an empty headline and the variable
  5893. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5894. of the headline by demoting and promoting it to likely levels. This
  5895. speeds up creation document structure by pressing `TAB' once or several
  5896. times right after creating a new headline.
  5897. When there is a numeric prefix, go up to a heading with level ARG, do
  5898. a `show-subtree' and return to the previous cursor position. If ARG
  5899. is negative, go up that many levels.
  5900. When point is not at the beginning of a headline, execute the global
  5901. binding for `TAB', which is re-indenting the line. See the option
  5902. `org-cycle-emulate-tab' for details.
  5903. As a special case, if point is at the beginning of the buffer and there is
  5904. no headline in line 1, this function will act as if called with prefix arg
  5905. \(`\\[universal-argument] TAB', same as `S-TAB') also when called without \
  5906. prefix arg, but only
  5907. if the variable `org-cycle-global-at-bob' is t."
  5908. (interactive "P")
  5909. (org-load-modules-maybe)
  5910. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5911. (and org-cycle-level-after-item/entry-creation
  5912. (or (org-cycle-level)
  5913. (org-cycle-item-indentation))))
  5914. (let* ((limit-level
  5915. (or org-cycle-max-level
  5916. (and (boundp 'org-inlinetask-min-level)
  5917. org-inlinetask-min-level
  5918. (1- org-inlinetask-min-level))))
  5919. (nstars (and limit-level
  5920. (if org-odd-levels-only
  5921. (and limit-level (1- (* limit-level 2)))
  5922. limit-level)))
  5923. (org-outline-regexp
  5924. (if (not (derived-mode-p 'org-mode))
  5925. outline-regexp
  5926. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5927. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5928. (not (looking-at org-outline-regexp))))
  5929. (org-cycle-hook
  5930. (if bob-special
  5931. (delq 'org-optimize-window-after-visibility-change
  5932. (copy-sequence org-cycle-hook))
  5933. org-cycle-hook))
  5934. (pos (point)))
  5935. (cond
  5936. ((equal arg '(16))
  5937. (setq last-command 'dummy)
  5938. (org-set-startup-visibility)
  5939. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5940. ((equal arg '(64))
  5941. (outline-show-all)
  5942. (org-unlogged-message "Entire buffer visible, including drawers"))
  5943. ((equal arg '(4)) (org-cycle-internal-global))
  5944. ;; Try hiding block at point.
  5945. ((org-hide-block-toggle-maybe))
  5946. ;; Try cdlatex TAB completion
  5947. ((org-try-cdlatex-tab))
  5948. ;; Table: enter it or move to the next field.
  5949. ((org-at-table-p 'any)
  5950. (if (org-at-table.el-p)
  5951. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5952. Use `\\[org-edit-special]' to edit table.el tables"))
  5953. (if arg (org-table-edit-field t)
  5954. (org-table-justify-field-maybe)
  5955. (call-interactively 'org-table-next-field))))
  5956. ((run-hook-with-args-until-success 'org-tab-after-check-for-table-hook))
  5957. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5958. (bob-special (org-cycle-internal-global))
  5959. ;; Drawers: delegate to `org-flag-drawer'.
  5960. ((save-excursion
  5961. (beginning-of-line 1)
  5962. (looking-at org-drawer-regexp))
  5963. (org-flag-drawer ; toggle block visibility
  5964. (not (get-char-property (match-end 0) 'invisible))))
  5965. ;; Show-subtree, ARG levels up from here.
  5966. ((integerp arg)
  5967. (save-excursion
  5968. (org-back-to-heading)
  5969. (outline-up-heading (if (< arg 0) (- arg)
  5970. (- (funcall outline-level) arg)))
  5971. (org-show-subtree)))
  5972. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5973. ((and (featurep 'org-inlinetask)
  5974. (org-inlinetask-at-task-p)
  5975. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5976. (org-inlinetask-toggle-visibility))
  5977. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5978. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5979. (save-excursion (move-beginning-of-line 1)
  5980. (looking-at org-outline-regexp)))
  5981. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5982. (org-cycle-internal-local))
  5983. ;; From there: TAB emulation and template completion.
  5984. (buffer-read-only (org-back-to-heading))
  5985. ((run-hook-with-args-until-success
  5986. 'org-tab-after-check-for-cycling-hook))
  5987. ((org-try-structure-completion))
  5988. ((run-hook-with-args-until-success
  5989. 'org-tab-before-tab-emulation-hook))
  5990. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5991. (or (not (bolp))
  5992. (not (looking-at org-outline-regexp))))
  5993. (call-interactively (global-key-binding "\t")))
  5994. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5995. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5996. (or (and (eq org-cycle-emulate-tab 'white)
  5997. (= (match-end 0) (point-at-eol)))
  5998. (and (eq org-cycle-emulate-tab 'whitestart)
  5999. (>= (match-end 0) pos))))
  6000. t
  6001. (eq org-cycle-emulate-tab t))
  6002. (call-interactively (global-key-binding "\t")))
  6003. (t (save-excursion
  6004. (org-back-to-heading)
  6005. (org-cycle)))))))
  6006. (defun org-cycle-internal-global ()
  6007. "Do the global cycling action."
  6008. ;; Hack to avoid display of messages for .org attachments in Gnus
  6009. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6010. (cond
  6011. ((and (eq last-command this-command)
  6012. (eq org-cycle-global-status 'overview))
  6013. ;; We just created the overview - now do table of contents
  6014. ;; This can be slow in very large buffers, so indicate action
  6015. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6016. (unless ga (org-unlogged-message "CONTENTS..."))
  6017. (org-content)
  6018. (unless ga (org-unlogged-message "CONTENTS...done"))
  6019. (setq org-cycle-global-status 'contents)
  6020. (run-hook-with-args 'org-cycle-hook 'contents))
  6021. ((and (eq last-command this-command)
  6022. (eq org-cycle-global-status 'contents))
  6023. ;; We just showed the table of contents - now show everything
  6024. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6025. (outline-show-all)
  6026. (unless ga (org-unlogged-message "SHOW ALL"))
  6027. (setq org-cycle-global-status 'all)
  6028. (run-hook-with-args 'org-cycle-hook 'all))
  6029. (t
  6030. ;; Default action: go to overview
  6031. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6032. (org-overview)
  6033. (unless ga (org-unlogged-message "OVERVIEW"))
  6034. (setq org-cycle-global-status 'overview)
  6035. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6036. (defvar org-called-with-limited-levels nil
  6037. "Non-nil when `org-with-limited-levels' is currently active.")
  6038. (defun org-invisible-p (&optional pos)
  6039. "Non-nil if the character after POS is invisible.
  6040. If POS is nil, use `point' instead."
  6041. (get-char-property (or pos (point)) 'invisible))
  6042. (defun org-cycle-internal-local ()
  6043. "Do the local cycling action."
  6044. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6045. ;; First, determine end of headline (EOH), end of subtree or item
  6046. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6047. (save-excursion
  6048. (if (org-at-item-p)
  6049. (progn
  6050. (beginning-of-line)
  6051. (setq struct (org-list-struct))
  6052. (setq eoh (point-at-eol))
  6053. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6054. (setq has-children (org-list-has-child-p (point) struct)))
  6055. (org-back-to-heading)
  6056. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6057. (setq eos (save-excursion (org-end-of-subtree t t)
  6058. (when (bolp) (backward-char)) (point)))
  6059. (setq has-children
  6060. (or (save-excursion
  6061. (let ((level (funcall outline-level)))
  6062. (outline-next-heading)
  6063. (and (org-at-heading-p t)
  6064. (> (funcall outline-level) level))))
  6065. (save-excursion
  6066. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6067. ;; Determine end invisible part of buffer (EOL)
  6068. (beginning-of-line 2)
  6069. (while (and (not (eobp)) ;This is like `next-line'.
  6070. (get-char-property (1- (point)) 'invisible))
  6071. (goto-char (next-single-char-property-change (point) 'invisible))
  6072. (and (eolp) (beginning-of-line 2)))
  6073. (setq eol (point)))
  6074. ;; Find out what to do next and set `this-command'
  6075. (cond
  6076. ((= eos eoh)
  6077. ;; Nothing is hidden behind this heading
  6078. (unless (org-before-first-heading-p)
  6079. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6080. (org-unlogged-message "EMPTY ENTRY")
  6081. (setq org-cycle-subtree-status nil)
  6082. (save-excursion
  6083. (goto-char eos)
  6084. (outline-next-heading)
  6085. (when (org-invisible-p) (org-flag-heading nil))))
  6086. ((and (or (>= eol eos)
  6087. (not (string-match "\\S-" (buffer-substring eol eos))))
  6088. (or has-children
  6089. (not (setq children-skipped
  6090. org-cycle-skip-children-state-if-no-children))))
  6091. ;; Entire subtree is hidden in one line: children view
  6092. (unless (org-before-first-heading-p)
  6093. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6094. (if (org-at-item-p)
  6095. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6096. (org-show-entry)
  6097. (org-with-limited-levels (org-show-children))
  6098. (org-show-set-visibility 'canonical)
  6099. ;; FIXME: This slows down the func way too much.
  6100. ;; How keep drawers hidden in subtree anyway?
  6101. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6102. ;; (org-cycle-hide-drawers 'subtree))
  6103. ;; Fold every list in subtree to top-level items.
  6104. (when (eq org-cycle-include-plain-lists 'integrate)
  6105. (save-excursion
  6106. (org-back-to-heading)
  6107. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6108. (beginning-of-line 1)
  6109. (let* ((struct (org-list-struct))
  6110. (prevs (org-list-prevs-alist struct))
  6111. (end (org-list-get-bottom-point struct)))
  6112. (dolist (e (org-list-get-all-items (point) struct prevs))
  6113. (org-list-set-item-visibility e struct 'folded))
  6114. (goto-char (if (< end eos) end eos)))))))
  6115. (org-unlogged-message "CHILDREN")
  6116. (save-excursion
  6117. (goto-char eos)
  6118. (outline-next-heading)
  6119. (when (org-invisible-p) (org-flag-heading nil)))
  6120. (setq org-cycle-subtree-status 'children)
  6121. (unless (org-before-first-heading-p)
  6122. (run-hook-with-args 'org-cycle-hook 'children)))
  6123. ((or children-skipped
  6124. (and (eq last-command this-command)
  6125. (eq org-cycle-subtree-status 'children)))
  6126. ;; We just showed the children, or no children are there,
  6127. ;; now show everything.
  6128. (unless (org-before-first-heading-p)
  6129. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6130. (outline-flag-region eoh eos nil)
  6131. (org-unlogged-message
  6132. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6133. (setq org-cycle-subtree-status 'subtree)
  6134. (unless (org-before-first-heading-p)
  6135. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6136. (t
  6137. ;; Default action: hide the subtree.
  6138. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6139. (outline-flag-region eoh eos t)
  6140. (org-unlogged-message "FOLDED")
  6141. (setq org-cycle-subtree-status 'folded)
  6142. (unless (org-before-first-heading-p)
  6143. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6144. ;;;###autoload
  6145. (defun org-global-cycle (&optional arg)
  6146. "Cycle the global visibility. For details see `org-cycle'.
  6147. With `\\[universal-argument]' prefix ARG, switch to startup visibility.
  6148. With a numeric prefix, show all headlines up to that level."
  6149. (interactive "P")
  6150. (let ((org-cycle-include-plain-lists
  6151. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6152. (cond
  6153. ((integerp arg)
  6154. (outline-show-all)
  6155. (outline-hide-sublevels arg)
  6156. (setq org-cycle-global-status 'contents))
  6157. ((equal arg '(4))
  6158. (org-set-startup-visibility)
  6159. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6160. (t
  6161. (org-cycle '(4))))))
  6162. (defun org-set-startup-visibility ()
  6163. "Set the visibility required by startup options and properties."
  6164. (cond
  6165. ((eq org-startup-folded t)
  6166. (org-overview))
  6167. ((eq org-startup-folded 'content)
  6168. (org-content))
  6169. ((or (eq org-startup-folded 'showeverything)
  6170. (eq org-startup-folded nil))
  6171. (outline-show-all)))
  6172. (unless (eq org-startup-folded 'showeverything)
  6173. (when org-hide-block-startup (org-hide-block-all))
  6174. (org-set-visibility-according-to-property 'no-cleanup)
  6175. (org-cycle-hide-archived-subtrees 'all)
  6176. (org-cycle-hide-drawers 'all)
  6177. (org-cycle-show-empty-lines t)))
  6178. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6179. "Switch subtree visibilities according to :VISIBILITY: property."
  6180. (interactive)
  6181. (org-with-wide-buffer
  6182. (goto-char (point-min))
  6183. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6184. (if (not (org-at-property-p)) (outline-next-heading)
  6185. (let ((state (match-string 3)))
  6186. (save-excursion
  6187. (org-back-to-heading t)
  6188. (outline-hide-subtree)
  6189. (org-reveal))
  6190. (cond
  6191. ((equal state "folded")
  6192. (outline-hide-subtree)
  6193. (org-end-of-subtree t t))
  6194. ((equal state "children")
  6195. (org-show-hidden-entry)
  6196. (org-show-children))
  6197. ((equal state "content")
  6198. (save-excursion
  6199. (save-restriction
  6200. (org-narrow-to-subtree)
  6201. (org-content)))
  6202. (org-end-of-subtree t t))
  6203. ((member state '("all" "showall"))
  6204. (outline-show-subtree))))))
  6205. (unless no-cleanup
  6206. (org-cycle-hide-archived-subtrees 'all)
  6207. (org-cycle-hide-drawers 'all)
  6208. (org-cycle-show-empty-lines 'all))))
  6209. ;; This function uses outline-regexp instead of the more fundamental
  6210. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6211. ;; buffers, where outline-regexp is needed.
  6212. (defun org-overview ()
  6213. "Switch to overview mode, showing only top-level headlines.
  6214. This shows all headlines with a level equal or greater than the level
  6215. of the first headline in the buffer. This is important, because if the
  6216. first headline is not level one, then (hide-sublevels 1) gives confusing
  6217. results."
  6218. (interactive)
  6219. (save-excursion
  6220. (let ((level
  6221. (save-excursion
  6222. (goto-char (point-min))
  6223. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6224. (goto-char (match-beginning 0))
  6225. (funcall outline-level)))))
  6226. (and level (outline-hide-sublevels level)))))
  6227. (defun org-content (&optional arg)
  6228. "Show all headlines in the buffer, like a table of contents.
  6229. With numerical argument N, show content up to level N."
  6230. (interactive "P")
  6231. (org-overview)
  6232. (save-excursion
  6233. ;; Visit all headings and show their offspring
  6234. (and (integerp arg) (org-overview))
  6235. (goto-char (point-max))
  6236. (catch 'exit
  6237. (while (and (progn (condition-case nil
  6238. (outline-previous-visible-heading 1)
  6239. (error (goto-char (point-min))))
  6240. t)
  6241. (looking-at org-outline-regexp))
  6242. (if (integerp arg)
  6243. (org-show-children (1- arg))
  6244. (outline-show-branches))
  6245. (when (bobp) (throw 'exit nil))))))
  6246. (defun org-optimize-window-after-visibility-change (state)
  6247. "Adjust the window after a change in outline visibility.
  6248. This function is the default value of the hook `org-cycle-hook'."
  6249. (when (get-buffer-window (current-buffer))
  6250. (cond
  6251. ((eq state 'content) nil)
  6252. ((eq state 'all) nil)
  6253. ((eq state 'folded) nil)
  6254. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6255. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6256. (defun org-remove-empty-overlays-at (pos)
  6257. "Remove outline overlays that do not contain non-white stuff."
  6258. (dolist (o (overlays-at pos))
  6259. (and (eq 'outline (overlay-get o 'invisible))
  6260. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6261. (overlay-end o))))
  6262. (delete-overlay o))))
  6263. (defun org-clean-visibility-after-subtree-move ()
  6264. "Fix visibility issues after moving a subtree."
  6265. ;; First, find a reasonable region to look at:
  6266. ;; Start two siblings above, end three below
  6267. (let* ((beg (save-excursion
  6268. (and (org-get-last-sibling)
  6269. (org-get-last-sibling))
  6270. (point)))
  6271. (end (save-excursion
  6272. (and (org-get-next-sibling)
  6273. (org-get-next-sibling)
  6274. (org-get-next-sibling))
  6275. (if (org-at-heading-p)
  6276. (point-at-eol)
  6277. (point))))
  6278. (level (looking-at "\\*+"))
  6279. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6280. (save-excursion
  6281. (save-restriction
  6282. (narrow-to-region beg end)
  6283. (when re
  6284. ;; Properly fold already folded siblings
  6285. (goto-char (point-min))
  6286. (while (re-search-forward re nil t)
  6287. (when (and (not (org-invisible-p))
  6288. (save-excursion
  6289. (goto-char (point-at-eol)) (org-invisible-p)))
  6290. (outline-hide-entry))))
  6291. (org-cycle-show-empty-lines 'overview)
  6292. (org-cycle-hide-drawers 'overview)))))
  6293. (defun org-cycle-show-empty-lines (state)
  6294. "Show empty lines above all visible headlines.
  6295. The region to be covered depends on STATE when called through
  6296. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6297. entire buffer covered. Note that an empty line is only shown if there
  6298. are at least `org-cycle-separator-lines' empty lines before the headline."
  6299. (when (/= org-cycle-separator-lines 0)
  6300. (save-excursion
  6301. (let* ((n (abs org-cycle-separator-lines))
  6302. (re (cond
  6303. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6304. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6305. (t (let ((ns (number-to-string (- n 2))))
  6306. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6307. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6308. beg end)
  6309. (cond
  6310. ((memq state '(overview contents t))
  6311. (setq beg (point-min) end (point-max)))
  6312. ((memq state '(children folded))
  6313. (setq beg (point)
  6314. end (progn (org-end-of-subtree t t)
  6315. (line-beginning-position 2)))))
  6316. (when beg
  6317. (goto-char beg)
  6318. (while (re-search-forward re end t)
  6319. (unless (get-char-property (match-end 1) 'invisible)
  6320. (let ((e (match-end 1))
  6321. (b (if (>= org-cycle-separator-lines 0)
  6322. (match-beginning 1)
  6323. (save-excursion
  6324. (goto-char (match-beginning 0))
  6325. (skip-chars-backward " \t\n")
  6326. (line-end-position)))))
  6327. (outline-flag-region b e nil))))))))
  6328. ;; Never hide empty lines at the end of the file.
  6329. (save-excursion
  6330. (goto-char (point-max))
  6331. (outline-previous-heading)
  6332. (outline-end-of-heading)
  6333. (when (and (looking-at "[ \t\n]+")
  6334. (= (match-end 0) (point-max)))
  6335. (outline-flag-region (point) (match-end 0) nil))))
  6336. (defun org-show-empty-lines-in-parent ()
  6337. "Move to the parent and re-show empty lines before visible headlines."
  6338. (save-excursion
  6339. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6340. (org-cycle-show-empty-lines context))))
  6341. (defun org-files-list ()
  6342. "Return `org-agenda-files' list, plus all open Org files.
  6343. This is useful for operations that need to scan all of a user's
  6344. open and agenda-wise Org files."
  6345. (let ((files (mapcar #'expand-file-name (org-agenda-files))))
  6346. (dolist (buf (buffer-list))
  6347. (with-current-buffer buf
  6348. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6349. (cl-pushnew (expand-file-name (buffer-file-name)) files
  6350. :test #'equal))))
  6351. files))
  6352. (defsubst org-entry-beginning-position ()
  6353. "Return the beginning position of the current entry."
  6354. (save-excursion (org-back-to-heading t) (point)))
  6355. (defsubst org-entry-end-position ()
  6356. "Return the end position of the current entry."
  6357. (save-excursion (outline-next-heading) (point)))
  6358. (defun org-cycle-hide-drawers (state &optional exceptions)
  6359. "Re-hide all drawers after a visibility state change.
  6360. STATE should be one of the symbols listed in the docstring of
  6361. `org-cycle-hook'. When non-nil, optional argument EXCEPTIONS is
  6362. a list of strings specifying which drawers should not be hidden."
  6363. (when (and (derived-mode-p 'org-mode)
  6364. (not (memq state '(overview folded contents))))
  6365. (save-excursion
  6366. (let* ((globalp (eq state 'all))
  6367. (beg (if globalp (point-min) (point)))
  6368. (end (if globalp (point-max)
  6369. (if (eq state 'children)
  6370. (save-excursion (outline-next-heading) (point))
  6371. (org-end-of-subtree t)))))
  6372. (goto-char beg)
  6373. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6374. (unless (member-ignore-case (match-string 1) exceptions)
  6375. (let ((drawer (org-element-at-point)))
  6376. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6377. (org-flag-drawer t drawer)
  6378. ;; Make sure to skip drawer entirely or we might flag
  6379. ;; it another time when matching its ending line with
  6380. ;; `org-drawer-regexp'.
  6381. (goto-char (org-element-property :end drawer))))))))))
  6382. (defun org-flag-drawer (flag &optional element)
  6383. "When FLAG is non-nil, hide the drawer we are at.
  6384. Otherwise make it visible. When optional argument ELEMENT is
  6385. a parsed drawer, as returned by `org-element-at-point', hide or
  6386. show that drawer instead."
  6387. (let ((drawer (or element
  6388. (and (save-excursion
  6389. (beginning-of-line)
  6390. (looking-at-p org-drawer-regexp))
  6391. (org-element-at-point)))))
  6392. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6393. (let ((post (org-element-property :post-affiliated drawer)))
  6394. (save-excursion
  6395. (outline-flag-region
  6396. (progn (goto-char post) (line-end-position))
  6397. (progn (goto-char (org-element-property :end drawer))
  6398. (skip-chars-backward " \r\t\n")
  6399. (line-end-position))
  6400. flag))
  6401. ;; When the drawer is hidden away, make sure point lies in
  6402. ;; a visible part of the buffer.
  6403. (when (and flag (> (line-beginning-position) post))
  6404. (goto-char post))))))
  6405. (defun org-subtree-end-visible-p ()
  6406. "Is the end of the current subtree visible?"
  6407. (pos-visible-in-window-p
  6408. (save-excursion (org-end-of-subtree t) (point))))
  6409. (defun org-first-headline-recenter ()
  6410. "Move cursor to the first headline and recenter the headline."
  6411. (let ((window (get-buffer-window)))
  6412. (when window
  6413. (goto-char (point-min))
  6414. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6415. (set-window-start window (line-beginning-position))))))
  6416. ;;; Saving and restoring visibility
  6417. (defun org-outline-overlay-data (&optional use-markers)
  6418. "Return a list of the locations of all outline overlays.
  6419. These are overlays with the `invisible' property value `outline'.
  6420. The return value is a list of cons cells, with start and stop
  6421. positions for each overlay.
  6422. If USE-MARKERS is set, return the positions as markers."
  6423. (let (beg end)
  6424. (org-with-wide-buffer
  6425. (delq nil
  6426. (mapcar (lambda (o)
  6427. (when (eq (overlay-get o 'invisible) 'outline)
  6428. (setq beg (overlay-start o)
  6429. end (overlay-end o))
  6430. (and beg end (> end beg)
  6431. (if use-markers
  6432. (cons (copy-marker beg)
  6433. (copy-marker end t))
  6434. (cons beg end)))))
  6435. (overlays-in (point-min) (point-max)))))))
  6436. (defun org-set-outline-overlay-data (data)
  6437. "Create visibility overlays for all positions in DATA.
  6438. DATA should have been made by `org-outline-overlay-data'."
  6439. (org-with-wide-buffer
  6440. (outline-show-all)
  6441. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6442. ;;; Folding of blocks
  6443. (defvar-local org-hide-block-overlays nil
  6444. "Overlays hiding blocks.")
  6445. (defun org-block-map (function &optional start end)
  6446. "Call FUNCTION at the head of all source blocks in the current buffer.
  6447. Optional arguments START and END can be used to limit the range."
  6448. (let ((start (or start (point-min)))
  6449. (end (or end (point-max))))
  6450. (save-excursion
  6451. (goto-char start)
  6452. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6453. (save-excursion
  6454. (save-match-data
  6455. (goto-char (match-beginning 0))
  6456. (funcall function)))))))
  6457. (defun org-hide-block-toggle-all ()
  6458. "Toggle the visibility of all blocks in the current buffer."
  6459. (org-block-map 'org-hide-block-toggle))
  6460. (defun org-hide-block-all ()
  6461. "Fold all blocks in the current buffer."
  6462. (interactive)
  6463. (org-show-block-all)
  6464. (org-block-map 'org-hide-block-toggle-maybe))
  6465. (defun org-show-block-all ()
  6466. "Unfold all blocks in the current buffer."
  6467. (interactive)
  6468. (mapc #'delete-overlay org-hide-block-overlays)
  6469. (setq org-hide-block-overlays nil))
  6470. (defun org-hide-block-toggle-maybe ()
  6471. "Toggle visibility of block at point.
  6472. Unlike to `org-hide-block-toggle', this function does not throw
  6473. an error. Return a non-nil value when toggling is successful."
  6474. (interactive)
  6475. (ignore-errors (org-hide-block-toggle)))
  6476. (defun org-hide-block-toggle (&optional force)
  6477. "Toggle the visibility of the current block.
  6478. When optional argument FORCE is `off', make block visible. If it
  6479. is non-nil, hide it unconditionally. Throw an error when not at
  6480. a block. Return a non-nil value when toggling is successful."
  6481. (interactive)
  6482. (let ((element (org-element-at-point)))
  6483. (unless (memq (org-element-type element)
  6484. '(center-block comment-block dynamic-block example-block
  6485. export-block quote-block special-block
  6486. src-block verse-block))
  6487. (user-error "Not at a block"))
  6488. (let* ((start (save-excursion
  6489. (goto-char (org-element-property :post-affiliated element))
  6490. (line-end-position)))
  6491. (end (save-excursion
  6492. (goto-char (org-element-property :end element))
  6493. (skip-chars-backward " \r\t\n")
  6494. (line-end-position)))
  6495. (overlays (overlays-at start)))
  6496. (cond
  6497. ;; Do nothing when not before or at the block opening line or
  6498. ;; at the block closing line.
  6499. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6500. ((and (not (eq force 'off))
  6501. (not (memq t (mapcar
  6502. (lambda (o)
  6503. (eq (overlay-get o 'invisible) 'org-hide-block))
  6504. overlays))))
  6505. (let ((ov (make-overlay start end)))
  6506. (overlay-put ov 'invisible 'org-hide-block)
  6507. ;; Make the block accessible to `isearch'.
  6508. (overlay-put
  6509. ov 'isearch-open-invisible
  6510. (lambda (ov)
  6511. (when (memq ov org-hide-block-overlays)
  6512. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6513. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6514. (delete-overlay ov))))
  6515. (push ov org-hide-block-overlays)
  6516. ;; When the block is hidden away, make sure point is left in
  6517. ;; a visible part of the buffer.
  6518. (when (> (line-beginning-position) start)
  6519. (goto-char start)
  6520. (beginning-of-line))
  6521. ;; Signal successful toggling.
  6522. t))
  6523. ((or (not force) (eq force 'off))
  6524. (dolist (ov overlays t)
  6525. (when (memq ov org-hide-block-overlays)
  6526. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6527. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6528. (delete-overlay ov))))))))
  6529. ;; Remove overlays when changing major mode
  6530. (add-hook 'org-mode-hook
  6531. (lambda () (add-hook 'change-major-mode-hook
  6532. 'org-show-block-all 'append 'local)))
  6533. ;;; Org-goto
  6534. (defvar org-goto-window-configuration nil)
  6535. (defvar org-goto-marker nil)
  6536. (defvar org-goto-map)
  6537. (defun org-goto-map ()
  6538. "Set the keymap `org-goto'."
  6539. (setq org-goto-map
  6540. (let ((map (make-sparse-keymap)))
  6541. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6542. mouse-drag-region universal-argument org-occur)))
  6543. (dolist (cmd cmds)
  6544. (substitute-key-definition cmd cmd map global-map)))
  6545. (suppress-keymap map)
  6546. (org-defkey map "\C-m" 'org-goto-ret)
  6547. (org-defkey map [(return)] 'org-goto-ret)
  6548. (org-defkey map [(left)] 'org-goto-left)
  6549. (org-defkey map [(right)] 'org-goto-right)
  6550. (org-defkey map [(control ?g)] 'org-goto-quit)
  6551. (org-defkey map "\C-i" 'org-cycle)
  6552. (org-defkey map [(tab)] 'org-cycle)
  6553. (org-defkey map [(down)] 'outline-next-visible-heading)
  6554. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6555. (if org-goto-auto-isearch
  6556. (if (fboundp 'define-key-after)
  6557. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6558. nil)
  6559. (org-defkey map "q" 'org-goto-quit)
  6560. (org-defkey map "n" 'outline-next-visible-heading)
  6561. (org-defkey map "p" 'outline-previous-visible-heading)
  6562. (org-defkey map "f" 'outline-forward-same-level)
  6563. (org-defkey map "b" 'outline-backward-same-level)
  6564. (org-defkey map "u" 'outline-up-heading))
  6565. (org-defkey map "/" 'org-occur)
  6566. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6567. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6568. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6569. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6570. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6571. map)))
  6572. (defconst org-goto-help
  6573. "Browse buffer copy, to find location or copy text.%s
  6574. RET=jump to location C-g=quit and return to previous location
  6575. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6576. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6577. (defun org-goto (&optional alternative-interface)
  6578. "Look up a different location in the current file, keeping current visibility.
  6579. When you want look-up or go to a different location in a
  6580. document, the fastest way is often to fold the entire buffer and
  6581. then dive into the tree. This method has the disadvantage, that
  6582. the previous location will be folded, which may not be what you
  6583. want.
  6584. This command works around this by showing a copy of the current
  6585. buffer in an indirect buffer, in overview mode. You can dive
  6586. into the tree in that copy, use org-occur and incremental search
  6587. to find a location. When pressing RET or `Q', the command
  6588. returns to the original buffer in which the visibility is still
  6589. unchanged. After RET it will also jump to the location selected
  6590. in the indirect buffer and expose the headline hierarchy above.
  6591. With a prefix argument, use the alternative interface: e.g., if
  6592. `org-goto-interface' is `outline' use `outline-path-completion'."
  6593. (interactive "P")
  6594. (org-goto-map)
  6595. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6596. (org-refile-use-outline-path t)
  6597. (org-refile-target-verify-function nil)
  6598. (interface
  6599. (if (not alternative-interface)
  6600. org-goto-interface
  6601. (if (eq org-goto-interface 'outline)
  6602. 'outline-path-completion
  6603. 'outline)))
  6604. (org-goto-start-pos (point))
  6605. (selected-point
  6606. (if (eq interface 'outline)
  6607. (car (org-get-location (current-buffer) org-goto-help))
  6608. (let ((pa (org-refile-get-location "Goto")))
  6609. (org-refile-check-position pa)
  6610. (nth 3 pa)))))
  6611. (if selected-point
  6612. (progn
  6613. (org-mark-ring-push org-goto-start-pos)
  6614. (goto-char selected-point)
  6615. (when (or (org-invisible-p) (org-invisible-p2))
  6616. (org-show-context 'org-goto)))
  6617. (message "Quit"))))
  6618. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6619. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6620. (defvar org-goto-local-auto-isearch-map) ; defined below
  6621. (defun org-get-location (_buf help)
  6622. "Let the user select a location in current buffer.
  6623. This function uses a recursive edit. It returns the selected position
  6624. or nil."
  6625. (org-no-popups
  6626. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6627. (isearch-hide-immediately nil)
  6628. (isearch-search-fun-function
  6629. (lambda () 'org-goto-local-search-headings))
  6630. (org-goto-selected-point org-goto-exit-command))
  6631. (save-excursion
  6632. (save-window-excursion
  6633. (delete-other-windows)
  6634. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6635. (pop-to-buffer-same-window
  6636. (condition-case nil
  6637. (make-indirect-buffer (current-buffer) "*org-goto*")
  6638. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6639. (with-output-to-temp-buffer "*Org Help*"
  6640. (princ (format help (if org-goto-auto-isearch
  6641. " Just type for auto-isearch."
  6642. " n/p/f/b/u to navigate, q to quit."))))
  6643. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6644. (setq buffer-read-only nil)
  6645. (let ((org-startup-truncated t)
  6646. (org-startup-folded nil)
  6647. (org-startup-align-all-tables nil))
  6648. (org-mode)
  6649. (org-overview))
  6650. (setq buffer-read-only t)
  6651. (if (and (boundp 'org-goto-start-pos)
  6652. (integer-or-marker-p org-goto-start-pos))
  6653. (progn (goto-char org-goto-start-pos)
  6654. (when (org-invisible-p)
  6655. (org-show-set-visibility 'lineage)))
  6656. (goto-char (point-min)))
  6657. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6658. (message "Select location and press RET")
  6659. (use-local-map org-goto-map)
  6660. (recursive-edit)))
  6661. (kill-buffer "*org-goto*")
  6662. (cons org-goto-selected-point org-goto-exit-command))))
  6663. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6664. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6665. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6666. (if (fboundp 'isearch-other-control-char)
  6667. (progn
  6668. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6669. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6670. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6671. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6672. (define-key org-goto-local-auto-isearch-map [return] nil))
  6673. (defun org-goto-local-search-headings (string bound noerror)
  6674. "Search and make sure that any matches are in headlines."
  6675. (catch 'return
  6676. (while (if isearch-forward
  6677. (search-forward string bound noerror)
  6678. (search-backward string bound noerror))
  6679. (when (save-match-data
  6680. (and (save-excursion
  6681. (beginning-of-line)
  6682. (looking-at org-complex-heading-regexp))
  6683. (or (not (match-beginning 5))
  6684. (< (point) (match-beginning 5)))))
  6685. (throw 'return (point))))))
  6686. (defun org-goto-local-auto-isearch ()
  6687. "Start isearch."
  6688. (interactive)
  6689. (goto-char (point-min))
  6690. (let ((keys (this-command-keys)))
  6691. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6692. (isearch-mode t)
  6693. (isearch-process-search-char (string-to-char keys)))))
  6694. (defun org-goto-ret (&optional _arg)
  6695. "Finish `org-goto' by going to the new location."
  6696. (interactive "P")
  6697. (setq org-goto-selected-point (point))
  6698. (setq org-goto-exit-command 'return)
  6699. (throw 'exit nil))
  6700. (defun org-goto-left ()
  6701. "Finish `org-goto' by going to the new location."
  6702. (interactive)
  6703. (if (org-at-heading-p)
  6704. (progn
  6705. (beginning-of-line 1)
  6706. (setq org-goto-selected-point (point)
  6707. org-goto-exit-command 'left)
  6708. (throw 'exit nil))
  6709. (user-error "Not on a heading")))
  6710. (defun org-goto-right ()
  6711. "Finish `org-goto' by going to the new location."
  6712. (interactive)
  6713. (if (org-at-heading-p)
  6714. (progn
  6715. (setq org-goto-selected-point (point)
  6716. org-goto-exit-command 'right)
  6717. (throw 'exit nil))
  6718. (user-error "Not on a heading")))
  6719. (defun org-goto-quit ()
  6720. "Finish `org-goto' without cursor motion."
  6721. (interactive)
  6722. (setq org-goto-selected-point nil)
  6723. (setq org-goto-exit-command 'quit)
  6724. (throw 'exit nil))
  6725. ;;; Indirect buffer display of subtrees
  6726. (defvar org-indirect-dedicated-frame nil
  6727. "This is the frame being used for indirect tree display.")
  6728. (defvar org-last-indirect-buffer nil)
  6729. (defun org-tree-to-indirect-buffer (&optional arg)
  6730. "Create indirect buffer and narrow it to current subtree.
  6731. With a numerical prefix ARG, go up to this level and then take that tree.
  6732. If ARG is negative, go up that many levels.
  6733. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6734. indirect buffer previously made with this command, to avoid proliferation of
  6735. indirect buffers. However, when you call the command with a \
  6736. `\\[universal-argument]' prefix, or
  6737. when `org-indirect-buffer-display' is `new-frame', the last buffer is kept
  6738. so that you can work with several indirect buffers at the same time. If
  6739. `org-indirect-buffer-display' is `dedicated-frame', the \
  6740. `\\[universal-argument]' prefix also
  6741. requests that a new frame be made for the new buffer, so that the dedicated
  6742. frame is not changed."
  6743. (interactive "P")
  6744. (let ((cbuf (current-buffer))
  6745. (cwin (selected-window))
  6746. (pos (point))
  6747. beg end level heading ibuf)
  6748. (save-excursion
  6749. (org-back-to-heading t)
  6750. (when (numberp arg)
  6751. (setq level (org-outline-level))
  6752. (when (< arg 0) (setq arg (+ level arg)))
  6753. (while (> (setq level (org-outline-level)) arg)
  6754. (org-up-heading-safe)))
  6755. (setq beg (point)
  6756. heading (org-get-heading 'no-tags))
  6757. (org-end-of-subtree t t)
  6758. (when (org-at-heading-p) (backward-char 1))
  6759. (setq end (point)))
  6760. (when (and (buffer-live-p org-last-indirect-buffer)
  6761. (not (eq org-indirect-buffer-display 'new-frame))
  6762. (not arg))
  6763. (kill-buffer org-last-indirect-buffer))
  6764. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6765. org-last-indirect-buffer ibuf)
  6766. (cond
  6767. ((or (eq org-indirect-buffer-display 'new-frame)
  6768. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6769. (select-frame (make-frame))
  6770. (delete-other-windows)
  6771. (pop-to-buffer-same-window ibuf)
  6772. (org-set-frame-title heading))
  6773. ((eq org-indirect-buffer-display 'dedicated-frame)
  6774. (raise-frame
  6775. (select-frame (or (and org-indirect-dedicated-frame
  6776. (frame-live-p org-indirect-dedicated-frame)
  6777. org-indirect-dedicated-frame)
  6778. (setq org-indirect-dedicated-frame (make-frame)))))
  6779. (delete-other-windows)
  6780. (pop-to-buffer-same-window ibuf)
  6781. (org-set-frame-title (concat "Indirect: " heading)))
  6782. ((eq org-indirect-buffer-display 'current-window)
  6783. (pop-to-buffer-same-window ibuf))
  6784. ((eq org-indirect-buffer-display 'other-window)
  6785. (pop-to-buffer ibuf))
  6786. (t (error "Invalid value")))
  6787. (narrow-to-region beg end)
  6788. (outline-show-all)
  6789. (goto-char pos)
  6790. (run-hook-with-args 'org-cycle-hook 'all)
  6791. (and (window-live-p cwin) (select-window cwin))))
  6792. (defun org-get-indirect-buffer (&optional buffer heading)
  6793. (setq buffer (or buffer (current-buffer)))
  6794. (let ((n 1) (base (buffer-name buffer)) bname)
  6795. (while (buffer-live-p
  6796. (get-buffer
  6797. (setq bname
  6798. (concat base "-"
  6799. (if heading (concat heading "-" (number-to-string n))
  6800. (number-to-string n))))))
  6801. (setq n (1+ n)))
  6802. (condition-case nil
  6803. (make-indirect-buffer buffer bname 'clone)
  6804. (error (make-indirect-buffer buffer bname)))))
  6805. (defun org-set-frame-title (title)
  6806. "Set the title of the current frame to the string TITLE."
  6807. (modify-frame-parameters (selected-frame) (list (cons 'name title))))
  6808. ;;;; Structure editing
  6809. ;;; Inserting headlines
  6810. (defun org--line-empty-p (n)
  6811. "Is the Nth next line empty?
  6812. Counts the current line as N = 1 and the previous line as N = 0;
  6813. see `beginning-of-line'."
  6814. (save-excursion
  6815. (and (not (bobp))
  6816. (or (beginning-of-line n) t)
  6817. (save-match-data
  6818. (looking-at "[ \t]*$")))))
  6819. (defun org-previous-line-empty-p ()
  6820. "Is the previous line a blank line?
  6821. When NEXT is non-nil, check the next line instead."
  6822. (org--line-empty-p 0))
  6823. (defun org-next-line-empty-p ()
  6824. "Is the previous line a blank line?
  6825. When NEXT is non-nil, check the next line instead."
  6826. (org--line-empty-p 2))
  6827. (defun org--blank-before-heading-p (&optional parent)
  6828. "Non-nil when an empty line should precede a new heading here.
  6829. When optional argument PARENT is non-nil, consider parent
  6830. headline instead of current one."
  6831. (pcase (assq 'heading org-blank-before-new-entry)
  6832. (`(heading . auto)
  6833. (save-excursion
  6834. (org-with-limited-levels
  6835. (unless (and (org-before-first-heading-p)
  6836. (not (outline-next-heading)))
  6837. (org-back-to-heading t)
  6838. (when parent (org-up-heading-safe))
  6839. (cond ((not (bobp))
  6840. (org-previous-line-empty-p))
  6841. ((outline-next-heading)
  6842. (org-previous-line-empty-p))
  6843. ;; Ignore trailing spaces on last buffer line.
  6844. ((progn (skip-chars-backward " \t") (bolp))
  6845. (org-previous-line-empty-p))
  6846. (t nil))))))
  6847. (`(heading . ,value) value)
  6848. (_ nil)))
  6849. (defun org-insert-heading (&optional arg invisible-ok top)
  6850. "Insert a new heading or an item with the same depth at point.
  6851. If point is at the beginning of a heading, insert a new heading
  6852. or a new headline above the current one. When at the beginning
  6853. of a regular line of text, turn it into a heading.
  6854. If point is in the middle of a line, split it and create a new
  6855. headline with the text in the current line after point (see
  6856. `org-M-RET-may-split-line' on how to modify this behavior). As
  6857. a special case, on a headline, splitting can only happen on the
  6858. title itself. E.g., this excludes breaking stars or tags.
  6859. With a `\\[universal-argument]' prefix, set \
  6860. `org-insert-heading-respect-content' to
  6861. a non-nil value for the duration of the command. This forces the
  6862. insertion of a heading after the current subtree, independently
  6863. on the location of point.
  6864. With a `\\[universal-argument] \\[universal-argument]' prefix, \
  6865. insert the heading at the end of the tree
  6866. above the current heading. For example, if point is within a
  6867. 2nd-level heading, then it will insert a 2nd-level heading at
  6868. the end of the 1st-level parent subtree.
  6869. When INVISIBLE-OK is set, stop at invisible headlines when going
  6870. back. This is important for non-interactive uses of the
  6871. command.
  6872. When optional argument TOP is non-nil, insert a level 1 heading,
  6873. unconditionally."
  6874. (interactive "P")
  6875. (let* ((blank? (org--blank-before-heading-p (equal arg '(16))))
  6876. (level (org-current-level))
  6877. (stars (make-string (if (and level (not top)) level 1) ?*)))
  6878. (cond
  6879. ((or org-insert-heading-respect-content
  6880. (member arg '((4) (16)))
  6881. (and (not invisible-ok)
  6882. (invisible-p (max (1- (point)) (point-min)))))
  6883. ;; Position point at the location of insertion.
  6884. (if (not level) ;before first headline
  6885. (org-with-limited-levels (outline-next-heading))
  6886. ;; Make sure we end up on a visible headline if INVISIBLE-OK
  6887. ;; is nil.
  6888. (org-with-limited-levels (org-back-to-heading invisible-ok))
  6889. (cond ((equal arg '(16))
  6890. (org-up-heading-safe)
  6891. (org-end-of-subtree t t))
  6892. (t
  6893. (org-end-of-subtree t t))))
  6894. (unless (bolp) (insert "\n")) ;ensure final newline
  6895. (unless (and blank? (org-previous-line-empty-p))
  6896. (org-N-empty-lines-before-current (if blank? 1 0)))
  6897. (insert stars " \n")
  6898. (forward-char -1))
  6899. ;; At a headline...
  6900. ((org-at-heading-p)
  6901. (cond ((bolp)
  6902. (when blank? (save-excursion (insert "\n")))
  6903. (save-excursion (insert stars " \n"))
  6904. (unless (and blank? (org-previous-line-empty-p))
  6905. (org-N-empty-lines-before-current (if blank? 1 0)))
  6906. (end-of-line))
  6907. ((and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6908. (org-match-line org-complex-heading-regexp)
  6909. (org-pos-in-match-range (point) 4))
  6910. ;; Grab the text that should moved to the new headline.
  6911. ;; Preserve tags.
  6912. (let ((split (delete-and-extract-region (point) (match-end 4))))
  6913. (if (looking-at "[ \t]*$") (replace-match "")
  6914. (org-set-tags nil t))
  6915. (end-of-line)
  6916. (when blank? (insert "\n"))
  6917. (insert "\n" stars " ")
  6918. (when (org-string-nw-p split) (insert split))
  6919. (when (eobp) (save-excursion (insert "\n")))))
  6920. (t
  6921. (end-of-line)
  6922. (when blank? (insert "\n"))
  6923. (insert "\n" stars " ")
  6924. (when (eobp) (save-excursion (insert "\n"))))))
  6925. ;; On regular text, turn line into a headline or split, if
  6926. ;; appropriate.
  6927. ((bolp)
  6928. (insert stars " ")
  6929. (unless (and blank? (org-previous-line-empty-p))
  6930. (org-N-empty-lines-before-current (if blank? 1 0))))
  6931. (t
  6932. (unless (org-get-alist-option org-M-RET-may-split-line 'headline)
  6933. (end-of-line))
  6934. (insert "\n" stars " ")
  6935. (unless (and blank? (org-previous-line-empty-p))
  6936. (org-N-empty-lines-before-current (if blank? 1 0))))))
  6937. (run-hooks 'org-insert-heading-hook))
  6938. (defun org-N-empty-lines-before-current (n)
  6939. "Make the number of empty lines before current exactly N.
  6940. So this will delete or add empty lines."
  6941. (let ((column (current-column)))
  6942. (beginning-of-line)
  6943. (unless (bobp)
  6944. (let ((start (save-excursion
  6945. (skip-chars-backward " \r\t\n")
  6946. (line-end-position))))
  6947. (delete-region start (line-end-position 0))))
  6948. (insert (make-string n ?\n))
  6949. (move-to-column column)))
  6950. (defun org-get-heading (&optional no-tags no-todo no-priority no-comment)
  6951. "Return the heading of the current entry, without the stars.
  6952. When NO-TAGS is non-nil, don't include tags.
  6953. When NO-TODO is non-nil, don't include TODO keywords.
  6954. When NO-PRIORITY is non-nil, don't include priority cookie.
  6955. When NO-COMMENT is non-nil, don't include COMMENT string."
  6956. (save-excursion
  6957. (org-back-to-heading t)
  6958. (let ((case-fold-search nil))
  6959. (looking-at org-complex-heading-regexp)
  6960. (let ((todo (and (not no-todo) (match-string 2)))
  6961. (priority (and (not no-priority) (match-string 3)))
  6962. (headline (pcase (match-string 4)
  6963. (`nil "")
  6964. ((and (guard no-comment) h)
  6965. (replace-regexp-in-string
  6966. (eval-when-compile
  6967. (format "\\`%s[ \t]+" org-comment-string))
  6968. "" h))
  6969. (h h)))
  6970. (tags (and (not no-tags) (match-string 5))))
  6971. (mapconcat #'identity
  6972. (delq nil (list todo priority headline tags))
  6973. " ")))))
  6974. (defvar orgstruct-mode) ; defined below
  6975. (defun org-heading-components ()
  6976. "Return the components of the current heading.
  6977. This is a list with the following elements:
  6978. - the level as an integer
  6979. - the reduced level, different if `org-odd-levels-only' is set.
  6980. - the TODO keyword, or nil
  6981. - the priority character, like ?A, or nil if no priority is given
  6982. - the headline text itself, or the tags string if no headline text
  6983. - the tags string, or nil."
  6984. (save-excursion
  6985. (org-back-to-heading t)
  6986. (when (let (case-fold-search)
  6987. (looking-at
  6988. (if orgstruct-mode
  6989. org-heading-regexp
  6990. org-complex-heading-regexp)))
  6991. (if orgstruct-mode
  6992. (list (length (match-string 1))
  6993. (org-reduced-level (length (match-string 1)))
  6994. nil
  6995. nil
  6996. (match-string 2)
  6997. nil)
  6998. (list (length (match-string 1))
  6999. (org-reduced-level (length (match-string 1)))
  7000. (match-string-no-properties 2)
  7001. (and (match-end 3) (aref (match-string 3) 2))
  7002. (match-string-no-properties 4)
  7003. (match-string-no-properties 5))))))
  7004. (defun org-get-entry ()
  7005. "Get the entry text, after heading, entire subtree."
  7006. (save-excursion
  7007. (org-back-to-heading t)
  7008. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7009. (defun org-edit-headline (&optional heading)
  7010. "Edit the current headline.
  7011. Set it to HEADING when provided."
  7012. (interactive)
  7013. (org-with-wide-buffer
  7014. (org-back-to-heading t)
  7015. (let ((case-fold-search nil))
  7016. (when (looking-at org-complex-heading-regexp)
  7017. (let* ((old (match-string-no-properties 4))
  7018. (new (save-match-data
  7019. (org-trim (or heading (read-string "Edit: " old))))))
  7020. (unless (equal old new)
  7021. (if old (replace-match new t t nil 4)
  7022. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7023. (insert " " new))
  7024. (org-set-tags nil t)
  7025. (when (looking-at "[ \t]*$") (replace-match ""))))))))
  7026. (defun org-insert-heading-after-current ()
  7027. "Insert a new heading with same level as current, after current subtree."
  7028. (interactive)
  7029. (org-back-to-heading)
  7030. (org-insert-heading)
  7031. (org-move-subtree-down)
  7032. (end-of-line 1))
  7033. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7034. "Insert heading with `org-insert-heading-respect-content' set to t."
  7035. (interactive)
  7036. (org-insert-heading '(4) invisible-ok))
  7037. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7038. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7039. (interactive)
  7040. (org-insert-todo-heading force-state '(4)))
  7041. (defun org-insert-todo-heading (arg &optional force-heading)
  7042. "Insert a new heading with the same level and TODO state as current heading.
  7043. If the heading has no TODO state, or if the state is DONE, use
  7044. the first state (TODO by default). Also with one prefix arg,
  7045. force first state. With two prefix args, force inserting at the
  7046. end of the parent subtree.
  7047. When called at a plain list item, insert a new item with an
  7048. unchecked check box."
  7049. (interactive "P")
  7050. (when (or force-heading (not (org-insert-item 'checkbox)))
  7051. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7052. force-heading))
  7053. (save-excursion
  7054. (org-forward-heading-same-level -1)
  7055. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp)))
  7056. (let* ((new-mark-x
  7057. (if (or (equal arg '(4))
  7058. (not (match-beginning 2))
  7059. (member (match-string 2) org-done-keywords))
  7060. (car org-todo-keywords-1)
  7061. (match-string 2)))
  7062. (new-mark
  7063. (or
  7064. (run-hook-with-args-until-success
  7065. 'org-todo-get-default-hook new-mark-x nil)
  7066. new-mark-x)))
  7067. (beginning-of-line 1)
  7068. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7069. (if org-treat-insert-todo-heading-as-state-change
  7070. (org-todo new-mark)
  7071. (insert new-mark " "))))
  7072. (when org-provide-todo-statistics
  7073. (org-update-parent-todo-statistics))))
  7074. (defun org-insert-subheading (arg)
  7075. "Insert a new subheading and demote it.
  7076. Works for outline headings and for plain lists alike."
  7077. (interactive "P")
  7078. (org-insert-heading arg)
  7079. (cond
  7080. ((org-at-heading-p) (org-do-demote))
  7081. ((org-at-item-p) (org-indent-item))))
  7082. (defun org-insert-todo-subheading (arg)
  7083. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7084. Works for outline headings and for plain lists alike."
  7085. (interactive "P")
  7086. (org-insert-todo-heading arg)
  7087. (cond
  7088. ((org-at-heading-p) (org-do-demote))
  7089. ((org-at-item-p) (org-indent-item))))
  7090. ;;; Promotion and Demotion
  7091. (defvar org-after-demote-entry-hook nil
  7092. "Hook run after an entry has been demoted.
  7093. The cursor will be at the beginning of the entry.
  7094. When a subtree is being demoted, the hook will be called for each node.")
  7095. (defvar org-after-promote-entry-hook nil
  7096. "Hook run after an entry has been promoted.
  7097. The cursor will be at the beginning of the entry.
  7098. When a subtree is being promoted, the hook will be called for each node.")
  7099. (defun org-promote-subtree ()
  7100. "Promote the entire subtree.
  7101. See also `org-promote'."
  7102. (interactive)
  7103. (save-excursion
  7104. (org-with-limited-levels (org-map-tree 'org-promote)))
  7105. (org-fix-position-after-promote))
  7106. (defun org-demote-subtree ()
  7107. "Demote the entire subtree.
  7108. See `org-demote' and `org-promote'."
  7109. (interactive)
  7110. (save-excursion
  7111. (org-with-limited-levels (org-map-tree 'org-demote)))
  7112. (org-fix-position-after-promote))
  7113. (defun org-do-promote ()
  7114. "Promote the current heading higher up the tree.
  7115. If the region is active in `transient-mark-mode', promote all
  7116. headings in the region."
  7117. (interactive)
  7118. (save-excursion
  7119. (if (org-region-active-p)
  7120. (org-map-region 'org-promote (region-beginning) (region-end))
  7121. (org-promote)))
  7122. (org-fix-position-after-promote))
  7123. (defun org-do-demote ()
  7124. "Demote the current heading lower down the tree.
  7125. If the region is active in `transient-mark-mode', demote all
  7126. headings in the region."
  7127. (interactive)
  7128. (save-excursion
  7129. (if (org-region-active-p)
  7130. (org-map-region 'org-demote (region-beginning) (region-end))
  7131. (org-demote)))
  7132. (org-fix-position-after-promote))
  7133. (defun org-fix-position-after-promote ()
  7134. "Fix cursor position and indentation after demoting/promoting."
  7135. (let ((pos (point)))
  7136. (when (save-excursion
  7137. (beginning-of-line)
  7138. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  7139. (or (eq pos (match-end 1)) (eq pos (match-end 2))))
  7140. (cond ((eobp) (insert " "))
  7141. ((eolp) (insert " "))
  7142. ((equal (char-after) ?\s) (forward-char 1))))))
  7143. (defun org-current-level ()
  7144. "Return the level of the current entry, or nil if before the first headline.
  7145. The level is the number of stars at the beginning of the
  7146. headline. Use `org-reduced-level' to remove the effect of
  7147. `org-odd-levels'. Unlike to `org-outline-level', this function
  7148. ignores inlinetasks."
  7149. (let ((level (org-with-limited-levels (org-outline-level))))
  7150. (and (> level 0) level)))
  7151. (defun org-get-previous-line-level ()
  7152. "Return the outline depth of the last headline before the current line.
  7153. Returns 0 for the first headline in the buffer, and nil if before the
  7154. first headline."
  7155. (and (org-current-level)
  7156. (or (and (/= (line-beginning-position) (point-min))
  7157. (save-excursion (beginning-of-line 0) (org-current-level)))
  7158. 0)))
  7159. (defun org-reduced-level (l)
  7160. "Compute the effective level of a heading.
  7161. This takes into account the setting of `org-odd-levels-only'."
  7162. (cond
  7163. ((zerop l) 0)
  7164. (org-odd-levels-only (1+ (floor (/ l 2))))
  7165. (t l)))
  7166. (defun org-level-increment ()
  7167. "Return the number of stars that will be added or removed at a
  7168. time to headlines when structure editing, based on the value of
  7169. `org-odd-levels-only'."
  7170. (if org-odd-levels-only 2 1))
  7171. (defun org-get-valid-level (level &optional change)
  7172. "Rectify a level change under the influence of `org-odd-levels-only'.
  7173. LEVEL is a current level, CHANGE is by how much the level should
  7174. be modified. Even if CHANGE is nil, LEVEL may be returned
  7175. modified because even level numbers will become the next higher
  7176. odd number. Returns values greater than 0."
  7177. (if org-odd-levels-only
  7178. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7179. ((> change 0) (1+ (* 2 (/ (+ (1- level) (* 2 change)) 2))))
  7180. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7181. (max 1 (+ level (or change 0)))))
  7182. (defun org-promote ()
  7183. "Promote the current heading higher up 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. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7190. (diff (abs (- level (length up-head) -1))))
  7191. (cond
  7192. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7193. (replace-match "# " nil t))
  7194. ((= level 1)
  7195. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7196. (t (replace-match up-head nil t)))
  7197. (unless (= level 1)
  7198. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7199. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7200. (run-hooks 'org-after-promote-entry-hook))))
  7201. (defun org-demote ()
  7202. "Demote the current heading lower down the tree."
  7203. (org-with-wide-buffer
  7204. (org-back-to-heading t)
  7205. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7206. after-change-functions))
  7207. (level (save-match-data (funcall outline-level)))
  7208. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7209. (diff (abs (- level (length down-head) -1))))
  7210. (replace-match down-head nil t)
  7211. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7212. (when org-adapt-indentation (org-fixup-indentation diff))
  7213. (run-hooks 'org-after-demote-entry-hook))))
  7214. (defun org-cycle-level ()
  7215. "Cycle the level of an empty headline through possible states.
  7216. This goes first to child, then to parent, level, then up the hierarchy.
  7217. After top level, it switches back to sibling level."
  7218. (interactive)
  7219. (let ((org-adapt-indentation nil))
  7220. (when (org-point-at-end-of-empty-headline)
  7221. (setq this-command 'org-cycle-level) ; Only needed for caching
  7222. (let ((cur-level (org-current-level))
  7223. (prev-level (org-get-previous-line-level)))
  7224. (cond
  7225. ;; If first headline in file, promote to top-level.
  7226. ((= prev-level 0)
  7227. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7228. do (org-do-promote)))
  7229. ;; If same level as prev, demote one.
  7230. ((= prev-level cur-level)
  7231. (org-do-demote))
  7232. ;; If parent is top-level, promote to top level if not already.
  7233. ((= prev-level 1)
  7234. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7235. do (org-do-promote)))
  7236. ;; If top-level, return to prev-level.
  7237. ((= cur-level 1)
  7238. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7239. do (org-do-demote)))
  7240. ;; If less than prev-level, promote one.
  7241. ((< cur-level prev-level)
  7242. (org-do-promote))
  7243. ;; If deeper than prev-level, promote until higher than
  7244. ;; prev-level.
  7245. ((> cur-level prev-level)
  7246. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7247. do (org-do-promote))))
  7248. t))))
  7249. (defun org-map-tree (fun)
  7250. "Call FUN for every heading underneath the current one."
  7251. (org-back-to-heading t)
  7252. (let ((level (funcall outline-level)))
  7253. (save-excursion
  7254. (funcall fun)
  7255. (while (and (progn
  7256. (outline-next-heading)
  7257. (> (funcall outline-level) level))
  7258. (not (eobp)))
  7259. (funcall fun)))))
  7260. (defun org-map-region (fun beg end)
  7261. "Call FUN for every heading between BEG and END."
  7262. (let ((org-ignore-region t))
  7263. (save-excursion
  7264. (setq end (copy-marker end))
  7265. (goto-char beg)
  7266. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7267. (< (point) end))
  7268. (funcall fun))
  7269. (while (and (progn
  7270. (outline-next-heading)
  7271. (< (point) end))
  7272. (not (eobp)))
  7273. (funcall fun)))))
  7274. (defun org-fixup-indentation (diff)
  7275. "Change the indentation in the current entry by DIFF.
  7276. DIFF is an integer. Indentation is done according to the
  7277. following rules:
  7278. - Planning information and property drawers are always indented
  7279. according to the new level of the headline;
  7280. - Footnote definitions and their contents are ignored;
  7281. - Inlinetasks' boundaries are not shifted;
  7282. - Empty lines are ignored;
  7283. - Other lines' indentation are shifted by DIFF columns, unless
  7284. it would introduce a structural change in the document, in
  7285. which case no shifting is done at all.
  7286. Assume point is at a heading or an inlinetask beginning."
  7287. (org-with-wide-buffer
  7288. (narrow-to-region (line-beginning-position)
  7289. (save-excursion
  7290. (if (org-with-limited-levels (org-at-heading-p))
  7291. (org-with-limited-levels (outline-next-heading))
  7292. (org-inlinetask-goto-end))
  7293. (point)))
  7294. (forward-line)
  7295. ;; Indent properly planning info and property drawer.
  7296. (when (looking-at-p org-planning-line-re)
  7297. (org-indent-line)
  7298. (forward-line))
  7299. (when (looking-at org-property-drawer-re)
  7300. (goto-char (match-end 0))
  7301. (forward-line)
  7302. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7303. (catch 'no-shift
  7304. (when (zerop diff) (throw 'no-shift nil))
  7305. ;; If DIFF is negative, first check if a shift is possible at all
  7306. ;; (e.g., it doesn't break structure). This can only happen if
  7307. ;; some contents are not properly indented.
  7308. (let ((case-fold-search t))
  7309. (when (< diff 0)
  7310. (let ((diff (- diff))
  7311. (forbidden-re (concat org-outline-regexp
  7312. "\\|"
  7313. (substring org-footnote-definition-re 1))))
  7314. (save-excursion
  7315. (while (not (eobp))
  7316. (cond
  7317. ((looking-at-p "[ \t]*$") (forward-line))
  7318. ((and (looking-at-p org-footnote-definition-re)
  7319. (let ((e (org-element-at-point)))
  7320. (and (eq (org-element-type e) 'footnote-definition)
  7321. (goto-char (org-element-property :end e))))))
  7322. ((looking-at-p org-outline-regexp) (forward-line))
  7323. ;; Give up if shifting would move before column 0 or
  7324. ;; if it would introduce a headline or a footnote
  7325. ;; definition.
  7326. (t
  7327. (skip-chars-forward " \t")
  7328. (let ((ind (current-column)))
  7329. (when (or (< ind diff)
  7330. (and (= ind diff) (looking-at-p forbidden-re)))
  7331. (throw 'no-shift nil)))
  7332. ;; Ignore contents of example blocks and source
  7333. ;; blocks if their indentation is meant to be
  7334. ;; preserved. Jump to block's closing line.
  7335. (beginning-of-line)
  7336. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7337. (let ((e (org-element-at-point)))
  7338. (and (memq (org-element-type e)
  7339. '(example-block src-block))
  7340. (or org-src-preserve-indentation
  7341. (org-element-property :preserve-indent e))
  7342. (goto-char (org-element-property :end e))
  7343. (progn (skip-chars-backward " \r\t\n")
  7344. (beginning-of-line)
  7345. t))))
  7346. (forward-line))))))))
  7347. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7348. ;; by DIFF. Also skip contents of source or example blocks
  7349. ;; when indentation is meant to be preserved.
  7350. (while (not (eobp))
  7351. (cond
  7352. ((and (looking-at-p org-footnote-definition-re)
  7353. (let ((e (org-element-at-point)))
  7354. (and (eq (org-element-type e) 'footnote-definition)
  7355. (goto-char (org-element-property :end e))))))
  7356. ((looking-at-p org-outline-regexp) (forward-line))
  7357. ((looking-at-p "[ \t]*$") (forward-line))
  7358. (t
  7359. (indent-line-to (+ (org-get-indentation) diff))
  7360. (beginning-of-line)
  7361. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7362. (let ((e (org-element-at-point)))
  7363. (and (memq (org-element-type e)
  7364. '(example-block src-block))
  7365. (or org-src-preserve-indentation
  7366. (org-element-property :preserve-indent e))
  7367. (goto-char (org-element-property :end e))
  7368. (progn (skip-chars-backward " \r\t\n")
  7369. (beginning-of-line)
  7370. t))))
  7371. (forward-line)))))))))
  7372. (defun org-convert-to-odd-levels ()
  7373. "Convert an Org file with all levels allowed to one with odd levels.
  7374. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7375. level 5 etc."
  7376. (interactive)
  7377. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7378. (let ((outline-level 'org-outline-level)
  7379. (org-odd-levels-only nil) n)
  7380. (save-excursion
  7381. (goto-char (point-min))
  7382. (while (re-search-forward "^\\*\\*+ " nil t)
  7383. (setq n (- (length (match-string 0)) 2))
  7384. (while (>= (setq n (1- n)) 0)
  7385. (org-demote))
  7386. (end-of-line 1))))))
  7387. (defun org-convert-to-oddeven-levels ()
  7388. "Convert an Org file with only odd levels to one with odd/even levels.
  7389. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7390. file contains a section with an even level, conversion would
  7391. destroy the structure of the file. An error is signaled in this
  7392. case."
  7393. (interactive)
  7394. (goto-char (point-min))
  7395. ;; First check if there are no even levels
  7396. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7397. (org-show-set-visibility 'canonical)
  7398. (error "Not all levels are odd in this file. Conversion not possible"))
  7399. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7400. (let ((outline-regexp org-outline-regexp)
  7401. (outline-level 'org-outline-level)
  7402. (org-odd-levels-only nil) n)
  7403. (save-excursion
  7404. (goto-char (point-min))
  7405. (while (re-search-forward "^\\*\\*+ " nil t)
  7406. (setq n (/ (1- (length (match-string 0))) 2))
  7407. (while (>= (setq n (1- n)) 0)
  7408. (org-promote))
  7409. (end-of-line 1))))))
  7410. (defun org-tr-level (n)
  7411. "Make N odd if required."
  7412. (if org-odd-levels-only (1+ (/ n 2)) n))
  7413. ;;; Vertical tree motion, cutting and pasting of subtrees
  7414. (defun org-move-subtree-up (&optional arg)
  7415. "Move the current subtree up past ARG headlines of the same level."
  7416. (interactive "p")
  7417. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7418. (defun org-move-subtree-down (&optional arg)
  7419. "Move the current subtree down past ARG headlines of the same level."
  7420. (interactive "p")
  7421. (setq arg (prefix-numeric-value arg))
  7422. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7423. 'org-get-last-sibling))
  7424. (ins-point (make-marker))
  7425. (cnt (abs arg))
  7426. (col (current-column))
  7427. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7428. ;; Select the tree
  7429. (org-back-to-heading)
  7430. (setq beg0 (point))
  7431. (save-excursion
  7432. (setq ne-beg (org-back-over-empty-lines))
  7433. (setq beg (point)))
  7434. (save-match-data
  7435. (save-excursion (outline-end-of-heading)
  7436. (setq folded (org-invisible-p)))
  7437. (progn (org-end-of-subtree nil t)
  7438. (unless (eobp) (backward-char))))
  7439. (outline-next-heading)
  7440. (setq ne-end (org-back-over-empty-lines))
  7441. (setq end (point))
  7442. (goto-char beg0)
  7443. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7444. ;; include less whitespace
  7445. (save-excursion
  7446. (goto-char beg)
  7447. (forward-line (- ne-beg ne-end))
  7448. (setq beg (point))))
  7449. ;; Find insertion point, with error handling
  7450. (while (> cnt 0)
  7451. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7452. (progn (goto-char beg0)
  7453. (user-error "Cannot move past superior level or buffer limit")))
  7454. (setq cnt (1- cnt)))
  7455. (when (> arg 0)
  7456. ;; Moving forward - still need to move over subtree
  7457. (org-end-of-subtree t t)
  7458. (save-excursion
  7459. (org-back-over-empty-lines)
  7460. (or (bolp) (newline))))
  7461. (setq ne-ins (org-back-over-empty-lines))
  7462. (move-marker ins-point (point))
  7463. (setq txt (buffer-substring beg end))
  7464. (org-save-markers-in-region beg end)
  7465. (delete-region beg end)
  7466. (org-remove-empty-overlays-at beg)
  7467. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7468. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7469. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7470. (let ((bbb (point)))
  7471. (insert-before-markers txt)
  7472. (org-reinstall-markers-in-region bbb)
  7473. (move-marker ins-point bbb))
  7474. (or (bolp) (insert "\n"))
  7475. (setq ins-end (point))
  7476. (goto-char ins-point)
  7477. (org-skip-whitespace)
  7478. (when (and (< arg 0)
  7479. (org-first-sibling-p)
  7480. (> ne-ins ne-beg))
  7481. ;; Move whitespace back to beginning
  7482. (save-excursion
  7483. (goto-char ins-end)
  7484. (let ((kill-whole-line t))
  7485. (kill-line (- ne-ins ne-beg)) (point)))
  7486. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7487. (move-marker ins-point nil)
  7488. (if folded
  7489. (outline-hide-subtree)
  7490. (org-show-entry)
  7491. (org-show-children)
  7492. (org-cycle-hide-drawers 'children))
  7493. (org-clean-visibility-after-subtree-move)
  7494. ;; move back to the initial column we were at
  7495. (move-to-column col)))
  7496. (defvar org-subtree-clip ""
  7497. "Clipboard for cut and paste of subtrees.
  7498. This is actually only a copy of the kill, because we use the normal kill
  7499. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7500. (defvar org-subtree-clip-folded nil
  7501. "Was the last copied subtree folded?
  7502. This is used to fold the tree back after pasting.")
  7503. (defun org-cut-subtree (&optional n)
  7504. "Cut the current subtree into the clipboard.
  7505. With prefix arg N, cut this many sequential subtrees.
  7506. This is a short-hand for marking the subtree and then cutting it."
  7507. (interactive "p")
  7508. (org-copy-subtree n 'cut))
  7509. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7510. "Copy the current subtree into the clipboard.
  7511. With prefix arg N, copy this many sequential subtrees.
  7512. This is a short-hand for marking the subtree and then copying it.
  7513. If CUT is non-nil, actually cut the subtree.
  7514. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7515. of some markers in the region, even if CUT is non-nil. This is
  7516. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7517. (interactive "p")
  7518. (let (beg end folded (beg0 (point)))
  7519. (if (called-interactively-p 'any)
  7520. (org-back-to-heading nil) ; take what looks like a subtree
  7521. (org-back-to-heading t)) ; take what is really there
  7522. (setq beg (point))
  7523. (skip-chars-forward " \t\r\n")
  7524. (save-match-data
  7525. (if nosubtrees
  7526. (outline-next-heading)
  7527. (save-excursion (outline-end-of-heading)
  7528. (setq folded (org-invisible-p)))
  7529. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7530. (org-end-of-subtree t t)))
  7531. ;; Include the end of an inlinetask
  7532. (when (and (featurep 'org-inlinetask)
  7533. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7534. "END[ \t]*$")))
  7535. (end-of-line))
  7536. (setq end (point))
  7537. (goto-char beg0)
  7538. (when (> end beg)
  7539. (setq org-subtree-clip-folded folded)
  7540. (when (or cut force-store-markers)
  7541. (org-save-markers-in-region beg end))
  7542. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7543. (setq org-subtree-clip (current-kill 0))
  7544. (message "%s: Subtree(s) with %d characters"
  7545. (if cut "Cut" "Copied")
  7546. (length org-subtree-clip)))))
  7547. (defun org-paste-subtree (&optional level tree for-yank remove)
  7548. "Paste the clipboard as a subtree, with modification of headline level.
  7549. The entire subtree is promoted or demoted in order to match a new headline
  7550. level.
  7551. If the cursor is at the beginning of a headline, the same level as
  7552. that headline is used to paste the tree.
  7553. If not, the new level is derived from the *visible* headings
  7554. before and after the insertion point, and taken to be the inferior headline
  7555. level of the two. So if the previous visible heading is level 3 and the
  7556. next is level 4 (or vice versa), level 4 will be used for insertion.
  7557. This makes sure that the subtree remains an independent subtree and does
  7558. not swallow low level entries.
  7559. You can also force a different level, either by using a numeric prefix
  7560. argument, or by inserting the heading marker by hand. For example, if the
  7561. cursor is after \"*****\", then the tree will be shifted to level 5.
  7562. If optional TREE is given, use this text instead of the kill ring.
  7563. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7564. move back over whitespace before inserting, and move point to the end of
  7565. the inserted text when done.
  7566. When REMOVE is non-nil, remove the subtree from the clipboard."
  7567. (interactive "P")
  7568. (setq tree (or tree (and kill-ring (current-kill 0))))
  7569. (unless (org-kill-is-subtree-p tree)
  7570. (user-error "%s"
  7571. (substitute-command-keys
  7572. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7573. (org-with-limited-levels
  7574. (let* ((visp (not (org-invisible-p)))
  7575. (txt tree)
  7576. (^re_ "\\(\\*+\\)[ \t]*")
  7577. (old-level (if (string-match org-outline-regexp-bol txt)
  7578. (- (match-end 0) (match-beginning 0) 1)
  7579. -1))
  7580. (force-level (cond (level (prefix-numeric-value level))
  7581. ((and (looking-at "[ \t]*$")
  7582. (string-match
  7583. "^\\*+$" (buffer-substring
  7584. (point-at-bol) (point))))
  7585. (- (match-end 0) (match-beginning 0)))
  7586. ((and (bolp)
  7587. (looking-at org-outline-regexp))
  7588. (- (match-end 0) (point) 1))))
  7589. (previous-level (save-excursion
  7590. (condition-case nil
  7591. (progn
  7592. (outline-previous-visible-heading 1)
  7593. (if (looking-at ^re_)
  7594. (- (match-end 0) (match-beginning 0) 1)
  7595. 1))
  7596. (error 1))))
  7597. (next-level (save-excursion
  7598. (condition-case nil
  7599. (progn
  7600. (or (looking-at org-outline-regexp)
  7601. (outline-next-visible-heading 1))
  7602. (if (looking-at ^re_)
  7603. (- (match-end 0) (match-beginning 0) 1)
  7604. 1))
  7605. (error 1))))
  7606. (new-level (or force-level (max previous-level next-level)))
  7607. (shift (if (or (= old-level -1)
  7608. (= new-level -1)
  7609. (= old-level new-level))
  7610. 0
  7611. (- new-level old-level)))
  7612. (delta (if (> shift 0) -1 1))
  7613. (func (if (> shift 0) 'org-demote 'org-promote))
  7614. (org-odd-levels-only nil)
  7615. beg end newend)
  7616. ;; Remove the forced level indicator
  7617. (when force-level
  7618. (delete-region (point-at-bol) (point)))
  7619. ;; Paste
  7620. (beginning-of-line (if (bolp) 1 2))
  7621. (setq beg (point))
  7622. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7623. (insert-before-markers txt)
  7624. (unless (string-suffix-p "\n" txt) (insert "\n"))
  7625. (setq newend (point))
  7626. (org-reinstall-markers-in-region beg)
  7627. (setq end (point))
  7628. (goto-char beg)
  7629. (skip-chars-forward " \t\n\r")
  7630. (setq beg (point))
  7631. (when (and (org-invisible-p) visp)
  7632. (save-excursion (outline-show-heading)))
  7633. ;; Shift if necessary
  7634. (unless (= shift 0)
  7635. (save-restriction
  7636. (narrow-to-region beg end)
  7637. (while (not (= shift 0))
  7638. (org-map-region func (point-min) (point-max))
  7639. (setq shift (+ delta shift)))
  7640. (goto-char (point-min))
  7641. (setq newend (point-max))))
  7642. (when (or (called-interactively-p 'interactive) for-yank)
  7643. (message "Clipboard pasted as level %d subtree" new-level))
  7644. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7645. kill-ring
  7646. (eq org-subtree-clip (current-kill 0))
  7647. org-subtree-clip-folded)
  7648. ;; The tree was folded before it was killed/copied
  7649. (outline-hide-subtree))
  7650. (and for-yank (goto-char newend))
  7651. (and remove (setq kill-ring (cdr kill-ring))))))
  7652. (defun org-kill-is-subtree-p (&optional txt)
  7653. "Check if the current kill is an outline subtree, or a set of trees.
  7654. Returns nil if kill does not start with a headline, or if the first
  7655. headline level is not the largest headline level in the tree.
  7656. So this will actually accept several entries of equal levels as well,
  7657. which is OK for `org-paste-subtree'.
  7658. If optional TXT is given, check this string instead of the current kill."
  7659. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7660. (re (org-get-limited-outline-regexp))
  7661. (^re (concat "^" re))
  7662. (start-level (and kill
  7663. (string-match
  7664. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7665. kill)
  7666. (- (match-end 2) (match-beginning 2) 1)))
  7667. (start (1+ (or (match-beginning 2) -1))))
  7668. (if (not start-level)
  7669. (progn
  7670. nil) ;; does not even start with a heading
  7671. (catch 'exit
  7672. (while (setq start (string-match ^re kill (1+ start)))
  7673. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7674. (throw 'exit nil)))
  7675. t))))
  7676. (defvar org-markers-to-move nil
  7677. "Markers that should be moved with a cut-and-paste operation.
  7678. Those markers are stored together with their positions relative to
  7679. the start of the region.")
  7680. (defun org-save-markers-in-region (beg end)
  7681. "Check markers in region.
  7682. If these markers are between BEG and END, record their position relative
  7683. to BEG, so that after moving the block of text, we can put the markers back
  7684. into place.
  7685. This function gets called just before an entry or tree gets cut from the
  7686. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7687. called immediately, to move the markers with the entries."
  7688. (setq org-markers-to-move nil)
  7689. (when (featurep 'org-clock)
  7690. (org-clock-save-markers-for-cut-and-paste beg end))
  7691. (when (featurep 'org-agenda)
  7692. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7693. (defun org-check-and-save-marker (marker beg end)
  7694. "Check if MARKER is between BEG and END.
  7695. If yes, remember the marker and the distance to BEG."
  7696. (when (and (marker-buffer marker)
  7697. (equal (marker-buffer marker) (current-buffer))
  7698. (>= marker beg) (< marker end))
  7699. (push (cons marker (- marker beg)) org-markers-to-move)))
  7700. (defun org-reinstall-markers-in-region (beg)
  7701. "Move all remembered markers to their position relative to BEG."
  7702. (dolist (x org-markers-to-move)
  7703. (move-marker (car x) (+ beg (cdr x))))
  7704. (setq org-markers-to-move nil))
  7705. (defun org-narrow-to-subtree ()
  7706. "Narrow buffer to the current subtree."
  7707. (interactive)
  7708. (save-excursion
  7709. (save-match-data
  7710. (org-with-limited-levels
  7711. (narrow-to-region
  7712. (progn (org-back-to-heading t) (point))
  7713. (progn (org-end-of-subtree t t)
  7714. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7715. (point)))))))
  7716. (defun org-narrow-to-block ()
  7717. "Narrow buffer to the current block."
  7718. (interactive)
  7719. (let* ((case-fold-search t)
  7720. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7721. "^[ \t]*#\\+end_.*")))
  7722. (if blockp
  7723. (narrow-to-region (car blockp) (cdr blockp))
  7724. (user-error "Not in a block"))))
  7725. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7726. "Clone the task (subtree) at point N times.
  7727. The clones will be inserted as siblings.
  7728. In interactive use, the user will be prompted for the number of
  7729. clones to be produced. If the entry has a timestamp, the user
  7730. will also be prompted for a time shift, which may be a repeater
  7731. as used in time stamps, for example `+3d'. To disable this,
  7732. you can call the function with a universal prefix argument.
  7733. When a valid repeater is given and the entry contains any time
  7734. stamps, the clones will become a sequence in time, with time
  7735. stamps in the subtree shifted for each clone produced. If SHIFT
  7736. is nil or the empty string, time stamps will be left alone. The
  7737. ID property of the original subtree is removed.
  7738. In each clone, all the CLOCK entries will be removed. This
  7739. prevents Org from considering that the clocked times overlap.
  7740. If the original subtree did contain time stamps with a repeater,
  7741. the following will happen:
  7742. - the repeater will be removed in each clone
  7743. - an additional clone will be produced, with the current, unshifted
  7744. date(s) in the entry.
  7745. - the original entry will be placed *after* all the clones, with
  7746. repeater intact.
  7747. - the start days in the repeater in the original entry will be shifted
  7748. to past the last clone.
  7749. In this way you can spell out a number of instances of a repeating task,
  7750. and still retain the repeater to cover future instances of the task.
  7751. As described above, N+1 clones are produced when the original
  7752. subtree has a repeater. Setting N to 0, then, can be used to
  7753. remove the repeater from a subtree and create a shifted clone
  7754. with the original repeater."
  7755. (interactive "nNumber of clones to produce: ")
  7756. (unless (wholenump n) (user-error "Invalid number of replications %s" n))
  7757. (when (org-before-first-heading-p) (user-error "No subtree to clone"))
  7758. (let* ((beg (save-excursion (org-back-to-heading t) (point)))
  7759. (end-of-tree (save-excursion (org-end-of-subtree t t) (point)))
  7760. (shift
  7761. (or shift
  7762. (if (and (not (equal current-prefix-arg '(4)))
  7763. (save-excursion
  7764. (goto-char beg)
  7765. (re-search-forward org-ts-regexp-both end-of-tree t)))
  7766. (read-from-minibuffer
  7767. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7768. ""))) ;No time shift
  7769. (doshift
  7770. (and (org-string-nw-p shift)
  7771. (or (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7772. shift)
  7773. (user-error "Invalid shift specification %s" shift)))))
  7774. (goto-char end-of-tree)
  7775. (unless (bolp) (insert "\n"))
  7776. (let* ((end (point))
  7777. (template (buffer-substring beg end))
  7778. (shift-n (and doshift (string-to-number (match-string 1 shift))))
  7779. (shift-what (pcase (and doshift (match-string 2 shift))
  7780. (`nil nil)
  7781. ("d" 'day)
  7782. ("w" (setq shift-n (* 7 shift-n)) 'day)
  7783. ("m" 'month)
  7784. ("y" 'year)
  7785. (_ (error "Unsupported time unit"))))
  7786. (nmin 1)
  7787. (nmax n)
  7788. (n-no-remove -1)
  7789. (idprop (org-entry-get nil "ID")))
  7790. (when (and doshift
  7791. (string-match-p "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>"
  7792. template))
  7793. (delete-region beg end)
  7794. (setq end beg)
  7795. (setq nmin 0)
  7796. (setq nmax (1+ nmax))
  7797. (setq n-no-remove nmax))
  7798. (goto-char end)
  7799. (cl-loop for n from nmin to nmax do
  7800. (insert
  7801. ;; Prepare clone.
  7802. (with-temp-buffer
  7803. (insert template)
  7804. (org-mode)
  7805. (goto-char (point-min))
  7806. (org-show-subtree)
  7807. (and idprop (if org-clone-delete-id
  7808. (org-entry-delete nil "ID")
  7809. (org-id-get-create t)))
  7810. (unless (= n 0)
  7811. (while (re-search-forward org-clock-line-re nil t)
  7812. (delete-region (line-beginning-position)
  7813. (line-beginning-position 2)))
  7814. (goto-char (point-min))
  7815. (while (re-search-forward org-drawer-regexp nil t)
  7816. (org-remove-empty-drawer-at (point))))
  7817. (goto-char (point-min))
  7818. (when doshift
  7819. (while (re-search-forward org-ts-regexp-both nil t)
  7820. (org-timestamp-change (* n shift-n) shift-what))
  7821. (unless (= n n-no-remove)
  7822. (goto-char (point-min))
  7823. (while (re-search-forward org-ts-regexp nil t)
  7824. (save-excursion
  7825. (goto-char (match-beginning 0))
  7826. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7827. (delete-region (match-beginning 1) (match-end 1)))))))
  7828. (buffer-string)))))
  7829. (goto-char beg)))
  7830. ;;; Outline Sorting
  7831. (defun org-sort (&optional with-case)
  7832. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7833. Optional argument WITH-CASE means sort case-sensitively."
  7834. (interactive "P")
  7835. (org-call-with-arg
  7836. (cond ((org-at-table-p) #'org-table-sort-lines)
  7837. ((org-at-item-p) #'org-sort-list)
  7838. (t #'org-sort-entries))
  7839. with-case))
  7840. (defun org-sort-remove-invisible (s)
  7841. "Remove invisible part of links and emphasis markers from string S."
  7842. (remove-text-properties 0 (length s) org-rm-props s)
  7843. (replace-regexp-in-string
  7844. org-verbatim-re (lambda (m) (format "%s " (match-string 4 m)))
  7845. (replace-regexp-in-string
  7846. org-emph-re (lambda (m) (format " %s " (match-string 4 m)))
  7847. (org-link-display-format s)
  7848. t t) t t))
  7849. (defvar org-priority-regexp) ; defined later in the file
  7850. (defvar org-after-sorting-entries-or-items-hook nil
  7851. "Hook that is run after a bunch of entries or items have been sorted.
  7852. When children are sorted, the cursor is in the parent line when this
  7853. hook gets called. When a region or a plain list is sorted, the cursor
  7854. will be in the first entry of the sorted region/list.")
  7855. (defun org-sort-entries
  7856. (&optional with-case sorting-type getkey-func compare-func property
  7857. interactive?)
  7858. "Sort entries on a certain level of an outline tree.
  7859. If there is an active region, the entries in the region are sorted.
  7860. Else, if the cursor is before the first entry, sort the top-level items.
  7861. Else, the children of the entry at point are sorted.
  7862. Sorting can be alphabetically, numerically, by date/time as given by
  7863. a time stamp, by a property, by priority order, or by a custom function.
  7864. The command prompts for the sorting type unless it has been given to the
  7865. function through the SORTING-TYPE argument, which needs to be a character,
  7866. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7867. the precise meaning of each character:
  7868. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7869. c By creation time, which is assumed to be the first inactive time stamp
  7870. at the beginning of a line.
  7871. d By deadline date/time.
  7872. k By clocking time.
  7873. n Numerically, by converting the beginning of the entry/item to a number.
  7874. o By order of TODO keywords.
  7875. p By priority according to the cookie.
  7876. r By the value of a property.
  7877. s By scheduled date/time.
  7878. t By date/time, either the first active time stamp in the entry, or, if
  7879. none exist, by the first inactive one.
  7880. Capital letters will reverse the sort order.
  7881. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7882. called with point at the beginning of the record. It must return a
  7883. value that is compatible with COMPARE-FUNC, the function used to
  7884. compare entries.
  7885. Comparing entries ignores case by default. However, with an optional argument
  7886. WITH-CASE, the sorting considers case as well.
  7887. Sorting is done against the visible part of the headlines, it ignores hidden
  7888. links.
  7889. When sorting is done, call `org-after-sorting-entries-or-items-hook'.
  7890. A non-nil value for INTERACTIVE? is used to signal that this
  7891. function is being called interactively."
  7892. (interactive (list current-prefix-arg nil nil nil nil t))
  7893. (let ((case-func (if with-case 'identity 'downcase))
  7894. start beg end stars re re2
  7895. txt what tmp)
  7896. ;; Find beginning and end of region to sort
  7897. (cond
  7898. ((org-region-active-p)
  7899. ;; we will sort the region
  7900. (setq end (region-end)
  7901. what "region")
  7902. (goto-char (region-beginning))
  7903. (unless (org-at-heading-p) (outline-next-heading))
  7904. (setq start (point)))
  7905. ((or (org-at-heading-p)
  7906. (ignore-errors (progn (org-back-to-heading) t)))
  7907. ;; we will sort the children of the current headline
  7908. (org-back-to-heading)
  7909. (setq start (point)
  7910. end (progn (org-end-of-subtree t t)
  7911. (or (bolp) (insert "\n"))
  7912. (when (>= (org-back-over-empty-lines) 1)
  7913. (forward-line 1))
  7914. (point))
  7915. what "children")
  7916. (goto-char start)
  7917. (outline-show-subtree)
  7918. (outline-next-heading))
  7919. (t
  7920. ;; we will sort the top-level entries in this file
  7921. (goto-char (point-min))
  7922. (or (org-at-heading-p) (outline-next-heading))
  7923. (setq start (point))
  7924. (goto-char (point-max))
  7925. (beginning-of-line 1)
  7926. (when (looking-at ".*?\\S-")
  7927. ;; File ends in a non-white line
  7928. (end-of-line 1)
  7929. (insert "\n"))
  7930. (setq end (point-max))
  7931. (setq what "top-level")
  7932. (goto-char start)
  7933. (outline-show-all)))
  7934. (setq beg (point))
  7935. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7936. (looking-at "\\(\\*+\\)")
  7937. (setq stars (match-string 1)
  7938. re (concat "^" (regexp-quote stars) " +")
  7939. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7940. txt (buffer-substring beg end))
  7941. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7942. (when (and (not (equal stars "*")) (string-match re2 txt))
  7943. (user-error "Region to sort contains a level above the first entry"))
  7944. (unless sorting-type
  7945. (message
  7946. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7947. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7948. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7949. what)
  7950. (setq sorting-type (read-char-exclusive)))
  7951. (unless getkey-func
  7952. (and (= (downcase sorting-type) ?f)
  7953. (setq getkey-func
  7954. (or (and interactive?
  7955. (org-read-function
  7956. "Function for extracting keys: "))
  7957. (error "Missing key extractor")))))
  7958. (and (= (downcase sorting-type) ?r)
  7959. (not property)
  7960. (setq property
  7961. (completing-read "Property: "
  7962. (mapcar #'list (org-buffer-property-keys t))
  7963. nil t)))
  7964. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7965. (message "Sorting entries...")
  7966. (save-restriction
  7967. (narrow-to-region start end)
  7968. (let ((restore-clock?
  7969. ;; The clock marker is lost when using `sort-subr'; mark
  7970. ;; the clock with temporary `:org-clock-marker-backup'
  7971. ;; text property.
  7972. (when (and (eq (org-clock-is-active) (current-buffer))
  7973. (<= start (marker-position org-clock-marker))
  7974. (>= end (marker-position org-clock-marker)))
  7975. (org-with-silent-modifications
  7976. (put-text-property (1- org-clock-marker) org-clock-marker
  7977. :org-clock-marker-backup t))
  7978. t))
  7979. (dcst (downcase sorting-type))
  7980. (case-fold-search nil)
  7981. (now (current-time)))
  7982. (sort-subr
  7983. (/= dcst sorting-type)
  7984. ;; This function moves to the beginning character of the "record" to
  7985. ;; be sorted.
  7986. (lambda nil
  7987. (if (re-search-forward re nil t)
  7988. (goto-char (match-beginning 0))
  7989. (goto-char (point-max))))
  7990. ;; This function moves to the last character of the "record" being
  7991. ;; sorted.
  7992. (lambda nil
  7993. (save-match-data
  7994. (condition-case nil
  7995. (outline-forward-same-level 1)
  7996. (error
  7997. (goto-char (point-max))))))
  7998. ;; This function returns the value that gets sorted against.
  7999. (lambda nil
  8000. (cond
  8001. ((= dcst ?n)
  8002. (if (looking-at org-complex-heading-regexp)
  8003. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8004. nil))
  8005. ((= dcst ?a)
  8006. (if (looking-at org-complex-heading-regexp)
  8007. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8008. nil))
  8009. ((= dcst ?k)
  8010. (or (get-text-property (point) :org-clock-minutes) 0))
  8011. ((= dcst ?t)
  8012. (let ((end (save-excursion (outline-next-heading) (point))))
  8013. (if (or (re-search-forward org-ts-regexp end t)
  8014. (re-search-forward org-ts-regexp-both end t))
  8015. (org-time-string-to-seconds (match-string 0))
  8016. (float-time now))))
  8017. ((= dcst ?c)
  8018. (let ((end (save-excursion (outline-next-heading) (point))))
  8019. (if (re-search-forward
  8020. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8021. end t)
  8022. (org-time-string-to-seconds (match-string 0))
  8023. (float-time now))))
  8024. ((= dcst ?s)
  8025. (let ((end (save-excursion (outline-next-heading) (point))))
  8026. (if (re-search-forward org-scheduled-time-regexp end t)
  8027. (org-time-string-to-seconds (match-string 1))
  8028. (float-time now))))
  8029. ((= dcst ?d)
  8030. (let ((end (save-excursion (outline-next-heading) (point))))
  8031. (if (re-search-forward org-deadline-time-regexp end t)
  8032. (org-time-string-to-seconds (match-string 1))
  8033. (float-time now))))
  8034. ((= dcst ?p)
  8035. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8036. (string-to-char (match-string 2))
  8037. org-default-priority))
  8038. ((= dcst ?r)
  8039. (or (org-entry-get nil property) ""))
  8040. ((= dcst ?o)
  8041. (when (looking-at org-complex-heading-regexp)
  8042. (let* ((m (match-string 2))
  8043. (s (if (member m org-done-keywords) '- '+)))
  8044. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8045. ((= dcst ?f)
  8046. (if getkey-func
  8047. (progn
  8048. (setq tmp (funcall getkey-func))
  8049. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8050. tmp)
  8051. (error "Invalid key function `%s'" getkey-func)))
  8052. (t (error "Invalid sorting type `%c'" sorting-type))))
  8053. nil
  8054. (cond
  8055. ((= dcst ?a) 'string<)
  8056. ((= dcst ?f)
  8057. (or compare-func
  8058. (and interactive?
  8059. (org-read-function
  8060. (concat "Function for comparing keys "
  8061. "(empty for default `sort-subr' predicate): ")
  8062. 'allow-empty))))
  8063. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))
  8064. (when restore-clock?
  8065. (move-marker org-clock-marker
  8066. (1+ (next-single-property-change
  8067. start :org-clock-marker-backup)))
  8068. (remove-text-properties (1- org-clock-marker) org-clock-marker
  8069. '(:org-clock-marker-backup t)))))
  8070. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8071. (message "Sorting entries...done")))
  8072. ;;; The orgstruct minor mode
  8073. ;; Define a minor mode which can be used in other modes in order to
  8074. ;; integrate the Org mode structure editing commands.
  8075. ;; This is really a hack, because the Org mode structure commands use
  8076. ;; keys which normally belong to the major mode. Here is how it
  8077. ;; works: The minor mode defines all the keys necessary to operate the
  8078. ;; structure commands, but wraps the commands into a function which
  8079. ;; tests if the cursor is currently at a headline or a plain list
  8080. ;; item. If that is the case, the structure command is used,
  8081. ;; temporarily setting many Org mode variables like regular
  8082. ;; expressions for filling etc. However, when any of those keys is
  8083. ;; used at a different location, function uses `key-binding' to look
  8084. ;; up if the key has an associated command in another currently active
  8085. ;; keymap (minor modes, major mode, global), and executes that
  8086. ;; command. There might be problems if any of the keys is otherwise
  8087. ;; used as a prefix key.
  8088. (defcustom orgstruct-heading-prefix-regexp ""
  8089. "Regexp that matches the custom prefix of Org headlines in
  8090. orgstruct(++)-mode."
  8091. :group 'org
  8092. :version "26.1"
  8093. :package-version '(Org . "8.3")
  8094. :type 'regexp)
  8095. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8096. (defcustom orgstruct-setup-hook nil
  8097. "Hook run after orgstruct-mode-map is filled."
  8098. :group 'org
  8099. :version "24.4"
  8100. :package-version '(Org . "8.0")
  8101. :type 'hook)
  8102. (defvar orgstruct-initialized nil)
  8103. (defvar org-local-vars nil
  8104. "List of local variables, for use by `orgstruct-mode'.")
  8105. ;;;###autoload
  8106. (define-minor-mode orgstruct-mode
  8107. "Toggle the minor mode `orgstruct-mode'.
  8108. This mode is for using Org mode structure commands in other
  8109. modes. The following keys behave as if Org mode were active, if
  8110. the cursor is on a headline, or on a plain list item (both as
  8111. defined by Org mode)."
  8112. nil " OrgStruct" (make-sparse-keymap)
  8113. (funcall (if orgstruct-mode
  8114. 'add-to-invisibility-spec
  8115. 'remove-from-invisibility-spec)
  8116. '(outline . t))
  8117. (when orgstruct-mode
  8118. (org-load-modules-maybe)
  8119. (unless orgstruct-initialized
  8120. (orgstruct-setup)
  8121. (setq orgstruct-initialized t))))
  8122. ;;;###autoload
  8123. (defun turn-on-orgstruct ()
  8124. "Unconditionally turn on `orgstruct-mode'."
  8125. (orgstruct-mode 1))
  8126. (defvar-local orgstruct-is-++ nil
  8127. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8128. (defvar-local org-fb-vars nil)
  8129. (defun orgstruct++-mode (&optional arg)
  8130. "Toggle `orgstruct-mode', the enhanced version of it.
  8131. In addition to setting orgstruct-mode, this also exports all
  8132. indentation and autofilling variables from Org mode into the
  8133. buffer. It will also recognize item context in multiline items."
  8134. (interactive "P")
  8135. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8136. (if (< arg 1)
  8137. (progn (orgstruct-mode -1)
  8138. (dolist (v org-fb-vars)
  8139. (set (make-local-variable (car v))
  8140. (if (eq (car-safe (cadr v)) 'quote)
  8141. (cl-cadadr v)
  8142. (nth 1 v)))))
  8143. (orgstruct-mode 1)
  8144. (setq org-fb-vars nil)
  8145. (unless org-local-vars
  8146. (setq org-local-vars (org-get-local-variables)))
  8147. (let (var val)
  8148. (dolist (x org-local-vars)
  8149. (when (string-match
  8150. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8151. \\|fill-prefix\\|indent-\\)"
  8152. (symbol-name (car x)))
  8153. (setq var (car x) val (nth 1 x))
  8154. (push (list var `(quote ,(eval var))) org-fb-vars)
  8155. (set (make-local-variable var)
  8156. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8157. (setq-local orgstruct-is-++ t))))
  8158. ;;;###autoload
  8159. (defun turn-on-orgstruct++ ()
  8160. "Unconditionally turn on `orgstruct++-mode'."
  8161. (orgstruct++-mode 1))
  8162. (defun orgstruct-error ()
  8163. "Error when there is no default binding for a structure key."
  8164. (interactive)
  8165. (funcall (if (fboundp 'user-error)
  8166. 'user-error
  8167. 'error)
  8168. "This key has no function outside structure elements"))
  8169. (defun orgstruct-setup ()
  8170. "Setup orgstruct keymap."
  8171. (dolist (cell '((org-demote . t)
  8172. (org-metaleft . t)
  8173. (org-metaright . t)
  8174. (org-promote . t)
  8175. (org-shiftmetaleft . t)
  8176. (org-shiftmetaright . t)
  8177. org-backward-element
  8178. org-backward-heading-same-level
  8179. org-ctrl-c-ret
  8180. org-ctrl-c-minus
  8181. org-ctrl-c-star
  8182. org-cycle
  8183. org-force-cycle-archived
  8184. org-forward-heading-same-level
  8185. org-insert-heading
  8186. org-insert-heading-respect-content
  8187. org-kill-note-or-show-branches
  8188. org-mark-subtree
  8189. org-meta-return
  8190. org-metadown
  8191. org-metaup
  8192. org-narrow-to-subtree
  8193. org-promote-subtree
  8194. org-reveal
  8195. org-shiftdown
  8196. org-shiftleft
  8197. org-shiftmetadown
  8198. org-shiftmetaup
  8199. org-shiftright
  8200. org-shifttab
  8201. org-shifttab
  8202. org-shiftup
  8203. org-show-children
  8204. org-show-subtree
  8205. org-sort
  8206. org-up-element
  8207. outline-demote
  8208. outline-next-visible-heading
  8209. outline-previous-visible-heading
  8210. outline-promote
  8211. outline-up-heading))
  8212. (let ((f (or (car-safe cell) cell))
  8213. (disable-when-heading-prefix (cdr-safe cell)))
  8214. (when (fboundp f)
  8215. (let ((new-bindings))
  8216. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8217. (where-is-internal f outline-mode-map)))
  8218. (push binding new-bindings)
  8219. ;; TODO use local-function-key-map
  8220. (dolist (rep '(("<tab>" . "TAB")
  8221. ("<return>" . "RET")
  8222. ("<escape>" . "ESC")
  8223. ("<delete>" . "DEL")))
  8224. (setq binding (read-kbd-macro
  8225. (let ((case-fold-search))
  8226. (replace-regexp-in-string
  8227. (regexp-quote (cdr rep))
  8228. (car rep)
  8229. (key-description binding)))))
  8230. (cl-pushnew binding new-bindings :test 'equal)))
  8231. (dolist (binding new-bindings)
  8232. (let ((key (lookup-key orgstruct-mode-map binding)))
  8233. (when (or (not key) (numberp key))
  8234. (ignore-errors
  8235. (org-defkey orgstruct-mode-map
  8236. binding
  8237. (orgstruct-make-binding
  8238. f binding disable-when-heading-prefix))))))))))
  8239. (run-hooks 'orgstruct-setup-hook))
  8240. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8241. "Create a function for binding in the structure minor mode.
  8242. FUN is the command to call inside a table. KEY is the key that
  8243. should be checked in for a command to execute outside of tables.
  8244. Non-nil `disable-when-heading-prefix' means to disable the command
  8245. if `orgstruct-heading-prefix-regexp' is not empty."
  8246. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8247. (let ((nname name)
  8248. (i 0))
  8249. (while (fboundp (intern nname))
  8250. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8251. (setq name (intern nname)))
  8252. (eval
  8253. (let ((bindings '((org-heading-regexp
  8254. (concat "^"
  8255. orgstruct-heading-prefix-regexp
  8256. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8257. (org-outline-regexp
  8258. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8259. (org-outline-regexp-bol
  8260. (concat "^" org-outline-regexp))
  8261. (outline-regexp org-outline-regexp)
  8262. (outline-heading-end-regexp "\n")
  8263. (outline-level 'org-outline-level)
  8264. (outline-heading-alist))))
  8265. `(defun ,name (arg)
  8266. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8267. "Outside of structure, run the binding of `"
  8268. (key-description key) "'."
  8269. (when disable-when-heading-prefix
  8270. (concat
  8271. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8272. "back to the default binding due to limitations of Org's implementation of\n"
  8273. "`" (symbol-name fun) "'.")))
  8274. (interactive "p")
  8275. (let* ((disable
  8276. ,(and disable-when-heading-prefix
  8277. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8278. (fallback
  8279. (or disable
  8280. (not
  8281. (let* ,bindings
  8282. (org-context-p 'headline 'item
  8283. ,(when (memq fun
  8284. '(org-insert-heading
  8285. org-insert-heading-respect-content
  8286. org-meta-return))
  8287. '(when orgstruct-is-++
  8288. 'item-body))))))))
  8289. (if fallback
  8290. (let* ((orgstruct-mode)
  8291. (binding
  8292. (let ((key ,key))
  8293. (catch 'exit
  8294. (dolist
  8295. (rep
  8296. '(nil
  8297. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8298. ("<\\([^>]*\\)return>" . "\\1RET")
  8299. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8300. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8301. nil)
  8302. (when rep
  8303. (setq key (read-kbd-macro
  8304. (let ((case-fold-search))
  8305. (replace-regexp-in-string
  8306. (car rep)
  8307. (cdr rep)
  8308. (key-description key))))))
  8309. (when (key-binding key)
  8310. (throw 'exit (key-binding key))))))))
  8311. (if (keymapp binding)
  8312. (org-set-transient-map binding)
  8313. (let ((func (or binding
  8314. (unless disable
  8315. 'orgstruct-error))))
  8316. (when func
  8317. (call-interactively func)))))
  8318. (org-run-like-in-org-mode
  8319. (lambda ()
  8320. (interactive)
  8321. (let* ,bindings
  8322. (call-interactively ',fun)))))))))
  8323. name))
  8324. (defun org-contextualize-keys (alist contexts)
  8325. "Return valid elements in ALIST depending on CONTEXTS.
  8326. `org-agenda-custom-commands' or `org-capture-templates' are the
  8327. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8328. or `org-capture-templates-contexts' are the associated contexts
  8329. definitions."
  8330. (let ((contexts
  8331. ;; normalize contexts
  8332. (mapcar
  8333. (lambda(c) (cond ((listp (cadr c))
  8334. (list (car c) (car c) (nth 1 c)))
  8335. ((string= "" (cadr c))
  8336. (list (car c) (car c) (nth 2 c)))
  8337. (t c)))
  8338. contexts))
  8339. (a alist) r s)
  8340. ;; loop over all commands or templates
  8341. (dolist (c a)
  8342. (let (vrules repl)
  8343. (cond
  8344. ((not (assoc (car c) contexts))
  8345. (push c r))
  8346. ((and (assoc (car c) contexts)
  8347. (setq vrules (org-contextualize-validate-key
  8348. (car c) contexts)))
  8349. (mapc (lambda (vr)
  8350. (unless (equal (car vr) (cadr vr))
  8351. (setq repl vr)))
  8352. vrules)
  8353. (if (not repl) (push c r)
  8354. (push (cadr repl) s)
  8355. (push
  8356. (cons (car c)
  8357. (cdr (or (assoc (cadr repl) alist)
  8358. (error "Undefined key `%s' as contextual replacement for `%s'"
  8359. (cadr repl) (car c)))))
  8360. r))))))
  8361. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8362. (delq
  8363. nil
  8364. (delete-dups
  8365. (mapcar (lambda (x)
  8366. (let ((tpl (car x)))
  8367. (unless (delq
  8368. nil
  8369. (mapcar (lambda (y)
  8370. (equal y tpl))
  8371. s))
  8372. x)))
  8373. (reverse r))))))
  8374. (defun org-contextualize-validate-key (key contexts)
  8375. "Check CONTEXTS for agenda or capture KEY."
  8376. (let (res)
  8377. (dolist (r contexts)
  8378. (dolist (rr (car (last r)))
  8379. (when
  8380. (and (equal key (car r))
  8381. (if (functionp rr) (funcall rr)
  8382. (or (and (eq (car rr) 'in-file)
  8383. (buffer-file-name)
  8384. (string-match (cdr rr) (buffer-file-name)))
  8385. (and (eq (car rr) 'in-mode)
  8386. (string-match (cdr rr) (symbol-name major-mode)))
  8387. (and (eq (car rr) 'in-buffer)
  8388. (string-match (cdr rr) (buffer-name)))
  8389. (when (and (eq (car rr) 'not-in-file)
  8390. (buffer-file-name))
  8391. (not (string-match (cdr rr) (buffer-file-name))))
  8392. (when (eq (car rr) 'not-in-mode)
  8393. (not (string-match (cdr rr) (symbol-name major-mode))))
  8394. (when (eq (car rr) 'not-in-buffer)
  8395. (not (string-match (cdr rr) (buffer-name)))))))
  8396. (push r res))))
  8397. (delete-dups (delq nil res))))
  8398. (defun org-context-p (&rest contexts)
  8399. "Check if local context is any of CONTEXTS.
  8400. Possible values in the list of contexts are `table', `headline', and `item'."
  8401. (let ((pos (point)))
  8402. (goto-char (point-at-bol))
  8403. (prog1 (or (and (memq 'table contexts)
  8404. (looking-at "[ \t]*|"))
  8405. (and (memq 'headline contexts)
  8406. (looking-at org-outline-regexp))
  8407. (and (memq 'item contexts)
  8408. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8409. (and (memq 'item-body contexts)
  8410. (org-in-item-p)))
  8411. (goto-char pos))))
  8412. ;;;###autoload
  8413. (defun org-run-like-in-org-mode (cmd)
  8414. "Run a command, pretending that the current buffer is in Org mode.
  8415. This will temporarily bind local variables that are typically bound in
  8416. Org mode to the values they have in Org mode, and then interactively
  8417. call CMD."
  8418. (org-load-modules-maybe)
  8419. (unless org-local-vars
  8420. (setq org-local-vars (org-get-local-variables)))
  8421. (let (binds)
  8422. (dolist (var org-local-vars)
  8423. (when (or (not (boundp (car var)))
  8424. (eq (symbol-value (car var))
  8425. (default-value (car var))))
  8426. (push (list (car var) `(quote ,(cadr var))) binds)))
  8427. (eval `(let ,binds
  8428. (call-interactively (quote ,cmd))))))
  8429. (defun org-get-category (&optional pos force-refresh)
  8430. "Get the category applying to position POS."
  8431. (save-match-data
  8432. (when force-refresh (org-refresh-category-properties))
  8433. (let ((pos (or pos (point))))
  8434. (or (get-text-property pos 'org-category)
  8435. (progn (org-refresh-category-properties)
  8436. (get-text-property pos 'org-category))))))
  8437. ;;; Refresh properties
  8438. (defun org-refresh-properties (dprop tprop)
  8439. "Refresh buffer text properties.
  8440. DPROP is the drawer property and TPROP is either the
  8441. corresponding text property to set, or an alist with each element
  8442. being a text property (as a symbol) and a function to apply to
  8443. the value of the drawer property."
  8444. (let* ((case-fold-search t)
  8445. (inhibit-read-only t)
  8446. (inherit? (org-property-inherit-p dprop))
  8447. (property-re (org-re-property (concat (regexp-quote dprop) "\\+?") t))
  8448. (global (and inherit? (org--property-global-value dprop nil))))
  8449. (org-with-silent-modifications
  8450. (org-with-point-at 1
  8451. ;; Set global values (e.g., values defined through
  8452. ;; "#+PROPERTY:" keywords) to the whole buffer.
  8453. (when global (put-text-property (point-min) (point-max) tprop global))
  8454. ;; Set local values.
  8455. (while (re-search-forward property-re nil t)
  8456. (when (org-at-property-p)
  8457. (org-refresh-property tprop (org-entry-get (point) dprop) inherit?))
  8458. (outline-next-heading))))))
  8459. (defun org-refresh-property (tprop p &optional inherit)
  8460. "Refresh the buffer text property TPROP from the drawer property P.
  8461. The refresh happens only for the current headline, or the whole
  8462. sub-tree if optional argument INHERIT is non-nil."
  8463. (unless (org-before-first-heading-p)
  8464. (save-excursion
  8465. (org-back-to-heading t)
  8466. (let ((start (point))
  8467. (end (save-excursion
  8468. (if inherit (org-end-of-subtree t t)
  8469. (or (outline-next-heading) (point-max))))))
  8470. (if (symbolp tprop)
  8471. ;; TPROP is a text property symbol.
  8472. (put-text-property start end tprop p)
  8473. ;; TPROP is an alist with (property . function) elements.
  8474. (pcase-dolist (`(,prop . ,f) tprop)
  8475. (put-text-property start end prop (funcall f p))))))))
  8476. (defun org-refresh-category-properties ()
  8477. "Refresh category text properties in the buffer."
  8478. (let ((case-fold-search t)
  8479. (inhibit-read-only t)
  8480. (default-category
  8481. (cond ((null org-category)
  8482. (if buffer-file-name
  8483. (file-name-sans-extension
  8484. (file-name-nondirectory buffer-file-name))
  8485. "???"))
  8486. ((symbolp org-category) (symbol-name org-category))
  8487. (t org-category))))
  8488. (org-with-silent-modifications
  8489. (org-with-wide-buffer
  8490. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8491. ;; This is the default category for the buffer. If none is
  8492. ;; found, fall-back to `org-category' or buffer file name.
  8493. (put-text-property
  8494. (point-min) (point-max)
  8495. 'org-category
  8496. (catch 'buffer-category
  8497. (goto-char (point-max))
  8498. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8499. (let ((element (org-element-at-point)))
  8500. (when (eq (org-element-type element) 'keyword)
  8501. (throw 'buffer-category
  8502. (org-element-property :value element)))))
  8503. default-category))
  8504. ;; Set sub-tree specific categories.
  8505. (goto-char (point-min))
  8506. (let ((regexp (org-re-property "CATEGORY")))
  8507. (while (re-search-forward regexp nil t)
  8508. (let ((value (match-string-no-properties 3)))
  8509. (when (org-at-property-p)
  8510. (put-text-property
  8511. (save-excursion (org-back-to-heading t) (point))
  8512. (save-excursion (org-end-of-subtree t t) (point))
  8513. 'org-category
  8514. value)))))))))
  8515. (defun org-refresh-stats-properties ()
  8516. "Refresh stats text properties in the buffer."
  8517. (org-with-silent-modifications
  8518. (org-with-point-at 1
  8519. (let ((regexp (concat org-outline-regexp-bol
  8520. ".*\\[\\([0-9]*\\)\\(?:%\\|/\\([0-9]*\\)\\)\\]")))
  8521. (while (re-search-forward regexp nil t)
  8522. (let* ((numerator (string-to-number (match-string 1)))
  8523. (denominator (and (match-end 2)
  8524. (string-to-number (match-string 2))))
  8525. (stats (cond ((not denominator) numerator) ;percent
  8526. ((= denominator 0) 0)
  8527. (t (/ (* numerator 100) denominator)))))
  8528. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8529. 'org-stats stats)))))))
  8530. (defun org-refresh-effort-properties ()
  8531. "Refresh effort properties"
  8532. (org-refresh-properties
  8533. org-effort-property
  8534. '((effort . identity)
  8535. (effort-minutes . org-duration-to-minutes))))
  8536. ;;;; Link Stuff
  8537. ;;; Link abbreviations
  8538. (defun org-link-expand-abbrev (link)
  8539. "Apply replacements as defined in `org-link-abbrev-alist'."
  8540. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8541. (let* ((key (match-string 1 link))
  8542. (as (or (assoc key org-link-abbrev-alist-local)
  8543. (assoc key org-link-abbrev-alist)))
  8544. (tag (and (match-end 2) (match-string 3 link)))
  8545. rpl)
  8546. (if (not as)
  8547. link
  8548. (setq rpl (cdr as))
  8549. (cond
  8550. ((symbolp rpl) (funcall rpl tag))
  8551. ((string-match "%(\\([^)]+\\))" rpl)
  8552. (replace-match
  8553. (save-match-data
  8554. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8555. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8556. ((string-match "%h" rpl)
  8557. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8558. (t (concat rpl tag)))))
  8559. link))
  8560. ;;; Storing and inserting links
  8561. (defvar org-insert-link-history nil
  8562. "Minibuffer history for links inserted with `org-insert-link'.")
  8563. (defvar org-stored-links nil
  8564. "Contains the links stored with `org-store-link'.")
  8565. (defvar org-store-link-plist nil
  8566. "Plist with info about the most recently link created with `org-store-link'.")
  8567. (defun org-store-link-functions ()
  8568. "Return a list of functions that are called to create and store a link.
  8569. The functions defined in the :store property of
  8570. `org-link-parameters'.
  8571. Each function will be called in turn until one returns a non-nil
  8572. value. Each function should check if it is responsible for
  8573. creating this link (for example by looking at the major mode).
  8574. If not, it must exit and return nil. If yes, it should return
  8575. a non-nil value after calling `org-store-link-props' with a list
  8576. of properties and values. Special properties are:
  8577. :type The link prefix, like \"http\". This must be given.
  8578. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8579. This is obligatory as well.
  8580. :description Optional default description for the second pair
  8581. of brackets in an Org mode link. The user can still change
  8582. this when inserting this link into an Org mode buffer.
  8583. In addition to these, any additional properties can be specified
  8584. and then used in capture templates."
  8585. (cl-loop for link in org-link-parameters
  8586. with store-func
  8587. do (setq store-func (org-link-get-parameter (car link) :store))
  8588. if store-func
  8589. collect store-func))
  8590. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8591. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8592. ;;;###autoload
  8593. (defun org-store-link (arg)
  8594. "Store an org-link to the current location.
  8595. \\<org-mode-map>
  8596. This link is added to `org-stored-links' and can later be inserted
  8597. into an Org buffer with `org-insert-link' (`\\[org-insert-link]').
  8598. For some link types, a `\\[universal-argument]' prefix ARG is interpreted. \
  8599. A single
  8600. `\\[universal-argument]' negates `org-context-in-file-links' for file links or
  8601. `org-gnus-prefer-web-links' for links to Usenet articles.
  8602. A `\\[universal-argument] \\[universal-argument]' prefix ARG forces \
  8603. skipping storing functions that are not
  8604. part of Org core.
  8605. A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  8606. prefix ARG forces storing a link for each line in the
  8607. active region."
  8608. (interactive "P")
  8609. (org-load-modules-maybe)
  8610. (if (and (equal arg '(64)) (org-region-active-p))
  8611. (save-excursion
  8612. (let ((end (region-end)))
  8613. (goto-char (region-beginning))
  8614. (set-mark (point))
  8615. (while (< (point-at-eol) end)
  8616. (move-end-of-line 1) (activate-mark)
  8617. (let (current-prefix-arg)
  8618. (call-interactively 'org-store-link))
  8619. (move-beginning-of-line 2)
  8620. (set-mark (point)))))
  8621. (setq org-store-link-plist nil)
  8622. (let (link cpltxt desc description search txt custom-id agenda-link)
  8623. (cond
  8624. ;; Store a link using an external link type, if any function is
  8625. ;; available. If more than one can generate a link from current
  8626. ;; location, ask which one to use.
  8627. ((and (not (equal arg '(16)))
  8628. (let ((results-alist nil))
  8629. (dolist (f (org-store-link-functions))
  8630. (when (funcall f)
  8631. ;; XXX: return value is not link's plist, so we
  8632. ;; store the new value before it is modified. It
  8633. ;; would be cleaner to ask store link functions to
  8634. ;; return the plist instead.
  8635. (push (cons f (copy-sequence org-store-link-plist))
  8636. results-alist)))
  8637. (pcase results-alist
  8638. (`nil nil)
  8639. (`((,_ . ,_)) t) ;single choice: nothing to do
  8640. (`((,name . ,_) . ,_)
  8641. ;; Reinstate link plist associated to the chosen
  8642. ;; function.
  8643. (apply #'org-store-link-props
  8644. (cdr (assoc-string
  8645. (completing-read
  8646. "Which function for creating the link? "
  8647. (mapcar #'car results-alist) nil t name)
  8648. results-alist)))
  8649. t))))
  8650. (setq link (plist-get org-store-link-plist :link))
  8651. (setq desc (or (plist-get org-store-link-plist :description)
  8652. link)))
  8653. ;; Store a link from a source code buffer.
  8654. ((org-src-edit-buffer-p)
  8655. (let ((coderef-format (org-src-coderef-format)))
  8656. (cond ((save-excursion
  8657. (beginning-of-line)
  8658. (looking-at (org-src-coderef-regexp coderef-format)))
  8659. (setq link (format "(%s)" (match-string-no-properties 3))))
  8660. ((called-interactively-p 'any)
  8661. (let ((label (read-string "Code line label: ")))
  8662. (end-of-line)
  8663. (setq link (format coderef-format label))
  8664. (let ((gc (- 79 (length link))))
  8665. (if (< (current-column) gc)
  8666. (org-move-to-column gc t)
  8667. (insert " ")))
  8668. (insert link)
  8669. (setq link (concat "(" label ")"))
  8670. (setq desc nil)))
  8671. (t (setq link nil)))))
  8672. ;; We are in the agenda, link to referenced location
  8673. ((equal (bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8674. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8675. (get-text-property (point) 'org-marker))))
  8676. (when m
  8677. (org-with-point-at m
  8678. (setq agenda-link
  8679. (if (called-interactively-p 'any)
  8680. (call-interactively 'org-store-link)
  8681. (org-store-link nil)))))))
  8682. ((eq major-mode 'calendar-mode)
  8683. (let ((cd (calendar-cursor-to-date)))
  8684. (setq link
  8685. (format-time-string
  8686. (car org-time-stamp-formats)
  8687. (apply 'encode-time
  8688. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8689. nil nil nil))))
  8690. (org-store-link-props :type "calendar" :date cd)))
  8691. ((eq major-mode 'help-mode)
  8692. (setq link (concat "help:" (save-excursion
  8693. (goto-char (point-min))
  8694. (looking-at "^[^ ]+")
  8695. (match-string 0))))
  8696. (org-store-link-props :type "help"))
  8697. ((eq major-mode 'w3-mode)
  8698. (setq cpltxt (if (and (buffer-name)
  8699. (not (string-match "Untitled" (buffer-name))))
  8700. (buffer-name)
  8701. (url-view-url t))
  8702. link (url-view-url t))
  8703. (org-store-link-props :type "w3" :url (url-view-url t)))
  8704. ((eq major-mode 'image-mode)
  8705. (setq cpltxt (concat "file:"
  8706. (abbreviate-file-name buffer-file-name))
  8707. link cpltxt)
  8708. (org-store-link-props :type "image" :file buffer-file-name))
  8709. ;; In dired, store a link to the file of the current line
  8710. ((derived-mode-p 'dired-mode)
  8711. (let ((file (dired-get-filename nil t)))
  8712. (setq file (if file
  8713. (abbreviate-file-name
  8714. (expand-file-name (dired-get-filename nil t)))
  8715. ;; otherwise, no file so use current directory.
  8716. default-directory))
  8717. (setq cpltxt (concat "file:" file)
  8718. link cpltxt)))
  8719. ((setq search (run-hook-with-args-until-success
  8720. 'org-create-file-search-functions))
  8721. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8722. "::" search))
  8723. (setq cpltxt (or description link)))
  8724. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8725. (org-with-limited-levels
  8726. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8727. (cond
  8728. ;; Store a link using the target at point
  8729. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8730. (setq cpltxt
  8731. (concat "file:"
  8732. (abbreviate-file-name
  8733. (buffer-file-name (buffer-base-buffer)))
  8734. "::" (match-string 1))
  8735. link cpltxt))
  8736. ((and (featurep 'org-id)
  8737. (or (eq org-id-link-to-org-use-id t)
  8738. (and (called-interactively-p 'any)
  8739. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8740. (and (eq org-id-link-to-org-use-id
  8741. 'create-if-interactive-and-no-custom-id)
  8742. (not custom-id))))
  8743. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8744. ;; Store a link using the ID at point
  8745. (setq link (condition-case nil
  8746. (prog1 (org-id-store-link)
  8747. (setq desc (or (plist-get org-store-link-plist
  8748. :description)
  8749. "")))
  8750. (error
  8751. ;; Probably before first headline, link only to file
  8752. (concat "file:"
  8753. (abbreviate-file-name
  8754. (buffer-file-name (buffer-base-buffer))))))))
  8755. (t
  8756. ;; Just link to current headline
  8757. (setq cpltxt (concat "file:"
  8758. (abbreviate-file-name
  8759. (buffer-file-name (buffer-base-buffer)))))
  8760. ;; Add a context search string
  8761. (when (org-xor org-context-in-file-links
  8762. (equal arg '(4)))
  8763. (let* ((element (org-element-at-point))
  8764. (name (org-element-property :name element)))
  8765. (setq txt (cond
  8766. ((org-at-heading-p) nil)
  8767. (name)
  8768. ((org-region-active-p)
  8769. (buffer-substring (region-beginning) (region-end)))))
  8770. (when (or (null txt) (string-match "\\S-" txt))
  8771. (setq cpltxt
  8772. (concat cpltxt "::"
  8773. (condition-case nil
  8774. (org-make-org-heading-search-string txt)
  8775. (error "")))
  8776. desc (or name
  8777. (nth 4 (ignore-errors (org-heading-components)))
  8778. "NONE")))))
  8779. (when (string-match "::\\'" cpltxt)
  8780. (setq cpltxt (substring cpltxt 0 -2)))
  8781. (setq link cpltxt)))))
  8782. ((buffer-file-name (buffer-base-buffer))
  8783. ;; Just link to this file here.
  8784. (setq cpltxt (concat "file:"
  8785. (abbreviate-file-name
  8786. (buffer-file-name (buffer-base-buffer)))))
  8787. ;; Add a context string.
  8788. (when (org-xor org-context-in-file-links
  8789. (equal arg '(4)))
  8790. (setq txt (if (org-region-active-p)
  8791. (buffer-substring (region-beginning) (region-end))
  8792. (buffer-substring (point-at-bol) (point-at-eol))))
  8793. ;; Only use search option if there is some text.
  8794. (when (string-match "\\S-" txt)
  8795. (setq cpltxt
  8796. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8797. desc "NONE")))
  8798. (setq link cpltxt))
  8799. ((called-interactively-p 'interactive)
  8800. (user-error "No method for storing a link from this buffer"))
  8801. (t (setq link nil)))
  8802. ;; We're done setting link and desc, clean up
  8803. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8804. (setq link (or link cpltxt)
  8805. desc (or desc cpltxt))
  8806. (cond ((not desc))
  8807. ((equal desc "NONE") (setq desc nil))
  8808. (t (setq desc
  8809. (replace-regexp-in-string
  8810. org-bracket-link-analytic-regexp
  8811. (lambda (m) (or (match-string 5 m) (match-string 3 m)))
  8812. desc))))
  8813. ;; Return the link
  8814. (if (not (and (or (called-interactively-p 'any)
  8815. executing-kbd-macro)
  8816. link))
  8817. (or agenda-link (and link (org-make-link-string link desc)))
  8818. (push (list link desc) org-stored-links)
  8819. (message "Stored: %s" (or desc link))
  8820. (when custom-id
  8821. (setq link (concat "file:" (abbreviate-file-name
  8822. (buffer-file-name)) "::#" custom-id))
  8823. (push (list link desc) org-stored-links))
  8824. (car org-stored-links)))))
  8825. (defun org-store-link-props (&rest plist)
  8826. "Store link properties.
  8827. The properties are pre-processed by extracting names, addresses
  8828. and dates."
  8829. (let ((x (plist-get plist :from)))
  8830. (when x
  8831. (let ((adr (mail-extract-address-components x)))
  8832. (setq plist (plist-put plist :fromname (car adr)))
  8833. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8834. (let ((x (plist-get plist :to)))
  8835. (when x
  8836. (let ((adr (mail-extract-address-components x)))
  8837. (setq plist (plist-put plist :toname (car adr)))
  8838. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8839. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8840. (when x
  8841. (setq plist (plist-put plist :date-timestamp
  8842. (format-time-string
  8843. (org-time-stamp-format t) x)))
  8844. (setq plist (plist-put plist :date-timestamp-inactive
  8845. (format-time-string
  8846. (org-time-stamp-format t t) x)))))
  8847. (let ((from (plist-get plist :from))
  8848. (to (plist-get plist :to)))
  8849. (when (and from to org-from-is-user-regexp)
  8850. (setq plist
  8851. (plist-put plist :fromto
  8852. (if (string-match org-from-is-user-regexp from)
  8853. (concat "to %t")
  8854. (concat "from %f"))))))
  8855. (setq org-store-link-plist plist))
  8856. (defun org-add-link-props (&rest plist)
  8857. "Add these properties to the link property list."
  8858. (let (key value)
  8859. (while plist
  8860. (setq key (pop plist) value (pop plist))
  8861. (setq org-store-link-plist
  8862. (plist-put org-store-link-plist key value)))))
  8863. (defun org-email-link-description (&optional fmt)
  8864. "Return the description part of an email link.
  8865. This takes information from `org-store-link-plist' and formats it
  8866. according to FMT (default from `org-email-link-description-format')."
  8867. (setq fmt (or fmt org-email-link-description-format))
  8868. (let* ((p org-store-link-plist)
  8869. (to (plist-get p :toaddress))
  8870. (from (plist-get p :fromaddress))
  8871. (table
  8872. (list
  8873. (cons "%c" (plist-get p :fromto))
  8874. (cons "%F" (plist-get p :from))
  8875. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8876. (cons "%T" (plist-get p :to))
  8877. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8878. (cons "%s" (plist-get p :subject))
  8879. (cons "%d" (plist-get p :date))
  8880. (cons "%m" (plist-get p :message-id)))))
  8881. (when (string-match "%c" fmt)
  8882. ;; Check if the user wrote this message
  8883. (if (and org-from-is-user-regexp from to
  8884. (save-match-data (string-match org-from-is-user-regexp from)))
  8885. (setq fmt (replace-match "to %t" t t fmt))
  8886. (setq fmt (replace-match "from %f" t t fmt))))
  8887. (org-replace-escapes fmt table)))
  8888. (defun org-make-org-heading-search-string (&optional string)
  8889. "Make search string for the current headline or STRING."
  8890. (let ((s (or string
  8891. (and (derived-mode-p 'org-mode)
  8892. (save-excursion
  8893. (org-back-to-heading t)
  8894. (org-element-property :raw-value (org-element-at-point))))))
  8895. (lines org-context-in-file-links))
  8896. (unless string (setq s (concat "*" s))) ;Add * for headlines
  8897. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8898. (when (and string (integerp lines) (> lines 0))
  8899. (let ((slines (org-split-string s "\n")))
  8900. (when (< lines (length slines))
  8901. (setq s (mapconcat
  8902. 'identity
  8903. (reverse (nthcdr (- (length slines) lines)
  8904. (reverse slines))) "\n")))))
  8905. (mapconcat #'identity (split-string s) " ")))
  8906. (defconst org-link-escape-chars
  8907. ;;%20 %5B %5D %25
  8908. '(?\s ?\[ ?\] ?%)
  8909. "List of characters that should be escaped in a link when stored to Org.
  8910. This is the list that is used for internal purposes.")
  8911. (defun org-make-link-string (link &optional description)
  8912. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8913. (unless (org-string-nw-p link) (error "Empty link"))
  8914. (let ((uri (cond ((string-match org-link-types-re link)
  8915. (concat (match-string 1 link)
  8916. (org-link-escape (substring link (match-end 1)))))
  8917. ((or (file-name-absolute-p link)
  8918. (string-match-p "\\`\\.\\.?/" link))
  8919. (org-link-escape link))
  8920. ;; For readability, do not encode space characters
  8921. ;; in fuzzy links.
  8922. (t (org-link-escape link (remq ?\s org-link-escape-chars)))))
  8923. (description
  8924. (and (org-string-nw-p description)
  8925. ;; Remove brackets from description, as they are fatal.
  8926. (replace-regexp-in-string
  8927. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8928. (org-trim description)))))
  8929. (format "[[%s]%s]"
  8930. uri
  8931. (if description (format "[%s]" description) ""))))
  8932. (defun org-link-escape (text &optional table merge)
  8933. "Return percent escaped representation of TEXT.
  8934. TEXT is a string with the text to escape.
  8935. Optional argument TABLE is a list with characters that should be
  8936. escaped. When nil, `org-link-escape-chars' is used.
  8937. If optional argument MERGE is set, merge TABLE into
  8938. `org-link-escape-chars'."
  8939. (let ((characters-to-encode
  8940. (cond ((null table) org-link-escape-chars)
  8941. (merge (append org-link-escape-chars table))
  8942. (t table))))
  8943. (mapconcat
  8944. (lambda (c)
  8945. (if (or (memq c characters-to-encode)
  8946. (and org-url-hexify-p (or (< c 32) (> c 126))))
  8947. (mapconcat (lambda (e) (format "%%%.2X" e))
  8948. (or (encode-coding-char c 'utf-8)
  8949. (error "Unable to percent escape character: %c" c))
  8950. "")
  8951. (char-to-string c)))
  8952. text "")))
  8953. (defun org-link-unescape (str)
  8954. "Unhex hexified Unicode parts in string STR.
  8955. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  8956. reciprocal of `org-link-escape', which see."
  8957. (if (org-string-nw-p str)
  8958. (replace-regexp-in-string
  8959. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  8960. str))
  8961. (defun org-link-unescape-compound (hex)
  8962. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8963. Note: this function also decodes single byte encodings like
  8964. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8965. (save-match-data
  8966. (let* ((bytes (cdr (split-string hex "%")))
  8967. (ret "")
  8968. (eat 0)
  8969. (sum 0))
  8970. (while bytes
  8971. (let* ((val (string-to-number (pop bytes) 16))
  8972. (shift-xor
  8973. (if (= 0 eat)
  8974. (cond
  8975. ((>= val 252) (cons 6 252))
  8976. ((>= val 248) (cons 5 248))
  8977. ((>= val 240) (cons 4 240))
  8978. ((>= val 224) (cons 3 224))
  8979. ((>= val 192) (cons 2 192))
  8980. (t (cons 0 0)))
  8981. (cons 6 128))))
  8982. (when (>= val 192) (setq eat (car shift-xor)))
  8983. (setq val (logxor val (cdr shift-xor)))
  8984. (setq sum (+ (lsh sum (car shift-xor)) val))
  8985. (when (> eat 0) (setq eat (- eat 1)))
  8986. (cond
  8987. ((= 0 eat) ;multi byte
  8988. (setq ret (concat ret (char-to-string sum)))
  8989. (setq sum 0))
  8990. ((not bytes) ; single byte(s)
  8991. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  8992. ret)))
  8993. (defun org-link-unescape-single-byte-sequence (hex)
  8994. "Unhexify hex-encoded single byte character sequences."
  8995. (mapconcat (lambda (byte)
  8996. (char-to-string (string-to-number byte 16)))
  8997. (cdr (split-string hex "%")) ""))
  8998. (defun org-xor (a b)
  8999. "Exclusive or."
  9000. (if a (not b) b))
  9001. (defun org-fixup-message-id-for-http (s)
  9002. "Replace special characters in a message id, so it can be used in an http query."
  9003. (when (string-match "%" s)
  9004. (setq s (mapconcat (lambda (c)
  9005. (if (eq c ?%)
  9006. "%25"
  9007. (char-to-string c)))
  9008. s "")))
  9009. (while (string-match "<" s)
  9010. (setq s (replace-match "%3C" t t s)))
  9011. (while (string-match ">" s)
  9012. (setq s (replace-match "%3E" t t s)))
  9013. (while (string-match "@" s)
  9014. (setq s (replace-match "%40" t t s)))
  9015. s)
  9016. (defun org-link-prettify (link)
  9017. "Return a human-readable representation of LINK.
  9018. The car of LINK must be a raw link.
  9019. The cdr of LINK must be either a link description or nil."
  9020. (let ((desc (or (cadr link) "<no description>")))
  9021. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9022. "<" (car link) ">")))
  9023. ;;;###autoload
  9024. (defun org-insert-link-global ()
  9025. "Insert a link like Org mode does.
  9026. This command can be called in any mode to insert a link in Org syntax."
  9027. (interactive)
  9028. (org-load-modules-maybe)
  9029. (org-run-like-in-org-mode 'org-insert-link))
  9030. (defun org-insert-all-links (arg &optional pre post)
  9031. "Insert all links in `org-stored-links'.
  9032. When a universal prefix, do not delete the links from `org-stored-links'.
  9033. When `ARG' is a number, insert the last N link(s).
  9034. `PRE' and `POST' are optional arguments to define a string to
  9035. prepend or to append."
  9036. (interactive "P")
  9037. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9038. (links (copy-sequence org-stored-links))
  9039. (pr (or pre "- "))
  9040. (po (or post "\n"))
  9041. (cnt 1) l)
  9042. (if (null org-stored-links)
  9043. (message "No link to insert")
  9044. (while (and (or (listp arg) (>= arg cnt))
  9045. (setq l (if (listp arg)
  9046. (pop links)
  9047. (pop org-stored-links))))
  9048. (setq cnt (1+ cnt))
  9049. (insert pr)
  9050. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9051. (insert po)))))
  9052. (defun org-insert-last-stored-link (arg)
  9053. "Insert the last link stored in `org-stored-links'."
  9054. (interactive "p")
  9055. (org-insert-all-links arg "" "\n"))
  9056. (defun org-link-fontify-links-to-this-file ()
  9057. "Fontify links to the current file in `org-stored-links'."
  9058. (let ((f (buffer-file-name)) a b)
  9059. (setq a (mapcar (lambda(l)
  9060. (let ((ll (car l)))
  9061. (when (and (string-match "^file:\\(.+\\)::" ll)
  9062. (equal f (expand-file-name (match-string 1 ll))))
  9063. ll)))
  9064. org-stored-links))
  9065. (when (featurep 'org-id)
  9066. (setq b (mapcar (lambda(l)
  9067. (let ((ll (car l)))
  9068. (when (and (string-match "^id:\\(.+\\)$" ll)
  9069. (equal f (expand-file-name
  9070. (or (org-id-find-id-file
  9071. (match-string 1 ll)) ""))))
  9072. ll)))
  9073. org-stored-links)))
  9074. (mapcar (lambda(l)
  9075. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9076. (delq nil (append a b)))))
  9077. (defvar org--links-history nil)
  9078. (defun org-insert-link (&optional complete-file link-location default-description)
  9079. "Insert a link. At the prompt, enter the link.
  9080. Completion can be used to insert any of the link protocol prefixes in use.
  9081. The history can be used to select a link previously stored with
  9082. `org-store-link'. When the empty string is entered (i.e. if you just
  9083. press `RET' at the prompt), the link defaults to the most recently
  9084. stored link. As `SPC' triggers completion in the minibuffer, you need to
  9085. use `M-SPC' or `C-q SPC' to force the insertion of a space character.
  9086. You will also be prompted for a description, and if one is given, it will
  9087. be displayed in the buffer instead of the link.
  9088. If there is already a link at point, this command will allow you to edit
  9089. link and description parts.
  9090. With a `\\[universal-argument]' prefix, prompts for a file to link to. The \
  9091. file name can be
  9092. selected using completion. The path to the file will be relative to the
  9093. current directory if the file is in the current directory or a subdirectory.
  9094. Otherwise, the link will be the absolute path as completed in the minibuffer
  9095. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9096. option `org-link-file-path-type'.
  9097. With a `\\[universal-argument] \\[universal-argument]' prefix, enforce an \
  9098. absolute path even if the file is in
  9099. the current directory or below.
  9100. A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  9101. prefix negates `org-keep-stored-link-after-insertion'.
  9102. If the LINK-LOCATION parameter is non-nil, this value will be used as
  9103. the link location instead of reading one interactively.
  9104. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9105. be used as the default description. Otherwise, if
  9106. `org-make-link-description-function' is non-nil, this function
  9107. will be called with the link target, and the result will be the
  9108. default link description."
  9109. (interactive "P")
  9110. (let* ((wcf (current-window-configuration))
  9111. (origbuf (current-buffer))
  9112. (region (when (org-region-active-p)
  9113. (buffer-substring (region-beginning) (region-end))))
  9114. (remove (and region (list (region-beginning) (region-end))))
  9115. (desc region)
  9116. (link link-location)
  9117. (abbrevs org-link-abbrev-alist-local)
  9118. entry all-prefixes auto-desc)
  9119. (cond
  9120. (link-location) ; specified by arg, just use it.
  9121. ((org-in-regexp org-bracket-link-regexp 1)
  9122. ;; We do have a link at point, and we are going to edit it.
  9123. (setq remove (list (match-beginning 0) (match-end 0)))
  9124. (setq desc (when (match-end 3) (match-string-no-properties 3)))
  9125. (setq link (read-string "Link: "
  9126. (org-link-unescape
  9127. (match-string-no-properties 1)))))
  9128. ((or (org-in-regexp org-angle-link-re)
  9129. (org-in-regexp org-plain-link-re))
  9130. ;; Convert to bracket link
  9131. (setq remove (list (match-beginning 0) (match-end 0))
  9132. link (read-string "Link: "
  9133. (org-unbracket-string "<" ">" (match-string 0)))))
  9134. ((member complete-file '((4) (16)))
  9135. ;; Completing read for file names.
  9136. (setq link (org-file-complete-link complete-file)))
  9137. (t
  9138. ;; Read link, with completion for stored links.
  9139. (org-link-fontify-links-to-this-file)
  9140. (org-switch-to-buffer-other-window "*Org Links*")
  9141. (with-current-buffer "*Org Links*"
  9142. (erase-buffer)
  9143. (insert "Insert a link.
  9144. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9145. (when org-stored-links
  9146. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9147. (insert (mapconcat 'org-link-prettify
  9148. (reverse org-stored-links) "\n")))
  9149. (goto-char (point-min)))
  9150. (let ((cw (selected-window)))
  9151. (select-window (get-buffer-window "*Org Links*" 'visible))
  9152. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9153. (unless (pos-visible-in-window-p (point-max))
  9154. (org-fit-window-to-buffer))
  9155. (and (window-live-p cw) (select-window cw)))
  9156. (setq all-prefixes (append (mapcar 'car abbrevs)
  9157. (mapcar 'car org-link-abbrev-alist)
  9158. (org-link-types)))
  9159. (unwind-protect
  9160. ;; Fake a link history, containing the stored links.
  9161. (let ((org--links-history
  9162. (append (mapcar #'car org-stored-links)
  9163. org-insert-link-history)))
  9164. (setq link
  9165. (org-completing-read
  9166. "Link: "
  9167. (append
  9168. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9169. (mapcar #'car org-stored-links))
  9170. nil nil nil
  9171. 'org--links-history
  9172. (caar org-stored-links)))
  9173. (unless (org-string-nw-p link) (user-error "No link selected"))
  9174. (dolist (l org-stored-links)
  9175. (when (equal link (cadr l))
  9176. (setq link (car l))
  9177. (setq auto-desc t)))
  9178. (when (or (member link all-prefixes)
  9179. (and (equal ":" (substring link -1))
  9180. (member (substring link 0 -1) all-prefixes)
  9181. (setq link (substring link 0 -1))))
  9182. (setq link (with-current-buffer origbuf
  9183. (org-link-try-special-completion link)))))
  9184. (set-window-configuration wcf)
  9185. (kill-buffer "*Org Links*"))
  9186. (setq entry (assoc link org-stored-links))
  9187. (or entry (push link org-insert-link-history))
  9188. (setq desc (or desc (nth 1 entry)))))
  9189. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9190. (not org-keep-stored-link-after-insertion))
  9191. (setq org-stored-links (delq (assoc link org-stored-links)
  9192. org-stored-links)))
  9193. (when (and (string-match org-plain-link-re link)
  9194. (not (string-match org-ts-regexp link)))
  9195. ;; URL-like link, normalize the use of angular brackets.
  9196. (setq link (org-unbracket-string "<" ">" link)))
  9197. ;; Check if we are linking to the current file with a search
  9198. ;; option If yes, simplify the link by using only the search
  9199. ;; option.
  9200. (when (and buffer-file-name
  9201. (let ((case-fold-search nil))
  9202. (string-match "\\`file:\\(.+?\\)::" link)))
  9203. (let ((path (match-string-no-properties 1 link))
  9204. (search (substring-no-properties link (match-end 0))))
  9205. (save-match-data
  9206. (when (equal (file-truename buffer-file-name) (file-truename path))
  9207. ;; We are linking to this same file, with a search option
  9208. (setq link search)))))
  9209. ;; Check if we can/should use a relative path. If yes, simplify
  9210. ;; the link.
  9211. (let ((case-fold-search nil))
  9212. (when (string-match "\\`\\(file\\|docview\\):" link)
  9213. (let* ((type (match-string-no-properties 0 link))
  9214. (path-start (match-end 0))
  9215. (search (and (string-match "::\\(.*\\)\\'" link)
  9216. (match-string 1 link)))
  9217. (path
  9218. (if search
  9219. (substring-no-properties
  9220. link path-start (match-beginning 0))
  9221. (substring-no-properties link (match-end 0))))
  9222. (origpath path))
  9223. (cond
  9224. ((or (eq org-link-file-path-type 'absolute)
  9225. (equal complete-file '(16)))
  9226. (setq path (abbreviate-file-name (expand-file-name path))))
  9227. ((eq org-link-file-path-type 'noabbrev)
  9228. (setq path (expand-file-name path)))
  9229. ((eq org-link-file-path-type 'relative)
  9230. (setq path (file-relative-name path)))
  9231. (t
  9232. (save-match-data
  9233. (if (string-match (concat "^" (regexp-quote
  9234. (expand-file-name
  9235. (file-name-as-directory
  9236. default-directory))))
  9237. (expand-file-name path))
  9238. ;; We are linking a file with relative path name.
  9239. (setq path (substring (expand-file-name path)
  9240. (match-end 0)))
  9241. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9242. (setq link (concat type path (and search (concat "::" search))))
  9243. (when (equal desc origpath)
  9244. (setq desc path)))))
  9245. (unless auto-desc
  9246. (let ((initial-input
  9247. (cond
  9248. (default-description)
  9249. ((not org-make-link-description-function) desc)
  9250. (t (condition-case nil
  9251. (funcall org-make-link-description-function link desc)
  9252. (error
  9253. (message "Can't get link description from `%s'"
  9254. (symbol-name org-make-link-description-function))
  9255. (sit-for 2)
  9256. nil))))))
  9257. (setq desc (read-string "Description: " initial-input))))
  9258. (unless (string-match "\\S-" desc) (setq desc nil))
  9259. (when remove (apply 'delete-region remove))
  9260. (insert (org-make-link-string link desc))
  9261. ;; Redisplay so as the new link has proper invisible characters.
  9262. (sit-for 0)))
  9263. (defun org-link-try-special-completion (type)
  9264. "If there is completion support for link type TYPE, offer it."
  9265. (let ((fun (org-link-get-parameter type :complete)))
  9266. (if (functionp fun)
  9267. (funcall fun)
  9268. (read-string "Link (no completion support): " (concat type ":")))))
  9269. (defun org-file-complete-link (&optional arg)
  9270. "Create a file link using completion."
  9271. (let ((file (read-file-name "File: "))
  9272. (pwd (file-name-as-directory (expand-file-name ".")))
  9273. (pwd1 (file-name-as-directory (abbreviate-file-name
  9274. (expand-file-name ".")))))
  9275. (cond ((equal arg '(16))
  9276. (concat "file:"
  9277. (abbreviate-file-name (expand-file-name file))))
  9278. ((string-match
  9279. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9280. (concat "file:" (match-string 1 file)))
  9281. ((string-match
  9282. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9283. (expand-file-name file))
  9284. (concat "file:"
  9285. (match-string 1 (expand-file-name file))))
  9286. (t (concat "file:" file)))))
  9287. (defun org-completing-read (&rest args)
  9288. "Completing-read with SPACE being a normal character."
  9289. (let ((enable-recursive-minibuffers t)
  9290. (minibuffer-local-completion-map
  9291. (copy-keymap minibuffer-local-completion-map)))
  9292. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9293. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9294. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9295. 'org-time-stamp-inactive)
  9296. (apply #'completing-read args)))
  9297. ;;; Opening/following a link
  9298. (defvar org-link-search-failed nil)
  9299. (defvar org-open-link-functions nil
  9300. "Hook for functions finding a plain text link.
  9301. These functions must take a single argument, the link content.
  9302. They will be called for links that look like [[link text][description]]
  9303. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9304. like a filename (e.g. \"./blue.png\").
  9305. These functions will be called *before* Org attempts to resolve the
  9306. link by doing text searches in the current buffer - so if you want a
  9307. link \"[[target]]\" to still find \"<<target>>\", your function should
  9308. handle this as a special case.
  9309. When the function does handle the link, it must return a non-nil value.
  9310. If it decides that it is not responsible for this link, it must return
  9311. nil to indicate that that Org can continue with other options like
  9312. exact and fuzzy text search.")
  9313. (defun org-next-link (&optional search-backward)
  9314. "Move forward to the next link.
  9315. If the link is in hidden text, expose it."
  9316. (interactive "P")
  9317. (when (and org-link-search-failed (eq this-command last-command))
  9318. (goto-char (point-min))
  9319. (message "Link search wrapped back to beginning of buffer"))
  9320. (setq org-link-search-failed nil)
  9321. (let* ((pos (point))
  9322. (ct (org-context))
  9323. (a (assq :link ct))
  9324. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9325. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9326. ((looking-at org-any-link-re)
  9327. ;; Don't stay stuck at link without an org-link face
  9328. (forward-char (if search-backward -1 1))))
  9329. (if (funcall srch-fun org-any-link-re nil t)
  9330. (progn
  9331. (goto-char (match-beginning 0))
  9332. (when (org-invisible-p) (org-show-context)))
  9333. (goto-char pos)
  9334. (setq org-link-search-failed t)
  9335. (message "No further link found"))))
  9336. (defun org-previous-link ()
  9337. "Move backward to the previous link.
  9338. If the link is in hidden text, expose it."
  9339. (interactive)
  9340. (funcall 'org-next-link t))
  9341. (defun org-translate-link (s)
  9342. "Translate a link string if a translation function has been defined."
  9343. (with-temp-buffer
  9344. (insert (org-trim s))
  9345. (org-trim (org-element-interpret-data (org-element-context)))))
  9346. (defun org-translate-link-from-planner (type path)
  9347. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9348. This is still an experimental function, your mileage may vary."
  9349. (cond
  9350. ((member type '("http" "https" "news" "ftp"))
  9351. ;; standard Internet links are the same.
  9352. nil)
  9353. ((and (equal type "irc") (string-match "^//" path))
  9354. ;; Planner has two / at the beginning of an irc link, we have 1.
  9355. ;; We should have zero, actually....
  9356. (setq path (substring path 1)))
  9357. ((and (equal type "lisp") (string-match "^/" path))
  9358. ;; Planner has a slash, we do not.
  9359. (setq type "elisp" path (substring path 1)))
  9360. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9361. ;; A typical message link. Planner has the id after the final slash,
  9362. ;; we separate it with a hash mark
  9363. (setq path (concat (match-string 1 path) "#"
  9364. (org-unbracket-string "<" ">" (match-string 2 path))))))
  9365. (cons type path))
  9366. (defun org-find-file-at-mouse (ev)
  9367. "Open file link or URL at mouse."
  9368. (interactive "e")
  9369. (mouse-set-point ev)
  9370. (org-open-at-point 'in-emacs))
  9371. (defun org-open-at-mouse (ev)
  9372. "Open file link or URL at mouse.
  9373. See the docstring of `org-open-file' for details."
  9374. (interactive "e")
  9375. (mouse-set-point ev)
  9376. (when (eq major-mode 'org-agenda-mode)
  9377. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9378. (org-open-at-point))
  9379. (defvar org-window-config-before-follow-link nil
  9380. "The window configuration before following a link.
  9381. This is saved in case the need arises to restore it.")
  9382. ;;;###autoload
  9383. (defun org-open-at-point-global ()
  9384. "Follow a link or time-stamp like Org mode does.
  9385. This command can be called in any mode to follow an external link
  9386. or a time-stamp that has Org mode syntax. Its behavior is
  9387. undefined when called on internal links (e.g., fuzzy links).
  9388. Raise an error when there is nothing to follow. "
  9389. (interactive)
  9390. (cond ((org-in-regexp org-any-link-re)
  9391. (org-open-link-from-string (match-string-no-properties 0)))
  9392. ((or (org-in-regexp org-ts-regexp-both nil t)
  9393. (org-in-regexp org-tsr-regexp-both nil t))
  9394. (org-follow-timestamp-link))
  9395. (t (user-error "No link found"))))
  9396. ;;;###autoload
  9397. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9398. "Open a link in the string S, as if it was in Org mode."
  9399. (interactive "sLink: \nP")
  9400. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9401. (with-temp-buffer
  9402. (let ((org-inhibit-startup (not reference-buffer)))
  9403. (org-mode)
  9404. (insert s)
  9405. (goto-char (point-min))
  9406. (when reference-buffer
  9407. (setq org-link-abbrev-alist-local
  9408. (with-current-buffer reference-buffer
  9409. org-link-abbrev-alist-local)))
  9410. (org-open-at-point arg reference-buffer)))))
  9411. (defvar org-open-at-point-functions nil
  9412. "Hook that is run when following a link at point.
  9413. Functions in this hook must return t if they identify and follow
  9414. a link at point. If they don't find anything interesting at point,
  9415. they must return nil.")
  9416. (defvar org-link-search-inhibit-query nil)
  9417. (defvar clean-buffer-list-kill-buffer-names) ;Defined in midnight.el
  9418. (defun org--open-doi-link (path)
  9419. "Open a \"doi\" type link.
  9420. PATH is a the path to search for, as a string."
  9421. (browse-url (url-encode-url (concat org-doi-server-url path))))
  9422. (defun org--open-elisp-link (path)
  9423. "Open a \"elisp\" type link.
  9424. PATH is the sexp to evaluate, as a string."
  9425. (let ((cmd path))
  9426. (if (or (and (org-string-nw-p
  9427. org-confirm-elisp-link-not-regexp)
  9428. (string-match-p org-confirm-elisp-link-not-regexp cmd))
  9429. (not org-confirm-elisp-link-function)
  9430. (funcall org-confirm-elisp-link-function
  9431. (format "Execute \"%s\" as elisp? "
  9432. (org-add-props cmd nil 'face 'org-warning))))
  9433. (message "%s => %s" cmd
  9434. (if (eq (string-to-char cmd) ?\()
  9435. (eval (read cmd))
  9436. (call-interactively (read cmd))))
  9437. (user-error "Abort"))))
  9438. (defun org--open-help-link (path)
  9439. "Open a \"help\" type link.
  9440. PATH is a symbol name, as a string."
  9441. (pcase (intern path)
  9442. ((and (pred fboundp) variable) (describe-function variable))
  9443. ((and (pred boundp) function) (describe-variable function))
  9444. (name (user-error "Unknown function or variable: %s" name))))
  9445. (defun org--open-shell-link (path)
  9446. "Open a \"shell\" type link.
  9447. PATH is the command to execute, as a string."
  9448. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9449. (cmd path))
  9450. (if (or (and (org-string-nw-p
  9451. org-confirm-shell-link-not-regexp)
  9452. (string-match
  9453. org-confirm-shell-link-not-regexp cmd))
  9454. (not org-confirm-shell-link-function)
  9455. (funcall org-confirm-shell-link-function
  9456. (format "Execute \"%s\" in shell? "
  9457. (org-add-props cmd nil
  9458. 'face 'org-warning))))
  9459. (progn
  9460. (message "Executing %s" cmd)
  9461. (shell-command cmd buf)
  9462. (when (featurep 'midnight)
  9463. (setq clean-buffer-list-kill-buffer-names
  9464. (cons (buffer-name buf)
  9465. clean-buffer-list-kill-buffer-names))))
  9466. (user-error "Abort"))))
  9467. (defun org-open-at-point (&optional arg reference-buffer)
  9468. "Open link, timestamp, footnote or tags at point.
  9469. When point is on a link, follow it. Normally, files will be
  9470. opened by an appropriate application. If the optional prefix
  9471. argument ARG is non-nil, Emacs will visit the file. With
  9472. a double prefix argument, try to open outside of Emacs, in the
  9473. application the system uses for this file type.
  9474. When point is on a timestamp, open the agenda at the day
  9475. specified.
  9476. When point is a footnote definition, move to the first reference
  9477. found. If it is on a reference, move to the associated
  9478. definition.
  9479. When point is on a headline, display a list of every link in the
  9480. entry, so it is possible to pick one, or all, of them. If point
  9481. is on a tag, call `org-tags-view' instead.
  9482. When optional argument REFERENCE-BUFFER is non-nil, it should
  9483. specify a buffer from where the link search should happen. This
  9484. is used internally by `org-open-link-from-string'.
  9485. On top of syntactically correct links, this function will also
  9486. try to open links and time-stamps in comments, example
  9487. blocks... i.e., whenever point is on something looking like
  9488. a timestamp or a link."
  9489. (interactive "P")
  9490. ;; On a code block, open block's results.
  9491. (unless (call-interactively 'org-babel-open-src-block-result)
  9492. (org-load-modules-maybe)
  9493. (setq org-window-config-before-follow-link (current-window-configuration))
  9494. (org-remove-occur-highlights nil nil t)
  9495. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9496. (let* ((context
  9497. ;; Only consider supported types, even if they are not
  9498. ;; the closest one.
  9499. (org-element-lineage
  9500. (org-element-context)
  9501. '(clock footnote-definition footnote-reference headline
  9502. inlinetask link timestamp)
  9503. t))
  9504. (type (org-element-type context))
  9505. (value (org-element-property :value context)))
  9506. (cond
  9507. ;; On a headline or an inlinetask, but not on a timestamp,
  9508. ;; a link, a footnote reference.
  9509. ((memq type '(headline inlinetask))
  9510. (org-match-line org-complex-heading-regexp)
  9511. (if (and (match-beginning 5)
  9512. (>= (point) (match-beginning 5))
  9513. (< (point) (match-end 5)))
  9514. ;; On tags.
  9515. (org-tags-view arg (substring (match-string 5) 0 -1))
  9516. ;; Not on tags.
  9517. (pcase (org-offer-links-in-entry (current-buffer) (point) arg)
  9518. (`(nil . ,_)
  9519. (require 'org-attach)
  9520. (org-attach-reveal 'if-exists))
  9521. (`(,links . ,links-end)
  9522. (dolist (link (if (stringp links) (list links) links))
  9523. (search-forward link nil links-end)
  9524. (goto-char (match-beginning 0))
  9525. (org-open-at-point))))))
  9526. ;; On a footnote reference or at definition's label.
  9527. ((or (eq type 'footnote-reference)
  9528. (and (eq type 'footnote-definition)
  9529. (save-excursion
  9530. ;; Do not validate action when point is on the
  9531. ;; spaces right after the footnote label, in
  9532. ;; order to be on par with behaviour on links.
  9533. (skip-chars-forward " \t")
  9534. (let ((begin
  9535. (org-element-property :contents-begin context)))
  9536. (if begin (< (point) begin)
  9537. (= (org-element-property :post-affiliated context)
  9538. (line-beginning-position)))))))
  9539. (org-footnote-action))
  9540. ;; No valid context. Ignore catch-all types like `headline'.
  9541. ;; If point is on something looking like a link or
  9542. ;; a time-stamp, try opening it. It may be useful in
  9543. ;; comments, example blocks...
  9544. ((memq type '(footnote-definition headline inlinetask nil))
  9545. (call-interactively #'org-open-at-point-global))
  9546. ;; On a clock line, make sure point is on the timestamp
  9547. ;; before opening it.
  9548. ((and (eq type 'clock)
  9549. value
  9550. (>= (point) (org-element-property :begin value))
  9551. (<= (point) (org-element-property :end value)))
  9552. (org-follow-timestamp-link))
  9553. ;; Do nothing on white spaces after an object.
  9554. ((>= (point)
  9555. (save-excursion
  9556. (goto-char (org-element-property :end context))
  9557. (skip-chars-backward " \t")
  9558. (point)))
  9559. (user-error "No link found"))
  9560. ((eq type 'timestamp) (org-follow-timestamp-link))
  9561. ((eq type 'link)
  9562. (let ((type (org-element-property :type context))
  9563. (path (org-link-unescape (org-element-property :path context))))
  9564. ;; Switch back to REFERENCE-BUFFER needed when called in
  9565. ;; a temporary buffer through `org-open-link-from-string'.
  9566. (with-current-buffer (or reference-buffer (current-buffer))
  9567. (cond
  9568. ((equal type "file")
  9569. (if (string-match "[*?{]" (file-name-nondirectory path))
  9570. (dired path)
  9571. ;; Look into `org-link-parameters' in order to find
  9572. ;; a DEDICATED-FUNCTION to open file. The function
  9573. ;; will be applied on raw link instead of parsed
  9574. ;; link due to the limitation in `org-add-link-type'
  9575. ;; ("open" function called with a single argument).
  9576. ;; If no such function is found, fallback to
  9577. ;; `org-open-file'.
  9578. (let* ((option (org-element-property :search-option context))
  9579. (app (org-element-property :application context))
  9580. (dedicated-function
  9581. (org-link-get-parameter
  9582. (if app (concat type "+" app) type)
  9583. :follow)))
  9584. (if dedicated-function
  9585. (funcall dedicated-function
  9586. (concat path
  9587. (and option (concat "::" option))))
  9588. (apply #'org-open-file
  9589. path
  9590. (cond (arg)
  9591. ((equal app "emacs") 'emacs)
  9592. ((equal app "sys") 'system))
  9593. (cond ((not option) nil)
  9594. ((string-match-p "\\`[0-9]+\\'" option)
  9595. (list (string-to-number option)))
  9596. (t (list nil
  9597. (org-link-unescape option)))))))))
  9598. ((functionp (org-link-get-parameter type :follow))
  9599. (funcall (org-link-get-parameter type :follow) path))
  9600. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9601. (unless (run-hook-with-args-until-success
  9602. 'org-open-link-functions path)
  9603. (if (not arg) (org-mark-ring-push)
  9604. (switch-to-buffer-other-window
  9605. (org-get-buffer-for-internal-link (current-buffer))))
  9606. (let ((destination
  9607. (org-with-wide-buffer
  9608. (if (equal type "radio")
  9609. (org-search-radio-target
  9610. (org-element-property :path context))
  9611. (org-link-search
  9612. (if (member type '("custom-id" "coderef"))
  9613. (org-element-property :raw-link context)
  9614. path)
  9615. ;; Prevent fuzzy links from matching
  9616. ;; themselves.
  9617. (and (equal type "fuzzy")
  9618. (+ 2 (org-element-property :begin context)))))
  9619. (point))))
  9620. (unless (and (<= (point-min) destination)
  9621. (>= (point-max) destination))
  9622. (widen))
  9623. (goto-char destination))))
  9624. (t (browse-url-at-point))))))
  9625. (t (user-error "No link found")))))
  9626. (run-hook-with-args 'org-follow-link-hook)))
  9627. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9628. "Offer links in the current entry and return the selected link.
  9629. If there is only one link, return it.
  9630. If NTH is an integer, return the NTH link found.
  9631. If ZERO is a string, check also this string for a link, and if
  9632. there is one, return it."
  9633. (with-current-buffer buffer
  9634. (org-with-wide-buffer
  9635. (goto-char marker)
  9636. (let ((cnt ?0)
  9637. have-zero end links link c)
  9638. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9639. (push (match-string 0 zero) links)
  9640. (setq cnt (1- cnt) have-zero t))
  9641. (save-excursion
  9642. (org-back-to-heading t)
  9643. (setq end (save-excursion (outline-next-heading) (point)))
  9644. (while (re-search-forward org-any-link-re end t)
  9645. (push (match-string 0) links))
  9646. (setq links (org-uniquify (reverse links))))
  9647. (cond
  9648. ((null links)
  9649. (message "No links"))
  9650. ((equal (length links) 1)
  9651. (setq link (car links)))
  9652. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9653. (setq link (nth (if have-zero nth (1- nth)) links)))
  9654. (t ; we have to select a link
  9655. (save-excursion
  9656. (save-window-excursion
  9657. (delete-other-windows)
  9658. (with-output-to-temp-buffer "*Select Link*"
  9659. (dolist (l links)
  9660. (cond
  9661. ((not (string-match org-bracket-link-regexp l))
  9662. (princ (format "[%c] %s\n" (cl-incf cnt)
  9663. (org-unbracket-string "<" ">" l))))
  9664. ((match-end 3)
  9665. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9666. (match-string 3 l) (match-string 1 l))))
  9667. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9668. (match-string 1 l)))))))
  9669. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9670. (message "Select link to open, RET to open all:")
  9671. (setq c (read-char-exclusive))
  9672. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9673. (when (equal c ?q) (user-error "Abort"))
  9674. (if (equal c ?\C-m)
  9675. (setq link links)
  9676. (setq nth (- c ?0))
  9677. (when have-zero (setq nth (1+ nth)))
  9678. (unless (and (integerp nth) (>= (length links) nth))
  9679. (user-error "Invalid link selection"))
  9680. (setq link (nth (1- nth) links)))))
  9681. (cons link end)))))
  9682. ;; TODO: These functions are deprecated since `org-open-at-point'
  9683. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9684. (defun org-open-file-with-system (path)
  9685. "Open file at PATH using the system way of opening it."
  9686. (org-open-file path 'system))
  9687. (defun org-open-file-with-emacs (path)
  9688. "Open file at PATH in Emacs."
  9689. (org-open-file path 'emacs))
  9690. ;;; File search
  9691. (defvar org-create-file-search-functions nil
  9692. "List of functions to construct the right search string for a file link.
  9693. These functions are called in turn with point at the location to
  9694. which the link should point.
  9695. A function in the hook should first test if it would like to
  9696. handle this file type, for example by checking the `major-mode'
  9697. or the file extension. If it decides not to handle this file, it
  9698. should just return nil to give other functions a chance. If it
  9699. does handle the file, it must return the search string to be used
  9700. when following the link. The search string will be part of the
  9701. file link, given after a double colon, and `org-open-at-point'
  9702. will automatically search for it. If special measures must be
  9703. taken to make the search successful, another function should be
  9704. added to the companion hook `org-execute-file-search-functions',
  9705. which see.
  9706. A function in this hook may also use `setq' to set the variable
  9707. `description' to provide a suggestion for the descriptive text to
  9708. be used for this link when it gets inserted into an Org buffer
  9709. with \\[org-insert-link].")
  9710. (defvar org-execute-file-search-functions nil
  9711. "List of functions to execute a file search triggered by a link.
  9712. Functions added to this hook must accept a single argument, the
  9713. search string that was part of the file link, the part after the
  9714. double colon. The function must first check if it would like to
  9715. handle this search, for example by checking the `major-mode' or
  9716. the file extension. If it decides not to handle this search, it
  9717. should just return nil to give other functions a chance. If it
  9718. does handle the search, it must return a non-nil value to keep
  9719. other functions from trying.
  9720. Each function can access the current prefix argument through the
  9721. variable `current-prefix-arg'. Note that a single prefix is used
  9722. to force opening a link in Emacs, so it may be good to only use a
  9723. numeric or double prefix to guide the search function.
  9724. In case this is needed, a function in this hook can also restore
  9725. the window configuration before `org-open-at-point' was called using:
  9726. (set-window-configuration org-window-config-before-follow-link)")
  9727. (defun org-search-radio-target (target)
  9728. "Search a radio target matching TARGET in current buffer.
  9729. White spaces are not significant."
  9730. (let ((re (format "<<<%s>>>"
  9731. (mapconcat #'regexp-quote
  9732. (split-string target)
  9733. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9734. (origin (point)))
  9735. (goto-char (point-min))
  9736. (catch :radio-match
  9737. (while (re-search-forward re nil t)
  9738. (backward-char)
  9739. (let ((object (org-element-context)))
  9740. (when (eq (org-element-type object) 'radio-target)
  9741. (goto-char (org-element-property :begin object))
  9742. (org-show-context 'link-search)
  9743. (throw :radio-match nil))))
  9744. (goto-char origin)
  9745. (user-error "No match for radio target: %s" target))))
  9746. (defun org-link-search (s &optional avoid-pos stealth)
  9747. "Search for a search string S.
  9748. If S starts with \"#\", it triggers a custom ID search.
  9749. If S is enclosed within parenthesis, it initiates a coderef
  9750. search.
  9751. If S is surrounded by forward slashes, it is interpreted as
  9752. a regular expression. In Org mode files, this will create an
  9753. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9754. to list matches. If the current buffer is in `dired-mode', grep
  9755. will be used to search in all files.
  9756. When AVOID-POS is given, ignore matches near that position.
  9757. When optional argument STEALTH is non-nil, do not modify
  9758. visibility around point, thus ignoring `org-show-context-detail'
  9759. variable.
  9760. Search is case-insensitive and ignores white spaces. Return type
  9761. of matched result, which is either `dedicated' or `fuzzy'."
  9762. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9763. (let* ((case-fold-search t)
  9764. (origin (point))
  9765. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9766. (starred (eq (string-to-char normalized) ?*))
  9767. (words (split-string (if starred (substring s 1) s)))
  9768. (s-multi-re (mapconcat #'regexp-quote words "\\(?:[ \t\n]+\\)"))
  9769. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9770. type)
  9771. (cond
  9772. ;; Check if there are any special search functions.
  9773. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9774. ((eq (string-to-char s) ?#)
  9775. ;; Look for a custom ID S if S starts with "#".
  9776. (let* ((id (substring normalized 1))
  9777. (match (org-find-property "CUSTOM_ID" id)))
  9778. (if match (progn (goto-char match) (setf type 'dedicated))
  9779. (error "No match for custom ID: %s" id))))
  9780. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9781. ;; Look for coderef targets if S is enclosed within parenthesis.
  9782. (let ((coderef (match-string-no-properties 1 normalized))
  9783. (re (substring s-single-re 1 -1)))
  9784. (goto-char (point-min))
  9785. (catch :coderef-match
  9786. (while (re-search-forward re nil t)
  9787. (let ((element (org-element-at-point)))
  9788. (when (and (memq (org-element-type element)
  9789. '(example-block src-block))
  9790. ;; Build proper regexp according to current
  9791. ;; block's label format.
  9792. (let ((label-fmt
  9793. (regexp-quote
  9794. (or (org-element-property :label-fmt element)
  9795. org-coderef-label-format))))
  9796. (save-excursion
  9797. (beginning-of-line)
  9798. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9799. (format label-fmt coderef))))))
  9800. (setq type 'dedicated)
  9801. (goto-char (match-beginning 1))
  9802. (throw :coderef-match nil))))
  9803. (goto-char origin)
  9804. (error "No match for coderef: %s" coderef))))
  9805. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9806. ;; Look for a regular expression.
  9807. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9808. (match-string 1 s)))
  9809. ;; From here, we handle fuzzy links.
  9810. ;;
  9811. ;; Look for targets, only if not in a headline search.
  9812. ((and (not starred)
  9813. (let ((target (format "<<%s>>" s-multi-re)))
  9814. (catch :target-match
  9815. (goto-char (point-min))
  9816. (while (re-search-forward target nil t)
  9817. (backward-char)
  9818. (let ((context (org-element-context)))
  9819. (when (eq (org-element-type context) 'target)
  9820. (setq type 'dedicated)
  9821. (goto-char (org-element-property :begin context))
  9822. (throw :target-match t))))
  9823. nil))))
  9824. ;; Look for elements named after S, only if not in a headline
  9825. ;; search.
  9826. ((and (not starred)
  9827. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9828. (catch :name-match
  9829. (goto-char (point-min))
  9830. (while (re-search-forward name nil t)
  9831. (let ((element (org-element-at-point)))
  9832. (when (equal words
  9833. (split-string
  9834. (org-element-property :name element)))
  9835. (setq type 'dedicated)
  9836. (beginning-of-line)
  9837. (throw :name-match t))))
  9838. nil))))
  9839. ;; Regular text search. Prefer headlines in Org mode buffers.
  9840. ;; Ignore COMMENT keyword, TODO keywords, priority cookies,
  9841. ;; statistics cookies and tags.
  9842. ((and (derived-mode-p 'org-mode)
  9843. (let ((title-re
  9844. (format "%s.*\\(?:%s[ \t]\\)?.*%s"
  9845. org-outline-regexp-bol
  9846. org-comment-string
  9847. (mapconcat #'regexp-quote words ".+")))
  9848. (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
  9849. (comment-re (eval-when-compile
  9850. (format "\\`%s[ \t]+" org-comment-string))))
  9851. (goto-char (point-min))
  9852. (catch :found
  9853. (while (re-search-forward title-re nil t)
  9854. (when (equal words
  9855. (split-string
  9856. (replace-regexp-in-string
  9857. cookie-re ""
  9858. (replace-regexp-in-string
  9859. comment-re "" (org-get-heading t t t)))))
  9860. (throw :found t)))
  9861. nil)))
  9862. (beginning-of-line)
  9863. (setq type 'dedicated))
  9864. ;; Offer to create non-existent headline depending on
  9865. ;; `org-link-search-must-match-exact-headline'.
  9866. ((and (derived-mode-p 'org-mode)
  9867. (not org-link-search-inhibit-query)
  9868. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9869. (yes-or-no-p "No match - create this as a new heading? "))
  9870. (goto-char (point-max))
  9871. (unless (bolp) (newline))
  9872. (org-insert-heading nil t t)
  9873. (insert s "\n")
  9874. (beginning-of-line 0))
  9875. ;; Only headlines are looked after. No need to process
  9876. ;; further: throw an error.
  9877. ((and (derived-mode-p 'org-mode)
  9878. (or starred org-link-search-must-match-exact-headline))
  9879. (goto-char origin)
  9880. (error "No match for fuzzy expression: %s" normalized))
  9881. ;; Regular text search.
  9882. ((catch :fuzzy-match
  9883. (goto-char (point-min))
  9884. (while (re-search-forward s-multi-re nil t)
  9885. ;; Skip match if it contains AVOID-POS or it is included in
  9886. ;; a link with a description but outside the description.
  9887. (unless (or (and avoid-pos
  9888. (<= (match-beginning 0) avoid-pos)
  9889. (> (match-end 0) avoid-pos))
  9890. (and (save-match-data
  9891. (org-in-regexp org-bracket-link-regexp))
  9892. (match-beginning 3)
  9893. (or (> (match-beginning 3) (point))
  9894. (<= (match-end 3) (point)))
  9895. (org-element-lineage
  9896. (save-match-data (org-element-context))
  9897. '(link) t)))
  9898. (goto-char (match-beginning 0))
  9899. (setq type 'fuzzy)
  9900. (throw :fuzzy-match t)))
  9901. nil))
  9902. ;; All failed. Throw an error.
  9903. (t (goto-char origin)
  9904. (error "No match for fuzzy expression: %s" normalized)))
  9905. ;; Disclose surroundings of match, if appropriate.
  9906. (when (and (derived-mode-p 'org-mode) (not stealth))
  9907. (org-show-context 'link-search))
  9908. type))
  9909. (defun org-get-buffer-for-internal-link (buffer)
  9910. "Return a buffer to be used for displaying the link target of internal links."
  9911. (cond
  9912. ((not org-display-internal-link-with-indirect-buffer)
  9913. buffer)
  9914. ((string-suffix-p "(Clone)" (buffer-name buffer))
  9915. (message "Buffer is already a clone, not making another one")
  9916. ;; we also do not modify visibility in this case
  9917. buffer)
  9918. (t ; make a new indirect buffer for displaying the link
  9919. (let* ((bn (buffer-name buffer))
  9920. (ibn (concat bn "(Clone)"))
  9921. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9922. (with-current-buffer ib (org-overview))
  9923. ib))))
  9924. (defun org-do-occur (regexp &optional cleanup)
  9925. "Call the Emacs command `occur'.
  9926. If CLEANUP is non-nil, remove the printout of the regular expression
  9927. in the *Occur* buffer. This is useful if the regex is long and not useful
  9928. to read."
  9929. (occur regexp)
  9930. (when cleanup
  9931. (let ((cwin (selected-window)) win beg end)
  9932. (when (setq win (get-buffer-window "*Occur*"))
  9933. (select-window win))
  9934. (goto-char (point-min))
  9935. (when (re-search-forward "match[a-z]+" nil t)
  9936. (setq beg (match-end 0))
  9937. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  9938. (setq end (1- (match-beginning 0)))))
  9939. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9940. (goto-char (point-min))
  9941. (select-window cwin))))
  9942. ;;; The mark ring for links jumps
  9943. (defvar org-mark-ring nil
  9944. "Mark ring for positions before jumps in Org mode.")
  9945. (defvar org-mark-ring-last-goto nil
  9946. "Last position in the mark ring used to go back.")
  9947. ;; Fill and close the ring
  9948. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9949. (dotimes (_ org-mark-ring-length)
  9950. (push (make-marker) org-mark-ring))
  9951. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9952. org-mark-ring)
  9953. (defun org-mark-ring-push (&optional pos buffer)
  9954. "Put the current position or POS into the mark ring and rotate it."
  9955. (interactive)
  9956. (setq pos (or pos (point)))
  9957. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9958. (move-marker (car org-mark-ring)
  9959. (or pos (point))
  9960. (or buffer (current-buffer)))
  9961. (message "%s"
  9962. (substitute-command-keys
  9963. "Position saved to mark ring, go back with \
  9964. `\\[org-mark-ring-goto]'.")))
  9965. (defun org-mark-ring-goto (&optional n)
  9966. "Jump to the previous position in the mark ring.
  9967. With prefix arg N, jump back that many stored positions. When
  9968. called several times in succession, walk through the entire ring.
  9969. Org mode commands jumping to a different position in the current file,
  9970. or to another Org file, automatically push the old position onto the ring."
  9971. (interactive "p")
  9972. (let (p m)
  9973. (if (eq last-command this-command)
  9974. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9975. (setq p org-mark-ring))
  9976. (setq org-mark-ring-last-goto p)
  9977. (setq m (car p))
  9978. (pop-to-buffer-same-window (marker-buffer m))
  9979. (goto-char m)
  9980. (when (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9981. (defun org-add-angle-brackets (s)
  9982. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  9983. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  9984. s)
  9985. ;;; Following specific links
  9986. (defvar org-agenda-buffer-tmp-name)
  9987. (defvar org-agenda-start-on-weekday)
  9988. (defun org-follow-timestamp-link ()
  9989. "Open an agenda view for the time-stamp date/range at point."
  9990. (cond
  9991. ((org-at-date-range-p t)
  9992. (let ((org-agenda-start-on-weekday)
  9993. (t1 (match-string 1))
  9994. (t2 (match-string 2)) tt1 tt2)
  9995. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9996. tt2 (time-to-days (org-time-string-to-time t2)))
  9997. (let ((org-agenda-buffer-tmp-name
  9998. (format "*Org Agenda(a:%s)"
  9999. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10000. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10001. ((org-at-timestamp-p 'lax)
  10002. (let ((org-agenda-buffer-tmp-name
  10003. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10004. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10005. (substring (match-string 1) 0 10)))
  10006. 1)))
  10007. (t (error "This should not happen"))))
  10008. ;;; Following file links
  10009. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10010. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10011. (declare-function mailcap-mime-info
  10012. "mailcap" (string &optional request no-decode))
  10013. (defvar org-wait nil)
  10014. (defun org-open-file (path &optional in-emacs line search)
  10015. "Open the file at PATH.
  10016. First, this expands any special file name abbreviations. Then the
  10017. configuration variable `org-file-apps' is checked if it contains an
  10018. entry for this file type, and if yes, the corresponding command is launched.
  10019. If no application is found, Emacs simply visits the file.
  10020. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10021. With a double \\[universal-argument] \\[universal-argument] \
  10022. prefix arg, Org tries to avoid opening in Emacs
  10023. and to use an external application to visit the file.
  10024. Optional LINE specifies a line to go to, optional SEARCH a string
  10025. to search for. If LINE or SEARCH is given, the file will be
  10026. opened in Emacs, unless an entry from org-file-apps that makes
  10027. use of groups in a regexp matches.
  10028. If you want to change the way frames are used when following a
  10029. link, please customize `org-link-frame-setup'.
  10030. If the file does not exist, an error is thrown."
  10031. (let* ((file (if (equal path "")
  10032. buffer-file-name
  10033. (substitute-in-file-name (expand-file-name path))))
  10034. (file-apps (append org-file-apps (org-default-apps)))
  10035. (apps (cl-remove-if
  10036. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10037. (apps-dlink (cl-remove-if-not
  10038. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10039. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10040. (dirp (unless remp (file-directory-p file)))
  10041. (file (if (and dirp org-open-directory-means-index-dot-org)
  10042. (concat (file-name-as-directory file) "index.org")
  10043. file))
  10044. (a-m-a-p (assq 'auto-mode apps))
  10045. (dfile (downcase file))
  10046. ;; Reconstruct the original link from the PATH, LINE and
  10047. ;; SEARCH args.
  10048. (link (cond (line (concat file "::" (number-to-string line)))
  10049. (search (concat file "::" search))
  10050. (t file)))
  10051. (dlink (downcase link))
  10052. (ext
  10053. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10054. (match-string 1 dfile)))
  10055. (save-position-maybe
  10056. (let ((old-buffer (current-buffer))
  10057. (old-pos (point))
  10058. (old-mode major-mode))
  10059. (lambda ()
  10060. (and (derived-mode-p 'org-mode)
  10061. (eq old-mode 'org-mode)
  10062. (or (not (eq old-buffer (current-buffer)))
  10063. (not (eq old-pos (point))))
  10064. (org-mark-ring-push old-pos old-buffer)))))
  10065. cmd link-match-data)
  10066. (cond
  10067. ((member in-emacs '((16) system))
  10068. (setq cmd (cdr (assq 'system apps))))
  10069. (in-emacs (setq cmd 'emacs))
  10070. (t
  10071. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10072. (and dirp (cdr (assq 'directory apps)))
  10073. ;; First, try matching against apps-dlink if we
  10074. ;; get a match here, store the match data for
  10075. ;; later.
  10076. (let ((match (assoc-default dlink apps-dlink
  10077. 'string-match)))
  10078. (if match
  10079. (progn (setq link-match-data (match-data))
  10080. match)
  10081. (progn (setq in-emacs (or in-emacs line search))
  10082. nil))) ; if we have no match in apps-dlink,
  10083. ; always open the file in emacs if line or search
  10084. ; is given (for backwards compatibility)
  10085. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10086. 'string-match)
  10087. (cdr (assoc ext apps))
  10088. (cdr (assq t apps))))))
  10089. (when (eq cmd 'system)
  10090. (setq cmd (cdr (assq 'system apps))))
  10091. (when (eq cmd 'default)
  10092. (setq cmd (cdr (assoc t apps))))
  10093. (when (eq cmd 'mailcap)
  10094. (require 'mailcap)
  10095. (mailcap-parse-mailcaps)
  10096. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10097. (command (mailcap-mime-info mime-type)))
  10098. (if (stringp command)
  10099. (setq cmd command)
  10100. (setq cmd 'emacs))))
  10101. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10102. (not (file-exists-p file))
  10103. (not org-open-non-existing-files))
  10104. (user-error "No such file: %s" file))
  10105. (cond
  10106. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10107. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10108. (while (string-match "['\"]%s['\"]" cmd)
  10109. (setq cmd (replace-match "%s" t t cmd)))
  10110. (setq cmd (replace-regexp-in-string
  10111. "%s"
  10112. (shell-quote-argument (convert-standard-filename file))
  10113. cmd
  10114. nil t))
  10115. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10116. (save-match-data
  10117. (let ((match-index 1)
  10118. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10119. (set-match-data link-match-data)
  10120. (while (<= match-index number-of-groups)
  10121. (let ((regex (concat "%" (number-to-string match-index)))
  10122. (replace-with (match-string match-index dlink)))
  10123. (while (string-match regex cmd)
  10124. (setq cmd (replace-match replace-with t t cmd))))
  10125. (setq match-index (+ match-index 1)))))
  10126. (save-window-excursion
  10127. (message "Running %s...done" cmd)
  10128. (start-process-shell-command cmd nil cmd)
  10129. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10130. ((or (stringp cmd)
  10131. (eq cmd 'emacs))
  10132. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10133. (widen)
  10134. (cond (line (org-goto-line line)
  10135. (when (derived-mode-p 'org-mode) (org-reveal)))
  10136. (search (condition-case err
  10137. (org-link-search search)
  10138. ;; Save position before error-ing out so user
  10139. ;; can easily move back to the original buffer.
  10140. (error (funcall save-position-maybe)
  10141. (error (nth 1 err)))))))
  10142. ((functionp cmd)
  10143. (save-match-data
  10144. (set-match-data link-match-data)
  10145. (condition-case nil
  10146. (funcall cmd file link)
  10147. ;; FIXME: Remove this check when most default installations
  10148. ;; of Emacs have at least Org 9.0.
  10149. ((debug wrong-number-of-arguments wrong-type-argument
  10150. invalid-function)
  10151. (user-error "Please see Org News for version 9.0 about \
  10152. `org-file-apps'--Lisp error: %S" cmd)))))
  10153. ((consp cmd)
  10154. ;; FIXME: Remove this check when most default installations of
  10155. ;; Emacs have at least Org 9.0. Heads-up instead of silently
  10156. ;; fall back to `org-link-frame-setup' for an old usage of
  10157. ;; `org-file-apps' with sexp instead of a function for `cmd'.
  10158. (user-error "Please see Org News for version 9.0 about \
  10159. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10160. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10161. (funcall save-position-maybe)))
  10162. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10163. "This function returns non-nil if `entry' uses a regular
  10164. expression which should be matched against the whole link by
  10165. org-open-file.
  10166. It assumes that is the case when the entry uses a regular
  10167. expression which has at least one grouping construct and the
  10168. action is either a lisp form or a command string containing
  10169. `%1', i.e. using at least one subexpression match as a
  10170. parameter."
  10171. (let ((selector (car entry))
  10172. (action (cdr entry)))
  10173. (if (stringp selector)
  10174. (and (> (regexp-opt-depth selector) 0)
  10175. (or (and (stringp action)
  10176. (string-match "%[0-9]" action))
  10177. (consp action)))
  10178. nil)))
  10179. (defun org-default-apps ()
  10180. "Return the default applications for this operating system."
  10181. (cond
  10182. ((eq system-type 'darwin)
  10183. org-file-apps-defaults-macosx)
  10184. ((eq system-type 'windows-nt)
  10185. org-file-apps-defaults-windowsnt)
  10186. (t org-file-apps-defaults-gnu)))
  10187. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10188. "Convert extensions to regular expressions in the cars of LIST.
  10189. Also, weed out any non-string entries, because the return value is used
  10190. only for regexp matching.
  10191. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10192. point to the symbol `emacs', indicating that the file should
  10193. be opened in Emacs."
  10194. (append
  10195. (delq nil
  10196. (mapcar (lambda (x)
  10197. (unless (not (stringp (car x)))
  10198. (if (string-match "\\W" (car x))
  10199. x
  10200. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10201. list))
  10202. (when add-auto-mode
  10203. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10204. (defvar ange-ftp-name-format)
  10205. (defun org-file-remote-p (file)
  10206. "Test whether FILE specifies a location on a remote system.
  10207. Return non-nil if the location is indeed remote.
  10208. For example, the filename \"/user@host:/foo\" specifies a location
  10209. on the system \"/user@host:\"."
  10210. (cond ((fboundp 'file-remote-p)
  10211. (file-remote-p file))
  10212. ((fboundp 'tramp-handle-file-remote-p)
  10213. (tramp-handle-file-remote-p file))
  10214. ((and (boundp 'ange-ftp-name-format)
  10215. (string-match (car ange-ftp-name-format) file))
  10216. t)))
  10217. ;;;; Refiling
  10218. (defun org-get-org-file ()
  10219. "Read a filename, with default directory `org-directory'."
  10220. (let ((default (or org-default-notes-file remember-data-file)))
  10221. (read-file-name (format "File name [%s]: " default)
  10222. (file-name-as-directory org-directory)
  10223. default)))
  10224. (defun org-notes-order-reversed-p ()
  10225. "Check if the current file should receive notes in reversed order."
  10226. (cond
  10227. ((not org-reverse-note-order) nil)
  10228. ((eq t org-reverse-note-order) t)
  10229. ((not (listp org-reverse-note-order)) nil)
  10230. (t (catch 'exit
  10231. (dolist (entry org-reverse-note-order)
  10232. (when (string-match (car entry) buffer-file-name)
  10233. (throw 'exit (cdr entry))))))))
  10234. (defvar org-refile-target-table nil
  10235. "The list of refile targets, created by `org-refile'.")
  10236. (defvar org-agenda-new-buffers nil
  10237. "Buffers created to visit agenda files.")
  10238. (defvar org-refile-cache nil
  10239. "Cache for refile targets.")
  10240. (defvar org-refile-markers nil
  10241. "All the markers used for caching refile locations.")
  10242. (defun org-refile-marker (pos)
  10243. "Get a new refile marker, but only if caching is in use."
  10244. (if (not org-refile-use-cache)
  10245. pos
  10246. (let ((m (make-marker)))
  10247. (move-marker m pos)
  10248. (push m org-refile-markers)
  10249. m)))
  10250. (defun org-refile-cache-clear ()
  10251. "Clear the refile cache and disable all the markers."
  10252. (dolist (m org-refile-markers) (move-marker m nil))
  10253. (setq org-refile-markers nil)
  10254. (setq org-refile-cache nil)
  10255. (message "Refile cache has been cleared"))
  10256. (defun org-refile-cache-check-set (set)
  10257. "Check if all the markers in the cache still have live buffers."
  10258. (let (marker)
  10259. (catch 'exit
  10260. (while (and set (setq marker (nth 3 (pop set))))
  10261. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10262. (when (and marker (null (marker-buffer marker)))
  10263. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10264. (sit-for 3)
  10265. (throw 'exit nil)))
  10266. t)))
  10267. (defun org-refile-cache-put (set &rest identifiers)
  10268. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10269. (let* ((key (sha1 (prin1-to-string identifiers)))
  10270. (entry (assoc key org-refile-cache)))
  10271. (if entry
  10272. (setcdr entry set)
  10273. (push (cons key set) org-refile-cache))))
  10274. (defun org-refile-cache-get (&rest identifiers)
  10275. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10276. (cond
  10277. ((not org-refile-cache) nil)
  10278. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10279. (t
  10280. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10281. org-refile-cache))))
  10282. (and set (org-refile-cache-check-set set) set)))))
  10283. (defvar org-outline-path-cache nil
  10284. "Alist between buffer positions and outline paths.
  10285. It value is an alist (POSITION . PATH) where POSITION is the
  10286. buffer position at the beginning of an entry and PATH is a list
  10287. of strings describing the outline path for that entry, in reverse
  10288. order.")
  10289. (defun org-refile-get-targets (&optional default-buffer)
  10290. "Produce a table with refile targets."
  10291. (let ((case-fold-search nil)
  10292. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10293. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10294. targets tgs files desc descre)
  10295. (message "Getting targets...")
  10296. (with-current-buffer (or default-buffer (current-buffer))
  10297. (dolist (entry entries)
  10298. (setq files (car entry) desc (cdr entry))
  10299. (cond
  10300. ((null files) (setq files (list (current-buffer))))
  10301. ((eq files 'org-agenda-files)
  10302. (setq files (org-agenda-files 'unrestricted)))
  10303. ((and (symbolp files) (fboundp files))
  10304. (setq files (funcall files)))
  10305. ((and (symbolp files) (boundp files))
  10306. (setq files (symbol-value files))))
  10307. (when (stringp files) (setq files (list files)))
  10308. (cond
  10309. ((eq (car desc) :tag)
  10310. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10311. ((eq (car desc) :todo)
  10312. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10313. ((eq (car desc) :regexp)
  10314. (setq descre (cdr desc)))
  10315. ((eq (car desc) :level)
  10316. (setq descre (concat "^\\*\\{" (number-to-string
  10317. (if org-odd-levels-only
  10318. (1- (* 2 (cdr desc)))
  10319. (cdr desc)))
  10320. "\\}[ \t]")))
  10321. ((eq (car desc) :maxlevel)
  10322. (setq descre (concat "^\\*\\{1," (number-to-string
  10323. (if org-odd-levels-only
  10324. (1- (* 2 (cdr desc)))
  10325. (cdr desc)))
  10326. "\\}[ \t]")))
  10327. (t (error "Bad refiling target description %s" desc)))
  10328. (dolist (f files)
  10329. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10330. (or
  10331. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10332. (progn
  10333. (when (bufferp f)
  10334. (setq f (buffer-file-name (buffer-base-buffer f))))
  10335. (setq f (and f (expand-file-name f)))
  10336. (when (eq org-refile-use-outline-path 'file)
  10337. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10338. (when (eq org-refile-use-outline-path 'buffer-name)
  10339. (push (list (buffer-name (buffer-base-buffer)) f nil nil) tgs))
  10340. (when (eq org-refile-use-outline-path 'full-file-path)
  10341. (push (list (file-truename (buffer-file-name (buffer-base-buffer))) f nil nil) tgs))
  10342. (org-with-wide-buffer
  10343. (goto-char (point-min))
  10344. (setq org-outline-path-cache nil)
  10345. (while (re-search-forward descre nil t)
  10346. (beginning-of-line)
  10347. (let ((case-fold-search nil))
  10348. (looking-at org-complex-heading-regexp))
  10349. (let ((begin (point))
  10350. (heading (match-string-no-properties 4)))
  10351. (unless (or (and
  10352. org-refile-target-verify-function
  10353. (not
  10354. (funcall org-refile-target-verify-function)))
  10355. (not heading))
  10356. (let ((re (format org-complex-heading-regexp-format
  10357. (regexp-quote heading)))
  10358. (target
  10359. (if (not org-refile-use-outline-path) heading
  10360. (mapconcat
  10361. #'identity
  10362. (append
  10363. (pcase org-refile-use-outline-path
  10364. (`file (list (file-name-nondirectory
  10365. (buffer-file-name
  10366. (buffer-base-buffer)))))
  10367. (`full-file-path
  10368. (list (buffer-file-name
  10369. (buffer-base-buffer))))
  10370. (`buffer-name
  10371. (list (buffer-name
  10372. (buffer-base-buffer))))
  10373. (_ nil))
  10374. (mapcar (lambda (s) (replace-regexp-in-string
  10375. "/" "\\/" s nil t))
  10376. (org-get-outline-path t t)))
  10377. "/"))))
  10378. (push (list target f re (org-refile-marker (point)))
  10379. tgs)))
  10380. (when (= (point) begin)
  10381. ;; Verification function has not moved point.
  10382. (end-of-line)))))))
  10383. (when org-refile-use-cache
  10384. (org-refile-cache-put tgs (buffer-file-name) descre))
  10385. (setq targets (append tgs targets))))))
  10386. (message "Getting targets...done")
  10387. (delete-dups (nreverse targets))))
  10388. (defun org--get-outline-path-1 (&optional use-cache)
  10389. "Return outline path to current headline.
  10390. Outline path is a list of strings, in reverse order. When
  10391. optional argument USE-CACHE is non-nil, make use of a cache. See
  10392. `org-get-outline-path' for details.
  10393. Assume buffer is widened and point is on a headline."
  10394. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10395. (let ((p (point))
  10396. (heading (let ((case-fold-search nil))
  10397. (looking-at org-complex-heading-regexp)
  10398. (if (not (match-end 4)) ""
  10399. ;; Remove statistics cookies.
  10400. (org-trim
  10401. (org-link-display-format
  10402. (replace-regexp-in-string
  10403. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10404. (match-string-no-properties 4))))))))
  10405. (if (org-up-heading-safe)
  10406. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10407. (when use-cache
  10408. (push (cons p path) org-outline-path-cache))
  10409. path)
  10410. ;; This is a new root node. Since we assume we are moving
  10411. ;; forward, we can drop previous cache so as to limit number
  10412. ;; of associations there.
  10413. (let ((path (list heading)))
  10414. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10415. path)))))
  10416. (defun org-get-outline-path (&optional with-self use-cache)
  10417. "Return the outline path to the current entry.
  10418. An outline path is a list of ancestors for current headline, as
  10419. a list of strings. Statistics cookies are removed and links are
  10420. replaced with their description, if any, or their path otherwise.
  10421. When optional argument WITH-SELF is non-nil, the path also
  10422. includes the current headline.
  10423. When optional argument USE-CACHE is non-nil, cache outline paths
  10424. between calls to this function so as to avoid backtracking. This
  10425. argument is useful when planning to find more than one outline
  10426. path in the same document. In that case, there are two
  10427. conditions to satisfy:
  10428. - `org-outline-path-cache' is set to nil before starting the
  10429. process;
  10430. - outline paths are computed by increasing buffer positions."
  10431. (org-with-wide-buffer
  10432. (and (or (and with-self (org-back-to-heading t))
  10433. (org-up-heading-safe))
  10434. (reverse (org--get-outline-path-1 use-cache)))))
  10435. (defun org-format-outline-path (path &optional width prefix separator)
  10436. "Format the outline path PATH for display.
  10437. WIDTH is the maximum number of characters that is available.
  10438. PREFIX is a prefix to be included in the returned string,
  10439. such as the file name.
  10440. SEPARATOR is inserted between the different parts of the path,
  10441. the default is \"/\"."
  10442. (setq width (or width 79))
  10443. (setq path (delq nil path))
  10444. (unless (> width 0)
  10445. (user-error "Argument `width' must be positive"))
  10446. (setq separator (or separator "/"))
  10447. (let* ((org-odd-levels-only nil)
  10448. (fpath (concat
  10449. prefix (and prefix path separator)
  10450. (mapconcat
  10451. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10452. (cl-loop for head in path
  10453. for n from 0
  10454. collect (org-add-props
  10455. head nil 'face
  10456. (nth (% n org-n-level-faces) org-level-faces)))
  10457. separator))))
  10458. (when (> (length fpath) width)
  10459. (if (< width 7)
  10460. ;; It's unlikely that `width' will be this small, but don't
  10461. ;; waste characters by adding ".." if it is.
  10462. (setq fpath (substring fpath 0 width))
  10463. (setf (substring fpath (- width 2)) "..")))
  10464. fpath))
  10465. (defun org-display-outline-path (&optional file current separator just-return-string)
  10466. "Display the current outline path in the echo area.
  10467. If FILE is non-nil, prepend the output with the file name.
  10468. If CURRENT is non-nil, append the current heading to the output.
  10469. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10470. the different parts of the path and defaults to \"/\".
  10471. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10472. (interactive "P")
  10473. (let* (case-fold-search
  10474. (bfn (buffer-file-name (buffer-base-buffer)))
  10475. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10476. res)
  10477. (when current (setq path (append path
  10478. (save-excursion
  10479. (org-back-to-heading t)
  10480. (when (looking-at org-complex-heading-regexp)
  10481. (list (match-string 4)))))))
  10482. (setq res
  10483. (org-format-outline-path
  10484. path
  10485. (1- (frame-width))
  10486. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10487. separator))
  10488. (if just-return-string
  10489. (org-no-properties res)
  10490. (org-unlogged-message "%s" res))))
  10491. (defvar org-refile-history nil
  10492. "History for refiling operations.")
  10493. (defvar org-after-refile-insert-hook nil
  10494. "Hook run after `org-refile' has inserted its stuff at the new location.
  10495. Note that this is still *before* the stuff will be removed from
  10496. the *old* location.")
  10497. (defvar org-capture-last-stored-marker)
  10498. (defvar org-refile-keep nil
  10499. "Non-nil means `org-refile' will copy instead of refile.")
  10500. (defun org-copy ()
  10501. "Like `org-refile', but copy."
  10502. (interactive)
  10503. (let ((org-refile-keep t))
  10504. (funcall 'org-refile nil nil nil "Copy")))
  10505. (defun org-refile (&optional arg default-buffer rfloc msg)
  10506. "Move the entry or entries at point to another heading.
  10507. The list of target headings is compiled using the information in
  10508. `org-refile-targets', which see.
  10509. At the target location, the entry is filed as a subitem of the
  10510. target heading. Depending on `org-reverse-note-order', the new
  10511. subitem will either be the first or the last subitem.
  10512. If there is an active region, all entries in that region will be
  10513. refiled. However, the region must fulfill the requirement that
  10514. the first heading sets the top-level of the moved text.
  10515. With a `\\[universal-argument]' ARG, the command will only visit the target \
  10516. location
  10517. and not actually move anything.
  10518. With a prefix `\\[universal-argument] \\[universal-argument]', go to the \
  10519. location where the last
  10520. refiling operation has put the subtree.
  10521. With a numeric prefix argument of `2', refile to the running clock.
  10522. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10523. being set to t and copy to the target location, don't move it.
  10524. Beware that keeping refiled entries may result in duplicated ID
  10525. properties.
  10526. RFLOC can be a refile location obtained in a different way.
  10527. MSG is a string to replace \"Refile\" in the default prompt with
  10528. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10529. See also `org-refile-use-outline-path'.
  10530. If you are using target caching (see `org-refile-use-cache'), you
  10531. have to clear the target cache in order to find new targets.
  10532. This can be done with a `0' prefix (`C-0 C-c C-w') or a triple
  10533. prefix argument (`C-u C-u C-u C-c C-w')."
  10534. (interactive "P")
  10535. (if (member arg '(0 (64)))
  10536. (org-refile-cache-clear)
  10537. (let* ((actionmsg (cond (msg msg)
  10538. ((equal arg 3) "Refile (and keep)")
  10539. (t "Refile")))
  10540. (regionp (org-region-active-p))
  10541. (region-start (and regionp (region-beginning)))
  10542. (region-end (and regionp (region-end)))
  10543. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10544. pos it nbuf file level reversed)
  10545. (setq last-command nil)
  10546. (when regionp
  10547. (goto-char region-start)
  10548. (or (bolp) (goto-char (point-at-bol)))
  10549. (setq region-start (point))
  10550. (unless (or (org-kill-is-subtree-p
  10551. (buffer-substring region-start region-end))
  10552. (prog1 org-refile-active-region-within-subtree
  10553. (let ((s (point-at-eol)))
  10554. (org-toggle-heading)
  10555. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10556. (user-error "The region is not a (sequence of) subtree(s)")))
  10557. (if (equal arg '(16))
  10558. (org-refile-goto-last-stored)
  10559. (when (or
  10560. (and (equal arg 2)
  10561. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10562. (prog1
  10563. (setq it (list (or org-clock-heading "running clock")
  10564. (buffer-file-name
  10565. (marker-buffer org-clock-hd-marker))
  10566. ""
  10567. (marker-position org-clock-hd-marker)))
  10568. (setq arg nil)))
  10569. (setq it
  10570. (or rfloc
  10571. (let (heading-text)
  10572. (save-excursion
  10573. (unless (and arg (listp arg))
  10574. (org-back-to-heading t)
  10575. (setq heading-text
  10576. (replace-regexp-in-string
  10577. org-bracket-link-regexp
  10578. "\\3"
  10579. (or (nth 4 (org-heading-components))
  10580. ""))))
  10581. (org-refile-get-location
  10582. (cond ((and arg (listp arg)) "Goto")
  10583. (regionp (concat actionmsg " region to"))
  10584. (t (concat actionmsg " subtree \""
  10585. heading-text "\" to")))
  10586. default-buffer
  10587. (and (not (equal '(4) arg))
  10588. org-refile-allow-creating-parent-nodes)))))))
  10589. (setq file (nth 1 it)
  10590. pos (nth 3 it))
  10591. (when (and (not arg)
  10592. pos
  10593. (equal (buffer-file-name) file)
  10594. (if regionp
  10595. (and (>= pos region-start)
  10596. (<= pos region-end))
  10597. (and (>= pos (point))
  10598. (< pos (save-excursion
  10599. (org-end-of-subtree t t))))))
  10600. (error "Cannot refile to position inside the tree or region"))
  10601. (setq nbuf (or (find-buffer-visiting file)
  10602. (find-file-noselect file)))
  10603. (if (and arg (not (equal arg 3)))
  10604. (progn
  10605. (pop-to-buffer-same-window nbuf)
  10606. (goto-char (cond (pos)
  10607. ((org-notes-order-reversed-p) (point-min))
  10608. (t (point-max))))
  10609. (org-show-context 'org-goto))
  10610. (if regionp
  10611. (progn
  10612. (org-kill-new (buffer-substring region-start region-end))
  10613. (org-save-markers-in-region region-start region-end))
  10614. (org-copy-subtree 1 nil t))
  10615. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10616. (find-file-noselect file)))
  10617. (setq reversed (org-notes-order-reversed-p))
  10618. (org-with-wide-buffer
  10619. (if pos
  10620. (progn
  10621. (goto-char pos)
  10622. (setq level (org-get-valid-level (funcall outline-level) 1))
  10623. (goto-char
  10624. (if reversed
  10625. (or (outline-next-heading) (point-max))
  10626. (or (save-excursion (org-get-next-sibling))
  10627. (org-end-of-subtree t t)
  10628. (point-max)))))
  10629. (setq level 1)
  10630. (if (not reversed)
  10631. (goto-char (point-max))
  10632. (goto-char (point-min))
  10633. (or (outline-next-heading) (goto-char (point-max)))))
  10634. (unless (bolp) (newline))
  10635. (org-paste-subtree level nil nil t)
  10636. (when org-log-refile
  10637. (org-add-log-setup 'refile nil nil org-log-refile)
  10638. (unless (eq org-log-refile 'note)
  10639. (save-excursion (org-add-log-note))))
  10640. (and org-auto-align-tags
  10641. (let ((org-loop-over-headlines-in-active-region nil))
  10642. (org-set-tags nil t)))
  10643. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10644. :last-refile)))
  10645. (when bookmark-name
  10646. (with-demoted-errors
  10647. (bookmark-set bookmark-name))))
  10648. ;; If we are refiling for capture, make sure that the
  10649. ;; last-capture pointers point here
  10650. (when (bound-and-true-p org-capture-is-refiling)
  10651. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10652. :last-capture-marker)))
  10653. (when bookmark-name
  10654. (with-demoted-errors
  10655. (bookmark-set bookmark-name))))
  10656. (move-marker org-capture-last-stored-marker (point)))
  10657. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10658. (run-hooks 'org-after-refile-insert-hook)))
  10659. (unless org-refile-keep
  10660. (if regionp
  10661. (delete-region (point) (+ (point) (- region-end region-start)))
  10662. (delete-region
  10663. (and (org-back-to-heading t) (point))
  10664. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10665. (when (featurep 'org-inlinetask)
  10666. (org-inlinetask-remove-END-maybe))
  10667. (setq org-markers-to-move nil)
  10668. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10669. (defun org-refile-goto-last-stored ()
  10670. "Go to the location where the last refile was stored."
  10671. (interactive)
  10672. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10673. (message "This is the location of the last refile"))
  10674. (defun org-refile--get-location (refloc tbl)
  10675. "When user refile to REFLOC, find the associated target in TBL.
  10676. Also check `org-refile-target-table'."
  10677. (car (delq
  10678. nil
  10679. (mapcar
  10680. (lambda (r) (or (assoc r tbl)
  10681. (assoc r org-refile-target-table)))
  10682. (list (replace-regexp-in-string "/$" "" refloc)
  10683. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10684. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  10685. "Prompt the user for a refile location, using PROMPT.
  10686. PROMPT should not be suffixed with a colon and a space, because
  10687. this function appends the default value from
  10688. `org-refile-history' automatically, if that is not empty."
  10689. (let ((org-refile-targets org-refile-targets)
  10690. (org-refile-use-outline-path org-refile-use-outline-path))
  10691. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  10692. (unless org-refile-target-table
  10693. (user-error "No refile targets"))
  10694. (let* ((cbuf (current-buffer))
  10695. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10696. (cfunc (if (and org-refile-use-outline-path
  10697. org-outline-path-complete-in-steps)
  10698. #'org-olpath-completing-read
  10699. #'completing-read))
  10700. (extra (if org-refile-use-outline-path "/" ""))
  10701. (cbnex (concat (buffer-name) extra))
  10702. (filename (and cfn (expand-file-name cfn)))
  10703. (tbl (mapcar
  10704. (lambda (x)
  10705. (if (and (not (member org-refile-use-outline-path
  10706. '(file full-file-path)))
  10707. (not (equal filename (nth 1 x))))
  10708. (cons (concat (car x) extra " ("
  10709. (file-name-nondirectory (nth 1 x)) ")")
  10710. (cdr x))
  10711. (cons (concat (car x) extra) (cdr x))))
  10712. org-refile-target-table))
  10713. (completion-ignore-case t)
  10714. cdef
  10715. (prompt (concat prompt
  10716. (or (and (car org-refile-history)
  10717. (concat " (default " (car org-refile-history) ")"))
  10718. (and (assoc cbnex tbl) (setq cdef cbnex)
  10719. (concat " (default " cbnex ")"))) ": "))
  10720. pa answ parent-target child parent old-hist)
  10721. (setq old-hist org-refile-history)
  10722. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10723. nil 'org-refile-history (or cdef (car org-refile-history))))
  10724. (if (setq pa (org-refile--get-location answ tbl))
  10725. (progn
  10726. (org-refile-check-position pa)
  10727. (when (or (not org-refile-history)
  10728. (not (eq old-hist org-refile-history))
  10729. (not (equal (car pa) (car org-refile-history))))
  10730. (setq org-refile-history
  10731. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10732. org-refile-history
  10733. (cdr org-refile-history))))
  10734. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10735. (pop org-refile-history)))
  10736. pa)
  10737. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10738. (progn
  10739. (setq parent (match-string 1 answ)
  10740. child (match-string 2 answ))
  10741. (setq parent-target (org-refile--get-location parent tbl))
  10742. (when (and parent-target
  10743. (or (eq new-nodes t)
  10744. (and (eq new-nodes 'confirm)
  10745. (y-or-n-p (format "Create new node \"%s\"? "
  10746. child)))))
  10747. (org-refile-new-child parent-target child)))
  10748. (user-error "Invalid target location")))))
  10749. (declare-function org-string-nw-p "org-macs" (s))
  10750. (defun org-refile-check-position (refile-pointer)
  10751. "Check if the refile pointer matches the headline to which it points."
  10752. (let* ((file (nth 1 refile-pointer))
  10753. (re (nth 2 refile-pointer))
  10754. (pos (nth 3 refile-pointer))
  10755. buffer)
  10756. (if (and (not (markerp pos)) (not file))
  10757. (user-error "Please indicate a target file in the refile path")
  10758. (when (org-string-nw-p re)
  10759. (setq buffer (if (markerp pos)
  10760. (marker-buffer pos)
  10761. (or (find-buffer-visiting file)
  10762. (find-file-noselect file))))
  10763. (with-current-buffer buffer
  10764. (org-with-wide-buffer
  10765. (goto-char pos)
  10766. (beginning-of-line 1)
  10767. (unless (looking-at-p re)
  10768. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10769. (defun org-refile-new-child (parent-target child)
  10770. "Use refile target PARENT-TARGET to add new CHILD below it."
  10771. (unless parent-target
  10772. (error "Cannot find parent for new node"))
  10773. (let ((file (nth 1 parent-target))
  10774. (pos (nth 3 parent-target))
  10775. level)
  10776. (with-current-buffer (or (find-buffer-visiting file)
  10777. (find-file-noselect file))
  10778. (org-with-wide-buffer
  10779. (if pos
  10780. (goto-char pos)
  10781. (goto-char (point-max))
  10782. (unless (bolp) (newline)))
  10783. (when (looking-at org-outline-regexp)
  10784. (setq level (funcall outline-level))
  10785. (org-end-of-subtree t t))
  10786. (org-back-over-empty-lines)
  10787. (insert "\n" (make-string
  10788. (if pos (org-get-valid-level level 1) 1) ?*)
  10789. " " child "\n")
  10790. (beginning-of-line 0)
  10791. (list (concat (car parent-target) "/" child) file "" (point))))))
  10792. (defun org-olpath-completing-read (prompt collection &rest args)
  10793. "Read an outline path like a file name."
  10794. (let ((thetable collection))
  10795. (apply #'completing-read
  10796. prompt
  10797. (lambda (string predicate &optional flag)
  10798. (cond
  10799. ((eq flag nil) (try-completion string thetable))
  10800. ((eq flag t)
  10801. (let ((l (length string)))
  10802. (mapcar (lambda (x)
  10803. (let ((r (substring x l))
  10804. (f (if (string-match " ([^)]*)$" x)
  10805. (match-string 0 x)
  10806. "")))
  10807. (if (string-match "/" r)
  10808. (concat string (substring r 0 (match-end 0)) f)
  10809. x)))
  10810. (all-completions string thetable predicate))))
  10811. ;; Exact match?
  10812. ((eq flag 'lambda) (assoc string thetable))))
  10813. args)))
  10814. ;;;; Dynamic blocks
  10815. (defun org-find-dblock (name)
  10816. "Find the first dynamic block with name NAME in the buffer.
  10817. If not found, stay at current position and return nil."
  10818. (let ((case-fold-search t) pos)
  10819. (save-excursion
  10820. (goto-char (point-min))
  10821. (setq pos (and (re-search-forward
  10822. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10823. (match-beginning 0))))
  10824. (when pos (goto-char pos))
  10825. pos))
  10826. (defun org-create-dblock (plist)
  10827. "Create a dynamic block section, with parameters taken from PLIST.
  10828. PLIST must contain a :name entry which is used as the name of the block."
  10829. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10830. (end-of-line 1)
  10831. (newline))
  10832. (let ((col (current-column))
  10833. (name (plist-get plist :name)))
  10834. (insert "#+BEGIN: " name)
  10835. (while plist
  10836. (if (eq (car plist) :name)
  10837. (setq plist (cddr plist))
  10838. (insert " " (prin1-to-string (pop plist)))))
  10839. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10840. (beginning-of-line -2)))
  10841. (defun org-prepare-dblock ()
  10842. "Prepare dynamic block for refresh.
  10843. This empties the block, puts the cursor at the insert position and returns
  10844. the property list including an extra property :name with the block name."
  10845. (unless (looking-at org-dblock-start-re)
  10846. (user-error "Not at a dynamic block"))
  10847. (let* ((begdel (1+ (match-end 0)))
  10848. (name (org-no-properties (match-string 1)))
  10849. (params (append (list :name name)
  10850. (read (concat "(" (match-string 3) ")")))))
  10851. (save-excursion
  10852. (beginning-of-line 1)
  10853. (skip-chars-forward " \t")
  10854. (setq params (plist-put params :indentation-column (current-column))))
  10855. (unless (re-search-forward org-dblock-end-re nil t)
  10856. (error "Dynamic block not terminated"))
  10857. (setq params
  10858. (append params
  10859. (list :content (buffer-substring
  10860. begdel (match-beginning 0)))))
  10861. (delete-region begdel (match-beginning 0))
  10862. (goto-char begdel)
  10863. (open-line 1)
  10864. params))
  10865. (defun org-map-dblocks (&optional command)
  10866. "Apply COMMAND to all dynamic blocks in the current buffer.
  10867. If COMMAND is not given, use `org-update-dblock'."
  10868. (let ((cmd (or command 'org-update-dblock)))
  10869. (save-excursion
  10870. (goto-char (point-min))
  10871. (while (re-search-forward org-dblock-start-re nil t)
  10872. (goto-char (match-beginning 0))
  10873. (save-excursion
  10874. (condition-case nil
  10875. (funcall cmd)
  10876. (error (message "Error during update of dynamic block"))))
  10877. (unless (re-search-forward org-dblock-end-re nil t)
  10878. (error "Dynamic block not terminated"))))))
  10879. (defun org-dblock-update (&optional arg)
  10880. "User command for updating dynamic blocks.
  10881. Update the dynamic block at point. With prefix ARG, update all dynamic
  10882. blocks in the buffer."
  10883. (interactive "P")
  10884. (if arg
  10885. (org-update-all-dblocks)
  10886. (or (looking-at org-dblock-start-re)
  10887. (org-beginning-of-dblock))
  10888. (org-update-dblock)))
  10889. (defun org-update-dblock ()
  10890. "Update the dynamic block at point.
  10891. This means to empty the block, parse for parameters and then call
  10892. the correct writing function."
  10893. (interactive)
  10894. (save-excursion
  10895. (let* ((win (selected-window))
  10896. (pos (point))
  10897. (line (org-current-line))
  10898. (params (org-prepare-dblock))
  10899. (name (plist-get params :name))
  10900. (indent (plist-get params :indentation-column))
  10901. (cmd (intern (concat "org-dblock-write:" name))))
  10902. (message "Updating dynamic block `%s' at line %d..." name line)
  10903. (funcall cmd params)
  10904. (message "Updating dynamic block `%s' at line %d...done" name line)
  10905. (goto-char pos)
  10906. (when (and indent (> indent 0))
  10907. (setq indent (make-string indent ?\ ))
  10908. (save-excursion
  10909. (select-window win)
  10910. (org-beginning-of-dblock)
  10911. (forward-line 1)
  10912. (while (not (looking-at org-dblock-end-re))
  10913. (insert indent)
  10914. (beginning-of-line 2))
  10915. (when (looking-at org-dblock-end-re)
  10916. (and (looking-at "[ \t]+")
  10917. (replace-match ""))
  10918. (insert indent)))))))
  10919. (defun org-beginning-of-dblock ()
  10920. "Find the beginning of the dynamic block at point.
  10921. Error if there is no such block at point."
  10922. (let ((pos (point))
  10923. beg)
  10924. (end-of-line 1)
  10925. (if (and (re-search-backward org-dblock-start-re nil t)
  10926. (setq beg (match-beginning 0))
  10927. (re-search-forward org-dblock-end-re nil t)
  10928. (> (match-end 0) pos))
  10929. (goto-char beg)
  10930. (goto-char pos)
  10931. (error "Not in a dynamic block"))))
  10932. (defun org-update-all-dblocks ()
  10933. "Update all dynamic blocks in the buffer.
  10934. This function can be used in a hook."
  10935. (interactive)
  10936. (when (derived-mode-p 'org-mode)
  10937. (org-map-dblocks 'org-update-dblock)))
  10938. ;;;; Completion
  10939. (declare-function org-export-backend-options "ox" (cl-x) t)
  10940. (defun org-get-export-keywords ()
  10941. "Return a list of all currently understood export keywords.
  10942. Export keywords include options, block names, attributes and
  10943. keywords relative to each registered export back-end."
  10944. (let (keywords)
  10945. (dolist (backend
  10946. (bound-and-true-p org-export-registered-backends)
  10947. (delq nil keywords))
  10948. ;; Back-end name (for keywords, like #+LATEX:)
  10949. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10950. (dolist (option-entry (org-export-backend-options backend))
  10951. ;; Back-end options.
  10952. (push (nth 1 option-entry) keywords)))))
  10953. (defconst org-options-keywords
  10954. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10955. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10956. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10957. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10958. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10959. (defcustom org-structure-template-alist
  10960. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  10961. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  10962. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  10963. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  10964. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  10965. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  10966. ("C" "#+BEGIN_COMMENT\n?\n#+END_COMMENT")
  10967. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  10968. ("L" "#+LaTeX: ")
  10969. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  10970. ("H" "#+HTML: ")
  10971. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  10972. ("A" "#+ASCII: ")
  10973. ("i" "#+INDEX: ?")
  10974. ("I" "#+INCLUDE: %file ?"))
  10975. "Structure completion elements.
  10976. This is a list of abbreviation keys and values. The value gets inserted
  10977. if you type `<' followed by the key and then press the completion key,
  10978. usually `TAB'. %file will be replaced by a file name after prompting
  10979. for the file using completion. The cursor will be placed at the position
  10980. of the `?' in the template.
  10981. There are two templates for each key, the first uses the original Org syntax,
  10982. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10983. the default when the /org-mtags.el/ module has been loaded. See also the
  10984. variable `org-mtags-prefer-muse-templates'."
  10985. :group 'org-edit-structure
  10986. :type '(repeat
  10987. (list
  10988. (string :tag "Key")
  10989. (string :tag "Template")))
  10990. :version "26.1"
  10991. :package-version '(Org . "8.3"))
  10992. (defun org-try-structure-completion ()
  10993. "Try to complete a structure template before point.
  10994. This looks for strings like \"<e\" on an otherwise empty line and
  10995. expands them."
  10996. (let ((l (buffer-substring (point-at-bol) (point)))
  10997. a)
  10998. (when (and (looking-at "[ \t]*$")
  10999. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11000. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11001. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11002. (match-beginning 1)) a)
  11003. t)))
  11004. (defun org-complete-expand-structure-template (start cell)
  11005. "Expand a structure template."
  11006. (let ((rpl (nth 1 cell))
  11007. (ind ""))
  11008. (delete-region start (point))
  11009. (when (string-match "\\`[ \t]*#\\+" rpl)
  11010. (cond
  11011. ((bolp))
  11012. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11013. (setq ind (buffer-substring (point-at-bol) (point))))
  11014. (t (newline))))
  11015. (setq start (point))
  11016. (when (string-match "%file" rpl)
  11017. (setq rpl (replace-match
  11018. (concat
  11019. "\""
  11020. (save-match-data
  11021. (abbreviate-file-name (read-file-name "Include file: ")))
  11022. "\"")
  11023. t t rpl)))
  11024. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11025. (concat "\n" ind)))
  11026. (insert rpl)
  11027. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11028. ;;;; TODO, DEADLINE, Comments
  11029. (defun org-toggle-comment ()
  11030. "Change the COMMENT state of an entry."
  11031. (interactive)
  11032. (save-excursion
  11033. (org-back-to-heading)
  11034. (let ((case-fold-search nil))
  11035. (looking-at org-complex-heading-regexp))
  11036. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11037. (skip-chars-forward " \t")
  11038. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11039. (if (org-in-commented-heading-p t)
  11040. (delete-region (point)
  11041. (progn (search-forward " " (line-end-position) 'move)
  11042. (skip-chars-forward " \t")
  11043. (point)))
  11044. (insert org-comment-string)
  11045. (unless (eolp) (insert " ")))))
  11046. (defvar org-last-todo-state-is-todo nil
  11047. "This is non-nil when the last TODO state change led to a TODO state.
  11048. If the last change removed the TODO tag or switched to DONE, then
  11049. this is nil.")
  11050. (defvar org-setting-tags nil) ; dynamically skipped
  11051. (defvar org-todo-setup-filter-hook nil
  11052. "Hook for functions that pre-filter todo specs.
  11053. Each function takes a todo spec and returns either nil or the spec
  11054. transformed into canonical form." )
  11055. (defvar org-todo-get-default-hook nil
  11056. "Hook for functions that get a default item for todo.
  11057. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11058. nil or a string to be used for the todo mark." )
  11059. (defvar org-agenda-headline-snapshot-before-repeat)
  11060. (defun org-current-effective-time ()
  11061. "Return current time adjusted for `org-extend-today-until' variable."
  11062. (let* ((ct (org-current-time))
  11063. (dct (decode-time ct))
  11064. (ct1
  11065. (cond
  11066. (org-use-last-clock-out-time-as-effective-time
  11067. (or (org-clock-get-last-clock-out-time) ct))
  11068. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11069. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11070. (t ct))))
  11071. ct1))
  11072. (defun org-todo-yesterday (&optional arg)
  11073. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11074. (interactive "P")
  11075. (if (eq major-mode 'org-agenda-mode)
  11076. (apply 'org-agenda-todo-yesterday arg)
  11077. (let* ((org-use-effective-time t)
  11078. (hour (nth 2 (decode-time (org-current-time))))
  11079. (org-extend-today-until (1+ hour)))
  11080. (org-todo arg))))
  11081. (defvar org-block-entry-blocking ""
  11082. "First entry preventing the TODO state change.")
  11083. (defun org-cancel-repeater ()
  11084. "Cancel a repeater by setting its numeric value to zero."
  11085. (interactive)
  11086. (save-excursion
  11087. (org-back-to-heading t)
  11088. (let ((bound1 (point))
  11089. (bound0 (save-excursion (outline-next-heading) (point))))
  11090. (when (and (re-search-forward
  11091. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11092. org-deadline-time-regexp "\\)\\|\\("
  11093. org-ts-regexp "\\)")
  11094. bound0 t)
  11095. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11096. bound1 t))
  11097. (replace-match "0" t nil nil 1)))))
  11098. (defvar org-state)
  11099. (defvar org-blocked-by-checkboxes)
  11100. (defun org-todo (&optional arg)
  11101. "Change the TODO state of an item.
  11102. The state of an item is given by a keyword at the start of the heading,
  11103. like
  11104. *** TODO Write paper
  11105. *** DONE Call mom
  11106. The different keywords are specified in the variable `org-todo-keywords'.
  11107. By default the available states are \"TODO\" and \"DONE\". So, for this
  11108. example: when the item starts with TODO, it is changed to DONE.
  11109. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11110. DONE are present, add TODO at the beginning of the heading.
  11111. With `\\[universal-argument]' prefix ARG, use completion to determine the new \
  11112. state.
  11113. With numeric prefix ARG, switch to that state.
  11114. With a `\\[universal-argument] \\[universal-argument]' prefix, switch to the \
  11115. next set of TODO \
  11116. keywords (nextset).
  11117. With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  11118. prefix, circumvent any state blocking.
  11119. With a numeric prefix arg of 0, inhibit note taking for the change.
  11120. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11121. When called through ELisp, arg is also interpreted in the following way:
  11122. `none' -> empty state
  11123. \"\" -> switch to empty state
  11124. `done' -> switch to DONE
  11125. `nextset' -> switch to the next set of keywords
  11126. `previousset' -> switch to the previous set of keywords
  11127. \"WAITING\" -> switch to the specified keyword, but only if it
  11128. really is a member of `org-todo-keywords'."
  11129. (interactive "P")
  11130. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11131. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11132. 'region-start-level 'region))
  11133. org-loop-over-headlines-in-active-region)
  11134. (org-map-entries
  11135. `(org-todo ,arg)
  11136. org-loop-over-headlines-in-active-region
  11137. cl (when (org-invisible-p) (org-end-of-subtree nil t))))
  11138. (when (equal arg '(16)) (setq arg 'nextset))
  11139. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11140. (let ((org-blocker-hook org-blocker-hook)
  11141. commentp
  11142. case-fold-search)
  11143. (when (equal arg '(64))
  11144. (setq arg nil org-blocker-hook nil))
  11145. (when (and org-blocker-hook
  11146. (or org-inhibit-blocking
  11147. (org-entry-get nil "NOBLOCKING")))
  11148. (setq org-blocker-hook nil))
  11149. (save-excursion
  11150. (catch 'exit
  11151. (org-back-to-heading t)
  11152. (when (org-in-commented-heading-p t)
  11153. (org-toggle-comment)
  11154. (setq commentp t))
  11155. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11156. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11157. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11158. (let* ((match-data (match-data))
  11159. (startpos (copy-marker (line-beginning-position)))
  11160. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11161. (org-log-done org-log-done)
  11162. (org-log-repeat org-log-repeat)
  11163. (org-todo-log-states org-todo-log-states)
  11164. (org-inhibit-logging
  11165. (if (equal arg 0)
  11166. (progn (setq arg nil) 'note) org-inhibit-logging))
  11167. (this (match-string 1))
  11168. (hl-pos (match-beginning 0))
  11169. (head (org-get-todo-sequence-head this))
  11170. (ass (assoc head org-todo-kwd-alist))
  11171. (interpret (nth 1 ass))
  11172. (done-word (nth 3 ass))
  11173. (final-done-word (nth 4 ass))
  11174. (org-last-state (or this ""))
  11175. (completion-ignore-case t)
  11176. (member (member this org-todo-keywords-1))
  11177. (tail (cdr member))
  11178. (org-state (cond
  11179. ((and org-todo-key-trigger
  11180. (or (and (equal arg '(4))
  11181. (eq org-use-fast-todo-selection 'prefix))
  11182. (and (not arg) org-use-fast-todo-selection
  11183. (not (eq org-use-fast-todo-selection
  11184. 'prefix)))))
  11185. ;; Use fast selection.
  11186. (org-fast-todo-selection))
  11187. ((and (equal arg '(4))
  11188. (or (not org-use-fast-todo-selection)
  11189. (not org-todo-key-trigger)))
  11190. ;; Read a state with completion.
  11191. (completing-read
  11192. "State: " (mapcar #'list org-todo-keywords-1)
  11193. nil t))
  11194. ((eq arg 'right)
  11195. (if this
  11196. (if tail (car tail) nil)
  11197. (car org-todo-keywords-1)))
  11198. ((eq arg 'left)
  11199. (unless (equal member org-todo-keywords-1)
  11200. (if this
  11201. (nth (- (length org-todo-keywords-1)
  11202. (length tail) 2)
  11203. org-todo-keywords-1)
  11204. (org-last org-todo-keywords-1))))
  11205. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11206. (setq arg nil))) ;hack to fall back to cycling
  11207. (arg
  11208. ;; User or caller requests a specific state.
  11209. (cond
  11210. ((equal arg "") nil)
  11211. ((eq arg 'none) nil)
  11212. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11213. ((eq arg 'nextset)
  11214. (or (car (cdr (member head org-todo-heads)))
  11215. (car org-todo-heads)))
  11216. ((eq arg 'previousset)
  11217. (let ((org-todo-heads (reverse org-todo-heads)))
  11218. (or (car (cdr (member head org-todo-heads)))
  11219. (car org-todo-heads))))
  11220. ((car (member arg org-todo-keywords-1)))
  11221. ((stringp arg)
  11222. (user-error "State `%s' not valid in this file" arg))
  11223. ((nth (1- (prefix-numeric-value arg))
  11224. org-todo-keywords-1))))
  11225. ((null member) (or head (car org-todo-keywords-1)))
  11226. ((equal this final-done-word) nil) ;-> make empty
  11227. ((null tail) nil) ;-> first entry
  11228. ((memq interpret '(type priority))
  11229. (if (eq this-command last-command)
  11230. (car tail)
  11231. (if (> (length tail) 0)
  11232. (or done-word (car org-done-keywords))
  11233. nil)))
  11234. (t
  11235. (car tail))))
  11236. (org-state (or
  11237. (run-hook-with-args-until-success
  11238. 'org-todo-get-default-hook org-state org-last-state)
  11239. org-state))
  11240. (next (if org-state (concat " " org-state " ") " "))
  11241. (change-plist (list :type 'todo-state-change :from this :to org-state
  11242. :position startpos))
  11243. dolog now-done-p)
  11244. (when org-blocker-hook
  11245. (let (org-blocked-by-checkboxes block-reason)
  11246. (setq org-last-todo-state-is-todo
  11247. (not (member this org-done-keywords)))
  11248. (unless (save-excursion
  11249. (save-match-data
  11250. (org-with-wide-buffer
  11251. (run-hook-with-args-until-failure
  11252. 'org-blocker-hook change-plist))))
  11253. (setq block-reason (if org-blocked-by-checkboxes
  11254. "contained checkboxes"
  11255. (format "\"%s\"" org-block-entry-blocking)))
  11256. (if (called-interactively-p 'interactive)
  11257. (user-error "TODO state change from %s to %s blocked (by %s)"
  11258. this org-state block-reason)
  11259. ;; Fail silently.
  11260. (message "TODO state change from %s to %s blocked (by %s)"
  11261. this org-state block-reason)
  11262. (throw 'exit nil)))))
  11263. (store-match-data match-data)
  11264. (replace-match next t t)
  11265. (cond ((equal this org-state)
  11266. (message "TODO state was already %s" (org-trim next)))
  11267. ((not (pos-visible-in-window-p hl-pos))
  11268. (message "TODO state changed to %s" (org-trim next))))
  11269. (unless head
  11270. (setq head (org-get-todo-sequence-head org-state)
  11271. ass (assoc head org-todo-kwd-alist)
  11272. interpret (nth 1 ass)
  11273. done-word (nth 3 ass)
  11274. final-done-word (nth 4 ass)))
  11275. (when (memq arg '(nextset previousset))
  11276. (message "Keyword-Set %d/%d: %s"
  11277. (- (length org-todo-sets) -1
  11278. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11279. (length org-todo-sets)
  11280. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11281. (setq org-last-todo-state-is-todo
  11282. (not (member org-state org-done-keywords)))
  11283. (setq now-done-p (and (member org-state org-done-keywords)
  11284. (not (member this org-done-keywords))))
  11285. (and logging (org-local-logging logging))
  11286. (when (and (or org-todo-log-states org-log-done)
  11287. (not (eq org-inhibit-logging t))
  11288. (not (memq arg '(nextset previousset))))
  11289. ;; We need to look at recording a time and note.
  11290. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11291. (nth 2 (assoc this org-todo-log-states))))
  11292. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11293. (setq dolog 'time))
  11294. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11295. (and org-state
  11296. (member org-state org-not-done-keywords)
  11297. (not (member this org-not-done-keywords))))
  11298. ;; This is now a todo state and was not one before
  11299. ;; If there was a CLOSED time stamp, get rid of it.
  11300. (org-add-planning-info nil nil 'closed))
  11301. (when (and now-done-p org-log-done)
  11302. ;; It is now done, and it was not done before.
  11303. (org-add-planning-info 'closed (org-current-effective-time))
  11304. (when (and (not dolog) (eq 'note org-log-done))
  11305. (org-add-log-setup 'done org-state this 'note)))
  11306. (when (and org-state dolog)
  11307. ;; This is a non-nil state, and we need to log it.
  11308. (org-add-log-setup 'state org-state this dolog)))
  11309. ;; Fixup tag positioning.
  11310. (org-todo-trigger-tag-changes org-state)
  11311. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11312. (when org-provide-todo-statistics
  11313. (org-update-parent-todo-statistics))
  11314. (run-hooks 'org-after-todo-state-change-hook)
  11315. (when (and arg (not (member org-state org-done-keywords)))
  11316. (setq head (org-get-todo-sequence-head org-state)))
  11317. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11318. ;; Do we need to trigger a repeat?
  11319. (when now-done-p
  11320. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11321. ;; This is for the agenda, take a snapshot of the headline.
  11322. (save-match-data
  11323. (setq org-agenda-headline-snapshot-before-repeat
  11324. (org-get-heading))))
  11325. (org-auto-repeat-maybe org-state))
  11326. ;; Fixup cursor location if close to the keyword.
  11327. (when (and (outline-on-heading-p)
  11328. (not (bolp))
  11329. (save-excursion (beginning-of-line 1)
  11330. (looking-at org-todo-line-regexp))
  11331. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11332. (goto-char (or (match-end 2) (match-end 1)))
  11333. (and (looking-at " ") (just-one-space)))
  11334. (when org-trigger-hook
  11335. (save-excursion
  11336. (run-hook-with-args 'org-trigger-hook change-plist)))
  11337. (when commentp (org-toggle-comment))))))))
  11338. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11339. "Block turning an entry into a TODO, using the hierarchy.
  11340. This checks whether the current task should be blocked from state
  11341. changes. Such blocking occurs when:
  11342. 1. The task has children which are not all in a completed state.
  11343. 2. A task has a parent with the property :ORDERED:, and there
  11344. are siblings prior to the current task with incomplete
  11345. status.
  11346. 3. The parent of the task is blocked because it has siblings that should
  11347. be done first, or is child of a block grandparent TODO entry."
  11348. (if (not org-enforce-todo-dependencies)
  11349. t ; if locally turned off don't block
  11350. (catch 'dont-block
  11351. ;; If this is not a todo state change, or if this entry is already DONE,
  11352. ;; do not block
  11353. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11354. (member (plist-get change-plist :from)
  11355. (cons 'done org-done-keywords))
  11356. (member (plist-get change-plist :to)
  11357. (cons 'todo org-not-done-keywords))
  11358. (not (plist-get change-plist :to)))
  11359. (throw 'dont-block t))
  11360. ;; If this task has children, and any are undone, it's blocked
  11361. (save-excursion
  11362. (org-back-to-heading t)
  11363. (let ((this-level (funcall outline-level)))
  11364. (outline-next-heading)
  11365. (let ((child-level (funcall outline-level)))
  11366. (while (and (not (eobp))
  11367. (> child-level this-level))
  11368. ;; this todo has children, check whether they are all
  11369. ;; completed
  11370. (when (and (not (org-entry-is-done-p))
  11371. (org-entry-is-todo-p))
  11372. (setq org-block-entry-blocking (org-get-heading))
  11373. (throw 'dont-block nil))
  11374. (outline-next-heading)
  11375. (setq child-level (funcall outline-level))))))
  11376. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11377. ;; any previous siblings are undone, it's blocked
  11378. (save-excursion
  11379. (org-back-to-heading t)
  11380. (let* ((pos (point))
  11381. (parent-pos (and (org-up-heading-safe) (point)))
  11382. (case-fold-search nil))
  11383. (unless parent-pos (throw 'dont-block t)) ; no parent
  11384. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11385. (forward-line 1)
  11386. (re-search-forward org-not-done-heading-regexp pos t))
  11387. (setq org-block-entry-blocking (match-string 0))
  11388. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11389. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11390. (while t
  11391. (goto-char parent-pos)
  11392. (unless (looking-at org-not-done-heading-regexp)
  11393. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11394. (setq pos (point))
  11395. (setq parent-pos (and (org-up-heading-safe) (point)))
  11396. (unless parent-pos (throw 'dont-block t)) ; no parent
  11397. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11398. (forward-line 1)
  11399. (re-search-forward org-not-done-heading-regexp pos t)
  11400. (setq org-block-entry-blocking (org-get-heading)))
  11401. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11402. (defcustom org-track-ordered-property-with-tag nil
  11403. "Should the ORDERED property also be shown as a tag?
  11404. The ORDERED property decides if an entry should require subtasks to be
  11405. completed in sequence. Since a property is not very visible, setting
  11406. this option means that toggling the ORDERED property with the command
  11407. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11408. not relevant for the behavior, but it makes things more visible.
  11409. Note that toggling the tag with tags commands will not change the property
  11410. and therefore not influence behavior!
  11411. This can be t, meaning the tag ORDERED should be used, It can also be a
  11412. string to select a different tag for this task."
  11413. :group 'org-todo
  11414. :type '(choice
  11415. (const :tag "No tracking" nil)
  11416. (const :tag "Track with ORDERED tag" t)
  11417. (string :tag "Use other tag")))
  11418. (defun org-toggle-ordered-property ()
  11419. "Toggle the ORDERED property of the current entry.
  11420. For better visibility, you can track the value of this property with a tag.
  11421. See variable `org-track-ordered-property-with-tag'."
  11422. (interactive)
  11423. (let* ((t1 org-track-ordered-property-with-tag)
  11424. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11425. (save-excursion
  11426. (org-back-to-heading)
  11427. (if (org-entry-get nil "ORDERED")
  11428. (progn
  11429. (org-delete-property "ORDERED")
  11430. (and tag (org-toggle-tag tag 'off))
  11431. (message "Subtasks can be completed in arbitrary order"))
  11432. (org-entry-put nil "ORDERED" "t")
  11433. (and tag (org-toggle-tag tag 'on))
  11434. (message "Subtasks must be completed in sequence")))))
  11435. (defun org-block-todo-from-checkboxes (change-plist)
  11436. "Block turning an entry into a TODO, using checkboxes.
  11437. This checks whether the current task should be blocked from state
  11438. changes because there are unchecked boxes in this entry."
  11439. (if (not org-enforce-todo-checkbox-dependencies)
  11440. t ; if locally turned off don't block
  11441. (catch 'dont-block
  11442. ;; If this is not a todo state change, or if this entry is already DONE,
  11443. ;; do not block
  11444. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11445. (member (plist-get change-plist :from)
  11446. (cons 'done org-done-keywords))
  11447. (member (plist-get change-plist :to)
  11448. (cons 'todo org-not-done-keywords))
  11449. (not (plist-get change-plist :to)))
  11450. (throw 'dont-block t))
  11451. ;; If this task has checkboxes that are not checked, it's blocked
  11452. (save-excursion
  11453. (org-back-to-heading t)
  11454. (let ((beg (point)) end)
  11455. (outline-next-heading)
  11456. (setq end (point))
  11457. (goto-char beg)
  11458. (when (org-list-search-forward
  11459. (concat (org-item-beginning-re)
  11460. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11461. "\\[[- ]\\]")
  11462. end t)
  11463. (when (boundp 'org-blocked-by-checkboxes)
  11464. (setq org-blocked-by-checkboxes t))
  11465. (throw 'dont-block nil))))
  11466. t))) ; do not block
  11467. (defun org-entry-blocked-p ()
  11468. "Non-nil if entry at point is blocked."
  11469. (and (not (org-entry-get nil "NOBLOCKING"))
  11470. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11471. (not (run-hook-with-args-until-failure
  11472. 'org-blocker-hook
  11473. (list :type 'todo-state-change
  11474. :position (point)
  11475. :from 'todo
  11476. :to 'done)))))
  11477. (defun org-update-statistics-cookies (all)
  11478. "Update the statistics cookie, either from TODO or from checkboxes.
  11479. This should be called with the cursor in a line with a statistics
  11480. cookie. When called with a \\[universal-argument] prefix, update
  11481. all statistics cookies in the buffer."
  11482. (interactive "P")
  11483. (if all
  11484. (progn
  11485. (org-update-checkbox-count 'all)
  11486. (org-map-entries 'org-update-parent-todo-statistics))
  11487. (if (not (org-at-heading-p))
  11488. (org-update-checkbox-count)
  11489. (let ((pos (point-marker))
  11490. end l1 l2)
  11491. (ignore-errors (org-back-to-heading t))
  11492. (if (not (org-at-heading-p))
  11493. (org-update-checkbox-count)
  11494. (setq l1 (org-outline-level))
  11495. (setq end (save-excursion
  11496. (outline-next-heading)
  11497. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11498. (point)))
  11499. (if (and (save-excursion
  11500. (re-search-forward
  11501. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11502. (not (save-excursion (re-search-forward
  11503. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11504. (org-update-checkbox-count)
  11505. (if (and l2 (> l2 l1))
  11506. (progn
  11507. (goto-char end)
  11508. (org-update-parent-todo-statistics))
  11509. (goto-char pos)
  11510. (beginning-of-line 1)
  11511. (while (re-search-forward
  11512. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11513. (point-at-eol) t)
  11514. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11515. (goto-char pos)
  11516. (move-marker pos nil)))))
  11517. (defvar org-entry-property-inherited-from) ;; defined below
  11518. (defun org-update-parent-todo-statistics ()
  11519. "Update any statistics cookie in the parent of the current headline.
  11520. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11521. the entire subtree and this will travel up the hierarchy and update
  11522. statistics everywhere."
  11523. (let* ((prop (save-excursion (org-up-heading-safe)
  11524. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11525. (recursive (or (not org-hierarchical-todo-statistics)
  11526. (and prop (string-match "\\<recursive\\>" prop))))
  11527. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11528. 0))
  11529. (first t)
  11530. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11531. level ltoggle l1 new ndel
  11532. (cnt-all 0) (cnt-done 0) is-percent kwd
  11533. checkbox-beg cookie-present)
  11534. (catch 'exit
  11535. (save-excursion
  11536. (beginning-of-line 1)
  11537. (setq ltoggle (funcall outline-level))
  11538. ;; Three situations are to consider:
  11539. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11540. ;; to the top-level ancestor on the headline;
  11541. ;; 2. If parent has "recursive" property, repeat up to the
  11542. ;; headline setting that property, taking inheritance into
  11543. ;; account;
  11544. ;; 3. Else, move up to direct parent and proceed only once.
  11545. (while (and (setq level (org-up-heading-safe))
  11546. (or recursive first)
  11547. (>= (point) lim))
  11548. (setq first nil cookie-present nil)
  11549. (unless (and level
  11550. (not (string-match
  11551. "\\<checkbox\\>"
  11552. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11553. "")))))
  11554. (throw 'exit nil))
  11555. (while (re-search-forward box-re (point-at-eol) t)
  11556. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11557. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11558. (save-match-data
  11559. (unless (outline-next-heading) (throw 'exit nil))
  11560. (while (and (looking-at org-complex-heading-regexp)
  11561. (> (setq l1 (length (match-string 1))) level))
  11562. (setq kwd (and (or recursive (= l1 ltoggle))
  11563. (match-string 2)))
  11564. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11565. (and (eq org-provide-todo-statistics t)
  11566. (or (member kwd org-done-keywords)))
  11567. (and (listp org-provide-todo-statistics)
  11568. (stringp (car org-provide-todo-statistics))
  11569. (or (member kwd org-provide-todo-statistics)
  11570. (member kwd org-done-keywords)))
  11571. (and (listp org-provide-todo-statistics)
  11572. (listp (car org-provide-todo-statistics))
  11573. (or (member kwd (car org-provide-todo-statistics))
  11574. (and (member kwd org-done-keywords)
  11575. (member kwd (cadr org-provide-todo-statistics))))))
  11576. (setq cnt-all (1+ cnt-all))
  11577. (and (eq org-provide-todo-statistics t)
  11578. kwd
  11579. (setq cnt-all (1+ cnt-all))))
  11580. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11581. (member kwd org-done-keywords))
  11582. (and (listp org-provide-todo-statistics)
  11583. (listp (car org-provide-todo-statistics))
  11584. (member kwd org-done-keywords)
  11585. (member kwd (cadr org-provide-todo-statistics)))
  11586. (and (listp org-provide-todo-statistics)
  11587. (stringp (car org-provide-todo-statistics))
  11588. (member kwd org-done-keywords)))
  11589. (setq cnt-done (1+ cnt-done)))
  11590. (outline-next-heading)))
  11591. (setq new
  11592. (if is-percent
  11593. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11594. (max 1 cnt-all)))
  11595. (format "[%d/%d]" cnt-done cnt-all))
  11596. ndel (- (match-end 0) checkbox-beg))
  11597. (goto-char checkbox-beg)
  11598. (insert new)
  11599. (delete-region (point) (+ (point) ndel))
  11600. (when org-auto-align-tags (org-fix-tags-on-the-fly)))
  11601. (when cookie-present
  11602. (run-hook-with-args 'org-after-todo-statistics-hook
  11603. cnt-done (- cnt-all cnt-done))))))
  11604. (run-hooks 'org-todo-statistics-hook)))
  11605. (defvar org-after-todo-statistics-hook nil
  11606. "Hook that is called after a TODO statistics cookie has been updated.
  11607. Each function is called with two arguments: the number of not-done entries
  11608. and the number of done entries.
  11609. For example, the following function, when added to this hook, will switch
  11610. an entry to DONE when all children are done, and back to TODO when new
  11611. entries are set to a TODO status. Note that this hook is only called
  11612. when there is a statistics cookie in the headline!
  11613. (defun org-summary-todo (n-done n-not-done)
  11614. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11615. (let (org-log-done org-log-states) ; turn off logging
  11616. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11617. ")
  11618. (defvar org-todo-statistics-hook nil
  11619. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11620. This hook runs even if there is no statistics cookie present, in which case
  11621. `org-after-todo-statistics-hook' would not run.")
  11622. (defun org-todo-trigger-tag-changes (state)
  11623. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11624. (let ((l org-todo-state-tags-triggers)
  11625. changes)
  11626. (when (or (not state) (equal state ""))
  11627. (setq changes (append changes (cdr (assoc "" l)))))
  11628. (when (and (stringp state) (> (length state) 0))
  11629. (setq changes (append changes (cdr (assoc state l)))))
  11630. (when (member state org-not-done-keywords)
  11631. (setq changes (append changes (cdr (assq 'todo l)))))
  11632. (when (member state org-done-keywords)
  11633. (setq changes (append changes (cdr (assq 'done l)))))
  11634. (dolist (c changes)
  11635. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11636. (defun org-local-logging (value)
  11637. "Get logging settings from a property VALUE."
  11638. ;; Directly set the variables, they are already local.
  11639. (setq org-log-done nil
  11640. org-log-repeat nil
  11641. org-todo-log-states nil)
  11642. (dolist (w (split-string value))
  11643. (let (a)
  11644. (cond
  11645. ((setq a (assoc w org-startup-options))
  11646. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11647. (set (nth 1 a) (nth 2 a))))
  11648. ((setq a (org-extract-log-state-settings w))
  11649. (and (member (car a) org-todo-keywords-1)
  11650. (push a org-todo-log-states)))))))
  11651. (defun org-get-todo-sequence-head (kwd)
  11652. "Return the head of the TODO sequence to which KWD belongs.
  11653. If KWD is not set, check if there is a text property remembering the
  11654. right sequence."
  11655. (let (p)
  11656. (cond
  11657. ((not kwd)
  11658. (or (get-text-property (point-at-bol) 'org-todo-head)
  11659. (progn
  11660. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11661. nil (point-at-eol)))
  11662. (get-text-property p 'org-todo-head))))
  11663. ((not (member kwd org-todo-keywords-1))
  11664. (car org-todo-keywords-1))
  11665. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11666. (defun org-fast-todo-selection ()
  11667. "Fast TODO keyword selection with single keys.
  11668. Returns the new TODO keyword, or nil if no state change should occur."
  11669. (let* ((fulltable org-todo-key-alist)
  11670. (done-keywords org-done-keywords) ;; needed for the faces.
  11671. (maxlen (apply 'max (mapcar
  11672. (lambda (x)
  11673. (if (stringp (car x)) (string-width (car x)) 0))
  11674. fulltable)))
  11675. (expert nil)
  11676. (fwidth (+ maxlen 3 1 3))
  11677. (ncol (/ (- (window-width) 4) fwidth))
  11678. tg cnt e c tbl
  11679. groups ingroup)
  11680. (save-excursion
  11681. (save-window-excursion
  11682. (if expert
  11683. (set-buffer (get-buffer-create " *Org todo*"))
  11684. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11685. (erase-buffer)
  11686. (setq-local org-done-keywords done-keywords)
  11687. (setq tbl fulltable cnt 0)
  11688. (while (setq e (pop tbl))
  11689. (cond
  11690. ((equal e '(:startgroup))
  11691. (push '() groups) (setq ingroup t)
  11692. (unless (= cnt 0)
  11693. (setq cnt 0)
  11694. (insert "\n"))
  11695. (insert "{ "))
  11696. ((equal e '(:endgroup))
  11697. (setq ingroup nil cnt 0)
  11698. (insert "}\n"))
  11699. ((equal e '(:newline))
  11700. (unless (= cnt 0)
  11701. (setq cnt 0)
  11702. (insert "\n")
  11703. (setq e (car tbl))
  11704. (while (equal (car tbl) '(:newline))
  11705. (insert "\n")
  11706. (setq tbl (cdr tbl)))))
  11707. (t
  11708. (setq tg (car e) c (cdr e))
  11709. (when ingroup (push tg (car groups)))
  11710. (setq tg (org-add-props tg nil 'face
  11711. (org-get-todo-face tg)))
  11712. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11713. (insert "[" c "] " tg (make-string
  11714. (- fwidth 4 (length tg)) ?\ ))
  11715. (when (= (setq cnt (1+ cnt)) ncol)
  11716. (insert "\n")
  11717. (when ingroup (insert " "))
  11718. (setq cnt 0)))))
  11719. (insert "\n")
  11720. (goto-char (point-min))
  11721. (unless expert (org-fit-window-to-buffer))
  11722. (message "[a-z..]:Set [SPC]:clear")
  11723. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11724. (cond
  11725. ((or (= c ?\C-g)
  11726. (and (= c ?q) (not (rassoc c fulltable))))
  11727. (setq quit-flag t))
  11728. ((= c ?\ ) nil)
  11729. ((setq e (rassoc c fulltable) tg (car e))
  11730. tg)
  11731. (t (setq quit-flag t)))))))
  11732. (defun org-entry-is-todo-p ()
  11733. (member (org-get-todo-state) org-not-done-keywords))
  11734. (defun org-entry-is-done-p ()
  11735. (member (org-get-todo-state) org-done-keywords))
  11736. (defun org-get-todo-state ()
  11737. "Return the TODO keyword of the current subtree."
  11738. (save-excursion
  11739. (org-back-to-heading t)
  11740. (and (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  11741. (match-end 2)
  11742. (match-string 2))))
  11743. (defun org-at-date-range-p (&optional inactive-ok)
  11744. "Non-nil if point is inside a date range.
  11745. When optional argument INACTIVE-OK is non-nil, also consider
  11746. inactive time ranges.
  11747. When this function returns a non-nil value, match data is set
  11748. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11749. on INACTIVE-OK."
  11750. (interactive)
  11751. (save-excursion
  11752. (catch 'exit
  11753. (let ((pos (point)))
  11754. (skip-chars-backward "^[<\r\n")
  11755. (skip-chars-backward "<[")
  11756. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11757. (>= (match-end 0) pos)
  11758. (throw 'exit t))
  11759. (skip-chars-backward "^<[\r\n")
  11760. (skip-chars-backward "<[")
  11761. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11762. (>= (match-end 0) pos)
  11763. (throw 'exit t)))
  11764. nil)))
  11765. (defun org-get-repeat (&optional timestamp)
  11766. "Check if there is a time-stamp with repeater in this entry.
  11767. Return the repeater, as a string, or nil. Also return nil when
  11768. this function is called before first heading.
  11769. When optional argument TIMESTAMP is a string, extract the
  11770. repeater from there instead."
  11771. (save-match-data
  11772. (cond (timestamp
  11773. (and (string-match org-repeat-re timestamp)
  11774. (match-string-no-properties 1 timestamp)))
  11775. ((org-before-first-heading-p) nil)
  11776. (t
  11777. (save-excursion
  11778. (org-back-to-heading t)
  11779. (let ((end (org-entry-end-position)))
  11780. (catch :repeat
  11781. (while (re-search-forward org-repeat-re end t)
  11782. (when (save-match-data (org-at-timestamp-p 'agenda))
  11783. (throw :repeat (match-string-no-properties 1)))))))))))
  11784. (defvar org-last-changed-timestamp)
  11785. (defvar org-last-inserted-timestamp)
  11786. (defvar org-log-post-message)
  11787. (defvar org-log-note-purpose)
  11788. (defvar org-log-note-how nil)
  11789. (defvar org-log-note-extra)
  11790. (defun org-auto-repeat-maybe (done-word)
  11791. "Check if the current headline contains a repeated time-stamp.
  11792. If yes, set TODO state back to what it was and change the base date
  11793. of repeating deadline/scheduled time stamps to new date.
  11794. This function is run automatically after each state change to a DONE state."
  11795. (let* ((repeat (org-get-repeat))
  11796. (aa (assoc org-last-state org-todo-kwd-alist))
  11797. (interpret (nth 1 aa))
  11798. (head (nth 2 aa))
  11799. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11800. (msg "Entry repeats: ")
  11801. (org-log-done nil)
  11802. (org-todo-log-states nil)
  11803. (end (copy-marker (org-entry-end-position))))
  11804. (unwind-protect
  11805. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11806. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11807. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11808. org-todo-repeat-to-state)))
  11809. (org-todo (cond
  11810. ((and to-state (member to-state org-todo-keywords-1))
  11811. to-state)
  11812. ((eq interpret 'type) org-last-state)
  11813. (head)
  11814. (t 'none))))
  11815. (org-back-to-heading t)
  11816. (org-add-planning-info nil nil 'closed)
  11817. ;; When `org-log-repeat' is non-nil or entry contains
  11818. ;; a clock, set LAST_REPEAT property.
  11819. (when (or org-log-repeat
  11820. (catch :clock
  11821. (save-excursion
  11822. (while (re-search-forward org-clock-line-re end t)
  11823. (when (org-at-clock-log-p) (throw :clock t))))))
  11824. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11825. (org-time-stamp-format t t)
  11826. (current-time))))
  11827. (when org-log-repeat
  11828. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11829. (memq 'org-add-log-note post-command-hook))
  11830. ;; We are already setup for some record.
  11831. (when (eq org-log-repeat 'note)
  11832. ;; Make sure we take a note, not only a time stamp.
  11833. (setq org-log-note-how 'note))
  11834. ;; Set up for taking a record.
  11835. (org-add-log-setup 'state
  11836. (or done-word (car org-done-keywords))
  11837. org-last-state
  11838. org-log-repeat)))
  11839. (let ((planning-re (regexp-opt
  11840. (list org-scheduled-string org-deadline-string))))
  11841. (while (re-search-forward org-ts-regexp end t)
  11842. (let* ((ts (match-string 0))
  11843. (planning? (org-at-planning-p))
  11844. (type (if (not planning?) "Plain:"
  11845. (save-excursion
  11846. (re-search-backward
  11847. planning-re (line-beginning-position) t)
  11848. (match-string 0)))))
  11849. (cond
  11850. ;; Ignore fake time-stamps (e.g., within comments).
  11851. ((not (org-at-timestamp-p 'agenda)))
  11852. ;; Time-stamps without a repeater are usually
  11853. ;; skipped. However, a SCHEDULED time-stamp without
  11854. ;; one is removed, as they are no longer relevant.
  11855. ((not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11856. ts))
  11857. (when (equal type org-scheduled-string)
  11858. (org-remove-timestamp-with-keyword type)))
  11859. (t
  11860. (let ((n (string-to-number (match-string 2 ts)))
  11861. (what (match-string 3 ts)))
  11862. (when (equal what "w") (setq n (* n 7) what "d"))
  11863. (when (and (equal what "h")
  11864. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11865. ts)))
  11866. (user-error
  11867. "Cannot repeat in Repeat in %d hour(s) because no hour \
  11868. has been set"
  11869. n))
  11870. ;; Preparation, see if we need to modify the start
  11871. ;; date for the change.
  11872. (when (match-end 1)
  11873. (let ((time (save-match-data
  11874. (org-time-string-to-time ts))))
  11875. (cond
  11876. ((equal (match-string 1 ts) ".")
  11877. ;; Shift starting date to today
  11878. (org-timestamp-change
  11879. (- (org-today) (time-to-days time))
  11880. 'day))
  11881. ((equal (match-string 1 ts) "+")
  11882. (let ((nshiftmax 10)
  11883. (nshift 0))
  11884. (while (or (= nshift 0)
  11885. (not (time-less-p (current-time) time)))
  11886. (when (= (cl-incf nshift) nshiftmax)
  11887. (or (y-or-n-p
  11888. (format "%d repeater intervals were not \
  11889. enough to shift date past today. Continue? "
  11890. nshift))
  11891. (user-error "Abort")))
  11892. (org-timestamp-change n (cdr (assoc what whata)))
  11893. (org-in-regexp org-ts-regexp3)
  11894. (setq ts (match-string 1))
  11895. (setq time
  11896. (save-match-data
  11897. (org-time-string-to-time ts)))))
  11898. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11899. ;; Rematch, so that we have everything in place
  11900. ;; for the real shift.
  11901. (org-in-regexp org-ts-regexp3)
  11902. (setq ts (match-string 1))
  11903. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11904. ts)))))
  11905. (save-excursion
  11906. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11907. (setq msg
  11908. (concat
  11909. msg type " " org-last-changed-timestamp " "))))))))
  11910. (setq org-log-post-message msg)
  11911. (message "%s" msg))
  11912. (set-marker end nil))))
  11913. (defun org-show-todo-tree (arg)
  11914. "Make a compact tree which shows all headlines marked with TODO.
  11915. The tree will show the lines where the regexp matches, and all higher
  11916. headlines above the match.
  11917. With a `\\[universal-argument]' prefix, prompt for a regexp to match.
  11918. With a numeric prefix N, construct a sparse tree for the Nth element
  11919. of `org-todo-keywords-1'."
  11920. (interactive "P")
  11921. (let ((case-fold-search nil)
  11922. (kwd-re
  11923. (cond ((null arg) org-not-done-regexp)
  11924. ((equal arg '(4))
  11925. (let ((kwd
  11926. (completing-read "Keyword (or KWD1|KWD2|...): "
  11927. (mapcar #'list org-todo-keywords-1))))
  11928. (concat "\\("
  11929. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11930. "\\)\\>")))
  11931. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11932. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11933. org-todo-keywords-1)))
  11934. (t (user-error "Invalid prefix argument: %s" arg)))))
  11935. (message "%d TODO entries found"
  11936. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11937. (defun org--deadline-or-schedule (arg type time)
  11938. "Insert DEADLINE or SCHEDULE information in current entry.
  11939. TYPE is either `deadline' or `scheduled'. See `org-deadline' or
  11940. `org-schedule' for information about ARG and TIME arguments."
  11941. (let* ((deadline? (eq type 'deadline))
  11942. (keyword (if deadline? org-deadline-string org-scheduled-string))
  11943. (log (if deadline? org-log-redeadline org-log-reschedule))
  11944. (old-date (org-entry-get nil (if deadline? "DEADLINE" "SCHEDULED")))
  11945. (old-date-time (and old-date (org-time-string-to-time old-date)))
  11946. ;; Save repeater cookie from either TIME or current scheduled
  11947. ;; time stamp. We are going to insert it back at the end of
  11948. ;; the process.
  11949. (repeater (or (and (org-string-nw-p time)
  11950. ;; We use `org-repeat-re' because we need
  11951. ;; to tell the difference between a real
  11952. ;; repeater and a time delta, e.g. "+2d".
  11953. (string-match org-repeat-re time)
  11954. (match-string 1 time))
  11955. (and (org-string-nw-p old-date)
  11956. (string-match "\\([.+-]+[0-9]+[hdwmy]\
  11957. \\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\)"
  11958. old-date)
  11959. (match-string 1 old-date)))))
  11960. (pcase arg
  11961. (`(4)
  11962. (when (and old-date log)
  11963. (org-add-log-setup (if deadline? 'deldeadline 'delschedule)
  11964. nil old-date log))
  11965. (org-remove-timestamp-with-keyword keyword)
  11966. (message (if deadline? "Item no longer has a deadline."
  11967. "Item is no longer scheduled.")))
  11968. (`(16)
  11969. (save-excursion
  11970. (org-back-to-heading t)
  11971. (let ((regexp (if deadline? org-deadline-time-regexp
  11972. org-scheduled-time-regexp)))
  11973. (if (not (re-search-forward regexp (line-end-position 2) t))
  11974. (user-error (if deadline? "No deadline information to update"
  11975. "No scheduled information to update"))
  11976. (let* ((rpl0 (match-string 1))
  11977. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0))
  11978. (msg (if deadline? "Warn starting from" "Delay until")))
  11979. (replace-match
  11980. (concat keyword
  11981. " <" rpl
  11982. (format " -%dd"
  11983. (abs (- (time-to-days
  11984. (save-match-data
  11985. (org-read-date
  11986. nil t nil msg old-date-time)))
  11987. (time-to-days old-date-time))))
  11988. ">") t t))))))
  11989. (_
  11990. (org-add-planning-info type time 'closed)
  11991. (when (and old-date
  11992. log
  11993. (not (equal old-date org-last-inserted-timestamp)))
  11994. (org-add-log-setup (if deadline? 'redeadline 'reschedule)
  11995. org-last-inserted-timestamp
  11996. old-date
  11997. log))
  11998. (when repeater
  11999. (save-excursion
  12000. (org-back-to-heading t)
  12001. (when (re-search-forward
  12002. (concat keyword " " org-last-inserted-timestamp)
  12003. (line-end-position 2)
  12004. t)
  12005. (goto-char (1- (match-end 0)))
  12006. (insert " " repeater)
  12007. (setq org-last-inserted-timestamp
  12008. (concat (substring org-last-inserted-timestamp 0 -1)
  12009. " " repeater
  12010. (substring org-last-inserted-timestamp -1))))))
  12011. (message (if deadline? "Deadline on %s" "Scheduled to %s")
  12012. org-last-inserted-timestamp)))))
  12013. (defun org-deadline (arg &optional time)
  12014. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12015. With one universal prefix argument, remove any deadline from the item.
  12016. With two universal prefix arguments, prompt for a warning delay.
  12017. With argument TIME, set the deadline at the corresponding date. TIME
  12018. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12019. (interactive "P")
  12020. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12021. (org-map-entries
  12022. (lambda () (org--deadline-or-schedule arg 'deadline time))
  12023. nil
  12024. (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12025. 'region-start-level
  12026. 'region)
  12027. (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
  12028. (org--deadline-or-schedule arg 'deadline time)))
  12029. (defun org-schedule (arg &optional time)
  12030. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12031. With one universal prefix argument, remove any scheduling date from the item.
  12032. With two universal prefix arguments, prompt for a delay cookie.
  12033. With argument TIME, scheduled at the corresponding date. TIME can
  12034. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12035. (interactive "P")
  12036. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12037. (org-map-entries
  12038. (lambda () (org--deadline-or-schedule arg 'scheduled time))
  12039. nil
  12040. (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12041. 'region-start-level
  12042. 'region)
  12043. (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
  12044. (org--deadline-or-schedule arg 'scheduled time)))
  12045. (defun org-get-scheduled-time (pom &optional inherit)
  12046. "Get the scheduled time as a time tuple, of a format suitable
  12047. for calling org-schedule with, or if there is no scheduling,
  12048. returns nil."
  12049. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12050. (when time
  12051. (apply 'encode-time (org-parse-time-string time)))))
  12052. (defun org-get-deadline-time (pom &optional inherit)
  12053. "Get the deadline as a time tuple, of a format suitable for
  12054. calling org-deadline with, or if there is no scheduling, returns
  12055. nil."
  12056. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12057. (when time
  12058. (apply 'encode-time (org-parse-time-string time)))))
  12059. (defun org-remove-timestamp-with-keyword (keyword)
  12060. "Remove all time stamps with KEYWORD in the current entry."
  12061. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12062. beg)
  12063. (save-excursion
  12064. (org-back-to-heading t)
  12065. (setq beg (point))
  12066. (outline-next-heading)
  12067. (while (re-search-backward re beg t)
  12068. (replace-match "")
  12069. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12070. (equal (char-before) ?\ ))
  12071. (backward-delete-char 1)
  12072. (when (string-match "^[ \t]*$" (buffer-substring
  12073. (point-at-bol) (point-at-eol)))
  12074. (delete-region (point-at-bol)
  12075. (min (point-max) (1+ (point-at-eol))))))))))
  12076. (defvar org-time-was-given) ; dynamically scoped parameter
  12077. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12078. (defun org-at-planning-p ()
  12079. "Non-nil when point is on a planning info line."
  12080. ;; This is as accurate and faster than `org-element-at-point' since
  12081. ;; planning info location is fixed in the section.
  12082. (org-with-wide-buffer
  12083. (beginning-of-line)
  12084. (and (looking-at-p org-planning-line-re)
  12085. (eq (point)
  12086. (ignore-errors
  12087. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12088. (org-back-to-heading t)
  12089. (org-with-limited-levels (org-back-to-heading t)))
  12090. (line-beginning-position 2))))))
  12091. (defun org-add-planning-info (what &optional time &rest remove)
  12092. "Insert new timestamp with keyword in the planning line.
  12093. WHAT indicates what kind of time stamp to add. It is a symbol
  12094. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12095. the time to use. If none is given, the user is prompted for
  12096. a date. REMOVE indicates what kind of entries to remove. An old
  12097. WHAT entry will also be removed."
  12098. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12099. (catch 'exit
  12100. (when (and (memq what '(scheduled deadline))
  12101. (or (not time)
  12102. (and (stringp time)
  12103. (string-match "^[-+]+[0-9]" time))))
  12104. ;; Try to get a default date/time from existing timestamp
  12105. (save-excursion
  12106. (org-back-to-heading t)
  12107. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12108. (when (re-search-forward (if (eq what 'scheduled)
  12109. org-scheduled-time-regexp
  12110. org-deadline-time-regexp)
  12111. end t)
  12112. (setq ts (match-string 1)
  12113. default-time (apply 'encode-time (org-parse-time-string ts))
  12114. default-input (and ts (org-get-compact-tod ts)))))))
  12115. (when what
  12116. (setq time
  12117. (if (stringp time)
  12118. ;; This is a string (relative or absolute), set
  12119. ;; proper date.
  12120. (apply #'encode-time
  12121. (org-read-date-analyze
  12122. time default-time (decode-time default-time)))
  12123. ;; If necessary, get the time from the user
  12124. (or time (org-read-date nil 'to-time nil nil
  12125. default-time default-input)))))
  12126. (org-with-wide-buffer
  12127. (org-back-to-heading t)
  12128. (forward-line)
  12129. (unless (bolp) (insert "\n"))
  12130. (cond ((looking-at-p org-planning-line-re)
  12131. ;; Move to current indentation.
  12132. (skip-chars-forward " \t")
  12133. ;; Check if we have to remove something.
  12134. (dolist (type (if what (cons what remove) remove))
  12135. (save-excursion
  12136. (when (re-search-forward
  12137. (cl-case type
  12138. (closed org-closed-time-regexp)
  12139. (deadline org-deadline-time-regexp)
  12140. (scheduled org-scheduled-time-regexp)
  12141. (otherwise
  12142. (error "Invalid planning type: %s" type)))
  12143. (line-end-position) t)
  12144. ;; Delete until next keyword or end of line.
  12145. (delete-region
  12146. (match-beginning 0)
  12147. (if (re-search-forward org-keyword-time-not-clock-regexp
  12148. (line-end-position)
  12149. t)
  12150. (match-beginning 0)
  12151. (line-end-position))))))
  12152. ;; If there is nothing more to add and no more keyword
  12153. ;; is left, remove the line completely.
  12154. (if (and (looking-at-p "[ \t]*$") (not what))
  12155. (delete-region (line-beginning-position)
  12156. (line-beginning-position 2))
  12157. ;; If we removed last keyword, do not leave trailing
  12158. ;; white space at the end of line.
  12159. (let ((p (point)))
  12160. (save-excursion
  12161. (end-of-line)
  12162. (unless (= (skip-chars-backward " \t" p) 0)
  12163. (delete-region (point) (line-end-position)))))))
  12164. ((not what) (throw 'exit nil)) ; Nothing to do.
  12165. (t (insert-before-markers "\n")
  12166. (backward-char 1)
  12167. (when org-adapt-indentation
  12168. (indent-to-column (1+ (org-outline-level))))))
  12169. (when what
  12170. ;; Insert planning keyword.
  12171. (insert (cl-case what
  12172. (closed org-closed-string)
  12173. (deadline org-deadline-string)
  12174. (scheduled org-scheduled-string)
  12175. (otherwise (error "Invalid planning type: %s" what)))
  12176. " ")
  12177. ;; Insert associated timestamp.
  12178. (let ((ts (org-insert-time-stamp
  12179. time
  12180. (or org-time-was-given
  12181. (and (eq what 'closed) org-log-done-with-time))
  12182. (eq what 'closed)
  12183. nil nil (list org-end-time-was-given))))
  12184. (unless (eolp) (insert " "))
  12185. ts))))))
  12186. (defvar org-log-note-marker (make-marker)
  12187. "Marker pointing at the entry where the note is to be inserted.")
  12188. (defvar org-log-note-purpose nil)
  12189. (defvar org-log-note-state nil)
  12190. (defvar org-log-note-previous-state nil)
  12191. (defvar org-log-note-extra nil)
  12192. (defvar org-log-note-window-configuration nil)
  12193. (defvar org-log-note-return-to (make-marker))
  12194. (defvar org-log-note-effective-time nil
  12195. "Remembered current time so that dynamically scoped
  12196. `org-extend-today-until' affects timestamps in state change log")
  12197. (defvar org-log-post-message nil
  12198. "Message to be displayed after a log note has been stored.
  12199. The auto-repeater uses this.")
  12200. (defun org-add-note ()
  12201. "Add a note to the current entry.
  12202. This is done in the same way as adding a state change note."
  12203. (interactive)
  12204. (org-add-log-setup 'note))
  12205. (defun org-log-beginning (&optional create)
  12206. "Return expected start of log notes in current entry.
  12207. When optional argument CREATE is non-nil, the function creates
  12208. a drawer to store notes, if necessary. Returned position ignores
  12209. narrowing."
  12210. (org-with-wide-buffer
  12211. (let ((drawer (org-log-into-drawer)))
  12212. (cond
  12213. (drawer
  12214. (org-end-of-meta-data)
  12215. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12216. (end (if (org-at-heading-p) (point)
  12217. (save-excursion (outline-next-heading) (point))))
  12218. (case-fold-search t))
  12219. (catch 'exit
  12220. ;; Try to find existing drawer.
  12221. (while (re-search-forward regexp end t)
  12222. (let ((element (org-element-at-point)))
  12223. (when (eq (org-element-type element) 'drawer)
  12224. (let ((cend (org-element-property :contents-end element)))
  12225. (when (and (not org-log-states-order-reversed) cend)
  12226. (goto-char cend)))
  12227. (throw 'exit nil))))
  12228. ;; No drawer found. Create one, if permitted.
  12229. (when create
  12230. (unless (bolp) (insert "\n"))
  12231. (let ((beg (point)))
  12232. (insert ":" drawer ":\n:END:\n")
  12233. (org-indent-region beg (point)))
  12234. (end-of-line -1)))))
  12235. (t
  12236. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12237. (skip-chars-forward " \t\n")
  12238. (beginning-of-line)
  12239. (unless org-log-states-order-reversed
  12240. (org-skip-over-state-notes)
  12241. (skip-chars-backward " \t\n")
  12242. (forward-line)))))
  12243. (if (bolp) (point) (line-beginning-position 2))))
  12244. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12245. "Set up the post command hook to take a note.
  12246. If this is about to TODO state change, the new state is expected in STATE.
  12247. HOW is an indicator what kind of note should be created.
  12248. EXTRA is additional text that will be inserted into the notes buffer."
  12249. (move-marker org-log-note-marker (point))
  12250. (setq org-log-note-purpose purpose
  12251. org-log-note-state state
  12252. org-log-note-previous-state prev-state
  12253. org-log-note-how how
  12254. org-log-note-extra extra
  12255. org-log-note-effective-time (org-current-effective-time))
  12256. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12257. (defun org-skip-over-state-notes ()
  12258. "Skip past the list of State notes in an entry."
  12259. (when (ignore-errors (goto-char (org-in-item-p)))
  12260. (let* ((struct (org-list-struct))
  12261. (prevs (org-list-prevs-alist struct))
  12262. (regexp
  12263. (concat "[ \t]*- +"
  12264. (replace-regexp-in-string
  12265. " +" " +"
  12266. (org-replace-escapes
  12267. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12268. `(("%d" . ,org-ts-regexp-inactive)
  12269. ("%D" . ,org-ts-regexp)
  12270. ("%s" . "\"\\S-+\"")
  12271. ("%S" . "\"\\S-+\"")
  12272. ("%t" . ,org-ts-regexp-inactive)
  12273. ("%T" . ,org-ts-regexp)
  12274. ("%u" . ".*?")
  12275. ("%U" . ".*?")))))))
  12276. (while (looking-at-p regexp)
  12277. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12278. (org-list-get-item-end (point) struct)))))))
  12279. (defun org-add-log-note (&optional _purpose)
  12280. "Pop up a window for taking a note, and add this note later."
  12281. (remove-hook 'post-command-hook 'org-add-log-note)
  12282. (setq org-log-note-window-configuration (current-window-configuration))
  12283. (delete-other-windows)
  12284. (move-marker org-log-note-return-to (point))
  12285. (pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12286. (goto-char org-log-note-marker)
  12287. (org-switch-to-buffer-other-window "*Org Note*")
  12288. (erase-buffer)
  12289. (if (memq org-log-note-how '(time state))
  12290. (org-store-log-note)
  12291. (let ((org-inhibit-startup t)) (org-mode))
  12292. (insert (format "# Insert note for %s.
  12293. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12294. (cond
  12295. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12296. ((eq org-log-note-purpose 'done) "closed todo item")
  12297. ((eq org-log-note-purpose 'state)
  12298. (format "state change from \"%s\" to \"%s\""
  12299. (or org-log-note-previous-state "")
  12300. (or org-log-note-state "")))
  12301. ((eq org-log-note-purpose 'reschedule)
  12302. "rescheduling")
  12303. ((eq org-log-note-purpose 'delschedule)
  12304. "no longer scheduled")
  12305. ((eq org-log-note-purpose 'redeadline)
  12306. "changing deadline")
  12307. ((eq org-log-note-purpose 'deldeadline)
  12308. "removing deadline")
  12309. ((eq org-log-note-purpose 'refile)
  12310. "refiling")
  12311. ((eq org-log-note-purpose 'note)
  12312. "this entry")
  12313. (t (error "This should not happen")))))
  12314. (when org-log-note-extra (insert org-log-note-extra))
  12315. (setq-local org-finish-function 'org-store-log-note)
  12316. (run-hooks 'org-log-buffer-setup-hook)))
  12317. (defvar org-note-abort nil) ; dynamically scoped
  12318. (defun org-store-log-note ()
  12319. "Finish taking a log note, and insert it to where it belongs."
  12320. (let ((txt (prog1 (buffer-string)
  12321. (kill-buffer)))
  12322. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12323. lines)
  12324. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12325. (setq txt (replace-match "" t t txt)))
  12326. (when (string-match "\\s-+\\'" txt)
  12327. (setq txt (replace-match "" t t txt)))
  12328. (setq lines (and (not (equal "" txt)) (org-split-string txt "\n")))
  12329. (when (org-string-nw-p note)
  12330. (setq note
  12331. (org-replace-escapes
  12332. note
  12333. (list (cons "%u" (user-login-name))
  12334. (cons "%U" user-full-name)
  12335. (cons "%t" (format-time-string
  12336. (org-time-stamp-format 'long 'inactive)
  12337. org-log-note-effective-time))
  12338. (cons "%T" (format-time-string
  12339. (org-time-stamp-format 'long nil)
  12340. org-log-note-effective-time))
  12341. (cons "%d" (format-time-string
  12342. (org-time-stamp-format nil 'inactive)
  12343. org-log-note-effective-time))
  12344. (cons "%D" (format-time-string
  12345. (org-time-stamp-format nil nil)
  12346. org-log-note-effective-time))
  12347. (cons "%s" (cond
  12348. ((not org-log-note-state) "")
  12349. ((string-match-p org-ts-regexp
  12350. org-log-note-state)
  12351. (format "\"[%s]\""
  12352. (substring org-log-note-state 1 -1)))
  12353. (t (format "\"%s\"" org-log-note-state))))
  12354. (cons "%S"
  12355. (cond
  12356. ((not org-log-note-previous-state) "")
  12357. ((string-match-p org-ts-regexp
  12358. org-log-note-previous-state)
  12359. (format "\"[%s]\""
  12360. (substring
  12361. org-log-note-previous-state 1 -1)))
  12362. (t (format "\"%s\""
  12363. org-log-note-previous-state)))))))
  12364. (when lines (setq note (concat note " \\\\")))
  12365. (push note lines))
  12366. (when (and lines (not org-note-abort))
  12367. (with-current-buffer (marker-buffer org-log-note-marker)
  12368. (org-with-wide-buffer
  12369. ;; Find location for the new note.
  12370. (goto-char org-log-note-marker)
  12371. (set-marker org-log-note-marker nil)
  12372. ;; Note associated to a clock is to be located right after
  12373. ;; the clock. Do not move point.
  12374. (unless (eq org-log-note-purpose 'clock-out)
  12375. (goto-char (org-log-beginning t)))
  12376. ;; Make sure point is at the beginning of an empty line.
  12377. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12378. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12379. ;; In an existing list, add a new item at the top level.
  12380. ;; Otherwise, indent line like a regular one.
  12381. (let ((itemp (org-in-item-p)))
  12382. (if itemp
  12383. (indent-line-to
  12384. (let ((struct (save-excursion
  12385. (goto-char itemp) (org-list-struct))))
  12386. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12387. (org-indent-line)))
  12388. (insert (org-list-bullet-string "-") (pop lines))
  12389. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12390. (dolist (line lines)
  12391. (insert "\n")
  12392. (indent-line-to ind)
  12393. (insert line)))
  12394. (message "Note stored")
  12395. (org-back-to-heading t)
  12396. (org-cycle-hide-drawers 'children))
  12397. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12398. ;; from within `org-add-log-note' because `buffer-undo-list'
  12399. ;; is then modified outside of `org-with-remote-undo'.
  12400. (when (eq this-command 'org-agenda-todo)
  12401. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12402. ;; Don't add undo information when called from `org-agenda-todo'.
  12403. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12404. (set-window-configuration org-log-note-window-configuration)
  12405. (with-current-buffer (marker-buffer org-log-note-return-to)
  12406. (goto-char org-log-note-return-to))
  12407. (move-marker org-log-note-return-to nil)
  12408. (when org-log-post-message (message "%s" org-log-post-message))))
  12409. (defun org-remove-empty-drawer-at (pos)
  12410. "Remove an empty drawer at position POS.
  12411. POS may also be a marker."
  12412. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12413. (org-with-wide-buffer
  12414. (goto-char pos)
  12415. (let ((drawer (org-element-at-point)))
  12416. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12417. (not (org-element-property :contents-begin drawer)))
  12418. (delete-region (org-element-property :begin drawer)
  12419. (progn (goto-char (org-element-property :end drawer))
  12420. (skip-chars-backward " \r\t\n")
  12421. (forward-line)
  12422. (point))))))))
  12423. (defvar org-ts-type nil)
  12424. (defun org-sparse-tree (&optional arg type)
  12425. "Create a sparse tree, prompt for the details.
  12426. This command can create sparse trees. You first need to select the type
  12427. of match used to create the tree:
  12428. t Show all TODO entries.
  12429. T Show entries with a specific TODO keyword.
  12430. m Show entries selected by a tags/property match.
  12431. p Enter a property name and its value (both with completion on existing
  12432. names/values) and show entries with that property.
  12433. r Show entries matching a regular expression (`/' can be used as well).
  12434. b Show deadlines and scheduled items before a date.
  12435. a Show deadlines and scheduled items after a date.
  12436. d Show deadlines due within `org-deadline-warning-days'.
  12437. D Show deadlines and scheduled items between a date range."
  12438. (interactive "P")
  12439. (setq type (or type org-sparse-tree-default-date-type))
  12440. (setq org-ts-type type)
  12441. (message "Sparse tree: [r]egexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12442. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12443. \[c]ycle through date types: %s"
  12444. (cl-case type
  12445. (all "all timestamps")
  12446. (scheduled "only scheduled")
  12447. (deadline "only deadline")
  12448. (active "only active timestamps")
  12449. (inactive "only inactive timestamps")
  12450. (closed "with a closed time-stamp")
  12451. (otherwise "scheduled/deadline")))
  12452. (let ((answer (read-char-exclusive)))
  12453. (cl-case answer
  12454. (?c
  12455. (org-sparse-tree
  12456. arg
  12457. (cadr
  12458. (memq type '(nil all scheduled deadline active inactive closed)))))
  12459. (?d (call-interactively 'org-check-deadlines))
  12460. (?b (call-interactively 'org-check-before-date))
  12461. (?a (call-interactively 'org-check-after-date))
  12462. (?D (call-interactively 'org-check-dates-range))
  12463. (?t (call-interactively 'org-show-todo-tree))
  12464. (?T (org-show-todo-tree '(4)))
  12465. (?m (call-interactively 'org-match-sparse-tree))
  12466. ((?p ?P)
  12467. (let* ((kwd (completing-read
  12468. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12469. (value (completing-read
  12470. "Value: " (mapcar #'list (org-property-values kwd)))))
  12471. (unless (string-match "\\`{.*}\\'" value)
  12472. (setq value (concat "\"" value "\"")))
  12473. (org-match-sparse-tree arg (concat kwd "=" value))))
  12474. ((?r ?R ?/) (call-interactively 'org-occur))
  12475. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12476. (defvar-local org-occur-highlights nil
  12477. "List of overlays used for occur matches.")
  12478. (defvar-local org-occur-parameters nil
  12479. "Parameters of the active org-occur calls.
  12480. This is a list, each call to org-occur pushes as cons cell,
  12481. containing the regular expression and the callback, onto the list.
  12482. The list can contain several entries if `org-occur' has been called
  12483. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12484. will only contain one set of parameters. When the highlights are
  12485. removed (for example with `C-c C-c', or with the next edit (depending
  12486. on `org-remove-highlights-with-change'), this variable is emptied
  12487. as well.")
  12488. (defun org-occur (regexp &optional keep-previous callback)
  12489. "Make a compact tree which shows all matches of REGEXP.
  12490. The tree will show the lines where the regexp matches, and any other context
  12491. defined in `org-show-context-detail', which see.
  12492. When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
  12493. done by a previous call to `org-occur' will be kept, to allow stacking of
  12494. calls to this command.
  12495. Optional argument CALLBACK can be a function of no argument. In this case,
  12496. it is called with point at the end of the match, match data being set
  12497. accordingly. Current match is shown only if the return value is non-nil.
  12498. The function must neither move point nor alter narrowing."
  12499. (interactive "sRegexp: \nP")
  12500. (when (equal regexp "")
  12501. (user-error "Regexp cannot be empty"))
  12502. (unless keep-previous
  12503. (org-remove-occur-highlights nil nil t))
  12504. (push (cons regexp callback) org-occur-parameters)
  12505. (let ((cnt 0))
  12506. (save-excursion
  12507. (goto-char (point-min))
  12508. (when (or (not keep-previous) ; do not want to keep
  12509. (not org-occur-highlights)) ; no previous matches
  12510. ;; hide everything
  12511. (org-overview))
  12512. (let ((case-fold-search (if (eq org-occur-case-fold-search 'smart)
  12513. (isearch-no-upper-case-p regexp t)
  12514. org-occur-case-fold-search)))
  12515. (while (re-search-forward regexp nil t)
  12516. (when (or (not callback)
  12517. (save-match-data (funcall callback)))
  12518. (setq cnt (1+ cnt))
  12519. (when org-highlight-sparse-tree-matches
  12520. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12521. (org-show-context 'occur-tree)))))
  12522. (when org-remove-highlights-with-change
  12523. (add-hook 'before-change-functions 'org-remove-occur-highlights
  12524. nil 'local))
  12525. (unless org-sparse-tree-open-archived-trees
  12526. (org-hide-archived-subtrees (point-min) (point-max)))
  12527. (run-hooks 'org-occur-hook)
  12528. (when (called-interactively-p 'interactive)
  12529. (message "%d match(es) for regexp %s" cnt regexp))
  12530. cnt))
  12531. (defun org-occur-next-match (&optional n _reset)
  12532. "Function for `next-error-function' to find sparse tree matches.
  12533. N is the number of matches to move, when negative move backwards.
  12534. This function always goes back to the starting point when no
  12535. match is found."
  12536. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12537. (search-func (if (< n 0)
  12538. 'previous-single-char-property-change
  12539. 'next-single-char-property-change))
  12540. (n (abs n))
  12541. (pos (point))
  12542. p1)
  12543. (catch 'exit
  12544. (while (setq p1 (funcall search-func (point) 'org-type))
  12545. (when (equal p1 limit)
  12546. (goto-char pos)
  12547. (user-error "No more matches"))
  12548. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12549. (setq n (1- n))
  12550. (when (= n 0)
  12551. (goto-char p1)
  12552. (throw 'exit (point))))
  12553. (goto-char p1))
  12554. (goto-char p1)
  12555. (user-error "No more matches"))))
  12556. (defun org-show-context (&optional key)
  12557. "Make sure point and context are visible.
  12558. Optional argument KEY, when non-nil, is a symbol. See
  12559. `org-show-context-detail' for allowed values and how much is to
  12560. be shown."
  12561. (org-show-set-visibility
  12562. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12563. ((cdr (assq key org-show-context-detail)))
  12564. (t (cdr (assq 'default org-show-context-detail))))))
  12565. (defun org-show-set-visibility (detail)
  12566. "Set visibility around point according to DETAIL.
  12567. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12568. `tree', `canonical' or t. See `org-show-context-detail' for more
  12569. information."
  12570. ;; Show current heading and possibly its entry, following headline
  12571. ;; or all children.
  12572. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12573. (org-flag-heading nil)
  12574. (org-show-entry)
  12575. ;; If point is hidden within a drawer or a block, make sure to
  12576. ;; expose it.
  12577. (dolist (o (overlays-at (point)))
  12578. (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
  12579. (delete-overlay o)))
  12580. (unless (org-before-first-heading-p)
  12581. (org-with-limited-levels
  12582. (cl-case detail
  12583. ((tree canonical t) (org-show-children))
  12584. ((nil minimal ancestors))
  12585. (t (save-excursion
  12586. (outline-next-heading)
  12587. (org-flag-heading nil)))))))
  12588. ;; Show all siblings.
  12589. (when (eq detail 'lineage) (org-show-siblings))
  12590. ;; Show ancestors, possibly with their children.
  12591. (when (memq detail '(ancestors lineage tree canonical t))
  12592. (save-excursion
  12593. (while (org-up-heading-safe)
  12594. (org-flag-heading nil)
  12595. (when (memq detail '(canonical t)) (org-show-entry))
  12596. (when (memq detail '(tree canonical t)) (org-show-children))))))
  12597. (defvar org-reveal-start-hook nil
  12598. "Hook run before revealing a location.")
  12599. (defun org-reveal (&optional siblings)
  12600. "Show current entry, hierarchy above it, and the following headline.
  12601. This can be used to show a consistent set of context around
  12602. locations exposed with `org-show-context'.
  12603. With optional argument SIBLINGS, on each level of the hierarchy all
  12604. siblings are shown. This repairs the tree structure to what it would
  12605. look like when opened with hierarchical calls to `org-cycle'.
  12606. With a \\[universal-argument] \\[universal-argument] prefix, \
  12607. go to the parent and show the entire tree."
  12608. (interactive "P")
  12609. (run-hooks 'org-reveal-start-hook)
  12610. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12611. ((equal siblings '(16))
  12612. (save-excursion
  12613. (when (org-up-heading-safe)
  12614. (org-show-subtree)
  12615. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12616. (t (org-show-set-visibility 'lineage))))
  12617. (defun org-highlight-new-match (beg end)
  12618. "Highlight from BEG to END and mark the highlight is an occur headline."
  12619. (let ((ov (make-overlay beg end)))
  12620. (overlay-put ov 'face 'secondary-selection)
  12621. (overlay-put ov 'org-type 'org-occur)
  12622. (push ov org-occur-highlights)))
  12623. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12624. "Remove the occur highlights from the buffer.
  12625. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12626. from the `before-change-functions' in the current buffer."
  12627. (interactive)
  12628. (unless org-inhibit-highlight-removal
  12629. (mapc #'delete-overlay org-occur-highlights)
  12630. (setq org-occur-highlights nil)
  12631. (setq org-occur-parameters nil)
  12632. (unless noremove
  12633. (remove-hook 'before-change-functions
  12634. 'org-remove-occur-highlights 'local))))
  12635. ;;;; Priorities
  12636. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12637. "Regular expression matching the priority indicator.")
  12638. (defvar org-remove-priority-next-time nil)
  12639. (defun org-priority-up ()
  12640. "Increase the priority of the current item."
  12641. (interactive)
  12642. (org-priority 'up))
  12643. (defun org-priority-down ()
  12644. "Decrease the priority of the current item."
  12645. (interactive)
  12646. (org-priority 'down))
  12647. (defun org-priority (&optional action _show)
  12648. "Change the priority of an item.
  12649. ACTION can be `set', `up', `down', or a character."
  12650. (interactive "P")
  12651. (if (equal action '(4))
  12652. (org-show-priority)
  12653. (unless org-enable-priority-commands
  12654. (user-error "Priority commands are disabled"))
  12655. (setq action (or action 'set))
  12656. (let (current new news have remove)
  12657. (save-excursion
  12658. (org-back-to-heading t)
  12659. (when (looking-at org-priority-regexp)
  12660. (setq current (string-to-char (match-string 2))
  12661. have t))
  12662. (cond
  12663. ((eq action 'remove)
  12664. (setq remove t new ?\ ))
  12665. ((or (eq action 'set)
  12666. (integerp action))
  12667. (if (not (eq action 'set))
  12668. (setq new action)
  12669. (message "Priority %c-%c, SPC to remove: "
  12670. org-highest-priority org-lowest-priority)
  12671. (save-match-data
  12672. (setq new (read-char-exclusive))))
  12673. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12674. (= (upcase org-lowest-priority) org-lowest-priority))
  12675. (setq new (upcase new)))
  12676. (cond ((equal new ?\ ) (setq remove t))
  12677. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12678. (user-error "Priority must be between `%c' and `%c'"
  12679. org-highest-priority org-lowest-priority))))
  12680. ((eq action 'up)
  12681. (setq new (if have
  12682. (1- current) ; normal cycling
  12683. ;; last priority was empty
  12684. (if (eq last-command this-command)
  12685. org-lowest-priority ; wrap around empty to lowest
  12686. ;; default
  12687. (if org-priority-start-cycle-with-default
  12688. org-default-priority
  12689. (1- org-default-priority))))))
  12690. ((eq action 'down)
  12691. (setq new (if have
  12692. (1+ current) ; normal cycling
  12693. ;; last priority was empty
  12694. (if (eq last-command this-command)
  12695. org-highest-priority ; wrap around empty to highest
  12696. ;; default
  12697. (if org-priority-start-cycle-with-default
  12698. org-default-priority
  12699. (1+ org-default-priority))))))
  12700. (t (user-error "Invalid action")))
  12701. (when (or (< (upcase new) org-highest-priority)
  12702. (> (upcase new) org-lowest-priority))
  12703. (if (and (memq action '(up down))
  12704. (not have) (not (eq last-command this-command)))
  12705. ;; `new' is from default priority
  12706. (error
  12707. "The default can not be set, see `org-default-priority' why")
  12708. ;; normal cycling: `new' is beyond highest/lowest priority
  12709. ;; and is wrapped around to the empty priority
  12710. (setq remove t)))
  12711. (setq news (format "%c" new))
  12712. (if have
  12713. (if remove
  12714. (replace-match "" t t nil 1)
  12715. (replace-match news t t nil 2))
  12716. (if remove
  12717. (user-error "No priority cookie found in line")
  12718. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  12719. (if (match-end 2)
  12720. (progn
  12721. (goto-char (match-end 2))
  12722. (insert " [#" news "]"))
  12723. (goto-char (match-beginning 3))
  12724. (insert "[#" news "] "))))
  12725. (org-set-tags nil 'align))
  12726. (if remove
  12727. (message "Priority removed")
  12728. (message "Priority of current item set to %s" news)))))
  12729. (defun org-show-priority ()
  12730. "Show the priority of the current item.
  12731. This priority is composed of the main priority given with the [#A] cookies,
  12732. and by additional input from the age of a schedules or deadline entry."
  12733. (interactive)
  12734. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12735. (org-get-at-bol 'priority)
  12736. (save-excursion
  12737. (save-match-data
  12738. (beginning-of-line)
  12739. (and (looking-at org-heading-regexp)
  12740. (org-get-priority (match-string 0))))))))
  12741. (message "Priority is %d" (if pri pri -1000))))
  12742. (defun org-get-priority (s)
  12743. "Find priority cookie and return priority."
  12744. (save-match-data
  12745. (if (functionp org-get-priority-function)
  12746. (funcall org-get-priority-function)
  12747. (if (not (string-match org-priority-regexp s))
  12748. (* 1000 (- org-lowest-priority org-default-priority))
  12749. (* 1000 (- org-lowest-priority
  12750. (string-to-char (match-string 2 s))))))))
  12751. ;;;; Tags
  12752. (defvar org-agenda-archives-mode)
  12753. (defvar org-map-continue-from nil
  12754. "Position from where mapping should continue.
  12755. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12756. (defvar org-scanner-tags nil
  12757. "The current tag list while the tags scanner is running.")
  12758. (defvar org-trust-scanner-tags nil
  12759. "Should `org-get-tags-at' use the tags for the scanner.
  12760. This is for internal dynamical scoping only.
  12761. When this is non-nil, the function `org-get-tags-at' will return the value
  12762. of `org-scanner-tags' instead of building the list by itself. This
  12763. can lead to large speed-ups when the tags scanner is used in a file with
  12764. many entries, and when the list of tags is retrieved, for example to
  12765. obtain a list of properties. Building the tags list for each entry in such
  12766. a file becomes an N^2 operation - but with this variable set, it scales
  12767. as N.")
  12768. (defvar org--matcher-tags-todo-only nil)
  12769. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12770. "Scan headline tags with inheritance and produce output ACTION.
  12771. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12772. or `agenda' to produce an entry list for an agenda view. It can also be
  12773. a Lisp form or a function that should be called at each matched headline, in
  12774. this case the return value is a list of all return values from these calls.
  12775. MATCHER is a function accepting three arguments, returning
  12776. a non-nil value whenever a given set of tags qualifies a headline
  12777. for inclusion. See `org-make-tags-matcher' for more information.
  12778. As a special case, it can also be set to t (respectively nil) in
  12779. order to match all (respectively none) headline.
  12780. When TODO-ONLY is non-nil, only lines with a TODO keyword are
  12781. included in the output.
  12782. START-LEVEL can be a string with asterisks, reducing the scope to
  12783. headlines matching this string."
  12784. (require 'org-agenda)
  12785. (let* ((re (concat "^"
  12786. (if start-level
  12787. ;; Get the correct level to match
  12788. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12789. org-outline-regexp)
  12790. " *\\(\\<\\("
  12791. (mapconcat #'regexp-quote org-todo-keywords-1 "\\|")
  12792. "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
  12793. (props (list 'face 'default
  12794. 'done-face 'org-agenda-done
  12795. 'undone-face 'default
  12796. 'mouse-face 'highlight
  12797. 'org-not-done-regexp org-not-done-regexp
  12798. 'org-todo-regexp org-todo-regexp
  12799. 'org-complex-heading-regexp org-complex-heading-regexp
  12800. 'help-echo
  12801. (format "mouse-2 or RET jump to org file %s"
  12802. (abbreviate-file-name
  12803. (or (buffer-file-name (buffer-base-buffer))
  12804. (buffer-name (buffer-base-buffer)))))))
  12805. (org-map-continue-from nil)
  12806. lspos tags tags-list
  12807. (tags-alist (list (cons 0 org-file-tags)))
  12808. (llast 0) rtn rtn1 level category i txt
  12809. todo marker entry priority
  12810. ts-date ts-date-type ts-date-pair)
  12811. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12812. (setq action (list 'lambda nil action)))
  12813. (save-excursion
  12814. (goto-char (point-min))
  12815. (when (eq action 'sparse-tree)
  12816. (org-overview)
  12817. (org-remove-occur-highlights))
  12818. (while (let (case-fold-search)
  12819. (re-search-forward re nil t))
  12820. (setq org-map-continue-from nil)
  12821. (catch :skip
  12822. (setq todo
  12823. ;; TODO: is the 1-2 difference a bug?
  12824. (when (match-end 1) (match-string-no-properties 2))
  12825. tags (when (match-end 4) (match-string-no-properties 4)))
  12826. (goto-char (setq lspos (match-beginning 0)))
  12827. (setq level (org-reduced-level (org-outline-level))
  12828. category (org-get-category))
  12829. (when (eq action 'agenda)
  12830. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12831. ts-date (car ts-date-pair)
  12832. ts-date-type (cdr ts-date-pair)))
  12833. (setq i llast llast level)
  12834. ;; remove tag lists from same and sublevels
  12835. (while (>= i level)
  12836. (when (setq entry (assoc i tags-alist))
  12837. (setq tags-alist (delete entry tags-alist)))
  12838. (setq i (1- i)))
  12839. ;; add the next tags
  12840. (when tags
  12841. (setq tags (org-split-string tags ":")
  12842. tags-alist
  12843. (cons (cons level tags) tags-alist)))
  12844. ;; compile tags for current headline
  12845. (setq tags-list
  12846. (if org-use-tag-inheritance
  12847. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12848. tags)
  12849. org-scanner-tags tags-list)
  12850. (when org-use-tag-inheritance
  12851. (setcdr (car tags-alist)
  12852. (mapcar (lambda (x)
  12853. (setq x (copy-sequence x))
  12854. (org-add-prop-inherited x))
  12855. (cdar tags-alist))))
  12856. (when (and tags org-use-tag-inheritance
  12857. (or (not (eq t org-use-tag-inheritance))
  12858. org-tags-exclude-from-inheritance))
  12859. ;; Selective inheritance, remove uninherited ones.
  12860. (setcdr (car tags-alist)
  12861. (org-remove-uninherited-tags (cdar tags-alist))))
  12862. (when (and
  12863. ;; eval matcher only when the todo condition is OK
  12864. (and (or (not todo-only) (member todo org-todo-keywords-1))
  12865. (if (functionp matcher)
  12866. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12867. (funcall matcher todo tags-list level))
  12868. matcher))
  12869. ;; Call the skipper, but return t if it does not
  12870. ;; skip, so that the `and' form continues evaluating.
  12871. (progn
  12872. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12873. t)
  12874. ;; Check if timestamps are deselecting this entry
  12875. (or (not todo-only)
  12876. (and (member todo org-todo-keywords-1)
  12877. (or (not org-agenda-tags-todo-honor-ignore-options)
  12878. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12879. ;; select this headline
  12880. (cond
  12881. ((eq action 'sparse-tree)
  12882. (and org-highlight-sparse-tree-matches
  12883. (org-get-heading) (match-end 0)
  12884. (org-highlight-new-match
  12885. (match-beginning 1) (match-end 1)))
  12886. (org-show-context 'tags-tree))
  12887. ((eq action 'agenda)
  12888. (setq txt (org-agenda-format-item
  12889. ""
  12890. (concat
  12891. (if (eq org-tags-match-list-sublevels 'indented)
  12892. (make-string (1- level) ?.) "")
  12893. (org-get-heading))
  12894. (make-string level ?\s)
  12895. category
  12896. tags-list)
  12897. priority (org-get-priority txt))
  12898. (goto-char lspos)
  12899. (setq marker (org-agenda-new-marker))
  12900. (org-add-props txt props
  12901. 'org-marker marker 'org-hd-marker marker 'org-category category
  12902. 'todo-state todo
  12903. 'ts-date ts-date
  12904. 'priority priority
  12905. 'type (concat "tagsmatch" ts-date-type))
  12906. (push txt rtn))
  12907. ((functionp action)
  12908. (setq org-map-continue-from nil)
  12909. (save-excursion
  12910. (setq rtn1 (funcall action))
  12911. (push rtn1 rtn)))
  12912. (t (user-error "Invalid action")))
  12913. ;; if we are to skip sublevels, jump to end of subtree
  12914. (unless org-tags-match-list-sublevels
  12915. (org-end-of-subtree t)
  12916. (backward-char 1))))
  12917. ;; Get the correct position from where to continue
  12918. (if org-map-continue-from
  12919. (goto-char org-map-continue-from)
  12920. (and (= (point) lspos) (end-of-line 1)))))
  12921. (when (and (eq action 'sparse-tree)
  12922. (not org-sparse-tree-open-archived-trees))
  12923. (org-hide-archived-subtrees (point-min) (point-max)))
  12924. (nreverse rtn)))
  12925. (defun org-remove-uninherited-tags (tags)
  12926. "Remove all tags that are not inherited from the list TAGS."
  12927. (cond
  12928. ((eq org-use-tag-inheritance t)
  12929. (if org-tags-exclude-from-inheritance
  12930. (org-delete-all org-tags-exclude-from-inheritance tags)
  12931. tags))
  12932. ((not org-use-tag-inheritance) nil)
  12933. ((stringp org-use-tag-inheritance)
  12934. (delq nil (mapcar
  12935. (lambda (x)
  12936. (if (and (string-match org-use-tag-inheritance x)
  12937. (not (member x org-tags-exclude-from-inheritance)))
  12938. x nil))
  12939. tags)))
  12940. ((listp org-use-tag-inheritance)
  12941. (delq nil (mapcar
  12942. (lambda (x)
  12943. (if (member x org-use-tag-inheritance) x nil))
  12944. tags)))))
  12945. (defun org-match-sparse-tree (&optional todo-only match)
  12946. "Create a sparse tree according to tags string MATCH.
  12947. MATCH is a string with match syntax. It can contain a selection
  12948. of tags (\"+work+urgent-boss\"), properties (\"LEVEL>3\"), and
  12949. TODO keywords (\"TODO=\\\"WAITING\\\"\") or a combination of
  12950. those. See the manual for details.
  12951. If optional argument TODO-ONLY is non-nil, only select lines that
  12952. are also TODO tasks."
  12953. (interactive "P")
  12954. (org-agenda-prepare-buffers (list (current-buffer)))
  12955. (let ((org--matcher-tags-todo-only todo-only))
  12956. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  12957. org--matcher-tags-todo-only)))
  12958. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12959. (defvar org-cached-props nil)
  12960. (defun org-cached-entry-get (pom property)
  12961. (if (or (eq t org-use-property-inheritance)
  12962. (and (stringp org-use-property-inheritance)
  12963. (let ((case-fold-search t))
  12964. (string-match-p org-use-property-inheritance property)))
  12965. (and (listp org-use-property-inheritance)
  12966. (member-ignore-case property org-use-property-inheritance)))
  12967. ;; Caching is not possible, check it directly.
  12968. (org-entry-get pom property 'inherit)
  12969. ;; Get all properties, so we can do complicated checks easily.
  12970. (cdr (assoc-string property
  12971. (or org-cached-props
  12972. (setq org-cached-props (org-entry-properties pom)))
  12973. t))))
  12974. (defun org-global-tags-completion-table (&optional files)
  12975. "Return the list of all tags in all agenda buffer/files.
  12976. Optional FILES argument is a list of files which can be used
  12977. instead of the agenda files."
  12978. (save-excursion
  12979. (org-uniquify
  12980. (delq nil
  12981. (apply #'append
  12982. (mapcar
  12983. (lambda (file)
  12984. (set-buffer (find-file-noselect file))
  12985. (mapcar (lambda (x)
  12986. (and (stringp (car-safe x))
  12987. (list (car-safe x))))
  12988. (or org-current-tag-alist (org-get-buffer-tags))))
  12989. (if (car-safe files) files
  12990. (org-agenda-files))))))))
  12991. (defun org-make-tags-matcher (match)
  12992. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12993. Returns a cons of the selection string MATCH and a function
  12994. implementing the matcher.
  12995. The matcher is to be called at an Org entry, with point on the
  12996. headline, and returns non-nil if the entry matches the selection
  12997. string MATCH. It must be called with three arguments: the TODO
  12998. keyword at the entry (or nil if none), the list of all tags at
  12999. the entry including inherited ones and the reduced level of the
  13000. headline. Additionally, the category of the entry, if any, must
  13001. be specified as the text property `org-category' on the headline.
  13002. This function sets the variable `org--matcher-tags-todo-only' to
  13003. a non-nil value if the matcher restricts matching to TODO
  13004. entries, otherwise it is not touched.
  13005. See also `org-scan-tags'."
  13006. (unless match
  13007. ;; Get a new match request, with completion against the global
  13008. ;; tags table and the local tags in current buffer.
  13009. (let ((org-last-tags-completion-table
  13010. (org-uniquify
  13011. (delq nil (append (org-get-buffer-tags)
  13012. (org-global-tags-completion-table))))))
  13013. (setq match
  13014. (completing-read
  13015. "Match: "
  13016. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13017. (let ((match0 match)
  13018. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)")
  13019. (start 0)
  13020. tagsmatch todomatch tagsmatcher todomatcher)
  13021. ;; Expand group tags.
  13022. (setq match (org-tags-expand match))
  13023. ;; Check if there is a TODO part of this match, which would be the
  13024. ;; part after a "/". To make sure that this slash is not part of
  13025. ;; a property value to be matched against, we also check that
  13026. ;; there is no / after that slash. First, find the last slash.
  13027. (let ((s 0))
  13028. (while (string-match "/+" match s)
  13029. (setq start (match-beginning 0))
  13030. (setq s (match-end 0))))
  13031. (if (and (string-match "/+" match start)
  13032. (not (string-match-p "\"" match start)))
  13033. ;; Match contains also a TODO-matching request.
  13034. (progn
  13035. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13036. (setq todomatch (substring match (match-end 0)))
  13037. (when (string-prefix-p "!" todomatch)
  13038. (setq org--matcher-tags-todo-only t)
  13039. (setq todomatch (substring todomatch 1)))
  13040. (when (string-match "\\`\\s-*\\'" todomatch)
  13041. (setq todomatch nil)))
  13042. ;; Only matching tags.
  13043. (setq tagsmatch match)
  13044. (setq todomatch nil))
  13045. ;; Make the tags matcher.
  13046. (when (org-string-nw-p tagsmatch)
  13047. (let ((orlist nil)
  13048. (orterms (org-split-string tagsmatch "|"))
  13049. term)
  13050. (while (setq term (pop orterms))
  13051. (while (and (equal (substring term -1) "\\") orterms)
  13052. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13053. (while (string-match re term)
  13054. (let* ((rest (substring term (match-end 0)))
  13055. (minus (and (match-end 1)
  13056. (equal (match-string 1 term) "-")))
  13057. (tag (save-match-data
  13058. (replace-regexp-in-string
  13059. "\\\\-" "-" (match-string 2 term))))
  13060. (regexp (eq (string-to-char tag) ?{))
  13061. (levelp (match-end 4))
  13062. (propp (match-end 5))
  13063. (mm
  13064. (cond
  13065. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13066. (levelp
  13067. `(,(org-op-to-function (match-string 3 term))
  13068. level
  13069. ,(string-to-number (match-string 4 term))))
  13070. (propp
  13071. (let* ((gv (pcase (upcase (match-string 5 term))
  13072. ("CATEGORY"
  13073. '(get-text-property (point) 'org-category))
  13074. ("TODO" 'todo)
  13075. (p `(org-cached-entry-get nil ,p))))
  13076. (pv (match-string 7 term))
  13077. (regexp (eq (string-to-char pv) ?{))
  13078. (strp (eq (string-to-char pv) ?\"))
  13079. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13080. (po (org-op-to-function (match-string 6 term)
  13081. (if timep 'time strp))))
  13082. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13083. (when timep (setq pv (org-matcher-time pv)))
  13084. (cond ((and regexp (eq po 'org<>))
  13085. `(not (string-match ,pv (or ,gv ""))))
  13086. (regexp `(string-match ,pv (or ,gv "")))
  13087. (strp `(,po (or ,gv "") ,pv))
  13088. (t
  13089. `(,po
  13090. (string-to-number (or ,gv ""))
  13091. ,(string-to-number pv))))))
  13092. (t `(member ,tag tags-list)))))
  13093. (push (if minus `(not ,mm) mm) tagsmatcher)
  13094. (setq term rest)))
  13095. (push `(and ,@tagsmatcher) orlist)
  13096. (setq tagsmatcher nil))
  13097. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13098. ;; Make the TODO matcher.
  13099. (when (org-string-nw-p todomatch)
  13100. (let ((orlist nil))
  13101. (dolist (term (org-split-string todomatch "|"))
  13102. (while (string-match re term)
  13103. (let* ((minus (and (match-end 1)
  13104. (equal (match-string 1 term) "-")))
  13105. (kwd (match-string 2 term))
  13106. (regexp (eq (string-to-char kwd) ?{))
  13107. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13108. `(equal todo ,kwd))))
  13109. (push (if minus `(not ,mm) mm) todomatcher))
  13110. (setq term (substring term (match-end 0))))
  13111. (push (if (> (length todomatcher) 1)
  13112. (cons 'and todomatcher)
  13113. (car todomatcher))
  13114. orlist)
  13115. (setq todomatcher nil))
  13116. (setq todomatcher (cons 'or orlist))))
  13117. ;; Return the string and function of the matcher. If no
  13118. ;; tags-specific or todo-specific matcher exists, match
  13119. ;; everything.
  13120. (let ((matcher (if (and tagsmatcher todomatcher)
  13121. `(and ,tagsmatcher ,todomatcher)
  13122. (or tagsmatcher todomatcher t))))
  13123. (when org--matcher-tags-todo-only
  13124. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13125. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13126. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13127. "Expand group tags in MATCH.
  13128. This replaces every group tag in MATCH with a regexp tag search.
  13129. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13130. will be replaced like this:
  13131. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13132. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13133. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13134. Replacing by a regexp preserves the structure of the match.
  13135. E.g., this expansion
  13136. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13137. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13138. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13139. A group tag in MATCH can contain regular expressions of its own.
  13140. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13141. will be replaced like this:
  13142. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13143. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13144. assumed to be a single group tag, and the function will return
  13145. the list of tags in this group.
  13146. When DOWNCASE is non-nil, expand downcased TAGS."
  13147. (if org-group-tags
  13148. (let* ((case-fold-search t)
  13149. (stable org-mode-syntax-table)
  13150. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13151. (taggroups (if downcased
  13152. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13153. taggroups)
  13154. taggroups))
  13155. (taggroups-keys (mapcar #'car taggroups))
  13156. (return-match (if downcased (downcase match) match))
  13157. (count 0)
  13158. (work-already-expanded tags-already-expanded)
  13159. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13160. ;; @ and _ are allowed as word-components in tags.
  13161. (modify-syntax-entry ?@ "w" stable)
  13162. (modify-syntax-entry ?_ "w" stable)
  13163. ;; Temporarily replace regexp-expressions in the match-expression.
  13164. (while (string-match "{.+?}" return-match)
  13165. (cl-incf count)
  13166. (push (match-string 0 return-match) regexps-in-match)
  13167. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13168. (while (and taggroups-keys
  13169. (with-syntax-table stable
  13170. (string-match
  13171. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13172. (regexp-opt taggroups-keys) "\\>\\)")
  13173. return-match)))
  13174. (let* ((dir (match-string 1 return-match))
  13175. (tag (match-string 2 return-match))
  13176. (tag (if downcased (downcase tag) tag)))
  13177. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13178. (member tag work-already-expanded))
  13179. (setq tags-in-group (assoc tag taggroups))
  13180. (push tag work-already-expanded)
  13181. ;; Recursively expand each tag in the group, if the tag hasn't
  13182. ;; already been expanded. Restore the match-data after all recursive calls.
  13183. (save-match-data
  13184. (let (tags-expanded)
  13185. (dolist (x (cdr tags-in-group))
  13186. (if (and (member x taggroups-keys)
  13187. (not (member x work-already-expanded)))
  13188. (setq tags-expanded
  13189. (delete-dups
  13190. (append
  13191. (org-tags-expand x t downcased
  13192. work-already-expanded)
  13193. tags-expanded)))
  13194. (setq tags-expanded
  13195. (append (list x) tags-expanded)))
  13196. (setq work-already-expanded
  13197. (delete-dups
  13198. (append tags-expanded
  13199. work-already-expanded))))
  13200. (setq tags-in-group
  13201. (delete-dups (cons (car tags-in-group)
  13202. tags-expanded)))))
  13203. ;; Filter tag-regexps from tags.
  13204. (setq regexp-in-group-escaped
  13205. (delq nil (mapcar (lambda (x)
  13206. (if (stringp x)
  13207. (and (equal "{" (substring x 0 1))
  13208. (equal "}" (substring x -1))
  13209. x)
  13210. x))
  13211. tags-in-group))
  13212. regexp-in-group
  13213. (mapcar (lambda (x)
  13214. (substring x 1 -1))
  13215. regexp-in-group-escaped)
  13216. tags-in-group
  13217. (delq nil (mapcar (lambda (x)
  13218. (if (stringp x)
  13219. (and (not (equal "{" (substring x 0 1)))
  13220. (not (equal "}" (substring x -1)))
  13221. x)
  13222. x))
  13223. tags-in-group)))
  13224. ;; If single-as-list, do no more in the while-loop.
  13225. (if (not single-as-list)
  13226. (progn
  13227. (when regexp-in-group
  13228. (setq regexp-in-group
  13229. (concat "\\|"
  13230. (mapconcat 'identity regexp-in-group
  13231. "\\|"))))
  13232. (setq tags-in-group
  13233. (concat dir
  13234. "{\\<"
  13235. (regexp-opt tags-in-group)
  13236. "\\>"
  13237. regexp-in-group
  13238. "}"))
  13239. (when (stringp tags-in-group)
  13240. (org-add-props tags-in-group '(grouptag t)))
  13241. (setq return-match
  13242. (replace-match tags-in-group t t return-match)))
  13243. (setq tags-in-group
  13244. (append regexp-in-group-escaped tags-in-group))))
  13245. (setq taggroups-keys (delete tag taggroups-keys))))
  13246. ;; Add the regular expressions back into the match-expression again.
  13247. (while regexps-in-match
  13248. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13249. (pop regexps-in-match)
  13250. return-match t t))
  13251. (cl-decf count))
  13252. (if single-as-list
  13253. (if tags-in-group tags-in-group (list return-match))
  13254. return-match))
  13255. (if single-as-list
  13256. (list (if downcased (downcase match) match))
  13257. match)))
  13258. (defun org-op-to-function (op &optional stringp)
  13259. "Turn an operator into the appropriate function."
  13260. (setq op
  13261. (cond
  13262. ((equal op "<" ) '(< string< org-time<))
  13263. ((equal op ">" ) '(> org-string> org-time>))
  13264. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13265. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13266. ((member op '("=" "==")) '(= string= org-time=))
  13267. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13268. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13269. (defun org<> (a b) (not (= a b)))
  13270. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13271. (defun org-string>= (a b) (not (string< a b)))
  13272. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13273. (defun org-string<> (a b) (not (string= a b)))
  13274. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13275. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13276. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13277. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13278. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13279. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13280. (defun org-2ft (s)
  13281. "Convert S to a floating point time.
  13282. If S is already a number, just return it. If it is a string, parse
  13283. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13284. (cond
  13285. ((numberp s) s)
  13286. ((stringp s)
  13287. (condition-case nil
  13288. (float-time (apply #'encode-time (org-parse-time-string s)))
  13289. (error 0.)))
  13290. (t 0.)))
  13291. (defun org-time-today ()
  13292. "Time in seconds today at 0:00.
  13293. Returns the float number of seconds since the beginning of the
  13294. epoch to the beginning of today (00:00)."
  13295. (float-time (apply 'encode-time
  13296. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13297. (defun org-matcher-time (s)
  13298. "Interpret a time comparison value."
  13299. (save-match-data
  13300. (cond
  13301. ((string= s "<now>") (float-time))
  13302. ((string= s "<today>") (org-time-today))
  13303. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13304. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13305. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13306. (+ (org-time-today)
  13307. (* (string-to-number (match-string 1 s))
  13308. (cdr (assoc (match-string 2 s)
  13309. '(("d" . 86400.0) ("w" . 604800.0)
  13310. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13311. (t (org-2ft s)))))
  13312. (defun org-match-any-p (re list)
  13313. "Does re match any element of list?"
  13314. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13315. (delq nil list))
  13316. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13317. (defvar org-tags-overlay (make-overlay 1 1))
  13318. (delete-overlay org-tags-overlay)
  13319. (defun org-get-local-tags-at (&optional pos)
  13320. "Get a list of tags defined in the current headline."
  13321. (org-get-tags-at pos 'local))
  13322. (defun org-get-local-tags ()
  13323. "Get a list of tags defined in the current headline."
  13324. (org-get-tags-at nil 'local))
  13325. (defun org-get-tags-at (&optional pos local)
  13326. "Get a list of all headline tags applicable at POS.
  13327. POS defaults to point. If tags are inherited, the list contains
  13328. the targets in the same sequence as the headlines appear, i.e.
  13329. the tags of the current headline come last.
  13330. When LOCAL is non-nil, only return tags from the current headline,
  13331. ignore inherited ones."
  13332. (interactive)
  13333. (if (and org-trust-scanner-tags
  13334. (or (not pos) (equal pos (point)))
  13335. (not local))
  13336. org-scanner-tags
  13337. (let (tags ltags lastpos parent)
  13338. (save-excursion
  13339. (save-restriction
  13340. (widen)
  13341. (goto-char (or pos (point)))
  13342. (save-match-data
  13343. (catch 'done
  13344. (condition-case nil
  13345. (progn
  13346. (org-back-to-heading t)
  13347. (while (not (equal lastpos (point)))
  13348. (setq lastpos (point))
  13349. (when (looking-at ".+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13350. (setq ltags (org-split-string
  13351. (match-string-no-properties 1) ":"))
  13352. (when parent
  13353. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13354. (setq tags (append
  13355. (if parent
  13356. (org-remove-uninherited-tags ltags)
  13357. ltags)
  13358. tags)))
  13359. (or org-use-tag-inheritance (throw 'done t))
  13360. (when local (throw 'done t))
  13361. (or (org-up-heading-safe) (error nil))
  13362. (setq parent t)))
  13363. (error nil)))))
  13364. (if local
  13365. tags
  13366. (reverse (delete-dups
  13367. (reverse (append
  13368. (org-remove-uninherited-tags
  13369. org-file-tags)
  13370. tags)))))))))
  13371. (defun org-add-prop-inherited (s)
  13372. (add-text-properties 0 (length s) '(inherited t) s)
  13373. s)
  13374. (defun org-toggle-tag (tag &optional onoff)
  13375. "Toggle the tag TAG for the current line.
  13376. If ONOFF is `on' or `off', don't toggle but set to this state."
  13377. (save-excursion
  13378. (org-back-to-heading t)
  13379. (let ((current
  13380. (when (re-search-forward "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$"
  13381. (line-end-position) t)
  13382. (let ((tags (match-string 1)))
  13383. ;; Clear current tags.
  13384. (replace-match "")
  13385. ;; Reverse the tags list so any new tag is appended to
  13386. ;; the current list of tags.
  13387. (nreverse (org-split-string tags ":")))))
  13388. res)
  13389. (pcase onoff
  13390. (`off (setq current (delete tag current)))
  13391. ((or `on (guard (not (member tag current))))
  13392. (setq res t)
  13393. (cl-pushnew tag current :test #'equal))
  13394. (_ (setq current (delete tag current))))
  13395. (end-of-line)
  13396. (if current
  13397. (progn
  13398. (insert " :" (mapconcat #'identity (nreverse current) ":") ":")
  13399. (org-set-tags nil t))
  13400. (delete-horizontal-space))
  13401. (run-hooks 'org-after-tags-change-hook)
  13402. res)))
  13403. (defun org--align-tags-here (to-col)
  13404. "Align tags on the current headline to TO-COL.
  13405. Assume point is on a headline."
  13406. (let ((pos (point)))
  13407. (beginning-of-line)
  13408. (if (or (not (looking-at ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13409. (>= pos (match-beginning 2)))
  13410. ;; No tags or point within tags: do not align.
  13411. (goto-char pos)
  13412. (goto-char (match-beginning 1))
  13413. (let ((shift (max (- (if (>= to-col 0) to-col
  13414. (- (abs to-col) (string-width (match-string 2))))
  13415. (current-column))
  13416. 1)))
  13417. (replace-match (make-string shift ?\s) nil nil nil 1)
  13418. ;; Preserve initial position, if possible. In any case, stop
  13419. ;; before tags.
  13420. (when (< pos (point)) (goto-char pos))))))
  13421. (defun org-set-tags-command (&optional arg just-align)
  13422. "Call the set-tags command for the current entry."
  13423. (interactive "P")
  13424. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13425. (org-set-tags arg just-align)
  13426. (save-excursion
  13427. (unless (and (org-region-active-p)
  13428. org-loop-over-headlines-in-active-region)
  13429. (org-back-to-heading t))
  13430. (org-set-tags arg just-align))))
  13431. (defun org-set-tags-to (data)
  13432. "Set the tags of the current entry to DATA, replacing the current tags.
  13433. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13434. If DATA is nil or the empty string, any tags will be removed."
  13435. (interactive "sTags: ")
  13436. (setq data
  13437. (cond
  13438. ((eq data nil) "")
  13439. ((equal data "") "")
  13440. ((stringp data)
  13441. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13442. ":"))
  13443. ((listp data)
  13444. (concat ":" (mapconcat 'identity data ":") ":"))))
  13445. (when data
  13446. (save-excursion
  13447. (org-back-to-heading t)
  13448. (when (let ((case-fold-search nil))
  13449. (looking-at org-complex-heading-regexp))
  13450. (if (match-end 5)
  13451. (progn
  13452. (goto-char (match-beginning 5))
  13453. (insert data)
  13454. (delete-region (point) (point-at-eol))
  13455. (org-set-tags nil 'align))
  13456. (goto-char (point-at-eol))
  13457. (insert " " data)
  13458. (org-set-tags nil 'align)))
  13459. (beginning-of-line 1)
  13460. (when (looking-at ".*?\\([ \t]+\\)$")
  13461. (delete-region (match-beginning 1) (match-end 1))))))
  13462. (defun org-align-all-tags ()
  13463. "Align the tags in all headings."
  13464. (interactive)
  13465. (save-excursion
  13466. (or (ignore-errors (org-back-to-heading t))
  13467. (outline-next-heading))
  13468. (if (org-at-heading-p)
  13469. (org-set-tags t)
  13470. (message "No headings"))))
  13471. (defvar org-indent-indentation-per-level)
  13472. (defun org-set-tags (&optional arg just-align)
  13473. "Set the tags for the current headline.
  13474. With prefix ARG, realign all tags in headings in the current buffer.
  13475. When JUST-ALIGN is non-nil, only align tags."
  13476. (interactive "P")
  13477. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13478. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13479. 'region-start-level
  13480. 'region))
  13481. org-loop-over-headlines-in-active-region)
  13482. (org-map-entries
  13483. ;; We don't use ARG and JUST-ALIGN here because these args
  13484. ;; are not useful when looping over headlines.
  13485. #'org-set-tags
  13486. org-loop-over-headlines-in-active-region
  13487. cl
  13488. '(when (org-invisible-p) (org-end-of-subtree nil t))))
  13489. (let ((org-setting-tags t))
  13490. (if arg
  13491. (save-excursion
  13492. (goto-char (point-min))
  13493. (while (re-search-forward org-outline-regexp-bol nil t)
  13494. (org-set-tags nil t)
  13495. (end-of-line))
  13496. (message "All tags realigned to column %d" org-tags-column))
  13497. (let* ((current (org-get-tags-string))
  13498. (tags
  13499. (if just-align current
  13500. ;; Get a new set of tags from the user.
  13501. (save-excursion
  13502. (let* ((seen)
  13503. (table
  13504. (setq
  13505. org-last-tags-completion-table
  13506. ;; Uniquify tags in alists, yet preserve
  13507. ;; structure (i.e., keywords).
  13508. (delq nil
  13509. (mapcar
  13510. (lambda (pair)
  13511. (let ((head (car pair)))
  13512. (cond ((symbolp head) pair)
  13513. ((member head seen) nil)
  13514. (t (push head seen)
  13515. pair))))
  13516. (append
  13517. (or org-current-tag-alist
  13518. (org-get-buffer-tags))
  13519. (and
  13520. org-complete-tags-always-offer-all-agenda-tags
  13521. (org-global-tags-completion-table
  13522. (org-agenda-files))))))))
  13523. (current-tags (org-split-string current ":"))
  13524. (inherited-tags
  13525. (nreverse (nthcdr (length current-tags)
  13526. (nreverse (org-get-tags-at))))))
  13527. (replace-regexp-in-string
  13528. "\\([-+&]+\\|,\\)"
  13529. ":"
  13530. (if (or (eq t org-use-fast-tag-selection)
  13531. (and org-use-fast-tag-selection
  13532. (delq nil (mapcar #'cdr table))))
  13533. (org-fast-tag-selection
  13534. current-tags inherited-tags table
  13535. (and org-fast-tag-selection-include-todo
  13536. org-todo-key-alist))
  13537. (let ((org-add-colon-after-tag-completion
  13538. (< 1 (length table))))
  13539. (org-trim
  13540. (completing-read
  13541. "Tags: "
  13542. #'org-tags-completion-function
  13543. nil nil current 'org-tags-history))))))))))
  13544. (when org-tags-sort-function
  13545. (setq tags
  13546. (mapconcat
  13547. #'identity
  13548. (sort (org-split-string tags "[^[:alnum:]_@#%]+")
  13549. org-tags-sort-function)
  13550. ":")))
  13551. (if (or (string= ":" tags)
  13552. (string= "::" tags))
  13553. (setq tags ""))
  13554. (if (not (org-string-nw-p tags)) (setq tags "")
  13555. (unless (string-suffix-p ":" tags) (setq tags (concat tags ":")))
  13556. (unless (string-prefix-p ":" tags) (setq tags (concat ":" tags))))
  13557. ;; Insert new tags at the correct column.
  13558. (unless (equal current tags)
  13559. (save-excursion
  13560. (beginning-of-line)
  13561. (let ((case-fold-search nil))
  13562. (looking-at org-complex-heading-regexp))
  13563. ;; Remove current tags, if any.
  13564. (when (match-end 5) (replace-match "" nil nil nil 5))
  13565. ;; Insert new tags, if any. Otherwise, remove trailing
  13566. ;; white spaces.
  13567. (end-of-line)
  13568. (if (not (equal tags ""))
  13569. ;; When text is being inserted on an invisible
  13570. ;; region boundary, it can be inadvertently sucked
  13571. ;; into invisibility.
  13572. (outline-flag-region (point) (progn (insert " " tags) (point)) nil)
  13573. (skip-chars-backward " \t")
  13574. (delete-region (point) (line-end-position)))))
  13575. ;; Align tags, if any. Fix tags column if `org-indent-mode'
  13576. ;; is on.
  13577. (unless (equal tags "")
  13578. (let* ((level (save-excursion
  13579. (beginning-of-line)
  13580. (skip-chars-forward "\\*")))
  13581. (offset (if (bound-and-true-p org-indent-mode)
  13582. (* (1- org-indent-indentation-per-level)
  13583. (1- level))
  13584. 0))
  13585. (tags-column
  13586. (+ org-tags-column
  13587. (if (> org-tags-column 0) (- offset) offset))))
  13588. (org--align-tags-here tags-column))))
  13589. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13590. (defun org-change-tag-in-region (beg end tag off)
  13591. "Add or remove TAG for each entry in the region.
  13592. This works in the agenda, and also in an Org buffer."
  13593. (interactive
  13594. (list (region-beginning) (region-end)
  13595. (let ((org-last-tags-completion-table
  13596. (if (derived-mode-p 'org-mode)
  13597. (org-uniquify
  13598. (delq nil (append (org-get-buffer-tags)
  13599. (org-global-tags-completion-table))))
  13600. (org-global-tags-completion-table))))
  13601. (completing-read
  13602. "Tag: " 'org-tags-completion-function nil nil nil
  13603. 'org-tags-history))
  13604. (progn
  13605. (message "[s]et or [r]emove? ")
  13606. (equal (read-char-exclusive) ?r))))
  13607. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13608. (let ((agendap (equal major-mode 'org-agenda-mode))
  13609. l1 l2 m buf pos newhead (cnt 0))
  13610. (goto-char end)
  13611. (setq l2 (1- (org-current-line)))
  13612. (goto-char beg)
  13613. (setq l1 (org-current-line))
  13614. (cl-loop for l from l1 to l2 do
  13615. (org-goto-line l)
  13616. (setq m (get-text-property (point) 'org-hd-marker))
  13617. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13618. (and agendap m))
  13619. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13620. pos (if agendap m (point)))
  13621. (with-current-buffer buf
  13622. (save-excursion
  13623. (save-restriction
  13624. (goto-char pos)
  13625. (setq cnt (1+ cnt))
  13626. (org-toggle-tag tag (if off 'off 'on))
  13627. (setq newhead (org-get-heading)))))
  13628. (and agendap (org-agenda-change-all-lines newhead m))))
  13629. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13630. (defun org-tags-completion-function (string _predicate &optional flag)
  13631. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13632. (confirm (lambda (x) (stringp (car x)))))
  13633. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13634. (setq s1 (match-string 1 string)
  13635. s2 (match-string 2 string))
  13636. (setq s1 "" s2 string))
  13637. (cond
  13638. ((eq flag nil)
  13639. ;; try completion
  13640. (setq rtn (try-completion s2 ctable confirm))
  13641. (when (stringp rtn)
  13642. (setq rtn
  13643. (concat s1 s2 (substring rtn (length s2))
  13644. (if (and org-add-colon-after-tag-completion
  13645. (assoc rtn ctable))
  13646. ":" ""))))
  13647. rtn)
  13648. ((eq flag t)
  13649. ;; all-completions
  13650. (all-completions s2 ctable confirm))
  13651. ((eq flag 'lambda)
  13652. ;; exact match?
  13653. (assoc s2 ctable)))))
  13654. (defun org-fast-tag-insert (kwd tags face &optional end)
  13655. "Insert KDW, and the TAGS, the latter with face FACE.
  13656. Also insert END."
  13657. (insert (format "%-12s" (concat kwd ":"))
  13658. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13659. (or end "")))
  13660. (defun org-fast-tag-show-exit (flag)
  13661. (save-excursion
  13662. (org-goto-line 3)
  13663. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13664. (replace-match ""))
  13665. (when flag
  13666. (end-of-line 1)
  13667. (org-move-to-column (- (window-width) 19) t)
  13668. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13669. (defun org-set-current-tags-overlay (current prefix)
  13670. "Add an overlay to CURRENT tag with PREFIX."
  13671. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13672. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13673. (org-overlay-display org-tags-overlay (concat prefix s))))
  13674. (defvar org-last-tag-selection-key nil)
  13675. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13676. "Fast tag selection with single keys.
  13677. CURRENT is the current list of tags in the headline, INHERITED is the
  13678. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13679. possibly with grouping information. TODO-TABLE is a similar table with
  13680. TODO keywords, should these have keys assigned to them.
  13681. If the keys are nil, a-z are automatically assigned.
  13682. Returns the new tags string, or nil to not change the current settings."
  13683. (let* ((fulltable (append table todo-table))
  13684. (maxlen (apply 'max (mapcar
  13685. (lambda (x)
  13686. (if (stringp (car x)) (string-width (car x)) 0))
  13687. fulltable)))
  13688. (buf (current-buffer))
  13689. (expert (eq org-fast-tag-selection-single-key 'expert))
  13690. (buffer-tags nil)
  13691. (fwidth (+ maxlen 3 1 3))
  13692. (ncol (/ (- (window-width) 4) fwidth))
  13693. (i-face 'org-done)
  13694. (c-face 'org-todo)
  13695. tg cnt e c char c1 c2 ntable tbl rtn
  13696. ov-start ov-end ov-prefix
  13697. (exit-after-next org-fast-tag-selection-single-key)
  13698. (done-keywords org-done-keywords)
  13699. groups ingroup intaggroup)
  13700. (save-excursion
  13701. (beginning-of-line 1)
  13702. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13703. (setq ov-start (match-beginning 1)
  13704. ov-end (match-end 1)
  13705. ov-prefix "")
  13706. (setq ov-start (1- (point-at-eol))
  13707. ov-end (1+ ov-start))
  13708. (skip-chars-forward "^\n\r")
  13709. (setq ov-prefix
  13710. (concat
  13711. (buffer-substring (1- (point)) (point))
  13712. (if (> (current-column) org-tags-column)
  13713. " "
  13714. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13715. (move-overlay org-tags-overlay ov-start ov-end)
  13716. (save-window-excursion
  13717. (if expert
  13718. (set-buffer (get-buffer-create " *Org tags*"))
  13719. (delete-other-windows)
  13720. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13721. (org-switch-to-buffer-other-window " *Org tags*"))
  13722. (erase-buffer)
  13723. (setq-local org-done-keywords done-keywords)
  13724. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13725. (org-fast-tag-insert "Current" current c-face "\n\n")
  13726. (org-fast-tag-show-exit exit-after-next)
  13727. (org-set-current-tags-overlay current ov-prefix)
  13728. (setq tbl fulltable char ?a cnt 0)
  13729. (while (setq e (pop tbl))
  13730. (cond
  13731. ((eq (car e) :startgroup)
  13732. (push '() groups) (setq ingroup t)
  13733. (unless (zerop cnt)
  13734. (setq cnt 0)
  13735. (insert "\n"))
  13736. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13737. ((eq (car e) :endgroup)
  13738. (setq ingroup nil cnt 0)
  13739. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13740. ((eq (car e) :startgrouptag)
  13741. (setq intaggroup t)
  13742. (unless (zerop cnt)
  13743. (setq cnt 0)
  13744. (insert "\n"))
  13745. (insert "[ "))
  13746. ((eq (car e) :endgrouptag)
  13747. (setq intaggroup nil cnt 0)
  13748. (insert "]\n"))
  13749. ((equal e '(:newline))
  13750. (unless (zerop cnt)
  13751. (setq cnt 0)
  13752. (insert "\n")
  13753. (setq e (car tbl))
  13754. (while (equal (car tbl) '(:newline))
  13755. (insert "\n")
  13756. (setq tbl (cdr tbl)))))
  13757. ((equal e '(:grouptags)) (insert " : "))
  13758. (t
  13759. (setq tg (copy-sequence (car e)) c2 nil)
  13760. (if (cdr e)
  13761. (setq c (cdr e))
  13762. ;; automatically assign a character.
  13763. (setq c1 (string-to-char
  13764. (downcase (substring
  13765. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13766. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13767. (while (or (rassoc char ntable) (rassoc char table))
  13768. (setq char (1+ char)))
  13769. (setq c2 c1))
  13770. (setq c (or c2 char)))
  13771. (when ingroup (push tg (car groups)))
  13772. (setq tg (org-add-props tg nil 'face
  13773. (cond
  13774. ((not (assoc tg table))
  13775. (org-get-todo-face tg))
  13776. ((member tg current) c-face)
  13777. ((member tg inherited) i-face))))
  13778. (when (equal (caar tbl) :grouptags)
  13779. (org-add-props tg nil 'face 'org-tag-group))
  13780. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13781. (insert "[" c "] " tg (make-string
  13782. (- fwidth 4 (length tg)) ?\ ))
  13783. (push (cons tg c) ntable)
  13784. (when (= (cl-incf cnt) ncol)
  13785. (insert "\n")
  13786. (when (or ingroup intaggroup) (insert " "))
  13787. (setq cnt 0)))))
  13788. (setq ntable (nreverse ntable))
  13789. (insert "\n")
  13790. (goto-char (point-min))
  13791. (unless expert (org-fit-window-to-buffer))
  13792. (setq rtn
  13793. (catch 'exit
  13794. (while t
  13795. (message "[a-z..]:toggle [SPC]:clear [RET]:accept [TAB]:edit [!] %sgroups%s"
  13796. (if (not groups) "no " "")
  13797. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13798. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13799. (setq org-last-tag-selection-key c)
  13800. (cond
  13801. ((= c ?\r) (throw 'exit t))
  13802. ((= c ?!)
  13803. (setq groups (not groups))
  13804. (goto-char (point-min))
  13805. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13806. ((= c ?\C-c)
  13807. (if (not expert)
  13808. (org-fast-tag-show-exit
  13809. (setq exit-after-next (not exit-after-next)))
  13810. (setq expert nil)
  13811. (delete-other-windows)
  13812. (set-window-buffer (split-window-vertically) " *Org tags*")
  13813. (org-switch-to-buffer-other-window " *Org tags*")
  13814. (org-fit-window-to-buffer)))
  13815. ((or (= c ?\C-g)
  13816. (and (= c ?q) (not (rassoc c ntable))))
  13817. (delete-overlay org-tags-overlay)
  13818. (setq quit-flag t))
  13819. ((= c ?\ )
  13820. (setq current nil)
  13821. (when exit-after-next (setq exit-after-next 'now)))
  13822. ((= c ?\t)
  13823. (condition-case nil
  13824. (setq tg (completing-read
  13825. "Tag: "
  13826. (or buffer-tags
  13827. (with-current-buffer buf
  13828. (setq buffer-tags
  13829. (org-get-buffer-tags))))))
  13830. (quit (setq tg "")))
  13831. (when (string-match "\\S-" tg)
  13832. (cl-pushnew (list tg) buffer-tags :test #'equal)
  13833. (if (member tg current)
  13834. (setq current (delete tg current))
  13835. (push tg current)))
  13836. (when exit-after-next (setq exit-after-next 'now)))
  13837. ((setq e (rassoc c todo-table) tg (car e))
  13838. (with-current-buffer buf
  13839. (save-excursion (org-todo tg)))
  13840. (when exit-after-next (setq exit-after-next 'now)))
  13841. ((setq e (rassoc c ntable) tg (car e))
  13842. (if (member tg current)
  13843. (setq current (delete tg current))
  13844. (cl-loop for g in groups do
  13845. (when (member tg g)
  13846. (dolist (x g) (setq current (delete x current)))))
  13847. (push tg current))
  13848. (when exit-after-next (setq exit-after-next 'now))))
  13849. ;; Create a sorted list
  13850. (setq current
  13851. (sort current
  13852. (lambda (a b)
  13853. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13854. (when (eq exit-after-next 'now) (throw 'exit t))
  13855. (goto-char (point-min))
  13856. (beginning-of-line 2)
  13857. (delete-region (point) (point-at-eol))
  13858. (org-fast-tag-insert "Current" current c-face)
  13859. (org-set-current-tags-overlay current ov-prefix)
  13860. (while (re-search-forward "\\[.\\] \\([[:alnum:]_@#%]+\\)" nil t)
  13861. (setq tg (match-string 1))
  13862. (add-text-properties
  13863. (match-beginning 1) (match-end 1)
  13864. (list 'face
  13865. (cond
  13866. ((member tg current) c-face)
  13867. ((member tg inherited) i-face)
  13868. (t (get-text-property (match-beginning 1) 'face))))))
  13869. (goto-char (point-min)))))
  13870. (delete-overlay org-tags-overlay)
  13871. (if rtn
  13872. (mapconcat 'identity current ":")
  13873. nil))))
  13874. (defun org-get-tags-string ()
  13875. "Get the TAGS string in the current headline."
  13876. (unless (org-at-heading-p t)
  13877. (user-error "Not on a heading"))
  13878. (save-excursion
  13879. (beginning-of-line 1)
  13880. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13881. (match-string-no-properties 1)
  13882. "")))
  13883. (defun org-get-tags ()
  13884. "Get the list of tags specified in the current headline."
  13885. (org-split-string (org-get-tags-string) ":"))
  13886. (defun org-get-buffer-tags ()
  13887. "Get a table of all tags used in the buffer, for completion."
  13888. (org-with-wide-buffer
  13889. (goto-char (point-min))
  13890. (let ((tag-re (concat org-outline-regexp-bol
  13891. "\\(?:.*?[ \t]\\)?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13892. tags)
  13893. (while (re-search-forward tag-re nil t)
  13894. (dolist (tag (org-split-string (match-string-no-properties 1) ":"))
  13895. (push tag tags)))
  13896. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13897. ;;;; The mapping API
  13898. (defvar org-agenda-skip-comment-trees)
  13899. (defvar org-agenda-skip-function)
  13900. (defun org-map-entries (func &optional match scope &rest skip)
  13901. "Call FUNC at each headline selected by MATCH in SCOPE.
  13902. FUNC is a function or a lisp form. The function will be called without
  13903. arguments, with the cursor positioned at the beginning of the headline.
  13904. The return values of all calls to the function will be collected and
  13905. returned as a list.
  13906. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13907. does not need to preserve point. After evaluation, the cursor will be
  13908. moved to the end of the line (presumably of the headline of the
  13909. processed entry) and search continues from there. Under some
  13910. circumstances, this may not produce the wanted results. For example,
  13911. if you have removed (e.g. archived) the current (sub)tree it could
  13912. mean that the next entry will be skipped entirely. In such cases, you
  13913. can specify the position from where search should continue by making
  13914. FUNC set the variable `org-map-continue-from' to the desired buffer
  13915. position.
  13916. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13917. Only headlines that are matched by this query will be considered during
  13918. the iteration. When MATCH is nil or t, all headlines will be
  13919. visited by the iteration.
  13920. SCOPE determines the scope of this command. It can be any of:
  13921. nil The current buffer, respecting the restriction if any
  13922. tree The subtree started with the entry at point
  13923. region The entries within the active region, if any
  13924. region-start-level
  13925. The entries within the active region, but only those at
  13926. the same level than the first one.
  13927. file The current buffer, without restriction
  13928. file-with-archives
  13929. The current buffer, and any archives associated with it
  13930. agenda All agenda files
  13931. agenda-with-archives
  13932. All agenda files with any archive files associated with them
  13933. \(file1 file2 ...)
  13934. If this is a list, all files in the list will be scanned
  13935. The remaining args are treated as settings for the skipping facilities of
  13936. the scanner. The following items can be given here:
  13937. archive skip trees with the archive tag
  13938. comment skip trees with the COMMENT keyword
  13939. function or Emacs Lisp form:
  13940. will be used as value for `org-agenda-skip-function', so
  13941. whenever the function returns a position, FUNC will not be
  13942. called for that entry and search will continue from the
  13943. position returned
  13944. If your function needs to retrieve the tags including inherited tags
  13945. at the *current* entry, you can use the value of the variable
  13946. `org-scanner-tags' which will be much faster than getting the value
  13947. with `org-get-tags-at'. If your function gets properties with
  13948. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13949. to t around the call to `org-entry-properties' to get the same speedup.
  13950. Note that if your function moves around to retrieve tags and properties at
  13951. a *different* entry, you cannot use these techniques."
  13952. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13953. (not (org-region-active-p)))
  13954. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13955. (org-agenda-skip-archived-trees (memq 'archive skip))
  13956. (org-agenda-skip-comment-trees (memq 'comment skip))
  13957. (org-agenda-skip-function
  13958. (car (org-delete-all '(comment archive) skip)))
  13959. (org-tags-match-list-sublevels t)
  13960. (start-level (eq scope 'region-start-level))
  13961. matcher res
  13962. org-todo-keywords-for-agenda
  13963. org-done-keywords-for-agenda
  13964. org-todo-keyword-alist-for-agenda
  13965. org-tag-alist-for-agenda
  13966. org--matcher-tags-todo-only)
  13967. (cond
  13968. ((eq match t) (setq matcher t))
  13969. ((eq match nil) (setq matcher t))
  13970. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13971. (save-excursion
  13972. (save-restriction
  13973. (cond ((eq scope 'tree)
  13974. (org-back-to-heading t)
  13975. (org-narrow-to-subtree)
  13976. (setq scope nil))
  13977. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13978. (org-region-active-p))
  13979. ;; If needed, set start-level to a string like "2"
  13980. (when start-level
  13981. (save-excursion
  13982. (goto-char (region-beginning))
  13983. (unless (org-at-heading-p) (outline-next-heading))
  13984. (setq start-level (org-current-level))))
  13985. (narrow-to-region (region-beginning)
  13986. (save-excursion
  13987. (goto-char (region-end))
  13988. (unless (and (bolp) (org-at-heading-p))
  13989. (outline-next-heading))
  13990. (point)))
  13991. (setq scope nil)))
  13992. (if (not scope)
  13993. (progn
  13994. (org-agenda-prepare-buffers
  13995. (and buffer-file-name (list buffer-file-name)))
  13996. (setq res
  13997. (org-scan-tags
  13998. func matcher org--matcher-tags-todo-only start-level)))
  13999. ;; Get the right scope
  14000. (cond
  14001. ((and scope (listp scope) (symbolp (car scope)))
  14002. (setq scope (eval scope)))
  14003. ((eq scope 'agenda)
  14004. (setq scope (org-agenda-files t)))
  14005. ((eq scope 'agenda-with-archives)
  14006. (setq scope (org-agenda-files t))
  14007. (setq scope (org-add-archive-files scope)))
  14008. ((eq scope 'file)
  14009. (setq scope (and buffer-file-name (list buffer-file-name))))
  14010. ((eq scope 'file-with-archives)
  14011. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14012. (org-agenda-prepare-buffers scope)
  14013. (dolist (file scope)
  14014. (with-current-buffer (org-find-base-buffer-visiting file)
  14015. (org-with-wide-buffer
  14016. (goto-char (point-min))
  14017. (setq res
  14018. (append
  14019. res
  14020. (org-scan-tags
  14021. func matcher org--matcher-tags-todo-only)))))))))
  14022. res)))
  14023. ;;; Properties API
  14024. (defconst org-special-properties
  14025. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14026. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14027. "The special properties valid in Org mode.
  14028. These are properties that are not defined in the property drawer,
  14029. but in some other way.")
  14030. (defconst org-default-properties
  14031. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14032. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14033. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14034. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14035. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14036. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14037. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14038. "Some properties that are used by Org mode for various purposes.
  14039. Being in this list makes sure that they are offered for completion.")
  14040. (defun org--valid-property-p (property)
  14041. "Non nil when string PROPERTY is a valid property name."
  14042. (not
  14043. (or (equal property "")
  14044. (string-match-p "\\s-" property))))
  14045. (defun org--update-property-plist (key val props)
  14046. "Associate KEY to VAL in alist PROPS.
  14047. Modifications are made by side-effect. Return new alist."
  14048. (let* ((appending (string= (substring key -1) "+"))
  14049. (key (if appending (substring key 0 -1) key))
  14050. (old (assoc-string key props t)))
  14051. (if (not old) (cons (cons key val) props)
  14052. (setcdr old (if appending (concat (cdr old) " " val) val))
  14053. props)))
  14054. (defun org-get-property-block (&optional beg force)
  14055. "Return the (beg . end) range of the body of the property drawer.
  14056. BEG is the beginning of the current subtree, or of the part
  14057. before the first headline. If it is not given, it will be found.
  14058. If the drawer does not exist, create it if FORCE is non-nil, or
  14059. return nil."
  14060. (org-with-wide-buffer
  14061. (when beg (goto-char beg))
  14062. (unless (org-before-first-heading-p)
  14063. (let ((beg (cond (beg)
  14064. ((or (not (featurep 'org-inlinetask))
  14065. (org-inlinetask-in-task-p))
  14066. (org-back-to-heading t))
  14067. (t (org-with-limited-levels (org-back-to-heading t))))))
  14068. (forward-line)
  14069. (when (looking-at-p org-planning-line-re) (forward-line))
  14070. (cond ((looking-at org-property-drawer-re)
  14071. (forward-line)
  14072. (cons (point) (progn (goto-char (match-end 0))
  14073. (line-beginning-position))))
  14074. (force
  14075. (goto-char beg)
  14076. (org-insert-property-drawer)
  14077. (let ((pos (save-excursion (search-forward ":END:")
  14078. (line-beginning-position))))
  14079. (cons pos pos))))))))
  14080. (defun org-at-property-p ()
  14081. "Non-nil when point is inside a property drawer.
  14082. See `org-property-re' for match data, if applicable."
  14083. (save-excursion
  14084. (beginning-of-line)
  14085. (and (looking-at org-property-re)
  14086. (let ((property-drawer (save-match-data (org-get-property-block))))
  14087. (and property-drawer
  14088. (>= (point) (car property-drawer))
  14089. (< (point) (cdr property-drawer)))))))
  14090. (defun org-property-action ()
  14091. "Do an action on properties."
  14092. (interactive)
  14093. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14094. (let ((c (read-char-exclusive)))
  14095. (cl-case c
  14096. (?s (call-interactively #'org-set-property))
  14097. (?d (call-interactively #'org-delete-property))
  14098. (?D (call-interactively #'org-delete-property-globally))
  14099. (?c (call-interactively #'org-compute-property-at-point))
  14100. (otherwise (user-error "No such property action %c" c)))))
  14101. (defun org-inc-effort ()
  14102. "Increment the value of the effort property in the current entry."
  14103. (interactive)
  14104. (org-set-effort nil t))
  14105. (defvar org-clock-effort) ; Defined in org-clock.el.
  14106. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14107. (defun org-set-effort (&optional value increment)
  14108. "Set the effort property of the current entry.
  14109. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14110. 10th allowed value.
  14111. When INCREMENT is non-nil, set the property to the next allowed value."
  14112. (interactive "P")
  14113. (when (equal value 0) (setq value 10))
  14114. (let* ((completion-ignore-case t)
  14115. (prop org-effort-property)
  14116. (cur (org-entry-get nil prop))
  14117. (allowed (org-property-get-allowed-values nil prop 'table))
  14118. (existing (mapcar 'list (org-property-values prop)))
  14119. (heading (nth 4 (org-heading-components)))
  14120. rpl
  14121. (val (cond
  14122. ((stringp value) value)
  14123. ((and allowed (integerp value))
  14124. (or (car (nth (1- value) allowed))
  14125. (car (org-last allowed))))
  14126. ((and allowed increment)
  14127. (or (cl-caadr (member (list cur) allowed))
  14128. (user-error "Allowed effort values are not set")))
  14129. (allowed
  14130. (message "Select 1-9,0, [RET%s]: %s"
  14131. (if cur (concat "=" cur) "")
  14132. (mapconcat 'car allowed " "))
  14133. (setq rpl (read-char-exclusive))
  14134. (if (equal rpl ?\r)
  14135. cur
  14136. (setq rpl (- rpl ?0))
  14137. (when (equal rpl 0) (setq rpl 10))
  14138. (if (and (> rpl 0) (<= rpl (length allowed)))
  14139. (car (nth (1- rpl) allowed))
  14140. (org-completing-read "Effort: " allowed nil))))
  14141. (t
  14142. (org-completing-read
  14143. (concat "Effort" (and cur (string-match "\\S-" cur)
  14144. (concat " [" cur "]"))
  14145. ": ")
  14146. existing nil nil "" nil cur)))))
  14147. (unless (equal (org-entry-get nil prop) val)
  14148. (org-entry-put nil prop val))
  14149. (org-refresh-property
  14150. '((effort . identity)
  14151. (effort-minutes . org-duration-to-minutes))
  14152. val)
  14153. (when (equal heading (bound-and-true-p org-clock-current-task))
  14154. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14155. (org-clock-update-mode-line))
  14156. (message "%s is now %s" prop val)))
  14157. (defun org-entry-properties (&optional pom which)
  14158. "Get all properties of the current entry.
  14159. When POM is a buffer position, get all properties from the entry
  14160. there instead.
  14161. This includes the TODO keyword, the tags, time strings for
  14162. deadline, scheduled, and clocking, and any additional properties
  14163. defined in the entry.
  14164. If WHICH is nil or `all', get all properties. If WHICH is
  14165. `special' or `standard', only get that subclass. If WHICH is
  14166. a string, only get that property.
  14167. Return value is an alist. Keys are properties, as upcased
  14168. strings."
  14169. (org-with-point-at pom
  14170. (when (and (derived-mode-p 'org-mode)
  14171. (ignore-errors (org-back-to-heading t)))
  14172. (catch 'exit
  14173. (let* ((beg (point))
  14174. (specific (and (stringp which) (upcase which)))
  14175. (which (cond ((not specific) which)
  14176. ((member specific org-special-properties) 'special)
  14177. (t 'standard)))
  14178. props)
  14179. ;; Get the special properties, like TODO and TAGS.
  14180. (when (memq which '(nil all special))
  14181. (when (or (not specific) (string= specific "CLOCKSUM"))
  14182. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14183. (when clocksum
  14184. (push (cons "CLOCKSUM" (org-duration-from-minutes clocksum))
  14185. props)))
  14186. (when specific (throw 'exit props)))
  14187. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14188. (let ((clocksumt (get-text-property (point)
  14189. :org-clock-minutes-today)))
  14190. (when clocksumt
  14191. (push (cons "CLOCKSUM_T"
  14192. (org-duration-from-minutes clocksumt))
  14193. props)))
  14194. (when specific (throw 'exit props)))
  14195. (when (or (not specific) (string= specific "ITEM"))
  14196. (let ((case-fold-search nil))
  14197. (when (looking-at org-complex-heading-regexp)
  14198. (push (cons "ITEM"
  14199. (let ((title (match-string-no-properties 4)))
  14200. (if (org-string-nw-p title)
  14201. (org-remove-tabs title)
  14202. "")))
  14203. props)))
  14204. (when specific (throw 'exit props)))
  14205. (when (or (not specific) (string= specific "TODO"))
  14206. (let ((case-fold-search nil))
  14207. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14208. (push (cons "TODO" (match-string-no-properties 2)) props)))
  14209. (when specific (throw 'exit props)))
  14210. (when (or (not specific) (string= specific "PRIORITY"))
  14211. (push (cons "PRIORITY"
  14212. (if (looking-at org-priority-regexp)
  14213. (match-string-no-properties 2)
  14214. (char-to-string org-default-priority)))
  14215. props)
  14216. (when specific (throw 'exit props)))
  14217. (when (or (not specific) (string= specific "FILE"))
  14218. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14219. props)
  14220. (when specific (throw 'exit props)))
  14221. (when (or (not specific) (string= specific "TAGS"))
  14222. (let ((value (org-string-nw-p (org-get-tags-string))))
  14223. (when value (push (cons "TAGS" value) props)))
  14224. (when specific (throw 'exit props)))
  14225. (when (or (not specific) (string= specific "ALLTAGS"))
  14226. (let ((value (org-get-tags-at)))
  14227. (when value
  14228. (push (cons "ALLTAGS"
  14229. (format ":%s:" (mapconcat #'identity value ":")))
  14230. props)))
  14231. (when specific (throw 'exit props)))
  14232. (when (or (not specific) (string= specific "BLOCKED"))
  14233. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14234. (when specific (throw 'exit props)))
  14235. (when (or (not specific)
  14236. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14237. (forward-line)
  14238. (when (looking-at-p org-planning-line-re)
  14239. (end-of-line)
  14240. (let ((bol (line-beginning-position))
  14241. ;; Backward compatibility: time keywords used to
  14242. ;; be configurable (before 8.3). Make sure we
  14243. ;; get the correct keyword.
  14244. (key-assoc `(("CLOSED" . ,org-closed-string)
  14245. ("DEADLINE" . ,org-deadline-string)
  14246. ("SCHEDULED" . ,org-scheduled-string))))
  14247. (dolist (pair (if specific (list (assoc specific key-assoc))
  14248. key-assoc))
  14249. (save-excursion
  14250. (when (search-backward (cdr pair) bol t)
  14251. (goto-char (match-end 0))
  14252. (skip-chars-forward " \t")
  14253. (and (looking-at org-ts-regexp-both)
  14254. (push (cons (car pair)
  14255. (match-string-no-properties 0))
  14256. props)))))))
  14257. (when specific (throw 'exit props)))
  14258. (when (or (not specific)
  14259. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14260. (let ((find-ts
  14261. (lambda (end ts)
  14262. ;; Fix next time-stamp before END. TS is the
  14263. ;; list of time-stamps found so far.
  14264. (let ((ts ts)
  14265. (regexp (cond
  14266. ((string= specific "TIMESTAMP")
  14267. org-ts-regexp)
  14268. ((string= specific "TIMESTAMP_IA")
  14269. org-ts-regexp-inactive)
  14270. ((assoc "TIMESTAMP_IA" ts)
  14271. org-ts-regexp)
  14272. ((assoc "TIMESTAMP" ts)
  14273. org-ts-regexp-inactive)
  14274. (t org-ts-regexp-both))))
  14275. (catch 'next
  14276. (while (re-search-forward regexp end t)
  14277. (backward-char)
  14278. (let ((object (org-element-context)))
  14279. ;; Accept to match timestamps in node
  14280. ;; properties, too.
  14281. (when (memq (org-element-type object)
  14282. '(node-property timestamp))
  14283. (let ((type
  14284. (org-element-property :type object)))
  14285. (cond
  14286. ((and (memq type '(active active-range))
  14287. (not (equal specific "TIMESTAMP_IA")))
  14288. (unless (assoc "TIMESTAMP" ts)
  14289. (push (cons "TIMESTAMP"
  14290. (org-element-property
  14291. :raw-value object))
  14292. ts)
  14293. (when specific (throw 'exit ts))))
  14294. ((and (memq type '(inactive inactive-range))
  14295. (not (string= specific "TIMESTAMP")))
  14296. (unless (assoc "TIMESTAMP_IA" ts)
  14297. (push (cons "TIMESTAMP_IA"
  14298. (org-element-property
  14299. :raw-value object))
  14300. ts)
  14301. (when specific (throw 'exit ts))))))
  14302. ;; Both timestamp types are found,
  14303. ;; move to next part.
  14304. (when (= (length ts) 2) (throw 'next ts)))))
  14305. ts)))))
  14306. (goto-char beg)
  14307. ;; First look for timestamps within headline.
  14308. (let ((ts (funcall find-ts (line-end-position) nil)))
  14309. (if (= (length ts) 2) (setq props (nconc ts props))
  14310. ;; Then find timestamps in the section, skipping
  14311. ;; planning line.
  14312. (let ((end (save-excursion (outline-next-heading))))
  14313. (forward-line)
  14314. (when (looking-at-p org-planning-line-re) (forward-line))
  14315. (setq props (nconc (funcall find-ts end ts) props))))))))
  14316. ;; Get the standard properties, like :PROP:.
  14317. (when (memq which '(nil all standard))
  14318. ;; If we are looking after a specific property, delegate
  14319. ;; to `org-entry-get', which is faster. However, make an
  14320. ;; exception for "CATEGORY", since it can be also set
  14321. ;; through keywords (i.e. #+CATEGORY).
  14322. (if (and specific (not (equal specific "CATEGORY")))
  14323. (let ((value (org-entry-get beg specific nil t)))
  14324. (throw 'exit (and value (list (cons specific value)))))
  14325. (let ((range (org-get-property-block beg)))
  14326. (when range
  14327. (let ((end (cdr range)) seen-base)
  14328. (goto-char (car range))
  14329. ;; Unlike to `org--update-property-plist', we
  14330. ;; handle the case where base values is found
  14331. ;; after its extension. We also forbid standard
  14332. ;; properties to be named as special properties.
  14333. (while (re-search-forward org-property-re end t)
  14334. (let* ((key (upcase (match-string-no-properties 2)))
  14335. (extendp (string-match-p "\\+\\'" key))
  14336. (key-base (if extendp (substring key 0 -1) key))
  14337. (value (match-string-no-properties 3)))
  14338. (cond
  14339. ((member-ignore-case key-base org-special-properties))
  14340. (extendp
  14341. (setq props
  14342. (org--update-property-plist key value props)))
  14343. ((member key seen-base))
  14344. (t (push key seen-base)
  14345. (let ((p (assoc-string key props t)))
  14346. (if p (setcdr p (concat value " " (cdr p)))
  14347. (push (cons key value) props))))))))))))
  14348. (unless (assoc "CATEGORY" props)
  14349. (push (cons "CATEGORY" (org-get-category beg)) props)
  14350. (when (string= specific "CATEGORY") (throw 'exit props)))
  14351. ;; Return value.
  14352. props)))))
  14353. (defun org--property-local-values (property literal-nil)
  14354. "Return value for PROPERTY in current entry.
  14355. Value is a list whose car is the base value for PROPERTY and cdr
  14356. a list of accumulated values. Return nil if neither is found in
  14357. the entry. Also return nil when PROPERTY is set to \"nil\",
  14358. unless LITERAL-NIL is non-nil."
  14359. (let ((range (org-get-property-block)))
  14360. (when range
  14361. (goto-char (car range))
  14362. (let* ((case-fold-search t)
  14363. (end (cdr range))
  14364. (value
  14365. ;; Base value.
  14366. (save-excursion
  14367. (let ((v (and (re-search-forward
  14368. (org-re-property property nil t) end t)
  14369. (match-string-no-properties 3))))
  14370. (list (if literal-nil v (org-not-nil v)))))))
  14371. ;; Find additional values.
  14372. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14373. (while (re-search-forward property+ end t)
  14374. (push (match-string-no-properties 3) value)))
  14375. ;; Return final values.
  14376. (and (not (equal value '(nil))) (nreverse value))))))
  14377. (defun org--property-global-value (property literal-nil)
  14378. "Return value for PROPERTY in current buffer.
  14379. Return value is a string. Return nil if property is not set
  14380. globally. Also return nil when PROPERTY is set to \"nil\",
  14381. unless LITERAL-NIL is non-nil."
  14382. (let ((global
  14383. (cdr (or (assoc-string property org-file-properties t)
  14384. (assoc-string property org-global-properties t)
  14385. (assoc-string property org-global-properties-fixed t)))))
  14386. (if literal-nil global (org-not-nil global))))
  14387. (defun org-entry-get (pom property &optional inherit literal-nil)
  14388. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14389. If INHERIT is non-nil and the entry does not have the property,
  14390. then also check higher levels of the hierarchy. If INHERIT is
  14391. the symbol `selective', use inheritance only if the setting in
  14392. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14393. If the property is present but empty, the return value is the
  14394. empty string. If the property is not present at all, nil is
  14395. returned. In any other case, return the value as a string.
  14396. Search is case-insensitive.
  14397. If LITERAL-NIL is set, return the string value \"nil\" as
  14398. a string, do not interpret it as the list atom nil. This is used
  14399. for inheritance when a \"nil\" value can supersede a non-nil
  14400. value higher up the hierarchy."
  14401. (org-with-point-at pom
  14402. (cond
  14403. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14404. ;; We need a special property. Use `org-entry-properties' to
  14405. ;; retrieve it, but specify the wanted property.
  14406. (cdr (assoc-string property (org-entry-properties nil property))))
  14407. ((and inherit
  14408. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14409. (org-entry-get-with-inheritance property literal-nil))
  14410. (t
  14411. (let* ((local (org--property-local-values property literal-nil))
  14412. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14413. (if literal-nil value (org-not-nil value)))))))
  14414. (defun org-property-or-variable-value (var &optional inherit)
  14415. "Check if there is a property fixing the value of VAR.
  14416. If yes, return this value. If not, return the current value of the variable."
  14417. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14418. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14419. (read prop)
  14420. (symbol-value var))))
  14421. (defun org-entry-delete (pom property)
  14422. "Delete PROPERTY from entry at point-or-marker POM.
  14423. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14424. non-nil when a property was removed."
  14425. (org-with-point-at pom
  14426. (pcase (org-get-property-block)
  14427. (`(,begin . ,origin)
  14428. (let* ((end (copy-marker origin))
  14429. (re (org-re-property
  14430. (concat (regexp-quote property) "\\+?") t t)))
  14431. (goto-char begin)
  14432. (while (re-search-forward re end t)
  14433. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14434. ;; If drawer is empty, remove it altogether.
  14435. (when (= begin end)
  14436. (delete-region (line-beginning-position 0)
  14437. (line-beginning-position 2)))
  14438. ;; Return non-nil if some property was removed.
  14439. (prog1 (/= end origin) (set-marker end nil))))
  14440. (_ nil))))
  14441. ;; Multi-values properties are properties that contain multiple values
  14442. ;; These values are assumed to be single words, separated by whitespace.
  14443. (defun org-entry-add-to-multivalued-property (pom property value)
  14444. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14445. (let* ((old (org-entry-get pom property))
  14446. (values (and old (split-string old))))
  14447. (setq value (org-entry-protect-space value))
  14448. (unless (member value values)
  14449. (setq values (append values (list value)))
  14450. (org-entry-put pom property (mapconcat #'identity values " ")))))
  14451. (defun org-entry-remove-from-multivalued-property (pom property value)
  14452. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14453. (let* ((old (org-entry-get pom property))
  14454. (values (and old (split-string old))))
  14455. (setq value (org-entry-protect-space value))
  14456. (when (member value values)
  14457. (setq values (delete value values))
  14458. (org-entry-put pom property (mapconcat #'identity values " ")))))
  14459. (defun org-entry-member-in-multivalued-property (pom property value)
  14460. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14461. (let* ((old (org-entry-get pom property))
  14462. (values (and old (split-string old))))
  14463. (setq value (org-entry-protect-space value))
  14464. (member value values)))
  14465. (defun org-entry-get-multivalued-property (pom property)
  14466. "Return a list of values in a multivalued property."
  14467. (let* ((value (org-entry-get pom property))
  14468. (values (and value (split-string value))))
  14469. (mapcar #'org-entry-restore-space values)))
  14470. (defun org-entry-put-multivalued-property (pom property &rest values)
  14471. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14472. VALUES should be a list of strings. Spaces will be protected."
  14473. (org-entry-put pom property (mapconcat #'org-entry-protect-space values " "))
  14474. (let* ((value (org-entry-get pom property))
  14475. (values (and value (split-string value))))
  14476. (mapcar #'org-entry-restore-space values)))
  14477. (defun org-entry-protect-space (s)
  14478. "Protect spaces and newline in string S."
  14479. (while (string-match " " s)
  14480. (setq s (replace-match "%20" t t s)))
  14481. (while (string-match "\n" s)
  14482. (setq s (replace-match "%0A" t t s)))
  14483. s)
  14484. (defun org-entry-restore-space (s)
  14485. "Restore spaces and newline in string S."
  14486. (while (string-match "%20" s)
  14487. (setq s (replace-match " " t t s)))
  14488. (while (string-match "%0A" s)
  14489. (setq s (replace-match "\n" t t s)))
  14490. s)
  14491. (defvar org-entry-property-inherited-from (make-marker)
  14492. "Marker pointing to the entry from where a property was inherited.
  14493. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14494. location of the entry where the inheritance search matched. If there was
  14495. no match, the marker will point nowhere.
  14496. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14497. is set.")
  14498. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14499. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14500. The search will stop at the first ancestor which has the property defined.
  14501. If the value found is \"nil\", return nil to show that the property
  14502. should be considered as undefined (this is the meaning of nil here).
  14503. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14504. (move-marker org-entry-property-inherited-from nil)
  14505. (org-with-wide-buffer
  14506. (let (value)
  14507. (catch 'exit
  14508. (while t
  14509. (let ((v (org--property-local-values property literal-nil)))
  14510. (when v
  14511. (setq value
  14512. (concat (mapconcat #'identity (delq nil v) " ")
  14513. (and value " ")
  14514. value)))
  14515. (cond
  14516. ((car v)
  14517. (org-back-to-heading t)
  14518. (move-marker org-entry-property-inherited-from (point))
  14519. (throw 'exit nil))
  14520. ((org-up-heading-safe))
  14521. (t
  14522. (let ((global (org--property-global-value property literal-nil)))
  14523. (cond ((not global))
  14524. (value (setq value (concat global " " value)))
  14525. (t (setq value global))))
  14526. (throw 'exit nil))))))
  14527. (if literal-nil value (org-not-nil value)))))
  14528. (defvar org-property-changed-functions nil
  14529. "Hook called when the value of a property has changed.
  14530. Each hook function should accept two arguments, the name of the property
  14531. and the new value.")
  14532. (defun org-entry-put (pom property value)
  14533. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14534. If the value is nil, it is converted to the empty string. If it
  14535. is not a string, an error is raised. Also raise an error on
  14536. invalid property names.
  14537. PROPERTY can be any regular property (see
  14538. `org-special-properties'). It can also be \"TODO\",
  14539. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14540. For the last two properties, VALUE may have any of the special
  14541. values \"earlier\" and \"later\". The function then increases or
  14542. decreases scheduled or deadline date by one day."
  14543. (cond ((null value) (setq value ""))
  14544. ((not (stringp value)) (error "Properties values should be strings"))
  14545. ((not (org--valid-property-p property))
  14546. (user-error "Invalid property name: \"%s\"" property)))
  14547. (org-with-point-at pom
  14548. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14549. (org-back-to-heading t)
  14550. (org-with-limited-levels (org-back-to-heading t)))
  14551. (let ((beg (point)))
  14552. (cond
  14553. ((equal property "TODO")
  14554. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14555. ((not (member value org-todo-keywords-1))
  14556. (user-error "\"%s\" is not a valid TODO state" value)))
  14557. (org-todo value)
  14558. (org-set-tags nil 'align))
  14559. ((equal property "PRIORITY")
  14560. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14561. (org-set-tags nil 'align))
  14562. ((equal property "SCHEDULED")
  14563. (forward-line)
  14564. (if (and (looking-at-p org-planning-line-re)
  14565. (re-search-forward
  14566. org-scheduled-time-regexp (line-end-position) t))
  14567. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14568. ((string= value "later") (org-timestamp-change 1 'day))
  14569. ((string= value "") (org-schedule '(4)))
  14570. (t (org-schedule nil value)))
  14571. (if (member value '("earlier" "later" ""))
  14572. (call-interactively #'org-schedule)
  14573. (org-schedule nil value))))
  14574. ((equal property "DEADLINE")
  14575. (forward-line)
  14576. (if (and (looking-at-p org-planning-line-re)
  14577. (re-search-forward
  14578. org-deadline-time-regexp (line-end-position) t))
  14579. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14580. ((string= value "later") (org-timestamp-change 1 'day))
  14581. ((string= value "") (org-deadline '(4)))
  14582. (t (org-deadline nil value)))
  14583. (if (member value '("earlier" "later" ""))
  14584. (call-interactively #'org-deadline)
  14585. (org-deadline nil value))))
  14586. ((member property org-special-properties)
  14587. (error "The %s property cannot be set with `org-entry-put'" property))
  14588. (t
  14589. (let* ((range (org-get-property-block beg 'force))
  14590. (end (cdr range))
  14591. (case-fold-search t))
  14592. (goto-char (car range))
  14593. (if (re-search-forward (org-re-property property nil t) end t)
  14594. (progn (delete-region (match-beginning 0) (match-end 0))
  14595. (goto-char (match-beginning 0)))
  14596. (goto-char end)
  14597. (insert "\n")
  14598. (backward-char))
  14599. (insert ":" property ":")
  14600. (when value (insert " " value))
  14601. (org-indent-line)))))
  14602. (run-hook-with-args 'org-property-changed-functions property value)))
  14603. (defun org-buffer-property-keys
  14604. (&optional specials defaults columns ignore-malformed)
  14605. "Get all property keys in the current buffer.
  14606. When SPECIALS is non-nil, also list the special properties that
  14607. reflect things like tags and TODO state.
  14608. When DEFAULTS is non-nil, also include properties that has
  14609. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14610. DESCRIPTION, LOCATION, and LOGGING and others.
  14611. When COLUMNS in non-nil, also include property names given in
  14612. COLUMN formats in the current buffer.
  14613. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14614. automatically performed, such drawers will be silently ignored."
  14615. (let ((case-fold-search t)
  14616. (props (append
  14617. (and specials org-special-properties)
  14618. (and defaults (cons org-effort-property org-default-properties))
  14619. nil)))
  14620. (org-with-wide-buffer
  14621. (goto-char (point-min))
  14622. (while (re-search-forward org-property-start-re nil t)
  14623. (let ((range (org-get-property-block)))
  14624. (catch 'skip
  14625. (unless range
  14626. (when (and (not ignore-malformed)
  14627. (not (org-before-first-heading-p))
  14628. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14629. (line-beginning-position))))
  14630. (org-get-property-block nil t))
  14631. (throw 'skip nil))
  14632. (goto-char (car range))
  14633. (let ((begin (car range))
  14634. (end (cdr range)))
  14635. ;; Make sure that found property block is not located
  14636. ;; before current point, as it would generate an infloop.
  14637. ;; It can happen, for example, in the following
  14638. ;; situation:
  14639. ;;
  14640. ;; * Headline
  14641. ;; :PROPERTIES:
  14642. ;; ...
  14643. ;; :END:
  14644. ;; *************** Inlinetask
  14645. ;; #+BEGIN_EXAMPLE
  14646. ;; :PROPERTIES:
  14647. ;; #+END_EXAMPLE
  14648. ;;
  14649. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14650. (while (< (point) end)
  14651. (let ((p (progn (looking-at org-property-re)
  14652. (match-string-no-properties 2))))
  14653. ;; Only add true property name, not extension symbol.
  14654. (push (if (not (string-match-p "\\+\\'" p)) p
  14655. (substring p 0 -1))
  14656. props))
  14657. (forward-line))))
  14658. (outline-next-heading)))
  14659. (when columns
  14660. (goto-char (point-min))
  14661. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14662. (let ((element (org-element-at-point)))
  14663. (when (memq (org-element-type element) '(keyword node-property))
  14664. (let ((value (org-element-property :value element))
  14665. (start 0))
  14666. (while (string-match "%[0-9]*\\([[:alnum:]_-]+\\)\\(([^)]+)\\)?\
  14667. \\(?:{[^}]+}\\)?"
  14668. value start)
  14669. (setq start (match-end 0))
  14670. (let ((p (match-string-no-properties 1 value)))
  14671. (unless (member-ignore-case p org-special-properties)
  14672. (push p props))))))))))
  14673. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14674. (defun org-property-values (key)
  14675. "List all non-nil values of property KEY in current buffer."
  14676. (org-with-wide-buffer
  14677. (goto-char (point-min))
  14678. (let ((case-fold-search t)
  14679. (re (org-re-property key))
  14680. values)
  14681. (while (re-search-forward re nil t)
  14682. (push (org-entry-get (point) key) values))
  14683. (delete-dups values))))
  14684. (defun org-insert-property-drawer ()
  14685. "Insert a property drawer into the current entry."
  14686. (org-with-wide-buffer
  14687. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14688. (org-back-to-heading t)
  14689. (org-with-limited-levels (org-back-to-heading t)))
  14690. (forward-line)
  14691. (when (looking-at-p org-planning-line-re) (forward-line))
  14692. (unless (looking-at-p org-property-drawer-re)
  14693. ;; Make sure we start editing a line from current entry, not from
  14694. ;; next one. It prevents extending text properties or overlays
  14695. ;; belonging to the latter.
  14696. (when (bolp) (backward-char))
  14697. (let ((begin (1+ (point)))
  14698. (inhibit-read-only t))
  14699. (insert "\n:PROPERTIES:\n:END:")
  14700. (when (eobp) (insert "\n"))
  14701. (org-indent-region begin (point))))))
  14702. (defun org-insert-drawer (&optional arg drawer)
  14703. "Insert a drawer at point.
  14704. When optional argument ARG is non-nil, insert a property drawer.
  14705. Optional argument DRAWER, when non-nil, is a string representing
  14706. drawer's name. Otherwise, the user is prompted for a name.
  14707. If a region is active, insert the drawer around that region
  14708. instead.
  14709. Point is left between drawer's boundaries."
  14710. (interactive "P")
  14711. (let* ((drawer (if arg "PROPERTIES"
  14712. (or drawer (read-from-minibuffer "Drawer: ")))))
  14713. (cond
  14714. ;; With C-u, fall back on `org-insert-property-drawer'
  14715. (arg (org-insert-property-drawer))
  14716. ;; Check validity of suggested drawer's name.
  14717. ((not (string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14718. (user-error "Invalid drawer name"))
  14719. ;; With an active region, insert a drawer at point.
  14720. ((not (org-region-active-p))
  14721. (progn
  14722. (unless (bolp) (insert "\n"))
  14723. (insert (format ":%s:\n\n:END:\n" drawer))
  14724. (forward-line -2)))
  14725. ;; Otherwise, insert the drawer at point
  14726. (t
  14727. (let ((rbeg (region-beginning))
  14728. (rend (copy-marker (region-end))))
  14729. (unwind-protect
  14730. (progn
  14731. (goto-char rbeg)
  14732. (beginning-of-line)
  14733. (when (save-excursion
  14734. (re-search-forward org-outline-regexp-bol rend t))
  14735. (user-error "Drawers cannot contain headlines"))
  14736. ;; Position point at the beginning of the first
  14737. ;; non-blank line in region. Insert drawer's opening
  14738. ;; there, then indent it.
  14739. (org-skip-whitespace)
  14740. (beginning-of-line)
  14741. (insert ":" drawer ":\n")
  14742. (forward-line -1)
  14743. (indent-for-tab-command)
  14744. ;; Move point to the beginning of the first blank line
  14745. ;; after the last non-blank line in region. Insert
  14746. ;; drawer's closing, then indent it.
  14747. (goto-char rend)
  14748. (skip-chars-backward " \r\t\n")
  14749. (insert "\n:END:")
  14750. (deactivate-mark t)
  14751. (indent-for-tab-command)
  14752. (unless (eolp) (insert "\n")))
  14753. ;; Clear marker, whatever the outcome of insertion is.
  14754. (set-marker rend nil)))))))
  14755. (defvar org-property-set-functions-alist nil
  14756. "Property set function alist.
  14757. Each entry should have the following format:
  14758. (PROPERTY . READ-FUNCTION)
  14759. The read function will be called with the same argument as
  14760. `org-completing-read'.")
  14761. (defun org-set-property-function (property)
  14762. "Get the function that should be used to set PROPERTY.
  14763. This is computed according to `org-property-set-functions-alist'."
  14764. (or (cdr (assoc property org-property-set-functions-alist))
  14765. 'org-completing-read))
  14766. (defun org-read-property-value (property)
  14767. "Read PROPERTY value from user."
  14768. (let* ((completion-ignore-case t)
  14769. (allowed (org-property-get-allowed-values nil property 'table))
  14770. (cur (org-entry-get nil property))
  14771. (prompt (concat property " value"
  14772. (if (and cur (string-match "\\S-" cur))
  14773. (concat " [" cur "]") "") ": "))
  14774. (set-function (org-set-property-function property))
  14775. (val (if allowed
  14776. (funcall set-function prompt allowed nil
  14777. (not (get-text-property 0 'org-unrestricted
  14778. (caar allowed))))
  14779. (funcall set-function prompt
  14780. (mapcar 'list (org-property-values property))
  14781. nil nil "" nil cur))))
  14782. (org-trim val)))
  14783. (defvar org-last-set-property nil)
  14784. (defvar org-last-set-property-value nil)
  14785. (defun org-read-property-name ()
  14786. "Read a property name."
  14787. (let ((completion-ignore-case t)
  14788. (default-prop (or (and (org-at-property-p)
  14789. (match-string-no-properties 2))
  14790. org-last-set-property)))
  14791. (org-completing-read
  14792. (concat "Property"
  14793. (if default-prop (concat " [" default-prop "]") "")
  14794. ": ")
  14795. (mapcar #'list (org-buffer-property-keys nil t t))
  14796. nil nil nil nil default-prop)))
  14797. (defun org-set-property-and-value (use-last)
  14798. "Allow to set [PROPERTY]: [value] direction from prompt.
  14799. When use-default, don't even ask, just use the last
  14800. \"[PROPERTY]: [value]\" string from the history."
  14801. (interactive "P")
  14802. (let* ((completion-ignore-case t)
  14803. (pv (or (and use-last org-last-set-property-value)
  14804. (org-completing-read
  14805. "Enter a \"[Property]: [value]\" pair: "
  14806. nil nil nil nil nil
  14807. org-last-set-property-value)))
  14808. prop val)
  14809. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14810. (setq prop (match-string 1 pv)
  14811. val (match-string 2 pv))
  14812. (org-set-property prop val))))
  14813. (defun org-set-property (property value)
  14814. "In the current entry, set PROPERTY to VALUE.
  14815. When called interactively, this will prompt for a property name, offering
  14816. completion on existing and default properties. And then it will prompt
  14817. for a value, offering completion either on allowed values (via an inherited
  14818. xxx_ALL property) or on existing values in other instances of this property
  14819. in the current file.
  14820. Throw an error when trying to set a property with an invalid name."
  14821. (interactive (list nil nil))
  14822. (let ((property (or property (org-read-property-name))))
  14823. ;; `org-entry-put' also makes the following check, but this one
  14824. ;; avoids polluting `org-last-set-property' and
  14825. ;; `org-last-set-property-value' needlessly.
  14826. (unless (org--valid-property-p property)
  14827. (user-error "Invalid property name: \"%s\"" property))
  14828. (let ((value (or value (org-read-property-value property)))
  14829. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14830. (setq org-last-set-property property)
  14831. (setq org-last-set-property-value (concat property ": " value))
  14832. ;; Possibly postprocess the inserted value:
  14833. (when fn (setq value (funcall fn value)))
  14834. (unless (equal (org-entry-get nil property) value)
  14835. (org-entry-put nil property value)))))
  14836. (defun org-find-property (property &optional value)
  14837. "Find first entry in buffer that sets PROPERTY.
  14838. When optional argument VALUE is non-nil, only consider an entry
  14839. if it contains PROPERTY set to this value. If PROPERTY should be
  14840. explicitly set to nil, use string \"nil\" for VALUE.
  14841. Return position where the entry begins, or nil if there is no
  14842. such entry. If narrowing is in effect, only search the visible
  14843. part of the buffer."
  14844. (save-excursion
  14845. (goto-char (point-min))
  14846. (let ((case-fold-search t)
  14847. (re (org-re-property property nil (not value) value)))
  14848. (catch 'exit
  14849. (while (re-search-forward re nil t)
  14850. (when (if value (org-at-property-p)
  14851. (org-entry-get (point) property nil t))
  14852. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14853. (defun org-delete-property (property)
  14854. "In the current entry, delete PROPERTY."
  14855. (interactive
  14856. (let* ((completion-ignore-case t)
  14857. (cat (org-entry-get (point) "CATEGORY"))
  14858. (props0 (org-entry-properties nil 'standard))
  14859. (props (if cat props0
  14860. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14861. (prop (if (< 1 (length props))
  14862. (completing-read "Property: " props nil t)
  14863. (caar props))))
  14864. (list prop)))
  14865. (if (not property)
  14866. (message "No property to delete in this entry")
  14867. (org-entry-delete nil property)
  14868. (message "Property \"%s\" deleted" property)))
  14869. (defun org-delete-property-globally (property)
  14870. "Remove PROPERTY globally, from all entries.
  14871. This function ignores narrowing, if any."
  14872. (interactive
  14873. (let* ((completion-ignore-case t)
  14874. (prop (completing-read
  14875. "Globally remove property: "
  14876. (mapcar #'list (org-buffer-property-keys)))))
  14877. (list prop)))
  14878. (org-with-wide-buffer
  14879. (goto-char (point-min))
  14880. (let ((count 0)
  14881. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14882. (while (re-search-forward re nil t)
  14883. (when (org-entry-delete (point) property) (cl-incf count)))
  14884. (message "Property \"%s\" removed from %d entries" property count))))
  14885. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14886. (defun org-compute-property-at-point ()
  14887. "Compute the property at point.
  14888. This looks for an enclosing column format, extracts the operator and
  14889. then applies it to the property in the column format's scope."
  14890. (interactive)
  14891. (unless (org-at-property-p)
  14892. (user-error "Not at a property"))
  14893. (let ((prop (match-string-no-properties 2)))
  14894. (org-columns-get-format-and-top-level)
  14895. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  14896. (user-error "No operator defined for property %s" prop))
  14897. (org-columns-compute prop)))
  14898. (defvar org-property-allowed-value-functions nil
  14899. "Hook for functions supplying allowed values for a specific property.
  14900. The functions must take a single argument, the name of the property, and
  14901. return a flat list of allowed values. If \":ETC\" is one of
  14902. the values, this means that these values are intended as defaults for
  14903. completion, but that other values should be allowed too.
  14904. The functions must return nil if they are not responsible for this
  14905. property.")
  14906. (defun org-property-get-allowed-values (pom property &optional table)
  14907. "Get allowed values for the property PROPERTY.
  14908. When TABLE is non-nil, return an alist that can directly be used for
  14909. completion."
  14910. (let (vals)
  14911. (cond
  14912. ((equal property "TODO")
  14913. (setq vals (org-with-point-at pom
  14914. (append org-todo-keywords-1 '("")))))
  14915. ((equal property "PRIORITY")
  14916. (let ((n org-lowest-priority))
  14917. (while (>= n org-highest-priority)
  14918. (push (char-to-string n) vals)
  14919. (setq n (1- n)))))
  14920. ((equal property "CATEGORY"))
  14921. ((member property org-special-properties))
  14922. ((setq vals (run-hook-with-args-until-success
  14923. 'org-property-allowed-value-functions property)))
  14924. (t
  14925. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14926. (when (and vals (string-match "\\S-" vals))
  14927. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14928. (setq vals (mapcar (lambda (x)
  14929. (cond ((stringp x) x)
  14930. ((numberp x) (number-to-string x))
  14931. ((symbolp x) (symbol-name x))
  14932. (t "???")))
  14933. vals)))))
  14934. (when (member ":ETC" vals)
  14935. (setq vals (remove ":ETC" vals))
  14936. (org-add-props (car vals) '(org-unrestricted t)))
  14937. (if table (mapcar 'list vals) vals)))
  14938. (defun org-property-previous-allowed-value (&optional _previous)
  14939. "Switch to the next allowed value for this property."
  14940. (interactive)
  14941. (org-property-next-allowed-value t))
  14942. (defun org-property-next-allowed-value (&optional previous)
  14943. "Switch to the next allowed value for this property."
  14944. (interactive)
  14945. (unless (org-at-property-p)
  14946. (user-error "Not at a property"))
  14947. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14948. (key (match-string 2))
  14949. (value (match-string 3))
  14950. (allowed (or (org-property-get-allowed-values (point) key)
  14951. (and (member value '("[ ]" "[-]" "[X]"))
  14952. '("[ ]" "[X]"))))
  14953. (heading (save-match-data (nth 4 (org-heading-components))))
  14954. nval)
  14955. (unless allowed
  14956. (user-error "Allowed values for this property have not been defined"))
  14957. (when previous (setq allowed (reverse allowed)))
  14958. (when (member value allowed)
  14959. (setq nval (car (cdr (member value allowed)))))
  14960. (setq nval (or nval (car allowed)))
  14961. (when (equal nval value)
  14962. (user-error "Only one allowed value for this property"))
  14963. (org-at-property-p)
  14964. (replace-match (concat " :" key ": " nval) t t)
  14965. (org-indent-line)
  14966. (beginning-of-line 1)
  14967. (skip-chars-forward " \t")
  14968. (when (equal prop org-effort-property)
  14969. (org-refresh-property
  14970. '((effort . identity)
  14971. (effort-minutes . org-duration-to-minutes))
  14972. nval)
  14973. (when (string= org-clock-current-task heading)
  14974. (setq org-clock-effort nval)
  14975. (org-clock-update-mode-line)))
  14976. (run-hook-with-args 'org-property-changed-functions key nval)))
  14977. (defun org-find-olp (path &optional this-buffer)
  14978. "Return a marker pointing to the entry at outline path OLP.
  14979. If anything goes wrong, throw an error.
  14980. You can wrap this call to catch the error like this:
  14981. (condition-case msg
  14982. (org-mobile-locate-entry (match-string 4))
  14983. (error (nth 1 msg)))
  14984. The return value will then be either a string with the error message,
  14985. or a marker if everything is OK.
  14986. If THIS-BUFFER is set, the outline path does not contain a file,
  14987. only headings."
  14988. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14989. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14990. (level 1)
  14991. (lmin 1)
  14992. (lmax 1)
  14993. end found flevel)
  14994. (unless buffer (error "File not found :%s" file))
  14995. (with-current-buffer buffer
  14996. (unless (derived-mode-p 'org-mode)
  14997. (error "Buffer %s needs to be in Org mode" buffer))
  14998. (org-with-wide-buffer
  14999. (goto-char (point-min))
  15000. (dolist (heading path)
  15001. (let ((re (format org-complex-heading-regexp-format
  15002. (regexp-quote heading)))
  15003. (cnt 0))
  15004. (while (re-search-forward re end t)
  15005. (setq level (- (match-end 1) (match-beginning 1)))
  15006. (when (and (>= level lmin) (<= level lmax))
  15007. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15008. (when (= cnt 0)
  15009. (error "Heading not found on level %d: %s" lmax heading))
  15010. (when (> cnt 1)
  15011. (error "Heading not unique on level %d: %s" lmax heading))
  15012. (goto-char found)
  15013. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15014. (setq end (save-excursion (org-end-of-subtree t t)))))
  15015. (when (org-at-heading-p)
  15016. (point-marker))))))
  15017. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15018. "Find node HEADING in BUFFER.
  15019. Return a marker to the heading if it was found, or nil if not.
  15020. If POS-ONLY is set, return just the position instead of a marker.
  15021. The heading text must match exact, but it may have a TODO keyword,
  15022. a priority cookie and tags in the standard locations."
  15023. (with-current-buffer (or buffer (current-buffer))
  15024. (org-with-wide-buffer
  15025. (goto-char (point-min))
  15026. (let (case-fold-search)
  15027. (when (re-search-forward
  15028. (format org-complex-heading-regexp-format
  15029. (regexp-quote heading)) nil t)
  15030. (if pos-only
  15031. (match-beginning 0)
  15032. (move-marker (make-marker) (match-beginning 0))))))))
  15033. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15034. "Find Org node headline HEADING in all .org files in directory DIR.
  15035. When the target headline is found, return a marker to this location."
  15036. (let ((files (directory-files (or dir default-directory)
  15037. t "\\`[^.#].*\\.org\\'"))
  15038. visiting m buffer)
  15039. (catch 'found
  15040. (dolist (file files)
  15041. (message "trying %s" file)
  15042. (setq visiting (org-find-base-buffer-visiting file))
  15043. (setq buffer (or visiting (find-file-noselect file)))
  15044. (setq m (org-find-exact-headline-in-buffer
  15045. heading buffer))
  15046. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15047. (and m (throw 'found m))))))
  15048. (defun org-find-entry-with-id (ident)
  15049. "Locate the entry that contains the ID property with exact value IDENT.
  15050. IDENT can be a string, a symbol or a number, this function will search for
  15051. the string representation of it.
  15052. Return the position where this entry starts, or nil if there is no such entry."
  15053. (interactive "sID: ")
  15054. (let ((id (cond
  15055. ((stringp ident) ident)
  15056. ((symbolp ident) (symbol-name ident))
  15057. ((numberp ident) (number-to-string ident))
  15058. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15059. (org-with-wide-buffer (org-find-property "ID" id))))
  15060. ;;;; Timestamps
  15061. (defvar org-last-changed-timestamp nil)
  15062. (defvar org-last-inserted-timestamp nil
  15063. "The last time stamp inserted with `org-insert-time-stamp'.")
  15064. (defun org-time-stamp (arg &optional inactive)
  15065. "Prompt for a date/time and insert a time stamp.
  15066. If the user specifies a time like HH:MM or if this command is
  15067. called with at least one prefix argument, the time stamp contains
  15068. the date and the time. Otherwise, only the date is included.
  15069. All parts of a date not specified by the user are filled in from
  15070. the timestamp at point, if any, or the current date/time
  15071. otherwise.
  15072. If there is already a timestamp at the cursor, it is replaced.
  15073. With two universal prefix arguments, insert an active timestamp
  15074. with the current time without prompting the user.
  15075. When called from lisp, the timestamp is inactive if INACTIVE is
  15076. non-nil."
  15077. (interactive "P")
  15078. (let* ((ts (cond
  15079. ((org-at-date-range-p t)
  15080. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15081. ((org-at-timestamp-p 'lax) (match-string 0))))
  15082. ;; Default time is either the timestamp at point or today.
  15083. ;; When entering a range, only the range start is considered.
  15084. (default-time (if (not ts) (current-time)
  15085. (apply #'encode-time (org-parse-time-string ts))))
  15086. (default-input (and ts (org-get-compact-tod ts)))
  15087. (repeater (and ts
  15088. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15089. (match-string 0 ts)))
  15090. org-time-was-given
  15091. org-end-time-was-given
  15092. (time
  15093. (and (if (equal arg '(16)) (current-time)
  15094. ;; Preserve `this-command' and `last-command'.
  15095. (let ((this-command this-command)
  15096. (last-command last-command))
  15097. (org-read-date
  15098. arg 'totime nil nil default-time default-input
  15099. inactive))))))
  15100. (cond
  15101. ((and ts
  15102. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15103. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15104. (insert "--")
  15105. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15106. (ts
  15107. ;; Make sure we're on a timestamp. When in the middle of a date
  15108. ;; range, move arbitrarily to range end.
  15109. (unless (org-at-timestamp-p 'lax)
  15110. (skip-chars-forward "-")
  15111. (org-at-timestamp-p 'lax))
  15112. (replace-match "")
  15113. (setq org-last-changed-timestamp
  15114. (org-insert-time-stamp
  15115. time (or org-time-was-given arg)
  15116. inactive nil nil (list org-end-time-was-given)))
  15117. (when repeater
  15118. (backward-char)
  15119. (insert " " repeater)
  15120. (setq org-last-changed-timestamp
  15121. (concat (substring org-last-inserted-timestamp 0 -1)
  15122. " " repeater ">")))
  15123. (message "Timestamp updated"))
  15124. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15125. (t (org-insert-time-stamp
  15126. time (or org-time-was-given arg) inactive nil nil
  15127. (list org-end-time-was-given))))))
  15128. ;; FIXME: can we use this for something else, like computing time differences?
  15129. (defun org-get-compact-tod (s)
  15130. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15131. (let* ((t1 (match-string 1 s))
  15132. (h1 (string-to-number (match-string 2 s)))
  15133. (m1 (string-to-number (match-string 3 s)))
  15134. (t2 (and (match-end 4) (match-string 5 s)))
  15135. (h2 (and t2 (string-to-number (match-string 6 s))))
  15136. (m2 (and t2 (string-to-number (match-string 7 s))))
  15137. dh dm)
  15138. (if (not t2)
  15139. t1
  15140. (setq dh (- h2 h1) dm (- m2 m1))
  15141. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15142. (concat t1 "+" (number-to-string dh)
  15143. (and (/= 0 dm) (format ":%02d" dm)))))))
  15144. (defun org-time-stamp-inactive (&optional arg)
  15145. "Insert an inactive time stamp.
  15146. An inactive time stamp is enclosed in square brackets instead of angle
  15147. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15148. does not link to the calendar and cannot be changed with the S-cursor keys.
  15149. So these are more for recording a certain time/date."
  15150. (interactive "P")
  15151. (org-time-stamp arg 'inactive))
  15152. (defvar org-date-ovl (make-overlay 1 1))
  15153. (overlay-put org-date-ovl 'face 'org-date-selected)
  15154. (delete-overlay org-date-ovl)
  15155. (defvar org-ans1) ; dynamically scoped parameter
  15156. (defvar org-ans2) ; dynamically scoped parameter
  15157. (defvar org-plain-time-of-day-regexp) ; defined below
  15158. (defvar org-overriding-default-time nil) ; dynamically scoped
  15159. (defvar org-read-date-overlay nil)
  15160. (defvar org-dcst nil) ; dynamically scoped
  15161. (defvar org-read-date-history nil)
  15162. (defvar org-read-date-final-answer nil)
  15163. (defvar org-read-date-analyze-futurep nil)
  15164. (defvar org-read-date-analyze-forced-year nil)
  15165. (defvar org-read-date-inactive)
  15166. (defvar org-read-date-minibuffer-local-map
  15167. (let* ((map (make-sparse-keymap)))
  15168. (set-keymap-parent map minibuffer-local-map)
  15169. (org-defkey map (kbd ".")
  15170. (lambda () (interactive)
  15171. ;; Are we at the beginning of the prompt?
  15172. (if (looking-back "^[^:]+: "
  15173. (let ((inhibit-field-text-motion t))
  15174. (line-beginning-position)))
  15175. (org-eval-in-calendar '(calendar-goto-today))
  15176. (insert "."))))
  15177. (org-defkey map (kbd "C-.")
  15178. (lambda () (interactive)
  15179. (org-eval-in-calendar '(calendar-goto-today))))
  15180. (org-defkey map [(meta shift left)]
  15181. (lambda () (interactive)
  15182. (org-eval-in-calendar '(calendar-backward-month 1))))
  15183. (org-defkey map [(meta shift right)]
  15184. (lambda () (interactive)
  15185. (org-eval-in-calendar '(calendar-forward-month 1))))
  15186. (org-defkey map [(meta shift up)]
  15187. (lambda () (interactive)
  15188. (org-eval-in-calendar '(calendar-backward-year 1))))
  15189. (org-defkey map [(meta shift down)]
  15190. (lambda () (interactive)
  15191. (org-eval-in-calendar '(calendar-forward-year 1))))
  15192. (org-defkey map [?\e (shift left)]
  15193. (lambda () (interactive)
  15194. (org-eval-in-calendar '(calendar-backward-month 1))))
  15195. (org-defkey map [?\e (shift right)]
  15196. (lambda () (interactive)
  15197. (org-eval-in-calendar '(calendar-forward-month 1))))
  15198. (org-defkey map [?\e (shift up)]
  15199. (lambda () (interactive)
  15200. (org-eval-in-calendar '(calendar-backward-year 1))))
  15201. (org-defkey map [?\e (shift down)]
  15202. (lambda () (interactive)
  15203. (org-eval-in-calendar '(calendar-forward-year 1))))
  15204. (org-defkey map [(shift up)]
  15205. (lambda () (interactive)
  15206. (org-eval-in-calendar '(calendar-backward-week 1))))
  15207. (org-defkey map [(shift down)]
  15208. (lambda () (interactive)
  15209. (org-eval-in-calendar '(calendar-forward-week 1))))
  15210. (org-defkey map [(shift left)]
  15211. (lambda () (interactive)
  15212. (org-eval-in-calendar '(calendar-backward-day 1))))
  15213. (org-defkey map [(shift right)]
  15214. (lambda () (interactive)
  15215. (org-eval-in-calendar '(calendar-forward-day 1))))
  15216. (org-defkey map "!"
  15217. (lambda () (interactive)
  15218. (org-eval-in-calendar '(diary-view-entries))
  15219. (message "")))
  15220. (org-defkey map ">"
  15221. (lambda () (interactive)
  15222. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15223. (org-defkey map "<"
  15224. (lambda () (interactive)
  15225. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15226. (org-defkey map "\C-v"
  15227. (lambda () (interactive)
  15228. (org-eval-in-calendar
  15229. '(calendar-scroll-left-three-months 1))))
  15230. (org-defkey map "\M-v"
  15231. (lambda () (interactive)
  15232. (org-eval-in-calendar
  15233. '(calendar-scroll-right-three-months 1))))
  15234. map)
  15235. "Keymap for minibuffer commands when using `org-read-date'.")
  15236. (defvar org-def)
  15237. (defvar org-defdecode)
  15238. (defvar org-with-time)
  15239. (defvar calendar-setup) ; Dynamically scoped.
  15240. (defun org-read-date (&optional with-time to-time from-string prompt
  15241. default-time default-input inactive)
  15242. "Read a date, possibly a time, and make things smooth for the user.
  15243. The prompt will suggest to enter an ISO date, but you can also enter anything
  15244. which will at least partially be understood by `parse-time-string'.
  15245. Unrecognized parts of the date will default to the current day, month, year,
  15246. hour and minute. If this command is called to replace a timestamp at point,
  15247. or to enter the second timestamp of a range, the default time is taken
  15248. from the existing stamp. Furthermore, the command prefers the future,
  15249. so if you are giving a date where the year is not given, and the day-month
  15250. combination is already past in the current year, it will assume you
  15251. mean next year. For details, see the manual. A few examples:
  15252. 3-2-5 --> 2003-02-05
  15253. feb 15 --> currentyear-02-15
  15254. 2/15 --> currentyear-02-15
  15255. sep 12 9 --> 2009-09-12
  15256. 12:45 --> today 12:45
  15257. 22 sept 0:34 --> currentyear-09-22 0:34
  15258. 12 --> currentyear-currentmonth-12
  15259. Fri --> nearest Friday after today
  15260. -Tue --> last Tuesday
  15261. etc.
  15262. Furthermore you can specify a relative date by giving, as the *first* thing
  15263. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15264. change in days weeks, months, years.
  15265. With a single plus or minus, the date is relative to today. With a double
  15266. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15267. +4d --> four days from today
  15268. +4 --> same as above
  15269. +2w --> two weeks from today
  15270. ++5 --> five days from default date
  15271. The function understands only English month and weekday abbreviations.
  15272. While prompting, a calendar is popped up - you can also select the
  15273. date with the mouse (button 1). The calendar shows a period of three
  15274. months. To scroll it to other months, use the keys `>' and `<'.
  15275. If you don't like the calendar, turn it off with
  15276. (setq org-read-date-popup-calendar nil)
  15277. With optional argument TO-TIME, the date will immediately be converted
  15278. to an internal time.
  15279. With an optional argument WITH-TIME, the prompt will suggest to
  15280. also insert a time. Note that when WITH-TIME is not set, you can
  15281. still enter a time, and this function will inform the calling routine
  15282. about this change. The calling routine may then choose to change the
  15283. format used to insert the time stamp into the buffer to include the time.
  15284. With optional argument FROM-STRING, read from this string instead from
  15285. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15286. the time/date that is used for everything that is not specified by the
  15287. user."
  15288. (require 'parse-time)
  15289. (let* ((org-with-time with-time)
  15290. (org-time-stamp-rounding-minutes
  15291. (if (equal org-with-time '(16))
  15292. '(0 0)
  15293. org-time-stamp-rounding-minutes))
  15294. (org-dcst org-display-custom-times)
  15295. (ct (org-current-time))
  15296. (org-def (or org-overriding-default-time default-time ct))
  15297. (org-defdecode (decode-time org-def))
  15298. (cur-frame (selected-frame))
  15299. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15300. (calendar-setup
  15301. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15302. (calendar-move-hook nil)
  15303. (calendar-view-diary-initially-flag nil)
  15304. (calendar-view-holidays-initially-flag nil)
  15305. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15306. ;; Rationalize `org-def' and `org-defdecode', if required.
  15307. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15308. (setf (nth 2 org-defdecode) -1)
  15309. (setf (nth 1 org-defdecode) 59)
  15310. (setq org-def (apply #'encode-time org-defdecode))
  15311. (setq org-defdecode (decode-time org-def)))
  15312. (let* ((timestr (format-time-string
  15313. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15314. org-def))
  15315. (prompt (concat (if prompt (concat prompt " ") "")
  15316. (format "Date+time [%s]: " timestr))))
  15317. (cond
  15318. (from-string (setq ans from-string))
  15319. (org-read-date-popup-calendar
  15320. (save-excursion
  15321. (save-window-excursion
  15322. (calendar)
  15323. (when (eq calendar-setup 'calendar-only)
  15324. (setq cal-frame
  15325. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15326. (select-frame cal-frame))
  15327. (org-eval-in-calendar '(setq cursor-type nil) t)
  15328. (unwind-protect
  15329. (progn
  15330. (calendar-forward-day (- (time-to-days org-def)
  15331. (calendar-absolute-from-gregorian
  15332. (calendar-current-date))))
  15333. (org-eval-in-calendar nil t)
  15334. (let* ((old-map (current-local-map))
  15335. (map (copy-keymap calendar-mode-map))
  15336. (minibuffer-local-map
  15337. (copy-keymap org-read-date-minibuffer-local-map)))
  15338. (org-defkey map (kbd "RET") 'org-calendar-select)
  15339. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15340. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15341. (unwind-protect
  15342. (progn
  15343. (use-local-map map)
  15344. (setq org-read-date-inactive inactive)
  15345. (add-hook 'post-command-hook 'org-read-date-display)
  15346. (setq org-ans0
  15347. (read-string prompt
  15348. default-input
  15349. 'org-read-date-history
  15350. nil))
  15351. ;; org-ans0: from prompt
  15352. ;; org-ans1: from mouse click
  15353. ;; org-ans2: from calendar motion
  15354. (setq ans
  15355. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15356. (remove-hook 'post-command-hook 'org-read-date-display)
  15357. (use-local-map old-map)
  15358. (when org-read-date-overlay
  15359. (delete-overlay org-read-date-overlay)
  15360. (setq org-read-date-overlay nil)))))
  15361. (bury-buffer "*Calendar*")
  15362. (when cal-frame
  15363. (delete-frame cal-frame)
  15364. (select-frame-set-input-focus cur-frame))))))
  15365. (t ; Naked prompt only
  15366. (unwind-protect
  15367. (setq ans (read-string prompt default-input
  15368. 'org-read-date-history timestr))
  15369. (when org-read-date-overlay
  15370. (delete-overlay org-read-date-overlay)
  15371. (setq org-read-date-overlay nil))))))
  15372. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15373. (when org-read-date-analyze-forced-year
  15374. (message "Year was forced into %s"
  15375. (if org-read-date-force-compatible-dates
  15376. "compatible range (1970-2037)"
  15377. "range representable on this machine"))
  15378. (ding))
  15379. ;; One round trip to get rid of 34th of August and stuff like that....
  15380. (setq final (decode-time (apply 'encode-time final)))
  15381. (setq org-read-date-final-answer ans)
  15382. (if to-time
  15383. (apply 'encode-time final)
  15384. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15385. (format "%04d-%02d-%02d %02d:%02d"
  15386. (nth 5 final) (nth 4 final) (nth 3 final)
  15387. (nth 2 final) (nth 1 final))
  15388. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15389. (defun org-read-date-display ()
  15390. "Display the current date prompt interpretation in the minibuffer."
  15391. (when org-read-date-display-live
  15392. (when org-read-date-overlay
  15393. (delete-overlay org-read-date-overlay))
  15394. (when (minibufferp (current-buffer))
  15395. (save-excursion
  15396. (end-of-line 1)
  15397. (while (not (equal (buffer-substring
  15398. (max (point-min) (- (point) 4)) (point))
  15399. " "))
  15400. (insert " ")))
  15401. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15402. " " (or org-ans1 org-ans2)))
  15403. (org-end-time-was-given nil)
  15404. (f (org-read-date-analyze ans org-def org-defdecode))
  15405. (fmts (if org-dcst
  15406. org-time-stamp-custom-formats
  15407. org-time-stamp-formats))
  15408. (fmt (if (or org-with-time
  15409. (and (boundp 'org-time-was-given) org-time-was-given))
  15410. (cdr fmts)
  15411. (car fmts)))
  15412. (txt (format-time-string fmt (apply 'encode-time f)))
  15413. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15414. (txt (concat "=> " txt)))
  15415. (when (and org-end-time-was-given
  15416. (string-match org-plain-time-of-day-regexp txt))
  15417. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15418. org-end-time-was-given
  15419. (substring txt (match-end 0)))))
  15420. (when org-read-date-analyze-futurep
  15421. (setq txt (concat txt " (=>F)")))
  15422. (setq org-read-date-overlay
  15423. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15424. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15425. (defun org-read-date-analyze (ans def defdecode)
  15426. "Analyze the combined answer of the date prompt."
  15427. ;; FIXME: cleanup and comment
  15428. ;; Pass `current-time' result to `decode-time' (instead of calling
  15429. ;; without arguments) so that only `current-time' has to be
  15430. ;; overridden in tests.
  15431. (let ((org-def def)
  15432. (org-defdecode defdecode)
  15433. (nowdecode (decode-time (current-time)))
  15434. delta deltan deltaw deltadef year month day
  15435. hour minute second wday pm h2 m2 tl wday1
  15436. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15437. (setq org-read-date-analyze-futurep nil
  15438. org-read-date-analyze-forced-year nil)
  15439. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15440. (setq ans "+0"))
  15441. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15442. (setq ans (replace-match "" t t ans)
  15443. deltan (car delta)
  15444. deltaw (nth 1 delta)
  15445. deltadef (nth 2 delta)))
  15446. ;; Check if there is an iso week date in there. If yes, store the
  15447. ;; info and postpone interpreting it until the rest of the parsing
  15448. ;; is done.
  15449. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15450. (setq iso-year (when (match-end 1)
  15451. (org-small-year-to-year
  15452. (string-to-number (match-string 1 ans))))
  15453. iso-weekday (when (match-end 3)
  15454. (string-to-number (match-string 3 ans)))
  15455. iso-week (string-to-number (match-string 2 ans)))
  15456. (setq ans (replace-match "" t t ans)))
  15457. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15458. (when (string-match
  15459. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15460. (setq year (if (match-end 2)
  15461. (string-to-number (match-string 2 ans))
  15462. (progn (setq kill-year t)
  15463. (string-to-number (format-time-string "%Y"))))
  15464. month (string-to-number (match-string 3 ans))
  15465. day (string-to-number (match-string 4 ans)))
  15466. (setq year (org-small-year-to-year year))
  15467. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15468. t nil ans)))
  15469. ;; Help matching dotted european dates
  15470. (when (string-match
  15471. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15472. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15473. (setq kill-year t)
  15474. (string-to-number (format-time-string "%Y")))
  15475. day (string-to-number (match-string 1 ans))
  15476. month (string-to-number (match-string 2 ans))
  15477. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15478. t nil ans)))
  15479. ;; Help matching american dates, like 5/30 or 5/30/7
  15480. (when (string-match
  15481. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15482. (setq year (if (match-end 4)
  15483. (string-to-number (match-string 4 ans))
  15484. (progn (setq kill-year t)
  15485. (string-to-number (format-time-string "%Y"))))
  15486. month (string-to-number (match-string 1 ans))
  15487. day (string-to-number (match-string 2 ans)))
  15488. (setq year (org-small-year-to-year year))
  15489. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15490. t nil ans)))
  15491. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15492. ;; If there is a time with am/pm, and *no* time without it, we convert
  15493. ;; so that matching will be successful.
  15494. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15495. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15496. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15497. (setq hour (string-to-number (match-string 1 ans))
  15498. minute (if (match-end 3)
  15499. (string-to-number (match-string 3 ans))
  15500. 0)
  15501. pm (equal ?p
  15502. (string-to-char (downcase (match-string 4 ans)))))
  15503. (if (and (= hour 12) (not pm))
  15504. (setq hour 0)
  15505. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15506. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15507. t t ans))))
  15508. ;; Check if a time range is given as a duration
  15509. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15510. (setq hour (string-to-number (match-string 1 ans))
  15511. h2 (+ hour (string-to-number (match-string 3 ans)))
  15512. minute (string-to-number (match-string 2 ans))
  15513. m2 (+ minute (if (match-end 5) (string-to-number
  15514. (match-string 5 ans))0)))
  15515. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15516. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15517. t t ans)))
  15518. ;; Check if there is a time range
  15519. (when (boundp 'org-end-time-was-given)
  15520. (setq org-time-was-given nil)
  15521. (when (and (string-match org-plain-time-of-day-regexp ans)
  15522. (match-end 8))
  15523. (setq org-end-time-was-given (match-string 8 ans))
  15524. (setq ans (concat (substring ans 0 (match-beginning 7))
  15525. (substring ans (match-end 7))))))
  15526. (setq tl (parse-time-string ans)
  15527. day (or (nth 3 tl) (nth 3 org-defdecode))
  15528. month
  15529. (cond ((nth 4 tl))
  15530. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15531. ;; Day was specified. Make sure DAY+MONTH
  15532. ;; combination happens in the future.
  15533. ((nth 3 tl)
  15534. (setq futurep t)
  15535. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15536. (nth 4 nowdecode)))
  15537. (t (nth 4 org-defdecode)))
  15538. year
  15539. (cond ((and (not kill-year) (nth 5 tl)))
  15540. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15541. ;; Month was guessed in the future and is at least
  15542. ;; equal to NOWDECODE's. Fix year accordingly.
  15543. (futurep
  15544. (if (or (> month (nth 4 nowdecode))
  15545. (>= day (nth 3 nowdecode)))
  15546. (nth 5 nowdecode)
  15547. (1+ (nth 5 nowdecode))))
  15548. ;; Month was specified. Make sure MONTH+YEAR
  15549. ;; combination happens in the future.
  15550. ((nth 4 tl)
  15551. (setq futurep t)
  15552. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15553. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15554. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15555. (t (nth 5 nowdecode))))
  15556. (t (nth 5 org-defdecode)))
  15557. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15558. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15559. second (or (nth 0 tl) 0)
  15560. wday (nth 6 tl))
  15561. (when (and (eq org-read-date-prefer-future 'time)
  15562. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15563. (equal day (nth 3 nowdecode))
  15564. (equal month (nth 4 nowdecode))
  15565. (equal year (nth 5 nowdecode))
  15566. (nth 2 tl)
  15567. (or (< (nth 2 tl) (nth 2 nowdecode))
  15568. (and (= (nth 2 tl) (nth 2 nowdecode))
  15569. (nth 1 tl)
  15570. (< (nth 1 tl) (nth 1 nowdecode)))))
  15571. (setq day (1+ day)
  15572. futurep t))
  15573. ;; Special date definitions below
  15574. (cond
  15575. (iso-week
  15576. ;; There was an iso week
  15577. (require 'cal-iso)
  15578. (setq futurep nil)
  15579. (setq year (or iso-year year)
  15580. day (or iso-weekday wday 1)
  15581. wday nil ; to make sure that the trigger below does not match
  15582. iso-date (calendar-gregorian-from-absolute
  15583. (calendar-iso-to-absolute
  15584. (list iso-week day year))))
  15585. ; FIXME: Should we also push ISO weeks into the future?
  15586. ; (when (and org-read-date-prefer-future
  15587. ; (not iso-year)
  15588. ; (< (calendar-absolute-from-gregorian iso-date)
  15589. ; (time-to-days (current-time))))
  15590. ; (setq year (1+ year)
  15591. ; iso-date (calendar-gregorian-from-absolute
  15592. ; (calendar-iso-to-absolute
  15593. ; (list iso-week day year)))))
  15594. (setq month (car iso-date)
  15595. year (nth 2 iso-date)
  15596. day (nth 1 iso-date)))
  15597. (deltan
  15598. (setq futurep nil)
  15599. (unless deltadef
  15600. ;; Pass `current-time' result to `decode-time' (instead of
  15601. ;; calling without arguments) so that only `current-time' has
  15602. ;; to be overridden in tests.
  15603. (let ((now (decode-time (current-time))))
  15604. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15605. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15606. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15607. ((equal deltaw "m") (setq month (+ month deltan)))
  15608. ((equal deltaw "y") (setq year (+ year deltan)))))
  15609. ((and wday (not (nth 3 tl)))
  15610. ;; Weekday was given, but no day, so pick that day in the week
  15611. ;; on or after the derived date.
  15612. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15613. (unless (equal wday wday1)
  15614. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15615. (when (and (boundp 'org-time-was-given)
  15616. (nth 2 tl))
  15617. (setq org-time-was-given t))
  15618. (when (< year 100) (setq year (+ 2000 year)))
  15619. ;; Check of the date is representable
  15620. (if org-read-date-force-compatible-dates
  15621. (progn
  15622. (when (< year 1970)
  15623. (setq year 1970 org-read-date-analyze-forced-year t))
  15624. (when (> year 2037)
  15625. (setq year 2037 org-read-date-analyze-forced-year t)))
  15626. (condition-case nil
  15627. (ignore (encode-time second minute hour day month year))
  15628. (error
  15629. (setq year (nth 5 org-defdecode))
  15630. (setq org-read-date-analyze-forced-year t))))
  15631. (setq org-read-date-analyze-futurep futurep)
  15632. (list second minute hour day month year)))
  15633. (defvar parse-time-weekdays)
  15634. (defun org-read-date-get-relative (s today default)
  15635. "Check string S for special relative date string.
  15636. TODAY and DEFAULT are internal times, for today and for a default.
  15637. Return shift list (N what def-flag)
  15638. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15639. N is the number of WHATs to shift.
  15640. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15641. the DEFAULT date rather than TODAY."
  15642. (require 'parse-time)
  15643. (when (and
  15644. (string-match
  15645. (concat
  15646. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15647. "\\([0-9]+\\)?"
  15648. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15649. "\\([ \t]\\|$\\)") s)
  15650. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15651. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15652. (string-to-char (substring (match-string 1 s) -1))
  15653. ?+))
  15654. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15655. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15656. (what (if (match-end 3) (match-string 3 s) "d"))
  15657. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15658. (date (if rel default today))
  15659. (wday (nth 6 (decode-time date)))
  15660. delta)
  15661. (if wday1
  15662. (progn
  15663. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15664. (when (= delta 0) (setq delta 7))
  15665. (when (= dir ?-)
  15666. (setq delta (- delta 7))
  15667. (when (= delta 0) (setq delta -7)))
  15668. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15669. (list delta "d" rel))
  15670. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15671. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15672. "Turn a user-specified date into the internal representation.
  15673. The internal representation needed by the calendar is (month day year).
  15674. This is a wrapper to handle the brain-dead convention in calendar that
  15675. user function argument order change dependent on argument order."
  15676. (pcase calendar-date-style
  15677. (`american (list arg1 arg2 arg3))
  15678. (`european (list arg2 arg1 arg3))
  15679. (`iso (list arg2 arg3 arg1))))
  15680. (defun org-eval-in-calendar (form &optional keepdate)
  15681. "Eval FORM in the calendar window and return to current window.
  15682. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15683. (let ((sf (selected-frame))
  15684. (sw (selected-window)))
  15685. (select-window (get-buffer-window "*Calendar*" t))
  15686. (eval form)
  15687. (when (and (not keepdate) (calendar-cursor-to-date))
  15688. (let* ((date (calendar-cursor-to-date))
  15689. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15690. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15691. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15692. (select-window sw)
  15693. (select-frame-set-input-focus sf)))
  15694. (defun org-calendar-select ()
  15695. "Return to `org-read-date' with the date currently selected.
  15696. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15697. (interactive)
  15698. (when (calendar-cursor-to-date)
  15699. (let* ((date (calendar-cursor-to-date))
  15700. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15701. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15702. (when (active-minibuffer-window) (exit-minibuffer))))
  15703. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15704. "Insert a date stamp for the date given by the internal TIME.
  15705. See `format-time-string' for the format of TIME.
  15706. WITH-HM means use the stamp format that includes the time of the day.
  15707. INACTIVE means use square brackets instead of angular ones, so that the
  15708. stamp will not contribute to the agenda.
  15709. PRE and POST are optional strings to be inserted before and after the
  15710. stamp.
  15711. The command returns the inserted time stamp."
  15712. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15713. stamp)
  15714. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15715. (insert-before-markers (or pre ""))
  15716. (when (listp extra)
  15717. (setq extra (car extra))
  15718. (if (and (stringp extra)
  15719. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15720. (setq extra (format "-%02d:%02d"
  15721. (string-to-number (match-string 1 extra))
  15722. (string-to-number (match-string 2 extra))))
  15723. (setq extra nil)))
  15724. (when extra
  15725. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15726. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15727. (insert-before-markers (or post ""))
  15728. (setq org-last-inserted-timestamp stamp)))
  15729. (defun org-toggle-time-stamp-overlays ()
  15730. "Toggle the use of custom time stamp formats."
  15731. (interactive)
  15732. (setq org-display-custom-times (not org-display-custom-times))
  15733. (unless org-display-custom-times
  15734. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15735. (while (setq p (next-single-property-change p 'display))
  15736. (when (and (get-text-property p 'display)
  15737. (eq (get-text-property p 'face) 'org-date))
  15738. (remove-text-properties
  15739. p (setq p (next-single-property-change p 'display))
  15740. '(display t))))
  15741. (set-buffer-modified-p bmp)))
  15742. (org-restart-font-lock)
  15743. (setq org-table-may-need-update t)
  15744. (if org-display-custom-times
  15745. (message "Time stamps are overlaid with custom format")
  15746. (message "Time stamp overlays removed")))
  15747. (defun org-display-custom-time (beg end)
  15748. "Overlay modified time stamp format over timestamp between BEG and END."
  15749. (let* ((ts (buffer-substring beg end))
  15750. t1 with-hm tf time str (off 0))
  15751. (save-match-data
  15752. (setq t1 (org-parse-time-string ts t))
  15753. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15754. (setq off (- (match-end 0) (match-beginning 0)))))
  15755. (setq end (- end off))
  15756. (setq with-hm (and (nth 1 t1) (nth 2 t1))
  15757. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15758. time (org-fix-decoded-time t1)
  15759. str (org-add-props
  15760. (format-time-string
  15761. (substring tf 1 -1) (apply 'encode-time time))
  15762. nil 'mouse-face 'highlight))
  15763. (put-text-property beg end 'display str)))
  15764. (defun org-fix-decoded-time (time)
  15765. "Set 0 instead of nil for the first 6 elements of time.
  15766. Don't touch the rest."
  15767. (let ((n 0))
  15768. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15769. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15770. "Difference between TIMESTAMP-STRING and now in days.
  15771. If SECONDS is non-nil, return the difference in seconds."
  15772. (let ((fdiff (if seconds #'float-time #'time-to-days)))
  15773. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15774. (funcall fdiff (current-time)))))
  15775. (defun org-deadline-close-p (timestamp-string &optional ndays)
  15776. "Is the time in TIMESTAMP-STRING close to the current date?"
  15777. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15778. (and (<= (org-time-stamp-to-now timestamp-string) ndays)
  15779. (not (org-entry-is-done-p))))
  15780. (defun org-get-wdays (ts &optional delay zero-delay)
  15781. "Get the deadline lead time appropriate for timestring TS.
  15782. When DELAY is non-nil, get the delay time for scheduled items
  15783. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15784. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15785. don't try to find the delay cookie in the scheduled timestamp."
  15786. (let ((tv (if delay org-scheduled-delay-days
  15787. org-deadline-warning-days)))
  15788. (cond
  15789. ((or (and delay (< tv 0))
  15790. (and delay zero-delay (<= tv 0))
  15791. (and (not delay) (<= tv 0)))
  15792. ;; Enforce this value no matter what
  15793. (- tv))
  15794. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15795. ;; lead time is specified.
  15796. (floor (* (string-to-number (match-string 1 ts))
  15797. (cdr (assoc (match-string 2 ts)
  15798. '(("d" . 1) ("w" . 7)
  15799. ("m" . 30.4) ("y" . 365.25)
  15800. ("h" . 0.041667)))))))
  15801. ;; go for the default.
  15802. (t tv))))
  15803. (defun org-calendar-select-mouse (ev)
  15804. "Return to `org-read-date' with the date currently selected.
  15805. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15806. (interactive "e")
  15807. (mouse-set-point ev)
  15808. (when (calendar-cursor-to-date)
  15809. (let* ((date (calendar-cursor-to-date))
  15810. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15811. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15812. (when (active-minibuffer-window) (exit-minibuffer))))
  15813. (defun org-check-deadlines (ndays)
  15814. "Check if there are any deadlines due or past due.
  15815. A deadline is considered due if it happens within `org-deadline-warning-days'
  15816. days from today's date. If the deadline appears in an entry marked DONE,
  15817. it is not shown. A numeric prefix argument NDAYS can be used to test that
  15818. many days. If the prefix is a raw `\\[universal-argument]', all deadlines \
  15819. are shown."
  15820. (interactive "P")
  15821. (let* ((org-warn-days
  15822. (cond
  15823. ((equal ndays '(4)) 100000)
  15824. (ndays (prefix-numeric-value ndays))
  15825. (t (abs org-deadline-warning-days))))
  15826. (case-fold-search nil)
  15827. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15828. (callback
  15829. (lambda () (org-deadline-close-p (match-string 1) org-warn-days))))
  15830. (message "%d deadlines past-due or due within %d days"
  15831. (org-occur regexp nil callback)
  15832. org-warn-days)))
  15833. (defsubst org-re-timestamp (type)
  15834. "Return a regexp for timestamp TYPE.
  15835. Allowed values for TYPE are:
  15836. all: all timestamps
  15837. active: only active timestamps (<...>)
  15838. inactive: only inactive timestamps ([...])
  15839. scheduled: only scheduled timestamps
  15840. deadline: only deadline timestamps
  15841. closed: only closed time-stamps
  15842. When TYPE is nil, fall back on returning a regexp that matches
  15843. both scheduled and deadline timestamps."
  15844. (cl-case type
  15845. (all org-ts-regexp-both)
  15846. (active org-ts-regexp)
  15847. (inactive org-ts-regexp-inactive)
  15848. (scheduled org-scheduled-time-regexp)
  15849. (deadline org-deadline-time-regexp)
  15850. (closed org-closed-time-regexp)
  15851. (otherwise
  15852. (concat "\\<"
  15853. (regexp-opt (list org-deadline-string org-scheduled-string))
  15854. " *<\\([^>]+\\)>"))))
  15855. (defun org-check-before-date (d)
  15856. "Check if there are deadlines or scheduled entries before date D."
  15857. (interactive (list (org-read-date)))
  15858. (let* ((case-fold-search nil)
  15859. (regexp (org-re-timestamp org-ts-type))
  15860. (ts-type org-ts-type)
  15861. (callback
  15862. (lambda ()
  15863. (let ((match (match-string 1)))
  15864. (and (if (memq ts-type '(active inactive all))
  15865. (eq (org-element-type (save-excursion
  15866. (backward-char)
  15867. (org-element-context)))
  15868. 'timestamp)
  15869. (org-at-planning-p))
  15870. (time-less-p
  15871. (org-time-string-to-time match)
  15872. (org-time-string-to-time d)))))))
  15873. (message "%d entries before %s"
  15874. (org-occur regexp nil callback)
  15875. d)))
  15876. (defun org-check-after-date (d)
  15877. "Check if there are deadlines or scheduled entries after date D."
  15878. (interactive (list (org-read-date)))
  15879. (let* ((case-fold-search nil)
  15880. (regexp (org-re-timestamp org-ts-type))
  15881. (ts-type org-ts-type)
  15882. (callback
  15883. (lambda ()
  15884. (let ((match (match-string 1)))
  15885. (and (if (memq ts-type '(active inactive all))
  15886. (eq (org-element-type (save-excursion
  15887. (backward-char)
  15888. (org-element-context)))
  15889. 'timestamp)
  15890. (org-at-planning-p))
  15891. (not (time-less-p
  15892. (org-time-string-to-time match)
  15893. (org-time-string-to-time d))))))))
  15894. (message "%d entries after %s"
  15895. (org-occur regexp nil callback)
  15896. d)))
  15897. (defun org-check-dates-range (start-date end-date)
  15898. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15899. (interactive (list (org-read-date nil nil nil "Range starts")
  15900. (org-read-date nil nil nil "Range end")))
  15901. (let ((case-fold-search nil)
  15902. (regexp (org-re-timestamp org-ts-type))
  15903. (callback
  15904. (let ((type org-ts-type))
  15905. (lambda ()
  15906. (let ((match (match-string 1)))
  15907. (and
  15908. (if (memq type '(active inactive all))
  15909. (eq (org-element-type (save-excursion
  15910. (backward-char)
  15911. (org-element-context)))
  15912. 'timestamp)
  15913. (org-at-planning-p))
  15914. (not (time-less-p
  15915. (org-time-string-to-time match)
  15916. (org-time-string-to-time start-date)))
  15917. (time-less-p
  15918. (org-time-string-to-time match)
  15919. (org-time-string-to-time end-date))))))))
  15920. (message "%d entries between %s and %s"
  15921. (org-occur regexp nil callback) start-date end-date)))
  15922. (defun org-evaluate-time-range (&optional to-buffer)
  15923. "Evaluate a time range by computing the difference between start and end.
  15924. Normally the result is just printed in the echo area, but with prefix arg
  15925. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15926. If the time range is actually in a table, the result is inserted into the
  15927. next column.
  15928. For time difference computation, a year is assumed to be exactly 365
  15929. days in order to avoid rounding problems."
  15930. (interactive "P")
  15931. (or
  15932. (org-clock-update-time-maybe)
  15933. (save-excursion
  15934. (unless (org-at-date-range-p t)
  15935. (goto-char (point-at-bol))
  15936. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15937. (unless (org-at-date-range-p t)
  15938. (user-error "Not at a time-stamp range, and none found in current line")))
  15939. (let* ((ts1 (match-string 1))
  15940. (ts2 (match-string 2))
  15941. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15942. (match-end (match-end 0))
  15943. (time1 (org-time-string-to-time ts1))
  15944. (time2 (org-time-string-to-time ts2))
  15945. (t1 (float-time time1))
  15946. (t2 (float-time time2))
  15947. (diff (abs (- t2 t1)))
  15948. (negative (< (- t2 t1) 0))
  15949. ;; (ys (floor (* 365 24 60 60)))
  15950. (ds (* 24 60 60))
  15951. (hs (* 60 60))
  15952. (fy "%dy %dd %02d:%02d")
  15953. (fy1 "%dy %dd")
  15954. (fd "%dd %02d:%02d")
  15955. (fd1 "%dd")
  15956. (fh "%02d:%02d")
  15957. y d h m align)
  15958. (if havetime
  15959. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15960. y 0
  15961. d (floor (/ diff ds)) diff (mod diff ds)
  15962. h (floor (/ diff hs)) diff (mod diff hs)
  15963. m (floor (/ diff 60)))
  15964. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15965. y 0
  15966. d (floor (+ (/ diff ds) 0.5))
  15967. h 0 m 0))
  15968. (if (not to-buffer)
  15969. (message "%s" (org-make-tdiff-string y d h m))
  15970. (if (org-at-table-p)
  15971. (progn
  15972. (goto-char match-end)
  15973. (setq align t)
  15974. (and (looking-at " *|") (goto-char (match-end 0))))
  15975. (goto-char match-end))
  15976. (when (looking-at
  15977. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15978. (replace-match ""))
  15979. (when negative (insert " -"))
  15980. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15981. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15982. (insert " " (format fh h m))))
  15983. (when align (org-table-align))
  15984. (message "Time difference inserted")))))
  15985. (defun org-make-tdiff-string (y d h m)
  15986. (let ((fmt "")
  15987. (l nil))
  15988. (when (> y 0)
  15989. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  15990. (push y l))
  15991. (when (> d 0)
  15992. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  15993. (push d l))
  15994. (when (> h 0)
  15995. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  15996. (push h l))
  15997. (when (> m 0)
  15998. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  15999. (push m l))
  16000. (apply 'format fmt (nreverse l))))
  16001. (defun org-time-string-to-time (s)
  16002. "Convert timestamp string S into internal time."
  16003. (apply #'encode-time (org-parse-time-string s)))
  16004. (defun org-time-string-to-seconds (s)
  16005. "Convert a timestamp string S into a number of seconds."
  16006. (float-time (org-time-string-to-time s)))
  16007. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16008. (defun org-time-string-to-absolute (s &optional daynr prefer buffer pos)
  16009. "Convert time stamp S to an absolute day number.
  16010. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16011. stamp, get the closest date to DAYNR. If PREFER is
  16012. `past' (respectively `future') return a date past (respectively
  16013. after) or equal to DAYNR.
  16014. POS is the location of time stamp S, as a buffer position in
  16015. BUFFER.
  16016. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16017. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16018. signaled."
  16019. (cond
  16020. ((string-match "\\`%%\\((.*)\\)" s)
  16021. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16022. ;; only able to match individual dates. If it fails, raise an
  16023. ;; error.
  16024. (if (and daynr
  16025. (org-diary-sexp-entry
  16026. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16027. daynr
  16028. (signal 'org-diary-sexp-no-match (list s))))
  16029. (daynr (org-closest-date s daynr prefer))
  16030. (t (time-to-days
  16031. (condition-case errdata
  16032. (apply #'encode-time (org-parse-time-string s))
  16033. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16034. s
  16035. (if (not (and buffer pos)) ""
  16036. (format-message " at %d in buffer `%s'" pos buffer))
  16037. (cdr errdata))))))))
  16038. (defun org-days-to-iso-week (days)
  16039. "Return the iso week number."
  16040. (require 'cal-iso)
  16041. (car (calendar-iso-from-absolute days)))
  16042. (defun org-small-year-to-year (year)
  16043. "Convert 2-digit years into 4-digit years.
  16044. YEAR is expanded into one of the 30 next years, if possible, or
  16045. into a past one. Any year larger than 99 is returned unchanged."
  16046. (if (>= year 100) year
  16047. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16048. (century (/ current 100))
  16049. (offset (- year (% current 100))))
  16050. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16051. ((> offset -70) (+ (* century 100) year))
  16052. (t (+ (* (1+ century) 100) year))))))
  16053. (defun org-time-from-absolute (d)
  16054. "Return the time corresponding to date D.
  16055. D may be an absolute day number, or a calendar-type list (month day year)."
  16056. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16057. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16058. (defvar org-agenda-current-date)
  16059. (defun org-calendar-holiday ()
  16060. "List of holidays, for Diary display in Org mode."
  16061. (require 'holidays)
  16062. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16063. (and hl (mapconcat #'identity hl "; "))))
  16064. (defun org-diary-sexp-entry (sexp entry d)
  16065. "Process a SEXP diary ENTRY for date D."
  16066. (require 'diary-lib)
  16067. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16068. ;; dynamically.
  16069. (let* ((sexp `(let ((entry ,entry)
  16070. (date ',d))
  16071. ,(car (read-from-string sexp))))
  16072. (result (if calendar-debug-sexp (eval sexp)
  16073. (condition-case nil
  16074. (eval sexp)
  16075. (error
  16076. (beep)
  16077. (message "Bad sexp at line %d in %s: %s"
  16078. (org-current-line)
  16079. (buffer-file-name) sexp)
  16080. (sleep-for 2))))))
  16081. (cond ((stringp result) (split-string result "; "))
  16082. ((and (consp result)
  16083. (not (consp (cdr result)))
  16084. (stringp (cdr result))) (cdr result))
  16085. ((and (consp result)
  16086. (stringp (car result))) result)
  16087. (result entry))))
  16088. (defun org-diary-to-ical-string (frombuf)
  16089. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16090. This uses the icalendar.el library."
  16091. (let* ((tmpdir temporary-file-directory)
  16092. (tmpfile (make-temp-name
  16093. (expand-file-name "orgics" tmpdir)))
  16094. buf rtn b e)
  16095. (with-current-buffer frombuf
  16096. (icalendar-export-region (point-min) (point-max) tmpfile)
  16097. (setq buf (find-buffer-visiting tmpfile))
  16098. (set-buffer buf)
  16099. (goto-char (point-min))
  16100. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16101. (setq b (match-beginning 0)))
  16102. (goto-char (point-max))
  16103. (when (re-search-backward "^END:VEVENT" nil t)
  16104. (setq e (match-end 0)))
  16105. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16106. (kill-buffer buf)
  16107. (delete-file tmpfile)
  16108. rtn))
  16109. (defun org-closest-date (start current prefer)
  16110. "Return closest date to CURRENT starting from START.
  16111. CURRENT and START are both time stamps.
  16112. When PREFER is `past', return a date that is either CURRENT or
  16113. past. When PREFER is `future', return a date that is either
  16114. CURRENT or future.
  16115. Only time stamps with a repeater are modified. Any other time
  16116. stamp stay unchanged. In any case, return value is an absolute
  16117. day number."
  16118. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16119. ;; No repeater. Do not shift time stamp.
  16120. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16121. (let ((value (string-to-number (match-string 1 start)))
  16122. (type (match-string 2 start)))
  16123. (if (= 0 value)
  16124. ;; Repeater with a 0-value is considered as void.
  16125. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16126. (let* ((base (org-date-to-gregorian start))
  16127. (target (org-date-to-gregorian current))
  16128. (sday (calendar-absolute-from-gregorian base))
  16129. (cday (calendar-absolute-from-gregorian target))
  16130. n1 n2)
  16131. ;; If START is already past CURRENT, just return START.
  16132. (if (<= cday sday) sday
  16133. ;; Compute closest date before (N1) and closest date past
  16134. ;; (N2) CURRENT.
  16135. (pcase type
  16136. ("h"
  16137. (let ((missing-hours
  16138. (mod (+ (- (* 24 (- cday sday))
  16139. (nth 2 (org-parse-time-string start)))
  16140. org-extend-today-until)
  16141. value)))
  16142. (setf n1 (if (= missing-hours 0) cday
  16143. (- cday (1+ (/ missing-hours 24)))))
  16144. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16145. ((or "d" "w")
  16146. (let ((value (if (equal type "w") (* 7 value) value)))
  16147. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16148. (setf n2 (+ n1 value))))
  16149. ("m"
  16150. (let* ((add-months
  16151. (lambda (d n)
  16152. ;; Add N months to gregorian date D, i.e.,
  16153. ;; a list (MONTH DAY YEAR). Return a valid
  16154. ;; gregorian date.
  16155. (let ((m (+ (nth 0 d) n)))
  16156. (list (mod m 12)
  16157. (nth 1 d)
  16158. (+ (/ m 12) (nth 2 d))))))
  16159. (months ; Complete months to TARGET.
  16160. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16161. (- (nth 0 target) (nth 0 base))
  16162. ;; If START's day is greater than
  16163. ;; TARGET's, remove incomplete month.
  16164. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16165. value)
  16166. value))
  16167. (before (funcall add-months base months)))
  16168. (setf n1 (calendar-absolute-from-gregorian before))
  16169. (setf n2
  16170. (calendar-absolute-from-gregorian
  16171. (funcall add-months before value)))))
  16172. (_
  16173. (let* ((d (nth 1 base))
  16174. (m (nth 0 base))
  16175. (y (nth 2 base))
  16176. (years ; Complete years to TARGET.
  16177. (* (/ (- (nth 2 target)
  16178. y
  16179. ;; If START's month and day are
  16180. ;; greater than TARGET's, remove
  16181. ;; incomplete year.
  16182. (if (or (> (nth 0 target) m)
  16183. (and (= (nth 0 target) m)
  16184. (> (nth 1 target) d)))
  16185. 0
  16186. 1))
  16187. value)
  16188. value))
  16189. (before (list m d (+ y years))))
  16190. (setf n1 (calendar-absolute-from-gregorian before))
  16191. (setf n2 (calendar-absolute-from-gregorian
  16192. (list m d (+ (nth 2 before) value)))))))
  16193. ;; Handle PREFER parameter, if any.
  16194. (cond
  16195. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16196. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16197. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16198. (defun org-date-to-gregorian (d)
  16199. "Turn any specification of date D into a Gregorian date for the calendar."
  16200. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16201. ((and (listp d) (= (length d) 3)) d)
  16202. ((stringp d)
  16203. (let ((d (org-parse-time-string d)))
  16204. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16205. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16206. (defun org-parse-time-string (s &optional nodefault)
  16207. "Parse the standard Org time string.
  16208. This should be a lot faster than the normal `parse-time-string'.
  16209. If time is not given, defaults to 0:00. However, with optional
  16210. NODEFAULT, hour and minute fields will be nil if not given."
  16211. (cond ((string-match org-ts-regexp0 s)
  16212. (list 0
  16213. (when (or (match-beginning 8) (not nodefault))
  16214. (string-to-number (or (match-string 8 s) "0")))
  16215. (when (or (match-beginning 7) (not nodefault))
  16216. (string-to-number (or (match-string 7 s) "0")))
  16217. (string-to-number (match-string 4 s))
  16218. (string-to-number (match-string 3 s))
  16219. (string-to-number (match-string 2 s))
  16220. nil nil nil))
  16221. ((string-match "^<[^>]+>$" s)
  16222. ;; FIXME: `decode-time' needs to be called with ZONE as its
  16223. ;; second argument. However, this requires at least Emacs
  16224. ;; 25.1. We can do it when we switch to this version as our
  16225. ;; minimal requirement.
  16226. (decode-time (seconds-to-time (org-matcher-time s))))
  16227. (t (error "Not a standard Org time string: %s" s))))
  16228. (defun org-timestamp-up (&optional arg)
  16229. "Increase the date item at the cursor by one.
  16230. If the cursor is on the year, change the year. If it is on the month,
  16231. the day or the time, change that.
  16232. With prefix ARG, change by that many units."
  16233. (interactive "p")
  16234. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16235. (defun org-timestamp-down (&optional arg)
  16236. "Decrease the date item at the cursor by one.
  16237. If the cursor is on the year, change the year. If it is on the month,
  16238. the day or the time, change that.
  16239. With prefix ARG, change by that many units."
  16240. (interactive "p")
  16241. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16242. (defun org-timestamp-up-day (&optional arg)
  16243. "Increase the date in the time stamp by one day.
  16244. With prefix ARG, change that many days."
  16245. (interactive "p")
  16246. (if (and (not (org-at-timestamp-p 'lax))
  16247. (org-at-heading-p))
  16248. (org-todo 'up)
  16249. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16250. (defun org-timestamp-down-day (&optional arg)
  16251. "Decrease the date in the time stamp by one day.
  16252. With prefix ARG, change that many days."
  16253. (interactive "p")
  16254. (if (and (not (org-at-timestamp-p 'lax))
  16255. (org-at-heading-p))
  16256. (org-todo 'down)
  16257. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16258. (defun org-at-timestamp-p (&optional extended)
  16259. "Non-nil if point is inside a timestamp.
  16260. By default, the function only consider syntactically valid active
  16261. timestamps. However, the caller may have a broader definition
  16262. for timestamps. As a consequence, optional argument EXTENDED can
  16263. be set to the following values
  16264. `inactive'
  16265. Include also syntactically valid inactive timestamps.
  16266. `agenda'
  16267. Include timestamps allowed in Agenda, i.e., those in
  16268. properties drawers, planning lines and clock lines.
  16269. `lax'
  16270. Ignore context. The function matches any part of the
  16271. document looking like a timestamp. This includes comments,
  16272. example blocks...
  16273. For backward-compatibility with Org 9.0, every other non-nil
  16274. value is equivalent to `inactive'.
  16275. When at a timestamp, return the position of the point as a symbol
  16276. among `bracket', `after', `year', `month', `hour', `minute',
  16277. `day' or a number of character from the last know part of the
  16278. time stamp.
  16279. When matching, the match groups are the following:
  16280. group 1: year
  16281. group 2: month
  16282. group 3: day number
  16283. group 4: day name
  16284. group 5: hours, if any
  16285. group 6: minutes, if any"
  16286. (let* ((regexp (if extended org-ts-regexp3 org-ts-regexp2))
  16287. (pos (point))
  16288. (match?
  16289. (let ((boundaries (org-in-regexp regexp)))
  16290. (save-match-data
  16291. (cond ((null boundaries) nil)
  16292. ((eq extended 'lax) t)
  16293. (t
  16294. (or (and (eq extended 'agenda)
  16295. (or (org-at-planning-p)
  16296. (org-at-property-p)
  16297. (and (bound-and-true-p
  16298. org-agenda-include-inactive-timestamps)
  16299. (org-at-clock-log-p))))
  16300. (eq 'timestamp
  16301. (save-excursion
  16302. (when (= pos (cdr boundaries)) (forward-char -1))
  16303. (org-element-type (org-element-context)))))))))))
  16304. (cond
  16305. ((not match?) nil)
  16306. ((= pos (match-beginning 0)) 'bracket)
  16307. ;; Distinguish location right before the closing bracket from
  16308. ;; right after it.
  16309. ((= pos (1- (match-end 0))) 'bracket)
  16310. ((= pos (match-end 0)) 'after)
  16311. ((org-pos-in-match-range pos 2) 'year)
  16312. ((org-pos-in-match-range pos 3) 'month)
  16313. ((org-pos-in-match-range pos 7) 'hour)
  16314. ((org-pos-in-match-range pos 8) 'minute)
  16315. ((or (org-pos-in-match-range pos 4)
  16316. (org-pos-in-match-range pos 5)) 'day)
  16317. ((and (> pos (or (match-end 8) (match-end 5)))
  16318. (< pos (match-end 0)))
  16319. (- pos (or (match-end 8) (match-end 5))))
  16320. (t 'day))))
  16321. (defun org-toggle-timestamp-type ()
  16322. "Toggle the type (<active> or [inactive]) of a time stamp."
  16323. (interactive)
  16324. (when (org-at-timestamp-p 'lax)
  16325. (let ((beg (match-beginning 0)) (end (match-end 0))
  16326. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16327. (save-excursion
  16328. (goto-char beg)
  16329. (while (re-search-forward "[][<>]" end t)
  16330. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16331. t t)))
  16332. (message "Timestamp is now %sactive"
  16333. (if (equal (char-after beg) ?<) "" "in")))))
  16334. (defun org-at-clock-log-p ()
  16335. "Non-nil if point is on a clock log line."
  16336. (and (org-match-line org-clock-line-re)
  16337. (eq (org-element-type (save-match-data (org-element-at-point))) 'clock)))
  16338. (defvar org-clock-history) ; defined in org-clock.el
  16339. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16340. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16341. "Change the date in the time stamp at point.
  16342. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16343. `year', `minute', `second'. If WHAT is not given, the cursor position
  16344. in the timestamp determines what will be changed.
  16345. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16346. (let ((origin (point))
  16347. (timestamp? (org-at-timestamp-p 'lax))
  16348. origin-cat
  16349. with-hm inactive
  16350. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16351. extra rem
  16352. ts time time0 fixnext clrgx)
  16353. (unless timestamp? (user-error "Not at a timestamp"))
  16354. (if (and (not what) (eq timestamp? 'bracket))
  16355. (org-toggle-timestamp-type)
  16356. ;; Point isn't on brackets. Remember the part of the time-stamp
  16357. ;; the point was in. Indeed, size of time-stamps may change,
  16358. ;; but point must be kept in the same category nonetheless.
  16359. (setq origin-cat timestamp?)
  16360. (when (and (not what) (not (eq timestamp? 'day))
  16361. org-display-custom-times
  16362. (get-text-property (point) 'display)
  16363. (not (get-text-property (1- (point)) 'display)))
  16364. (setq timestamp? 'day))
  16365. (setq timestamp? (or what timestamp?)
  16366. inactive (= (char-after (match-beginning 0)) ?\[)
  16367. ts (match-string 0))
  16368. (replace-match "")
  16369. (when (string-match
  16370. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16371. ts)
  16372. (setq extra (match-string 1 ts))
  16373. (when suppress-tmp-delay
  16374. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16375. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16376. (setq with-hm t))
  16377. (setq time0 (org-parse-time-string ts))
  16378. (when (and updown
  16379. (eq timestamp? 'minute)
  16380. (not current-prefix-arg))
  16381. ;; This looks like s-up and s-down. Change by one rounding step.
  16382. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16383. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16384. (setcar (cdr time0) (+ (nth 1 time0)
  16385. (if (> n 0) (- rem) (- dm rem))))))
  16386. (setq time
  16387. (apply #'encode-time
  16388. (or (car time0) 0)
  16389. (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
  16390. (+ (if (eq timestamp? 'hour) n 0) (nth 2 time0))
  16391. (+ (if (eq timestamp? 'day) n 0) (nth 3 time0))
  16392. (+ (if (eq timestamp? 'month) n 0) (nth 4 time0))
  16393. (+ (if (eq timestamp? 'year) n 0) (nth 5 time0))
  16394. (nthcdr 6 time0)))
  16395. (when (and (memq timestamp? '(hour minute))
  16396. extra
  16397. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16398. (setq extra (org-modify-ts-extra
  16399. extra
  16400. (if (eq timestamp? 'hour) 2 5)
  16401. n dm)))
  16402. (when (integerp timestamp?)
  16403. (setq extra (org-modify-ts-extra extra timestamp? n dm)))
  16404. (when (eq what 'calendar)
  16405. (let ((cal-date (org-get-date-from-calendar)))
  16406. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16407. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16408. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16409. (setcar time0 (or (car time0) 0))
  16410. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16411. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16412. (setq time (apply 'encode-time time0))))
  16413. ;; Insert the new time-stamp, and ensure point stays in the same
  16414. ;; category as before (i.e. not after the last position in that
  16415. ;; category).
  16416. (let ((pos (point)))
  16417. ;; Stay before inserted string. `save-excursion' is of no use.
  16418. (setq org-last-changed-timestamp
  16419. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16420. (goto-char pos))
  16421. (save-match-data
  16422. (looking-at org-ts-regexp3)
  16423. (goto-char
  16424. (pcase origin-cat
  16425. ;; `day' category ends before `hour' if any, or at the end
  16426. ;; of the day name.
  16427. (`day (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16428. (`hour (min (match-end 7) origin))
  16429. (`minute (min (1- (match-end 8)) origin))
  16430. ((pred integerp) (min (1- (match-end 0)) origin))
  16431. ;; Point was right after the time-stamp. However, the
  16432. ;; time-stamp length might have changed, so refer to
  16433. ;; (match-end 0) instead.
  16434. (`after (match-end 0))
  16435. ;; `year' and `month' have both fixed size: point couldn't
  16436. ;; have moved into another part.
  16437. (_ origin))))
  16438. ;; Update clock if on a CLOCK line.
  16439. (org-clock-update-time-maybe)
  16440. ;; Maybe adjust the closest clock in `org-clock-history'
  16441. (when org-clock-adjust-closest
  16442. (if (not (and (org-at-clock-log-p)
  16443. (< 1 (length (delq nil (mapcar 'marker-position
  16444. org-clock-history))))))
  16445. (message "No clock to adjust")
  16446. (cond ((save-excursion ; fix previous clock?
  16447. (re-search-backward org-ts-regexp0 nil t)
  16448. (looking-back (concat org-clock-string " \\[")
  16449. (line-beginning-position)))
  16450. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16451. ((save-excursion ; fix next clock?
  16452. (re-search-backward org-ts-regexp0 nil t)
  16453. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16454. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16455. (save-window-excursion
  16456. ;; Find closest clock to point, adjust the previous/next one in history
  16457. (let* ((p (save-excursion (org-back-to-heading t)))
  16458. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16459. (clfixnth
  16460. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16461. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16462. (if (not clfixpos)
  16463. (message "No clock to adjust")
  16464. (save-excursion
  16465. (org-goto-marker-or-bmk clfixpos)
  16466. (org-show-subtree)
  16467. (when (re-search-forward clrgx nil t)
  16468. (goto-char (match-beginning 1))
  16469. (let (org-clock-adjust-closest)
  16470. (org-timestamp-change n timestamp? updown))
  16471. (message "Clock adjusted in %s for heading: %s"
  16472. (file-name-nondirectory (buffer-file-name))
  16473. (org-get-heading t t)))))))))
  16474. ;; Try to recenter the calendar window, if any.
  16475. (when (and org-calendar-follow-timestamp-change
  16476. (get-buffer-window "*Calendar*" t)
  16477. (memq timestamp? '(day month year)))
  16478. (org-recenter-calendar (time-to-days time))))))
  16479. (defun org-modify-ts-extra (s pos n dm)
  16480. "Change the different parts of the lead-time and repeat fields in timestamp."
  16481. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16482. ng h m new rem)
  16483. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16484. (cond
  16485. ((or (org-pos-in-match-range pos 2)
  16486. (org-pos-in-match-range pos 3))
  16487. (setq m (string-to-number (match-string 3 s))
  16488. h (string-to-number (match-string 2 s)))
  16489. (if (org-pos-in-match-range pos 2)
  16490. (setq h (+ h n))
  16491. (setq n (* dm (with-no-warnings (signum n))))
  16492. (unless (= 0 (setq rem (% m dm)))
  16493. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16494. (setq m (+ m n)))
  16495. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16496. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16497. (setq h (mod h 24))
  16498. (setq ng 1 new (format "-%02d:%02d" h m)))
  16499. ((org-pos-in-match-range pos 6)
  16500. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16501. ((org-pos-in-match-range pos 5)
  16502. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16503. ((org-pos-in-match-range pos 9)
  16504. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16505. ((org-pos-in-match-range pos 8)
  16506. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16507. (when ng
  16508. (setq s (concat
  16509. (substring s 0 (match-beginning ng))
  16510. new
  16511. (substring s (match-end ng))))))
  16512. s))
  16513. (defun org-recenter-calendar (d)
  16514. "If the calendar is visible, recenter it to date D."
  16515. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16516. (when cwin
  16517. (let ((calendar-move-hook nil))
  16518. (with-selected-window cwin
  16519. (calendar-goto-date
  16520. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16521. (defun org-goto-calendar (&optional arg)
  16522. "Go to the Emacs calendar at the current date.
  16523. If there is a time stamp in the current line, go to that date.
  16524. A prefix ARG can be used to force the current date."
  16525. (interactive "P")
  16526. (let ((calendar-move-hook nil)
  16527. (calendar-view-holidays-initially-flag nil)
  16528. (calendar-view-diary-initially-flag nil)
  16529. diff)
  16530. (when (or (org-at-timestamp-p 'lax)
  16531. (org-match-line (concat ".*" org-ts-regexp)))
  16532. (let ((d1 (time-to-days (current-time)))
  16533. (d2 (time-to-days (org-time-string-to-time (match-string 1)))))
  16534. (setq diff (- d2 d1))))
  16535. (calendar)
  16536. (calendar-goto-today)
  16537. (when (and diff (not arg)) (calendar-forward-day diff))))
  16538. (defun org-get-date-from-calendar ()
  16539. "Return a list (month day year) of date at point in calendar."
  16540. (with-current-buffer "*Calendar*"
  16541. (save-match-data
  16542. (calendar-cursor-to-date))))
  16543. (defun org-date-from-calendar ()
  16544. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16545. If there is already a time stamp at the cursor position, update it."
  16546. (interactive)
  16547. (if (org-at-timestamp-p 'lax)
  16548. (org-timestamp-change 0 'calendar)
  16549. (let ((cal-date (org-get-date-from-calendar)))
  16550. (org-insert-time-stamp
  16551. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16552. (defcustom org-effort-durations
  16553. `(("min" . 1)
  16554. ("h" . 60)
  16555. ("d" . ,(* 60 8))
  16556. ("w" . ,(* 60 8 5))
  16557. ("m" . ,(* 60 8 5 4))
  16558. ("y" . ,(* 60 8 5 40)))
  16559. "Conversion factor to minutes for an effort modifier.
  16560. Each entry has the form (MODIFIER . MINUTES).
  16561. In an effort string, a number followed by MODIFIER is multiplied
  16562. by the specified number of MINUTES to obtain an effort in
  16563. minutes.
  16564. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16565. effort string \"2hours\" is equivalent to 120 minutes."
  16566. :group 'org-agenda
  16567. :version "26.1"
  16568. :package-version '(Org . "8.3")
  16569. :type '(alist :key-type (string :tag "Modifier")
  16570. :value-type (number :tag "Minutes")))
  16571. (defcustom org-image-actual-width t
  16572. "Should we use the actual width of images when inlining them?
  16573. When set to t, always use the image width.
  16574. When set to a number, use imagemagick (when available) to set
  16575. the image's width to this value.
  16576. When set to a number in a list, try to get the width from any
  16577. #+ATTR.* keyword if it matches a width specification like
  16578. #+ATTR_HTML: :width 300px
  16579. and fall back on that number if none is found.
  16580. When set to nil, try to get the width from an #+ATTR.* keyword
  16581. and fall back on the original width if none is found.
  16582. This requires Emacs >= 24.1, build with imagemagick support."
  16583. :group 'org-appearance
  16584. :version "24.4"
  16585. :package-version '(Org . "8.0")
  16586. :type '(choice
  16587. (const :tag "Use the image width" t)
  16588. (integer :tag "Use a number of pixels")
  16589. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16590. (const :tag "Use #+ATTR* or don't resize" nil)))
  16591. (defcustom org-agenda-inhibit-startup nil
  16592. "Inhibit startup when preparing agenda buffers.
  16593. When this variable is t, the initialization of the Org agenda
  16594. buffers is inhibited: e.g. the visibility state is not set, the
  16595. tables are not re-aligned, etc."
  16596. :type 'boolean
  16597. :version "24.3"
  16598. :group 'org-agenda)
  16599. (defcustom org-agenda-ignore-properties nil
  16600. "Avoid updating text properties when building the agenda.
  16601. Properties are used to prepare buffers for effort estimates,
  16602. appointments, statistics and subtree-local categories.
  16603. If you don't use these in the agenda, you can add them to this
  16604. list and agenda building will be a bit faster.
  16605. The value is a list, with zero or more of the symbols `effort', `appt',
  16606. `stats' or `category'."
  16607. :type '(set :greedy t
  16608. (const effort)
  16609. (const appt)
  16610. (const stats)
  16611. (const category))
  16612. :version "26.1"
  16613. :package-version '(Org . "8.3")
  16614. :group 'org-agenda)
  16615. ;;;; Files
  16616. (defun org-save-all-org-buffers ()
  16617. "Save all Org buffers without user confirmation."
  16618. (interactive)
  16619. (message "Saving all Org buffers...")
  16620. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16621. (when (featurep 'org-id) (org-id-locations-save))
  16622. (message "Saving all Org buffers... done"))
  16623. (defun org-revert-all-org-buffers ()
  16624. "Revert all Org buffers.
  16625. Prompt for confirmation when there are unsaved changes.
  16626. Be sure you know what you are doing before letting this function
  16627. overwrite your changes.
  16628. This function is useful in a setup where one tracks org files
  16629. with a version control system, to revert on one machine after pulling
  16630. changes from another. I believe the procedure must be like this:
  16631. 1. M-x org-save-all-org-buffers
  16632. 2. Pull changes from the other machine, resolve conflicts
  16633. 3. M-x org-revert-all-org-buffers"
  16634. (interactive)
  16635. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16636. (user-error "Abort"))
  16637. (save-excursion
  16638. (save-window-excursion
  16639. (dolist (b (buffer-list))
  16640. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16641. (with-current-buffer b buffer-file-name))
  16642. (pop-to-buffer-same-window b)
  16643. (revert-buffer t 'no-confirm)))
  16644. (when (and (featurep 'org-id) org-id-track-globally)
  16645. (org-id-locations-load)))))
  16646. ;;;; Agenda files
  16647. ;;;###autoload
  16648. (defun org-switchb (&optional arg)
  16649. "Switch between Org buffers.
  16650. With `\\[universal-argument]' prefix, restrict available buffers to files.
  16651. With `\\[universal-argument] \\[universal-argument]' \
  16652. prefix, restrict available buffers to agenda files."
  16653. (interactive "P")
  16654. (let ((blist (org-buffer-list
  16655. (cond ((equal arg '(4)) 'files)
  16656. ((equal arg '(16)) 'agenda)))))
  16657. (pop-to-buffer-same-window
  16658. (completing-read "Org buffer: "
  16659. (mapcar #'list (mapcar #'buffer-name blist))
  16660. nil t))))
  16661. (defun org-buffer-list (&optional predicate exclude-tmp)
  16662. "Return a list of Org buffers.
  16663. PREDICATE can be `export', `files' or `agenda'.
  16664. export restrict the list to Export buffers.
  16665. files restrict the list to buffers visiting Org files.
  16666. agenda restrict the list to buffers visiting agenda files.
  16667. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16668. (let* ((bfn nil)
  16669. (agenda-files (and (eq predicate 'agenda)
  16670. (mapcar 'file-truename (org-agenda-files t))))
  16671. (filter
  16672. (cond
  16673. ((eq predicate 'files)
  16674. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16675. ((eq predicate 'export)
  16676. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16677. ((eq predicate 'agenda)
  16678. (lambda (b)
  16679. (with-current-buffer b
  16680. (and (derived-mode-p 'org-mode)
  16681. (setq bfn (buffer-file-name b))
  16682. (member (file-truename bfn) agenda-files)))))
  16683. (t (lambda (b) (with-current-buffer b
  16684. (or (derived-mode-p 'org-mode)
  16685. (string-match "\\*Org .*Export"
  16686. (buffer-name b)))))))))
  16687. (delq nil
  16688. (mapcar
  16689. (lambda(b)
  16690. (if (and (funcall filter b)
  16691. (or (not exclude-tmp)
  16692. (not (string-match "tmp" (buffer-name b)))))
  16693. b
  16694. nil))
  16695. (buffer-list)))))
  16696. (defun org-agenda-files (&optional unrestricted archives)
  16697. "Get the list of agenda files.
  16698. Optional UNRESTRICTED means return the full list even if a restriction
  16699. is currently in place.
  16700. When ARCHIVES is t, include all archive files that are really being
  16701. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16702. `org-agenda-archives-mode' is t."
  16703. (let ((files
  16704. (cond
  16705. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16706. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16707. ((listp org-agenda-files) org-agenda-files)
  16708. (t (error "Invalid value of `org-agenda-files'")))))
  16709. (setq files (apply 'append
  16710. (mapcar (lambda (f)
  16711. (if (file-directory-p f)
  16712. (directory-files
  16713. f t org-agenda-file-regexp)
  16714. (list f)))
  16715. files)))
  16716. (when org-agenda-skip-unavailable-files
  16717. (setq files (delq nil
  16718. (mapcar (function
  16719. (lambda (file)
  16720. (and (file-readable-p file) file)))
  16721. files))))
  16722. (when (or (eq archives t)
  16723. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16724. (setq files (org-add-archive-files files)))
  16725. files))
  16726. (defun org-agenda-file-p (&optional file)
  16727. "Return non-nil, if FILE is an agenda file.
  16728. If FILE is omitted, use the file associated with the current
  16729. buffer."
  16730. (let ((fname (or file (buffer-file-name))))
  16731. (and fname
  16732. (member (file-truename fname)
  16733. (mapcar #'file-truename (org-agenda-files t))))))
  16734. (defun org-edit-agenda-file-list ()
  16735. "Edit the list of agenda files.
  16736. Depending on setup, this either uses customize to edit the variable
  16737. `org-agenda-files', or it visits the file that is holding the list. In the
  16738. latter case, the buffer is set up in a way that saving it automatically kills
  16739. the buffer and restores the previous window configuration."
  16740. (interactive)
  16741. (if (stringp org-agenda-files)
  16742. (let ((cw (current-window-configuration)))
  16743. (find-file org-agenda-files)
  16744. (setq-local org-window-configuration cw)
  16745. (add-hook 'after-save-hook
  16746. (lambda ()
  16747. (set-window-configuration
  16748. (prog1 org-window-configuration
  16749. (kill-buffer (current-buffer))))
  16750. (org-install-agenda-files-menu)
  16751. (message "New agenda file list installed"))
  16752. nil 'local)
  16753. (message "%s" (substitute-command-keys
  16754. "Edit list and finish with \\[save-buffer]")))
  16755. (customize-variable 'org-agenda-files)))
  16756. (defun org-store-new-agenda-file-list (list)
  16757. "Set new value for the agenda file list and save it correctly."
  16758. (if (stringp org-agenda-files)
  16759. (let ((fe (org-read-agenda-file-list t)) b u)
  16760. (while (setq b (find-buffer-visiting org-agenda-files))
  16761. (kill-buffer b))
  16762. (with-temp-file org-agenda-files
  16763. (insert
  16764. (mapconcat
  16765. (lambda (f) ;; Keep un-expanded entries.
  16766. (if (setq u (assoc f fe))
  16767. (cdr u)
  16768. f))
  16769. list "\n")
  16770. "\n")))
  16771. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16772. (org-insert-mode-line-in-empty-file nil))
  16773. (setq org-agenda-files list)
  16774. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16775. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16776. "Read the list of agenda files from a file.
  16777. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16778. filenames, used by `org-store-new-agenda-file-list' to write back
  16779. un-expanded file names."
  16780. (when (file-directory-p org-agenda-files)
  16781. (error "`org-agenda-files' cannot be a single directory"))
  16782. (when (stringp org-agenda-files)
  16783. (with-temp-buffer
  16784. (insert-file-contents org-agenda-files)
  16785. (mapcar
  16786. (lambda (f)
  16787. (let ((e (expand-file-name (substitute-in-file-name f)
  16788. org-directory)))
  16789. (if pair-with-expansion
  16790. (cons e f)
  16791. e)))
  16792. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16793. ;;;###autoload
  16794. (defun org-cycle-agenda-files ()
  16795. "Cycle through the files in `org-agenda-files'.
  16796. If the current buffer visits an agenda file, find the next one in the list.
  16797. If the current buffer does not, find the first agenda file."
  16798. (interactive)
  16799. (let* ((fs (or (org-agenda-files t)
  16800. (user-error "No agenda files")))
  16801. (files (copy-sequence fs))
  16802. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  16803. file)
  16804. (when tcf
  16805. (while (and (setq file (pop files))
  16806. (not (equal (file-truename file) tcf)))))
  16807. (find-file (car (or files fs)))
  16808. (when (buffer-base-buffer) (pop-to-buffer-same-window (buffer-base-buffer)))))
  16809. (defun org-agenda-file-to-front (&optional to-end)
  16810. "Move/add the current file to the top of the agenda file list.
  16811. If the file is not present in the list, it is added to the front. If it is
  16812. present, it is moved there. With optional argument TO-END, add/move to the
  16813. end of the list."
  16814. (interactive "P")
  16815. (let ((org-agenda-skip-unavailable-files nil)
  16816. (file-alist (mapcar (lambda (x)
  16817. (cons (file-truename x) x))
  16818. (org-agenda-files t)))
  16819. (ctf (file-truename
  16820. (or buffer-file-name
  16821. (user-error "Please save the current buffer to a file"))))
  16822. x had)
  16823. (setq x (assoc ctf file-alist) had x)
  16824. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16825. (if to-end
  16826. (setq file-alist (append (delq x file-alist) (list x)))
  16827. (setq file-alist (cons x (delq x file-alist))))
  16828. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16829. (org-install-agenda-files-menu)
  16830. (message "File %s to %s of agenda file list"
  16831. (if had "moved" "added") (if to-end "end" "front"))))
  16832. (defun org-remove-file (&optional file)
  16833. "Remove current file from the list of files in variable `org-agenda-files'.
  16834. These are the files which are being checked for agenda entries.
  16835. Optional argument FILE means use this file instead of the current."
  16836. (interactive)
  16837. (let* ((org-agenda-skip-unavailable-files nil)
  16838. (file (or file buffer-file-name
  16839. (user-error "Current buffer does not visit a file")))
  16840. (true-file (file-truename file))
  16841. (afile (abbreviate-file-name file))
  16842. (files (delq nil (mapcar
  16843. (lambda (x)
  16844. (unless (equal true-file
  16845. (file-truename x))
  16846. x))
  16847. (org-agenda-files t)))))
  16848. (if (not (= (length files) (length (org-agenda-files t))))
  16849. (progn
  16850. (org-store-new-agenda-file-list files)
  16851. (org-install-agenda-files-menu)
  16852. (message "Removed from Org Agenda list: %s" afile))
  16853. (message "File was not in list: %s (not removed)" afile))))
  16854. (defun org-file-menu-entry (file)
  16855. (vector file (list 'find-file file) t))
  16856. (defun org-check-agenda-file (file)
  16857. "Make sure FILE exists. If not, ask user what to do."
  16858. (unless (file-exists-p file)
  16859. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16860. (abbreviate-file-name file))
  16861. (let ((r (downcase (read-char-exclusive))))
  16862. (cond
  16863. ((equal r ?r)
  16864. (org-remove-file file)
  16865. (throw 'nextfile t))
  16866. (t (user-error "Abort"))))))
  16867. (defun org-get-agenda-file-buffer (file)
  16868. "Get an agenda buffer visiting FILE.
  16869. If the buffer needs to be created, add it to the list of buffers
  16870. which might be released later."
  16871. (let ((buf (org-find-base-buffer-visiting file)))
  16872. (if buf
  16873. buf ; just return it
  16874. ;; Make a new buffer and remember it
  16875. (setq buf (find-file-noselect file))
  16876. (when buf (push buf org-agenda-new-buffers))
  16877. buf)))
  16878. (defun org-release-buffers (blist)
  16879. "Release all buffers in list, asking the user for confirmation when needed.
  16880. When a buffer is unmodified, it is just killed. When modified, it is saved
  16881. \(if the user agrees) and then killed."
  16882. (let (file)
  16883. (dolist (buf blist)
  16884. (setq file (buffer-file-name buf))
  16885. (when (and (buffer-modified-p buf)
  16886. file
  16887. (y-or-n-p (format "Save file %s? " file)))
  16888. (with-current-buffer buf (save-buffer)))
  16889. (kill-buffer buf))))
  16890. (defun org-agenda-prepare-buffers (files)
  16891. "Create buffers for all agenda files, protect archived trees and comments."
  16892. (interactive)
  16893. (let ((pa '(:org-archived t))
  16894. (pc '(:org-comment t))
  16895. (pall '(:org-archived t :org-comment t))
  16896. (inhibit-read-only t)
  16897. (org-inhibit-startup org-agenda-inhibit-startup)
  16898. (rea (concat ":" org-archive-tag ":"))
  16899. re pos)
  16900. (setq org-tag-alist-for-agenda nil
  16901. org-tag-groups-alist-for-agenda nil)
  16902. (save-excursion
  16903. (save-restriction
  16904. (dolist (file files)
  16905. (catch 'nextfile
  16906. (if (bufferp file)
  16907. (set-buffer file)
  16908. (org-check-agenda-file file)
  16909. (set-buffer (org-get-agenda-file-buffer file)))
  16910. (widen)
  16911. (org-set-regexps-and-options 'tags-only)
  16912. (setq pos (point))
  16913. (or (memq 'category org-agenda-ignore-properties)
  16914. (org-refresh-category-properties))
  16915. (or (memq 'stats org-agenda-ignore-properties)
  16916. (org-refresh-stats-properties))
  16917. (or (memq 'effort org-agenda-ignore-properties)
  16918. (org-refresh-effort-properties))
  16919. (or (memq 'appt org-agenda-ignore-properties)
  16920. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16921. (setq org-todo-keywords-for-agenda
  16922. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16923. (setq org-done-keywords-for-agenda
  16924. (append org-done-keywords-for-agenda org-done-keywords))
  16925. (setq org-todo-keyword-alist-for-agenda
  16926. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16927. (setq org-tag-alist-for-agenda
  16928. (org-uniquify
  16929. (append org-tag-alist-for-agenda
  16930. org-current-tag-alist)))
  16931. ;; Merge current file's tag groups into global
  16932. ;; `org-tag-groups-alist-for-agenda'.
  16933. (when org-group-tags
  16934. (dolist (alist org-tag-groups-alist)
  16935. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  16936. (if old
  16937. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  16938. (push alist org-tag-groups-alist-for-agenda)))))
  16939. (org-with-silent-modifications
  16940. (save-excursion
  16941. (remove-text-properties (point-min) (point-max) pall)
  16942. (when org-agenda-skip-archived-trees
  16943. (goto-char (point-min))
  16944. (while (re-search-forward rea nil t)
  16945. (when (org-at-heading-p t)
  16946. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16947. (goto-char (point-min))
  16948. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  16949. (while (re-search-forward re nil t)
  16950. (when (save-match-data (org-in-commented-heading-p t))
  16951. (add-text-properties
  16952. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16953. (goto-char pos)))))
  16954. (setq org-todo-keywords-for-agenda
  16955. (org-uniquify org-todo-keywords-for-agenda))
  16956. (setq org-todo-keyword-alist-for-agenda
  16957. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16958. ;;;; CDLaTeX minor mode
  16959. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16960. "Keymap for the minor `org-cdlatex-mode'.")
  16961. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16962. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16963. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16964. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16965. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16966. (defvar org-cdlatex-texmathp-advice-is-done nil
  16967. "Flag remembering if we have applied the advice to texmathp already.")
  16968. (define-minor-mode org-cdlatex-mode
  16969. "Toggle the minor `org-cdlatex-mode'.
  16970. This mode supports entering LaTeX environment and math in LaTeX fragments
  16971. in Org mode.
  16972. \\{org-cdlatex-mode-map}"
  16973. nil " OCDL" nil
  16974. (when org-cdlatex-mode
  16975. (require 'cdlatex)
  16976. (run-hooks 'cdlatex-mode-hook)
  16977. (cdlatex-compute-tables))
  16978. (unless org-cdlatex-texmathp-advice-is-done
  16979. (setq org-cdlatex-texmathp-advice-is-done t)
  16980. (defadvice texmathp (around org-math-always-on activate)
  16981. "Always return t in Org buffers.
  16982. This is because we want to insert math symbols without dollars even outside
  16983. the LaTeX math segments. If Org mode thinks that point is actually inside
  16984. an embedded LaTeX fragment, let `texmathp' do its job.
  16985. `\\[org-cdlatex-mode-map]'"
  16986. (interactive)
  16987. (let (p)
  16988. (cond
  16989. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16990. ((eq this-command 'cdlatex-math-symbol)
  16991. (setq ad-return-value t
  16992. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16993. (t
  16994. (let ((p (org-inside-LaTeX-fragment-p)))
  16995. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16996. (setq ad-return-value t
  16997. texmathp-why '("Org mode embedded math" . 0))
  16998. (when p ad-do-it)))))))))
  16999. (defun turn-on-org-cdlatex ()
  17000. "Unconditionally turn on `org-cdlatex-mode'."
  17001. (org-cdlatex-mode 1))
  17002. (defun org-try-cdlatex-tab ()
  17003. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17004. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17005. - inside a LaTeX fragment, or
  17006. - after the first word in a line, where an abbreviation expansion could
  17007. insert a LaTeX environment."
  17008. (when org-cdlatex-mode
  17009. (cond
  17010. ;; Before any word on the line: No expansion possible.
  17011. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17012. ;; Just after first word on the line: Expand it. Make sure it
  17013. ;; cannot happen on headlines, though.
  17014. ((save-excursion
  17015. (skip-chars-backward "a-zA-Z0-9*")
  17016. (skip-chars-backward " \t")
  17017. (and (bolp) (not (org-at-heading-p))))
  17018. (cdlatex-tab) t)
  17019. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17020. (defun org-cdlatex-underscore-caret (&optional _arg)
  17021. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17022. Revert to the normal definition outside of these fragments."
  17023. (interactive "P")
  17024. (if (org-inside-LaTeX-fragment-p)
  17025. (call-interactively 'cdlatex-sub-superscript)
  17026. (let (org-cdlatex-mode)
  17027. (call-interactively (key-binding (vector last-input-event))))))
  17028. (defun org-cdlatex-math-modify (&optional _arg)
  17029. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17030. Revert to the normal definition outside of these fragments."
  17031. (interactive "P")
  17032. (if (org-inside-LaTeX-fragment-p)
  17033. (call-interactively 'cdlatex-math-modify)
  17034. (let (org-cdlatex-mode)
  17035. (call-interactively (key-binding (vector last-input-event))))))
  17036. (defun org-cdlatex-environment-indent (&optional environment item)
  17037. "Execute `cdlatex-environment' and indent the inserted environment.
  17038. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17039. The inserted environment is indented to current indentation
  17040. unless point is at the beginning of the line, in which the
  17041. environment remains unintended."
  17042. (interactive)
  17043. ;; cdlatex-environment always return nil. Therefore, capture output
  17044. ;; first and determine if an environment was selected.
  17045. (let* ((beg (point-marker))
  17046. (end (copy-marker (point) t))
  17047. (inserted (progn
  17048. (ignore-errors (cdlatex-environment environment item))
  17049. (< beg end)))
  17050. ;; Figure out how many lines to move forward after the
  17051. ;; environment has been inserted.
  17052. (lines (when inserted
  17053. (save-excursion
  17054. (- (cl-loop while (< beg (point))
  17055. with x = 0
  17056. do (forward-line -1)
  17057. (cl-incf x)
  17058. finally return x)
  17059. (if (progn (goto-char beg)
  17060. (and (progn (skip-chars-forward " \t") (eolp))
  17061. (progn (skip-chars-backward " \t") (bolp))))
  17062. 1 0)))))
  17063. (env (org-trim (delete-and-extract-region beg end))))
  17064. (when inserted
  17065. ;; Get indentation of next line unless at column 0.
  17066. (let ((ind (if (bolp) 0
  17067. (save-excursion
  17068. (org-return-indent)
  17069. (prog1 (org-get-indentation)
  17070. (when (progn (skip-chars-forward " \t") (eolp))
  17071. (delete-region beg (point)))))))
  17072. (bol (progn (skip-chars-backward " \t") (bolp))))
  17073. ;; Insert a newline before environment unless at column zero
  17074. ;; to "escape" the current line. Insert a newline if
  17075. ;; something is one the same line as \end{ENVIRONMENT}.
  17076. (insert
  17077. (concat (unless bol "\n") env
  17078. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17079. (unless (zerop ind)
  17080. (save-excursion
  17081. (goto-char beg)
  17082. (while (< (point) end)
  17083. (unless (eolp) (indent-line-to ind))
  17084. (forward-line))))
  17085. (goto-char beg)
  17086. (forward-line lines)
  17087. (indent-line-to ind)))
  17088. (set-marker beg nil)
  17089. (set-marker end nil)))
  17090. ;;;; LaTeX fragments
  17091. (defun org-inside-LaTeX-fragment-p ()
  17092. "Test if point is inside a LaTeX fragment.
  17093. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17094. sequence appearing also before point.
  17095. Even though the matchers for math are configurable, this function assumes
  17096. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17097. delimiters are skipped when they have been removed by customization.
  17098. The return value is nil, or a cons cell with the delimiter and the
  17099. position of this delimiter.
  17100. This function does a reasonably good job, but can locally be fooled by
  17101. for example currency specifications. For example it will assume being in
  17102. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17103. fragments that are properly closed, but during editing, we have to live
  17104. with the uncertainty caused by missing closing delimiters. This function
  17105. looks only before point, not after."
  17106. (catch 'exit
  17107. (let ((pos (point))
  17108. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17109. (lim (save-excursion (org-backward-paragraph) (point)))
  17110. dd-on str (start 0) m re)
  17111. (goto-char pos)
  17112. (when dodollar
  17113. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17114. re (nth 1 (assoc "$" org-latex-regexps)))
  17115. (while (string-match re str start)
  17116. (cond
  17117. ((= (match-end 0) (length str))
  17118. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17119. ((= (match-end 0) (- (length str) 5))
  17120. (throw 'exit nil))
  17121. (t (setq start (match-end 0))))))
  17122. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17123. (goto-char pos)
  17124. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17125. (and (match-beginning 2) (throw 'exit nil))
  17126. ;; count $$
  17127. (while (re-search-backward "\\$\\$" lim t)
  17128. (setq dd-on (not dd-on)))
  17129. (goto-char pos)
  17130. (when dd-on (cons "$$" m))))))
  17131. (defun org-inside-latex-macro-p ()
  17132. "Is point inside a LaTeX macro or its arguments?"
  17133. (save-match-data
  17134. (org-in-regexp
  17135. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17136. (defun org--format-latex-make-overlay (beg end image &optional imagetype)
  17137. "Build an overlay between BEG and END using IMAGE file.
  17138. Argument IMAGETYPE is the extension of the displayed image,
  17139. as a string. It defaults to \"png\"."
  17140. (let ((ov (make-overlay beg end))
  17141. (imagetype (or (intern imagetype) 'png)))
  17142. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17143. (overlay-put ov 'evaporate t)
  17144. (overlay-put ov
  17145. 'modification-hooks
  17146. (list (lambda (o _flag _beg _end &optional _l)
  17147. (delete-overlay o))))
  17148. (overlay-put ov
  17149. 'display
  17150. (list 'image :type imagetype :file image :ascent 'center))))
  17151. (defun org--list-latex-overlays (&optional beg end)
  17152. "List all Org LaTeX overlays in current buffer.
  17153. Limit to overlays between BEG and END when those are provided."
  17154. (cl-remove-if-not
  17155. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17156. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17157. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17158. "Remove all overlays with LaTeX fragment images in current buffer.
  17159. When optional arguments BEG and END are non-nil, remove all
  17160. overlays between them instead. Return a non-nil value when some
  17161. overlays were removed, nil otherwise."
  17162. (let ((overlays (org--list-latex-overlays beg end)))
  17163. (mapc #'delete-overlay overlays)
  17164. overlays))
  17165. (defun org-toggle-latex-fragment (&optional arg)
  17166. "Preview the LaTeX fragment at point, or all locally or globally.
  17167. If the cursor is on a LaTeX fragment, create the image and overlay
  17168. it over the source code, if there is none. Remove it otherwise.
  17169. If there is no fragment at point, display all fragments in the
  17170. current section.
  17171. With prefix ARG, preview or clear image for all fragments in the
  17172. current subtree or in the whole buffer when used before the first
  17173. headline. With a prefix ARG `\\[universal-argument] \
  17174. \\[universal-argument]' preview or clear images
  17175. for all fragments in the buffer."
  17176. (interactive "P")
  17177. (when (display-graphic-p)
  17178. (catch 'exit
  17179. (save-excursion
  17180. (let (beg end msg)
  17181. (cond
  17182. ((or (equal arg '(16))
  17183. (and (equal arg '(4))
  17184. (org-with-limited-levels (org-before-first-heading-p))))
  17185. (if (org-remove-latex-fragment-image-overlays)
  17186. (progn (message "LaTeX fragments images removed from buffer")
  17187. (throw 'exit nil))
  17188. (setq msg "Creating images for buffer...")))
  17189. ((equal arg '(4))
  17190. (org-with-limited-levels (org-back-to-heading t))
  17191. (setq beg (point))
  17192. (setq end (progn (org-end-of-subtree t) (point)))
  17193. (if (org-remove-latex-fragment-image-overlays beg end)
  17194. (progn
  17195. (message "LaTeX fragment images removed from subtree")
  17196. (throw 'exit nil))
  17197. (setq msg "Creating images for subtree...")))
  17198. ((let ((datum (org-element-context)))
  17199. (when (memq (org-element-type datum)
  17200. '(latex-environment latex-fragment))
  17201. (setq beg (org-element-property :begin datum))
  17202. (setq end (org-element-property :end datum))
  17203. (if (org-remove-latex-fragment-image-overlays beg end)
  17204. (progn (message "LaTeX fragment image removed")
  17205. (throw 'exit nil))
  17206. (setq msg "Creating image...")))))
  17207. (t
  17208. (org-with-limited-levels
  17209. (setq beg (if (org-at-heading-p) (line-beginning-position)
  17210. (outline-previous-heading)
  17211. (point)))
  17212. (setq end (progn (outline-next-heading) (point)))
  17213. (if (org-remove-latex-fragment-image-overlays beg end)
  17214. (progn
  17215. (message "LaTeX fragment images removed from section")
  17216. (throw 'exit nil))
  17217. (setq msg "Creating images for section...")))))
  17218. (let ((file (buffer-file-name (buffer-base-buffer))))
  17219. (org-format-latex
  17220. (concat org-preview-latex-image-directory "org-ltximg")
  17221. beg end
  17222. ;; Emacs cannot overlay images from remote hosts. Create
  17223. ;; it in `temporary-file-directory' instead.
  17224. (if (or (not file) (file-remote-p file))
  17225. temporary-file-directory
  17226. default-directory)
  17227. 'overlays msg 'forbuffer org-preview-latex-default-process))
  17228. (message (concat msg "done")))))))
  17229. (defun org-format-latex
  17230. (prefix &optional beg end dir overlays msg forbuffer processing-type)
  17231. "Replace LaTeX fragments with links to an image.
  17232. The function takes care of creating the replacement image.
  17233. Only consider fragments between BEG and END when those are
  17234. provided.
  17235. When optional argument OVERLAYS is non-nil, display the image on
  17236. top of the fragment instead of replacing it.
  17237. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17238. Some of the options can be changed using the variable
  17239. `org-format-latex-options', which see."
  17240. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17241. (unless (eq processing-type 'verbatim)
  17242. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17243. (cnt 0)
  17244. checkdir-flag)
  17245. (goto-char (or beg (point-min)))
  17246. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17247. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17248. (overlay-recenter (or end (point-max))))
  17249. (while (re-search-forward math-regexp end t)
  17250. (unless (and overlays
  17251. (eq (get-char-property (point) 'org-overlay-type)
  17252. 'org-latex-overlay))
  17253. (let* ((context (org-element-context))
  17254. (type (org-element-type context)))
  17255. (when (memq type '(latex-environment latex-fragment))
  17256. (let ((block-type (eq type 'latex-environment))
  17257. (value (org-element-property :value context))
  17258. (beg (org-element-property :begin context))
  17259. (end (save-excursion
  17260. (goto-char (org-element-property :end context))
  17261. (skip-chars-backward " \r\t\n")
  17262. (point))))
  17263. (cond
  17264. ((eq processing-type 'mathjax)
  17265. ;; Prepare for MathJax processing.
  17266. (if (not (string-match "\\`\\$\\$?" value))
  17267. (goto-char end)
  17268. (delete-region beg end)
  17269. (if (string= (match-string 0 value) "$$")
  17270. (insert "\\[" (substring value 2 -2) "\\]")
  17271. (insert "\\(" (substring value 1 -1) "\\)"))))
  17272. ((assq processing-type org-preview-latex-process-alist)
  17273. ;; Process to an image.
  17274. (cl-incf cnt)
  17275. (goto-char beg)
  17276. (let* ((processing-info
  17277. (cdr (assq processing-type org-preview-latex-process-alist)))
  17278. (face (face-at-point))
  17279. ;; Get the colors from the face at point.
  17280. (fg
  17281. (let ((color (plist-get org-format-latex-options
  17282. :foreground)))
  17283. (if (and forbuffer (eq color 'auto))
  17284. (face-attribute face :foreground nil 'default)
  17285. color)))
  17286. (bg
  17287. (let ((color (plist-get org-format-latex-options
  17288. :background)))
  17289. (if (and forbuffer (eq color 'auto))
  17290. (face-attribute face :background nil 'default)
  17291. color)))
  17292. (hash (sha1 (prin1-to-string
  17293. (list org-format-latex-header
  17294. org-latex-default-packages-alist
  17295. org-latex-packages-alist
  17296. org-format-latex-options
  17297. forbuffer value fg bg))))
  17298. (imagetype (or (plist-get processing-info :image-output-type) "png"))
  17299. (absprefix (expand-file-name prefix dir))
  17300. (linkfile (format "%s_%s.%s" prefix hash imagetype))
  17301. (movefile (format "%s_%s.%s" absprefix hash imagetype))
  17302. (sep (and block-type "\n\n"))
  17303. (link (concat sep "[[file:" linkfile "]]" sep))
  17304. (options
  17305. (org-combine-plists
  17306. org-format-latex-options
  17307. `(:foreground ,fg :background ,bg))))
  17308. (when msg (message msg cnt))
  17309. (unless checkdir-flag ; Ensure the directory exists.
  17310. (setq checkdir-flag t)
  17311. (let ((todir (file-name-directory absprefix)))
  17312. (unless (file-directory-p todir)
  17313. (make-directory todir t))))
  17314. (unless (file-exists-p movefile)
  17315. (org-create-formula-image
  17316. value movefile options forbuffer processing-type))
  17317. (if overlays
  17318. (progn
  17319. (dolist (o (overlays-in beg end))
  17320. (when (eq (overlay-get o 'org-overlay-type)
  17321. 'org-latex-overlay)
  17322. (delete-overlay o)))
  17323. (org--format-latex-make-overlay beg end movefile imagetype)
  17324. (goto-char end))
  17325. (delete-region beg end)
  17326. (insert
  17327. (org-add-props link
  17328. (list 'org-latex-src
  17329. (replace-regexp-in-string "\"" "" value)
  17330. 'org-latex-src-embed-type
  17331. (if block-type 'paragraph 'character)))))))
  17332. ((eq processing-type 'mathml)
  17333. ;; Process to MathML.
  17334. (unless (org-format-latex-mathml-available-p)
  17335. (user-error "LaTeX to MathML converter not configured"))
  17336. (cl-incf cnt)
  17337. (when msg (message msg cnt))
  17338. (goto-char beg)
  17339. (delete-region beg end)
  17340. (insert (org-format-latex-as-mathml
  17341. value block-type prefix dir)))
  17342. (t
  17343. (error "Unknown conversion process %s for LaTeX fragments"
  17344. processing-type)))))))))))
  17345. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17346. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17347. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17348. sucessful, return the portion between \"<math...> </math>\"
  17349. elements otherwise return nil. When MATHML-FILE is specified,
  17350. write the results in to that file. When invoked as an
  17351. interactive command, prompt for LATEX-FRAG, with initial value
  17352. set to the current active region and echo the results for user
  17353. inspection."
  17354. (interactive (list (let ((frag (when (org-region-active-p)
  17355. (buffer-substring-no-properties
  17356. (region-beginning) (region-end)))))
  17357. (read-string "LaTeX Fragment: " frag nil frag))))
  17358. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17359. (let* ((tmp-in-file
  17360. (let ((file (file-relative-name
  17361. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17362. (write-region latex-frag nil file)
  17363. file))
  17364. (tmp-out-file (file-relative-name
  17365. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17366. (cmd (format-spec
  17367. org-latex-to-mathml-convert-command
  17368. `((?j . ,(and org-latex-to-mathml-jar-file
  17369. (shell-quote-argument
  17370. (expand-file-name
  17371. org-latex-to-mathml-jar-file))))
  17372. (?I . ,(shell-quote-argument tmp-in-file))
  17373. (?i . ,latex-frag)
  17374. (?o . ,(shell-quote-argument tmp-out-file)))))
  17375. mathml shell-command-output)
  17376. (when (called-interactively-p 'any)
  17377. (unless (org-format-latex-mathml-available-p)
  17378. (user-error "LaTeX to MathML converter not configured")))
  17379. (message "Running %s" cmd)
  17380. (setq shell-command-output (shell-command-to-string cmd))
  17381. (setq mathml
  17382. (when (file-readable-p tmp-out-file)
  17383. (with-current-buffer (find-file-noselect tmp-out-file t)
  17384. (goto-char (point-min))
  17385. (when (re-search-forward
  17386. (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
  17387. (regexp-quote
  17388. "xmlns=\"http://www.w3.org/1998/Math/MathML\""))
  17389. nil t)
  17390. (prog1 (match-string 0) (kill-buffer))))))
  17391. (cond
  17392. (mathml
  17393. (setq mathml
  17394. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17395. (when mathml-file
  17396. (write-region mathml nil mathml-file))
  17397. (when (called-interactively-p 'any)
  17398. (message mathml)))
  17399. ((message "LaTeX to MathML conversion failed")
  17400. (message shell-command-output)))
  17401. (delete-file tmp-in-file)
  17402. (when (file-exists-p tmp-out-file)
  17403. (delete-file tmp-out-file))
  17404. mathml))
  17405. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17406. prefix &optional dir)
  17407. "Use `org-create-math-formula' but check local cache first."
  17408. (let* ((absprefix (expand-file-name prefix dir))
  17409. (print-length nil) (print-level nil)
  17410. (formula-id (concat
  17411. "formula-"
  17412. (sha1
  17413. (prin1-to-string
  17414. (list latex-frag
  17415. org-latex-to-mathml-convert-command)))))
  17416. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17417. (formula-cache-dir (file-name-directory formula-cache)))
  17418. (unless (file-directory-p formula-cache-dir)
  17419. (make-directory formula-cache-dir t))
  17420. (unless (file-exists-p formula-cache)
  17421. (org-create-math-formula latex-frag formula-cache))
  17422. (if (file-exists-p formula-cache)
  17423. ;; Successful conversion. Return the link to MathML file.
  17424. (org-add-props
  17425. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17426. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17427. 'org-latex-src-embed-type (if latex-frag-type
  17428. 'paragraph 'character)))
  17429. ;; Failed conversion. Return the LaTeX fragment verbatim
  17430. latex-frag)))
  17431. (defun org--get-display-dpi ()
  17432. "Get the DPI of the display.
  17433. The function assumes that the display has the same pixel width in
  17434. the horizontal and vertical directions."
  17435. (if (display-graphic-p)
  17436. (round (/ (display-pixel-height)
  17437. (/ (display-mm-height) 25.4)))
  17438. (error "Attempt to calculate the dpi of a non-graphic display")))
  17439. (defun org-create-formula-image
  17440. (string tofile options buffer &optional processing-type)
  17441. "Create an image from LaTeX source using external processes.
  17442. The LaTeX STRING is saved to a temporary LaTeX file, then
  17443. converted to an image file by process PROCESSING-TYPE defined in
  17444. `org-preview-latex-process-alist'. A nil value defaults to
  17445. `org-preview-latex-default-process'.
  17446. The generated image file is eventually moved to TOFILE.
  17447. The OPTIONS argument controls the size, foreground color and
  17448. background color of the generated image.
  17449. When BUFFER non-nil, this function is used for LaTeX previewing.
  17450. Otherwise, it is used to deal with LaTeX snippets showed in
  17451. a HTML file."
  17452. (let* ((processing-type (or processing-type
  17453. org-preview-latex-default-process))
  17454. (processing-info
  17455. (cdr (assq processing-type org-preview-latex-process-alist)))
  17456. (programs (plist-get processing-info :programs))
  17457. (error-message (or (plist-get processing-info :message) ""))
  17458. (use-xcolor (plist-get processing-info :use-xcolor))
  17459. (image-input-type (plist-get processing-info :image-input-type))
  17460. (image-output-type (plist-get processing-info :image-output-type))
  17461. (post-clean (or (plist-get processing-info :post-clean)
  17462. '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
  17463. ".svg" ".png" ".jpg" ".jpeg" ".out")))
  17464. (latex-header
  17465. (or (plist-get processing-info :latex-header)
  17466. (org-latex-make-preamble
  17467. (org-export-get-environment (org-export-get-backend 'latex))
  17468. org-format-latex-header
  17469. 'snippet)))
  17470. (latex-compiler (plist-get processing-info :latex-compiler))
  17471. (image-converter (plist-get processing-info :image-converter))
  17472. (tmpdir temporary-file-directory)
  17473. (texfilebase (make-temp-name
  17474. (expand-file-name "orgtex" tmpdir)))
  17475. (texfile (concat texfilebase ".tex"))
  17476. (image-size-adjust (or (plist-get processing-info :image-size-adjust)
  17477. '(1.0 . 1.0)))
  17478. (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
  17479. (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
  17480. (dpi (* scale (if buffer (org--get-display-dpi) 140.0)))
  17481. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17482. "Black"))
  17483. (bg (or (plist-get options (if buffer :background :html-background))
  17484. "Transparent"))
  17485. (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
  17486. (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
  17487. (dolist (program programs)
  17488. (org-check-external-command program error-message))
  17489. (if use-xcolor
  17490. (progn (if (eq fg 'default)
  17491. (setq fg (org-latex-color :foreground))
  17492. (setq fg (org-latex-color-format fg)))
  17493. (if (eq bg 'default)
  17494. (setq bg (org-latex-color :background))
  17495. (setq bg (org-latex-color-format
  17496. (if (string= bg "Transparent") "white" bg))))
  17497. (with-temp-file texfile
  17498. (insert latex-header)
  17499. (insert "\n\\begin{document}\n"
  17500. "\\definecolor{fg}{rgb}{" fg "}\n"
  17501. "\\definecolor{bg}{rgb}{" bg "}\n"
  17502. "\n\\pagecolor{bg}\n"
  17503. "\n{\\color{fg}\n"
  17504. string
  17505. "\n}\n"
  17506. "\n\\end{document}\n")))
  17507. (if (eq fg 'default)
  17508. (setq fg (org-dvipng-color :foreground))
  17509. (unless (string= fg "Transparent")
  17510. (setq fg (org-dvipng-color-format fg))))
  17511. (if (eq bg 'default)
  17512. (setq bg (org-dvipng-color :background))
  17513. (unless (string= bg "Transparent")
  17514. (setq bg (org-dvipng-color-format bg))))
  17515. (with-temp-file texfile
  17516. (insert latex-header)
  17517. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17518. (let* ((err-msg (format "Please adjust `%s' part of \
  17519. `org-preview-latex-process-alist'."
  17520. processing-type))
  17521. (image-input-file
  17522. (org-compile-file
  17523. texfile latex-compiler image-input-type err-msg log-buf))
  17524. (image-output-file
  17525. (org-compile-file
  17526. image-input-file image-converter image-output-type err-msg log-buf
  17527. `((?F . ,(shell-quote-argument fg))
  17528. (?B . ,(shell-quote-argument bg))
  17529. (?D . ,(shell-quote-argument (format "%s" dpi)))
  17530. (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
  17531. (copy-file image-output-file tofile 'replace)
  17532. (dolist (e post-clean)
  17533. (when (file-exists-p (concat texfilebase e))
  17534. (delete-file (concat texfilebase e))))
  17535. image-output-file)))
  17536. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17537. "Fill a LaTeX header template TPL.
  17538. In the template, the following place holders will be recognized:
  17539. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17540. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17541. [PACKAGES] \\usepackage statements for PKG
  17542. [NO-PACKAGES] do not include PKG
  17543. [EXTRA] the string EXTRA
  17544. [NO-EXTRA] do not include EXTRA
  17545. For backward compatibility, if both the positive and the negative place
  17546. holder is missing, the positive one (without the \"NO-\") will be
  17547. assumed to be present at the end of the template.
  17548. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17549. EXTRA is a string.
  17550. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17551. (let (rpl (end ""))
  17552. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17553. (setq rpl (if (or (match-end 1) (not def-pkg))
  17554. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17555. tpl (replace-match rpl t t tpl))
  17556. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17557. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17558. (setq rpl (if (or (match-end 1) (not pkg))
  17559. "" (org-latex-packages-to-string pkg snippets-p t))
  17560. tpl (replace-match rpl t t tpl))
  17561. (when pkg (setq end
  17562. (concat end "\n"
  17563. (org-latex-packages-to-string pkg snippets-p)))))
  17564. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17565. (setq rpl (if (or (match-end 1) (not extra))
  17566. "" (concat extra "\n"))
  17567. tpl (replace-match rpl t t tpl))
  17568. (when (and extra (string-match "\\S-" extra))
  17569. (setq end (concat end "\n" extra))))
  17570. (if (string-match "\\S-" end)
  17571. (concat tpl "\n" end)
  17572. tpl)))
  17573. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17574. "Turn an alist of packages into a string with the \\usepackage macros."
  17575. (setq pkg (mapconcat (lambda(p)
  17576. (cond
  17577. ((stringp p) p)
  17578. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17579. (format "%% Package %s omitted" (cadr p)))
  17580. ((equal "" (car p))
  17581. (format "\\usepackage{%s}" (cadr p)))
  17582. (t
  17583. (format "\\usepackage[%s]{%s}"
  17584. (car p) (cadr p)))))
  17585. pkg
  17586. "\n"))
  17587. (if newline (concat pkg "\n") pkg))
  17588. (defun org-dvipng-color (attr)
  17589. "Return a RGB color specification for dvipng."
  17590. (org-dvipng-color-format (face-attribute 'default attr nil)))
  17591. (defun org-dvipng-color-format (color-name)
  17592. "Convert COLOR-NAME to a RGB color value for dvipng."
  17593. (apply #'format "rgb %s %s %s"
  17594. (mapcar 'org-normalize-color
  17595. (color-values color-name))))
  17596. (defun org-latex-color (attr)
  17597. "Return a RGB color for the LaTeX color package."
  17598. (org-latex-color-format (face-attribute 'default attr nil)))
  17599. (defun org-latex-color-format (color-name)
  17600. "Convert COLOR-NAME to a RGB color value."
  17601. (apply #'format "%s,%s,%s"
  17602. (mapcar 'org-normalize-color
  17603. (color-values color-name))))
  17604. (defun org-normalize-color (value)
  17605. "Return string to be used as color value for an RGB component."
  17606. (format "%g" (/ value 65535.0)))
  17607. ;; Image display
  17608. (defvar-local org-inline-image-overlays nil)
  17609. (defun org-toggle-inline-images (&optional include-linked)
  17610. "Toggle the display of inline images.
  17611. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17612. (interactive "P")
  17613. (if org-inline-image-overlays
  17614. (progn
  17615. (org-remove-inline-images)
  17616. (when (called-interactively-p 'interactive)
  17617. (message "Inline image display turned off")))
  17618. (org-display-inline-images include-linked)
  17619. (when (called-interactively-p 'interactive)
  17620. (message (if org-inline-image-overlays
  17621. (format "%d images displayed inline"
  17622. (length org-inline-image-overlays))
  17623. "No images to display inline")))))
  17624. (defun org-redisplay-inline-images ()
  17625. "Refresh the display of inline images."
  17626. (interactive)
  17627. (if (not org-inline-image-overlays)
  17628. (org-toggle-inline-images)
  17629. (org-toggle-inline-images)
  17630. (org-toggle-inline-images)))
  17631. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17632. "Display inline images.
  17633. An inline image is a link which follows either of these
  17634. conventions:
  17635. 1. Its path is a file with an extension matching return value
  17636. from `image-file-name-regexp' and it has no contents.
  17637. 2. Its description consists in a single link of the previous
  17638. type.
  17639. When optional argument INCLUDE-LINKED is non-nil, also links with
  17640. a text description part will be inlined. This can be nice for
  17641. a quick look at those images, but it does not reflect what
  17642. exported files will look like.
  17643. When optional argument REFRESH is non-nil, refresh existing
  17644. images between BEG and END. This will create new image displays
  17645. only if necessary. BEG and END default to the buffer
  17646. boundaries."
  17647. (interactive "P")
  17648. (when (display-graphic-p)
  17649. (unless refresh
  17650. (org-remove-inline-images)
  17651. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17652. (org-with-wide-buffer
  17653. (goto-char (or beg (point-min)))
  17654. (let* ((case-fold-search t)
  17655. (file-extension-re (image-file-name-regexp))
  17656. (link-abbrevs (mapcar #'car
  17657. (append org-link-abbrev-alist-local
  17658. org-link-abbrev-alist)))
  17659. ;; Check absolute, relative file names and explicit
  17660. ;; "file:" links. Also check link abbreviations since
  17661. ;; some might expand to "file" links.
  17662. (file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)"
  17663. (if (not link-abbrevs) ""
  17664. (format "\\|\\(?:%s:\\)"
  17665. (regexp-opt link-abbrevs))))))
  17666. (while (re-search-forward file-types-re end t)
  17667. (let ((link (save-match-data (org-element-context))))
  17668. ;; Check if we're at an inline image, i.e., an image file
  17669. ;; link without a description (unless INCLUDE-LINKED is
  17670. ;; non-nil).
  17671. (when (and (equal "file" (org-element-property :type link))
  17672. (or include-linked
  17673. (null (org-element-contents link)))
  17674. (string-match-p file-extension-re
  17675. (org-element-property :path link)))
  17676. (let ((file (expand-file-name
  17677. (org-link-unescape
  17678. (org-element-property :path link)))))
  17679. (when (file-exists-p file)
  17680. (let ((width
  17681. ;; Apply `org-image-actual-width' specifications.
  17682. (cond
  17683. ((not (image-type-available-p 'imagemagick)) nil)
  17684. ((eq org-image-actual-width t) nil)
  17685. ((listp org-image-actual-width)
  17686. (or
  17687. ;; First try to find a width among
  17688. ;; attributes associated to the paragraph
  17689. ;; containing link.
  17690. (let ((paragraph
  17691. (let ((e link))
  17692. (while (and (setq e (org-element-property
  17693. :parent e))
  17694. (not (eq (org-element-type e)
  17695. 'paragraph))))
  17696. e)))
  17697. (when paragraph
  17698. (save-excursion
  17699. (goto-char (org-element-property :begin paragraph))
  17700. (when
  17701. (re-search-forward
  17702. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17703. (org-element-property
  17704. :post-affiliated paragraph)
  17705. t)
  17706. (string-to-number (match-string 1))))))
  17707. ;; Otherwise, fall-back to provided number.
  17708. (car org-image-actual-width)))
  17709. ((numberp org-image-actual-width)
  17710. org-image-actual-width)))
  17711. (old (get-char-property-and-overlay
  17712. (org-element-property :begin link)
  17713. 'org-image-overlay)))
  17714. (if (and (car-safe old) refresh)
  17715. (image-refresh (overlay-get (cdr old) 'display))
  17716. (let ((image (create-image file
  17717. (and width 'imagemagick)
  17718. nil
  17719. :width width)))
  17720. (when image
  17721. (let ((ov (make-overlay
  17722. (org-element-property :begin link)
  17723. (progn
  17724. (goto-char
  17725. (org-element-property :end link))
  17726. (skip-chars-backward " \t")
  17727. (point)))))
  17728. (overlay-put ov 'display image)
  17729. (overlay-put ov 'face 'default)
  17730. (overlay-put ov 'org-image-overlay t)
  17731. (overlay-put
  17732. ov 'modification-hooks
  17733. (list 'org-display-inline-remove-overlay))
  17734. (push ov org-inline-image-overlays)))))))))))))))
  17735. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  17736. "Remove inline-display overlay if a corresponding region is modified."
  17737. (let ((inhibit-modification-hooks t))
  17738. (when (and ov after)
  17739. (delete ov org-inline-image-overlays)
  17740. (delete-overlay ov))))
  17741. (defun org-remove-inline-images ()
  17742. "Remove inline display of images."
  17743. (interactive)
  17744. (mapc #'delete-overlay org-inline-image-overlays)
  17745. (setq org-inline-image-overlays nil))
  17746. ;;;; Key bindings
  17747. (defun org-remap (map &rest commands)
  17748. "In MAP, remap the functions given in COMMANDS.
  17749. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17750. (let (new old)
  17751. (while commands
  17752. (setq old (pop commands) new (pop commands))
  17753. (org-defkey map (vector 'remap old) new))))
  17754. ;; Outline functions from `outline-mode-prefix-map'
  17755. ;; that can be remapped in Org:
  17756. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17757. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  17758. (define-key org-mode-map [remap outline-forward-same-level]
  17759. 'org-forward-heading-same-level)
  17760. (define-key org-mode-map [remap outline-backward-same-level]
  17761. 'org-backward-heading-same-level)
  17762. (define-key org-mode-map [remap outline-show-branches]
  17763. 'org-kill-note-or-show-branches)
  17764. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17765. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17766. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17767. (define-key org-mode-map [remap outline-next-visible-heading]
  17768. 'org-next-visible-heading)
  17769. (define-key org-mode-map [remap outline-previous-visible-heading]
  17770. 'org-previous-visible-heading)
  17771. (define-key org-mode-map [remap show-children] 'org-show-children)
  17772. ;; Outline functions from `outline-mode-prefix-map' that can not
  17773. ;; be remapped in Org:
  17774. ;; - the column "key binding" shows whether the Outline function is still
  17775. ;; available in Org mode on the same key that it has been bound to in
  17776. ;; Outline mode:
  17777. ;; - "overridden": key used for a different functionality in Org mode
  17778. ;; - else: key still bound to the same Outline function in Org mode
  17779. ;; | Outline function | key binding | Org replacement |
  17780. ;; |------------------------------------+-------------+--------------------------|
  17781. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17782. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17783. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17784. ;; | `show-entry' | overridden | no replacement |
  17785. ;; | `show-branches' | `C-c C-k' | still same function |
  17786. ;; | `show-subtree' | overridden | visibility cycling |
  17787. ;; | `show-all' | overridden | no replacement |
  17788. ;; | `hide-subtree' | overridden | visibility cycling |
  17789. ;; | `hide-body' | overridden | no replacement |
  17790. ;; | `hide-entry' | overridden | visibility cycling |
  17791. ;; | `hide-leaves' | overridden | no replacement |
  17792. ;; | `hide-sublevels' | overridden | no replacement |
  17793. ;; | `hide-other' | overridden | no replacement |
  17794. ;; Make `C-c C-x' a prefix key
  17795. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17796. ;; TAB key with modifiers
  17797. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17798. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17799. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17800. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  17801. ;; The following line is necessary under Suse GNU/Linux
  17802. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab)
  17803. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17804. (define-key org-mode-map [backtab] 'org-shifttab)
  17805. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17806. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17807. (org-defkey org-mode-map (kbd "M-RET") #'org-meta-return)
  17808. ;; Cursor keys with modifiers
  17809. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17810. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17811. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17812. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17813. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17814. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17815. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17816. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17817. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17818. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17819. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17820. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17821. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17822. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17823. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17824. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17825. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17826. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17827. ;; Babel keys
  17828. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17829. (dolist (pair org-babel-key-bindings)
  17830. (define-key org-babel-map (car pair) (cdr pair)))
  17831. ;;; Extra keys for tty access.
  17832. ;; We only set them when really needed because otherwise the
  17833. ;; menus don't show the simple keys
  17834. (when (or org-use-extra-keys (not window-system))
  17835. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17836. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17837. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17838. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17839. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17840. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17841. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17842. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17843. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17844. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17845. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17846. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17847. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17848. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17849. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17850. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17851. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17852. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17853. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17854. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17855. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17856. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17857. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  17858. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17859. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17860. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17861. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17862. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17863. ;; All the other keys
  17864. (org-remap org-mode-map
  17865. 'self-insert-command 'org-self-insert-command
  17866. 'delete-char 'org-delete-char
  17867. 'delete-backward-char 'org-delete-backward-char)
  17868. (org-defkey org-mode-map "|" 'org-force-self-insert)
  17869. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  17870. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17871. (if (boundp 'narrow-map)
  17872. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17873. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17874. (if (boundp 'narrow-map)
  17875. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17876. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17877. (if (boundp 'narrow-map)
  17878. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17879. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17880. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17881. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17882. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17883. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17884. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17885. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17886. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17887. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17888. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17889. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17890. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  17891. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17892. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17893. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17894. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17895. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17896. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17897. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17898. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17899. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17900. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17901. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17902. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17903. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17904. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17905. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17906. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17907. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17908. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17909. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17910. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17911. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17912. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17913. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17914. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17915. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17916. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17917. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17918. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17919. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17920. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17921. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17922. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17923. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17924. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17925. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17926. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17927. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17928. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17929. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17930. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17931. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17932. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17933. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17934. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17935. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17936. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17937. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17938. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17939. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17940. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17941. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17942. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17943. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  17944. (org-defkey org-mode-map "\C-m" 'org-return)
  17945. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17946. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17947. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17948. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17949. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17950. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17951. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17952. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17953. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17954. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17955. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17956. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17957. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17958. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17959. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17960. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17961. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17962. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17963. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17964. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17965. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17966. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17967. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17968. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17969. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17970. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17971. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17972. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17973. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17974. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17975. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17976. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17977. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17978. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17979. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17980. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17981. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17982. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17983. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17984. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17985. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17986. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17987. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17988. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17989. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17990. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17991. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17992. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17993. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  17994. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17995. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17996. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17997. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17998. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17999. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18000. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18001. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18002. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18003. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18004. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18005. (defconst org-speed-commands-default
  18006. '(
  18007. ("Outline Navigation")
  18008. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18009. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18010. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18011. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18012. ("F" . org-next-block)
  18013. ("B" . org-previous-block)
  18014. ("u" . (org-speed-move-safe 'outline-up-heading))
  18015. ("j" . org-goto)
  18016. ("g" . (org-refile t))
  18017. ("Outline Visibility")
  18018. ("c" . org-cycle)
  18019. ("C" . org-shifttab)
  18020. (" " . org-display-outline-path)
  18021. ("s" . org-narrow-to-subtree)
  18022. ("=" . org-columns)
  18023. ("Outline Structure Editing")
  18024. ("U" . org-metaup)
  18025. ("D" . org-metadown)
  18026. ("r" . org-metaright)
  18027. ("l" . org-metaleft)
  18028. ("R" . org-shiftmetaright)
  18029. ("L" . org-shiftmetaleft)
  18030. ("i" . (progn (forward-char 1) (call-interactively
  18031. 'org-insert-heading-respect-content)))
  18032. ("^" . org-sort)
  18033. ("w" . org-refile)
  18034. ("a" . org-archive-subtree-default-with-confirmation)
  18035. ("@" . org-mark-subtree)
  18036. ("#" . org-toggle-comment)
  18037. ("Clock Commands")
  18038. ("I" . org-clock-in)
  18039. ("O" . org-clock-out)
  18040. ("Meta Data Editing")
  18041. ("t" . org-todo)
  18042. ("," . (org-priority))
  18043. ("0" . (org-priority ?\ ))
  18044. ("1" . (org-priority ?A))
  18045. ("2" . (org-priority ?B))
  18046. ("3" . (org-priority ?C))
  18047. (":" . org-set-tags-command)
  18048. ("e" . org-set-effort)
  18049. ("E" . org-inc-effort)
  18050. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18051. (org-entry-put (point) "APPT_WARNTIME" m)))
  18052. ("Agenda Views etc")
  18053. ("v" . org-agenda)
  18054. ("/" . org-sparse-tree)
  18055. ("Misc")
  18056. ("o" . org-open-at-point)
  18057. ("?" . org-speed-command-help)
  18058. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18059. (">" . (org-agenda-remove-restriction-lock))
  18060. )
  18061. "The default speed commands.")
  18062. (defun org-print-speed-command (e)
  18063. (if (> (length (car e)) 1)
  18064. (progn
  18065. (princ "\n")
  18066. (princ (car e))
  18067. (princ "\n")
  18068. (princ (make-string (length (car e)) ?-))
  18069. (princ "\n"))
  18070. (princ (car e))
  18071. (princ " ")
  18072. (if (symbolp (cdr e))
  18073. (princ (symbol-name (cdr e)))
  18074. (prin1 (cdr e)))
  18075. (princ "\n")))
  18076. (defun org-speed-command-help ()
  18077. "Show the available speed commands."
  18078. (interactive)
  18079. (if (not org-use-speed-commands)
  18080. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18081. (with-output-to-temp-buffer "*Help*"
  18082. (princ "User-defined Speed commands\n===========================\n")
  18083. (mapc #'org-print-speed-command org-speed-commands-user)
  18084. (princ "\n")
  18085. (princ "Built-in Speed commands\n=======================\n")
  18086. (mapc #'org-print-speed-command org-speed-commands-default))
  18087. (with-current-buffer "*Help*"
  18088. (setq truncate-lines t))))
  18089. (defun org-speed-move-safe (cmd)
  18090. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18091. If not, return to the original position and throw an error."
  18092. (interactive)
  18093. (let ((pos (point)))
  18094. (call-interactively cmd)
  18095. (unless (and (bolp) (org-at-heading-p))
  18096. (goto-char pos)
  18097. (error "Boundary reached while executing %s" cmd))))
  18098. (defvar org-self-insert-command-undo-counter 0)
  18099. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18100. (defvar org-speed-command nil)
  18101. (defun org-speed-command-activate (keys)
  18102. "Hook for activating single-letter speed commands.
  18103. `org-speed-commands-default' specifies a minimal command set.
  18104. Use `org-speed-commands-user' for further customization."
  18105. (when (or (and (bolp) (looking-at org-outline-regexp))
  18106. (and (functionp org-use-speed-commands)
  18107. (funcall org-use-speed-commands)))
  18108. (cdr (assoc keys (append org-speed-commands-user
  18109. org-speed-commands-default)))))
  18110. (defun org-babel-speed-command-activate (keys)
  18111. "Hook for activating single-letter code block commands."
  18112. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18113. (cdr (assoc keys org-babel-key-bindings))))
  18114. (defcustom org-speed-command-hook
  18115. '(org-speed-command-activate org-babel-speed-command-activate)
  18116. "Hook for activating speed commands at strategic locations.
  18117. Hook functions are called in sequence until a valid handler is
  18118. found.
  18119. Each hook takes a single argument, a user-pressed command key
  18120. which is also a `self-insert-command' from the global map.
  18121. Within the hook, examine the cursor position and the command key
  18122. and return nil or a valid handler as appropriate. Handler could
  18123. be one of an interactive command, a function, or a form.
  18124. Set `org-use-speed-commands' to non-nil value to enable this
  18125. hook. The default setting is `org-speed-command-activate'."
  18126. :group 'org-structure
  18127. :version "24.1"
  18128. :type 'hook)
  18129. (defun org-self-insert-command (N)
  18130. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18131. If the cursor is in a table looking at whitespace, the whitespace is
  18132. overwritten, and the table is not marked as requiring realignment."
  18133. (interactive "p")
  18134. (org-check-before-invisible-edit 'insert)
  18135. (cond
  18136. ((and org-use-speed-commands
  18137. (let ((kv (this-command-keys-vector)))
  18138. (setq org-speed-command
  18139. (run-hook-with-args-until-success
  18140. 'org-speed-command-hook
  18141. (make-string 1 (aref kv (1- (length kv))))))))
  18142. (cond
  18143. ((commandp org-speed-command)
  18144. (setq this-command org-speed-command)
  18145. (call-interactively org-speed-command))
  18146. ((functionp org-speed-command)
  18147. (funcall org-speed-command))
  18148. ((and org-speed-command (listp org-speed-command))
  18149. (eval org-speed-command))
  18150. (t (let (org-use-speed-commands)
  18151. (call-interactively 'org-self-insert-command)))))
  18152. ((and
  18153. (org-at-table-p)
  18154. (eq N 1)
  18155. (not (org-region-active-p))
  18156. (progn
  18157. ;; Check if we blank the field, and if that triggers align.
  18158. (and (featurep 'org-table) org-table-auto-blank-field
  18159. (memq last-command
  18160. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18161. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18162. ;; Got extra space, this field does not determine
  18163. ;; column width.
  18164. (let (org-table-may-need-update) (org-table-blank-field))
  18165. ;; No extra space, this field may determine column
  18166. ;; width.
  18167. (org-table-blank-field)))
  18168. t)
  18169. (looking-at "[^|\n]* \\( \\)|"))
  18170. ;; There is room for insertion without re-aligning the table.
  18171. (delete-region (match-beginning 1) (match-end 1))
  18172. (self-insert-command N))
  18173. (t
  18174. (setq org-table-may-need-update t)
  18175. (self-insert-command N)
  18176. (org-fix-tags-on-the-fly)
  18177. (when org-self-insert-cluster-for-undo
  18178. (if (not (eq last-command 'org-self-insert-command))
  18179. (setq org-self-insert-command-undo-counter 1)
  18180. (if (>= org-self-insert-command-undo-counter 20)
  18181. (setq org-self-insert-command-undo-counter 1)
  18182. (and (> org-self-insert-command-undo-counter 0)
  18183. buffer-undo-list (listp buffer-undo-list)
  18184. (not (cadr buffer-undo-list)) ; remove nil entry
  18185. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18186. (setq org-self-insert-command-undo-counter
  18187. (1+ org-self-insert-command-undo-counter))))))))
  18188. (defun org-check-before-invisible-edit (kind)
  18189. "Check is editing if kind KIND would be dangerous with invisible text around.
  18190. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18191. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18192. (when (and org-catch-invisible-edits
  18193. (or (not (boundp 'visible-mode)) (not visible-mode))
  18194. (or (get-char-property (point) 'invisible)
  18195. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18196. ;; OK, we need to take a closer look. Do not consider
  18197. ;; invisibility obtained through text properties (e.g., link
  18198. ;; fontification), as it cannot be toggled.
  18199. (let* ((invisible-at-point
  18200. (pcase (get-char-property-and-overlay (point) 'invisible)
  18201. (`(,_ . ,(and (pred overlayp) o)) o)))
  18202. ;; Assume that point cannot land in the middle of an
  18203. ;; overlay, or between two overlays.
  18204. (invisible-before-point
  18205. (and (not invisible-at-point)
  18206. (not (bobp))
  18207. (pcase (get-char-property-and-overlay (1- (point)) 'invisible)
  18208. (`(,_ . ,(and (pred overlayp) o)) o))))
  18209. (border-and-ok-direction
  18210. (or
  18211. ;; Check if we are acting predictably before invisible
  18212. ;; text.
  18213. (and invisible-at-point
  18214. (memq kind '(insert delete-backward)))
  18215. ;; Check if we are acting predictably after invisible text
  18216. ;; This works not well, and I have turned it off. It seems
  18217. ;; better to always show and stop after invisible text.
  18218. ;; (and (not invisible-at-point) invisible-before-point
  18219. ;; (memq kind '(insert delete)))
  18220. )))
  18221. (when (or invisible-at-point invisible-before-point)
  18222. (when (eq org-catch-invisible-edits 'error)
  18223. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18224. (if (and org-custom-properties-overlays
  18225. (y-or-n-p "Display invisible properties in this buffer? "))
  18226. (org-toggle-custom-properties-visibility)
  18227. ;; Make the area visible
  18228. (save-excursion
  18229. (when invisible-before-point
  18230. (goto-char
  18231. (previous-single-char-property-change (point) 'invisible)))
  18232. ;; Remove whatever overlay is currently making yet-to-be
  18233. ;; edited text invisible. Also remove nested invisibility
  18234. ;; related overlays.
  18235. (delete-overlay (or invisible-at-point invisible-before-point))
  18236. (let ((origin (if invisible-at-point (point) (1- (point)))))
  18237. (while (pcase (get-char-property-and-overlay origin 'invisible)
  18238. (`(,_ . ,(and (pred overlayp) o))
  18239. (delete-overlay o)
  18240. t)))))
  18241. (cond
  18242. ((eq org-catch-invisible-edits 'show)
  18243. ;; That's it, we do the edit after showing
  18244. (message
  18245. "Unfolding invisible region around point before editing")
  18246. (sit-for 1))
  18247. ((and (eq org-catch-invisible-edits 'smart)
  18248. border-and-ok-direction)
  18249. (message "Unfolding invisible region around point before editing"))
  18250. (t
  18251. ;; Don't do the edit, make the user repeat it in full visibility
  18252. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18253. (defun org-fix-tags-on-the-fly ()
  18254. "Align tags in headline at point.
  18255. Unlike to `org-set-tags', it ignores region and sorting."
  18256. (when (and (eq (char-after (line-beginning-position)) ?*) ;short-circuit
  18257. (org-at-heading-p))
  18258. (let ((org-ignore-region t)
  18259. (org-tags-sort-function nil))
  18260. (org-set-tags nil t))))
  18261. (defun org-delete-backward-char (N)
  18262. "Like `delete-backward-char', insert whitespace at field end in tables.
  18263. When deleting backwards, in tables this function will insert whitespace in
  18264. front of the next \"|\" separator, to keep the table aligned. The table will
  18265. still be marked for re-alignment if the field did fill the entire column,
  18266. because, in this case the deletion might narrow the column."
  18267. (interactive "p")
  18268. (save-match-data
  18269. (org-check-before-invisible-edit 'delete-backward)
  18270. (if (and (org-at-table-p)
  18271. (eq N 1)
  18272. (not (org-region-active-p))
  18273. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18274. (looking-at ".*?|"))
  18275. (let ((pos (point))
  18276. (noalign (looking-at "[^|\n\r]* |"))
  18277. (c org-table-may-need-update))
  18278. (backward-delete-char N)
  18279. (unless overwrite-mode
  18280. (skip-chars-forward "^|")
  18281. (insert " ")
  18282. (goto-char (1- pos)))
  18283. ;; noalign: if there were two spaces at the end, this field
  18284. ;; does not determine the width of the column.
  18285. (when noalign (setq org-table-may-need-update c)))
  18286. (backward-delete-char N)
  18287. (org-fix-tags-on-the-fly))))
  18288. (defun org-delete-char (N)
  18289. "Like `delete-char', but insert whitespace at field end in tables.
  18290. When deleting characters, in tables this function will insert whitespace in
  18291. front of the next \"|\" separator, to keep the table aligned. The table will
  18292. still be marked for re-alignment if the field did fill the entire column,
  18293. because, in this case the deletion might narrow the column."
  18294. (interactive "p")
  18295. (save-match-data
  18296. (org-check-before-invisible-edit 'delete)
  18297. (if (and (org-at-table-p)
  18298. (not (bolp))
  18299. (not (= (char-after) ?|))
  18300. (eq N 1))
  18301. (if (looking-at ".*?|")
  18302. (let ((pos (point))
  18303. (noalign (looking-at "[^|\n\r]* |"))
  18304. (c org-table-may-need-update))
  18305. (replace-match
  18306. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18307. (goto-char pos)
  18308. ;; noalign: if there were two spaces at the end, this field
  18309. ;; does not determine the width of the column.
  18310. (when noalign (setq org-table-may-need-update c)))
  18311. (delete-char N))
  18312. (delete-char N)
  18313. (org-fix-tags-on-the-fly))))
  18314. ;; Make `delete-selection-mode' work with Org mode and Orgtbl mode
  18315. (put 'org-self-insert-command 'delete-selection
  18316. (lambda ()
  18317. (not (run-hook-with-args-until-success
  18318. 'self-insert-uses-region-functions))))
  18319. (put 'orgtbl-self-insert-command 'delete-selection
  18320. (lambda ()
  18321. (not (run-hook-with-args-until-success
  18322. 'self-insert-uses-region-functions))))
  18323. (put 'org-delete-char 'delete-selection 'supersede)
  18324. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18325. (put 'org-yank 'delete-selection 'yank)
  18326. ;; Make `flyspell-mode' delay after some commands
  18327. (put 'org-self-insert-command 'flyspell-delayed t)
  18328. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18329. (put 'org-delete-char 'flyspell-delayed t)
  18330. (put 'org-delete-backward-char 'flyspell-delayed t)
  18331. ;; Make pabbrev-mode expand after Org mode commands
  18332. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18333. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18334. (defun org-transpose-words ()
  18335. "Transpose words for Org.
  18336. This uses the `org-mode-transpose-word-syntax-table' syntax
  18337. table, which interprets characters in `org-emphasis-alist' as
  18338. word constituents."
  18339. (interactive)
  18340. (with-syntax-table org-mode-transpose-word-syntax-table
  18341. (call-interactively 'transpose-words)))
  18342. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18343. (defvar org-ctrl-c-ctrl-c-hook nil
  18344. "Hook for functions attaching themselves to `C-c C-c'.
  18345. This can be used to add additional functionality to the C-c C-c
  18346. key which executes context-dependent commands. This hook is run
  18347. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18348. run after the last test.
  18349. Each function will be called with no arguments. The function
  18350. must check if the context is appropriate for it to act. If yes,
  18351. it should do its thing and then return a non-nil value. If the
  18352. context is wrong, just do nothing and return nil.")
  18353. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18354. "Hook for functions attaching themselves to `C-c C-c'.
  18355. This can be used to add additional functionality to the C-c C-c
  18356. key which executes context-dependent commands. This hook is run
  18357. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18358. before the first test.
  18359. Each function will be called with no arguments. The function
  18360. must check if the context is appropriate for it to act. If yes,
  18361. it should do its thing and then return a non-nil value. If the
  18362. context is wrong, just do nothing and return nil.")
  18363. (defvar org-tab-first-hook nil
  18364. "Hook for functions to attach themselves to TAB.
  18365. See `org-ctrl-c-ctrl-c-hook' for more information.
  18366. This hook runs as the first action when TAB is pressed, even before
  18367. `org-cycle' messes around with the `outline-regexp' to cater for
  18368. inline tasks and plain list item folding.
  18369. If any function in this hook returns t, any other actions that
  18370. would have been caused by TAB (such as table field motion or visibility
  18371. cycling) will not occur.")
  18372. (defvar org-tab-after-check-for-table-hook nil
  18373. "Hook for functions to attach themselves to TAB.
  18374. See `org-ctrl-c-ctrl-c-hook' for more information.
  18375. This hook runs after it has been established that the cursor is not in a
  18376. table, but before checking if the cursor is in a headline or if global cycling
  18377. should be done.
  18378. If any function in this hook returns t, not other actions like visibility
  18379. cycling will be done.")
  18380. (defvar org-tab-after-check-for-cycling-hook nil
  18381. "Hook for functions to attach themselves to TAB.
  18382. See `org-ctrl-c-ctrl-c-hook' for more information.
  18383. This hook runs after it has been established that not table field motion and
  18384. not visibility should be done because of current context. This is probably
  18385. the place where a package like yasnippets can hook in.")
  18386. (defvar org-tab-before-tab-emulation-hook nil
  18387. "Hook for functions to attach themselves to TAB.
  18388. See `org-ctrl-c-ctrl-c-hook' for more information.
  18389. This hook runs after every other options for TAB have been exhausted, but
  18390. before indentation and \t insertion takes place.")
  18391. (defvar org-metaleft-hook nil
  18392. "Hook for functions attaching themselves to `M-left'.
  18393. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18394. (defvar org-metaright-hook nil
  18395. "Hook for functions attaching themselves to `M-right'.
  18396. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18397. (defvar org-metaup-hook nil
  18398. "Hook for functions attaching themselves to `M-up'.
  18399. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18400. (defvar org-metadown-hook nil
  18401. "Hook for functions attaching themselves to `M-down'.
  18402. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18403. (defvar org-shiftmetaleft-hook nil
  18404. "Hook for functions attaching themselves to `M-S-left'.
  18405. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18406. (defvar org-shiftmetaright-hook nil
  18407. "Hook for functions attaching themselves to `M-S-right'.
  18408. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18409. (defvar org-shiftmetaup-hook nil
  18410. "Hook for functions attaching themselves to `M-S-up'.
  18411. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18412. (defvar org-shiftmetadown-hook nil
  18413. "Hook for functions attaching themselves to `M-S-down'.
  18414. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18415. (defvar org-metareturn-hook nil
  18416. "Hook for functions attaching themselves to `M-RET'.
  18417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18418. (defvar org-shiftup-hook nil
  18419. "Hook for functions attaching themselves to `S-up'.
  18420. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18421. (defvar org-shiftup-final-hook nil
  18422. "Hook for functions attaching themselves to `S-up'.
  18423. This one runs after all other options except shift-select have been excluded.
  18424. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18425. (defvar org-shiftdown-hook nil
  18426. "Hook for functions attaching themselves to `S-down'.
  18427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18428. (defvar org-shiftdown-final-hook nil
  18429. "Hook for functions attaching themselves to `S-down'.
  18430. This one runs after all other options except shift-select have been excluded.
  18431. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18432. (defvar org-shiftleft-hook nil
  18433. "Hook for functions attaching themselves to `S-left'.
  18434. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18435. (defvar org-shiftleft-final-hook nil
  18436. "Hook for functions attaching themselves to `S-left'.
  18437. This one runs after all other options except shift-select have been excluded.
  18438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18439. (defvar org-shiftright-hook nil
  18440. "Hook for functions attaching themselves to `S-right'.
  18441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18442. (defvar org-shiftright-final-hook nil
  18443. "Hook for functions attaching themselves to `S-right'.
  18444. This one runs after all other options except shift-select have been excluded.
  18445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18446. (defun org-modifier-cursor-error ()
  18447. "Throw an error, a modified cursor command was applied in wrong context."
  18448. (user-error "This command is active in special context like tables, headlines or items"))
  18449. (defun org-shiftselect-error ()
  18450. "Throw an error because Shift-Cursor command was applied in wrong context."
  18451. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18452. (user-error "To use shift-selection with Org mode, customize `org-support-shift-select'")
  18453. (user-error "This command works only in special context like headlines or timestamps")))
  18454. (defun org-call-for-shift-select (cmd)
  18455. (let ((this-command-keys-shift-translated t))
  18456. (call-interactively cmd)))
  18457. (defun org-shifttab (&optional arg)
  18458. "Global visibility cycling or move to previous table field.
  18459. Call `org-table-previous-field' within a table.
  18460. When ARG is nil, cycle globally through visibility states.
  18461. When ARG is a numeric prefix, show contents of this level."
  18462. (interactive "P")
  18463. (cond
  18464. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18465. ((integerp arg)
  18466. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18467. (message "Content view to level: %d" arg)
  18468. (org-content (prefix-numeric-value arg2))
  18469. (org-cycle-show-empty-lines t)
  18470. (setq org-cycle-global-status 'overview)))
  18471. (t (call-interactively 'org-global-cycle))))
  18472. (defun org-shiftmetaleft ()
  18473. "Promote subtree or delete table column.
  18474. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18475. `org-table-delete-column', depending on context. See the
  18476. individual commands for more information."
  18477. (interactive)
  18478. (cond
  18479. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18480. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18481. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18482. ((if (not (org-region-active-p)) (org-at-item-p)
  18483. (save-excursion (goto-char (region-beginning))
  18484. (org-at-item-p)))
  18485. (call-interactively 'org-outdent-item-tree))
  18486. (t (org-modifier-cursor-error))))
  18487. (defun org-shiftmetaright ()
  18488. "Demote subtree or insert table column.
  18489. Calls `org-demote-subtree', `org-indent-item-tree', or
  18490. `org-table-insert-column', depending on context. See the
  18491. individual commands for more information."
  18492. (interactive)
  18493. (cond
  18494. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18495. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18496. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18497. ((if (not (org-region-active-p)) (org-at-item-p)
  18498. (save-excursion (goto-char (region-beginning))
  18499. (org-at-item-p)))
  18500. (call-interactively 'org-indent-item-tree))
  18501. (t (org-modifier-cursor-error))))
  18502. (defun org-shiftmetaup (&optional _arg)
  18503. "Drag the line at point up.
  18504. In a table, kill the current row.
  18505. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18506. but also adjust the previous clocked item in the clock history.
  18507. Everywhere else, drag the line at point up."
  18508. (interactive "P")
  18509. (cond
  18510. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18511. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18512. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18513. (call-interactively 'org-timestamp-up)))
  18514. (t (call-interactively 'org-drag-line-backward))))
  18515. (defun org-shiftmetadown (&optional _arg)
  18516. "Drag the line at point down.
  18517. In a table, insert an empty row at the current line.
  18518. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18519. but also adjust the previous clocked item in the clock history.
  18520. Everywhere else, drag the line at point down."
  18521. (interactive "P")
  18522. (cond
  18523. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18524. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18525. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18526. (call-interactively 'org-timestamp-down)))
  18527. (t (call-interactively 'org-drag-line-forward))))
  18528. (defsubst org-hidden-tree-error ()
  18529. (user-error
  18530. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18531. (defun org-metaleft (&optional _arg)
  18532. "Promote heading, list item at point or move table column left.
  18533. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18534. depending on context. With no specific context, calls the Emacs
  18535. default `backward-word'. See the individual commands for more
  18536. information.
  18537. This function runs the hook `org-metaleft-hook' as a first step,
  18538. and returns at first non-nil value."
  18539. (interactive "P")
  18540. (cond
  18541. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18542. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18543. ((org-with-limited-levels
  18544. (or (org-at-heading-p)
  18545. (and (org-region-active-p)
  18546. (save-excursion
  18547. (goto-char (region-beginning))
  18548. (org-at-heading-p)))))
  18549. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18550. (call-interactively 'org-do-promote))
  18551. ;; At an inline task.
  18552. ((org-at-heading-p)
  18553. (call-interactively 'org-inlinetask-promote))
  18554. ((or (org-at-item-p)
  18555. (and (org-region-active-p)
  18556. (save-excursion
  18557. (goto-char (region-beginning))
  18558. (org-at-item-p))))
  18559. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18560. (call-interactively 'org-outdent-item))
  18561. (t (call-interactively 'backward-word))))
  18562. (defun org-metaright (&optional _arg)
  18563. "Demote heading, list item at point or move table column right.
  18564. In front of a drawer or a block keyword, indent it correctly.
  18565. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18566. `org-indent-drawer' or `org-indent-block' depending on context.
  18567. With no specific context, calls the Emacs default `forward-word'.
  18568. See the individual commands for more information.
  18569. This function runs the hook `org-metaright-hook' as a first step,
  18570. and returns at first non-nil value."
  18571. (interactive "P")
  18572. (cond
  18573. ((run-hook-with-args-until-success 'org-metaright-hook))
  18574. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18575. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18576. ((org-at-block-p) (call-interactively 'org-indent-block))
  18577. ((org-with-limited-levels
  18578. (or (org-at-heading-p)
  18579. (and (org-region-active-p)
  18580. (save-excursion
  18581. (goto-char (region-beginning))
  18582. (org-at-heading-p)))))
  18583. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18584. (call-interactively 'org-do-demote))
  18585. ;; At an inline task.
  18586. ((org-at-heading-p)
  18587. (call-interactively 'org-inlinetask-demote))
  18588. ((or (org-at-item-p)
  18589. (and (org-region-active-p)
  18590. (save-excursion
  18591. (goto-char (region-beginning))
  18592. (org-at-item-p))))
  18593. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18594. (call-interactively 'org-indent-item))
  18595. (t (call-interactively 'forward-word))))
  18596. (defun org-check-for-hidden (what)
  18597. "Check if there are hidden headlines/items in the current visual line.
  18598. WHAT can be either `headlines' or `items'. If the current line is
  18599. an outline or item heading and it has a folded subtree below it,
  18600. this function returns t, nil otherwise."
  18601. (let ((re (cond
  18602. ((eq what 'headlines) org-outline-regexp-bol)
  18603. ((eq what 'items) (org-item-beginning-re))
  18604. (t (error "This should not happen"))))
  18605. beg end)
  18606. (save-excursion
  18607. (catch 'exit
  18608. (unless (org-region-active-p)
  18609. (setq beg (point-at-bol))
  18610. (beginning-of-line 2)
  18611. (while (and (not (eobp)) ;; this is like `next-line'
  18612. (get-char-property (1- (point)) 'invisible))
  18613. (beginning-of-line 2))
  18614. (setq end (point))
  18615. (goto-char beg)
  18616. (goto-char (point-at-eol))
  18617. (setq end (max end (point)))
  18618. (while (re-search-forward re end t)
  18619. (when (get-char-property (match-beginning 0) 'invisible)
  18620. (throw 'exit t))))
  18621. nil))))
  18622. (defun org-metaup (&optional _arg)
  18623. "Move subtree up or move table row up.
  18624. Calls `org-move-subtree-up' or `org-table-move-row' or
  18625. `org-move-item-up', depending on context. See the individual commands
  18626. for more information."
  18627. (interactive "P")
  18628. (cond
  18629. ((run-hook-with-args-until-success 'org-metaup-hook))
  18630. ((org-region-active-p)
  18631. (let* ((a (min (region-beginning) (region-end)))
  18632. (b (1- (max (region-beginning) (region-end))))
  18633. (c (save-excursion (goto-char a)
  18634. (move-beginning-of-line 0)))
  18635. (d (save-excursion (goto-char a)
  18636. (move-end-of-line 0) (point))))
  18637. (transpose-regions a b c d)
  18638. (goto-char c)))
  18639. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18640. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18641. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18642. (t (org-drag-element-backward))))
  18643. (defun org-metadown (&optional _arg)
  18644. "Move subtree down or move table row down.
  18645. Calls `org-move-subtree-down' or `org-table-move-row' or
  18646. `org-move-item-down', depending on context. See the individual
  18647. commands for more information."
  18648. (interactive "P")
  18649. (cond
  18650. ((run-hook-with-args-until-success 'org-metadown-hook))
  18651. ((org-region-active-p)
  18652. (let* ((a (min (region-beginning) (region-end)))
  18653. (b (max (region-beginning) (region-end)))
  18654. (c (save-excursion (goto-char b)
  18655. (move-beginning-of-line 1)))
  18656. (d (save-excursion (goto-char b)
  18657. (move-end-of-line 1) (1+ (point)))))
  18658. (transpose-regions a b c d)
  18659. (goto-char d)))
  18660. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18661. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18662. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18663. (t (org-drag-element-forward))))
  18664. (defun org-shiftup (&optional arg)
  18665. "Increase item in timestamp or increase priority of current headline.
  18666. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18667. depending on context. See the individual commands for more information."
  18668. (interactive "P")
  18669. (cond
  18670. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18671. ((and org-support-shift-select (org-region-active-p))
  18672. (org-call-for-shift-select 'previous-line))
  18673. ((org-at-timestamp-p 'lax)
  18674. (call-interactively (if org-edit-timestamp-down-means-later
  18675. 'org-timestamp-down 'org-timestamp-up)))
  18676. ((and (not (eq org-support-shift-select 'always))
  18677. org-enable-priority-commands
  18678. (org-at-heading-p))
  18679. (call-interactively 'org-priority-up))
  18680. ((and (not org-support-shift-select) (org-at-item-p))
  18681. (call-interactively 'org-previous-item))
  18682. ((org-clocktable-try-shift 'up arg))
  18683. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18684. (org-support-shift-select
  18685. (org-call-for-shift-select 'previous-line))
  18686. (t (org-shiftselect-error))))
  18687. (defun org-shiftdown (&optional arg)
  18688. "Decrease item in timestamp or decrease priority of current headline.
  18689. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18690. depending on context. See the individual commands for more information."
  18691. (interactive "P")
  18692. (cond
  18693. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18694. ((and org-support-shift-select (org-region-active-p))
  18695. (org-call-for-shift-select 'next-line))
  18696. ((org-at-timestamp-p 'lax)
  18697. (call-interactively (if org-edit-timestamp-down-means-later
  18698. 'org-timestamp-up 'org-timestamp-down)))
  18699. ((and (not (eq org-support-shift-select 'always))
  18700. org-enable-priority-commands
  18701. (org-at-heading-p))
  18702. (call-interactively 'org-priority-down))
  18703. ((and (not org-support-shift-select) (org-at-item-p))
  18704. (call-interactively 'org-next-item))
  18705. ((org-clocktable-try-shift 'down arg))
  18706. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18707. (org-support-shift-select
  18708. (org-call-for-shift-select 'next-line))
  18709. (t (org-shiftselect-error))))
  18710. (defun org-shiftright (&optional arg)
  18711. "Cycle the thing at point or in the current line, depending on context.
  18712. Depending on context, this does one of the following:
  18713. - switch a timestamp at point one day into the future
  18714. - on a headline, switch to the next TODO keyword.
  18715. - on an item, switch entire list to the next bullet type
  18716. - on a property line, switch to the next allowed value
  18717. - on a clocktable definition line, move time block into the future"
  18718. (interactive "P")
  18719. (cond
  18720. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18721. ((and org-support-shift-select (org-region-active-p))
  18722. (org-call-for-shift-select 'forward-char))
  18723. ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-up-day))
  18724. ((and (not (eq org-support-shift-select 'always))
  18725. (org-at-heading-p))
  18726. (let ((org-inhibit-logging
  18727. (not org-treat-S-cursor-todo-selection-as-state-change))
  18728. (org-inhibit-blocking
  18729. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18730. (org-call-with-arg 'org-todo 'right)))
  18731. ((or (and org-support-shift-select
  18732. (not (eq org-support-shift-select 'always))
  18733. (org-at-item-bullet-p))
  18734. (and (not org-support-shift-select) (org-at-item-p)))
  18735. (org-call-with-arg 'org-cycle-list-bullet nil))
  18736. ((and (not (eq org-support-shift-select 'always))
  18737. (org-at-property-p))
  18738. (call-interactively 'org-property-next-allowed-value))
  18739. ((org-clocktable-try-shift 'right arg))
  18740. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18741. (org-support-shift-select
  18742. (org-call-for-shift-select 'forward-char))
  18743. (t (org-shiftselect-error))))
  18744. (defun org-shiftleft (&optional arg)
  18745. "Cycle the thing at point or in the current line, depending on context.
  18746. Depending on context, this does one of the following:
  18747. - switch a timestamp at point one day into the past
  18748. - on a headline, switch to the previous TODO keyword.
  18749. - on an item, switch entire list to the previous bullet type
  18750. - on a property line, switch to the previous allowed value
  18751. - on a clocktable definition line, move time block into the past"
  18752. (interactive "P")
  18753. (cond
  18754. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18755. ((and org-support-shift-select (org-region-active-p))
  18756. (org-call-for-shift-select 'backward-char))
  18757. ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-down-day))
  18758. ((and (not (eq org-support-shift-select 'always))
  18759. (org-at-heading-p))
  18760. (let ((org-inhibit-logging
  18761. (not org-treat-S-cursor-todo-selection-as-state-change))
  18762. (org-inhibit-blocking
  18763. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18764. (org-call-with-arg 'org-todo 'left)))
  18765. ((or (and org-support-shift-select
  18766. (not (eq org-support-shift-select 'always))
  18767. (org-at-item-bullet-p))
  18768. (and (not org-support-shift-select) (org-at-item-p)))
  18769. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18770. ((and (not (eq org-support-shift-select 'always))
  18771. (org-at-property-p))
  18772. (call-interactively 'org-property-previous-allowed-value))
  18773. ((org-clocktable-try-shift 'left arg))
  18774. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18775. (org-support-shift-select
  18776. (org-call-for-shift-select 'backward-char))
  18777. (t (org-shiftselect-error))))
  18778. (defun org-shiftcontrolright ()
  18779. "Switch to next TODO set."
  18780. (interactive)
  18781. (cond
  18782. ((and org-support-shift-select (org-region-active-p))
  18783. (org-call-for-shift-select 'forward-word))
  18784. ((and (not (eq org-support-shift-select 'always))
  18785. (org-at-heading-p))
  18786. (org-call-with-arg 'org-todo 'nextset))
  18787. (org-support-shift-select
  18788. (org-call-for-shift-select 'forward-word))
  18789. (t (org-shiftselect-error))))
  18790. (defun org-shiftcontrolleft ()
  18791. "Switch to previous TODO set."
  18792. (interactive)
  18793. (cond
  18794. ((and org-support-shift-select (org-region-active-p))
  18795. (org-call-for-shift-select 'backward-word))
  18796. ((and (not (eq org-support-shift-select 'always))
  18797. (org-at-heading-p))
  18798. (org-call-with-arg 'org-todo 'previousset))
  18799. (org-support-shift-select
  18800. (org-call-for-shift-select 'backward-word))
  18801. (t (org-shiftselect-error))))
  18802. (defun org-shiftcontrolup (&optional n)
  18803. "Change timestamps synchronously up in CLOCK log lines.
  18804. Optional argument N tells to change by that many units."
  18805. (interactive "P")
  18806. (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
  18807. (let (org-support-shift-select)
  18808. (org-clock-timestamps-up n))
  18809. (user-error "Not at a clock log")))
  18810. (defun org-shiftcontroldown (&optional n)
  18811. "Change timestamps synchronously down in CLOCK log lines.
  18812. Optional argument N tells to change by that many units."
  18813. (interactive "P")
  18814. (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
  18815. (let (org-support-shift-select)
  18816. (org-clock-timestamps-down n))
  18817. (user-error "Not at a clock log")))
  18818. (defun org-increase-number-at-point (&optional inc)
  18819. "Increment the number at point.
  18820. With an optional prefix numeric argument INC, increment using
  18821. this numeric value."
  18822. (interactive "p")
  18823. (if (not (number-at-point))
  18824. (user-error "Not on a number")
  18825. (unless inc (setq inc 1))
  18826. (let ((pos (point))
  18827. (beg (skip-chars-backward "-+^/*0-9eE."))
  18828. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18829. (setq nap (buffer-substring-no-properties
  18830. (+ pos beg) (+ pos beg end)))
  18831. (delete-region (+ pos beg) (+ pos beg end))
  18832. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18833. (when (org-at-table-p)
  18834. (org-table-align)
  18835. (org-table-end-of-field 1))))
  18836. (defun org-decrease-number-at-point (&optional inc)
  18837. "Decrement the number at point.
  18838. With an optional prefix numeric argument INC, decrement using
  18839. this numeric value."
  18840. (interactive "p")
  18841. (org-increase-number-at-point (- (or inc 1))))
  18842. (defun org-ctrl-c-ret ()
  18843. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18844. (interactive)
  18845. (cond
  18846. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18847. (t (call-interactively 'org-insert-heading))))
  18848. (defun org-find-visible ()
  18849. (let ((s (point)))
  18850. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18851. (get-char-property s 'invisible)))
  18852. s))
  18853. (defun org-find-invisible ()
  18854. (let ((s (point)))
  18855. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18856. (not (get-char-property s 'invisible))))
  18857. s))
  18858. (defun org-copy-visible (beg end)
  18859. "Copy the visible parts of the region."
  18860. (interactive "r")
  18861. (let ((result ""))
  18862. (while (/= beg end)
  18863. (when (get-char-property beg 'invisible)
  18864. (setq beg (next-single-char-property-change beg 'invisible nil end)))
  18865. (let ((next (next-single-char-property-change beg 'invisible nil end)))
  18866. (setq result (concat result (buffer-substring beg next)))
  18867. (setq beg next)))
  18868. (kill-new result)))
  18869. (defun org-copy-special ()
  18870. "Copy region in table or copy current subtree.
  18871. Calls `org-table-copy-region' or `org-copy-subtree', depending on
  18872. context. See the individual commands for more information."
  18873. (interactive)
  18874. (call-interactively
  18875. (if (org-at-table-p) #'org-table-copy-region #'org-copy-subtree)))
  18876. (defun org-cut-special ()
  18877. "Cut region in table or cut current subtree.
  18878. Calls `org-table-cut-region' or `org-cut-subtree', depending on
  18879. context. See the individual commands for more information."
  18880. (interactive)
  18881. (call-interactively
  18882. (if (org-at-table-p) #'org-table-cut-region #'org-cut-subtree)))
  18883. (defun org-paste-special (arg)
  18884. "Paste rectangular region into table, or past subtree relative to level.
  18885. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18886. See the individual commands for more information."
  18887. (interactive "P")
  18888. (if (org-at-table-p)
  18889. (org-table-paste-rectangle)
  18890. (org-paste-subtree arg)))
  18891. (defun org-edit-special (&optional arg)
  18892. "Call a special editor for the element at point.
  18893. When at a table, call the formula editor with `org-table-edit-formulas'.
  18894. When in a source code block, call `org-edit-src-code'.
  18895. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18896. When in an export block, call `org-edit-export-block'.
  18897. When in a LaTeX environment, call `org-edit-latex-environment'.
  18898. When at an #+INCLUDE keyword, visit the included file.
  18899. When at a footnote reference, call `org-edit-footnote-reference'
  18900. On a link, call `ffap' to visit the link at point.
  18901. Otherwise, return a user error."
  18902. (interactive "P")
  18903. (let ((element (org-element-at-point)))
  18904. (barf-if-buffer-read-only)
  18905. (pcase (org-element-type element)
  18906. (`src-block
  18907. (if (not arg) (org-edit-src-code)
  18908. (let* ((info (org-babel-get-src-block-info))
  18909. (lang (nth 0 info))
  18910. (params (nth 2 info))
  18911. (session (cdr (assq :session params))))
  18912. (if (not session) (org-edit-src-code)
  18913. ;; At a src-block with a session and function called with
  18914. ;; an ARG: switch to the buffer related to the inferior
  18915. ;; process.
  18916. (switch-to-buffer
  18917. (funcall (intern (concat "org-babel-prep-session:" lang))
  18918. session params))))))
  18919. (`keyword
  18920. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18921. (org-open-link-from-string
  18922. (format "[[%s]]"
  18923. (expand-file-name
  18924. (let ((value (org-element-property :value element)))
  18925. (cond ((org-file-url-p value)
  18926. (user-error "The file is specified as a URL, cannot be edited"))
  18927. ((not (org-string-nw-p value))
  18928. (user-error "No file to edit"))
  18929. ((string-match "\\`\"\\(.*?\\)\"" value)
  18930. (match-string 1 value))
  18931. ((string-match "\\`[^ \t\"]\\S-*" value)
  18932. (match-string 0 value))
  18933. (t (user-error "No valid file specified")))))))
  18934. (user-error "No special environment to edit here")))
  18935. (`table
  18936. (if (eq (org-element-property :type element) 'table.el)
  18937. (org-edit-table.el)
  18938. (call-interactively 'org-table-edit-formulas)))
  18939. ;; Only Org tables contain `table-row' type elements.
  18940. (`table-row (call-interactively 'org-table-edit-formulas))
  18941. (`example-block (org-edit-src-code))
  18942. (`export-block (org-edit-export-block))
  18943. (`fixed-width (org-edit-fixed-width-region))
  18944. (`latex-environment (org-edit-latex-environment))
  18945. (_
  18946. ;; No notable element at point. Though, we may be at a link or
  18947. ;; a footnote reference, which are objects. Thus, scan deeper.
  18948. (let ((context (org-element-context element)))
  18949. (pcase (org-element-type context)
  18950. (`footnote-reference (org-edit-footnote-reference))
  18951. (`inline-src-block (org-edit-inline-src-code))
  18952. (`link (call-interactively #'ffap))
  18953. (_ (user-error "No special environment to edit here"))))))))
  18954. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18955. (defun org-ctrl-c-ctrl-c (&optional arg)
  18956. "Set tags in headline, or update according to changed information at point.
  18957. This command does many different things, depending on context:
  18958. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18959. this is what we do.
  18960. - If the cursor is on a statistics cookie, update it.
  18961. - If the cursor is in a headline, prompt for tags and insert them
  18962. into the current line, aligned to `org-tags-column'. When called
  18963. with prefix arg, realign all tags in the current buffer.
  18964. - If the cursor is in one of the special #+KEYWORD lines, this
  18965. triggers scanning the buffer for these lines and updating the
  18966. information.
  18967. - If the cursor is inside a table, realign the table. This command
  18968. works even if the automatic table editor has been turned off.
  18969. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18970. the entire table.
  18971. - If the cursor is at a footnote reference or definition, jump to
  18972. the corresponding definition or references, respectively.
  18973. - If the cursor is a the beginning of a dynamic block, update it.
  18974. - If the current buffer is a capture buffer, close note and file it.
  18975. - If the cursor is on a <<<target>>>, update radio targets and
  18976. corresponding links in this buffer.
  18977. - If the cursor is on a numbered item in a plain list, renumber the
  18978. ordered list.
  18979. - If the cursor is on a checkbox, toggle it.
  18980. - If the cursor is on a code block, evaluate it. The variable
  18981. `org-confirm-babel-evaluate' can be used to control prompting
  18982. before code block evaluation, by default every code block
  18983. evaluation requires confirmation. Code block evaluation can be
  18984. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18985. (interactive "P")
  18986. (cond
  18987. ((or (bound-and-true-p org-clock-overlays) org-occur-highlights)
  18988. (when (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18989. (org-remove-occur-highlights)
  18990. (message "Temporary highlights/overlays removed from current buffer"))
  18991. ((and (local-variable-p 'org-finish-function)
  18992. (fboundp org-finish-function))
  18993. (funcall org-finish-function))
  18994. ((org-babel-hash-at-point))
  18995. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18996. (t
  18997. (let* ((context
  18998. (org-element-lineage
  18999. (org-element-context)
  19000. ;; Limit to supported contexts.
  19001. '(babel-call clock dynamic-block footnote-definition
  19002. footnote-reference inline-babel-call inline-src-block
  19003. inlinetask item keyword node-property paragraph
  19004. plain-list planning property-drawer radio-target
  19005. src-block statistics-cookie table table-cell table-row
  19006. timestamp)
  19007. t))
  19008. (type (org-element-type context)))
  19009. ;; For convenience: at the first line of a paragraph on the same
  19010. ;; line as an item, apply function on that item instead.
  19011. (when (eq type 'paragraph)
  19012. (let ((parent (org-element-property :parent context)))
  19013. (when (and (eq (org-element-type parent) 'item)
  19014. (= (line-beginning-position)
  19015. (org-element-property :begin parent)))
  19016. (setq context parent)
  19017. (setq type 'item))))
  19018. ;; Act according to type of element or object at point.
  19019. ;;
  19020. ;; Do nothing on a blank line, except if it is contained in
  19021. ;; a src block. Hence, we first check if point is in such
  19022. ;; a block and then if it is at a blank line.
  19023. (pcase type
  19024. ((or `inline-src-block `src-block)
  19025. (unless org-babel-no-eval-on-ctrl-c-ctrl-c
  19026. (org-babel-eval-wipe-error-buffer)
  19027. (org-babel-execute-src-block
  19028. current-prefix-arg (org-babel-get-src-block-info nil context))))
  19029. ((guard (org-match-line "[ \t]*$"))
  19030. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19031. (user-error
  19032. (substitute-command-keys
  19033. "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))
  19034. ((or `babel-call `inline-babel-call)
  19035. (let ((info (org-babel-lob-get-info context)))
  19036. (when info (org-babel-execute-src-block nil info))))
  19037. (`clock (org-clock-update-time-maybe))
  19038. (`dynamic-block
  19039. (save-excursion
  19040. (goto-char (org-element-property :post-affiliated context))
  19041. (org-update-dblock)))
  19042. (`footnote-definition
  19043. (goto-char (org-element-property :post-affiliated context))
  19044. (call-interactively 'org-footnote-action))
  19045. (`footnote-reference (call-interactively #'org-footnote-action))
  19046. ((or `headline `inlinetask)
  19047. (save-excursion (goto-char (org-element-property :begin context))
  19048. (call-interactively #'org-set-tags)))
  19049. (`item
  19050. ;; At an item: `C-u C-u' sets checkbox to "[-]"
  19051. ;; unconditionally, whereas `C-u' will toggle its presence.
  19052. ;; Without a universal argument, if the item has a checkbox,
  19053. ;; toggle it. Otherwise repair the list.
  19054. (let* ((box (org-element-property :checkbox context))
  19055. (struct (org-element-property :structure context))
  19056. (old-struct (copy-tree struct))
  19057. (parents (org-list-parents-alist struct))
  19058. (prevs (org-list-prevs-alist struct))
  19059. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19060. (org-list-set-checkbox
  19061. (org-element-property :begin context) struct
  19062. (cond ((equal arg '(16)) "[-]")
  19063. ((and (not box) (equal arg '(4))) "[ ]")
  19064. ((or (not box) (equal arg '(4))) nil)
  19065. ((eq box 'on) "[ ]")
  19066. (t "[X]")))
  19067. ;; Mimic `org-list-write-struct' but with grabbing a return
  19068. ;; value from `org-list-struct-fix-box'.
  19069. (org-list-struct-fix-ind struct parents 2)
  19070. (org-list-struct-fix-item-end struct)
  19071. (org-list-struct-fix-bul struct prevs)
  19072. (org-list-struct-fix-ind struct parents)
  19073. (let ((block-item
  19074. (org-list-struct-fix-box struct parents prevs orderedp)))
  19075. (if (and box (equal struct old-struct))
  19076. (if (equal arg '(16))
  19077. (message "Checkboxes already reset")
  19078. (user-error "Cannot toggle this checkbox: %s"
  19079. (if (eq box 'on)
  19080. "all subitems checked"
  19081. "unchecked subitems")))
  19082. (org-list-struct-apply-struct struct old-struct)
  19083. (org-update-checkbox-count-maybe))
  19084. (when block-item
  19085. (message "Checkboxes were removed due to empty box at line %d"
  19086. (org-current-line block-item))))))
  19087. (`keyword
  19088. (let ((org-inhibit-startup-visibility-stuff t)
  19089. (org-startup-align-all-tables nil))
  19090. (when (boundp 'org-table-coordinate-overlays)
  19091. (mapc #'delete-overlay org-table-coordinate-overlays)
  19092. (setq org-table-coordinate-overlays nil))
  19093. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19094. (message "Local setup has been refreshed"))
  19095. (`plain-list
  19096. ;; At a plain list, with a double C-u argument, set
  19097. ;; checkboxes of each item to "[-]", whereas a single one
  19098. ;; will toggle their presence according to the state of the
  19099. ;; first item in the list. Without an argument, repair the
  19100. ;; list.
  19101. (let* ((begin (org-element-property :contents-begin context))
  19102. (beginm (move-marker (make-marker) begin))
  19103. (struct (org-element-property :structure context))
  19104. (old-struct (copy-tree struct))
  19105. (first-box (save-excursion
  19106. (goto-char begin)
  19107. (looking-at org-list-full-item-re)
  19108. (match-string-no-properties 3)))
  19109. (new-box (cond ((equal arg '(16)) "[-]")
  19110. ((equal arg '(4)) (unless first-box "[ ]"))
  19111. ((equal first-box "[X]") "[ ]")
  19112. (t "[X]"))))
  19113. (cond
  19114. (arg
  19115. (dolist (pos
  19116. (org-list-get-all-items
  19117. begin struct (org-list-prevs-alist struct)))
  19118. (org-list-set-checkbox pos struct new-box)))
  19119. ((and first-box (eq (point) begin))
  19120. ;; For convenience, when point is at bol on the first
  19121. ;; item of the list and no argument is provided, simply
  19122. ;; toggle checkbox of that item, if any.
  19123. (org-list-set-checkbox begin struct new-box)))
  19124. (org-list-write-struct
  19125. struct (org-list-parents-alist struct) old-struct)
  19126. (org-update-checkbox-count-maybe)
  19127. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19128. ((or `property-drawer `node-property)
  19129. (call-interactively #'org-property-action))
  19130. (`radio-target
  19131. (call-interactively #'org-update-radio-target-regexp))
  19132. (`statistics-cookie
  19133. (call-interactively #'org-update-statistics-cookies))
  19134. ((or `table `table-cell `table-row)
  19135. ;; At a table, recalculate every field and align it. Also
  19136. ;; send the table if necessary. If the table has
  19137. ;; a `table.el' type, just give up. At a table row or cell,
  19138. ;; maybe recalculate line but always align table.
  19139. (if (eq (org-element-property :type context) 'table.el)
  19140. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19141. Use `\\[org-edit-special]' to edit table.el tables"))
  19142. (if (or (eq type 'table)
  19143. ;; Check if point is at a TBLFM line.
  19144. (and (eq type 'table-row)
  19145. (= (point) (org-element-property :end context))))
  19146. (save-excursion
  19147. (if (org-at-TBLFM-p)
  19148. (progn (require 'org-table)
  19149. (org-table-calc-current-TBLFM))
  19150. (goto-char (org-element-property :contents-begin context))
  19151. (org-call-with-arg 'org-table-recalculate (or arg t))
  19152. (orgtbl-send-table 'maybe)))
  19153. (org-table-maybe-eval-formula)
  19154. (cond (arg (call-interactively #'org-table-recalculate))
  19155. ((org-table-maybe-recalculate-line))
  19156. (t (org-table-align))))))
  19157. ((or `timestamp (and `planning (guard (org-at-timestamp-p 'lax))))
  19158. (org-timestamp-change 0 'day))
  19159. ((and `nil (guard (org-at-heading-p)))
  19160. ;; When point is on an unsupported object type, we can miss
  19161. ;; the fact that it also is at a heading. Handle it here.
  19162. (call-interactively #'org-set-tags))
  19163. ((guard
  19164. (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)))
  19165. (_
  19166. (user-error
  19167. (substitute-command-keys
  19168. "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))))))
  19169. (defun org-mode-restart ()
  19170. (interactive)
  19171. (let ((indent-status (bound-and-true-p org-indent-mode)))
  19172. (funcall major-mode)
  19173. (hack-local-variables)
  19174. (when (and indent-status (not (bound-and-true-p org-indent-mode)))
  19175. (org-indent-mode -1))
  19176. (org-reset-file-cache))
  19177. (message "%s restarted" major-mode))
  19178. (defun org-kill-note-or-show-branches ()
  19179. "Abort storing current note, or call `outline-show-branches'."
  19180. (interactive)
  19181. (if (not org-finish-function)
  19182. (progn
  19183. (outline-hide-subtree)
  19184. (call-interactively 'outline-show-branches))
  19185. (let ((org-note-abort t))
  19186. (funcall org-finish-function))))
  19187. (defun org-delete-indentation (&optional arg)
  19188. "Join current line to previous and fix whitespace at join.
  19189. If previous line is a headline add to headline title. Otherwise
  19190. the function calls `delete-indentation'.
  19191. With a non-nil optional argument, join it to the following one."
  19192. (interactive "*P")
  19193. (if (save-excursion
  19194. (beginning-of-line (if arg 1 0))
  19195. (let ((case-fold-search nil))
  19196. (looking-at org-complex-heading-regexp)))
  19197. ;; At headline.
  19198. (let ((tags-column (when (match-beginning 5)
  19199. (save-excursion (goto-char (match-beginning 5))
  19200. (current-column))))
  19201. (string (concat " " (progn (when arg (forward-line 1))
  19202. (org-trim (delete-and-extract-region
  19203. (line-beginning-position)
  19204. (line-end-position)))))))
  19205. (unless (bobp) (delete-region (point) (1- (point))))
  19206. (goto-char (or (match-end 4)
  19207. (match-beginning 5)
  19208. (match-end 0)))
  19209. (skip-chars-backward " \t")
  19210. (save-excursion (insert string))
  19211. ;; Adjust alignment of tags.
  19212. (cond
  19213. ((not tags-column)) ;no tags
  19214. (org-auto-align-tags (org-set-tags nil t))
  19215. (t (org--align-tags-here tags-column)))) ;preserve tags column
  19216. (delete-indentation arg)))
  19217. (defun org-open-line (n)
  19218. "Insert a new row in tables, call `open-line' elsewhere.
  19219. If `org-special-ctrl-o' is nil, just call `open-line' everywhere.
  19220. As a special case, when a document starts with a table, allow to
  19221. call `open-line' on the very first character."
  19222. (interactive "*p")
  19223. (if (and org-special-ctrl-o (/= (point) 1) (org-at-table-p))
  19224. (org-table-insert-row)
  19225. (open-line n)))
  19226. (defun org-return (&optional indent)
  19227. "Goto next table row or insert a newline.
  19228. Calls `org-table-next-row' or `newline', depending on context.
  19229. When optional INDENT argument is non-nil, call
  19230. `newline-and-indent' instead of `newline'.
  19231. When `org-return-follows-link' is non-nil and point is on
  19232. a timestamp or a link, call `org-open-at-point'. However, it
  19233. will not happen if point is in a table or on a \"dead\"
  19234. object (e.g., within a comment). In these case, you need to use
  19235. `org-open-at-point' directly."
  19236. (interactive)
  19237. (let ((context (if org-return-follows-link (org-element-context)
  19238. (org-element-at-point))))
  19239. (cond
  19240. ;; In a table, call `org-table-next-row'. However, before first
  19241. ;; column or after last one, split the table.
  19242. ((or (and (eq (org-element-type context) 'table)
  19243. (>= (point) (org-element-property :contents-begin context))
  19244. (< (point) (org-element-property :contents-end context)))
  19245. (org-element-lineage context '(table-row table-cell) t))
  19246. (if (or (looking-at-p "[ \t]*$")
  19247. (save-excursion (skip-chars-backward " \t") (bolp)))
  19248. (insert "\n")
  19249. (org-table-justify-field-maybe)
  19250. (call-interactively #'org-table-next-row)))
  19251. ;; On a link or a timestamp, call `org-open-at-point' if
  19252. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19253. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19254. ((and org-return-follows-link
  19255. (or (org-in-regexp org-ts-regexp-both nil t)
  19256. (org-in-regexp org-tsr-regexp-both nil t)
  19257. (org-in-regexp org-any-link-re nil t)))
  19258. (call-interactively #'org-open-at-point))
  19259. ;; Insert newline in heading, but preserve tags.
  19260. ((and (not (bolp))
  19261. (save-excursion (beginning-of-line)
  19262. (let ((case-fold-search nil))
  19263. (looking-at org-complex-heading-regexp))))
  19264. ;; At headline. Split line. However, if point is on keyword,
  19265. ;; priority cookie or tags, do not break any of them: add
  19266. ;; a newline after the headline instead.
  19267. (let ((tags-column (and (match-beginning 5)
  19268. (save-excursion (goto-char (match-beginning 5))
  19269. (current-column))))
  19270. (string
  19271. (when (and (match-end 4) (org-point-in-group (point) 4))
  19272. (delete-and-extract-region (point) (match-end 4)))))
  19273. ;; Adjust tag alignment.
  19274. (cond
  19275. ((not (and tags-column string)))
  19276. (org-auto-align-tags (org-set-tags nil t))
  19277. (t (org--align-tags-here tags-column))) ;preserve tags column
  19278. (end-of-line)
  19279. (org-show-entry)
  19280. (if indent (newline-and-indent) (newline))
  19281. (when string (save-excursion (insert (org-trim string))))))
  19282. ;; In a list, make sure indenting keeps trailing text within.
  19283. ((and indent
  19284. (not (eolp))
  19285. (org-element-lineage context '(item)))
  19286. (let ((trailing-data
  19287. (delete-and-extract-region (point) (line-end-position))))
  19288. (newline-and-indent)
  19289. (save-excursion (insert trailing-data))))
  19290. (t (if indent (newline-and-indent) (newline))))))
  19291. (defun org-return-indent ()
  19292. "Goto next table row or insert a newline and indent.
  19293. Calls `org-table-next-row' or `newline-and-indent', depending on
  19294. context. See the individual commands for more information."
  19295. (interactive)
  19296. (org-return t))
  19297. (defun org-ctrl-c-star ()
  19298. "Compute table, or change heading status of lines.
  19299. Calls `org-table-recalculate' or `org-toggle-heading',
  19300. depending on context."
  19301. (interactive)
  19302. (cond
  19303. ((org-at-table-p)
  19304. (call-interactively 'org-table-recalculate))
  19305. (t
  19306. ;; Convert all lines in region to list items
  19307. (call-interactively 'org-toggle-heading))))
  19308. (defun org-ctrl-c-minus ()
  19309. "Insert separator line in table or modify bullet status of line.
  19310. Also turns a plain line or a region of lines into list items.
  19311. Calls `org-table-insert-hline', `org-toggle-item', or
  19312. `org-cycle-list-bullet', depending on context."
  19313. (interactive)
  19314. (cond
  19315. ((org-at-table-p)
  19316. (call-interactively 'org-table-insert-hline))
  19317. ((org-region-active-p)
  19318. (call-interactively 'org-toggle-item))
  19319. ((org-in-item-p)
  19320. (call-interactively 'org-cycle-list-bullet))
  19321. (t
  19322. (call-interactively 'org-toggle-item))))
  19323. (defun org-toggle-heading (&optional nstars)
  19324. "Convert headings to normal text, or items or text to headings.
  19325. If there is no active region, only convert the current line.
  19326. With a `\\[universal-argument]' prefix, convert the whole list at
  19327. point into heading.
  19328. In a region:
  19329. - If the first non blank line is a headline, remove the stars
  19330. from all headlines in the region.
  19331. - If it is a normal line, turn each and every normal line (i.e.,
  19332. not an heading or an item) in the region into headings. If you
  19333. want to convert only the first line of this region, use one
  19334. universal prefix argument.
  19335. - If it is a plain list item, turn all plain list items into headings.
  19336. When converting a line into a heading, the number of stars is chosen
  19337. such that the lines become children of the current entry. However,
  19338. when a numeric prefix argument is given, its value determines the
  19339. number of stars to add."
  19340. (interactive "P")
  19341. (let ((skip-blanks
  19342. (function
  19343. ;; Return beginning of first non-blank line, starting from
  19344. ;; line at POS.
  19345. (lambda (pos)
  19346. (save-excursion
  19347. (goto-char pos)
  19348. (while (org-at-comment-p) (forward-line))
  19349. (skip-chars-forward " \r\t\n")
  19350. (point-at-bol)))))
  19351. beg end toggled)
  19352. ;; Determine boundaries of changes. If a universal prefix has
  19353. ;; been given, put the list in a region. If region ends at a bol,
  19354. ;; do not consider the last line to be in the region.
  19355. (when (and current-prefix-arg (org-at-item-p))
  19356. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19357. (org-mark-element))
  19358. (if (org-region-active-p)
  19359. (setq beg (funcall skip-blanks (region-beginning))
  19360. end (copy-marker (save-excursion
  19361. (goto-char (region-end))
  19362. (if (bolp) (point) (point-at-eol)))))
  19363. (setq beg (funcall skip-blanks (point-at-bol))
  19364. end (copy-marker (point-at-eol))))
  19365. ;; Ensure inline tasks don't count as headings.
  19366. (org-with-limited-levels
  19367. (save-excursion
  19368. (goto-char beg)
  19369. (cond
  19370. ;; Case 1. Started at an heading: de-star headings.
  19371. ((org-at-heading-p)
  19372. (while (< (point) end)
  19373. (when (org-at-heading-p t)
  19374. (looking-at org-outline-regexp) (replace-match "")
  19375. (setq toggled t))
  19376. (forward-line)))
  19377. ;; Case 2. Started at an item: change items into headlines.
  19378. ;; One star will be added by `org-list-to-subtree'.
  19379. ((org-at-item-p)
  19380. (while (< (point) end)
  19381. (when (org-at-item-p)
  19382. ;; Pay attention to cases when region ends before list.
  19383. (let* ((struct (org-list-struct))
  19384. (list-end
  19385. (min (org-list-get-bottom-point struct) (1+ end))))
  19386. (save-restriction
  19387. (narrow-to-region (point) list-end)
  19388. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19389. (setq toggled t))
  19390. (forward-line)))
  19391. ;; Case 3. Started at normal text: make every line an heading,
  19392. ;; skipping headlines and items.
  19393. (t (let* ((stars
  19394. (make-string
  19395. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19396. (add-stars
  19397. (cond (nstars "") ; stars from prefix only
  19398. ((equal stars "") "*") ; before first heading
  19399. (org-odd-levels-only "**") ; inside heading, odd
  19400. (t "*"))) ; inside heading, oddeven
  19401. (rpl (concat stars add-stars " "))
  19402. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19403. (while (< (point) (if (equal nstars '(4)) lend end))
  19404. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19405. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19406. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19407. (forward-line)))))))
  19408. (unless toggled (message "Cannot toggle heading from here"))))
  19409. (defun org-meta-return (&optional arg)
  19410. "Insert a new heading or wrap a region in a table.
  19411. Calls `org-insert-heading', `org-insert-item' or
  19412. `org-table-wrap-region', depending on context. When called with
  19413. an argument, unconditionally call `org-insert-heading'."
  19414. (interactive "P")
  19415. (org-check-before-invisible-edit 'insert)
  19416. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19417. (call-interactively (cond (arg #'org-insert-heading)
  19418. ((org-at-table-p) #'org-table-wrap-region)
  19419. ((org-in-item-p) #'org-insert-item)
  19420. (t #'org-insert-heading)))))
  19421. ;;; Menu entries
  19422. (defsubst org-in-subtree-not-table-p ()
  19423. "Are we in a subtree and not in a table?"
  19424. (and (not (org-before-first-heading-p))
  19425. (not (org-at-table-p))))
  19426. ;; Define the Org mode menus
  19427. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19428. '("Tbl"
  19429. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19430. ["Next Field" org-cycle (org-at-table-p)]
  19431. ["Previous Field" org-shifttab (org-at-table-p)]
  19432. ["Next Row" org-return (org-at-table-p)]
  19433. "--"
  19434. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19435. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19436. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19437. "--"
  19438. ("Column"
  19439. ["Move Column Left" org-metaleft (org-at-table-p)]
  19440. ["Move Column Right" org-metaright (org-at-table-p)]
  19441. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19442. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19443. ("Row"
  19444. ["Move Row Up" org-metaup (org-at-table-p)]
  19445. ["Move Row Down" org-metadown (org-at-table-p)]
  19446. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19447. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19448. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19449. "--"
  19450. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19451. ("Rectangle"
  19452. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19453. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19454. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19455. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19456. "--"
  19457. ("Calculate"
  19458. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19459. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19460. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19461. "--"
  19462. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19463. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19464. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19465. "--"
  19466. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19467. "--"
  19468. ["Sum Column/Rectangle" org-table-sum
  19469. (or (org-at-table-p) (org-region-active-p))]
  19470. ["Which Column?" org-table-current-column (org-at-table-p)])
  19471. ["Debug Formulas"
  19472. org-table-toggle-formula-debugger
  19473. :style toggle :selected (bound-and-true-p org-table-formula-debug)]
  19474. ["Show Col/Row Numbers"
  19475. org-table-toggle-coordinate-overlays
  19476. :style toggle
  19477. :selected (bound-and-true-p org-table-overlay-coordinates)]
  19478. "--"
  19479. ["Create" org-table-create (not (org-at-table-p))]
  19480. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19481. ["Import from File" org-table-import (not (org-at-table-p))]
  19482. ["Export to File" org-table-export (org-at-table-p)]
  19483. "--"
  19484. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19485. "--"
  19486. ("Plot"
  19487. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19488. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19489. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19490. '("Org"
  19491. ("Show/Hide"
  19492. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19493. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19494. ["Sparse Tree..." org-sparse-tree t]
  19495. ["Reveal Context" org-reveal t]
  19496. ["Show All" outline-show-all t]
  19497. "--"
  19498. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19499. "--"
  19500. ["New Heading" org-insert-heading t]
  19501. ("Navigate Headings"
  19502. ["Up" outline-up-heading t]
  19503. ["Next" outline-next-visible-heading t]
  19504. ["Previous" outline-previous-visible-heading t]
  19505. ["Next Same Level" outline-forward-same-level t]
  19506. ["Previous Same Level" outline-backward-same-level t]
  19507. "--"
  19508. ["Jump" org-goto t])
  19509. ("Edit Structure"
  19510. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19511. "--"
  19512. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19513. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19514. "--"
  19515. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19516. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19517. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19518. "--"
  19519. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19520. "--"
  19521. ["Copy visible text" org-copy-visible t]
  19522. "--"
  19523. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19524. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19525. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19526. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19527. "--"
  19528. ["Sort Region/Children" org-sort t]
  19529. "--"
  19530. ["Convert to odd levels" org-convert-to-odd-levels t]
  19531. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19532. ("Editing"
  19533. ["Emphasis..." org-emphasize t]
  19534. ["Edit Source Example" org-edit-special t]
  19535. "--"
  19536. ["Footnote new/jump" org-footnote-action t]
  19537. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19538. ("Archive"
  19539. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19540. "--"
  19541. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19542. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19543. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19544. )
  19545. "--"
  19546. ("Hyperlinks"
  19547. ["Store Link (Global)" org-store-link t]
  19548. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19549. ["Insert Link" org-insert-link t]
  19550. ["Follow Link" org-open-at-point t]
  19551. "--"
  19552. ["Next link" org-next-link t]
  19553. ["Previous link" org-previous-link t]
  19554. "--"
  19555. ["Descriptive Links"
  19556. org-toggle-link-display
  19557. :style radio
  19558. :selected org-descriptive-links
  19559. ]
  19560. ["Literal Links"
  19561. org-toggle-link-display
  19562. :style radio
  19563. :selected (not org-descriptive-links)])
  19564. "--"
  19565. ("TODO Lists"
  19566. ["TODO/DONE/-" org-todo t]
  19567. ("Select keyword"
  19568. ["Next keyword" org-shiftright (org-at-heading-p)]
  19569. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19570. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19571. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19572. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19573. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19574. ["Global TODO list" org-todo-list :active t :keys "\\[org-agenda] t"]
  19575. "--"
  19576. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19577. :selected org-enforce-todo-dependencies :style toggle :active t]
  19578. "Settings for tree at point"
  19579. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19580. :selected (org-entry-get nil "ORDERED")
  19581. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19582. ["Do Children parallel" org-toggle-ordered-property :style radio
  19583. :selected (not (org-entry-get nil "ORDERED"))
  19584. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19585. "--"
  19586. ["Set Priority" org-priority t]
  19587. ["Priority Up" org-shiftup t]
  19588. ["Priority Down" org-shiftdown t]
  19589. "--"
  19590. ["Get news from all feeds" org-feed-update-all t]
  19591. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19592. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19593. ("TAGS and Properties"
  19594. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19595. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19596. "--"
  19597. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19598. ["Column view of properties" org-columns t]
  19599. ["Insert Column View DBlock" org-columns-insert-dblock t])
  19600. ("Dates and Scheduling"
  19601. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19602. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19603. ("Change Date"
  19604. ["1 Day Later" org-shiftright (org-at-timestamp-p 'lax)]
  19605. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p 'lax)]
  19606. ["1 ... Later" org-shiftup (org-at-timestamp-p 'lax)]
  19607. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p 'lax)])
  19608. ["Compute Time Range" org-evaluate-time-range t]
  19609. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19610. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19611. "--"
  19612. ["Custom time format" org-toggle-time-stamp-overlays
  19613. :style radio :selected org-display-custom-times]
  19614. "--"
  19615. ["Goto Calendar" org-goto-calendar t]
  19616. ["Date from Calendar" org-date-from-calendar t]
  19617. "--"
  19618. ["Start/Restart Timer" org-timer-start t]
  19619. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19620. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19621. ["Insert Timer String" org-timer t]
  19622. ["Insert Timer Item" org-timer-item t])
  19623. ("Logging work"
  19624. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19625. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19626. ["Clock out" org-clock-out t]
  19627. ["Clock cancel" org-clock-cancel t]
  19628. "--"
  19629. ["Mark as default task" org-clock-mark-default-task t]
  19630. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19631. ["Goto running clock" org-clock-goto t]
  19632. "--"
  19633. ["Display times" org-clock-display t]
  19634. ["Create clock table" org-clock-report t]
  19635. "--"
  19636. ["Record DONE time"
  19637. (progn (setq org-log-done (not org-log-done))
  19638. (message "Switching to %s will %s record a timestamp"
  19639. (car org-done-keywords)
  19640. (if org-log-done "automatically" "not")))
  19641. :style toggle :selected org-log-done])
  19642. "--"
  19643. ["Agenda Command..." org-agenda t]
  19644. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19645. ("File List for Agenda")
  19646. ("Special views current file"
  19647. ["TODO Tree" org-show-todo-tree t]
  19648. ["Check Deadlines" org-check-deadlines t]
  19649. ["Tags/Property tree" org-match-sparse-tree t])
  19650. "--"
  19651. ["Export/Publish..." org-export-dispatch t]
  19652. ("LaTeX"
  19653. ["Org CDLaTeX mode" org-cdlatex-mode :active (require 'cdlatex nil t)
  19654. :style toggle :selected org-cdlatex-mode]
  19655. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19656. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19657. ["Modify math symbol" org-cdlatex-math-modify
  19658. (org-inside-LaTeX-fragment-p)]
  19659. ["Insert citation" org-reftex-citation t])
  19660. "--"
  19661. ("MobileOrg"
  19662. ["Push Files and Views" org-mobile-push t]
  19663. ["Get Captured and Flagged" org-mobile-pull t]
  19664. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "\\[org-agenda] ?"]
  19665. "--"
  19666. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19667. "--"
  19668. ("Documentation"
  19669. ["Show Version" org-version t]
  19670. ["Info Documentation" org-info t])
  19671. ("Customize"
  19672. ["Browse Org Group" org-customize t]
  19673. "--"
  19674. ["Expand This Menu" org-create-customize-menu
  19675. (fboundp 'customize-menu-create)])
  19676. ["Send bug report" org-submit-bug-report t]
  19677. "--"
  19678. ("Refresh/Reload"
  19679. ["Refresh setup current buffer" org-mode-restart t]
  19680. ["Reload Org (after update)" org-reload t]
  19681. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19682. ))
  19683. (defun org-info (&optional node)
  19684. "Read documentation for Org in the info system.
  19685. With optional NODE, go directly to that node."
  19686. (interactive)
  19687. (info (format "(org)%s" (or node ""))))
  19688. ;;;###autoload
  19689. (defun org-submit-bug-report ()
  19690. "Submit a bug report on Org via mail.
  19691. Don't hesitate to report any problems or inaccurate documentation.
  19692. If you don't have setup sending mail from (X)Emacs, please copy the
  19693. output buffer into your mail program, as it gives us important
  19694. information about your Org version and configuration."
  19695. (interactive)
  19696. (require 'reporter)
  19697. (defvar reporter-prompt-for-summary-p)
  19698. (org-load-modules-maybe)
  19699. (org-require-autoloaded-modules)
  19700. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19701. (reporter-submit-bug-report
  19702. "emacs-orgmode@gnu.org"
  19703. (org-version nil 'full)
  19704. (let (list)
  19705. (save-window-excursion
  19706. (pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19707. (delete-other-windows)
  19708. (erase-buffer)
  19709. (insert "You are about to submit a bug report to the Org mailing list.
  19710. We would like to add your full Org and Outline configuration to the
  19711. bug report. This greatly simplifies the work of the maintainer and
  19712. other experts on the mailing list.
  19713. HOWEVER, some variables you have customized may contain private
  19714. information. The names of customers, colleagues, or friends, might
  19715. appear in the form of file names, tags, todo states, or search strings.
  19716. If you answer yes to the prompt, you might want to check and remove
  19717. such private information before sending the email.")
  19718. (add-text-properties (point-min) (point-max) '(face org-warning))
  19719. (when (yes-or-no-p "Include your Org configuration ")
  19720. (mapatoms
  19721. (lambda (v)
  19722. (and (boundp v)
  19723. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19724. (or (and (symbol-value v)
  19725. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19726. (and
  19727. (get v 'custom-type) (get v 'standard-value)
  19728. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19729. (push v list)))))
  19730. (kill-buffer (get-buffer "*Warn about privacy*"))
  19731. list))
  19732. nil nil
  19733. "Remember to cover the basics, that is, what you expected to happen and
  19734. what in fact did happen. You don't know how to make a good report? See
  19735. http://orgmode.org/manual/Feedback.html#Feedback
  19736. Your bug report will be posted to the Org mailing list.
  19737. ------------------------------------------------------------------------")
  19738. (save-excursion
  19739. (when (re-search-backward "^\\(Subject: \\)Org mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19740. (replace-match "\\1Bug: \\3 [\\2]")))))
  19741. (defun org-install-agenda-files-menu ()
  19742. (let ((bl (buffer-list)))
  19743. (save-excursion
  19744. (while bl
  19745. (set-buffer (pop bl))
  19746. (when (derived-mode-p 'org-mode) (setq bl nil)))
  19747. (when (derived-mode-p 'org-mode)
  19748. (easy-menu-change
  19749. '("Org") "File List for Agenda"
  19750. (append
  19751. (list
  19752. ["Edit File List" (org-edit-agenda-file-list) t]
  19753. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19754. ["Remove Current File from List" org-remove-file t]
  19755. ["Cycle through agenda files" org-cycle-agenda-files t]
  19756. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19757. "--")
  19758. (mapcar 'org-file-menu-entry
  19759. ;; Prevent initialization from failing.
  19760. (ignore-errors (org-agenda-files t)))))))))
  19761. ;;;; Documentation
  19762. (defun org-require-autoloaded-modules ()
  19763. (interactive)
  19764. (mapc #'require
  19765. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19766. org-table org-timer)))
  19767. ;;;###autoload
  19768. (defun org-reload (&optional uncompiled)
  19769. "Reload all org lisp files.
  19770. With prefix arg UNCOMPILED, load the uncompiled versions."
  19771. (interactive "P")
  19772. (require 'loadhist)
  19773. (let* ((org-dir (org-find-library-dir "org"))
  19774. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19775. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19776. (remove-re (format "\\`%s\\'"
  19777. (regexp-opt '("org" "org-loaddefs" "org-version"))))
  19778. (feats (delete-dups
  19779. (mapcar 'file-name-sans-extension
  19780. (mapcar 'file-name-nondirectory
  19781. (delq nil
  19782. (mapcar 'feature-file
  19783. features))))))
  19784. (lfeat (append
  19785. (sort
  19786. (setq feats
  19787. (delq nil (mapcar
  19788. (lambda (f)
  19789. (if (and (string-match feature-re f)
  19790. (not (string-match remove-re f)))
  19791. f nil))
  19792. feats)))
  19793. 'string-lessp)
  19794. (list "org-version" "org")))
  19795. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19796. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19797. load-uncore load-misses)
  19798. (setq load-misses
  19799. (delq 't
  19800. (mapcar (lambda (f)
  19801. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19802. (and (string= org-dir contrib-dir)
  19803. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19804. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19805. (add-to-list 'load-uncore f 'append)
  19806. 't)
  19807. f))
  19808. lfeat)))
  19809. (when load-uncore
  19810. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19811. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19812. (if load-misses
  19813. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19814. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19815. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19816. ;;;###autoload
  19817. (defun org-customize ()
  19818. "Call the customize function with org as argument."
  19819. (interactive)
  19820. (org-load-modules-maybe)
  19821. (org-require-autoloaded-modules)
  19822. (customize-browse 'org))
  19823. (defun org-create-customize-menu ()
  19824. "Create a full customization menu for Org mode, insert it into the menu."
  19825. (interactive)
  19826. (org-load-modules-maybe)
  19827. (org-require-autoloaded-modules)
  19828. (if (fboundp 'customize-menu-create)
  19829. (progn
  19830. (easy-menu-change
  19831. '("Org") "Customize"
  19832. `(["Browse Org group" org-customize t]
  19833. "--"
  19834. ,(customize-menu-create 'org)
  19835. ["Set" Custom-set t]
  19836. ["Save" Custom-save t]
  19837. ["Reset to Current" Custom-reset-current t]
  19838. ["Reset to Saved" Custom-reset-saved t]
  19839. ["Reset to Standard Settings" Custom-reset-standard t]))
  19840. (message "\"Org\"-menu now contains full customization menu"))
  19841. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19842. ;;;; Miscellaneous stuff
  19843. ;;; Generally useful functions
  19844. (defun org-get-at-eol (property n)
  19845. "Get text property PROPERTY at the end of line less N characters."
  19846. (get-text-property (- (point-at-eol) n) property))
  19847. (defun org-find-text-property-in-string (prop s)
  19848. "Return the first non-nil value of property PROP in string S."
  19849. (or (get-text-property 0 prop s)
  19850. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19851. prop s)))
  19852. (defun org-display-warning (message)
  19853. "Display the given MESSAGE as a warning."
  19854. (display-warning 'org message :warning))
  19855. (defun org-eval (form)
  19856. "Eval FORM and return result."
  19857. (condition-case error
  19858. (eval form)
  19859. (error (format "%%![Error: %s]" error))))
  19860. (defun org-in-clocktable-p ()
  19861. "Check if the cursor is in a clocktable."
  19862. (let ((pos (point)) start)
  19863. (save-excursion
  19864. (end-of-line 1)
  19865. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19866. (setq start (match-beginning 0))
  19867. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19868. (>= (match-end 0) pos)
  19869. start))))
  19870. (defun org-in-verbatim-emphasis ()
  19871. (save-match-data
  19872. (and (org-in-regexp org-verbatim-re 2)
  19873. (>= (point) (match-beginning 3))
  19874. (<= (point) (match-end 4)))))
  19875. (defun org-overlay-display (ovl text &optional face evap)
  19876. "Make overlay OVL display TEXT with face FACE."
  19877. (overlay-put ovl 'display text)
  19878. (if face (overlay-put ovl 'face face))
  19879. (if evap (overlay-put ovl 'evaporate t)))
  19880. (defun org-overlay-before-string (ovl text &optional face evap)
  19881. "Make overlay OVL display TEXT with face FACE."
  19882. (if face (org-add-props text nil 'face face))
  19883. (overlay-put ovl 'before-string text)
  19884. (if evap (overlay-put ovl 'evaporate t)))
  19885. (defun org-find-overlays (prop &optional pos delete)
  19886. "Find all overlays specifying PROP at POS or point.
  19887. If DELETE is non-nil, delete all those overlays."
  19888. (let (found)
  19889. (dolist (ov (overlays-at (or pos (point))) found)
  19890. (cond ((not (overlay-get ov prop)))
  19891. (delete (delete-overlay ov))
  19892. (t (push ov found))))))
  19893. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19894. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19895. (if (and marker (marker-buffer marker)
  19896. (buffer-live-p (marker-buffer marker)))
  19897. (progn
  19898. (pop-to-buffer-same-window (marker-buffer marker))
  19899. (when (or (> marker (point-max)) (< marker (point-min)))
  19900. (widen))
  19901. (goto-char marker)
  19902. (org-show-context 'org-goto))
  19903. (if bookmark
  19904. (bookmark-jump bookmark)
  19905. (error "Cannot find location"))))
  19906. (defun org-quote-csv-field (s)
  19907. "Quote field for inclusion in CSV material."
  19908. (if (string-match "[\",]" s)
  19909. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19910. s))
  19911. (defun org-force-self-insert (N)
  19912. "Needed to enforce self-insert under remapping."
  19913. (interactive "p")
  19914. (self-insert-command N))
  19915. (defun org-shorten-string (s maxlength)
  19916. "Shorten string S so that it is no longer than MAXLENGTH characters.
  19917. If the string is shorter or has length MAXLENGTH, just return the
  19918. original string. If it is longer, the functions finds a space in the
  19919. string, breaks this string off at that locations and adds three dots
  19920. as ellipsis. Including the ellipsis, the string will not be longer
  19921. than MAXLENGTH. If finding a good breaking point in the string does
  19922. not work, the string is just chopped off in the middle of a word
  19923. if necessary."
  19924. (if (<= (length s) maxlength)
  19925. s
  19926. (let* ((n (max (- maxlength 4) 1))
  19927. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19928. (if (string-match re s)
  19929. (concat (match-string 1 s) "...")
  19930. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19931. (defun org-get-indentation (&optional line)
  19932. "Get the indentation of the current line, interpreting tabs.
  19933. When LINE is given, assume it represents a line and compute its indentation."
  19934. (if line
  19935. (when (string-match "^ *" (org-remove-tabs line))
  19936. (match-end 0))
  19937. (save-excursion
  19938. (beginning-of-line 1)
  19939. (skip-chars-forward " \t")
  19940. (current-column))))
  19941. (defun org-get-string-indentation (s)
  19942. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19943. (let ((n -1) (i 0) (w tab-width) c)
  19944. (catch 'exit
  19945. (while (< (setq n (1+ n)) (length s))
  19946. (setq c (aref s n))
  19947. (cond ((= c ?\ ) (setq i (1+ i)))
  19948. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19949. (t (throw 'exit t)))))
  19950. i))
  19951. (defun org-remove-tabs (s &optional width)
  19952. "Replace tabulators in S with spaces.
  19953. Assumes that s is a single line, starting in column 0."
  19954. (setq width (or width tab-width))
  19955. (while (string-match "\t" s)
  19956. (setq s (replace-match
  19957. (make-string
  19958. (- (* width (/ (+ (match-beginning 0) width) width))
  19959. (match-beginning 0)) ?\ )
  19960. t t s)))
  19961. s)
  19962. (defun org-fix-indentation (line ind)
  19963. "Fix indentation in LINE.
  19964. IND is a cons cell with target and minimum indentation.
  19965. If the current indentation in LINE is smaller than the minimum,
  19966. leave it alone. If it is larger than ind, set it to the target."
  19967. (let* ((l (org-remove-tabs line))
  19968. (i (org-get-indentation l))
  19969. (i1 (car ind)) (i2 (cdr ind)))
  19970. (when (>= i i2) (setq l (substring line i2)))
  19971. (if (> i1 0)
  19972. (concat (make-string i1 ?\ ) l)
  19973. l)))
  19974. (defun org-remove-indentation (code &optional n)
  19975. "Remove maximum common indentation in string CODE and return it.
  19976. N may optionally be the number of columns to remove. Return CODE
  19977. as-is if removal failed."
  19978. (with-temp-buffer
  19979. (insert code)
  19980. (if (org-do-remove-indentation n) (buffer-string) code)))
  19981. (defun org-do-remove-indentation (&optional n)
  19982. "Remove the maximum common indentation from the buffer.
  19983. When optional argument N is a positive integer, remove exactly
  19984. that much characters from indentation, if possible. Return nil
  19985. if it fails."
  19986. (catch :exit
  19987. (goto-char (point-min))
  19988. ;; Find maximum common indentation, if not specified.
  19989. (let ((n (or n
  19990. (let ((min-ind (point-max)))
  19991. (save-excursion
  19992. (while (re-search-forward "^[ \t]*\\S-" nil t)
  19993. (let ((ind (1- (current-column))))
  19994. (if (zerop ind) (throw :exit nil)
  19995. (setq min-ind (min min-ind ind))))))
  19996. min-ind))))
  19997. (if (zerop n) (throw :exit nil)
  19998. ;; Remove exactly N indentation, but give up if not possible.
  19999. (while (not (eobp))
  20000. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20001. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20002. ((< ind n) (throw :exit nil))
  20003. (t (indent-line-to (- ind n))))
  20004. (forward-line)))
  20005. ;; Signal success.
  20006. t))))
  20007. (defun org-fill-template (template alist)
  20008. "Find each %key of ALIST in TEMPLATE and replace it."
  20009. (let ((case-fold-search nil))
  20010. (dolist (entry (sort (copy-sequence alist)
  20011. (lambda (a b) (< (length (car a)) (length (car b))))))
  20012. (setq template
  20013. (replace-regexp-in-string
  20014. (concat "%" (regexp-quote (car entry)))
  20015. (or (cdr entry) "") template t t)))
  20016. template))
  20017. (defun org-base-buffer (buffer)
  20018. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20019. (if (not buffer)
  20020. buffer
  20021. (or (buffer-base-buffer buffer)
  20022. buffer)))
  20023. (defun org-wrap (string &optional width lines)
  20024. "Wrap string to either a number of lines, or a width in characters.
  20025. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20026. that costs. If there is a word longer than WIDTH, the text is actually
  20027. wrapped to the length of that word.
  20028. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20029. many lines, whatever width that takes.
  20030. The return value is a list of lines, without newlines at the end."
  20031. (let* ((words (split-string string))
  20032. (maxword (apply 'max (mapcar 'org-string-width words)))
  20033. w ll)
  20034. (cond (width
  20035. (org-do-wrap words (max maxword width)))
  20036. (lines
  20037. (setq w maxword)
  20038. (setq ll (org-do-wrap words maxword))
  20039. (if (<= (length ll) lines)
  20040. ll
  20041. (setq ll words)
  20042. (while (> (length ll) lines)
  20043. (setq w (1+ w))
  20044. (setq ll (org-do-wrap words w)))
  20045. ll))
  20046. (t (error "Cannot wrap this")))))
  20047. (defun org-do-wrap (words width)
  20048. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20049. (let (lines line)
  20050. (while words
  20051. (setq line (pop words))
  20052. (while (and words (< (+ (length line) (length (car words))) width))
  20053. (setq line (concat line " " (pop words))))
  20054. (setq lines (push line lines)))
  20055. (nreverse lines)))
  20056. (defun org-quote-vert (s)
  20057. "Replace \"|\" with \"\\vert\"."
  20058. (while (string-match "|" s)
  20059. (setq s (replace-match "\\vert" t t s)))
  20060. s)
  20061. (defun org-uuidgen-p (s)
  20062. "Is S an ID created by UUIDGEN?"
  20063. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20064. (defun org-in-src-block-p (&optional inside)
  20065. "Whether point is in a code source block.
  20066. When INSIDE is non-nil, don't consider we are within a src block
  20067. when point is at #+BEGIN_SRC or #+END_SRC."
  20068. (let ((case-fold-search t))
  20069. (or (and (eq (get-char-property (point) 'src-block) t))
  20070. (and (not inside)
  20071. (save-match-data
  20072. (save-excursion
  20073. (beginning-of-line)
  20074. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20075. (defun org-context ()
  20076. "Return a list of contexts of the current cursor position.
  20077. If several contexts apply, all are returned.
  20078. Each context entry is a list with a symbol naming the context, and
  20079. two positions indicating start and end of the context. Possible
  20080. contexts are:
  20081. :headline anywhere in a headline
  20082. :headline-stars on the leading stars in a headline
  20083. :todo-keyword on a TODO keyword (including DONE) in a headline
  20084. :tags on the TAGS in a headline
  20085. :priority on the priority cookie in a headline
  20086. :item on the first line of a plain list item
  20087. :item-bullet on the bullet/number of a plain list item
  20088. :checkbox on the checkbox in a plain list item
  20089. :table in an Org table
  20090. :table-special on a special filed in a table
  20091. :table-table in a table.el table
  20092. :clocktable in a clocktable
  20093. :src-block in a source block
  20094. :link on a hyperlink
  20095. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20096. :target on a <<target>>
  20097. :radio-target on a <<<radio-target>>>
  20098. :latex-fragment on a LaTeX fragment
  20099. :latex-preview on a LaTeX fragment with overlaid preview image
  20100. This function expects the position to be visible because it uses font-lock
  20101. faces as a help to recognize the following contexts: :table-special, :link,
  20102. and :keyword."
  20103. (let* ((f (get-text-property (point) 'face))
  20104. (faces (if (listp f) f (list f)))
  20105. (case-fold-search t)
  20106. (p (point)) clist o)
  20107. ;; First the large context
  20108. (cond
  20109. ((org-at-heading-p t)
  20110. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20111. (when (progn
  20112. (beginning-of-line 1)
  20113. (looking-at org-todo-line-tags-regexp))
  20114. (push (org-point-in-group p 1 :headline-stars) clist)
  20115. (push (org-point-in-group p 2 :todo-keyword) clist)
  20116. (push (org-point-in-group p 4 :tags) clist))
  20117. (goto-char p)
  20118. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20119. (when (looking-at "\\[#[A-Z0-9]\\]")
  20120. (push (org-point-in-group p 0 :priority) clist)))
  20121. ((org-at-item-p)
  20122. (push (org-point-in-group p 2 :item-bullet) clist)
  20123. (push (list :item (point-at-bol)
  20124. (save-excursion (org-end-of-item) (point)))
  20125. clist)
  20126. (and (org-at-item-checkbox-p)
  20127. (push (org-point-in-group p 0 :checkbox) clist)))
  20128. ((org-at-table-p)
  20129. (push (list :table (org-table-begin) (org-table-end)) clist)
  20130. (when (memq 'org-formula faces)
  20131. (push (list :table-special
  20132. (previous-single-property-change p 'face)
  20133. (next-single-property-change p 'face)) clist)))
  20134. ((org-at-table-p 'any)
  20135. (push (list :table-table) clist)))
  20136. (goto-char p)
  20137. (let ((case-fold-search t))
  20138. ;; New the "medium" contexts: clocktables, source blocks
  20139. (cond ((org-in-clocktable-p)
  20140. (push (list :clocktable
  20141. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20142. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20143. (match-beginning 1))
  20144. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20145. (match-end 0))) clist))
  20146. ((org-in-src-block-p)
  20147. (push (list :src-block
  20148. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20149. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20150. (match-beginning 1))
  20151. (and (search-forward "#+END_SRC" nil t)
  20152. (match-beginning 0))) clist))))
  20153. (goto-char p)
  20154. ;; Now the small context
  20155. (cond
  20156. ((org-at-timestamp-p)
  20157. (push (org-point-in-group p 0 :timestamp) clist))
  20158. ((memq 'org-link faces)
  20159. (push (list :link
  20160. (previous-single-property-change p 'face)
  20161. (next-single-property-change p 'face)) clist))
  20162. ((memq 'org-special-keyword faces)
  20163. (push (list :keyword
  20164. (previous-single-property-change p 'face)
  20165. (next-single-property-change p 'face)) clist))
  20166. ((org-at-target-p)
  20167. (push (org-point-in-group p 0 :target) clist)
  20168. (goto-char (1- (match-beginning 0)))
  20169. (when (looking-at org-radio-target-regexp)
  20170. (push (org-point-in-group p 0 :radio-target) clist))
  20171. (goto-char p))
  20172. ((setq o (cl-some
  20173. (lambda (o)
  20174. (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
  20175. o))
  20176. (overlays-at (point))))
  20177. (push (list :latex-fragment
  20178. (overlay-start o) (overlay-end o)) clist)
  20179. (push (list :latex-preview
  20180. (overlay-start o) (overlay-end o)) clist))
  20181. ((org-inside-LaTeX-fragment-p)
  20182. ;; FIXME: positions wrong.
  20183. (push (list :latex-fragment (point) (point)) clist)))
  20184. (setq clist (nreverse (delq nil clist)))
  20185. clist))
  20186. (defun org-in-regexp (regexp &optional nlines visually)
  20187. "Check if point is inside a match of REGEXP.
  20188. Normally only the current line is checked, but you can include
  20189. NLINES extra lines around point into the search. If VISUALLY is
  20190. set, require that the cursor is not after the match but really
  20191. on, so that the block visually is on the match.
  20192. Return nil or a cons cell (BEG . END) where BEG and END are,
  20193. respectively, the positions at the beginning and the end of the
  20194. match."
  20195. (catch :exit
  20196. (let ((pos (point))
  20197. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20198. (save-excursion
  20199. (beginning-of-line (- 1 (or nlines 0)))
  20200. (while (and (re-search-forward regexp eol t)
  20201. (<= (match-beginning 0) pos))
  20202. (let ((end (match-end 0)))
  20203. (when (or (> end pos) (and (= end pos) (not visually)))
  20204. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20205. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20206. "Non-nil when point is between matches of START-RE and END-RE.
  20207. Also return a non-nil value when point is on one of the matches.
  20208. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20209. buffer positions. Default values are the positions of headlines
  20210. surrounding the point.
  20211. The functions returns a cons cell whose car (resp. cdr) is the
  20212. position before START-RE (resp. after END-RE)."
  20213. (save-match-data
  20214. (let ((pos (point))
  20215. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20216. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20217. beg end)
  20218. (save-excursion
  20219. ;; Point is on a block when on START-RE or if START-RE can be
  20220. ;; found before it...
  20221. (and (or (org-in-regexp start-re)
  20222. (re-search-backward start-re limit-up t))
  20223. (setq beg (match-beginning 0))
  20224. ;; ... and END-RE after it...
  20225. (goto-char (match-end 0))
  20226. (re-search-forward end-re limit-down t)
  20227. (> (setq end (match-end 0)) pos)
  20228. ;; ... without another START-RE in-between.
  20229. (goto-char (match-beginning 0))
  20230. (not (re-search-backward start-re (1+ beg) t))
  20231. ;; Return value.
  20232. (cons beg end))))))
  20233. (defun org-in-block-p (names)
  20234. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20235. NAMES is a list of strings containing names of blocks.
  20236. Return first block name matched, or nil. Beware that in case of
  20237. nested blocks, the returned name may not belong to the closest
  20238. block from point."
  20239. (save-match-data
  20240. (catch 'exit
  20241. (let ((case-fold-search t)
  20242. (lim-up (save-excursion (outline-previous-heading)))
  20243. (lim-down (save-excursion (outline-next-heading))))
  20244. (dolist (name names)
  20245. (let ((n (regexp-quote name)))
  20246. (when (org-between-regexps-p
  20247. (concat "^[ \t]*#\\+begin_" n)
  20248. (concat "^[ \t]*#\\+end_" n)
  20249. lim-up lim-down)
  20250. (throw 'exit n)))))
  20251. nil)))
  20252. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20253. "Call `multi-occur' with buffers for all agenda files."
  20254. (interactive "sOrg-files matching: ")
  20255. (let* ((files (org-agenda-files))
  20256. (tnames (mapcar #'file-truename files))
  20257. (extra org-agenda-text-search-extra-files))
  20258. (when (eq (car extra) 'agenda-archives)
  20259. (setq extra (cdr extra))
  20260. (setq files (org-add-archive-files files)))
  20261. (dolist (f extra)
  20262. (unless (member (file-truename f) tnames)
  20263. (unless (member f files) (setq files (append files (list f))))
  20264. (setq tnames (append tnames (list (file-truename f))))))
  20265. (multi-occur
  20266. (mapcar (lambda (x)
  20267. (with-current-buffer
  20268. ;; FIXME: Why not just (find-file-noselect x)?
  20269. ;; Is it to avoid the "revert buffer" prompt?
  20270. (or (get-file-buffer x) (find-file-noselect x))
  20271. (widen)
  20272. (current-buffer)))
  20273. files)
  20274. regexp)))
  20275. (add-hook 'occur-mode-find-occurrence-hook
  20276. (lambda () (when (derived-mode-p 'org-mode) (org-reveal))))
  20277. (defun org-occur-link-in-agenda-files ()
  20278. "Create a link and search for it in the agendas.
  20279. The link is not stored in `org-stored-links', it is just created
  20280. for the search purpose."
  20281. (interactive)
  20282. (let ((link (condition-case nil
  20283. (org-store-link nil)
  20284. (error "Unable to create a link to here"))))
  20285. (org-occur-in-agenda-files (regexp-quote link))))
  20286. (defun org-reverse-string (string)
  20287. "Return the reverse of STRING."
  20288. (apply 'string (reverse (string-to-list string))))
  20289. ;; defsubst org-uniquify must be defined before first use
  20290. (defun org-uniquify-alist (alist)
  20291. "Merge elements of ALIST with the same key.
  20292. For example, in this alist:
  20293. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20294. => \\='((a 1 3) (b 2))
  20295. merge (a 1) and (a 3) into (a 1 3).
  20296. The function returns the new ALIST."
  20297. (let (rtn)
  20298. (dolist (e alist rtn)
  20299. (let (n)
  20300. (if (not (assoc (car e) rtn))
  20301. (push e rtn)
  20302. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20303. (setq rtn (assq-delete-all (car e) rtn))
  20304. (push n rtn))))))
  20305. (defun org-delete-all (elts list)
  20306. "Remove all elements in ELTS from LIST.
  20307. Comparison is done with `equal'. It is a destructive operation
  20308. that may remove elements by altering the list structure."
  20309. (while elts
  20310. (setq list (delete (pop elts) list)))
  20311. list)
  20312. (defun org-back-over-empty-lines ()
  20313. "Move backwards over whitespace, to the beginning of the first empty line.
  20314. Returns the number of empty lines passed."
  20315. (let ((pos (point)))
  20316. (if (cdr (assq 'heading org-blank-before-new-entry))
  20317. (skip-chars-backward " \t\n\r")
  20318. (unless (eobp)
  20319. (forward-line -1)))
  20320. (beginning-of-line 2)
  20321. (goto-char (min (point) pos))
  20322. (count-lines (point) pos)))
  20323. (defun org-skip-whitespace ()
  20324. (skip-chars-forward " \t\n\r"))
  20325. (defun org-point-in-group (point group &optional context)
  20326. "Check if POINT is in match-group GROUP.
  20327. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20328. match. If the match group does not exist or point is not inside it,
  20329. return nil."
  20330. (and (match-beginning group)
  20331. (>= point (match-beginning group))
  20332. (<= point (match-end group))
  20333. (if context
  20334. (list context (match-beginning group) (match-end group))
  20335. t)))
  20336. (defun org-switch-to-buffer-other-window (&rest args)
  20337. "Switch to buffer in a second window on the current frame.
  20338. In particular, do not allow pop-up frames.
  20339. Returns the newly created buffer."
  20340. (org-no-popups
  20341. (apply 'switch-to-buffer-other-window args)))
  20342. (defun org-combine-plists (&rest plists)
  20343. "Create a single property list from all plists in PLISTS.
  20344. The process starts by copying the first list, and then setting properties
  20345. from the other lists. Settings in the last list are the most significant
  20346. ones and overrule settings in the other lists."
  20347. (let ((rtn (copy-sequence (pop plists)))
  20348. p v ls)
  20349. (while plists
  20350. (setq ls (pop plists))
  20351. (while ls
  20352. (setq p (pop ls) v (pop ls))
  20353. (setq rtn (plist-put rtn p v))))
  20354. rtn))
  20355. (defun org-replace-escapes (string table)
  20356. "Replace %-escapes in STRING with values in TABLE.
  20357. TABLE is an association list with keys like \"%a\" and string values.
  20358. The sequences in STRING may contain normal field width and padding information,
  20359. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20360. so values can contain further %-escapes if they are define later in TABLE."
  20361. (let ((tbl (copy-alist table))
  20362. (case-fold-search nil)
  20363. (pchg 0)
  20364. re rpl)
  20365. (dolist (e tbl)
  20366. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20367. (when (and (cdr e) (string-match re (cdr e)))
  20368. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20369. (safe "SREF"))
  20370. (add-text-properties 0 3 (list 'sref sref) safe)
  20371. (setcdr e (replace-match safe t t (cdr e)))))
  20372. (while (string-match re string)
  20373. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20374. (cdr e)))
  20375. (setq string (replace-match rpl t t string))))
  20376. (while (setq pchg (next-property-change pchg string))
  20377. (let ((sref (get-text-property pchg 'sref string)))
  20378. (when (and sref (string-match "SREF" string pchg))
  20379. (setq string (replace-match sref t t string)))))
  20380. string))
  20381. (defun org-find-base-buffer-visiting (file)
  20382. "Like `find-buffer-visiting' but always return the base buffer and
  20383. not an indirect buffer."
  20384. (let ((buf (or (get-file-buffer file)
  20385. (find-buffer-visiting file))))
  20386. (if buf
  20387. (or (buffer-base-buffer buf) buf)
  20388. nil)))
  20389. ;;; TODO: Only called once, from ox-odt which should probably use
  20390. ;;; org-export-inline-image-p or something.
  20391. (defun org-file-image-p (file)
  20392. "Return non-nil if FILE is an image."
  20393. (save-match-data
  20394. (string-match (image-file-name-regexp) file)))
  20395. (defun org-get-cursor-date (&optional with-time)
  20396. "Return the date at cursor in as a time.
  20397. This works in the calendar and in the agenda, anywhere else it just
  20398. returns the current time.
  20399. If WITH-TIME is non-nil, returns the time of the event at point (in
  20400. the agenda) or the current time of the day."
  20401. (let (date day defd tp hod mod)
  20402. (when with-time
  20403. (setq tp (get-text-property (point) 'time))
  20404. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20405. (setq hod (string-to-number (match-string 1 tp))
  20406. mod (string-to-number (match-string 2 tp))))
  20407. (or tp (let ((now (decode-time)))
  20408. (setq hod (nth 2 now)
  20409. mod (nth 1 now)))))
  20410. (cond
  20411. ((eq major-mode 'calendar-mode)
  20412. (setq date (calendar-cursor-to-date)
  20413. defd (encode-time 0 (or mod 0) (or hod 0)
  20414. (nth 1 date) (nth 0 date) (nth 2 date))))
  20415. ((eq major-mode 'org-agenda-mode)
  20416. (setq day (get-text-property (point) 'day))
  20417. (when day
  20418. (setq date (calendar-gregorian-from-absolute day)
  20419. defd (encode-time 0 (or mod 0) (or hod 0)
  20420. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20421. (or defd (current-time))))
  20422. (defun org-mark-subtree (&optional up)
  20423. "Mark the current subtree.
  20424. This puts point at the start of the current subtree, and mark at
  20425. the end. If a numeric prefix UP is given, move up into the
  20426. hierarchy of headlines by UP levels before marking the subtree."
  20427. (interactive "P")
  20428. (org-with-limited-levels
  20429. (cond ((org-at-heading-p) (beginning-of-line))
  20430. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20431. (t (outline-previous-visible-heading 1))))
  20432. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20433. (if (called-interactively-p 'any)
  20434. (call-interactively 'org-mark-element)
  20435. (org-mark-element)))
  20436. (defun org-file-newer-than-p (file time)
  20437. "Non-nil if FILE is newer than TIME.
  20438. FILE is a filename, as a string, TIME is a list of integers, as
  20439. returned by, e.g., `current-time'."
  20440. (and (file-exists-p file)
  20441. ;; Only compare times up to whole seconds as some file-systems
  20442. ;; (e.g. HFS+) do not retain any finer granularity. As
  20443. ;; a consequence, make sure we return non-nil when the two
  20444. ;; times are equal.
  20445. (not (time-less-p (cl-subseq (nth 5 (file-attributes file)) 0 2)
  20446. (cl-subseq time 0 2)))))
  20447. (defun org-compile-file (source process ext &optional err-msg log-buf spec)
  20448. "Compile a SOURCE file using PROCESS.
  20449. PROCESS is either a function or a list of shell commands, as
  20450. strings. EXT is a file extension, without the leading dot, as
  20451. a string. It is used to check if the process actually succeeded.
  20452. PROCESS must create a file with the same base name and directory
  20453. as SOURCE, but ending with EXT. The function then returns its
  20454. filename. Otherwise, it raises an error. The error message can
  20455. then be refined by providing string ERR-MSG, which is appended to
  20456. the standard message.
  20457. If PROCESS is a function, it is called with a single argument:
  20458. the SOURCE file.
  20459. If it is a list of commands, each of them is called using
  20460. `shell-command'. By default, in each command, %b, %f, %F, %o and
  20461. %O are replaced with, respectively, SOURCE base name, name, full
  20462. name, directory and absolute output file name. It is possible,
  20463. however, to use more place-holders by specifying them in optional
  20464. argument SPEC, as an alist following the pattern
  20465. (CHARACTER . REPLACEMENT-STRING).
  20466. When PROCESS is a list of commands, optional argument LOG-BUF can
  20467. be set to a buffer or a buffer name. `shell-command' then uses
  20468. it for output."
  20469. (let* ((base-name (file-name-base source))
  20470. (full-name (file-truename source))
  20471. (out-dir (or (file-name-directory source) "./"))
  20472. (output (expand-file-name (concat base-name "." ext) out-dir))
  20473. (time (current-time))
  20474. (err-msg (if (stringp err-msg) (concat ". " err-msg) "")))
  20475. (save-window-excursion
  20476. (pcase process
  20477. ((pred functionp) (funcall process (shell-quote-argument source)))
  20478. ((pred consp)
  20479. (let ((log-buf (and log-buf (get-buffer-create log-buf)))
  20480. (spec (append spec
  20481. `((?b . ,(shell-quote-argument base-name))
  20482. (?f . ,(shell-quote-argument source))
  20483. (?F . ,(shell-quote-argument full-name))
  20484. (?o . ,(shell-quote-argument out-dir))
  20485. (?O . ,(shell-quote-argument output))))))
  20486. (dolist (command process)
  20487. (shell-command (format-spec command spec) log-buf))
  20488. (when log-buf (with-current-buffer log-buf (compilation-mode)))))
  20489. (_ (error "No valid command to process %S%s" source err-msg))))
  20490. ;; Check for process failure. Output file is expected to be
  20491. ;; located in the same directory as SOURCE.
  20492. (unless (org-file-newer-than-p output time)
  20493. (error (format "File %S wasn't produced%s" output err-msg)))
  20494. output))
  20495. ;;; Indentation
  20496. (defvar org-element-greater-elements)
  20497. (defun org--get-expected-indentation (element contentsp)
  20498. "Expected indentation column for current line, according to ELEMENT.
  20499. ELEMENT is an element containing point. CONTENTSP is non-nil
  20500. when indentation is to be computed according to contents of
  20501. ELEMENT."
  20502. (let ((type (org-element-type element))
  20503. (start (org-element-property :begin element))
  20504. (post-affiliated (org-element-property :post-affiliated element)))
  20505. (org-with-wide-buffer
  20506. (cond
  20507. (contentsp
  20508. (cl-case type
  20509. ((diary-sexp footnote-definition) 0)
  20510. ((headline inlinetask nil)
  20511. (if (not org-adapt-indentation) 0
  20512. (let ((level (org-current-level)))
  20513. (if level (1+ level) 0))))
  20514. ((item plain-list) (org-list-item-body-column post-affiliated))
  20515. (t
  20516. (goto-char start)
  20517. (org-get-indentation))))
  20518. ((memq type '(headline inlinetask nil))
  20519. (if (org-match-line "[ \t]*$")
  20520. (org--get-expected-indentation element t)
  20521. 0))
  20522. ((memq type '(diary-sexp footnote-definition)) 0)
  20523. ;; First paragraph of a footnote definition or an item.
  20524. ;; Indent like parent.
  20525. ((< (line-beginning-position) start)
  20526. (org--get-expected-indentation
  20527. (org-element-property :parent element) t))
  20528. ;; At first line: indent according to previous sibling, if any,
  20529. ;; ignoring footnote definitions and inline tasks, or parent's
  20530. ;; contents.
  20531. ((= (line-beginning-position) start)
  20532. (catch 'exit
  20533. (while t
  20534. (if (= (point-min) start) (throw 'exit 0)
  20535. (goto-char (1- start))
  20536. (let* ((previous (org-element-at-point))
  20537. (parent previous))
  20538. (while (and parent (<= (org-element-property :end parent) start))
  20539. (setq previous parent
  20540. parent (org-element-property :parent parent)))
  20541. (cond
  20542. ((not previous) (throw 'exit 0))
  20543. ((> (org-element-property :end previous) start)
  20544. (throw 'exit (org--get-expected-indentation previous t)))
  20545. ((memq (org-element-type previous)
  20546. '(footnote-definition inlinetask))
  20547. (setq start (org-element-property :begin previous)))
  20548. (t (goto-char (org-element-property :begin previous))
  20549. (throw 'exit
  20550. (if (bolp) (org-get-indentation)
  20551. ;; At first paragraph in an item or
  20552. ;; a footnote definition.
  20553. (org--get-expected-indentation
  20554. (org-element-property :parent previous) t))))))))))
  20555. ;; Otherwise, move to the first non-blank line above.
  20556. (t
  20557. (beginning-of-line)
  20558. (let ((pos (point)))
  20559. (skip-chars-backward " \r\t\n")
  20560. (cond
  20561. ;; Two blank lines end a footnote definition or a plain
  20562. ;; list. When we indent an empty line after them, the
  20563. ;; containing list or footnote definition is over, so it
  20564. ;; qualifies as a previous sibling. Therefore, we indent
  20565. ;; like its first line.
  20566. ((and (memq type '(footnote-definition plain-list))
  20567. (> (count-lines (point) pos) 2))
  20568. (goto-char start)
  20569. (org-get-indentation))
  20570. ;; Line above is the first one of a paragraph at the
  20571. ;; beginning of an item or a footnote definition. Indent
  20572. ;; like parent.
  20573. ((< (line-beginning-position) start)
  20574. (org--get-expected-indentation
  20575. (org-element-property :parent element) t))
  20576. ;; Line above is the beginning of an element, i.e., point
  20577. ;; was originally on the blank lines between element's start
  20578. ;; and contents.
  20579. ((= (line-beginning-position) post-affiliated)
  20580. (org--get-expected-indentation element t))
  20581. ;; POS is after contents in a greater element. Indent like
  20582. ;; the beginning of the element.
  20583. ((and (memq type org-element-greater-elements)
  20584. (let ((cend (org-element-property :contents-end element)))
  20585. (and cend (<= cend pos))))
  20586. ;; As a special case, if point is at the end of a footnote
  20587. ;; definition or an item, indent like the very last element
  20588. ;; within. If that last element is an item, indent like
  20589. ;; its contents.
  20590. (if (memq type '(footnote-definition item plain-list))
  20591. (let ((last (org-element-at-point)))
  20592. (goto-char pos)
  20593. (org--get-expected-indentation
  20594. last (eq (org-element-type last) 'item)))
  20595. (goto-char start)
  20596. (org-get-indentation)))
  20597. ;; In any other case, indent like the current line.
  20598. (t (org-get-indentation)))))))))
  20599. (defun org--align-node-property ()
  20600. "Align node property at point.
  20601. Alignment is done according to `org-property-format', which see."
  20602. (when (save-excursion
  20603. (beginning-of-line)
  20604. (looking-at org-property-re))
  20605. (replace-match
  20606. (concat (match-string 4)
  20607. (org-trim
  20608. (format org-property-format (match-string 1) (match-string 3))))
  20609. t t)))
  20610. (defun org-indent-line ()
  20611. "Indent line depending on context.
  20612. Indentation is done according to the following rules:
  20613. - Footnote definitions, diary sexps, headlines and inline tasks
  20614. have to start at column 0.
  20615. - On the very first line of an element, consider, in order, the
  20616. next rules until one matches:
  20617. 1. If there's a sibling element before, ignoring footnote
  20618. definitions and inline tasks, indent like its first line.
  20619. 2. If element has a parent, indent like its contents. More
  20620. precisely, if parent is an item, indent after the
  20621. description part, if any, or the bullet (see
  20622. `org-list-description-max-indent'). Else, indent like
  20623. parent's first line.
  20624. 3. Otherwise, indent relatively to current level, if
  20625. `org-adapt-indentation' is non-nil, or to left margin.
  20626. - On a blank line at the end of an element, indent according to
  20627. the type of the element. More precisely
  20628. 1. If element is a plain list, an item, or a footnote
  20629. definition, indent like the very last element within.
  20630. 2. If element is a paragraph, indent like its last non blank
  20631. line.
  20632. 3. Otherwise, indent like its very first line.
  20633. - In the code part of a source block, use language major mode
  20634. to indent current line if `org-src-tab-acts-natively' is
  20635. non-nil. If it is nil, do nothing.
  20636. - Otherwise, indent like the first non-blank line above.
  20637. The function doesn't indent an item as it could break the whole
  20638. list structure. Instead, use \\<org-mode-map>`\\[org-shiftmetaleft]' or \
  20639. `\\[org-shiftmetaright]'.
  20640. Also align node properties according to `org-property-format'."
  20641. (interactive)
  20642. (cond
  20643. (orgstruct-is-++
  20644. (let ((indent-line-function
  20645. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  20646. (indent-according-to-mode)))
  20647. ((org-at-heading-p) 'noindent)
  20648. (t
  20649. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20650. (type (org-element-type element)))
  20651. (cond ((and (memq type '(plain-list item))
  20652. (= (line-beginning-position)
  20653. (org-element-property :post-affiliated element)))
  20654. 'noindent)
  20655. ((and (eq type 'latex-environment)
  20656. (>= (point) (org-element-property :post-affiliated element))
  20657. (< (point) (org-with-wide-buffer
  20658. (goto-char (org-element-property :end element))
  20659. (skip-chars-backward " \r\t\n")
  20660. (line-beginning-position 2))))
  20661. 'noindent)
  20662. ((and (eq type 'src-block)
  20663. org-src-tab-acts-natively
  20664. (> (line-beginning-position)
  20665. (org-element-property :post-affiliated element))
  20666. (< (line-beginning-position)
  20667. (org-with-wide-buffer
  20668. (goto-char (org-element-property :end element))
  20669. (skip-chars-backward " \r\t\n")
  20670. (line-beginning-position))))
  20671. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  20672. (t
  20673. (let ((column (org--get-expected-indentation element nil)))
  20674. ;; Preserve current column.
  20675. (if (<= (current-column) (current-indentation))
  20676. (indent-line-to column)
  20677. (save-excursion (indent-line-to column))))
  20678. ;; Align node property. Also preserve current column.
  20679. (when (eq type 'node-property)
  20680. (let ((column (current-column)))
  20681. (org--align-node-property)
  20682. (org-move-to-column column)))))))))
  20683. (defun org-indent-region (start end)
  20684. "Indent each non-blank line in the region.
  20685. Called from a program, START and END specify the region to
  20686. indent. The function will not indent contents of example blocks,
  20687. verse blocks and export blocks as leading white spaces are
  20688. assumed to be significant there."
  20689. (interactive "r")
  20690. (save-excursion
  20691. (goto-char start)
  20692. (skip-chars-forward " \r\t\n")
  20693. (unless (eobp) (beginning-of-line))
  20694. (let ((indent-to
  20695. (lambda (ind pos)
  20696. ;; Set IND as indentation for all lines between point and
  20697. ;; POS. Blank lines are ignored. Leave point after POS
  20698. ;; once done.
  20699. (let ((limit (copy-marker pos)))
  20700. (while (< (point) limit)
  20701. (unless (looking-at-p "[ \t]*$") (indent-line-to ind))
  20702. (forward-line))
  20703. (set-marker limit nil))))
  20704. (end (copy-marker end)))
  20705. (while (< (point) end)
  20706. (if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20707. (let* ((element (org-element-at-point))
  20708. (type (org-element-type element))
  20709. (element-end (copy-marker (org-element-property :end element)))
  20710. (ind (org--get-expected-indentation element nil)))
  20711. (cond
  20712. ;; Element indented as a single block. Example blocks
  20713. ;; preserving indentation are a special case since the
  20714. ;; "contents" must not be indented whereas the block
  20715. ;; boundaries can.
  20716. ((or (memq type '(export-block latex-environment))
  20717. (and (eq type 'example-block)
  20718. (not
  20719. (or org-src-preserve-indentation
  20720. (org-element-property :preserve-indent element)))))
  20721. (let ((offset (- ind (org-get-indentation))))
  20722. (unless (zerop offset)
  20723. (indent-rigidly (org-element-property :begin element)
  20724. (org-element-property :end element)
  20725. offset)))
  20726. (goto-char element-end))
  20727. ;; Elements indented line wise. Be sure to exclude
  20728. ;; example blocks (preserving indentation) and source
  20729. ;; blocks from this category as they are treated
  20730. ;; specially later.
  20731. ((or (memq type '(paragraph table table-row))
  20732. (not (or (org-element-property :contents-begin element)
  20733. (memq type '(example-block src-block)))))
  20734. (when (eq type 'node-property)
  20735. (org--align-node-property)
  20736. (beginning-of-line))
  20737. (funcall indent-to ind (min element-end end)))
  20738. ;; Elements consisting of three parts: before the
  20739. ;; contents, the contents, and after the contents. The
  20740. ;; contents are treated specially, according to the
  20741. ;; element type, or not indented at all. Other parts are
  20742. ;; indented as a single block.
  20743. (t
  20744. (let* ((post (copy-marker
  20745. (org-element-property :post-affiliated element)))
  20746. (cbeg
  20747. (copy-marker
  20748. (cond
  20749. ((not (org-element-property :contents-begin element))
  20750. ;; Fake contents for source blocks.
  20751. (org-with-wide-buffer
  20752. (goto-char post)
  20753. (line-beginning-position 2)))
  20754. ((memq type '(footnote-definition item plain-list))
  20755. ;; Contents in these elements could start on
  20756. ;; the same line as the beginning of the
  20757. ;; element. Make sure we start indenting
  20758. ;; from the second line.
  20759. (org-with-wide-buffer
  20760. (goto-char post)
  20761. (end-of-line)
  20762. (skip-chars-forward " \r\t\n")
  20763. (if (eobp) (point) (line-beginning-position))))
  20764. (t (org-element-property :contents-begin element)))))
  20765. (cend (copy-marker
  20766. (or (org-element-property :contents-end element)
  20767. ;; Fake contents for source blocks.
  20768. (org-with-wide-buffer
  20769. (goto-char element-end)
  20770. (skip-chars-backward " \r\t\n")
  20771. (line-beginning-position)))
  20772. t)))
  20773. ;; Do not change items indentation individually as it
  20774. ;; might break the list as a whole. On the other
  20775. ;; hand, when at a plain list, indent it as a whole.
  20776. (cond ((eq type 'plain-list)
  20777. (let ((offset (- ind (org-get-indentation))))
  20778. (unless (zerop offset)
  20779. (indent-rigidly (org-element-property :begin element)
  20780. (org-element-property :end element)
  20781. offset))
  20782. (goto-char cbeg)))
  20783. ((eq type 'item) (goto-char cbeg))
  20784. (t (funcall indent-to ind (min cbeg end))))
  20785. (when (< (point) end)
  20786. (cl-case type
  20787. ((example-block verse-block))
  20788. (src-block
  20789. ;; In a source block, indent source code
  20790. ;; according to language major mode, but only if
  20791. ;; `org-src-tab-acts-natively' is non-nil.
  20792. (when (and (< (point) end) org-src-tab-acts-natively)
  20793. (ignore-errors
  20794. (org-babel-do-in-edit-buffer
  20795. (indent-region (point-min) (point-max))))))
  20796. (t (org-indent-region (point) (min cend end))))
  20797. (goto-char (min cend end))
  20798. (when (< (point) end)
  20799. (funcall indent-to ind (min element-end end))))
  20800. (set-marker post nil)
  20801. (set-marker cbeg nil)
  20802. (set-marker cend nil))))
  20803. (set-marker element-end nil))))
  20804. (set-marker end nil))))
  20805. (defun org-indent-drawer ()
  20806. "Indent the drawer at point."
  20807. (interactive)
  20808. (unless (save-excursion
  20809. (beginning-of-line)
  20810. (looking-at-p org-drawer-regexp))
  20811. (user-error "Not at a drawer"))
  20812. (let ((element (org-element-at-point)))
  20813. (unless (memq (org-element-type element) '(drawer property-drawer))
  20814. (user-error "Not at a drawer"))
  20815. (org-with-wide-buffer
  20816. (org-indent-region (org-element-property :begin element)
  20817. (org-element-property :end element))))
  20818. (message "Drawer at point indented"))
  20819. (defun org-indent-block ()
  20820. "Indent the block at point."
  20821. (interactive)
  20822. (unless (save-excursion
  20823. (beginning-of-line)
  20824. (let ((case-fold-search t))
  20825. (looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20826. (user-error "Not at a block"))
  20827. (let ((element (org-element-at-point)))
  20828. (unless (memq (org-element-type element)
  20829. '(comment-block center-block dynamic-block example-block
  20830. export-block quote-block special-block
  20831. src-block verse-block))
  20832. (user-error "Not at a block"))
  20833. (org-with-wide-buffer
  20834. (org-indent-region (org-element-property :begin element)
  20835. (org-element-property :end element))))
  20836. (message "Block at point indented"))
  20837. ;;; Filling
  20838. ;; We use our own fill-paragraph and auto-fill functions.
  20839. ;; `org-fill-paragraph' relies on adaptive filling and context
  20840. ;; checking. Appropriate `fill-prefix' is computed with
  20841. ;; `org-adaptive-fill-function'.
  20842. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20843. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20844. ;; `org-adaptive-fill-function' value. Internally,
  20845. ;; `org-comment-line-break-function' breaks the line.
  20846. ;; `org-setup-filling' installs filling and auto-filling related
  20847. ;; variables during `org-mode' initialization.
  20848. (defun org-setup-filling ()
  20849. (require 'org-element)
  20850. ;; Prevent auto-fill from inserting unwanted new items.
  20851. (when (boundp 'fill-nobreak-predicate)
  20852. (setq-local
  20853. fill-nobreak-predicate
  20854. (org-uniquify
  20855. (append fill-nobreak-predicate
  20856. '(org-fill-line-break-nobreak-p
  20857. org-fill-n-macro-as-item-nobreak-p
  20858. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20859. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20860. (setq-local paragraph-start paragraph-ending)
  20861. (setq-local paragraph-separate paragraph-ending))
  20862. (setq-local fill-paragraph-function 'org-fill-paragraph)
  20863. (setq-local auto-fill-inhibit-regexp nil)
  20864. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  20865. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  20866. (setq-local comment-line-break-function 'org-comment-line-break-function))
  20867. (defun org-fill-line-break-nobreak-p ()
  20868. "Non-nil when a new line at point would create an Org line break."
  20869. (save-excursion
  20870. (skip-chars-backward "[ \t]")
  20871. (skip-chars-backward "\\\\")
  20872. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20873. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20874. "Non-nil when a new line at point would split a timestamp."
  20875. (and (org-at-timestamp-p 'lax)
  20876. (not (looking-at org-ts-regexp-both))))
  20877. (defun org-fill-n-macro-as-item-nobreak-p ()
  20878. "Non-nil when a new line at point would create a new list."
  20879. ;; During export, a "n" macro followed by a dot or a closing
  20880. ;; parenthesis can end up being parsed as a new list item.
  20881. (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)"))
  20882. (declare-function message-in-body-p "message" ())
  20883. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20884. (defun org-adaptive-fill-function ()
  20885. "Compute a fill prefix for the current line.
  20886. Return fill prefix, as a string, or nil if current line isn't
  20887. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20888. matches in paragraphs or comments, use it."
  20889. (catch 'exit
  20890. (when (derived-mode-p 'message-mode)
  20891. (save-excursion
  20892. (beginning-of-line)
  20893. (cond ((not (message-in-body-p)) (throw 'exit nil))
  20894. ((looking-at-p org-table-line-regexp) (throw 'exit nil))
  20895. ((looking-at message-cite-prefix-regexp)
  20896. (throw 'exit (match-string-no-properties 0)))
  20897. ((looking-at org-outline-regexp)
  20898. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20899. (org-with-wide-buffer
  20900. (unless (org-at-heading-p)
  20901. (let* ((p (line-beginning-position))
  20902. (element (save-excursion
  20903. (beginning-of-line)
  20904. (org-element-at-point)))
  20905. (type (org-element-type element))
  20906. (post-affiliated (org-element-property :post-affiliated element)))
  20907. (unless (< p post-affiliated)
  20908. (cl-case type
  20909. (comment
  20910. (save-excursion
  20911. (beginning-of-line)
  20912. (looking-at "[ \t]*")
  20913. (concat (match-string 0) "# ")))
  20914. (footnote-definition "")
  20915. ((item plain-list)
  20916. (make-string (org-list-item-body-column post-affiliated) ?\s))
  20917. (paragraph
  20918. ;; Fill prefix is usually the same as the current line,
  20919. ;; unless the paragraph is at the beginning of an item.
  20920. (let ((parent (org-element-property :parent element)))
  20921. (save-excursion
  20922. (beginning-of-line)
  20923. (cond ((eq (org-element-type parent) 'item)
  20924. (make-string (org-list-item-body-column
  20925. (org-element-property :begin parent))
  20926. ?\s))
  20927. ((and adaptive-fill-regexp
  20928. ;; Locally disable
  20929. ;; `adaptive-fill-function' to let
  20930. ;; `fill-context-prefix' handle
  20931. ;; `adaptive-fill-regexp' variable.
  20932. (let (adaptive-fill-function)
  20933. (fill-context-prefix
  20934. post-affiliated
  20935. (org-element-property :end element)))))
  20936. ((looking-at "[ \t]+") (match-string 0))
  20937. (t "")))))
  20938. (comment-block
  20939. ;; Only fill contents if P is within block boundaries.
  20940. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20941. (forward-line)
  20942. (point)))
  20943. (cend (save-excursion
  20944. (goto-char (org-element-property :end element))
  20945. (skip-chars-backward " \r\t\n")
  20946. (line-beginning-position))))
  20947. (when (and (>= p cbeg) (< p cend))
  20948. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20949. (match-string 0)
  20950. "")))))))))))
  20951. (declare-function message-goto-body "message" ())
  20952. (defvar message-cite-prefix-regexp) ; From message.el
  20953. (defun org-fill-element (&optional justify)
  20954. "Fill element at point, when applicable.
  20955. This function only applies to comment blocks, comments, example
  20956. blocks and paragraphs. Also, as a special case, re-align table
  20957. when point is at one.
  20958. If JUSTIFY is non-nil (interactively, with prefix argument),
  20959. justify as well. If `sentence-end-double-space' is non-nil, then
  20960. period followed by one space does not end a sentence, so don't
  20961. break a line there. The variable `fill-column' controls the
  20962. width for filling.
  20963. For convenience, when point is at a plain list, an item or
  20964. a footnote definition, try to fill the first paragraph within."
  20965. (with-syntax-table org-mode-transpose-word-syntax-table
  20966. ;; Move to end of line in order to get the first paragraph within
  20967. ;; a plain list or a footnote definition.
  20968. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20969. ;; First check if point is in a blank line at the beginning of
  20970. ;; the buffer. In that case, ignore filling.
  20971. (cl-case (org-element-type element)
  20972. ;; Use major mode filling function is src blocks.
  20973. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20974. ;; Align Org tables, leave table.el tables as-is.
  20975. (table-row (org-table-align) t)
  20976. (table
  20977. (when (eq (org-element-property :type element) 'org)
  20978. (save-excursion
  20979. (goto-char (org-element-property :post-affiliated element))
  20980. (org-table-align)))
  20981. t)
  20982. (paragraph
  20983. ;; Paragraphs may contain `line-break' type objects.
  20984. (let ((beg (max (point-min)
  20985. (org-element-property :contents-begin element)))
  20986. (end (min (point-max)
  20987. (org-element-property :contents-end element))))
  20988. ;; Do nothing if point is at an affiliated keyword.
  20989. (if (< (line-end-position) beg) t
  20990. (when (derived-mode-p 'message-mode)
  20991. ;; In `message-mode', do not fill following citation
  20992. ;; in current paragraph nor text before message body.
  20993. (let ((body-start (save-excursion (message-goto-body))))
  20994. (when body-start (setq beg (max body-start beg))))
  20995. (when (save-excursion
  20996. (re-search-forward
  20997. (concat "^" message-cite-prefix-regexp) end t))
  20998. (setq end (match-beginning 0))))
  20999. ;; Fill paragraph, taking line breaks into account.
  21000. (save-excursion
  21001. (goto-char beg)
  21002. (let ((cuts (list beg)))
  21003. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21004. (when (eq 'line-break
  21005. (org-element-type
  21006. (save-excursion (backward-char)
  21007. (org-element-context))))
  21008. (push (point) cuts)))
  21009. (dolist (c (delq end cuts))
  21010. (fill-region-as-paragraph c end justify)
  21011. (setq end c))))
  21012. t)))
  21013. ;; Contents of `comment-block' type elements should be
  21014. ;; filled as plain text, but only if point is within block
  21015. ;; markers.
  21016. (comment-block
  21017. (let* ((case-fold-search t)
  21018. (beg (save-excursion
  21019. (goto-char (org-element-property :begin element))
  21020. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21021. (forward-line)
  21022. (point)))
  21023. (end (save-excursion
  21024. (goto-char (org-element-property :end element))
  21025. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21026. (line-beginning-position))))
  21027. (if (or (< (point) beg) (> (point) end)) t
  21028. (fill-region-as-paragraph
  21029. (save-excursion (end-of-line)
  21030. (re-search-backward "^[ \t]*$" beg 'move)
  21031. (line-beginning-position))
  21032. (save-excursion (beginning-of-line)
  21033. (re-search-forward "^[ \t]*$" end 'move)
  21034. (line-beginning-position))
  21035. justify))))
  21036. ;; Fill comments.
  21037. (comment
  21038. (let ((begin (org-element-property :post-affiliated element))
  21039. (end (org-element-property :end element)))
  21040. (when (and (>= (point) begin) (<= (point) end))
  21041. (let ((begin (save-excursion
  21042. (end-of-line)
  21043. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21044. (progn (forward-line) (point))
  21045. begin)))
  21046. (end (save-excursion
  21047. (end-of-line)
  21048. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21049. (1- (line-beginning-position))
  21050. (skip-chars-backward " \r\t\n")
  21051. (line-end-position)))))
  21052. ;; Do not fill comments when at a blank line.
  21053. (when (> end begin)
  21054. (let ((fill-prefix
  21055. (save-excursion
  21056. (beginning-of-line)
  21057. (looking-at "[ \t]*#")
  21058. (let ((comment-prefix (match-string 0)))
  21059. (goto-char (match-end 0))
  21060. (if (looking-at adaptive-fill-regexp)
  21061. (concat comment-prefix (match-string 0))
  21062. (concat comment-prefix " "))))))
  21063. (save-excursion
  21064. (fill-region-as-paragraph begin end justify))))))
  21065. t))
  21066. ;; Ignore every other element.
  21067. (otherwise t)))))
  21068. (defun org-fill-paragraph (&optional justify region)
  21069. "Fill element at point, when applicable.
  21070. This function only applies to comment blocks, comments, example
  21071. blocks and paragraphs. Also, as a special case, re-align table
  21072. when point is at one.
  21073. For convenience, when point is at a plain list, an item or
  21074. a footnote definition, try to fill the first paragraph within.
  21075. If JUSTIFY is non-nil (interactively, with prefix argument),
  21076. justify as well. If `sentence-end-double-space' is non-nil, then
  21077. period followed by one space does not end a sentence, so don't
  21078. break a line there. The variable `fill-column' controls the
  21079. width for filling.
  21080. The REGION argument is non-nil if called interactively; in that
  21081. case, if Transient Mark mode is enabled and the mark is active,
  21082. fill each of the elements in the active region, instead of just
  21083. filling the current element."
  21084. (interactive (progn
  21085. (barf-if-buffer-read-only)
  21086. (list (if current-prefix-arg 'full) t)))
  21087. (cond
  21088. ((and (derived-mode-p 'message-mode)
  21089. (or (not (message-in-body-p))
  21090. (save-excursion (move-beginning-of-line 1)
  21091. (looking-at message-cite-prefix-regexp))))
  21092. ;; First ensure filling is correct in message-mode.
  21093. (let ((fill-paragraph-function
  21094. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21095. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21096. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21097. (paragraph-separate
  21098. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21099. (fill-paragraph nil)))
  21100. ((and region transient-mark-mode mark-active
  21101. (not (eq (region-beginning) (region-end))))
  21102. (let ((origin (point-marker))
  21103. (start (region-beginning)))
  21104. (unwind-protect
  21105. (progn
  21106. (goto-char (region-end))
  21107. (while (> (point) start)
  21108. (org-backward-paragraph)
  21109. (org-fill-element justify)))
  21110. (goto-char origin)
  21111. (set-marker origin nil))))
  21112. (t (org-fill-element justify))))
  21113. (org-remap org-mode-map 'fill-paragraph 'org-fill-paragraph)
  21114. (defun org-auto-fill-function ()
  21115. "Auto-fill function."
  21116. ;; Check if auto-filling is meaningful.
  21117. (let ((fc (current-fill-column)))
  21118. (when (and fc (> (current-column) fc))
  21119. (let* ((fill-prefix (org-adaptive-fill-function))
  21120. ;; Enforce empty fill prefix, if required. Otherwise, it
  21121. ;; will be computed again.
  21122. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21123. (when fill-prefix (do-auto-fill))))))
  21124. (defun org-comment-line-break-function (&optional soft)
  21125. "Break line at point and indent, continuing comment if within one.
  21126. The inserted newline is marked hard if variable
  21127. `use-hard-newlines' is true, unless optional argument SOFT is
  21128. non-nil."
  21129. (if soft (insert-and-inherit ?\n) (newline 1))
  21130. (save-excursion (forward-char -1) (delete-horizontal-space))
  21131. (delete-horizontal-space)
  21132. (indent-to-left-margin)
  21133. (insert-before-markers-and-inherit fill-prefix))
  21134. ;;; Fixed Width Areas
  21135. (defun org-toggle-fixed-width ()
  21136. "Toggle fixed-width markup.
  21137. Add or remove fixed-width markup on current line, whenever it
  21138. makes sense. Return an error otherwise.
  21139. If a region is active and if it contains only fixed-width areas
  21140. or blank lines, remove all fixed-width markup in it. If the
  21141. region contains anything else, convert all non-fixed-width lines
  21142. to fixed-width ones.
  21143. Blank lines at the end of the region are ignored unless the
  21144. region only contains such lines."
  21145. (interactive)
  21146. (if (not (org-region-active-p))
  21147. ;; No region:
  21148. ;;
  21149. ;; Remove fixed width marker only in a fixed-with element.
  21150. ;;
  21151. ;; Add fixed width maker in paragraphs, in blank lines after
  21152. ;; elements or at the beginning of a headline or an inlinetask,
  21153. ;; and before any one-line elements (e.g., a clock).
  21154. (progn
  21155. (beginning-of-line)
  21156. (let* ((element (org-element-at-point))
  21157. (type (org-element-type element)))
  21158. (cond
  21159. ((and (eq type 'fixed-width)
  21160. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21161. (replace-match
  21162. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21163. ((and (memq type '(babel-call clock comment diary-sexp headline
  21164. horizontal-rule keyword paragraph
  21165. planning))
  21166. (<= (org-element-property :post-affiliated element) (point)))
  21167. (skip-chars-forward " \t")
  21168. (insert ": "))
  21169. ((and (looking-at-p "[ \t]*$")
  21170. (or (eq type 'inlinetask)
  21171. (save-excursion
  21172. (skip-chars-forward " \r\t\n")
  21173. (<= (org-element-property :end element) (point)))))
  21174. (delete-region (point) (line-end-position))
  21175. (org-indent-line)
  21176. (insert ": "))
  21177. (t (user-error "Cannot insert a fixed-width line here")))))
  21178. ;; Region active.
  21179. (let* ((begin (save-excursion
  21180. (goto-char (region-beginning))
  21181. (line-beginning-position)))
  21182. (end (copy-marker
  21183. (save-excursion
  21184. (goto-char (region-end))
  21185. (unless (eolp) (beginning-of-line))
  21186. (if (save-excursion (re-search-backward "\\S-" begin t))
  21187. (progn (skip-chars-backward " \r\t\n") (point))
  21188. (point)))))
  21189. (all-fixed-width-p
  21190. (catch 'not-all-p
  21191. (save-excursion
  21192. (goto-char begin)
  21193. (skip-chars-forward " \r\t\n")
  21194. (when (eobp) (throw 'not-all-p nil))
  21195. (while (< (point) end)
  21196. (let ((element (org-element-at-point)))
  21197. (if (eq (org-element-type element) 'fixed-width)
  21198. (goto-char (org-element-property :end element))
  21199. (throw 'not-all-p nil))))
  21200. t))))
  21201. (if all-fixed-width-p
  21202. (save-excursion
  21203. (goto-char begin)
  21204. (while (< (point) end)
  21205. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21206. (replace-match
  21207. "" nil nil nil
  21208. (if (= (line-end-position) (match-end 0)) 0 1)))
  21209. (forward-line)))
  21210. (let ((min-ind (point-max)))
  21211. ;; Find minimum indentation across all lines.
  21212. (save-excursion
  21213. (goto-char begin)
  21214. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21215. (setq min-ind 0)
  21216. (catch 'zerop
  21217. (while (< (point) end)
  21218. (unless (looking-at-p "[ \t]*$")
  21219. (let ((ind (org-get-indentation)))
  21220. (setq min-ind (min min-ind ind))
  21221. (when (zerop ind) (throw 'zerop t))))
  21222. (forward-line)))))
  21223. ;; Loop over all lines and add fixed-width markup everywhere
  21224. ;; but in fixed-width lines.
  21225. (save-excursion
  21226. (goto-char begin)
  21227. (while (< (point) end)
  21228. (cond
  21229. ((org-at-heading-p)
  21230. (insert ": ")
  21231. (forward-line)
  21232. (while (and (< (point) end) (looking-at-p "[ \t]*$"))
  21233. (insert ":")
  21234. (forward-line)))
  21235. ((looking-at-p "[ \t]*:\\( \\|$\\)")
  21236. (let* ((element (org-element-at-point))
  21237. (element-end (org-element-property :end element)))
  21238. (if (eq (org-element-type element) 'fixed-width)
  21239. (progn (goto-char element-end)
  21240. (skip-chars-backward " \r\t\n")
  21241. (forward-line))
  21242. (let ((limit (min end element-end)))
  21243. (while (< (point) limit)
  21244. (org-move-to-column min-ind t)
  21245. (insert ": ")
  21246. (forward-line))))))
  21247. (t
  21248. (org-move-to-column min-ind t)
  21249. (insert ": ")
  21250. (forward-line)))))))
  21251. (set-marker end nil))))
  21252. ;;; Comments
  21253. ;; Org comments syntax is quite complex. It requires the entire line
  21254. ;; to be just a comment. Also, even with the right syntax at the
  21255. ;; beginning of line, some elements (e.g., verse-block or
  21256. ;; example-block) don't accept comments. Usual Emacs comment commands
  21257. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21258. ;; Org still relies on `comment-dwim', but cannot trust
  21259. ;; `comment-only-p'. So, `comment-region-function' and
  21260. ;; `uncomment-region-function' both point
  21261. ;; to`org-comment-or-uncomment-region'. Eventually,
  21262. ;; `org-insert-comment' takes care of insertion of comments at the
  21263. ;; beginning of line.
  21264. ;; `org-setup-comments-handling' install comments related variables
  21265. ;; during `org-mode' initialization.
  21266. (defun org-setup-comments-handling ()
  21267. (interactive)
  21268. (setq-local comment-use-syntax nil)
  21269. (setq-local comment-start "# ")
  21270. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21271. (setq-local comment-insert-comment-function 'org-insert-comment)
  21272. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21273. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21274. (defun org-insert-comment ()
  21275. "Insert an empty comment above current line.
  21276. If the line is empty, insert comment at its beginning. When
  21277. point is within a source block, comment according to the related
  21278. major mode."
  21279. (if (let ((element (org-element-at-point)))
  21280. (and (eq (org-element-type element) 'src-block)
  21281. (< (save-excursion
  21282. (goto-char (org-element-property :post-affiliated element))
  21283. (line-end-position))
  21284. (point))
  21285. (> (save-excursion
  21286. (goto-char (org-element-property :end element))
  21287. (skip-chars-backward " \r\t\n")
  21288. (line-beginning-position))
  21289. (point))))
  21290. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21291. (beginning-of-line)
  21292. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21293. (open-line 1))
  21294. (org-indent-line)
  21295. (insert "# ")))
  21296. (defvar comment-empty-lines) ; From newcomment.el.
  21297. (defun org-comment-or-uncomment-region (beg end &rest _)
  21298. "Comment or uncomment each non-blank line in the region.
  21299. Uncomment each non-blank line between BEG and END if it only
  21300. contains commented lines. Otherwise, comment them. If region is
  21301. strictly within a source block, use appropriate comment syntax."
  21302. (if (let ((element (org-element-at-point)))
  21303. (and (eq (org-element-type element) 'src-block)
  21304. (< (save-excursion
  21305. (goto-char (org-element-property :post-affiliated element))
  21306. (line-end-position))
  21307. beg)
  21308. (>= (save-excursion
  21309. (goto-char (org-element-property :end element))
  21310. (skip-chars-backward " \r\t\n")
  21311. (line-beginning-position))
  21312. end)))
  21313. ;; Translate region boundaries for the Org buffer to the source
  21314. ;; buffer.
  21315. (let ((offset (- end beg)))
  21316. (save-excursion
  21317. (goto-char beg)
  21318. (org-babel-do-in-edit-buffer
  21319. (comment-or-uncomment-region (point) (+ offset (point))))))
  21320. (save-restriction
  21321. ;; Restrict region
  21322. (narrow-to-region (save-excursion (goto-char beg)
  21323. (skip-chars-forward " \r\t\n" end)
  21324. (line-beginning-position))
  21325. (save-excursion (goto-char end)
  21326. (skip-chars-backward " \r\t\n" beg)
  21327. (line-end-position)))
  21328. (let ((uncommentp
  21329. ;; UNCOMMENTP is non-nil when every non blank line between
  21330. ;; BEG and END is a comment.
  21331. (save-excursion
  21332. (goto-char (point-min))
  21333. (while (and (not (eobp))
  21334. (let ((element (org-element-at-point)))
  21335. (and (eq (org-element-type element) 'comment)
  21336. (goto-char (min (point-max)
  21337. (org-element-property
  21338. :end element)))))))
  21339. (eobp))))
  21340. (if uncommentp
  21341. ;; Only blank lines and comments in region: uncomment it.
  21342. (save-excursion
  21343. (goto-char (point-min))
  21344. (while (not (eobp))
  21345. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21346. (replace-match "" nil nil nil 1))
  21347. (forward-line)))
  21348. ;; Comment each line in region.
  21349. (let ((min-indent (point-max)))
  21350. ;; First find the minimum indentation across all lines.
  21351. (save-excursion
  21352. (goto-char (point-min))
  21353. (while (and (not (eobp)) (not (zerop min-indent)))
  21354. (unless (looking-at "[ \t]*$")
  21355. (setq min-indent (min min-indent (current-indentation))))
  21356. (forward-line)))
  21357. ;; Then loop over all lines.
  21358. (save-excursion
  21359. (goto-char (point-min))
  21360. (while (not (eobp))
  21361. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21362. ;; Don't get fooled by invisible text (e.g. link path)
  21363. ;; when moving to column MIN-INDENT.
  21364. (let ((buffer-invisibility-spec nil))
  21365. (org-move-to-column min-indent t))
  21366. (insert comment-start))
  21367. (forward-line)))))))))
  21368. (defun org-comment-dwim (_arg)
  21369. "Call `comment-dwim' within a source edit buffer if needed."
  21370. (interactive "*P")
  21371. (if (org-in-src-block-p)
  21372. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21373. (call-interactively 'comment-dwim)))
  21374. ;;; Timestamps API
  21375. ;; This section contains tools to operate on timestamp objects, as
  21376. ;; returned by, e.g. `org-element-context'.
  21377. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21378. "Encode TIMESTAMP object into Emacs internal time.
  21379. Use end of date range or time range when END is non-nil."
  21380. (apply #'encode-time
  21381. (cons 0
  21382. (mapcar
  21383. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21384. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21385. '(:minute-start :hour-start :day-start :month-start
  21386. :year-start))))))
  21387. (defun org-timestamp-has-time-p (timestamp)
  21388. "Non-nil when TIMESTAMP has a time specified."
  21389. (org-element-property :hour-start timestamp))
  21390. (defun org-timestamp-format (timestamp format &optional end utc)
  21391. "Format a TIMESTAMP object into a string.
  21392. FORMAT is a format specifier to be passed to
  21393. `format-time-string'.
  21394. When optional argument END is non-nil, use end of date-range or
  21395. time-range, if possible.
  21396. When optional argument UTC is non-nil, time will be expressed as
  21397. Universal Time."
  21398. (format-time-string
  21399. format (org-timestamp--to-internal-time timestamp end)
  21400. (and utc t)))
  21401. (defun org-timestamp-split-range (timestamp &optional end)
  21402. "Extract a TIMESTAMP object from a date or time range.
  21403. END, when non-nil, means extract the end of the range.
  21404. Otherwise, extract its start.
  21405. Return a new timestamp object."
  21406. (let ((type (org-element-property :type timestamp)))
  21407. (if (memq type '(active inactive diary)) timestamp
  21408. (let ((split-ts (org-element-copy timestamp)))
  21409. ;; Set new type.
  21410. (org-element-put-property
  21411. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21412. ;; Copy start properties over end properties if END is
  21413. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21414. (let ((p-alist '((:minute-start . :minute-end)
  21415. (:hour-start . :hour-end)
  21416. (:day-start . :day-end)
  21417. (:month-start . :month-end)
  21418. (:year-start . :year-end))))
  21419. (dolist (p-cell p-alist)
  21420. (org-element-put-property
  21421. split-ts
  21422. (funcall (if end #'car #'cdr) p-cell)
  21423. (org-element-property
  21424. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21425. ;; Eventually refresh `:raw-value'.
  21426. (org-element-put-property split-ts :raw-value nil)
  21427. (org-element-put-property
  21428. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21429. (defun org-timestamp-translate (timestamp &optional boundary)
  21430. "Translate TIMESTAMP object to custom format.
  21431. Format string is defined in `org-time-stamp-custom-formats',
  21432. which see.
  21433. When optional argument BOUNDARY is non-nil, it is either the
  21434. symbol `start' or `end'. In this case, only translate the
  21435. starting or ending part of TIMESTAMP if it is a date or time
  21436. range. Otherwise, translate both parts.
  21437. Return timestamp as-is if `org-display-custom-times' is nil or if
  21438. it has a `diary' type."
  21439. (let ((type (org-element-property :type timestamp)))
  21440. (if (or (not org-display-custom-times) (eq type 'diary))
  21441. (org-element-interpret-data timestamp)
  21442. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21443. org-time-stamp-custom-formats)))
  21444. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21445. (concat (org-timestamp-format timestamp fmt)
  21446. "--"
  21447. (org-timestamp-format timestamp fmt t))
  21448. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21449. ;;; Other stuff.
  21450. (defvar reftex-docstruct-symbol)
  21451. (defvar org--rds)
  21452. (defun org-reftex-citation ()
  21453. "Use reftex-citation to insert a citation into the buffer.
  21454. This looks for a line like
  21455. #+BIBLIOGRAPHY: foo plain option:-d
  21456. and derives from it that foo.bib is the bibliography file relevant
  21457. for this document. It then installs the necessary environment for RefTeX
  21458. to work in this buffer and calls `reftex-citation' to insert a citation
  21459. into the buffer.
  21460. Export of such citations to both LaTeX and HTML is handled by the contributed
  21461. package ox-bibtex by Taru Karttunen."
  21462. (interactive)
  21463. (let ((reftex-docstruct-symbol 'org--rds)
  21464. org--rds bib)
  21465. (org-with-wide-buffer
  21466. (let ((case-fold-search t)
  21467. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21468. (if (not (save-excursion
  21469. (or (re-search-forward re nil t)
  21470. (re-search-backward re nil t))))
  21471. (user-error "No bibliography defined in file")
  21472. (setq bib (concat (match-string 1) ".bib")
  21473. org--rds (list (list 'bib bib))))))
  21474. (call-interactively 'reftex-citation)))
  21475. ;;;; Functions extending outline functionality
  21476. (defun org-beginning-of-line (&optional n)
  21477. "Go to the beginning of the current visible line.
  21478. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21479. tags on the first attempt, and only move to after the tags when
  21480. the cursor is already beyond the end of the headline.
  21481. With argument N not nil or 1, move forward N - 1 lines first."
  21482. (interactive "^p")
  21483. (let ((origin (point))
  21484. (special (pcase org-special-ctrl-a/e
  21485. (`(,C-a . ,_) C-a) (_ org-special-ctrl-a/e)))
  21486. deactivate-mark)
  21487. ;; First move to a visible line.
  21488. (if (bound-and-true-p visual-line-mode)
  21489. (beginning-of-visual-line n)
  21490. (move-beginning-of-line n)
  21491. ;; `move-beginning-of-line' may leave point after invisible
  21492. ;; characters if line starts with such of these (e.g., with
  21493. ;; a link at column 0). Really move to the beginning of the
  21494. ;; current visible line.
  21495. (beginning-of-line))
  21496. (cond
  21497. ;; No special behavior. Point is already at the beginning of
  21498. ;; a line, logical or visual.
  21499. ((not special))
  21500. ;; `beginning-of-visual-line' left point before logical beginning
  21501. ;; of line: point is at the beginning of a visual line. Bail
  21502. ;; out.
  21503. ((and (bound-and-true-p visual-line-mode) (not (bolp))))
  21504. ((let ((case-fold-search nil)) (looking-at org-complex-heading-regexp))
  21505. ;; At a headline, special position is before the title, but
  21506. ;; after any TODO keyword or priority cookie.
  21507. (let ((refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21508. (line-end-position)))
  21509. (bol (point)))
  21510. (if (eq special 'reversed)
  21511. (when (and (= origin bol) (eq last-command this-command))
  21512. (goto-char refpos))
  21513. (when (or (> origin refpos) (= origin bol))
  21514. (goto-char refpos)))))
  21515. ((and (looking-at org-list-full-item-re)
  21516. (memq (org-element-type (save-match-data (org-element-at-point)))
  21517. '(item plain-list)))
  21518. ;; Set special position at first white space character after
  21519. ;; bullet, and check-box, if any.
  21520. (let ((after-bullet
  21521. (let ((box (match-end 3)))
  21522. (cond ((not box) (match-end 1))
  21523. ((eq (char-after box) ?\s) (1+ box))
  21524. (t box)))))
  21525. (if (eq special 'reversed)
  21526. (when (and (= (point) origin) (eq last-command this-command))
  21527. (goto-char after-bullet))
  21528. (when (or (> origin after-bullet) (= (point) origin))
  21529. (goto-char after-bullet)))))
  21530. ;; No special context. Point is already at beginning of line.
  21531. (t nil))))
  21532. (defun org-end-of-line (&optional n)
  21533. "Go to the end of the line, but before ellipsis, if any.
  21534. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21535. tags on the first attempt, and only move to after the tags when
  21536. the cursor is already beyond the end of the headline.
  21537. With argument N not nil or 1, move forward N - 1 lines first."
  21538. (interactive "^p")
  21539. (let ((origin (point))
  21540. (special (pcase org-special-ctrl-a/e
  21541. (`(,_ . ,C-e) C-e) (_ org-special-ctrl-a/e)))
  21542. deactivate-mark)
  21543. ;; First move to a visible line.
  21544. (if (bound-and-true-p visual-line-mode)
  21545. (beginning-of-visual-line n)
  21546. (move-beginning-of-line n))
  21547. (cond
  21548. ;; At a headline, with tags.
  21549. ((and special
  21550. (save-excursion
  21551. (beginning-of-line)
  21552. (let ((case-fold-search nil))
  21553. (looking-at org-complex-heading-regexp)))
  21554. (match-end 5))
  21555. (let ((tags (save-excursion
  21556. (goto-char (match-beginning 5))
  21557. (skip-chars-backward " \t")
  21558. (point)))
  21559. (visual-end (and (bound-and-true-p visual-line-mode)
  21560. (save-excursion
  21561. (end-of-visual-line)
  21562. (point)))))
  21563. ;; If `end-of-visual-line' brings us before end of line or
  21564. ;; even tags, i.e., the headline spans over multiple visual
  21565. ;; lines, move there.
  21566. (cond ((and visual-end
  21567. (< visual-end tags)
  21568. (<= origin visual-end))
  21569. (goto-char visual-end))
  21570. ((eq special 'reversed)
  21571. (if (and (= origin (line-end-position))
  21572. (eq this-command last-command))
  21573. (goto-char tags)
  21574. (end-of-line)))
  21575. (t
  21576. (if (or (< origin tags) (= origin (line-end-position)))
  21577. (goto-char tags)
  21578. (end-of-line))))))
  21579. ((bound-and-true-p visual-line-mode)
  21580. (let ((bol (line-beginning-position)))
  21581. (end-of-visual-line)
  21582. ;; If `end-of-visual-line' gets us past the ellipsis at the
  21583. ;; end of a line, backtrack and use `end-of-line' instead.
  21584. (when (/= bol (line-beginning-position))
  21585. (goto-char bol)
  21586. (end-of-line))))
  21587. (t (end-of-line)))))
  21588. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21589. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21590. (defun org-backward-sentence (&optional _arg)
  21591. "Go to beginning of sentence, or beginning of table field.
  21592. This will call `backward-sentence' or `org-table-beginning-of-field',
  21593. depending on context."
  21594. (interactive)
  21595. (let* ((element (org-element-at-point))
  21596. (contents-begin (org-element-property :contents-begin element))
  21597. (table (org-element-lineage element '(table) t)))
  21598. (if (and table
  21599. (> (point) contents-begin)
  21600. (<= (point) (org-element-property :contents-end table)))
  21601. (call-interactively #'org-table-beginning-of-field)
  21602. (save-restriction
  21603. (when (and contents-begin
  21604. (< (point-min) contents-begin)
  21605. (> (point) contents-begin))
  21606. (narrow-to-region contents-begin
  21607. (org-element-property :contents-end element)))
  21608. (call-interactively #'backward-sentence)))))
  21609. (defun org-forward-sentence (&optional _arg)
  21610. "Go to end of sentence, or end of table field.
  21611. This will call `forward-sentence' or `org-table-end-of-field',
  21612. depending on context."
  21613. (interactive)
  21614. (if (and (org-at-heading-p)
  21615. (save-restriction (skip-chars-forward " \t") (not (eolp))))
  21616. (save-restriction
  21617. (narrow-to-region (line-beginning-position) (line-end-position))
  21618. (call-interactively #'forward-sentence))
  21619. (let* ((element (org-element-at-point))
  21620. (contents-end (org-element-property :contents-end element))
  21621. (table (org-element-lineage element '(table) t)))
  21622. (if (and table
  21623. (>= (point) (org-element-property :contents-begin table))
  21624. (< (point) contents-end))
  21625. (call-interactively #'org-table-end-of-field)
  21626. (save-restriction
  21627. (when (and contents-end
  21628. (> (point-max) contents-end)
  21629. ;; Skip blank lines between elements.
  21630. (< (org-element-property :end element)
  21631. (save-excursion (goto-char contents-end)
  21632. (skip-chars-forward " \r\t\n"))))
  21633. (narrow-to-region (org-element-property :contents-begin element)
  21634. contents-end))
  21635. ;; End of heading is considered as the end of a sentence.
  21636. (let ((sentence-end (concat (sentence-end) "\\|^\\*+ .*$")))
  21637. (call-interactively #'forward-sentence)))))))
  21638. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21639. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21640. (defun org-kill-line (&optional _arg)
  21641. "Kill line, to tags or end of line."
  21642. (interactive)
  21643. (cond
  21644. ((or (not org-special-ctrl-k)
  21645. (bolp)
  21646. (not (org-at-heading-p)))
  21647. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21648. org-ctrl-k-protect-subtree
  21649. (or (eq org-ctrl-k-protect-subtree 'error)
  21650. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  21651. (user-error "C-k aborted as it would kill a hidden subtree"))
  21652. (call-interactively
  21653. (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21654. ((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
  21655. (kill-region (point) (match-beginning 1))
  21656. (org-set-tags nil t))
  21657. (t (kill-region (point) (point-at-eol)))))
  21658. (define-key org-mode-map "\C-k" 'org-kill-line)
  21659. (defun org-yank (&optional arg)
  21660. "Yank. If the kill is a subtree, treat it specially.
  21661. This command will look at the current kill and check if is a single
  21662. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21663. cursor is at the beginning of a line or after the stars of a currently
  21664. empty headline, then the yank is handled specially. How exactly depends
  21665. on the value of the following variables.
  21666. `org-yank-folded-subtrees'
  21667. By default, this variable is non-nil, which results in
  21668. subtree(s) being folded after insertion, except if doing so
  21669. would swallow text after the yanked text.
  21670. `org-yank-adjusted-subtrees'
  21671. When non-nil (the default value is nil), the subtree will be
  21672. promoted or demoted in order to fit into the local outline tree
  21673. structure, which means that the level will be adjusted so that it
  21674. becomes the smaller one of the two *visible* surrounding headings.
  21675. Any prefix to this command will cause `yank' to be called directly with
  21676. no special treatment. In particular, a simple `\\[universal-argument]' prefix \
  21677. will just
  21678. plainly yank the text as it is.
  21679. \[1] The test checks if the first non-white line is a heading
  21680. and if there are no other headings with fewer stars."
  21681. (interactive "P")
  21682. (org-yank-generic 'yank arg))
  21683. (defun org-yank-generic (command arg)
  21684. "Perform some yank-like command.
  21685. This function implements the behavior described in the `org-yank'
  21686. documentation. However, it has been generalized to work for any
  21687. interactive command with similar behavior."
  21688. ;; pretend to be command COMMAND
  21689. (setq this-command command)
  21690. (if arg
  21691. (call-interactively command)
  21692. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21693. (and (org-kill-is-subtree-p)
  21694. (or (bolp)
  21695. (and (looking-at "[ \t]*$")
  21696. (string-match
  21697. "\\`\\*+\\'"
  21698. (buffer-substring (point-at-bol) (point)))))))
  21699. swallowp)
  21700. (cond
  21701. ((and subtreep org-yank-folded-subtrees)
  21702. (let ((beg (point))
  21703. end)
  21704. (if (and subtreep org-yank-adjusted-subtrees)
  21705. (org-paste-subtree nil nil 'for-yank)
  21706. (call-interactively command))
  21707. (setq end (point))
  21708. (goto-char beg)
  21709. (when (and (bolp) subtreep
  21710. (not (setq swallowp
  21711. (org-yank-folding-would-swallow-text beg end))))
  21712. (org-with-limited-levels
  21713. (or (looking-at org-outline-regexp)
  21714. (re-search-forward org-outline-regexp-bol end t))
  21715. (while (and (< (point) end) (looking-at org-outline-regexp))
  21716. (outline-hide-subtree)
  21717. (org-cycle-show-empty-lines 'folded)
  21718. (condition-case nil
  21719. (outline-forward-same-level 1)
  21720. (error (goto-char end))))))
  21721. (when swallowp
  21722. (message
  21723. "Inserted text not folded because that would swallow text"))
  21724. (goto-char end)
  21725. (skip-chars-forward " \t\n\r")
  21726. (beginning-of-line 1)
  21727. (push-mark beg 'nomsg)))
  21728. ((and subtreep org-yank-adjusted-subtrees)
  21729. (let ((beg (point-at-bol)))
  21730. (org-paste-subtree nil nil 'for-yank)
  21731. (push-mark beg 'nomsg)))
  21732. (t
  21733. (call-interactively command))))))
  21734. (defun org-yank-folding-would-swallow-text (beg end)
  21735. "Would hide-subtree at BEG swallow any text after END?"
  21736. (let (level)
  21737. (org-with-limited-levels
  21738. (save-excursion
  21739. (goto-char beg)
  21740. (when (or (looking-at org-outline-regexp)
  21741. (re-search-forward org-outline-regexp-bol end t))
  21742. (setq level (org-outline-level)))
  21743. (goto-char end)
  21744. (skip-chars-forward " \t\r\n\v\f")
  21745. (not (or (eobp)
  21746. (and (bolp) (looking-at-p org-outline-regexp)
  21747. (<= (org-outline-level) level))))))))
  21748. (define-key org-mode-map "\C-y" 'org-yank)
  21749. (defun org-truely-invisible-p ()
  21750. "Check if point is at a character currently not visible.
  21751. This version does not only check the character property, but also
  21752. `visible-mode'."
  21753. (unless (bound-and-true-p visible-mode)
  21754. (org-invisible-p)))
  21755. (defun org-invisible-p2 ()
  21756. "Check if point is at a character currently not visible.
  21757. If the point is at EOL (and not at the beginning of a buffer too),
  21758. move it back by one char before doing this check."
  21759. (save-excursion
  21760. (when (and (eolp) (not (bobp)))
  21761. (backward-char 1))
  21762. (org-invisible-p)))
  21763. (defun org-back-to-heading (&optional invisible-ok)
  21764. "Call `outline-back-to-heading', but provide a better error message."
  21765. (condition-case nil
  21766. (outline-back-to-heading invisible-ok)
  21767. (error (error "Before first headline at position %d in buffer %s"
  21768. (point) (current-buffer)))))
  21769. (defun org-before-first-heading-p ()
  21770. "Before first heading?"
  21771. (save-excursion
  21772. (end-of-line)
  21773. (null (re-search-backward org-outline-regexp-bol nil t))))
  21774. (defun org-at-heading-p (&optional ignored)
  21775. (outline-on-heading-p t))
  21776. (defun org-in-commented-heading-p (&optional no-inheritance)
  21777. "Non-nil if point is under a commented heading.
  21778. This function also checks ancestors of the current headline,
  21779. unless optional argument NO-INHERITANCE is non-nil."
  21780. (cond
  21781. ((org-before-first-heading-p) nil)
  21782. ((let ((headline (nth 4 (org-heading-components))))
  21783. (and headline
  21784. (let ((case-fold-search nil))
  21785. (string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21786. headline)))))
  21787. (no-inheritance nil)
  21788. (t
  21789. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21790. (defun org-at-comment-p nil
  21791. "Is cursor in a commented line?"
  21792. (save-excursion
  21793. (save-match-data
  21794. (beginning-of-line)
  21795. (looking-at "^[ \t]*# "))))
  21796. (defun org-at-drawer-p nil
  21797. "Is cursor at a drawer keyword?"
  21798. (save-excursion
  21799. (move-beginning-of-line 1)
  21800. (looking-at org-drawer-regexp)))
  21801. (defun org-at-block-p nil
  21802. "Is cursor at a block keyword?"
  21803. (save-excursion
  21804. (move-beginning-of-line 1)
  21805. (looking-at org-block-regexp)))
  21806. (defun org-point-at-end-of-empty-headline ()
  21807. "If point is at the end of an empty headline, return t, else nil.
  21808. If the heading only contains a TODO keyword, it is still still considered
  21809. empty."
  21810. (let ((case-fold-search nil))
  21811. (and (looking-at "[ \t]*$")
  21812. org-todo-line-regexp
  21813. (save-excursion
  21814. (beginning-of-line)
  21815. (looking-at org-todo-line-regexp)
  21816. (string= (match-string 3) "")))))
  21817. (defun org-at-heading-or-item-p ()
  21818. (or (org-at-heading-p) (org-at-item-p)))
  21819. (defun org-at-target-p ()
  21820. (or (org-in-regexp org-radio-target-regexp)
  21821. (org-in-regexp org-target-regexp)))
  21822. ;; Compatibility alias with Org versions < 7.8.03
  21823. (defalias 'org-on-target-p 'org-at-target-p)
  21824. (defun org-up-heading-all (arg)
  21825. "Move to the heading line of which the present line is a subheading.
  21826. This function considers both visible and invisible heading lines.
  21827. With argument, move up ARG levels."
  21828. (outline-up-heading arg t))
  21829. (defun org-up-heading-safe ()
  21830. "Move to the heading line of which the present line is a subheading.
  21831. This version will not throw an error. It will return the level of the
  21832. headline found, or nil if no higher level is found.
  21833. Also, this function will be a lot faster than `outline-up-heading',
  21834. because it relies on stars being the outline starters. This can really
  21835. make a significant difference in outlines with very many siblings."
  21836. (when (ignore-errors (org-back-to-heading t))
  21837. (let ((level-up (1- (funcall outline-level))))
  21838. (and (> level-up 0)
  21839. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21840. (funcall outline-level)))))
  21841. (defun org-first-sibling-p ()
  21842. "Is this heading the first child of its parents?"
  21843. (interactive)
  21844. (let ((re org-outline-regexp-bol)
  21845. level l)
  21846. (unless (org-at-heading-p t)
  21847. (user-error "Not at a heading"))
  21848. (setq level (funcall outline-level))
  21849. (save-excursion
  21850. (if (not (re-search-backward re nil t))
  21851. t
  21852. (setq l (funcall outline-level))
  21853. (< l level)))))
  21854. (defun org-goto-sibling (&optional previous)
  21855. "Goto the next sibling, even if it is invisible.
  21856. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21857. when a sibling was found. When none is found, return nil and don't
  21858. move point."
  21859. (let ((fun (if previous 're-search-backward 're-search-forward))
  21860. (pos (point))
  21861. (re org-outline-regexp-bol)
  21862. level l)
  21863. (when (ignore-errors (org-back-to-heading t))
  21864. (setq level (funcall outline-level))
  21865. (catch 'exit
  21866. (or previous (forward-char 1))
  21867. (while (funcall fun re nil t)
  21868. (setq l (funcall outline-level))
  21869. (when (< l level) (goto-char pos) (throw 'exit nil))
  21870. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21871. (goto-char pos)
  21872. nil))))
  21873. (defun org-show-siblings ()
  21874. "Show all siblings of the current headline."
  21875. (save-excursion
  21876. (while (org-goto-sibling) (org-flag-heading nil)))
  21877. (save-excursion
  21878. (while (org-goto-sibling 'previous)
  21879. (org-flag-heading nil))))
  21880. (defun org-goto-first-child ()
  21881. "Goto the first child, even if it is invisible.
  21882. Return t when a child was found. Otherwise don't move point and
  21883. return nil."
  21884. (let (level (pos (point)) (re org-outline-regexp-bol))
  21885. (when (ignore-errors (org-back-to-heading t))
  21886. (setq level (outline-level))
  21887. (forward-char 1)
  21888. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21889. (progn (goto-char (match-beginning 0)) t)
  21890. (goto-char pos) nil))))
  21891. (defun org-show-hidden-entry ()
  21892. "Show an entry where even the heading is hidden."
  21893. (save-excursion
  21894. (org-show-entry)))
  21895. (defun org-flag-heading (flag &optional entry)
  21896. "Flag the current heading. FLAG non-nil means make invisible.
  21897. When ENTRY is non-nil, show the entire entry."
  21898. (save-excursion
  21899. (org-back-to-heading t)
  21900. ;; Check if we should show the entire entry
  21901. (if entry
  21902. (progn
  21903. (org-show-entry)
  21904. (save-excursion
  21905. (and (outline-next-heading)
  21906. (org-flag-heading nil))))
  21907. (outline-flag-region (max (point-min) (1- (point)))
  21908. (save-excursion (outline-end-of-heading) (point))
  21909. flag))))
  21910. (defun org-get-next-sibling ()
  21911. "Move to next heading of the same level, and return point.
  21912. If there is no such heading, return nil.
  21913. This is like outline-next-sibling, but invisible headings are ok."
  21914. (let ((level (funcall outline-level)))
  21915. (outline-next-heading)
  21916. (while (and (not (eobp)) (> (funcall outline-level) level))
  21917. (outline-next-heading))
  21918. (unless (or (eobp) (< (funcall outline-level) level))
  21919. (point))))
  21920. (defun org-get-last-sibling ()
  21921. "Move to previous heading of the same level, and return point.
  21922. If there is no such heading, return nil."
  21923. (let ((opoint (point))
  21924. (level (funcall outline-level)))
  21925. (outline-previous-heading)
  21926. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21927. (while (and (> (funcall outline-level) level)
  21928. (not (bobp)))
  21929. (outline-previous-heading))
  21930. (unless (< (funcall outline-level) level)
  21931. (point)))))
  21932. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21933. "Goto to the end of a subtree."
  21934. ;; This contains an exact copy of the original function, but it uses
  21935. ;; `org-back-to-heading', to make it work also in invisible
  21936. ;; trees. And is uses an invisible-ok argument.
  21937. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21938. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21939. ;; with many children and grandchildren etc, this can be much faster
  21940. ;; than the outline version.
  21941. (org-back-to-heading invisible-ok)
  21942. (let ((first t)
  21943. (level (funcall outline-level)))
  21944. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21945. ;; A true heading (not a plain list item), in Org
  21946. ;; This means we can easily find the end by looking
  21947. ;; only for the right number of stars. Using a regexp to do
  21948. ;; this is so much faster than using a Lisp loop.
  21949. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21950. (forward-char 1)
  21951. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21952. ;; something else, do it the slow way
  21953. (while (and (not (eobp))
  21954. (or first (> (funcall outline-level) level)))
  21955. (setq first nil)
  21956. (outline-next-heading)))
  21957. (unless to-heading
  21958. (when (memq (preceding-char) '(?\n ?\^M))
  21959. ;; Go to end of line before heading
  21960. (forward-char -1)
  21961. (when (memq (preceding-char) '(?\n ?\^M))
  21962. ;; leave blank line before heading
  21963. (forward-char -1)))))
  21964. (point))
  21965. (defun org-end-of-meta-data (&optional full)
  21966. "Skip planning line and properties drawer in current entry.
  21967. When optional argument FULL is non-nil, also skip empty lines,
  21968. clocking lines and regular drawers at the beginning of the
  21969. entry."
  21970. (org-back-to-heading t)
  21971. (forward-line)
  21972. (when (looking-at-p org-planning-line-re) (forward-line))
  21973. (when (looking-at org-property-drawer-re)
  21974. (goto-char (match-end 0))
  21975. (forward-line))
  21976. (when (and full (not (org-at-heading-p)))
  21977. (catch 'exit
  21978. (let ((end (save-excursion (outline-next-heading) (point)))
  21979. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  21980. (while (not (eobp))
  21981. (cond ((looking-at-p org-drawer-regexp)
  21982. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  21983. (forward-line)
  21984. (throw 'exit t)))
  21985. ((looking-at-p re) (forward-line))
  21986. (t (throw 'exit t))))))))
  21987. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21988. "Move forward to the ARG'th subheading at same level as this one.
  21989. Stop at the first and last subheadings of a superior heading.
  21990. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21991. non-nil it will also look at invisible ones."
  21992. (interactive "p")
  21993. (let ((backward? (and arg (< arg 0))))
  21994. (if (org-before-first-heading-p)
  21995. (if backward? (goto-char (point-min)) (outline-next-heading))
  21996. (org-back-to-heading invisible-ok)
  21997. (unless backward? (end-of-line)) ;do not match current headline
  21998. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21999. (f (if backward? #'re-search-backward #'re-search-forward))
  22000. (count (if arg (abs arg) 1))
  22001. (result (point)))
  22002. (while (and (> count 0)
  22003. (funcall f org-outline-regexp-bol nil 'move))
  22004. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22005. (cond ((< l level) (setq count 0))
  22006. ((and (= l level)
  22007. (or invisible-ok
  22008. (not (org-invisible-p
  22009. (line-beginning-position)))))
  22010. (cl-decf count)
  22011. (when (= l level) (setq result (point)))))))
  22012. (goto-char result))
  22013. (beginning-of-line))))
  22014. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22015. "Move backward to the ARG'th subheading at same level as this one.
  22016. Stop at the first and last subheadings of a superior heading."
  22017. (interactive "p")
  22018. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22019. (defun org-next-visible-heading (arg)
  22020. "Move to the next visible heading.
  22021. This function wraps `outline-next-visible-heading' with
  22022. `org-with-limited-levels' in order to skip over inline tasks and
  22023. respect customization of `org-odd-levels-only'."
  22024. (interactive "p")
  22025. (org-with-limited-levels
  22026. (outline-next-visible-heading arg)))
  22027. (defun org-previous-visible-heading (arg)
  22028. "Move to the previous visible heading.
  22029. This function wraps `outline-previous-visible-heading' with
  22030. `org-with-limited-levels' in order to skip over inline tasks and
  22031. respect customization of `org-odd-levels-only'."
  22032. (interactive "p")
  22033. (org-with-limited-levels
  22034. (outline-previous-visible-heading arg)))
  22035. (defun org-next-block (arg &optional backward block-regexp)
  22036. "Jump to the next block.
  22037. With a prefix argument ARG, jump forward ARG many blocks.
  22038. When BACKWARD is non-nil, jump to the previous block.
  22039. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22040. Match data is set according to this regexp when the function
  22041. returns.
  22042. Return point at beginning of the opening line of found block.
  22043. Throw an error if no block is found."
  22044. (interactive "p")
  22045. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22046. (case-fold-search t)
  22047. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22048. (count (or arg 1))
  22049. (origin (point))
  22050. last-element)
  22051. (if backward (beginning-of-line) (end-of-line))
  22052. (while (and (> count 0) (funcall search-fn re nil t))
  22053. (let ((element (save-excursion
  22054. (goto-char (match-beginning 0))
  22055. (save-match-data (org-element-at-point)))))
  22056. (when (and (memq (org-element-type element)
  22057. '(center-block comment-block dynamic-block
  22058. example-block export-block quote-block
  22059. special-block src-block verse-block))
  22060. (<= (match-beginning 0)
  22061. (org-element-property :post-affiliated element)))
  22062. (setq last-element element)
  22063. (cl-decf count))))
  22064. (if (= count 0)
  22065. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22066. (save-match-data (org-show-context)))
  22067. (goto-char origin)
  22068. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22069. (defun org-previous-block (arg &optional block-regexp)
  22070. "Jump to the previous block.
  22071. With a prefix argument ARG, jump backward ARG many source blocks.
  22072. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22073. (interactive "p")
  22074. (org-next-block arg t block-regexp))
  22075. (defun org-forward-paragraph ()
  22076. "Move forward to beginning of next paragraph or equivalent.
  22077. The function moves point to the beginning of the next visible
  22078. structural element, which can be a paragraph, a table, a list
  22079. item, etc. It also provides some special moves for convenience:
  22080. - On an affiliated keyword, jump to the beginning of the
  22081. relative element.
  22082. - On an item or a footnote definition, move to the second
  22083. element inside, if any.
  22084. - On a table or a property drawer, jump after it.
  22085. - On a verse or source block, stop after blank lines."
  22086. (interactive)
  22087. (unless (eobp)
  22088. (let* ((deactivate-mark nil)
  22089. (element (org-element-at-point))
  22090. (type (org-element-type element))
  22091. (post-affiliated (org-element-property :post-affiliated element))
  22092. (contents-begin (org-element-property :contents-begin element))
  22093. (contents-end (org-element-property :contents-end element))
  22094. (end (let ((end (org-element-property :end element)) (parent element))
  22095. (while (and (setq parent (org-element-property :parent parent))
  22096. (= (org-element-property :contents-end parent) end))
  22097. (setq end (org-element-property :end parent)))
  22098. end)))
  22099. (cond ((not element)
  22100. (skip-chars-forward " \r\t\n")
  22101. (or (eobp) (beginning-of-line)))
  22102. ;; On affiliated keywords, move to element's beginning.
  22103. ((< (point) post-affiliated)
  22104. (goto-char post-affiliated))
  22105. ;; At a table row, move to the end of the table. Similarly,
  22106. ;; at a node property, move to the end of the property
  22107. ;; drawer.
  22108. ((memq type '(node-property table-row))
  22109. (goto-char (org-element-property
  22110. :end (org-element-property :parent element))))
  22111. ((memq type '(property-drawer table)) (goto-char end))
  22112. ;; Consider blank lines as separators in verse and source
  22113. ;; blocks to ease editing.
  22114. ((memq type '(src-block verse-block))
  22115. (when (eq type 'src-block)
  22116. (setq contents-end
  22117. (save-excursion (goto-char end)
  22118. (skip-chars-backward " \r\t\n")
  22119. (line-beginning-position))))
  22120. (beginning-of-line)
  22121. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22122. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22123. (goto-char end)
  22124. (skip-chars-forward " \r\t\n")
  22125. (if (= (point) contents-end) (goto-char end)
  22126. (beginning-of-line))))
  22127. ;; With no contents, just skip element.
  22128. ((not contents-begin) (goto-char end))
  22129. ;; If contents are invisible, skip the element altogether.
  22130. ((org-invisible-p (line-end-position))
  22131. (cl-case type
  22132. (headline
  22133. (org-with-limited-levels (outline-next-visible-heading 1)))
  22134. ;; At a plain list, make sure we move to the next item
  22135. ;; instead of skipping the whole list.
  22136. (plain-list (forward-char)
  22137. (org-forward-paragraph))
  22138. (otherwise (goto-char end))))
  22139. ((>= (point) contents-end) (goto-char end))
  22140. ((>= (point) contents-begin)
  22141. ;; This can only happen on paragraphs and plain lists.
  22142. (cl-case type
  22143. (paragraph (goto-char end))
  22144. ;; At a plain list, try to move to second element in
  22145. ;; first item, if possible.
  22146. (plain-list (end-of-line)
  22147. (org-forward-paragraph))))
  22148. ;; When contents start on the middle of a line (e.g. in
  22149. ;; items and footnote definitions), try to reach first
  22150. ;; element starting after current line.
  22151. ((> (line-end-position) contents-begin)
  22152. (end-of-line)
  22153. (org-forward-paragraph))
  22154. (t (goto-char contents-begin))))))
  22155. (defun org-backward-paragraph ()
  22156. "Move backward to start of previous paragraph or equivalent.
  22157. The function moves point to the beginning of the current
  22158. structural element, which can be a paragraph, a table, a list
  22159. item, etc., or to the beginning of the previous visible one if
  22160. point is already there. It also provides some special moves for
  22161. convenience:
  22162. - On an affiliated keyword, jump to the first one.
  22163. - On a table or a property drawer, move to its beginning.
  22164. - On comment, example, export, src and verse blocks, stop
  22165. before blank lines."
  22166. (interactive)
  22167. (unless (bobp)
  22168. (let* ((deactivate-mark nil)
  22169. (element (org-element-at-point))
  22170. (type (org-element-type element))
  22171. (contents-end (org-element-property :contents-end element))
  22172. (post-affiliated (org-element-property :post-affiliated element))
  22173. (begin (org-element-property :begin element))
  22174. (special? ;blocks handled specially
  22175. (memq type '(comment-block example-block export-block src-block
  22176. verse-block)))
  22177. (contents-begin
  22178. (if special?
  22179. ;; These types have no proper contents. Fake line
  22180. ;; below the block opening line as contents beginning.
  22181. (save-excursion (goto-char begin) (line-beginning-position 2))
  22182. (org-element-property :contents-begin element))))
  22183. (cond
  22184. ((not element) (goto-char (point-min)))
  22185. ((= (point) begin)
  22186. (backward-char)
  22187. (org-backward-paragraph))
  22188. ((<= (point) post-affiliated) (goto-char begin))
  22189. ;; Special behavior: on a table or a property drawer, move to
  22190. ;; its beginning.
  22191. ((memq type '(node-property table-row))
  22192. (goto-char (org-element-property
  22193. :post-affiliated (org-element-property :parent element))))
  22194. (special?
  22195. (if (<= (point) contents-begin) (goto-char post-affiliated)
  22196. ;; Inside a verse block, see blank lines as paragraph
  22197. ;; separators.
  22198. (let ((origin (point)))
  22199. (skip-chars-backward " \r\t\n" contents-begin)
  22200. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22201. (skip-chars-forward " \r\t\n" origin)
  22202. (if (= (point) origin) (goto-char contents-begin)
  22203. (beginning-of-line))))))
  22204. ((eq type 'paragraph) (goto-char contents-begin)
  22205. ;; When at first paragraph in an item or a footnote definition,
  22206. ;; move directly to beginning of line.
  22207. (let ((parent-contents
  22208. (org-element-property
  22209. :contents-begin (org-element-property :parent element))))
  22210. (when (and parent-contents (= parent-contents contents-begin))
  22211. (beginning-of-line))))
  22212. ;; At the end of a greater element, move to the beginning of
  22213. ;; the last element within.
  22214. ((and contents-end (>= (point) contents-end))
  22215. (goto-char (1- contents-end))
  22216. (org-backward-paragraph))
  22217. (t (goto-char (or post-affiliated begin))))
  22218. ;; Ensure we never leave point invisible.
  22219. (when (org-invisible-p (point)) (beginning-of-visual-line)))))
  22220. (defun org-forward-element ()
  22221. "Move forward by one element.
  22222. Move to the next element at the same level, when possible."
  22223. (interactive)
  22224. (cond ((eobp) (user-error "Cannot move further down"))
  22225. ((org-with-limited-levels (org-at-heading-p))
  22226. (let ((origin (point)))
  22227. (goto-char (org-end-of-subtree nil t))
  22228. (unless (org-with-limited-levels (org-at-heading-p))
  22229. (goto-char origin)
  22230. (user-error "Cannot move further down"))))
  22231. (t
  22232. (let* ((elem (org-element-at-point))
  22233. (end (org-element-property :end elem))
  22234. (parent (org-element-property :parent elem)))
  22235. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22236. (goto-char (org-element-property :end parent)))
  22237. ((integer-or-marker-p end) (goto-char end))
  22238. (t (message "No element at point")))))))
  22239. (defun org-backward-element ()
  22240. "Move backward by one element.
  22241. Move to the previous element at the same level, when possible."
  22242. (interactive)
  22243. (cond ((bobp) (user-error "Cannot move further up"))
  22244. ((org-with-limited-levels (org-at-heading-p))
  22245. ;; At a headline, move to the previous one, if any, or stay
  22246. ;; here.
  22247. (let ((origin (point)))
  22248. (org-with-limited-levels (org-backward-heading-same-level 1))
  22249. ;; When current headline has no sibling above, move to its
  22250. ;; parent.
  22251. (when (= (point) origin)
  22252. (or (org-with-limited-levels (org-up-heading-safe))
  22253. (progn (goto-char origin)
  22254. (user-error "Cannot move further up"))))))
  22255. (t
  22256. (let* ((elem (org-element-at-point))
  22257. (beg (org-element-property :begin elem)))
  22258. (cond
  22259. ;; Move to beginning of current element if point isn't
  22260. ;; there already.
  22261. ((null beg) (message "No element at point"))
  22262. ((/= (point) beg) (goto-char beg))
  22263. (t (goto-char beg)
  22264. (skip-chars-backward " \r\t\n")
  22265. (unless (bobp)
  22266. (let ((prev (org-element-at-point)))
  22267. (goto-char (org-element-property :begin prev))
  22268. (while (and (setq prev (org-element-property :parent prev))
  22269. (<= (org-element-property :end prev) beg))
  22270. (goto-char (org-element-property :begin prev)))))))))))
  22271. (defun org-up-element ()
  22272. "Move to upper element."
  22273. (interactive)
  22274. (if (org-with-limited-levels (org-at-heading-p))
  22275. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22276. (let* ((elem (org-element-at-point))
  22277. (parent (org-element-property :parent elem)))
  22278. (if parent (goto-char (org-element-property :begin parent))
  22279. (if (org-with-limited-levels (org-before-first-heading-p))
  22280. (user-error "No surrounding element")
  22281. (org-with-limited-levels (org-back-to-heading)))))))
  22282. (defun org-down-element ()
  22283. "Move to inner element."
  22284. (interactive)
  22285. (let ((element (org-element-at-point)))
  22286. (cond
  22287. ((memq (org-element-type element) '(plain-list table))
  22288. (goto-char (org-element-property :contents-begin element))
  22289. (forward-char))
  22290. ((memq (org-element-type element) org-element-greater-elements)
  22291. ;; If contents are hidden, first disclose them.
  22292. (when (org-invisible-p (line-end-position)) (org-cycle))
  22293. (goto-char (or (org-element-property :contents-begin element)
  22294. (user-error "No content for this element"))))
  22295. (t (user-error "No inner element")))))
  22296. (defun org-drag-element-backward ()
  22297. "Move backward element at point."
  22298. (interactive)
  22299. (let ((elem (or (org-element-at-point)
  22300. (user-error "No element at point"))))
  22301. (if (eq (org-element-type elem) 'headline)
  22302. ;; Preserve point when moving a whole tree, even if point was
  22303. ;; on blank lines below the headline.
  22304. (let ((offset (skip-chars-backward " \t\n")))
  22305. (unwind-protect (org-move-subtree-up)
  22306. (forward-char (- offset))))
  22307. (let ((prev-elem
  22308. (save-excursion
  22309. (goto-char (org-element-property :begin elem))
  22310. (skip-chars-backward " \r\t\n")
  22311. (unless (bobp)
  22312. (let* ((beg (org-element-property :begin elem))
  22313. (prev (org-element-at-point))
  22314. (up prev))
  22315. (while (and (setq up (org-element-property :parent up))
  22316. (<= (org-element-property :end up) beg))
  22317. (setq prev up))
  22318. prev)))))
  22319. ;; Error out if no previous element or previous element is
  22320. ;; a parent of the current one.
  22321. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22322. (user-error "Cannot drag element backward")
  22323. (let ((pos (point)))
  22324. (org-element-swap-A-B prev-elem elem)
  22325. (goto-char (+ (org-element-property :begin prev-elem)
  22326. (- pos (org-element-property :begin elem))))))))))
  22327. (defun org-drag-element-forward ()
  22328. "Move forward element at point."
  22329. (interactive)
  22330. (let* ((pos (point))
  22331. (elem (or (org-element-at-point)
  22332. (user-error "No element at point"))))
  22333. (when (= (point-max) (org-element-property :end elem))
  22334. (user-error "Cannot drag element forward"))
  22335. (goto-char (org-element-property :end elem))
  22336. (let ((next-elem (org-element-at-point)))
  22337. (when (or (org-element-nested-p elem next-elem)
  22338. (and (eq (org-element-type next-elem) 'headline)
  22339. (not (eq (org-element-type elem) 'headline))))
  22340. (goto-char pos)
  22341. (user-error "Cannot drag element forward"))
  22342. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22343. ;; body's length (without final blanks) and by the length of
  22344. ;; blanks between ELEM and NEXT-ELEM.
  22345. (let ((size-next (- (save-excursion
  22346. (goto-char (org-element-property :end next-elem))
  22347. (skip-chars-backward " \r\t\n")
  22348. (forward-line)
  22349. ;; Small correction if buffer doesn't end
  22350. ;; with a newline character.
  22351. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22352. (org-element-property :begin next-elem)))
  22353. (size-blank (- (org-element-property :end elem)
  22354. (save-excursion
  22355. (goto-char (org-element-property :end elem))
  22356. (skip-chars-backward " \r\t\n")
  22357. (forward-line)
  22358. (point)))))
  22359. (org-element-swap-A-B elem next-elem)
  22360. (goto-char (+ pos size-next size-blank))))))
  22361. (defun org-drag-line-forward (arg)
  22362. "Drag the line at point ARG lines forward."
  22363. (interactive "p")
  22364. (dotimes (_ (abs arg))
  22365. (let ((c (current-column)))
  22366. (if (< 0 arg)
  22367. (progn
  22368. (beginning-of-line 2)
  22369. (transpose-lines 1)
  22370. (beginning-of-line 0))
  22371. (transpose-lines 1)
  22372. (beginning-of-line -1))
  22373. (org-move-to-column c))))
  22374. (defun org-drag-line-backward (arg)
  22375. "Drag the line at point ARG lines backward."
  22376. (interactive "p")
  22377. (org-drag-line-forward (- arg)))
  22378. (defun org-mark-element ()
  22379. "Put point at beginning of this element, mark at end.
  22380. Interactively, if this command is repeated or (in Transient Mark
  22381. mode) if the mark is active, it marks the next element after the
  22382. ones already marked."
  22383. (interactive)
  22384. (let (deactivate-mark)
  22385. (if (and (called-interactively-p 'any)
  22386. (or (and (eq last-command this-command) (mark t))
  22387. (and transient-mark-mode mark-active)))
  22388. (set-mark
  22389. (save-excursion
  22390. (goto-char (mark))
  22391. (goto-char (org-element-property :end (org-element-at-point)))))
  22392. (let ((element (org-element-at-point)))
  22393. (end-of-line)
  22394. (push-mark (org-element-property :end element) t t)
  22395. (goto-char (org-element-property :begin element))))))
  22396. (defun org-narrow-to-element ()
  22397. "Narrow buffer to current element."
  22398. (interactive)
  22399. (let ((elem (org-element-at-point)))
  22400. (cond
  22401. ((eq (car elem) 'headline)
  22402. (narrow-to-region
  22403. (org-element-property :begin elem)
  22404. (org-element-property :end elem)))
  22405. ((memq (car elem) org-element-greater-elements)
  22406. (narrow-to-region
  22407. (org-element-property :contents-begin elem)
  22408. (org-element-property :contents-end elem)))
  22409. (t
  22410. (narrow-to-region
  22411. (org-element-property :begin elem)
  22412. (org-element-property :end elem))))))
  22413. (defun org-transpose-element ()
  22414. "Transpose current and previous elements, keeping blank lines between.
  22415. Point is moved after both elements."
  22416. (interactive)
  22417. (org-skip-whitespace)
  22418. (let ((end (org-element-property :end (org-element-at-point))))
  22419. (org-drag-element-backward)
  22420. (goto-char end)))
  22421. (defun org-unindent-buffer ()
  22422. "Un-indent the visible part of the buffer.
  22423. Relative indentation (between items, inside blocks, etc.) isn't
  22424. modified."
  22425. (interactive)
  22426. (unless (eq major-mode 'org-mode)
  22427. (user-error "Cannot un-indent a buffer not in Org mode"))
  22428. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22429. (unindent-tree
  22430. (lambda (contents)
  22431. (dolist (element (reverse contents))
  22432. (if (memq (org-element-type element) '(headline section))
  22433. (funcall unindent-tree (org-element-contents element))
  22434. (save-excursion
  22435. (save-restriction
  22436. (narrow-to-region
  22437. (org-element-property :begin element)
  22438. (org-element-property :end element))
  22439. (org-do-remove-indentation))))))))
  22440. (funcall unindent-tree (org-element-contents parse-tree))))
  22441. (defun org-show-children (&optional level)
  22442. "Show all direct subheadings of this heading.
  22443. Prefix arg LEVEL is how many levels below the current level
  22444. should be shown. Default is enough to cause the following
  22445. heading to appear."
  22446. (interactive "p")
  22447. ;; If `orgstruct-mode' is active, use the slower version.
  22448. (if orgstruct-mode (call-interactively #'outline-show-children)
  22449. (save-excursion
  22450. (org-back-to-heading t)
  22451. (let* ((current-level (funcall outline-level))
  22452. (max-level (org-get-valid-level
  22453. current-level
  22454. (if level (prefix-numeric-value level) 1)))
  22455. (end (save-excursion (org-end-of-subtree t t)))
  22456. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22457. (past-first-child nil)
  22458. ;; Make sure to skip inlinetasks.
  22459. (re (format regexp-fmt
  22460. current-level
  22461. (cond
  22462. ((not (featurep 'org-inlinetask)) "")
  22463. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22464. 3))
  22465. (t (1- org-inlinetask-min-level))))))
  22466. ;; Display parent heading.
  22467. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22468. (forward-line)
  22469. ;; Display children. First child may be deeper than expected
  22470. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22471. ;; MAX-LEVEL accordingly.
  22472. (while (re-search-forward re end t)
  22473. (unless past-first-child
  22474. (setq re (format regexp-fmt
  22475. current-level
  22476. (max (funcall outline-level) max-level)))
  22477. (setq past-first-child t))
  22478. (outline-flag-region
  22479. (line-end-position 0) (line-end-position) nil))))))
  22480. (defun org-show-subtree ()
  22481. "Show everything after this heading at deeper levels."
  22482. (interactive)
  22483. (outline-flag-region
  22484. (point)
  22485. (save-excursion
  22486. (org-end-of-subtree t t))
  22487. nil))
  22488. (defun org-show-entry ()
  22489. "Show the body directly following this heading.
  22490. Show the heading too, if it is currently invisible."
  22491. (interactive)
  22492. (save-excursion
  22493. (ignore-errors
  22494. (org-back-to-heading t)
  22495. (outline-flag-region
  22496. (max (point-min) (1- (point)))
  22497. (save-excursion
  22498. (if (re-search-forward
  22499. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22500. (match-beginning 1)
  22501. (point-max)))
  22502. nil)
  22503. (org-cycle-hide-drawers 'children))))
  22504. (defun org-make-options-regexp (kwds &optional extra)
  22505. "Make a regular expression for keyword lines.
  22506. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22507. when non-nil, is a regexp matching keywords names."
  22508. (concat "^[ \t]*#\\+\\("
  22509. (regexp-opt kwds)
  22510. (and extra (concat (and kwds "\\|") extra))
  22511. "\\):[ \t]*\\(.*\\)"))
  22512. ;;;; Integration with and fixes for other packages
  22513. ;;; Imenu support
  22514. (defvar-local org-imenu-markers nil
  22515. "All markers currently used by Imenu.")
  22516. (defun org-imenu-new-marker (&optional pos)
  22517. "Return a new marker for use by Imenu, and remember the marker."
  22518. (let ((m (make-marker)))
  22519. (move-marker m (or pos (point)))
  22520. (push m org-imenu-markers)
  22521. m))
  22522. (defun org-imenu-get-tree ()
  22523. "Produce the index for Imenu."
  22524. (dolist (x org-imenu-markers) (move-marker x nil))
  22525. (setq org-imenu-markers nil)
  22526. (let* ((case-fold-search nil)
  22527. (n org-imenu-depth)
  22528. (re (concat "^" (org-get-limited-outline-regexp)))
  22529. (subs (make-vector (1+ n) nil))
  22530. (last-level 0)
  22531. m level head0 head)
  22532. (org-with-wide-buffer
  22533. (goto-char (point-max))
  22534. (while (re-search-backward re nil t)
  22535. (setq level (org-reduced-level (funcall outline-level)))
  22536. (when (and (<= level n)
  22537. (looking-at org-complex-heading-regexp)
  22538. (setq head0 (match-string-no-properties 4)))
  22539. (setq head (org-link-display-format head0)
  22540. m (org-imenu-new-marker))
  22541. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22542. (if (>= level last-level)
  22543. (push (cons head m) (aref subs level))
  22544. (push (cons head (aref subs (1+ level))) (aref subs level))
  22545. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22546. (setq last-level level))))
  22547. (aref subs 1)))
  22548. (eval-after-load "imenu"
  22549. '(progn
  22550. (add-hook 'imenu-after-jump-hook
  22551. (lambda ()
  22552. (when (derived-mode-p 'org-mode)
  22553. (org-show-context 'org-goto))))))
  22554. (defun org-link-display-format (s)
  22555. "Replace links in string S with their description.
  22556. If there is no description, use the link target."
  22557. (save-match-data
  22558. (replace-regexp-in-string
  22559. org-bracket-link-analytic-regexp
  22560. (lambda (m)
  22561. (if (match-end 5) (match-string 5 m)
  22562. (concat (match-string 1 m) (match-string 3 m))))
  22563. s nil t)))
  22564. (defun org-toggle-link-display ()
  22565. "Toggle the literal or descriptive display of links."
  22566. (interactive)
  22567. (if org-descriptive-links
  22568. (progn (org-remove-from-invisibility-spec '(org-link))
  22569. (org-restart-font-lock)
  22570. (setq org-descriptive-links nil))
  22571. (progn (add-to-invisibility-spec '(org-link))
  22572. (org-restart-font-lock)
  22573. (setq org-descriptive-links t))))
  22574. ;; Speedbar support
  22575. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22576. "Overlay marking the agenda restriction line in speedbar.")
  22577. (overlay-put org-speedbar-restriction-lock-overlay
  22578. 'face 'org-agenda-restriction-lock)
  22579. (overlay-put org-speedbar-restriction-lock-overlay
  22580. 'help-echo "Agendas are currently limited to this item.")
  22581. (delete-overlay org-speedbar-restriction-lock-overlay)
  22582. (defun org-speedbar-set-agenda-restriction ()
  22583. "Restrict future agenda commands to the location at point in speedbar.
  22584. To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
  22585. (interactive)
  22586. (require 'org-agenda)
  22587. (let (p m tp np dir txt)
  22588. (cond
  22589. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22590. 'org-imenu t))
  22591. (setq m (get-text-property p 'org-imenu-marker))
  22592. (with-current-buffer (marker-buffer m)
  22593. (goto-char m)
  22594. (org-agenda-set-restriction-lock 'subtree)))
  22595. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22596. 'speedbar-function 'speedbar-find-file))
  22597. (setq tp (previous-single-property-change
  22598. (1+ p) 'speedbar-function)
  22599. np (next-single-property-change
  22600. tp 'speedbar-function)
  22601. dir (speedbar-line-directory)
  22602. txt (buffer-substring-no-properties (or tp (point-min))
  22603. (or np (point-max))))
  22604. (with-current-buffer (find-file-noselect
  22605. (let ((default-directory dir))
  22606. (expand-file-name txt)))
  22607. (unless (derived-mode-p 'org-mode)
  22608. (user-error "Cannot restrict to non-Org mode file"))
  22609. (org-agenda-set-restriction-lock 'file)))
  22610. (t (user-error "Don't know how to restrict Org mode agenda")))
  22611. (move-overlay org-speedbar-restriction-lock-overlay
  22612. (point-at-bol) (point-at-eol))
  22613. (setq current-prefix-arg nil)
  22614. (org-agenda-maybe-redo)))
  22615. (defvar speedbar-file-key-map)
  22616. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22617. (eval-after-load "speedbar"
  22618. '(progn
  22619. (speedbar-add-supported-extension ".org")
  22620. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22621. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22622. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22623. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22624. (add-hook 'speedbar-visiting-tag-hook
  22625. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22626. ;;; Fixes and Hacks for problems with other packages
  22627. (defun org--flyspell-object-check-p (element)
  22628. "Non-nil when Flyspell can check object at point.
  22629. ELEMENT is the element at point."
  22630. (let ((object (save-excursion
  22631. (when (looking-at-p "\\>") (backward-char))
  22632. (org-element-context element))))
  22633. (cl-case (org-element-type object)
  22634. ;; Prevent checks in links due to keybinding conflict with
  22635. ;; Flyspell.
  22636. ((code entity export-snippet inline-babel-call
  22637. inline-src-block line-break latex-fragment link macro
  22638. statistics-cookie target timestamp verbatim)
  22639. nil)
  22640. (footnote-reference
  22641. ;; Only in inline footnotes, within the definition.
  22642. (and (eq (org-element-property :type object) 'inline)
  22643. (< (save-excursion
  22644. (goto-char (org-element-property :begin object))
  22645. (search-forward ":" nil t 2))
  22646. (point))))
  22647. (otherwise t))))
  22648. (defun org-mode-flyspell-verify ()
  22649. "Function used for `flyspell-generic-check-word-predicate'."
  22650. (if (org-at-heading-p)
  22651. ;; At a headline or an inlinetask, check title only. This is
  22652. ;; faster than relying on `org-element-at-point'.
  22653. (and (save-excursion (beginning-of-line)
  22654. (and (let ((case-fold-search t))
  22655. (not (looking-at-p "\\*+ END[ \t]*$")))
  22656. (let ((case-fold-search nil))
  22657. (looking-at org-complex-heading-regexp))))
  22658. (match-beginning 4)
  22659. (>= (point) (match-beginning 4))
  22660. (or (not (match-beginning 5))
  22661. (< (point) (match-beginning 5))))
  22662. (let* ((element (org-element-at-point))
  22663. (post-affiliated (org-element-property :post-affiliated element)))
  22664. (cond
  22665. ;; Ignore checks in all affiliated keywords but captions.
  22666. ((< (point) post-affiliated)
  22667. (and (save-excursion
  22668. (beginning-of-line)
  22669. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22670. (> (point) (match-end 0))
  22671. (org--flyspell-object-check-p element)))
  22672. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22673. ((let ((log (org-log-into-drawer)))
  22674. (and log
  22675. (let ((drawer (org-element-lineage element '(drawer))))
  22676. (and drawer
  22677. (eq (compare-strings
  22678. log nil nil
  22679. (org-element-property :drawer-name drawer) nil nil t)
  22680. t)))))
  22681. nil)
  22682. (t
  22683. (cl-case (org-element-type element)
  22684. ((comment quote-section) t)
  22685. (comment-block
  22686. ;; Allow checks between block markers, not on them.
  22687. (and (> (line-beginning-position) post-affiliated)
  22688. (save-excursion
  22689. (end-of-line)
  22690. (skip-chars-forward " \r\t\n")
  22691. (< (point) (org-element-property :end element)))))
  22692. ;; Arbitrary list of keywords where checks are meaningful.
  22693. ;; Make sure point is on the value part of the element.
  22694. (keyword
  22695. (and (member (org-element-property :key element)
  22696. '("DESCRIPTION" "TITLE"))
  22697. (save-excursion
  22698. (search-backward ":" (line-beginning-position) t))))
  22699. ;; Check is globally allowed in paragraphs verse blocks and
  22700. ;; table rows (after affiliated keywords) but some objects
  22701. ;; must not be affected.
  22702. ((paragraph table-row verse-block)
  22703. (let ((cbeg (org-element-property :contents-begin element))
  22704. (cend (org-element-property :contents-end element)))
  22705. (and cbeg (>= (point) cbeg) (< (point) cend)
  22706. (org--flyspell-object-check-p element))))))))))
  22707. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22708. (defun org-remove-flyspell-overlays-in (beg end)
  22709. "Remove flyspell overlays in region."
  22710. (and (bound-and-true-p flyspell-mode)
  22711. (fboundp 'flyspell-delete-region-overlays)
  22712. (flyspell-delete-region-overlays beg end)))
  22713. (defvar flyspell-delayed-commands)
  22714. (eval-after-load "flyspell"
  22715. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22716. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22717. (eval-after-load "bookmark"
  22718. '(if (boundp 'bookmark-after-jump-hook)
  22719. ;; We can use the hook
  22720. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22721. ;; Hook not available, use advice
  22722. (defadvice bookmark-jump (after org-make-visible activate)
  22723. "Make the position visible."
  22724. (org-bookmark-jump-unhide))))
  22725. ;; Make sure saveplace shows the location if it was hidden
  22726. (eval-after-load "saveplace"
  22727. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22728. "Make the position visible."
  22729. (org-bookmark-jump-unhide)))
  22730. ;; Make sure ecb shows the location if it was hidden
  22731. (eval-after-load "ecb"
  22732. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22733. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22734. (when (derived-mode-p 'org-mode)
  22735. (org-show-context))))
  22736. (defun org-bookmark-jump-unhide ()
  22737. "Unhide the current position, to show the bookmark location."
  22738. (and (derived-mode-p 'org-mode)
  22739. (or (org-invisible-p)
  22740. (save-excursion (goto-char (max (point-min) (1- (point))))
  22741. (org-invisible-p)))
  22742. (org-show-context 'bookmark-jump)))
  22743. (defun org-mark-jump-unhide ()
  22744. "Make the point visible with `org-show-context' after jumping to the mark."
  22745. (when (and (derived-mode-p 'org-mode)
  22746. (org-invisible-p))
  22747. (org-show-context 'mark-goto)))
  22748. (eval-after-load "simple"
  22749. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22750. "Make the point visible with `org-show-context'."
  22751. (org-mark-jump-unhide)))
  22752. (eval-after-load "simple"
  22753. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22754. "Make the point visible with `org-show-context'."
  22755. (org-mark-jump-unhide)))
  22756. (eval-after-load "simple"
  22757. '(defadvice pop-global-mark (after org-make-visible activate)
  22758. "Make the point visible with `org-show-context'."
  22759. (org-mark-jump-unhide)))
  22760. ;; Make session.el ignore our circular variable
  22761. (defvar session-globals-exclude)
  22762. (eval-after-load "session"
  22763. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22764. ;;;; Finish up
  22765. (provide 'org)
  22766. (run-hooks 'org-load-hook)
  22767. ;;; org.el ends here