org.el 941 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888
  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-2017 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org 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) ;Silence byte-compiler
  171. (defsubst org-uniquify (list)
  172. "Non-destructively remove duplicate elements from LIST."
  173. (let ((res (copy-sequence list))) (delete-dups res)))
  174. (defsubst org-get-at-bol (property)
  175. "Get text property PROPERTY at the beginning of line."
  176. (get-text-property (point-at-bol) property))
  177. (defsubst org-trim (s &optional keep-lead)
  178. "Remove whitespace at the beginning and the end of string S.
  179. When optional argument KEEP-LEAD is non-nil, removing blank lines
  180. at the beginning of the string does not affect leading indentation."
  181. (replace-regexp-in-string
  182. (if keep-lead "\\`\\([ \t]*\n\\)+" "\\`[ \t\n\r]+") ""
  183. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  184. ;; load languages based on value of `org-babel-load-languages'
  185. (defvar org-babel-load-languages)
  186. ;;;###autoload
  187. (defun org-babel-do-load-languages (sym value)
  188. "Load the languages defined in `org-babel-load-languages'."
  189. (set-default sym value)
  190. (dolist (pair org-babel-load-languages)
  191. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  192. (if active
  193. (require (intern (concat "ob-" lang)))
  194. (funcall 'fmakunbound
  195. (intern (concat "org-babel-execute:" lang)))
  196. (funcall 'fmakunbound
  197. (intern (concat "org-babel-expand-body:" lang)))))))
  198. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  199. ;;;###autoload
  200. (defun org-babel-load-file (file &optional compile)
  201. "Load Emacs Lisp source code blocks in the Org FILE.
  202. This function exports the source code using `org-babel-tangle'
  203. and then loads the resulting file using `load-file'. With prefix
  204. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  205. file to byte-code before it is loaded."
  206. (interactive "fFile to load: \nP")
  207. (let* ((age (lambda (file)
  208. (float-time
  209. (time-subtract (current-time)
  210. (nth 5 (or (file-attributes (file-truename file))
  211. (file-attributes file)))))))
  212. (base-name (file-name-sans-extension file))
  213. (exported-file (concat base-name ".el")))
  214. ;; tangle if the Org file is newer than the elisp file
  215. (unless (and (file-exists-p exported-file)
  216. (> (funcall age file) (funcall age exported-file)))
  217. ;; Tangle-file traversal returns reversed list of tangled files
  218. ;; and we want to evaluate the first target.
  219. (setq exported-file
  220. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  221. (message "%s %s"
  222. (if compile
  223. (progn (byte-compile-file exported-file 'load)
  224. "Compiled and loaded")
  225. (progn (load-file exported-file) "Loaded"))
  226. exported-file)))
  227. (defcustom org-babel-load-languages '((emacs-lisp . t))
  228. "Languages which can be evaluated in Org buffers.
  229. This list can be used to load support for any of the languages
  230. below, note that each language will depend on a different set of
  231. system executables and/or Emacs modes. When a language is
  232. \"loaded\", then code blocks in that language can be evaluated
  233. with `org-babel-execute-src-block' bound by default to C-c
  234. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  235. be set to remove code block evaluation from the C-c C-c
  236. keybinding. By default only Emacs Lisp (which has no
  237. requirements) is loaded."
  238. :group 'org-babel
  239. :set 'org-babel-do-load-languages
  240. :version "24.1"
  241. :type '(alist :tag "Babel Languages"
  242. :key-type
  243. (choice
  244. (const :tag "Awk" awk)
  245. (const :tag "C" C)
  246. (const :tag "R" R)
  247. (const :tag "Asymptote" asymptote)
  248. (const :tag "Calc" calc)
  249. (const :tag "Clojure" clojure)
  250. (const :tag "CSS" css)
  251. (const :tag "Ditaa" ditaa)
  252. (const :tag "Dot" dot)
  253. (const :tag "Ebnf2ps" ebnf2ps)
  254. (const :tag "Emacs Lisp" emacs-lisp)
  255. (const :tag "Forth" forth)
  256. (const :tag "Fortran" fortran)
  257. (const :tag "Gnuplot" gnuplot)
  258. (const :tag "Haskell" haskell)
  259. (const :tag "hledger" hledger)
  260. (const :tag "IO" io)
  261. (const :tag "J" J)
  262. (const :tag "Java" java)
  263. (const :tag "Javascript" js)
  264. (const :tag "LaTeX" latex)
  265. (const :tag "Ledger" ledger)
  266. (const :tag "Lilypond" lilypond)
  267. (const :tag "Lisp" lisp)
  268. (const :tag "Makefile" makefile)
  269. (const :tag "Maxima" maxima)
  270. (const :tag "Matlab" matlab)
  271. (const :tag "Mscgen" mscgen)
  272. (const :tag "Ocaml" ocaml)
  273. (const :tag "Octave" octave)
  274. (const :tag "Org" org)
  275. (const :tag "Perl" perl)
  276. (const :tag "Pico Lisp" picolisp)
  277. (const :tag "PlantUML" plantuml)
  278. (const :tag "Python" python)
  279. (const :tag "Ruby" ruby)
  280. (const :tag "Sass" sass)
  281. (const :tag "Scala" scala)
  282. (const :tag "Scheme" scheme)
  283. (const :tag "Screen" screen)
  284. (const :tag "Shell Script" shell)
  285. (const :tag "Shen" shen)
  286. (const :tag "Sql" sql)
  287. (const :tag "Sqlite" sqlite)
  288. (const :tag "Stan" stan)
  289. (const :tag "Vala" vala))
  290. :value-type (boolean :tag "Activate" :value t)))
  291. ;;;; Customization variables
  292. (defcustom org-clone-delete-id nil
  293. "Remove ID property of clones of a subtree.
  294. When non-nil, clones of a subtree don't inherit the ID property.
  295. Otherwise they inherit the ID property with a new unique
  296. identifier."
  297. :type 'boolean
  298. :version "24.1"
  299. :group 'org-id)
  300. ;;; Version
  301. (org-check-version)
  302. ;;;###autoload
  303. (defun org-version (&optional here full message)
  304. "Show the Org version.
  305. Interactively, or when MESSAGE is non-nil, show it in echo area.
  306. With prefix argument, or when HERE is non-nil, insert it at point.
  307. In non-interactive uses, a reduced version string is output unless
  308. FULL is given."
  309. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  310. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  311. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  312. (load-suffixes (list ".el"))
  313. (org-install-dir
  314. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  315. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  316. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  317. (let* ((load-suffixes save-load-suffixes)
  318. (release (org-release))
  319. (git-version (org-git-version))
  320. (version (format "Org mode version %s (%s @ %s)"
  321. release
  322. git-version
  323. (if org-install-dir
  324. (if (string= org-dir org-install-dir)
  325. org-install-dir
  326. (concat "mixed installation! "
  327. org-install-dir
  328. " and "
  329. org-dir))
  330. "org-loaddefs.el can not be found!")))
  331. (version1 (if full version release)))
  332. (when here (insert version1))
  333. (when message (message "%s" version1))
  334. version1)))
  335. (defconst org-version (org-version))
  336. ;;; Syntax Constants
  337. ;;;; Block
  338. (defconst org-block-regexp
  339. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  340. "Regular expression for hiding blocks.")
  341. (defconst org-dblock-start-re
  342. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  343. "Matches the start line of a dynamic block, with parameters.")
  344. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  345. "Matches the end of a dynamic block.")
  346. ;;;; Clock and Planning
  347. (defconst org-clock-string "CLOCK:"
  348. "String used as prefix for timestamps clocking work hours on an item.")
  349. (defvar org-closed-string "CLOSED:"
  350. "String used as the prefix for timestamps logging closing a TODO entry.")
  351. (defvar org-deadline-string "DEADLINE:"
  352. "String to mark deadline entries.
  353. \\<org-mode-map>
  354. A deadline is this string, followed by a time stamp. It must be
  355. a word, terminated by a colon. You can insert a schedule keyword
  356. and a timestamp with `\\[org-deadline]'.")
  357. (defvar org-scheduled-string "SCHEDULED:"
  358. "String to mark scheduled TODO entries.
  359. \\<org-mode-map>
  360. A schedule is this string, followed by a time stamp. It must be
  361. a word, terminated by a colon. You can insert a schedule keyword
  362. and a timestamp with `\\[org-schedule]'.")
  363. (defconst org-ds-keyword-length
  364. (+ 2
  365. (apply #'max
  366. (mapcar #'length
  367. (list org-deadline-string org-scheduled-string
  368. org-clock-string org-closed-string))))
  369. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  370. (defconst org-planning-line-re
  371. (concat "^[ \t]*"
  372. (regexp-opt
  373. (list org-closed-string org-deadline-string org-scheduled-string)
  374. t))
  375. "Matches a line with planning info.
  376. Matched keyword is in group 1.")
  377. (defconst org-clock-line-re
  378. (concat "^[ \t]*" org-clock-string)
  379. "Matches a line with clock info.")
  380. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  381. "Matches the DEADLINE keyword.")
  382. (defconst org-deadline-time-regexp
  383. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  384. "Matches the DEADLINE keyword together with a time stamp.")
  385. (defconst org-deadline-time-hour-regexp
  386. (concat "\\<" org-deadline-string
  387. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  388. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  389. (defconst org-deadline-line-regexp
  390. (concat "\\<\\(" org-deadline-string "\\).*")
  391. "Matches the DEADLINE keyword and the rest of the line.")
  392. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  393. "Matches the SCHEDULED keyword.")
  394. (defconst org-scheduled-time-regexp
  395. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  396. "Matches the SCHEDULED keyword together with a time stamp.")
  397. (defconst org-scheduled-time-hour-regexp
  398. (concat "\\<" org-scheduled-string
  399. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  400. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  401. (defconst org-closed-time-regexp
  402. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  403. "Matches the CLOSED keyword together with a time stamp.")
  404. (defconst org-keyword-time-regexp
  405. (concat "\\<"
  406. (regexp-opt
  407. (list org-scheduled-string org-deadline-string org-closed-string
  408. org-clock-string)
  409. t)
  410. " *[[<]\\([^]>]+\\)[]>]")
  411. "Matches any of the 4 keywords, together with the time stamp.")
  412. (defconst org-keyword-time-not-clock-regexp
  413. (concat
  414. "\\<"
  415. (regexp-opt
  416. (list org-scheduled-string org-deadline-string org-closed-string) t)
  417. " *[[<]\\([^]>]+\\)[]>]")
  418. "Matches any of the 3 keywords, together with the time stamp.")
  419. (defconst org-maybe-keyword-time-regexp
  420. (concat "\\(\\<"
  421. (regexp-opt
  422. (list org-scheduled-string org-deadline-string org-closed-string
  423. org-clock-string)
  424. t)
  425. "\\)?"
  426. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  427. "\\|"
  428. "<%%([^\r\n>]*>\\)")
  429. "Matches a timestamp, possibly preceded by a keyword.")
  430. (defconst org-all-time-keywords
  431. (mapcar (lambda (w) (substring w 0 -1))
  432. (list org-scheduled-string org-deadline-string
  433. org-clock-string org-closed-string))
  434. "List of time keywords.")
  435. ;;;; Drawer
  436. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  437. "Matches first or last line of a hidden block.
  438. Group 1 contains drawer's name or \"END\".")
  439. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  440. "Regular expression matching the first line of a property drawer.")
  441. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  442. "Regular expression matching the last line of a property drawer.")
  443. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  444. "Regular expression matching the first line of a clock drawer.")
  445. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  446. "Regular expression matching the last line of a clock drawer.")
  447. (defconst org-property-drawer-re
  448. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  449. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  450. "[ \t]*:END:[ \t]*$")
  451. "Matches an entire property drawer.")
  452. (defconst org-clock-drawer-re
  453. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  454. org-clock-drawer-end-re "\\)\n?")
  455. "Matches an entire clock drawer.")
  456. ;;;; Headline
  457. (defconst org-heading-keyword-regexp-format
  458. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  459. "Printf format for a regexp matching a headline with some keyword.
  460. This regexp will match the headline of any node which has the
  461. exact keyword that is put into the format. The keyword isn't in
  462. any group by default, but the stars and the body are.")
  463. (defconst org-heading-keyword-maybe-regexp-format
  464. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  465. "Printf format for a regexp matching a headline, possibly with some keyword.
  466. This regexp can match any headline with the specified keyword, or
  467. without a keyword. The keyword isn't in any group by default,
  468. but the stars and the body are.")
  469. (defconst org-archive-tag "ARCHIVE"
  470. "The tag that marks a subtree as archived.
  471. An archived subtree does not open during visibility cycling, and does
  472. not contribute to the agenda listings.")
  473. (eval-and-compile
  474. (defconst org-comment-string "COMMENT"
  475. "Entries starting with this keyword will never be exported.
  476. \\<org-mode-map>
  477. An entry can be toggled between COMMENT and normal with
  478. `\\[org-toggle-comment]'."))
  479. ;;;; LaTeX Environments and Fragments
  480. (defconst org-latex-regexps
  481. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  482. ;; ("$" "\\([ \t(]\\|^\\)\\(\\(\\([$]\\)\\([^ \t\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \t\n,.$]\\)\\4\\)\\)\\([ \t.,?;:'\")]\\|$\\)" 2 nil)
  483. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  484. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \t\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
  485. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \t\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \t\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|'\\|$\\)" 2 nil)
  486. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  487. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  488. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  489. "Regular expressions for matching embedded LaTeX.")
  490. ;;;; Node Property
  491. (defconst org-effort-property "Effort"
  492. "The property that is being used to keep track of effort estimates.
  493. Effort estimates given in this property need to have the format H:MM.")
  494. ;;;; Table
  495. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  496. "Detect an org-type or table-type table.")
  497. (defconst org-table-line-regexp "^[ \t]*|"
  498. "Detect an org-type table line.")
  499. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  500. "Detect an org-type table line.")
  501. (defconst org-table-hline-regexp "^[ \t]*|-"
  502. "Detect an org-type table hline.")
  503. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  504. "Detect a table-type table hline.")
  505. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  506. "Detect the first line outside a table when searching from within it.
  507. This works for both table types.")
  508. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  509. "Detect a #+TBLFM line.")
  510. ;;;; Timestamp
  511. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  512. "Regular expression for fast time stamp matching.")
  513. (defconst org-ts-regexp-inactive
  514. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  515. "Regular expression for fast inactive time stamp matching.")
  516. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  517. "Regular expression for fast time stamp matching.")
  518. (defconst org-ts-regexp0
  519. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  520. "Regular expression matching time strings for analysis.
  521. This one does not require the space after the date, so it can be used
  522. on a string that terminates immediately after the date.")
  523. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  524. "Regular expression matching time strings for analysis.")
  525. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  526. "Regular expression matching time stamps, with groups.")
  527. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  528. "Regular expression matching time stamps (also [..]), with groups.")
  529. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  530. "Regular expression matching a time stamp range.")
  531. (defconst org-tr-regexp-both
  532. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  533. "Regular expression matching a time stamp range.")
  534. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  535. org-ts-regexp "\\)?")
  536. "Regular expression matching a time stamp or time stamp range.")
  537. (defconst org-tsr-regexp-both
  538. (concat org-ts-regexp-both "\\(--?-?"
  539. org-ts-regexp-both "\\)?")
  540. "Regular expression matching a time stamp or time stamp range.
  541. The time stamps may be either active or inactive.")
  542. (defconst org-repeat-re
  543. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  544. "Regular expression for specifying repeated events.
  545. After a match, group 1 contains the repeat expression.")
  546. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  547. "Formats for `format-time-string' which are used for time stamps.")
  548. ;;; The custom variables
  549. (defgroup org nil
  550. "Outline-based notes management and organizer."
  551. :tag "Org"
  552. :group 'outlines
  553. :group 'calendar)
  554. (defcustom org-mode-hook nil
  555. "Mode hook for Org mode, run after the mode was turned on."
  556. :group 'org
  557. :type 'hook)
  558. (defcustom org-load-hook nil
  559. "Hook that is run after org.el has been loaded."
  560. :group 'org
  561. :type 'hook)
  562. (defcustom org-log-buffer-setup-hook nil
  563. "Hook that is run after an Org log buffer is created."
  564. :group 'org
  565. :version "24.1"
  566. :type 'hook)
  567. (defvar org-modules) ; defined below
  568. (defvar org-modules-loaded nil
  569. "Have the modules been loaded already?")
  570. (defun org-load-modules-maybe (&optional force)
  571. "Load all extensions listed in `org-modules'."
  572. (when (or force (not org-modules-loaded))
  573. (dolist (ext org-modules)
  574. (condition-case nil (require ext)
  575. (error (message "Problems while trying to load feature `%s'" ext))))
  576. (setq org-modules-loaded t)))
  577. (defun org-set-modules (var value)
  578. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  579. (set var value)
  580. (when (featurep 'org)
  581. (org-load-modules-maybe 'force)
  582. (org-element-cache-reset 'all)))
  583. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  584. "Modules that should always be loaded together with org.el.
  585. If a description starts with <C>, the file is not part of Emacs
  586. and loading it will require that you have downloaded and properly
  587. installed the Org mode distribution.
  588. You can also use this system to load external packages (i.e. neither Org
  589. core modules, nor modules from the CONTRIB directory). Just add symbols
  590. to the end of the list. If the package is called org-xyz.el, then you need
  591. to add the symbol `xyz', and the package must have a call to:
  592. (provide \\='org-xyz)
  593. For export specific modules, see also `org-export-backends'."
  594. :group 'org
  595. :set 'org-set-modules
  596. :version "24.4"
  597. :package-version '(Org . "8.0")
  598. :type
  599. '(set :greedy t
  600. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  601. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  602. (const :tag " crypt: Encryption of subtrees" org-crypt)
  603. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  604. (const :tag " docview: Links to doc-view buffers" org-docview)
  605. (const :tag " eww: Store link to url of eww" org-eww)
  606. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  607. (const :tag " habit: Track your consistency with habits" org-habit)
  608. (const :tag " id: Global IDs for identifying entries" org-id)
  609. (const :tag " info: Links to Info nodes" org-info)
  610. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  611. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  612. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  613. (const :tag " mouse: Additional mouse support" org-mouse)
  614. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  615. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  616. (const :tag " w3m: Special cut/paste from w3m to Org mode." org-w3m)
  617. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  618. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  619. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  620. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  621. (const :tag "C collector: Collect properties into tables" org-collector)
  622. (const :tag "C depend: TODO dependencies for Org mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  623. (const :tag "C drill: Flashcards and spaced repetition for Org mode" org-drill)
  624. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  625. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  626. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  627. (const :tag "C eval: Include command output as text" org-eval)
  628. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  629. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  630. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  631. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  632. (const :tag "C invoice: Help manage client invoices in Org mode" org-invoice)
  633. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  634. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  635. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  636. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  637. (const :tag "C man: Support for links to manpages in Org mode" org-man)
  638. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  639. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  640. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  641. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  642. (const :tag "C registry: A registry for Org links" org-registry)
  643. (const :tag "C screen: Visit screen sessions through Org links" org-screen)
  644. (const :tag "C secretary: Team management with org-mode" org-secretary)
  645. (const :tag "C sqlinsert: Convert Org tables to SQL insertions" orgtbl-sqlinsert)
  646. (const :tag "C toc: Table of contents for Org buffer" org-toc)
  647. (const :tag "C track: Keep up with Org mode development" org-track)
  648. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  649. (const :tag "C vm: Links to VM folders/messages" org-vm)
  650. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  651. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  652. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  653. (defvar org-export-registered-backends) ; From ox.el.
  654. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  655. (declare-function org-export-backend-name "ox" (backend) t)
  656. (defcustom org-export-backends '(ascii html icalendar latex odt)
  657. "List of export back-ends that should be always available.
  658. If a description starts with <C>, the file is not part of Emacs
  659. and loading it will require that you have downloaded and properly
  660. installed the Org mode distribution.
  661. Unlike to `org-modules', libraries in this list will not be
  662. loaded along with Org, but only once the export framework is
  663. needed.
  664. This variable needs to be set before org.el is loaded. If you
  665. need to make a change while Emacs is running, use the customize
  666. interface or run the following code, where VAL stands for the new
  667. value of the variable, after updating it:
  668. (progn
  669. (setq org-export-registered-backends
  670. (cl-remove-if-not
  671. (lambda (backend)
  672. (let ((name (org-export-backend-name backend)))
  673. (or (memq name val)
  674. (catch \\='parentp
  675. (dolist (b val)
  676. (and (org-export-derived-backend-p b name)
  677. (throw \\='parentp t)))))))
  678. org-export-registered-backends))
  679. (let ((new-list (mapcar #\\='org-export-backend-name
  680. org-export-registered-backends)))
  681. (dolist (backend val)
  682. (cond
  683. ((not (load (format \"ox-%s\" backend) t t))
  684. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  685. backend))
  686. ((not (memq backend new-list)) (push backend new-list))))
  687. (set-default \\='org-export-backends new-list)))
  688. Adding a back-end to this list will also pull the back-end it
  689. depends on, if any."
  690. :group 'org
  691. :group 'org-export
  692. :version "26.1"
  693. :package-version '(Org . "9.0")
  694. :initialize 'custom-initialize-set
  695. :set (lambda (var val)
  696. (if (not (featurep 'ox)) (set-default var val)
  697. ;; Any back-end not required anymore (not present in VAL and not
  698. ;; a parent of any back-end in the new value) is removed from the
  699. ;; list of registered back-ends.
  700. (setq org-export-registered-backends
  701. (cl-remove-if-not
  702. (lambda (backend)
  703. (let ((name (org-export-backend-name backend)))
  704. (or (memq name val)
  705. (catch 'parentp
  706. (dolist (b val)
  707. (and (org-export-derived-backend-p b name)
  708. (throw 'parentp t)))))))
  709. org-export-registered-backends))
  710. ;; Now build NEW-LIST of both new back-ends and required
  711. ;; parents.
  712. (let ((new-list (mapcar #'org-export-backend-name
  713. org-export-registered-backends)))
  714. (dolist (backend val)
  715. (cond
  716. ((not (load (format "ox-%s" backend) t t))
  717. (message "Problems while trying to load export back-end `%s'"
  718. backend))
  719. ((not (memq backend new-list)) (push backend new-list))))
  720. ;; Set VAR to that list with fixed dependencies.
  721. (set-default var new-list))))
  722. :type '(set :greedy t
  723. (const :tag " ascii Export buffer to ASCII format" ascii)
  724. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  725. (const :tag " html Export buffer to HTML format" html)
  726. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  727. (const :tag " latex Export buffer to LaTeX format" latex)
  728. (const :tag " man Export buffer to MAN format" man)
  729. (const :tag " md Export buffer to Markdown format" md)
  730. (const :tag " odt Export buffer to ODT format" odt)
  731. (const :tag " org Export buffer to Org format" org)
  732. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  733. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  734. (const :tag "C deck Export buffer to deck.js presentations" deck)
  735. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  736. (const :tag "C groff Export buffer to Groff format" groff)
  737. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  738. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  739. (const :tag "C s5 Export buffer to s5 presentations" s5)
  740. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  741. (eval-after-load 'ox
  742. '(dolist (backend org-export-backends)
  743. (condition-case nil (require (intern (format "ox-%s" backend)))
  744. (error (message "Problems while trying to load export back-end `%s'"
  745. backend)))))
  746. (defcustom org-support-shift-select nil
  747. "Non-nil means make shift-cursor commands select text when possible.
  748. \\<org-mode-map>\
  749. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  750. start selecting a region, or enlarge regions started in this way.
  751. In Org mode, in special contexts, these same keys are used for
  752. other purposes, important enough to compete with shift selection.
  753. Org tries to balance these needs by supporting `shift-select-mode'
  754. outside these special contexts, under control of this variable.
  755. The default of this variable is nil, to avoid confusing behavior. Shifted
  756. cursor keys will then execute Org commands in the following contexts:
  757. - on a headline, changing TODO state (left/right) and priority (up/down)
  758. - on a time stamp, changing the time
  759. - in a plain list item, changing the bullet type
  760. - in a property definition line, switching between allowed values
  761. - in the BEGIN line of a clock table (changing the time block).
  762. Outside these contexts, the commands will throw an error.
  763. When this variable is t and the cursor is not in a special
  764. context, Org mode will support shift-selection for making and
  765. enlarging regions. To make this more effective, the bullet
  766. cycling will no longer happen anywhere in an item line, but only
  767. if the cursor is exactly on the bullet.
  768. If you set this variable to the symbol `always', then the keys
  769. will not be special in headlines, property lines, and item lines,
  770. to make shift selection work there as well. If this is what you
  771. want, you can use the following alternative commands:
  772. `\\[org-todo]' and `\\[org-priority]' \
  773. to change TODO state and priority,
  774. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  775. can be used to switch TODO sets,
  776. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  777. and properties can be edited by hand or in column view.
  778. However, when the cursor is on a timestamp, shift-cursor commands
  779. will still edit the time stamp - this is just too good to give up."
  780. :group 'org
  781. :type '(choice
  782. (const :tag "Never" nil)
  783. (const :tag "When outside special context" t)
  784. (const :tag "Everywhere except timestamps" always)))
  785. (defcustom org-loop-over-headlines-in-active-region nil
  786. "Shall some commands act upon headlines in the active region?
  787. When set to t, some commands will be performed in all headlines
  788. within the active region.
  789. When set to `start-level', some commands will be performed in all
  790. headlines within the active region, provided that these headlines
  791. are of the same level than the first one.
  792. When set to a string, those commands will be performed on the
  793. matching headlines within the active region. Such string must be
  794. a tags/property/todo match as it is used in the agenda tags view.
  795. The list of commands is: `org-schedule', `org-deadline',
  796. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  797. `org-archive-to-archive-sibling'. The archiving commands skip
  798. already archived entries."
  799. :type '(choice (const :tag "Don't loop" nil)
  800. (const :tag "All headlines in active region" t)
  801. (const :tag "In active region, headlines at the same level than the first one" start-level)
  802. (string :tag "Tags/Property/Todo matcher"))
  803. :version "24.1"
  804. :group 'org-todo
  805. :group 'org-archive)
  806. (defgroup org-startup nil
  807. "Options concerning startup of Org mode."
  808. :tag "Org Startup"
  809. :group 'org)
  810. (defcustom org-startup-folded t
  811. "Non-nil means entering Org mode will switch to OVERVIEW.
  812. This can also be configured on a per-file basis by adding one of
  813. the following lines anywhere in the buffer:
  814. #+STARTUP: fold (or `overview', this is equivalent)
  815. #+STARTUP: nofold (or `showall', this is equivalent)
  816. #+STARTUP: content
  817. #+STARTUP: showeverything
  818. Set `org-agenda-inhibit-startup' to a non-nil value if you want
  819. to ignore this option when Org opens agenda files for the first
  820. time."
  821. :group 'org-startup
  822. :type '(choice
  823. (const :tag "nofold: show all" nil)
  824. (const :tag "fold: overview" t)
  825. (const :tag "content: all headlines" content)
  826. (const :tag "show everything, even drawers" showeverything)))
  827. (defcustom org-startup-truncated t
  828. "Non-nil means entering Org mode will set `truncate-lines'.
  829. This is useful since some lines containing links can be very long and
  830. uninteresting. Also tables look terrible when wrapped.
  831. The variable `org-startup-truncated' allows to configure
  832. truncation for Org mode different to the other modes that use the
  833. variable `truncate-lines' and as a shortcut instead of putting
  834. the variable `truncate-lines' into the `org-mode-hook'. If one
  835. wants to configure truncation for Org mode not statically but
  836. dynamically e. g. in a hook like `ediff-prepare-buffer-hook' then
  837. the variable `truncate-lines' has to be used because in such a
  838. case it is too late to set the variable `org-startup-truncated'."
  839. :group 'org-startup
  840. :type 'boolean)
  841. (defcustom org-startup-indented nil
  842. "Non-nil means turn on `org-indent-mode' on startup.
  843. This can also be configured on a per-file basis by adding one of
  844. the following lines anywhere in the buffer:
  845. #+STARTUP: indent
  846. #+STARTUP: noindent"
  847. :group 'org-structure
  848. :type '(choice
  849. (const :tag "Not" nil)
  850. (const :tag "Globally (slow on startup in large files)" t)))
  851. (defcustom org-use-sub-superscripts t
  852. "Non-nil means interpret \"_\" and \"^\" for display.
  853. If you want to control how Org exports those characters, see
  854. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  855. used to be an alias for `org-export-with-sub-superscripts' in
  856. Org <8.0, it is not anymore.
  857. When this option is turned on, you can use TeX-like syntax for
  858. sub- and superscripts within the buffer. Several characters after
  859. \"_\" or \"^\" will be considered as a single item - so grouping
  860. with {} is normally not needed. For example, the following things
  861. will be parsed as single sub- or superscripts:
  862. 10^24 or 10^tau several digits will be considered 1 item.
  863. 10^-12 or 10^-tau a leading sign with digits or a word
  864. x^2-y^3 will be read as x^2 - y^3, because items are
  865. terminated by almost any nonword/nondigit char.
  866. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  867. Still, ambiguity is possible. So when in doubt, use {} to enclose
  868. the sub/superscript. If you set this variable to the symbol `{}',
  869. the braces are *required* in order to trigger interpretations as
  870. sub/superscript. This can be helpful in documents that need \"_\"
  871. frequently in plain text."
  872. :group 'org-startup
  873. :version "24.4"
  874. :package-version '(Org . "8.0")
  875. :type '(choice
  876. (const :tag "Always interpret" t)
  877. (const :tag "Only with braces" {})
  878. (const :tag "Never interpret" nil)))
  879. (defcustom org-startup-with-beamer-mode nil
  880. "Non-nil means turn on `org-beamer-mode' on startup.
  881. This can also be configured on a per-file basis by adding one of
  882. the following lines anywhere in the buffer:
  883. #+STARTUP: beamer"
  884. :group 'org-startup
  885. :version "24.1"
  886. :type 'boolean)
  887. (defcustom org-startup-align-all-tables nil
  888. "Non-nil means align all tables when visiting a file.
  889. This is useful when the column width in tables is forced with <N> cookies
  890. in table fields. Such tables will look correct only after the first re-align.
  891. This can also be configured on a per-file basis by adding one of
  892. the following lines anywhere in the buffer:
  893. #+STARTUP: align
  894. #+STARTUP: noalign"
  895. :group 'org-startup
  896. :type 'boolean)
  897. (defcustom org-startup-with-inline-images nil
  898. "Non-nil means show inline images when loading a new Org file.
  899. This can also be configured on a per-file basis by adding one of
  900. the following lines anywhere in the buffer:
  901. #+STARTUP: inlineimages
  902. #+STARTUP: noinlineimages"
  903. :group 'org-startup
  904. :version "24.1"
  905. :type 'boolean)
  906. (defcustom org-startup-with-latex-preview nil
  907. "Non-nil means preview LaTeX fragments when loading a new Org file.
  908. This can also be configured on a per-file basis by adding one of
  909. the following lines anywhere in the buffer:
  910. #+STARTUP: latexpreview
  911. #+STARTUP: nolatexpreview"
  912. :group 'org-startup
  913. :version "24.4"
  914. :package-version '(Org . "8.0")
  915. :type 'boolean)
  916. (defcustom org-insert-mode-line-in-empty-file nil
  917. "Non-nil means insert the first line setting Org mode in empty files.
  918. When the function `org-mode' is called interactively in an empty file, this
  919. normally means that the file name does not automatically trigger Org mode.
  920. To ensure that the file will always be in Org mode in the future, a
  921. line enforcing Org mode will be inserted into the buffer, if this option
  922. has been set."
  923. :group 'org-startup
  924. :type 'boolean)
  925. (defcustom org-replace-disputed-keys nil
  926. "Non-nil means use alternative key bindings for some keys.
  927. Org mode uses S-<cursor> keys for changing timestamps and priorities.
  928. These keys are also used by other packages like shift-selection-mode'
  929. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  930. If you want to use Org mode together with one of these other modes,
  931. or more generally if you would like to move some Org mode commands to
  932. other keys, set this variable and configure the keys with the variable
  933. `org-disputed-keys'.
  934. This option is only relevant at load-time of Org mode, and must be set
  935. *before* org.el is loaded. Changing it requires a restart of Emacs to
  936. become effective."
  937. :group 'org-startup
  938. :type 'boolean)
  939. (defcustom org-use-extra-keys nil
  940. "Non-nil means use extra key sequence definitions for certain commands.
  941. This happens automatically if `window-system' is nil. This
  942. variable lets you do the same manually. You must set it before
  943. loading Org."
  944. :group 'org-startup
  945. :type 'boolean)
  946. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  947. (defcustom org-disputed-keys
  948. '(([(shift up)] . [(meta p)])
  949. ([(shift down)] . [(meta n)])
  950. ([(shift left)] . [(meta -)])
  951. ([(shift right)] . [(meta +)])
  952. ([(control shift right)] . [(meta shift +)])
  953. ([(control shift left)] . [(meta shift -)]))
  954. "Keys for which Org mode and other modes compete.
  955. This is an alist, cars are the default keys, second element specifies
  956. the alternative to use when `org-replace-disputed-keys' is t.
  957. Keys can be specified in any syntax supported by `define-key'.
  958. The value of this option takes effect only at Org mode startup,
  959. therefore you'll have to restart Emacs to apply it after changing."
  960. :group 'org-startup
  961. :type 'alist)
  962. (defun org-key (key)
  963. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  964. Or return the original if not disputed."
  965. (when org-replace-disputed-keys
  966. (let* ((nkey (key-description key))
  967. (x (cl-find-if (lambda (x) (equal (key-description (car x)) nkey))
  968. org-disputed-keys)))
  969. (setq key (if x (cdr x) key))))
  970. key)
  971. (defun org-defkey (keymap key def)
  972. "Define a key, possibly translated, as returned by `org-key'."
  973. (define-key keymap (org-key key) def))
  974. (defcustom org-ellipsis nil
  975. "The ellipsis to use in the Org mode outline.
  976. When nil, just use the standard three dots. When a non-empty string,
  977. use that string instead.
  978. The change affects only Org mode (which will then use its own display table).
  979. Changing this requires executing `\\[org-mode]' in a buffer to become
  980. effective."
  981. :group 'org-startup
  982. :type '(choice (const :tag "Default" nil)
  983. (string :tag "String" :value "...#"))
  984. :safe (lambda (v) (and (string-or-null-p v) (not (equal "" v)))))
  985. (defvar org-display-table nil
  986. "The display table for Org mode, in case `org-ellipsis' is non-nil.")
  987. (defgroup org-keywords nil
  988. "Keywords in Org mode."
  989. :tag "Org Keywords"
  990. :group 'org)
  991. (defcustom org-closed-keep-when-no-todo nil
  992. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  993. :group 'org-todo
  994. :group 'org-keywords
  995. :version "24.4"
  996. :package-version '(Org . "8.0")
  997. :type 'boolean)
  998. (defgroup org-structure nil
  999. "Options concerning the general structure of Org files."
  1000. :tag "Org Structure"
  1001. :group 'org)
  1002. (defgroup org-reveal-location nil
  1003. "Options about how to make context of a location visible."
  1004. :tag "Org Reveal Location"
  1005. :group 'org-structure)
  1006. (defcustom org-show-context-detail '((agenda . local)
  1007. (bookmark-jump . lineage)
  1008. (isearch . lineage)
  1009. (default . ancestors))
  1010. "Alist between context and visibility span when revealing a location.
  1011. \\<org-mode-map>Some actions may move point into invisible
  1012. locations. As a consequence, Org always expose a neighborhood
  1013. around point. How much is shown depends on the initial action,
  1014. or context. Valid contexts are
  1015. agenda when exposing an entry from the agenda
  1016. org-goto when using the command `org-goto' (`\\[org-goto]')
  1017. occur-tree when using the command `org-occur' (`\\[org-sparse-tree] /')
  1018. tags-tree when constructing a sparse tree based on tags matches
  1019. link-search when exposing search matches associated with a link
  1020. mark-goto when exposing the jump goal of a mark
  1021. bookmark-jump when exposing a bookmark location
  1022. isearch when exiting from an incremental search
  1023. default default for all contexts not set explicitly
  1024. Allowed visibility spans are
  1025. minimal show current headline; if point is not on headline,
  1026. also show entry
  1027. local show current headline, entry and next headline
  1028. ancestors show current headline and its direct ancestors; if
  1029. point is not on headline, also show entry
  1030. lineage show current headline, its direct ancestors and all
  1031. their children; if point is not on headline, also show
  1032. entry and first child
  1033. tree show current headline, its direct ancestors and all
  1034. their children; if point is not on headline, also show
  1035. entry and all children
  1036. canonical show current headline, its direct ancestors along with
  1037. their entries and children; if point is not located on
  1038. the headline, also show current entry and all children
  1039. As special cases, a nil or t value means show all contexts in
  1040. `minimal' or `canonical' view, respectively.
  1041. Some views can make displayed information very compact, but also
  1042. make it harder to edit the location of the match. In such
  1043. a case, use the command `org-reveal' (`\\[org-reveal]') to show
  1044. more context."
  1045. :group 'org-reveal-location
  1046. :version "26.1"
  1047. :package-version '(Org . "9.0")
  1048. :type '(choice
  1049. (const :tag "Canonical" t)
  1050. (const :tag "Minimal" nil)
  1051. (repeat :greedy t :tag "Individual contexts"
  1052. (cons
  1053. (choice :tag "Context"
  1054. (const agenda)
  1055. (const org-goto)
  1056. (const occur-tree)
  1057. (const tags-tree)
  1058. (const link-search)
  1059. (const mark-goto)
  1060. (const bookmark-jump)
  1061. (const isearch)
  1062. (const default))
  1063. (choice :tag "Detail level"
  1064. (const minimal)
  1065. (const local)
  1066. (const ancestors)
  1067. (const lineage)
  1068. (const tree)
  1069. (const canonical))))))
  1070. (defcustom org-indirect-buffer-display 'other-window
  1071. "How should indirect tree buffers be displayed?
  1072. This applies to indirect buffers created with the commands
  1073. `org-tree-to-indirect-buffer' and `org-agenda-tree-to-indirect-buffer'.
  1074. Valid values are:
  1075. current-window Display in the current window
  1076. other-window Just display in another window.
  1077. dedicated-frame Create one new frame, and re-use it each time.
  1078. new-frame Make a new frame each time. Note that in this case
  1079. previously-made indirect buffers are kept, and you need to
  1080. kill these buffers yourself."
  1081. :group 'org-structure
  1082. :group 'org-agenda-windows
  1083. :type '(choice
  1084. (const :tag "In current window" current-window)
  1085. (const :tag "In current frame, other window" other-window)
  1086. (const :tag "Each time a new frame" new-frame)
  1087. (const :tag "One dedicated frame" dedicated-frame)))
  1088. (defcustom org-use-speed-commands nil
  1089. "Non-nil means activate single letter commands at beginning of a headline.
  1090. This may also be a function to test for appropriate locations where speed
  1091. commands should be active.
  1092. For example, to activate speed commands when the point is on any
  1093. star at the beginning of the headline, you can do this:
  1094. (setq org-use-speed-commands
  1095. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1096. :group 'org-structure
  1097. :type '(choice
  1098. (const :tag "Never" nil)
  1099. (const :tag "At beginning of headline stars" t)
  1100. (function)))
  1101. (defcustom org-speed-commands-user nil
  1102. "Alist of additional speed commands.
  1103. This list will be checked before `org-speed-commands-default'
  1104. when the variable `org-use-speed-commands' is non-nil
  1105. and when the cursor is at the beginning of a headline.
  1106. The car if each entry is a string with a single letter, which must
  1107. be assigned to `self-insert-command' in the global map.
  1108. The cdr is either a command to be called interactively, a function
  1109. to be called, or a form to be evaluated.
  1110. An entry that is just a list with a single string will be interpreted
  1111. as a descriptive headline that will be added when listing the speed
  1112. commands in the Help buffer using the `?' speed command."
  1113. :group 'org-structure
  1114. :type '(repeat :value ("k" . ignore)
  1115. (choice :value ("k" . ignore)
  1116. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1117. (cons :tag "Letter and Command"
  1118. (string :tag "Command letter")
  1119. (choice
  1120. (function)
  1121. (sexp))))))
  1122. (defcustom org-bookmark-names-plist
  1123. '(:last-capture "org-capture-last-stored"
  1124. :last-refile "org-refile-last-stored"
  1125. :last-capture-marker "org-capture-last-stored-marker")
  1126. "Names for bookmarks automatically set by some Org commands.
  1127. This can provide strings as names for a number of bookmarks Org sets
  1128. automatically. The following keys are currently implemented:
  1129. :last-capture
  1130. :last-capture-marker
  1131. :last-refile
  1132. When a key does not show up in the property list, the corresponding bookmark
  1133. is not set."
  1134. :group 'org-structure
  1135. :type 'plist)
  1136. (defgroup org-cycle nil
  1137. "Options concerning visibility cycling in Org mode."
  1138. :tag "Org Cycle"
  1139. :group 'org-structure)
  1140. (defcustom org-cycle-skip-children-state-if-no-children t
  1141. "Non-nil means skip CHILDREN state in entries that don't have any."
  1142. :group 'org-cycle
  1143. :type 'boolean)
  1144. (defcustom org-cycle-max-level nil
  1145. "Maximum level which should still be subject to visibility cycling.
  1146. Levels higher than this will, for cycling, be treated as text, not a headline.
  1147. When `org-odd-levels-only' is set, a value of N in this variable actually
  1148. means 2N-1 stars as the limiting headline.
  1149. When nil, cycle all levels.
  1150. Note that the limiting level of cycling is also influenced by
  1151. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1152. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1153. than its value."
  1154. :group 'org-cycle
  1155. :type '(choice
  1156. (const :tag "No limit" nil)
  1157. (integer :tag "Maximum level")))
  1158. (defcustom org-hide-block-startup nil
  1159. "Non-nil means entering Org mode will fold all blocks.
  1160. This can also be set in on a per-file basis with
  1161. #+STARTUP: hideblocks
  1162. #+STARTUP: showblocks"
  1163. :group 'org-startup
  1164. :group 'org-cycle
  1165. :type 'boolean)
  1166. (defcustom org-cycle-global-at-bob nil
  1167. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1168. This makes it possible to do global cycling without having to use `S-TAB'
  1169. or `\\[universal-argument] TAB'. For this special case to work, the first \
  1170. line of the buffer
  1171. must not be a headline -- it may be empty or some other text.
  1172. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1173. sure the cursor stays at the beginning of the buffer.
  1174. When this option is nil, don't do anything special at the beginning of
  1175. the buffer."
  1176. :group 'org-cycle
  1177. :type 'boolean)
  1178. (defcustom org-cycle-level-after-item/entry-creation t
  1179. "Non-nil means cycle entry level or item indentation in new empty entries.
  1180. When the cursor is at the end of an empty headline, i.e., with only stars
  1181. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1182. and then all possible ancestor states, before returning to the original state.
  1183. This makes data entry extremely fast: M-RET to create a new headline,
  1184. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1185. When the cursor is at the end of an empty plain list item, one TAB will
  1186. make it a subitem, two or more tabs will back up to make this an item
  1187. higher up in the item hierarchy."
  1188. :group 'org-cycle
  1189. :type 'boolean)
  1190. (defcustom org-cycle-emulate-tab t
  1191. "Where should `org-cycle' emulate TAB.
  1192. nil Never
  1193. white Only in completely white lines
  1194. whitestart Only at the beginning of lines, before the first non-white char
  1195. t Everywhere except in headlines
  1196. exc-hl-bol Everywhere except at the start of a headline
  1197. If TAB is used in a place where it does not emulate TAB, the current subtree
  1198. visibility is cycled."
  1199. :group 'org-cycle
  1200. :type '(choice (const :tag "Never" nil)
  1201. (const :tag "Only in completely white lines" white)
  1202. (const :tag "Before first char in a line" whitestart)
  1203. (const :tag "Everywhere except in headlines" t)
  1204. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1205. (defcustom org-cycle-separator-lines 2
  1206. "Number of empty lines needed to keep an empty line between collapsed trees.
  1207. If you leave an empty line between the end of a subtree and the following
  1208. headline, this empty line is hidden when the subtree is folded.
  1209. Org mode will leave (exactly) one empty line visible if the number of
  1210. empty lines is equal or larger to the number given in this variable.
  1211. So the default 2 means at least 2 empty lines after the end of a subtree
  1212. are needed to produce free space between a collapsed subtree and the
  1213. following headline.
  1214. If the number is negative, and the number of empty lines is at least -N,
  1215. all empty lines are shown.
  1216. Special case: when 0, never leave empty lines in collapsed view."
  1217. :group 'org-cycle
  1218. :type 'integer)
  1219. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1220. (defcustom org-pre-cycle-hook nil
  1221. "Hook that is run before visibility cycling is happening.
  1222. The function(s) in this hook must accept a single argument which indicates
  1223. the new state that will be set right after running this hook. The
  1224. argument is a symbol. Before a global state change, it can have the values
  1225. `overview', `content', or `all'. Before a local state change, it can have
  1226. the values `folded', `children', or `subtree'."
  1227. :group 'org-cycle
  1228. :type 'hook)
  1229. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1230. org-cycle-hide-drawers
  1231. org-cycle-show-empty-lines
  1232. org-optimize-window-after-visibility-change)
  1233. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1234. The function(s) in this hook must accept a single argument which indicates
  1235. the new state that was set by the most recent `org-cycle' command. The
  1236. argument is a symbol. After a global state change, it can have the values
  1237. `overview', `contents', or `all'. After a local state change, it can have
  1238. the values `folded', `children', or `subtree'."
  1239. :group 'org-cycle
  1240. :type 'hook
  1241. :version "26.1"
  1242. :package-version '(Org . "8.3"))
  1243. (defgroup org-edit-structure nil
  1244. "Options concerning structure editing in Org mode."
  1245. :tag "Org Edit Structure"
  1246. :group 'org-structure)
  1247. (defcustom org-odd-levels-only nil
  1248. "Non-nil means skip even levels and only use odd levels for the outline.
  1249. This has the effect that two stars are being added/taken away in
  1250. promotion/demotion commands. It also influences how levels are
  1251. handled by the exporters.
  1252. Changing it requires restart of `font-lock-mode' to become effective
  1253. for fontification also in regions already fontified.
  1254. You may also set this on a per-file basis by adding one of the following
  1255. lines to the buffer:
  1256. #+STARTUP: odd
  1257. #+STARTUP: oddeven"
  1258. :group 'org-edit-structure
  1259. :group 'org-appearance
  1260. :type 'boolean)
  1261. (defcustom org-adapt-indentation t
  1262. "Non-nil means adapt indentation to outline node level.
  1263. When this variable is set, Org assumes that you write outlines by
  1264. indenting text in each node to align with the headline (after the
  1265. stars). The following issues are influenced by this variable:
  1266. - The indentation is increased by one space in a demotion
  1267. command, and decreased by one in a promotion command. However,
  1268. in the latter case, if shifting some line in the entry body
  1269. would alter document structure (e.g., insert a new headline),
  1270. indentation is not changed at all.
  1271. - Property drawers and planning information is inserted indented
  1272. when this variable is set. When nil, they will not be indented.
  1273. - TAB indents a line relative to current level. The lines below
  1274. a headline will be indented when this variable is set.
  1275. Note that this is all about true indentation, by adding and
  1276. removing space characters. See also `org-indent.el' which does
  1277. level-dependent indentation in a virtual way, i.e. at display
  1278. time in Emacs."
  1279. :group 'org-edit-structure
  1280. :type 'boolean)
  1281. (defcustom org-special-ctrl-a/e nil
  1282. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1283. When t, `C-a' will bring back the cursor to the beginning of the
  1284. headline text, i.e. after the stars and after a possible TODO
  1285. keyword. In an item, this will be the position after bullet and
  1286. check-box, if any. When the cursor is already at that position,
  1287. another `C-a' will bring it to the beginning of the line.
  1288. `C-e' will jump to the end of the headline, ignoring the presence
  1289. of tags in the headline. A second `C-e' will then jump to the
  1290. true end of the line, after any tags. This also means that, when
  1291. this variable is non-nil, `C-e' also will never jump beyond the
  1292. end of the heading of a folded section, i.e. not after the
  1293. ellipses.
  1294. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1295. normally, going to the true line boundary first. Only a directly
  1296. following, identical keypress will bring the cursor to the
  1297. special positions.
  1298. This may also be a cons cell where the behavior for `C-a' and
  1299. `C-e' is set separately."
  1300. :group 'org-edit-structure
  1301. :type '(choice
  1302. (const :tag "off" nil)
  1303. (const :tag "on: after stars/bullet and before tags first" t)
  1304. (const :tag "reversed: true line boundary first" reversed)
  1305. (cons :tag "Set C-a and C-e separately"
  1306. (choice :tag "Special C-a"
  1307. (const :tag "off" nil)
  1308. (const :tag "on: after stars/bullet first" t)
  1309. (const :tag "reversed: before stars/bullet first" reversed))
  1310. (choice :tag "Special C-e"
  1311. (const :tag "off" nil)
  1312. (const :tag "on: before tags first" t)
  1313. (const :tag "reversed: after tags first" reversed)))))
  1314. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1315. (defcustom org-special-ctrl-k nil
  1316. "Non-nil means `C-k' will behave specially in headlines.
  1317. When nil, `C-k' will call the default `kill-line' command.
  1318. When t, the following will happen while the cursor is in the headline:
  1319. - When the cursor is at the beginning of a headline, kill the entire
  1320. line and possible the folded subtree below the line.
  1321. - When in the middle of the headline text, kill the headline up to the tags.
  1322. - When after the headline text, kill the tags."
  1323. :group 'org-edit-structure
  1324. :type 'boolean)
  1325. (defcustom org-ctrl-k-protect-subtree nil
  1326. "Non-nil means, do not delete a hidden subtree with C-k.
  1327. When set to the symbol `error', simply throw an error when C-k is
  1328. used to kill (part-of) a headline that has hidden text behind it.
  1329. Any other non-nil value will result in a query to the user, if it is
  1330. OK to kill that hidden subtree. When nil, kill without remorse."
  1331. :group 'org-edit-structure
  1332. :version "24.1"
  1333. :type '(choice
  1334. (const :tag "Do not protect hidden subtrees" nil)
  1335. (const :tag "Protect hidden subtrees with a security query" t)
  1336. (const :tag "Never kill a hidden subtree with C-k" error)))
  1337. (defcustom org-special-ctrl-o t
  1338. "Non-nil means, make `C-o' insert a row in tables."
  1339. :group 'org-edit-structure
  1340. :type 'boolean)
  1341. (defcustom org-catch-invisible-edits nil
  1342. "Check if in invisible region before inserting or deleting a character.
  1343. Valid values are:
  1344. nil Do not check, so just do invisible edits.
  1345. error Throw an error and do nothing.
  1346. show Make point visible, and do the requested edit.
  1347. show-and-error Make point visible, then throw an error and abort the edit.
  1348. smart Make point visible, and do insertion/deletion if it is
  1349. adjacent to visible text and the change feels predictable.
  1350. Never delete a previously invisible character or add in the
  1351. middle or right after an invisible region. Basically, this
  1352. allows insertion and backward-delete right before ellipses.
  1353. FIXME: maybe in this case we should not even show?"
  1354. :group 'org-edit-structure
  1355. :version "24.1"
  1356. :type '(choice
  1357. (const :tag "Do not check" nil)
  1358. (const :tag "Throw error when trying to edit" error)
  1359. (const :tag "Unhide, but do not do the edit" show-and-error)
  1360. (const :tag "Show invisible part and do the edit" show)
  1361. (const :tag "Be smart and do the right thing" smart)))
  1362. (defcustom org-yank-folded-subtrees t
  1363. "Non-nil means when yanking subtrees, fold them.
  1364. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1365. it starts with a heading and all other headings in it are either children
  1366. or siblings, then fold all the subtrees. However, do this only if no
  1367. text after the yank would be swallowed into a folded tree by this action."
  1368. :group 'org-edit-structure
  1369. :type 'boolean)
  1370. (defcustom org-yank-adjusted-subtrees nil
  1371. "Non-nil means when yanking subtrees, adjust the level.
  1372. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1373. this function for details."
  1374. :group 'org-edit-structure
  1375. :type 'boolean)
  1376. (defcustom org-M-RET-may-split-line '((default . t))
  1377. "Non-nil means M-RET will split the line at the cursor position.
  1378. When nil, it will go to the end of the line before making a
  1379. new line.
  1380. You may also set this option in a different way for different
  1381. contexts. Valid contexts are:
  1382. headline when creating a new headline
  1383. item when creating a new item
  1384. table in a table field
  1385. default the value to be used for all contexts not explicitly
  1386. customized"
  1387. :group 'org-structure
  1388. :group 'org-table
  1389. :type '(choice
  1390. (const :tag "Always" t)
  1391. (const :tag "Never" nil)
  1392. (repeat :greedy t :tag "Individual contexts"
  1393. (cons
  1394. (choice :tag "Context"
  1395. (const headline)
  1396. (const item)
  1397. (const table)
  1398. (const default))
  1399. (boolean)))))
  1400. (defcustom org-insert-heading-respect-content nil
  1401. "Non-nil means insert new headings after the current subtree.
  1402. \\<org-mode-map>
  1403. When nil, the new heading is created directly after the current line.
  1404. The commands `\\[org-insert-heading-respect-content]' and \
  1405. `\\[org-insert-todo-heading-respect-content]' turn this variable on
  1406. for the duration of the command."
  1407. :group 'org-structure
  1408. :type 'boolean)
  1409. (defcustom org-blank-before-new-entry '((heading . auto)
  1410. (plain-list-item . auto))
  1411. "Should `org-insert-heading' leave a blank line before new heading/item?
  1412. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1413. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1414. which case Org will look at the surrounding headings/items and try to
  1415. make an intelligent decision whether to insert a blank line or not."
  1416. :group 'org-edit-structure
  1417. :type '(list
  1418. (cons (const heading)
  1419. (choice (const :tag "Never" nil)
  1420. (const :tag "Always" t)
  1421. (const :tag "Auto" auto)))
  1422. (cons (const plain-list-item)
  1423. (choice (const :tag "Never" nil)
  1424. (const :tag "Always" t)
  1425. (const :tag "Auto" auto)))))
  1426. (defcustom org-insert-heading-hook nil
  1427. "Hook being run after inserting a new heading."
  1428. :group 'org-edit-structure
  1429. :type 'hook)
  1430. (defcustom org-enable-fixed-width-editor t
  1431. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1432. This currently only means they are never auto-wrapped.
  1433. When nil, such lines will be treated like ordinary lines."
  1434. :group 'org-edit-structure
  1435. :type 'boolean)
  1436. (defcustom org-goto-auto-isearch t
  1437. "Non-nil means typing characters in `org-goto' starts incremental search.
  1438. When nil, you can use these keybindings to navigate the buffer:
  1439. q Quit the org-goto interface
  1440. n Go to the next visible heading
  1441. p Go to the previous visible heading
  1442. f Go one heading forward on same level
  1443. b Go one heading backward on same level
  1444. u Go one heading up"
  1445. :group 'org-edit-structure
  1446. :type 'boolean)
  1447. (defgroup org-sparse-trees nil
  1448. "Options concerning sparse trees in Org mode."
  1449. :tag "Org Sparse Trees"
  1450. :group 'org-structure)
  1451. (defcustom org-highlight-sparse-tree-matches t
  1452. "Non-nil means highlight all matches that define a sparse tree.
  1453. The highlights will automatically disappear the next time the buffer is
  1454. changed by an edit command."
  1455. :group 'org-sparse-trees
  1456. :type 'boolean)
  1457. (defcustom org-remove-highlights-with-change t
  1458. "Non-nil means any change to the buffer will remove temporary highlights.
  1459. \\<org-mode-map>\
  1460. Such highlights are created by `org-occur' and `org-clock-display'.
  1461. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1462. to get rid of the highlights.
  1463. The highlights created by `org-toggle-latex-fragment' always need
  1464. `\\[org-toggle-latex-fragment]' to be removed."
  1465. :group 'org-sparse-trees
  1466. :group 'org-time
  1467. :type 'boolean)
  1468. (defcustom org-occur-case-fold-search t
  1469. "Non-nil means `org-occur' should be case-insensitive.
  1470. If set to `smart' the search will be case-insensitive only if it
  1471. doesn't specify any upper case character."
  1472. :group 'org-sparse-trees
  1473. :version "26.1"
  1474. :type '(choice
  1475. (const :tag "Case-sensitive" nil)
  1476. (const :tag "Case-insensitive" t)
  1477. (const :tag "Case-insensitive for lower case searches only" 'smart)))
  1478. (defcustom org-occur-hook '(org-first-headline-recenter)
  1479. "Hook that is run after `org-occur' has constructed a sparse tree.
  1480. This can be used to recenter the window to show as much of the structure
  1481. as possible."
  1482. :group 'org-sparse-trees
  1483. :type 'hook)
  1484. (defgroup org-imenu-and-speedbar nil
  1485. "Options concerning imenu and speedbar in Org mode."
  1486. :tag "Org Imenu and Speedbar"
  1487. :group 'org-structure)
  1488. (defcustom org-imenu-depth 2
  1489. "The maximum level for Imenu access to Org headlines.
  1490. This also applied for speedbar access."
  1491. :group 'org-imenu-and-speedbar
  1492. :type 'integer)
  1493. (defgroup org-table nil
  1494. "Options concerning tables in Org mode."
  1495. :tag "Org Table"
  1496. :group 'org)
  1497. (defcustom org-enable-table-editor 'optimized
  1498. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1499. When nil, such lines will be treated like ordinary lines.
  1500. When equal to the symbol `optimized', the table editor will be optimized to
  1501. do the following:
  1502. - Automatic overwrite mode in front of whitespace in table fields.
  1503. This makes the structure of the table stay in tact as long as the edited
  1504. field does not exceed the column width.
  1505. - Minimize the number of realigns. Normally, the table is aligned each time
  1506. TAB or RET are pressed to move to another field. With optimization this
  1507. happens only if changes to a field might have changed the column width.
  1508. Optimization requires replacing the functions `self-insert-command',
  1509. `delete-char', and `backward-delete-char' in Org buffers, with a
  1510. slight (in fact: unnoticeable) speed impact for normal typing. Org is very
  1511. good at guessing when a re-align will be necessary, but you can always
  1512. force one with `\\[org-ctrl-c-ctrl-c]'.
  1513. If you would like to use the optimized version in Org mode, but the
  1514. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1515. This variable can be used to turn on and off the table editor during a session,
  1516. but in order to toggle optimization, a restart is required.
  1517. See also the variable `org-table-auto-blank-field'."
  1518. :group 'org-table
  1519. :type '(choice
  1520. (const :tag "off" nil)
  1521. (const :tag "on" t)
  1522. (const :tag "on, optimized" optimized)))
  1523. (defcustom org-self-insert-cluster-for-undo nil
  1524. "Non-nil means cluster self-insert commands for undo when possible.
  1525. If this is set, then, like in the Emacs command loop, 20 consecutive
  1526. characters will be undone together.
  1527. This is configurable, because there is some impact on typing performance."
  1528. :group 'org-table
  1529. :type 'boolean)
  1530. (defcustom org-table-tab-recognizes-table.el t
  1531. "Non-nil means TAB will automatically notice a table.el table.
  1532. When it sees such a table, it moves point into it and - if necessary -
  1533. calls `table-recognize-table'."
  1534. :group 'org-table-editing
  1535. :type 'boolean)
  1536. (defgroup org-link nil
  1537. "Options concerning links in Org mode."
  1538. :tag "Org Link"
  1539. :group 'org)
  1540. (defvar-local org-link-abbrev-alist-local nil
  1541. "Buffer-local version of `org-link-abbrev-alist', which see.
  1542. The value of this is taken from the #+LINK lines.")
  1543. (defcustom org-link-parameters
  1544. '(("doi" :follow org--open-doi-link)
  1545. ("elisp" :follow org--open-elisp-link)
  1546. ("file" :complete org-file-complete-link)
  1547. ("ftp" :follow (lambda (path) (browse-url (concat "ftp:" path))))
  1548. ("help" :follow org--open-help-link)
  1549. ("http" :follow (lambda (path) (browse-url (concat "http:" path))))
  1550. ("https" :follow (lambda (path) (browse-url (concat "https:" path))))
  1551. ("mailto" :follow (lambda (path) (browse-url (concat "mailto:" path))))
  1552. ("news" :follow (lambda (path) (browse-url (concat "news:" path))))
  1553. ("shell" :follow org--open-shell-link))
  1554. "An alist of properties that defines all the links in Org mode.
  1555. The key in each association is a string of the link type.
  1556. Subsequent optional elements make up a p-list of link properties.
  1557. :follow - A function that takes the link path as an argument.
  1558. :export - A function that takes the link path, description and
  1559. export-backend as arguments.
  1560. :store - A function responsible for storing the link. See the
  1561. function `org-store-link-functions'.
  1562. :complete - A function that inserts a link with completion. The
  1563. function takes one optional prefix arg.
  1564. :face - A face for the link, or a function that returns a face.
  1565. The function takes one argument which is the link path. The
  1566. default face is `org-link'.
  1567. :mouse-face - The mouse-face. The default is `highlight'.
  1568. :display - `full' will not fold the link in descriptive
  1569. display. Default is `org-link'.
  1570. :help-echo - A string or function that takes (window object position)
  1571. as arguments and returns a string.
  1572. :keymap - A keymap that is active on the link. The default is
  1573. `org-mouse-map'.
  1574. :htmlize-link - A function for the htmlize-link. Defaults
  1575. to (list :uri \"type:path\")
  1576. :activate-func - A function to run at the end of font-lock
  1577. activation. The function must accept (link-start link-end path bracketp)
  1578. as arguments."
  1579. :group 'org-link
  1580. :type '(alist :tag "Link display parameters"
  1581. :value-type plist)
  1582. :version "26.1"
  1583. :package-version '(Org . "9.1"))
  1584. (defun org-link-get-parameter (type key)
  1585. "Get TYPE link property for KEY.
  1586. TYPE is a string and KEY is a plist keyword."
  1587. (plist-get
  1588. (cdr (assoc type org-link-parameters))
  1589. key))
  1590. (defun org-link-set-parameters (type &rest parameters)
  1591. "Set link TYPE properties to PARAMETERS.
  1592. PARAMETERS should be :key val pairs."
  1593. (let ((data (assoc type org-link-parameters)))
  1594. (if data (setcdr data (org-combine-plists (cdr data) parameters))
  1595. (push (cons type parameters) org-link-parameters)
  1596. (org-make-link-regexps)
  1597. (org-element-update-syntax))))
  1598. (defun org-link-types ()
  1599. "Return a list of known link types."
  1600. (mapcar #'car org-link-parameters))
  1601. (defcustom org-link-abbrev-alist nil
  1602. "Alist of link abbreviations.
  1603. The car of each element is a string, to be replaced at the start of a link.
  1604. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1605. links in Org buffers can have an optional tag after a double colon, e.g.,
  1606. [[linkkey:tag][description]]
  1607. The `linkkey' must be a single word, starting with a letter, followed
  1608. by letters, numbers, `-' or `_'.
  1609. If REPLACE is a string, the tag will simply be appended to create the link.
  1610. If the string contains \"%s\", the tag will be inserted there. If the string
  1611. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1612. at that point (see the function `url-hexify-string'). If the string contains
  1613. the specifier \"%(my-function)\", then the custom function `my-function' will
  1614. be invoked: this function takes the tag as its only argument and must return
  1615. a string.
  1616. REPLACE may also be a function that will be called with the tag as the
  1617. only argument to create the link, which should be returned as a string.
  1618. See the manual for examples."
  1619. :group 'org-link
  1620. :type '(repeat
  1621. (cons
  1622. (string :tag "Protocol")
  1623. (choice
  1624. (string :tag "Format")
  1625. (function)))))
  1626. (defcustom org-descriptive-links t
  1627. "Non-nil means Org will display descriptive links.
  1628. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1629. \"Org Website\", hiding the link itself and just displaying its
  1630. description. When set to nil, Org will display the full links
  1631. literally.
  1632. You can interactively set the value of this variable by calling
  1633. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1634. :group 'org-link
  1635. :type 'boolean)
  1636. (defcustom org-link-file-path-type 'adaptive
  1637. "How the path name in file links should be stored.
  1638. Valid values are:
  1639. relative Relative to the current directory, i.e. the directory of the file
  1640. into which the link is being inserted.
  1641. absolute Absolute path, if possible with ~ for home directory.
  1642. noabbrev Absolute path, no abbreviation of home directory.
  1643. adaptive Use relative path for files in the current directory and sub-
  1644. directories of it. For other files, use an absolute path."
  1645. :group 'org-link
  1646. :type '(choice
  1647. (const relative)
  1648. (const absolute)
  1649. (const noabbrev)
  1650. (const adaptive)))
  1651. (defvaralias 'org-activate-links 'org-highlight-links)
  1652. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1653. "Types of links that should be highlighted in Org files.
  1654. This is a list of symbols, each one of them leading to the
  1655. highlighting of a certain link type.
  1656. You can still open links that are not highlighted.
  1657. In principle, it does not hurt to turn on highlighting for all
  1658. link types. There may be a small gain when turning off unused
  1659. link types. The types are:
  1660. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1661. angle Links in angular brackets that may contain whitespace like
  1662. <bbdb:Carsten Dominik>.
  1663. plain Plain links in normal text, no whitespace, like http://google.com.
  1664. radio Text that is matched by a radio target, see manual for details.
  1665. tag Tag settings in a headline (link to tag search).
  1666. date Time stamps (link to calendar).
  1667. footnote Footnote labels.
  1668. If you set this variable during an Emacs session, use `org-mode-restart'
  1669. in the Org buffer so that the change takes effect."
  1670. :group 'org-link
  1671. :group 'org-appearance
  1672. :type '(set :greedy t
  1673. (const :tag "Double bracket links" bracket)
  1674. (const :tag "Angular bracket links" angle)
  1675. (const :tag "Plain text links" plain)
  1676. (const :tag "Radio target matches" radio)
  1677. (const :tag "Tags" tag)
  1678. (const :tag "Timestamps" date)
  1679. (const :tag "Footnotes" footnote)))
  1680. (defcustom org-make-link-description-function nil
  1681. "Function to use for generating link descriptions from links.
  1682. When nil, the link location will be used. This function must take
  1683. two parameters: the first one is the link, the second one is the
  1684. description generated by `org-insert-link'. The function should
  1685. return the description to use."
  1686. :group 'org-link
  1687. :type '(choice (const nil) (function)))
  1688. (defgroup org-link-store nil
  1689. "Options concerning storing links in Org mode."
  1690. :tag "Org Store Link"
  1691. :group 'org-link)
  1692. (defcustom org-url-hexify-p t
  1693. "When non-nil, hexify URL when creating a link."
  1694. :type 'boolean
  1695. :version "24.3"
  1696. :group 'org-link-store)
  1697. (defcustom org-email-link-description-format "Email %c: %.30s"
  1698. "Format of the description part of a link to an email or usenet message.
  1699. The following %-escapes will be replaced by corresponding information:
  1700. %F full \"From\" field
  1701. %f name, taken from \"From\" field, address if no name
  1702. %T full \"To\" field
  1703. %t first name in \"To\" field, address if no name
  1704. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1705. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1706. %s subject
  1707. %d date
  1708. %m message-id.
  1709. You may use normal field width specification between the % and the letter.
  1710. This is for example useful to limit the length of the subject.
  1711. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1712. :group 'org-link-store
  1713. :type 'string)
  1714. (defcustom org-from-is-user-regexp
  1715. (let (r1 r2)
  1716. (when (and user-mail-address (not (string= user-mail-address "")))
  1717. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1718. (when (and user-full-name (not (string= user-full-name "")))
  1719. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1720. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1721. "Regexp matched against the \"From:\" header of an email or usenet message.
  1722. It should match if the message is from the user him/herself."
  1723. :group 'org-link-store
  1724. :type 'regexp)
  1725. (defcustom org-context-in-file-links t
  1726. "Non-nil means file links from `org-store-link' contain context.
  1727. \\<org-mode-map>
  1728. A search string will be added to the file name with :: as separator
  1729. and used to find the context when the link is activated by the command
  1730. `org-open-at-point'. When this option is t, the entire active region
  1731. will be placed in the search string of the file link. If set to a
  1732. positive integer, only the first n lines of context will be stored.
  1733. Using a prefix arg to the command `org-store-link' (`\\[universal-argument] \
  1734. \\[org-store-link]')
  1735. negates this setting for the duration of the command."
  1736. :group 'org-link-store
  1737. :type '(choice boolean integer))
  1738. (defcustom org-keep-stored-link-after-insertion nil
  1739. "Non-nil means keep link in list for entire session.
  1740. \\<org-mode-map>
  1741. The command `org-store-link' adds a link pointing to the current
  1742. location to an internal list. These links accumulate during a session.
  1743. The command `org-insert-link' can be used to insert links into any
  1744. Org file (offering completion for all stored links).
  1745. When this option is nil, every link which has been inserted once using
  1746. `\\[org-insert-link]' will be removed from the list, to make completing the \
  1747. unused
  1748. links more efficient."
  1749. :group 'org-link-store
  1750. :type 'boolean)
  1751. (defgroup org-link-follow nil
  1752. "Options concerning following links in Org mode."
  1753. :tag "Org Follow Link"
  1754. :group 'org-link)
  1755. (defcustom org-link-translation-function nil
  1756. "Function to translate links with different syntax to Org syntax.
  1757. This can be used to translate links created for example by the Planner
  1758. or emacs-wiki packages to Org syntax.
  1759. The function must accept two parameters, a TYPE containing the link
  1760. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1761. which is everything after the link protocol. It should return a cons
  1762. with possibly modified values of type and path.
  1763. Org contains a function for this, so if you set this variable to
  1764. `org-translate-link-from-planner', you should be able follow many
  1765. links created by planner."
  1766. :group 'org-link-follow
  1767. :type '(choice (const nil) (function)))
  1768. (defcustom org-follow-link-hook nil
  1769. "Hook that is run after a link has been followed."
  1770. :group 'org-link-follow
  1771. :type 'hook)
  1772. (defcustom org-tab-follows-link nil
  1773. "Non-nil means on links TAB will follow the link.
  1774. Needs to be set before org.el is loaded.
  1775. This really should not be used, it does not make sense, and the
  1776. implementation is bad."
  1777. :group 'org-link-follow
  1778. :type 'boolean)
  1779. (defcustom org-return-follows-link nil
  1780. "Non-nil means on links RET will follow the link.
  1781. In tables, the special behavior of RET has precedence."
  1782. :group 'org-link-follow
  1783. :type 'boolean)
  1784. (defcustom org-mouse-1-follows-link
  1785. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1786. "Non-nil means mouse-1 on a link will follow the link.
  1787. A longer mouse click will still set point. Needs to be set
  1788. before org.el is loaded."
  1789. :group 'org-link-follow
  1790. :version "24.4"
  1791. :package-version '(Org . "8.3")
  1792. :type '(choice
  1793. (const :tag "A double click follows the link" double)
  1794. (const :tag "Unconditionally follow the link with mouse-1" t)
  1795. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1796. (defcustom org-mark-ring-length 4
  1797. "Number of different positions to be recorded in the ring.
  1798. Changing this requires a restart of Emacs to work correctly."
  1799. :group 'org-link-follow
  1800. :type 'integer)
  1801. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1802. "Non-nil means internal fuzzy links can only match headlines.
  1803. When nil, the a fuzzy link may point to a target or a named
  1804. construct in the document. When set to the special value
  1805. `query-to-create', offer to create a new headline when none
  1806. matched.
  1807. Spaces and statistics cookies are ignored during heading searches."
  1808. :group 'org-link-follow
  1809. :version "24.1"
  1810. :type '(choice
  1811. (const :tag "Use fuzzy text search" nil)
  1812. (const :tag "Match only exact headline" t)
  1813. (const :tag "Match exact headline or query to create it"
  1814. query-to-create))
  1815. :safe #'symbolp)
  1816. (defcustom org-link-frame-setup
  1817. '((vm . vm-visit-folder-other-frame)
  1818. (vm-imap . vm-visit-imap-folder-other-frame)
  1819. (gnus . org-gnus-no-new-news)
  1820. (file . find-file-other-window)
  1821. (wl . wl-other-frame))
  1822. "Setup the frame configuration for following links.
  1823. When following a link with Emacs, it may often be useful to display
  1824. this link in another window or frame. This variable can be used to
  1825. set this up for the different types of links.
  1826. For VM, use any of
  1827. `vm-visit-folder'
  1828. `vm-visit-folder-other-window'
  1829. `vm-visit-folder-other-frame'
  1830. For Gnus, use any of
  1831. `gnus'
  1832. `gnus-other-frame'
  1833. `org-gnus-no-new-news'
  1834. For FILE, use any of
  1835. `find-file'
  1836. `find-file-other-window'
  1837. `find-file-other-frame'
  1838. For Wanderlust use any of
  1839. `wl'
  1840. `wl-other-frame'
  1841. For the calendar, use the variable `calendar-setup'.
  1842. For BBDB, it is currently only possible to display the matches in
  1843. another window."
  1844. :group 'org-link-follow
  1845. :type '(list
  1846. (cons (const vm)
  1847. (choice
  1848. (const vm-visit-folder)
  1849. (const vm-visit-folder-other-window)
  1850. (const vm-visit-folder-other-frame)))
  1851. (cons (const vm-imap)
  1852. (choice
  1853. (const vm-visit-imap-folder)
  1854. (const vm-visit-imap-folder-other-window)
  1855. (const vm-visit-imap-folder-other-frame)))
  1856. (cons (const gnus)
  1857. (choice
  1858. (const gnus)
  1859. (const gnus-other-frame)
  1860. (const org-gnus-no-new-news)))
  1861. (cons (const file)
  1862. (choice
  1863. (const find-file)
  1864. (const find-file-other-window)
  1865. (const find-file-other-frame)))
  1866. (cons (const wl)
  1867. (choice
  1868. (const wl)
  1869. (const wl-other-frame)))))
  1870. (defcustom org-display-internal-link-with-indirect-buffer nil
  1871. "Non-nil means use indirect buffer to display infile links.
  1872. Activating internal links (from one location in a file to another location
  1873. in the same file) normally just jumps to the location. When the link is
  1874. activated with a `\\[universal-argument]' prefix (or with mouse-3), the link \
  1875. is displayed in
  1876. another window. When this option is set, the other window actually displays
  1877. an indirect buffer clone of the current buffer, to avoid any visibility
  1878. changes to the current buffer."
  1879. :group 'org-link-follow
  1880. :type 'boolean)
  1881. (defcustom org-open-non-existing-files nil
  1882. "Non-nil means `org-open-file' will open non-existing files.
  1883. When nil, an error will be generated.
  1884. This variable applies only to external applications because they
  1885. might choke on non-existing files. If the link is to a file that
  1886. will be opened in Emacs, the variable is ignored."
  1887. :group 'org-link-follow
  1888. :type 'boolean)
  1889. (defcustom org-open-directory-means-index-dot-org nil
  1890. "Non-nil means a link to a directory really means to index.org.
  1891. When nil, following a directory link will run dired or open a finder/explorer
  1892. window on that directory."
  1893. :group 'org-link-follow
  1894. :type 'boolean)
  1895. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1896. "Non-nil means ask for confirmation before executing shell links.
  1897. Shell links can be dangerous: just think about a link
  1898. [[shell:rm -rf ~/*][Google Search]]
  1899. This link would show up in your Org document as \"Google Search\",
  1900. but really it would remove your entire home directory.
  1901. Therefore we advise against setting this variable to nil.
  1902. Just change it to `y-or-n-p' if you want to confirm with a
  1903. single keystroke rather than having to type \"yes\"."
  1904. :group 'org-link-follow
  1905. :type '(choice
  1906. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1907. (const :tag "with y-or-n (faster)" y-or-n-p)
  1908. (const :tag "no confirmation (dangerous)" nil)))
  1909. (put 'org-confirm-shell-link-function
  1910. 'safe-local-variable
  1911. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1912. (defcustom org-confirm-shell-link-not-regexp ""
  1913. "A regexp to skip confirmation for shell links."
  1914. :group 'org-link-follow
  1915. :version "24.1"
  1916. :type 'regexp)
  1917. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1918. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1919. Elisp links can be dangerous: just think about a link
  1920. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1921. This link would show up in your Org document as \"Google Search\",
  1922. but really it would remove your entire home directory.
  1923. Therefore we advise against setting this variable to nil.
  1924. Just change it to `y-or-n-p' if you want to confirm with a
  1925. single keystroke rather than having to type \"yes\"."
  1926. :group 'org-link-follow
  1927. :type '(choice
  1928. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1929. (const :tag "with y-or-n (faster)" y-or-n-p)
  1930. (const :tag "no confirmation (dangerous)" nil)))
  1931. (put 'org-confirm-shell-link-function
  1932. 'safe-local-variable
  1933. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1934. (defcustom org-confirm-elisp-link-not-regexp ""
  1935. "A regexp to skip confirmation for Elisp links."
  1936. :group 'org-link-follow
  1937. :version "24.1"
  1938. :type 'regexp)
  1939. (defconst org-file-apps-defaults-gnu
  1940. '((remote . emacs)
  1941. (system . mailcap)
  1942. (t . mailcap))
  1943. "Default file applications on a UNIX or GNU/Linux system.
  1944. See `org-file-apps'.")
  1945. (defconst org-file-apps-defaults-macosx
  1946. '((remote . emacs)
  1947. (system . "open %s")
  1948. ("ps.gz" . "gv %s")
  1949. ("eps.gz" . "gv %s")
  1950. ("dvi" . "xdvi %s")
  1951. ("fig" . "xfig %s")
  1952. (t . "open %s"))
  1953. "Default file applications on a macOS system.
  1954. The system \"open\" is known as a default, but we use X11 applications
  1955. for some files for which the OS does not have a good default.
  1956. See `org-file-apps'.")
  1957. (defconst org-file-apps-defaults-windowsnt
  1958. (list '(remote . emacs)
  1959. (cons 'system (lambda (file _path)
  1960. (with-no-warnings (w32-shell-execute "open" file))))
  1961. (cons t (lambda (file _path)
  1962. (with-no-warnings (w32-shell-execute "open" file)))))
  1963. "Default file applications on a Windows NT system.
  1964. The system \"open\" is used for most files.
  1965. See `org-file-apps'.")
  1966. (defcustom org-file-apps
  1967. '((auto-mode . emacs)
  1968. ("\\.mm\\'" . default)
  1969. ("\\.x?html?\\'" . default)
  1970. ("\\.pdf\\'" . default))
  1971. "External applications for opening `file:path' items in a document.
  1972. \\<org-mode-map>\
  1973. Org mode uses system defaults for different file types, but
  1974. you can use this variable to set the application for a given file
  1975. extension. The entries in this list are cons cells where the car identifies
  1976. files and the cdr the corresponding command.
  1977. Possible values for the file identifier are:
  1978. \"string\" A string as a file identifier can be interpreted in different
  1979. ways, depending on its contents:
  1980. - Alphanumeric characters only:
  1981. Match links with this file extension.
  1982. Example: (\"pdf\" . \"evince %s\")
  1983. to open PDFs with evince.
  1984. - Regular expression: Match links where the
  1985. filename matches the regexp. If you want to
  1986. use groups here, use shy groups.
  1987. Example: (\"\\\\.x?html\\\\\\='\" . \"firefox %s\")
  1988. (\"\\\\(?:xhtml\\\\|html\\\\)\\\\\\='\" . \"firefox %s\")
  1989. to open *.html and *.xhtml with firefox.
  1990. - Regular expression which contains (non-shy) groups:
  1991. Match links where the whole link, including \"::\", and
  1992. anything after that, matches the regexp.
  1993. In a custom command string, %1, %2, etc. are replaced with
  1994. the parts of the link that were matched by the groups.
  1995. For backwards compatibility, if a command string is given
  1996. that does not use any of the group matches, this case is
  1997. handled identically to the second one (i.e. match against
  1998. file name only).
  1999. In a custom function, you can access the group matches with
  2000. (match-string n link).
  2001. Example: (\"\\\\.pdf::\\\\(\\\\d+\\\\)\\\\\\='\" . \
  2002. \"evince -p %1 %s\")
  2003. to open [[file:document.pdf::5]] with evince at page 5.
  2004. `directory' Matches a directory
  2005. `remote' Matches a remote file, accessible through tramp or efs.
  2006. Remote files most likely should be visited through Emacs
  2007. because external applications cannot handle such paths.
  2008. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  2009. so all files Emacs knows how to handle. Using this with
  2010. command `emacs' will open most files in Emacs. Beware that this
  2011. will also open html files inside Emacs, unless you add
  2012. (\"html\" . default) to the list as well.
  2013. `system' The system command to open files, like `open' on Windows
  2014. and macOS, and mailcap under GNU/Linux. This is the command
  2015. that will be selected if you call `org-open-at-point' with a
  2016. double prefix argument (`\\[universal-argument] \
  2017. \\[universal-argument] \\[org-open-at-point]').
  2018. t Default for files not matched by any of the other options.
  2019. Possible values for the command are:
  2020. `emacs' The file will be visited by the current Emacs process.
  2021. `default' Use the default application for this file type, which is the
  2022. association for t in the list, most likely in the system-specific
  2023. part. This can be used to overrule an unwanted setting in the
  2024. system-specific variable.
  2025. `system' Use the system command for opening files, like \"open\".
  2026. This command is specified by the entry whose car is `system'.
  2027. Most likely, the system-specific version of this variable
  2028. does define this command, but you can overrule/replace it
  2029. here.
  2030. `mailcap' Use command specified in the mailcaps.
  2031. string A command to be executed by a shell; %s will be replaced
  2032. by the path to the file.
  2033. function A Lisp function, which will be called with two arguments:
  2034. the file path and the original link string, without the
  2035. \"file:\" prefix.
  2036. For more examples, see the system specific constants
  2037. `org-file-apps-defaults-macosx'
  2038. `org-file-apps-defaults-windowsnt'
  2039. `org-file-apps-defaults-gnu'."
  2040. :group 'org-link-follow
  2041. :type '(repeat
  2042. (cons (choice :value ""
  2043. (string :tag "Extension")
  2044. (const :tag "System command to open files" system)
  2045. (const :tag "Default for unrecognized files" t)
  2046. (const :tag "Remote file" remote)
  2047. (const :tag "Links to a directory" directory)
  2048. (const :tag "Any files that have Emacs modes"
  2049. auto-mode))
  2050. (choice :value ""
  2051. (const :tag "Visit with Emacs" emacs)
  2052. (const :tag "Use default" default)
  2053. (const :tag "Use the system command" system)
  2054. (string :tag "Command")
  2055. (function :tag "Function")))))
  2056. (defcustom org-doi-server-url "http://dx.doi.org/"
  2057. "The URL of the DOI server."
  2058. :type 'string
  2059. :version "24.3"
  2060. :group 'org-link-follow)
  2061. (defgroup org-refile nil
  2062. "Options concerning refiling entries in Org mode."
  2063. :tag "Org Refile"
  2064. :group 'org)
  2065. (defcustom org-directory "~/org"
  2066. "Directory with Org files.
  2067. This is just a default location to look for Org files. There is no need
  2068. at all to put your files into this directory. It is used in the
  2069. following situations:
  2070. 1. When a capture template specifies a target file that is not an
  2071. absolute path. The path will then be interpreted relative to
  2072. `org-directory'
  2073. 2. When the value of variable `org-agenda-files' is a single file, any
  2074. relative paths in this file will be taken as relative to
  2075. `org-directory'."
  2076. :group 'org-refile
  2077. :group 'org-capture
  2078. :type 'directory)
  2079. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2080. "Default target for storing notes.
  2081. Used as a fall back file for org-capture.el, for templates that
  2082. do not specify a target file."
  2083. :group 'org-refile
  2084. :group 'org-capture
  2085. :type 'file)
  2086. (defcustom org-goto-interface 'outline
  2087. "The default interface to be used for `org-goto'.
  2088. Allowed values are:
  2089. outline The interface shows an outline of the relevant file
  2090. and the correct heading is found by moving through
  2091. the outline or by searching with incremental search.
  2092. outline-path-completion Headlines in the current buffer are offered via
  2093. completion. This is the interface also used by
  2094. the refile command."
  2095. :group 'org-refile
  2096. :type '(choice
  2097. (const :tag "Outline" outline)
  2098. (const :tag "Outline-path-completion" outline-path-completion)))
  2099. (defcustom org-goto-max-level 5
  2100. "Maximum target level when running `org-goto' with refile interface."
  2101. :group 'org-refile
  2102. :type 'integer)
  2103. (defcustom org-reverse-note-order nil
  2104. "Non-nil means store new notes at the beginning of a file or entry.
  2105. When nil, new notes will be filed to the end of a file or entry.
  2106. This can also be a list with cons cells of regular expressions that
  2107. are matched against file names, and values."
  2108. :group 'org-capture
  2109. :group 'org-refile
  2110. :type '(choice
  2111. (const :tag "Reverse always" t)
  2112. (const :tag "Reverse never" nil)
  2113. (repeat :tag "By file name regexp"
  2114. (cons regexp boolean))))
  2115. (defcustom org-log-refile nil
  2116. "Information to record when a task is refiled.
  2117. Possible values are:
  2118. nil Don't add anything
  2119. time Add a time stamp to the task
  2120. note Prompt for a note and add it with template `org-log-note-headings'
  2121. This option can also be set with on a per-file-basis with
  2122. #+STARTUP: nologrefile
  2123. #+STARTUP: logrefile
  2124. #+STARTUP: lognoterefile
  2125. You can have local logging settings for a subtree by setting the LOGGING
  2126. property to one or more of these keywords.
  2127. When bulk-refiling from the agenda, the value `note' is forbidden and
  2128. will temporarily be changed to `time'."
  2129. :group 'org-refile
  2130. :group 'org-progress
  2131. :version "24.1"
  2132. :type '(choice
  2133. (const :tag "No logging" nil)
  2134. (const :tag "Record timestamp" time)
  2135. (const :tag "Record timestamp with note." note)))
  2136. (defcustom org-refile-targets nil
  2137. "Targets for refiling entries with `\\[org-refile]'.
  2138. This is a list of cons cells. Each cell contains:
  2139. - a specification of the files to be considered, either a list of files,
  2140. or a symbol whose function or variable value will be used to retrieve
  2141. a file name or a list of file names. If you use `org-agenda-files' for
  2142. that, all agenda files will be scanned for targets. Nil means consider
  2143. headings in the current buffer.
  2144. - A specification of how to find candidate refile targets. This may be
  2145. any of:
  2146. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2147. This tag has to be present in all target headlines, inheritance will
  2148. not be considered.
  2149. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2150. todo keyword.
  2151. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2152. headlines that are refiling targets.
  2153. - a cons cell (:level . N). Any headline of level N is considered a target.
  2154. Note that, when `org-odd-levels-only' is set, level corresponds to
  2155. order in hierarchy, not to the number of stars.
  2156. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2157. Note that, when `org-odd-levels-only' is set, level corresponds to
  2158. order in hierarchy, not to the number of stars.
  2159. Each element of this list generates a set of possible targets.
  2160. The union of these sets is presented (with completion) to
  2161. the user by `org-refile'.
  2162. You can set the variable `org-refile-target-verify-function' to a function
  2163. to verify each headline found by the simple criteria above.
  2164. When this variable is nil, all top-level headlines in the current buffer
  2165. are used, equivalent to the value `((nil . (:level . 1))'."
  2166. :group 'org-refile
  2167. :type '(repeat
  2168. (cons
  2169. (choice :value org-agenda-files
  2170. (const :tag "All agenda files" org-agenda-files)
  2171. (const :tag "Current buffer" nil)
  2172. (function) (variable) (file))
  2173. (choice :tag "Identify target headline by"
  2174. (cons :tag "Specific tag" (const :value :tag) (string))
  2175. (cons :tag "TODO keyword" (const :value :todo) (string))
  2176. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2177. (cons :tag "Level number" (const :value :level) (integer))
  2178. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2179. (defcustom org-refile-target-verify-function nil
  2180. "Function to verify if the headline at point should be a refile target.
  2181. The function will be called without arguments, with point at the
  2182. beginning of the headline. It should return t and leave point
  2183. where it is if the headline is a valid target for refiling.
  2184. If the target should not be selected, the function must return nil.
  2185. In addition to this, it may move point to a place from where the search
  2186. should be continued. For example, the function may decide that the entire
  2187. subtree of the current entry should be excluded and move point to the end
  2188. of the subtree."
  2189. :group 'org-refile
  2190. :type '(choice
  2191. (const nil)
  2192. (function)))
  2193. (defcustom org-refile-use-cache nil
  2194. "Non-nil means cache refile targets to speed up the process.
  2195. \\<org-mode-map>\
  2196. The cache for a particular file will be updated automatically when
  2197. the buffer has been killed, or when any of the marker used for flagging
  2198. refile targets no longer points at a live buffer.
  2199. If you have added new entries to a buffer that might themselves be targets,
  2200. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2201. if you find that easier, \
  2202. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2203. \\[org-refile]'."
  2204. :group 'org-refile
  2205. :version "24.1"
  2206. :type 'boolean)
  2207. (defcustom org-refile-use-outline-path nil
  2208. "Non-nil means provide refile targets as paths.
  2209. So a level 3 headline will be available as level1/level2/level3.
  2210. When the value is `file', also include the file name (without directory)
  2211. into the path. In this case, you can also stop the completion after
  2212. the file name, to get entries inserted as top level in the file.
  2213. When `full-file-path', include the full file path.
  2214. When `buffer-name', use the buffer name."
  2215. :group 'org-refile
  2216. :type '(choice
  2217. (const :tag "Not" nil)
  2218. (const :tag "Yes" t)
  2219. (const :tag "Start with file name" file)
  2220. (const :tag "Start with full file path" full-file-path)
  2221. (const :tag "Start with buffer name" buffer-name)))
  2222. (defcustom org-outline-path-complete-in-steps t
  2223. "Non-nil means complete the outline path in hierarchical steps.
  2224. When Org uses the refile interface to select an outline path (see
  2225. `org-refile-use-outline-path'), the completion of the path can be
  2226. done in a single go, or it can be done in steps down the headline
  2227. hierarchy. Going in steps is probably the best if you do not use
  2228. a special completion package like `ido' or `icicles'. However,
  2229. when using these packages, going in one step can be very fast,
  2230. while still showing the whole path to the entry."
  2231. :group 'org-refile
  2232. :type 'boolean)
  2233. (defcustom org-refile-allow-creating-parent-nodes nil
  2234. "Non-nil means allow the creation of new nodes as refile targets.
  2235. New nodes are then created by adding \"/new node name\" to the completion
  2236. of an existing node. When the value of this variable is `confirm',
  2237. new node creation must be confirmed by the user (recommended).
  2238. When nil, the completion must match an existing entry.
  2239. Note that, if the new heading is not seen by the criteria
  2240. listed in `org-refile-targets', multiple instances of the same
  2241. heading would be created by trying again to file under the new
  2242. heading."
  2243. :group 'org-refile
  2244. :type '(choice
  2245. (const :tag "Never" nil)
  2246. (const :tag "Always" t)
  2247. (const :tag "Prompt for confirmation" confirm)))
  2248. (defcustom org-refile-active-region-within-subtree nil
  2249. "Non-nil means also refile active region within a subtree.
  2250. By default `org-refile' doesn't allow refiling regions if they
  2251. don't contain a set of subtrees, but it might be convenient to
  2252. do so sometimes: in that case, the first line of the region is
  2253. converted to a headline before refiling."
  2254. :group 'org-refile
  2255. :version "24.1"
  2256. :type 'boolean)
  2257. (defgroup org-todo nil
  2258. "Options concerning TODO items in Org mode."
  2259. :tag "Org TODO"
  2260. :group 'org)
  2261. (defgroup org-progress nil
  2262. "Options concerning Progress logging in Org mode."
  2263. :tag "Org Progress"
  2264. :group 'org-time)
  2265. (defvar org-todo-interpretation-widgets
  2266. '((:tag "Sequence (cycling hits every state)" sequence)
  2267. (:tag "Type (cycling directly to DONE)" type))
  2268. "The available interpretation symbols for customizing `org-todo-keywords'.
  2269. Interested libraries should add to this list.")
  2270. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2271. "List of TODO entry keyword sequences and their interpretation.
  2272. \\<org-mode-map>This is a list of sequences.
  2273. Each sequence starts with a symbol, either `sequence' or `type',
  2274. indicating if the keywords should be interpreted as a sequence of
  2275. action steps, or as different types of TODO items. The first
  2276. keywords are states requiring action - these states will select a headline
  2277. for inclusion into the global TODO list Org produces. If one of the
  2278. \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2279. signify that no further action is necessary. If \"|\" is not found,
  2280. the last keyword is treated as the only DONE state of the sequence.
  2281. The command `\\[org-todo]' cycles an entry through these states, and one
  2282. additional state where no keyword is present. For details about this
  2283. cycling, see the manual.
  2284. TODO keywords and interpretation can also be set on a per-file basis with
  2285. the special #+SEQ_TODO and #+TYP_TODO lines.
  2286. Each keyword can optionally specify a character for fast state selection
  2287. \(in combination with the variable `org-use-fast-todo-selection')
  2288. and specifiers for state change logging, using the same syntax that
  2289. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2290. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2291. indicates to record a time stamp each time this state is selected.
  2292. Each keyword may also specify if a timestamp or a note should be
  2293. recorded when entering or leaving the state, by adding additional
  2294. characters in the parenthesis after the keyword. This looks like this:
  2295. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2296. record only the time of the state change. With X and Y being either
  2297. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2298. Y when leaving the state if and only if the *target* state does not
  2299. define X. You may omit any of the fast-selection key or X or /Y,
  2300. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2301. For backward compatibility, this variable may also be just a list
  2302. of keywords. In this case the interpretation (sequence or type) will be
  2303. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2304. :group 'org-todo
  2305. :group 'org-keywords
  2306. :type '(choice
  2307. (repeat :tag "Old syntax, just keywords"
  2308. (string :tag "Keyword"))
  2309. (repeat :tag "New syntax"
  2310. (cons
  2311. (choice
  2312. :tag "Interpretation"
  2313. ;;Quick and dirty way to see
  2314. ;;`org-todo-interpretations'. This takes the
  2315. ;;place of item arguments
  2316. :convert-widget
  2317. (lambda (widget)
  2318. (widget-put widget
  2319. :args (mapcar
  2320. (lambda (x)
  2321. (widget-convert
  2322. (cons 'const x)))
  2323. org-todo-interpretation-widgets))
  2324. widget))
  2325. (repeat
  2326. (string :tag "Keyword"))))))
  2327. (defvar-local org-todo-keywords-1 nil
  2328. "All TODO and DONE keywords active in a buffer.")
  2329. (defvar org-todo-keywords-for-agenda nil)
  2330. (defvar org-done-keywords-for-agenda nil)
  2331. (defvar org-todo-keyword-alist-for-agenda nil)
  2332. (defvar org-tag-alist-for-agenda nil
  2333. "Alist of all tags from all agenda files.")
  2334. (defvar org-tag-groups-alist-for-agenda nil
  2335. "Alist of all groups tags from all current agenda files.")
  2336. (defvar-local org-tag-groups-alist nil)
  2337. (defvar org-agenda-contributing-files nil)
  2338. (defvar-local org-current-tag-alist nil
  2339. "Alist of all tag groups in current buffer.
  2340. This variable takes into consideration `org-tag-alist',
  2341. `org-tag-persistent-alist' and TAGS keywords in the buffer.")
  2342. (defvar-local org-not-done-keywords nil)
  2343. (defvar-local org-done-keywords nil)
  2344. (defvar-local org-todo-heads nil)
  2345. (defvar-local org-todo-sets nil)
  2346. (defvar-local org-todo-log-states nil)
  2347. (defvar-local org-todo-kwd-alist nil)
  2348. (defvar-local org-todo-key-alist nil)
  2349. (defvar-local org-todo-key-trigger nil)
  2350. (defcustom org-todo-interpretation 'sequence
  2351. "Controls how TODO keywords are interpreted.
  2352. This variable is in principle obsolete and is only used for
  2353. backward compatibility, if the interpretation of todo keywords is
  2354. not given already in `org-todo-keywords'. See that variable for
  2355. more information."
  2356. :group 'org-todo
  2357. :group 'org-keywords
  2358. :type '(choice (const sequence)
  2359. (const type)))
  2360. (defcustom org-use-fast-todo-selection t
  2361. "\\<org-mode-map>\
  2362. Non-nil means use the fast todo selection scheme with `\\[org-todo]'.
  2363. This variable describes if and under what circumstances the cycling
  2364. mechanism for TODO keywords will be replaced by a single-key, direct
  2365. selection scheme.
  2366. When nil, fast selection is never used.
  2367. When the symbol `prefix', it will be used when `org-todo' is called
  2368. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2369. in an Org buffer, and
  2370. `\\[universal-argument] t' in an agenda buffer.
  2371. When t, fast selection is used by default. In this case, the prefix
  2372. argument forces cycling instead.
  2373. In all cases, the special interface is only used if access keys have
  2374. actually been assigned by the user, i.e. if keywords in the configuration
  2375. are followed by a letter in parenthesis, like TODO(t)."
  2376. :group 'org-todo
  2377. :type '(choice
  2378. (const :tag "Never" nil)
  2379. (const :tag "By default" t)
  2380. (const :tag "Only with C-u C-c C-t" prefix)))
  2381. (defcustom org-provide-todo-statistics t
  2382. "Non-nil means update todo statistics after insert and toggle.
  2383. ALL-HEADLINES means update todo statistics by including headlines
  2384. with no TODO keyword as well, counting them as not done.
  2385. A list of TODO keywords means the same, but skip keywords that are
  2386. not in this list.
  2387. When set to a list of two lists, the first list contains keywords
  2388. to consider as TODO keywords, the second list contains keywords
  2389. to consider as DONE keywords.
  2390. When this is set, todo statistics is updated in the parent of the
  2391. current entry each time a todo state is changed."
  2392. :group 'org-todo
  2393. :type '(choice
  2394. (const :tag "Yes, only for TODO entries" t)
  2395. (const :tag "Yes, including all entries" all-headlines)
  2396. (repeat :tag "Yes, for TODOs in this list"
  2397. (string :tag "TODO keyword"))
  2398. (list :tag "Yes, for TODOs and DONEs in these lists"
  2399. (repeat (string :tag "TODO keyword"))
  2400. (repeat (string :tag "DONE keyword")))
  2401. (other :tag "No TODO statistics" nil)))
  2402. (defcustom org-hierarchical-todo-statistics t
  2403. "Non-nil means TODO statistics covers just direct children.
  2404. When nil, all entries in the subtree are considered.
  2405. This has only an effect if `org-provide-todo-statistics' is set.
  2406. To set this to nil for only a single subtree, use a COOKIE_DATA
  2407. property and include the word \"recursive\" into the value."
  2408. :group 'org-todo
  2409. :type 'boolean)
  2410. (defcustom org-after-todo-state-change-hook nil
  2411. "Hook which is run after the state of a TODO item was changed.
  2412. The new state (a string with a TODO keyword, or nil) is available in the
  2413. Lisp variable `org-state'."
  2414. :group 'org-todo
  2415. :type 'hook)
  2416. (defvar org-blocker-hook nil
  2417. "Hook for functions that are allowed to block a state change.
  2418. Functions in this hook should not modify the buffer.
  2419. Each function gets as its single argument a property list,
  2420. see `org-trigger-hook' for more information about this list.
  2421. If any of the functions in this hook returns nil, the state change
  2422. is blocked.")
  2423. (defvar org-trigger-hook nil
  2424. "Hook for functions that are triggered by a state change.
  2425. Each function gets as its single argument a property list with at
  2426. least the following elements:
  2427. (:type type-of-change :position pos-at-entry-start
  2428. :from old-state :to new-state)
  2429. Depending on the type, more properties may be present.
  2430. This mechanism is currently implemented for:
  2431. TODO state changes
  2432. ------------------
  2433. :type todo-state-change
  2434. :from previous state (keyword as a string), or nil, or a symbol
  2435. `todo' or `done', to indicate the general type of state.
  2436. :to new state, like in :from")
  2437. (defcustom org-enforce-todo-dependencies nil
  2438. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2439. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2440. be blocked if any prior sibling is not yet done.
  2441. Finally, if the parent is blocked because of ordered siblings of its own,
  2442. the child will also be blocked."
  2443. :set (lambda (var val)
  2444. (set var val)
  2445. (if val
  2446. (add-hook 'org-blocker-hook
  2447. 'org-block-todo-from-children-or-siblings-or-parent)
  2448. (remove-hook 'org-blocker-hook
  2449. 'org-block-todo-from-children-or-siblings-or-parent)))
  2450. :group 'org-todo
  2451. :type 'boolean)
  2452. (defcustom org-enforce-todo-checkbox-dependencies nil
  2453. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2454. When this is nil, checkboxes have no influence on switching TODO states.
  2455. When non-nil, you first need to check off all check boxes before the TODO
  2456. entry can be switched to DONE.
  2457. This variable needs to be set before org.el is loaded, and you need to
  2458. restart Emacs after a change to make the change effective. The only way
  2459. to change is while Emacs is running is through the customize interface."
  2460. :set (lambda (var val)
  2461. (set var val)
  2462. (if val
  2463. (add-hook 'org-blocker-hook
  2464. 'org-block-todo-from-checkboxes)
  2465. (remove-hook 'org-blocker-hook
  2466. 'org-block-todo-from-checkboxes)))
  2467. :group 'org-todo
  2468. :type 'boolean)
  2469. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2470. "Non-nil means inserting a TODO heading is treated as state change.
  2471. So when the command `\\[org-insert-todo-heading]' is used, state change
  2472. logging will apply if appropriate. When nil, the new TODO item will
  2473. be inserted directly, and no logging will take place."
  2474. :group 'org-todo
  2475. :type 'boolean)
  2476. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2477. "Non-nil means switching TODO states with S-cursor counts as state change.
  2478. This is the default behavior. However, setting this to nil allows a
  2479. convenient way to select a TODO state and bypass any logging associated
  2480. with that."
  2481. :group 'org-todo
  2482. :type 'boolean)
  2483. (defcustom org-todo-state-tags-triggers nil
  2484. "Tag changes that should be triggered by TODO state changes.
  2485. This is a list. Each entry is
  2486. (state-change (tag . flag) .......)
  2487. State-change can be a string with a state, and empty string to indicate the
  2488. state that has no TODO keyword, or it can be one of the symbols `todo'
  2489. or `done', meaning any not-done or done state, respectively."
  2490. :group 'org-todo
  2491. :group 'org-tags
  2492. :type '(repeat
  2493. (cons (choice :tag "When changing to"
  2494. (const :tag "Not-done state" todo)
  2495. (const :tag "Done state" done)
  2496. (string :tag "State"))
  2497. (repeat
  2498. (cons :tag "Tag action"
  2499. (string :tag "Tag")
  2500. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2501. (defcustom org-log-done nil
  2502. "Information to record when a task moves to the DONE state.
  2503. Possible values are:
  2504. nil Don't add anything, just change the keyword
  2505. time Add a time stamp to the task
  2506. note Prompt for a note and add it with template `org-log-note-headings'
  2507. This option can also be set with on a per-file-basis with
  2508. #+STARTUP: nologdone
  2509. #+STARTUP: logdone
  2510. #+STARTUP: lognotedone
  2511. You can have local logging settings for a subtree by setting the LOGGING
  2512. property to one or more of these keywords."
  2513. :group 'org-todo
  2514. :group 'org-progress
  2515. :type '(choice
  2516. (const :tag "No logging" nil)
  2517. (const :tag "Record CLOSED timestamp" time)
  2518. (const :tag "Record CLOSED timestamp with note." note)))
  2519. ;; Normalize old uses of org-log-done.
  2520. (cond
  2521. ((eq org-log-done t) (setq org-log-done 'time))
  2522. ((and (listp org-log-done) (memq 'done org-log-done))
  2523. (setq org-log-done 'note)))
  2524. (defcustom org-log-reschedule nil
  2525. "Information to record when the scheduling date of a tasks is modified.
  2526. Possible values are:
  2527. nil Don't add anything, just change the date
  2528. time Add a time stamp to the task
  2529. note Prompt for a note and add it with template `org-log-note-headings'
  2530. This option can also be set with on a per-file-basis with
  2531. #+STARTUP: nologreschedule
  2532. #+STARTUP: logreschedule
  2533. #+STARTUP: lognotereschedule"
  2534. :group 'org-todo
  2535. :group 'org-progress
  2536. :type '(choice
  2537. (const :tag "No logging" nil)
  2538. (const :tag "Record timestamp" time)
  2539. (const :tag "Record timestamp with note." note)))
  2540. (defcustom org-log-redeadline nil
  2541. "Information to record when the deadline date of a tasks is modified.
  2542. Possible values are:
  2543. nil Don't add anything, just change the date
  2544. time Add a time stamp to the task
  2545. note Prompt for a note and add it with template `org-log-note-headings'
  2546. This option can also be set with on a per-file-basis with
  2547. #+STARTUP: nologredeadline
  2548. #+STARTUP: logredeadline
  2549. #+STARTUP: lognoteredeadline
  2550. You can have local logging settings for a subtree by setting the LOGGING
  2551. property to one or more of these keywords."
  2552. :group 'org-todo
  2553. :group 'org-progress
  2554. :type '(choice
  2555. (const :tag "No logging" nil)
  2556. (const :tag "Record timestamp" time)
  2557. (const :tag "Record timestamp with note." note)))
  2558. (defcustom org-log-note-clock-out nil
  2559. "Non-nil means record a note when clocking out of an item.
  2560. This can also be configured on a per-file basis by adding one of
  2561. the following lines anywhere in the buffer:
  2562. #+STARTUP: lognoteclock-out
  2563. #+STARTUP: nolognoteclock-out"
  2564. :group 'org-todo
  2565. :group 'org-progress
  2566. :type 'boolean)
  2567. (defcustom org-log-done-with-time t
  2568. "Non-nil means the CLOSED time stamp will contain date and time.
  2569. When nil, only the date will be recorded."
  2570. :group 'org-progress
  2571. :type 'boolean)
  2572. (defcustom org-log-note-headings
  2573. '((done . "CLOSING NOTE %t")
  2574. (state . "State %-12s from %-12S %t")
  2575. (note . "Note taken on %t")
  2576. (reschedule . "Rescheduled from %S on %t")
  2577. (delschedule . "Not scheduled, was %S on %t")
  2578. (redeadline . "New deadline from %S on %t")
  2579. (deldeadline . "Removed deadline, was %S on %t")
  2580. (refile . "Refiled on %t")
  2581. (clock-out . ""))
  2582. "Headings for notes added to entries.
  2583. The value is an alist, with the car being a symbol indicating the
  2584. note context, and the cdr is the heading to be used. The heading
  2585. may also be the empty string. The following placeholders can be
  2586. used:
  2587. %t a time stamp.
  2588. %T an active time stamp instead the default inactive one
  2589. %d a short-format time stamp.
  2590. %D an active short-format time stamp.
  2591. %s the new TODO state or time stamp (inactive), in double quotes.
  2592. %S the old TODO state or time stamp (inactive), in double quotes.
  2593. %u the user name.
  2594. %U full user name.
  2595. In fact, it is not a good idea to change the `state' entry,
  2596. because Agenda Log mode depends on the format of these entries."
  2597. :group 'org-todo
  2598. :group 'org-progress
  2599. :type '(list :greedy t
  2600. (cons (const :tag "Heading when closing an item" done) string)
  2601. (cons (const :tag
  2602. "Heading when changing todo state (todo sequence only)"
  2603. state) string)
  2604. (cons (const :tag "Heading when just taking a note" note) string)
  2605. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2606. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2607. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2608. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2609. (cons (const :tag "Heading when refiling" refile) string)
  2610. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2611. (unless (assq 'note org-log-note-headings)
  2612. (push '(note . "%t") org-log-note-headings))
  2613. (defcustom org-log-into-drawer nil
  2614. "Non-nil means insert state change notes and time stamps into a drawer.
  2615. When nil, state changes notes will be inserted after the headline and
  2616. any scheduling and clock lines, but not inside a drawer.
  2617. The value of this variable should be the name of the drawer to use.
  2618. LOGBOOK is proposed as the default drawer for this purpose, you can
  2619. also set this to a string to define the drawer of your choice.
  2620. A value of t is also allowed, representing \"LOGBOOK\".
  2621. A value of t or nil can also be set with on a per-file-basis with
  2622. #+STARTUP: logdrawer
  2623. #+STARTUP: nologdrawer
  2624. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2625. will be ignored.
  2626. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2627. a subtree.
  2628. Do not check directly this variable in a Lisp program. Call
  2629. function `org-log-into-drawer' instead."
  2630. :group 'org-todo
  2631. :group 'org-progress
  2632. :type '(choice
  2633. (const :tag "Not into a drawer" nil)
  2634. (const :tag "LOGBOOK" t)
  2635. (string :tag "Other")))
  2636. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2637. (defun org-log-into-drawer ()
  2638. "Name of the log drawer, as a string, or nil.
  2639. This is the value of `org-log-into-drawer'. However, if the
  2640. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2641. be used instead of the default value."
  2642. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2643. (cond ((equal p "nil") nil)
  2644. ((equal p "t") "LOGBOOK")
  2645. ((stringp p) p)
  2646. (p "LOGBOOK")
  2647. ((stringp org-log-into-drawer) org-log-into-drawer)
  2648. (org-log-into-drawer "LOGBOOK"))))
  2649. (defcustom org-log-state-notes-insert-after-drawers nil
  2650. "Non-nil means insert state change notes after any drawers in entry.
  2651. Only the drawers that *immediately* follow the headline and the
  2652. deadline/scheduled line are skipped.
  2653. When nil, insert notes right after the heading and perhaps the line
  2654. with deadline/scheduling if present.
  2655. This variable will have no effect if `org-log-into-drawer' is
  2656. set."
  2657. :group 'org-todo
  2658. :group 'org-progress
  2659. :type 'boolean)
  2660. (defcustom org-log-states-order-reversed t
  2661. "Non-nil means the latest state note will be directly after heading.
  2662. When nil, the state change notes will be ordered according to time.
  2663. This option can also be set with on a per-file-basis with
  2664. #+STARTUP: logstatesreversed
  2665. #+STARTUP: nologstatesreversed"
  2666. :group 'org-todo
  2667. :group 'org-progress
  2668. :type 'boolean)
  2669. (defcustom org-todo-repeat-to-state nil
  2670. "The TODO state to which a repeater should return the repeating task.
  2671. By default this is the first task in a TODO sequence, or the previous state
  2672. in a TODO_TYP set. But you can specify another task here.
  2673. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2674. :group 'org-todo
  2675. :version "24.1"
  2676. :type '(choice (const :tag "Head of sequence" nil)
  2677. (string :tag "Specific state")))
  2678. (defcustom org-log-repeat 'time
  2679. "Non-nil means record moving through the DONE state when triggering repeat.
  2680. An auto-repeating task is immediately switched back to TODO when
  2681. marked DONE. If you are not logging state changes (by adding \"@\"
  2682. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2683. record a closing note, there will be no record of the task moving
  2684. through DONE. This variable forces taking a note anyway.
  2685. nil Don't force a record
  2686. time Record a time stamp
  2687. note Prompt for a note and add it with template `org-log-note-headings'
  2688. This option can also be set with on a per-file-basis with
  2689. #+STARTUP: nologrepeat
  2690. #+STARTUP: logrepeat
  2691. #+STARTUP: lognoterepeat
  2692. You can have local logging settings for a subtree by setting the LOGGING
  2693. property to one or more of these keywords."
  2694. :group 'org-todo
  2695. :group 'org-progress
  2696. :type '(choice
  2697. (const :tag "Don't force a record" nil)
  2698. (const :tag "Force recording the DONE state" time)
  2699. (const :tag "Force recording a note with the DONE state" note)))
  2700. (defgroup org-priorities nil
  2701. "Priorities in Org mode."
  2702. :tag "Org Priorities"
  2703. :group 'org-todo)
  2704. (defcustom org-enable-priority-commands t
  2705. "Non-nil means priority commands are active.
  2706. When nil, these commands will be disabled, so that you never accidentally
  2707. set a priority."
  2708. :group 'org-priorities
  2709. :type 'boolean)
  2710. (defcustom org-highest-priority ?A
  2711. "The highest priority of TODO items. A character like ?A, ?B etc.
  2712. Must have a smaller ASCII number than `org-lowest-priority'."
  2713. :group 'org-priorities
  2714. :type 'character)
  2715. (defcustom org-lowest-priority ?C
  2716. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2717. Must have a larger ASCII number than `org-highest-priority'."
  2718. :group 'org-priorities
  2719. :type 'character)
  2720. (defcustom org-default-priority ?B
  2721. "The default priority of TODO items.
  2722. This is the priority an item gets if no explicit priority is given.
  2723. When starting to cycle on an empty priority the first step in the cycle
  2724. depends on `org-priority-start-cycle-with-default'. The resulting first
  2725. step priority must not exceed the range from `org-highest-priority' to
  2726. `org-lowest-priority' which means that `org-default-priority' has to be
  2727. in this range exclusive or inclusive the range boundaries. Else the
  2728. first step refuses to set the default and the second will fall back
  2729. to (depending on the command used) the highest or lowest priority."
  2730. :group 'org-priorities
  2731. :type 'character)
  2732. (defcustom org-priority-start-cycle-with-default t
  2733. "Non-nil means start with default priority when starting to cycle.
  2734. When this is nil, the first step in the cycle will be (depending on the
  2735. command used) one higher or lower than the default priority.
  2736. See also `org-default-priority'."
  2737. :group 'org-priorities
  2738. :type 'boolean)
  2739. (defcustom org-get-priority-function nil
  2740. "Function to extract the priority from a string.
  2741. The string is normally the headline. If this is nil Org computes the
  2742. priority from the priority cookie like [#A] in the headline. It returns
  2743. an integer, increasing by 1000 for each priority level.
  2744. The user can set a different function here, which should take a string
  2745. as an argument and return the numeric priority."
  2746. :group 'org-priorities
  2747. :version "24.1"
  2748. :type '(choice
  2749. (const nil)
  2750. (function)))
  2751. (defgroup org-time nil
  2752. "Options concerning time stamps and deadlines in Org mode."
  2753. :tag "Org Time"
  2754. :group 'org)
  2755. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2756. "Number of minutes to round time stamps to.
  2757. \\<org-mode-map>\
  2758. These are two values, the first applies when first creating a time stamp.
  2759. The second applies when changing it with the commands `S-up' and `S-down'.
  2760. When changing the time stamp, this means that it will change in steps
  2761. of N minutes, as given by the second value.
  2762. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2763. numbers should be factors of 60, so for example 5, 10, 15.
  2764. When this is larger than 1, you can still force an exact time stamp by using
  2765. a double prefix argument to a time stamp command like \
  2766. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2767. and by using a prefix arg to `S-up/down' to specify the exact number
  2768. of minutes to shift."
  2769. :group 'org-time
  2770. :get (lambda (var) ; Make sure both elements are there
  2771. (if (integerp (default-value var))
  2772. (list (default-value var) 5)
  2773. (default-value var)))
  2774. :type '(list
  2775. (integer :tag "when inserting times")
  2776. (integer :tag "when modifying times")))
  2777. ;; Normalize old customizations of this variable.
  2778. (when (integerp org-time-stamp-rounding-minutes)
  2779. (setq org-time-stamp-rounding-minutes
  2780. (list org-time-stamp-rounding-minutes
  2781. org-time-stamp-rounding-minutes)))
  2782. (defcustom org-display-custom-times nil
  2783. "Non-nil means overlay custom formats over all time stamps.
  2784. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2785. To turn this on on a per-file basis, insert anywhere in the file:
  2786. #+STARTUP: customtime"
  2787. :group 'org-time
  2788. :set 'set-default
  2789. :type 'sexp)
  2790. (make-variable-buffer-local 'org-display-custom-times)
  2791. (defcustom org-time-stamp-custom-formats
  2792. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2793. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2794. These are overlaid over the default ISO format if the variable
  2795. `org-display-custom-times' is set. Time like %H:%M should be at the
  2796. end of the second format. The custom formats are also honored by export
  2797. commands, if custom time display is turned on at the time of export."
  2798. :group 'org-time
  2799. :type 'sexp)
  2800. (defun org-time-stamp-format (&optional long inactive)
  2801. "Get the right format for a time string."
  2802. (let ((f (if long (cdr org-time-stamp-formats)
  2803. (car org-time-stamp-formats))))
  2804. (if inactive
  2805. (concat "[" (substring f 1 -1) "]")
  2806. f)))
  2807. (defcustom org-deadline-warning-days 14
  2808. "Number of days before expiration during which a deadline becomes active.
  2809. This variable governs the display in sparse trees and in the agenda.
  2810. When 0 or negative, it means use this number (the absolute value of it)
  2811. even if a deadline has a different individual lead time specified.
  2812. Custom commands can set this variable in the options section."
  2813. :group 'org-time
  2814. :group 'org-agenda-daily/weekly
  2815. :type 'integer)
  2816. (defcustom org-scheduled-delay-days 0
  2817. "Number of days before a scheduled item becomes active.
  2818. This variable governs the display in sparse trees and in the agenda.
  2819. The default value (i.e. 0) means: don't delay scheduled item.
  2820. When negative, it means use this number (the absolute value of it)
  2821. even if a scheduled item has a different individual delay time
  2822. specified.
  2823. Custom commands can set this variable in the options section."
  2824. :group 'org-time
  2825. :group 'org-agenda-daily/weekly
  2826. :version "24.4"
  2827. :package-version '(Org . "8.0")
  2828. :type 'integer)
  2829. (defcustom org-read-date-prefer-future t
  2830. "Non-nil means assume future for incomplete date input from user.
  2831. This affects the following situations:
  2832. 1. The user gives a month but not a year.
  2833. For example, if it is April and you enter \"feb 2\", this will be read
  2834. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2835. 2. The user gives a day, but no month.
  2836. For example, if today is the 15th, and you enter \"3\", Org will read
  2837. this as the third of *next* month. However, if you enter \"17\",
  2838. it will be considered as *this* month.
  2839. If you set this variable to the symbol `time', then also the following
  2840. will work:
  2841. 3. If the user gives a time.
  2842. If the time is before now, it will be interpreted as tomorrow.
  2843. Currently none of this works for ISO week specifications.
  2844. When this option is nil, the current day, month and year will always be
  2845. used as defaults.
  2846. See also `org-agenda-jump-prefer-future'."
  2847. :group 'org-time
  2848. :type '(choice
  2849. (const :tag "Never" nil)
  2850. (const :tag "Check month and day" t)
  2851. (const :tag "Check month, day, and time" time)))
  2852. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2853. "Should the agenda jump command prefer the future for incomplete dates?
  2854. The default is to do the same as configured in `org-read-date-prefer-future'.
  2855. But you can also set a deviating value here.
  2856. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2857. :group 'org-agenda
  2858. :group 'org-time
  2859. :version "24.1"
  2860. :type '(choice
  2861. (const :tag "Use org-read-date-prefer-future"
  2862. org-read-date-prefer-future)
  2863. (const :tag "Never" nil)
  2864. (const :tag "Always" t)))
  2865. (defcustom org-read-date-force-compatible-dates t
  2866. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2867. Depending on the system Emacs is running on, certain dates cannot
  2868. be represented with the type used internally to represent time.
  2869. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2870. correctly. Some systems allow for earlier dates, some for later,
  2871. some for both. One way to find out it to insert any date into an
  2872. Org buffer, putting the cursor on the year and hitting S-up and
  2873. S-down to test the range.
  2874. When this variable is set to t, the date/time prompt will not let
  2875. you specify dates outside the 1970-2037 range, so it is certain that
  2876. these dates will work in whatever version of Emacs you are
  2877. running, and also that you can move a file from one Emacs implementation
  2878. to another. WHenever Org is forcing the year for you, it will display
  2879. a message and beep.
  2880. When this variable is nil, Org will check if the date is
  2881. representable in the specific Emacs implementation you are using.
  2882. If not, it will force a year, usually the current year, and beep
  2883. to remind you. Currently this setting is not recommended because
  2884. the likelihood that you will open your Org files in an Emacs that
  2885. has limited date range is not negligible.
  2886. A workaround for this problem is to use diary sexp dates for time
  2887. stamps outside of this range."
  2888. :group 'org-time
  2889. :version "24.1"
  2890. :type 'boolean)
  2891. (defcustom org-read-date-display-live t
  2892. "Non-nil means display current interpretation of date prompt live.
  2893. This display will be in an overlay, in the minibuffer."
  2894. :group 'org-time
  2895. :type 'boolean)
  2896. (defcustom org-read-date-popup-calendar t
  2897. "Non-nil means pop up a calendar when prompting for a date.
  2898. In the calendar, the date can be selected with mouse-1. However, the
  2899. minibuffer will also be active, and you can simply enter the date as well.
  2900. When nil, only the minibuffer will be available."
  2901. :group 'org-time
  2902. :type 'boolean)
  2903. (defvaralias 'org-popup-calendar-for-date-prompt
  2904. 'org-read-date-popup-calendar)
  2905. (defcustom org-extend-today-until 0
  2906. "The hour when your day really ends. Must be an integer.
  2907. This has influence for the following applications:
  2908. - When switching the agenda to \"today\". It it is still earlier than
  2909. the time given here, the day recognized as TODAY is actually yesterday.
  2910. - When a date is read from the user and it is still before the time given
  2911. here, the current date and time will be assumed to be yesterday, 23:59.
  2912. Also, timestamps inserted in capture templates follow this rule.
  2913. IMPORTANT: This is a feature whose implementation is and likely will
  2914. remain incomplete. Really, it is only here because past midnight seems to
  2915. be the favorite working time of John Wiegley :-)"
  2916. :group 'org-time
  2917. :type 'integer)
  2918. (defcustom org-use-effective-time nil
  2919. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2920. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2921. \"effective time\" of any timestamps between midnight and 8am will be
  2922. 23:59 of the previous day."
  2923. :group 'org-time
  2924. :version "24.1"
  2925. :type 'boolean)
  2926. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2927. "When non-nil, use the last clock out time for `org-todo'.
  2928. Note that this option has precedence over the combined use of
  2929. `org-use-effective-time' and `org-extend-today-until'."
  2930. :group 'org-time
  2931. :version "24.4"
  2932. :package-version '(Org . "8.0")
  2933. :type 'boolean)
  2934. (defcustom org-edit-timestamp-down-means-later nil
  2935. "Non-nil means S-down will increase the time in a time stamp.
  2936. When nil, S-up will increase."
  2937. :group 'org-time
  2938. :type 'boolean)
  2939. (defcustom org-calendar-follow-timestamp-change t
  2940. "Non-nil means make the calendar window follow timestamp changes.
  2941. When a timestamp is modified and the calendar window is visible, it will be
  2942. moved to the new date."
  2943. :group 'org-time
  2944. :type 'boolean)
  2945. (defgroup org-tags nil
  2946. "Options concerning tags in Org mode."
  2947. :tag "Org Tags"
  2948. :group 'org)
  2949. (defcustom org-tag-alist nil
  2950. "Default tags available in Org files.
  2951. The value of this variable is an alist. Associations either:
  2952. (TAG)
  2953. (TAG . SELECT)
  2954. (SPECIAL)
  2955. where TAG is a tag as a string, SELECT is character, used to
  2956. select that tag through the fast tag selection interface, and
  2957. SPECIAL is one of the following keywords: `:startgroup',
  2958. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2959. `:newline'. These keywords are used to define a hierarchy of
  2960. tags. See manual for details.
  2961. When this variable is nil, Org mode bases tag input on what is
  2962. already in the buffer. The value can be overridden locally by
  2963. using a TAGS keyword, e.g.,
  2964. #+TAGS: tag1 tag2
  2965. See also `org-tag-persistent-alist' to sidestep this behavior."
  2966. :group 'org-tags
  2967. :type '(repeat
  2968. (choice
  2969. (cons :tag "Tag with key"
  2970. (string :tag "Tag name")
  2971. (character :tag "Access char"))
  2972. (list :tag "Tag" (string :tag "Tag name"))
  2973. (const :tag "Start radio group" (:startgroup))
  2974. (const :tag "Start tag group, non distinct" (:startgrouptag))
  2975. (const :tag "Group tags delimiter" (:grouptags))
  2976. (const :tag "End radio group" (:endgroup))
  2977. (const :tag "End tag group, non distinct" (:endgrouptag))
  2978. (const :tag "New line" (:newline)))))
  2979. (defcustom org-tag-persistent-alist nil
  2980. "Tags always available in Org files.
  2981. The value of this variable is an alist. Associations either:
  2982. (TAG)
  2983. (TAG . SELECT)
  2984. (SPECIAL)
  2985. where TAG is a tag as a string, SELECT is a character, used to
  2986. select that tag through the fast tag selection interface, and
  2987. SPECIAL is one of the following keywords: `:startgroup',
  2988. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2989. `:newline'. These keywords are used to define a hierarchy of
  2990. tags. See manual for details.
  2991. Unlike to `org-tag-alist', tags defined in this variable do not
  2992. depend on a local TAGS keyword. Instead, to disable these tags
  2993. on a per-file basis, insert anywhere in the file:
  2994. #+STARTUP: noptag"
  2995. :group 'org-tags
  2996. :type '(repeat
  2997. (choice
  2998. (cons :tag "Tag with key"
  2999. (string :tag "Tag name")
  3000. (character :tag "Access char"))
  3001. (list :tag "Tag" (string :tag "Tag name"))
  3002. (const :tag "Start radio group" (:startgroup))
  3003. (const :tag "Start tag group, non distinct" (:startgrouptag))
  3004. (const :tag "Group tags delimiter" (:grouptags))
  3005. (const :tag "End radio group" (:endgroup))
  3006. (const :tag "End tag group, non distinct" (:endgrouptag))
  3007. (const :tag "New line" (:newline)))))
  3008. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3009. "If non-nil, always offer completion for all tags of all agenda files.
  3010. Instead of customizing this variable directly, you might want to
  3011. set it locally for capture buffers, because there no list of
  3012. tags in that file can be created dynamically (there are none).
  3013. (add-hook \\='org-capture-mode-hook
  3014. (lambda ()
  3015. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3016. :group 'org-tags
  3017. :version "24.1"
  3018. :type 'boolean)
  3019. (defvar org-file-tags nil
  3020. "List of tags that can be inherited by all entries in the file.
  3021. The tags will be inherited if the variable `org-use-tag-inheritance'
  3022. says they should be.
  3023. This variable is populated from #+FILETAGS lines.")
  3024. (defcustom org-use-fast-tag-selection 'auto
  3025. "Non-nil means use fast tag selection scheme.
  3026. This is a special interface to select and deselect tags with single keys.
  3027. When nil, fast selection is never used.
  3028. When the symbol `auto', fast selection is used if and only if selection
  3029. characters for tags have been configured, either through the variable
  3030. `org-tag-alist' or through a #+TAGS line in the buffer.
  3031. When t, fast selection is always used and selection keys are assigned
  3032. automatically if necessary."
  3033. :group 'org-tags
  3034. :type '(choice
  3035. (const :tag "Always" t)
  3036. (const :tag "Never" nil)
  3037. (const :tag "When selection characters are configured" auto)))
  3038. (defcustom org-fast-tag-selection-single-key nil
  3039. "Non-nil means fast tag selection exits after first change.
  3040. When nil, you have to press RET to exit it.
  3041. During fast tag selection, you can toggle this flag with `C-c'.
  3042. This variable can also have the value `expert'. In this case, the window
  3043. displaying the tags menu is not even shown, until you press C-c again."
  3044. :group 'org-tags
  3045. :type '(choice
  3046. (const :tag "No" nil)
  3047. (const :tag "Yes" t)
  3048. (const :tag "Expert" expert)))
  3049. (defvar org-fast-tag-selection-include-todo nil
  3050. "Non-nil means fast tags selection interface will also offer TODO states.
  3051. This is an undocumented feature, you should not rely on it.")
  3052. (defcustom org-tags-column -77
  3053. "The column to which tags should be indented in a headline.
  3054. If this number is positive, it specifies the column. If it is negative,
  3055. it means that the tags should be flushright to that column. For example,
  3056. -80 works well for a normal 80 character screen.
  3057. When 0, place tags directly after headline text, with only one space in
  3058. between."
  3059. :group 'org-tags
  3060. :type 'integer)
  3061. (defcustom org-auto-align-tags t
  3062. "Non-nil keeps tags aligned when modifying headlines.
  3063. Some operations (i.e. demoting) change the length of a headline and
  3064. therefore shift the tags around. With this option turned on, after
  3065. each such operation the tags are again aligned to `org-tags-column'."
  3066. :group 'org-tags
  3067. :type 'boolean)
  3068. (defcustom org-use-tag-inheritance t
  3069. "Non-nil means tags in levels apply also for sublevels.
  3070. When nil, only the tags directly given in a specific line apply there.
  3071. This may also be a list of tags that should be inherited, or a regexp that
  3072. matches tags that should be inherited. Additional control is possible
  3073. with the variable `org-tags-exclude-from-inheritance' which gives an
  3074. explicit list of tags to be excluded from inheritance, even if the value of
  3075. `org-use-tag-inheritance' would select it for inheritance.
  3076. If this option is t, a match early-on in a tree can lead to a large
  3077. number of matches in the subtree when constructing the agenda or creating
  3078. a sparse tree. If you only want to see the first match in a tree during
  3079. a search, check out the variable `org-tags-match-list-sublevels'."
  3080. :group 'org-tags
  3081. :type '(choice
  3082. (const :tag "Not" nil)
  3083. (const :tag "Always" t)
  3084. (repeat :tag "Specific tags" (string :tag "Tag"))
  3085. (regexp :tag "Tags matched by regexp")))
  3086. (defcustom org-tags-exclude-from-inheritance nil
  3087. "List of tags that should never be inherited.
  3088. This is a way to exclude a few tags from inheritance. For way to do
  3089. the opposite, to actively allow inheritance for selected tags,
  3090. see the variable `org-use-tag-inheritance'."
  3091. :group 'org-tags
  3092. :type '(repeat (string :tag "Tag")))
  3093. (defun org-tag-inherit-p (tag)
  3094. "Check if TAG is one that should be inherited."
  3095. (cond
  3096. ((member tag org-tags-exclude-from-inheritance) nil)
  3097. ((eq org-use-tag-inheritance t) t)
  3098. ((not org-use-tag-inheritance) nil)
  3099. ((stringp org-use-tag-inheritance)
  3100. (string-match org-use-tag-inheritance tag))
  3101. ((listp org-use-tag-inheritance)
  3102. (member tag org-use-tag-inheritance))
  3103. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3104. (defcustom org-tags-match-list-sublevels t
  3105. "Non-nil means list also sublevels of headlines matching a search.
  3106. This variable applies to tags/property searches, and also to stuck
  3107. projects because this search is based on a tags match as well.
  3108. When set to the symbol `indented', sublevels are indented with
  3109. leading dots.
  3110. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3111. the sublevels of a headline matching a tag search often also match
  3112. the same search. Listing all of them can create very long lists.
  3113. Setting this variable to nil causes subtrees of a match to be skipped.
  3114. This variable is semi-obsolete and probably should always be true. It
  3115. is better to limit inheritance to certain tags using the variables
  3116. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3117. :group 'org-tags
  3118. :type '(choice
  3119. (const :tag "No, don't list them" nil)
  3120. (const :tag "Yes, do list them" t)
  3121. (const :tag "List them, indented with leading dots" indented)))
  3122. (defcustom org-tags-sort-function nil
  3123. "When set, tags are sorted using this function as a comparator."
  3124. :group 'org-tags
  3125. :type '(choice
  3126. (const :tag "No sorting" nil)
  3127. (const :tag "Alphabetical" string<)
  3128. (const :tag "Reverse alphabetical" string>)
  3129. (function :tag "Custom function" nil)))
  3130. (defvar org-tags-history nil
  3131. "History of minibuffer reads for tags.")
  3132. (defvar org-last-tags-completion-table nil
  3133. "The last used completion table for tags.")
  3134. (defvar org-after-tags-change-hook nil
  3135. "Hook that is run after the tags in a line have changed.")
  3136. (defgroup org-properties nil
  3137. "Options concerning properties in Org mode."
  3138. :tag "Org Properties"
  3139. :group 'org)
  3140. (defcustom org-property-format "%-10s %s"
  3141. "How property key/value pairs should be formatted by `indent-line'.
  3142. When `indent-line' hits a property definition, it will format the line
  3143. according to this format, mainly to make sure that the values are
  3144. lined-up with respect to each other."
  3145. :group 'org-properties
  3146. :type 'string)
  3147. (defcustom org-properties-postprocess-alist nil
  3148. "Alist of properties and functions to adjust inserted values.
  3149. Elements of this alist must be of the form
  3150. ([string] [function])
  3151. where [string] must be a property name and [function] must be a
  3152. lambda expression: this lambda expression must take one argument,
  3153. the value to adjust, and return the new value as a string.
  3154. For example, this element will allow the property \"Remaining\"
  3155. to be updated wrt the relation between the \"Effort\" property
  3156. and the clock summary:
  3157. ((\"Remaining\" (lambda(value)
  3158. (let ((clocksum (org-clock-sum-current-item))
  3159. (effort (org-duration-to-minutes
  3160. (org-entry-get (point) \"Effort\"))))
  3161. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3162. :group 'org-properties
  3163. :version "24.1"
  3164. :type '(alist :key-type (string :tag "Property")
  3165. :value-type (function :tag "Function")))
  3166. (defcustom org-use-property-inheritance nil
  3167. "Non-nil means properties apply also for sublevels.
  3168. This setting is chiefly used during property searches. Turning it on can
  3169. cause significant overhead when doing a search, which is why it is not
  3170. on by default.
  3171. When nil, only the properties directly given in the current entry count.
  3172. When t, every property is inherited. The value may also be a list of
  3173. properties that should have inheritance, or a regular expression matching
  3174. properties that should be inherited.
  3175. However, note that some special properties use inheritance under special
  3176. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3177. and the properties ending in \"_ALL\" when they are used as descriptor
  3178. for valid values of a property.
  3179. Note for programmers:
  3180. When querying an entry with `org-entry-get', you can control if inheritance
  3181. should be used. By default, `org-entry-get' looks only at the local
  3182. properties. You can request inheritance by setting the inherit argument
  3183. to t (to force inheritance) or to `selective' (to respect the setting
  3184. in this variable)."
  3185. :group 'org-properties
  3186. :type '(choice
  3187. (const :tag "Not" nil)
  3188. (const :tag "Always" t)
  3189. (repeat :tag "Specific properties" (string :tag "Property"))
  3190. (regexp :tag "Properties matched by regexp")))
  3191. (defun org-property-inherit-p (property)
  3192. "Return a non-nil value if PROPERTY should be inherited."
  3193. (cond
  3194. ((eq org-use-property-inheritance t) t)
  3195. ((not org-use-property-inheritance) nil)
  3196. ((stringp org-use-property-inheritance)
  3197. (string-match org-use-property-inheritance property))
  3198. ((listp org-use-property-inheritance)
  3199. (member-ignore-case property org-use-property-inheritance))
  3200. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3201. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3202. "The default column format, if no other format has been defined.
  3203. This variable can be set on the per-file basis by inserting a line
  3204. #+COLUMNS: %25ITEM ....."
  3205. :group 'org-properties
  3206. :type 'string)
  3207. (defcustom org-columns-ellipses ".."
  3208. "The ellipses to be used when a field in column view is truncated.
  3209. When this is the empty string, as many characters as possible are shown,
  3210. but then there will be no visual indication that the field has been truncated.
  3211. When this is a string of length N, the last N characters of a truncated
  3212. field are replaced by this string. If the column is narrower than the
  3213. ellipses string, only part of the ellipses string will be shown."
  3214. :group 'org-properties
  3215. :type 'string)
  3216. (defconst org-global-properties-fixed
  3217. '(("VISIBILITY_ALL" . "folded children content all")
  3218. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3219. "List of property/value pairs that can be inherited by any entry.
  3220. These are fixed values, for the preset properties. The user variable
  3221. that can be used to add to this list is `org-global-properties'.
  3222. The entries in this list are cons cells where the car is a property
  3223. name and cdr is a string with the value. If the value represents
  3224. multiple items like an \"_ALL\" property, separate the items by
  3225. spaces.")
  3226. (defcustom org-global-properties nil
  3227. "List of property/value pairs that can be inherited by any entry.
  3228. This list will be combined with the constant `org-global-properties-fixed'.
  3229. The entries in this list are cons cells where the car is a property
  3230. name and cdr is a string with the value.
  3231. You can set buffer-local values for the same purpose in the variable
  3232. `org-file-properties' this by adding lines like
  3233. #+PROPERTY: NAME VALUE"
  3234. :group 'org-properties
  3235. :type '(repeat
  3236. (cons (string :tag "Property")
  3237. (string :tag "Value"))))
  3238. (defvar-local org-file-properties nil
  3239. "List of property/value pairs that can be inherited by any entry.
  3240. Valid for the current buffer.
  3241. This variable is populated from #+PROPERTY lines.")
  3242. (defgroup org-agenda nil
  3243. "Options concerning agenda views in Org mode."
  3244. :tag "Org Agenda"
  3245. :group 'org)
  3246. (defvar-local org-category nil
  3247. "Variable used by org files to set a category for agenda display.
  3248. Such files should use a file variable to set it, for example
  3249. # -*- mode: org; org-category: \"ELisp\"
  3250. or contain a special line
  3251. #+CATEGORY: ELisp
  3252. If the file does not specify a category, then file's base name
  3253. is used instead.")
  3254. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3255. (defcustom org-agenda-files nil
  3256. "The files to be used for agenda display.
  3257. If an entry is a directory, all files in that directory that are matched
  3258. by `org-agenda-file-regexp' will be part of the file list.
  3259. If the value of the variable is not a list but a single file name, then
  3260. the list of agenda files is actually stored and maintained in that file,
  3261. one agenda file per line. In this file paths can be given relative to
  3262. `org-directory'. Tilde expansion and environment variable substitution
  3263. are also made.
  3264. Entries may be added to this list with `\\[org-agenda-file-to-front]'
  3265. and removed with `\\[org-remove-file]'."
  3266. :group 'org-agenda
  3267. :type '(choice
  3268. (repeat :tag "List of files and directories" file)
  3269. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3270. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3271. "Regular expression to match files for `org-agenda-files'.
  3272. If any element in the list in that variable contains a directory instead
  3273. of a normal file, all files in that directory that are matched by this
  3274. regular expression will be included."
  3275. :group 'org-agenda
  3276. :type 'regexp)
  3277. (defcustom org-agenda-text-search-extra-files nil
  3278. "List of extra files to be searched by text search commands.
  3279. These files will be searched in addition to the agenda files by the
  3280. commands `org-search-view' (`\\[org-agenda] s') \
  3281. and `org-occur-in-agenda-files'.
  3282. Note that these files will only be searched for text search commands,
  3283. not for the other agenda views like todo lists, tag searches or the weekly
  3284. agenda. This variable is intended to list notes and possibly archive files
  3285. that should also be searched by these two commands.
  3286. In fact, if the first element in the list is the symbol `agenda-archives',
  3287. then all archive files of all agenda files will be added to the search
  3288. scope."
  3289. :group 'org-agenda
  3290. :type '(set :greedy t
  3291. (const :tag "Agenda Archives" agenda-archives)
  3292. (repeat :inline t (file))))
  3293. (defvaralias 'org-agenda-multi-occur-extra-files
  3294. 'org-agenda-text-search-extra-files)
  3295. (defcustom org-agenda-skip-unavailable-files nil
  3296. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3297. A nil value means to remove them, after a query, from the list."
  3298. :group 'org-agenda
  3299. :type 'boolean)
  3300. (defcustom org-calendar-to-agenda-key [?c]
  3301. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3302. The command `org-calendar-goto-agenda' will be bound to this key. The
  3303. default is the character `c' because then `c' can be used to switch back and
  3304. forth between agenda and calendar."
  3305. :group 'org-agenda
  3306. :type 'sexp)
  3307. (defcustom org-calendar-insert-diary-entry-key [?i]
  3308. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3309. This option is irrelevant until `org-agenda-diary-file' has been configured
  3310. to point to an Org file. When that is the case, the command
  3311. `org-agenda-diary-entry' will be bound to the key given here, by default
  3312. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3313. if you want to continue doing this, you need to change this to a different
  3314. key."
  3315. :group 'org-agenda
  3316. :type 'sexp)
  3317. (defcustom org-agenda-diary-file 'diary-file
  3318. "File to which to add new entries with the `i' key in agenda and calendar.
  3319. When this is the symbol `diary-file', the functionality in the Emacs
  3320. calendar will be used to add entries to the `diary-file'. But when this
  3321. points to a file, `org-agenda-diary-entry' will be used instead."
  3322. :group 'org-agenda
  3323. :type '(choice
  3324. (const :tag "The standard Emacs diary file" diary-file)
  3325. (file :tag "Special Org file diary entries")))
  3326. (eval-after-load "calendar"
  3327. '(progn
  3328. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3329. 'org-calendar-goto-agenda)
  3330. (add-hook 'calendar-mode-hook
  3331. (lambda ()
  3332. (unless (eq org-agenda-diary-file 'diary-file)
  3333. (define-key calendar-mode-map
  3334. org-calendar-insert-diary-entry-key
  3335. 'org-agenda-diary-entry))))))
  3336. (defgroup org-latex nil
  3337. "Options for embedding LaTeX code into Org mode."
  3338. :tag "Org LaTeX"
  3339. :group 'org)
  3340. (defcustom org-format-latex-options
  3341. '(:foreground default :background default :scale 1.0
  3342. :html-foreground "Black" :html-background "Transparent"
  3343. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3344. "Options for creating images from LaTeX fragments.
  3345. This is a property list with the following properties:
  3346. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3347. `default' means use the foreground of the default face.
  3348. `auto' means use the foreground from the text face.
  3349. :background the background color, or \"Transparent\".
  3350. `default' means use the background of the default face.
  3351. `auto' means use the background from the text face.
  3352. :scale a scaling factor for the size of the images, to get more pixels
  3353. :html-foreground, :html-background, :html-scale
  3354. the same numbers for HTML export.
  3355. :matchers a list indicating which matchers should be used to
  3356. find LaTeX fragments. Valid members of this list are:
  3357. \"begin\" find environments
  3358. \"$1\" find single characters surrounded by $.$
  3359. \"$\" find math expressions surrounded by $...$
  3360. \"$$\" find math expressions surrounded by $$....$$
  3361. \"\\(\" find math expressions surrounded by \\(...\\)
  3362. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3363. :group 'org-latex
  3364. :type 'plist)
  3365. (defcustom org-format-latex-signal-error t
  3366. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3367. When nil, just push out a message."
  3368. :group 'org-latex
  3369. :version "24.1"
  3370. :type 'boolean)
  3371. (defcustom org-latex-to-mathml-jar-file nil
  3372. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3373. Use this to specify additional executable file say a jar file.
  3374. When using MathToWeb as the converter, specify the full-path to
  3375. your mathtoweb.jar file."
  3376. :group 'org-latex
  3377. :version "24.1"
  3378. :type '(choice
  3379. (const :tag "None" nil)
  3380. (file :tag "JAR file" :must-match t)))
  3381. (defcustom org-latex-to-mathml-convert-command nil
  3382. "Command to convert LaTeX fragments to MathML.
  3383. Replace format-specifiers in the command as noted below and use
  3384. `shell-command' to convert LaTeX to MathML.
  3385. %j: Executable file in fully expanded form as specified by
  3386. `org-latex-to-mathml-jar-file'.
  3387. %I: Input LaTeX file in fully expanded form.
  3388. %i: The latex fragment to be converted.
  3389. %o: Output MathML file.
  3390. This command is used by `org-create-math-formula'.
  3391. When using MathToWeb as the converter, set this option to
  3392. \"java -jar %j -unicode -force -df %o %I\".
  3393. When using LaTeXML set this option to
  3394. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3395. :group 'org-latex
  3396. :version "24.1"
  3397. :type '(choice
  3398. (const :tag "None" nil)
  3399. (string :tag "\nShell command")))
  3400. (defcustom org-preview-latex-default-process 'dvipng
  3401. "The default process to convert LaTeX fragments to image files.
  3402. All available processes and theirs documents can be found in
  3403. `org-preview-latex-process-alist', which see."
  3404. :group 'org-latex
  3405. :version "26.1"
  3406. :package-version '(Org . "9.0")
  3407. :type 'symbol)
  3408. (defcustom org-preview-latex-process-alist
  3409. '((dvipng
  3410. :programs ("latex" "dvipng")
  3411. :description "dvi > png"
  3412. :message "you need to install the programs: latex and dvipng."
  3413. :image-input-type "dvi"
  3414. :image-output-type "png"
  3415. :image-size-adjust (1.0 . 1.0)
  3416. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3417. :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %O %f"))
  3418. (dvisvgm
  3419. :programs ("latex" "dvisvgm")
  3420. :description "dvi > svg"
  3421. :message "you need to install the programs: latex and dvisvgm."
  3422. :use-xcolor t
  3423. :image-input-type "dvi"
  3424. :image-output-type "svg"
  3425. :image-size-adjust (1.7 . 1.5)
  3426. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3427. :image-converter ("dvisvgm %f -n -b min -c %S -o %O"))
  3428. (imagemagick
  3429. :programs ("latex" "convert")
  3430. :description "pdf > png"
  3431. :message "you need to install the programs: latex and imagemagick."
  3432. :use-xcolor t
  3433. :image-input-type "pdf"
  3434. :image-output-type "png"
  3435. :image-size-adjust (1.0 . 1.0)
  3436. :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
  3437. :image-converter
  3438. ("convert -density %D -trim -antialias %f -quality 100 %O")))
  3439. "Definitions of external processes for LaTeX previewing.
  3440. Org mode can use some external commands to generate TeX snippet's images for
  3441. previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
  3442. `org-create-formula-image' how to call them.
  3443. The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
  3444. PROPERTIES accepts the following attributes:
  3445. :programs list of strings, required programs.
  3446. :description string, describe the process.
  3447. :message string, message it when required programs cannot be found.
  3448. :image-input-type string, input file type of image converter (e.g., \"dvi\").
  3449. :image-output-type string, output file type of image converter (e.g., \"png\").
  3450. :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
  3451. deal with background and foreground color of image.
  3452. Otherwise, dvipng style background and foreground color
  3453. format are generated. You may then refer to them in
  3454. command options with \"%F\" and \"%B\".
  3455. :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
  3456. image size showed in buffer and the cdr element is for
  3457. HTML file. This option is only useful for process
  3458. developers, users should use variable
  3459. `org-format-latex-options' instead.
  3460. :post-clean list of strings, files matched are to be cleaned up once
  3461. the image is generated. When nil, the files with \".dvi\",
  3462. \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
  3463. \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
  3464. be cleaned up.
  3465. :latex-header list of strings, the LaTeX header of the snippet file.
  3466. When nil, the fallback value is used instead, which is
  3467. controlled by `org-format-latex-header',
  3468. `org-latex-default-packages-alist' and
  3469. `org-latex-packages-alist', which see.
  3470. :latex-compiler list of LaTeX commands, as strings. Each of them is given
  3471. to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
  3472. replaced with values defined below.
  3473. :image-converter list of image converter commands strings. Each of them is
  3474. given to the shell and supports any of the following
  3475. place-holders defined below.
  3476. Place-holders used by `:image-converter' and `:latex-compiler':
  3477. %f input file name
  3478. %b base name of input file
  3479. %o base directory of input file
  3480. %O absolute output file name
  3481. Place-holders only used by `:image-converter':
  3482. %F foreground of image
  3483. %B background of image
  3484. %D dpi, which is used to adjust image size by some processing commands.
  3485. %S the image size scale ratio, which is used to adjust image size by some
  3486. processing commands."
  3487. :group 'org-latex
  3488. :version "26.1"
  3489. :package-version '(Org . "9.0")
  3490. :type '(alist :tag "LaTeX to image backends"
  3491. :value-type (plist)))
  3492. (defcustom org-preview-latex-image-directory "ltximg/"
  3493. "Path to store latex preview images.
  3494. A relative path here creates many directories relative to the
  3495. processed org files paths. An absolute path puts all preview
  3496. images at the same place."
  3497. :group 'org-latex
  3498. :version "26.1"
  3499. :package-version '(Org . "9.0")
  3500. :type 'string)
  3501. (defun org-format-latex-mathml-available-p ()
  3502. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3503. (save-match-data
  3504. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3505. org-latex-to-mathml-convert-command)
  3506. (let ((executable (car (split-string
  3507. org-latex-to-mathml-convert-command))))
  3508. (when (executable-find executable)
  3509. (if (string-match
  3510. "%j" org-latex-to-mathml-convert-command)
  3511. (file-readable-p org-latex-to-mathml-jar-file)
  3512. t))))))
  3513. (defcustom org-format-latex-header "\\documentclass{article}
  3514. \\usepackage[usenames]{color}
  3515. \[PACKAGES]
  3516. \[DEFAULT-PACKAGES]
  3517. \\pagestyle{empty} % do not remove
  3518. % The settings below are copied from fullpage.sty
  3519. \\setlength{\\textwidth}{\\paperwidth}
  3520. \\addtolength{\\textwidth}{-3cm}
  3521. \\setlength{\\oddsidemargin}{1.5cm}
  3522. \\addtolength{\\oddsidemargin}{-2.54cm}
  3523. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3524. \\setlength{\\textheight}{\\paperheight}
  3525. \\addtolength{\\textheight}{-\\headheight}
  3526. \\addtolength{\\textheight}{-\\headsep}
  3527. \\addtolength{\\textheight}{-\\footskip}
  3528. \\addtolength{\\textheight}{-3cm}
  3529. \\setlength{\\topmargin}{1.5cm}
  3530. \\addtolength{\\topmargin}{-2.54cm}"
  3531. "The document header used for processing LaTeX fragments.
  3532. It is imperative that this header make sure that no page number
  3533. appears on the page. The package defined in the variables
  3534. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3535. will either replace the placeholder \"[PACKAGES]\" in this
  3536. header, or they will be appended."
  3537. :group 'org-latex
  3538. :type 'string)
  3539. (defun org-set-packages-alist (var val)
  3540. "Set the packages alist and make sure it has 3 elements per entry."
  3541. (set var (mapcar (lambda (x)
  3542. (if (and (consp x) (= (length x) 2))
  3543. (list (car x) (nth 1 x) t)
  3544. x))
  3545. val)))
  3546. (defun org-get-packages-alist (var)
  3547. "Get the packages alist and make sure it has 3 elements per entry."
  3548. (mapcar (lambda (x)
  3549. (if (and (consp x) (= (length x) 2))
  3550. (list (car x) (nth 1 x) t)
  3551. x))
  3552. (default-value var)))
  3553. (defcustom org-latex-default-packages-alist
  3554. '(("AUTO" "inputenc" t ("pdflatex"))
  3555. ("T1" "fontenc" t ("pdflatex"))
  3556. ("" "graphicx" t)
  3557. ("" "grffile" t)
  3558. ("" "longtable" nil)
  3559. ("" "wrapfig" nil)
  3560. ("" "rotating" nil)
  3561. ("normalem" "ulem" t)
  3562. ("" "amsmath" t)
  3563. ("" "textcomp" t)
  3564. ("" "amssymb" t)
  3565. ("" "capt-of" nil)
  3566. ("" "hyperref" nil))
  3567. "Alist of default packages to be inserted in the header.
  3568. Change this only if one of the packages here causes an
  3569. incompatibility with another package you are using.
  3570. The packages in this list are needed by one part or another of
  3571. Org mode to function properly:
  3572. - inputenc, fontenc: for basic font and character selection
  3573. - graphicx: for including images
  3574. - grffile: allow periods and spaces in graphics file names
  3575. - longtable: For multipage tables
  3576. - wrapfig: for figure placement
  3577. - rotating: for sideways figures and tables
  3578. - ulem: for underline and strike-through
  3579. - amsmath: for subscript and superscript and math environments
  3580. - textcomp, amssymb: for various symbols used
  3581. for interpreting the entities in `org-entities'. You can skip
  3582. some of these packages if you don't use any of their symbols.
  3583. - capt-of: for captions outside of floats
  3584. - hyperref: for cross references
  3585. Therefore you should not modify this variable unless you know
  3586. what you are doing. The one reason to change it anyway is that
  3587. you might be loading some other package that conflicts with one
  3588. of the default packages. Each element is either a cell or
  3589. a string.
  3590. A cell is of the format
  3591. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3592. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3593. when compiling LaTeX snippets into images for inclusion into
  3594. non-LaTeX output. COMPILERS is a list of compilers that should
  3595. include the package, see `org-latex-compiler'. If the document
  3596. compiler is not in the list, and the list is non-nil, the package
  3597. will not be inserted in the final document.
  3598. A string will be inserted as-is in the header of the document."
  3599. :group 'org-latex
  3600. :group 'org-export-latex
  3601. :set 'org-set-packages-alist
  3602. :get 'org-get-packages-alist
  3603. :version "26.1"
  3604. :package-version '(Org . "8.3")
  3605. :type '(repeat
  3606. (choice
  3607. (list :tag "options/package pair"
  3608. (string :tag "options")
  3609. (string :tag "package")
  3610. (boolean :tag "Snippet"))
  3611. (string :tag "A line of LaTeX"))))
  3612. (defcustom org-latex-packages-alist nil
  3613. "Alist of packages to be inserted in every LaTeX header.
  3614. These will be inserted after `org-latex-default-packages-alist'.
  3615. Each element is either a cell or a string.
  3616. A cell is of the format:
  3617. (\"options\" \"package\" SNIPPET-FLAG)
  3618. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3619. needed when turning LaTeX snippets into images for inclusion into
  3620. non-LaTeX output.
  3621. A string will be inserted as-is in the header of the document.
  3622. Make sure that you only list packages here which:
  3623. - you want in every file;
  3624. - do not conflict with the setup in `org-format-latex-header';
  3625. - do not conflict with the default packages in
  3626. `org-latex-default-packages-alist'."
  3627. :group 'org-latex
  3628. :group 'org-export-latex
  3629. :set 'org-set-packages-alist
  3630. :get 'org-get-packages-alist
  3631. :type '(repeat
  3632. (choice
  3633. (list :tag "options/package pair"
  3634. (string :tag "options")
  3635. (string :tag "package")
  3636. (boolean :tag "Snippet"))
  3637. (string :tag "A line of LaTeX"))))
  3638. (defgroup org-appearance nil
  3639. "Settings for Org mode appearance."
  3640. :tag "Org Appearance"
  3641. :group 'org)
  3642. (defcustom org-level-color-stars-only nil
  3643. "Non-nil means fontify only the stars in each headline.
  3644. When nil, the entire headline is fontified.
  3645. Changing it requires restart of `font-lock-mode' to become effective
  3646. also in regions already fontified."
  3647. :group 'org-appearance
  3648. :type 'boolean)
  3649. (defcustom org-hide-leading-stars nil
  3650. "Non-nil means hide the first N-1 stars in a headline.
  3651. This works by using the face `org-hide' for these stars. This
  3652. face is white for a light background, and black for a dark
  3653. background. You may have to customize the face `org-hide' to
  3654. make this work.
  3655. Changing it requires restart of `font-lock-mode' to become effective
  3656. also in regions already fontified.
  3657. You may also set this on a per-file basis by adding one of the following
  3658. lines to the buffer:
  3659. #+STARTUP: hidestars
  3660. #+STARTUP: showstars"
  3661. :group 'org-appearance
  3662. :type 'boolean)
  3663. (defcustom org-hidden-keywords nil
  3664. "List of symbols corresponding to keywords to be hidden the org buffer.
  3665. For example, a value \\='(title) for this list will make the document's title
  3666. appear in the buffer without the initial #+TITLE: keyword."
  3667. :group 'org-appearance
  3668. :version "24.1"
  3669. :type '(set (const :tag "#+AUTHOR" author)
  3670. (const :tag "#+DATE" date)
  3671. (const :tag "#+EMAIL" email)
  3672. (const :tag "#+TITLE" title)))
  3673. (defcustom org-custom-properties nil
  3674. "List of properties (as strings) with a special meaning.
  3675. The default use of these custom properties is to let the user
  3676. hide them with `org-toggle-custom-properties-visibility'."
  3677. :group 'org-properties
  3678. :group 'org-appearance
  3679. :version "24.3"
  3680. :type '(repeat (string :tag "Property Name")))
  3681. (defcustom org-fontify-done-headline nil
  3682. "Non-nil means change the face of a headline if it is marked DONE.
  3683. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3684. When this is non-nil, the headline after the keyword is set to the
  3685. `org-headline-done' as an additional indication."
  3686. :group 'org-appearance
  3687. :type 'boolean)
  3688. (defcustom org-fontify-emphasized-text t
  3689. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3690. Changing this variable requires a restart of Emacs to take effect."
  3691. :group 'org-appearance
  3692. :type 'boolean)
  3693. (defcustom org-fontify-whole-heading-line nil
  3694. "Non-nil means fontify the whole line for headings.
  3695. This is useful when setting a background color for the
  3696. org-level-* faces."
  3697. :group 'org-appearance
  3698. :type 'boolean)
  3699. (defcustom org-highlight-latex-and-related nil
  3700. "Non-nil means highlight LaTeX related syntax in the buffer.
  3701. When non nil, the value should be a list containing any of the
  3702. following symbols:
  3703. `latex' Highlight LaTeX snippets and environments.
  3704. `script' Highlight subscript and superscript.
  3705. `entities' Highlight entities."
  3706. :group 'org-appearance
  3707. :version "24.4"
  3708. :package-version '(Org . "8.0")
  3709. :type '(choice
  3710. (const :tag "No highlighting" nil)
  3711. (set :greedy t :tag "Highlight"
  3712. (const :tag "LaTeX snippets and environments" latex)
  3713. (const :tag "Subscript and superscript" script)
  3714. (const :tag "Entities" entities))))
  3715. (defcustom org-hide-emphasis-markers nil
  3716. "Non-nil mean font-lock should hide the emphasis marker characters."
  3717. :group 'org-appearance
  3718. :type 'boolean)
  3719. (defcustom org-hide-macro-markers nil
  3720. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3721. :group 'org-appearance
  3722. :type 'boolean)
  3723. (defcustom org-pretty-entities nil
  3724. "Non-nil means show entities as UTF8 characters.
  3725. When nil, the \\name form remains in the buffer."
  3726. :group 'org-appearance
  3727. :version "24.1"
  3728. :type 'boolean)
  3729. (defcustom org-pretty-entities-include-sub-superscripts t
  3730. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3731. :group 'org-appearance
  3732. :version "24.1"
  3733. :type 'boolean)
  3734. (defvar org-emph-re nil
  3735. "Regular expression for matching emphasis.
  3736. After a match, the match groups contain these elements:
  3737. 0 The match of the full regular expression, including the characters
  3738. before and after the proper match
  3739. 1 The character before the proper match, or empty at beginning of line
  3740. 2 The proper match, including the leading and trailing markers
  3741. 3 The leading marker like * or /, indicating the type of highlighting
  3742. 4 The text between the emphasis markers, not including the markers
  3743. 5 The character after the match, empty at the end of a line")
  3744. (defvar org-verbatim-re nil
  3745. "Regular expression for matching verbatim text.")
  3746. (defvar org-emphasis-regexp-components) ; defined just below
  3747. (defvar org-emphasis-alist) ; defined just below
  3748. (defun org-set-emph-re (var val)
  3749. "Set variable and compute the emphasis regular expression."
  3750. (set var val)
  3751. (when (and (boundp 'org-emphasis-alist)
  3752. (boundp 'org-emphasis-regexp-components)
  3753. org-emphasis-alist org-emphasis-regexp-components)
  3754. (pcase-let*
  3755. ((`(,pre ,post ,border ,body ,nl) org-emphasis-regexp-components)
  3756. (body (if (<= nl 0) body
  3757. (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl)))
  3758. (template
  3759. (format (concat "\\([%s]\\|^\\)" ;before markers
  3760. "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
  3761. "\\([%s]\\|$\\)") ;after markers
  3762. pre border border body border post)))
  3763. (setq org-emph-re (format template "*/_+"))
  3764. (setq org-verbatim-re (format template "=~")))))
  3765. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3766. ;; set this option proved cumbersome. See this message/thread:
  3767. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3768. (defvar org-emphasis-regexp-components
  3769. '("- \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3770. "Components used to build the regular expression for emphasis.
  3771. This is a list with five entries. Terminology: In an emphasis string
  3772. like \" *strong word* \", we call the initial space PREMATCH, the final
  3773. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3774. and \"trong wor\" is the body. The different components in this variable
  3775. specify what is allowed/forbidden in each part:
  3776. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3777. post Chars allowed as postmatch. End of line will be allowed too.
  3778. border The chars *forbidden* as border characters.
  3779. body-regexp A regexp like \".\" to match a body character. Don't use
  3780. non-shy groups here, and don't allow newline here.
  3781. newline The maximum number of newlines allowed in an emphasis exp.
  3782. You need to reload Org or to restart Emacs after customizing this.")
  3783. (defcustom org-emphasis-alist
  3784. '(("*" bold)
  3785. ("/" italic)
  3786. ("_" underline)
  3787. ("=" org-verbatim verbatim)
  3788. ("~" org-code verbatim)
  3789. ("+" (:strike-through t)))
  3790. "Alist of characters and faces to emphasize text.
  3791. Text starting and ending with a special character will be emphasized,
  3792. for example *bold*, _underlined_ and /italic/. This variable sets the
  3793. marker characters and the face to be used by font-lock for highlighting
  3794. in Org buffers.
  3795. You need to reload Org or to restart Emacs after customizing this."
  3796. :group 'org-appearance
  3797. :set 'org-set-emph-re
  3798. :version "24.4"
  3799. :package-version '(Org . "8.0")
  3800. :type '(repeat
  3801. (list
  3802. (string :tag "Marker character")
  3803. (choice
  3804. (face :tag "Font-lock-face")
  3805. (plist :tag "Face property list"))
  3806. (option (const verbatim)))))
  3807. (defvar org-protecting-blocks '("src" "example" "export")
  3808. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3809. This is needed for font-lock setup.")
  3810. ;;; Functions and variables from their packages
  3811. ;; Declared here to avoid compiler warnings
  3812. (defvar mark-active)
  3813. ;; Various packages
  3814. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3815. (declare-function calendar-forward-day "cal-move" (arg))
  3816. (declare-function calendar-goto-date "cal-move" (date))
  3817. (declare-function calendar-goto-today "cal-move" ())
  3818. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3819. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3820. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3821. (declare-function cdlatex-tab "ext:cdlatex" ())
  3822. (declare-function dired-get-filename
  3823. "dired"
  3824. (&optional localp no-error-if-not-filep))
  3825. (declare-function iswitchb-read-buffer
  3826. "iswitchb"
  3827. (prompt &optional
  3828. default require-match _predicate start matches-set))
  3829. (declare-function org-agenda-change-all-lines
  3830. "org-agenda"
  3831. (newhead hdmarker &optional fixface just-this))
  3832. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3833. "org-agenda"
  3834. (&optional end))
  3835. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3836. (declare-function org-agenda-format-item
  3837. "org-agenda"
  3838. (extra txt &optional level category tags dotime
  3839. remove-re habitp))
  3840. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3841. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3842. (declare-function org-agenda-save-markers-for-cut-and-paste
  3843. "org-agenda"
  3844. (beg end))
  3845. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3846. (declare-function org-agenda-skip "org-agenda" ())
  3847. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3848. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3849. (declare-function org-indent-mode "org-indent" (&optional arg))
  3850. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3851. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3852. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3853. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3854. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3855. (declare-function parse-time-string "parse-time" (string))
  3856. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3857. (defvar align-mode-rules-list)
  3858. (defvar calc-embedded-close-formula)
  3859. (defvar calc-embedded-open-formula)
  3860. (defvar calc-embedded-open-mode)
  3861. (defvar font-lock-unfontify-region-function)
  3862. (defvar iswitchb-temp-buflist)
  3863. (defvar org-agenda-tags-todo-honor-ignore-options)
  3864. (defvar remember-data-file)
  3865. (defvar texmathp-why)
  3866. ;;;###autoload
  3867. (defun turn-on-orgtbl ()
  3868. "Unconditionally turn on `orgtbl-mode'."
  3869. (require 'org-table)
  3870. (orgtbl-mode 1))
  3871. (defun org-at-table-p (&optional table-type)
  3872. "Non-nil if the cursor is inside an Org table.
  3873. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3874. If `org-enable-table-editor' is nil, return nil unconditionally."
  3875. (and
  3876. org-enable-table-editor
  3877. (save-excursion
  3878. (beginning-of-line)
  3879. (looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3880. (or (not (derived-mode-p 'org-mode))
  3881. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3882. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3883. (defun org-at-table.el-p ()
  3884. "Non-nil when point is at a table.el table."
  3885. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3886. (let ((element (org-element-at-point)))
  3887. (and (eq (org-element-type element) 'table)
  3888. (eq (org-element-property :type element) 'table.el)))))
  3889. (defun org-at-table-hline-p ()
  3890. "Non-nil when point is inside a hline in a table.
  3891. Assume point is already in a table. If `org-enable-table-editor'
  3892. is nil, return nil unconditionally."
  3893. (and org-enable-table-editor
  3894. (save-excursion
  3895. (beginning-of-line)
  3896. (looking-at org-table-hline-regexp))))
  3897. (defun org-table-map-tables (function &optional quietly)
  3898. "Apply FUNCTION to the start of all tables in the buffer."
  3899. (org-with-wide-buffer
  3900. (goto-char (point-min))
  3901. (while (re-search-forward org-table-any-line-regexp nil t)
  3902. (unless quietly
  3903. (message "Mapping tables: %d%%"
  3904. (floor (* 100.0 (point)) (buffer-size))))
  3905. (beginning-of-line 1)
  3906. (when (and (looking-at org-table-line-regexp)
  3907. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3908. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3909. (save-excursion (funcall function))
  3910. (or (looking-at org-table-line-regexp)
  3911. (forward-char 1)))
  3912. (re-search-forward org-table-any-border-regexp nil 1)))
  3913. (unless quietly (message "Mapping tables: done")))
  3914. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3915. (declare-function org-clock-update-mode-line "org-clock" ())
  3916. (declare-function org-resolve-clocks "org-clock"
  3917. (&optional also-non-dangling-p prompt last-valid))
  3918. (defun org-at-TBLFM-p (&optional pos)
  3919. "Non-nil when point (or POS) is in #+TBLFM line."
  3920. (save-excursion
  3921. (goto-char (or pos (point)))
  3922. (beginning-of-line)
  3923. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3924. (eq (org-element-type (org-element-at-point)) 'table))))
  3925. (defvar org-clock-start-time)
  3926. (defvar org-clock-marker (make-marker)
  3927. "Marker recording the last clock-in.")
  3928. (defvar org-clock-hd-marker (make-marker)
  3929. "Marker recording the last clock-in, but the headline position.")
  3930. (defvar org-clock-heading ""
  3931. "The heading of the current clock entry.")
  3932. (defun org-clock-is-active ()
  3933. "Return the buffer where the clock is currently running.
  3934. Return nil if no clock is running."
  3935. (marker-buffer org-clock-marker))
  3936. (defun org-check-running-clock ()
  3937. "Check if the current buffer contains the running clock.
  3938. If yes, offer to stop it and to save the buffer with the changes."
  3939. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3940. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3941. (buffer-name))))
  3942. (org-clock-out)
  3943. (when (y-or-n-p "Save changed buffer?")
  3944. (save-buffer))))
  3945. (defun org-clocktable-try-shift (dir n)
  3946. "Check if this line starts a clock table, if yes, shift the time block."
  3947. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3948. (org-clocktable-shift dir n)))
  3949. ;;;###autoload
  3950. (defun org-clock-persistence-insinuate ()
  3951. "Set up hooks for clock persistence."
  3952. (require 'org-clock)
  3953. (add-hook 'org-mode-hook 'org-clock-load)
  3954. (add-hook 'kill-emacs-hook 'org-clock-save))
  3955. (defgroup org-archive nil
  3956. "Options concerning archiving in Org mode."
  3957. :tag "Org Archive"
  3958. :group 'org-structure)
  3959. (defcustom org-archive-location "%s_archive::"
  3960. "The location where subtrees should be archived.
  3961. The value of this variable is a string, consisting of two parts,
  3962. separated by a double-colon. The first part is a filename and
  3963. the second part is a headline.
  3964. When the filename is omitted, archiving happens in the same file.
  3965. %s in the filename will be replaced by the current file
  3966. name (without the directory part). Archiving to a different file
  3967. is useful to keep archived entries from contributing to the
  3968. Org Agenda.
  3969. The archived entries will be filed as subtrees of the specified
  3970. headline. When the headline is omitted, the subtrees are simply
  3971. filed away at the end of the file, as top-level entries. Also in
  3972. the heading you can use %s to represent the file name, this can be
  3973. useful when using the same archive for a number of different files.
  3974. Here are a few examples:
  3975. \"%s_archive::\"
  3976. If the current file is Projects.org, archive in file
  3977. Projects.org_archive, as top-level trees. This is the default.
  3978. \"::* Archived Tasks\"
  3979. Archive in the current file, under the top-level headline
  3980. \"* Archived Tasks\".
  3981. \"~/org/archive.org::\"
  3982. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3983. \"~/org/archive.org::* From %s\"
  3984. Archive in file ~/org/archive.org (absolute path), under headlines
  3985. \"From FILENAME\" where file name is the current file name.
  3986. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3987. The \"datetree/\" string is special, signifying to archive
  3988. items to the datetree. Items are placed in either the CLOSED
  3989. date of the item, or the current date if there is no CLOSED date.
  3990. The heading will be a subentry to the current date. There doesn't
  3991. need to be a heading, but there always needs to be a slash after
  3992. datetree. For example, to store archived items directly in the
  3993. datetree, use \"~/org/datetree.org::datetree/\".
  3994. \"basement::** Finished Tasks\"
  3995. Archive in file ./basement (relative path), as level 3 trees
  3996. below the level 2 heading \"** Finished Tasks\".
  3997. You may set this option on a per-file basis by adding to the buffer a
  3998. line like
  3999. #+ARCHIVE: basement::** Finished Tasks
  4000. You may also define it locally for a subtree by setting an ARCHIVE property
  4001. in the entry. If such a property is found in an entry, or anywhere up
  4002. the hierarchy, it will be used."
  4003. :group 'org-archive
  4004. :type 'string)
  4005. (defcustom org-agenda-skip-archived-trees t
  4006. "Non-nil means the agenda will skip any items located in archived trees.
  4007. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4008. variable is no longer recommended, you should leave it at the value t.
  4009. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4010. :group 'org-archive
  4011. :group 'org-agenda-skip
  4012. :type 'boolean)
  4013. (defcustom org-columns-skip-archived-trees t
  4014. "Non-nil means ignore archived trees when creating column view."
  4015. :group 'org-archive
  4016. :group 'org-properties
  4017. :type 'boolean)
  4018. (defcustom org-cycle-open-archived-trees nil
  4019. "Non-nil means `org-cycle' will open archived trees.
  4020. An archived tree is a tree marked with the tag ARCHIVE.
  4021. When nil, archived trees will stay folded. You can still open them with
  4022. normal outline commands like `show-all', but not with the cycling commands."
  4023. :group 'org-archive
  4024. :group 'org-cycle
  4025. :type 'boolean)
  4026. (defcustom org-sparse-tree-open-archived-trees nil
  4027. "Non-nil means sparse tree construction shows matches in archived trees.
  4028. When nil, matches in these trees are highlighted, but the trees are kept in
  4029. collapsed state."
  4030. :group 'org-archive
  4031. :group 'org-sparse-trees
  4032. :type 'boolean)
  4033. (defcustom org-sparse-tree-default-date-type nil
  4034. "The default date type when building a sparse tree.
  4035. When this is nil, a date is a scheduled or a deadline timestamp.
  4036. Otherwise, these types are allowed:
  4037. all: all timestamps
  4038. active: only active timestamps (<...>)
  4039. inactive: only inactive timestamps ([...])
  4040. scheduled: only scheduled timestamps
  4041. deadline: only deadline timestamps"
  4042. :type '(choice (const :tag "Scheduled or deadline" nil)
  4043. (const :tag "All timestamps" all)
  4044. (const :tag "Only active timestamps" active)
  4045. (const :tag "Only inactive timestamps" inactive)
  4046. (const :tag "Only scheduled timestamps" scheduled)
  4047. (const :tag "Only deadline timestamps" deadline)
  4048. (const :tag "Only closed timestamps" closed))
  4049. :version "26.1"
  4050. :package-version '(Org . "8.3")
  4051. :group 'org-sparse-trees)
  4052. (defun org-cycle-hide-archived-subtrees (state)
  4053. "Re-hide all archived subtrees after a visibility state change.
  4054. STATE should be one of the symbols listed in the docstring of
  4055. `org-cycle-hook'."
  4056. (when (and (not org-cycle-open-archived-trees)
  4057. (not (memq state '(overview folded))))
  4058. (save-excursion
  4059. (let* ((globalp (memq state '(contents all)))
  4060. (beg (if globalp (point-min) (point)))
  4061. (end (if globalp (point-max) (org-end-of-subtree t))))
  4062. (org-hide-archived-subtrees beg end)
  4063. (goto-char beg)
  4064. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4065. (message "%s" (substitute-command-keys
  4066. "Subtree is archived and stays closed. Use \
  4067. `\\[org-force-cycle-archived]' to cycle it anyway.")))))))
  4068. (defun org-force-cycle-archived ()
  4069. "Cycle subtree even if it is archived."
  4070. (interactive)
  4071. (setq this-command 'org-cycle)
  4072. (let ((org-cycle-open-archived-trees t))
  4073. (call-interactively 'org-cycle)))
  4074. (defun org-hide-archived-subtrees (beg end)
  4075. "Re-hide all archived subtrees after a visibility state change."
  4076. (org-with-wide-buffer
  4077. (let ((case-fold-search nil)
  4078. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4079. (goto-char beg)
  4080. ;; Include headline point is currently on.
  4081. (beginning-of-line)
  4082. (while (and (< (point) end) (re-search-forward re end t))
  4083. (when (member org-archive-tag (org-get-tags))
  4084. (org-flag-subtree t)
  4085. (org-end-of-subtree t))))))
  4086. (declare-function outline-end-of-heading "outline" ())
  4087. (declare-function outline-flag-region "outline" (from to flag))
  4088. (defun org-flag-subtree (flag)
  4089. (save-excursion
  4090. (org-back-to-heading t)
  4091. (outline-end-of-heading)
  4092. (outline-flag-region (point)
  4093. (progn (org-end-of-subtree t) (point))
  4094. flag)))
  4095. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4096. ;; Declare Column View Code
  4097. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4098. (declare-function org-columns-compute "org-colview" (property))
  4099. ;; Declare ID code
  4100. (declare-function org-id-store-link "org-id")
  4101. (declare-function org-id-locations-load "org-id")
  4102. (declare-function org-id-locations-save "org-id")
  4103. (defvar org-id-track-globally)
  4104. ;;; Variables for pre-computed regular expressions, all buffer local
  4105. (defvar-local org-todo-regexp nil
  4106. "Matches any of the TODO state keywords.
  4107. Since TODO keywords are case-sensitive, `case-fold-search' is
  4108. expected to be bound to nil when matching against this regexp.")
  4109. (defvar-local org-not-done-regexp nil
  4110. "Matches any of the TODO state keywords except the last one.
  4111. Since TODO keywords are case-sensitive, `case-fold-search' is
  4112. expected to be bound to nil when matching against this regexp.")
  4113. (defvar-local org-not-done-heading-regexp nil
  4114. "Matches a TODO headline that is not done.
  4115. Since TODO keywords are case-sensitive, `case-fold-search' is
  4116. expected to be bound to nil when matching against this regexp.")
  4117. (defvar-local org-todo-line-regexp nil
  4118. "Matches a headline and puts TODO state into group 2 if present.
  4119. Since TODO keywords are case-sensitive, `case-fold-search' is
  4120. expected to be bound to nil when matching against this regexp.")
  4121. (defvar-local org-complex-heading-regexp nil
  4122. "Matches a headline and puts everything into groups:
  4123. group 1: Stars
  4124. group 2: The TODO keyword, maybe
  4125. group 3: Priority cookie
  4126. group 4: True headline
  4127. group 5: Tags
  4128. Since TODO keywords are case-sensitive, `case-fold-search' is
  4129. expected to be bound to nil when matching against this regexp.")
  4130. (defvar-local org-complex-heading-regexp-format nil
  4131. "Printf format to make regexp to match an exact headline.
  4132. This regexp will match the headline of any node which has the
  4133. exact headline text that is put into the format, but may have any
  4134. TODO state, priority and tags.")
  4135. (defvar-local org-todo-line-tags-regexp nil
  4136. "Matches a headline and puts TODO state into group 2 if present.
  4137. Also put tags into group 4 if tags are present.")
  4138. (defconst org-plain-time-of-day-regexp
  4139. (concat
  4140. "\\(\\<[012]?[0-9]"
  4141. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4142. "\\(--?"
  4143. "\\(\\<[012]?[0-9]"
  4144. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4145. "\\)?")
  4146. "Regular expression to match a plain time or time range.
  4147. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4148. groups carry important information:
  4149. 0 the full match
  4150. 1 the first time, range or not
  4151. 8 the second time, if it is a range.")
  4152. (defconst org-plain-time-extension-regexp
  4153. (concat
  4154. "\\(\\<[012]?[0-9]"
  4155. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4156. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4157. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4158. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4159. groups carry important information:
  4160. 0 the full match
  4161. 7 hours of duration
  4162. 9 minutes of duration")
  4163. (defconst org-stamp-time-of-day-regexp
  4164. (concat
  4165. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4166. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4167. "\\(--?"
  4168. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4169. "Regular expression to match a timestamp time or time range.
  4170. After a match, the following groups carry important information:
  4171. 0 the full match
  4172. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4173. 2 the first time, range or not
  4174. 4 the second time, if it is a range.")
  4175. (defconst org-startup-options
  4176. '(("fold" org-startup-folded t)
  4177. ("overview" org-startup-folded t)
  4178. ("nofold" org-startup-folded nil)
  4179. ("showall" org-startup-folded nil)
  4180. ("showeverything" org-startup-folded showeverything)
  4181. ("content" org-startup-folded content)
  4182. ("indent" org-startup-indented t)
  4183. ("noindent" org-startup-indented nil)
  4184. ("hidestars" org-hide-leading-stars t)
  4185. ("showstars" org-hide-leading-stars nil)
  4186. ("odd" org-odd-levels-only t)
  4187. ("oddeven" org-odd-levels-only nil)
  4188. ("align" org-startup-align-all-tables t)
  4189. ("noalign" org-startup-align-all-tables nil)
  4190. ("inlineimages" org-startup-with-inline-images t)
  4191. ("noinlineimages" org-startup-with-inline-images nil)
  4192. ("latexpreview" org-startup-with-latex-preview t)
  4193. ("nolatexpreview" org-startup-with-latex-preview nil)
  4194. ("customtime" org-display-custom-times t)
  4195. ("logdone" org-log-done time)
  4196. ("lognotedone" org-log-done note)
  4197. ("nologdone" org-log-done nil)
  4198. ("lognoteclock-out" org-log-note-clock-out t)
  4199. ("nolognoteclock-out" org-log-note-clock-out nil)
  4200. ("logrepeat" org-log-repeat state)
  4201. ("lognoterepeat" org-log-repeat note)
  4202. ("logdrawer" org-log-into-drawer t)
  4203. ("nologdrawer" org-log-into-drawer nil)
  4204. ("logstatesreversed" org-log-states-order-reversed t)
  4205. ("nologstatesreversed" org-log-states-order-reversed nil)
  4206. ("nologrepeat" org-log-repeat nil)
  4207. ("logreschedule" org-log-reschedule time)
  4208. ("lognotereschedule" org-log-reschedule note)
  4209. ("nologreschedule" org-log-reschedule nil)
  4210. ("logredeadline" org-log-redeadline time)
  4211. ("lognoteredeadline" org-log-redeadline note)
  4212. ("nologredeadline" org-log-redeadline nil)
  4213. ("logrefile" org-log-refile time)
  4214. ("lognoterefile" org-log-refile note)
  4215. ("nologrefile" org-log-refile nil)
  4216. ("fninline" org-footnote-define-inline t)
  4217. ("nofninline" org-footnote-define-inline nil)
  4218. ("fnlocal" org-footnote-section nil)
  4219. ("fnauto" org-footnote-auto-label t)
  4220. ("fnprompt" org-footnote-auto-label nil)
  4221. ("fnconfirm" org-footnote-auto-label confirm)
  4222. ("fnplain" org-footnote-auto-label plain)
  4223. ("fnadjust" org-footnote-auto-adjust t)
  4224. ("nofnadjust" org-footnote-auto-adjust nil)
  4225. ("constcgs" constants-unit-system cgs)
  4226. ("constSI" constants-unit-system SI)
  4227. ("noptag" org-tag-persistent-alist nil)
  4228. ("hideblocks" org-hide-block-startup t)
  4229. ("nohideblocks" org-hide-block-startup nil)
  4230. ("beamer" org-startup-with-beamer-mode t)
  4231. ("entitiespretty" org-pretty-entities t)
  4232. ("entitiesplain" org-pretty-entities nil))
  4233. "Variable associated with STARTUP options for org-mode.
  4234. Each element is a list of three items: the startup options (as written
  4235. in the #+STARTUP line), the corresponding variable, and the value to set
  4236. this variable to if the option is found. An optional forth element PUSH
  4237. means to push this value onto the list in the variable.")
  4238. (defcustom org-group-tags t
  4239. "When non-nil (the default), use group tags.
  4240. This can be turned on/off through `org-toggle-tags-groups'."
  4241. :group 'org-tags
  4242. :group 'org-startup
  4243. :type 'boolean)
  4244. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4245. (defun org-toggle-tags-groups ()
  4246. "Toggle support for group tags.
  4247. Support for group tags is controlled by the option
  4248. `org-group-tags', which is non-nil by default."
  4249. (interactive)
  4250. (setq org-group-tags (not org-group-tags))
  4251. (cond ((and (derived-mode-p 'org-agenda-mode)
  4252. org-group-tags)
  4253. (org-agenda-redo))
  4254. ((derived-mode-p 'org-mode)
  4255. (let ((org-inhibit-startup t)) (org-mode))))
  4256. (message "Groups tags support has been turned %s"
  4257. (if org-group-tags "on" "off")))
  4258. (defun org-set-regexps-and-options (&optional tags-only)
  4259. "Precompute regular expressions used in the current buffer.
  4260. When optional argument TAGS-ONLY is non-nil, only compute tags
  4261. related expressions."
  4262. (when (derived-mode-p 'org-mode)
  4263. (let ((alist (org--setup-collect-keywords
  4264. (org-make-options-regexp
  4265. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4266. (and (not tags-only)
  4267. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4268. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4269. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4270. ;; Startup options. Get this early since it does change
  4271. ;; behavior for other options (e.g., tags).
  4272. (let ((startup (cdr (assq 'startup alist))))
  4273. (dolist (option startup)
  4274. (let ((entry (assoc-string option org-startup-options t)))
  4275. (when entry
  4276. (let ((var (nth 1 entry))
  4277. (val (nth 2 entry)))
  4278. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4279. (unless (listp (symbol-value var))
  4280. (set (make-local-variable var) nil))
  4281. (add-to-list var val)))))))
  4282. (setq-local org-file-tags
  4283. (mapcar #'org-add-prop-inherited
  4284. (cdr (assq 'filetags alist))))
  4285. (setq org-current-tag-alist
  4286. (append org-tag-persistent-alist
  4287. (let ((tags (cdr (assq 'tags alist))))
  4288. (if tags (org-tag-string-to-alist tags)
  4289. org-tag-alist))))
  4290. (setq org-tag-groups-alist
  4291. (org-tag-alist-to-groups org-current-tag-alist))
  4292. (unless tags-only
  4293. ;; File properties.
  4294. (setq-local org-file-properties (cdr (assq 'property alist)))
  4295. ;; Archive location.
  4296. (let ((archive (cdr (assq 'archive alist))))
  4297. (when archive (setq-local org-archive-location archive)))
  4298. ;; Category.
  4299. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4300. (when cat
  4301. (setq-local org-category (intern cat))
  4302. (setq-local org-file-properties
  4303. (org--update-property-plist
  4304. "CATEGORY" cat org-file-properties))))
  4305. ;; Columns.
  4306. (let ((column (cdr (assq 'columns alist))))
  4307. (when column (setq-local org-columns-default-format column)))
  4308. ;; Constants.
  4309. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4310. ;; Link abbreviations.
  4311. (let ((links (cdr (assq 'link alist))))
  4312. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4313. ;; Priorities.
  4314. (let ((priorities (cdr (assq 'priorities alist))))
  4315. (when priorities
  4316. (setq-local org-highest-priority (nth 0 priorities))
  4317. (setq-local org-lowest-priority (nth 1 priorities))
  4318. (setq-local org-default-priority (nth 2 priorities))))
  4319. ;; Scripts.
  4320. (let ((scripts (assq 'scripts alist)))
  4321. (when scripts
  4322. (setq-local org-use-sub-superscripts (cdr scripts))))
  4323. ;; TODO keywords.
  4324. (setq-local org-todo-kwd-alist nil)
  4325. (setq-local org-todo-key-alist nil)
  4326. (setq-local org-todo-key-trigger nil)
  4327. (setq-local org-todo-keywords-1 nil)
  4328. (setq-local org-done-keywords nil)
  4329. (setq-local org-todo-heads nil)
  4330. (setq-local org-todo-sets nil)
  4331. (setq-local org-todo-log-states nil)
  4332. (let ((todo-sequences
  4333. (or (nreverse (cdr (assq 'todo alist)))
  4334. (let ((d (default-value 'org-todo-keywords)))
  4335. (if (not (stringp (car d))) d
  4336. ;; XXX: Backward compatibility code.
  4337. (list (cons org-todo-interpretation d)))))))
  4338. (dolist (sequence todo-sequences)
  4339. (let* ((sequence (or (run-hook-with-args-until-success
  4340. 'org-todo-setup-filter-hook sequence)
  4341. sequence))
  4342. (sequence-type (car sequence))
  4343. (keywords (cdr sequence))
  4344. (sep (member "|" keywords))
  4345. names alist)
  4346. (dolist (k (remove "|" keywords))
  4347. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4348. k)
  4349. (error "Invalid TODO keyword %s" k))
  4350. (let ((name (match-string 1 k))
  4351. (key (match-string 2 k))
  4352. (log (org-extract-log-state-settings k)))
  4353. (push name names)
  4354. (push (cons name (and key (string-to-char key))) alist)
  4355. (when log (push log org-todo-log-states))))
  4356. (let* ((names (nreverse names))
  4357. (done (if sep (org-remove-keyword-keys (cdr sep))
  4358. (last names)))
  4359. (head (car names))
  4360. (tail (list sequence-type head (car done) (org-last done))))
  4361. (add-to-list 'org-todo-heads head 'append)
  4362. (push names org-todo-sets)
  4363. (setq org-done-keywords (append org-done-keywords done nil))
  4364. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4365. (setq org-todo-key-alist
  4366. (append org-todo-key-alist
  4367. (and alist
  4368. (append '((:startgroup))
  4369. (nreverse alist)
  4370. '((:endgroup))))))
  4371. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4372. (setq org-todo-sets (nreverse org-todo-sets)
  4373. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4374. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4375. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4376. ;; Compute the regular expressions and other local variables.
  4377. ;; Using `org-outline-regexp-bol' would complicate them much,
  4378. ;; because of the fixed white space at the end of that string.
  4379. (unless org-done-keywords
  4380. (setq org-done-keywords
  4381. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4382. (setq org-not-done-keywords
  4383. (org-delete-all org-done-keywords
  4384. (copy-sequence org-todo-keywords-1))
  4385. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4386. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4387. org-not-done-heading-regexp
  4388. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4389. org-todo-line-regexp
  4390. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4391. org-complex-heading-regexp
  4392. (concat "^\\(\\*+\\)"
  4393. "\\(?: +" org-todo-regexp "\\)?"
  4394. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4395. "\\(?: +\\(.*?\\)\\)??"
  4396. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
  4397. "[ \t]*$")
  4398. org-complex-heading-regexp-format
  4399. (concat "^\\(\\*+\\)"
  4400. "\\(?: +" org-todo-regexp "\\)?"
  4401. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4402. "\\(?: +"
  4403. ;; Stats cookies can be stuck to body.
  4404. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4405. "\\(%s\\)"
  4406. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4407. "\\)"
  4408. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
  4409. "[ \t]*$")
  4410. org-todo-line-tags-regexp
  4411. (concat "^\\(\\*+\\)"
  4412. "\\(?: +" org-todo-regexp "\\)?"
  4413. "\\(?: +\\(.*?\\)\\)??"
  4414. "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
  4415. "[ \t]*$"))
  4416. (org-compute-latex-and-related-regexp)))))
  4417. (defun org--setup-collect-keywords (regexp &optional files alist)
  4418. "Return setup keywords values as an alist.
  4419. REGEXP matches a subset of setup keywords. FILES is a list of
  4420. file names already visited. It is used to avoid circular setup
  4421. files. ALIST, when non-nil, is the alist computed so far.
  4422. Return value contains the following keys: `archive', `category',
  4423. `columns', `constants', `filetags', `link', `priorities',
  4424. `property', `scripts', `startup', `tags' and `todo'."
  4425. (org-with-wide-buffer
  4426. (goto-char (point-min))
  4427. (let ((case-fold-search t))
  4428. (while (re-search-forward regexp nil t)
  4429. (let ((element (org-element-at-point)))
  4430. (when (eq (org-element-type element) 'keyword)
  4431. (let ((key (org-element-property :key element))
  4432. (value (org-element-property :value element)))
  4433. (cond
  4434. ((equal key "ARCHIVE")
  4435. (when (org-string-nw-p value)
  4436. (push (cons 'archive value) alist)))
  4437. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4438. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4439. ((equal key "CONSTANTS")
  4440. (let* ((constants (assq 'constants alist))
  4441. (store (cdr constants)))
  4442. (dolist (pair (org-split-string value))
  4443. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4444. pair)
  4445. (let* ((name (match-string 1 pair))
  4446. (value (match-string 2 pair))
  4447. (old (assoc name store)))
  4448. (if old (setcdr old value)
  4449. (push (cons name value) store)))))
  4450. (if constants (setcdr constants store)
  4451. (push (cons 'constants store) alist))))
  4452. ((equal key "FILETAGS")
  4453. (when (org-string-nw-p value)
  4454. (let ((old (assq 'filetags alist))
  4455. (new (apply #'nconc
  4456. (mapcar (lambda (x) (org-split-string x ":"))
  4457. (org-split-string value)))))
  4458. (if old (setcdr old (append new (cdr old)))
  4459. (push (cons 'filetags new) alist)))))
  4460. ((equal key "LINK")
  4461. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4462. (let ((links (assq 'link alist))
  4463. (pair (cons (match-string-no-properties 1 value)
  4464. (match-string-no-properties 2 value))))
  4465. (if links (push pair (cdr links))
  4466. (push (list 'link pair) alist)))))
  4467. ((equal key "OPTIONS")
  4468. (when (and (org-string-nw-p value)
  4469. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4470. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4471. ((equal key "PRIORITIES")
  4472. (push (cons 'priorities
  4473. (let ((prio (org-split-string value)))
  4474. (if (< (length prio) 3) '(?A ?C ?B)
  4475. (mapcar #'string-to-char prio))))
  4476. alist))
  4477. ((equal key "PROPERTY")
  4478. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4479. (let* ((property (assq 'property alist))
  4480. (value (org--update-property-plist
  4481. (match-string-no-properties 1 value)
  4482. (match-string-no-properties 2 value)
  4483. (cdr property))))
  4484. (if property (setcdr property value)
  4485. (push (cons 'property value) alist)))))
  4486. ((equal key "STARTUP")
  4487. (let ((startup (assq 'startup alist)))
  4488. (if startup
  4489. (setcdr startup
  4490. (append (cdr startup) (org-split-string value)))
  4491. (push (cons 'startup (org-split-string value)) alist))))
  4492. ((equal key "TAGS")
  4493. (let ((tag-cell (assq 'tags alist)))
  4494. (if tag-cell
  4495. (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
  4496. (push (cons 'tags value) alist))))
  4497. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4498. (let ((todo (assq 'todo alist))
  4499. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4500. (org-split-string value))))
  4501. (if todo (push value (cdr todo))
  4502. (push (list 'todo value) alist))))
  4503. ((equal key "SETUPFILE")
  4504. (unless buffer-read-only ; Do not check in Gnus messages.
  4505. (let ((f (and (org-string-nw-p value)
  4506. (expand-file-name
  4507. (org-unbracket-string "\"" "\"" value)))))
  4508. (when (and f (file-readable-p f) (not (member f files)))
  4509. (with-temp-buffer
  4510. (setq default-directory (file-name-directory f))
  4511. (insert-file-contents f)
  4512. (setq alist
  4513. ;; Fake Org mode to benefit from cache
  4514. ;; without recurring needlessly.
  4515. (let ((major-mode 'org-mode))
  4516. (org--setup-collect-keywords
  4517. regexp (cons f files) alist)))))))))))))))
  4518. alist)
  4519. (defun org-tag-string-to-alist (s)
  4520. "Return tag alist associated to string S.
  4521. S is a value for TAGS keyword or produced with
  4522. `org-tag-alist-to-string'. Return value is an alist suitable for
  4523. `org-tag-alist' or `org-tag-persistent-alist'."
  4524. (let ((lines (mapcar #'split-string (split-string s "\n" t)))
  4525. (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
  4526. "\\|{.+?}\\)" ; regular expression
  4527. "\\(?:(\\(.\\))\\)?\\'"))
  4528. alist group-flag)
  4529. (dolist (tokens lines (cdr (nreverse alist)))
  4530. (push '(:newline) alist)
  4531. (while tokens
  4532. (let ((token (pop tokens)))
  4533. (pcase token
  4534. ("{"
  4535. (push '(:startgroup) alist)
  4536. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4537. ("}"
  4538. (push '(:endgroup) alist)
  4539. (setq group-flag nil))
  4540. ("["
  4541. (push '(:startgrouptag) alist)
  4542. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4543. ("]"
  4544. (push '(:endgrouptag) alist)
  4545. (setq group-flag nil))
  4546. (":"
  4547. (push '(:grouptags) alist))
  4548. ((guard (string-match tag-re token))
  4549. (let ((tag (match-string 1 token))
  4550. (key (and (match-beginning 2)
  4551. (string-to-char (match-string 2 token)))))
  4552. ;; Push all tags in groups, no matter if they already
  4553. ;; appear somewhere else in the list.
  4554. (when (or group-flag (not (assoc tag alist)))
  4555. (push (cons tag key) alist))))))))))
  4556. (defun org-tag-alist-to-string (alist &optional skip-key)
  4557. "Return tag string associated to ALIST.
  4558. ALIST is an alist, as defined in `org-tag-alist' or
  4559. `org-tag-persistent-alist', or produced with
  4560. `org-tag-string-to-alist'.
  4561. Return value is a string suitable as a value for \"TAGS\"
  4562. keyword.
  4563. When optional argument SKIP-KEY is non-nil, skip selection keys
  4564. next to tags."
  4565. (mapconcat (lambda (token)
  4566. (pcase token
  4567. (`(:startgroup) "{")
  4568. (`(:endgroup) "}")
  4569. (`(:startgrouptag) "[")
  4570. (`(:endgrouptag) "]")
  4571. (`(:grouptags) ":")
  4572. (`(:newline) "\\n")
  4573. ((and
  4574. (guard (not skip-key))
  4575. `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
  4576. (format "%s(%c)" tag key))
  4577. (`(,(and tag (pred stringp)) . ,_) tag)
  4578. (_ (user-error "Invalid tag token: %S" token))))
  4579. alist
  4580. " "))
  4581. (defun org-tag-alist-to-groups (alist)
  4582. "Return group alist from tag ALIST.
  4583. ALIST is an alist, as defined in `org-tag-alist' or
  4584. `org-tag-persistent-alist', or produced with
  4585. `org-tag-string-to-alist'. Return value is an alist following
  4586. the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
  4587. a string, summarizing TAGS, as a list of strings."
  4588. (let (groups group-status current-group)
  4589. (dolist (token alist (nreverse groups))
  4590. (pcase token
  4591. (`(,(or :startgroup :startgrouptag)) (setq group-status t))
  4592. (`(,(or :endgroup :endgrouptag))
  4593. (when (eq group-status 'append)
  4594. (push (nreverse current-group) groups))
  4595. (setq group-status nil))
  4596. (`(:grouptags) (setq group-status 'append))
  4597. ((and `(,tag . ,_) (guard group-status))
  4598. (if (eq group-status 'append) (push tag current-group)
  4599. (setq current-group (list tag))))
  4600. (_ nil)))))
  4601. (defvar org--file-cache (make-hash-table :test #'equal)
  4602. "Hash table to store contents of files referenced via a URL.
  4603. This is the cache of file URLs read using `org-file-contents'.")
  4604. (defun org-reset-file-cache ()
  4605. "Reset the cache of files downloaded by `org-file-contents'."
  4606. (clrhash org--file-cache))
  4607. (defun org-file-url-p (file)
  4608. "Non-nil if FILE is a URL."
  4609. (require 'ffap)
  4610. (string-match-p ffap-url-regexp file))
  4611. (defun org-file-contents (file &optional noerror nocache)
  4612. "Return the contents of FILE, as a string.
  4613. FILE can be a file name or URL.
  4614. If FILE is a URL, download the contents. If the URL contents are
  4615. already cached in the `org--file-cache' hash table, the download step
  4616. is skipped.
  4617. If NOERROR is non-nil, ignore the error when unable to read the FILE
  4618. from file or URL.
  4619. If NOCACHE is non-nil, do a fresh fetch of FILE even if cached version
  4620. is available. This option applies only if FILE is a URL."
  4621. (let* ((is-url (org-file-url-p file))
  4622. (cache (and is-url
  4623. (not nocache)
  4624. (gethash file org--file-cache))))
  4625. (cond
  4626. (cache)
  4627. (is-url
  4628. (with-current-buffer (url-retrieve-synchronously file)
  4629. (goto-char (point-min))
  4630. ;; Move point to after the url-retrieve header.
  4631. (search-forward "\n\n" nil :move)
  4632. ;; Search for the success code only in the url-retrieve header.
  4633. (if (save-excursion (re-search-backward "HTTP.*\\s-+200\\s-OK" nil :noerror))
  4634. ;; Update the cache `org--file-cache' and return contents.
  4635. (puthash file
  4636. (buffer-substring-no-properties (point) (point-max))
  4637. org--file-cache)
  4638. (funcall (if noerror #'message #'user-error)
  4639. "Unable to fetch file from %S"
  4640. file))))
  4641. (t
  4642. (with-temp-buffer
  4643. (condition-case err
  4644. (progn
  4645. (insert-file-contents file)
  4646. (buffer-string))
  4647. (file-error
  4648. (funcall (if noerror #'message #'user-error)
  4649. (error-message-string err)))))))))
  4650. (defun org-extract-log-state-settings (x)
  4651. "Extract the log state setting from a TODO keyword string.
  4652. This will extract info from a string like \"WAIT(w@/!)\"."
  4653. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4654. (let ((kw (match-string 1 x))
  4655. (log1 (and (match-end 3) (match-string 3 x)))
  4656. (log2 (and (match-end 4) (match-string 4 x))))
  4657. (and (or log1 log2)
  4658. (list kw
  4659. (and log1 (if (equal log1 "!") 'time 'note))
  4660. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4661. (defun org-remove-keyword-keys (list)
  4662. "Remove a pair of parenthesis at the end of each string in LIST."
  4663. (mapcar (lambda (x)
  4664. (if (string-match "(.*)$" x)
  4665. (substring x 0 (match-beginning 0))
  4666. x))
  4667. list))
  4668. (defun org-assign-fast-keys (alist)
  4669. "Assign fast keys to a keyword-key alist.
  4670. Respect keys that are already there."
  4671. (let (new e (alt ?0))
  4672. (while (setq e (pop alist))
  4673. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4674. (cdr e)) ;; Key already assigned.
  4675. (push e new)
  4676. (let ((clist (string-to-list (downcase (car e))))
  4677. (used (append new alist)))
  4678. (when (= (car clist) ?@)
  4679. (pop clist))
  4680. (while (and clist (rassoc (car clist) used))
  4681. (pop clist))
  4682. (unless clist
  4683. (while (rassoc alt used)
  4684. (cl-incf alt)))
  4685. (push (cons (car e) (or (car clist) alt)) new))))
  4686. (nreverse new)))
  4687. ;;; Some variables used in various places
  4688. (defvar org-window-configuration nil
  4689. "Used in various places to store a window configuration.")
  4690. (defvar org-selected-window nil
  4691. "Used in various places to store a window configuration.")
  4692. (defvar org-finish-function nil
  4693. "Function to be called when `C-c C-c' is used.
  4694. This is for getting out of special buffers like capture.")
  4695. (defvar org-last-state)
  4696. ;; Defined somewhere in this file, but used before definition.
  4697. (defvar org-entities) ;; defined in org-entities.el
  4698. (defvar org-struct-menu)
  4699. (defvar org-org-menu)
  4700. (defvar org-tbl-menu)
  4701. ;;;; Define the Org mode
  4702. ;; We use a before-change function to check if a table might need
  4703. ;; an update.
  4704. (defvar org-table-may-need-update t
  4705. "Indicates that a table might need an update.
  4706. This variable is set by `org-before-change-function'.
  4707. `org-table-align' sets it back to nil.")
  4708. (defun org-before-change-function (_beg _end)
  4709. "Every change indicates that a table might need an update."
  4710. (setq org-table-may-need-update t))
  4711. (defvar org-mode-map)
  4712. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4713. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4714. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4715. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4716. (defvar org-table-buffer-is-an nil)
  4717. (defvar bidi-paragraph-direction)
  4718. (defvar buffer-face-mode-face)
  4719. (require 'outline)
  4720. ;; Other stuff we need.
  4721. (require 'time-date)
  4722. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4723. (require 'easymenu)
  4724. (autoload 'easy-menu-add "easymenu")
  4725. (require 'overlay)
  4726. ;; (require 'org-macs) moved higher up in the file before it is first used
  4727. (require 'org-entities)
  4728. ;; (require 'org-compat) moved higher up in the file before it is first used
  4729. (require 'org-faces)
  4730. (require 'org-list)
  4731. (require 'org-pcomplete)
  4732. (require 'org-src)
  4733. (require 'org-footnote)
  4734. (require 'org-macro)
  4735. ;; babel
  4736. (require 'ob)
  4737. ;;;###autoload
  4738. (define-derived-mode org-mode outline-mode "Org"
  4739. "Outline-based notes management and organizer, alias
  4740. \"Carsten's outline-mode for keeping track of everything.\"
  4741. Org mode develops organizational tasks around a NOTES file which
  4742. contains information about projects as plain text. Org mode is
  4743. implemented on top of Outline mode, which is ideal to keep the content
  4744. of large files well structured. It supports ToDo items, deadlines and
  4745. time stamps, which magically appear in the diary listing of the Emacs
  4746. calendar. Tables are easily created with a built-in table editor.
  4747. Plain text URL-like links connect to websites, emails (VM), Usenet
  4748. messages (Gnus), BBDB entries, and any files related to the project.
  4749. For printing and sharing of notes, an Org file (or a part of it)
  4750. can be exported as a structured ASCII or HTML file.
  4751. The following commands are available:
  4752. \\{org-mode-map}"
  4753. ;; Get rid of Outline menus, they are not needed
  4754. ;; Need to do this here because define-derived-mode sets up
  4755. ;; the keymap so late. Still, it is a waste to call this each time
  4756. ;; we switch another buffer into Org mode.
  4757. (define-key org-mode-map [menu-bar headings] 'undefined)
  4758. (define-key org-mode-map [menu-bar hide] 'undefined)
  4759. (define-key org-mode-map [menu-bar show] 'undefined)
  4760. (org-load-modules-maybe)
  4761. (org-install-agenda-files-menu)
  4762. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4763. (add-to-invisibility-spec '(org-cwidth))
  4764. (add-to-invisibility-spec '(org-hide-block . t))
  4765. (setq-local outline-regexp org-outline-regexp)
  4766. (setq-local outline-level 'org-outline-level)
  4767. (setq bidi-paragraph-direction 'left-to-right)
  4768. (when (and (stringp org-ellipsis) (not (equal "" org-ellipsis)))
  4769. (unless org-display-table
  4770. (setq org-display-table (make-display-table)))
  4771. (set-display-table-slot
  4772. org-display-table 4
  4773. (vconcat (mapcar (lambda (c) (make-glyph-code c 'org-ellipsis))
  4774. org-ellipsis)))
  4775. (setq buffer-display-table org-display-table))
  4776. (org-set-regexps-and-options)
  4777. (org-set-font-lock-defaults)
  4778. (when (and org-tag-faces (not org-tags-special-faces-re))
  4779. ;; tag faces set outside customize.... force initialization.
  4780. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4781. ;; Calc embedded
  4782. (setq-local calc-embedded-open-mode "# ")
  4783. ;; Modify a few syntax entries
  4784. (modify-syntax-entry ?@ "w")
  4785. (modify-syntax-entry ?\" "\"")
  4786. (modify-syntax-entry ?\\ "_")
  4787. (modify-syntax-entry ?~ "_")
  4788. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4789. ;; Activate before-change-function
  4790. (setq-local org-table-may-need-update t)
  4791. (add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4792. ;; Check for running clock before killing a buffer
  4793. (add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4794. ;; Initialize macros templates.
  4795. (org-macro-initialize-templates)
  4796. ;; Initialize radio targets.
  4797. (org-update-radio-target-regexp)
  4798. ;; Indentation.
  4799. (setq-local indent-line-function 'org-indent-line)
  4800. (setq-local indent-region-function 'org-indent-region)
  4801. ;; Filling and auto-filling.
  4802. (org-setup-filling)
  4803. ;; Comments.
  4804. (org-setup-comments-handling)
  4805. ;; Initialize cache.
  4806. (org-element-cache-reset)
  4807. ;; Beginning/end of defun
  4808. (setq-local beginning-of-defun-function 'org-backward-element)
  4809. (setq-local end-of-defun-function
  4810. (lambda ()
  4811. (if (not (org-at-heading-p))
  4812. (org-forward-element)
  4813. (org-forward-element)
  4814. (forward-char -1))))
  4815. ;; Next error for sparse trees
  4816. (setq-local next-error-function 'org-occur-next-match)
  4817. ;; Make sure dependence stuff works reliably, even for users who set it
  4818. ;; too late :-(
  4819. (if org-enforce-todo-dependencies
  4820. (add-hook 'org-blocker-hook
  4821. 'org-block-todo-from-children-or-siblings-or-parent)
  4822. (remove-hook 'org-blocker-hook
  4823. 'org-block-todo-from-children-or-siblings-or-parent))
  4824. (if org-enforce-todo-checkbox-dependencies
  4825. (add-hook 'org-blocker-hook
  4826. 'org-block-todo-from-checkboxes)
  4827. (remove-hook 'org-blocker-hook
  4828. 'org-block-todo-from-checkboxes))
  4829. ;; Align options lines
  4830. (setq-local
  4831. align-mode-rules-list
  4832. '((org-in-buffer-settings
  4833. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4834. (modes . '(org-mode)))))
  4835. ;; Imenu
  4836. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4837. ;; Make isearch reveal context
  4838. (setq-local outline-isearch-open-invisible-function
  4839. (lambda (&rest _) (org-show-context 'isearch)))
  4840. ;; Setup the pcomplete hooks
  4841. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4842. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4843. (setq-local pcomplete-default-completion-function 'ignore)
  4844. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4845. (setq-local pcomplete-termination-string "")
  4846. (setq-local buffer-face-mode-face 'org-default)
  4847. ;; If empty file that did not turn on Org mode automatically, make
  4848. ;; it to.
  4849. (when (and org-insert-mode-line-in-empty-file
  4850. (called-interactively-p 'any)
  4851. (= (point-min) (point-max)))
  4852. (insert "# -*- mode: org -*-\n\n"))
  4853. (unless org-inhibit-startup
  4854. (org-unmodified
  4855. (when org-startup-with-beamer-mode (org-beamer-mode))
  4856. (when org-startup-align-all-tables
  4857. (org-table-map-tables #'org-table-align t))
  4858. (when org-startup-with-inline-images (org-display-inline-images))
  4859. (when org-startup-with-latex-preview (org-toggle-latex-fragment '(16)))
  4860. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4861. (when org-startup-truncated (setq truncate-lines t))
  4862. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4863. (org-refresh-effort-properties)))
  4864. ;; Try to set `org-hide' face correctly.
  4865. (let ((foreground (org-find-invisible-foreground)))
  4866. (when foreground
  4867. (set-face-foreground 'org-hide foreground))))
  4868. ;; Update `customize-package-emacs-version-alist'
  4869. (add-to-list 'customize-package-emacs-version-alist
  4870. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4871. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4872. ("8.2.6" . "24.4") ("8.2.10" . "24.5")
  4873. ("9.0" . "26.1")))
  4874. (defvar org-mode-transpose-word-syntax-table
  4875. (let ((st (make-syntax-table text-mode-syntax-table)))
  4876. (dolist (c org-emphasis-alist st)
  4877. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4878. (when (fboundp 'abbrev-table-put)
  4879. (abbrev-table-put org-mode-abbrev-table
  4880. :parents (list text-mode-abbrev-table)))
  4881. (defun org-find-invisible-foreground ()
  4882. (let ((candidates (remove
  4883. "unspecified-bg"
  4884. (nconc
  4885. (list (face-background 'default)
  4886. (face-background 'org-default))
  4887. (mapcar
  4888. (lambda (alist)
  4889. (when (boundp alist)
  4890. (cdr (assq 'background-color (symbol-value alist)))))
  4891. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4892. (list (face-foreground 'org-hide))))))
  4893. (car (remove nil candidates))))
  4894. (defun org-current-time (&optional rounding-minutes past)
  4895. "Current time, possibly rounded to ROUNDING-MINUTES.
  4896. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4897. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4898. the rounding returns a past time."
  4899. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4900. (car org-time-stamp-rounding-minutes)))
  4901. (time (decode-time)) res)
  4902. (if (< r 1)
  4903. (current-time)
  4904. (setq res
  4905. (apply 'encode-time
  4906. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4907. (nthcdr 2 time))))
  4908. (if (and past (< (float-time (time-subtract (current-time) res)) 0))
  4909. (seconds-to-time (- (float-time res) (* r 60)))
  4910. res))))
  4911. (defun org-today ()
  4912. "Return today date, considering `org-extend-today-until'."
  4913. (time-to-days
  4914. (time-subtract (current-time)
  4915. (list 0 (* 3600 org-extend-today-until) 0))))
  4916. ;;;; Font-Lock stuff, including the activators
  4917. (defvar org-mouse-map (make-sparse-keymap))
  4918. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4919. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4920. (when org-mouse-1-follows-link
  4921. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4922. (when org-tab-follows-link
  4923. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4924. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4925. (require 'font-lock)
  4926. (defconst org-non-link-chars "]\t\n\r<>")
  4927. (defvar org-link-types-re nil
  4928. "Matches a link that has a url-like prefix like \"http:\"")
  4929. (defvar org-link-re-with-space nil
  4930. "Matches a link with spaces, optional angular brackets around it.")
  4931. (defvar org-link-re-with-space2 nil
  4932. "Matches a link with spaces, optional angular brackets around it.")
  4933. (defvar org-link-re-with-space3 nil
  4934. "Matches a link with spaces, only for internal part in bracket links.")
  4935. (defvar org-angle-link-re nil
  4936. "Matches link with angular brackets, spaces are allowed.")
  4937. (defvar org-plain-link-re nil
  4938. "Matches plain link, without spaces.")
  4939. (defvar org-bracket-link-regexp nil
  4940. "Matches a link in double brackets.")
  4941. (defvar org-bracket-link-analytic-regexp nil
  4942. "Regular expression used to analyze links.
  4943. Here is what the match groups contain after a match:
  4944. 1: http:
  4945. 2: http
  4946. 3: path
  4947. 4: [desc]
  4948. 5: desc")
  4949. (defvar org-bracket-link-analytic-regexp++ nil
  4950. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4951. (defvar org-any-link-re nil
  4952. "Regular expression matching any link.")
  4953. (defconst org-match-sexp-depth 3
  4954. "Number of stacked braces for sub/superscript matching.")
  4955. (defun org-create-multibrace-regexp (left right n)
  4956. "Create a regular expression which will match a balanced sexp.
  4957. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4958. as single character strings.
  4959. The regexp returned will match the entire expression including the
  4960. delimiters. It will also define a single group which contains the
  4961. match except for the outermost delimiters. The maximum depth of
  4962. stacked delimiters is N. Escaping delimiters is not possible."
  4963. (let* ((nothing (concat "[^" left right "]*?"))
  4964. (or "\\|")
  4965. (re nothing)
  4966. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4967. (while (> n 1)
  4968. (setq n (1- n)
  4969. re (concat re or next)
  4970. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4971. (concat left "\\(" re "\\)" right)))
  4972. (defconst org-match-substring-regexp
  4973. (concat
  4974. "\\(\\S-\\)\\([_^]\\)\\("
  4975. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4976. "\\|"
  4977. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4978. "\\|"
  4979. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4980. "The regular expression matching a sub- or superscript.")
  4981. (defconst org-match-substring-with-braces-regexp
  4982. (concat
  4983. "\\(\\S-\\)\\([_^]\\)"
  4984. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4985. "The regular expression matching a sub- or superscript, forcing braces.")
  4986. (defun org-make-link-regexps ()
  4987. "Update the link regular expressions.
  4988. This should be called after the variable `org-link-parameters' has changed."
  4989. (let ((types-re (regexp-opt (org-link-types) t)))
  4990. (setq org-link-types-re
  4991. (concat "\\`" types-re ":")
  4992. org-link-re-with-space
  4993. (concat "<?" types-re ":"
  4994. "\\([^" org-non-link-chars " ]"
  4995. "[^" org-non-link-chars "]*"
  4996. "[^" org-non-link-chars " ]\\)>?")
  4997. org-link-re-with-space2
  4998. (concat "<?" types-re ":"
  4999. "\\([^" org-non-link-chars " ]"
  5000. "[^\t\n\r]*"
  5001. "[^" org-non-link-chars " ]\\)>?")
  5002. org-link-re-with-space3
  5003. (concat "<?" types-re ":"
  5004. "\\([^" org-non-link-chars " ]"
  5005. "[^\t\n\r]*\\)")
  5006. org-angle-link-re
  5007. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5008. types-re)
  5009. org-plain-link-re
  5010. (concat
  5011. "\\<" types-re ":"
  5012. "\\([^][ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)")
  5013. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5014. org-bracket-link-regexp
  5015. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5016. org-bracket-link-analytic-regexp
  5017. (concat
  5018. "\\[\\["
  5019. "\\(" types-re ":\\)?"
  5020. "\\([^]]+\\)"
  5021. "\\]"
  5022. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5023. "\\]")
  5024. org-bracket-link-analytic-regexp++
  5025. (concat
  5026. "\\[\\["
  5027. "\\(" (regexp-opt (cons "coderef" (org-link-types)) t) ":\\)?"
  5028. "\\([^]]+\\)"
  5029. "\\]"
  5030. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5031. "\\]")
  5032. org-any-link-re
  5033. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5034. org-angle-link-re "\\)\\|\\("
  5035. org-plain-link-re "\\)"))))
  5036. (org-make-link-regexps)
  5037. (defvar org-emph-face nil)
  5038. (defun org-do-emphasis-faces (limit)
  5039. "Run through the buffer and emphasize strings."
  5040. (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)"
  5041. (car org-emphasis-regexp-components))))
  5042. (catch :exit
  5043. (while (re-search-forward quick-re limit t)
  5044. (let* ((marker (match-string 2))
  5045. (verbatim? (member marker '("~" "="))))
  5046. (when (save-excursion
  5047. (goto-char (match-beginning 0))
  5048. ;; Do not match headline stars. Do not consider
  5049. ;; stars of a headline as closing marker for bold
  5050. ;; markup either. Do not match table hlines.
  5051. (and
  5052. (not (looking-at-p org-outline-regexp-bol))
  5053. (not (and (equal marker "+")
  5054. (org-match-line
  5055. "^[ \t]*\\(|[-+]+|?\\|\\+[-+]+\\+\\)[ \t]*$")))
  5056. (looking-at (if verbatim? org-verbatim-re org-emph-re))
  5057. (not (string-match-p
  5058. (concat org-outline-regexp-bol "\\'")
  5059. (match-string 0)))))
  5060. (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)))
  5061. (font-lock-prepend-text-property
  5062. (match-beginning 2) (match-end 2) 'face face)
  5063. (when verbatim?
  5064. (org-remove-flyspell-overlays-in
  5065. (match-beginning 0) (match-end 0)))
  5066. (add-text-properties (match-beginning 2) (match-end 2)
  5067. '(font-lock-multiline t org-emphasis t))
  5068. (when org-hide-emphasis-markers
  5069. (add-text-properties (match-end 4) (match-beginning 5)
  5070. '(invisible org-link))
  5071. (add-text-properties (match-beginning 3) (match-end 3)
  5072. '(invisible org-link)))
  5073. (throw :exit t))))))))
  5074. (defun org-emphasize (&optional char)
  5075. "Insert or change an emphasis, i.e. a font like bold or italic.
  5076. If there is an active region, change that region to a new emphasis.
  5077. If there is no region, just insert the marker characters and position
  5078. the cursor between them.
  5079. CHAR should be the marker character. If it is a space, it means to
  5080. remove the emphasis of the selected region.
  5081. If CHAR is not given (for example in an interactive call) it will be
  5082. prompted for."
  5083. (interactive)
  5084. (let ((erc org-emphasis-regexp-components)
  5085. (string "") beg end move s)
  5086. (if (org-region-active-p)
  5087. (setq beg (region-beginning)
  5088. end (region-end)
  5089. string (buffer-substring beg end))
  5090. (setq move t))
  5091. (unless char
  5092. (message "Emphasis marker or tag: [%s]"
  5093. (mapconcat #'car org-emphasis-alist ""))
  5094. (setq char (read-char-exclusive)))
  5095. (if (equal char ?\s)
  5096. (setq s ""
  5097. move nil)
  5098. (unless (assoc (char-to-string char) org-emphasis-alist)
  5099. (user-error "No such emphasis marker: \"%c\"" char))
  5100. (setq s (char-to-string char)))
  5101. (while (and (> (length string) 1)
  5102. (equal (substring string 0 1) (substring string -1))
  5103. (assoc (substring string 0 1) org-emphasis-alist))
  5104. (setq string (substring string 1 -1)))
  5105. (setq string (concat s string s))
  5106. (when beg (delete-region beg end))
  5107. (unless (or (bolp)
  5108. (string-match (concat "[" (nth 0 erc) "\n]")
  5109. (char-to-string (char-before (point)))))
  5110. (insert " "))
  5111. (unless (or (eobp)
  5112. (string-match (concat "[" (nth 1 erc) "\n]")
  5113. (char-to-string (char-after (point)))))
  5114. (insert " ") (backward-char 1))
  5115. (insert string)
  5116. (and move (backward-char 1))))
  5117. (defconst org-nonsticky-props
  5118. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5119. (defsubst org-rear-nonsticky-at (pos)
  5120. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5121. (defun org-activate-links (limit)
  5122. "Add link properties to links.
  5123. This includes angle, plain, and bracket links."
  5124. (catch :exit
  5125. (while (re-search-forward org-any-link-re limit t)
  5126. (let* ((start (match-beginning 0))
  5127. (end (match-end 0))
  5128. (style (cond ((eq ?< (char-after start)) 'angle)
  5129. ((eq ?\[ (char-after (1+ start))) 'bracket)
  5130. (t 'plain))))
  5131. (when (and (memq style org-highlight-links)
  5132. ;; Do not confuse plain links with tags.
  5133. (not (and (eq style 'plain)
  5134. (let ((face (get-text-property
  5135. (max (1- start) (point-min)) 'face)))
  5136. (if (consp face) (memq 'org-tag face)
  5137. (eq 'org-tag face))))))
  5138. (let* ((link-object (save-excursion
  5139. (goto-char start)
  5140. (save-match-data (org-element-link-parser))))
  5141. (link (org-element-property :raw-link link-object))
  5142. (type (org-element-property :type link-object))
  5143. (path (org-element-property :path link-object))
  5144. (properties ;for link's visible part
  5145. (list
  5146. 'face (pcase (org-link-get-parameter type :face)
  5147. ((and (pred functionp) face) (funcall face path))
  5148. ((and (pred facep) face) face)
  5149. ((and (pred consp) face) face) ;anonymous
  5150. (_ 'org-link))
  5151. 'mouse-face (or (org-link-get-parameter type :mouse-face)
  5152. 'highlight)
  5153. 'keymap (or (org-link-get-parameter type :keymap)
  5154. org-mouse-map)
  5155. 'help-echo (pcase (org-link-get-parameter type :help-echo)
  5156. ((and (pred stringp) echo) echo)
  5157. ((and (pred functionp) echo) echo)
  5158. (_ (concat "LINK: " link)))
  5159. 'htmlize-link (pcase (org-link-get-parameter type
  5160. :htmlize-link)
  5161. ((and (pred functionp) f) (funcall f))
  5162. (_ `(:uri ,link)))
  5163. 'font-lock-multiline t)))
  5164. (org-remove-flyspell-overlays-in start end)
  5165. (org-rear-nonsticky-at end)
  5166. (if (not (eq 'bracket style))
  5167. (add-text-properties start end properties)
  5168. ;; Handle invisible parts in bracket links.
  5169. (remove-text-properties start end '(invisible nil))
  5170. (let ((hidden
  5171. (append `(invisible
  5172. ,(or (org-link-get-parameter type :display)
  5173. 'org-link))
  5174. properties))
  5175. (visible-start (or (match-beginning 4) (match-beginning 2)))
  5176. (visible-end (or (match-end 4) (match-end 2))))
  5177. (add-text-properties start visible-start hidden)
  5178. (add-text-properties visible-start visible-end properties)
  5179. (add-text-properties visible-end end hidden)
  5180. (org-rear-nonsticky-at visible-start)
  5181. (org-rear-nonsticky-at visible-end)))
  5182. (let ((f (org-link-get-parameter type :activate-func)))
  5183. (when (functionp f)
  5184. (funcall f start end path (eq style 'bracket))))
  5185. (throw :exit t))))) ;signal success
  5186. nil))
  5187. (defun org-activate-code (limit)
  5188. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5189. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5190. (remove-text-properties (match-beginning 0) (match-end 0)
  5191. '(display t invisible t intangible t))
  5192. t))
  5193. (defcustom org-src-fontify-natively t
  5194. "When non-nil, fontify code in code blocks.
  5195. See also the `org-block' face."
  5196. :type 'boolean
  5197. :version "24.4"
  5198. :package-version '(Org . "8.3")
  5199. :group 'org-appearance
  5200. :group 'org-babel)
  5201. (defcustom org-allow-promoting-top-level-subtree nil
  5202. "When non-nil, allow promoting a top level subtree.
  5203. The leading star of the top level headline will be replaced
  5204. by a #."
  5205. :type 'boolean
  5206. :version "24.1"
  5207. :group 'org-appearance)
  5208. (defun org-fontify-meta-lines-and-blocks (limit)
  5209. (condition-case nil
  5210. (org-fontify-meta-lines-and-blocks-1 limit)
  5211. (error (message "org-mode fontification error in %S at %d"
  5212. (current-buffer)
  5213. (line-number-at-pos)))))
  5214. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5215. "Fontify #+ lines and blocks."
  5216. (let ((case-fold-search t))
  5217. (when (re-search-forward
  5218. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5219. limit t)
  5220. (let ((beg (match-beginning 0))
  5221. (block-start (match-end 0))
  5222. (block-end nil)
  5223. (lang (match-string 7))
  5224. (beg1 (line-beginning-position 2))
  5225. (dc1 (downcase (match-string 2)))
  5226. (dc3 (downcase (match-string 3)))
  5227. end end1 quoting block-type)
  5228. (cond
  5229. ((and (match-end 4) (equal dc3 "+begin"))
  5230. ;; Truly a block
  5231. (setq block-type (downcase (match-string 5))
  5232. quoting (member block-type org-protecting-blocks))
  5233. (when (re-search-forward
  5234. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5235. nil t) ;; on purpose, we look further than LIMIT
  5236. (setq end (min (point-max) (match-end 0))
  5237. end1 (min (point-max) (1- (match-beginning 0))))
  5238. (setq block-end (match-beginning 0))
  5239. (when quoting
  5240. (org-remove-flyspell-overlays-in beg1 end1)
  5241. (remove-text-properties beg end
  5242. '(display t invisible t intangible t)))
  5243. (add-text-properties
  5244. beg end '(font-lock-fontified t font-lock-multiline t))
  5245. (add-text-properties beg beg1 '(face org-meta-line))
  5246. (org-remove-flyspell-overlays-in beg beg1)
  5247. (add-text-properties ; For end_src
  5248. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5249. (org-remove-flyspell-overlays-in end1 end)
  5250. (cond
  5251. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5252. (org-src-font-lock-fontify-block lang block-start block-end)
  5253. (add-text-properties beg1 block-end '(src-block t)))
  5254. (quoting
  5255. (add-text-properties beg1 (min (point-max) (1+ end1))
  5256. (list 'face
  5257. (list :inherit
  5258. (let ((face-name
  5259. (intern (format "org-block-%s" lang))))
  5260. (append (and (facep face-name) (list face-name))
  5261. '(org-block))))))) ; end of source block
  5262. ((not org-fontify-quote-and-verse-blocks))
  5263. ((string= block-type "quote")
  5264. (add-face-text-property
  5265. beg1 (min (point-max) (1+ end1)) 'org-quote t))
  5266. ((string= block-type "verse")
  5267. (add-face-text-property
  5268. beg1 (min (point-max) (1+ end1)) 'org-verse t)))
  5269. (add-text-properties beg beg1 '(face org-block-begin-line))
  5270. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5271. '(face org-block-end-line))
  5272. t))
  5273. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5274. (org-remove-flyspell-overlays-in
  5275. (match-beginning 0)
  5276. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5277. (add-text-properties
  5278. beg (match-end 3)
  5279. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5280. '(font-lock-fontified t invisible t)
  5281. '(font-lock-fontified t face org-document-info-keyword)))
  5282. (add-text-properties
  5283. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5284. (if (string-equal dc1 "+title:")
  5285. '(font-lock-fontified t face org-document-title)
  5286. '(font-lock-fontified t face org-document-info))))
  5287. ((string-prefix-p "+caption" dc1)
  5288. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5289. (remove-text-properties (match-beginning 0) (match-end 0)
  5290. '(display t invisible t intangible t))
  5291. ;; Handle short captions.
  5292. (save-excursion
  5293. (beginning-of-line)
  5294. (looking-at "\\([ \t]*#\\+caption\\(?:\\[.*\\]\\)?:\\)[ \t]*"))
  5295. (add-text-properties (line-beginning-position) (match-end 1)
  5296. '(font-lock-fontified t face org-meta-line))
  5297. (add-text-properties (match-end 0) (line-end-position)
  5298. '(font-lock-fontified t face org-block))
  5299. t)
  5300. ((member dc3 '(" " ""))
  5301. (org-remove-flyspell-overlays-in beg (match-end 0))
  5302. (add-text-properties
  5303. beg (match-end 0)
  5304. '(font-lock-fontified t face font-lock-comment-face)))
  5305. (t ;; just any other in-buffer setting, but not indented
  5306. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5307. (remove-text-properties (match-beginning 0) (match-end 0)
  5308. '(display t invisible t intangible t))
  5309. (add-text-properties beg (match-end 0)
  5310. '(font-lock-fontified t face org-meta-line))
  5311. t))))))
  5312. (defun org-fontify-drawers (limit)
  5313. "Fontify drawers."
  5314. (when (re-search-forward org-drawer-regexp limit t)
  5315. (add-text-properties
  5316. (match-beginning 0) (match-end 0)
  5317. '(font-lock-fontified t face org-special-keyword))
  5318. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5319. t))
  5320. (defun org-fontify-macros (limit)
  5321. "Fontify macros."
  5322. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5323. (add-text-properties
  5324. (match-beginning 0) (match-end 0)
  5325. '(font-lock-fontified t face org-macro))
  5326. (when org-hide-macro-markers
  5327. (add-text-properties (match-end 2) (match-beginning 2)
  5328. '(invisible t))
  5329. (add-text-properties (match-beginning 1) (match-end 1)
  5330. '(invisible t)))
  5331. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5332. t))
  5333. (defun org-activate-footnote-links (limit)
  5334. "Add text properties for footnotes."
  5335. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5336. (when fn
  5337. (let* ((beg (nth 1 fn))
  5338. (end (nth 2 fn))
  5339. (label (car fn))
  5340. (referencep (/= (line-beginning-position) beg)))
  5341. (when (and referencep (nth 3 fn))
  5342. (save-excursion
  5343. (goto-char beg)
  5344. (search-forward (or label "fn:"))
  5345. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5346. (add-text-properties beg end
  5347. (list 'mouse-face 'highlight
  5348. 'keymap org-mouse-map
  5349. 'help-echo
  5350. (if referencep "Footnote reference"
  5351. "Footnote definition")
  5352. 'font-lock-fontified t
  5353. 'font-lock-multiline t
  5354. 'face 'org-footnote))))))
  5355. (defun org-activate-dates (limit)
  5356. "Add text properties for dates."
  5357. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5358. (not (equal (char-before (match-beginning 0)) 91)))
  5359. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5360. (add-text-properties (match-beginning 0) (match-end 0)
  5361. (list 'mouse-face 'highlight
  5362. 'keymap org-mouse-map))
  5363. (org-rear-nonsticky-at (match-end 0))
  5364. (when org-display-custom-times
  5365. ;; If it's a date range, activate custom time for second date.
  5366. (when (match-end 3)
  5367. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5368. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5369. t))
  5370. (defvar-local org-target-link-regexp nil
  5371. "Regular expression matching radio targets in plain text.")
  5372. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5373. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5374. border border border))
  5375. "Regular expression matching a link target.")
  5376. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5377. "Regular expression matching a radio target.")
  5378. (defconst org-any-target-regexp
  5379. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5380. "Regular expression matching any target.")
  5381. (defun org-activate-target-links (limit)
  5382. "Add text properties for target matches."
  5383. (when org-target-link-regexp
  5384. (let ((case-fold-search t))
  5385. (when (re-search-forward org-target-link-regexp limit t)
  5386. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5387. (add-text-properties (match-beginning 1) (match-end 1)
  5388. (list 'mouse-face 'highlight
  5389. 'keymap org-mouse-map
  5390. 'help-echo "Radio target link"
  5391. 'org-linked-text t))
  5392. (org-rear-nonsticky-at (match-end 1))
  5393. t))))
  5394. (defun org-update-radio-target-regexp ()
  5395. "Find all radio targets in this file and update the regular expression.
  5396. Also refresh fontification if needed."
  5397. (interactive)
  5398. (let ((old-regexp org-target-link-regexp)
  5399. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5400. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5401. (targets
  5402. (org-with-wide-buffer
  5403. (goto-char (point-min))
  5404. (let (rtn)
  5405. (while (re-search-forward org-radio-target-regexp nil t)
  5406. ;; Make sure point is really within the object.
  5407. (backward-char)
  5408. (let ((obj (org-element-context)))
  5409. (when (eq (org-element-type obj) 'radio-target)
  5410. (cl-pushnew (org-element-property :value obj) rtn
  5411. :test #'equal))))
  5412. rtn))))
  5413. (setq org-target-link-regexp
  5414. (and targets
  5415. (concat before-re
  5416. (mapconcat
  5417. (lambda (x)
  5418. (replace-regexp-in-string
  5419. " +" "\\s-+" (regexp-quote x) t t))
  5420. targets
  5421. "\\|")
  5422. after-re)))
  5423. (unless (equal old-regexp org-target-link-regexp)
  5424. ;; Clean-up cache.
  5425. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5426. ((not org-target-link-regexp) old-regexp)
  5427. (t
  5428. (concat before-re
  5429. (mapconcat
  5430. (lambda (re)
  5431. (substring re (length before-re)
  5432. (- (length after-re))))
  5433. (list old-regexp org-target-link-regexp)
  5434. "\\|")
  5435. after-re)))))
  5436. (org-with-wide-buffer
  5437. (goto-char (point-min))
  5438. (while (re-search-forward regexp nil t)
  5439. (org-element-cache-refresh (match-beginning 1)))))
  5440. ;; Re fontify buffer.
  5441. (when (memq 'radio org-highlight-links)
  5442. (org-restart-font-lock)))))
  5443. (defun org-hide-wide-columns (limit)
  5444. (let (s e)
  5445. (setq s (text-property-any (point) (or limit (point-max))
  5446. 'org-cwidth t))
  5447. (when s
  5448. (setq e (next-single-property-change s 'org-cwidth))
  5449. (add-text-properties s e '(invisible org-cwidth))
  5450. (goto-char e)
  5451. t)))
  5452. (defvar org-latex-and-related-regexp nil
  5453. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5454. (defun org-compute-latex-and-related-regexp ()
  5455. "Compute regular expression for LaTeX, entities and sub/superscript.
  5456. Result depends on variable `org-highlight-latex-and-related'."
  5457. (setq-local
  5458. org-latex-and-related-regexp
  5459. (let* ((re-sub
  5460. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5461. ((eq org-use-sub-superscripts '{})
  5462. (list org-match-substring-with-braces-regexp))
  5463. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5464. (re-latex
  5465. (when (memq 'latex org-highlight-latex-and-related)
  5466. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5467. (delq nil
  5468. (mapcar (lambda (x)
  5469. (and (member (car x) matchers) (nth 1 x)))
  5470. org-latex-regexps)))))
  5471. (re-entities
  5472. (when (memq 'entities org-highlight-latex-and-related)
  5473. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5474. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5475. (defun org-do-latex-and-related (limit)
  5476. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5477. LIMIT bounds the search for syntax to highlight. Stop at first
  5478. highlighted object, if any. Return t if some highlighting was
  5479. done, nil otherwise."
  5480. (when (org-string-nw-p org-latex-and-related-regexp)
  5481. (catch 'found
  5482. (while (re-search-forward org-latex-and-related-regexp limit t)
  5483. (unless
  5484. (cl-some
  5485. (lambda (f)
  5486. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5487. (save-excursion
  5488. (goto-char (1+ (match-beginning 0)))
  5489. (face-at-point nil t)))
  5490. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5491. '(?_ ?^))
  5492. 1
  5493. 0)))
  5494. (font-lock-prepend-text-property
  5495. (+ offset (match-beginning 0)) (match-end 0)
  5496. 'face 'org-latex-and-related)
  5497. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5498. '(font-lock-multiline t)))
  5499. (throw 'found t)))
  5500. nil)))
  5501. (defun org-restart-font-lock ()
  5502. "Restart `font-lock-mode', to force refontification."
  5503. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5504. (font-lock-mode -1)
  5505. (font-lock-mode 1)))
  5506. (defun org-activate-tags (limit)
  5507. (when (re-search-forward
  5508. "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" limit t)
  5509. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5510. (add-text-properties (match-beginning 1) (match-end 1)
  5511. (list 'mouse-face 'highlight
  5512. 'keymap org-mouse-map))
  5513. (org-rear-nonsticky-at (match-end 1))
  5514. t))
  5515. (defun org-outline-level ()
  5516. "Compute the outline level of the heading at point.
  5517. If this is called at a normal headline, the level is the number
  5518. of stars. Use `org-reduced-level' to remove the effect of
  5519. `org-odd-levels'. Unlike to `org-current-level', this function
  5520. takes into consideration inlinetasks."
  5521. (org-with-wide-buffer
  5522. (end-of-line)
  5523. (if (re-search-backward org-outline-regexp-bol nil t)
  5524. (1- (- (match-end 0) (match-beginning 0)))
  5525. 0)))
  5526. (defvar org-font-lock-keywords nil)
  5527. (defsubst org-re-property (property &optional literal allow-null value)
  5528. "Return a regexp matching a PROPERTY line.
  5529. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5530. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5531. non-nil, match properties even without a value.
  5532. Match group 3 is set to the value when it exists. If there is no
  5533. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5534. With optional argument VALUE, match only property lines with
  5535. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5536. unless LITERAL is non-nil."
  5537. (concat
  5538. "^\\(?4:[ \t]*\\)"
  5539. (format "\\(?1::\\(?2:%s\\):\\)"
  5540. (if literal property (regexp-quote property)))
  5541. (cond (value
  5542. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5543. (if literal value (regexp-quote value))))
  5544. (allow-null
  5545. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5546. (t
  5547. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5548. (defconst org-property-re
  5549. (org-re-property "\\S-+" 'literal t)
  5550. "Regular expression matching a property line.
  5551. There are four matching groups:
  5552. 1: :PROPKEY: including the leading and trailing colon,
  5553. 2: PROPKEY without the leading and trailing colon,
  5554. 3: PROPVAL without leading or trailing spaces,
  5555. 4: the indentation of the current line,
  5556. 5: trailing whitespace.")
  5557. (defvar org-font-lock-hook nil
  5558. "Functions to be called for special font lock stuff.")
  5559. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5560. (defvar org-font-lock-set-keywords-hook nil
  5561. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5562. This is called after `org-font-lock-extra-keywords' is defined, but before
  5563. it is installed to be used by font lock. This can be useful if something
  5564. needs to be inserted at a specific position in the font-lock sequence.")
  5565. (defun org-font-lock-hook (limit)
  5566. "Run `org-font-lock-hook' within LIMIT."
  5567. (run-hook-with-args 'org-font-lock-hook limit))
  5568. (defun org-set-font-lock-defaults ()
  5569. "Set font lock defaults for the current buffer."
  5570. (let* ((em org-fontify-emphasized-text)
  5571. (lk org-highlight-links)
  5572. (org-font-lock-extra-keywords
  5573. (list
  5574. ;; Call the hook
  5575. '(org-font-lock-hook)
  5576. ;; Headlines
  5577. `(,(if org-fontify-whole-heading-line
  5578. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5579. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5580. (1 (org-get-level-face 1))
  5581. (2 (org-get-level-face 2))
  5582. (3 (org-get-level-face 3)))
  5583. ;; Table lines
  5584. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5585. (1 'org-table t))
  5586. ;; Table internals
  5587. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5588. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5589. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5590. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5591. ;; Drawers
  5592. '(org-fontify-drawers)
  5593. ;; Properties
  5594. (list org-property-re
  5595. '(1 'org-special-keyword t)
  5596. '(3 'org-property-value t))
  5597. ;; Link related fontification.
  5598. '(org-activate-links)
  5599. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5600. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5601. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5602. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5603. ;; Targets.
  5604. (list org-any-target-regexp '(0 'org-target t))
  5605. ;; Diary sexps.
  5606. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5607. ;; Macro
  5608. '(org-fontify-macros)
  5609. '(org-hide-wide-columns (0 nil append))
  5610. ;; TODO keyword
  5611. (list (format org-heading-keyword-regexp-format
  5612. org-todo-regexp)
  5613. '(2 (org-get-todo-face 2) t))
  5614. ;; DONE
  5615. (if org-fontify-done-headline
  5616. (list (format org-heading-keyword-regexp-format
  5617. (concat
  5618. "\\(?:"
  5619. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5620. "\\)"))
  5621. '(2 'org-headline-done t))
  5622. nil)
  5623. ;; Priorities
  5624. '(org-font-lock-add-priority-faces)
  5625. ;; Tags
  5626. '(org-font-lock-add-tag-faces)
  5627. ;; Tags groups
  5628. (when (and org-group-tags org-tag-groups-alist)
  5629. (list (concat org-outline-regexp-bol ".+\\(:"
  5630. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5631. ":\\).*$")
  5632. '(1 'org-tag-group prepend)))
  5633. ;; Special keywords
  5634. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5635. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5636. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5637. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5638. ;; Emphasis
  5639. (when em '(org-do-emphasis-faces))
  5640. ;; Checkboxes
  5641. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5642. 1 'org-checkbox prepend)
  5643. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5644. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5645. (0 (org-get-checkbox-statistics-face) t)))
  5646. ;; Description list items
  5647. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5648. 1 'org-list-dt prepend)
  5649. ;; ARCHIVEd headings
  5650. (list (concat
  5651. org-outline-regexp-bol
  5652. "\\(.*:" org-archive-tag ":.*\\)")
  5653. '(1 'org-archived prepend))
  5654. ;; Specials
  5655. '(org-do-latex-and-related)
  5656. '(org-fontify-entities)
  5657. '(org-raise-scripts)
  5658. ;; Code
  5659. '(org-activate-code (1 'org-code t))
  5660. ;; COMMENT
  5661. (list (format
  5662. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5663. org-todo-regexp
  5664. org-comment-string)
  5665. '(9 'org-special-keyword t))
  5666. ;; Blocks and meta lines
  5667. '(org-fontify-meta-lines-and-blocks))))
  5668. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5669. (run-hooks 'org-font-lock-set-keywords-hook)
  5670. ;; Now set the full font-lock-keywords
  5671. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5672. (setq-local font-lock-defaults
  5673. '(org-font-lock-keywords t nil nil backward-paragraph))
  5674. (kill-local-variable 'font-lock-keywords)
  5675. nil))
  5676. (defun org-toggle-pretty-entities ()
  5677. "Toggle the composition display of entities as UTF8 characters."
  5678. (interactive)
  5679. (setq-local org-pretty-entities (not org-pretty-entities))
  5680. (org-restart-font-lock)
  5681. (if org-pretty-entities
  5682. (message "Entities are now displayed as UTF8 characters")
  5683. (save-restriction
  5684. (widen)
  5685. (decompose-region (point-min) (point-max))
  5686. (message "Entities are now displayed as plain text"))))
  5687. (defvar-local org-custom-properties-overlays nil
  5688. "List of overlays used for custom properties.")
  5689. (defun org-toggle-custom-properties-visibility ()
  5690. "Display or hide properties in `org-custom-properties'."
  5691. (interactive)
  5692. (if org-custom-properties-overlays
  5693. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5694. (setq org-custom-properties-overlays nil))
  5695. (when org-custom-properties
  5696. (org-with-wide-buffer
  5697. (goto-char (point-min))
  5698. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5699. (while (re-search-forward regexp nil t)
  5700. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5701. (when (and end (< (point) end))
  5702. ;; Hide first custom property in current drawer.
  5703. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5704. (overlay-put o 'invisible t)
  5705. (overlay-put o 'org-custom-property t)
  5706. (push o org-custom-properties-overlays))
  5707. ;; Hide additional custom properties in the same drawer.
  5708. (while (re-search-forward regexp end t)
  5709. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5710. (overlay-put o 'invisible t)
  5711. (overlay-put o 'org-custom-property t)
  5712. (push o org-custom-properties-overlays)))))
  5713. ;; Each entry is limited to a single property drawer.
  5714. (outline-next-heading)))))))
  5715. (defun org-fontify-entities (limit)
  5716. "Find an entity to fontify."
  5717. (let (ee)
  5718. (when org-pretty-entities
  5719. (catch 'match
  5720. ;; "\_ "-family is left out on purpose. Only the first one,
  5721. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5722. ;; confusing when adding a second white space character.
  5723. (while (re-search-forward
  5724. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5725. limit t)
  5726. (when (and (not (org-at-comment-p))
  5727. (setq ee (org-entity-get (match-string 1)))
  5728. (= (length (nth 6 ee)) 1))
  5729. (let* ((end (if (equal (match-string 2) "{}")
  5730. (match-end 2)
  5731. (match-end 1))))
  5732. (add-text-properties
  5733. (match-beginning 0) end
  5734. (list 'font-lock-fontified t))
  5735. (compose-region (match-beginning 0) end
  5736. (nth 6 ee) nil)
  5737. (backward-char 1)
  5738. (throw 'match t))))
  5739. nil))))
  5740. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5741. "Fontify string S like in Org mode."
  5742. (with-temp-buffer
  5743. (insert s)
  5744. (let ((org-odd-levels-only odd-levels))
  5745. (org-mode)
  5746. (org-font-lock-ensure)
  5747. (buffer-string))))
  5748. (defvar org-m nil)
  5749. (defvar org-l nil)
  5750. (defvar org-f nil)
  5751. (defun org-get-level-face (n)
  5752. "Get the right face for match N in font-lock matching of headlines."
  5753. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5754. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5755. (if org-cycle-level-faces
  5756. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5757. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5758. (cond
  5759. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5760. ((eq n 2) org-f)
  5761. (t (unless org-level-color-stars-only org-f))))
  5762. (defun org-face-from-face-or-color (context inherit face-or-color)
  5763. "Create a face list that inherits INHERIT, but sets the foreground color.
  5764. When FACE-OR-COLOR is not a string, just return it."
  5765. (if (stringp face-or-color)
  5766. (list :inherit inherit
  5767. (cdr (assoc context org-faces-easy-properties))
  5768. face-or-color)
  5769. face-or-color))
  5770. (defun org-get-todo-face (kwd)
  5771. "Get the right face for a TODO keyword KWD.
  5772. If KWD is a number, get the corresponding match group."
  5773. (when (numberp kwd) (setq kwd (match-string kwd)))
  5774. (or (org-face-from-face-or-color
  5775. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5776. (and (member kwd org-done-keywords) 'org-done)
  5777. 'org-todo))
  5778. (defun org-get-priority-face (priority)
  5779. "Get the right face for PRIORITY.
  5780. PRIORITY is a character."
  5781. (or (org-face-from-face-or-color
  5782. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5783. 'org-priority))
  5784. (defun org-get-tag-face (tag)
  5785. "Get the right face for TAG.
  5786. If TAG is a number, get the corresponding match group."
  5787. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5788. (or (org-face-from-face-or-color
  5789. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5790. 'org-tag)))
  5791. (defun org-font-lock-add-priority-faces (limit)
  5792. "Add the special priority faces."
  5793. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5794. (add-text-properties
  5795. (match-beginning 1) (match-end 1)
  5796. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5797. 'font-lock-fontified t))))
  5798. (defun org-font-lock-add-tag-faces (limit)
  5799. "Add the special tag faces."
  5800. (when (and org-tag-faces org-tags-special-faces-re)
  5801. (while (re-search-forward org-tags-special-faces-re limit t)
  5802. (add-text-properties (match-beginning 1) (match-end 1)
  5803. (list 'face (org-get-tag-face 1)
  5804. 'font-lock-fontified t))
  5805. (backward-char 1))))
  5806. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5807. "Remove fontification and activation overlays from links."
  5808. (font-lock-default-unfontify-region beg end)
  5809. (let* ((buffer-undo-list t)
  5810. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5811. (inhibit-modification-hooks t)
  5812. deactivate-mark buffer-file-name buffer-file-truename)
  5813. (decompose-region beg end)
  5814. (remove-text-properties beg end
  5815. '(mouse-face t keymap t org-linked-text t
  5816. invisible t intangible t
  5817. org-emphasis t))
  5818. (org-remove-font-lock-display-properties beg end)))
  5819. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5820. ((raise 0.3) (height 0.7))
  5821. ((raise -0.5))
  5822. ((raise 0.5)))
  5823. "Display properties for showing superscripts and subscripts.")
  5824. (defun org-remove-font-lock-display-properties (beg end)
  5825. "Remove specific display properties that have been added by font lock.
  5826. The will remove the raise properties that are used to show superscripts
  5827. and subscripts."
  5828. (let (next prop)
  5829. (while (< beg end)
  5830. (setq next (next-single-property-change beg 'display nil end)
  5831. prop (get-text-property beg 'display))
  5832. (when (member prop org-script-display)
  5833. (put-text-property beg next 'display nil))
  5834. (setq beg next))))
  5835. (defun org-raise-scripts (limit)
  5836. "Add raise properties to sub/superscripts."
  5837. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5838. (re-search-forward
  5839. (if (eq org-use-sub-superscripts t)
  5840. org-match-substring-regexp
  5841. org-match-substring-with-braces-regexp)
  5842. limit t))
  5843. (let* ((pos (point)) table-p comment-p
  5844. (mpos (match-beginning 3))
  5845. (emph-p (get-text-property mpos 'org-emphasis))
  5846. (link-p (get-text-property mpos 'mouse-face))
  5847. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5848. (goto-char (point-at-bol))
  5849. (setq table-p (looking-at-p org-table-dataline-regexp)
  5850. comment-p (looking-at-p "^[ \t]*#[ +]"))
  5851. (goto-char pos)
  5852. ;; Handle a_b^c
  5853. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5854. (unless (or comment-p emph-p link-p keyw-p)
  5855. (put-text-property (match-beginning 3) (match-end 0)
  5856. 'display
  5857. (if (equal (char-after (match-beginning 2)) ?^)
  5858. (nth (if table-p 3 1) org-script-display)
  5859. (nth (if table-p 2 0) org-script-display)))
  5860. (add-text-properties (match-beginning 2) (match-end 2)
  5861. (list 'invisible t))
  5862. (when (and (eq (char-after (match-beginning 3)) ?{)
  5863. (eq (char-before (match-end 3)) ?}))
  5864. (add-text-properties (match-beginning 3) (1+ (match-beginning 3))
  5865. (list 'invisible t))
  5866. (add-text-properties (1- (match-end 3)) (match-end 3)
  5867. (list 'invisible t))))
  5868. t)))
  5869. ;;;; Visibility cycling, including org-goto and indirect buffer
  5870. ;;; Cycling
  5871. (defvar-local org-cycle-global-status nil)
  5872. (put 'org-cycle-global-status 'org-state t)
  5873. (defvar-local org-cycle-subtree-status nil)
  5874. (put 'org-cycle-subtree-status 'org-state t)
  5875. (defvar org-inlinetask-min-level)
  5876. (defun org-unlogged-message (&rest args)
  5877. "Display a message, but avoid logging it in the *Messages* buffer."
  5878. (let ((message-log-max nil))
  5879. (apply 'message args)))
  5880. ;;;###autoload
  5881. (defun org-cycle (&optional arg)
  5882. "TAB-action and visibility cycling for Org mode.
  5883. This is the command invoked in Org mode by the `TAB' key. Its main
  5884. purpose is outline visibility cycling, but it also invokes other actions
  5885. in special contexts.
  5886. When this function is called with a `\\[universal-argument]' prefix, rotate \
  5887. the entire
  5888. buffer through 3 states (global cycling)
  5889. 1. OVERVIEW: Show only top-level headlines.
  5890. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5891. 3. SHOW ALL: Show everything.
  5892. With a `\\[universal-argument] \\[universal-argument]' prefix argument, \
  5893. switch to the startup visibility,
  5894. determined by the variable `org-startup-folded', and by any VISIBILITY
  5895. properties in the buffer.
  5896. With a `\\[universal-argument] \\[universal-argument] \
  5897. \\[universal-argument]' prefix argument, show the entire buffer, including
  5898. any drawers.
  5899. When inside a table, re-align the table and move to the next field.
  5900. When point is at the beginning of a headline, rotate the subtree started
  5901. by this line through 3 different states (local cycling)
  5902. 1. FOLDED: Only the main headline is shown.
  5903. 2. CHILDREN: The main headline and the direct children are shown.
  5904. From this state, you can move to one of the children
  5905. and zoom in further.
  5906. 3. SUBTREE: Show the entire subtree, including body text.
  5907. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5908. When point is at the beginning of an empty headline and the variable
  5909. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5910. of the headline by demoting and promoting it to likely levels. This
  5911. speeds up creation document structure by pressing `TAB' once or several
  5912. times right after creating a new headline.
  5913. When there is a numeric prefix, go up to a heading with level ARG, do
  5914. a `show-subtree' and return to the previous cursor position. If ARG
  5915. is negative, go up that many levels.
  5916. When point is not at the beginning of a headline, execute the global
  5917. binding for `TAB', which is re-indenting the line. See the option
  5918. `org-cycle-emulate-tab' for details.
  5919. As a special case, if point is at the beginning of the buffer and there is
  5920. no headline in line 1, this function will act as if called with prefix arg
  5921. \(`\\[universal-argument] TAB', same as `S-TAB') also when called without \
  5922. prefix arg, but only
  5923. if the variable `org-cycle-global-at-bob' is t."
  5924. (interactive "P")
  5925. (org-load-modules-maybe)
  5926. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5927. (and org-cycle-level-after-item/entry-creation
  5928. (or (org-cycle-level)
  5929. (org-cycle-item-indentation))))
  5930. (let* ((limit-level
  5931. (or org-cycle-max-level
  5932. (and (boundp 'org-inlinetask-min-level)
  5933. org-inlinetask-min-level
  5934. (1- org-inlinetask-min-level))))
  5935. (nstars (and limit-level
  5936. (if org-odd-levels-only
  5937. (and limit-level (1- (* limit-level 2)))
  5938. limit-level)))
  5939. (org-outline-regexp
  5940. (if (not (derived-mode-p 'org-mode))
  5941. outline-regexp
  5942. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5943. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5944. (not (looking-at org-outline-regexp))))
  5945. (org-cycle-hook
  5946. (if bob-special
  5947. (delq 'org-optimize-window-after-visibility-change
  5948. (copy-sequence org-cycle-hook))
  5949. org-cycle-hook))
  5950. (pos (point)))
  5951. (cond
  5952. ((equal arg '(16))
  5953. (setq last-command 'dummy)
  5954. (org-set-startup-visibility)
  5955. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5956. ((equal arg '(64))
  5957. (outline-show-all)
  5958. (org-unlogged-message "Entire buffer visible, including drawers"))
  5959. ((equal arg '(4)) (org-cycle-internal-global))
  5960. ;; Try hiding block at point.
  5961. ((org-hide-block-toggle-maybe))
  5962. ;; Try cdlatex TAB completion
  5963. ((org-try-cdlatex-tab))
  5964. ;; Table: enter it or move to the next field.
  5965. ((org-at-table-p 'any)
  5966. (if (org-at-table.el-p)
  5967. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5968. Use `\\[org-edit-special]' to edit table.el tables"))
  5969. (if arg (org-table-edit-field t)
  5970. (org-table-justify-field-maybe)
  5971. (call-interactively 'org-table-next-field))))
  5972. ((run-hook-with-args-until-success 'org-tab-after-check-for-table-hook))
  5973. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5974. (bob-special (org-cycle-internal-global))
  5975. ;; Drawers: delegate to `org-flag-drawer'.
  5976. ((save-excursion
  5977. (beginning-of-line 1)
  5978. (looking-at org-drawer-regexp))
  5979. (org-flag-drawer ; toggle block visibility
  5980. (not (get-char-property (match-end 0) 'invisible))))
  5981. ;; Show-subtree, ARG levels up from here.
  5982. ((integerp arg)
  5983. (save-excursion
  5984. (org-back-to-heading)
  5985. (outline-up-heading (if (< arg 0) (- arg)
  5986. (- (funcall outline-level) arg)))
  5987. (org-show-subtree)))
  5988. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5989. ((and (featurep 'org-inlinetask)
  5990. (org-inlinetask-at-task-p)
  5991. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5992. (org-inlinetask-toggle-visibility))
  5993. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5994. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5995. (save-excursion (move-beginning-of-line 1)
  5996. (looking-at org-outline-regexp)))
  5997. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5998. (org-cycle-internal-local))
  5999. ;; From there: TAB emulation and template completion.
  6000. (buffer-read-only (org-back-to-heading))
  6001. ((run-hook-with-args-until-success
  6002. 'org-tab-after-check-for-cycling-hook))
  6003. ((org-try-structure-completion))
  6004. ((run-hook-with-args-until-success
  6005. 'org-tab-before-tab-emulation-hook))
  6006. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6007. (or (not (bolp))
  6008. (not (looking-at org-outline-regexp))))
  6009. (call-interactively (global-key-binding "\t")))
  6010. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6011. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6012. (or (and (eq org-cycle-emulate-tab 'white)
  6013. (= (match-end 0) (point-at-eol)))
  6014. (and (eq org-cycle-emulate-tab 'whitestart)
  6015. (>= (match-end 0) pos))))
  6016. t
  6017. (eq org-cycle-emulate-tab t))
  6018. (call-interactively (global-key-binding "\t")))
  6019. (t (save-excursion
  6020. (org-back-to-heading)
  6021. (org-cycle)))))))
  6022. (defun org-cycle-internal-global ()
  6023. "Do the global cycling action."
  6024. ;; Hack to avoid display of messages for .org attachments in Gnus
  6025. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6026. (cond
  6027. ((and (eq last-command this-command)
  6028. (eq org-cycle-global-status 'overview))
  6029. ;; We just created the overview - now do table of contents
  6030. ;; This can be slow in very large buffers, so indicate action
  6031. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6032. (unless ga (org-unlogged-message "CONTENTS..."))
  6033. (org-content)
  6034. (unless ga (org-unlogged-message "CONTENTS...done"))
  6035. (setq org-cycle-global-status 'contents)
  6036. (run-hook-with-args 'org-cycle-hook 'contents))
  6037. ((and (eq last-command this-command)
  6038. (eq org-cycle-global-status 'contents))
  6039. ;; We just showed the table of contents - now show everything
  6040. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6041. (outline-show-all)
  6042. (unless ga (org-unlogged-message "SHOW ALL"))
  6043. (setq org-cycle-global-status 'all)
  6044. (run-hook-with-args 'org-cycle-hook 'all))
  6045. (t
  6046. ;; Default action: go to overview
  6047. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6048. (org-overview)
  6049. (unless ga (org-unlogged-message "OVERVIEW"))
  6050. (setq org-cycle-global-status 'overview)
  6051. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6052. (defvar org-called-with-limited-levels nil
  6053. "Non-nil when `org-with-limited-levels' is currently active.")
  6054. (defun org-invisible-p (&optional pos)
  6055. "Non-nil if the character after POS is invisible.
  6056. If POS is nil, use `point' instead."
  6057. (get-char-property (or pos (point)) 'invisible))
  6058. (defun org-cycle-internal-local ()
  6059. "Do the local cycling action."
  6060. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6061. ;; First, determine end of headline (EOH), end of subtree or item
  6062. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6063. (save-excursion
  6064. (if (org-at-item-p)
  6065. (progn
  6066. (beginning-of-line)
  6067. (setq struct (org-list-struct))
  6068. (setq eoh (point-at-eol))
  6069. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6070. (setq has-children (org-list-has-child-p (point) struct)))
  6071. (org-back-to-heading)
  6072. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6073. (setq eos (save-excursion (org-end-of-subtree t t)
  6074. (when (bolp) (backward-char)) (point)))
  6075. (setq has-children
  6076. (or (save-excursion
  6077. (let ((level (funcall outline-level)))
  6078. (outline-next-heading)
  6079. (and (org-at-heading-p t)
  6080. (> (funcall outline-level) level))))
  6081. (save-excursion
  6082. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6083. ;; Determine end invisible part of buffer (EOL)
  6084. (beginning-of-line 2)
  6085. (while (and (not (eobp)) ;This is like `next-line'.
  6086. (get-char-property (1- (point)) 'invisible))
  6087. (goto-char (next-single-char-property-change (point) 'invisible))
  6088. (and (eolp) (beginning-of-line 2)))
  6089. (setq eol (point)))
  6090. ;; Find out what to do next and set `this-command'
  6091. (cond
  6092. ((= eos eoh)
  6093. ;; Nothing is hidden behind this heading
  6094. (unless (org-before-first-heading-p)
  6095. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6096. (org-unlogged-message "EMPTY ENTRY")
  6097. (setq org-cycle-subtree-status nil)
  6098. (save-excursion
  6099. (goto-char eos)
  6100. (outline-next-heading)
  6101. (when (org-invisible-p) (org-flag-heading nil))))
  6102. ((and (or (>= eol eos)
  6103. (not (string-match "\\S-" (buffer-substring eol eos))))
  6104. (or has-children
  6105. (not (setq children-skipped
  6106. org-cycle-skip-children-state-if-no-children))))
  6107. ;; Entire subtree is hidden in one line: children view
  6108. (unless (org-before-first-heading-p)
  6109. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6110. (if (org-at-item-p)
  6111. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6112. (org-show-entry)
  6113. (org-with-limited-levels (org-show-children))
  6114. ;; FIXME: This slows down the func way too much.
  6115. ;; How keep drawers hidden in subtree anyway?
  6116. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6117. ;; (org-cycle-hide-drawers 'subtree))
  6118. ;; Fold every list in subtree to top-level items.
  6119. (when (eq org-cycle-include-plain-lists 'integrate)
  6120. (save-excursion
  6121. (org-back-to-heading)
  6122. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6123. (beginning-of-line 1)
  6124. (let* ((struct (org-list-struct))
  6125. (prevs (org-list-prevs-alist struct))
  6126. (end (org-list-get-bottom-point struct)))
  6127. (dolist (e (org-list-get-all-items (point) struct prevs))
  6128. (org-list-set-item-visibility e struct 'folded))
  6129. (goto-char (if (< end eos) end eos)))))))
  6130. (org-unlogged-message "CHILDREN")
  6131. (save-excursion
  6132. (goto-char eos)
  6133. (outline-next-heading)
  6134. (when (org-invisible-p) (org-flag-heading nil)))
  6135. (setq org-cycle-subtree-status 'children)
  6136. (unless (org-before-first-heading-p)
  6137. (run-hook-with-args 'org-cycle-hook 'children)))
  6138. ((or children-skipped
  6139. (and (eq last-command this-command)
  6140. (eq org-cycle-subtree-status 'children)))
  6141. ;; We just showed the children, or no children are there,
  6142. ;; now show everything.
  6143. (unless (org-before-first-heading-p)
  6144. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6145. (outline-flag-region eoh eos nil)
  6146. (org-unlogged-message
  6147. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6148. (setq org-cycle-subtree-status 'subtree)
  6149. (unless (org-before-first-heading-p)
  6150. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6151. (t
  6152. ;; Default action: hide the subtree.
  6153. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6154. (outline-flag-region eoh eos t)
  6155. (org-unlogged-message "FOLDED")
  6156. (setq org-cycle-subtree-status 'folded)
  6157. (unless (org-before-first-heading-p)
  6158. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6159. ;;;###autoload
  6160. (defun org-global-cycle (&optional arg)
  6161. "Cycle the global visibility. For details see `org-cycle'.
  6162. With `\\[universal-argument]' prefix ARG, switch to startup visibility.
  6163. With a numeric prefix, show all headlines up to that level."
  6164. (interactive "P")
  6165. (let ((org-cycle-include-plain-lists
  6166. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6167. (cond
  6168. ((integerp arg)
  6169. (outline-show-all)
  6170. (outline-hide-sublevels arg)
  6171. (setq org-cycle-global-status 'contents))
  6172. ((equal arg '(4))
  6173. (org-set-startup-visibility)
  6174. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6175. (t
  6176. (org-cycle '(4))))))
  6177. (defun org-set-startup-visibility ()
  6178. "Set the visibility required by startup options and properties."
  6179. (cond
  6180. ((eq org-startup-folded t)
  6181. (org-overview))
  6182. ((eq org-startup-folded 'content)
  6183. (org-content))
  6184. ((or (eq org-startup-folded 'showeverything)
  6185. (eq org-startup-folded nil))
  6186. (outline-show-all)))
  6187. (unless (eq org-startup-folded 'showeverything)
  6188. (when org-hide-block-startup (org-hide-block-all))
  6189. (org-set-visibility-according-to-property 'no-cleanup)
  6190. (org-cycle-hide-archived-subtrees 'all)
  6191. (org-cycle-hide-drawers 'all)
  6192. (org-cycle-show-empty-lines t)))
  6193. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6194. "Switch subtree visibilities according to :VISIBILITY: property."
  6195. (interactive)
  6196. (org-with-wide-buffer
  6197. (goto-char (point-min))
  6198. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6199. (if (not (org-at-property-p)) (outline-next-heading)
  6200. (let ((state (match-string 3)))
  6201. (save-excursion
  6202. (org-back-to-heading t)
  6203. (outline-hide-subtree)
  6204. (org-reveal)
  6205. (cond
  6206. ((equal state "folded")
  6207. (outline-hide-subtree))
  6208. ((equal state "children")
  6209. (org-show-hidden-entry)
  6210. (org-show-children))
  6211. ((equal state "content")
  6212. (save-excursion
  6213. (save-restriction
  6214. (org-narrow-to-subtree)
  6215. (org-content))))
  6216. ((member state '("all" "showall"))
  6217. (outline-show-subtree)))))))
  6218. (unless no-cleanup
  6219. (org-cycle-hide-archived-subtrees 'all)
  6220. (org-cycle-hide-drawers 'all)
  6221. (org-cycle-show-empty-lines 'all))))
  6222. ;; This function uses outline-regexp instead of the more fundamental
  6223. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6224. ;; buffers, where outline-regexp is needed.
  6225. (defun org-overview ()
  6226. "Switch to overview mode, showing only top-level headlines.
  6227. This shows all headlines with a level equal or greater than the level
  6228. of the first headline in the buffer. This is important, because if the
  6229. first headline is not level one, then (hide-sublevels 1) gives confusing
  6230. results."
  6231. (interactive)
  6232. (save-excursion
  6233. (let ((level
  6234. (save-excursion
  6235. (goto-char (point-min))
  6236. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6237. (goto-char (match-beginning 0))
  6238. (funcall outline-level)))))
  6239. (and level (outline-hide-sublevels level)))))
  6240. (defun org-content (&optional arg)
  6241. "Show all headlines in the buffer, like a table of contents.
  6242. With numerical argument N, show content up to level N."
  6243. (interactive "P")
  6244. (org-overview)
  6245. (save-excursion
  6246. ;; Visit all headings and show their offspring
  6247. (and (integerp arg) (org-overview))
  6248. (goto-char (point-max))
  6249. (catch 'exit
  6250. (while (and (progn (condition-case nil
  6251. (outline-previous-visible-heading 1)
  6252. (error (goto-char (point-min))))
  6253. t)
  6254. (looking-at org-outline-regexp))
  6255. (if (integerp arg)
  6256. (org-show-children (1- arg))
  6257. (outline-show-branches))
  6258. (when (bobp) (throw 'exit nil))))))
  6259. (defun org-optimize-window-after-visibility-change (state)
  6260. "Adjust the window after a change in outline visibility.
  6261. This function is the default value of the hook `org-cycle-hook'."
  6262. (when (get-buffer-window (current-buffer))
  6263. (cond
  6264. ((eq state 'content) nil)
  6265. ((eq state 'all) nil)
  6266. ((eq state 'folded) nil)
  6267. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6268. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6269. (defun org-remove-empty-overlays-at (pos)
  6270. "Remove outline overlays that do not contain non-white stuff."
  6271. (dolist (o (overlays-at pos))
  6272. (and (eq 'outline (overlay-get o 'invisible))
  6273. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6274. (overlay-end o))))
  6275. (delete-overlay o))))
  6276. (defun org-clean-visibility-after-subtree-move ()
  6277. "Fix visibility issues after moving a subtree."
  6278. ;; First, find a reasonable region to look at:
  6279. ;; Start two siblings above, end three below
  6280. (let* ((beg (save-excursion
  6281. (and (org-get-last-sibling)
  6282. (org-get-last-sibling))
  6283. (point)))
  6284. (end (save-excursion
  6285. (and (org-get-next-sibling)
  6286. (org-get-next-sibling)
  6287. (org-get-next-sibling))
  6288. (if (org-at-heading-p)
  6289. (point-at-eol)
  6290. (point))))
  6291. (level (looking-at "\\*+"))
  6292. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6293. (save-excursion
  6294. (save-restriction
  6295. (narrow-to-region beg end)
  6296. (when re
  6297. ;; Properly fold already folded siblings
  6298. (goto-char (point-min))
  6299. (while (re-search-forward re nil t)
  6300. (when (and (not (org-invisible-p))
  6301. (save-excursion
  6302. (goto-char (point-at-eol)) (org-invisible-p)))
  6303. (outline-hide-entry))))
  6304. (org-cycle-show-empty-lines 'overview)
  6305. (org-cycle-hide-drawers 'overview)))))
  6306. (defun org-cycle-show-empty-lines (state)
  6307. "Show empty lines above all visible headlines.
  6308. The region to be covered depends on STATE when called through
  6309. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6310. entire buffer covered. Note that an empty line is only shown if there
  6311. are at least `org-cycle-separator-lines' empty lines before the headline."
  6312. (when (/= org-cycle-separator-lines 0)
  6313. (save-excursion
  6314. (let* ((n (abs org-cycle-separator-lines))
  6315. (re (cond
  6316. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6317. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6318. (t (let ((ns (number-to-string (- n 2))))
  6319. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6320. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6321. beg end)
  6322. (cond
  6323. ((memq state '(overview contents t))
  6324. (setq beg (point-min) end (point-max)))
  6325. ((memq state '(children folded))
  6326. (setq beg (point)
  6327. end (progn (org-end-of-subtree t t)
  6328. (line-beginning-position 2)))))
  6329. (when beg
  6330. (goto-char beg)
  6331. (while (re-search-forward re end t)
  6332. (unless (get-char-property (match-end 1) 'invisible)
  6333. (let ((e (match-end 1))
  6334. (b (if (>= org-cycle-separator-lines 0)
  6335. (match-beginning 1)
  6336. (save-excursion
  6337. (goto-char (match-beginning 0))
  6338. (skip-chars-backward " \t\n")
  6339. (line-end-position)))))
  6340. (outline-flag-region b e nil))))))))
  6341. ;; Never hide empty lines at the end of the file.
  6342. (save-excursion
  6343. (goto-char (point-max))
  6344. (outline-previous-heading)
  6345. (outline-end-of-heading)
  6346. (when (and (looking-at "[ \t\n]+")
  6347. (= (match-end 0) (point-max)))
  6348. (outline-flag-region (point) (match-end 0) nil))))
  6349. (defun org-show-empty-lines-in-parent ()
  6350. "Move to the parent and re-show empty lines before visible headlines."
  6351. (save-excursion
  6352. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6353. (org-cycle-show-empty-lines context))))
  6354. (defun org-files-list ()
  6355. "Return `org-agenda-files' list, plus all open Org files.
  6356. This is useful for operations that need to scan all of a user's
  6357. open and agenda-wise Org files."
  6358. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6359. (dolist (buf (buffer-list))
  6360. (with-current-buffer buf
  6361. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6362. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6363. files))
  6364. (defsubst org-entry-beginning-position ()
  6365. "Return the beginning position of the current entry."
  6366. (save-excursion (org-back-to-heading t) (point)))
  6367. (defsubst org-entry-end-position ()
  6368. "Return the end position of the current entry."
  6369. (save-excursion (outline-next-heading) (point)))
  6370. (defun org-cycle-hide-drawers (state &optional exceptions)
  6371. "Re-hide all drawers after a visibility state change.
  6372. STATE should be one of the symbols listed in the docstring of
  6373. `org-cycle-hook'. When non-nil, optional argument EXCEPTIONS is
  6374. a list of strings specifying which drawers should not be hidden."
  6375. (when (and (derived-mode-p 'org-mode)
  6376. (not (memq state '(overview folded contents))))
  6377. (save-excursion
  6378. (let* ((globalp (eq state 'all))
  6379. (beg (if globalp (point-min) (point)))
  6380. (end (if globalp (point-max)
  6381. (if (eq state 'children)
  6382. (save-excursion (outline-next-heading) (point))
  6383. (org-end-of-subtree t)))))
  6384. (goto-char beg)
  6385. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6386. (unless (member-ignore-case (match-string 1) exceptions)
  6387. (let ((drawer (org-element-at-point)))
  6388. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6389. (org-flag-drawer t drawer)
  6390. ;; Make sure to skip drawer entirely or we might flag
  6391. ;; it another time when matching its ending line with
  6392. ;; `org-drawer-regexp'.
  6393. (goto-char (org-element-property :end drawer))))))))))
  6394. (defun org-flag-drawer (flag &optional element)
  6395. "When FLAG is non-nil, hide the drawer we are at.
  6396. Otherwise make it visible. When optional argument ELEMENT is
  6397. a parsed drawer, as returned by `org-element-at-point', hide or
  6398. show that drawer instead."
  6399. (let ((drawer (or element
  6400. (and (save-excursion
  6401. (beginning-of-line)
  6402. (looking-at-p org-drawer-regexp))
  6403. (org-element-at-point)))))
  6404. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6405. (let ((post (org-element-property :post-affiliated drawer)))
  6406. (save-excursion
  6407. (outline-flag-region
  6408. (progn (goto-char post) (line-end-position))
  6409. (progn (goto-char (org-element-property :end drawer))
  6410. (skip-chars-backward " \r\t\n")
  6411. (line-end-position))
  6412. flag))
  6413. ;; When the drawer is hidden away, make sure point lies in
  6414. ;; a visible part of the buffer.
  6415. (when (and flag (> (line-beginning-position) post))
  6416. (goto-char post))))))
  6417. (defun org-subtree-end-visible-p ()
  6418. "Is the end of the current subtree visible?"
  6419. (pos-visible-in-window-p
  6420. (save-excursion (org-end-of-subtree t) (point))))
  6421. (defun org-first-headline-recenter ()
  6422. "Move cursor to the first headline and recenter the headline."
  6423. (let ((window (get-buffer-window)))
  6424. (when window
  6425. (goto-char (point-min))
  6426. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6427. (set-window-start window (line-beginning-position))))))
  6428. ;;; Saving and restoring visibility
  6429. (defun org-outline-overlay-data (&optional use-markers)
  6430. "Return a list of the locations of all outline overlays.
  6431. These are overlays with the `invisible' property value `outline'.
  6432. The return value is a list of cons cells, with start and stop
  6433. positions for each overlay.
  6434. If USE-MARKERS is set, return the positions as markers."
  6435. (let (beg end)
  6436. (org-with-wide-buffer
  6437. (delq nil
  6438. (mapcar (lambda (o)
  6439. (when (eq (overlay-get o 'invisible) 'outline)
  6440. (setq beg (overlay-start o)
  6441. end (overlay-end o))
  6442. (and beg end (> end beg)
  6443. (if use-markers
  6444. (cons (copy-marker beg)
  6445. (copy-marker end t))
  6446. (cons beg end)))))
  6447. (overlays-in (point-min) (point-max)))))))
  6448. (defun org-set-outline-overlay-data (data)
  6449. "Create visibility overlays for all positions in DATA.
  6450. DATA should have been made by `org-outline-overlay-data'."
  6451. (org-with-wide-buffer
  6452. (outline-show-all)
  6453. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6454. ;;; Folding of blocks
  6455. (defvar-local org-hide-block-overlays nil
  6456. "Overlays hiding blocks.")
  6457. (defun org-block-map (function &optional start end)
  6458. "Call FUNCTION at the head of all source blocks in the current buffer.
  6459. Optional arguments START and END can be used to limit the range."
  6460. (let ((start (or start (point-min)))
  6461. (end (or end (point-max))))
  6462. (save-excursion
  6463. (goto-char start)
  6464. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6465. (save-excursion
  6466. (save-match-data
  6467. (goto-char (match-beginning 0))
  6468. (funcall function)))))))
  6469. (defun org-hide-block-toggle-all ()
  6470. "Toggle the visibility of all blocks in the current buffer."
  6471. (org-block-map 'org-hide-block-toggle))
  6472. (defun org-hide-block-all ()
  6473. "Fold all blocks in the current buffer."
  6474. (interactive)
  6475. (org-show-block-all)
  6476. (org-block-map 'org-hide-block-toggle-maybe))
  6477. (defun org-show-block-all ()
  6478. "Unfold all blocks in the current buffer."
  6479. (interactive)
  6480. (mapc #'delete-overlay org-hide-block-overlays)
  6481. (setq org-hide-block-overlays nil))
  6482. (defun org-hide-block-toggle-maybe ()
  6483. "Toggle visibility of block at point.
  6484. Unlike to `org-hide-block-toggle', this function does not throw
  6485. an error. Return a non-nil value when toggling is successful."
  6486. (interactive)
  6487. (ignore-errors (org-hide-block-toggle)))
  6488. (defun org-hide-block-toggle (&optional force)
  6489. "Toggle the visibility of the current block.
  6490. When optional argument FORCE is `off', make block visible. If it
  6491. is non-nil, hide it unconditionally. Throw an error when not at
  6492. a block. Return a non-nil value when toggling is successful."
  6493. (interactive)
  6494. (let ((element (org-element-at-point)))
  6495. (unless (memq (org-element-type element)
  6496. '(center-block comment-block dynamic-block example-block
  6497. export-block quote-block special-block
  6498. src-block verse-block))
  6499. (user-error "Not at a block"))
  6500. (let* ((start (save-excursion
  6501. (goto-char (org-element-property :post-affiliated element))
  6502. (line-end-position)))
  6503. (end (save-excursion
  6504. (goto-char (org-element-property :end element))
  6505. (skip-chars-backward " \r\t\n")
  6506. (line-end-position)))
  6507. (overlays (overlays-at start)))
  6508. (cond
  6509. ;; Do nothing when not before or at the block opening line or
  6510. ;; at the block closing line.
  6511. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6512. ((and (not (eq force 'off))
  6513. (not (memq t (mapcar
  6514. (lambda (o)
  6515. (eq (overlay-get o 'invisible) 'org-hide-block))
  6516. overlays))))
  6517. (let ((ov (make-overlay start end)))
  6518. (overlay-put ov 'invisible 'org-hide-block)
  6519. ;; Make the block accessible to `isearch'.
  6520. (overlay-put
  6521. ov 'isearch-open-invisible
  6522. (lambda (ov)
  6523. (when (memq ov org-hide-block-overlays)
  6524. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6525. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6526. (delete-overlay ov))))
  6527. (push ov org-hide-block-overlays)
  6528. ;; When the block is hidden away, make sure point is left in
  6529. ;; a visible part of the buffer.
  6530. (when (> (line-beginning-position) start)
  6531. (goto-char start)
  6532. (beginning-of-line))
  6533. ;; Signal successful toggling.
  6534. t))
  6535. ((or (not force) (eq force 'off))
  6536. (dolist (ov overlays t)
  6537. (when (memq ov org-hide-block-overlays)
  6538. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6539. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6540. (delete-overlay ov))))))))
  6541. ;; Remove overlays when changing major mode
  6542. (add-hook 'org-mode-hook
  6543. (lambda () (add-hook 'change-major-mode-hook
  6544. 'org-show-block-all 'append 'local)))
  6545. ;;; Org-goto
  6546. (defvar org-goto-window-configuration nil)
  6547. (defvar org-goto-marker nil)
  6548. (defvar org-goto-map)
  6549. (defun org-goto-map ()
  6550. "Set the keymap `org-goto'."
  6551. (setq org-goto-map
  6552. (let ((map (make-sparse-keymap)))
  6553. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6554. mouse-drag-region universal-argument org-occur)))
  6555. (dolist (cmd cmds)
  6556. (substitute-key-definition cmd cmd map global-map)))
  6557. (suppress-keymap map)
  6558. (org-defkey map "\C-m" 'org-goto-ret)
  6559. (org-defkey map [(return)] 'org-goto-ret)
  6560. (org-defkey map [(left)] 'org-goto-left)
  6561. (org-defkey map [(right)] 'org-goto-right)
  6562. (org-defkey map [(control ?g)] 'org-goto-quit)
  6563. (org-defkey map "\C-i" 'org-cycle)
  6564. (org-defkey map [(tab)] 'org-cycle)
  6565. (org-defkey map [(down)] 'outline-next-visible-heading)
  6566. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6567. (if org-goto-auto-isearch
  6568. (if (fboundp 'define-key-after)
  6569. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6570. nil)
  6571. (org-defkey map "q" 'org-goto-quit)
  6572. (org-defkey map "n" 'outline-next-visible-heading)
  6573. (org-defkey map "p" 'outline-previous-visible-heading)
  6574. (org-defkey map "f" 'outline-forward-same-level)
  6575. (org-defkey map "b" 'outline-backward-same-level)
  6576. (org-defkey map "u" 'outline-up-heading))
  6577. (org-defkey map "/" 'org-occur)
  6578. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6579. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6580. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6581. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6582. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6583. map)))
  6584. (defconst org-goto-help
  6585. "Browse buffer copy, to find location or copy text.%s
  6586. RET=jump to location C-g=quit and return to previous location
  6587. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6588. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6589. (defun org-goto (&optional alternative-interface)
  6590. "Look up a different location in the current file, keeping current visibility.
  6591. When you want look-up or go to a different location in a
  6592. document, the fastest way is often to fold the entire buffer and
  6593. then dive into the tree. This method has the disadvantage, that
  6594. the previous location will be folded, which may not be what you
  6595. want.
  6596. This command works around this by showing a copy of the current
  6597. buffer in an indirect buffer, in overview mode. You can dive
  6598. into the tree in that copy, use org-occur and incremental search
  6599. to find a location. When pressing RET or `Q', the command
  6600. returns to the original buffer in which the visibility is still
  6601. unchanged. After RET it will also jump to the location selected
  6602. in the indirect buffer and expose the headline hierarchy above.
  6603. With a prefix argument, use the alternative interface: e.g., if
  6604. `org-goto-interface' is `outline' use `outline-path-completion'."
  6605. (interactive "P")
  6606. (org-goto-map)
  6607. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6608. (org-refile-use-outline-path t)
  6609. (org-refile-target-verify-function nil)
  6610. (interface
  6611. (if (not alternative-interface)
  6612. org-goto-interface
  6613. (if (eq org-goto-interface 'outline)
  6614. 'outline-path-completion
  6615. 'outline)))
  6616. (org-goto-start-pos (point))
  6617. (selected-point
  6618. (if (eq interface 'outline)
  6619. (car (org-get-location (current-buffer) org-goto-help))
  6620. (let ((pa (org-refile-get-location "Goto")))
  6621. (org-refile-check-position pa)
  6622. (nth 3 pa)))))
  6623. (if selected-point
  6624. (progn
  6625. (org-mark-ring-push org-goto-start-pos)
  6626. (goto-char selected-point)
  6627. (when (or (org-invisible-p) (org-invisible-p2))
  6628. (org-show-context 'org-goto)))
  6629. (message "Quit"))))
  6630. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6631. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6632. (defvar org-goto-local-auto-isearch-map) ; defined below
  6633. (defun org-get-location (_buf help)
  6634. "Let the user select a location in current buffer.
  6635. This function uses a recursive edit. It returns the selected position
  6636. or nil."
  6637. (org-no-popups
  6638. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6639. (isearch-hide-immediately nil)
  6640. (isearch-search-fun-function
  6641. (lambda () 'org-goto-local-search-headings))
  6642. (org-goto-selected-point org-goto-exit-command))
  6643. (save-excursion
  6644. (save-window-excursion
  6645. (delete-other-windows)
  6646. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6647. (pop-to-buffer-same-window
  6648. (condition-case nil
  6649. (make-indirect-buffer (current-buffer) "*org-goto*")
  6650. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6651. (with-output-to-temp-buffer "*Org Help*"
  6652. (princ (format help (if org-goto-auto-isearch
  6653. " Just type for auto-isearch."
  6654. " n/p/f/b/u to navigate, q to quit."))))
  6655. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6656. (setq buffer-read-only nil)
  6657. (let ((org-startup-truncated t)
  6658. (org-startup-folded nil)
  6659. (org-startup-align-all-tables nil))
  6660. (org-mode)
  6661. (org-overview))
  6662. (setq buffer-read-only t)
  6663. (if (and (boundp 'org-goto-start-pos)
  6664. (integer-or-marker-p org-goto-start-pos))
  6665. (progn (goto-char org-goto-start-pos)
  6666. (when (org-invisible-p)
  6667. (org-show-set-visibility 'lineage)))
  6668. (goto-char (point-min)))
  6669. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6670. (message "Select location and press RET")
  6671. (use-local-map org-goto-map)
  6672. (recursive-edit)))
  6673. (kill-buffer "*org-goto*")
  6674. (cons org-goto-selected-point org-goto-exit-command))))
  6675. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6676. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6677. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6678. (if (fboundp 'isearch-other-control-char)
  6679. (progn
  6680. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6681. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6682. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6683. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6684. (define-key org-goto-local-auto-isearch-map [return] nil))
  6685. (defun org-goto-local-search-headings (string bound noerror)
  6686. "Search and make sure that any matches are in headlines."
  6687. (catch 'return
  6688. (while (if isearch-forward
  6689. (search-forward string bound noerror)
  6690. (search-backward string bound noerror))
  6691. (when (save-match-data
  6692. (and (save-excursion
  6693. (beginning-of-line)
  6694. (looking-at org-complex-heading-regexp))
  6695. (or (not (match-beginning 5))
  6696. (< (point) (match-beginning 5)))))
  6697. (throw 'return (point))))))
  6698. (defun org-goto-local-auto-isearch ()
  6699. "Start isearch."
  6700. (interactive)
  6701. (goto-char (point-min))
  6702. (let ((keys (this-command-keys)))
  6703. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6704. (isearch-mode t)
  6705. (isearch-process-search-char (string-to-char keys)))))
  6706. (defun org-goto-ret (&optional _arg)
  6707. "Finish `org-goto' by going to the new location."
  6708. (interactive "P")
  6709. (setq org-goto-selected-point (point))
  6710. (setq org-goto-exit-command 'return)
  6711. (throw 'exit nil))
  6712. (defun org-goto-left ()
  6713. "Finish `org-goto' by going to the new location."
  6714. (interactive)
  6715. (if (org-at-heading-p)
  6716. (progn
  6717. (beginning-of-line 1)
  6718. (setq org-goto-selected-point (point)
  6719. org-goto-exit-command 'left)
  6720. (throw 'exit nil))
  6721. (user-error "Not on a heading")))
  6722. (defun org-goto-right ()
  6723. "Finish `org-goto' by going to the new location."
  6724. (interactive)
  6725. (if (org-at-heading-p)
  6726. (progn
  6727. (setq org-goto-selected-point (point)
  6728. org-goto-exit-command 'right)
  6729. (throw 'exit nil))
  6730. (user-error "Not on a heading")))
  6731. (defun org-goto-quit ()
  6732. "Finish `org-goto' without cursor motion."
  6733. (interactive)
  6734. (setq org-goto-selected-point nil)
  6735. (setq org-goto-exit-command 'quit)
  6736. (throw 'exit nil))
  6737. ;;; Indirect buffer display of subtrees
  6738. (defvar org-indirect-dedicated-frame nil
  6739. "This is the frame being used for indirect tree display.")
  6740. (defvar org-last-indirect-buffer nil)
  6741. (defun org-tree-to-indirect-buffer (&optional arg)
  6742. "Create indirect buffer and narrow it to current subtree.
  6743. With a numerical prefix ARG, go up to this level and then take that tree.
  6744. If ARG is negative, go up that many levels.
  6745. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6746. indirect buffer previously made with this command, to avoid proliferation of
  6747. indirect buffers. However, when you call the command with a \
  6748. `\\[universal-argument]' prefix, or
  6749. when `org-indirect-buffer-display' is `new-frame', the last buffer is kept
  6750. so that you can work with several indirect buffers at the same time. If
  6751. `org-indirect-buffer-display' is `dedicated-frame', the \
  6752. `\\[universal-argument]' prefix also
  6753. requests that a new frame be made for the new buffer, so that the dedicated
  6754. frame is not changed."
  6755. (interactive "P")
  6756. (let ((cbuf (current-buffer))
  6757. (cwin (selected-window))
  6758. (pos (point))
  6759. beg end level heading ibuf)
  6760. (save-excursion
  6761. (org-back-to-heading t)
  6762. (when (numberp arg)
  6763. (setq level (org-outline-level))
  6764. (when (< arg 0) (setq arg (+ level arg)))
  6765. (while (> (setq level (org-outline-level)) arg)
  6766. (org-up-heading-safe)))
  6767. (setq beg (point)
  6768. heading (org-get-heading 'no-tags))
  6769. (org-end-of-subtree t t)
  6770. (when (org-at-heading-p) (backward-char 1))
  6771. (setq end (point)))
  6772. (when (and (buffer-live-p org-last-indirect-buffer)
  6773. (not (eq org-indirect-buffer-display 'new-frame))
  6774. (not arg))
  6775. (kill-buffer org-last-indirect-buffer))
  6776. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6777. org-last-indirect-buffer ibuf)
  6778. (cond
  6779. ((or (eq org-indirect-buffer-display 'new-frame)
  6780. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6781. (select-frame (make-frame))
  6782. (delete-other-windows)
  6783. (pop-to-buffer-same-window ibuf)
  6784. (org-set-frame-title heading))
  6785. ((eq org-indirect-buffer-display 'dedicated-frame)
  6786. (raise-frame
  6787. (select-frame (or (and org-indirect-dedicated-frame
  6788. (frame-live-p org-indirect-dedicated-frame)
  6789. org-indirect-dedicated-frame)
  6790. (setq org-indirect-dedicated-frame (make-frame)))))
  6791. (delete-other-windows)
  6792. (pop-to-buffer-same-window ibuf)
  6793. (org-set-frame-title (concat "Indirect: " heading)))
  6794. ((eq org-indirect-buffer-display 'current-window)
  6795. (pop-to-buffer-same-window ibuf))
  6796. ((eq org-indirect-buffer-display 'other-window)
  6797. (pop-to-buffer ibuf))
  6798. (t (error "Invalid value")))
  6799. (narrow-to-region beg end)
  6800. (outline-show-all)
  6801. (goto-char pos)
  6802. (run-hook-with-args 'org-cycle-hook 'all)
  6803. (and (window-live-p cwin) (select-window cwin))))
  6804. (defun org-get-indirect-buffer (&optional buffer heading)
  6805. (setq buffer (or buffer (current-buffer)))
  6806. (let ((n 1) (base (buffer-name buffer)) bname)
  6807. (while (buffer-live-p
  6808. (get-buffer
  6809. (setq bname
  6810. (concat base "-"
  6811. (if heading (concat heading "-" (number-to-string n))
  6812. (number-to-string n))))))
  6813. (setq n (1+ n)))
  6814. (condition-case nil
  6815. (make-indirect-buffer buffer bname 'clone)
  6816. (error (make-indirect-buffer buffer bname)))))
  6817. (defun org-set-frame-title (title)
  6818. "Set the title of the current frame to the string TITLE."
  6819. (modify-frame-parameters (selected-frame) (list (cons 'name title))))
  6820. ;;;; Structure editing
  6821. ;;; Inserting headlines
  6822. (defun org--line-empty-p (n)
  6823. "Is the Nth next line empty?
  6824. Counts the current line as N = 1 and the previous line as N = 0;
  6825. see `beginning-of-line'."
  6826. (save-excursion
  6827. (and (not (bobp))
  6828. (or (beginning-of-line n) t)
  6829. (save-match-data
  6830. (looking-at "[ \t]*$")))))
  6831. (defun org-previous-line-empty-p ()
  6832. "Is the previous line a blank line?
  6833. When NEXT is non-nil, check the next line instead."
  6834. (org--line-empty-p 0))
  6835. (defun org-next-line-empty-p ()
  6836. "Is the previous line a blank line?
  6837. When NEXT is non-nil, check the next line instead."
  6838. (org--line-empty-p 2))
  6839. (defun org--blank-before-heading-p (&optional parent)
  6840. "Non-nil when an empty line should precede a new heading here.
  6841. When optional argument PARENT is non-nil, consider parent
  6842. headline instead of current one."
  6843. (pcase (assq 'heading org-blank-before-new-entry)
  6844. (`(heading . auto)
  6845. (save-excursion
  6846. (org-with-limited-levels
  6847. (unless (and (org-before-first-heading-p)
  6848. (not (outline-next-heading)))
  6849. (org-back-to-heading t)
  6850. (when parent (org-up-heading-safe))
  6851. (cond ((not (bobp))
  6852. (org-previous-line-empty-p))
  6853. ((outline-next-heading)
  6854. (org-previous-line-empty-p))
  6855. ;; Ignore trailing spaces on last buffer line.
  6856. ((progn (skip-chars-backward " \t") (bolp))
  6857. (org-previous-line-empty-p))
  6858. (t nil))))))
  6859. (`(heading . ,value) value)
  6860. (_ nil)))
  6861. (defun org-insert-heading (&optional arg invisible-ok top)
  6862. "Insert a new heading or an item with the same depth at point.
  6863. If point is at the beginning of a heading, insert a new heading
  6864. or a new headline above the current one. When at the beginning
  6865. of a regular line of text, turn it into a heading.
  6866. If point is in the middle of a line, split it and create a new
  6867. headline with the text in the current line after point (see
  6868. `org-M-RET-may-split-line' on how to modify this behavior). As
  6869. a special case, on a headline, splitting can only happen on the
  6870. title itself. E.g., this excludes breaking stars or tags.
  6871. With a `\\[universal-argument]' prefix, set \
  6872. `org-insert-heading-respect-content' to
  6873. a non-nil value for the duration of the command. This forces the
  6874. insertion of a heading after the current subtree, independently
  6875. on the location of point.
  6876. With a `\\[universal-argument] \\[universal-argument]' prefix, \
  6877. insert the heading at the end of the tree
  6878. above the current heading. For example, if point is within a
  6879. 2nd-level heading, then it will insert a 2nd-level heading at
  6880. the end of the 1st-level parent subtree.
  6881. When INVISIBLE-OK is set, stop at invisible headlines when going
  6882. back. This is important for non-interactive uses of the
  6883. command.
  6884. When optional argument TOP is non-nil, insert a level 1 heading,
  6885. unconditionally."
  6886. (interactive "P")
  6887. (let* ((blank? (org--blank-before-heading-p (equal arg '(16))))
  6888. (level (org-current-level))
  6889. (stars (make-string (if (and level (not top)) level 1) ?*)))
  6890. (cond
  6891. ((or org-insert-heading-respect-content
  6892. (member arg '((4) (16)))
  6893. (and (not invisible-ok)
  6894. (invisible-p (max (1- (point)) (point-min)))))
  6895. ;; Position point at the location of insertion.
  6896. (if (not level) ;before first headline
  6897. (org-with-limited-levels (outline-next-heading))
  6898. ;; Make sure we end up on a visible headline if INVISIBLE-OK
  6899. ;; is nil.
  6900. (org-with-limited-levels (org-back-to-heading invisible-ok))
  6901. (cond ((equal arg '(16))
  6902. (org-up-heading-safe)
  6903. (org-end-of-subtree t t))
  6904. (t
  6905. (org-end-of-subtree t t))))
  6906. (unless (bolp) (insert "\n")) ;ensure final newline
  6907. (unless (and blank? (org-previous-line-empty-p))
  6908. (org-N-empty-lines-before-current (if blank? 1 0)))
  6909. (insert stars " \n")
  6910. (forward-char -1))
  6911. ;; At a headline...
  6912. ((org-at-heading-p)
  6913. (cond ((bolp)
  6914. (when blank? (save-excursion (insert "\n")))
  6915. (save-excursion (insert stars " \n"))
  6916. (unless (and blank? (org-previous-line-empty-p))
  6917. (org-N-empty-lines-before-current (if blank? 1 0)))
  6918. (end-of-line))
  6919. ((and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6920. (org-match-line org-complex-heading-regexp)
  6921. (org-pos-in-match-range (point) 4))
  6922. ;; Grab the text that should moved to the new headline.
  6923. ;; Preserve tags.
  6924. (let ((split (delete-and-extract-region (point) (match-end 4))))
  6925. (if (looking-at "[ \t]*$") (replace-match "")
  6926. (org-set-tags nil t))
  6927. (end-of-line)
  6928. (when blank? (insert "\n"))
  6929. (insert "\n" stars " ")
  6930. (when (org-string-nw-p split) (insert split))
  6931. (when (eobp) (save-excursion (insert "\n")))))
  6932. (t
  6933. (end-of-line)
  6934. (when blank? (insert "\n"))
  6935. (insert "\n" stars " ")
  6936. (when (eobp) (save-excursion (insert "\n"))))))
  6937. ;; On regular text, turn line into a headline or split, if
  6938. ;; appropriate.
  6939. ((bolp)
  6940. (insert stars " ")
  6941. (unless (and blank? (org-previous-line-empty-p))
  6942. (org-N-empty-lines-before-current (if blank? 1 0))))
  6943. (t
  6944. (unless (org-get-alist-option org-M-RET-may-split-line 'headline)
  6945. (end-of-line))
  6946. (insert "\n" stars " ")
  6947. (unless (and blank? (org-previous-line-empty-p))
  6948. (org-N-empty-lines-before-current (if blank? 1 0))))))
  6949. (run-hooks 'org-insert-heading-hook))
  6950. (defun org-N-empty-lines-before-current (n)
  6951. "Make the number of empty lines before current exactly N.
  6952. So this will delete or add empty lines."
  6953. (let ((column (current-column)))
  6954. (beginning-of-line)
  6955. (unless (bobp)
  6956. (let ((start (save-excursion
  6957. (skip-chars-backward " \r\t\n")
  6958. (line-end-position))))
  6959. (delete-region start (line-end-position 0))))
  6960. (insert (make-string n ?\n))
  6961. (move-to-column column)))
  6962. (defun org-get-heading (&optional no-tags no-todo no-priority no-comment)
  6963. "Return the heading of the current entry, without the stars.
  6964. When NO-TAGS is non-nil, don't include tags.
  6965. When NO-TODO is non-nil, don't include TODO keywords.
  6966. When NO-PRIORITY is non-nil, don't include priority cookie.
  6967. When NO-COMMENT is non-nil, don't include COMMENT string."
  6968. (save-excursion
  6969. (org-back-to-heading t)
  6970. (let ((case-fold-search nil))
  6971. (looking-at org-complex-heading-regexp)
  6972. (let ((todo (and (not no-todo) (match-string 2)))
  6973. (priority (and (not no-priority) (match-string 3)))
  6974. (headline (pcase (match-string 4)
  6975. (`nil "")
  6976. ((and (guard no-comment) h)
  6977. (replace-regexp-in-string
  6978. (eval-when-compile
  6979. (format "\\`%s[ \t]+" org-comment-string))
  6980. "" h))
  6981. (h h)))
  6982. (tags (and (not no-tags) (match-string 5))))
  6983. (mapconcat #'identity
  6984. (delq nil (list todo priority headline tags))
  6985. " ")))))
  6986. (defvar orgstruct-mode) ; defined below
  6987. (defun org-heading-components ()
  6988. "Return the components of the current heading.
  6989. This is a list with the following elements:
  6990. - the level as an integer
  6991. - the reduced level, different if `org-odd-levels-only' is set.
  6992. - the TODO keyword, or nil
  6993. - the priority character, like ?A, or nil if no priority is given
  6994. - the headline text itself, or the tags string if no headline text
  6995. - the tags string, or nil."
  6996. (save-excursion
  6997. (org-back-to-heading t)
  6998. (when (let (case-fold-search)
  6999. (looking-at
  7000. (if orgstruct-mode
  7001. org-heading-regexp
  7002. org-complex-heading-regexp)))
  7003. (if orgstruct-mode
  7004. (list (length (match-string 1))
  7005. (org-reduced-level (length (match-string 1)))
  7006. nil
  7007. nil
  7008. (match-string 2)
  7009. nil)
  7010. (list (length (match-string 1))
  7011. (org-reduced-level (length (match-string 1)))
  7012. (match-string-no-properties 2)
  7013. (and (match-end 3) (aref (match-string 3) 2))
  7014. (match-string-no-properties 4)
  7015. (match-string-no-properties 5))))))
  7016. (defun org-get-entry ()
  7017. "Get the entry text, after heading, entire subtree."
  7018. (save-excursion
  7019. (org-back-to-heading t)
  7020. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7021. (defun org-edit-headline (&optional heading)
  7022. "Edit the current headline.
  7023. Set it to HEADING when provided."
  7024. (interactive)
  7025. (org-with-wide-buffer
  7026. (org-back-to-heading t)
  7027. (let ((case-fold-search nil))
  7028. (when (looking-at org-complex-heading-regexp)
  7029. (let* ((old (match-string-no-properties 4))
  7030. (new (save-match-data
  7031. (org-trim (or heading (read-string "Edit: " old))))))
  7032. (unless (equal old new)
  7033. (if old (replace-match new t t nil 4)
  7034. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7035. (insert " " new))
  7036. (org-set-tags nil t)
  7037. (when (looking-at "[ \t]*$") (replace-match ""))))))))
  7038. (defun org-insert-heading-after-current ()
  7039. "Insert a new heading with same level as current, after current subtree."
  7040. (interactive)
  7041. (org-back-to-heading)
  7042. (org-insert-heading)
  7043. (org-move-subtree-down)
  7044. (end-of-line 1))
  7045. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7046. "Insert heading with `org-insert-heading-respect-content' set to t."
  7047. (interactive)
  7048. (org-insert-heading '(4) invisible-ok))
  7049. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7050. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7051. (interactive)
  7052. (org-insert-todo-heading force-state '(4)))
  7053. (defun org-insert-todo-heading (arg &optional force-heading)
  7054. "Insert a new heading with the same level and TODO state as current heading.
  7055. If the heading has no TODO state, or if the state is DONE, use
  7056. the first state (TODO by default). Also with one prefix arg,
  7057. force first state. With two prefix args, force inserting at the
  7058. end of the parent subtree.
  7059. When called at a plain list item, insert a new item with an
  7060. unchecked check box."
  7061. (interactive "P")
  7062. (when (or force-heading (not (org-insert-item 'checkbox)))
  7063. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7064. force-heading))
  7065. (save-excursion
  7066. (org-back-to-heading)
  7067. (outline-previous-heading)
  7068. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp)))
  7069. (let* ((new-mark-x
  7070. (if (or (equal arg '(4))
  7071. (not (match-beginning 2))
  7072. (member (match-string 2) org-done-keywords))
  7073. (car org-todo-keywords-1)
  7074. (match-string 2)))
  7075. (new-mark
  7076. (or
  7077. (run-hook-with-args-until-success
  7078. 'org-todo-get-default-hook new-mark-x nil)
  7079. new-mark-x)))
  7080. (beginning-of-line 1)
  7081. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7082. (if org-treat-insert-todo-heading-as-state-change
  7083. (org-todo new-mark)
  7084. (insert new-mark " "))))
  7085. (when org-provide-todo-statistics
  7086. (org-update-parent-todo-statistics))))
  7087. (defun org-insert-subheading (arg)
  7088. "Insert a new subheading and demote it.
  7089. Works for outline headings and for plain lists alike."
  7090. (interactive "P")
  7091. (org-insert-heading arg)
  7092. (cond
  7093. ((org-at-heading-p) (org-do-demote))
  7094. ((org-at-item-p) (org-indent-item))))
  7095. (defun org-insert-todo-subheading (arg)
  7096. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7097. Works for outline headings and for plain lists alike."
  7098. (interactive "P")
  7099. (org-insert-todo-heading arg)
  7100. (cond
  7101. ((org-at-heading-p) (org-do-demote))
  7102. ((org-at-item-p) (org-indent-item))))
  7103. ;;; Promotion and Demotion
  7104. (defvar org-after-demote-entry-hook nil
  7105. "Hook run after an entry has been demoted.
  7106. The cursor will be at the beginning of the entry.
  7107. When a subtree is being demoted, the hook will be called for each node.")
  7108. (defvar org-after-promote-entry-hook nil
  7109. "Hook run after an entry has been promoted.
  7110. The cursor will be at the beginning of the entry.
  7111. When a subtree is being promoted, the hook will be called for each node.")
  7112. (defun org-promote-subtree ()
  7113. "Promote the entire subtree.
  7114. See also `org-promote'."
  7115. (interactive)
  7116. (save-excursion
  7117. (org-with-limited-levels (org-map-tree 'org-promote)))
  7118. (org-fix-position-after-promote))
  7119. (defun org-demote-subtree ()
  7120. "Demote the entire subtree.
  7121. See `org-demote' and `org-promote'."
  7122. (interactive)
  7123. (save-excursion
  7124. (org-with-limited-levels (org-map-tree 'org-demote)))
  7125. (org-fix-position-after-promote))
  7126. (defun org-do-promote ()
  7127. "Promote the current heading higher up the tree.
  7128. If the region is active in `transient-mark-mode', promote all
  7129. headings in the region."
  7130. (interactive)
  7131. (save-excursion
  7132. (if (org-region-active-p)
  7133. (org-map-region 'org-promote (region-beginning) (region-end))
  7134. (org-promote)))
  7135. (org-fix-position-after-promote))
  7136. (defun org-do-demote ()
  7137. "Demote the current heading lower down the tree.
  7138. If the region is active in `transient-mark-mode', demote all
  7139. headings in the region."
  7140. (interactive)
  7141. (save-excursion
  7142. (if (org-region-active-p)
  7143. (org-map-region 'org-demote (region-beginning) (region-end))
  7144. (org-demote)))
  7145. (org-fix-position-after-promote))
  7146. (defun org-fix-position-after-promote ()
  7147. "Fix cursor position and indentation after demoting/promoting."
  7148. (let ((pos (point)))
  7149. (when (save-excursion
  7150. (beginning-of-line)
  7151. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  7152. (or (eq pos (match-end 1)) (eq pos (match-end 2))))
  7153. (cond ((eobp) (insert " "))
  7154. ((eolp) (insert " "))
  7155. ((equal (char-after) ?\s) (forward-char 1))))))
  7156. (defun org-current-level ()
  7157. "Return the level of the current entry, or nil if before the first headline.
  7158. The level is the number of stars at the beginning of the
  7159. headline. Use `org-reduced-level' to remove the effect of
  7160. `org-odd-levels'. Unlike to `org-outline-level', this function
  7161. ignores inlinetasks."
  7162. (let ((level (org-with-limited-levels (org-outline-level))))
  7163. (and (> level 0) level)))
  7164. (defun org-get-previous-line-level ()
  7165. "Return the outline depth of the last headline before the current line.
  7166. Returns 0 for the first headline in the buffer, and nil if before the
  7167. first headline."
  7168. (and (org-current-level)
  7169. (or (and (/= (line-beginning-position) (point-min))
  7170. (save-excursion (beginning-of-line 0) (org-current-level)))
  7171. 0)))
  7172. (defun org-reduced-level (l)
  7173. "Compute the effective level of a heading.
  7174. This takes into account the setting of `org-odd-levels-only'."
  7175. (cond
  7176. ((zerop l) 0)
  7177. (org-odd-levels-only (1+ (floor (/ l 2))))
  7178. (t l)))
  7179. (defun org-level-increment ()
  7180. "Return the number of stars that will be added or removed at a
  7181. time to headlines when structure editing, based on the value of
  7182. `org-odd-levels-only'."
  7183. (if org-odd-levels-only 2 1))
  7184. (defun org-get-valid-level (level &optional change)
  7185. "Rectify a level change under the influence of `org-odd-levels-only'.
  7186. LEVEL is a current level, CHANGE is by how much the level should
  7187. be modified. Even if CHANGE is nil, LEVEL may be returned
  7188. modified because even level numbers will become the next higher
  7189. odd number. Returns values greater than 0."
  7190. (if org-odd-levels-only
  7191. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7192. ((> change 0) (1+ (* 2 (/ (+ (1- level) (* 2 change)) 2))))
  7193. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7194. (max 1 (+ level (or change 0)))))
  7195. (defun org-promote ()
  7196. "Promote the current heading higher up the tree."
  7197. (org-with-wide-buffer
  7198. (org-back-to-heading t)
  7199. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7200. after-change-functions))
  7201. (level (save-match-data (funcall outline-level)))
  7202. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7203. (diff (abs (- level (length up-head) -1))))
  7204. (cond
  7205. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7206. (replace-match "# " nil t))
  7207. ((= level 1)
  7208. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7209. (t (replace-match up-head nil t)))
  7210. (unless (= level 1)
  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-promote-entry-hook))))
  7214. (defun org-demote ()
  7215. "Demote the current heading lower down the tree."
  7216. (org-with-wide-buffer
  7217. (org-back-to-heading t)
  7218. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7219. after-change-functions))
  7220. (level (save-match-data (funcall outline-level)))
  7221. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7222. (diff (abs (- level (length down-head) -1))))
  7223. (replace-match down-head nil t)
  7224. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7225. (when org-adapt-indentation (org-fixup-indentation diff))
  7226. (run-hooks 'org-after-demote-entry-hook))))
  7227. (defun org-cycle-level ()
  7228. "Cycle the level of an empty headline through possible states.
  7229. This goes first to child, then to parent, level, then up the hierarchy.
  7230. After top level, it switches back to sibling level."
  7231. (interactive)
  7232. (let ((org-adapt-indentation nil))
  7233. (when (org-point-at-end-of-empty-headline)
  7234. (setq this-command 'org-cycle-level) ; Only needed for caching
  7235. (let ((cur-level (org-current-level))
  7236. (prev-level (org-get-previous-line-level)))
  7237. (cond
  7238. ;; If first headline in file, promote to top-level.
  7239. ((= prev-level 0)
  7240. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7241. do (org-do-promote)))
  7242. ;; If same level as prev, demote one.
  7243. ((= prev-level cur-level)
  7244. (org-do-demote))
  7245. ;; If parent is top-level, promote to top level if not already.
  7246. ((= prev-level 1)
  7247. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7248. do (org-do-promote)))
  7249. ;; If top-level, return to prev-level.
  7250. ((= cur-level 1)
  7251. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7252. do (org-do-demote)))
  7253. ;; If less than prev-level, promote one.
  7254. ((< cur-level prev-level)
  7255. (org-do-promote))
  7256. ;; If deeper than prev-level, promote until higher than
  7257. ;; prev-level.
  7258. ((> cur-level prev-level)
  7259. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7260. do (org-do-promote))))
  7261. t))))
  7262. (defun org-map-tree (fun)
  7263. "Call FUN for every heading underneath the current one."
  7264. (org-back-to-heading t)
  7265. (let ((level (funcall outline-level)))
  7266. (save-excursion
  7267. (funcall fun)
  7268. (while (and (progn
  7269. (outline-next-heading)
  7270. (> (funcall outline-level) level))
  7271. (not (eobp)))
  7272. (funcall fun)))))
  7273. (defun org-map-region (fun beg end)
  7274. "Call FUN for every heading between BEG and END."
  7275. (let ((org-ignore-region t))
  7276. (save-excursion
  7277. (setq end (copy-marker end))
  7278. (goto-char beg)
  7279. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7280. (< (point) end))
  7281. (funcall fun))
  7282. (while (and (progn
  7283. (outline-next-heading)
  7284. (< (point) end))
  7285. (not (eobp)))
  7286. (funcall fun)))))
  7287. (defun org-fixup-indentation (diff)
  7288. "Change the indentation in the current entry by DIFF.
  7289. DIFF is an integer. Indentation is done according to the
  7290. following rules:
  7291. - Planning information and property drawers are always indented
  7292. according to the new level of the headline;
  7293. - Footnote definitions and their contents are ignored;
  7294. - Inlinetasks' boundaries are not shifted;
  7295. - Empty lines are ignored;
  7296. - Other lines' indentation are shifted by DIFF columns, unless
  7297. it would introduce a structural change in the document, in
  7298. which case no shifting is done at all.
  7299. Assume point is at a heading or an inlinetask beginning."
  7300. (org-with-wide-buffer
  7301. (narrow-to-region (line-beginning-position)
  7302. (save-excursion
  7303. (if (org-with-limited-levels (org-at-heading-p))
  7304. (org-with-limited-levels (outline-next-heading))
  7305. (org-inlinetask-goto-end))
  7306. (point)))
  7307. (forward-line)
  7308. ;; Indent properly planning info and property drawer.
  7309. (when (looking-at-p org-planning-line-re)
  7310. (org-indent-line)
  7311. (forward-line))
  7312. (when (looking-at org-property-drawer-re)
  7313. (goto-char (match-end 0))
  7314. (forward-line)
  7315. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7316. (catch 'no-shift
  7317. (when (zerop diff) (throw 'no-shift nil))
  7318. ;; If DIFF is negative, first check if a shift is possible at all
  7319. ;; (e.g., it doesn't break structure). This can only happen if
  7320. ;; some contents are not properly indented.
  7321. (let ((case-fold-search t))
  7322. (when (< diff 0)
  7323. (let ((diff (- diff))
  7324. (forbidden-re (concat org-outline-regexp
  7325. "\\|"
  7326. (substring org-footnote-definition-re 1))))
  7327. (save-excursion
  7328. (while (not (eobp))
  7329. (cond
  7330. ((looking-at-p "[ \t]*$") (forward-line))
  7331. ((and (looking-at-p org-footnote-definition-re)
  7332. (let ((e (org-element-at-point)))
  7333. (and (eq (org-element-type e) 'footnote-definition)
  7334. (goto-char (org-element-property :end e))))))
  7335. ((looking-at-p org-outline-regexp) (forward-line))
  7336. ;; Give up if shifting would move before column 0 or
  7337. ;; if it would introduce a headline or a footnote
  7338. ;; definition.
  7339. (t
  7340. (skip-chars-forward " \t")
  7341. (let ((ind (current-column)))
  7342. (when (or (< ind diff)
  7343. (and (= ind diff) (looking-at-p forbidden-re)))
  7344. (throw 'no-shift nil)))
  7345. ;; Ignore contents of example blocks and source
  7346. ;; blocks if their indentation is meant to be
  7347. ;; preserved. Jump to block's closing line.
  7348. (beginning-of-line)
  7349. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7350. (let ((e (org-element-at-point)))
  7351. (and (memq (org-element-type e)
  7352. '(example-block src-block))
  7353. (or org-src-preserve-indentation
  7354. (org-element-property :preserve-indent e))
  7355. (goto-char (org-element-property :end e))
  7356. (progn (skip-chars-backward " \r\t\n")
  7357. (beginning-of-line)
  7358. t))))
  7359. (forward-line))))))))
  7360. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7361. ;; by DIFF. Also skip contents of source or example blocks
  7362. ;; when indentation is meant to be preserved.
  7363. (while (not (eobp))
  7364. (cond
  7365. ((and (looking-at-p org-footnote-definition-re)
  7366. (let ((e (org-element-at-point)))
  7367. (and (eq (org-element-type e) 'footnote-definition)
  7368. (goto-char (org-element-property :end e))))))
  7369. ((looking-at-p org-outline-regexp) (forward-line))
  7370. ((looking-at-p "[ \t]*$") (forward-line))
  7371. (t
  7372. (indent-line-to (+ (org-get-indentation) diff))
  7373. (beginning-of-line)
  7374. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7375. (let ((e (org-element-at-point)))
  7376. (and (memq (org-element-type e)
  7377. '(example-block src-block))
  7378. (or org-src-preserve-indentation
  7379. (org-element-property :preserve-indent e))
  7380. (goto-char (org-element-property :end e))
  7381. (progn (skip-chars-backward " \r\t\n")
  7382. (beginning-of-line)
  7383. t))))
  7384. (forward-line)))))))))
  7385. (defun org-convert-to-odd-levels ()
  7386. "Convert an Org file with all levels allowed to one with odd levels.
  7387. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7388. level 5 etc."
  7389. (interactive)
  7390. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7391. (let ((outline-level 'org-outline-level)
  7392. (org-odd-levels-only nil) n)
  7393. (save-excursion
  7394. (goto-char (point-min))
  7395. (while (re-search-forward "^\\*\\*+ " nil t)
  7396. (setq n (- (length (match-string 0)) 2))
  7397. (while (>= (setq n (1- n)) 0)
  7398. (org-demote))
  7399. (end-of-line 1))))))
  7400. (defun org-convert-to-oddeven-levels ()
  7401. "Convert an Org file with only odd levels to one with odd/even levels.
  7402. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7403. file contains a section with an even level, conversion would
  7404. destroy the structure of the file. An error is signaled in this
  7405. case."
  7406. (interactive)
  7407. (goto-char (point-min))
  7408. ;; First check if there are no even levels
  7409. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7410. (org-show-set-visibility 'canonical)
  7411. (error "Not all levels are odd in this file. Conversion not possible"))
  7412. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7413. (let ((outline-regexp org-outline-regexp)
  7414. (outline-level 'org-outline-level)
  7415. (org-odd-levels-only nil) n)
  7416. (save-excursion
  7417. (goto-char (point-min))
  7418. (while (re-search-forward "^\\*\\*+ " nil t)
  7419. (setq n (/ (1- (length (match-string 0))) 2))
  7420. (while (>= (setq n (1- n)) 0)
  7421. (org-promote))
  7422. (end-of-line 1))))))
  7423. (defun org-tr-level (n)
  7424. "Make N odd if required."
  7425. (if org-odd-levels-only (1+ (/ n 2)) n))
  7426. ;;; Vertical tree motion, cutting and pasting of subtrees
  7427. (defun org-move-subtree-up (&optional arg)
  7428. "Move the current subtree up past ARG headlines of the same level."
  7429. (interactive "p")
  7430. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7431. (defun org-move-subtree-down (&optional arg)
  7432. "Move the current subtree down past ARG headlines of the same level."
  7433. (interactive "p")
  7434. (setq arg (prefix-numeric-value arg))
  7435. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7436. 'org-get-last-sibling))
  7437. (ins-point (make-marker))
  7438. (cnt (abs arg))
  7439. (col (current-column))
  7440. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7441. ;; Select the tree
  7442. (org-back-to-heading)
  7443. (setq beg0 (point))
  7444. (save-excursion
  7445. (setq ne-beg (org-back-over-empty-lines))
  7446. (setq beg (point)))
  7447. (save-match-data
  7448. (save-excursion (outline-end-of-heading)
  7449. (setq folded (org-invisible-p)))
  7450. (progn (org-end-of-subtree nil t)
  7451. (unless (eobp) (backward-char))))
  7452. (outline-next-heading)
  7453. (setq ne-end (org-back-over-empty-lines))
  7454. (setq end (point))
  7455. (goto-char beg0)
  7456. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7457. ;; include less whitespace
  7458. (save-excursion
  7459. (goto-char beg)
  7460. (forward-line (- ne-beg ne-end))
  7461. (setq beg (point))))
  7462. ;; Find insertion point, with error handling
  7463. (while (> cnt 0)
  7464. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7465. (progn (goto-char beg0)
  7466. (user-error "Cannot move past superior level or buffer limit")))
  7467. (setq cnt (1- cnt)))
  7468. (when (> arg 0)
  7469. ;; Moving forward - still need to move over subtree
  7470. (org-end-of-subtree t t)
  7471. (save-excursion
  7472. (org-back-over-empty-lines)
  7473. (or (bolp) (newline))))
  7474. (setq ne-ins (org-back-over-empty-lines))
  7475. (move-marker ins-point (point))
  7476. (setq txt (buffer-substring beg end))
  7477. (org-save-markers-in-region beg end)
  7478. (delete-region beg end)
  7479. (org-remove-empty-overlays-at beg)
  7480. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7481. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7482. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7483. (let ((bbb (point)))
  7484. (insert-before-markers txt)
  7485. (org-reinstall-markers-in-region bbb)
  7486. (move-marker ins-point bbb))
  7487. (or (bolp) (insert "\n"))
  7488. (setq ins-end (point))
  7489. (goto-char ins-point)
  7490. (org-skip-whitespace)
  7491. (when (and (< arg 0)
  7492. (org-first-sibling-p)
  7493. (> ne-ins ne-beg))
  7494. ;; Move whitespace back to beginning
  7495. (save-excursion
  7496. (goto-char ins-end)
  7497. (let ((kill-whole-line t))
  7498. (kill-line (- ne-ins ne-beg)) (point)))
  7499. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7500. (move-marker ins-point nil)
  7501. (if folded
  7502. (outline-hide-subtree)
  7503. (org-show-entry)
  7504. (org-show-children)
  7505. (org-cycle-hide-drawers 'children))
  7506. (org-clean-visibility-after-subtree-move)
  7507. ;; move back to the initial column we were at
  7508. (move-to-column col)))
  7509. (defvar org-subtree-clip ""
  7510. "Clipboard for cut and paste of subtrees.
  7511. This is actually only a copy of the kill, because we use the normal kill
  7512. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7513. (defvar org-subtree-clip-folded nil
  7514. "Was the last copied subtree folded?
  7515. This is used to fold the tree back after pasting.")
  7516. (defun org-cut-subtree (&optional n)
  7517. "Cut the current subtree into the clipboard.
  7518. With prefix arg N, cut this many sequential subtrees.
  7519. This is a short-hand for marking the subtree and then cutting it."
  7520. (interactive "p")
  7521. (org-copy-subtree n 'cut))
  7522. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7523. "Copy the current subtree it in the clipboard.
  7524. With prefix arg N, copy this many sequential subtrees.
  7525. This is a short-hand for marking the subtree and then copying it.
  7526. If CUT is non-nil, actually cut the subtree.
  7527. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7528. of some markers in the region, even if CUT is non-nil. This is
  7529. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7530. (interactive "p")
  7531. (let (beg end folded (beg0 (point)))
  7532. (if (called-interactively-p 'any)
  7533. (org-back-to-heading nil) ; take what looks like a subtree
  7534. (org-back-to-heading t)) ; take what is really there
  7535. (setq beg (point))
  7536. (skip-chars-forward " \t\r\n")
  7537. (save-match-data
  7538. (if nosubtrees
  7539. (outline-next-heading)
  7540. (save-excursion (outline-end-of-heading)
  7541. (setq folded (org-invisible-p)))
  7542. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7543. (org-end-of-subtree t t)))
  7544. ;; Include the end of an inlinetask
  7545. (when (and (featurep 'org-inlinetask)
  7546. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7547. "END[ \t]*$")))
  7548. (end-of-line))
  7549. (setq end (point))
  7550. (goto-char beg0)
  7551. (when (> end beg)
  7552. (setq org-subtree-clip-folded folded)
  7553. (when (or cut force-store-markers)
  7554. (org-save-markers-in-region beg end))
  7555. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7556. (setq org-subtree-clip (current-kill 0))
  7557. (message "%s: Subtree(s) with %d characters"
  7558. (if cut "Cut" "Copied")
  7559. (length org-subtree-clip)))))
  7560. (defun org-paste-subtree (&optional level tree for-yank remove)
  7561. "Paste the clipboard as a subtree, with modification of headline level.
  7562. The entire subtree is promoted or demoted in order to match a new headline
  7563. level.
  7564. If the cursor is at the beginning of a headline, the same level as
  7565. that headline is used to paste the tree.
  7566. If not, the new level is derived from the *visible* headings
  7567. before and after the insertion point, and taken to be the inferior headline
  7568. level of the two. So if the previous visible heading is level 3 and the
  7569. next is level 4 (or vice versa), level 4 will be used for insertion.
  7570. This makes sure that the subtree remains an independent subtree and does
  7571. not swallow low level entries.
  7572. You can also force a different level, either by using a numeric prefix
  7573. argument, or by inserting the heading marker by hand. For example, if the
  7574. cursor is after \"*****\", then the tree will be shifted to level 5.
  7575. If optional TREE is given, use this text instead of the kill ring.
  7576. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7577. move back over whitespace before inserting, and move point to the end of
  7578. the inserted text when done.
  7579. When REMOVE is non-nil, remove the subtree from the clipboard."
  7580. (interactive "P")
  7581. (setq tree (or tree (and kill-ring (current-kill 0))))
  7582. (unless (org-kill-is-subtree-p tree)
  7583. (user-error "%s"
  7584. (substitute-command-keys
  7585. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7586. (org-with-limited-levels
  7587. (let* ((visp (not (org-invisible-p)))
  7588. (txt tree)
  7589. (^re_ "\\(\\*+\\)[ \t]*")
  7590. (old-level (if (string-match org-outline-regexp-bol txt)
  7591. (- (match-end 0) (match-beginning 0) 1)
  7592. -1))
  7593. (force-level (cond (level (prefix-numeric-value level))
  7594. ((and (looking-at "[ \t]*$")
  7595. (string-match
  7596. "^\\*+$" (buffer-substring
  7597. (point-at-bol) (point))))
  7598. (- (match-end 0) (match-beginning 0)))
  7599. ((and (bolp)
  7600. (looking-at org-outline-regexp))
  7601. (- (match-end 0) (point) 1))))
  7602. (previous-level (save-excursion
  7603. (condition-case nil
  7604. (progn
  7605. (outline-previous-visible-heading 1)
  7606. (if (looking-at ^re_)
  7607. (- (match-end 0) (match-beginning 0) 1)
  7608. 1))
  7609. (error 1))))
  7610. (next-level (save-excursion
  7611. (condition-case nil
  7612. (progn
  7613. (or (looking-at org-outline-regexp)
  7614. (outline-next-visible-heading 1))
  7615. (if (looking-at ^re_)
  7616. (- (match-end 0) (match-beginning 0) 1)
  7617. 1))
  7618. (error 1))))
  7619. (new-level (or force-level (max previous-level next-level)))
  7620. (shift (if (or (= old-level -1)
  7621. (= new-level -1)
  7622. (= old-level new-level))
  7623. 0
  7624. (- new-level old-level)))
  7625. (delta (if (> shift 0) -1 1))
  7626. (func (if (> shift 0) 'org-demote 'org-promote))
  7627. (org-odd-levels-only nil)
  7628. beg end newend)
  7629. ;; Remove the forced level indicator
  7630. (when force-level
  7631. (delete-region (point-at-bol) (point)))
  7632. ;; Paste
  7633. (beginning-of-line (if (bolp) 1 2))
  7634. (setq beg (point))
  7635. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7636. (insert-before-markers txt)
  7637. (unless (string-suffix-p "\n" txt) (insert "\n"))
  7638. (setq newend (point))
  7639. (org-reinstall-markers-in-region beg)
  7640. (setq end (point))
  7641. (goto-char beg)
  7642. (skip-chars-forward " \t\n\r")
  7643. (setq beg (point))
  7644. (when (and (org-invisible-p) visp)
  7645. (save-excursion (outline-show-heading)))
  7646. ;; Shift if necessary
  7647. (unless (= shift 0)
  7648. (save-restriction
  7649. (narrow-to-region beg end)
  7650. (while (not (= shift 0))
  7651. (org-map-region func (point-min) (point-max))
  7652. (setq shift (+ delta shift)))
  7653. (goto-char (point-min))
  7654. (setq newend (point-max))))
  7655. (when (or (called-interactively-p 'interactive) for-yank)
  7656. (message "Clipboard pasted as level %d subtree" new-level))
  7657. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7658. kill-ring
  7659. (eq org-subtree-clip (current-kill 0))
  7660. org-subtree-clip-folded)
  7661. ;; The tree was folded before it was killed/copied
  7662. (outline-hide-subtree))
  7663. (and for-yank (goto-char newend))
  7664. (and remove (setq kill-ring (cdr kill-ring))))))
  7665. (defun org-kill-is-subtree-p (&optional txt)
  7666. "Check if the current kill is an outline subtree, or a set of trees.
  7667. Returns nil if kill does not start with a headline, or if the first
  7668. headline level is not the largest headline level in the tree.
  7669. So this will actually accept several entries of equal levels as well,
  7670. which is OK for `org-paste-subtree'.
  7671. If optional TXT is given, check this string instead of the current kill."
  7672. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7673. (re (org-get-limited-outline-regexp))
  7674. (^re (concat "^" re))
  7675. (start-level (and kill
  7676. (string-match
  7677. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7678. kill)
  7679. (- (match-end 2) (match-beginning 2) 1)))
  7680. (start (1+ (or (match-beginning 2) -1))))
  7681. (if (not start-level)
  7682. (progn
  7683. nil) ;; does not even start with a heading
  7684. (catch 'exit
  7685. (while (setq start (string-match ^re kill (1+ start)))
  7686. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7687. (throw 'exit nil)))
  7688. t))))
  7689. (defvar org-markers-to-move nil
  7690. "Markers that should be moved with a cut-and-paste operation.
  7691. Those markers are stored together with their positions relative to
  7692. the start of the region.")
  7693. (defun org-save-markers-in-region (beg end)
  7694. "Check markers in region.
  7695. If these markers are between BEG and END, record their position relative
  7696. to BEG, so that after moving the block of text, we can put the markers back
  7697. into place.
  7698. This function gets called just before an entry or tree gets cut from the
  7699. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7700. called immediately, to move the markers with the entries."
  7701. (setq org-markers-to-move nil)
  7702. (when (featurep 'org-clock)
  7703. (org-clock-save-markers-for-cut-and-paste beg end))
  7704. (when (featurep 'org-agenda)
  7705. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7706. (defun org-check-and-save-marker (marker beg end)
  7707. "Check if MARKER is between BEG and END.
  7708. If yes, remember the marker and the distance to BEG."
  7709. (when (and (marker-buffer marker)
  7710. (equal (marker-buffer marker) (current-buffer))
  7711. (>= marker beg) (< marker end))
  7712. (push (cons marker (- marker beg)) org-markers-to-move)))
  7713. (defun org-reinstall-markers-in-region (beg)
  7714. "Move all remembered markers to their position relative to BEG."
  7715. (dolist (x org-markers-to-move)
  7716. (move-marker (car x) (+ beg (cdr x))))
  7717. (setq org-markers-to-move nil))
  7718. (defun org-narrow-to-subtree ()
  7719. "Narrow buffer to the current subtree."
  7720. (interactive)
  7721. (save-excursion
  7722. (save-match-data
  7723. (org-with-limited-levels
  7724. (narrow-to-region
  7725. (progn (org-back-to-heading t) (point))
  7726. (progn (org-end-of-subtree t t)
  7727. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7728. (point)))))))
  7729. (defun org-narrow-to-block ()
  7730. "Narrow buffer to the current block."
  7731. (interactive)
  7732. (let* ((case-fold-search t)
  7733. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7734. "^[ \t]*#\\+end_.*")))
  7735. (if blockp
  7736. (narrow-to-region (car blockp) (cdr blockp))
  7737. (user-error "Not in a block"))))
  7738. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7739. "Clone the task (subtree) at point N times.
  7740. The clones will be inserted as siblings.
  7741. In interactive use, the user will be prompted for the number of
  7742. clones to be produced. If the entry has a timestamp, the user
  7743. will also be prompted for a time shift, which may be a repeater
  7744. as used in time stamps, for example `+3d'. To disable this,
  7745. you can call the function with a universal prefix argument.
  7746. When a valid repeater is given and the entry contains any time
  7747. stamps, the clones will become a sequence in time, with time
  7748. stamps in the subtree shifted for each clone produced. If SHIFT
  7749. is nil or the empty string, time stamps will be left alone. The
  7750. ID property of the original subtree is removed.
  7751. In each clone, all the CLOCK entries will be removed. This
  7752. prevents Org from considering that the clocked times overlap.
  7753. If the original subtree did contain time stamps with a repeater,
  7754. the following will happen:
  7755. - the repeater will be removed in each clone
  7756. - an additional clone will be produced, with the current, unshifted
  7757. date(s) in the entry.
  7758. - the original entry will be placed *after* all the clones, with
  7759. repeater intact.
  7760. - the start days in the repeater in the original entry will be shifted
  7761. to past the last clone.
  7762. In this way you can spell out a number of instances of a repeating task,
  7763. and still retain the repeater to cover future instances of the task.
  7764. As described above, N+1 clones are produced when the original
  7765. subtree has a repeater. Setting N to 0, then, can be used to
  7766. remove the repeater from a subtree and create a shifted clone
  7767. with the original repeater."
  7768. (interactive "nNumber of clones to produce: ")
  7769. (unless (wholenump n) (user-error "Invalid number of replications %s" n))
  7770. (when (org-before-first-heading-p) (user-error "No subtree to clone"))
  7771. (let* ((beg (save-excursion (org-back-to-heading t) (point)))
  7772. (end-of-tree (save-excursion (org-end-of-subtree t t) (point)))
  7773. (shift
  7774. (or shift
  7775. (if (and (not (equal current-prefix-arg '(4)))
  7776. (save-excursion
  7777. (goto-char beg)
  7778. (re-search-forward org-ts-regexp-both end-of-tree t)))
  7779. (read-from-minibuffer
  7780. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7781. ""))) ;No time shift
  7782. (doshift
  7783. (and (org-string-nw-p shift)
  7784. (or (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  7785. shift)
  7786. (user-error "Invalid shift specification %s" shift)))))
  7787. (goto-char end-of-tree)
  7788. (unless (bolp) (insert "\n"))
  7789. (let* ((end (point))
  7790. (template (buffer-substring beg end))
  7791. (shift-n (and doshift (string-to-number (match-string 1 shift))))
  7792. (shift-what (pcase (and doshift (match-string 2 shift))
  7793. (`nil nil)
  7794. ("d" 'day)
  7795. ("w" (setq shift-n (* 7 shift-n)) 'day)
  7796. ("m" 'month)
  7797. ("y" 'year)
  7798. (_ (error "Unsupported time unit"))))
  7799. (nmin 1)
  7800. (nmax n)
  7801. (n-no-remove -1)
  7802. (idprop (org-entry-get nil "ID")))
  7803. (when (and doshift
  7804. (string-match-p "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>"
  7805. template))
  7806. (delete-region beg end)
  7807. (setq end beg)
  7808. (setq nmin 0)
  7809. (setq nmax (1+ nmax))
  7810. (setq n-no-remove nmax))
  7811. (goto-char end)
  7812. (cl-loop for n from nmin to nmax do
  7813. (insert
  7814. ;; Prepare clone.
  7815. (with-temp-buffer
  7816. (insert template)
  7817. (org-mode)
  7818. (goto-char (point-min))
  7819. (org-show-subtree)
  7820. (and idprop (if org-clone-delete-id
  7821. (org-entry-delete nil "ID")
  7822. (org-id-get-create t)))
  7823. (unless (= n 0)
  7824. (while (re-search-forward org-clock-line-re nil t)
  7825. (delete-region (line-beginning-position)
  7826. (line-beginning-position 2)))
  7827. (goto-char (point-min))
  7828. (while (re-search-forward org-drawer-regexp nil t)
  7829. (org-remove-empty-drawer-at (point))))
  7830. (goto-char (point-min))
  7831. (when doshift
  7832. (while (re-search-forward org-ts-regexp-both nil t)
  7833. (org-timestamp-change (* n shift-n) shift-what))
  7834. (unless (= n n-no-remove)
  7835. (goto-char (point-min))
  7836. (while (re-search-forward org-ts-regexp nil t)
  7837. (save-excursion
  7838. (goto-char (match-beginning 0))
  7839. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7840. (delete-region (match-beginning 1) (match-end 1)))))))
  7841. (buffer-string)))))
  7842. (goto-char beg)))
  7843. ;;; Outline Sorting
  7844. (defun org-sort (&optional with-case)
  7845. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7846. Optional argument WITH-CASE means sort case-sensitively."
  7847. (interactive "P")
  7848. (org-call-with-arg
  7849. (cond ((org-at-table-p) #'org-table-sort-lines)
  7850. ((org-at-item-p) #'org-sort-list)
  7851. (t #'org-sort-entries))
  7852. with-case))
  7853. (defun org-sort-remove-invisible (s)
  7854. "Remove invisible part of links and emphasis markers from string S."
  7855. (remove-text-properties 0 (length s) org-rm-props s)
  7856. (replace-regexp-in-string
  7857. org-verbatim-re (lambda (m) (format "%s " (match-string 4 m)))
  7858. (replace-regexp-in-string
  7859. org-emph-re (lambda (m) (format " %s " (match-string 4 m)))
  7860. (org-link-display-format s)
  7861. t t) t t))
  7862. (defvar org-priority-regexp) ; defined later in the file
  7863. (defvar org-after-sorting-entries-or-items-hook nil
  7864. "Hook that is run after a bunch of entries or items have been sorted.
  7865. When children are sorted, the cursor is in the parent line when this
  7866. hook gets called. When a region or a plain list is sorted, the cursor
  7867. will be in the first entry of the sorted region/list.")
  7868. (defun org-sort-entries
  7869. (&optional with-case sorting-type getkey-func compare-func property
  7870. interactive?)
  7871. "Sort entries on a certain level of an outline tree.
  7872. If there is an active region, the entries in the region are sorted.
  7873. Else, if the cursor is before the first entry, sort the top-level items.
  7874. Else, the children of the entry at point are sorted.
  7875. Sorting can be alphabetically, numerically, by date/time as given by
  7876. a time stamp, by a property, by priority order, or by a custom function.
  7877. The command prompts for the sorting type unless it has been given to the
  7878. function through the SORTING-TYPE argument, which needs to be a character,
  7879. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7880. the precise meaning of each character:
  7881. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7882. c By creation time, which is assumed to be the first inactive time stamp
  7883. at the beginning of a line.
  7884. d By deadline date/time.
  7885. k By clocking time.
  7886. n Numerically, by converting the beginning of the entry/item to a number.
  7887. o By order of TODO keywords.
  7888. p By priority according to the cookie.
  7889. r By the value of a property.
  7890. s By scheduled date/time.
  7891. t By date/time, either the first active time stamp in the entry, or, if
  7892. none exist, by the first inactive one.
  7893. Capital letters will reverse the sort order.
  7894. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7895. called with point at the beginning of the record. It must return a
  7896. value that is compatible with COMPARE-FUNC, the function used to
  7897. compare entries.
  7898. Comparing entries ignores case by default. However, with an optional argument
  7899. WITH-CASE, the sorting considers case as well.
  7900. Sorting is done against the visible part of the headlines, it ignores hidden
  7901. links.
  7902. When sorting is done, call `org-after-sorting-entries-or-items-hook'.
  7903. A non-nil value for INTERACTIVE? is used to signal that this
  7904. function is being called interactively."
  7905. (interactive (list current-prefix-arg nil nil nil nil t))
  7906. (let ((case-func (if with-case 'identity 'downcase))
  7907. start beg end stars re re2
  7908. txt what tmp)
  7909. ;; Find beginning and end of region to sort
  7910. (cond
  7911. ((org-region-active-p)
  7912. ;; we will sort the region
  7913. (setq end (region-end)
  7914. what "region")
  7915. (goto-char (region-beginning))
  7916. (unless (org-at-heading-p) (outline-next-heading))
  7917. (setq start (point)))
  7918. ((or (org-at-heading-p)
  7919. (ignore-errors (progn (org-back-to-heading) t)))
  7920. ;; we will sort the children of the current headline
  7921. (org-back-to-heading)
  7922. (setq start (point)
  7923. end (progn (org-end-of-subtree t t)
  7924. (or (bolp) (insert "\n"))
  7925. (when (>= (org-back-over-empty-lines) 1)
  7926. (forward-line 1))
  7927. (point))
  7928. what "children")
  7929. (goto-char start)
  7930. (outline-show-subtree)
  7931. (outline-next-heading))
  7932. (t
  7933. ;; we will sort the top-level entries in this file
  7934. (goto-char (point-min))
  7935. (or (org-at-heading-p) (outline-next-heading))
  7936. (setq start (point))
  7937. (goto-char (point-max))
  7938. (beginning-of-line 1)
  7939. (when (looking-at ".*?\\S-")
  7940. ;; File ends in a non-white line
  7941. (end-of-line 1)
  7942. (insert "\n"))
  7943. (setq end (point-max))
  7944. (setq what "top-level")
  7945. (goto-char start)
  7946. (outline-show-all)))
  7947. (setq beg (point))
  7948. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7949. (looking-at "\\(\\*+\\)")
  7950. (setq stars (match-string 1)
  7951. re (concat "^" (regexp-quote stars) " +")
  7952. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7953. txt (buffer-substring beg end))
  7954. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7955. (when (and (not (equal stars "*")) (string-match re2 txt))
  7956. (user-error "Region to sort contains a level above the first entry"))
  7957. (unless sorting-type
  7958. (message
  7959. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7960. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7961. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7962. what)
  7963. (setq sorting-type (read-char-exclusive)))
  7964. (unless getkey-func
  7965. (and (= (downcase sorting-type) ?f)
  7966. (setq getkey-func
  7967. (or (and interactive?
  7968. (org-read-function
  7969. "Function for extracting keys: "))
  7970. (error "Missing key extractor")))))
  7971. (and (= (downcase sorting-type) ?r)
  7972. (not property)
  7973. (setq property
  7974. (completing-read "Property: "
  7975. (mapcar #'list (org-buffer-property-keys t))
  7976. nil t)))
  7977. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7978. (message "Sorting entries...")
  7979. (save-restriction
  7980. (narrow-to-region start end)
  7981. (let ((restore-clock?
  7982. ;; The clock marker is lost when using `sort-subr'; mark
  7983. ;; the clock with temporary `:org-clock-marker-backup'
  7984. ;; text property.
  7985. (when (and (eq (org-clocking-buffer) (current-buffer))
  7986. (<= start (marker-position org-clock-marker))
  7987. (>= end (marker-position org-clock-marker)))
  7988. (org-with-silent-modifications
  7989. (put-text-property (1- org-clock-marker) org-clock-marker
  7990. :org-clock-marker-backup t))
  7991. t))
  7992. (dcst (downcase sorting-type))
  7993. (case-fold-search nil)
  7994. (now (current-time)))
  7995. (sort-subr
  7996. (/= dcst sorting-type)
  7997. ;; This function moves to the beginning character of the "record" to
  7998. ;; be sorted.
  7999. (lambda nil
  8000. (if (re-search-forward re nil t)
  8001. (goto-char (match-beginning 0))
  8002. (goto-char (point-max))))
  8003. ;; This function moves to the last character of the "record" being
  8004. ;; sorted.
  8005. (lambda nil
  8006. (save-match-data
  8007. (condition-case nil
  8008. (outline-forward-same-level 1)
  8009. (error
  8010. (goto-char (point-max))))))
  8011. ;; This function returns the value that gets sorted against.
  8012. (lambda nil
  8013. (cond
  8014. ((= dcst ?n)
  8015. (if (looking-at org-complex-heading-regexp)
  8016. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8017. nil))
  8018. ((= dcst ?a)
  8019. (if (looking-at org-complex-heading-regexp)
  8020. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8021. nil))
  8022. ((= dcst ?k)
  8023. (or (get-text-property (point) :org-clock-minutes) 0))
  8024. ((= dcst ?t)
  8025. (let ((end (save-excursion (outline-next-heading) (point))))
  8026. (if (or (re-search-forward org-ts-regexp end t)
  8027. (re-search-forward org-ts-regexp-both end t))
  8028. (org-time-string-to-seconds (match-string 0))
  8029. (float-time now))))
  8030. ((= dcst ?c)
  8031. (let ((end (save-excursion (outline-next-heading) (point))))
  8032. (if (re-search-forward
  8033. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8034. end t)
  8035. (org-time-string-to-seconds (match-string 0))
  8036. (float-time now))))
  8037. ((= dcst ?s)
  8038. (let ((end (save-excursion (outline-next-heading) (point))))
  8039. (if (re-search-forward org-scheduled-time-regexp end t)
  8040. (org-time-string-to-seconds (match-string 1))
  8041. (float-time now))))
  8042. ((= dcst ?d)
  8043. (let ((end (save-excursion (outline-next-heading) (point))))
  8044. (if (re-search-forward org-deadline-time-regexp end t)
  8045. (org-time-string-to-seconds (match-string 1))
  8046. (float-time now))))
  8047. ((= dcst ?p)
  8048. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8049. (string-to-char (match-string 2))
  8050. org-default-priority))
  8051. ((= dcst ?r)
  8052. (or (org-entry-get nil property) ""))
  8053. ((= dcst ?o)
  8054. (when (looking-at org-complex-heading-regexp)
  8055. (let* ((m (match-string 2))
  8056. (s (if (member m org-done-keywords) '- '+)))
  8057. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8058. ((= dcst ?f)
  8059. (if getkey-func
  8060. (progn
  8061. (setq tmp (funcall getkey-func))
  8062. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8063. tmp)
  8064. (error "Invalid key function `%s'" getkey-func)))
  8065. (t (error "Invalid sorting type `%c'" sorting-type))))
  8066. nil
  8067. (cond
  8068. ((= dcst ?a) 'string<)
  8069. ((= dcst ?f)
  8070. (or compare-func
  8071. (and interactive?
  8072. (org-read-function
  8073. (concat "Function for comparing keys "
  8074. "(empty for default `sort-subr' predicate): ")
  8075. 'allow-empty))))
  8076. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))
  8077. (when restore-clock?
  8078. (move-marker org-clock-marker
  8079. (1+ (next-single-property-change
  8080. start :org-clock-marker-backup)))
  8081. (remove-text-properties (1- org-clock-marker) org-clock-marker
  8082. '(:org-clock-marker-backup t)))))
  8083. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8084. (message "Sorting entries...done")))
  8085. ;;; The orgstruct minor mode
  8086. ;; Define a minor mode which can be used in other modes in order to
  8087. ;; integrate the Org mode structure editing commands.
  8088. ;; This is really a hack, because the Org mode structure commands use
  8089. ;; keys which normally belong to the major mode. Here is how it
  8090. ;; works: The minor mode defines all the keys necessary to operate the
  8091. ;; structure commands, but wraps the commands into a function which
  8092. ;; tests if the cursor is currently at a headline or a plain list
  8093. ;; item. If that is the case, the structure command is used,
  8094. ;; temporarily setting many Org mode variables like regular
  8095. ;; expressions for filling etc. However, when any of those keys is
  8096. ;; used at a different location, function uses `key-binding' to look
  8097. ;; up if the key has an associated command in another currently active
  8098. ;; keymap (minor modes, major mode, global), and executes that
  8099. ;; command. There might be problems if any of the keys is otherwise
  8100. ;; used as a prefix key.
  8101. (defcustom orgstruct-heading-prefix-regexp ""
  8102. "Regexp that matches the custom prefix of Org headlines in
  8103. orgstruct(++)-mode."
  8104. :group 'org
  8105. :version "24.4"
  8106. :package-version '(Org . "8.3")
  8107. :type 'regexp)
  8108. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8109. (defcustom orgstruct-setup-hook nil
  8110. "Hook run after orgstruct-mode-map is filled."
  8111. :group 'org
  8112. :version "24.4"
  8113. :package-version '(Org . "8.0")
  8114. :type 'hook)
  8115. (defvar orgstruct-initialized nil)
  8116. (defvar org-local-vars nil
  8117. "List of local variables, for use by `orgstruct-mode'.")
  8118. ;;;###autoload
  8119. (define-minor-mode orgstruct-mode
  8120. "Toggle the minor mode `orgstruct-mode'.
  8121. This mode is for using Org mode structure commands in other
  8122. modes. The following keys behave as if Org mode were active, if
  8123. the cursor is on a headline, or on a plain list item (both as
  8124. defined by Org mode)."
  8125. nil " OrgStruct" (make-sparse-keymap)
  8126. (funcall (if orgstruct-mode
  8127. 'add-to-invisibility-spec
  8128. 'remove-from-invisibility-spec)
  8129. '(outline . t))
  8130. (when orgstruct-mode
  8131. (org-load-modules-maybe)
  8132. (unless orgstruct-initialized
  8133. (orgstruct-setup)
  8134. (setq orgstruct-initialized t))))
  8135. ;;;###autoload
  8136. (defun turn-on-orgstruct ()
  8137. "Unconditionally turn on `orgstruct-mode'."
  8138. (orgstruct-mode 1))
  8139. (defvar-local orgstruct-is-++ nil
  8140. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8141. (defvar-local org-fb-vars nil)
  8142. (defun orgstruct++-mode (&optional arg)
  8143. "Toggle `orgstruct-mode', the enhanced version of it.
  8144. In addition to setting orgstruct-mode, this also exports all
  8145. indentation and autofilling variables from Org mode into the
  8146. buffer. It will also recognize item context in multiline items."
  8147. (interactive "P")
  8148. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8149. (if (< arg 1)
  8150. (progn (orgstruct-mode -1)
  8151. (dolist (v org-fb-vars)
  8152. (set (make-local-variable (car v))
  8153. (if (eq (car-safe (cadr v)) 'quote)
  8154. (cl-cadadr v)
  8155. (nth 1 v)))))
  8156. (orgstruct-mode 1)
  8157. (setq org-fb-vars nil)
  8158. (unless org-local-vars
  8159. (setq org-local-vars (org-get-local-variables)))
  8160. (let (var val)
  8161. (dolist (x org-local-vars)
  8162. (when (string-match
  8163. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8164. \\|fill-prefix\\|indent-\\)"
  8165. (symbol-name (car x)))
  8166. (setq var (car x) val (nth 1 x))
  8167. (push (list var `(quote ,(eval var))) org-fb-vars)
  8168. (set (make-local-variable var)
  8169. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8170. (setq-local orgstruct-is-++ t))))
  8171. ;;;###autoload
  8172. (defun turn-on-orgstruct++ ()
  8173. "Unconditionally turn on `orgstruct++-mode'."
  8174. (orgstruct++-mode 1))
  8175. (defun orgstruct-error ()
  8176. "Error when there is no default binding for a structure key."
  8177. (interactive)
  8178. (funcall (if (fboundp 'user-error)
  8179. 'user-error
  8180. 'error)
  8181. "This key has no function outside structure elements"))
  8182. (defun orgstruct-setup ()
  8183. "Setup orgstruct keymap."
  8184. (dolist (cell '((org-demote . t)
  8185. (org-metaleft . t)
  8186. (org-metaright . t)
  8187. (org-promote . t)
  8188. (org-shiftmetaleft . t)
  8189. (org-shiftmetaright . t)
  8190. org-backward-element
  8191. org-backward-heading-same-level
  8192. org-ctrl-c-ret
  8193. org-ctrl-c-minus
  8194. org-ctrl-c-star
  8195. org-cycle
  8196. org-force-cycle-archived
  8197. org-forward-heading-same-level
  8198. org-insert-heading
  8199. org-insert-heading-respect-content
  8200. org-kill-note-or-show-branches
  8201. org-mark-subtree
  8202. org-meta-return
  8203. org-metadown
  8204. org-metaup
  8205. org-narrow-to-subtree
  8206. org-promote-subtree
  8207. org-reveal
  8208. org-shiftdown
  8209. org-shiftleft
  8210. org-shiftmetadown
  8211. org-shiftmetaup
  8212. org-shiftright
  8213. org-shifttab
  8214. org-shifttab
  8215. org-shiftup
  8216. org-show-children
  8217. org-show-subtree
  8218. org-sort
  8219. org-up-element
  8220. outline-demote
  8221. outline-next-visible-heading
  8222. outline-previous-visible-heading
  8223. outline-promote
  8224. outline-up-heading))
  8225. (let ((f (or (car-safe cell) cell))
  8226. (disable-when-heading-prefix (cdr-safe cell)))
  8227. (when (fboundp f)
  8228. (let ((new-bindings))
  8229. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8230. (where-is-internal f outline-mode-map)))
  8231. (push binding new-bindings)
  8232. ;; TODO use local-function-key-map
  8233. (dolist (rep '(("<tab>" . "TAB")
  8234. ("<return>" . "RET")
  8235. ("<escape>" . "ESC")
  8236. ("<delete>" . "DEL")))
  8237. (setq binding (read-kbd-macro
  8238. (let ((case-fold-search))
  8239. (replace-regexp-in-string
  8240. (regexp-quote (cdr rep))
  8241. (car rep)
  8242. (key-description binding)))))
  8243. (cl-pushnew binding new-bindings :test 'equal)))
  8244. (dolist (binding new-bindings)
  8245. (let ((key (lookup-key orgstruct-mode-map binding)))
  8246. (when (or (not key) (numberp key))
  8247. (ignore-errors
  8248. (org-defkey orgstruct-mode-map
  8249. binding
  8250. (orgstruct-make-binding
  8251. f binding disable-when-heading-prefix))))))))))
  8252. (run-hooks 'orgstruct-setup-hook))
  8253. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8254. "Create a function for binding in the structure minor mode.
  8255. FUN is the command to call inside a table. KEY is the key that
  8256. should be checked in for a command to execute outside of tables.
  8257. Non-nil `disable-when-heading-prefix' means to disable the command
  8258. if `orgstruct-heading-prefix-regexp' is not empty."
  8259. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8260. (let ((nname name)
  8261. (i 0))
  8262. (while (fboundp (intern nname))
  8263. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8264. (setq name (intern nname)))
  8265. (eval
  8266. (let ((bindings '((org-heading-regexp
  8267. (concat "^"
  8268. orgstruct-heading-prefix-regexp
  8269. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8270. (org-outline-regexp
  8271. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8272. (org-outline-regexp-bol
  8273. (concat "^" org-outline-regexp))
  8274. (outline-regexp org-outline-regexp)
  8275. (outline-heading-end-regexp "\n")
  8276. (outline-level 'org-outline-level)
  8277. (outline-heading-alist))))
  8278. `(defun ,name (arg)
  8279. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8280. "Outside of structure, run the binding of `"
  8281. (key-description key) "'."
  8282. (when disable-when-heading-prefix
  8283. (concat
  8284. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8285. "back to the default binding due to limitations of Org's implementation of\n"
  8286. "`" (symbol-name fun) "'.")))
  8287. (interactive "p")
  8288. (let* ((disable
  8289. ,(and disable-when-heading-prefix
  8290. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8291. (fallback
  8292. (or disable
  8293. (not
  8294. (let* ,bindings
  8295. (org-context-p 'headline 'item
  8296. ,(when (memq fun
  8297. '(org-insert-heading
  8298. org-insert-heading-respect-content
  8299. org-meta-return))
  8300. '(when orgstruct-is-++
  8301. 'item-body))))))))
  8302. (if fallback
  8303. (let* ((orgstruct-mode)
  8304. (binding
  8305. (let ((key ,key))
  8306. (catch 'exit
  8307. (dolist
  8308. (rep
  8309. '(nil
  8310. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8311. ("<\\([^>]*\\)return>" . "\\1RET")
  8312. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8313. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8314. nil)
  8315. (when rep
  8316. (setq key (read-kbd-macro
  8317. (let ((case-fold-search))
  8318. (replace-regexp-in-string
  8319. (car rep)
  8320. (cdr rep)
  8321. (key-description key))))))
  8322. (when (key-binding key)
  8323. (throw 'exit (key-binding key))))))))
  8324. (if (keymapp binding)
  8325. (org-set-transient-map binding)
  8326. (let ((func (or binding
  8327. (unless disable
  8328. 'orgstruct-error))))
  8329. (when func
  8330. (call-interactively func)))))
  8331. (org-run-like-in-org-mode
  8332. (lambda ()
  8333. (interactive)
  8334. (let* ,bindings
  8335. (call-interactively ',fun)))))))))
  8336. name))
  8337. (defun org-contextualize-keys (alist contexts)
  8338. "Return valid elements in ALIST depending on CONTEXTS.
  8339. `org-agenda-custom-commands' or `org-capture-templates' are the
  8340. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8341. or `org-capture-templates-contexts' are the associated contexts
  8342. definitions."
  8343. (let ((contexts
  8344. ;; normalize contexts
  8345. (mapcar
  8346. (lambda(c) (cond ((listp (cadr c))
  8347. (list (car c) (car c) (nth 1 c)))
  8348. ((string= "" (cadr c))
  8349. (list (car c) (car c) (nth 2 c)))
  8350. (t c)))
  8351. contexts))
  8352. (a alist) r s)
  8353. ;; loop over all commands or templates
  8354. (dolist (c a)
  8355. (let (vrules repl)
  8356. (cond
  8357. ((not (assoc (car c) contexts))
  8358. (push c r))
  8359. ((and (assoc (car c) contexts)
  8360. (setq vrules (org-contextualize-validate-key
  8361. (car c) contexts)))
  8362. (mapc (lambda (vr)
  8363. (unless (equal (car vr) (cadr vr))
  8364. (setq repl vr)))
  8365. vrules)
  8366. (if (not repl) (push c r)
  8367. (push (cadr repl) s)
  8368. (push
  8369. (cons (car c)
  8370. (cdr (or (assoc (cadr repl) alist)
  8371. (error "Undefined key `%s' as contextual replacement for `%s'"
  8372. (cadr repl) (car c)))))
  8373. r))))))
  8374. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8375. (delq
  8376. nil
  8377. (delete-dups
  8378. (mapcar (lambda (x)
  8379. (let ((tpl (car x)))
  8380. (unless (delq
  8381. nil
  8382. (mapcar (lambda (y)
  8383. (equal y tpl))
  8384. s))
  8385. x)))
  8386. (reverse r))))))
  8387. (defun org-contextualize-validate-key (key contexts)
  8388. "Check CONTEXTS for agenda or capture KEY."
  8389. (let (res)
  8390. (dolist (r contexts)
  8391. (dolist (rr (car (last r)))
  8392. (when
  8393. (and (equal key (car r))
  8394. (if (functionp rr) (funcall rr)
  8395. (or (and (eq (car rr) 'in-file)
  8396. (buffer-file-name)
  8397. (string-match (cdr rr) (buffer-file-name)))
  8398. (and (eq (car rr) 'in-mode)
  8399. (string-match (cdr rr) (symbol-name major-mode)))
  8400. (and (eq (car rr) 'in-buffer)
  8401. (string-match (cdr rr) (buffer-name)))
  8402. (when (and (eq (car rr) 'not-in-file)
  8403. (buffer-file-name))
  8404. (not (string-match (cdr rr) (buffer-file-name))))
  8405. (when (eq (car rr) 'not-in-mode)
  8406. (not (string-match (cdr rr) (symbol-name major-mode))))
  8407. (when (eq (car rr) 'not-in-buffer)
  8408. (not (string-match (cdr rr) (buffer-name)))))))
  8409. (push r res))))
  8410. (delete-dups (delq nil res))))
  8411. (defun org-context-p (&rest contexts)
  8412. "Check if local context is any of CONTEXTS.
  8413. Possible values in the list of contexts are `table', `headline', and `item'."
  8414. (let ((pos (point)))
  8415. (goto-char (point-at-bol))
  8416. (prog1 (or (and (memq 'table contexts)
  8417. (looking-at "[ \t]*|"))
  8418. (and (memq 'headline contexts)
  8419. (looking-at org-outline-regexp))
  8420. (and (memq 'item contexts)
  8421. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8422. (and (memq 'item-body contexts)
  8423. (org-in-item-p)))
  8424. (goto-char pos))))
  8425. ;;;###autoload
  8426. (defun org-run-like-in-org-mode (cmd)
  8427. "Run a command, pretending that the current buffer is in Org mode.
  8428. This will temporarily bind local variables that are typically bound in
  8429. Org mode to the values they have in Org mode, and then interactively
  8430. call CMD."
  8431. (org-load-modules-maybe)
  8432. (unless org-local-vars
  8433. (setq org-local-vars (org-get-local-variables)))
  8434. (let (binds)
  8435. (dolist (var org-local-vars)
  8436. (when (or (not (boundp (car var)))
  8437. (eq (symbol-value (car var))
  8438. (default-value (car var))))
  8439. (push (list (car var) `(quote ,(cadr var))) binds)))
  8440. (eval `(let ,binds
  8441. (call-interactively (quote ,cmd))))))
  8442. (defun org-get-category (&optional pos force-refresh)
  8443. "Get the category applying to position POS."
  8444. (save-match-data
  8445. (when force-refresh (org-refresh-category-properties))
  8446. (let ((pos (or pos (point))))
  8447. (or (get-text-property pos 'org-category)
  8448. (progn (org-refresh-category-properties)
  8449. (get-text-property pos 'org-category))))))
  8450. ;;; Refresh properties
  8451. (defun org-refresh-properties (dprop tprop)
  8452. "Refresh buffer text properties.
  8453. DPROP is the drawer property and TPROP is either the
  8454. corresponding text property to set, or an alist with each element
  8455. being a text property (as a symbol) and a function to apply to
  8456. the value of the drawer property."
  8457. (let* ((case-fold-search t)
  8458. (inhibit-read-only t)
  8459. (inherit? (org-property-inherit-p dprop))
  8460. (property-re (org-re-property (concat (regexp-quote dprop) "\\+?") t))
  8461. (global (and inherit? (org--property-global-value dprop nil))))
  8462. (org-with-silent-modifications
  8463. (org-with-point-at 1
  8464. ;; Set global values (e.g., values defined through
  8465. ;; "#+PROPERTY:" keywords) to the whole buffer.
  8466. (when global (put-text-property (point-min) (point-max) tprop global))
  8467. ;; Set local values.
  8468. (while (re-search-forward property-re nil t)
  8469. (when (org-at-property-p)
  8470. (org-refresh-property tprop (org-entry-get (point) dprop) inherit?))
  8471. (outline-next-heading))))))
  8472. (defun org-refresh-property (tprop p &optional inherit)
  8473. "Refresh the buffer text property TPROP from the drawer property P.
  8474. The refresh happens only for the current headline, or the whole
  8475. sub-tree if optional argument INHERIT is non-nil."
  8476. (unless (org-before-first-heading-p)
  8477. (save-excursion
  8478. (org-back-to-heading t)
  8479. (let ((start (point))
  8480. (end (save-excursion
  8481. (if inherit (org-end-of-subtree t t)
  8482. (or (outline-next-heading) (point-max))))))
  8483. (if (symbolp tprop)
  8484. ;; TPROP is a text property symbol.
  8485. (put-text-property start end tprop p)
  8486. ;; TPROP is an alist with (property . function) elements.
  8487. (pcase-dolist (`(,prop . ,f) tprop)
  8488. (put-text-property start end prop (funcall f p))))))))
  8489. (defun org-refresh-category-properties ()
  8490. "Refresh category text properties in the buffer."
  8491. (let ((case-fold-search t)
  8492. (inhibit-read-only t)
  8493. (default-category
  8494. (cond ((null org-category)
  8495. (if buffer-file-name
  8496. (file-name-sans-extension
  8497. (file-name-nondirectory buffer-file-name))
  8498. "???"))
  8499. ((symbolp org-category) (symbol-name org-category))
  8500. (t org-category))))
  8501. (org-with-silent-modifications
  8502. (org-with-wide-buffer
  8503. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8504. ;; This is the default category for the buffer. If none is
  8505. ;; found, fall-back to `org-category' or buffer file name.
  8506. (put-text-property
  8507. (point-min) (point-max)
  8508. 'org-category
  8509. (catch 'buffer-category
  8510. (goto-char (point-max))
  8511. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8512. (let ((element (org-element-at-point)))
  8513. (when (eq (org-element-type element) 'keyword)
  8514. (throw 'buffer-category
  8515. (org-element-property :value element)))))
  8516. default-category))
  8517. ;; Set sub-tree specific categories.
  8518. (goto-char (point-min))
  8519. (let ((regexp (org-re-property "CATEGORY")))
  8520. (while (re-search-forward regexp nil t)
  8521. (let ((value (match-string-no-properties 3)))
  8522. (when (org-at-property-p)
  8523. (put-text-property
  8524. (save-excursion (org-back-to-heading t) (point))
  8525. (save-excursion (org-end-of-subtree t t) (point))
  8526. 'org-category
  8527. value)))))))))
  8528. (defun org-refresh-stats-properties ()
  8529. "Refresh stats text properties in the buffer."
  8530. (org-with-silent-modifications
  8531. (org-with-point-at 1
  8532. (let ((regexp (concat org-outline-regexp-bol
  8533. ".*\\[\\([0-9]*\\)\\(?:%\\|/\\([0-9]*\\)\\)\\]")))
  8534. (while (re-search-forward regexp nil t)
  8535. (let* ((numerator (string-to-number (match-string 1)))
  8536. (denominator (and (match-end 2)
  8537. (string-to-number (match-string 2))))
  8538. (stats (cond ((not denominator) numerator) ;percent
  8539. ((= denominator 0) 0)
  8540. (t (/ (* numerator 100) denominator)))))
  8541. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8542. 'org-stats stats)))))))
  8543. (defun org-refresh-effort-properties ()
  8544. "Refresh effort properties"
  8545. (org-refresh-properties
  8546. org-effort-property
  8547. '((effort . identity)
  8548. (effort-minutes . org-duration-to-minutes))))
  8549. ;;;; Link Stuff
  8550. ;;; Link abbreviations
  8551. (defun org-link-expand-abbrev (link)
  8552. "Apply replacements as defined in `org-link-abbrev-alist'."
  8553. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8554. (let* ((key (match-string 1 link))
  8555. (as (or (assoc key org-link-abbrev-alist-local)
  8556. (assoc key org-link-abbrev-alist)))
  8557. (tag (and (match-end 2) (match-string 3 link)))
  8558. rpl)
  8559. (if (not as)
  8560. link
  8561. (setq rpl (cdr as))
  8562. (cond
  8563. ((symbolp rpl) (funcall rpl tag))
  8564. ((string-match "%(\\([^)]+\\))" rpl)
  8565. (replace-match
  8566. (save-match-data
  8567. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8568. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8569. ((string-match "%h" rpl)
  8570. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8571. (t (concat rpl tag)))))
  8572. link))
  8573. ;;; Storing and inserting links
  8574. (defvar org-insert-link-history nil
  8575. "Minibuffer history for links inserted with `org-insert-link'.")
  8576. (defvar org-stored-links nil
  8577. "Contains the links stored with `org-store-link'.")
  8578. (defvar org-store-link-plist nil
  8579. "Plist with info about the most recently link created with `org-store-link'.")
  8580. (defun org-store-link-functions ()
  8581. "Return a list of functions that are called to create and store a link.
  8582. The functions defined in the :store property of
  8583. `org-link-parameters'.
  8584. Each function will be called in turn until one returns a non-nil
  8585. value. Each function should check if it is responsible for
  8586. creating this link (for example by looking at the major mode).
  8587. If not, it must exit and return nil. If yes, it should return
  8588. a non-nil value after calling `org-store-link-props' with a list
  8589. of properties and values. Special properties are:
  8590. :type The link prefix, like \"http\". This must be given.
  8591. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8592. This is obligatory as well.
  8593. :description Optional default description for the second pair
  8594. of brackets in an Org mode link. The user can still change
  8595. this when inserting this link into an Org mode buffer.
  8596. In addition to these, any additional properties can be specified
  8597. and then used in capture templates."
  8598. (cl-loop for link in org-link-parameters
  8599. with store-func
  8600. do (setq store-func (org-link-get-parameter (car link) :store))
  8601. if store-func
  8602. collect store-func))
  8603. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8604. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8605. ;;;###autoload
  8606. (defun org-store-link (arg)
  8607. "Store an org-link to the current location.
  8608. \\<org-mode-map>
  8609. This link is added to `org-stored-links' and can later be inserted
  8610. into an Org buffer with `org-insert-link' (`\\[org-insert-link]').
  8611. For some link types, a `\\[universal-argument]' prefix ARG is interpreted. \
  8612. A single
  8613. `\\[universal-argument]' negates `org-context-in-file-links' for file links or
  8614. `org-gnus-prefer-web-links' for links to Usenet articles.
  8615. A `\\[universal-argument] \\[universal-argument]' prefix ARG forces \
  8616. skipping storing functions that are not
  8617. part of Org core.
  8618. A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  8619. prefix ARG forces storing a link for each line in the
  8620. active region."
  8621. (interactive "P")
  8622. (org-load-modules-maybe)
  8623. (if (and (equal arg '(64)) (org-region-active-p))
  8624. (save-excursion
  8625. (let ((end (region-end)))
  8626. (goto-char (region-beginning))
  8627. (set-mark (point))
  8628. (while (< (point-at-eol) end)
  8629. (move-end-of-line 1) (activate-mark)
  8630. (let (current-prefix-arg)
  8631. (call-interactively 'org-store-link))
  8632. (move-beginning-of-line 2)
  8633. (set-mark (point)))))
  8634. (setq org-store-link-plist nil)
  8635. (let (link cpltxt desc description search
  8636. txt custom-id agenda-link sfuns sfunsn)
  8637. (cond
  8638. ;; Store a link using an external link type
  8639. ((and (not (equal arg '(16)))
  8640. (setq sfuns
  8641. (delq
  8642. nil (mapcar (lambda (f)
  8643. (let (fs) (if (funcall f) (push f fs))))
  8644. (org-store-link-functions)))
  8645. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8646. (or (and (cdr sfuns)
  8647. (funcall (intern
  8648. (completing-read
  8649. "Which function for creating the link? "
  8650. sfunsn nil t (car sfunsn)))))
  8651. (funcall (caar sfuns)))
  8652. (setq link (plist-get org-store-link-plist :link)
  8653. desc (or (plist-get org-store-link-plist
  8654. :description)
  8655. link))))
  8656. ;; Store a link from a source code buffer.
  8657. ((org-src-edit-buffer-p)
  8658. (let ((coderef-format (org-src-coderef-format)))
  8659. (cond ((save-excursion
  8660. (beginning-of-line)
  8661. (looking-at (org-src-coderef-regexp coderef-format)))
  8662. (setq link (format "(%s)" (match-string-no-properties 3))))
  8663. ((called-interactively-p 'any)
  8664. (let ((label (read-string "Code line label: ")))
  8665. (end-of-line)
  8666. (setq link (format coderef-format label))
  8667. (let ((gc (- 79 (length link))))
  8668. (if (< (current-column) gc)
  8669. (org-move-to-column gc t)
  8670. (insert " ")))
  8671. (insert link)
  8672. (setq link (concat "(" label ")"))
  8673. (setq desc nil)))
  8674. (t (setq link nil)))))
  8675. ;; We are in the agenda, link to referenced location
  8676. ((equal (bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8677. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8678. (get-text-property (point) 'org-marker))))
  8679. (when m
  8680. (org-with-point-at m
  8681. (setq agenda-link
  8682. (if (called-interactively-p 'any)
  8683. (call-interactively 'org-store-link)
  8684. (org-store-link nil)))))))
  8685. ((eq major-mode 'calendar-mode)
  8686. (let ((cd (calendar-cursor-to-date)))
  8687. (setq link
  8688. (format-time-string
  8689. (car org-time-stamp-formats)
  8690. (apply 'encode-time
  8691. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8692. nil nil nil))))
  8693. (org-store-link-props :type "calendar" :date cd)))
  8694. ((eq major-mode 'help-mode)
  8695. (setq link (concat "help:" (save-excursion
  8696. (goto-char (point-min))
  8697. (looking-at "^[^ ]+")
  8698. (match-string 0))))
  8699. (org-store-link-props :type "help"))
  8700. ((eq major-mode 'w3-mode)
  8701. (setq cpltxt (if (and (buffer-name)
  8702. (not (string-match "Untitled" (buffer-name))))
  8703. (buffer-name)
  8704. (url-view-url t))
  8705. link (url-view-url t))
  8706. (org-store-link-props :type "w3" :url (url-view-url t)))
  8707. ((eq major-mode 'image-mode)
  8708. (setq cpltxt (concat "file:"
  8709. (abbreviate-file-name buffer-file-name))
  8710. link cpltxt)
  8711. (org-store-link-props :type "image" :file buffer-file-name))
  8712. ;; In dired, store a link to the file of the current line
  8713. ((derived-mode-p 'dired-mode)
  8714. (let ((file (dired-get-filename nil t)))
  8715. (setq file (if file
  8716. (abbreviate-file-name
  8717. (expand-file-name (dired-get-filename nil t)))
  8718. ;; otherwise, no file so use current directory.
  8719. default-directory))
  8720. (setq cpltxt (concat "file:" file)
  8721. link cpltxt)))
  8722. ((setq search (run-hook-with-args-until-success
  8723. 'org-create-file-search-functions))
  8724. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8725. "::" search))
  8726. (setq cpltxt (or description link)))
  8727. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8728. (org-with-limited-levels
  8729. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8730. (cond
  8731. ;; Store a link using the target at point
  8732. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8733. (setq cpltxt
  8734. (concat "file:"
  8735. (abbreviate-file-name
  8736. (buffer-file-name (buffer-base-buffer)))
  8737. "::" (match-string 1))
  8738. link cpltxt))
  8739. ((and (featurep 'org-id)
  8740. (or (eq org-id-link-to-org-use-id t)
  8741. (and (called-interactively-p 'any)
  8742. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8743. (and (eq org-id-link-to-org-use-id
  8744. 'create-if-interactive-and-no-custom-id)
  8745. (not custom-id))))
  8746. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8747. ;; Store a link using the ID at point
  8748. (setq link (condition-case nil
  8749. (prog1 (org-id-store-link)
  8750. (setq desc (or (plist-get org-store-link-plist
  8751. :description)
  8752. "")))
  8753. (error
  8754. ;; Probably before first headline, link only to file
  8755. (concat "file:"
  8756. (abbreviate-file-name
  8757. (buffer-file-name (buffer-base-buffer))))))))
  8758. (t
  8759. ;; Just link to current headline
  8760. (setq cpltxt (concat "file:"
  8761. (abbreviate-file-name
  8762. (buffer-file-name (buffer-base-buffer)))))
  8763. ;; Add a context search string
  8764. (when (org-xor org-context-in-file-links
  8765. (equal arg '(4)))
  8766. (let* ((element (org-element-at-point))
  8767. (name (org-element-property :name element)))
  8768. (setq txt (cond
  8769. ((org-at-heading-p) nil)
  8770. (name)
  8771. ((org-region-active-p)
  8772. (buffer-substring (region-beginning) (region-end)))))
  8773. (when (or (null txt) (string-match "\\S-" txt))
  8774. (setq cpltxt
  8775. (concat cpltxt "::"
  8776. (condition-case nil
  8777. (org-make-org-heading-search-string txt)
  8778. (error "")))
  8779. desc (or name
  8780. (nth 4 (ignore-errors (org-heading-components)))
  8781. "NONE")))))
  8782. (when (string-match "::\\'" cpltxt)
  8783. (setq cpltxt (substring cpltxt 0 -2)))
  8784. (setq link cpltxt)))))
  8785. ((buffer-file-name (buffer-base-buffer))
  8786. ;; Just link to this file here.
  8787. (setq cpltxt (concat "file:"
  8788. (abbreviate-file-name
  8789. (buffer-file-name (buffer-base-buffer)))))
  8790. ;; Add a context string.
  8791. (when (org-xor org-context-in-file-links
  8792. (equal arg '(4)))
  8793. (setq txt (if (org-region-active-p)
  8794. (buffer-substring (region-beginning) (region-end))
  8795. (buffer-substring (point-at-bol) (point-at-eol))))
  8796. ;; Only use search option if there is some text.
  8797. (when (string-match "\\S-" txt)
  8798. (setq cpltxt
  8799. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8800. desc "NONE")))
  8801. (setq link cpltxt))
  8802. ((called-interactively-p 'interactive)
  8803. (user-error "No method for storing a link from this buffer"))
  8804. (t (setq link nil)))
  8805. ;; We're done setting link and desc, clean up
  8806. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8807. (setq link (or link cpltxt)
  8808. desc (or desc cpltxt))
  8809. (cond ((not desc))
  8810. ((equal desc "NONE") (setq desc nil))
  8811. (t (setq desc
  8812. (replace-regexp-in-string
  8813. org-bracket-link-analytic-regexp
  8814. (lambda (m) (or (match-string 5 m) (match-string 3 m)))
  8815. desc))))
  8816. ;; Return the link
  8817. (if (not (and (or (called-interactively-p 'any)
  8818. executing-kbd-macro)
  8819. link))
  8820. (or agenda-link (and link (org-make-link-string link desc)))
  8821. (push (list link desc) org-stored-links)
  8822. (message "Stored: %s" (or desc link))
  8823. (when custom-id
  8824. (setq link (concat "file:" (abbreviate-file-name
  8825. (buffer-file-name)) "::#" custom-id))
  8826. (push (list link desc) org-stored-links))
  8827. (car org-stored-links)))))
  8828. (defun org-store-link-props (&rest plist)
  8829. "Store link properties, extract names, addresses and dates."
  8830. (let ((x (plist-get plist :from)))
  8831. (when x
  8832. (let ((adr (mail-extract-address-components x)))
  8833. (setq plist (plist-put plist :fromname (car adr)))
  8834. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8835. (let ((x (plist-get plist :to)))
  8836. (when x
  8837. (let ((adr (mail-extract-address-components x)))
  8838. (setq plist (plist-put plist :toname (car adr)))
  8839. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8840. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8841. (when x
  8842. (setq plist (plist-put plist :date-timestamp
  8843. (format-time-string
  8844. (org-time-stamp-format t) x)))
  8845. (setq plist (plist-put plist :date-timestamp-inactive
  8846. (format-time-string
  8847. (org-time-stamp-format t t) x)))))
  8848. (let ((from (plist-get plist :from))
  8849. (to (plist-get plist :to)))
  8850. (when (and from to org-from-is-user-regexp)
  8851. (setq plist
  8852. (plist-put plist :fromto
  8853. (if (string-match org-from-is-user-regexp from)
  8854. (concat "to %t")
  8855. (concat "from %f"))))))
  8856. (setq org-store-link-plist plist))
  8857. (defun org-add-link-props (&rest plist)
  8858. "Add these properties to the link property list."
  8859. (let (key value)
  8860. (while plist
  8861. (setq key (pop plist) value (pop plist))
  8862. (setq org-store-link-plist
  8863. (plist-put org-store-link-plist key value)))))
  8864. (defun org-email-link-description (&optional fmt)
  8865. "Return the description part of an email link.
  8866. This takes information from `org-store-link-plist' and formats it
  8867. according to FMT (default from `org-email-link-description-format')."
  8868. (setq fmt (or fmt org-email-link-description-format))
  8869. (let* ((p org-store-link-plist)
  8870. (to (plist-get p :toaddress))
  8871. (from (plist-get p :fromaddress))
  8872. (table
  8873. (list
  8874. (cons "%c" (plist-get p :fromto))
  8875. (cons "%F" (plist-get p :from))
  8876. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8877. (cons "%T" (plist-get p :to))
  8878. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8879. (cons "%s" (plist-get p :subject))
  8880. (cons "%d" (plist-get p :date))
  8881. (cons "%m" (plist-get p :message-id)))))
  8882. (when (string-match "%c" fmt)
  8883. ;; Check if the user wrote this message
  8884. (if (and org-from-is-user-regexp from to
  8885. (save-match-data (string-match org-from-is-user-regexp from)))
  8886. (setq fmt (replace-match "to %t" t t fmt))
  8887. (setq fmt (replace-match "from %f" t t fmt))))
  8888. (org-replace-escapes fmt table)))
  8889. (defun org-make-org-heading-search-string (&optional string)
  8890. "Make search string for the current headline or STRING."
  8891. (let ((s (or string
  8892. (and (derived-mode-p 'org-mode)
  8893. (save-excursion
  8894. (org-back-to-heading t)
  8895. (org-element-property :raw-value (org-element-at-point))))))
  8896. (lines org-context-in-file-links))
  8897. (or string (setq s (concat "*" s))) ; Add * for headlines
  8898. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8899. (when (and string (integerp lines) (> lines 0))
  8900. (let ((slines (org-split-string s "\n")))
  8901. (when (< lines (length slines))
  8902. (setq s (mapconcat
  8903. 'identity
  8904. (reverse (nthcdr (- (length slines) lines)
  8905. (reverse slines))) "\n")))))
  8906. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8907. (defun org-make-link-string (link &optional description)
  8908. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8909. (unless (org-string-nw-p link) (error "Empty link"))
  8910. (let ((uri (cond ((string-match org-link-types-re link)
  8911. (concat (match-string 1 link)
  8912. (org-link-escape (substring link (match-end 1)))))
  8913. ;; For readability, url-encode internal links only
  8914. ;; when absolutely needed (i.e, when they contain
  8915. ;; square brackets). File links however, are
  8916. ;; encoded since, e.g., spaces are significant.
  8917. ((or (file-name-absolute-p link)
  8918. (string-match-p "\\`\\.\\.?/\\|[][]" link))
  8919. (org-link-escape link))
  8920. (t link)))
  8921. (description
  8922. (and (org-string-nw-p description)
  8923. ;; Remove brackets from description, as they are fatal.
  8924. (replace-regexp-in-string
  8925. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8926. (org-trim description)))))
  8927. (format "[[%s]%s]"
  8928. uri
  8929. (if description (format "[%s]" description) ""))))
  8930. (defconst org-link-escape-chars
  8931. ;;%20 %5B %5D %25
  8932. '(?\s ?\[ ?\] ?%)
  8933. "List of characters that should be escaped in a link when stored to Org.
  8934. This is the list that is used for internal purposes.")
  8935. (defun org-link-escape (text &optional table merge)
  8936. "Return percent escaped representation of TEXT.
  8937. TEXT is a string with the text to escape.
  8938. Optional argument TABLE is a list with characters that should be
  8939. escaped. When nil, `org-link-escape-chars' is used.
  8940. If optional argument MERGE is set, merge TABLE into
  8941. `org-link-escape-chars'."
  8942. (let ((characters-to-encode
  8943. (cond ((null table) org-link-escape-chars)
  8944. (merge (append org-link-escape-chars table))
  8945. (t table))))
  8946. (mapconcat
  8947. (lambda (c)
  8948. (if (or (memq c characters-to-encode)
  8949. (and org-url-hexify-p (or (< c 32) (> c 126))))
  8950. (mapconcat (lambda (e) (format "%%%.2X" e))
  8951. (or (encode-coding-char c 'utf-8)
  8952. (error "Unable to percent escape character: %c" c))
  8953. "")
  8954. (char-to-string c)))
  8955. text "")))
  8956. (defun org-link-unescape (str)
  8957. "Unhex hexified Unicode parts in string STR.
  8958. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  8959. reciprocal of `org-link-escape', which see."
  8960. (if (org-string-nw-p str)
  8961. (replace-regexp-in-string
  8962. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  8963. str))
  8964. (defun org-link-unescape-compound (hex)
  8965. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8966. Note: this function also decodes single byte encodings like
  8967. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8968. (save-match-data
  8969. (let* ((bytes (cdr (split-string hex "%")))
  8970. (ret "")
  8971. (eat 0)
  8972. (sum 0))
  8973. (while bytes
  8974. (let* ((val (string-to-number (pop bytes) 16))
  8975. (shift-xor
  8976. (if (= 0 eat)
  8977. (cond
  8978. ((>= val 252) (cons 6 252))
  8979. ((>= val 248) (cons 5 248))
  8980. ((>= val 240) (cons 4 240))
  8981. ((>= val 224) (cons 3 224))
  8982. ((>= val 192) (cons 2 192))
  8983. (t (cons 0 0)))
  8984. (cons 6 128))))
  8985. (when (>= val 192) (setq eat (car shift-xor)))
  8986. (setq val (logxor val (cdr shift-xor)))
  8987. (setq sum (+ (lsh sum (car shift-xor)) val))
  8988. (when (> eat 0) (setq eat (- eat 1)))
  8989. (cond
  8990. ((= 0 eat) ;multi byte
  8991. (setq ret (concat ret (char-to-string sum)))
  8992. (setq sum 0))
  8993. ((not bytes) ; single byte(s)
  8994. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  8995. ret)))
  8996. (defun org-link-unescape-single-byte-sequence (hex)
  8997. "Unhexify hex-encoded single byte character sequences."
  8998. (mapconcat (lambda (byte)
  8999. (char-to-string (string-to-number byte 16)))
  9000. (cdr (split-string hex "%")) ""))
  9001. (defun org-xor (a b)
  9002. "Exclusive or."
  9003. (if a (not b) b))
  9004. (defun org-fixup-message-id-for-http (s)
  9005. "Replace special characters in a message id, so it can be used in an http query."
  9006. (when (string-match "%" s)
  9007. (setq s (mapconcat (lambda (c)
  9008. (if (eq c ?%)
  9009. "%25"
  9010. (char-to-string c)))
  9011. s "")))
  9012. (while (string-match "<" s)
  9013. (setq s (replace-match "%3C" t t s)))
  9014. (while (string-match ">" s)
  9015. (setq s (replace-match "%3E" t t s)))
  9016. (while (string-match "@" s)
  9017. (setq s (replace-match "%40" t t s)))
  9018. s)
  9019. (defun org-link-prettify (link)
  9020. "Return a human-readable representation of LINK.
  9021. The car of LINK must be a raw link.
  9022. The cdr of LINK must be either a link description or nil."
  9023. (let ((desc (or (cadr link) "<no description>")))
  9024. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9025. "<" (car link) ">")))
  9026. ;;;###autoload
  9027. (defun org-insert-link-global ()
  9028. "Insert a link like Org mode does.
  9029. This command can be called in any mode to insert a link in Org syntax."
  9030. (interactive)
  9031. (org-load-modules-maybe)
  9032. (org-run-like-in-org-mode 'org-insert-link))
  9033. (defun org-insert-all-links (arg &optional pre post)
  9034. "Insert all links in `org-stored-links'.
  9035. When a universal prefix, do not delete the links from `org-stored-links'.
  9036. When `ARG' is a number, insert the last N link(s).
  9037. `PRE' and `POST' are optional arguments to define a string to
  9038. prepend or to append."
  9039. (interactive "P")
  9040. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9041. (links (copy-sequence org-stored-links))
  9042. (pr (or pre "- "))
  9043. (po (or post "\n"))
  9044. (cnt 1) l)
  9045. (if (null org-stored-links)
  9046. (message "No link to insert")
  9047. (while (and (or (listp arg) (>= arg cnt))
  9048. (setq l (if (listp arg)
  9049. (pop links)
  9050. (pop org-stored-links))))
  9051. (setq cnt (1+ cnt))
  9052. (insert pr)
  9053. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9054. (insert po)))))
  9055. (defun org-insert-last-stored-link (arg)
  9056. "Insert the last link stored in `org-stored-links'."
  9057. (interactive "p")
  9058. (org-insert-all-links arg "" "\n"))
  9059. (defun org-link-fontify-links-to-this-file ()
  9060. "Fontify links to the current file in `org-stored-links'."
  9061. (let ((f (buffer-file-name)) a b)
  9062. (setq a (mapcar (lambda(l)
  9063. (let ((ll (car l)))
  9064. (when (and (string-match "^file:\\(.+\\)::" ll)
  9065. (equal f (expand-file-name (match-string 1 ll))))
  9066. ll)))
  9067. org-stored-links))
  9068. (when (featurep 'org-id)
  9069. (setq b (mapcar (lambda(l)
  9070. (let ((ll (car l)))
  9071. (when (and (string-match "^id:\\(.+\\)$" ll)
  9072. (equal f (expand-file-name
  9073. (or (org-id-find-id-file
  9074. (match-string 1 ll)) ""))))
  9075. ll)))
  9076. org-stored-links)))
  9077. (mapcar (lambda(l)
  9078. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9079. (delq nil (append a b)))))
  9080. (defvar org--links-history nil)
  9081. (defun org-insert-link (&optional complete-file link-location default-description)
  9082. "Insert a link. At the prompt, enter the link.
  9083. Completion can be used to insert any of the link protocol prefixes in use.
  9084. The history can be used to select a link previously stored with
  9085. `org-store-link'. When the empty string is entered (i.e. if you just
  9086. press `RET' at the prompt), the link defaults to the most recently
  9087. stored link. As `SPC' triggers completion in the minibuffer, you need to
  9088. use `M-SPC' or `C-q SPC' to force the insertion of a space character.
  9089. You will also be prompted for a description, and if one is given, it will
  9090. be displayed in the buffer instead of the link.
  9091. If there is already a link at point, this command will allow you to edit
  9092. link and description parts.
  9093. With a `\\[universal-argument]' prefix, prompts for a file to link to. The \
  9094. file name can be
  9095. selected using completion. The path to the file will be relative to the
  9096. current directory if the file is in the current directory or a subdirectory.
  9097. Otherwise, the link will be the absolute path as completed in the minibuffer
  9098. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9099. option `org-link-file-path-type'.
  9100. With a `\\[universal-argument] \\[universal-argument]' prefix, enforce an \
  9101. absolute path even if the file is in
  9102. the current directory or below.
  9103. A `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  9104. prefix negates `org-keep-stored-link-after-insertion'.
  9105. If `org-make-link-description-function' is non-nil, this function will be
  9106. called with the link target, and the result will be the default
  9107. link description.
  9108. If the LINK-LOCATION parameter is non-nil, this value will be used as
  9109. the link location instead of reading one interactively.
  9110. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will be used
  9111. as the default description."
  9112. (interactive "P")
  9113. (let* ((wcf (current-window-configuration))
  9114. (origbuf (current-buffer))
  9115. (region (when (org-region-active-p)
  9116. (buffer-substring (region-beginning) (region-end))))
  9117. (remove (and region (list (region-beginning) (region-end))))
  9118. (desc region)
  9119. (link link-location)
  9120. (abbrevs org-link-abbrev-alist-local)
  9121. entry all-prefixes auto-desc)
  9122. (cond
  9123. (link-location) ; specified by arg, just use it.
  9124. ((org-in-regexp org-bracket-link-regexp 1)
  9125. ;; We do have a link at point, and we are going to edit it.
  9126. (setq remove (list (match-beginning 0) (match-end 0)))
  9127. (setq desc (when (match-end 3) (match-string-no-properties 3)))
  9128. (setq link (read-string "Link: "
  9129. (org-link-unescape
  9130. (match-string-no-properties 1)))))
  9131. ((or (org-in-regexp org-angle-link-re)
  9132. (org-in-regexp org-plain-link-re))
  9133. ;; Convert to bracket link
  9134. (setq remove (list (match-beginning 0) (match-end 0))
  9135. link (read-string "Link: "
  9136. (org-unbracket-string "<" ">" (match-string 0)))))
  9137. ((member complete-file '((4) (16)))
  9138. ;; Completing read for file names.
  9139. (setq link (org-file-complete-link complete-file)))
  9140. (t
  9141. ;; Read link, with completion for stored links.
  9142. (org-link-fontify-links-to-this-file)
  9143. (org-switch-to-buffer-other-window "*Org Links*")
  9144. (with-current-buffer "*Org Links*"
  9145. (erase-buffer)
  9146. (insert "Insert a link.
  9147. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9148. (when org-stored-links
  9149. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9150. (insert (mapconcat 'org-link-prettify
  9151. (reverse org-stored-links) "\n")))
  9152. (goto-char (point-min)))
  9153. (let ((cw (selected-window)))
  9154. (select-window (get-buffer-window "*Org Links*" 'visible))
  9155. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9156. (unless (pos-visible-in-window-p (point-max))
  9157. (org-fit-window-to-buffer))
  9158. (and (window-live-p cw) (select-window cw)))
  9159. (setq all-prefixes (append (mapcar 'car abbrevs)
  9160. (mapcar 'car org-link-abbrev-alist)
  9161. (org-link-types)))
  9162. (unwind-protect
  9163. ;; Fake a link history, containing the stored links.
  9164. (let ((org--links-history
  9165. (append (mapcar #'car org-stored-links)
  9166. org-insert-link-history)))
  9167. (setq link
  9168. (org-completing-read
  9169. "Link: "
  9170. (append
  9171. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9172. (mapcar #'car org-stored-links))
  9173. nil nil nil
  9174. 'org--links-history
  9175. (caar org-stored-links)))
  9176. (unless (org-string-nw-p link) (user-error "No link selected"))
  9177. (dolist (l org-stored-links)
  9178. (when (equal link (cadr l))
  9179. (setq link (car l))
  9180. (setq auto-desc t)))
  9181. (when (or (member link all-prefixes)
  9182. (and (equal ":" (substring link -1))
  9183. (member (substring link 0 -1) all-prefixes)
  9184. (setq link (substring link 0 -1))))
  9185. (setq link (with-current-buffer origbuf
  9186. (org-link-try-special-completion link)))))
  9187. (set-window-configuration wcf)
  9188. (kill-buffer "*Org Links*"))
  9189. (setq entry (assoc link org-stored-links))
  9190. (or entry (push link org-insert-link-history))
  9191. (setq desc (or desc (nth 1 entry)))))
  9192. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9193. (not org-keep-stored-link-after-insertion))
  9194. (setq org-stored-links (delq (assoc link org-stored-links)
  9195. org-stored-links)))
  9196. (when (and (string-match org-plain-link-re link)
  9197. (not (string-match org-ts-regexp link)))
  9198. ;; URL-like link, normalize the use of angular brackets.
  9199. (setq link (org-unbracket-string "<" ">" link)))
  9200. ;; Check if we are linking to the current file with a search
  9201. ;; option If yes, simplify the link by using only the search
  9202. ;; option.
  9203. (when (and buffer-file-name
  9204. (let ((case-fold-search nil))
  9205. (string-match "\\`file:\\(.+?\\)::" link)))
  9206. (let ((path (match-string-no-properties 1 link))
  9207. (search (substring-no-properties link (match-end 0))))
  9208. (save-match-data
  9209. (when (equal (file-truename buffer-file-name) (file-truename path))
  9210. ;; We are linking to this same file, with a search option
  9211. (setq link search)))))
  9212. ;; Check if we can/should use a relative path. If yes, simplify the link
  9213. (let ((case-fold-search nil))
  9214. (when (string-match "\\`\\(file\\|docview\\):" link)
  9215. (let* ((type (match-string-no-properties 0 link))
  9216. (path (substring-no-properties link (match-end 0)))
  9217. (origpath path))
  9218. (cond
  9219. ((or (eq org-link-file-path-type 'absolute)
  9220. (equal complete-file '(16)))
  9221. (setq path (abbreviate-file-name (expand-file-name path))))
  9222. ((eq org-link-file-path-type 'noabbrev)
  9223. (setq path (expand-file-name path)))
  9224. ((eq org-link-file-path-type 'relative)
  9225. (setq path (file-relative-name path)))
  9226. (t
  9227. (save-match-data
  9228. (if (string-match (concat "^" (regexp-quote
  9229. (expand-file-name
  9230. (file-name-as-directory
  9231. default-directory))))
  9232. (expand-file-name path))
  9233. ;; We are linking a file with relative path name.
  9234. (setq path (substring (expand-file-name path)
  9235. (match-end 0)))
  9236. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9237. (setq link (concat type path))
  9238. (when (equal desc origpath)
  9239. (setq desc path)))))
  9240. (if org-make-link-description-function
  9241. (setq desc
  9242. (or (condition-case nil
  9243. (funcall org-make-link-description-function link desc)
  9244. (error (progn (message "Can't get link description from `%s'"
  9245. (symbol-name org-make-link-description-function))
  9246. (sit-for 2) nil)))
  9247. (read-string "Description: " default-description)))
  9248. (if default-description (setq desc default-description)
  9249. (setq desc (or (and auto-desc desc)
  9250. (read-string "Description: " desc)))))
  9251. (unless (string-match "\\S-" desc) (setq desc nil))
  9252. (when remove (apply 'delete-region remove))
  9253. (insert (org-make-link-string link desc))
  9254. ;; Redisplay so as the new link has proper invisible characters.
  9255. (sit-for 0)))
  9256. (defun org-link-try-special-completion (type)
  9257. "If there is completion support for link type TYPE, offer it."
  9258. (let ((fun (org-link-get-parameter type :complete)))
  9259. (if (functionp fun)
  9260. (funcall fun)
  9261. (read-string "Link (no completion support): " (concat type ":")))))
  9262. (defun org-file-complete-link (&optional arg)
  9263. "Create a file link using completion."
  9264. (let ((file (read-file-name "File: "))
  9265. (pwd (file-name-as-directory (expand-file-name ".")))
  9266. (pwd1 (file-name-as-directory (abbreviate-file-name
  9267. (expand-file-name ".")))))
  9268. (cond ((equal arg '(16))
  9269. (concat "file:"
  9270. (abbreviate-file-name (expand-file-name file))))
  9271. ((string-match
  9272. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9273. (concat "file:" (match-string 1 file)))
  9274. ((string-match
  9275. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9276. (expand-file-name file))
  9277. (concat "file:"
  9278. (match-string 1 (expand-file-name file))))
  9279. (t (concat "file:" file)))))
  9280. (defun org-completing-read (&rest args)
  9281. "Completing-read with SPACE being a normal character."
  9282. (let ((enable-recursive-minibuffers t)
  9283. (minibuffer-local-completion-map
  9284. (copy-keymap minibuffer-local-completion-map)))
  9285. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9286. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9287. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9288. 'org-time-stamp-inactive)
  9289. (apply #'completing-read args)))
  9290. ;;; Opening/following a link
  9291. (defvar org-link-search-failed nil)
  9292. (defvar org-open-link-functions nil
  9293. "Hook for functions finding a plain text link.
  9294. These functions must take a single argument, the link content.
  9295. They will be called for links that look like [[link text][description]]
  9296. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9297. like a filename (e.g. \"./blue.png\").
  9298. These functions will be called *before* Org attempts to resolve the
  9299. link by doing text searches in the current buffer - so if you want a
  9300. link \"[[target]]\" to still find \"<<target>>\", your function should
  9301. handle this as a special case.
  9302. When the function does handle the link, it must return a non-nil value.
  9303. If it decides that it is not responsible for this link, it must return
  9304. nil to indicate that that Org can continue with other options like
  9305. exact and fuzzy text search.")
  9306. (defun org-next-link (&optional search-backward)
  9307. "Move forward to the next link.
  9308. If the link is in hidden text, expose it."
  9309. (interactive "P")
  9310. (when (and org-link-search-failed (eq this-command last-command))
  9311. (goto-char (point-min))
  9312. (message "Link search wrapped back to beginning of buffer"))
  9313. (setq org-link-search-failed nil)
  9314. (let* ((pos (point))
  9315. (ct (org-context))
  9316. (a (assq :link ct))
  9317. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9318. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9319. ((looking-at org-any-link-re)
  9320. ;; Don't stay stuck at link without an org-link face
  9321. (forward-char (if search-backward -1 1))))
  9322. (if (funcall srch-fun org-any-link-re nil t)
  9323. (progn
  9324. (goto-char (match-beginning 0))
  9325. (when (org-invisible-p) (org-show-context)))
  9326. (goto-char pos)
  9327. (setq org-link-search-failed t)
  9328. (message "No further link found"))))
  9329. (defun org-previous-link ()
  9330. "Move backward to the previous link.
  9331. If the link is in hidden text, expose it."
  9332. (interactive)
  9333. (funcall 'org-next-link t))
  9334. (defun org-translate-link (s)
  9335. "Translate a link string if a translation function has been defined."
  9336. (with-temp-buffer
  9337. (insert (org-trim s))
  9338. (org-trim (org-element-interpret-data (org-element-context)))))
  9339. (defun org-translate-link-from-planner (type path)
  9340. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9341. This is still an experimental function, your mileage may vary."
  9342. (cond
  9343. ((member type '("http" "https" "news" "ftp"))
  9344. ;; standard Internet links are the same.
  9345. nil)
  9346. ((and (equal type "irc") (string-match "^//" path))
  9347. ;; Planner has two / at the beginning of an irc link, we have 1.
  9348. ;; We should have zero, actually....
  9349. (setq path (substring path 1)))
  9350. ((and (equal type "lisp") (string-match "^/" path))
  9351. ;; Planner has a slash, we do not.
  9352. (setq type "elisp" path (substring path 1)))
  9353. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9354. ;; A typical message link. Planner has the id after the final slash,
  9355. ;; we separate it with a hash mark
  9356. (setq path (concat (match-string 1 path) "#"
  9357. (org-unbracket-string "<" ">" (match-string 2 path))))))
  9358. (cons type path))
  9359. (defun org-find-file-at-mouse (ev)
  9360. "Open file link or URL at mouse."
  9361. (interactive "e")
  9362. (mouse-set-point ev)
  9363. (org-open-at-point 'in-emacs))
  9364. (defun org-open-at-mouse (ev)
  9365. "Open file link or URL at mouse.
  9366. See the docstring of `org-open-file' for details."
  9367. (interactive "e")
  9368. (mouse-set-point ev)
  9369. (when (eq major-mode 'org-agenda-mode)
  9370. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9371. (org-open-at-point))
  9372. (defvar org-window-config-before-follow-link nil
  9373. "The window configuration before following a link.
  9374. This is saved in case the need arises to restore it.")
  9375. ;;;###autoload
  9376. (defun org-open-at-point-global ()
  9377. "Follow a link or time-stamp like Org mode does.
  9378. This command can be called in any mode to follow an external link
  9379. or a time-stamp that has Org mode syntax. Its behavior is
  9380. undefined when called on internal links (e.g., fuzzy links).
  9381. Raise an error when there is nothing to follow. "
  9382. (interactive)
  9383. (cond ((org-in-regexp org-any-link-re)
  9384. (org-open-link-from-string (match-string-no-properties 0)))
  9385. ((or (org-in-regexp org-ts-regexp-both nil t)
  9386. (org-in-regexp org-tsr-regexp-both nil t))
  9387. (org-follow-timestamp-link))
  9388. (t (user-error "No link found"))))
  9389. ;;;###autoload
  9390. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9391. "Open a link in the string S, as if it was in Org mode."
  9392. (interactive "sLink: \nP")
  9393. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9394. (with-temp-buffer
  9395. (let ((org-inhibit-startup (not reference-buffer)))
  9396. (org-mode)
  9397. (insert s)
  9398. (goto-char (point-min))
  9399. (when reference-buffer
  9400. (setq org-link-abbrev-alist-local
  9401. (with-current-buffer reference-buffer
  9402. org-link-abbrev-alist-local)))
  9403. (org-open-at-point arg reference-buffer)))))
  9404. (defvar org-open-at-point-functions nil
  9405. "Hook that is run when following a link at point.
  9406. Functions in this hook must return t if they identify and follow
  9407. a link at point. If they don't find anything interesting at point,
  9408. they must return nil.")
  9409. (defvar org-link-search-inhibit-query nil)
  9410. (defvar clean-buffer-list-kill-buffer-names) ;Defined in midnight.el
  9411. (defun org--open-doi-link (path)
  9412. "Open a \"doi\" type link.
  9413. PATH is a the path to search for, as a string."
  9414. (browse-url (url-encode-url (concat org-doi-server-url path))))
  9415. (defun org--open-elisp-link (path)
  9416. "Open a \"elisp\" type link.
  9417. PATH is the sexp to evaluate, as a string."
  9418. (let ((cmd path))
  9419. (if (or (and (org-string-nw-p
  9420. org-confirm-elisp-link-not-regexp)
  9421. (string-match-p org-confirm-elisp-link-not-regexp cmd))
  9422. (not org-confirm-elisp-link-function)
  9423. (funcall org-confirm-elisp-link-function
  9424. (format "Execute \"%s\" as elisp? "
  9425. (org-add-props cmd nil 'face 'org-warning))))
  9426. (message "%s => %s" cmd
  9427. (if (eq (string-to-char cmd) ?\()
  9428. (eval (read cmd))
  9429. (call-interactively (read cmd))))
  9430. (user-error "Abort"))))
  9431. (defun org--open-help-link (path)
  9432. "Open a \"help\" type link.
  9433. PATH is a symbol name, as a string."
  9434. (pcase (intern path)
  9435. ((and (pred fboundp) variable) (describe-function variable))
  9436. ((and (pred boundp) function) (describe-variable function))
  9437. (name (user-error "Unknown function or variable: %s" name))))
  9438. (defun org--open-shell-link (path)
  9439. "Open a \"shell\" type link.
  9440. PATH is the command to execute, as a string."
  9441. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9442. (cmd path))
  9443. (if (or (and (org-string-nw-p
  9444. org-confirm-shell-link-not-regexp)
  9445. (string-match
  9446. org-confirm-shell-link-not-regexp cmd))
  9447. (not org-confirm-shell-link-function)
  9448. (funcall org-confirm-shell-link-function
  9449. (format "Execute \"%s\" in shell? "
  9450. (org-add-props cmd nil
  9451. 'face 'org-warning))))
  9452. (progn
  9453. (message "Executing %s" cmd)
  9454. (shell-command cmd buf)
  9455. (when (featurep 'midnight)
  9456. (setq clean-buffer-list-kill-buffer-names
  9457. (cons (buffer-name buf)
  9458. clean-buffer-list-kill-buffer-names))))
  9459. (user-error "Abort"))))
  9460. (defun org-open-at-point (&optional arg reference-buffer)
  9461. "Open link, timestamp, footnote or tags at point.
  9462. When point is on a link, follow it. Normally, files will be
  9463. opened by an appropriate application. If the optional prefix
  9464. argument ARG is non-nil, Emacs will visit the file. With
  9465. a double prefix argument, try to open outside of Emacs, in the
  9466. application the system uses for this file type.
  9467. When point is on a timestamp, open the agenda at the day
  9468. specified.
  9469. When point is a footnote definition, move to the first reference
  9470. found. If it is on a reference, move to the associated
  9471. definition.
  9472. When point is on a headline, display a list of every link in the
  9473. entry, so it is possible to pick one, or all, of them. If point
  9474. is on a tag, call `org-tags-view' instead.
  9475. When optional argument REFERENCE-BUFFER is non-nil, it should
  9476. specify a buffer from where the link search should happen. This
  9477. is used internally by `org-open-link-from-string'.
  9478. On top of syntactically correct links, this function will also
  9479. try to open links and time-stamps in comments, example
  9480. blocks... i.e., whenever point is on something looking like
  9481. a timestamp or a link."
  9482. (interactive "P")
  9483. ;; On a code block, open block's results.
  9484. (unless (call-interactively 'org-babel-open-src-block-result)
  9485. (org-load-modules-maybe)
  9486. (setq org-window-config-before-follow-link (current-window-configuration))
  9487. (org-remove-occur-highlights nil nil t)
  9488. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9489. (let* ((context
  9490. ;; Only consider supported types, even if they are not
  9491. ;; the closest one.
  9492. (org-element-lineage
  9493. (org-element-context)
  9494. '(clock footnote-definition footnote-reference headline
  9495. inlinetask link timestamp)
  9496. t))
  9497. (type (org-element-type context))
  9498. (value (org-element-property :value context)))
  9499. (cond
  9500. ;; On a headline or an inlinetask, but not on a timestamp,
  9501. ;; a link, a footnote reference.
  9502. ((memq type '(headline inlinetask))
  9503. (org-match-line org-complex-heading-regexp)
  9504. (if (and (match-beginning 5)
  9505. (>= (point) (match-beginning 5))
  9506. (< (point) (match-end 5)))
  9507. ;; On tags.
  9508. (org-tags-view arg (substring (match-string 5) 0 -1))
  9509. ;; Not on tags.
  9510. (pcase (org-offer-links-in-entry (current-buffer) (point) arg)
  9511. (`(nil . ,_)
  9512. (require 'org-attach)
  9513. (org-attach-reveal 'if-exists))
  9514. (`(,links . ,links-end)
  9515. (dolist (link (if (stringp links) (list links) links))
  9516. (search-forward link nil links-end)
  9517. (goto-char (match-beginning 0))
  9518. (org-open-at-point))))))
  9519. ;; On a footnote reference or at definition's label.
  9520. ((or (eq type 'footnote-reference)
  9521. (and (eq type 'footnote-definition)
  9522. (save-excursion
  9523. ;; Do not validate action when point is on the
  9524. ;; spaces right after the footnote label, in
  9525. ;; order to be on par with behaviour on links.
  9526. (skip-chars-forward " \t")
  9527. (let ((begin
  9528. (org-element-property :contents-begin context)))
  9529. (if begin (< (point) begin)
  9530. (= (org-element-property :post-affiliated context)
  9531. (line-beginning-position)))))))
  9532. (org-footnote-action))
  9533. ;; No valid context. Ignore catch-all types like `headline'.
  9534. ;; If point is on something looking like a link or
  9535. ;; a time-stamp, try opening it. It may be useful in
  9536. ;; comments, example blocks...
  9537. ((memq type '(footnote-definition headline inlinetask nil))
  9538. (call-interactively #'org-open-at-point-global))
  9539. ;; On a clock line, make sure point is on the timestamp
  9540. ;; before opening it.
  9541. ((and (eq type 'clock)
  9542. value
  9543. (>= (point) (org-element-property :begin value))
  9544. (<= (point) (org-element-property :end value)))
  9545. (org-follow-timestamp-link))
  9546. ;; Do nothing on white spaces after an object.
  9547. ((>= (point)
  9548. (save-excursion
  9549. (goto-char (org-element-property :end context))
  9550. (skip-chars-backward " \t")
  9551. (point)))
  9552. (user-error "No link found"))
  9553. ((eq type 'timestamp) (org-follow-timestamp-link))
  9554. ((eq type 'link)
  9555. (let ((type (org-element-property :type context))
  9556. (path (org-link-unescape (org-element-property :path context))))
  9557. ;; Switch back to REFERENCE-BUFFER needed when called in
  9558. ;; a temporary buffer through `org-open-link-from-string'.
  9559. (with-current-buffer (or reference-buffer (current-buffer))
  9560. (cond
  9561. ((equal type "file")
  9562. (if (string-match "[*?{]" (file-name-nondirectory path))
  9563. (dired path)
  9564. ;; Look into `org-link-parameters' in order to find
  9565. ;; a DEDICATED-FUNCTION to open file. The function
  9566. ;; will be applied on raw link instead of parsed
  9567. ;; link due to the limitation in `org-add-link-type'
  9568. ;; ("open" function called with a single argument).
  9569. ;; If no such function is found, fallback to
  9570. ;; `org-open-file'.
  9571. (let* ((option (org-element-property :search-option context))
  9572. (app (org-element-property :application context))
  9573. (dedicated-function
  9574. (org-link-get-parameter
  9575. (if app (concat type "+" app) type)
  9576. :follow)))
  9577. (if dedicated-function
  9578. (funcall dedicated-function
  9579. (concat path
  9580. (and option (concat "::" option))))
  9581. (apply #'org-open-file
  9582. path
  9583. (cond (arg)
  9584. ((equal app "emacs") 'emacs)
  9585. ((equal app "sys") 'system))
  9586. (cond ((not option) nil)
  9587. ((string-match-p "\\`[0-9]+\\'" option)
  9588. (list (string-to-number option)))
  9589. (t (list nil
  9590. (org-link-unescape option)))))))))
  9591. ((functionp (org-link-get-parameter type :follow))
  9592. (funcall (org-link-get-parameter type :follow) path))
  9593. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9594. (unless (run-hook-with-args-until-success
  9595. 'org-open-link-functions path)
  9596. (if (not arg) (org-mark-ring-push)
  9597. (switch-to-buffer-other-window
  9598. (org-get-buffer-for-internal-link (current-buffer))))
  9599. (let ((destination
  9600. (org-with-wide-buffer
  9601. (if (equal type "radio")
  9602. (org-search-radio-target
  9603. (org-element-property :path context))
  9604. (org-link-search
  9605. (if (member type '("custom-id" "coderef"))
  9606. (org-element-property :raw-link context)
  9607. path)
  9608. ;; Prevent fuzzy links from matching
  9609. ;; themselves.
  9610. (and (equal type "fuzzy")
  9611. (+ 2 (org-element-property :begin context)))))
  9612. (point))))
  9613. (unless (and (<= (point-min) destination)
  9614. (>= (point-max) destination))
  9615. (widen))
  9616. (goto-char destination))))
  9617. (t (browse-url-at-point))))))
  9618. (t (user-error "No link found")))))
  9619. (run-hook-with-args 'org-follow-link-hook)))
  9620. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9621. "Offer links in the current entry and return the selected link.
  9622. If there is only one link, return it.
  9623. If NTH is an integer, return the NTH link found.
  9624. If ZERO is a string, check also this string for a link, and if
  9625. there is one, return it."
  9626. (with-current-buffer buffer
  9627. (org-with-wide-buffer
  9628. (goto-char marker)
  9629. (let ((cnt ?0)
  9630. have-zero end links link c)
  9631. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9632. (push (match-string 0 zero) links)
  9633. (setq cnt (1- cnt) have-zero t))
  9634. (save-excursion
  9635. (org-back-to-heading t)
  9636. (setq end (save-excursion (outline-next-heading) (point)))
  9637. (while (re-search-forward org-any-link-re end t)
  9638. (push (match-string 0) links))
  9639. (setq links (org-uniquify (reverse links))))
  9640. (cond
  9641. ((null links)
  9642. (message "No links"))
  9643. ((equal (length links) 1)
  9644. (setq link (car links)))
  9645. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9646. (setq link (nth (if have-zero nth (1- nth)) links)))
  9647. (t ; we have to select a link
  9648. (save-excursion
  9649. (save-window-excursion
  9650. (delete-other-windows)
  9651. (with-output-to-temp-buffer "*Select Link*"
  9652. (dolist (l links)
  9653. (cond
  9654. ((not (string-match org-bracket-link-regexp l))
  9655. (princ (format "[%c] %s\n" (cl-incf cnt)
  9656. (org-unbracket-string "<" ">" l))))
  9657. ((match-end 3)
  9658. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9659. (match-string 3 l) (match-string 1 l))))
  9660. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9661. (match-string 1 l)))))))
  9662. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9663. (message "Select link to open, RET to open all:")
  9664. (setq c (read-char-exclusive))
  9665. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9666. (when (equal c ?q) (user-error "Abort"))
  9667. (if (equal c ?\C-m)
  9668. (setq link links)
  9669. (setq nth (- c ?0))
  9670. (when have-zero (setq nth (1+ nth)))
  9671. (unless (and (integerp nth) (>= (length links) nth))
  9672. (user-error "Invalid link selection"))
  9673. (setq link (nth (1- nth) links)))))
  9674. (cons link end)))))
  9675. ;; TODO: These functions are deprecated since `org-open-at-point'
  9676. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9677. (defun org-open-file-with-system (path)
  9678. "Open file at PATH using the system way of opening it."
  9679. (org-open-file path 'system))
  9680. (defun org-open-file-with-emacs (path)
  9681. "Open file at PATH in Emacs."
  9682. (org-open-file path 'emacs))
  9683. ;;; File search
  9684. (defvar org-create-file-search-functions nil
  9685. "List of functions to construct the right search string for a file link.
  9686. These functions are called in turn with point at the location to
  9687. which the link should point.
  9688. A function in the hook should first test if it would like to
  9689. handle this file type, for example by checking the `major-mode'
  9690. or the file extension. If it decides not to handle this file, it
  9691. should just return nil to give other functions a chance. If it
  9692. does handle the file, it must return the search string to be used
  9693. when following the link. The search string will be part of the
  9694. file link, given after a double colon, and `org-open-at-point'
  9695. will automatically search for it. If special measures must be
  9696. taken to make the search successful, another function should be
  9697. added to the companion hook `org-execute-file-search-functions',
  9698. which see.
  9699. A function in this hook may also use `setq' to set the variable
  9700. `description' to provide a suggestion for the descriptive text to
  9701. be used for this link when it gets inserted into an Org buffer
  9702. with \\[org-insert-link].")
  9703. (defvar org-execute-file-search-functions nil
  9704. "List of functions to execute a file search triggered by a link.
  9705. Functions added to this hook must accept a single argument, the
  9706. search string that was part of the file link, the part after the
  9707. double colon. The function must first check if it would like to
  9708. handle this search, for example by checking the `major-mode' or
  9709. the file extension. If it decides not to handle this search, it
  9710. should just return nil to give other functions a chance. If it
  9711. does handle the search, it must return a non-nil value to keep
  9712. other functions from trying.
  9713. Each function can access the current prefix argument through the
  9714. variable `current-prefix-arg'. Note that a single prefix is used
  9715. to force opening a link in Emacs, so it may be good to only use a
  9716. numeric or double prefix to guide the search function.
  9717. In case this is needed, a function in this hook can also restore
  9718. the window configuration before `org-open-at-point' was called using:
  9719. (set-window-configuration org-window-config-before-follow-link)")
  9720. (defun org-search-radio-target (target)
  9721. "Search a radio target matching TARGET in current buffer.
  9722. White spaces are not significant."
  9723. (let ((re (format "<<<%s>>>"
  9724. (mapconcat #'regexp-quote
  9725. (org-split-string target "[ \t\n]+")
  9726. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9727. (origin (point)))
  9728. (goto-char (point-min))
  9729. (catch :radio-match
  9730. (while (re-search-forward re nil t)
  9731. (backward-char)
  9732. (let ((object (org-element-context)))
  9733. (when (eq (org-element-type object) 'radio-target)
  9734. (goto-char (org-element-property :begin object))
  9735. (org-show-context 'link-search)
  9736. (throw :radio-match nil))))
  9737. (goto-char origin)
  9738. (user-error "No match for radio target: %s" target))))
  9739. (defun org-link-search (s &optional avoid-pos stealth)
  9740. "Search for a search string S.
  9741. If S starts with \"#\", it triggers a custom ID search.
  9742. If S is enclosed within parenthesis, it initiates a coderef
  9743. search.
  9744. If S is surrounded by forward slashes, it is interpreted as
  9745. a regular expression. In Org mode files, this will create an
  9746. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9747. to list matches. If the current buffer is in `dired-mode', grep
  9748. will be used to search in all files.
  9749. When AVOID-POS is given, ignore matches near that position.
  9750. When optional argument STEALTH is non-nil, do not modify
  9751. visibility around point, thus ignoring `org-show-context-detail'
  9752. variable.
  9753. Search is case-insensitive and ignores white spaces. Return type
  9754. of matched result, which is either `dedicated' or `fuzzy'."
  9755. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9756. (let* ((case-fold-search t)
  9757. (origin (point))
  9758. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9759. (starred (eq (string-to-char normalized) ?*))
  9760. (words (split-string (if starred (substring s 1) s)))
  9761. (s-multi-re (mapconcat #'regexp-quote words "\\(?:[ \t\n]+\\)"))
  9762. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9763. type)
  9764. (cond
  9765. ;; Check if there are any special search functions.
  9766. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9767. ((eq (string-to-char s) ?#)
  9768. ;; Look for a custom ID S if S starts with "#".
  9769. (let* ((id (substring normalized 1))
  9770. (match (org-find-property "CUSTOM_ID" id)))
  9771. (if match (progn (goto-char match) (setf type 'dedicated))
  9772. (error "No match for custom ID: %s" id))))
  9773. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9774. ;; Look for coderef targets if S is enclosed within parenthesis.
  9775. (let ((coderef (match-string-no-properties 1 normalized))
  9776. (re (substring s-single-re 1 -1)))
  9777. (goto-char (point-min))
  9778. (catch :coderef-match
  9779. (while (re-search-forward re nil t)
  9780. (let ((element (org-element-at-point)))
  9781. (when (and (memq (org-element-type element)
  9782. '(example-block src-block))
  9783. ;; Build proper regexp according to current
  9784. ;; block's label format.
  9785. (let ((label-fmt
  9786. (regexp-quote
  9787. (or (org-element-property :label-fmt element)
  9788. org-coderef-label-format))))
  9789. (save-excursion
  9790. (beginning-of-line)
  9791. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9792. (format label-fmt coderef))))))
  9793. (setq type 'dedicated)
  9794. (goto-char (match-beginning 1))
  9795. (throw :coderef-match nil))))
  9796. (goto-char origin)
  9797. (error "No match for coderef: %s" coderef))))
  9798. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9799. ;; Look for a regular expression.
  9800. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9801. (match-string 1 s)))
  9802. ;; From here, we handle fuzzy links.
  9803. ;;
  9804. ;; Look for targets, only if not in a headline search.
  9805. ((and (not starred)
  9806. (let ((target (format "<<%s>>" s-multi-re)))
  9807. (catch :target-match
  9808. (goto-char (point-min))
  9809. (while (re-search-forward target nil t)
  9810. (backward-char)
  9811. (let ((context (org-element-context)))
  9812. (when (eq (org-element-type context) 'target)
  9813. (setq type 'dedicated)
  9814. (goto-char (org-element-property :begin context))
  9815. (throw :target-match t))))
  9816. nil))))
  9817. ;; Look for elements named after S, only if not in a headline
  9818. ;; search.
  9819. ((and (not starred)
  9820. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9821. (catch :name-match
  9822. (goto-char (point-min))
  9823. (while (re-search-forward name nil t)
  9824. (let ((element (org-element-at-point)))
  9825. (when (equal words
  9826. (split-string
  9827. (org-element-property :name element)))
  9828. (setq type 'dedicated)
  9829. (beginning-of-line)
  9830. (throw :name-match t))))
  9831. nil))))
  9832. ;; Regular text search. Prefer headlines in Org mode buffers.
  9833. ;; Ignore COMMENT keyword, TODO keywords, priority cookies,
  9834. ;; statistics cookies and tags.
  9835. ((and (derived-mode-p 'org-mode)
  9836. (let ((title-re
  9837. (format "%s.*\\(?:%s[ \t]\\)?.*%s"
  9838. org-outline-regexp-bol
  9839. org-comment-string
  9840. (mapconcat #'regexp-quote words ".+")))
  9841. (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
  9842. (comment-re (eval-when-compile
  9843. (format "\\`%s[ \t]+" org-comment-string))))
  9844. (goto-char (point-min))
  9845. (catch :found
  9846. (while (re-search-forward title-re nil t)
  9847. (when (equal words
  9848. (split-string
  9849. (replace-regexp-in-string
  9850. cookie-re ""
  9851. (replace-regexp-in-string
  9852. comment-re "" (org-get-heading t t t)))))
  9853. (throw :found t)))
  9854. nil)))
  9855. (beginning-of-line)
  9856. (setq type 'dedicated))
  9857. ;; Offer to create non-existent headline depending on
  9858. ;; `org-link-search-must-match-exact-headline'.
  9859. ((and (derived-mode-p 'org-mode)
  9860. (not org-link-search-inhibit-query)
  9861. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9862. (yes-or-no-p "No match - create this as a new heading? "))
  9863. (goto-char (point-max))
  9864. (unless (bolp) (newline))
  9865. (org-insert-heading nil t t)
  9866. (insert s "\n")
  9867. (beginning-of-line 0))
  9868. ;; Only headlines are looked after. No need to process
  9869. ;; further: throw an error.
  9870. ((and (derived-mode-p 'org-mode)
  9871. (or starred org-link-search-must-match-exact-headline))
  9872. (goto-char origin)
  9873. (error "No match for fuzzy expression: %s" normalized))
  9874. ;; Regular text search.
  9875. ((catch :fuzzy-match
  9876. (goto-char (point-min))
  9877. (while (re-search-forward s-multi-re nil t)
  9878. ;; Skip match if it contains AVOID-POS or it is included in
  9879. ;; a link with a description but outside the description.
  9880. (unless (or (and avoid-pos
  9881. (<= (match-beginning 0) avoid-pos)
  9882. (> (match-end 0) avoid-pos))
  9883. (and (save-match-data
  9884. (org-in-regexp org-bracket-link-regexp))
  9885. (match-beginning 3)
  9886. (or (> (match-beginning 3) (point))
  9887. (<= (match-end 3) (point)))
  9888. (org-element-lineage
  9889. (save-match-data (org-element-context))
  9890. '(link) t)))
  9891. (goto-char (match-beginning 0))
  9892. (setq type 'fuzzy)
  9893. (throw :fuzzy-match t)))
  9894. nil))
  9895. ;; All failed. Throw an error.
  9896. (t (goto-char origin)
  9897. (error "No match for fuzzy expression: %s" normalized)))
  9898. ;; Disclose surroundings of match, if appropriate.
  9899. (when (and (derived-mode-p 'org-mode) (not stealth))
  9900. (org-show-context 'link-search))
  9901. type))
  9902. (defun org-get-buffer-for-internal-link (buffer)
  9903. "Return a buffer to be used for displaying the link target of internal links."
  9904. (cond
  9905. ((not org-display-internal-link-with-indirect-buffer)
  9906. buffer)
  9907. ((string-suffix-p "(Clone)" (buffer-name buffer))
  9908. (message "Buffer is already a clone, not making another one")
  9909. ;; we also do not modify visibility in this case
  9910. buffer)
  9911. (t ; make a new indirect buffer for displaying the link
  9912. (let* ((bn (buffer-name buffer))
  9913. (ibn (concat bn "(Clone)"))
  9914. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9915. (with-current-buffer ib (org-overview))
  9916. ib))))
  9917. (defun org-do-occur (regexp &optional cleanup)
  9918. "Call the Emacs command `occur'.
  9919. If CLEANUP is non-nil, remove the printout of the regular expression
  9920. in the *Occur* buffer. This is useful if the regex is long and not useful
  9921. to read."
  9922. (occur regexp)
  9923. (when cleanup
  9924. (let ((cwin (selected-window)) win beg end)
  9925. (when (setq win (get-buffer-window "*Occur*"))
  9926. (select-window win))
  9927. (goto-char (point-min))
  9928. (when (re-search-forward "match[a-z]+" nil t)
  9929. (setq beg (match-end 0))
  9930. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  9931. (setq end (1- (match-beginning 0)))))
  9932. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9933. (goto-char (point-min))
  9934. (select-window cwin))))
  9935. ;;; The mark ring for links jumps
  9936. (defvar org-mark-ring nil
  9937. "Mark ring for positions before jumps in Org mode.")
  9938. (defvar org-mark-ring-last-goto nil
  9939. "Last position in the mark ring used to go back.")
  9940. ;; Fill and close the ring
  9941. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9942. (dotimes (_ org-mark-ring-length)
  9943. (push (make-marker) org-mark-ring))
  9944. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9945. org-mark-ring)
  9946. (defun org-mark-ring-push (&optional pos buffer)
  9947. "Put the current position or POS into the mark ring and rotate it."
  9948. (interactive)
  9949. (setq pos (or pos (point)))
  9950. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9951. (move-marker (car org-mark-ring)
  9952. (or pos (point))
  9953. (or buffer (current-buffer)))
  9954. (message "%s"
  9955. (substitute-command-keys
  9956. "Position saved to mark ring, go back with \
  9957. `\\[org-mark-ring-goto]'.")))
  9958. (defun org-mark-ring-goto (&optional n)
  9959. "Jump to the previous position in the mark ring.
  9960. With prefix arg N, jump back that many stored positions. When
  9961. called several times in succession, walk through the entire ring.
  9962. Org mode commands jumping to a different position in the current file,
  9963. or to another Org file, automatically push the old position onto the ring."
  9964. (interactive "p")
  9965. (let (p m)
  9966. (if (eq last-command this-command)
  9967. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9968. (setq p org-mark-ring))
  9969. (setq org-mark-ring-last-goto p)
  9970. (setq m (car p))
  9971. (pop-to-buffer-same-window (marker-buffer m))
  9972. (goto-char m)
  9973. (when (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9974. (defun org-add-angle-brackets (s)
  9975. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  9976. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  9977. s)
  9978. ;;; Following specific links
  9979. (defvar org-agenda-buffer-tmp-name)
  9980. (defvar org-agenda-start-on-weekday)
  9981. (defun org-follow-timestamp-link ()
  9982. "Open an agenda view for the time-stamp date/range at point."
  9983. (cond
  9984. ((org-at-date-range-p t)
  9985. (let ((org-agenda-start-on-weekday)
  9986. (t1 (match-string 1))
  9987. (t2 (match-string 2)) tt1 tt2)
  9988. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9989. tt2 (time-to-days (org-time-string-to-time t2)))
  9990. (let ((org-agenda-buffer-tmp-name
  9991. (format "*Org Agenda(a:%s)"
  9992. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9993. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9994. ((org-at-timestamp-p 'lax)
  9995. (let ((org-agenda-buffer-tmp-name
  9996. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9997. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9998. (substring (match-string 1) 0 10)))
  9999. 1)))
  10000. (t (error "This should not happen"))))
  10001. ;;; Following file links
  10002. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10003. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10004. (declare-function mailcap-mime-info
  10005. "mailcap" (string &optional request no-decode))
  10006. (defvar org-wait nil)
  10007. (defun org-open-file (path &optional in-emacs line search)
  10008. "Open the file at PATH.
  10009. First, this expands any special file name abbreviations. Then the
  10010. configuration variable `org-file-apps' is checked if it contains an
  10011. entry for this file type, and if yes, the corresponding command is launched.
  10012. If no application is found, Emacs simply visits the file.
  10013. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10014. With a double \\[universal-argument] \\[universal-argument] \
  10015. prefix arg, Org tries to avoid opening in Emacs
  10016. and to use an external application to visit the file.
  10017. Optional LINE specifies a line to go to, optional SEARCH a string
  10018. to search for. If LINE or SEARCH is given, the file will be
  10019. opened in Emacs, unless an entry from org-file-apps that makes
  10020. use of groups in a regexp matches.
  10021. If you want to change the way frames are used when following a
  10022. link, please customize `org-link-frame-setup'.
  10023. If the file does not exist, an error is thrown."
  10024. (let* ((file (if (equal path "")
  10025. buffer-file-name
  10026. (substitute-in-file-name (expand-file-name path))))
  10027. (file-apps (append org-file-apps (org-default-apps)))
  10028. (apps (cl-remove-if
  10029. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10030. (apps-dlink (cl-remove-if-not
  10031. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10032. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10033. (dirp (unless remp (file-directory-p file)))
  10034. (file (if (and dirp org-open-directory-means-index-dot-org)
  10035. (concat (file-name-as-directory file) "index.org")
  10036. file))
  10037. (a-m-a-p (assq 'auto-mode apps))
  10038. (dfile (downcase file))
  10039. ;; Reconstruct the original link from the PATH, LINE and
  10040. ;; SEARCH args.
  10041. (link (cond (line (concat file "::" (number-to-string line)))
  10042. (search (concat file "::" search))
  10043. (t file)))
  10044. (dlink (downcase link))
  10045. (ext
  10046. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10047. (match-string 1 dfile)))
  10048. (save-position-maybe
  10049. (let ((old-buffer (current-buffer))
  10050. (old-pos (point))
  10051. (old-mode major-mode))
  10052. (lambda ()
  10053. (and (derived-mode-p 'org-mode)
  10054. (eq old-mode 'org-mode)
  10055. (or (not (eq old-buffer (current-buffer)))
  10056. (not (eq old-pos (point))))
  10057. (org-mark-ring-push old-pos old-buffer)))))
  10058. cmd link-match-data)
  10059. (cond
  10060. ((member in-emacs '((16) system))
  10061. (setq cmd (cdr (assq 'system apps))))
  10062. (in-emacs (setq cmd 'emacs))
  10063. (t
  10064. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10065. (and dirp (cdr (assq 'directory apps)))
  10066. ;; First, try matching against apps-dlink if we
  10067. ;; get a match here, store the match data for
  10068. ;; later.
  10069. (let ((match (assoc-default dlink apps-dlink
  10070. 'string-match)))
  10071. (if match
  10072. (progn (setq link-match-data (match-data))
  10073. match)
  10074. (progn (setq in-emacs (or in-emacs line search))
  10075. nil))) ; if we have no match in apps-dlink,
  10076. ; always open the file in emacs if line or search
  10077. ; is given (for backwards compatibility)
  10078. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10079. 'string-match)
  10080. (cdr (assoc ext apps))
  10081. (cdr (assq t apps))))))
  10082. (when (eq cmd 'system)
  10083. (setq cmd (cdr (assq 'system apps))))
  10084. (when (eq cmd 'default)
  10085. (setq cmd (cdr (assoc t apps))))
  10086. (when (eq cmd 'mailcap)
  10087. (require 'mailcap)
  10088. (mailcap-parse-mailcaps)
  10089. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10090. (command (mailcap-mime-info mime-type)))
  10091. (if (stringp command)
  10092. (setq cmd command)
  10093. (setq cmd 'emacs))))
  10094. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10095. (not (file-exists-p file))
  10096. (not org-open-non-existing-files))
  10097. (user-error "No such file: %s" file))
  10098. (cond
  10099. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10100. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10101. (while (string-match "['\"]%s['\"]" cmd)
  10102. (setq cmd (replace-match "%s" t t cmd)))
  10103. (setq cmd (replace-regexp-in-string
  10104. "%s"
  10105. (shell-quote-argument (convert-standard-filename file))
  10106. cmd
  10107. nil t))
  10108. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10109. (save-match-data
  10110. (let ((match-index 1)
  10111. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10112. (set-match-data link-match-data)
  10113. (while (<= match-index number-of-groups)
  10114. (let ((regex (concat "%" (number-to-string match-index)))
  10115. (replace-with (match-string match-index dlink)))
  10116. (while (string-match regex cmd)
  10117. (setq cmd (replace-match replace-with t t cmd))))
  10118. (setq match-index (+ match-index 1)))))
  10119. (save-window-excursion
  10120. (message "Running %s...done" cmd)
  10121. (start-process-shell-command cmd nil cmd)
  10122. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10123. ((or (stringp cmd)
  10124. (eq cmd 'emacs))
  10125. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10126. (widen)
  10127. (cond (line (org-goto-line line)
  10128. (when (derived-mode-p 'org-mode) (org-reveal)))
  10129. (search (condition-case err
  10130. (org-link-search search)
  10131. ;; Save position before error-ing out so user
  10132. ;; can easily move back to the original buffer.
  10133. (error (funcall save-position-maybe)
  10134. (error (nth 1 err)))))))
  10135. ((functionp cmd)
  10136. (save-match-data
  10137. (set-match-data link-match-data)
  10138. (condition-case nil
  10139. (funcall cmd file link)
  10140. ;; FIXME: Remove this check when most default installations
  10141. ;; of Emacs have at least Org 9.0.
  10142. ((debug wrong-number-of-arguments wrong-type-argument
  10143. invalid-function)
  10144. (user-error "Please see Org News for version 9.0 about \
  10145. `org-file-apps'--Lisp error: %S" cmd)))))
  10146. ((consp cmd)
  10147. ;; FIXME: Remove this check when most default installations of
  10148. ;; Emacs have at least Org 9.0. Heads-up instead of silently
  10149. ;; fall back to `org-link-frame-setup' for an old usage of
  10150. ;; `org-file-apps' with sexp instead of a function for `cmd'.
  10151. (user-error "Please see Org News for version 9.0 about \
  10152. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10153. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10154. (funcall save-position-maybe)))
  10155. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10156. "This function returns non-nil if `entry' uses a regular
  10157. expression which should be matched against the whole link by
  10158. org-open-file.
  10159. It assumes that is the case when the entry uses a regular
  10160. expression which has at least one grouping construct and the
  10161. action is either a lisp form or a command string containing
  10162. `%1', i.e. using at least one subexpression match as a
  10163. parameter."
  10164. (let ((selector (car entry))
  10165. (action (cdr entry)))
  10166. (if (stringp selector)
  10167. (and (> (regexp-opt-depth selector) 0)
  10168. (or (and (stringp action)
  10169. (string-match "%[0-9]" action))
  10170. (consp action)))
  10171. nil)))
  10172. (defun org-default-apps ()
  10173. "Return the default applications for this operating system."
  10174. (cond
  10175. ((eq system-type 'darwin)
  10176. org-file-apps-defaults-macosx)
  10177. ((eq system-type 'windows-nt)
  10178. org-file-apps-defaults-windowsnt)
  10179. (t org-file-apps-defaults-gnu)))
  10180. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10181. "Convert extensions to regular expressions in the cars of LIST.
  10182. Also, weed out any non-string entries, because the return value is used
  10183. only for regexp matching.
  10184. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10185. point to the symbol `emacs', indicating that the file should
  10186. be opened in Emacs."
  10187. (append
  10188. (delq nil
  10189. (mapcar (lambda (x)
  10190. (unless (not (stringp (car x)))
  10191. (if (string-match "\\W" (car x))
  10192. x
  10193. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10194. list))
  10195. (when add-auto-mode
  10196. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10197. (defvar ange-ftp-name-format)
  10198. (defun org-file-remote-p (file)
  10199. "Test whether FILE specifies a location on a remote system.
  10200. Return non-nil if the location is indeed remote.
  10201. For example, the filename \"/user@host:/foo\" specifies a location
  10202. on the system \"/user@host:\"."
  10203. (cond ((fboundp 'file-remote-p)
  10204. (file-remote-p file))
  10205. ((fboundp 'tramp-handle-file-remote-p)
  10206. (tramp-handle-file-remote-p file))
  10207. ((and (boundp 'ange-ftp-name-format)
  10208. (string-match (car ange-ftp-name-format) file))
  10209. t)))
  10210. ;;;; Refiling
  10211. (defun org-get-org-file ()
  10212. "Read a filename, with default directory `org-directory'."
  10213. (let ((default (or org-default-notes-file remember-data-file)))
  10214. (read-file-name (format "File name [%s]: " default)
  10215. (file-name-as-directory org-directory)
  10216. default)))
  10217. (defun org-notes-order-reversed-p ()
  10218. "Check if the current file should receive notes in reversed order."
  10219. (cond
  10220. ((not org-reverse-note-order) nil)
  10221. ((eq t org-reverse-note-order) t)
  10222. ((not (listp org-reverse-note-order)) nil)
  10223. (t (catch 'exit
  10224. (dolist (entry org-reverse-note-order)
  10225. (when (string-match (car entry) buffer-file-name)
  10226. (throw 'exit (cdr entry))))))))
  10227. (defvar org-refile-target-table nil
  10228. "The list of refile targets, created by `org-refile'.")
  10229. (defvar org-agenda-new-buffers nil
  10230. "Buffers created to visit agenda files.")
  10231. (defvar org-refile-cache nil
  10232. "Cache for refile targets.")
  10233. (defvar org-refile-markers nil
  10234. "All the markers used for caching refile locations.")
  10235. (defun org-refile-marker (pos)
  10236. "Get a new refile marker, but only if caching is in use."
  10237. (if (not org-refile-use-cache)
  10238. pos
  10239. (let ((m (make-marker)))
  10240. (move-marker m pos)
  10241. (push m org-refile-markers)
  10242. m)))
  10243. (defun org-refile-cache-clear ()
  10244. "Clear the refile cache and disable all the markers."
  10245. (dolist (m org-refile-markers) (move-marker m nil))
  10246. (setq org-refile-markers nil)
  10247. (setq org-refile-cache nil)
  10248. (message "Refile cache has been cleared"))
  10249. (defun org-refile-cache-check-set (set)
  10250. "Check if all the markers in the cache still have live buffers."
  10251. (let (marker)
  10252. (catch 'exit
  10253. (while (and set (setq marker (nth 3 (pop set))))
  10254. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10255. (when (and marker (null (marker-buffer marker)))
  10256. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10257. (sit-for 3)
  10258. (throw 'exit nil)))
  10259. t)))
  10260. (defun org-refile-cache-put (set &rest identifiers)
  10261. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10262. (let* ((key (sha1 (prin1-to-string identifiers)))
  10263. (entry (assoc key org-refile-cache)))
  10264. (if entry
  10265. (setcdr entry set)
  10266. (push (cons key set) org-refile-cache))))
  10267. (defun org-refile-cache-get (&rest identifiers)
  10268. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10269. (cond
  10270. ((not org-refile-cache) nil)
  10271. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10272. (t
  10273. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10274. org-refile-cache))))
  10275. (and set (org-refile-cache-check-set set) set)))))
  10276. (defvar org-outline-path-cache nil
  10277. "Alist between buffer positions and outline paths.
  10278. It value is an alist (POSITION . PATH) where POSITION is the
  10279. buffer position at the beginning of an entry and PATH is a list
  10280. of strings describing the outline path for that entry, in reverse
  10281. order.")
  10282. (defun org-refile-get-targets (&optional default-buffer)
  10283. "Produce a table with refile targets."
  10284. (let ((case-fold-search nil)
  10285. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10286. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10287. targets tgs files desc descre)
  10288. (message "Getting targets...")
  10289. (with-current-buffer (or default-buffer (current-buffer))
  10290. (dolist (entry entries)
  10291. (setq files (car entry) desc (cdr entry))
  10292. (cond
  10293. ((null files) (setq files (list (current-buffer))))
  10294. ((eq files 'org-agenda-files)
  10295. (setq files (org-agenda-files 'unrestricted)))
  10296. ((and (symbolp files) (fboundp files))
  10297. (setq files (funcall files)))
  10298. ((and (symbolp files) (boundp files))
  10299. (setq files (symbol-value files))))
  10300. (when (stringp files) (setq files (list files)))
  10301. (cond
  10302. ((eq (car desc) :tag)
  10303. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10304. ((eq (car desc) :todo)
  10305. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10306. ((eq (car desc) :regexp)
  10307. (setq descre (cdr desc)))
  10308. ((eq (car desc) :level)
  10309. (setq descre (concat "^\\*\\{" (number-to-string
  10310. (if org-odd-levels-only
  10311. (1- (* 2 (cdr desc)))
  10312. (cdr desc)))
  10313. "\\}[ \t]")))
  10314. ((eq (car desc) :maxlevel)
  10315. (setq descre (concat "^\\*\\{1," (number-to-string
  10316. (if org-odd-levels-only
  10317. (1- (* 2 (cdr desc)))
  10318. (cdr desc)))
  10319. "\\}[ \t]")))
  10320. (t (error "Bad refiling target description %s" desc)))
  10321. (dolist (f files)
  10322. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10323. (or
  10324. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10325. (progn
  10326. (when (bufferp f)
  10327. (setq f (buffer-file-name (buffer-base-buffer f))))
  10328. (setq f (and f (expand-file-name f)))
  10329. (when (eq org-refile-use-outline-path 'file)
  10330. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10331. (when (eq org-refile-use-outline-path 'buffer-name)
  10332. (push (list (buffer-name (buffer-base-buffer)) f nil nil) tgs))
  10333. (when (eq org-refile-use-outline-path 'full-file-path)
  10334. (push (list (file-truename (buffer-file-name (buffer-base-buffer))) f nil nil) tgs))
  10335. (org-with-wide-buffer
  10336. (goto-char (point-min))
  10337. (setq org-outline-path-cache nil)
  10338. (while (re-search-forward descre nil t)
  10339. (beginning-of-line)
  10340. (let ((case-fold-search nil))
  10341. (looking-at org-complex-heading-regexp))
  10342. (let ((begin (point))
  10343. (heading (match-string-no-properties 4)))
  10344. (unless (or (and
  10345. org-refile-target-verify-function
  10346. (not
  10347. (funcall org-refile-target-verify-function)))
  10348. (not heading))
  10349. (let ((re (format org-complex-heading-regexp-format
  10350. (regexp-quote heading)))
  10351. (target
  10352. (if (not org-refile-use-outline-path) heading
  10353. (mapconcat
  10354. #'identity
  10355. (append
  10356. (pcase org-refile-use-outline-path
  10357. (`file (list (file-name-nondirectory
  10358. (buffer-file-name
  10359. (buffer-base-buffer)))))
  10360. (`full-file-path
  10361. (list (buffer-file-name
  10362. (buffer-base-buffer))))
  10363. (`buffer-name
  10364. (list (buffer-name
  10365. (buffer-base-buffer))))
  10366. (_ nil))
  10367. (mapcar (lambda (s) (replace-regexp-in-string
  10368. "/" "\\/" s nil t))
  10369. (org-get-outline-path t t)))
  10370. "/"))))
  10371. (push (list target f re (org-refile-marker (point)))
  10372. tgs)))
  10373. (when (= (point) begin)
  10374. ;; Verification function has not moved point.
  10375. (end-of-line)))))))
  10376. (when org-refile-use-cache
  10377. (org-refile-cache-put tgs (buffer-file-name) descre))
  10378. (setq targets (append tgs targets))))))
  10379. (message "Getting targets...done")
  10380. (delete-dups (nreverse targets))))
  10381. (defun org--get-outline-path-1 (&optional use-cache)
  10382. "Return outline path to current headline.
  10383. Outline path is a list of strings, in reverse order. When
  10384. optional argument USE-CACHE is non-nil, make use of a cache. See
  10385. `org-get-outline-path' for details.
  10386. Assume buffer is widened and point is on a headline."
  10387. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10388. (let ((p (point))
  10389. (heading (let ((case-fold-search nil))
  10390. (looking-at org-complex-heading-regexp)
  10391. (if (not (match-end 4)) ""
  10392. ;; Remove statistics cookies.
  10393. (org-trim
  10394. (org-link-display-format
  10395. (replace-regexp-in-string
  10396. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10397. (match-string-no-properties 4))))))))
  10398. (if (org-up-heading-safe)
  10399. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10400. (when use-cache
  10401. (push (cons p path) org-outline-path-cache))
  10402. path)
  10403. ;; This is a new root node. Since we assume we are moving
  10404. ;; forward, we can drop previous cache so as to limit number
  10405. ;; of associations there.
  10406. (let ((path (list heading)))
  10407. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10408. path)))))
  10409. (defun org-get-outline-path (&optional with-self use-cache)
  10410. "Return the outline path to the current entry.
  10411. An outline path is a list of ancestors for current headline, as
  10412. a list of strings. Statistics cookies are removed and links are
  10413. replaced with their description, if any, or their path otherwise.
  10414. When optional argument WITH-SELF is non-nil, the path also
  10415. includes the current headline.
  10416. When optional argument USE-CACHE is non-nil, cache outline paths
  10417. between calls to this function so as to avoid backtracking. This
  10418. argument is useful when planning to find more than one outline
  10419. path in the same document. In that case, there are two
  10420. conditions to satisfy:
  10421. - `org-outline-path-cache' is set to nil before starting the
  10422. process;
  10423. - outline paths are computed by increasing buffer positions."
  10424. (org-with-wide-buffer
  10425. (and (or (and with-self (org-back-to-heading t))
  10426. (org-up-heading-safe))
  10427. (reverse (org--get-outline-path-1 use-cache)))))
  10428. (defun org-format-outline-path (path &optional width prefix separator)
  10429. "Format the outline path PATH for display.
  10430. WIDTH is the maximum number of characters that is available.
  10431. PREFIX is a prefix to be included in the returned string,
  10432. such as the file name.
  10433. SEPARATOR is inserted between the different parts of the path,
  10434. the default is \"/\"."
  10435. (setq width (or width 79))
  10436. (setq path (delq nil path))
  10437. (unless (> width 0)
  10438. (user-error "Argument `width' must be positive"))
  10439. (setq separator (or separator "/"))
  10440. (let* ((org-odd-levels-only nil)
  10441. (fpath (concat
  10442. prefix (and prefix path separator)
  10443. (mapconcat
  10444. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10445. (cl-loop for head in path
  10446. for n from 0
  10447. collect (org-add-props
  10448. head nil 'face
  10449. (nth (% n org-n-level-faces) org-level-faces)))
  10450. separator))))
  10451. (when (> (length fpath) width)
  10452. (if (< width 7)
  10453. ;; It's unlikely that `width' will be this small, but don't
  10454. ;; waste characters by adding ".." if it is.
  10455. (setq fpath (substring fpath 0 width))
  10456. (setf (substring fpath (- width 2)) "..")))
  10457. fpath))
  10458. (defun org-display-outline-path (&optional file current separator just-return-string)
  10459. "Display the current outline path in the echo area.
  10460. If FILE is non-nil, prepend the output with the file name.
  10461. If CURRENT is non-nil, append the current heading to the output.
  10462. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10463. the different parts of the path and defaults to \"/\".
  10464. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10465. (interactive "P")
  10466. (let* (case-fold-search
  10467. (bfn (buffer-file-name (buffer-base-buffer)))
  10468. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10469. res)
  10470. (when current (setq path (append path
  10471. (save-excursion
  10472. (org-back-to-heading t)
  10473. (when (looking-at org-complex-heading-regexp)
  10474. (list (match-string 4)))))))
  10475. (setq res
  10476. (org-format-outline-path
  10477. path
  10478. (1- (frame-width))
  10479. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10480. separator))
  10481. (if just-return-string
  10482. (org-no-properties res)
  10483. (org-unlogged-message "%s" res))))
  10484. (defvar org-refile-history nil
  10485. "History for refiling operations.")
  10486. (defvar org-after-refile-insert-hook nil
  10487. "Hook run after `org-refile' has inserted its stuff at the new location.
  10488. Note that this is still *before* the stuff will be removed from
  10489. the *old* location.")
  10490. (defvar org-capture-last-stored-marker)
  10491. (defvar org-refile-keep nil
  10492. "Non-nil means `org-refile' will copy instead of refile.")
  10493. (defun org-copy ()
  10494. "Like `org-refile', but copy."
  10495. (interactive)
  10496. (let ((org-refile-keep t))
  10497. (funcall 'org-refile nil nil nil "Copy")))
  10498. (defun org-refile (&optional arg default-buffer rfloc msg)
  10499. "Move the entry or entries at point to another heading.
  10500. The list of target headings is compiled using the information in
  10501. `org-refile-targets', which see.
  10502. At the target location, the entry is filed as a subitem of the
  10503. target heading. Depending on `org-reverse-note-order', the new
  10504. subitem will either be the first or the last subitem.
  10505. If there is an active region, all entries in that region will be
  10506. refiled. However, the region must fulfill the requirement that
  10507. the first heading sets the top-level of the moved text.
  10508. With a `\\[universal-argument]' ARG, the command will only visit the target \
  10509. location
  10510. and not actually move anything.
  10511. With a prefix `\\[universal-argument] \\[universal-argument]', go to the \
  10512. location where the last
  10513. refiling operation has put the subtree.
  10514. With a numeric prefix argument of `2', refile to the running clock.
  10515. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10516. being set to t and copy to the target location, don't move it.
  10517. Beware that keeping refiled entries may result in duplicated ID
  10518. properties.
  10519. RFLOC can be a refile location obtained in a different way.
  10520. MSG is a string to replace \"Refile\" in the default prompt with
  10521. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10522. See also `org-refile-use-outline-path'.
  10523. If you are using target caching (see `org-refile-use-cache'), you
  10524. have to clear the target cache in order to find new targets.
  10525. This can be done with a `0' prefix (`C-0 C-c C-w') or a triple
  10526. prefix argument (`C-u C-u C-u C-c C-w')."
  10527. (interactive "P")
  10528. (if (member arg '(0 (64)))
  10529. (org-refile-cache-clear)
  10530. (let* ((actionmsg (cond (msg msg)
  10531. ((equal arg 3) "Refile (and keep)")
  10532. (t "Refile")))
  10533. (regionp (org-region-active-p))
  10534. (region-start (and regionp (region-beginning)))
  10535. (region-end (and regionp (region-end)))
  10536. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10537. pos it nbuf file level reversed)
  10538. (setq last-command nil)
  10539. (when regionp
  10540. (goto-char region-start)
  10541. (or (bolp) (goto-char (point-at-bol)))
  10542. (setq region-start (point))
  10543. (unless (or (org-kill-is-subtree-p
  10544. (buffer-substring region-start region-end))
  10545. (prog1 org-refile-active-region-within-subtree
  10546. (let ((s (point-at-eol)))
  10547. (org-toggle-heading)
  10548. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10549. (user-error "The region is not a (sequence of) subtree(s)")))
  10550. (if (equal arg '(16))
  10551. (org-refile-goto-last-stored)
  10552. (when (or
  10553. (and (equal arg 2)
  10554. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10555. (prog1
  10556. (setq it (list (or org-clock-heading "running clock")
  10557. (buffer-file-name
  10558. (marker-buffer org-clock-hd-marker))
  10559. ""
  10560. (marker-position org-clock-hd-marker)))
  10561. (setq arg nil)))
  10562. (setq it
  10563. (or rfloc
  10564. (let (heading-text)
  10565. (save-excursion
  10566. (unless (and arg (listp arg))
  10567. (org-back-to-heading t)
  10568. (setq heading-text
  10569. (replace-regexp-in-string
  10570. org-bracket-link-regexp
  10571. "\\3"
  10572. (or (nth 4 (org-heading-components))
  10573. ""))))
  10574. (org-refile-get-location
  10575. (cond ((and arg (listp arg)) "Goto")
  10576. (regionp (concat actionmsg " region to"))
  10577. (t (concat actionmsg " subtree \""
  10578. heading-text "\" to")))
  10579. default-buffer
  10580. (and (not (equal '(4) arg))
  10581. org-refile-allow-creating-parent-nodes)))))))
  10582. (setq file (nth 1 it)
  10583. pos (nth 3 it))
  10584. (when (and (not arg)
  10585. pos
  10586. (equal (buffer-file-name) file)
  10587. (if regionp
  10588. (and (>= pos region-start)
  10589. (<= pos region-end))
  10590. (and (>= pos (point))
  10591. (< pos (save-excursion
  10592. (org-end-of-subtree t t))))))
  10593. (error "Cannot refile to position inside the tree or region"))
  10594. (setq nbuf (or (find-buffer-visiting file)
  10595. (find-file-noselect file)))
  10596. (if (and arg (not (equal arg 3)))
  10597. (progn
  10598. (pop-to-buffer-same-window nbuf)
  10599. (goto-char (cond (pos)
  10600. ((org-notes-order-reversed-p) (point-min))
  10601. (t (point-max))))
  10602. (org-show-context 'org-goto))
  10603. (if regionp
  10604. (progn
  10605. (org-kill-new (buffer-substring region-start region-end))
  10606. (org-save-markers-in-region region-start region-end))
  10607. (org-copy-subtree 1 nil t))
  10608. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10609. (find-file-noselect file)))
  10610. (setq reversed (org-notes-order-reversed-p))
  10611. (org-with-wide-buffer
  10612. (if pos
  10613. (progn
  10614. (goto-char pos)
  10615. (setq level (org-get-valid-level (funcall outline-level) 1))
  10616. (goto-char
  10617. (if reversed
  10618. (or (outline-next-heading) (point-max))
  10619. (or (save-excursion (org-get-next-sibling))
  10620. (org-end-of-subtree t t)
  10621. (point-max)))))
  10622. (setq level 1)
  10623. (if (not reversed)
  10624. (goto-char (point-max))
  10625. (goto-char (point-min))
  10626. (or (outline-next-heading) (goto-char (point-max)))))
  10627. (unless (bolp) (newline))
  10628. (org-paste-subtree level nil nil t)
  10629. (when org-log-refile
  10630. (org-add-log-setup 'refile nil nil org-log-refile)
  10631. (unless (eq org-log-refile 'note)
  10632. (save-excursion (org-add-log-note))))
  10633. (and org-auto-align-tags
  10634. (let ((org-loop-over-headlines-in-active-region nil))
  10635. (org-set-tags nil t)))
  10636. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10637. :last-refile)))
  10638. (when bookmark-name
  10639. (with-demoted-errors
  10640. (bookmark-set bookmark-name))))
  10641. ;; If we are refiling for capture, make sure that the
  10642. ;; last-capture pointers point here
  10643. (when (bound-and-true-p org-capture-is-refiling)
  10644. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10645. :last-capture-marker)))
  10646. (when bookmark-name
  10647. (with-demoted-errors
  10648. (bookmark-set bookmark-name))))
  10649. (move-marker org-capture-last-stored-marker (point)))
  10650. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10651. (run-hooks 'org-after-refile-insert-hook)))
  10652. (unless org-refile-keep
  10653. (if regionp
  10654. (delete-region (point) (+ (point) (- region-end region-start)))
  10655. (delete-region
  10656. (and (org-back-to-heading t) (point))
  10657. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10658. (when (featurep 'org-inlinetask)
  10659. (org-inlinetask-remove-END-maybe))
  10660. (setq org-markers-to-move nil)
  10661. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10662. (defun org-refile-goto-last-stored ()
  10663. "Go to the location where the last refile was stored."
  10664. (interactive)
  10665. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10666. (message "This is the location of the last refile"))
  10667. (defun org-refile--get-location (refloc tbl)
  10668. "When user refile to REFLOC, find the associated target in TBL.
  10669. Also check `org-refile-target-table'."
  10670. (car (delq
  10671. nil
  10672. (mapcar
  10673. (lambda (r) (or (assoc r tbl)
  10674. (assoc r org-refile-target-table)))
  10675. (list (replace-regexp-in-string "/$" "" refloc)
  10676. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10677. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  10678. "Prompt the user for a refile location, using PROMPT.
  10679. PROMPT should not be suffixed with a colon and a space, because
  10680. this function appends the default value from
  10681. `org-refile-history' automatically, if that is not empty."
  10682. (let ((org-refile-targets org-refile-targets)
  10683. (org-refile-use-outline-path org-refile-use-outline-path))
  10684. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  10685. (unless org-refile-target-table
  10686. (user-error "No refile targets"))
  10687. (let* ((cbuf (current-buffer))
  10688. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10689. (cfunc (if (and org-refile-use-outline-path
  10690. org-outline-path-complete-in-steps)
  10691. #'org-olpath-completing-read
  10692. #'completing-read))
  10693. (extra (if org-refile-use-outline-path "/" ""))
  10694. (cbnex (concat (buffer-name) extra))
  10695. (filename (and cfn (expand-file-name cfn)))
  10696. (tbl (mapcar
  10697. (lambda (x)
  10698. (if (and (not (member org-refile-use-outline-path
  10699. '(file full-file-path)))
  10700. (not (equal filename (nth 1 x))))
  10701. (cons (concat (car x) extra " ("
  10702. (file-name-nondirectory (nth 1 x)) ")")
  10703. (cdr x))
  10704. (cons (concat (car x) extra) (cdr x))))
  10705. org-refile-target-table))
  10706. (completion-ignore-case t)
  10707. cdef
  10708. (prompt (concat prompt
  10709. (or (and (car org-refile-history)
  10710. (concat " (default " (car org-refile-history) ")"))
  10711. (and (assoc cbnex tbl) (setq cdef cbnex)
  10712. (concat " (default " cbnex ")"))) ": "))
  10713. pa answ parent-target child parent old-hist)
  10714. (setq old-hist org-refile-history)
  10715. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10716. nil 'org-refile-history (or cdef (car org-refile-history))))
  10717. (if (setq pa (org-refile--get-location answ tbl))
  10718. (progn
  10719. (org-refile-check-position pa)
  10720. (when (or (not org-refile-history)
  10721. (not (eq old-hist org-refile-history))
  10722. (not (equal (car pa) (car org-refile-history))))
  10723. (setq org-refile-history
  10724. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10725. org-refile-history
  10726. (cdr org-refile-history))))
  10727. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10728. (pop org-refile-history)))
  10729. pa)
  10730. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10731. (progn
  10732. (setq parent (match-string 1 answ)
  10733. child (match-string 2 answ))
  10734. (setq parent-target (org-refile--get-location parent tbl))
  10735. (when (and parent-target
  10736. (or (eq new-nodes t)
  10737. (and (eq new-nodes 'confirm)
  10738. (y-or-n-p (format "Create new node \"%s\"? "
  10739. child)))))
  10740. (org-refile-new-child parent-target child)))
  10741. (user-error "Invalid target location")))))
  10742. (declare-function org-string-nw-p "org-macs" (s))
  10743. (defun org-refile-check-position (refile-pointer)
  10744. "Check if the refile pointer matches the headline to which it points."
  10745. (let* ((file (nth 1 refile-pointer))
  10746. (re (nth 2 refile-pointer))
  10747. (pos (nth 3 refile-pointer))
  10748. buffer)
  10749. (if (and (not (markerp pos)) (not file))
  10750. (user-error "Please indicate a target file in the refile path")
  10751. (when (org-string-nw-p re)
  10752. (setq buffer (if (markerp pos)
  10753. (marker-buffer pos)
  10754. (or (find-buffer-visiting file)
  10755. (find-file-noselect file))))
  10756. (with-current-buffer buffer
  10757. (org-with-wide-buffer
  10758. (goto-char pos)
  10759. (beginning-of-line 1)
  10760. (unless (looking-at-p re)
  10761. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  10762. (defun org-refile-new-child (parent-target child)
  10763. "Use refile target PARENT-TARGET to add new CHILD below it."
  10764. (unless parent-target
  10765. (error "Cannot find parent for new node"))
  10766. (let ((file (nth 1 parent-target))
  10767. (pos (nth 3 parent-target))
  10768. level)
  10769. (with-current-buffer (or (find-buffer-visiting file)
  10770. (find-file-noselect file))
  10771. (org-with-wide-buffer
  10772. (if pos
  10773. (goto-char pos)
  10774. (goto-char (point-max))
  10775. (unless (bolp) (newline)))
  10776. (when (looking-at org-outline-regexp)
  10777. (setq level (funcall outline-level))
  10778. (org-end-of-subtree t t))
  10779. (org-back-over-empty-lines)
  10780. (insert "\n" (make-string
  10781. (if pos (org-get-valid-level level 1) 1) ?*)
  10782. " " child "\n")
  10783. (beginning-of-line 0)
  10784. (list (concat (car parent-target) "/" child) file "" (point))))))
  10785. (defun org-olpath-completing-read (prompt collection &rest args)
  10786. "Read an outline path like a file name."
  10787. (let ((thetable collection))
  10788. (apply #'completing-read
  10789. prompt
  10790. (lambda (string predicate &optional flag)
  10791. (cond
  10792. ((eq flag nil) (try-completion string thetable))
  10793. ((eq flag t)
  10794. (let ((l (length string)))
  10795. (mapcar (lambda (x)
  10796. (let ((r (substring x l))
  10797. (f (if (string-match " ([^)]*)$" x)
  10798. (match-string 0 x)
  10799. "")))
  10800. (if (string-match "/" r)
  10801. (concat string (substring r 0 (match-end 0)) f)
  10802. x)))
  10803. (all-completions string thetable predicate))))
  10804. ;; Exact match?
  10805. ((eq flag 'lambda) (assoc string thetable))))
  10806. args)))
  10807. ;;;; Dynamic blocks
  10808. (defun org-find-dblock (name)
  10809. "Find the first dynamic block with name NAME in the buffer.
  10810. If not found, stay at current position and return nil."
  10811. (let ((case-fold-search t) pos)
  10812. (save-excursion
  10813. (goto-char (point-min))
  10814. (setq pos (and (re-search-forward
  10815. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10816. (match-beginning 0))))
  10817. (when pos (goto-char pos))
  10818. pos))
  10819. (defun org-create-dblock (plist)
  10820. "Create a dynamic block section, with parameters taken from PLIST.
  10821. PLIST must contain a :name entry which is used as the name of the block."
  10822. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10823. (end-of-line 1)
  10824. (newline))
  10825. (let ((col (current-column))
  10826. (name (plist-get plist :name)))
  10827. (insert "#+BEGIN: " name)
  10828. (while plist
  10829. (if (eq (car plist) :name)
  10830. (setq plist (cddr plist))
  10831. (insert " " (prin1-to-string (pop plist)))))
  10832. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10833. (beginning-of-line -2)))
  10834. (defun org-prepare-dblock ()
  10835. "Prepare dynamic block for refresh.
  10836. This empties the block, puts the cursor at the insert position and returns
  10837. the property list including an extra property :name with the block name."
  10838. (unless (looking-at org-dblock-start-re)
  10839. (user-error "Not at a dynamic block"))
  10840. (let* ((begdel (1+ (match-end 0)))
  10841. (name (org-no-properties (match-string 1)))
  10842. (params (append (list :name name)
  10843. (read (concat "(" (match-string 3) ")")))))
  10844. (save-excursion
  10845. (beginning-of-line 1)
  10846. (skip-chars-forward " \t")
  10847. (setq params (plist-put params :indentation-column (current-column))))
  10848. (unless (re-search-forward org-dblock-end-re nil t)
  10849. (error "Dynamic block not terminated"))
  10850. (setq params
  10851. (append params
  10852. (list :content (buffer-substring
  10853. begdel (match-beginning 0)))))
  10854. (delete-region begdel (match-beginning 0))
  10855. (goto-char begdel)
  10856. (open-line 1)
  10857. params))
  10858. (defun org-map-dblocks (&optional command)
  10859. "Apply COMMAND to all dynamic blocks in the current buffer.
  10860. If COMMAND is not given, use `org-update-dblock'."
  10861. (let ((cmd (or command 'org-update-dblock)))
  10862. (save-excursion
  10863. (goto-char (point-min))
  10864. (while (re-search-forward org-dblock-start-re nil t)
  10865. (goto-char (match-beginning 0))
  10866. (save-excursion
  10867. (condition-case nil
  10868. (funcall cmd)
  10869. (error (message "Error during update of dynamic block"))))
  10870. (unless (re-search-forward org-dblock-end-re nil t)
  10871. (error "Dynamic block not terminated"))))))
  10872. (defun org-dblock-update (&optional arg)
  10873. "User command for updating dynamic blocks.
  10874. Update the dynamic block at point. With prefix ARG, update all dynamic
  10875. blocks in the buffer."
  10876. (interactive "P")
  10877. (if arg
  10878. (org-update-all-dblocks)
  10879. (or (looking-at org-dblock-start-re)
  10880. (org-beginning-of-dblock))
  10881. (org-update-dblock)))
  10882. (defun org-update-dblock ()
  10883. "Update the dynamic block at point.
  10884. This means to empty the block, parse for parameters and then call
  10885. the correct writing function."
  10886. (interactive)
  10887. (save-excursion
  10888. (let* ((win (selected-window))
  10889. (pos (point))
  10890. (line (org-current-line))
  10891. (params (org-prepare-dblock))
  10892. (name (plist-get params :name))
  10893. (indent (plist-get params :indentation-column))
  10894. (cmd (intern (concat "org-dblock-write:" name))))
  10895. (message "Updating dynamic block `%s' at line %d..." name line)
  10896. (funcall cmd params)
  10897. (message "Updating dynamic block `%s' at line %d...done" name line)
  10898. (goto-char pos)
  10899. (when (and indent (> indent 0))
  10900. (setq indent (make-string indent ?\ ))
  10901. (save-excursion
  10902. (select-window win)
  10903. (org-beginning-of-dblock)
  10904. (forward-line 1)
  10905. (while (not (looking-at org-dblock-end-re))
  10906. (insert indent)
  10907. (beginning-of-line 2))
  10908. (when (looking-at org-dblock-end-re)
  10909. (and (looking-at "[ \t]+")
  10910. (replace-match ""))
  10911. (insert indent)))))))
  10912. (defun org-beginning-of-dblock ()
  10913. "Find the beginning of the dynamic block at point.
  10914. Error if there is no such block at point."
  10915. (let ((pos (point))
  10916. beg)
  10917. (end-of-line 1)
  10918. (if (and (re-search-backward org-dblock-start-re nil t)
  10919. (setq beg (match-beginning 0))
  10920. (re-search-forward org-dblock-end-re nil t)
  10921. (> (match-end 0) pos))
  10922. (goto-char beg)
  10923. (goto-char pos)
  10924. (error "Not in a dynamic block"))))
  10925. (defun org-update-all-dblocks ()
  10926. "Update all dynamic blocks in the buffer.
  10927. This function can be used in a hook."
  10928. (interactive)
  10929. (when (derived-mode-p 'org-mode)
  10930. (org-map-dblocks 'org-update-dblock)))
  10931. ;;;; Completion
  10932. (declare-function org-export-backend-options "ox" (cl-x) t)
  10933. (defun org-get-export-keywords ()
  10934. "Return a list of all currently understood export keywords.
  10935. Export keywords include options, block names, attributes and
  10936. keywords relative to each registered export back-end."
  10937. (let (keywords)
  10938. (dolist (backend
  10939. (bound-and-true-p org-export-registered-backends)
  10940. (delq nil keywords))
  10941. ;; Back-end name (for keywords, like #+LATEX:)
  10942. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10943. (dolist (option-entry (org-export-backend-options backend))
  10944. ;; Back-end options.
  10945. (push (nth 1 option-entry) keywords)))))
  10946. (defconst org-options-keywords
  10947. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10948. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10949. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10950. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10951. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10952. (defcustom org-structure-template-alist
  10953. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  10954. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  10955. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  10956. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  10957. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  10958. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  10959. ("C" "#+BEGIN_COMMENT\n?\n#+END_COMMENT")
  10960. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  10961. ("L" "#+LaTeX: ")
  10962. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  10963. ("H" "#+HTML: ")
  10964. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  10965. ("A" "#+ASCII: ")
  10966. ("i" "#+INDEX: ?")
  10967. ("I" "#+INCLUDE: %file ?"))
  10968. "Structure completion elements.
  10969. This is a list of abbreviation keys and values. The value gets inserted
  10970. if you type `<' followed by the key and then press the completion key,
  10971. usually `TAB'. %file will be replaced by a file name after prompting
  10972. for the file using completion. The cursor will be placed at the position
  10973. of the `?' in the template.
  10974. There are two templates for each key, the first uses the original Org syntax,
  10975. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10976. the default when the /org-mtags.el/ module has been loaded. See also the
  10977. variable `org-mtags-prefer-muse-templates'."
  10978. :group 'org-completion
  10979. :type '(repeat
  10980. (list
  10981. (string :tag "Key")
  10982. (string :tag "Template")))
  10983. :version "26.1"
  10984. :package-version '(Org . "8.3"))
  10985. (defun org-try-structure-completion ()
  10986. "Try to complete a structure template before point.
  10987. This looks for strings like \"<e\" on an otherwise empty line and
  10988. expands them."
  10989. (let ((l (buffer-substring (point-at-bol) (point)))
  10990. a)
  10991. (when (and (looking-at "[ \t]*$")
  10992. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10993. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10994. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10995. (match-beginning 1)) a)
  10996. t)))
  10997. (defun org-complete-expand-structure-template (start cell)
  10998. "Expand a structure template."
  10999. (let ((rpl (nth 1 cell))
  11000. (ind ""))
  11001. (delete-region start (point))
  11002. (when (string-match "\\`[ \t]*#\\+" rpl)
  11003. (cond
  11004. ((bolp))
  11005. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11006. (setq ind (buffer-substring (point-at-bol) (point))))
  11007. (t (newline))))
  11008. (setq start (point))
  11009. (when (string-match "%file" rpl)
  11010. (setq rpl (replace-match
  11011. (concat
  11012. "\""
  11013. (save-match-data
  11014. (abbreviate-file-name (read-file-name "Include file: ")))
  11015. "\"")
  11016. t t rpl)))
  11017. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11018. (concat "\n" ind)))
  11019. (insert rpl)
  11020. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11021. ;;;; TODO, DEADLINE, Comments
  11022. (defun org-toggle-comment ()
  11023. "Change the COMMENT state of an entry."
  11024. (interactive)
  11025. (save-excursion
  11026. (org-back-to-heading)
  11027. (let ((case-fold-search nil))
  11028. (looking-at org-complex-heading-regexp))
  11029. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11030. (skip-chars-forward " \t")
  11031. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11032. (if (org-in-commented-heading-p t)
  11033. (delete-region (point)
  11034. (progn (search-forward " " (line-end-position) 'move)
  11035. (skip-chars-forward " \t")
  11036. (point)))
  11037. (insert org-comment-string)
  11038. (unless (eolp) (insert " ")))))
  11039. (defvar org-last-todo-state-is-todo nil
  11040. "This is non-nil when the last TODO state change led to a TODO state.
  11041. If the last change removed the TODO tag or switched to DONE, then
  11042. this is nil.")
  11043. (defvar org-setting-tags nil) ; dynamically skipped
  11044. (defvar org-todo-setup-filter-hook nil
  11045. "Hook for functions that pre-filter todo specs.
  11046. Each function takes a todo spec and returns either nil or the spec
  11047. transformed into canonical form." )
  11048. (defvar org-todo-get-default-hook nil
  11049. "Hook for functions that get a default item for todo.
  11050. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11051. nil or a string to be used for the todo mark." )
  11052. (defvar org-agenda-headline-snapshot-before-repeat)
  11053. (defun org-current-effective-time ()
  11054. "Return current time adjusted for `org-extend-today-until' variable."
  11055. (let* ((ct (org-current-time))
  11056. (dct (decode-time ct))
  11057. (ct1
  11058. (cond
  11059. (org-use-last-clock-out-time-as-effective-time
  11060. (or (org-clock-get-last-clock-out-time) ct))
  11061. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11062. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11063. (t ct))))
  11064. ct1))
  11065. (defun org-todo-yesterday (&optional arg)
  11066. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11067. (interactive "P")
  11068. (if (eq major-mode 'org-agenda-mode)
  11069. (apply 'org-agenda-todo-yesterday arg)
  11070. (let* ((org-use-effective-time t)
  11071. (hour (nth 2 (decode-time (org-current-time))))
  11072. (org-extend-today-until (1+ hour)))
  11073. (org-todo arg))))
  11074. (defvar org-block-entry-blocking ""
  11075. "First entry preventing the TODO state change.")
  11076. (defun org-cancel-repeater ()
  11077. "Cancel a repeater by setting its numeric value to zero."
  11078. (interactive)
  11079. (save-excursion
  11080. (org-back-to-heading t)
  11081. (let ((bound1 (point))
  11082. (bound0 (save-excursion (outline-next-heading) (point))))
  11083. (when (and (re-search-forward
  11084. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11085. org-deadline-time-regexp "\\)\\|\\("
  11086. org-ts-regexp "\\)")
  11087. bound0 t)
  11088. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11089. bound1 t))
  11090. (replace-match "0" t nil nil 1)))))
  11091. (defvar org-state)
  11092. (defvar org-blocked-by-checkboxes)
  11093. (defun org-todo (&optional arg)
  11094. "Change the TODO state of an item.
  11095. The state of an item is given by a keyword at the start of the heading,
  11096. like
  11097. *** TODO Write paper
  11098. *** DONE Call mom
  11099. The different keywords are specified in the variable `org-todo-keywords'.
  11100. By default the available states are \"TODO\" and \"DONE\". So, for this
  11101. example: when the item starts with TODO, it is changed to DONE.
  11102. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11103. DONE are present, add TODO at the beginning of the heading.
  11104. With `\\[universal-argument]' prefix ARG, use completion to determine the new \
  11105. state.
  11106. With numeric prefix ARG, switch to that state.
  11107. With a `\\[universal-argument] \\[universal-argument]' prefix, switch to the \
  11108. next set of TODO \
  11109. keywords (nextset).
  11110. With a `\\[universal-argument] \\[universal-argument] \\[universal-argument]' \
  11111. prefix, circumvent any state blocking.
  11112. With a numeric prefix arg of 0, inhibit note taking for the change.
  11113. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11114. When called through ELisp, arg is also interpreted in the following way:
  11115. `none' -> empty state
  11116. \"\" -> switch to empty state
  11117. `done' -> switch to DONE
  11118. `nextset' -> switch to the next set of keywords
  11119. `previousset' -> switch to the previous set of keywords
  11120. \"WAITING\" -> switch to the specified keyword, but only if it
  11121. really is a member of `org-todo-keywords'."
  11122. (interactive "P")
  11123. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11124. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11125. 'region-start-level 'region))
  11126. org-loop-over-headlines-in-active-region)
  11127. (org-map-entries
  11128. `(org-todo ,arg)
  11129. org-loop-over-headlines-in-active-region
  11130. cl (when (org-invisible-p) (org-end-of-subtree nil t))))
  11131. (when (equal arg '(16)) (setq arg 'nextset))
  11132. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11133. (let ((org-blocker-hook org-blocker-hook)
  11134. commentp
  11135. case-fold-search)
  11136. (when (equal arg '(64))
  11137. (setq arg nil org-blocker-hook nil))
  11138. (when (and org-blocker-hook
  11139. (or org-inhibit-blocking
  11140. (org-entry-get nil "NOBLOCKING")))
  11141. (setq org-blocker-hook nil))
  11142. (save-excursion
  11143. (catch 'exit
  11144. (org-back-to-heading t)
  11145. (when (org-in-commented-heading-p t)
  11146. (org-toggle-comment)
  11147. (setq commentp t))
  11148. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11149. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11150. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11151. (let* ((match-data (match-data))
  11152. (startpos (point-at-bol))
  11153. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11154. (org-log-done org-log-done)
  11155. (org-log-repeat org-log-repeat)
  11156. (org-todo-log-states org-todo-log-states)
  11157. (org-inhibit-logging
  11158. (if (equal arg 0)
  11159. (progn (setq arg nil) 'note) org-inhibit-logging))
  11160. (this (match-string 1))
  11161. (hl-pos (match-beginning 0))
  11162. (head (org-get-todo-sequence-head this))
  11163. (ass (assoc head org-todo-kwd-alist))
  11164. (interpret (nth 1 ass))
  11165. (done-word (nth 3 ass))
  11166. (final-done-word (nth 4 ass))
  11167. (org-last-state (or this ""))
  11168. (completion-ignore-case t)
  11169. (member (member this org-todo-keywords-1))
  11170. (tail (cdr member))
  11171. (org-state (cond
  11172. ((and org-todo-key-trigger
  11173. (or (and (equal arg '(4))
  11174. (eq org-use-fast-todo-selection 'prefix))
  11175. (and (not arg) org-use-fast-todo-selection
  11176. (not (eq org-use-fast-todo-selection
  11177. 'prefix)))))
  11178. ;; Use fast selection.
  11179. (org-fast-todo-selection))
  11180. ((and (equal arg '(4))
  11181. (or (not org-use-fast-todo-selection)
  11182. (not org-todo-key-trigger)))
  11183. ;; Read a state with completion.
  11184. (completing-read
  11185. "State: " (mapcar #'list org-todo-keywords-1)
  11186. nil t))
  11187. ((eq arg 'right)
  11188. (if this
  11189. (if tail (car tail) nil)
  11190. (car org-todo-keywords-1)))
  11191. ((eq arg 'left)
  11192. (unless (equal member org-todo-keywords-1)
  11193. (if this
  11194. (nth (- (length org-todo-keywords-1)
  11195. (length tail) 2)
  11196. org-todo-keywords-1)
  11197. (org-last org-todo-keywords-1))))
  11198. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11199. (setq arg nil))) ;hack to fall back to cycling
  11200. (arg
  11201. ;; User or caller requests a specific state.
  11202. (cond
  11203. ((equal arg "") nil)
  11204. ((eq arg 'none) nil)
  11205. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11206. ((eq arg 'nextset)
  11207. (or (car (cdr (member head org-todo-heads)))
  11208. (car org-todo-heads)))
  11209. ((eq arg 'previousset)
  11210. (let ((org-todo-heads (reverse org-todo-heads)))
  11211. (or (car (cdr (member head org-todo-heads)))
  11212. (car org-todo-heads))))
  11213. ((car (member arg org-todo-keywords-1)))
  11214. ((stringp arg)
  11215. (user-error "State `%s' not valid in this file" arg))
  11216. ((nth (1- (prefix-numeric-value arg))
  11217. org-todo-keywords-1))))
  11218. ((null member) (or head (car org-todo-keywords-1)))
  11219. ((equal this final-done-word) nil) ;-> make empty
  11220. ((null tail) nil) ;-> first entry
  11221. ((memq interpret '(type priority))
  11222. (if (eq this-command last-command)
  11223. (car tail)
  11224. (if (> (length tail) 0)
  11225. (or done-word (car org-done-keywords))
  11226. nil)))
  11227. (t
  11228. (car tail))))
  11229. (org-state (or
  11230. (run-hook-with-args-until-success
  11231. 'org-todo-get-default-hook org-state org-last-state)
  11232. org-state))
  11233. (next (if org-state (concat " " org-state " ") " "))
  11234. (change-plist (list :type 'todo-state-change :from this :to org-state
  11235. :position startpos))
  11236. dolog now-done-p)
  11237. (when org-blocker-hook
  11238. (let (org-blocked-by-checkboxes block-reason)
  11239. (setq org-last-todo-state-is-todo
  11240. (not (member this org-done-keywords)))
  11241. (unless (save-excursion
  11242. (save-match-data
  11243. (org-with-wide-buffer
  11244. (run-hook-with-args-until-failure
  11245. 'org-blocker-hook change-plist))))
  11246. (setq block-reason (if org-blocked-by-checkboxes
  11247. "contained checkboxes"
  11248. (format "\"%s\"" org-block-entry-blocking)))
  11249. (if (called-interactively-p 'interactive)
  11250. (user-error "TODO state change from %s to %s blocked (by %s)"
  11251. this org-state block-reason)
  11252. ;; Fail silently.
  11253. (message "TODO state change from %s to %s blocked (by %s)"
  11254. this org-state block-reason)
  11255. (throw 'exit nil)))))
  11256. (store-match-data match-data)
  11257. (replace-match next t t)
  11258. (cond ((equal this org-state)
  11259. (message "TODO state was already %s" (org-trim next)))
  11260. ((not (pos-visible-in-window-p hl-pos))
  11261. (message "TODO state changed to %s" (org-trim next))))
  11262. (unless head
  11263. (setq head (org-get-todo-sequence-head org-state)
  11264. ass (assoc head org-todo-kwd-alist)
  11265. interpret (nth 1 ass)
  11266. done-word (nth 3 ass)
  11267. final-done-word (nth 4 ass)))
  11268. (when (memq arg '(nextset previousset))
  11269. (message "Keyword-Set %d/%d: %s"
  11270. (- (length org-todo-sets) -1
  11271. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11272. (length org-todo-sets)
  11273. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11274. (setq org-last-todo-state-is-todo
  11275. (not (member org-state org-done-keywords)))
  11276. (setq now-done-p (and (member org-state org-done-keywords)
  11277. (not (member this org-done-keywords))))
  11278. (and logging (org-local-logging logging))
  11279. (when (and (or org-todo-log-states org-log-done)
  11280. (not (eq org-inhibit-logging t))
  11281. (not (memq arg '(nextset previousset))))
  11282. ;; We need to look at recording a time and note.
  11283. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11284. (nth 2 (assoc this org-todo-log-states))))
  11285. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11286. (setq dolog 'time))
  11287. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11288. (and org-state
  11289. (member org-state org-not-done-keywords)
  11290. (not (member this org-not-done-keywords))))
  11291. ;; This is now a todo state and was not one before
  11292. ;; If there was a CLOSED time stamp, get rid of it.
  11293. (org-add-planning-info nil nil 'closed))
  11294. (when (and now-done-p org-log-done)
  11295. ;; It is now done, and it was not done before.
  11296. (org-add-planning-info 'closed (org-current-effective-time))
  11297. (when (and (not dolog) (eq 'note org-log-done))
  11298. (org-add-log-setup 'done org-state this 'note)))
  11299. (when (and org-state dolog)
  11300. ;; This is a non-nil state, and we need to log it.
  11301. (org-add-log-setup 'state org-state this dolog)))
  11302. ;; Fixup tag positioning.
  11303. (org-todo-trigger-tag-changes org-state)
  11304. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11305. (when org-provide-todo-statistics
  11306. (org-update-parent-todo-statistics))
  11307. (run-hooks 'org-after-todo-state-change-hook)
  11308. (when (and arg (not (member org-state org-done-keywords)))
  11309. (setq head (org-get-todo-sequence-head org-state)))
  11310. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11311. ;; Do we need to trigger a repeat?
  11312. (when now-done-p
  11313. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11314. ;; This is for the agenda, take a snapshot of the headline.
  11315. (save-match-data
  11316. (setq org-agenda-headline-snapshot-before-repeat
  11317. (org-get-heading))))
  11318. (org-auto-repeat-maybe org-state))
  11319. ;; Fixup cursor location if close to the keyword.
  11320. (when (and (outline-on-heading-p)
  11321. (not (bolp))
  11322. (save-excursion (beginning-of-line 1)
  11323. (looking-at org-todo-line-regexp))
  11324. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11325. (goto-char (or (match-end 2) (match-end 1)))
  11326. (and (looking-at " ") (just-one-space)))
  11327. (when org-trigger-hook
  11328. (save-excursion
  11329. (run-hook-with-args 'org-trigger-hook change-plist)))
  11330. (when commentp (org-toggle-comment))))))))
  11331. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11332. "Block turning an entry into a TODO, using the hierarchy.
  11333. This checks whether the current task should be blocked from state
  11334. changes. Such blocking occurs when:
  11335. 1. The task has children which are not all in a completed state.
  11336. 2. A task has a parent with the property :ORDERED:, and there
  11337. are siblings prior to the current task with incomplete
  11338. status.
  11339. 3. The parent of the task is blocked because it has siblings that should
  11340. be done first, or is child of a block grandparent TODO entry."
  11341. (if (not org-enforce-todo-dependencies)
  11342. t ; if locally turned off don't block
  11343. (catch 'dont-block
  11344. ;; If this is not a todo state change, or if this entry is already DONE,
  11345. ;; do not block
  11346. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11347. (member (plist-get change-plist :from)
  11348. (cons 'done org-done-keywords))
  11349. (member (plist-get change-plist :to)
  11350. (cons 'todo org-not-done-keywords))
  11351. (not (plist-get change-plist :to)))
  11352. (throw 'dont-block t))
  11353. ;; If this task has children, and any are undone, it's blocked
  11354. (save-excursion
  11355. (org-back-to-heading t)
  11356. (let ((this-level (funcall outline-level)))
  11357. (outline-next-heading)
  11358. (let ((child-level (funcall outline-level)))
  11359. (while (and (not (eobp))
  11360. (> child-level this-level))
  11361. ;; this todo has children, check whether they are all
  11362. ;; completed
  11363. (when (and (not (org-entry-is-done-p))
  11364. (org-entry-is-todo-p))
  11365. (setq org-block-entry-blocking (org-get-heading))
  11366. (throw 'dont-block nil))
  11367. (outline-next-heading)
  11368. (setq child-level (funcall outline-level))))))
  11369. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11370. ;; any previous siblings are undone, it's blocked
  11371. (save-excursion
  11372. (org-back-to-heading t)
  11373. (let* ((pos (point))
  11374. (parent-pos (and (org-up-heading-safe) (point)))
  11375. (case-fold-search nil))
  11376. (unless parent-pos (throw 'dont-block t)) ; no parent
  11377. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11378. (forward-line 1)
  11379. (re-search-forward org-not-done-heading-regexp pos t))
  11380. (setq org-block-entry-blocking (match-string 0))
  11381. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11382. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11383. (while t
  11384. (goto-char parent-pos)
  11385. (unless (looking-at org-not-done-heading-regexp)
  11386. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11387. (setq pos (point))
  11388. (setq parent-pos (and (org-up-heading-safe) (point)))
  11389. (unless parent-pos (throw 'dont-block t)) ; no parent
  11390. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11391. (forward-line 1)
  11392. (re-search-forward org-not-done-heading-regexp pos t)
  11393. (setq org-block-entry-blocking (org-get-heading)))
  11394. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11395. (defcustom org-track-ordered-property-with-tag nil
  11396. "Should the ORDERED property also be shown as a tag?
  11397. The ORDERED property decides if an entry should require subtasks to be
  11398. completed in sequence. Since a property is not very visible, setting
  11399. this option means that toggling the ORDERED property with the command
  11400. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11401. not relevant for the behavior, but it makes things more visible.
  11402. Note that toggling the tag with tags commands will not change the property
  11403. and therefore not influence behavior!
  11404. This can be t, meaning the tag ORDERED should be used, It can also be a
  11405. string to select a different tag for this task."
  11406. :group 'org-todo
  11407. :type '(choice
  11408. (const :tag "No tracking" nil)
  11409. (const :tag "Track with ORDERED tag" t)
  11410. (string :tag "Use other tag")))
  11411. (defun org-toggle-ordered-property ()
  11412. "Toggle the ORDERED property of the current entry.
  11413. For better visibility, you can track the value of this property with a tag.
  11414. See variable `org-track-ordered-property-with-tag'."
  11415. (interactive)
  11416. (let* ((t1 org-track-ordered-property-with-tag)
  11417. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11418. (save-excursion
  11419. (org-back-to-heading)
  11420. (if (org-entry-get nil "ORDERED")
  11421. (progn
  11422. (org-delete-property "ORDERED")
  11423. (and tag (org-toggle-tag tag 'off))
  11424. (message "Subtasks can be completed in arbitrary order"))
  11425. (org-entry-put nil "ORDERED" "t")
  11426. (and tag (org-toggle-tag tag 'on))
  11427. (message "Subtasks must be completed in sequence")))))
  11428. (defun org-block-todo-from-checkboxes (change-plist)
  11429. "Block turning an entry into a TODO, using checkboxes.
  11430. This checks whether the current task should be blocked from state
  11431. changes because there are unchecked boxes in this entry."
  11432. (if (not org-enforce-todo-checkbox-dependencies)
  11433. t ; if locally turned off don't block
  11434. (catch 'dont-block
  11435. ;; If this is not a todo state change, or if this entry is already DONE,
  11436. ;; do not block
  11437. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11438. (member (plist-get change-plist :from)
  11439. (cons 'done org-done-keywords))
  11440. (member (plist-get change-plist :to)
  11441. (cons 'todo org-not-done-keywords))
  11442. (not (plist-get change-plist :to)))
  11443. (throw 'dont-block t))
  11444. ;; If this task has checkboxes that are not checked, it's blocked
  11445. (save-excursion
  11446. (org-back-to-heading t)
  11447. (let ((beg (point)) end)
  11448. (outline-next-heading)
  11449. (setq end (point))
  11450. (goto-char beg)
  11451. (when (org-list-search-forward
  11452. (concat (org-item-beginning-re)
  11453. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11454. "\\[[- ]\\]")
  11455. end t)
  11456. (when (boundp 'org-blocked-by-checkboxes)
  11457. (setq org-blocked-by-checkboxes t))
  11458. (throw 'dont-block nil))))
  11459. t))) ; do not block
  11460. (defun org-entry-blocked-p ()
  11461. "Non-nil if entry at point is blocked."
  11462. (and (not (org-entry-get nil "NOBLOCKING"))
  11463. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11464. (not (run-hook-with-args-until-failure
  11465. 'org-blocker-hook
  11466. (list :type 'todo-state-change
  11467. :position (point)
  11468. :from 'todo
  11469. :to 'done)))))
  11470. (defun org-update-statistics-cookies (all)
  11471. "Update the statistics cookie, either from TODO or from checkboxes.
  11472. This should be called with the cursor in a line with a statistics
  11473. cookie. When called with a \\[universal-argument] prefix, update
  11474. all statistics cookies in the buffer."
  11475. (interactive "P")
  11476. (if all
  11477. (progn
  11478. (org-update-checkbox-count 'all)
  11479. (org-map-entries 'org-update-parent-todo-statistics))
  11480. (if (not (org-at-heading-p))
  11481. (org-update-checkbox-count)
  11482. (let ((pos (point-marker))
  11483. end l1 l2)
  11484. (ignore-errors (org-back-to-heading t))
  11485. (if (not (org-at-heading-p))
  11486. (org-update-checkbox-count)
  11487. (setq l1 (org-outline-level))
  11488. (setq end (save-excursion
  11489. (outline-next-heading)
  11490. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11491. (point)))
  11492. (if (and (save-excursion
  11493. (re-search-forward
  11494. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11495. (not (save-excursion (re-search-forward
  11496. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11497. (org-update-checkbox-count)
  11498. (if (and l2 (> l2 l1))
  11499. (progn
  11500. (goto-char end)
  11501. (org-update-parent-todo-statistics))
  11502. (goto-char pos)
  11503. (beginning-of-line 1)
  11504. (while (re-search-forward
  11505. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11506. (point-at-eol) t)
  11507. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11508. (goto-char pos)
  11509. (move-marker pos nil)))))
  11510. (defvar org-entry-property-inherited-from) ;; defined below
  11511. (defun org-update-parent-todo-statistics ()
  11512. "Update any statistics cookie in the parent of the current headline.
  11513. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11514. the entire subtree and this will travel up the hierarchy and update
  11515. statistics everywhere."
  11516. (let* ((prop (save-excursion (org-up-heading-safe)
  11517. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11518. (recursive (or (not org-hierarchical-todo-statistics)
  11519. (and prop (string-match "\\<recursive\\>" prop))))
  11520. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11521. 0))
  11522. (first t)
  11523. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11524. level ltoggle l1 new ndel
  11525. (cnt-all 0) (cnt-done 0) is-percent kwd
  11526. checkbox-beg cookie-present)
  11527. (catch 'exit
  11528. (save-excursion
  11529. (beginning-of-line 1)
  11530. (setq ltoggle (funcall outline-level))
  11531. ;; Three situations are to consider:
  11532. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11533. ;; to the top-level ancestor on the headline;
  11534. ;; 2. If parent has "recursive" property, repeat up to the
  11535. ;; headline setting that property, taking inheritance into
  11536. ;; account;
  11537. ;; 3. Else, move up to direct parent and proceed only once.
  11538. (while (and (setq level (org-up-heading-safe))
  11539. (or recursive first)
  11540. (>= (point) lim))
  11541. (setq first nil cookie-present nil)
  11542. (unless (and level
  11543. (not (string-match
  11544. "\\<checkbox\\>"
  11545. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11546. "")))))
  11547. (throw 'exit nil))
  11548. (while (re-search-forward box-re (point-at-eol) t)
  11549. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11550. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11551. (save-match-data
  11552. (unless (outline-next-heading) (throw 'exit nil))
  11553. (while (and (looking-at org-complex-heading-regexp)
  11554. (> (setq l1 (length (match-string 1))) level))
  11555. (setq kwd (and (or recursive (= l1 ltoggle))
  11556. (match-string 2)))
  11557. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11558. (and (eq org-provide-todo-statistics t)
  11559. (or (member kwd org-done-keywords)))
  11560. (and (listp org-provide-todo-statistics)
  11561. (stringp (car org-provide-todo-statistics))
  11562. (or (member kwd org-provide-todo-statistics)
  11563. (member kwd org-done-keywords)))
  11564. (and (listp org-provide-todo-statistics)
  11565. (listp (car org-provide-todo-statistics))
  11566. (or (member kwd (car org-provide-todo-statistics))
  11567. (and (member kwd org-done-keywords)
  11568. (member kwd (cadr org-provide-todo-statistics))))))
  11569. (setq cnt-all (1+ cnt-all))
  11570. (and (eq org-provide-todo-statistics t)
  11571. kwd
  11572. (setq cnt-all (1+ cnt-all))))
  11573. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11574. (member kwd org-done-keywords))
  11575. (and (listp org-provide-todo-statistics)
  11576. (listp (car org-provide-todo-statistics))
  11577. (member kwd org-done-keywords)
  11578. (member kwd (cadr org-provide-todo-statistics)))
  11579. (and (listp org-provide-todo-statistics)
  11580. (stringp (car org-provide-todo-statistics))
  11581. (member kwd org-done-keywords)))
  11582. (setq cnt-done (1+ cnt-done)))
  11583. (outline-next-heading)))
  11584. (setq new
  11585. (if is-percent
  11586. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11587. (max 1 cnt-all)))
  11588. (format "[%d/%d]" cnt-done cnt-all))
  11589. ndel (- (match-end 0) checkbox-beg))
  11590. (goto-char checkbox-beg)
  11591. (insert new)
  11592. (delete-region (point) (+ (point) ndel))
  11593. (when org-auto-align-tags (org-fix-tags-on-the-fly)))
  11594. (when cookie-present
  11595. (run-hook-with-args 'org-after-todo-statistics-hook
  11596. cnt-done (- cnt-all cnt-done))))))
  11597. (run-hooks 'org-todo-statistics-hook)))
  11598. (defvar org-after-todo-statistics-hook nil
  11599. "Hook that is called after a TODO statistics cookie has been updated.
  11600. Each function is called with two arguments: the number of not-done entries
  11601. and the number of done entries.
  11602. For example, the following function, when added to this hook, will switch
  11603. an entry to DONE when all children are done, and back to TODO when new
  11604. entries are set to a TODO status. Note that this hook is only called
  11605. when there is a statistics cookie in the headline!
  11606. (defun org-summary-todo (n-done n-not-done)
  11607. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11608. (let (org-log-done org-log-states) ; turn off logging
  11609. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11610. ")
  11611. (defvar org-todo-statistics-hook nil
  11612. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11613. This hook runs even if there is no statistics cookie present, in which case
  11614. `org-after-todo-statistics-hook' would not run.")
  11615. (defun org-todo-trigger-tag-changes (state)
  11616. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11617. (let ((l org-todo-state-tags-triggers)
  11618. changes)
  11619. (when (or (not state) (equal state ""))
  11620. (setq changes (append changes (cdr (assoc "" l)))))
  11621. (when (and (stringp state) (> (length state) 0))
  11622. (setq changes (append changes (cdr (assoc state l)))))
  11623. (when (member state org-not-done-keywords)
  11624. (setq changes (append changes (cdr (assq 'todo l)))))
  11625. (when (member state org-done-keywords)
  11626. (setq changes (append changes (cdr (assq 'done l)))))
  11627. (dolist (c changes)
  11628. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11629. (defun org-local-logging (value)
  11630. "Get logging settings from a property VALUE."
  11631. ;; Directly set the variables, they are already local.
  11632. (setq org-log-done nil
  11633. org-log-repeat nil
  11634. org-todo-log-states nil)
  11635. (dolist (w (org-split-string value))
  11636. (let (a)
  11637. (cond
  11638. ((setq a (assoc w org-startup-options))
  11639. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11640. (set (nth 1 a) (nth 2 a))))
  11641. ((setq a (org-extract-log-state-settings w))
  11642. (and (member (car a) org-todo-keywords-1)
  11643. (push a org-todo-log-states)))))))
  11644. (defun org-get-todo-sequence-head (kwd)
  11645. "Return the head of the TODO sequence to which KWD belongs.
  11646. If KWD is not set, check if there is a text property remembering the
  11647. right sequence."
  11648. (let (p)
  11649. (cond
  11650. ((not kwd)
  11651. (or (get-text-property (point-at-bol) 'org-todo-head)
  11652. (progn
  11653. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11654. nil (point-at-eol)))
  11655. (get-text-property p 'org-todo-head))))
  11656. ((not (member kwd org-todo-keywords-1))
  11657. (car org-todo-keywords-1))
  11658. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11659. (defun org-fast-todo-selection ()
  11660. "Fast TODO keyword selection with single keys.
  11661. Returns the new TODO keyword, or nil if no state change should occur."
  11662. (let* ((fulltable org-todo-key-alist)
  11663. (done-keywords org-done-keywords) ;; needed for the faces.
  11664. (maxlen (apply 'max (mapcar
  11665. (lambda (x)
  11666. (if (stringp (car x)) (string-width (car x)) 0))
  11667. fulltable)))
  11668. (expert nil)
  11669. (fwidth (+ maxlen 3 1 3))
  11670. (ncol (/ (- (window-width) 4) fwidth))
  11671. tg cnt e c tbl
  11672. groups ingroup)
  11673. (save-excursion
  11674. (save-window-excursion
  11675. (if expert
  11676. (set-buffer (get-buffer-create " *Org todo*"))
  11677. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11678. (erase-buffer)
  11679. (setq-local org-done-keywords done-keywords)
  11680. (setq tbl fulltable cnt 0)
  11681. (while (setq e (pop tbl))
  11682. (cond
  11683. ((equal e '(:startgroup))
  11684. (push '() groups) (setq ingroup t)
  11685. (unless (= cnt 0)
  11686. (setq cnt 0)
  11687. (insert "\n"))
  11688. (insert "{ "))
  11689. ((equal e '(:endgroup))
  11690. (setq ingroup nil cnt 0)
  11691. (insert "}\n"))
  11692. ((equal e '(:newline))
  11693. (unless (= cnt 0)
  11694. (setq cnt 0)
  11695. (insert "\n")
  11696. (setq e (car tbl))
  11697. (while (equal (car tbl) '(:newline))
  11698. (insert "\n")
  11699. (setq tbl (cdr tbl)))))
  11700. (t
  11701. (setq tg (car e) c (cdr e))
  11702. (when ingroup (push tg (car groups)))
  11703. (setq tg (org-add-props tg nil 'face
  11704. (org-get-todo-face tg)))
  11705. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11706. (insert "[" c "] " tg (make-string
  11707. (- fwidth 4 (length tg)) ?\ ))
  11708. (when (= (setq cnt (1+ cnt)) ncol)
  11709. (insert "\n")
  11710. (when ingroup (insert " "))
  11711. (setq cnt 0)))))
  11712. (insert "\n")
  11713. (goto-char (point-min))
  11714. (unless expert (org-fit-window-to-buffer))
  11715. (message "[a-z..]:Set [SPC]:clear")
  11716. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11717. (cond
  11718. ((or (= c ?\C-g)
  11719. (and (= c ?q) (not (rassoc c fulltable))))
  11720. (setq quit-flag t))
  11721. ((= c ?\ ) nil)
  11722. ((setq e (rassoc c fulltable) tg (car e))
  11723. tg)
  11724. (t (setq quit-flag t)))))))
  11725. (defun org-entry-is-todo-p ()
  11726. (member (org-get-todo-state) org-not-done-keywords))
  11727. (defun org-entry-is-done-p ()
  11728. (member (org-get-todo-state) org-done-keywords))
  11729. (defun org-get-todo-state ()
  11730. "Return the TODO keyword of the current subtree."
  11731. (save-excursion
  11732. (org-back-to-heading t)
  11733. (and (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  11734. (match-end 2)
  11735. (match-string 2))))
  11736. (defun org-at-date-range-p (&optional inactive-ok)
  11737. "Non-nil if point is inside a date range.
  11738. When optional argument INACTIVE-OK is non-nil, also consider
  11739. inactive time ranges.
  11740. When this function returns a non-nil value, match data is set
  11741. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11742. on INACTIVE-OK."
  11743. (interactive)
  11744. (save-excursion
  11745. (catch 'exit
  11746. (let ((pos (point)))
  11747. (skip-chars-backward "^[<\r\n")
  11748. (skip-chars-backward "<[")
  11749. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11750. (>= (match-end 0) pos)
  11751. (throw 'exit t))
  11752. (skip-chars-backward "^<[\r\n")
  11753. (skip-chars-backward "<[")
  11754. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11755. (>= (match-end 0) pos)
  11756. (throw 'exit t)))
  11757. nil)))
  11758. (defun org-get-repeat (&optional timestamp)
  11759. "Check if there is a time-stamp with repeater in this entry.
  11760. Return the repeater, as a string, or nil. Also return nil when
  11761. this function is called before first heading.
  11762. When optional argument TIMESTAMP is a string, extract the
  11763. repeater from there instead."
  11764. (save-match-data
  11765. (cond (timestamp
  11766. (and (string-match org-repeat-re timestamp)
  11767. (match-string-no-properties 1 timestamp)))
  11768. ((org-before-first-heading-p) nil)
  11769. (t
  11770. (save-excursion
  11771. (org-back-to-heading t)
  11772. (let ((end (org-entry-end-position)))
  11773. (catch :repeat
  11774. (while (re-search-forward org-repeat-re end t)
  11775. (when (save-match-data (org-at-timestamp-p 'agenda))
  11776. (throw :repeat (match-string-no-properties 1)))))))))))
  11777. (defvar org-last-changed-timestamp)
  11778. (defvar org-last-inserted-timestamp)
  11779. (defvar org-log-post-message)
  11780. (defvar org-log-note-purpose)
  11781. (defvar org-log-note-how nil)
  11782. (defvar org-log-note-extra)
  11783. (defun org-auto-repeat-maybe (done-word)
  11784. "Check if the current headline contains a repeated time-stamp.
  11785. If yes, set TODO state back to what it was and change the base date
  11786. of repeating deadline/scheduled time stamps to new date.
  11787. This function is run automatically after each state change to a DONE state."
  11788. (let* ((repeat (org-get-repeat))
  11789. (aa (assoc org-last-state org-todo-kwd-alist))
  11790. (interpret (nth 1 aa))
  11791. (head (nth 2 aa))
  11792. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11793. (msg "Entry repeats: ")
  11794. (org-log-done nil)
  11795. (org-todo-log-states nil)
  11796. (end (copy-marker (org-entry-end-position))))
  11797. (unwind-protect
  11798. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11799. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11800. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11801. org-todo-repeat-to-state)))
  11802. (org-todo (cond
  11803. ((and to-state (member to-state org-todo-keywords-1))
  11804. to-state)
  11805. ((eq interpret 'type) org-last-state)
  11806. (head)
  11807. (t 'none))))
  11808. (org-back-to-heading t)
  11809. (org-add-planning-info nil nil 'closed)
  11810. ;; When `org-log-repeat' is non-nil or entry contains
  11811. ;; a clock, set LAST_REPEAT property.
  11812. (when (or org-log-repeat
  11813. (catch :clock
  11814. (while (re-search-forward org-clock-line-re end t)
  11815. (when (org-at-clock-log-p) (throw :clock t)))))
  11816. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11817. (org-time-stamp-format t t)
  11818. (current-time))))
  11819. (when org-log-repeat
  11820. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11821. (memq 'org-add-log-note post-command-hook))
  11822. ;; We are already setup for some record.
  11823. (when (eq org-log-repeat 'note)
  11824. ;; Make sure we take a note, not only a time stamp.
  11825. (setq org-log-note-how 'note))
  11826. ;; Set up for taking a record.
  11827. (org-add-log-setup 'state
  11828. (or done-word (car org-done-keywords))
  11829. org-last-state
  11830. org-log-repeat)))
  11831. (let ((planning-re (regexp-opt
  11832. (list org-scheduled-string org-deadline-string))))
  11833. (while (re-search-forward org-ts-regexp end t)
  11834. (let* ((ts (match-string 0))
  11835. (planning? (org-at-planning-p))
  11836. (type (if (not planning?) "Plain:"
  11837. (save-excursion
  11838. (re-search-backward
  11839. planning-re (line-beginning-position) t)
  11840. (match-string 0)))))
  11841. (cond
  11842. ;; Ignore fake time-stamps (e.g., within comments).
  11843. ((not (org-at-timestamp-p 'agenda)))
  11844. ;; Time-stamps without a repeater are usually
  11845. ;; skipped. However, a SCHEDULED time-stamp without
  11846. ;; one is removed, as they are no longer relevant.
  11847. ((not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11848. ts))
  11849. (when (equal type org-scheduled-string)
  11850. (org-remove-timestamp-with-keyword type)))
  11851. (t
  11852. (let ((n (string-to-number (match-string 2 ts)))
  11853. (what (match-string 3 ts)))
  11854. (when (equal what "w") (setq n (* n 7) what "d"))
  11855. (when (and (equal what "h")
  11856. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11857. ts)))
  11858. (user-error
  11859. "Cannot repeat in Repeat in %d hour(s) because no hour \
  11860. has been set"
  11861. n))
  11862. ;; Preparation, see if we need to modify the start
  11863. ;; date for the change.
  11864. (when (match-end 1)
  11865. (let ((time (save-match-data
  11866. (org-time-string-to-time ts))))
  11867. (cond
  11868. ((equal (match-string 1 ts) ".")
  11869. ;; Shift starting date to today
  11870. (org-timestamp-change
  11871. (- (org-today) (time-to-days time))
  11872. 'day))
  11873. ((equal (match-string 1 ts) "+")
  11874. (let ((nshiftmax 10)
  11875. (nshift 0))
  11876. (while (or (= nshift 0)
  11877. (not (time-less-p (current-time) time)))
  11878. (when (= (cl-incf nshift) nshiftmax)
  11879. (or (y-or-n-p
  11880. (format "%d repeater intervals were not \
  11881. enough to shift date past today. Continue? "
  11882. nshift))
  11883. (user-error "Abort")))
  11884. (org-timestamp-change n (cdr (assoc what whata)))
  11885. (org-in-regexp org-ts-regexp3)
  11886. (setq ts (match-string 1))
  11887. (setq time
  11888. (save-match-data
  11889. (org-time-string-to-time ts)))))
  11890. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11891. ;; Rematch, so that we have everything in place
  11892. ;; for the real shift.
  11893. (org-in-regexp org-ts-regexp3)
  11894. (setq ts (match-string 1))
  11895. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11896. ts)))))
  11897. (save-excursion
  11898. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11899. (setq msg
  11900. (concat
  11901. msg type " " org-last-changed-timestamp " "))))))))
  11902. (setq org-log-post-message msg)
  11903. (message "%s" msg))
  11904. (set-marker end nil))))
  11905. (defun org-show-todo-tree (arg)
  11906. "Make a compact tree which shows all headlines marked with TODO.
  11907. The tree will show the lines where the regexp matches, and all higher
  11908. headlines above the match.
  11909. With a `\\[universal-argument]' prefix, prompt for a regexp to match.
  11910. With a numeric prefix N, construct a sparse tree for the Nth element
  11911. of `org-todo-keywords-1'."
  11912. (interactive "P")
  11913. (let ((case-fold-search nil)
  11914. (kwd-re
  11915. (cond ((null arg) org-not-done-regexp)
  11916. ((equal arg '(4))
  11917. (let ((kwd
  11918. (completing-read "Keyword (or KWD1|KWD2|...): "
  11919. (mapcar #'list org-todo-keywords-1))))
  11920. (concat "\\("
  11921. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11922. "\\)\\>")))
  11923. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11924. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11925. org-todo-keywords-1)))
  11926. (t (user-error "Invalid prefix argument: %s" arg)))))
  11927. (message "%d TODO entries found"
  11928. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11929. (defun org--deadline-or-schedule (arg type time)
  11930. "Insert DEADLINE or SCHEDULE information in current entry.
  11931. TYPE is either `deadline' or `scheduled'. See `org-deadline' or
  11932. `org-schedule' for information about ARG and TIME arguments."
  11933. (let* ((deadline? (eq type 'deadline))
  11934. (keyword (if deadline? org-deadline-string org-scheduled-string))
  11935. (log (if deadline? org-log-redeadline org-log-reschedule))
  11936. (old-date (org-entry-get nil (if deadline? "DEADLINE" "SCHEDULED")))
  11937. (old-date-time (and old-date (org-time-string-to-time old-date)))
  11938. ;; Save repeater cookie from either TIME or current scheduled
  11939. ;; time stamp. We are going to insert it back at the end of
  11940. ;; the process.
  11941. (repeater (or (and (org-string-nw-p time)
  11942. ;; We use `org-repeat-re' because we need
  11943. ;; to tell the difference between a real
  11944. ;; repeater and a time delta, e.g. "+2d".
  11945. (string-match org-repeat-re time)
  11946. (match-string 1 time))
  11947. (and (org-string-nw-p old-date)
  11948. (string-match "\\([.+-]+[0-9]+[hdwmy]\
  11949. \\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\)"
  11950. old-date)
  11951. (match-string 1 old-date)))))
  11952. (pcase arg
  11953. (`(4)
  11954. (when (and old-date log)
  11955. (org-add-log-setup (if deadline? 'deldeadline 'delschedule)
  11956. nil old-date log))
  11957. (org-remove-timestamp-with-keyword keyword)
  11958. (message (if deadline? "Item no longer has a deadline."
  11959. "Item is no longer scheduled.")))
  11960. (`(16)
  11961. (save-excursion
  11962. (org-back-to-heading t)
  11963. (let ((regexp (if deadline? org-deadline-time-regexp
  11964. org-scheduled-time-regexp)))
  11965. (if (not (re-search-forward regexp (line-end-position 2) t))
  11966. (user-error (if deadline? "No deadline information to update"
  11967. "No scheduled information to update"))
  11968. (let* ((rpl0 (match-string 1))
  11969. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0))
  11970. (msg (if deadline? "Warn starting from" "Delay until")))
  11971. (replace-match
  11972. (concat keyword
  11973. " <" rpl
  11974. (format " -%dd"
  11975. (abs (- (time-to-days
  11976. (save-match-data
  11977. (org-read-date
  11978. nil t nil msg old-date-time)))
  11979. (time-to-days old-date-time))))
  11980. ">") t t))))))
  11981. (_
  11982. (org-add-planning-info type time 'closed)
  11983. (when (and old-date
  11984. log
  11985. (not (equal old-date org-last-inserted-timestamp)))
  11986. (org-add-log-setup (if deadline? 'redeadline 'reschedule)
  11987. org-last-inserted-timestamp
  11988. old-date
  11989. log))
  11990. (when repeater
  11991. (save-excursion
  11992. (org-back-to-heading t)
  11993. (when (re-search-forward
  11994. (concat keyword " " org-last-inserted-timestamp)
  11995. (line-end-position 2)
  11996. t)
  11997. (goto-char (1- (match-end 0)))
  11998. (insert " " repeater)
  11999. (setq org-last-inserted-timestamp
  12000. (concat (substring org-last-inserted-timestamp 0 -1)
  12001. " " repeater
  12002. (substring org-last-inserted-timestamp -1))))))
  12003. (message (if deadline? "Deadline on %s" "Scheduled to %s")
  12004. org-last-inserted-timestamp)))))
  12005. (defun org-deadline (arg &optional time)
  12006. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12007. With one universal prefix argument, remove any deadline from the item.
  12008. With two universal prefix arguments, prompt for a warning delay.
  12009. With argument TIME, set the deadline at the corresponding date. TIME
  12010. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12011. (interactive "P")
  12012. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12013. (org-map-entries
  12014. (lambda () (org--deadline-or-schedule arg 'deadline time))
  12015. nil
  12016. (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12017. 'region-start-level
  12018. 'region)
  12019. (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
  12020. (org--deadline-or-schedule arg 'deadline time)))
  12021. (defun org-schedule (arg &optional time)
  12022. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12023. With one universal prefix argument, remove any scheduling date from the item.
  12024. With two universal prefix arguments, prompt for a delay cookie.
  12025. With argument TIME, scheduled at the corresponding date. TIME can
  12026. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12027. (interactive "P")
  12028. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12029. (org-map-entries
  12030. (lambda () (org--deadline-or-schedule arg 'scheduled time))
  12031. nil
  12032. (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12033. 'region-start-level
  12034. 'region)
  12035. (lambda () (when (org-invisible-p) (org-end-of-subtree nil t))))
  12036. (org--deadline-or-schedule arg 'scheduled time)))
  12037. (defun org-get-scheduled-time (pom &optional inherit)
  12038. "Get the scheduled time as a time tuple, of a format suitable
  12039. for calling org-schedule with, or if there is no scheduling,
  12040. returns nil."
  12041. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12042. (when time
  12043. (apply 'encode-time (org-parse-time-string time)))))
  12044. (defun org-get-deadline-time (pom &optional inherit)
  12045. "Get the deadline as a time tuple, of a format suitable for
  12046. calling org-deadline with, or if there is no scheduling, returns
  12047. nil."
  12048. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12049. (when time
  12050. (apply 'encode-time (org-parse-time-string time)))))
  12051. (defun org-remove-timestamp-with-keyword (keyword)
  12052. "Remove all time stamps with KEYWORD in the current entry."
  12053. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12054. beg)
  12055. (save-excursion
  12056. (org-back-to-heading t)
  12057. (setq beg (point))
  12058. (outline-next-heading)
  12059. (while (re-search-backward re beg t)
  12060. (replace-match "")
  12061. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12062. (equal (char-before) ?\ ))
  12063. (backward-delete-char 1)
  12064. (when (string-match "^[ \t]*$" (buffer-substring
  12065. (point-at-bol) (point-at-eol)))
  12066. (delete-region (point-at-bol)
  12067. (min (point-max) (1+ (point-at-eol))))))))))
  12068. (defvar org-time-was-given) ; dynamically scoped parameter
  12069. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12070. (defun org-at-planning-p ()
  12071. "Non-nil when point is on a planning info line."
  12072. ;; This is as accurate and faster than `org-element-at-point' since
  12073. ;; planning info location is fixed in the section.
  12074. (org-with-wide-buffer
  12075. (beginning-of-line)
  12076. (and (looking-at-p org-planning-line-re)
  12077. (eq (point)
  12078. (ignore-errors
  12079. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12080. (org-back-to-heading t)
  12081. (org-with-limited-levels (org-back-to-heading t)))
  12082. (line-beginning-position 2))))))
  12083. (defun org-add-planning-info (what &optional time &rest remove)
  12084. "Insert new timestamp with keyword in the planning line.
  12085. WHAT indicates what kind of time stamp to add. It is a symbol
  12086. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12087. the time to use. If none is given, the user is prompted for
  12088. a date. REMOVE indicates what kind of entries to remove. An old
  12089. WHAT entry will also be removed."
  12090. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12091. (catch 'exit
  12092. (when (and (memq what '(scheduled deadline))
  12093. (or (not time)
  12094. (and (stringp time)
  12095. (string-match "^[-+]+[0-9]" time))))
  12096. ;; Try to get a default date/time from existing timestamp
  12097. (save-excursion
  12098. (org-back-to-heading t)
  12099. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12100. (when (re-search-forward (if (eq what 'scheduled)
  12101. org-scheduled-time-regexp
  12102. org-deadline-time-regexp)
  12103. end t)
  12104. (setq ts (match-string 1)
  12105. default-time (apply 'encode-time (org-parse-time-string ts))
  12106. default-input (and ts (org-get-compact-tod ts)))))))
  12107. (when what
  12108. (setq time
  12109. (if (stringp time)
  12110. ;; This is a string (relative or absolute), set
  12111. ;; proper date.
  12112. (apply #'encode-time
  12113. (org-read-date-analyze
  12114. time default-time (decode-time default-time)))
  12115. ;; If necessary, get the time from the user
  12116. (or time (org-read-date nil 'to-time nil nil
  12117. default-time default-input)))))
  12118. (org-with-wide-buffer
  12119. (org-back-to-heading t)
  12120. (forward-line)
  12121. (unless (bolp) (insert "\n"))
  12122. (cond ((looking-at-p org-planning-line-re)
  12123. ;; Move to current indentation.
  12124. (skip-chars-forward " \t")
  12125. ;; Check if we have to remove something.
  12126. (dolist (type (if what (cons what remove) remove))
  12127. (save-excursion
  12128. (when (re-search-forward
  12129. (cl-case type
  12130. (closed org-closed-time-regexp)
  12131. (deadline org-deadline-time-regexp)
  12132. (scheduled org-scheduled-time-regexp)
  12133. (otherwise
  12134. (error "Invalid planning type: %s" type)))
  12135. (line-end-position) t)
  12136. ;; Delete until next keyword or end of line.
  12137. (delete-region
  12138. (match-beginning 0)
  12139. (if (re-search-forward org-keyword-time-not-clock-regexp
  12140. (line-end-position)
  12141. t)
  12142. (match-beginning 0)
  12143. (line-end-position))))))
  12144. ;; If there is nothing more to add and no more keyword
  12145. ;; is left, remove the line completely.
  12146. (if (and (looking-at-p "[ \t]*$") (not what))
  12147. (delete-region (line-beginning-position)
  12148. (line-beginning-position 2))
  12149. ;; If we removed last keyword, do not leave trailing
  12150. ;; white space at the end of line.
  12151. (let ((p (point)))
  12152. (save-excursion
  12153. (end-of-line)
  12154. (unless (= (skip-chars-backward " \t" p) 0)
  12155. (delete-region (point) (line-end-position)))))))
  12156. ((not what) (throw 'exit nil)) ; Nothing to do.
  12157. (t (insert-before-markers "\n")
  12158. (backward-char 1)
  12159. (when org-adapt-indentation
  12160. (indent-to-column (1+ (org-outline-level))))))
  12161. (when what
  12162. ;; Insert planning keyword.
  12163. (insert (cl-case what
  12164. (closed org-closed-string)
  12165. (deadline org-deadline-string)
  12166. (scheduled org-scheduled-string)
  12167. (otherwise (error "Invalid planning type: %s" what)))
  12168. " ")
  12169. ;; Insert associated timestamp.
  12170. (let ((ts (org-insert-time-stamp
  12171. time
  12172. (or org-time-was-given
  12173. (and (eq what 'closed) org-log-done-with-time))
  12174. (eq what 'closed)
  12175. nil nil (list org-end-time-was-given))))
  12176. (unless (eolp) (insert " "))
  12177. ts))))))
  12178. (defvar org-log-note-marker (make-marker)
  12179. "Marker pointing at the entry where the note is to be inserted.")
  12180. (defvar org-log-note-purpose nil)
  12181. (defvar org-log-note-state nil)
  12182. (defvar org-log-note-previous-state nil)
  12183. (defvar org-log-note-extra nil)
  12184. (defvar org-log-note-window-configuration nil)
  12185. (defvar org-log-note-return-to (make-marker))
  12186. (defvar org-log-note-effective-time nil
  12187. "Remembered current time so that dynamically scoped
  12188. `org-extend-today-until' affects timestamps in state change log")
  12189. (defvar org-log-post-message nil
  12190. "Message to be displayed after a log note has been stored.
  12191. The auto-repeater uses this.")
  12192. (defun org-add-note ()
  12193. "Add a note to the current entry.
  12194. This is done in the same way as adding a state change note."
  12195. (interactive)
  12196. (org-add-log-setup 'note))
  12197. (defun org-log-beginning (&optional create)
  12198. "Return expected start of log notes in current entry.
  12199. When optional argument CREATE is non-nil, the function creates
  12200. a drawer to store notes, if necessary. Returned position ignores
  12201. narrowing."
  12202. (org-with-wide-buffer
  12203. (let ((drawer (org-log-into-drawer)))
  12204. (cond
  12205. (drawer
  12206. (org-end-of-meta-data)
  12207. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12208. (end (if (org-at-heading-p) (point)
  12209. (save-excursion (outline-next-heading) (point))))
  12210. (case-fold-search t))
  12211. (catch 'exit
  12212. ;; Try to find existing drawer.
  12213. (while (re-search-forward regexp end t)
  12214. (let ((element (org-element-at-point)))
  12215. (when (eq (org-element-type element) 'drawer)
  12216. (let ((cend (org-element-property :contents-end element)))
  12217. (when (and (not org-log-states-order-reversed) cend)
  12218. (goto-char cend)))
  12219. (throw 'exit nil))))
  12220. ;; No drawer found. Create one, if permitted.
  12221. (when create
  12222. (unless (bolp) (insert "\n"))
  12223. (let ((beg (point)))
  12224. (insert ":" drawer ":\n:END:\n")
  12225. (org-indent-region beg (point)))
  12226. (end-of-line -1)))))
  12227. (t
  12228. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12229. (skip-chars-forward " \t\n")
  12230. (beginning-of-line)
  12231. (unless org-log-states-order-reversed
  12232. (org-skip-over-state-notes)
  12233. (skip-chars-backward " \t\n")
  12234. (forward-line)))))
  12235. (if (bolp) (point) (line-beginning-position 2))))
  12236. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12237. "Set up the post command hook to take a note.
  12238. If this is about to TODO state change, the new state is expected in STATE.
  12239. HOW is an indicator what kind of note should be created.
  12240. EXTRA is additional text that will be inserted into the notes buffer."
  12241. (move-marker org-log-note-marker (point))
  12242. (setq org-log-note-purpose purpose
  12243. org-log-note-state state
  12244. org-log-note-previous-state prev-state
  12245. org-log-note-how how
  12246. org-log-note-extra extra
  12247. org-log-note-effective-time (org-current-effective-time))
  12248. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12249. (defun org-skip-over-state-notes ()
  12250. "Skip past the list of State notes in an entry."
  12251. (when (ignore-errors (goto-char (org-in-item-p)))
  12252. (let* ((struct (org-list-struct))
  12253. (prevs (org-list-prevs-alist struct))
  12254. (regexp
  12255. (concat "[ \t]*- +"
  12256. (replace-regexp-in-string
  12257. " +" " +"
  12258. (org-replace-escapes
  12259. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12260. `(("%d" . ,org-ts-regexp-inactive)
  12261. ("%D" . ,org-ts-regexp)
  12262. ("%s" . "\"\\S-+\"")
  12263. ("%S" . "\"\\S-+\"")
  12264. ("%t" . ,org-ts-regexp-inactive)
  12265. ("%T" . ,org-ts-regexp)
  12266. ("%u" . ".*?")
  12267. ("%U" . ".*?")))))))
  12268. (while (looking-at-p regexp)
  12269. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12270. (org-list-get-item-end (point) struct)))))))
  12271. (defun org-add-log-note (&optional _purpose)
  12272. "Pop up a window for taking a note, and add this note later."
  12273. (remove-hook 'post-command-hook 'org-add-log-note)
  12274. (setq org-log-note-window-configuration (current-window-configuration))
  12275. (delete-other-windows)
  12276. (move-marker org-log-note-return-to (point))
  12277. (pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12278. (goto-char org-log-note-marker)
  12279. (org-switch-to-buffer-other-window "*Org Note*")
  12280. (erase-buffer)
  12281. (if (memq org-log-note-how '(time state))
  12282. (org-store-log-note)
  12283. (let ((org-inhibit-startup t)) (org-mode))
  12284. (insert (format "# Insert note for %s.
  12285. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12286. (cond
  12287. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12288. ((eq org-log-note-purpose 'done) "closed todo item")
  12289. ((eq org-log-note-purpose 'state)
  12290. (format "state change from \"%s\" to \"%s\""
  12291. (or org-log-note-previous-state "")
  12292. (or org-log-note-state "")))
  12293. ((eq org-log-note-purpose 'reschedule)
  12294. "rescheduling")
  12295. ((eq org-log-note-purpose 'delschedule)
  12296. "no longer scheduled")
  12297. ((eq org-log-note-purpose 'redeadline)
  12298. "changing deadline")
  12299. ((eq org-log-note-purpose 'deldeadline)
  12300. "removing deadline")
  12301. ((eq org-log-note-purpose 'refile)
  12302. "refiling")
  12303. ((eq org-log-note-purpose 'note)
  12304. "this entry")
  12305. (t (error "This should not happen")))))
  12306. (when org-log-note-extra (insert org-log-note-extra))
  12307. (setq-local org-finish-function 'org-store-log-note)
  12308. (run-hooks 'org-log-buffer-setup-hook)))
  12309. (defvar org-note-abort nil) ; dynamically scoped
  12310. (defun org-store-log-note ()
  12311. "Finish taking a log note, and insert it to where it belongs."
  12312. (let ((txt (prog1 (buffer-string)
  12313. (kill-buffer)))
  12314. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12315. lines)
  12316. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12317. (setq txt (replace-match "" t t txt)))
  12318. (when (string-match "\\s-+\\'" txt)
  12319. (setq txt (replace-match "" t t txt)))
  12320. (setq lines (and (not (equal "" txt)) (org-split-string txt "\n")))
  12321. (when (org-string-nw-p note)
  12322. (setq note
  12323. (org-replace-escapes
  12324. note
  12325. (list (cons "%u" (user-login-name))
  12326. (cons "%U" user-full-name)
  12327. (cons "%t" (format-time-string
  12328. (org-time-stamp-format 'long 'inactive)
  12329. org-log-note-effective-time))
  12330. (cons "%T" (format-time-string
  12331. (org-time-stamp-format 'long nil)
  12332. org-log-note-effective-time))
  12333. (cons "%d" (format-time-string
  12334. (org-time-stamp-format nil 'inactive)
  12335. org-log-note-effective-time))
  12336. (cons "%D" (format-time-string
  12337. (org-time-stamp-format nil nil)
  12338. org-log-note-effective-time))
  12339. (cons "%s" (cond
  12340. ((not org-log-note-state) "")
  12341. ((string-match-p org-ts-regexp
  12342. org-log-note-state)
  12343. (format "\"[%s]\""
  12344. (substring org-log-note-state 1 -1)))
  12345. (t (format "\"%s\"" org-log-note-state))))
  12346. (cons "%S"
  12347. (cond
  12348. ((not org-log-note-previous-state) "")
  12349. ((string-match-p org-ts-regexp
  12350. org-log-note-previous-state)
  12351. (format "\"[%s]\""
  12352. (substring
  12353. org-log-note-previous-state 1 -1)))
  12354. (t (format "\"%s\""
  12355. org-log-note-previous-state)))))))
  12356. (when lines (setq note (concat note " \\\\")))
  12357. (push note lines))
  12358. (when (and lines (not org-note-abort))
  12359. (with-current-buffer (marker-buffer org-log-note-marker)
  12360. (org-with-wide-buffer
  12361. ;; Find location for the new note.
  12362. (goto-char org-log-note-marker)
  12363. (set-marker org-log-note-marker nil)
  12364. ;; Note associated to a clock is to be located right after
  12365. ;; the clock. Do not move point.
  12366. (unless (eq org-log-note-purpose 'clock-out)
  12367. (goto-char (org-log-beginning t)))
  12368. ;; Make sure point is at the beginning of an empty line.
  12369. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12370. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12371. ;; In an existing list, add a new item at the top level.
  12372. ;; Otherwise, indent line like a regular one.
  12373. (let ((itemp (org-in-item-p)))
  12374. (if itemp
  12375. (indent-line-to
  12376. (let ((struct (save-excursion
  12377. (goto-char itemp) (org-list-struct))))
  12378. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12379. (org-indent-line)))
  12380. (insert (org-list-bullet-string "-") (pop lines))
  12381. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12382. (dolist (line lines)
  12383. (insert "\n")
  12384. (indent-line-to ind)
  12385. (insert line)))
  12386. (message "Note stored")
  12387. (org-back-to-heading t)
  12388. (org-cycle-hide-drawers 'children))
  12389. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12390. ;; from within `org-add-log-note' because `buffer-undo-list'
  12391. ;; is then modified outside of `org-with-remote-undo'.
  12392. (when (eq this-command 'org-agenda-todo)
  12393. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12394. ;; Don't add undo information when called from `org-agenda-todo'.
  12395. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12396. (set-window-configuration org-log-note-window-configuration)
  12397. (with-current-buffer (marker-buffer org-log-note-return-to)
  12398. (goto-char org-log-note-return-to))
  12399. (move-marker org-log-note-return-to nil)
  12400. (when org-log-post-message (message "%s" org-log-post-message))))
  12401. (defun org-remove-empty-drawer-at (pos)
  12402. "Remove an empty drawer at position POS.
  12403. POS may also be a marker."
  12404. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12405. (org-with-wide-buffer
  12406. (goto-char pos)
  12407. (let ((drawer (org-element-at-point)))
  12408. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12409. (not (org-element-property :contents-begin drawer)))
  12410. (delete-region (org-element-property :begin drawer)
  12411. (progn (goto-char (org-element-property :end drawer))
  12412. (skip-chars-backward " \r\t\n")
  12413. (forward-line)
  12414. (point))))))))
  12415. (defvar org-ts-type nil)
  12416. (defun org-sparse-tree (&optional arg type)
  12417. "Create a sparse tree, prompt for the details.
  12418. This command can create sparse trees. You first need to select the type
  12419. of match used to create the tree:
  12420. t Show all TODO entries.
  12421. T Show entries with a specific TODO keyword.
  12422. m Show entries selected by a tags/property match.
  12423. p Enter a property name and its value (both with completion on existing
  12424. names/values) and show entries with that property.
  12425. r Show entries matching a regular expression (`/' can be used as well).
  12426. b Show deadlines and scheduled items before a date.
  12427. a Show deadlines and scheduled items after a date.
  12428. d Show deadlines due within `org-deadline-warning-days'.
  12429. D Show deadlines and scheduled items between a date range."
  12430. (interactive "P")
  12431. (setq type (or type org-sparse-tree-default-date-type))
  12432. (setq org-ts-type type)
  12433. (message "Sparse tree: [r]egexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12434. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12435. \[c]ycle through date types: %s"
  12436. (cl-case type
  12437. (all "all timestamps")
  12438. (scheduled "only scheduled")
  12439. (deadline "only deadline")
  12440. (active "only active timestamps")
  12441. (inactive "only inactive timestamps")
  12442. (closed "with a closed time-stamp")
  12443. (otherwise "scheduled/deadline")))
  12444. (let ((answer (read-char-exclusive)))
  12445. (cl-case answer
  12446. (?c
  12447. (org-sparse-tree
  12448. arg
  12449. (cadr
  12450. (memq type '(nil all scheduled deadline active inactive closed)))))
  12451. (?d (call-interactively 'org-check-deadlines))
  12452. (?b (call-interactively 'org-check-before-date))
  12453. (?a (call-interactively 'org-check-after-date))
  12454. (?D (call-interactively 'org-check-dates-range))
  12455. (?t (call-interactively 'org-show-todo-tree))
  12456. (?T (org-show-todo-tree '(4)))
  12457. (?m (call-interactively 'org-match-sparse-tree))
  12458. ((?p ?P)
  12459. (let* ((kwd (completing-read
  12460. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12461. (value (completing-read
  12462. "Value: " (mapcar #'list (org-property-values kwd)))))
  12463. (unless (string-match "\\`{.*}\\'" value)
  12464. (setq value (concat "\"" value "\"")))
  12465. (org-match-sparse-tree arg (concat kwd "=" value))))
  12466. ((?r ?R ?/) (call-interactively 'org-occur))
  12467. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12468. (defvar-local org-occur-highlights nil
  12469. "List of overlays used for occur matches.")
  12470. (defvar-local org-occur-parameters nil
  12471. "Parameters of the active org-occur calls.
  12472. This is a list, each call to org-occur pushes as cons cell,
  12473. containing the regular expression and the callback, onto the list.
  12474. The list can contain several entries if `org-occur' has been called
  12475. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12476. will only contain one set of parameters. When the highlights are
  12477. removed (for example with `C-c C-c', or with the next edit (depending
  12478. on `org-remove-highlights-with-change'), this variable is emptied
  12479. as well.")
  12480. (defun org-occur (regexp &optional keep-previous callback)
  12481. "Make a compact tree which shows all matches of REGEXP.
  12482. The tree will show the lines where the regexp matches, and any other context
  12483. defined in `org-show-context-detail', which see.
  12484. When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
  12485. done by a previous call to `org-occur' will be kept, to allow stacking of
  12486. calls to this command.
  12487. Optional argument CALLBACK can be a function of no argument. In this case,
  12488. it is called with point at the end of the match, match data being set
  12489. accordingly. Current match is shown only if the return value is non-nil.
  12490. The function must neither move point nor alter narrowing."
  12491. (interactive "sRegexp: \nP")
  12492. (when (equal regexp "")
  12493. (user-error "Regexp cannot be empty"))
  12494. (unless keep-previous
  12495. (org-remove-occur-highlights nil nil t))
  12496. (push (cons regexp callback) org-occur-parameters)
  12497. (let ((cnt 0))
  12498. (save-excursion
  12499. (goto-char (point-min))
  12500. (when (or (not keep-previous) ; do not want to keep
  12501. (not org-occur-highlights)) ; no previous matches
  12502. ;; hide everything
  12503. (org-overview))
  12504. (let ((case-fold-search (if (eq org-occur-case-fold-search 'smart)
  12505. (isearch-no-upper-case-p regexp t)
  12506. org-occur-case-fold-search)))
  12507. (while (re-search-forward regexp nil t)
  12508. (when (or (not callback)
  12509. (save-match-data (funcall callback)))
  12510. (setq cnt (1+ cnt))
  12511. (when org-highlight-sparse-tree-matches
  12512. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12513. (org-show-context 'occur-tree)))))
  12514. (when org-remove-highlights-with-change
  12515. (add-hook 'before-change-functions 'org-remove-occur-highlights
  12516. nil 'local))
  12517. (unless org-sparse-tree-open-archived-trees
  12518. (org-hide-archived-subtrees (point-min) (point-max)))
  12519. (run-hooks 'org-occur-hook)
  12520. (when (called-interactively-p 'interactive)
  12521. (message "%d match(es) for regexp %s" cnt regexp))
  12522. cnt))
  12523. (defun org-occur-next-match (&optional n _reset)
  12524. "Function for `next-error-function' to find sparse tree matches.
  12525. N is the number of matches to move, when negative move backwards.
  12526. This function always goes back to the starting point when no
  12527. match is found."
  12528. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12529. (search-func (if (< n 0)
  12530. 'previous-single-char-property-change
  12531. 'next-single-char-property-change))
  12532. (n (abs n))
  12533. (pos (point))
  12534. p1)
  12535. (catch 'exit
  12536. (while (setq p1 (funcall search-func (point) 'org-type))
  12537. (when (equal p1 limit)
  12538. (goto-char pos)
  12539. (user-error "No more matches"))
  12540. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12541. (setq n (1- n))
  12542. (when (= n 0)
  12543. (goto-char p1)
  12544. (throw 'exit (point))))
  12545. (goto-char p1))
  12546. (goto-char p1)
  12547. (user-error "No more matches"))))
  12548. (defun org-show-context (&optional key)
  12549. "Make sure point and context are visible.
  12550. Optional argument KEY, when non-nil, is a symbol. See
  12551. `org-show-context-detail' for allowed values and how much is to
  12552. be shown."
  12553. (org-show-set-visibility
  12554. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12555. ((cdr (assq key org-show-context-detail)))
  12556. (t (cdr (assq 'default org-show-context-detail))))))
  12557. (defun org-show-set-visibility (detail)
  12558. "Set visibility around point according to DETAIL.
  12559. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12560. `tree', `canonical' or t. See `org-show-context-detail' for more
  12561. information."
  12562. ;; Show current heading and possibly its entry, following headline
  12563. ;; or all children.
  12564. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12565. (org-flag-heading nil)
  12566. (org-show-entry)
  12567. ;; If point is hidden within a drawer or a block, make sure to
  12568. ;; expose it.
  12569. (dolist (o (overlays-at (point)))
  12570. (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
  12571. (delete-overlay o)))
  12572. (unless (org-before-first-heading-p)
  12573. (org-with-limited-levels
  12574. (cl-case detail
  12575. ((tree canonical t) (org-show-children))
  12576. ((nil minimal ancestors))
  12577. (t (save-excursion
  12578. (outline-next-heading)
  12579. (org-flag-heading nil)))))))
  12580. ;; Show all siblings.
  12581. (when (eq detail 'lineage) (org-show-siblings))
  12582. ;; Show ancestors, possibly with their children.
  12583. (when (memq detail '(ancestors lineage tree canonical t))
  12584. (save-excursion
  12585. (while (org-up-heading-safe)
  12586. (org-flag-heading nil)
  12587. (when (memq detail '(canonical t)) (org-show-entry))
  12588. (when (memq detail '(tree canonical t)) (org-show-children))))))
  12589. (defvar org-reveal-start-hook nil
  12590. "Hook run before revealing a location.")
  12591. (defun org-reveal (&optional siblings)
  12592. "Show current entry, hierarchy above it, and the following headline.
  12593. This can be used to show a consistent set of context around
  12594. locations exposed with `org-show-context'.
  12595. With optional argument SIBLINGS, on each level of the hierarchy all
  12596. siblings are shown. This repairs the tree structure to what it would
  12597. look like when opened with hierarchical calls to `org-cycle'.
  12598. With a \\[universal-argument] \\[universal-argument] prefix, \
  12599. go to the parent and show the entire tree."
  12600. (interactive "P")
  12601. (run-hooks 'org-reveal-start-hook)
  12602. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12603. ((equal siblings '(16))
  12604. (save-excursion
  12605. (when (org-up-heading-safe)
  12606. (org-show-subtree)
  12607. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12608. (t (org-show-set-visibility 'lineage))))
  12609. (defun org-highlight-new-match (beg end)
  12610. "Highlight from BEG to END and mark the highlight is an occur headline."
  12611. (let ((ov (make-overlay beg end)))
  12612. (overlay-put ov 'face 'secondary-selection)
  12613. (overlay-put ov 'org-type 'org-occur)
  12614. (push ov org-occur-highlights)))
  12615. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12616. "Remove the occur highlights from the buffer.
  12617. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12618. from the `before-change-functions' in the current buffer."
  12619. (interactive)
  12620. (unless org-inhibit-highlight-removal
  12621. (mapc #'delete-overlay org-occur-highlights)
  12622. (setq org-occur-highlights nil)
  12623. (setq org-occur-parameters nil)
  12624. (unless noremove
  12625. (remove-hook 'before-change-functions
  12626. 'org-remove-occur-highlights 'local))))
  12627. ;;;; Priorities
  12628. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12629. "Regular expression matching the priority indicator.")
  12630. (defvar org-remove-priority-next-time nil)
  12631. (defun org-priority-up ()
  12632. "Increase the priority of the current item."
  12633. (interactive)
  12634. (org-priority 'up))
  12635. (defun org-priority-down ()
  12636. "Decrease the priority of the current item."
  12637. (interactive)
  12638. (org-priority 'down))
  12639. (defun org-priority (&optional action _show)
  12640. "Change the priority of an item.
  12641. ACTION can be `set', `up', `down', or a character."
  12642. (interactive "P")
  12643. (if (equal action '(4))
  12644. (org-show-priority)
  12645. (unless org-enable-priority-commands
  12646. (user-error "Priority commands are disabled"))
  12647. (setq action (or action 'set))
  12648. (let (current new news have remove)
  12649. (save-excursion
  12650. (org-back-to-heading t)
  12651. (when (looking-at org-priority-regexp)
  12652. (setq current (string-to-char (match-string 2))
  12653. have t))
  12654. (cond
  12655. ((eq action 'remove)
  12656. (setq remove t new ?\ ))
  12657. ((or (eq action 'set)
  12658. (integerp action))
  12659. (if (not (eq action 'set))
  12660. (setq new action)
  12661. (message "Priority %c-%c, SPC to remove: "
  12662. org-highest-priority org-lowest-priority)
  12663. (save-match-data
  12664. (setq new (read-char-exclusive))))
  12665. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12666. (= (upcase org-lowest-priority) org-lowest-priority))
  12667. (setq new (upcase new)))
  12668. (cond ((equal new ?\ ) (setq remove t))
  12669. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12670. (user-error "Priority must be between `%c' and `%c'"
  12671. org-highest-priority org-lowest-priority))))
  12672. ((eq action 'up)
  12673. (setq new (if have
  12674. (1- current) ; normal cycling
  12675. ;; last priority was empty
  12676. (if (eq last-command this-command)
  12677. org-lowest-priority ; wrap around empty to lowest
  12678. ;; default
  12679. (if org-priority-start-cycle-with-default
  12680. org-default-priority
  12681. (1- org-default-priority))))))
  12682. ((eq action 'down)
  12683. (setq new (if have
  12684. (1+ current) ; normal cycling
  12685. ;; last priority was empty
  12686. (if (eq last-command this-command)
  12687. org-highest-priority ; wrap around empty to highest
  12688. ;; default
  12689. (if org-priority-start-cycle-with-default
  12690. org-default-priority
  12691. (1+ org-default-priority))))))
  12692. (t (user-error "Invalid action")))
  12693. (when (or (< (upcase new) org-highest-priority)
  12694. (> (upcase new) org-lowest-priority))
  12695. (if (and (memq action '(up down))
  12696. (not have) (not (eq last-command this-command)))
  12697. ;; `new' is from default priority
  12698. (error
  12699. "The default can not be set, see `org-default-priority' why")
  12700. ;; normal cycling: `new' is beyond highest/lowest priority
  12701. ;; and is wrapped around to the empty priority
  12702. (setq remove t)))
  12703. (setq news (format "%c" new))
  12704. (if have
  12705. (if remove
  12706. (replace-match "" t t nil 1)
  12707. (replace-match news t t nil 2))
  12708. (if remove
  12709. (user-error "No priority cookie found in line")
  12710. (let ((case-fold-search nil)) (looking-at org-todo-line-regexp))
  12711. (if (match-end 2)
  12712. (progn
  12713. (goto-char (match-end 2))
  12714. (insert " [#" news "]"))
  12715. (goto-char (match-beginning 3))
  12716. (insert "[#" news "] "))))
  12717. (org-set-tags nil 'align))
  12718. (if remove
  12719. (message "Priority removed")
  12720. (message "Priority of current item set to %s" news)))))
  12721. (defun org-show-priority ()
  12722. "Show the priority of the current item.
  12723. This priority is composed of the main priority given with the [#A] cookies,
  12724. and by additional input from the age of a schedules or deadline entry."
  12725. (interactive)
  12726. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12727. (org-get-at-bol 'priority)
  12728. (save-excursion
  12729. (save-match-data
  12730. (beginning-of-line)
  12731. (and (looking-at org-heading-regexp)
  12732. (org-get-priority (match-string 0))))))))
  12733. (message "Priority is %d" (if pri pri -1000))))
  12734. (defun org-get-priority (s)
  12735. "Find priority cookie and return priority."
  12736. (save-match-data
  12737. (if (functionp org-get-priority-function)
  12738. (funcall org-get-priority-function)
  12739. (if (not (string-match org-priority-regexp s))
  12740. (* 1000 (- org-lowest-priority org-default-priority))
  12741. (* 1000 (- org-lowest-priority
  12742. (string-to-char (match-string 2 s))))))))
  12743. ;;;; Tags
  12744. (defvar org-agenda-archives-mode)
  12745. (defvar org-map-continue-from nil
  12746. "Position from where mapping should continue.
  12747. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12748. (defvar org-scanner-tags nil
  12749. "The current tag list while the tags scanner is running.")
  12750. (defvar org-trust-scanner-tags nil
  12751. "Should `org-get-tags-at' use the tags for the scanner.
  12752. This is for internal dynamical scoping only.
  12753. When this is non-nil, the function `org-get-tags-at' will return the value
  12754. of `org-scanner-tags' instead of building the list by itself. This
  12755. can lead to large speed-ups when the tags scanner is used in a file with
  12756. many entries, and when the list of tags is retrieved, for example to
  12757. obtain a list of properties. Building the tags list for each entry in such
  12758. a file becomes an N^2 operation - but with this variable set, it scales
  12759. as N.")
  12760. (defvar org--matcher-tags-todo-only nil)
  12761. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12762. "Scan headline tags with inheritance and produce output ACTION.
  12763. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12764. or `agenda' to produce an entry list for an agenda view. It can also be
  12765. a Lisp form or a function that should be called at each matched headline, in
  12766. this case the return value is a list of all return values from these calls.
  12767. MATCHER is a function accepting three arguments, returning
  12768. a non-nil value whenever a given set of tags qualifies a headline
  12769. for inclusion. See `org-make-tags-matcher' for more information.
  12770. As a special case, it can also be set to t (respectively nil) in
  12771. order to match all (respectively none) headline.
  12772. When TODO-ONLY is non-nil, only lines with a TODO keyword are
  12773. included in the output.
  12774. START-LEVEL can be a string with asterisks, reducing the scope to
  12775. headlines matching this string."
  12776. (require 'org-agenda)
  12777. (let* ((re (concat "^"
  12778. (if start-level
  12779. ;; Get the correct level to match
  12780. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12781. org-outline-regexp)
  12782. " *\\(\\<\\("
  12783. (mapconcat #'regexp-quote org-todo-keywords-1 "\\|")
  12784. "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
  12785. (props (list 'face 'default
  12786. 'done-face 'org-agenda-done
  12787. 'undone-face 'default
  12788. 'mouse-face 'highlight
  12789. 'org-not-done-regexp org-not-done-regexp
  12790. 'org-todo-regexp org-todo-regexp
  12791. 'org-complex-heading-regexp org-complex-heading-regexp
  12792. 'help-echo
  12793. (format "mouse-2 or RET jump to org file %s"
  12794. (abbreviate-file-name
  12795. (or (buffer-file-name (buffer-base-buffer))
  12796. (buffer-name (buffer-base-buffer)))))))
  12797. (org-map-continue-from nil)
  12798. lspos tags tags-list
  12799. (tags-alist (list (cons 0 org-file-tags)))
  12800. (llast 0) rtn rtn1 level category i txt
  12801. todo marker entry priority
  12802. ts-date ts-date-type ts-date-pair)
  12803. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12804. (setq action (list 'lambda nil action)))
  12805. (save-excursion
  12806. (goto-char (point-min))
  12807. (when (eq action 'sparse-tree)
  12808. (org-overview)
  12809. (org-remove-occur-highlights))
  12810. (while (let (case-fold-search)
  12811. (re-search-forward re nil t))
  12812. (setq org-map-continue-from nil)
  12813. (catch :skip
  12814. (setq todo
  12815. ;; TODO: is the 1-2 difference a bug?
  12816. (when (match-end 1) (match-string-no-properties 2))
  12817. tags (when (match-end 4) (match-string-no-properties 4)))
  12818. (goto-char (setq lspos (match-beginning 0)))
  12819. (setq level (org-reduced-level (org-outline-level))
  12820. category (org-get-category))
  12821. (when (eq action 'agenda)
  12822. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12823. ts-date (car ts-date-pair)
  12824. ts-date-type (cdr ts-date-pair)))
  12825. (setq i llast llast level)
  12826. ;; remove tag lists from same and sublevels
  12827. (while (>= i level)
  12828. (when (setq entry (assoc i tags-alist))
  12829. (setq tags-alist (delete entry tags-alist)))
  12830. (setq i (1- i)))
  12831. ;; add the next tags
  12832. (when tags
  12833. (setq tags (org-split-string tags ":")
  12834. tags-alist
  12835. (cons (cons level tags) tags-alist)))
  12836. ;; compile tags for current headline
  12837. (setq tags-list
  12838. (if org-use-tag-inheritance
  12839. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12840. tags)
  12841. org-scanner-tags tags-list)
  12842. (when org-use-tag-inheritance
  12843. (setcdr (car tags-alist)
  12844. (mapcar (lambda (x)
  12845. (setq x (copy-sequence x))
  12846. (org-add-prop-inherited x))
  12847. (cdar tags-alist))))
  12848. (when (and tags org-use-tag-inheritance
  12849. (or (not (eq t org-use-tag-inheritance))
  12850. org-tags-exclude-from-inheritance))
  12851. ;; Selective inheritance, remove uninherited ones.
  12852. (setcdr (car tags-alist)
  12853. (org-remove-uninherited-tags (cdar tags-alist))))
  12854. (when (and
  12855. ;; eval matcher only when the todo condition is OK
  12856. (and (or (not todo-only) (member todo org-todo-keywords-1))
  12857. (if (functionp matcher)
  12858. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12859. (funcall matcher todo tags-list level))
  12860. matcher))
  12861. ;; Call the skipper, but return t if it does not
  12862. ;; skip, so that the `and' form continues evaluating.
  12863. (progn
  12864. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12865. t)
  12866. ;; Check if timestamps are deselecting this entry
  12867. (or (not todo-only)
  12868. (and (member todo org-todo-keywords-1)
  12869. (or (not org-agenda-tags-todo-honor-ignore-options)
  12870. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12871. ;; select this headline
  12872. (cond
  12873. ((eq action 'sparse-tree)
  12874. (and org-highlight-sparse-tree-matches
  12875. (org-get-heading) (match-end 0)
  12876. (org-highlight-new-match
  12877. (match-beginning 1) (match-end 1)))
  12878. (org-show-context 'tags-tree))
  12879. ((eq action 'agenda)
  12880. (setq txt (org-agenda-format-item
  12881. ""
  12882. (concat
  12883. (if (eq org-tags-match-list-sublevels 'indented)
  12884. (make-string (1- level) ?.) "")
  12885. (org-get-heading))
  12886. (make-string level ?\s)
  12887. category
  12888. tags-list)
  12889. priority (org-get-priority txt))
  12890. (goto-char lspos)
  12891. (setq marker (org-agenda-new-marker))
  12892. (org-add-props txt props
  12893. 'org-marker marker 'org-hd-marker marker 'org-category category
  12894. 'todo-state todo
  12895. 'ts-date ts-date
  12896. 'priority priority
  12897. 'type (concat "tagsmatch" ts-date-type))
  12898. (push txt rtn))
  12899. ((functionp action)
  12900. (setq org-map-continue-from nil)
  12901. (save-excursion
  12902. (setq rtn1 (funcall action))
  12903. (push rtn1 rtn)))
  12904. (t (user-error "Invalid action")))
  12905. ;; if we are to skip sublevels, jump to end of subtree
  12906. (unless org-tags-match-list-sublevels
  12907. (org-end-of-subtree t)
  12908. (backward-char 1))))
  12909. ;; Get the correct position from where to continue
  12910. (if org-map-continue-from
  12911. (goto-char org-map-continue-from)
  12912. (and (= (point) lspos) (end-of-line 1)))))
  12913. (when (and (eq action 'sparse-tree)
  12914. (not org-sparse-tree-open-archived-trees))
  12915. (org-hide-archived-subtrees (point-min) (point-max)))
  12916. (nreverse rtn)))
  12917. (defun org-remove-uninherited-tags (tags)
  12918. "Remove all tags that are not inherited from the list TAGS."
  12919. (cond
  12920. ((eq org-use-tag-inheritance t)
  12921. (if org-tags-exclude-from-inheritance
  12922. (org-delete-all org-tags-exclude-from-inheritance tags)
  12923. tags))
  12924. ((not org-use-tag-inheritance) nil)
  12925. ((stringp org-use-tag-inheritance)
  12926. (delq nil (mapcar
  12927. (lambda (x)
  12928. (if (and (string-match org-use-tag-inheritance x)
  12929. (not (member x org-tags-exclude-from-inheritance)))
  12930. x nil))
  12931. tags)))
  12932. ((listp org-use-tag-inheritance)
  12933. (delq nil (mapcar
  12934. (lambda (x)
  12935. (if (member x org-use-tag-inheritance) x nil))
  12936. tags)))))
  12937. (defun org-match-sparse-tree (&optional todo-only match)
  12938. "Create a sparse tree according to tags string MATCH.
  12939. MATCH is a string with match syntax. It can contain a selection
  12940. of tags (\"+work+urgent-boss\"), properties (\"LEVEL>3\"), and
  12941. TODO keywords (\"TODO=\\\"WAITING\\\"\") or a combination of
  12942. those. See the manual for details.
  12943. If optional argument TODO-ONLY is non-nil, only select lines that
  12944. are also TODO tasks."
  12945. (interactive "P")
  12946. (org-agenda-prepare-buffers (list (current-buffer)))
  12947. (let ((org--matcher-tags-todo-only todo-only))
  12948. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  12949. org--matcher-tags-todo-only)))
  12950. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12951. (defvar org-cached-props nil)
  12952. (defun org-cached-entry-get (pom property)
  12953. (if (or (eq t org-use-property-inheritance)
  12954. (and (stringp org-use-property-inheritance)
  12955. (let ((case-fold-search t))
  12956. (string-match-p org-use-property-inheritance property)))
  12957. (and (listp org-use-property-inheritance)
  12958. (member-ignore-case property org-use-property-inheritance)))
  12959. ;; Caching is not possible, check it directly.
  12960. (org-entry-get pom property 'inherit)
  12961. ;; Get all properties, so we can do complicated checks easily.
  12962. (cdr (assoc-string property
  12963. (or org-cached-props
  12964. (setq org-cached-props (org-entry-properties pom)))
  12965. t))))
  12966. (defun org-global-tags-completion-table (&optional files)
  12967. "Return the list of all tags in all agenda buffer/files.
  12968. Optional FILES argument is a list of files which can be used
  12969. instead of the agenda files."
  12970. (save-excursion
  12971. (org-uniquify
  12972. (delq nil
  12973. (apply #'append
  12974. (mapcar
  12975. (lambda (file)
  12976. (set-buffer (find-file-noselect file))
  12977. (mapcar (lambda (x)
  12978. (and (stringp (car-safe x))
  12979. (list (car-safe x))))
  12980. (or org-current-tag-alist (org-get-buffer-tags))))
  12981. (if (car-safe files) files
  12982. (org-agenda-files))))))))
  12983. (defun org-make-tags-matcher (match)
  12984. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12985. Returns a cons of the selection string MATCH and a function
  12986. implementing the matcher.
  12987. The matcher is to be called at an Org entry, with point on the
  12988. headline, and returns non-nil if the entry matches the selection
  12989. string MATCH. It must be called with three arguments: the TODO
  12990. keyword at the entry (or nil if none), the list of all tags at
  12991. the entry including inherited ones and the reduced level of the
  12992. headline. Additionally, the category of the entry, if any, must
  12993. be specified as the text property `org-category' on the headline.
  12994. This function sets the variable `org--matcher-tags-todo-only' to
  12995. a non-nil value if the matcher restricts matching to TODO
  12996. entries, otherwise it is not touched.
  12997. See also `org-scan-tags'."
  12998. (unless match
  12999. ;; Get a new match request, with completion against the global
  13000. ;; tags table and the local tags in current buffer.
  13001. (let ((org-last-tags-completion-table
  13002. (org-uniquify
  13003. (delq nil (append (org-get-buffer-tags)
  13004. (org-global-tags-completion-table))))))
  13005. (setq match
  13006. (completing-read
  13007. "Match: "
  13008. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13009. (let ((match0 match)
  13010. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)")
  13011. (start 0)
  13012. tagsmatch todomatch tagsmatcher todomatcher)
  13013. ;; Expand group tags.
  13014. (setq match (org-tags-expand match))
  13015. ;; Check if there is a TODO part of this match, which would be the
  13016. ;; part after a "/". To make sure that this slash is not part of
  13017. ;; a property value to be matched against, we also check that
  13018. ;; there is no / after that slash. First, find the last slash.
  13019. (let ((s 0))
  13020. (while (string-match "/+" match s)
  13021. (setq start (match-beginning 0))
  13022. (setq s (match-end 0))))
  13023. (if (and (string-match "/+" match start)
  13024. (not (string-match-p "\"" match start)))
  13025. ;; Match contains also a TODO-matching request.
  13026. (progn
  13027. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13028. (setq todomatch (substring match (match-end 0)))
  13029. (when (string-prefix-p "!" todomatch)
  13030. (setq org--matcher-tags-todo-only t)
  13031. (setq todomatch (substring todomatch 1)))
  13032. (when (string-match "\\`\\s-*\\'" todomatch)
  13033. (setq todomatch nil)))
  13034. ;; Only matching tags.
  13035. (setq tagsmatch match)
  13036. (setq todomatch nil))
  13037. ;; Make the tags matcher.
  13038. (when (org-string-nw-p tagsmatch)
  13039. (let ((orlist nil)
  13040. (orterms (org-split-string tagsmatch "|"))
  13041. term)
  13042. (while (setq term (pop orterms))
  13043. (while (and (equal (substring term -1) "\\") orterms)
  13044. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13045. (while (string-match re term)
  13046. (let* ((rest (substring term (match-end 0)))
  13047. (minus (and (match-end 1)
  13048. (equal (match-string 1 term) "-")))
  13049. (tag (save-match-data
  13050. (replace-regexp-in-string
  13051. "\\\\-" "-" (match-string 2 term))))
  13052. (regexp (eq (string-to-char tag) ?{))
  13053. (levelp (match-end 4))
  13054. (propp (match-end 5))
  13055. (mm
  13056. (cond
  13057. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13058. (levelp
  13059. `(,(org-op-to-function (match-string 3 term))
  13060. level
  13061. ,(string-to-number (match-string 4 term))))
  13062. (propp
  13063. (let* ((gv (pcase (upcase (match-string 5 term))
  13064. ("CATEGORY"
  13065. '(get-text-property (point) 'org-category))
  13066. ("TODO" 'todo)
  13067. (p `(org-cached-entry-get nil ,p))))
  13068. (pv (match-string 7 term))
  13069. (regexp (eq (string-to-char pv) ?{))
  13070. (strp (eq (string-to-char pv) ?\"))
  13071. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13072. (po (org-op-to-function (match-string 6 term)
  13073. (if timep 'time strp))))
  13074. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13075. (when timep (setq pv (org-matcher-time pv)))
  13076. (cond ((and regexp (eq po 'org<>))
  13077. `(not (string-match ,pv (or ,gv ""))))
  13078. (regexp `(string-match ,pv (or ,gv "")))
  13079. (strp `(,po (or ,gv "") ,pv))
  13080. (t
  13081. `(,po
  13082. (string-to-number (or ,gv ""))
  13083. ,(string-to-number pv))))))
  13084. (t `(member ,tag tags-list)))))
  13085. (push (if minus `(not ,mm) mm) tagsmatcher)
  13086. (setq term rest)))
  13087. (push `(and ,@tagsmatcher) orlist)
  13088. (setq tagsmatcher nil))
  13089. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13090. ;; Make the TODO matcher.
  13091. (when (org-string-nw-p todomatch)
  13092. (let ((orlist nil))
  13093. (dolist (term (org-split-string todomatch "|"))
  13094. (while (string-match re term)
  13095. (let* ((minus (and (match-end 1)
  13096. (equal (match-string 1 term) "-")))
  13097. (kwd (match-string 2 term))
  13098. (regexp (eq (string-to-char kwd) ?{))
  13099. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13100. `(equal todo ,kwd))))
  13101. (push (if minus `(not ,mm) mm) todomatcher))
  13102. (setq term (substring term (match-end 0))))
  13103. (push (if (> (length todomatcher) 1)
  13104. (cons 'and todomatcher)
  13105. (car todomatcher))
  13106. orlist)
  13107. (setq todomatcher nil))
  13108. (setq todomatcher (cons 'or orlist))))
  13109. ;; Return the string and function of the matcher. If no
  13110. ;; tags-specific or todo-specific matcher exists, match
  13111. ;; everything.
  13112. (let ((matcher (if (and tagsmatcher todomatcher)
  13113. `(and ,tagsmatcher ,todomatcher)
  13114. (or tagsmatcher todomatcher t))))
  13115. (when org--matcher-tags-todo-only
  13116. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13117. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13118. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13119. "Expand group tags in MATCH.
  13120. This replaces every group tag in MATCH with a regexp tag search.
  13121. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13122. will be replaced like this:
  13123. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13124. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13125. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13126. Replacing by a regexp preserves the structure of the match.
  13127. E.g., this expansion
  13128. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13129. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13130. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13131. A group tag in MATCH can contain regular expressions of its own.
  13132. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13133. will be replaced like this:
  13134. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13135. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13136. assumed to be a single group tag, and the function will return
  13137. the list of tags in this group.
  13138. When DOWNCASE is non-nil, expand downcased TAGS."
  13139. (if org-group-tags
  13140. (let* ((case-fold-search t)
  13141. (stable org-mode-syntax-table)
  13142. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13143. (taggroups (if downcased
  13144. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13145. taggroups)
  13146. taggroups))
  13147. (taggroups-keys (mapcar #'car taggroups))
  13148. (return-match (if downcased (downcase match) match))
  13149. (count 0)
  13150. (work-already-expanded tags-already-expanded)
  13151. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13152. ;; @ and _ are allowed as word-components in tags.
  13153. (modify-syntax-entry ?@ "w" stable)
  13154. (modify-syntax-entry ?_ "w" stable)
  13155. ;; Temporarily replace regexp-expressions in the match-expression.
  13156. (while (string-match "{.+?}" return-match)
  13157. (cl-incf count)
  13158. (push (match-string 0 return-match) regexps-in-match)
  13159. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13160. (while (and taggroups-keys
  13161. (with-syntax-table stable
  13162. (string-match
  13163. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13164. (regexp-opt taggroups-keys) "\\>\\)")
  13165. return-match)))
  13166. (let* ((dir (match-string 1 return-match))
  13167. (tag (match-string 2 return-match))
  13168. (tag (if downcased (downcase tag) tag)))
  13169. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13170. (member tag work-already-expanded))
  13171. (setq tags-in-group (assoc tag taggroups))
  13172. (push tag work-already-expanded)
  13173. ;; Recursively expand each tag in the group, if the tag hasn't
  13174. ;; already been expanded. Restore the match-data after all recursive calls.
  13175. (save-match-data
  13176. (let (tags-expanded)
  13177. (dolist (x (cdr tags-in-group))
  13178. (if (and (member x taggroups-keys)
  13179. (not (member x work-already-expanded)))
  13180. (setq tags-expanded
  13181. (delete-dups
  13182. (append
  13183. (org-tags-expand x t downcased
  13184. work-already-expanded)
  13185. tags-expanded)))
  13186. (setq tags-expanded
  13187. (append (list x) tags-expanded)))
  13188. (setq work-already-expanded
  13189. (delete-dups
  13190. (append tags-expanded
  13191. work-already-expanded))))
  13192. (setq tags-in-group
  13193. (delete-dups (cons (car tags-in-group)
  13194. tags-expanded)))))
  13195. ;; Filter tag-regexps from tags.
  13196. (setq regexp-in-group-escaped
  13197. (delq nil (mapcar (lambda (x)
  13198. (if (stringp x)
  13199. (and (equal "{" (substring x 0 1))
  13200. (equal "}" (substring x -1))
  13201. x)
  13202. x))
  13203. tags-in-group))
  13204. regexp-in-group
  13205. (mapcar (lambda (x)
  13206. (substring x 1 -1))
  13207. regexp-in-group-escaped)
  13208. tags-in-group
  13209. (delq nil (mapcar (lambda (x)
  13210. (if (stringp x)
  13211. (and (not (equal "{" (substring x 0 1)))
  13212. (not (equal "}" (substring x -1)))
  13213. x)
  13214. x))
  13215. tags-in-group)))
  13216. ;; If single-as-list, do no more in the while-loop.
  13217. (if (not single-as-list)
  13218. (progn
  13219. (when regexp-in-group
  13220. (setq regexp-in-group
  13221. (concat "\\|"
  13222. (mapconcat 'identity regexp-in-group
  13223. "\\|"))))
  13224. (setq tags-in-group
  13225. (concat dir
  13226. "{\\<"
  13227. (regexp-opt tags-in-group)
  13228. "\\>"
  13229. regexp-in-group
  13230. "}"))
  13231. (when (stringp tags-in-group)
  13232. (org-add-props tags-in-group '(grouptag t)))
  13233. (setq return-match
  13234. (replace-match tags-in-group t t return-match)))
  13235. (setq tags-in-group
  13236. (append regexp-in-group-escaped tags-in-group))))
  13237. (setq taggroups-keys (delete tag taggroups-keys))))
  13238. ;; Add the regular expressions back into the match-expression again.
  13239. (while regexps-in-match
  13240. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13241. (pop regexps-in-match)
  13242. return-match t t))
  13243. (cl-decf count))
  13244. (if single-as-list
  13245. (if tags-in-group tags-in-group (list return-match))
  13246. return-match))
  13247. (if single-as-list
  13248. (list (if downcased (downcase match) match))
  13249. match)))
  13250. (defun org-op-to-function (op &optional stringp)
  13251. "Turn an operator into the appropriate function."
  13252. (setq op
  13253. (cond
  13254. ((equal op "<" ) '(< string< org-time<))
  13255. ((equal op ">" ) '(> org-string> org-time>))
  13256. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13257. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13258. ((member op '("=" "==")) '(= string= org-time=))
  13259. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13260. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13261. (defun org<> (a b) (not (= a b)))
  13262. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13263. (defun org-string>= (a b) (not (string< a b)))
  13264. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13265. (defun org-string<> (a b) (not (string= a b)))
  13266. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13267. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13268. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13269. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13270. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13271. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13272. (defun org-2ft (s)
  13273. "Convert S to a floating point time.
  13274. If S is already a number, just return it. If it is a string, parse
  13275. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13276. (cond
  13277. ((numberp s) s)
  13278. ((stringp s)
  13279. (condition-case nil
  13280. (float-time (apply #'encode-time (org-parse-time-string s nil t)))
  13281. (error 0.)))
  13282. (t 0.)))
  13283. (defun org-time-today ()
  13284. "Time in seconds today at 0:00.
  13285. Returns the float number of seconds since the beginning of the
  13286. epoch to the beginning of today (00:00)."
  13287. (float-time (apply 'encode-time
  13288. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13289. (defun org-matcher-time (s)
  13290. "Interpret a time comparison value."
  13291. (save-match-data
  13292. (cond
  13293. ((string= s "<now>") (float-time))
  13294. ((string= s "<today>") (org-time-today))
  13295. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13296. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13297. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13298. (+ (org-time-today)
  13299. (* (string-to-number (match-string 1 s))
  13300. (cdr (assoc (match-string 2 s)
  13301. '(("d" . 86400.0) ("w" . 604800.0)
  13302. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13303. (t (org-2ft s)))))
  13304. (defun org-match-any-p (re list)
  13305. "Does re match any element of list?"
  13306. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13307. (delq nil list))
  13308. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13309. (defvar org-tags-overlay (make-overlay 1 1))
  13310. (delete-overlay org-tags-overlay)
  13311. (defun org-get-local-tags-at (&optional pos)
  13312. "Get a list of tags defined in the current headline."
  13313. (org-get-tags-at pos 'local))
  13314. (defun org-get-local-tags ()
  13315. "Get a list of tags defined in the current headline."
  13316. (org-get-tags-at nil 'local))
  13317. (defun org-get-tags-at (&optional pos local)
  13318. "Get a list of all headline tags applicable at POS.
  13319. POS defaults to point. If tags are inherited, the list contains
  13320. the targets in the same sequence as the headlines appear, i.e.
  13321. the tags of the current headline come last.
  13322. When LOCAL is non-nil, only return tags from the current headline,
  13323. ignore inherited ones."
  13324. (interactive)
  13325. (if (and org-trust-scanner-tags
  13326. (or (not pos) (equal pos (point)))
  13327. (not local))
  13328. org-scanner-tags
  13329. (let (tags ltags lastpos parent)
  13330. (save-excursion
  13331. (save-restriction
  13332. (widen)
  13333. (goto-char (or pos (point)))
  13334. (save-match-data
  13335. (catch 'done
  13336. (condition-case nil
  13337. (progn
  13338. (org-back-to-heading t)
  13339. (while (not (equal lastpos (point)))
  13340. (setq lastpos (point))
  13341. (when (looking-at ".+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13342. (setq ltags (org-split-string
  13343. (match-string-no-properties 1) ":"))
  13344. (when parent
  13345. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13346. (setq tags (append
  13347. (if parent
  13348. (org-remove-uninherited-tags ltags)
  13349. ltags)
  13350. tags)))
  13351. (or org-use-tag-inheritance (throw 'done t))
  13352. (when local (throw 'done t))
  13353. (or (org-up-heading-safe) (error nil))
  13354. (setq parent t)))
  13355. (error nil)))))
  13356. (if local
  13357. tags
  13358. (reverse (delete-dups
  13359. (reverse (append
  13360. (org-remove-uninherited-tags
  13361. org-file-tags)
  13362. tags)))))))))
  13363. (defun org-add-prop-inherited (s)
  13364. (add-text-properties 0 (length s) '(inherited t) s)
  13365. s)
  13366. (defun org-toggle-tag (tag &optional onoff)
  13367. "Toggle the tag TAG for the current line.
  13368. If ONOFF is `on' or `off', don't toggle but set to this state."
  13369. (save-excursion
  13370. (org-back-to-heading t)
  13371. (let ((current
  13372. (when (re-search-forward "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$"
  13373. (line-end-position) t)
  13374. (let ((tags (match-string 1)))
  13375. ;; Clear current tags.
  13376. (replace-match "")
  13377. ;; Reverse the tags list so any new tag is appended to
  13378. ;; the current list of tags.
  13379. (nreverse (org-split-string tags ":")))))
  13380. res)
  13381. (pcase onoff
  13382. (`off (setq current (delete tag current)))
  13383. ((or `on (guard (not (member tag current))))
  13384. (setq res t)
  13385. (cl-pushnew tag current :test #'equal))
  13386. (_ (setq current (delete tag current))))
  13387. (end-of-line)
  13388. (if current
  13389. (progn
  13390. (insert " :" (mapconcat #'identity (nreverse current) ":") ":")
  13391. (org-set-tags nil t))
  13392. (delete-horizontal-space))
  13393. (run-hooks 'org-after-tags-change-hook)
  13394. res)))
  13395. (defun org--align-tags-here (to-col)
  13396. "Align tags on the current headline to TO-COL.
  13397. Assume point is on a headline."
  13398. (let ((pos (point)))
  13399. (beginning-of-line)
  13400. (if (or (not (looking-at ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13401. (>= pos (match-beginning 2)))
  13402. ;; No tags or point within tags: do not align.
  13403. (goto-char pos)
  13404. (goto-char (match-beginning 1))
  13405. (let ((shift (max (- (if (>= to-col 0) to-col
  13406. (- (abs to-col) (string-width (match-string 2))))
  13407. (current-column))
  13408. 1)))
  13409. (replace-match (make-string shift ?\s) nil nil nil 1)
  13410. ;; Preserve initial position, if possible. In any case, stop
  13411. ;; before tags.
  13412. (when (< pos (point)) (goto-char pos))))))
  13413. (defun org-set-tags-command (&optional arg just-align)
  13414. "Call the set-tags command for the current entry."
  13415. (interactive "P")
  13416. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13417. (org-set-tags arg just-align)
  13418. (save-excursion
  13419. (unless (and (org-region-active-p)
  13420. org-loop-over-headlines-in-active-region)
  13421. (org-back-to-heading t))
  13422. (org-set-tags arg just-align))))
  13423. (defun org-set-tags-to (data)
  13424. "Set the tags of the current entry to DATA, replacing the current tags.
  13425. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13426. If DATA is nil or the empty string, any tags will be removed."
  13427. (interactive "sTags: ")
  13428. (setq data
  13429. (cond
  13430. ((eq data nil) "")
  13431. ((equal data "") "")
  13432. ((stringp data)
  13433. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13434. ":"))
  13435. ((listp data)
  13436. (concat ":" (mapconcat 'identity data ":") ":"))))
  13437. (when data
  13438. (save-excursion
  13439. (org-back-to-heading t)
  13440. (when (let ((case-fold-search nil))
  13441. (looking-at org-complex-heading-regexp))
  13442. (if (match-end 5)
  13443. (progn
  13444. (goto-char (match-beginning 5))
  13445. (insert data)
  13446. (delete-region (point) (point-at-eol))
  13447. (org-set-tags nil 'align))
  13448. (goto-char (point-at-eol))
  13449. (insert " " data)
  13450. (org-set-tags nil 'align)))
  13451. (beginning-of-line 1)
  13452. (when (looking-at ".*?\\([ \t]+\\)$")
  13453. (delete-region (match-beginning 1) (match-end 1))))))
  13454. (defun org-align-all-tags ()
  13455. "Align the tags in all headings."
  13456. (interactive)
  13457. (save-excursion
  13458. (or (ignore-errors (org-back-to-heading t))
  13459. (outline-next-heading))
  13460. (if (org-at-heading-p)
  13461. (org-set-tags t)
  13462. (message "No headings"))))
  13463. (defvar org-indent-indentation-per-level)
  13464. (defun org-set-tags (&optional arg just-align)
  13465. "Set the tags for the current headline.
  13466. With prefix ARG, realign all tags in headings in the current buffer.
  13467. When JUST-ALIGN is non-nil, only align tags."
  13468. (interactive "P")
  13469. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13470. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13471. 'region-start-level
  13472. 'region))
  13473. org-loop-over-headlines-in-active-region)
  13474. (org-map-entries
  13475. ;; We don't use ARG and JUST-ALIGN here because these args
  13476. ;; are not useful when looping over headlines.
  13477. #'org-set-tags
  13478. org-loop-over-headlines-in-active-region
  13479. cl
  13480. '(when (org-invisible-p) (org-end-of-subtree nil t))))
  13481. (let ((org-setting-tags t))
  13482. (if arg
  13483. (save-excursion
  13484. (goto-char (point-min))
  13485. (while (re-search-forward org-outline-regexp-bol nil t)
  13486. (org-set-tags nil t)
  13487. (end-of-line))
  13488. (message "All tags realigned to column %d" org-tags-column))
  13489. (let* ((current (org-get-tags-string))
  13490. (tags
  13491. (if just-align current
  13492. ;; Get a new set of tags from the user.
  13493. (save-excursion
  13494. (let* ((seen)
  13495. (table
  13496. (setq
  13497. org-last-tags-completion-table
  13498. ;; Uniquify tags in alists, yet preserve
  13499. ;; structure (i.e., keywords).
  13500. (delq nil
  13501. (mapcar
  13502. (lambda (pair)
  13503. (let ((head (car pair)))
  13504. (cond ((symbolp head) pair)
  13505. ((member head seen) nil)
  13506. (t (push head seen)
  13507. pair))))
  13508. (append
  13509. (or org-current-tag-alist
  13510. (org-get-buffer-tags))
  13511. (and
  13512. org-complete-tags-always-offer-all-agenda-tags
  13513. (org-global-tags-completion-table
  13514. (org-agenda-files))))))))
  13515. (current-tags (org-split-string current ":"))
  13516. (inherited-tags
  13517. (nreverse (nthcdr (length current-tags)
  13518. (nreverse (org-get-tags-at))))))
  13519. (replace-regexp-in-string
  13520. "\\([-+&]+\\|,\\)"
  13521. ":"
  13522. (if (or (eq t org-use-fast-tag-selection)
  13523. (and org-use-fast-tag-selection
  13524. (delq nil (mapcar #'cdr table))))
  13525. (org-fast-tag-selection
  13526. current-tags inherited-tags table
  13527. (and org-fast-tag-selection-include-todo
  13528. org-todo-key-alist))
  13529. (let ((org-add-colon-after-tag-completion
  13530. (< 1 (length table))))
  13531. (org-trim
  13532. (completing-read
  13533. "Tags: "
  13534. #'org-tags-completion-function
  13535. nil nil current 'org-tags-history))))))))))
  13536. (when org-tags-sort-function
  13537. (setq tags
  13538. (mapconcat
  13539. #'identity
  13540. (sort (org-split-string tags "[^[:alnum:]_@#%]+")
  13541. org-tags-sort-function)
  13542. ":")))
  13543. (if (or (string= ":" tags)
  13544. (string= "::" tags))
  13545. (setq tags ""))
  13546. (if (not (org-string-nw-p tags)) (setq tags "")
  13547. (unless (string-suffix-p ":" tags) (setq tags (concat tags ":")))
  13548. (unless (string-prefix-p ":" tags) (setq tags (concat ":" tags))))
  13549. ;; Insert new tags at the correct column.
  13550. (unless (equal current tags)
  13551. (save-excursion
  13552. (beginning-of-line)
  13553. (let ((case-fold-search nil))
  13554. (looking-at org-complex-heading-regexp))
  13555. ;; Remove current tags, if any.
  13556. (when (match-end 5) (replace-match "" nil nil nil 5))
  13557. ;; Insert new tags, if any. Otherwise, remove trailing
  13558. ;; white spaces.
  13559. (end-of-line)
  13560. (if (not (equal tags ""))
  13561. ;; When text is being inserted on an invisible
  13562. ;; region boundary, it can be inadvertently sucked
  13563. ;; into invisibility.
  13564. (outline-flag-region (point) (progn (insert " " tags) (point)) nil)
  13565. (skip-chars-backward " \t")
  13566. (delete-region (point) (line-end-position)))))
  13567. ;; Align tags, if any. Fix tags column if `org-indent-mode'
  13568. ;; is on.
  13569. (unless (equal tags "")
  13570. (let* ((level (save-excursion
  13571. (beginning-of-line)
  13572. (skip-chars-forward "\\*")))
  13573. (offset (if (bound-and-true-p org-indent-mode)
  13574. (* (1- org-indent-indentation-per-level)
  13575. (1- level))
  13576. 0))
  13577. (tags-column
  13578. (+ org-tags-column
  13579. (if (> org-tags-column 0) (- offset) offset))))
  13580. (org--align-tags-here tags-column))))
  13581. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13582. (defun org-change-tag-in-region (beg end tag off)
  13583. "Add or remove TAG for each entry in the region.
  13584. This works in the agenda, and also in an Org buffer."
  13585. (interactive
  13586. (list (region-beginning) (region-end)
  13587. (let ((org-last-tags-completion-table
  13588. (if (derived-mode-p 'org-mode)
  13589. (org-uniquify
  13590. (delq nil (append (org-get-buffer-tags)
  13591. (org-global-tags-completion-table))))
  13592. (org-global-tags-completion-table))))
  13593. (completing-read
  13594. "Tag: " 'org-tags-completion-function nil nil nil
  13595. 'org-tags-history))
  13596. (progn
  13597. (message "[s]et or [r]emove? ")
  13598. (equal (read-char-exclusive) ?r))))
  13599. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13600. (let ((agendap (equal major-mode 'org-agenda-mode))
  13601. l1 l2 m buf pos newhead (cnt 0))
  13602. (goto-char end)
  13603. (setq l2 (1- (org-current-line)))
  13604. (goto-char beg)
  13605. (setq l1 (org-current-line))
  13606. (cl-loop for l from l1 to l2 do
  13607. (org-goto-line l)
  13608. (setq m (get-text-property (point) 'org-hd-marker))
  13609. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13610. (and agendap m))
  13611. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13612. pos (if agendap m (point)))
  13613. (with-current-buffer buf
  13614. (save-excursion
  13615. (save-restriction
  13616. (goto-char pos)
  13617. (setq cnt (1+ cnt))
  13618. (org-toggle-tag tag (if off 'off 'on))
  13619. (setq newhead (org-get-heading)))))
  13620. (and agendap (org-agenda-change-all-lines newhead m))))
  13621. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13622. (defun org-tags-completion-function (string _predicate &optional flag)
  13623. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13624. (confirm (lambda (x) (stringp (car x)))))
  13625. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13626. (setq s1 (match-string 1 string)
  13627. s2 (match-string 2 string))
  13628. (setq s1 "" s2 string))
  13629. (cond
  13630. ((eq flag nil)
  13631. ;; try completion
  13632. (setq rtn (try-completion s2 ctable confirm))
  13633. (when (stringp rtn)
  13634. (setq rtn
  13635. (concat s1 s2 (substring rtn (length s2))
  13636. (if (and org-add-colon-after-tag-completion
  13637. (assoc rtn ctable))
  13638. ":" ""))))
  13639. rtn)
  13640. ((eq flag t)
  13641. ;; all-completions
  13642. (all-completions s2 ctable confirm))
  13643. ((eq flag 'lambda)
  13644. ;; exact match?
  13645. (assoc s2 ctable)))))
  13646. (defun org-fast-tag-insert (kwd tags face &optional end)
  13647. "Insert KDW, and the TAGS, the latter with face FACE.
  13648. Also insert END."
  13649. (insert (format "%-12s" (concat kwd ":"))
  13650. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13651. (or end "")))
  13652. (defun org-fast-tag-show-exit (flag)
  13653. (save-excursion
  13654. (org-goto-line 3)
  13655. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13656. (replace-match ""))
  13657. (when flag
  13658. (end-of-line 1)
  13659. (org-move-to-column (- (window-width) 19) t)
  13660. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13661. (defun org-set-current-tags-overlay (current prefix)
  13662. "Add an overlay to CURRENT tag with PREFIX."
  13663. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13664. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13665. (org-overlay-display org-tags-overlay (concat prefix s))))
  13666. (defvar org-last-tag-selection-key nil)
  13667. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13668. "Fast tag selection with single keys.
  13669. CURRENT is the current list of tags in the headline, INHERITED is the
  13670. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13671. possibly with grouping information. TODO-TABLE is a similar table with
  13672. TODO keywords, should these have keys assigned to them.
  13673. If the keys are nil, a-z are automatically assigned.
  13674. Returns the new tags string, or nil to not change the current settings."
  13675. (let* ((fulltable (append table todo-table))
  13676. (maxlen (apply 'max (mapcar
  13677. (lambda (x)
  13678. (if (stringp (car x)) (string-width (car x)) 0))
  13679. fulltable)))
  13680. (buf (current-buffer))
  13681. (expert (eq org-fast-tag-selection-single-key 'expert))
  13682. (buffer-tags nil)
  13683. (fwidth (+ maxlen 3 1 3))
  13684. (ncol (/ (- (window-width) 4) fwidth))
  13685. (i-face 'org-done)
  13686. (c-face 'org-todo)
  13687. tg cnt e c char c1 c2 ntable tbl rtn
  13688. ov-start ov-end ov-prefix
  13689. (exit-after-next org-fast-tag-selection-single-key)
  13690. (done-keywords org-done-keywords)
  13691. groups ingroup intaggroup)
  13692. (save-excursion
  13693. (beginning-of-line 1)
  13694. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13695. (setq ov-start (match-beginning 1)
  13696. ov-end (match-end 1)
  13697. ov-prefix "")
  13698. (setq ov-start (1- (point-at-eol))
  13699. ov-end (1+ ov-start))
  13700. (skip-chars-forward "^\n\r")
  13701. (setq ov-prefix
  13702. (concat
  13703. (buffer-substring (1- (point)) (point))
  13704. (if (> (current-column) org-tags-column)
  13705. " "
  13706. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13707. (move-overlay org-tags-overlay ov-start ov-end)
  13708. (save-window-excursion
  13709. (if expert
  13710. (set-buffer (get-buffer-create " *Org tags*"))
  13711. (delete-other-windows)
  13712. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13713. (org-switch-to-buffer-other-window " *Org tags*"))
  13714. (erase-buffer)
  13715. (setq-local org-done-keywords done-keywords)
  13716. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13717. (org-fast-tag-insert "Current" current c-face "\n\n")
  13718. (org-fast-tag-show-exit exit-after-next)
  13719. (org-set-current-tags-overlay current ov-prefix)
  13720. (setq tbl fulltable char ?a cnt 0)
  13721. (while (setq e (pop tbl))
  13722. (cond
  13723. ((eq (car e) :startgroup)
  13724. (push '() groups) (setq ingroup t)
  13725. (unless (zerop cnt)
  13726. (setq cnt 0)
  13727. (insert "\n"))
  13728. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13729. ((eq (car e) :endgroup)
  13730. (setq ingroup nil cnt 0)
  13731. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13732. ((eq (car e) :startgrouptag)
  13733. (setq intaggroup t)
  13734. (unless (zerop cnt)
  13735. (setq cnt 0)
  13736. (insert "\n"))
  13737. (insert "[ "))
  13738. ((eq (car e) :endgrouptag)
  13739. (setq intaggroup nil cnt 0)
  13740. (insert "]\n"))
  13741. ((equal e '(:newline))
  13742. (unless (zerop cnt)
  13743. (setq cnt 0)
  13744. (insert "\n")
  13745. (setq e (car tbl))
  13746. (while (equal (car tbl) '(:newline))
  13747. (insert "\n")
  13748. (setq tbl (cdr tbl)))))
  13749. ((equal e '(:grouptags)) (insert " : "))
  13750. (t
  13751. (setq tg (copy-sequence (car e)) c2 nil)
  13752. (if (cdr e)
  13753. (setq c (cdr e))
  13754. ;; automatically assign a character.
  13755. (setq c1 (string-to-char
  13756. (downcase (substring
  13757. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13758. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13759. (while (or (rassoc char ntable) (rassoc char table))
  13760. (setq char (1+ char)))
  13761. (setq c2 c1))
  13762. (setq c (or c2 char)))
  13763. (when ingroup (push tg (car groups)))
  13764. (setq tg (org-add-props tg nil 'face
  13765. (cond
  13766. ((not (assoc tg table))
  13767. (org-get-todo-face tg))
  13768. ((member tg current) c-face)
  13769. ((member tg inherited) i-face))))
  13770. (when (equal (caar tbl) :grouptags)
  13771. (org-add-props tg nil 'face 'org-tag-group))
  13772. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13773. (insert "[" c "] " tg (make-string
  13774. (- fwidth 4 (length tg)) ?\ ))
  13775. (push (cons tg c) ntable)
  13776. (when (= (cl-incf cnt) ncol)
  13777. (insert "\n")
  13778. (when (or ingroup intaggroup) (insert " "))
  13779. (setq cnt 0)))))
  13780. (setq ntable (nreverse ntable))
  13781. (insert "\n")
  13782. (goto-char (point-min))
  13783. (unless expert (org-fit-window-to-buffer))
  13784. (setq rtn
  13785. (catch 'exit
  13786. (while t
  13787. (message "[a-z..]:toggle [SPC]:clear [RET]:accept [TAB]:edit [!] %sgroups%s"
  13788. (if (not groups) "no " "")
  13789. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13790. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13791. (setq org-last-tag-selection-key c)
  13792. (cond
  13793. ((= c ?\r) (throw 'exit t))
  13794. ((= c ?!)
  13795. (setq groups (not groups))
  13796. (goto-char (point-min))
  13797. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13798. ((= c ?\C-c)
  13799. (if (not expert)
  13800. (org-fast-tag-show-exit
  13801. (setq exit-after-next (not exit-after-next)))
  13802. (setq expert nil)
  13803. (delete-other-windows)
  13804. (set-window-buffer (split-window-vertically) " *Org tags*")
  13805. (org-switch-to-buffer-other-window " *Org tags*")
  13806. (org-fit-window-to-buffer)))
  13807. ((or (= c ?\C-g)
  13808. (and (= c ?q) (not (rassoc c ntable))))
  13809. (delete-overlay org-tags-overlay)
  13810. (setq quit-flag t))
  13811. ((= c ?\ )
  13812. (setq current nil)
  13813. (when exit-after-next (setq exit-after-next 'now)))
  13814. ((= c ?\t)
  13815. (condition-case nil
  13816. (setq tg (completing-read
  13817. "Tag: "
  13818. (or buffer-tags
  13819. (with-current-buffer buf
  13820. (setq buffer-tags
  13821. (org-get-buffer-tags))))))
  13822. (quit (setq tg "")))
  13823. (when (string-match "\\S-" tg)
  13824. (cl-pushnew (list tg) buffer-tags :test #'equal)
  13825. (if (member tg current)
  13826. (setq current (delete tg current))
  13827. (push tg current)))
  13828. (when exit-after-next (setq exit-after-next 'now)))
  13829. ((setq e (rassoc c todo-table) tg (car e))
  13830. (with-current-buffer buf
  13831. (save-excursion (org-todo tg)))
  13832. (when exit-after-next (setq exit-after-next 'now)))
  13833. ((setq e (rassoc c ntable) tg (car e))
  13834. (if (member tg current)
  13835. (setq current (delete tg current))
  13836. (cl-loop for g in groups do
  13837. (when (member tg g)
  13838. (dolist (x g) (setq current (delete x current)))))
  13839. (push tg current))
  13840. (when exit-after-next (setq exit-after-next 'now))))
  13841. ;; Create a sorted list
  13842. (setq current
  13843. (sort current
  13844. (lambda (a b)
  13845. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13846. (when (eq exit-after-next 'now) (throw 'exit t))
  13847. (goto-char (point-min))
  13848. (beginning-of-line 2)
  13849. (delete-region (point) (point-at-eol))
  13850. (org-fast-tag-insert "Current" current c-face)
  13851. (org-set-current-tags-overlay current ov-prefix)
  13852. (while (re-search-forward "\\[.\\] \\([[:alnum:]_@#%]+\\)" nil t)
  13853. (setq tg (match-string 1))
  13854. (add-text-properties
  13855. (match-beginning 1) (match-end 1)
  13856. (list 'face
  13857. (cond
  13858. ((member tg current) c-face)
  13859. ((member tg inherited) i-face)
  13860. (t (get-text-property (match-beginning 1) 'face))))))
  13861. (goto-char (point-min)))))
  13862. (delete-overlay org-tags-overlay)
  13863. (if rtn
  13864. (mapconcat 'identity current ":")
  13865. nil))))
  13866. (defun org-get-tags-string ()
  13867. "Get the TAGS string in the current headline."
  13868. (unless (org-at-heading-p t)
  13869. (user-error "Not on a heading"))
  13870. (save-excursion
  13871. (beginning-of-line 1)
  13872. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13873. (match-string-no-properties 1)
  13874. "")))
  13875. (defun org-get-tags ()
  13876. "Get the list of tags specified in the current headline."
  13877. (org-split-string (org-get-tags-string) ":"))
  13878. (defun org-get-buffer-tags ()
  13879. "Get a table of all tags used in the buffer, for completion."
  13880. (org-with-wide-buffer
  13881. (goto-char (point-min))
  13882. (let ((tag-re (concat org-outline-regexp-bol
  13883. "\\(?:.*?[ \t]\\)?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13884. tags)
  13885. (while (re-search-forward tag-re nil t)
  13886. (dolist (tag (org-split-string (match-string-no-properties 1) ":"))
  13887. (push tag tags)))
  13888. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13889. ;;;; The mapping API
  13890. (defvar org-agenda-skip-comment-trees)
  13891. (defvar org-agenda-skip-function)
  13892. (defun org-map-entries (func &optional match scope &rest skip)
  13893. "Call FUNC at each headline selected by MATCH in SCOPE.
  13894. FUNC is a function or a lisp form. The function will be called without
  13895. arguments, with the cursor positioned at the beginning of the headline.
  13896. The return values of all calls to the function will be collected and
  13897. returned as a list.
  13898. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13899. does not need to preserve point. After evaluation, the cursor will be
  13900. moved to the end of the line (presumably of the headline of the
  13901. processed entry) and search continues from there. Under some
  13902. circumstances, this may not produce the wanted results. For example,
  13903. if you have removed (e.g. archived) the current (sub)tree it could
  13904. mean that the next entry will be skipped entirely. In such cases, you
  13905. can specify the position from where search should continue by making
  13906. FUNC set the variable `org-map-continue-from' to the desired buffer
  13907. position.
  13908. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13909. Only headlines that are matched by this query will be considered during
  13910. the iteration. When MATCH is nil or t, all headlines will be
  13911. visited by the iteration.
  13912. SCOPE determines the scope of this command. It can be any of:
  13913. nil The current buffer, respecting the restriction if any
  13914. tree The subtree started with the entry at point
  13915. region The entries within the active region, if any
  13916. region-start-level
  13917. The entries within the active region, but only those at
  13918. the same level than the first one.
  13919. file The current buffer, without restriction
  13920. file-with-archives
  13921. The current buffer, and any archives associated with it
  13922. agenda All agenda files
  13923. agenda-with-archives
  13924. All agenda files with any archive files associated with them
  13925. \(file1 file2 ...)
  13926. If this is a list, all files in the list will be scanned
  13927. The remaining args are treated as settings for the skipping facilities of
  13928. the scanner. The following items can be given here:
  13929. archive skip trees with the archive tag
  13930. comment skip trees with the COMMENT keyword
  13931. function or Emacs Lisp form:
  13932. will be used as value for `org-agenda-skip-function', so
  13933. whenever the function returns a position, FUNC will not be
  13934. called for that entry and search will continue from the
  13935. position returned
  13936. If your function needs to retrieve the tags including inherited tags
  13937. at the *current* entry, you can use the value of the variable
  13938. `org-scanner-tags' which will be much faster than getting the value
  13939. with `org-get-tags-at'. If your function gets properties with
  13940. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13941. to t around the call to `org-entry-properties' to get the same speedup.
  13942. Note that if your function moves around to retrieve tags and properties at
  13943. a *different* entry, you cannot use these techniques."
  13944. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13945. (not (org-region-active-p)))
  13946. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13947. (org-agenda-skip-archived-trees (memq 'archive skip))
  13948. (org-agenda-skip-comment-trees (memq 'comment skip))
  13949. (org-agenda-skip-function
  13950. (car (org-delete-all '(comment archive) skip)))
  13951. (org-tags-match-list-sublevels t)
  13952. (start-level (eq scope 'region-start-level))
  13953. matcher res
  13954. org-todo-keywords-for-agenda
  13955. org-done-keywords-for-agenda
  13956. org-todo-keyword-alist-for-agenda
  13957. org-tag-alist-for-agenda
  13958. org--matcher-tags-todo-only)
  13959. (cond
  13960. ((eq match t) (setq matcher t))
  13961. ((eq match nil) (setq matcher t))
  13962. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13963. (save-excursion
  13964. (save-restriction
  13965. (cond ((eq scope 'tree)
  13966. (org-back-to-heading t)
  13967. (org-narrow-to-subtree)
  13968. (setq scope nil))
  13969. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13970. (org-region-active-p))
  13971. ;; If needed, set start-level to a string like "2"
  13972. (when start-level
  13973. (save-excursion
  13974. (goto-char (region-beginning))
  13975. (unless (org-at-heading-p) (outline-next-heading))
  13976. (setq start-level (org-current-level))))
  13977. (narrow-to-region (region-beginning)
  13978. (save-excursion
  13979. (goto-char (region-end))
  13980. (unless (and (bolp) (org-at-heading-p))
  13981. (outline-next-heading))
  13982. (point)))
  13983. (setq scope nil)))
  13984. (if (not scope)
  13985. (progn
  13986. (org-agenda-prepare-buffers
  13987. (and buffer-file-name (list buffer-file-name)))
  13988. (setq res
  13989. (org-scan-tags
  13990. func matcher org--matcher-tags-todo-only start-level)))
  13991. ;; Get the right scope
  13992. (cond
  13993. ((and scope (listp scope) (symbolp (car scope)))
  13994. (setq scope (eval scope)))
  13995. ((eq scope 'agenda)
  13996. (setq scope (org-agenda-files t)))
  13997. ((eq scope 'agenda-with-archives)
  13998. (setq scope (org-agenda-files t))
  13999. (setq scope (org-add-archive-files scope)))
  14000. ((eq scope 'file)
  14001. (setq scope (and buffer-file-name (list buffer-file-name))))
  14002. ((eq scope 'file-with-archives)
  14003. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14004. (org-agenda-prepare-buffers scope)
  14005. (dolist (file scope)
  14006. (with-current-buffer (org-find-base-buffer-visiting file)
  14007. (org-with-wide-buffer
  14008. (goto-char (point-min))
  14009. (setq res
  14010. (append
  14011. res
  14012. (org-scan-tags
  14013. func matcher org--matcher-tags-todo-only)))))))))
  14014. res)))
  14015. ;;; Properties API
  14016. (defconst org-special-properties
  14017. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14018. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14019. "The special properties valid in Org mode.
  14020. These are properties that are not defined in the property drawer,
  14021. but in some other way.")
  14022. (defconst org-default-properties
  14023. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14024. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14025. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14026. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14027. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14028. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14029. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14030. "Some properties that are used by Org mode for various purposes.
  14031. Being in this list makes sure that they are offered for completion.")
  14032. (defun org--valid-property-p (property)
  14033. "Non nil when string PROPERTY is a valid property name."
  14034. (not
  14035. (or (equal property "")
  14036. (string-match-p "\\s-" property))))
  14037. (defun org--update-property-plist (key val props)
  14038. "Associate KEY to VAL in alist PROPS.
  14039. Modifications are made by side-effect. Return new alist."
  14040. (let* ((appending (string= (substring key -1) "+"))
  14041. (key (if appending (substring key 0 -1) key))
  14042. (old (assoc-string key props t)))
  14043. (if (not old) (cons (cons key val) props)
  14044. (setcdr old (if appending (concat (cdr old) " " val) val))
  14045. props)))
  14046. (defun org-get-property-block (&optional beg force)
  14047. "Return the (beg . end) range of the body of the property drawer.
  14048. BEG is the beginning of the current subtree, or of the part
  14049. before the first headline. If it is not given, it will be found.
  14050. If the drawer does not exist, create it if FORCE is non-nil, or
  14051. return nil."
  14052. (org-with-wide-buffer
  14053. (when beg (goto-char beg))
  14054. (unless (org-before-first-heading-p)
  14055. (let ((beg (cond (beg)
  14056. ((or (not (featurep 'org-inlinetask))
  14057. (org-inlinetask-in-task-p))
  14058. (org-back-to-heading t))
  14059. (t (org-with-limited-levels (org-back-to-heading t))))))
  14060. (forward-line)
  14061. (when (looking-at-p org-planning-line-re) (forward-line))
  14062. (cond ((looking-at org-property-drawer-re)
  14063. (forward-line)
  14064. (cons (point) (progn (goto-char (match-end 0))
  14065. (line-beginning-position))))
  14066. (force
  14067. (goto-char beg)
  14068. (org-insert-property-drawer)
  14069. (let ((pos (save-excursion (search-forward ":END:")
  14070. (line-beginning-position))))
  14071. (cons pos pos))))))))
  14072. (defun org-at-property-p ()
  14073. "Non-nil when point is inside a property drawer.
  14074. See `org-property-re' for match data, if applicable."
  14075. (save-excursion
  14076. (beginning-of-line)
  14077. (and (looking-at org-property-re)
  14078. (let ((property-drawer (save-match-data (org-get-property-block))))
  14079. (and property-drawer
  14080. (>= (point) (car property-drawer))
  14081. (< (point) (cdr property-drawer)))))))
  14082. (defun org-property-action ()
  14083. "Do an action on properties."
  14084. (interactive)
  14085. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14086. (let ((c (read-char-exclusive)))
  14087. (cl-case c
  14088. (?s (call-interactively #'org-set-property))
  14089. (?d (call-interactively #'org-delete-property))
  14090. (?D (call-interactively #'org-delete-property-globally))
  14091. (?c (call-interactively #'org-compute-property-at-point))
  14092. (otherwise (user-error "No such property action %c" c)))))
  14093. (defun org-inc-effort ()
  14094. "Increment the value of the effort property in the current entry."
  14095. (interactive)
  14096. (org-set-effort nil t))
  14097. (defvar org-clock-effort) ; Defined in org-clock.el.
  14098. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14099. (defun org-set-effort (&optional value increment)
  14100. "Set the effort property of the current entry.
  14101. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14102. 10th allowed value.
  14103. When INCREMENT is non-nil, set the property to the next allowed value."
  14104. (interactive "P")
  14105. (when (equal value 0) (setq value 10))
  14106. (let* ((completion-ignore-case t)
  14107. (prop org-effort-property)
  14108. (cur (org-entry-get nil prop))
  14109. (allowed (org-property-get-allowed-values nil prop 'table))
  14110. (existing (mapcar 'list (org-property-values prop)))
  14111. (heading (nth 4 (org-heading-components)))
  14112. rpl
  14113. (val (cond
  14114. ((stringp value) value)
  14115. ((and allowed (integerp value))
  14116. (or (car (nth (1- value) allowed))
  14117. (car (org-last allowed))))
  14118. ((and allowed increment)
  14119. (or (cl-caadr (member (list cur) allowed))
  14120. (user-error "Allowed effort values are not set")))
  14121. (allowed
  14122. (message "Select 1-9,0, [RET%s]: %s"
  14123. (if cur (concat "=" cur) "")
  14124. (mapconcat 'car allowed " "))
  14125. (setq rpl (read-char-exclusive))
  14126. (if (equal rpl ?\r)
  14127. cur
  14128. (setq rpl (- rpl ?0))
  14129. (when (equal rpl 0) (setq rpl 10))
  14130. (if (and (> rpl 0) (<= rpl (length allowed)))
  14131. (car (nth (1- rpl) allowed))
  14132. (org-completing-read "Effort: " allowed nil))))
  14133. (t
  14134. (org-completing-read
  14135. (concat "Effort" (and cur (string-match "\\S-" cur)
  14136. (concat " [" cur "]"))
  14137. ": ")
  14138. existing nil nil "" nil cur)))))
  14139. (unless (equal (org-entry-get nil prop) val)
  14140. (org-entry-put nil prop val))
  14141. (org-refresh-property
  14142. '((effort . identity)
  14143. (effort-minutes . org-duration-to-minutes))
  14144. val)
  14145. (when (equal heading (bound-and-true-p org-clock-current-task))
  14146. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14147. (org-clock-update-mode-line))
  14148. (message "%s is now %s" prop val)))
  14149. (defun org-entry-properties (&optional pom which)
  14150. "Get all properties of the current entry.
  14151. When POM is a buffer position, get all properties from the entry
  14152. there instead.
  14153. This includes the TODO keyword, the tags, time strings for
  14154. deadline, scheduled, and clocking, and any additional properties
  14155. defined in the entry.
  14156. If WHICH is nil or `all', get all properties. If WHICH is
  14157. `special' or `standard', only get that subclass. If WHICH is
  14158. a string, only get that property.
  14159. Return value is an alist. Keys are properties, as upcased
  14160. strings."
  14161. (org-with-point-at pom
  14162. (when (and (derived-mode-p 'org-mode)
  14163. (ignore-errors (org-back-to-heading t)))
  14164. (catch 'exit
  14165. (let* ((beg (point))
  14166. (specific (and (stringp which) (upcase which)))
  14167. (which (cond ((not specific) which)
  14168. ((member specific org-special-properties) 'special)
  14169. (t 'standard)))
  14170. props)
  14171. ;; Get the special properties, like TODO and TAGS.
  14172. (when (memq which '(nil all special))
  14173. (when (or (not specific) (string= specific "CLOCKSUM"))
  14174. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14175. (when clocksum
  14176. (push (cons "CLOCKSUM" (org-duration-from-minutes clocksum))
  14177. props)))
  14178. (when specific (throw 'exit props)))
  14179. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14180. (let ((clocksumt (get-text-property (point)
  14181. :org-clock-minutes-today)))
  14182. (when clocksumt
  14183. (push (cons "CLOCKSUM_T"
  14184. (org-duration-from-minutes clocksumt))
  14185. props)))
  14186. (when specific (throw 'exit props)))
  14187. (when (or (not specific) (string= specific "ITEM"))
  14188. (let ((case-fold-search nil))
  14189. (when (looking-at org-complex-heading-regexp)
  14190. (push (cons "ITEM"
  14191. (let ((title (match-string-no-properties 4)))
  14192. (if (org-string-nw-p title)
  14193. (org-remove-tabs title)
  14194. "")))
  14195. props)))
  14196. (when specific (throw 'exit props)))
  14197. (when (or (not specific) (string= specific "TODO"))
  14198. (let ((case-fold-search nil))
  14199. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14200. (push (cons "TODO" (match-string-no-properties 2)) props)))
  14201. (when specific (throw 'exit props)))
  14202. (when (or (not specific) (string= specific "PRIORITY"))
  14203. (push (cons "PRIORITY"
  14204. (if (looking-at org-priority-regexp)
  14205. (match-string-no-properties 2)
  14206. (char-to-string org-default-priority)))
  14207. props)
  14208. (when specific (throw 'exit props)))
  14209. (when (or (not specific) (string= specific "FILE"))
  14210. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14211. props)
  14212. (when specific (throw 'exit props)))
  14213. (when (or (not specific) (string= specific "TAGS"))
  14214. (let ((value (org-string-nw-p (org-get-tags-string))))
  14215. (when value (push (cons "TAGS" value) props)))
  14216. (when specific (throw 'exit props)))
  14217. (when (or (not specific) (string= specific "ALLTAGS"))
  14218. (let ((value (org-get-tags-at)))
  14219. (when value
  14220. (push (cons "ALLTAGS"
  14221. (format ":%s:" (mapconcat #'identity value ":")))
  14222. props)))
  14223. (when specific (throw 'exit props)))
  14224. (when (or (not specific) (string= specific "BLOCKED"))
  14225. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14226. (when specific (throw 'exit props)))
  14227. (when (or (not specific)
  14228. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14229. (forward-line)
  14230. (when (looking-at-p org-planning-line-re)
  14231. (end-of-line)
  14232. (let ((bol (line-beginning-position))
  14233. ;; Backward compatibility: time keywords used to
  14234. ;; be configurable (before 8.3). Make sure we
  14235. ;; get the correct keyword.
  14236. (key-assoc `(("CLOSED" . ,org-closed-string)
  14237. ("DEADLINE" . ,org-deadline-string)
  14238. ("SCHEDULED" . ,org-scheduled-string))))
  14239. (dolist (pair (if specific (list (assoc specific key-assoc))
  14240. key-assoc))
  14241. (save-excursion
  14242. (when (search-backward (cdr pair) bol t)
  14243. (goto-char (match-end 0))
  14244. (skip-chars-forward " \t")
  14245. (and (looking-at org-ts-regexp-both)
  14246. (push (cons (car pair)
  14247. (match-string-no-properties 0))
  14248. props)))))))
  14249. (when specific (throw 'exit props)))
  14250. (when (or (not specific)
  14251. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14252. (let ((find-ts
  14253. (lambda (end ts)
  14254. ;; Fix next time-stamp before END. TS is the
  14255. ;; list of time-stamps found so far.
  14256. (let ((ts ts)
  14257. (regexp (cond
  14258. ((string= specific "TIMESTAMP")
  14259. org-ts-regexp)
  14260. ((string= specific "TIMESTAMP_IA")
  14261. org-ts-regexp-inactive)
  14262. ((assoc "TIMESTAMP_IA" ts)
  14263. org-ts-regexp)
  14264. ((assoc "TIMESTAMP" ts)
  14265. org-ts-regexp-inactive)
  14266. (t org-ts-regexp-both))))
  14267. (catch 'next
  14268. (while (re-search-forward regexp end t)
  14269. (backward-char)
  14270. (let ((object (org-element-context)))
  14271. ;; Accept to match timestamps in node
  14272. ;; properties, too.
  14273. (when (memq (org-element-type object)
  14274. '(node-property timestamp))
  14275. (let ((type
  14276. (org-element-property :type object)))
  14277. (cond
  14278. ((and (memq type '(active active-range))
  14279. (not (equal specific "TIMESTAMP_IA")))
  14280. (unless (assoc "TIMESTAMP" ts)
  14281. (push (cons "TIMESTAMP"
  14282. (org-element-property
  14283. :raw-value object))
  14284. ts)
  14285. (when specific (throw 'exit ts))))
  14286. ((and (memq type '(inactive inactive-range))
  14287. (not (string= specific "TIMESTAMP")))
  14288. (unless (assoc "TIMESTAMP_IA" ts)
  14289. (push (cons "TIMESTAMP_IA"
  14290. (org-element-property
  14291. :raw-value object))
  14292. ts)
  14293. (when specific (throw 'exit ts))))))
  14294. ;; Both timestamp types are found,
  14295. ;; move to next part.
  14296. (when (= (length ts) 2) (throw 'next ts)))))
  14297. ts)))))
  14298. (goto-char beg)
  14299. ;; First look for timestamps within headline.
  14300. (let ((ts (funcall find-ts (line-end-position) nil)))
  14301. (if (= (length ts) 2) (setq props (nconc ts props))
  14302. ;; Then find timestamps in the section, skipping
  14303. ;; planning line.
  14304. (let ((end (save-excursion (outline-next-heading))))
  14305. (forward-line)
  14306. (when (looking-at-p org-planning-line-re) (forward-line))
  14307. (setq props (nconc (funcall find-ts end ts) props))))))))
  14308. ;; Get the standard properties, like :PROP:.
  14309. (when (memq which '(nil all standard))
  14310. ;; If we are looking after a specific property, delegate
  14311. ;; to `org-entry-get', which is faster. However, make an
  14312. ;; exception for "CATEGORY", since it can be also set
  14313. ;; through keywords (i.e. #+CATEGORY).
  14314. (if (and specific (not (equal specific "CATEGORY")))
  14315. (let ((value (org-entry-get beg specific nil t)))
  14316. (throw 'exit (and value (list (cons specific value)))))
  14317. (let ((range (org-get-property-block beg)))
  14318. (when range
  14319. (let ((end (cdr range)) seen-base)
  14320. (goto-char (car range))
  14321. ;; Unlike to `org--update-property-plist', we
  14322. ;; handle the case where base values is found
  14323. ;; after its extension. We also forbid standard
  14324. ;; properties to be named as special properties.
  14325. (while (re-search-forward org-property-re end t)
  14326. (let* ((key (upcase (match-string-no-properties 2)))
  14327. (extendp (string-match-p "\\+\\'" key))
  14328. (key-base (if extendp (substring key 0 -1) key))
  14329. (value (match-string-no-properties 3)))
  14330. (cond
  14331. ((member-ignore-case key-base org-special-properties))
  14332. (extendp
  14333. (setq props
  14334. (org--update-property-plist key value props)))
  14335. ((member key seen-base))
  14336. (t (push key seen-base)
  14337. (let ((p (assoc-string key props t)))
  14338. (if p (setcdr p (concat value " " (cdr p)))
  14339. (push (cons key value) props))))))))))))
  14340. (unless (assoc "CATEGORY" props)
  14341. (push (cons "CATEGORY" (org-get-category beg)) props)
  14342. (when (string= specific "CATEGORY") (throw 'exit props)))
  14343. ;; Return value.
  14344. props)))))
  14345. (defun org--property-local-values (property literal-nil)
  14346. "Return value for PROPERTY in current entry.
  14347. Value is a list whose car is the base value for PROPERTY and cdr
  14348. a list of accumulated values. Return nil if neither is found in
  14349. the entry. Also return nil when PROPERTY is set to \"nil\",
  14350. unless LITERAL-NIL is non-nil."
  14351. (let ((range (org-get-property-block)))
  14352. (when range
  14353. (goto-char (car range))
  14354. (let* ((case-fold-search t)
  14355. (end (cdr range))
  14356. (value
  14357. ;; Base value.
  14358. (save-excursion
  14359. (let ((v (and (re-search-forward
  14360. (org-re-property property nil t) end t)
  14361. (match-string-no-properties 3))))
  14362. (list (if literal-nil v (org-not-nil v)))))))
  14363. ;; Find additional values.
  14364. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14365. (while (re-search-forward property+ end t)
  14366. (push (match-string-no-properties 3) value)))
  14367. ;; Return final values.
  14368. (and (not (equal value '(nil))) (nreverse value))))))
  14369. (defun org--property-global-value (property literal-nil)
  14370. "Return value for PROPERTY in current buffer.
  14371. Return value is a string. Return nil if property is not set
  14372. globally. Also return nil when PROPERTY is set to \"nil\",
  14373. unless LITERAL-NIL is non-nil."
  14374. (let ((global
  14375. (cdr (or (assoc-string property org-file-properties t)
  14376. (assoc-string property org-global-properties t)
  14377. (assoc-string property org-global-properties-fixed t)))))
  14378. (if literal-nil global (org-not-nil global))))
  14379. (defun org-entry-get (pom property &optional inherit literal-nil)
  14380. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14381. If INHERIT is non-nil and the entry does not have the property,
  14382. then also check higher levels of the hierarchy. If INHERIT is
  14383. the symbol `selective', use inheritance only if the setting in
  14384. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14385. If the property is present but empty, the return value is the
  14386. empty string. If the property is not present at all, nil is
  14387. returned. In any other case, return the value as a string.
  14388. Search is case-insensitive.
  14389. If LITERAL-NIL is set, return the string value \"nil\" as
  14390. a string, do not interpret it as the list atom nil. This is used
  14391. for inheritance when a \"nil\" value can supersede a non-nil
  14392. value higher up the hierarchy."
  14393. (org-with-point-at pom
  14394. (cond
  14395. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14396. ;; We need a special property. Use `org-entry-properties' to
  14397. ;; retrieve it, but specify the wanted property.
  14398. (cdr (assoc-string property (org-entry-properties nil property))))
  14399. ((and inherit
  14400. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14401. (org-entry-get-with-inheritance property literal-nil))
  14402. (t
  14403. (let* ((local (org--property-local-values property literal-nil))
  14404. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14405. (if literal-nil value (org-not-nil value)))))))
  14406. (defun org-property-or-variable-value (var &optional inherit)
  14407. "Check if there is a property fixing the value of VAR.
  14408. If yes, return this value. If not, return the current value of the variable."
  14409. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14410. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14411. (read prop)
  14412. (symbol-value var))))
  14413. (defun org-entry-delete (pom property)
  14414. "Delete PROPERTY from entry at point-or-marker POM.
  14415. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14416. non-nil when a property was removed."
  14417. (org-with-point-at pom
  14418. (pcase (org-get-property-block)
  14419. (`(,begin . ,origin)
  14420. (let* ((end (copy-marker origin))
  14421. (re (org-re-property
  14422. (concat (regexp-quote property) "\\+?") t t)))
  14423. (goto-char begin)
  14424. (while (re-search-forward re end t)
  14425. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14426. ;; If drawer is empty, remove it altogether.
  14427. (when (= begin end)
  14428. (delete-region (line-beginning-position 0)
  14429. (line-beginning-position 2)))
  14430. ;; Return non-nil if some property was removed.
  14431. (prog1 (/= end origin) (set-marker end nil))))
  14432. (_ nil))))
  14433. ;; Multi-values properties are properties that contain multiple values
  14434. ;; These values are assumed to be single words, separated by whitespace.
  14435. (defun org-entry-add-to-multivalued-property (pom property value)
  14436. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14437. (let* ((old (org-entry-get pom property))
  14438. (values (and old (org-split-string old "[ \t]"))))
  14439. (setq value (org-entry-protect-space value))
  14440. (unless (member value values)
  14441. (setq values (append values (list value)))
  14442. (org-entry-put pom property
  14443. (mapconcat 'identity values " ")))))
  14444. (defun org-entry-remove-from-multivalued-property (pom property value)
  14445. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14446. (let* ((old (org-entry-get pom property))
  14447. (values (and old (org-split-string old "[ \t]"))))
  14448. (setq value (org-entry-protect-space value))
  14449. (when (member value values)
  14450. (setq values (delete value values))
  14451. (org-entry-put pom property
  14452. (mapconcat 'identity values " ")))))
  14453. (defun org-entry-member-in-multivalued-property (pom property value)
  14454. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14455. (let* ((old (org-entry-get pom property))
  14456. (values (and old (org-split-string old "[ \t]"))))
  14457. (setq value (org-entry-protect-space value))
  14458. (member value values)))
  14459. (defun org-entry-get-multivalued-property (pom property)
  14460. "Return a list of values in a multivalued property."
  14461. (let* ((value (org-entry-get pom property))
  14462. (values (and value (org-split-string value "[ \t]"))))
  14463. (mapcar 'org-entry-restore-space values)))
  14464. (defun org-entry-put-multivalued-property (pom property &rest values)
  14465. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14466. VALUES should be a list of strings. Spaces will be protected."
  14467. (org-entry-put pom property
  14468. (mapconcat 'org-entry-protect-space values " "))
  14469. (let* ((value (org-entry-get pom property))
  14470. (values (and value (org-split-string value "[ \t]"))))
  14471. (mapcar 'org-entry-restore-space values)))
  14472. (defun org-entry-protect-space (s)
  14473. "Protect spaces and newline in string S."
  14474. (while (string-match " " s)
  14475. (setq s (replace-match "%20" t t s)))
  14476. (while (string-match "\n" s)
  14477. (setq s (replace-match "%0A" t t s)))
  14478. s)
  14479. (defun org-entry-restore-space (s)
  14480. "Restore spaces and newline in string S."
  14481. (while (string-match "%20" s)
  14482. (setq s (replace-match " " t t s)))
  14483. (while (string-match "%0A" s)
  14484. (setq s (replace-match "\n" t t s)))
  14485. s)
  14486. (defvar org-entry-property-inherited-from (make-marker)
  14487. "Marker pointing to the entry from where a property was inherited.
  14488. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14489. location of the entry where the inheritance search matched. If there was
  14490. no match, the marker will point nowhere.
  14491. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14492. is set.")
  14493. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14494. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14495. The search will stop at the first ancestor which has the property defined.
  14496. If the value found is \"nil\", return nil to show that the property
  14497. should be considered as undefined (this is the meaning of nil here).
  14498. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14499. (move-marker org-entry-property-inherited-from nil)
  14500. (org-with-wide-buffer
  14501. (let (value)
  14502. (catch 'exit
  14503. (while t
  14504. (let ((v (org--property-local-values property literal-nil)))
  14505. (when v
  14506. (setq value
  14507. (concat (mapconcat #'identity (delq nil v) " ")
  14508. (and value " ")
  14509. value)))
  14510. (cond
  14511. ((car v)
  14512. (org-back-to-heading t)
  14513. (move-marker org-entry-property-inherited-from (point))
  14514. (throw 'exit nil))
  14515. ((org-up-heading-safe))
  14516. (t
  14517. (let ((global (org--property-global-value property literal-nil)))
  14518. (cond ((not global))
  14519. (value (setq value (concat global " " value)))
  14520. (t (setq value global))))
  14521. (throw 'exit nil))))))
  14522. (if literal-nil value (org-not-nil value)))))
  14523. (defvar org-property-changed-functions nil
  14524. "Hook called when the value of a property has changed.
  14525. Each hook function should accept two arguments, the name of the property
  14526. and the new value.")
  14527. (defun org-entry-put (pom property value)
  14528. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14529. If the value is nil, it is converted to the empty string. If it
  14530. is not a string, an error is raised. Also raise an error on
  14531. invalid property names.
  14532. PROPERTY can be any regular property (see
  14533. `org-special-properties'). It can also be \"TODO\",
  14534. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14535. For the last two properties, VALUE may have any of the special
  14536. values \"earlier\" and \"later\". The function then increases or
  14537. decreases scheduled or deadline date by one day."
  14538. (cond ((null value) (setq value ""))
  14539. ((not (stringp value)) (error "Properties values should be strings"))
  14540. ((not (org--valid-property-p property))
  14541. (user-error "Invalid property name: \"%s\"" property)))
  14542. (org-with-point-at pom
  14543. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14544. (org-back-to-heading t)
  14545. (org-with-limited-levels (org-back-to-heading t)))
  14546. (let ((beg (point)))
  14547. (cond
  14548. ((equal property "TODO")
  14549. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14550. ((not (member value org-todo-keywords-1))
  14551. (user-error "\"%s\" is not a valid TODO state" value)))
  14552. (org-todo value)
  14553. (org-set-tags nil 'align))
  14554. ((equal property "PRIORITY")
  14555. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14556. (org-set-tags nil 'align))
  14557. ((equal property "SCHEDULED")
  14558. (forward-line)
  14559. (if (and (looking-at-p org-planning-line-re)
  14560. (re-search-forward
  14561. org-scheduled-time-regexp (line-end-position) t))
  14562. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14563. ((string= value "later") (org-timestamp-change 1 'day))
  14564. ((string= value "") (org-schedule '(4)))
  14565. (t (org-schedule nil value)))
  14566. (if (member value '("earlier" "later" ""))
  14567. (call-interactively #'org-schedule)
  14568. (org-schedule nil value))))
  14569. ((equal property "DEADLINE")
  14570. (forward-line)
  14571. (if (and (looking-at-p org-planning-line-re)
  14572. (re-search-forward
  14573. org-deadline-time-regexp (line-end-position) t))
  14574. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14575. ((string= value "later") (org-timestamp-change 1 'day))
  14576. ((string= value "") (org-deadline '(4)))
  14577. (t (org-deadline nil value)))
  14578. (if (member value '("earlier" "later" ""))
  14579. (call-interactively #'org-deadline)
  14580. (org-deadline nil value))))
  14581. ((member property org-special-properties)
  14582. (error "The %s property cannot be set with `org-entry-put'" property))
  14583. (t
  14584. (let* ((range (org-get-property-block beg 'force))
  14585. (end (cdr range))
  14586. (case-fold-search t))
  14587. (goto-char (car range))
  14588. (if (re-search-forward (org-re-property property nil t) end t)
  14589. (progn (delete-region (match-beginning 0) (match-end 0))
  14590. (goto-char (match-beginning 0)))
  14591. (goto-char end)
  14592. (insert "\n")
  14593. (backward-char))
  14594. (insert ":" property ":")
  14595. (when value (insert " " value))
  14596. (org-indent-line)))))
  14597. (run-hook-with-args 'org-property-changed-functions property value)))
  14598. (defun org-buffer-property-keys
  14599. (&optional specials defaults columns ignore-malformed)
  14600. "Get all property keys in the current buffer.
  14601. When SPECIALS is non-nil, also list the special properties that
  14602. reflect things like tags and TODO state.
  14603. When DEFAULTS is non-nil, also include properties that has
  14604. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14605. DESCRIPTION, LOCATION, and LOGGING and others.
  14606. When COLUMNS in non-nil, also include property names given in
  14607. COLUMN formats in the current buffer.
  14608. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14609. automatically performed, such drawers will be silently ignored."
  14610. (let ((case-fold-search t)
  14611. (props (append
  14612. (and specials org-special-properties)
  14613. (and defaults (cons org-effort-property org-default-properties))
  14614. nil)))
  14615. (org-with-wide-buffer
  14616. (goto-char (point-min))
  14617. (while (re-search-forward org-property-start-re nil t)
  14618. (let ((range (org-get-property-block)))
  14619. (catch 'skip
  14620. (unless range
  14621. (when (and (not ignore-malformed)
  14622. (not (org-before-first-heading-p))
  14623. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14624. (line-beginning-position))))
  14625. (org-get-property-block nil t))
  14626. (throw 'skip nil))
  14627. (goto-char (car range))
  14628. (let ((begin (car range))
  14629. (end (cdr range)))
  14630. ;; Make sure that found property block is not located
  14631. ;; before current point, as it would generate an infloop.
  14632. ;; It can happen, for example, in the following
  14633. ;; situation:
  14634. ;;
  14635. ;; * Headline
  14636. ;; :PROPERTIES:
  14637. ;; ...
  14638. ;; :END:
  14639. ;; *************** Inlinetask
  14640. ;; #+BEGIN_EXAMPLE
  14641. ;; :PROPERTIES:
  14642. ;; #+END_EXAMPLE
  14643. ;;
  14644. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14645. (while (< (point) end)
  14646. (let ((p (progn (looking-at org-property-re)
  14647. (match-string-no-properties 2))))
  14648. ;; Only add true property name, not extension symbol.
  14649. (push (if (not (string-match-p "\\+\\'" p)) p
  14650. (substring p 0 -1))
  14651. props))
  14652. (forward-line))))
  14653. (outline-next-heading)))
  14654. (when columns
  14655. (goto-char (point-min))
  14656. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14657. (let ((element (org-element-at-point)))
  14658. (when (memq (org-element-type element) '(keyword node-property))
  14659. (let ((value (org-element-property :value element))
  14660. (start 0))
  14661. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14662. (setq start (match-end 0))
  14663. (let ((p (match-string-no-properties 1 value)))
  14664. (unless (member-ignore-case p org-special-properties)
  14665. (push p props))))))))))
  14666. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14667. (defun org-property-values (key)
  14668. "List all non-nil values of property KEY in current buffer."
  14669. (org-with-wide-buffer
  14670. (goto-char (point-min))
  14671. (let ((case-fold-search t)
  14672. (re (org-re-property key))
  14673. values)
  14674. (while (re-search-forward re nil t)
  14675. (push (org-entry-get (point) key) values))
  14676. (delete-dups values))))
  14677. (defun org-insert-property-drawer ()
  14678. "Insert a property drawer into the current entry."
  14679. (org-with-wide-buffer
  14680. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14681. (org-back-to-heading t)
  14682. (org-with-limited-levels (org-back-to-heading t)))
  14683. (forward-line)
  14684. (when (looking-at-p org-planning-line-re) (forward-line))
  14685. (unless (looking-at-p org-property-drawer-re)
  14686. ;; Make sure we start editing a line from current entry, not from
  14687. ;; next one. It prevents extending text properties or overlays
  14688. ;; belonging to the latter.
  14689. (when (bolp) (backward-char))
  14690. (let ((begin (1+ (point)))
  14691. (inhibit-read-only t))
  14692. (insert "\n:PROPERTIES:\n:END:")
  14693. (when (eobp) (insert "\n"))
  14694. (org-indent-region begin (point))))))
  14695. (defun org-insert-drawer (&optional arg drawer)
  14696. "Insert a drawer at point.
  14697. When optional argument ARG is non-nil, insert a property drawer.
  14698. Optional argument DRAWER, when non-nil, is a string representing
  14699. drawer's name. Otherwise, the user is prompted for a name.
  14700. If a region is active, insert the drawer around that region
  14701. instead.
  14702. Point is left between drawer's boundaries."
  14703. (interactive "P")
  14704. (let* ((drawer (if arg "PROPERTIES"
  14705. (or drawer (read-from-minibuffer "Drawer: ")))))
  14706. (cond
  14707. ;; With C-u, fall back on `org-insert-property-drawer'
  14708. (arg (org-insert-property-drawer))
  14709. ;; Check validity of suggested drawer's name.
  14710. ((not (string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14711. (user-error "Invalid drawer name"))
  14712. ;; With an active region, insert a drawer at point.
  14713. ((not (org-region-active-p))
  14714. (progn
  14715. (unless (bolp) (insert "\n"))
  14716. (insert (format ":%s:\n\n:END:\n" drawer))
  14717. (forward-line -2)))
  14718. ;; Otherwise, insert the drawer at point
  14719. (t
  14720. (let ((rbeg (region-beginning))
  14721. (rend (copy-marker (region-end))))
  14722. (unwind-protect
  14723. (progn
  14724. (goto-char rbeg)
  14725. (beginning-of-line)
  14726. (when (save-excursion
  14727. (re-search-forward org-outline-regexp-bol rend t))
  14728. (user-error "Drawers cannot contain headlines"))
  14729. ;; Position point at the beginning of the first
  14730. ;; non-blank line in region. Insert drawer's opening
  14731. ;; there, then indent it.
  14732. (org-skip-whitespace)
  14733. (beginning-of-line)
  14734. (insert ":" drawer ":\n")
  14735. (forward-line -1)
  14736. (indent-for-tab-command)
  14737. ;; Move point to the beginning of the first blank line
  14738. ;; after the last non-blank line in region. Insert
  14739. ;; drawer's closing, then indent it.
  14740. (goto-char rend)
  14741. (skip-chars-backward " \r\t\n")
  14742. (insert "\n:END:")
  14743. (deactivate-mark t)
  14744. (indent-for-tab-command)
  14745. (unless (eolp) (insert "\n")))
  14746. ;; Clear marker, whatever the outcome of insertion is.
  14747. (set-marker rend nil)))))))
  14748. (defvar org-property-set-functions-alist nil
  14749. "Property set function alist.
  14750. Each entry should have the following format:
  14751. (PROPERTY . READ-FUNCTION)
  14752. The read function will be called with the same argument as
  14753. `org-completing-read'.")
  14754. (defun org-set-property-function (property)
  14755. "Get the function that should be used to set PROPERTY.
  14756. This is computed according to `org-property-set-functions-alist'."
  14757. (or (cdr (assoc property org-property-set-functions-alist))
  14758. 'org-completing-read))
  14759. (defun org-read-property-value (property)
  14760. "Read PROPERTY value from user."
  14761. (let* ((completion-ignore-case t)
  14762. (allowed (org-property-get-allowed-values nil property 'table))
  14763. (cur (org-entry-get nil property))
  14764. (prompt (concat property " value"
  14765. (if (and cur (string-match "\\S-" cur))
  14766. (concat " [" cur "]") "") ": "))
  14767. (set-function (org-set-property-function property))
  14768. (val (if allowed
  14769. (funcall set-function prompt allowed nil
  14770. (not (get-text-property 0 'org-unrestricted
  14771. (caar allowed))))
  14772. (funcall set-function prompt
  14773. (mapcar 'list (org-property-values property))
  14774. nil nil "" nil cur))))
  14775. (org-trim val)))
  14776. (defvar org-last-set-property nil)
  14777. (defvar org-last-set-property-value nil)
  14778. (defun org-read-property-name ()
  14779. "Read a property name."
  14780. (let ((completion-ignore-case t)
  14781. (default-prop (or (and (org-at-property-p)
  14782. (match-string-no-properties 2))
  14783. org-last-set-property)))
  14784. (org-completing-read
  14785. (concat "Property"
  14786. (if default-prop (concat " [" default-prop "]") "")
  14787. ": ")
  14788. (mapcar #'list (org-buffer-property-keys nil t t))
  14789. nil nil nil nil default-prop)))
  14790. (defun org-set-property-and-value (use-last)
  14791. "Allow to set [PROPERTY]: [value] direction from prompt.
  14792. When use-default, don't even ask, just use the last
  14793. \"[PROPERTY]: [value]\" string from the history."
  14794. (interactive "P")
  14795. (let* ((completion-ignore-case t)
  14796. (pv (or (and use-last org-last-set-property-value)
  14797. (org-completing-read
  14798. "Enter a \"[Property]: [value]\" pair: "
  14799. nil nil nil nil nil
  14800. org-last-set-property-value)))
  14801. prop val)
  14802. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14803. (setq prop (match-string 1 pv)
  14804. val (match-string 2 pv))
  14805. (org-set-property prop val))))
  14806. (defun org-set-property (property value)
  14807. "In the current entry, set PROPERTY to VALUE.
  14808. When called interactively, this will prompt for a property name, offering
  14809. completion on existing and default properties. And then it will prompt
  14810. for a value, offering completion either on allowed values (via an inherited
  14811. xxx_ALL property) or on existing values in other instances of this property
  14812. in the current file.
  14813. Throw an error when trying to set a property with an invalid name."
  14814. (interactive (list nil nil))
  14815. (let ((property (or property (org-read-property-name))))
  14816. ;; `org-entry-put' also makes the following check, but this one
  14817. ;; avoids polluting `org-last-set-property' and
  14818. ;; `org-last-set-property-value' needlessly.
  14819. (unless (org--valid-property-p property)
  14820. (user-error "Invalid property name: \"%s\"" property))
  14821. (let ((value (or value (org-read-property-value property)))
  14822. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14823. (setq org-last-set-property property)
  14824. (setq org-last-set-property-value (concat property ": " value))
  14825. ;; Possibly postprocess the inserted value:
  14826. (when fn (setq value (funcall fn value)))
  14827. (unless (equal (org-entry-get nil property) value)
  14828. (org-entry-put nil property value)))))
  14829. (defun org-find-property (property &optional value)
  14830. "Find first entry in buffer that sets PROPERTY.
  14831. When optional argument VALUE is non-nil, only consider an entry
  14832. if it contains PROPERTY set to this value. If PROPERTY should be
  14833. explicitly set to nil, use string \"nil\" for VALUE.
  14834. Return position where the entry begins, or nil if there is no
  14835. such entry. If narrowing is in effect, only search the visible
  14836. part of the buffer."
  14837. (save-excursion
  14838. (goto-char (point-min))
  14839. (let ((case-fold-search t)
  14840. (re (org-re-property property nil (not value) value)))
  14841. (catch 'exit
  14842. (while (re-search-forward re nil t)
  14843. (when (if value (org-at-property-p)
  14844. (org-entry-get (point) property nil t))
  14845. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14846. (defun org-delete-property (property)
  14847. "In the current entry, delete PROPERTY."
  14848. (interactive
  14849. (let* ((completion-ignore-case t)
  14850. (cat (org-entry-get (point) "CATEGORY"))
  14851. (props0 (org-entry-properties nil 'standard))
  14852. (props (if cat props0
  14853. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14854. (prop (if (< 1 (length props))
  14855. (completing-read "Property: " props nil t)
  14856. (caar props))))
  14857. (list prop)))
  14858. (if (not property)
  14859. (message "No property to delete in this entry")
  14860. (org-entry-delete nil property)
  14861. (message "Property \"%s\" deleted" property)))
  14862. (defun org-delete-property-globally (property)
  14863. "Remove PROPERTY globally, from all entries.
  14864. This function ignores narrowing, if any."
  14865. (interactive
  14866. (let* ((completion-ignore-case t)
  14867. (prop (completing-read
  14868. "Globally remove property: "
  14869. (mapcar #'list (org-buffer-property-keys)))))
  14870. (list prop)))
  14871. (org-with-wide-buffer
  14872. (goto-char (point-min))
  14873. (let ((count 0)
  14874. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14875. (while (re-search-forward re nil t)
  14876. (when (org-entry-delete (point) property) (cl-incf count)))
  14877. (message "Property \"%s\" removed from %d entries" property count))))
  14878. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14879. (defun org-compute-property-at-point ()
  14880. "Compute the property at point.
  14881. This looks for an enclosing column format, extracts the operator and
  14882. then applies it to the property in the column format's scope."
  14883. (interactive)
  14884. (unless (org-at-property-p)
  14885. (user-error "Not at a property"))
  14886. (let ((prop (match-string-no-properties 2)))
  14887. (org-columns-get-format-and-top-level)
  14888. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  14889. (user-error "No operator defined for property %s" prop))
  14890. (org-columns-compute prop)))
  14891. (defvar org-property-allowed-value-functions nil
  14892. "Hook for functions supplying allowed values for a specific property.
  14893. The functions must take a single argument, the name of the property, and
  14894. return a flat list of allowed values. If \":ETC\" is one of
  14895. the values, this means that these values are intended as defaults for
  14896. completion, but that other values should be allowed too.
  14897. The functions must return nil if they are not responsible for this
  14898. property.")
  14899. (defun org-property-get-allowed-values (pom property &optional table)
  14900. "Get allowed values for the property PROPERTY.
  14901. When TABLE is non-nil, return an alist that can directly be used for
  14902. completion."
  14903. (let (vals)
  14904. (cond
  14905. ((equal property "TODO")
  14906. (setq vals (org-with-point-at pom
  14907. (append org-todo-keywords-1 '("")))))
  14908. ((equal property "PRIORITY")
  14909. (let ((n org-lowest-priority))
  14910. (while (>= n org-highest-priority)
  14911. (push (char-to-string n) vals)
  14912. (setq n (1- n)))))
  14913. ((equal property "CATEGORY"))
  14914. ((member property org-special-properties))
  14915. ((setq vals (run-hook-with-args-until-success
  14916. 'org-property-allowed-value-functions property)))
  14917. (t
  14918. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14919. (when (and vals (string-match "\\S-" vals))
  14920. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14921. (setq vals (mapcar (lambda (x)
  14922. (cond ((stringp x) x)
  14923. ((numberp x) (number-to-string x))
  14924. ((symbolp x) (symbol-name x))
  14925. (t "???")))
  14926. vals)))))
  14927. (when (member ":ETC" vals)
  14928. (setq vals (remove ":ETC" vals))
  14929. (org-add-props (car vals) '(org-unrestricted t)))
  14930. (if table (mapcar 'list vals) vals)))
  14931. (defun org-property-previous-allowed-value (&optional _previous)
  14932. "Switch to the next allowed value for this property."
  14933. (interactive)
  14934. (org-property-next-allowed-value t))
  14935. (defun org-property-next-allowed-value (&optional previous)
  14936. "Switch to the next allowed value for this property."
  14937. (interactive)
  14938. (unless (org-at-property-p)
  14939. (user-error "Not at a property"))
  14940. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14941. (key (match-string 2))
  14942. (value (match-string 3))
  14943. (allowed (or (org-property-get-allowed-values (point) key)
  14944. (and (member value '("[ ]" "[-]" "[X]"))
  14945. '("[ ]" "[X]"))))
  14946. (heading (save-match-data (nth 4 (org-heading-components))))
  14947. nval)
  14948. (unless allowed
  14949. (user-error "Allowed values for this property have not been defined"))
  14950. (when previous (setq allowed (reverse allowed)))
  14951. (when (member value allowed)
  14952. (setq nval (car (cdr (member value allowed)))))
  14953. (setq nval (or nval (car allowed)))
  14954. (when (equal nval value)
  14955. (user-error "Only one allowed value for this property"))
  14956. (org-at-property-p)
  14957. (replace-match (concat " :" key ": " nval) t t)
  14958. (org-indent-line)
  14959. (beginning-of-line 1)
  14960. (skip-chars-forward " \t")
  14961. (when (equal prop org-effort-property)
  14962. (org-refresh-property
  14963. '((effort . identity)
  14964. (effort-minutes . org-duration-to-minutes))
  14965. nval)
  14966. (when (string= org-clock-current-task heading)
  14967. (setq org-clock-effort nval)
  14968. (org-clock-update-mode-line)))
  14969. (run-hook-with-args 'org-property-changed-functions key nval)))
  14970. (defun org-find-olp (path &optional this-buffer)
  14971. "Return a marker pointing to the entry at outline path OLP.
  14972. If anything goes wrong, throw an error.
  14973. You can wrap this call to catch the error like this:
  14974. (condition-case msg
  14975. (org-mobile-locate-entry (match-string 4))
  14976. (error (nth 1 msg)))
  14977. The return value will then be either a string with the error message,
  14978. or a marker if everything is OK.
  14979. If THIS-BUFFER is set, the outline path does not contain a file,
  14980. only headings."
  14981. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14982. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14983. (level 1)
  14984. (lmin 1)
  14985. (lmax 1)
  14986. end found flevel)
  14987. (unless buffer (error "File not found :%s" file))
  14988. (with-current-buffer buffer
  14989. (unless (derived-mode-p 'org-mode)
  14990. (error "Buffer %s needs to be in Org mode" buffer))
  14991. (org-with-wide-buffer
  14992. (goto-char (point-min))
  14993. (dolist (heading path)
  14994. (let ((re (format org-complex-heading-regexp-format
  14995. (regexp-quote heading)))
  14996. (cnt 0))
  14997. (while (re-search-forward re end t)
  14998. (setq level (- (match-end 1) (match-beginning 1)))
  14999. (when (and (>= level lmin) (<= level lmax))
  15000. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15001. (when (= cnt 0)
  15002. (error "Heading not found on level %d: %s" lmax heading))
  15003. (when (> cnt 1)
  15004. (error "Heading not unique on level %d: %s" lmax heading))
  15005. (goto-char found)
  15006. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15007. (setq end (save-excursion (org-end-of-subtree t t)))))
  15008. (when (org-at-heading-p)
  15009. (point-marker))))))
  15010. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15011. "Find node HEADING in BUFFER.
  15012. Return a marker to the heading if it was found, or nil if not.
  15013. If POS-ONLY is set, return just the position instead of a marker.
  15014. The heading text must match exact, but it may have a TODO keyword,
  15015. a priority cookie and tags in the standard locations."
  15016. (with-current-buffer (or buffer (current-buffer))
  15017. (org-with-wide-buffer
  15018. (goto-char (point-min))
  15019. (let (case-fold-search)
  15020. (when (re-search-forward
  15021. (format org-complex-heading-regexp-format
  15022. (regexp-quote heading)) nil t)
  15023. (if pos-only
  15024. (match-beginning 0)
  15025. (move-marker (make-marker) (match-beginning 0))))))))
  15026. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15027. "Find Org node headline HEADING in all .org files in directory DIR.
  15028. When the target headline is found, return a marker to this location."
  15029. (let ((files (directory-files (or dir default-directory)
  15030. t "\\`[^.#].*\\.org\\'"))
  15031. visiting m buffer)
  15032. (catch 'found
  15033. (dolist (file files)
  15034. (message "trying %s" file)
  15035. (setq visiting (org-find-base-buffer-visiting file))
  15036. (setq buffer (or visiting (find-file-noselect file)))
  15037. (setq m (org-find-exact-headline-in-buffer
  15038. heading buffer))
  15039. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15040. (and m (throw 'found m))))))
  15041. (defun org-find-entry-with-id (ident)
  15042. "Locate the entry that contains the ID property with exact value IDENT.
  15043. IDENT can be a string, a symbol or a number, this function will search for
  15044. the string representation of it.
  15045. Return the position where this entry starts, or nil if there is no such entry."
  15046. (interactive "sID: ")
  15047. (let ((id (cond
  15048. ((stringp ident) ident)
  15049. ((symbolp ident) (symbol-name ident))
  15050. ((numberp ident) (number-to-string ident))
  15051. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15052. (org-with-wide-buffer (org-find-property "ID" id))))
  15053. ;;;; Timestamps
  15054. (defvar org-last-changed-timestamp nil)
  15055. (defvar org-last-inserted-timestamp nil
  15056. "The last time stamp inserted with `org-insert-time-stamp'.")
  15057. (defun org-time-stamp (arg &optional inactive)
  15058. "Prompt for a date/time and insert a time stamp.
  15059. If the user specifies a time like HH:MM or if this command is
  15060. called with at least one prefix argument, the time stamp contains
  15061. the date and the time. Otherwise, only the date is included.
  15062. All parts of a date not specified by the user are filled in from
  15063. the timestamp at point, if any, or the current date/time
  15064. otherwise.
  15065. If there is already a timestamp at the cursor, it is replaced.
  15066. With two universal prefix arguments, insert an active timestamp
  15067. with the current time without prompting the user.
  15068. When called from lisp, the timestamp is inactive if INACTIVE is
  15069. non-nil."
  15070. (interactive "P")
  15071. (let* ((ts (cond
  15072. ((org-at-date-range-p t)
  15073. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15074. ((org-at-timestamp-p 'lax) (match-string 0))))
  15075. ;; Default time is either the timestamp at point or today.
  15076. ;; When entering a range, only the range start is considered.
  15077. (default-time (if (not ts) (current-time)
  15078. (apply #'encode-time (org-parse-time-string ts))))
  15079. (default-input (and ts (org-get-compact-tod ts)))
  15080. (repeater (and ts
  15081. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15082. (match-string 0 ts)))
  15083. org-time-was-given
  15084. org-end-time-was-given
  15085. (time
  15086. (and (if (equal arg '(16)) (current-time)
  15087. ;; Preserve `this-command' and `last-command'.
  15088. (let ((this-command this-command)
  15089. (last-command last-command))
  15090. (org-read-date
  15091. arg 'totime nil nil default-time default-input
  15092. inactive))))))
  15093. (cond
  15094. ((and ts
  15095. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15096. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15097. (insert "--")
  15098. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15099. (ts
  15100. ;; Make sure we're on a timestamp. When in the middle of a date
  15101. ;; range, move arbitrarily to range end.
  15102. (unless (org-at-timestamp-p 'lax)
  15103. (skip-chars-forward "-")
  15104. (org-at-timestamp-p 'lax))
  15105. (replace-match "")
  15106. (setq org-last-changed-timestamp
  15107. (org-insert-time-stamp
  15108. time (or org-time-was-given arg)
  15109. inactive nil nil (list org-end-time-was-given)))
  15110. (when repeater
  15111. (backward-char)
  15112. (insert " " repeater)
  15113. (setq org-last-changed-timestamp
  15114. (concat (substring org-last-inserted-timestamp 0 -1)
  15115. " " repeater ">")))
  15116. (message "Timestamp updated"))
  15117. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15118. (t (org-insert-time-stamp
  15119. time (or org-time-was-given arg) inactive nil nil
  15120. (list org-end-time-was-given))))))
  15121. ;; FIXME: can we use this for something else, like computing time differences?
  15122. (defun org-get-compact-tod (s)
  15123. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15124. (let* ((t1 (match-string 1 s))
  15125. (h1 (string-to-number (match-string 2 s)))
  15126. (m1 (string-to-number (match-string 3 s)))
  15127. (t2 (and (match-end 4) (match-string 5 s)))
  15128. (h2 (and t2 (string-to-number (match-string 6 s))))
  15129. (m2 (and t2 (string-to-number (match-string 7 s))))
  15130. dh dm)
  15131. (if (not t2)
  15132. t1
  15133. (setq dh (- h2 h1) dm (- m2 m1))
  15134. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15135. (concat t1 "+" (number-to-string dh)
  15136. (and (/= 0 dm) (format ":%02d" dm)))))))
  15137. (defun org-time-stamp-inactive (&optional arg)
  15138. "Insert an inactive time stamp.
  15139. An inactive time stamp is enclosed in square brackets instead of angle
  15140. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15141. does not link to the calendar and cannot be changed with the S-cursor keys.
  15142. So these are more for recording a certain time/date."
  15143. (interactive "P")
  15144. (org-time-stamp arg 'inactive))
  15145. (defvar org-date-ovl (make-overlay 1 1))
  15146. (overlay-put org-date-ovl 'face 'org-date-selected)
  15147. (delete-overlay org-date-ovl)
  15148. (defvar org-ans1) ; dynamically scoped parameter
  15149. (defvar org-ans2) ; dynamically scoped parameter
  15150. (defvar org-plain-time-of-day-regexp) ; defined below
  15151. (defvar org-overriding-default-time nil) ; dynamically scoped
  15152. (defvar org-read-date-overlay nil)
  15153. (defvar org-dcst nil) ; dynamically scoped
  15154. (defvar org-read-date-history nil)
  15155. (defvar org-read-date-final-answer nil)
  15156. (defvar org-read-date-analyze-futurep nil)
  15157. (defvar org-read-date-analyze-forced-year nil)
  15158. (defvar org-read-date-inactive)
  15159. (defvar org-read-date-minibuffer-local-map
  15160. (let* ((map (make-sparse-keymap)))
  15161. (set-keymap-parent map minibuffer-local-map)
  15162. (org-defkey map (kbd ".")
  15163. (lambda () (interactive)
  15164. ;; Are we at the beginning of the prompt?
  15165. (if (looking-back "^[^:]+: "
  15166. (let ((inhibit-field-text-motion t))
  15167. (line-beginning-position)))
  15168. (org-eval-in-calendar '(calendar-goto-today))
  15169. (insert "."))))
  15170. (org-defkey map (kbd "C-.")
  15171. (lambda () (interactive)
  15172. (org-eval-in-calendar '(calendar-goto-today))))
  15173. (org-defkey map [(meta shift left)]
  15174. (lambda () (interactive)
  15175. (org-eval-in-calendar '(calendar-backward-month 1))))
  15176. (org-defkey map [(meta shift right)]
  15177. (lambda () (interactive)
  15178. (org-eval-in-calendar '(calendar-forward-month 1))))
  15179. (org-defkey map [(meta shift up)]
  15180. (lambda () (interactive)
  15181. (org-eval-in-calendar '(calendar-backward-year 1))))
  15182. (org-defkey map [(meta shift down)]
  15183. (lambda () (interactive)
  15184. (org-eval-in-calendar '(calendar-forward-year 1))))
  15185. (org-defkey map [?\e (shift left)]
  15186. (lambda () (interactive)
  15187. (org-eval-in-calendar '(calendar-backward-month 1))))
  15188. (org-defkey map [?\e (shift right)]
  15189. (lambda () (interactive)
  15190. (org-eval-in-calendar '(calendar-forward-month 1))))
  15191. (org-defkey map [?\e (shift up)]
  15192. (lambda () (interactive)
  15193. (org-eval-in-calendar '(calendar-backward-year 1))))
  15194. (org-defkey map [?\e (shift down)]
  15195. (lambda () (interactive)
  15196. (org-eval-in-calendar '(calendar-forward-year 1))))
  15197. (org-defkey map [(shift up)]
  15198. (lambda () (interactive)
  15199. (org-eval-in-calendar '(calendar-backward-week 1))))
  15200. (org-defkey map [(shift down)]
  15201. (lambda () (interactive)
  15202. (org-eval-in-calendar '(calendar-forward-week 1))))
  15203. (org-defkey map [(shift left)]
  15204. (lambda () (interactive)
  15205. (org-eval-in-calendar '(calendar-backward-day 1))))
  15206. (org-defkey map [(shift right)]
  15207. (lambda () (interactive)
  15208. (org-eval-in-calendar '(calendar-forward-day 1))))
  15209. (org-defkey map "!"
  15210. (lambda () (interactive)
  15211. (org-eval-in-calendar '(diary-view-entries))
  15212. (message "")))
  15213. (org-defkey map ">"
  15214. (lambda () (interactive)
  15215. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15216. (org-defkey map "<"
  15217. (lambda () (interactive)
  15218. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15219. (org-defkey map "\C-v"
  15220. (lambda () (interactive)
  15221. (org-eval-in-calendar
  15222. '(calendar-scroll-left-three-months 1))))
  15223. (org-defkey map "\M-v"
  15224. (lambda () (interactive)
  15225. (org-eval-in-calendar
  15226. '(calendar-scroll-right-three-months 1))))
  15227. map)
  15228. "Keymap for minibuffer commands when using `org-read-date'.")
  15229. (defvar org-def)
  15230. (defvar org-defdecode)
  15231. (defvar org-with-time)
  15232. (defvar calendar-setup) ; Dynamically scoped.
  15233. (defun org-read-date (&optional with-time to-time from-string prompt
  15234. default-time default-input inactive)
  15235. "Read a date, possibly a time, and make things smooth for the user.
  15236. The prompt will suggest to enter an ISO date, but you can also enter anything
  15237. which will at least partially be understood by `parse-time-string'.
  15238. Unrecognized parts of the date will default to the current day, month, year,
  15239. hour and minute. If this command is called to replace a timestamp at point,
  15240. or to enter the second timestamp of a range, the default time is taken
  15241. from the existing stamp. Furthermore, the command prefers the future,
  15242. so if you are giving a date where the year is not given, and the day-month
  15243. combination is already past in the current year, it will assume you
  15244. mean next year. For details, see the manual. A few examples:
  15245. 3-2-5 --> 2003-02-05
  15246. feb 15 --> currentyear-02-15
  15247. 2/15 --> currentyear-02-15
  15248. sep 12 9 --> 2009-09-12
  15249. 12:45 --> today 12:45
  15250. 22 sept 0:34 --> currentyear-09-22 0:34
  15251. 12 --> currentyear-currentmonth-12
  15252. Fri --> nearest Friday after today
  15253. -Tue --> last Tuesday
  15254. etc.
  15255. Furthermore you can specify a relative date by giving, as the *first* thing
  15256. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15257. change in days weeks, months, years.
  15258. With a single plus or minus, the date is relative to today. With a double
  15259. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15260. +4d --> four days from today
  15261. +4 --> same as above
  15262. +2w --> two weeks from today
  15263. ++5 --> five days from default date
  15264. The function understands only English month and weekday abbreviations.
  15265. While prompting, a calendar is popped up - you can also select the
  15266. date with the mouse (button 1). The calendar shows a period of three
  15267. months. To scroll it to other months, use the keys `>' and `<'.
  15268. If you don't like the calendar, turn it off with
  15269. (setq org-read-date-popup-calendar nil)
  15270. With optional argument TO-TIME, the date will immediately be converted
  15271. to an internal time.
  15272. With an optional argument WITH-TIME, the prompt will suggest to
  15273. also insert a time. Note that when WITH-TIME is not set, you can
  15274. still enter a time, and this function will inform the calling routine
  15275. about this change. The calling routine may then choose to change the
  15276. format used to insert the time stamp into the buffer to include the time.
  15277. With optional argument FROM-STRING, read from this string instead from
  15278. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15279. the time/date that is used for everything that is not specified by the
  15280. user."
  15281. (require 'parse-time)
  15282. (let* ((org-with-time with-time)
  15283. (org-time-stamp-rounding-minutes
  15284. (if (equal org-with-time '(16))
  15285. '(0 0)
  15286. org-time-stamp-rounding-minutes))
  15287. (org-dcst org-display-custom-times)
  15288. (ct (org-current-time))
  15289. (org-def (or org-overriding-default-time default-time ct))
  15290. (org-defdecode (decode-time org-def))
  15291. (cur-frame (selected-frame))
  15292. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15293. (calendar-setup
  15294. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15295. (calendar-move-hook nil)
  15296. (calendar-view-diary-initially-flag nil)
  15297. (calendar-view-holidays-initially-flag nil)
  15298. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15299. ;; Rationalize `org-def' and `org-defdecode', if required.
  15300. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15301. (setf (nth 2 org-defdecode) -1)
  15302. (setf (nth 1 org-defdecode) 59)
  15303. (setq org-def (apply #'encode-time org-defdecode))
  15304. (setq org-defdecode (decode-time org-def)))
  15305. (let* ((timestr (format-time-string
  15306. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15307. org-def))
  15308. (prompt (concat (if prompt (concat prompt " ") "")
  15309. (format "Date+time [%s]: " timestr))))
  15310. (cond
  15311. (from-string (setq ans from-string))
  15312. (org-read-date-popup-calendar
  15313. (save-excursion
  15314. (save-window-excursion
  15315. (calendar)
  15316. (when (eq calendar-setup 'calendar-only)
  15317. (setq cal-frame
  15318. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15319. (select-frame cal-frame))
  15320. (org-eval-in-calendar '(setq cursor-type nil) t)
  15321. (unwind-protect
  15322. (progn
  15323. (calendar-forward-day (- (time-to-days org-def)
  15324. (calendar-absolute-from-gregorian
  15325. (calendar-current-date))))
  15326. (org-eval-in-calendar nil t)
  15327. (let* ((old-map (current-local-map))
  15328. (map (copy-keymap calendar-mode-map))
  15329. (minibuffer-local-map
  15330. (copy-keymap org-read-date-minibuffer-local-map)))
  15331. (org-defkey map (kbd "RET") 'org-calendar-select)
  15332. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15333. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15334. (unwind-protect
  15335. (progn
  15336. (use-local-map map)
  15337. (setq org-read-date-inactive inactive)
  15338. (add-hook 'post-command-hook 'org-read-date-display)
  15339. (setq org-ans0
  15340. (read-string prompt
  15341. default-input
  15342. 'org-read-date-history
  15343. nil))
  15344. ;; org-ans0: from prompt
  15345. ;; org-ans1: from mouse click
  15346. ;; org-ans2: from calendar motion
  15347. (setq ans
  15348. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15349. (remove-hook 'post-command-hook 'org-read-date-display)
  15350. (use-local-map old-map)
  15351. (when org-read-date-overlay
  15352. (delete-overlay org-read-date-overlay)
  15353. (setq org-read-date-overlay nil)))))
  15354. (bury-buffer "*Calendar*")
  15355. (when cal-frame
  15356. (delete-frame cal-frame)
  15357. (select-frame-set-input-focus cur-frame))))))
  15358. (t ; Naked prompt only
  15359. (unwind-protect
  15360. (setq ans (read-string prompt default-input
  15361. 'org-read-date-history timestr))
  15362. (when org-read-date-overlay
  15363. (delete-overlay org-read-date-overlay)
  15364. (setq org-read-date-overlay nil))))))
  15365. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15366. (when org-read-date-analyze-forced-year
  15367. (message "Year was forced into %s"
  15368. (if org-read-date-force-compatible-dates
  15369. "compatible range (1970-2037)"
  15370. "range representable on this machine"))
  15371. (ding))
  15372. ;; One round trip to get rid of 34th of August and stuff like that....
  15373. (setq final (decode-time (apply 'encode-time final)))
  15374. (setq org-read-date-final-answer ans)
  15375. (if to-time
  15376. (apply 'encode-time final)
  15377. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15378. (format "%04d-%02d-%02d %02d:%02d"
  15379. (nth 5 final) (nth 4 final) (nth 3 final)
  15380. (nth 2 final) (nth 1 final))
  15381. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15382. (defun org-read-date-display ()
  15383. "Display the current date prompt interpretation in the minibuffer."
  15384. (when org-read-date-display-live
  15385. (when org-read-date-overlay
  15386. (delete-overlay org-read-date-overlay))
  15387. (when (minibufferp (current-buffer))
  15388. (save-excursion
  15389. (end-of-line 1)
  15390. (while (not (equal (buffer-substring
  15391. (max (point-min) (- (point) 4)) (point))
  15392. " "))
  15393. (insert " ")))
  15394. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15395. " " (or org-ans1 org-ans2)))
  15396. (org-end-time-was-given nil)
  15397. (f (org-read-date-analyze ans org-def org-defdecode))
  15398. (fmts (if org-dcst
  15399. org-time-stamp-custom-formats
  15400. org-time-stamp-formats))
  15401. (fmt (if (or org-with-time
  15402. (and (boundp 'org-time-was-given) org-time-was-given))
  15403. (cdr fmts)
  15404. (car fmts)))
  15405. (txt (format-time-string fmt (apply 'encode-time f)))
  15406. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15407. (txt (concat "=> " txt)))
  15408. (when (and org-end-time-was-given
  15409. (string-match org-plain-time-of-day-regexp txt))
  15410. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15411. org-end-time-was-given
  15412. (substring txt (match-end 0)))))
  15413. (when org-read-date-analyze-futurep
  15414. (setq txt (concat txt " (=>F)")))
  15415. (setq org-read-date-overlay
  15416. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15417. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15418. (defun org-read-date-analyze (ans def defdecode)
  15419. "Analyze the combined answer of the date prompt."
  15420. ;; FIXME: cleanup and comment
  15421. ;; Pass `current-time' result to `decode-time' (instead of calling
  15422. ;; without arguments) so that only `current-time' has to be
  15423. ;; overridden in tests.
  15424. (let ((org-def def)
  15425. (org-defdecode defdecode)
  15426. (nowdecode (decode-time (current-time)))
  15427. delta deltan deltaw deltadef year month day
  15428. hour minute second wday pm h2 m2 tl wday1
  15429. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15430. (setq org-read-date-analyze-futurep nil
  15431. org-read-date-analyze-forced-year nil)
  15432. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15433. (setq ans "+0"))
  15434. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15435. (setq ans (replace-match "" t t ans)
  15436. deltan (car delta)
  15437. deltaw (nth 1 delta)
  15438. deltadef (nth 2 delta)))
  15439. ;; Check if there is an iso week date in there. If yes, store the
  15440. ;; info and postpone interpreting it until the rest of the parsing
  15441. ;; is done.
  15442. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15443. (setq iso-year (when (match-end 1)
  15444. (org-small-year-to-year
  15445. (string-to-number (match-string 1 ans))))
  15446. iso-weekday (when (match-end 3)
  15447. (string-to-number (match-string 3 ans)))
  15448. iso-week (string-to-number (match-string 2 ans)))
  15449. (setq ans (replace-match "" t t ans)))
  15450. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15451. (when (string-match
  15452. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15453. (setq year (if (match-end 2)
  15454. (string-to-number (match-string 2 ans))
  15455. (progn (setq kill-year t)
  15456. (string-to-number (format-time-string "%Y"))))
  15457. month (string-to-number (match-string 3 ans))
  15458. day (string-to-number (match-string 4 ans)))
  15459. (setq year (org-small-year-to-year year))
  15460. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15461. t nil ans)))
  15462. ;; Help matching dotted european dates
  15463. (when (string-match
  15464. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15465. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15466. (setq kill-year t)
  15467. (string-to-number (format-time-string "%Y")))
  15468. day (string-to-number (match-string 1 ans))
  15469. month (string-to-number (match-string 2 ans))
  15470. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15471. t nil ans)))
  15472. ;; Help matching american dates, like 5/30 or 5/30/7
  15473. (when (string-match
  15474. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15475. (setq year (if (match-end 4)
  15476. (string-to-number (match-string 4 ans))
  15477. (progn (setq kill-year t)
  15478. (string-to-number (format-time-string "%Y"))))
  15479. month (string-to-number (match-string 1 ans))
  15480. day (string-to-number (match-string 2 ans)))
  15481. (setq year (org-small-year-to-year year))
  15482. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15483. t nil ans)))
  15484. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15485. ;; If there is a time with am/pm, and *no* time without it, we convert
  15486. ;; so that matching will be successful.
  15487. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15488. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15489. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15490. (setq hour (string-to-number (match-string 1 ans))
  15491. minute (if (match-end 3)
  15492. (string-to-number (match-string 3 ans))
  15493. 0)
  15494. pm (equal ?p
  15495. (string-to-char (downcase (match-string 4 ans)))))
  15496. (if (and (= hour 12) (not pm))
  15497. (setq hour 0)
  15498. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15499. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15500. t t ans))))
  15501. ;; Check if a time range is given as a duration
  15502. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15503. (setq hour (string-to-number (match-string 1 ans))
  15504. h2 (+ hour (string-to-number (match-string 3 ans)))
  15505. minute (string-to-number (match-string 2 ans))
  15506. m2 (+ minute (if (match-end 5) (string-to-number
  15507. (match-string 5 ans))0)))
  15508. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15509. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15510. t t ans)))
  15511. ;; Check if there is a time range
  15512. (when (boundp 'org-end-time-was-given)
  15513. (setq org-time-was-given nil)
  15514. (when (and (string-match org-plain-time-of-day-regexp ans)
  15515. (match-end 8))
  15516. (setq org-end-time-was-given (match-string 8 ans))
  15517. (setq ans (concat (substring ans 0 (match-beginning 7))
  15518. (substring ans (match-end 7))))))
  15519. (setq tl (parse-time-string ans)
  15520. day (or (nth 3 tl) (nth 3 org-defdecode))
  15521. month
  15522. (cond ((nth 4 tl))
  15523. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15524. ;; Day was specified. Make sure DAY+MONTH
  15525. ;; combination happens in the future.
  15526. ((nth 3 tl)
  15527. (setq futurep t)
  15528. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15529. (nth 4 nowdecode)))
  15530. (t (nth 4 org-defdecode)))
  15531. year
  15532. (cond ((and (not kill-year) (nth 5 tl)))
  15533. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15534. ;; Month was guessed in the future and is at least
  15535. ;; equal to NOWDECODE's. Fix year accordingly.
  15536. (futurep
  15537. (if (or (> month (nth 4 nowdecode))
  15538. (>= day (nth 3 nowdecode)))
  15539. (nth 5 nowdecode)
  15540. (1+ (nth 5 nowdecode))))
  15541. ;; Month was specified. Make sure MONTH+YEAR
  15542. ;; combination happens in the future.
  15543. ((nth 4 tl)
  15544. (setq futurep t)
  15545. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15546. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15547. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15548. (t (nth 5 nowdecode))))
  15549. (t (nth 5 org-defdecode)))
  15550. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15551. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15552. second (or (nth 0 tl) 0)
  15553. wday (nth 6 tl))
  15554. (when (and (eq org-read-date-prefer-future 'time)
  15555. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15556. (equal day (nth 3 nowdecode))
  15557. (equal month (nth 4 nowdecode))
  15558. (equal year (nth 5 nowdecode))
  15559. (nth 2 tl)
  15560. (or (< (nth 2 tl) (nth 2 nowdecode))
  15561. (and (= (nth 2 tl) (nth 2 nowdecode))
  15562. (nth 1 tl)
  15563. (< (nth 1 tl) (nth 1 nowdecode)))))
  15564. (setq day (1+ day)
  15565. futurep t))
  15566. ;; Special date definitions below
  15567. (cond
  15568. (iso-week
  15569. ;; There was an iso week
  15570. (require 'cal-iso)
  15571. (setq futurep nil)
  15572. (setq year (or iso-year year)
  15573. day (or iso-weekday wday 1)
  15574. wday nil ; to make sure that the trigger below does not match
  15575. iso-date (calendar-gregorian-from-absolute
  15576. (calendar-iso-to-absolute
  15577. (list iso-week day year))))
  15578. ; FIXME: Should we also push ISO weeks into the future?
  15579. ; (when (and org-read-date-prefer-future
  15580. ; (not iso-year)
  15581. ; (< (calendar-absolute-from-gregorian iso-date)
  15582. ; (time-to-days (current-time))))
  15583. ; (setq year (1+ year)
  15584. ; iso-date (calendar-gregorian-from-absolute
  15585. ; (calendar-iso-to-absolute
  15586. ; (list iso-week day year)))))
  15587. (setq month (car iso-date)
  15588. year (nth 2 iso-date)
  15589. day (nth 1 iso-date)))
  15590. (deltan
  15591. (setq futurep nil)
  15592. (unless deltadef
  15593. ;; Pass `current-time' result to `decode-time' (instead of
  15594. ;; calling without arguments) so that only `current-time' has
  15595. ;; to be overridden in tests.
  15596. (let ((now (decode-time (current-time))))
  15597. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15598. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15599. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15600. ((equal deltaw "m") (setq month (+ month deltan)))
  15601. ((equal deltaw "y") (setq year (+ year deltan)))))
  15602. ((and wday (not (nth 3 tl)))
  15603. ;; Weekday was given, but no day, so pick that day in the week
  15604. ;; on or after the derived date.
  15605. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15606. (unless (equal wday wday1)
  15607. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15608. (when (and (boundp 'org-time-was-given)
  15609. (nth 2 tl))
  15610. (setq org-time-was-given t))
  15611. (when (< year 100) (setq year (+ 2000 year)))
  15612. ;; Check of the date is representable
  15613. (if org-read-date-force-compatible-dates
  15614. (progn
  15615. (when (< year 1970)
  15616. (setq year 1970 org-read-date-analyze-forced-year t))
  15617. (when (> year 2037)
  15618. (setq year 2037 org-read-date-analyze-forced-year t)))
  15619. (condition-case nil
  15620. (ignore (encode-time second minute hour day month year))
  15621. (error
  15622. (setq year (nth 5 org-defdecode))
  15623. (setq org-read-date-analyze-forced-year t))))
  15624. (setq org-read-date-analyze-futurep futurep)
  15625. (list second minute hour day month year)))
  15626. (defvar parse-time-weekdays)
  15627. (defun org-read-date-get-relative (s today default)
  15628. "Check string S for special relative date string.
  15629. TODAY and DEFAULT are internal times, for today and for a default.
  15630. Return shift list (N what def-flag)
  15631. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15632. N is the number of WHATs to shift.
  15633. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15634. the DEFAULT date rather than TODAY."
  15635. (require 'parse-time)
  15636. (when (and
  15637. (string-match
  15638. (concat
  15639. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15640. "\\([0-9]+\\)?"
  15641. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15642. "\\([ \t]\\|$\\)") s)
  15643. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15644. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15645. (string-to-char (substring (match-string 1 s) -1))
  15646. ?+))
  15647. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15648. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15649. (what (if (match-end 3) (match-string 3 s) "d"))
  15650. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15651. (date (if rel default today))
  15652. (wday (nth 6 (decode-time date)))
  15653. delta)
  15654. (if wday1
  15655. (progn
  15656. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15657. (when (= delta 0) (setq delta 7))
  15658. (when (= dir ?-)
  15659. (setq delta (- delta 7))
  15660. (when (= delta 0) (setq delta -7)))
  15661. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15662. (list delta "d" rel))
  15663. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15664. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15665. "Turn a user-specified date into the internal representation.
  15666. The internal representation needed by the calendar is (month day year).
  15667. This is a wrapper to handle the brain-dead convention in calendar that
  15668. user function argument order change dependent on argument order."
  15669. (pcase calendar-date-style
  15670. (`american (list arg1 arg2 arg3))
  15671. (`european (list arg2 arg1 arg3))
  15672. (`iso (list arg2 arg3 arg1))))
  15673. (defun org-eval-in-calendar (form &optional keepdate)
  15674. "Eval FORM in the calendar window and return to current window.
  15675. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15676. (let ((sf (selected-frame))
  15677. (sw (selected-window)))
  15678. (select-window (get-buffer-window "*Calendar*" t))
  15679. (eval form)
  15680. (when (and (not keepdate) (calendar-cursor-to-date))
  15681. (let* ((date (calendar-cursor-to-date))
  15682. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15683. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15684. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15685. (select-window sw)
  15686. (select-frame-set-input-focus sf)))
  15687. (defun org-calendar-select ()
  15688. "Return to `org-read-date' with the date currently selected.
  15689. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15690. (interactive)
  15691. (when (calendar-cursor-to-date)
  15692. (let* ((date (calendar-cursor-to-date))
  15693. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15694. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15695. (when (active-minibuffer-window) (exit-minibuffer))))
  15696. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15697. "Insert a date stamp for the date given by the internal TIME.
  15698. See `format-time-string' for the format of TIME.
  15699. WITH-HM means use the stamp format that includes the time of the day.
  15700. INACTIVE means use square brackets instead of angular ones, so that the
  15701. stamp will not contribute to the agenda.
  15702. PRE and POST are optional strings to be inserted before and after the
  15703. stamp.
  15704. The command returns the inserted time stamp."
  15705. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15706. stamp)
  15707. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15708. (insert-before-markers (or pre ""))
  15709. (when (listp extra)
  15710. (setq extra (car extra))
  15711. (if (and (stringp extra)
  15712. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15713. (setq extra (format "-%02d:%02d"
  15714. (string-to-number (match-string 1 extra))
  15715. (string-to-number (match-string 2 extra))))
  15716. (setq extra nil)))
  15717. (when extra
  15718. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15719. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15720. (insert-before-markers (or post ""))
  15721. (setq org-last-inserted-timestamp stamp)))
  15722. (defun org-toggle-time-stamp-overlays ()
  15723. "Toggle the use of custom time stamp formats."
  15724. (interactive)
  15725. (setq org-display-custom-times (not org-display-custom-times))
  15726. (unless org-display-custom-times
  15727. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15728. (while (setq p (next-single-property-change p 'display))
  15729. (when (and (get-text-property p 'display)
  15730. (eq (get-text-property p 'face) 'org-date))
  15731. (remove-text-properties
  15732. p (setq p (next-single-property-change p 'display))
  15733. '(display t))))
  15734. (set-buffer-modified-p bmp)))
  15735. (org-restart-font-lock)
  15736. (setq org-table-may-need-update t)
  15737. (if org-display-custom-times
  15738. (message "Time stamps are overlaid with custom format")
  15739. (message "Time stamp overlays removed")))
  15740. (defun org-display-custom-time (beg end)
  15741. "Overlay modified time stamp format over timestamp between BEG and END."
  15742. (let* ((ts (buffer-substring beg end))
  15743. t1 with-hm tf time str (off 0))
  15744. (save-match-data
  15745. (setq t1 (org-parse-time-string ts t))
  15746. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15747. (setq off (- (match-end 0) (match-beginning 0)))))
  15748. (setq end (- end off))
  15749. (setq with-hm (and (nth 1 t1) (nth 2 t1))
  15750. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15751. time (org-fix-decoded-time t1)
  15752. str (org-add-props
  15753. (format-time-string
  15754. (substring tf 1 -1) (apply 'encode-time time))
  15755. nil 'mouse-face 'highlight))
  15756. (put-text-property beg end 'display str)))
  15757. (defun org-fix-decoded-time (time)
  15758. "Set 0 instead of nil for the first 6 elements of time.
  15759. Don't touch the rest."
  15760. (let ((n 0))
  15761. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15762. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15763. "Difference between TIMESTAMP-STRING and now in days.
  15764. If SECONDS is non-nil, return the difference in seconds."
  15765. (let ((fdiff (if seconds #'float-time #'time-to-days)))
  15766. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15767. (funcall fdiff (current-time)))))
  15768. (defun org-deadline-close-p (timestamp-string &optional ndays)
  15769. "Is the time in TIMESTAMP-STRING close to the current date?"
  15770. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15771. (and (<= (org-time-stamp-to-now timestamp-string) ndays)
  15772. (not (org-entry-is-done-p))))
  15773. (defun org-get-wdays (ts &optional delay zero-delay)
  15774. "Get the deadline lead time appropriate for timestring TS.
  15775. When DELAY is non-nil, get the delay time for scheduled items
  15776. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15777. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15778. don't try to find the delay cookie in the scheduled timestamp."
  15779. (let ((tv (if delay org-scheduled-delay-days
  15780. org-deadline-warning-days)))
  15781. (cond
  15782. ((or (and delay (< tv 0))
  15783. (and delay zero-delay (<= tv 0))
  15784. (and (not delay) (<= tv 0)))
  15785. ;; Enforce this value no matter what
  15786. (- tv))
  15787. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15788. ;; lead time is specified.
  15789. (floor (* (string-to-number (match-string 1 ts))
  15790. (cdr (assoc (match-string 2 ts)
  15791. '(("d" . 1) ("w" . 7)
  15792. ("m" . 30.4) ("y" . 365.25)
  15793. ("h" . 0.041667)))))))
  15794. ;; go for the default.
  15795. (t tv))))
  15796. (defun org-calendar-select-mouse (ev)
  15797. "Return to `org-read-date' with the date currently selected.
  15798. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15799. (interactive "e")
  15800. (mouse-set-point ev)
  15801. (when (calendar-cursor-to-date)
  15802. (let* ((date (calendar-cursor-to-date))
  15803. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15804. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15805. (when (active-minibuffer-window) (exit-minibuffer))))
  15806. (defun org-check-deadlines (ndays)
  15807. "Check if there are any deadlines due or past due.
  15808. A deadline is considered due if it happens within `org-deadline-warning-days'
  15809. days from today's date. If the deadline appears in an entry marked DONE,
  15810. it is not shown. A numeric prefix argument NDAYS can be used to test that
  15811. many days. If the prefix is a raw `\\[universal-argument]', all deadlines \
  15812. are shown."
  15813. (interactive "P")
  15814. (let* ((org-warn-days
  15815. (cond
  15816. ((equal ndays '(4)) 100000)
  15817. (ndays (prefix-numeric-value ndays))
  15818. (t (abs org-deadline-warning-days))))
  15819. (case-fold-search nil)
  15820. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15821. (callback
  15822. (lambda () (org-deadline-close-p (match-string 1) org-warn-days))))
  15823. (message "%d deadlines past-due or due within %d days"
  15824. (org-occur regexp nil callback)
  15825. org-warn-days)))
  15826. (defsubst org-re-timestamp (type)
  15827. "Return a regexp for timestamp TYPE.
  15828. Allowed values for TYPE are:
  15829. all: all timestamps
  15830. active: only active timestamps (<...>)
  15831. inactive: only inactive timestamps ([...])
  15832. scheduled: only scheduled timestamps
  15833. deadline: only deadline timestamps
  15834. closed: only closed time-stamps
  15835. When TYPE is nil, fall back on returning a regexp that matches
  15836. both scheduled and deadline timestamps."
  15837. (cl-case type
  15838. (all org-ts-regexp-both)
  15839. (active org-ts-regexp)
  15840. (inactive org-ts-regexp-inactive)
  15841. (scheduled org-scheduled-time-regexp)
  15842. (deadline org-deadline-time-regexp)
  15843. (closed org-closed-time-regexp)
  15844. (otherwise
  15845. (concat "\\<"
  15846. (regexp-opt (list org-deadline-string org-scheduled-string))
  15847. " *<\\([^>]+\\)>"))))
  15848. (defun org-check-before-date (d)
  15849. "Check if there are deadlines or scheduled entries before date D."
  15850. (interactive (list (org-read-date)))
  15851. (let* ((case-fold-search nil)
  15852. (regexp (org-re-timestamp org-ts-type))
  15853. (ts-type org-ts-type)
  15854. (callback
  15855. (lambda ()
  15856. (let ((match (match-string 1)))
  15857. (and (if (memq ts-type '(active inactive all))
  15858. (eq (org-element-type (save-excursion
  15859. (backward-char)
  15860. (org-element-context)))
  15861. 'timestamp)
  15862. (org-at-planning-p))
  15863. (time-less-p
  15864. (org-time-string-to-time match t)
  15865. (org-time-string-to-time d t)))))))
  15866. (message "%d entries before %s"
  15867. (org-occur regexp nil callback)
  15868. d)))
  15869. (defun org-check-after-date (d)
  15870. "Check if there are deadlines or scheduled entries after date D."
  15871. (interactive (list (org-read-date)))
  15872. (let* ((case-fold-search nil)
  15873. (regexp (org-re-timestamp org-ts-type))
  15874. (ts-type org-ts-type)
  15875. (callback
  15876. (lambda ()
  15877. (let ((match (match-string 1)))
  15878. (and (if (memq ts-type '(active inactive all))
  15879. (eq (org-element-type (save-excursion
  15880. (backward-char)
  15881. (org-element-context)))
  15882. 'timestamp)
  15883. (org-at-planning-p))
  15884. (not (time-less-p
  15885. (org-time-string-to-time match t)
  15886. (org-time-string-to-time d t))))))))
  15887. (message "%d entries after %s"
  15888. (org-occur regexp nil callback)
  15889. d)))
  15890. (defun org-check-dates-range (start-date end-date)
  15891. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15892. (interactive (list (org-read-date nil nil nil "Range starts")
  15893. (org-read-date nil nil nil "Range end")))
  15894. (let ((case-fold-search nil)
  15895. (regexp (org-re-timestamp org-ts-type))
  15896. (callback
  15897. (let ((type org-ts-type))
  15898. (lambda ()
  15899. (let ((match (match-string 1)))
  15900. (and
  15901. (if (memq type '(active inactive all))
  15902. (eq (org-element-type (save-excursion
  15903. (backward-char)
  15904. (org-element-context)))
  15905. 'timestamp)
  15906. (org-at-planning-p))
  15907. (not (time-less-p
  15908. (org-time-string-to-time match t)
  15909. (org-time-string-to-time start-date t)))
  15910. (time-less-p
  15911. (org-time-string-to-time match t)
  15912. (org-time-string-to-time end-date t))))))))
  15913. (message "%d entries between %s and %s"
  15914. (org-occur regexp nil callback) start-date end-date)))
  15915. (defun org-evaluate-time-range (&optional to-buffer)
  15916. "Evaluate a time range by computing the difference between start and end.
  15917. Normally the result is just printed in the echo area, but with prefix arg
  15918. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15919. If the time range is actually in a table, the result is inserted into the
  15920. next column.
  15921. For time difference computation, a year is assumed to be exactly 365
  15922. days in order to avoid rounding problems."
  15923. (interactive "P")
  15924. (or
  15925. (org-clock-update-time-maybe)
  15926. (save-excursion
  15927. (unless (org-at-date-range-p t)
  15928. (goto-char (point-at-bol))
  15929. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15930. (unless (org-at-date-range-p t)
  15931. (user-error "Not at a time-stamp range, and none found in current line")))
  15932. (let* ((ts1 (match-string 1))
  15933. (ts2 (match-string 2))
  15934. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15935. (match-end (match-end 0))
  15936. (time1 (org-time-string-to-time ts1))
  15937. (time2 (org-time-string-to-time ts2))
  15938. (t1 (float-time time1))
  15939. (t2 (float-time time2))
  15940. (diff (abs (- t2 t1)))
  15941. (negative (< (- t2 t1) 0))
  15942. ;; (ys (floor (* 365 24 60 60)))
  15943. (ds (* 24 60 60))
  15944. (hs (* 60 60))
  15945. (fy "%dy %dd %02d:%02d")
  15946. (fy1 "%dy %dd")
  15947. (fd "%dd %02d:%02d")
  15948. (fd1 "%dd")
  15949. (fh "%02d:%02d")
  15950. y d h m align)
  15951. (if havetime
  15952. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15953. y 0
  15954. d (floor (/ diff ds)) diff (mod diff ds)
  15955. h (floor (/ diff hs)) diff (mod diff hs)
  15956. m (floor (/ diff 60)))
  15957. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15958. y 0
  15959. d (floor (+ (/ diff ds) 0.5))
  15960. h 0 m 0))
  15961. (if (not to-buffer)
  15962. (message "%s" (org-make-tdiff-string y d h m))
  15963. (if (org-at-table-p)
  15964. (progn
  15965. (goto-char match-end)
  15966. (setq align t)
  15967. (and (looking-at " *|") (goto-char (match-end 0))))
  15968. (goto-char match-end))
  15969. (when (looking-at
  15970. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15971. (replace-match ""))
  15972. (when negative (insert " -"))
  15973. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15974. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15975. (insert " " (format fh h m))))
  15976. (when align (org-table-align))
  15977. (message "Time difference inserted")))))
  15978. (defun org-make-tdiff-string (y d h m)
  15979. (let ((fmt "")
  15980. (l nil))
  15981. (when (> y 0)
  15982. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  15983. (push y l))
  15984. (when (> d 0)
  15985. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  15986. (push d l))
  15987. (when (> h 0)
  15988. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  15989. (push h l))
  15990. (when (> m 0)
  15991. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  15992. (push m l))
  15993. (apply 'format fmt (nreverse l))))
  15994. (defun org-time-string-to-time (s &optional zone)
  15995. "Convert timestamp string S into internal time.
  15996. The optional ZONE is omitted or nil for Emacs local time, t for
  15997. Universal Time, ‘wall’ for system wall clock time, or a string as
  15998. in the TZ environment variable."
  15999. (apply #'encode-time (org-parse-time-string s nil zone)))
  16000. (defun org-time-string-to-seconds (s &optional zone)
  16001. "Convert a timestamp string S into a number of seconds.
  16002. The optional ZONE is omitted or nil for Emacs local time, t for
  16003. Universal Time, ‘wall’ for system wall clock time, or a string as
  16004. in the TZ environment variable."
  16005. (float-time (org-time-string-to-time s zone)))
  16006. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16007. (defun org-time-string-to-absolute (s &optional daynr prefer buffer pos)
  16008. "Convert time stamp S to an absolute day number.
  16009. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16010. stamp, get the closest date to DAYNR. If PREFER is
  16011. `past' (respectively `future') return a date past (respectively
  16012. after) or equal to DAYNR.
  16013. POS is the location of time stamp S, as a buffer position in
  16014. BUFFER.
  16015. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16016. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16017. signaled."
  16018. (cond
  16019. ((string-match "\\`%%\\((.*)\\)" s)
  16020. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16021. ;; only able to match individual dates. If it fails, raise an
  16022. ;; error.
  16023. (if (and daynr
  16024. (org-diary-sexp-entry
  16025. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16026. daynr
  16027. (signal 'org-diary-sexp-no-match (list s))))
  16028. (daynr (org-closest-date s daynr prefer))
  16029. (t (time-to-days
  16030. (condition-case errdata
  16031. (apply #'encode-time (org-parse-time-string s))
  16032. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16033. s
  16034. (if (not (and buffer pos)) ""
  16035. (format-message " at %d in buffer `%s'" pos buffer))
  16036. (cdr errdata))))))))
  16037. (defun org-days-to-iso-week (days)
  16038. "Return the iso week number."
  16039. (require 'cal-iso)
  16040. (car (calendar-iso-from-absolute days)))
  16041. (defun org-small-year-to-year (year)
  16042. "Convert 2-digit years into 4-digit years.
  16043. YEAR is expanded into one of the 30 next years, if possible, or
  16044. into a past one. Any year larger than 99 is returned unchanged."
  16045. (if (>= year 100) year
  16046. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16047. (century (/ current 100))
  16048. (offset (- year (% current 100))))
  16049. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16050. ((> offset -70) (+ (* century 100) year))
  16051. (t (+ (* (1+ century) 100) year))))))
  16052. (defun org-time-from-absolute (d)
  16053. "Return the time corresponding to date D.
  16054. D may be an absolute day number, or a calendar-type list (month day year)."
  16055. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16056. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16057. (defvar org-agenda-current-date)
  16058. (defun org-calendar-holiday ()
  16059. "List of holidays, for Diary display in Org mode."
  16060. (require 'holidays)
  16061. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16062. (and hl (mapconcat #'identity hl "; "))))
  16063. (defun org-diary-sexp-entry (sexp entry d)
  16064. "Process a SEXP diary ENTRY for date D."
  16065. (require 'diary-lib)
  16066. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16067. ;; dynamically.
  16068. (let* ((sexp `(let ((entry ,entry)
  16069. (date ',d))
  16070. ,(car (read-from-string sexp))))
  16071. (result (if calendar-debug-sexp (eval sexp)
  16072. (condition-case nil
  16073. (eval sexp)
  16074. (error
  16075. (beep)
  16076. (message "Bad sexp at line %d in %s: %s"
  16077. (org-current-line)
  16078. (buffer-file-name) sexp)
  16079. (sleep-for 2))))))
  16080. (cond ((stringp result) (split-string result "; "))
  16081. ((and (consp result)
  16082. (not (consp (cdr result)))
  16083. (stringp (cdr result))) (cdr result))
  16084. ((and (consp result)
  16085. (stringp (car result))) result)
  16086. (result entry))))
  16087. (defun org-diary-to-ical-string (frombuf)
  16088. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16089. This uses the icalendar.el library."
  16090. (let* ((tmpdir temporary-file-directory)
  16091. (tmpfile (make-temp-name
  16092. (expand-file-name "orgics" tmpdir)))
  16093. buf rtn b e)
  16094. (with-current-buffer frombuf
  16095. (icalendar-export-region (point-min) (point-max) tmpfile)
  16096. (setq buf (find-buffer-visiting tmpfile))
  16097. (set-buffer buf)
  16098. (goto-char (point-min))
  16099. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16100. (setq b (match-beginning 0)))
  16101. (goto-char (point-max))
  16102. (when (re-search-backward "^END:VEVENT" nil t)
  16103. (setq e (match-end 0)))
  16104. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16105. (kill-buffer buf)
  16106. (delete-file tmpfile)
  16107. rtn))
  16108. (defun org-closest-date (start current prefer)
  16109. "Return closest date to CURRENT starting from START.
  16110. CURRENT and START are both time stamps.
  16111. When PREFER is `past', return a date that is either CURRENT or
  16112. past. When PREFER is `future', return a date that is either
  16113. CURRENT or future.
  16114. Only time stamps with a repeater are modified. Any other time
  16115. stamp stay unchanged. In any case, return value is an absolute
  16116. day number."
  16117. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16118. ;; No repeater. Do not shift time stamp.
  16119. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16120. (let ((value (string-to-number (match-string 1 start)))
  16121. (type (match-string 2 start)))
  16122. (if (= 0 value)
  16123. ;; Repeater with a 0-value is considered as void.
  16124. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16125. (let* ((base (org-date-to-gregorian start))
  16126. (target (org-date-to-gregorian current))
  16127. (sday (calendar-absolute-from-gregorian base))
  16128. (cday (calendar-absolute-from-gregorian target))
  16129. n1 n2)
  16130. ;; If START is already past CURRENT, just return START.
  16131. (if (<= cday sday) sday
  16132. ;; Compute closest date before (N1) and closest date past
  16133. ;; (N2) CURRENT.
  16134. (pcase type
  16135. ("h"
  16136. (let ((missing-hours
  16137. (mod (+ (- (* 24 (- cday sday))
  16138. (nth 2 (org-parse-time-string start)))
  16139. org-extend-today-until)
  16140. value)))
  16141. (setf n1 (if (= missing-hours 0) cday
  16142. (- cday (1+ (/ missing-hours 24)))))
  16143. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16144. ((or "d" "w")
  16145. (let ((value (if (equal type "w") (* 7 value) value)))
  16146. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16147. (setf n2 (+ n1 value))))
  16148. ("m"
  16149. (let* ((add-months
  16150. (lambda (d n)
  16151. ;; Add N months to gregorian date D, i.e.,
  16152. ;; a list (MONTH DAY YEAR). Return a valid
  16153. ;; gregorian date.
  16154. (let ((m (+ (nth 0 d) n)))
  16155. (list (mod m 12)
  16156. (nth 1 d)
  16157. (+ (/ m 12) (nth 2 d))))))
  16158. (months ; Complete months to TARGET.
  16159. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16160. (- (nth 0 target) (nth 0 base))
  16161. ;; If START's day is greater than
  16162. ;; TARGET's, remove incomplete month.
  16163. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16164. value)
  16165. value))
  16166. (before (funcall add-months base months)))
  16167. (setf n1 (calendar-absolute-from-gregorian before))
  16168. (setf n2
  16169. (calendar-absolute-from-gregorian
  16170. (funcall add-months before value)))))
  16171. (_
  16172. (let* ((d (nth 1 base))
  16173. (m (nth 0 base))
  16174. (y (nth 2 base))
  16175. (years ; Complete years to TARGET.
  16176. (* (/ (- (nth 2 target)
  16177. y
  16178. ;; If START's month and day are
  16179. ;; greater than TARGET's, remove
  16180. ;; incomplete year.
  16181. (if (or (> (nth 0 target) m)
  16182. (and (= (nth 0 target) m)
  16183. (> (nth 1 target) d)))
  16184. 0
  16185. 1))
  16186. value)
  16187. value))
  16188. (before (list m d (+ y years))))
  16189. (setf n1 (calendar-absolute-from-gregorian before))
  16190. (setf n2 (calendar-absolute-from-gregorian
  16191. (list m d (+ (nth 2 before) value)))))))
  16192. ;; Handle PREFER parameter, if any.
  16193. (cond
  16194. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16195. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16196. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16197. (defun org-date-to-gregorian (d)
  16198. "Turn any specification of date D into a Gregorian date for the calendar."
  16199. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16200. ((and (listp d) (= (length d) 3)) d)
  16201. ((stringp d)
  16202. (let ((d (org-parse-time-string d)))
  16203. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16204. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16205. (defun org-parse-time-string (s &optional nodefault zone)
  16206. "Parse the standard Org time string.
  16207. This should be a lot faster than the normal `parse-time-string'.
  16208. If time is not given, defaults to 0:00. However, with optional
  16209. NODEFAULT, hour and minute fields will be nil if not given.
  16210. The optional ZONE is omitted or nil for Emacs local time, t for
  16211. Universal Time, ‘wall’ for system wall clock time, or a string as
  16212. in the TZ environment variable."
  16213. (cond ((string-match org-ts-regexp0 s)
  16214. (list 0
  16215. (when (or (match-beginning 8) (not nodefault))
  16216. (string-to-number (or (match-string 8 s) "0")))
  16217. (when (or (match-beginning 7) (not nodefault))
  16218. (string-to-number (or (match-string 7 s) "0")))
  16219. (string-to-number (match-string 4 s))
  16220. (string-to-number (match-string 3 s))
  16221. (string-to-number (match-string 2 s))
  16222. nil nil zone))
  16223. ((string-match "^<[^>]+>$" s)
  16224. ;; FIXME: `decode-time' needs to be called with ZONE as its
  16225. ;; second argument. However, this requires at least Emacs
  16226. ;; 25.1. We can do it when we switch to this version as our
  16227. ;; minimal requirement.
  16228. (decode-time (seconds-to-time (org-matcher-time s))))
  16229. (t (error "Not a standard Org time string: %s" s))))
  16230. (defun org-timestamp-up (&optional arg)
  16231. "Increase the date item at the cursor by one.
  16232. If the cursor is on the year, change the year. If it is on the month,
  16233. the day or the time, change that.
  16234. With prefix ARG, change by that many units."
  16235. (interactive "p")
  16236. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16237. (defun org-timestamp-down (&optional arg)
  16238. "Decrease the date item at the cursor by one.
  16239. If the cursor is on the year, change the year. If it is on the month,
  16240. the day or the time, change that.
  16241. With prefix ARG, change by that many units."
  16242. (interactive "p")
  16243. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16244. (defun org-timestamp-up-day (&optional arg)
  16245. "Increase the date in the time stamp by one day.
  16246. With prefix ARG, change that many days."
  16247. (interactive "p")
  16248. (if (and (not (org-at-timestamp-p 'lax))
  16249. (org-at-heading-p))
  16250. (org-todo 'up)
  16251. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16252. (defun org-timestamp-down-day (&optional arg)
  16253. "Decrease the date in the time stamp by one day.
  16254. With prefix ARG, change that many days."
  16255. (interactive "p")
  16256. (if (and (not (org-at-timestamp-p 'lax))
  16257. (org-at-heading-p))
  16258. (org-todo 'down)
  16259. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16260. (defun org-at-timestamp-p (&optional extended)
  16261. "Non-nil if point is inside a timestamp.
  16262. By default, the function only consider syntactically valid active
  16263. timestamps. However, the caller may have a broader definition
  16264. for timestamps. As a consequence, optional argument EXTENDED can
  16265. be set to the following values
  16266. `inactive'
  16267. Include also syntactically valid inactive timestamps.
  16268. `agenda'
  16269. Include timestamps allowed in Agenda, i.e., those in
  16270. properties drawers, planning lines and clock lines.
  16271. `lax'
  16272. Ignore context. The function matches any part of the
  16273. document looking like a timestamp. This includes comments,
  16274. example blocks...
  16275. For backward-compatibility with Org 9.0, every other non-nil
  16276. value is equivalent to `inactive'.
  16277. When at a timestamp, return the position of the point as a symbol
  16278. among `bracket', `after', `year', `month', `hour', `minute',
  16279. `day' or a number of character from the last know part of the
  16280. time stamp.
  16281. When matching, the match groups are the following:
  16282. group 1: year
  16283. group 2: month
  16284. group 3: day number
  16285. group 4: day name
  16286. group 5: hours, if any
  16287. group 6: minutes, if any"
  16288. (let* ((regexp (if extended org-ts-regexp3 org-ts-regexp2))
  16289. (pos (point))
  16290. (match?
  16291. (let ((boundaries (org-in-regexp regexp)))
  16292. (save-match-data
  16293. (cond ((null boundaries) nil)
  16294. ((eq extended 'lax) t)
  16295. (t
  16296. (or (and (eq extended 'agenda)
  16297. (or (org-at-planning-p)
  16298. (org-at-property-p)
  16299. (and (bound-and-true-p
  16300. org-agenda-include-inactive-timestamps)
  16301. (org-at-clock-log-p))))
  16302. (eq 'timestamp
  16303. (save-excursion
  16304. (when (= pos (cdr boundaries)) (forward-char -1))
  16305. (org-element-type (org-element-context)))))))))))
  16306. (cond
  16307. ((not match?) nil)
  16308. ((= pos (match-beginning 0)) 'bracket)
  16309. ;; Distinguish location right before the closing bracket from
  16310. ;; right after it.
  16311. ((= pos (1- (match-end 0))) 'bracket)
  16312. ((= pos (match-end 0)) 'after)
  16313. ((org-pos-in-match-range pos 2) 'year)
  16314. ((org-pos-in-match-range pos 3) 'month)
  16315. ((org-pos-in-match-range pos 7) 'hour)
  16316. ((org-pos-in-match-range pos 8) 'minute)
  16317. ((or (org-pos-in-match-range pos 4)
  16318. (org-pos-in-match-range pos 5)) 'day)
  16319. ((and (> pos (or (match-end 8) (match-end 5)))
  16320. (< pos (match-end 0)))
  16321. (- pos (or (match-end 8) (match-end 5))))
  16322. (t 'day))))
  16323. (defun org-toggle-timestamp-type ()
  16324. "Toggle the type (<active> or [inactive]) of a time stamp."
  16325. (interactive)
  16326. (when (org-at-timestamp-p 'lax)
  16327. (let ((beg (match-beginning 0)) (end (match-end 0))
  16328. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16329. (save-excursion
  16330. (goto-char beg)
  16331. (while (re-search-forward "[][<>]" end t)
  16332. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16333. t t)))
  16334. (message "Timestamp is now %sactive"
  16335. (if (equal (char-after beg) ?<) "" "in")))))
  16336. (defun org-at-clock-log-p ()
  16337. "Non-nil if point is on a clock log line."
  16338. (and (org-match-line org-clock-line-re)
  16339. (eq (org-element-type (save-match-data (org-element-at-point))) 'clock)))
  16340. (defvar org-clock-history) ; defined in org-clock.el
  16341. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16342. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16343. "Change the date in the time stamp at point.
  16344. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16345. `year', `minute', `second'. If WHAT is not given, the cursor position
  16346. in the timestamp determines what will be changed.
  16347. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16348. (let ((origin (point))
  16349. (timestamp? (org-at-timestamp-p 'lax))
  16350. origin-cat
  16351. with-hm inactive
  16352. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16353. extra rem
  16354. ts time time0 fixnext clrgx)
  16355. (unless timestamp? (user-error "Not at a timestamp"))
  16356. (if (and (not what) (eq timestamp? 'bracket))
  16357. (org-toggle-timestamp-type)
  16358. ;; Point isn't on brackets. Remember the part of the time-stamp
  16359. ;; the point was in. Indeed, size of time-stamps may change,
  16360. ;; but point must be kept in the same category nonetheless.
  16361. (setq origin-cat timestamp?)
  16362. (when (and (not what) (not (eq timestamp? 'day))
  16363. org-display-custom-times
  16364. (get-text-property (point) 'display)
  16365. (not (get-text-property (1- (point)) 'display)))
  16366. (setq timestamp? 'day))
  16367. (setq timestamp? (or what timestamp?)
  16368. inactive (= (char-after (match-beginning 0)) ?\[)
  16369. ts (match-string 0))
  16370. (replace-match "")
  16371. (when (string-match
  16372. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16373. ts)
  16374. (setq extra (match-string 1 ts))
  16375. (when suppress-tmp-delay
  16376. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16377. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16378. (setq with-hm t))
  16379. (setq time0 (org-parse-time-string ts))
  16380. (when (and updown
  16381. (eq timestamp? 'minute)
  16382. (not current-prefix-arg))
  16383. ;; This looks like s-up and s-down. Change by one rounding step.
  16384. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16385. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16386. (setcar (cdr time0) (+ (nth 1 time0)
  16387. (if (> n 0) (- rem) (- dm rem))))))
  16388. (setq time
  16389. (apply #'encode-time
  16390. (or (car time0) 0)
  16391. (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0))
  16392. (+ (if (eq timestamp? 'hour) n 0) (nth 2 time0))
  16393. (+ (if (eq timestamp? 'day) n 0) (nth 3 time0))
  16394. (+ (if (eq timestamp? 'month) n 0) (nth 4 time0))
  16395. (+ (if (eq timestamp? 'year) n 0) (nth 5 time0))
  16396. (nthcdr 6 time0)))
  16397. (when (and (memq timestamp? '(hour minute))
  16398. extra
  16399. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16400. (setq extra (org-modify-ts-extra
  16401. extra
  16402. (if (eq timestamp? 'hour) 2 5)
  16403. n dm)))
  16404. (when (integerp timestamp?)
  16405. (setq extra (org-modify-ts-extra extra timestamp? n dm)))
  16406. (when (eq what 'calendar)
  16407. (let ((cal-date (org-get-date-from-calendar)))
  16408. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16409. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16410. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16411. (setcar time0 (or (car time0) 0))
  16412. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16413. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16414. (setq time (apply 'encode-time time0))))
  16415. ;; Insert the new time-stamp, and ensure point stays in the same
  16416. ;; category as before (i.e. not after the last position in that
  16417. ;; category).
  16418. (let ((pos (point)))
  16419. ;; Stay before inserted string. `save-excursion' is of no use.
  16420. (setq org-last-changed-timestamp
  16421. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16422. (goto-char pos))
  16423. (save-match-data
  16424. (looking-at org-ts-regexp3)
  16425. (goto-char
  16426. (pcase origin-cat
  16427. ;; `day' category ends before `hour' if any, or at the end
  16428. ;; of the day name.
  16429. (`day (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16430. (`hour (min (match-end 7) origin))
  16431. (`minute (min (1- (match-end 8)) origin))
  16432. ((pred integerp) (min (1- (match-end 0)) origin))
  16433. ;; Point was right after the time-stamp. However, the
  16434. ;; time-stamp length might have changed, so refer to
  16435. ;; (match-end 0) instead.
  16436. (`after (match-end 0))
  16437. ;; `year' and `month' have both fixed size: point couldn't
  16438. ;; have moved into another part.
  16439. (_ origin))))
  16440. ;; Update clock if on a CLOCK line.
  16441. (org-clock-update-time-maybe)
  16442. ;; Maybe adjust the closest clock in `org-clock-history'
  16443. (when org-clock-adjust-closest
  16444. (if (not (and (org-at-clock-log-p)
  16445. (< 1 (length (delq nil (mapcar 'marker-position
  16446. org-clock-history))))))
  16447. (message "No clock to adjust")
  16448. (cond ((save-excursion ; fix previous clock?
  16449. (re-search-backward org-ts-regexp0 nil t)
  16450. (looking-back (concat org-clock-string " \\[")
  16451. (line-beginning-position)))
  16452. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16453. ((save-excursion ; fix next clock?
  16454. (re-search-backward org-ts-regexp0 nil t)
  16455. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16456. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16457. (save-window-excursion
  16458. ;; Find closest clock to point, adjust the previous/next one in history
  16459. (let* ((p (save-excursion (org-back-to-heading t)))
  16460. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16461. (clfixnth
  16462. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16463. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16464. (if (not clfixpos)
  16465. (message "No clock to adjust")
  16466. (save-excursion
  16467. (org-goto-marker-or-bmk clfixpos)
  16468. (org-show-subtree)
  16469. (when (re-search-forward clrgx nil t)
  16470. (goto-char (match-beginning 1))
  16471. (let (org-clock-adjust-closest)
  16472. (org-timestamp-change n timestamp? updown))
  16473. (message "Clock adjusted in %s for heading: %s"
  16474. (file-name-nondirectory (buffer-file-name))
  16475. (org-get-heading t t)))))))))
  16476. ;; Try to recenter the calendar window, if any.
  16477. (when (and org-calendar-follow-timestamp-change
  16478. (get-buffer-window "*Calendar*" t)
  16479. (memq timestamp? '(day month year)))
  16480. (org-recenter-calendar (time-to-days time))))))
  16481. (defun org-modify-ts-extra (s pos n dm)
  16482. "Change the different parts of the lead-time and repeat fields in timestamp."
  16483. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16484. ng h m new rem)
  16485. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16486. (cond
  16487. ((or (org-pos-in-match-range pos 2)
  16488. (org-pos-in-match-range pos 3))
  16489. (setq m (string-to-number (match-string 3 s))
  16490. h (string-to-number (match-string 2 s)))
  16491. (if (org-pos-in-match-range pos 2)
  16492. (setq h (+ h n))
  16493. (setq n (* dm (with-no-warnings (signum n))))
  16494. (unless (= 0 (setq rem (% m dm)))
  16495. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16496. (setq m (+ m n)))
  16497. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16498. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16499. (setq h (mod h 24))
  16500. (setq ng 1 new (format "-%02d:%02d" h m)))
  16501. ((org-pos-in-match-range pos 6)
  16502. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16503. ((org-pos-in-match-range pos 5)
  16504. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16505. ((org-pos-in-match-range pos 9)
  16506. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16507. ((org-pos-in-match-range pos 8)
  16508. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16509. (when ng
  16510. (setq s (concat
  16511. (substring s 0 (match-beginning ng))
  16512. new
  16513. (substring s (match-end ng))))))
  16514. s))
  16515. (defun org-recenter-calendar (d)
  16516. "If the calendar is visible, recenter it to date D."
  16517. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16518. (when cwin
  16519. (let ((calendar-move-hook nil))
  16520. (with-selected-window cwin
  16521. (calendar-goto-date
  16522. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16523. (defun org-goto-calendar (&optional arg)
  16524. "Go to the Emacs calendar at the current date.
  16525. If there is a time stamp in the current line, go to that date.
  16526. A prefix ARG can be used to force the current date."
  16527. (interactive "P")
  16528. (let ((calendar-move-hook nil)
  16529. (calendar-view-holidays-initially-flag nil)
  16530. (calendar-view-diary-initially-flag nil)
  16531. diff)
  16532. (when (or (org-at-timestamp-p 'lax)
  16533. (org-match-line (concat ".*" org-ts-regexp)))
  16534. (let ((d1 (time-to-days (current-time)))
  16535. (d2 (time-to-days (org-time-string-to-time (match-string 1)))))
  16536. (setq diff (- d2 d1))))
  16537. (calendar)
  16538. (calendar-goto-today)
  16539. (when (and diff (not arg)) (calendar-forward-day diff))))
  16540. (defun org-get-date-from-calendar ()
  16541. "Return a list (month day year) of date at point in calendar."
  16542. (with-current-buffer "*Calendar*"
  16543. (save-match-data
  16544. (calendar-cursor-to-date))))
  16545. (defun org-date-from-calendar ()
  16546. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16547. If there is already a time stamp at the cursor position, update it."
  16548. (interactive)
  16549. (if (org-at-timestamp-p 'lax)
  16550. (org-timestamp-change 0 'calendar)
  16551. (let ((cal-date (org-get-date-from-calendar)))
  16552. (org-insert-time-stamp
  16553. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16554. (defcustom org-effort-durations
  16555. `(("min" . 1)
  16556. ("h" . 60)
  16557. ("d" . ,(* 60 8))
  16558. ("w" . ,(* 60 8 5))
  16559. ("m" . ,(* 60 8 5 4))
  16560. ("y" . ,(* 60 8 5 40)))
  16561. "Conversion factor to minutes for an effort modifier.
  16562. Each entry has the form (MODIFIER . MINUTES).
  16563. In an effort string, a number followed by MODIFIER is multiplied
  16564. by the specified number of MINUTES to obtain an effort in
  16565. minutes.
  16566. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16567. effort string \"2hours\" is equivalent to 120 minutes."
  16568. :group 'org-agenda
  16569. :version "26.1"
  16570. :package-version '(Org . "8.3")
  16571. :type '(alist :key-type (string :tag "Modifier")
  16572. :value-type (number :tag "Minutes")))
  16573. (defcustom org-image-actual-width t
  16574. "Should we use the actual width of images when inlining them?
  16575. When set to t, always use the image width.
  16576. When set to a number, use imagemagick (when available) to set
  16577. the image's width to this value.
  16578. When set to a number in a list, try to get the width from any
  16579. #+ATTR.* keyword if it matches a width specification like
  16580. #+ATTR_HTML: :width 300px
  16581. and fall back on that number if none is found.
  16582. When set to nil, try to get the width from an #+ATTR.* keyword
  16583. and fall back on the original width if none is found.
  16584. This requires Emacs >= 24.1, build with imagemagick support."
  16585. :group 'org-appearance
  16586. :version "24.4"
  16587. :package-version '(Org . "8.0")
  16588. :type '(choice
  16589. (const :tag "Use the image width" t)
  16590. (integer :tag "Use a number of pixels")
  16591. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16592. (const :tag "Use #+ATTR* or don't resize" nil)))
  16593. (defcustom org-agenda-inhibit-startup nil
  16594. "Inhibit startup when preparing agenda buffers.
  16595. When this variable is t, the initialization of the Org agenda
  16596. buffers is inhibited: e.g. the visibility state is not set, the
  16597. tables are not re-aligned, etc."
  16598. :type 'boolean
  16599. :version "24.3"
  16600. :group 'org-agenda)
  16601. (defcustom org-agenda-ignore-properties nil
  16602. "Avoid updating text properties when building the agenda.
  16603. Properties are used to prepare buffers for effort estimates,
  16604. appointments, statistics and subtree-local categories.
  16605. If you don't use these in the agenda, you can add them to this
  16606. list and agenda building will be a bit faster.
  16607. The value is a list, with zero or more of the symbols `effort', `appt',
  16608. `stats' or `category'."
  16609. :type '(set :greedy t
  16610. (const effort)
  16611. (const appt)
  16612. (const stats)
  16613. (const category))
  16614. :version "26.1"
  16615. :package-version '(Org . "8.3")
  16616. :group 'org-agenda)
  16617. ;;;; Files
  16618. (defun org-save-all-org-buffers ()
  16619. "Save all Org buffers without user confirmation."
  16620. (interactive)
  16621. (message "Saving all Org buffers...")
  16622. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16623. (when (featurep 'org-id) (org-id-locations-save))
  16624. (message "Saving all Org buffers... done"))
  16625. (defun org-revert-all-org-buffers ()
  16626. "Revert all Org buffers.
  16627. Prompt for confirmation when there are unsaved changes.
  16628. Be sure you know what you are doing before letting this function
  16629. overwrite your changes.
  16630. This function is useful in a setup where one tracks org files
  16631. with a version control system, to revert on one machine after pulling
  16632. changes from another. I believe the procedure must be like this:
  16633. 1. M-x org-save-all-org-buffers
  16634. 2. Pull changes from the other machine, resolve conflicts
  16635. 3. M-x org-revert-all-org-buffers"
  16636. (interactive)
  16637. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16638. (user-error "Abort"))
  16639. (save-excursion
  16640. (save-window-excursion
  16641. (dolist (b (buffer-list))
  16642. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16643. (with-current-buffer b buffer-file-name))
  16644. (pop-to-buffer-same-window b)
  16645. (revert-buffer t 'no-confirm)))
  16646. (when (and (featurep 'org-id) org-id-track-globally)
  16647. (org-id-locations-load)))))
  16648. ;;;; Agenda files
  16649. ;;;###autoload
  16650. (defun org-switchb (&optional arg)
  16651. "Switch between Org buffers.
  16652. With `\\[universal-argument]' prefix, restrict available buffers to files.
  16653. With `\\[universal-argument] \\[universal-argument]' \
  16654. prefix, restrict available buffers to agenda files."
  16655. (interactive "P")
  16656. (let ((blist (org-buffer-list
  16657. (cond ((equal arg '(4)) 'files)
  16658. ((equal arg '(16)) 'agenda)))))
  16659. (pop-to-buffer-same-window
  16660. (completing-read "Org buffer: "
  16661. (mapcar #'list (mapcar #'buffer-name blist))
  16662. nil t))))
  16663. (defun org-buffer-list (&optional predicate exclude-tmp)
  16664. "Return a list of Org buffers.
  16665. PREDICATE can be `export', `files' or `agenda'.
  16666. export restrict the list to Export buffers.
  16667. files restrict the list to buffers visiting Org files.
  16668. agenda restrict the list to buffers visiting agenda files.
  16669. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16670. (let* ((bfn nil)
  16671. (agenda-files (and (eq predicate 'agenda)
  16672. (mapcar 'file-truename (org-agenda-files t))))
  16673. (filter
  16674. (cond
  16675. ((eq predicate 'files)
  16676. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16677. ((eq predicate 'export)
  16678. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16679. ((eq predicate 'agenda)
  16680. (lambda (b)
  16681. (with-current-buffer b
  16682. (and (derived-mode-p 'org-mode)
  16683. (setq bfn (buffer-file-name b))
  16684. (member (file-truename bfn) agenda-files)))))
  16685. (t (lambda (b) (with-current-buffer b
  16686. (or (derived-mode-p 'org-mode)
  16687. (string-match "\\*Org .*Export"
  16688. (buffer-name b)))))))))
  16689. (delq nil
  16690. (mapcar
  16691. (lambda(b)
  16692. (if (and (funcall filter b)
  16693. (or (not exclude-tmp)
  16694. (not (string-match "tmp" (buffer-name b)))))
  16695. b
  16696. nil))
  16697. (buffer-list)))))
  16698. (defun org-agenda-files (&optional unrestricted archives)
  16699. "Get the list of agenda files.
  16700. Optional UNRESTRICTED means return the full list even if a restriction
  16701. is currently in place.
  16702. When ARCHIVES is t, include all archive files that are really being
  16703. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16704. `org-agenda-archives-mode' is t."
  16705. (let ((files
  16706. (cond
  16707. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16708. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16709. ((listp org-agenda-files) org-agenda-files)
  16710. (t (error "Invalid value of `org-agenda-files'")))))
  16711. (setq files (apply 'append
  16712. (mapcar (lambda (f)
  16713. (if (file-directory-p f)
  16714. (directory-files
  16715. f t org-agenda-file-regexp)
  16716. (list f)))
  16717. files)))
  16718. (when org-agenda-skip-unavailable-files
  16719. (setq files (delq nil
  16720. (mapcar (function
  16721. (lambda (file)
  16722. (and (file-readable-p file) file)))
  16723. files))))
  16724. (when (or (eq archives t)
  16725. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16726. (setq files (org-add-archive-files files)))
  16727. files))
  16728. (defun org-agenda-file-p (&optional file)
  16729. "Return non-nil, if FILE is an agenda file.
  16730. If FILE is omitted, use the file associated with the current
  16731. buffer."
  16732. (let ((fname (or file (buffer-file-name))))
  16733. (and fname
  16734. (member (file-truename fname)
  16735. (mapcar #'file-truename (org-agenda-files t))))))
  16736. (defun org-edit-agenda-file-list ()
  16737. "Edit the list of agenda files.
  16738. Depending on setup, this either uses customize to edit the variable
  16739. `org-agenda-files', or it visits the file that is holding the list. In the
  16740. latter case, the buffer is set up in a way that saving it automatically kills
  16741. the buffer and restores the previous window configuration."
  16742. (interactive)
  16743. (if (stringp org-agenda-files)
  16744. (let ((cw (current-window-configuration)))
  16745. (find-file org-agenda-files)
  16746. (setq-local org-window-configuration cw)
  16747. (add-hook 'after-save-hook
  16748. (lambda ()
  16749. (set-window-configuration
  16750. (prog1 org-window-configuration
  16751. (kill-buffer (current-buffer))))
  16752. (org-install-agenda-files-menu)
  16753. (message "New agenda file list installed"))
  16754. nil 'local)
  16755. (message "%s" (substitute-command-keys
  16756. "Edit list and finish with \\[save-buffer]")))
  16757. (customize-variable 'org-agenda-files)))
  16758. (defun org-store-new-agenda-file-list (list)
  16759. "Set new value for the agenda file list and save it correctly."
  16760. (if (stringp org-agenda-files)
  16761. (let ((fe (org-read-agenda-file-list t)) b u)
  16762. (while (setq b (find-buffer-visiting org-agenda-files))
  16763. (kill-buffer b))
  16764. (with-temp-file org-agenda-files
  16765. (insert
  16766. (mapconcat
  16767. (lambda (f) ;; Keep un-expanded entries.
  16768. (if (setq u (assoc f fe))
  16769. (cdr u)
  16770. f))
  16771. list "\n")
  16772. "\n")))
  16773. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16774. (org-insert-mode-line-in-empty-file nil))
  16775. (setq org-agenda-files list)
  16776. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16777. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16778. "Read the list of agenda files from a file.
  16779. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16780. filenames, used by `org-store-new-agenda-file-list' to write back
  16781. un-expanded file names."
  16782. (when (file-directory-p org-agenda-files)
  16783. (error "`org-agenda-files' cannot be a single directory"))
  16784. (when (stringp org-agenda-files)
  16785. (with-temp-buffer
  16786. (insert-file-contents org-agenda-files)
  16787. (mapcar
  16788. (lambda (f)
  16789. (let ((e (expand-file-name (substitute-in-file-name f)
  16790. org-directory)))
  16791. (if pair-with-expansion
  16792. (cons e f)
  16793. e)))
  16794. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16795. ;;;###autoload
  16796. (defun org-cycle-agenda-files ()
  16797. "Cycle through the files in `org-agenda-files'.
  16798. If the current buffer visits an agenda file, find the next one in the list.
  16799. If the current buffer does not, find the first agenda file."
  16800. (interactive)
  16801. (let* ((fs (or (org-agenda-files t)
  16802. (user-error "No agenda files")))
  16803. (files (copy-sequence fs))
  16804. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  16805. file)
  16806. (when tcf
  16807. (while (and (setq file (pop files))
  16808. (not (equal (file-truename file) tcf)))))
  16809. (find-file (car (or files fs)))
  16810. (when (buffer-base-buffer) (pop-to-buffer-same-window (buffer-base-buffer)))))
  16811. (defun org-agenda-file-to-front (&optional to-end)
  16812. "Move/add the current file to the top of the agenda file list.
  16813. If the file is not present in the list, it is added to the front. If it is
  16814. present, it is moved there. With optional argument TO-END, add/move to the
  16815. end of the list."
  16816. (interactive "P")
  16817. (let ((org-agenda-skip-unavailable-files nil)
  16818. (file-alist (mapcar (lambda (x)
  16819. (cons (file-truename x) x))
  16820. (org-agenda-files t)))
  16821. (ctf (file-truename
  16822. (or buffer-file-name
  16823. (user-error "Please save the current buffer to a file"))))
  16824. x had)
  16825. (setq x (assoc ctf file-alist) had x)
  16826. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16827. (if to-end
  16828. (setq file-alist (append (delq x file-alist) (list x)))
  16829. (setq file-alist (cons x (delq x file-alist))))
  16830. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16831. (org-install-agenda-files-menu)
  16832. (message "File %s to %s of agenda file list"
  16833. (if had "moved" "added") (if to-end "end" "front"))))
  16834. (defun org-remove-file (&optional file)
  16835. "Remove current file from the list of files in variable `org-agenda-files'.
  16836. These are the files which are being checked for agenda entries.
  16837. Optional argument FILE means use this file instead of the current."
  16838. (interactive)
  16839. (let* ((org-agenda-skip-unavailable-files nil)
  16840. (file (or file buffer-file-name
  16841. (user-error "Current buffer does not visit a file")))
  16842. (true-file (file-truename file))
  16843. (afile (abbreviate-file-name file))
  16844. (files (delq nil (mapcar
  16845. (lambda (x)
  16846. (unless (equal true-file
  16847. (file-truename x))
  16848. x))
  16849. (org-agenda-files t)))))
  16850. (if (not (= (length files) (length (org-agenda-files t))))
  16851. (progn
  16852. (org-store-new-agenda-file-list files)
  16853. (org-install-agenda-files-menu)
  16854. (message "Removed from Org Agenda list: %s" afile))
  16855. (message "File was not in list: %s (not removed)" afile))))
  16856. (defun org-file-menu-entry (file)
  16857. (vector file (list 'find-file file) t))
  16858. (defun org-check-agenda-file (file)
  16859. "Make sure FILE exists. If not, ask user what to do."
  16860. (unless (file-exists-p file)
  16861. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16862. (abbreviate-file-name file))
  16863. (let ((r (downcase (read-char-exclusive))))
  16864. (cond
  16865. ((equal r ?r)
  16866. (org-remove-file file)
  16867. (throw 'nextfile t))
  16868. (t (user-error "Abort"))))))
  16869. (defun org-get-agenda-file-buffer (file)
  16870. "Get an agenda buffer visiting FILE.
  16871. If the buffer needs to be created, add it to the list of buffers
  16872. which might be released later."
  16873. (let ((buf (org-find-base-buffer-visiting file)))
  16874. (if buf
  16875. buf ; just return it
  16876. ;; Make a new buffer and remember it
  16877. (setq buf (find-file-noselect file))
  16878. (when buf (push buf org-agenda-new-buffers))
  16879. buf)))
  16880. (defun org-release-buffers (blist)
  16881. "Release all buffers in list, asking the user for confirmation when needed.
  16882. When a buffer is unmodified, it is just killed. When modified, it is saved
  16883. \(if the user agrees) and then killed."
  16884. (let (file)
  16885. (dolist (buf blist)
  16886. (setq file (buffer-file-name buf))
  16887. (when (and (buffer-modified-p buf)
  16888. file
  16889. (y-or-n-p (format "Save file %s? " file)))
  16890. (with-current-buffer buf (save-buffer)))
  16891. (kill-buffer buf))))
  16892. (defun org-agenda-prepare-buffers (files)
  16893. "Create buffers for all agenda files, protect archived trees and comments."
  16894. (interactive)
  16895. (let ((pa '(:org-archived t))
  16896. (pc '(:org-comment t))
  16897. (pall '(:org-archived t :org-comment t))
  16898. (inhibit-read-only t)
  16899. (org-inhibit-startup org-agenda-inhibit-startup)
  16900. (rea (concat ":" org-archive-tag ":"))
  16901. re pos)
  16902. (setq org-tag-alist-for-agenda nil
  16903. org-tag-groups-alist-for-agenda nil)
  16904. (save-excursion
  16905. (save-restriction
  16906. (dolist (file files)
  16907. (catch 'nextfile
  16908. (if (bufferp file)
  16909. (set-buffer file)
  16910. (org-check-agenda-file file)
  16911. (set-buffer (org-get-agenda-file-buffer file)))
  16912. (widen)
  16913. (org-set-regexps-and-options 'tags-only)
  16914. (setq pos (point))
  16915. (or (memq 'category org-agenda-ignore-properties)
  16916. (org-refresh-category-properties))
  16917. (or (memq 'stats org-agenda-ignore-properties)
  16918. (org-refresh-stats-properties))
  16919. (or (memq 'effort org-agenda-ignore-properties)
  16920. (org-refresh-effort-properties))
  16921. (or (memq 'appt org-agenda-ignore-properties)
  16922. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16923. (setq org-todo-keywords-for-agenda
  16924. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16925. (setq org-done-keywords-for-agenda
  16926. (append org-done-keywords-for-agenda org-done-keywords))
  16927. (setq org-todo-keyword-alist-for-agenda
  16928. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16929. (setq org-tag-alist-for-agenda
  16930. (org-uniquify
  16931. (append org-tag-alist-for-agenda
  16932. org-current-tag-alist)))
  16933. ;; Merge current file's tag groups into global
  16934. ;; `org-tag-groups-alist-for-agenda'.
  16935. (when org-group-tags
  16936. (dolist (alist org-tag-groups-alist)
  16937. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  16938. (if old
  16939. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  16940. (push alist org-tag-groups-alist-for-agenda)))))
  16941. (org-with-silent-modifications
  16942. (save-excursion
  16943. (remove-text-properties (point-min) (point-max) pall)
  16944. (when org-agenda-skip-archived-trees
  16945. (goto-char (point-min))
  16946. (while (re-search-forward rea nil t)
  16947. (when (org-at-heading-p t)
  16948. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16949. (goto-char (point-min))
  16950. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  16951. (while (re-search-forward re nil t)
  16952. (when (save-match-data (org-in-commented-heading-p t))
  16953. (add-text-properties
  16954. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16955. (goto-char pos)))))
  16956. (setq org-todo-keywords-for-agenda
  16957. (org-uniquify org-todo-keywords-for-agenda))
  16958. (setq org-todo-keyword-alist-for-agenda
  16959. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16960. ;;;; CDLaTeX minor mode
  16961. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16962. "Keymap for the minor `org-cdlatex-mode'.")
  16963. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16964. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16965. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16966. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16967. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16968. (defvar org-cdlatex-texmathp-advice-is-done nil
  16969. "Flag remembering if we have applied the advice to texmathp already.")
  16970. (define-minor-mode org-cdlatex-mode
  16971. "Toggle the minor `org-cdlatex-mode'.
  16972. This mode supports entering LaTeX environment and math in LaTeX fragments
  16973. in Org mode.
  16974. \\{org-cdlatex-mode-map}"
  16975. nil " OCDL" nil
  16976. (when org-cdlatex-mode
  16977. (require 'cdlatex)
  16978. (run-hooks 'cdlatex-mode-hook)
  16979. (cdlatex-compute-tables))
  16980. (unless org-cdlatex-texmathp-advice-is-done
  16981. (setq org-cdlatex-texmathp-advice-is-done t)
  16982. (defadvice texmathp (around org-math-always-on activate)
  16983. "Always return t in Org buffers.
  16984. This is because we want to insert math symbols without dollars even outside
  16985. the LaTeX math segments. If Org mode thinks that point is actually inside
  16986. an embedded LaTeX fragment, let `texmathp' do its job.
  16987. `\\[org-cdlatex-mode-map]'"
  16988. (interactive)
  16989. (let (p)
  16990. (cond
  16991. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16992. ((eq this-command 'cdlatex-math-symbol)
  16993. (setq ad-return-value t
  16994. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16995. (t
  16996. (let ((p (org-inside-LaTeX-fragment-p)))
  16997. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16998. (setq ad-return-value t
  16999. texmathp-why '("Org mode embedded math" . 0))
  17000. (when p ad-do-it)))))))))
  17001. (defun turn-on-org-cdlatex ()
  17002. "Unconditionally turn on `org-cdlatex-mode'."
  17003. (org-cdlatex-mode 1))
  17004. (defun org-try-cdlatex-tab ()
  17005. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17006. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17007. - inside a LaTeX fragment, or
  17008. - after the first word in a line, where an abbreviation expansion could
  17009. insert a LaTeX environment."
  17010. (when org-cdlatex-mode
  17011. (cond
  17012. ;; Before any word on the line: No expansion possible.
  17013. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17014. ;; Just after first word on the line: Expand it. Make sure it
  17015. ;; cannot happen on headlines, though.
  17016. ((save-excursion
  17017. (skip-chars-backward "a-zA-Z0-9*")
  17018. (skip-chars-backward " \t")
  17019. (and (bolp) (not (org-at-heading-p))))
  17020. (cdlatex-tab) t)
  17021. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17022. (defun org-cdlatex-underscore-caret (&optional _arg)
  17023. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17024. Revert to the normal definition outside of these fragments."
  17025. (interactive "P")
  17026. (if (org-inside-LaTeX-fragment-p)
  17027. (call-interactively 'cdlatex-sub-superscript)
  17028. (let (org-cdlatex-mode)
  17029. (call-interactively (key-binding (vector last-input-event))))))
  17030. (defun org-cdlatex-math-modify (&optional _arg)
  17031. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17032. Revert to the normal definition outside of these fragments."
  17033. (interactive "P")
  17034. (if (org-inside-LaTeX-fragment-p)
  17035. (call-interactively 'cdlatex-math-modify)
  17036. (let (org-cdlatex-mode)
  17037. (call-interactively (key-binding (vector last-input-event))))))
  17038. (defun org-cdlatex-environment-indent (&optional environment item)
  17039. "Execute `cdlatex-environment' and indent the inserted environment.
  17040. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17041. The inserted environment is indented to current indentation
  17042. unless point is at the beginning of the line, in which the
  17043. environment remains unintended."
  17044. (interactive)
  17045. ;; cdlatex-environment always return nil. Therefore, capture output
  17046. ;; first and determine if an environment was selected.
  17047. (let* ((beg (point-marker))
  17048. (end (copy-marker (point) t))
  17049. (inserted (progn
  17050. (ignore-errors (cdlatex-environment environment item))
  17051. (< beg end)))
  17052. ;; Figure out how many lines to move forward after the
  17053. ;; environment has been inserted.
  17054. (lines (when inserted
  17055. (save-excursion
  17056. (- (cl-loop while (< beg (point))
  17057. with x = 0
  17058. do (forward-line -1)
  17059. (cl-incf x)
  17060. finally return x)
  17061. (if (progn (goto-char beg)
  17062. (and (progn (skip-chars-forward " \t") (eolp))
  17063. (progn (skip-chars-backward " \t") (bolp))))
  17064. 1 0)))))
  17065. (env (org-trim (delete-and-extract-region beg end))))
  17066. (when inserted
  17067. ;; Get indentation of next line unless at column 0.
  17068. (let ((ind (if (bolp) 0
  17069. (save-excursion
  17070. (org-return-indent)
  17071. (prog1 (org-get-indentation)
  17072. (when (progn (skip-chars-forward " \t") (eolp))
  17073. (delete-region beg (point)))))))
  17074. (bol (progn (skip-chars-backward " \t") (bolp))))
  17075. ;; Insert a newline before environment unless at column zero
  17076. ;; to "escape" the current line. Insert a newline if
  17077. ;; something is one the same line as \end{ENVIRONMENT}.
  17078. (insert
  17079. (concat (unless bol "\n") env
  17080. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17081. (unless (zerop ind)
  17082. (save-excursion
  17083. (goto-char beg)
  17084. (while (< (point) end)
  17085. (unless (eolp) (indent-line-to ind))
  17086. (forward-line))))
  17087. (goto-char beg)
  17088. (forward-line lines)
  17089. (indent-line-to ind)))
  17090. (set-marker beg nil)
  17091. (set-marker end nil)))
  17092. ;;;; LaTeX fragments
  17093. (defun org-inside-LaTeX-fragment-p ()
  17094. "Test if point is inside a LaTeX fragment.
  17095. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17096. sequence appearing also before point.
  17097. Even though the matchers for math are configurable, this function assumes
  17098. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17099. delimiters are skipped when they have been removed by customization.
  17100. The return value is nil, or a cons cell with the delimiter and the
  17101. position of this delimiter.
  17102. This function does a reasonably good job, but can locally be fooled by
  17103. for example currency specifications. For example it will assume being in
  17104. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17105. fragments that are properly closed, but during editing, we have to live
  17106. with the uncertainty caused by missing closing delimiters. This function
  17107. looks only before point, not after."
  17108. (catch 'exit
  17109. (let ((pos (point))
  17110. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17111. (lim (save-excursion (org-backward-paragraph) (point)))
  17112. dd-on str (start 0) m re)
  17113. (goto-char pos)
  17114. (when dodollar
  17115. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17116. re (nth 1 (assoc "$" org-latex-regexps)))
  17117. (while (string-match re str start)
  17118. (cond
  17119. ((= (match-end 0) (length str))
  17120. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17121. ((= (match-end 0) (- (length str) 5))
  17122. (throw 'exit nil))
  17123. (t (setq start (match-end 0))))))
  17124. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17125. (goto-char pos)
  17126. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17127. (and (match-beginning 2) (throw 'exit nil))
  17128. ;; count $$
  17129. (while (re-search-backward "\\$\\$" lim t)
  17130. (setq dd-on (not dd-on)))
  17131. (goto-char pos)
  17132. (when dd-on (cons "$$" m))))))
  17133. (defun org-inside-latex-macro-p ()
  17134. "Is point inside a LaTeX macro or its arguments?"
  17135. (save-match-data
  17136. (org-in-regexp
  17137. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17138. (defun org--format-latex-make-overlay (beg end image &optional imagetype)
  17139. "Build an overlay between BEG and END using IMAGE file.
  17140. Argument IMAGETYPE is the extension of the displayed image,
  17141. as a string. It defaults to \"png\"."
  17142. (let ((ov (make-overlay beg end))
  17143. (imagetype (or (intern imagetype) 'png)))
  17144. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17145. (overlay-put ov 'evaporate t)
  17146. (overlay-put ov
  17147. 'modification-hooks
  17148. (list (lambda (o _flag _beg _end &optional _l)
  17149. (delete-overlay o))))
  17150. (overlay-put ov
  17151. 'display
  17152. (list 'image :type imagetype :file image :ascent 'center))))
  17153. (defun org--list-latex-overlays (&optional beg end)
  17154. "List all Org LaTeX overlays in current buffer.
  17155. Limit to overlays between BEG and END when those are provided."
  17156. (cl-remove-if-not
  17157. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17158. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17159. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17160. "Remove all overlays with LaTeX fragment images in current buffer.
  17161. When optional arguments BEG and END are non-nil, remove all
  17162. overlays between them instead. Return a non-nil value when some
  17163. overlays were removed, nil otherwise."
  17164. (let ((overlays (org--list-latex-overlays beg end)))
  17165. (mapc #'delete-overlay overlays)
  17166. overlays))
  17167. (defun org-toggle-latex-fragment (&optional arg)
  17168. "Preview the LaTeX fragment at point, or all locally or globally.
  17169. If the cursor is on a LaTeX fragment, create the image and overlay
  17170. it over the source code, if there is none. Remove it otherwise.
  17171. If there is no fragment at point, display all fragments in the
  17172. current section.
  17173. With prefix ARG, preview or clear image for all fragments in the
  17174. current subtree or in the whole buffer when used before the first
  17175. headline. With a prefix ARG `\\[universal-argument] \
  17176. \\[universal-argument]' preview or clear images
  17177. for all fragments in the buffer."
  17178. (interactive "P")
  17179. (when (display-graphic-p)
  17180. (catch 'exit
  17181. (save-excursion
  17182. (let (beg end msg)
  17183. (cond
  17184. ((or (equal arg '(16))
  17185. (and (equal arg '(4))
  17186. (org-with-limited-levels (org-before-first-heading-p))))
  17187. (if (org-remove-latex-fragment-image-overlays)
  17188. (progn (message "LaTeX fragments images removed from buffer")
  17189. (throw 'exit nil))
  17190. (setq msg "Creating images for buffer...")))
  17191. ((equal arg '(4))
  17192. (org-with-limited-levels (org-back-to-heading t))
  17193. (setq beg (point))
  17194. (setq end (progn (org-end-of-subtree t) (point)))
  17195. (if (org-remove-latex-fragment-image-overlays beg end)
  17196. (progn
  17197. (message "LaTeX fragment images removed from subtree")
  17198. (throw 'exit nil))
  17199. (setq msg "Creating images for subtree...")))
  17200. ((let ((datum (org-element-context)))
  17201. (when (memq (org-element-type datum)
  17202. '(latex-environment latex-fragment))
  17203. (setq beg (org-element-property :begin datum))
  17204. (setq end (org-element-property :end datum))
  17205. (if (org-remove-latex-fragment-image-overlays beg end)
  17206. (progn (message "LaTeX fragment image removed")
  17207. (throw 'exit nil))
  17208. (setq msg "Creating image...")))))
  17209. (t
  17210. (org-with-limited-levels
  17211. (setq beg (if (org-at-heading-p) (line-beginning-position)
  17212. (outline-previous-heading)
  17213. (point)))
  17214. (setq end (progn (outline-next-heading) (point)))
  17215. (if (org-remove-latex-fragment-image-overlays beg end)
  17216. (progn
  17217. (message "LaTeX fragment images removed from section")
  17218. (throw 'exit nil))
  17219. (setq msg "Creating images for section...")))))
  17220. (let ((file (buffer-file-name (buffer-base-buffer))))
  17221. (org-format-latex
  17222. (concat org-preview-latex-image-directory "org-ltximg")
  17223. beg end
  17224. ;; Emacs cannot overlay images from remote hosts. Create
  17225. ;; it in `temporary-file-directory' instead.
  17226. (if (or (not file) (file-remote-p file))
  17227. temporary-file-directory
  17228. default-directory)
  17229. 'overlays msg 'forbuffer org-preview-latex-default-process))
  17230. (message (concat msg "done")))))))
  17231. (defun org-format-latex
  17232. (prefix &optional beg end dir overlays msg forbuffer processing-type)
  17233. "Replace LaTeX fragments with links to an image.
  17234. The function takes care of creating the replacement image.
  17235. Only consider fragments between BEG and END when those are
  17236. provided.
  17237. When optional argument OVERLAYS is non-nil, display the image on
  17238. top of the fragment instead of replacing it.
  17239. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17240. Some of the options can be changed using the variable
  17241. `org-format-latex-options', which see."
  17242. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17243. (unless (eq processing-type 'verbatim)
  17244. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17245. (cnt 0)
  17246. checkdir-flag)
  17247. (goto-char (or beg (point-min)))
  17248. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17249. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17250. (overlay-recenter (or end (point-max))))
  17251. (while (re-search-forward math-regexp end t)
  17252. (unless (and overlays
  17253. (eq (get-char-property (point) 'org-overlay-type)
  17254. 'org-latex-overlay))
  17255. (let* ((context (org-element-context))
  17256. (type (org-element-type context)))
  17257. (when (memq type '(latex-environment latex-fragment))
  17258. (let ((block-type (eq type 'latex-environment))
  17259. (value (org-element-property :value context))
  17260. (beg (org-element-property :begin context))
  17261. (end (save-excursion
  17262. (goto-char (org-element-property :end context))
  17263. (skip-chars-backward " \r\t\n")
  17264. (point))))
  17265. (cond
  17266. ((eq processing-type 'mathjax)
  17267. ;; Prepare for MathJax processing.
  17268. (if (not (string-match "\\`\\$\\$?" value))
  17269. (goto-char end)
  17270. (delete-region beg end)
  17271. (if (string= (match-string 0 value) "$$")
  17272. (insert "\\[" (substring value 2 -2) "\\]")
  17273. (insert "\\(" (substring value 1 -1) "\\)"))))
  17274. ((assq processing-type org-preview-latex-process-alist)
  17275. ;; Process to an image.
  17276. (cl-incf cnt)
  17277. (goto-char beg)
  17278. (let* ((processing-info
  17279. (cdr (assq processing-type org-preview-latex-process-alist)))
  17280. (face (face-at-point))
  17281. ;; Get the colors from the face at point.
  17282. (fg
  17283. (let ((color (plist-get org-format-latex-options
  17284. :foreground)))
  17285. (if (and forbuffer (eq color 'auto))
  17286. (face-attribute face :foreground nil 'default)
  17287. color)))
  17288. (bg
  17289. (let ((color (plist-get org-format-latex-options
  17290. :background)))
  17291. (if (and forbuffer (eq color 'auto))
  17292. (face-attribute face :background nil 'default)
  17293. color)))
  17294. (hash (sha1 (prin1-to-string
  17295. (list org-format-latex-header
  17296. org-latex-default-packages-alist
  17297. org-latex-packages-alist
  17298. org-format-latex-options
  17299. forbuffer value fg bg))))
  17300. (imagetype (or (plist-get processing-info :image-output-type) "png"))
  17301. (absprefix (expand-file-name prefix dir))
  17302. (linkfile (format "%s_%s.%s" prefix hash imagetype))
  17303. (movefile (format "%s_%s.%s" absprefix hash imagetype))
  17304. (sep (and block-type "\n\n"))
  17305. (link (concat sep "[[file:" linkfile "]]" sep))
  17306. (options
  17307. (org-combine-plists
  17308. org-format-latex-options
  17309. `(:foreground ,fg :background ,bg))))
  17310. (when msg (message msg cnt))
  17311. (unless checkdir-flag ; Ensure the directory exists.
  17312. (setq checkdir-flag t)
  17313. (let ((todir (file-name-directory absprefix)))
  17314. (unless (file-directory-p todir)
  17315. (make-directory todir t))))
  17316. (unless (file-exists-p movefile)
  17317. (org-create-formula-image
  17318. value movefile options forbuffer processing-type))
  17319. (if overlays
  17320. (progn
  17321. (dolist (o (overlays-in beg end))
  17322. (when (eq (overlay-get o 'org-overlay-type)
  17323. 'org-latex-overlay)
  17324. (delete-overlay o)))
  17325. (org--format-latex-make-overlay beg end movefile imagetype)
  17326. (goto-char end))
  17327. (delete-region beg end)
  17328. (insert
  17329. (org-add-props link
  17330. (list 'org-latex-src
  17331. (replace-regexp-in-string "\"" "" value)
  17332. 'org-latex-src-embed-type
  17333. (if block-type 'paragraph 'character)))))))
  17334. ((eq processing-type 'mathml)
  17335. ;; Process to MathML.
  17336. (unless (org-format-latex-mathml-available-p)
  17337. (user-error "LaTeX to MathML converter not configured"))
  17338. (cl-incf cnt)
  17339. (when msg (message msg cnt))
  17340. (goto-char beg)
  17341. (delete-region beg end)
  17342. (insert (org-format-latex-as-mathml
  17343. value block-type prefix dir)))
  17344. (t
  17345. (error "Unknown conversion process %s for LaTeX fragments"
  17346. processing-type)))))))))))
  17347. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17348. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17349. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17350. sucessful, return the portion between \"<math...> </math>\"
  17351. elements otherwise return nil. When MATHML-FILE is specified,
  17352. write the results in to that file. When invoked as an
  17353. interactive command, prompt for LATEX-FRAG, with initial value
  17354. set to the current active region and echo the results for user
  17355. inspection."
  17356. (interactive (list (let ((frag (when (org-region-active-p)
  17357. (buffer-substring-no-properties
  17358. (region-beginning) (region-end)))))
  17359. (read-string "LaTeX Fragment: " frag nil frag))))
  17360. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17361. (let* ((tmp-in-file
  17362. (let ((file (file-relative-name
  17363. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17364. (write-region latex-frag nil file)
  17365. file))
  17366. (tmp-out-file (file-relative-name
  17367. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17368. (cmd (format-spec
  17369. org-latex-to-mathml-convert-command
  17370. `((?j . ,(and org-latex-to-mathml-jar-file
  17371. (shell-quote-argument
  17372. (expand-file-name
  17373. org-latex-to-mathml-jar-file))))
  17374. (?I . ,(shell-quote-argument tmp-in-file))
  17375. (?i . ,latex-frag)
  17376. (?o . ,(shell-quote-argument tmp-out-file)))))
  17377. mathml shell-command-output)
  17378. (when (called-interactively-p 'any)
  17379. (unless (org-format-latex-mathml-available-p)
  17380. (user-error "LaTeX to MathML converter not configured")))
  17381. (message "Running %s" cmd)
  17382. (setq shell-command-output (shell-command-to-string cmd))
  17383. (setq mathml
  17384. (when (file-readable-p tmp-out-file)
  17385. (with-current-buffer (find-file-noselect tmp-out-file t)
  17386. (goto-char (point-min))
  17387. (when (re-search-forward
  17388. (format "<math[^>]*?%s[^>]*?>\\(.\\|\n\\)*</math>"
  17389. (regexp-quote
  17390. "xmlns=\"http://www.w3.org/1998/Math/MathML\""))
  17391. nil t)
  17392. (prog1 (match-string 0) (kill-buffer))))))
  17393. (cond
  17394. (mathml
  17395. (setq mathml
  17396. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17397. (when mathml-file
  17398. (write-region mathml nil mathml-file))
  17399. (when (called-interactively-p 'any)
  17400. (message mathml)))
  17401. ((message "LaTeX to MathML conversion failed")
  17402. (message shell-command-output)))
  17403. (delete-file tmp-in-file)
  17404. (when (file-exists-p tmp-out-file)
  17405. (delete-file tmp-out-file))
  17406. mathml))
  17407. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17408. prefix &optional dir)
  17409. "Use `org-create-math-formula' but check local cache first."
  17410. (let* ((absprefix (expand-file-name prefix dir))
  17411. (print-length nil) (print-level nil)
  17412. (formula-id (concat
  17413. "formula-"
  17414. (sha1
  17415. (prin1-to-string
  17416. (list latex-frag
  17417. org-latex-to-mathml-convert-command)))))
  17418. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17419. (formula-cache-dir (file-name-directory formula-cache)))
  17420. (unless (file-directory-p formula-cache-dir)
  17421. (make-directory formula-cache-dir t))
  17422. (unless (file-exists-p formula-cache)
  17423. (org-create-math-formula latex-frag formula-cache))
  17424. (if (file-exists-p formula-cache)
  17425. ;; Successful conversion. Return the link to MathML file.
  17426. (org-add-props
  17427. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17428. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17429. 'org-latex-src-embed-type (if latex-frag-type
  17430. 'paragraph 'character)))
  17431. ;; Failed conversion. Return the LaTeX fragment verbatim
  17432. latex-frag)))
  17433. (defun org--get-display-dpi ()
  17434. "Get the DPI of the display.
  17435. The function assumes that the display has the same pixel width in
  17436. the horizontal and vertical directions."
  17437. (if (display-graphic-p)
  17438. (round (/ (display-pixel-height)
  17439. (/ (display-mm-height) 25.4)))
  17440. (error "Attempt to calculate the dpi of a non-graphic display")))
  17441. (defun org-create-formula-image
  17442. (string tofile options buffer &optional processing-type)
  17443. "Create an image from LaTeX source using external processes.
  17444. The LaTeX STRING is saved to a temporary LaTeX file, then
  17445. converted to an image file by process PROCESSING-TYPE defined in
  17446. `org-preview-latex-process-alist'. A nil value defaults to
  17447. `org-preview-latex-default-process'.
  17448. The generated image file is eventually moved to TOFILE.
  17449. The OPTIONS argument controls the size, foreground color and
  17450. background color of the generated image.
  17451. When BUFFER non-nil, this function is used for LaTeX previewing.
  17452. Otherwise, it is used to deal with LaTeX snippets showed in
  17453. a HTML file."
  17454. (let* ((processing-type (or processing-type
  17455. org-preview-latex-default-process))
  17456. (processing-info
  17457. (cdr (assq processing-type org-preview-latex-process-alist)))
  17458. (programs (plist-get processing-info :programs))
  17459. (error-message (or (plist-get processing-info :message) ""))
  17460. (use-xcolor (plist-get processing-info :use-xcolor))
  17461. (image-input-type (plist-get processing-info :image-input-type))
  17462. (image-output-type (plist-get processing-info :image-output-type))
  17463. (post-clean (or (plist-get processing-info :post-clean)
  17464. '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
  17465. ".svg" ".png" ".jpg" ".jpeg" ".out")))
  17466. (latex-header
  17467. (or (plist-get processing-info :latex-header)
  17468. (org-latex-make-preamble
  17469. (org-export-get-environment (org-export-get-backend 'latex))
  17470. org-format-latex-header
  17471. 'snippet)))
  17472. (latex-compiler (plist-get processing-info :latex-compiler))
  17473. (image-converter (plist-get processing-info :image-converter))
  17474. (tmpdir temporary-file-directory)
  17475. (texfilebase (make-temp-name
  17476. (expand-file-name "orgtex" tmpdir)))
  17477. (texfile (concat texfilebase ".tex"))
  17478. (image-size-adjust (or (plist-get processing-info :image-size-adjust)
  17479. '(1.0 . 1.0)))
  17480. (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
  17481. (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
  17482. (dpi (* scale (if buffer (org--get-display-dpi) 140.0)))
  17483. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17484. "Black"))
  17485. (bg (or (plist-get options (if buffer :background :html-background))
  17486. "Transparent"))
  17487. (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
  17488. (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
  17489. (dolist (program programs)
  17490. (org-check-external-command program error-message))
  17491. (if use-xcolor
  17492. (progn (if (eq fg 'default)
  17493. (setq fg (org-latex-color :foreground))
  17494. (setq fg (org-latex-color-format fg)))
  17495. (if (eq bg 'default)
  17496. (setq bg (org-latex-color :background))
  17497. (setq bg (org-latex-color-format
  17498. (if (string= bg "Transparent") "white" bg))))
  17499. (with-temp-file texfile
  17500. (insert latex-header)
  17501. (insert "\n\\begin{document}\n"
  17502. "\\definecolor{fg}{rgb}{" fg "}\n"
  17503. "\\definecolor{bg}{rgb}{" bg "}\n"
  17504. "\n\\pagecolor{bg}\n"
  17505. "\n{\\color{fg}\n"
  17506. string
  17507. "\n}\n"
  17508. "\n\\end{document}\n")))
  17509. (if (eq fg 'default)
  17510. (setq fg (org-dvipng-color :foreground))
  17511. (unless (string= fg "Transparent")
  17512. (setq fg (org-dvipng-color-format fg))))
  17513. (if (eq bg 'default)
  17514. (setq bg (org-dvipng-color :background))
  17515. (unless (string= bg "Transparent")
  17516. (setq bg (org-dvipng-color-format bg))))
  17517. (with-temp-file texfile
  17518. (insert latex-header)
  17519. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17520. (let* ((err-msg (format "Please adjust `%s' part of \
  17521. `org-preview-latex-process-alist'."
  17522. processing-type))
  17523. (image-input-file
  17524. (org-compile-file
  17525. texfile latex-compiler image-input-type err-msg log-buf))
  17526. (image-output-file
  17527. (org-compile-file
  17528. image-input-file image-converter image-output-type err-msg log-buf
  17529. `((?F . ,(shell-quote-argument fg))
  17530. (?B . ,(shell-quote-argument bg))
  17531. (?D . ,(shell-quote-argument (format "%s" dpi)))
  17532. (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
  17533. (copy-file image-output-file tofile 'replace)
  17534. (dolist (e post-clean)
  17535. (when (file-exists-p (concat texfilebase e))
  17536. (delete-file (concat texfilebase e))))
  17537. image-output-file)))
  17538. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17539. "Fill a LaTeX header template TPL.
  17540. In the template, the following place holders will be recognized:
  17541. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17542. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17543. [PACKAGES] \\usepackage statements for PKG
  17544. [NO-PACKAGES] do not include PKG
  17545. [EXTRA] the string EXTRA
  17546. [NO-EXTRA] do not include EXTRA
  17547. For backward compatibility, if both the positive and the negative place
  17548. holder is missing, the positive one (without the \"NO-\") will be
  17549. assumed to be present at the end of the template.
  17550. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17551. EXTRA is a string.
  17552. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17553. (let (rpl (end ""))
  17554. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17555. (setq rpl (if (or (match-end 1) (not def-pkg))
  17556. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17557. tpl (replace-match rpl t t tpl))
  17558. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17559. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17560. (setq rpl (if (or (match-end 1) (not pkg))
  17561. "" (org-latex-packages-to-string pkg snippets-p t))
  17562. tpl (replace-match rpl t t tpl))
  17563. (when pkg (setq end
  17564. (concat end "\n"
  17565. (org-latex-packages-to-string pkg snippets-p)))))
  17566. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17567. (setq rpl (if (or (match-end 1) (not extra))
  17568. "" (concat extra "\n"))
  17569. tpl (replace-match rpl t t tpl))
  17570. (when (and extra (string-match "\\S-" extra))
  17571. (setq end (concat end "\n" extra))))
  17572. (if (string-match "\\S-" end)
  17573. (concat tpl "\n" end)
  17574. tpl)))
  17575. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17576. "Turn an alist of packages into a string with the \\usepackage macros."
  17577. (setq pkg (mapconcat (lambda(p)
  17578. (cond
  17579. ((stringp p) p)
  17580. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17581. (format "%% Package %s omitted" (cadr p)))
  17582. ((equal "" (car p))
  17583. (format "\\usepackage{%s}" (cadr p)))
  17584. (t
  17585. (format "\\usepackage[%s]{%s}"
  17586. (car p) (cadr p)))))
  17587. pkg
  17588. "\n"))
  17589. (if newline (concat pkg "\n") pkg))
  17590. (defun org-dvipng-color (attr)
  17591. "Return a RGB color specification for dvipng."
  17592. (org-dvipng-color-format (face-attribute 'default attr nil)))
  17593. (defun org-dvipng-color-format (color-name)
  17594. "Convert COLOR-NAME to a RGB color value for dvipng."
  17595. (apply #'format "rgb %s %s %s"
  17596. (mapcar 'org-normalize-color
  17597. (color-values color-name))))
  17598. (defun org-latex-color (attr)
  17599. "Return a RGB color for the LaTeX color package."
  17600. (org-latex-color-format (face-attribute 'default attr nil)))
  17601. (defun org-latex-color-format (color-name)
  17602. "Convert COLOR-NAME to a RGB color value."
  17603. (apply #'format "%s,%s,%s"
  17604. (mapcar 'org-normalize-color
  17605. (color-values color-name))))
  17606. (defun org-normalize-color (value)
  17607. "Return string to be used as color value for an RGB component."
  17608. (format "%g" (/ value 65535.0)))
  17609. ;; Image display
  17610. (defvar-local org-inline-image-overlays nil)
  17611. (defun org-toggle-inline-images (&optional include-linked)
  17612. "Toggle the display of inline images.
  17613. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17614. (interactive "P")
  17615. (if org-inline-image-overlays
  17616. (progn
  17617. (org-remove-inline-images)
  17618. (when (called-interactively-p 'interactive)
  17619. (message "Inline image display turned off")))
  17620. (org-display-inline-images include-linked)
  17621. (when (called-interactively-p 'interactive)
  17622. (message (if org-inline-image-overlays
  17623. (format "%d images displayed inline"
  17624. (length org-inline-image-overlays))
  17625. "No images to display inline")))))
  17626. (defun org-redisplay-inline-images ()
  17627. "Refresh the display of inline images."
  17628. (interactive)
  17629. (if (not org-inline-image-overlays)
  17630. (org-toggle-inline-images)
  17631. (org-toggle-inline-images)
  17632. (org-toggle-inline-images)))
  17633. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17634. "Display inline images.
  17635. An inline image is a link which follows either of these
  17636. conventions:
  17637. 1. Its path is a file with an extension matching return value
  17638. from `image-file-name-regexp' and it has no contents.
  17639. 2. Its description consists in a single link of the previous
  17640. type.
  17641. When optional argument INCLUDE-LINKED is non-nil, also links with
  17642. a text description part will be inlined. This can be nice for
  17643. a quick look at those images, but it does not reflect what
  17644. exported files will look like.
  17645. When optional argument REFRESH is non-nil, refresh existing
  17646. images between BEG and END. This will create new image displays
  17647. only if necessary. BEG and END default to the buffer
  17648. boundaries."
  17649. (interactive "P")
  17650. (when (display-graphic-p)
  17651. (unless refresh
  17652. (org-remove-inline-images)
  17653. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17654. (org-with-wide-buffer
  17655. (goto-char (or beg (point-min)))
  17656. (let* ((case-fold-search t)
  17657. (file-extension-re (image-file-name-regexp))
  17658. (link-abbrevs (mapcar #'car
  17659. (append org-link-abbrev-alist-local
  17660. org-link-abbrev-alist)))
  17661. ;; Check absolute, relative file names and explicit
  17662. ;; "file:" links. Also check link abbreviations since
  17663. ;; some might expand to "file" links.
  17664. (file-types-re (format "[][]\\[\\(?:file\\|[./~]%s\\)"
  17665. (and link-abbrevs
  17666. (format "\\|\\(?:%s:\\)"
  17667. (regexp-opt link-abbrevs))))))
  17668. (while (re-search-forward file-types-re end t)
  17669. (let ((link (save-match-data (org-element-context))))
  17670. ;; Check if we're at an inline image, i.e., an image file
  17671. ;; link without a description (unless INCLUDE-LINKED is
  17672. ;; non-nil).
  17673. (when (and (equal "file" (org-element-property :type link))
  17674. (or include-linked
  17675. (null (org-element-contents link)))
  17676. (string-match-p file-extension-re
  17677. (org-element-property :path link)))
  17678. (let ((file (expand-file-name
  17679. (org-link-unescape
  17680. (org-element-property :path link)))))
  17681. (when (file-exists-p file)
  17682. (let ((width
  17683. ;; Apply `org-image-actual-width' specifications.
  17684. (cond
  17685. ((not (image-type-available-p 'imagemagick)) nil)
  17686. ((eq org-image-actual-width t) nil)
  17687. ((listp org-image-actual-width)
  17688. (or
  17689. ;; First try to find a width among
  17690. ;; attributes associated to the paragraph
  17691. ;; containing link.
  17692. (let ((paragraph
  17693. (let ((e link))
  17694. (while (and (setq e (org-element-property
  17695. :parent e))
  17696. (not (eq (org-element-type e)
  17697. 'paragraph))))
  17698. e)))
  17699. (when paragraph
  17700. (save-excursion
  17701. (goto-char (org-element-property :begin paragraph))
  17702. (when
  17703. (re-search-forward
  17704. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17705. (org-element-property
  17706. :post-affiliated paragraph)
  17707. t)
  17708. (string-to-number (match-string 1))))))
  17709. ;; Otherwise, fall-back to provided number.
  17710. (car org-image-actual-width)))
  17711. ((numberp org-image-actual-width)
  17712. org-image-actual-width)))
  17713. (old (get-char-property-and-overlay
  17714. (org-element-property :begin link)
  17715. 'org-image-overlay)))
  17716. (if (and (car-safe old) refresh)
  17717. (image-refresh (overlay-get (cdr old) 'display))
  17718. (let ((image (create-image file
  17719. (and width 'imagemagick)
  17720. nil
  17721. :width width)))
  17722. (when image
  17723. (let ((ov (make-overlay
  17724. (org-element-property :begin link)
  17725. (progn
  17726. (goto-char
  17727. (org-element-property :end link))
  17728. (skip-chars-backward " \t")
  17729. (point)))))
  17730. (overlay-put ov 'display image)
  17731. (overlay-put ov 'face 'default)
  17732. (overlay-put ov 'org-image-overlay t)
  17733. (overlay-put
  17734. ov 'modification-hooks
  17735. (list 'org-display-inline-remove-overlay))
  17736. (push ov org-inline-image-overlays)))))))))))))))
  17737. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  17738. "Remove inline-display overlay if a corresponding region is modified."
  17739. (let ((inhibit-modification-hooks t))
  17740. (when (and ov after)
  17741. (delete ov org-inline-image-overlays)
  17742. (delete-overlay ov))))
  17743. (defun org-remove-inline-images ()
  17744. "Remove inline display of images."
  17745. (interactive)
  17746. (mapc #'delete-overlay org-inline-image-overlays)
  17747. (setq org-inline-image-overlays nil))
  17748. ;;;; Key bindings
  17749. ;; Outline functions from `outline-mode-prefix-map'
  17750. ;; that can be remapped in Org:
  17751. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17752. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  17753. (define-key org-mode-map [remap outline-forward-same-level]
  17754. 'org-forward-heading-same-level)
  17755. (define-key org-mode-map [remap outline-backward-same-level]
  17756. 'org-backward-heading-same-level)
  17757. (define-key org-mode-map [remap outline-show-branches]
  17758. 'org-kill-note-or-show-branches)
  17759. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17760. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17761. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17762. (define-key org-mode-map [remap outline-next-visible-heading]
  17763. 'org-next-visible-heading)
  17764. (define-key org-mode-map [remap outline-previous-visible-heading]
  17765. 'org-previous-visible-heading)
  17766. (define-key org-mode-map [remap show-children] 'org-show-children)
  17767. ;; Outline functions from `outline-mode-prefix-map' that can not
  17768. ;; be remapped in Org:
  17769. ;; - the column "key binding" shows whether the Outline function is still
  17770. ;; available in Org mode on the same key that it has been bound to in
  17771. ;; Outline mode:
  17772. ;; - "overridden": key used for a different functionality in Org mode
  17773. ;; - else: key still bound to the same Outline function in Org mode
  17774. ;; | Outline function | key binding | Org replacement |
  17775. ;; |------------------------------------+-------------+--------------------------|
  17776. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17777. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17778. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17779. ;; | `show-entry' | overridden | no replacement |
  17780. ;; | `show-branches' | `C-c C-k' | still same function |
  17781. ;; | `show-subtree' | overridden | visibility cycling |
  17782. ;; | `show-all' | overridden | no replacement |
  17783. ;; | `hide-subtree' | overridden | visibility cycling |
  17784. ;; | `hide-body' | overridden | no replacement |
  17785. ;; | `hide-entry' | overridden | visibility cycling |
  17786. ;; | `hide-leaves' | overridden | no replacement |
  17787. ;; | `hide-sublevels' | overridden | no replacement |
  17788. ;; | `hide-other' | overridden | no replacement |
  17789. ;; Make `C-c C-x' a prefix key
  17790. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17791. ;; TAB key with modifiers
  17792. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17793. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17794. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17795. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  17796. ;; The following line is necessary under Suse GNU/Linux
  17797. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab)
  17798. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17799. (define-key org-mode-map [backtab] 'org-shifttab)
  17800. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17801. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17802. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17803. ;; Cursor keys with modifiers
  17804. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17805. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17806. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17807. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17808. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17809. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17810. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17811. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17812. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17813. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17814. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17815. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17816. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17817. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17818. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17819. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17820. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17821. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17822. ;; Babel keys
  17823. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17824. (dolist (pair org-babel-key-bindings)
  17825. (define-key org-babel-map (car pair) (cdr pair)))
  17826. ;;; Extra keys for tty access.
  17827. ;; We only set them when really needed because otherwise the
  17828. ;; menus don't show the simple keys
  17829. (when (or org-use-extra-keys (not window-system))
  17830. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17831. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17832. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17833. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17834. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17835. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17836. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17837. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17838. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17839. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17840. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17841. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17842. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17843. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17844. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17845. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17846. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17847. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17848. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17849. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17850. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17851. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17852. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  17853. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17854. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17855. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17856. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17857. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17858. ;; All the other keys
  17859. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  17860. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17861. (if (boundp 'narrow-map)
  17862. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17863. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17864. (if (boundp 'narrow-map)
  17865. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17866. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17867. (if (boundp 'narrow-map)
  17868. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17869. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17870. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17871. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17872. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17873. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17874. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17875. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17876. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17877. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17878. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17879. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17880. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  17881. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17882. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17883. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17884. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17885. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17886. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17887. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17888. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17889. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17890. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17891. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17892. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17893. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17894. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17895. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17896. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17897. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17898. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17899. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17900. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17901. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17902. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17903. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17904. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17905. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17906. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17907. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17908. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17909. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17910. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17911. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17912. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17913. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17914. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17915. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17916. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17917. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17918. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17919. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17920. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17921. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17922. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17923. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17924. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17925. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17926. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17927. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17928. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17929. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17930. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17931. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17932. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17933. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  17934. (org-defkey org-mode-map "\C-m" 'org-return)
  17935. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17936. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17937. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17938. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17939. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17940. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17941. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17942. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17943. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17944. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17945. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17946. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17947. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17948. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17949. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17950. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17951. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17952. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17953. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17954. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17955. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17956. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17957. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17958. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17959. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17960. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17961. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17962. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17963. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17964. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17965. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17966. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17967. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17968. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17969. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17970. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17971. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17972. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17973. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17974. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17975. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17976. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17977. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17978. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17979. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17980. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17981. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17982. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17983. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  17984. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17985. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17986. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17987. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17988. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17989. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17990. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17991. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17992. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17993. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17994. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17995. (defconst org-speed-commands-default
  17996. '(
  17997. ("Outline Navigation")
  17998. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  17999. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18000. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18001. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18002. ("F" . org-next-block)
  18003. ("B" . org-previous-block)
  18004. ("u" . (org-speed-move-safe 'outline-up-heading))
  18005. ("j" . org-goto)
  18006. ("g" . (org-refile t))
  18007. ("Outline Visibility")
  18008. ("c" . org-cycle)
  18009. ("C" . org-shifttab)
  18010. (" " . org-display-outline-path)
  18011. ("s" . org-narrow-to-subtree)
  18012. ("=" . org-columns)
  18013. ("Outline Structure Editing")
  18014. ("U" . org-metaup)
  18015. ("D" . org-metadown)
  18016. ("r" . org-metaright)
  18017. ("l" . org-metaleft)
  18018. ("R" . org-shiftmetaright)
  18019. ("L" . org-shiftmetaleft)
  18020. ("i" . (progn (forward-char 1) (call-interactively
  18021. 'org-insert-heading-respect-content)))
  18022. ("^" . org-sort)
  18023. ("w" . org-refile)
  18024. ("a" . org-archive-subtree-default-with-confirmation)
  18025. ("@" . org-mark-subtree)
  18026. ("#" . org-toggle-comment)
  18027. ("Clock Commands")
  18028. ("I" . org-clock-in)
  18029. ("O" . org-clock-out)
  18030. ("Meta Data Editing")
  18031. ("t" . org-todo)
  18032. ("," . (org-priority))
  18033. ("0" . (org-priority ?\ ))
  18034. ("1" . (org-priority ?A))
  18035. ("2" . (org-priority ?B))
  18036. ("3" . (org-priority ?C))
  18037. (":" . org-set-tags-command)
  18038. ("e" . org-set-effort)
  18039. ("E" . org-inc-effort)
  18040. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18041. (org-entry-put (point) "APPT_WARNTIME" m)))
  18042. ("Agenda Views etc")
  18043. ("v" . org-agenda)
  18044. ("/" . org-sparse-tree)
  18045. ("Misc")
  18046. ("o" . org-open-at-point)
  18047. ("?" . org-speed-command-help)
  18048. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18049. (">" . (org-agenda-remove-restriction-lock))
  18050. )
  18051. "The default speed commands.")
  18052. (defun org-print-speed-command (e)
  18053. (if (> (length (car e)) 1)
  18054. (progn
  18055. (princ "\n")
  18056. (princ (car e))
  18057. (princ "\n")
  18058. (princ (make-string (length (car e)) ?-))
  18059. (princ "\n"))
  18060. (princ (car e))
  18061. (princ " ")
  18062. (if (symbolp (cdr e))
  18063. (princ (symbol-name (cdr e)))
  18064. (prin1 (cdr e)))
  18065. (princ "\n")))
  18066. (defun org-speed-command-help ()
  18067. "Show the available speed commands."
  18068. (interactive)
  18069. (if (not org-use-speed-commands)
  18070. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18071. (with-output-to-temp-buffer "*Help*"
  18072. (princ "User-defined Speed commands\n===========================\n")
  18073. (mapc #'org-print-speed-command org-speed-commands-user)
  18074. (princ "\n")
  18075. (princ "Built-in Speed commands\n=======================\n")
  18076. (mapc #'org-print-speed-command org-speed-commands-default))
  18077. (with-current-buffer "*Help*"
  18078. (setq truncate-lines t))))
  18079. (defun org-speed-move-safe (cmd)
  18080. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18081. If not, return to the original position and throw an error."
  18082. (interactive)
  18083. (let ((pos (point)))
  18084. (call-interactively cmd)
  18085. (unless (and (bolp) (org-at-heading-p))
  18086. (goto-char pos)
  18087. (error "Boundary reached while executing %s" cmd))))
  18088. (defvar org-self-insert-command-undo-counter 0)
  18089. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18090. (defvar org-speed-command nil)
  18091. (defun org-speed-command-activate (keys)
  18092. "Hook for activating single-letter speed commands.
  18093. `org-speed-commands-default' specifies a minimal command set.
  18094. Use `org-speed-commands-user' for further customization."
  18095. (when (or (and (bolp) (looking-at org-outline-regexp))
  18096. (and (functionp org-use-speed-commands)
  18097. (funcall org-use-speed-commands)))
  18098. (cdr (assoc keys (append org-speed-commands-user
  18099. org-speed-commands-default)))))
  18100. (defun org-babel-speed-command-activate (keys)
  18101. "Hook for activating single-letter code block commands."
  18102. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18103. (cdr (assoc keys org-babel-key-bindings))))
  18104. (defcustom org-speed-command-hook
  18105. '(org-speed-command-activate org-babel-speed-command-activate)
  18106. "Hook for activating speed commands at strategic locations.
  18107. Hook functions are called in sequence until a valid handler is
  18108. found.
  18109. Each hook takes a single argument, a user-pressed command key
  18110. which is also a `self-insert-command' from the global map.
  18111. Within the hook, examine the cursor position and the command key
  18112. and return nil or a valid handler as appropriate. Handler could
  18113. be one of an interactive command, a function, or a form.
  18114. Set `org-use-speed-commands' to non-nil value to enable this
  18115. hook. The default setting is `org-speed-command-activate'."
  18116. :group 'org-structure
  18117. :version "24.1"
  18118. :type 'hook)
  18119. (defun org-self-insert-command (N)
  18120. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18121. If the cursor is in a table looking at whitespace, the whitespace is
  18122. overwritten, and the table is not marked as requiring realignment."
  18123. (interactive "p")
  18124. (org-check-before-invisible-edit 'insert)
  18125. (cond
  18126. ((and org-use-speed-commands
  18127. (let ((kv (this-command-keys-vector)))
  18128. (setq org-speed-command
  18129. (run-hook-with-args-until-success
  18130. 'org-speed-command-hook
  18131. (make-string 1 (aref kv (1- (length kv))))))))
  18132. (cond
  18133. ((commandp org-speed-command)
  18134. (setq this-command org-speed-command)
  18135. (call-interactively org-speed-command))
  18136. ((functionp org-speed-command)
  18137. (funcall org-speed-command))
  18138. ((and org-speed-command (listp org-speed-command))
  18139. (eval org-speed-command))
  18140. (t (let (org-use-speed-commands)
  18141. (call-interactively 'org-self-insert-command)))))
  18142. ((and
  18143. (org-at-table-p)
  18144. (progn
  18145. ;; Check if we blank the field, and if that triggers align.
  18146. (and (featurep 'org-table) org-table-auto-blank-field
  18147. (memq last-command
  18148. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18149. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18150. ;; Got extra space, this field does not determine
  18151. ;; column width.
  18152. (let (org-table-may-need-update) (org-table-blank-field))
  18153. ;; No extra space, this field may determine column
  18154. ;; width.
  18155. (org-table-blank-field)))
  18156. t)
  18157. (eq N 1)
  18158. (looking-at "[^|\n]* \\( \\)|"))
  18159. ;; There is room for insertion without re-aligning the table.
  18160. (delete-region (match-beginning 1) (match-end 1))
  18161. (self-insert-command N))
  18162. (t
  18163. (setq org-table-may-need-update t)
  18164. (self-insert-command N)
  18165. (org-fix-tags-on-the-fly)
  18166. (when org-self-insert-cluster-for-undo
  18167. (if (not (eq last-command 'org-self-insert-command))
  18168. (setq org-self-insert-command-undo-counter 1)
  18169. (if (>= org-self-insert-command-undo-counter 20)
  18170. (setq org-self-insert-command-undo-counter 1)
  18171. (and (> org-self-insert-command-undo-counter 0)
  18172. buffer-undo-list (listp buffer-undo-list)
  18173. (not (cadr buffer-undo-list)) ; remove nil entry
  18174. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18175. (setq org-self-insert-command-undo-counter
  18176. (1+ org-self-insert-command-undo-counter))))))))
  18177. (defun org-check-before-invisible-edit (kind)
  18178. "Check is editing if kind KIND would be dangerous with invisible text around.
  18179. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18180. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18181. (when (and org-catch-invisible-edits
  18182. (or (not (boundp 'visible-mode)) (not visible-mode))
  18183. (or (get-char-property (point) 'invisible)
  18184. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18185. ;; OK, we need to take a closer look. Do not consider
  18186. ;; invisibility obtained through text properties (e.g., link
  18187. ;; fontification), as it cannot be toggled.
  18188. (let* ((invisible-at-point
  18189. (pcase (get-char-property-and-overlay (point) 'invisible)
  18190. (`(,_ . ,(and (pred overlayp) o)) o)))
  18191. ;; Assume that point cannot land in the middle of an
  18192. ;; overlay, or between two overlays.
  18193. (invisible-before-point
  18194. (and (not invisible-at-point)
  18195. (not (bobp))
  18196. (pcase (get-char-property-and-overlay (1- (point)) 'invisible)
  18197. (`(,_ . ,(and (pred overlayp) o)) o))))
  18198. (border-and-ok-direction
  18199. (or
  18200. ;; Check if we are acting predictably before invisible
  18201. ;; text.
  18202. (and invisible-at-point
  18203. (memq kind '(insert delete-backward)))
  18204. ;; Check if we are acting predictably after invisible text
  18205. ;; This works not well, and I have turned it off. It seems
  18206. ;; better to always show and stop after invisible text.
  18207. ;; (and (not invisible-at-point) invisible-before-point
  18208. ;; (memq kind '(insert delete)))
  18209. )))
  18210. (when (or invisible-at-point invisible-before-point)
  18211. (when (eq org-catch-invisible-edits 'error)
  18212. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18213. (if (and org-custom-properties-overlays
  18214. (y-or-n-p "Display invisible properties in this buffer? "))
  18215. (org-toggle-custom-properties-visibility)
  18216. ;; Make the area visible
  18217. (save-excursion
  18218. (when invisible-before-point
  18219. (goto-char
  18220. (previous-single-char-property-change (point) 'invisible)))
  18221. ;; Remove whatever overlay is currently making yet-to-be
  18222. ;; edited text invisible. Also remove nested invisibility
  18223. ;; related overlays.
  18224. (delete-overlay (or invisible-at-point invisible-before-point))
  18225. (let ((origin (if invisible-at-point (point) (1- (point)))))
  18226. (while (pcase (get-char-property-and-overlay origin 'invisible)
  18227. (`(,_ . ,(and (pred overlayp) o))
  18228. (delete-overlay o)
  18229. t)))))
  18230. (cond
  18231. ((eq org-catch-invisible-edits 'show)
  18232. ;; That's it, we do the edit after showing
  18233. (message
  18234. "Unfolding invisible region around point before editing")
  18235. (sit-for 1))
  18236. ((and (eq org-catch-invisible-edits 'smart)
  18237. border-and-ok-direction)
  18238. (message "Unfolding invisible region around point before editing"))
  18239. (t
  18240. ;; Don't do the edit, make the user repeat it in full visibility
  18241. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18242. (defun org-fix-tags-on-the-fly ()
  18243. "Align tags in headline at point.
  18244. Unlike to `org-set-tags', it ignores region and sorting."
  18245. (when (and (eq (char-after (line-beginning-position)) ?*) ;short-circuit
  18246. (org-at-heading-p))
  18247. (let ((org-ignore-region t)
  18248. (org-tags-sort-function nil))
  18249. (org-set-tags nil t))))
  18250. (defun org-delete-backward-char (N)
  18251. "Like `delete-backward-char', insert whitespace at field end in tables.
  18252. When deleting backwards, in tables this function will insert whitespace in
  18253. front of the next \"|\" separator, to keep the table aligned. The table will
  18254. still be marked for re-alignment if the field did fill the entire column,
  18255. because, in this case the deletion might narrow the column."
  18256. (interactive "p")
  18257. (save-match-data
  18258. (org-check-before-invisible-edit 'delete-backward)
  18259. (if (and (org-at-table-p)
  18260. (eq N 1)
  18261. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18262. (looking-at ".*?|"))
  18263. (let ((pos (point))
  18264. (noalign (looking-at "[^|\n\r]* |"))
  18265. (c org-table-may-need-update))
  18266. (backward-delete-char N)
  18267. (unless overwrite-mode
  18268. (skip-chars-forward "^|")
  18269. (insert " ")
  18270. (goto-char (1- pos)))
  18271. ;; noalign: if there were two spaces at the end, this field
  18272. ;; does not determine the width of the column.
  18273. (when noalign (setq org-table-may-need-update c)))
  18274. (backward-delete-char N)
  18275. (org-fix-tags-on-the-fly))))
  18276. (defun org-delete-char (N)
  18277. "Like `delete-char', but insert whitespace at field end in tables.
  18278. When deleting characters, in tables this function will insert whitespace in
  18279. front of the next \"|\" separator, to keep the table aligned. The table will
  18280. still be marked for re-alignment if the field did fill the entire column,
  18281. because, in this case the deletion might narrow the column."
  18282. (interactive "p")
  18283. (save-match-data
  18284. (org-check-before-invisible-edit 'delete)
  18285. (if (and (org-at-table-p)
  18286. (not (bolp))
  18287. (not (= (char-after) ?|))
  18288. (eq N 1))
  18289. (if (looking-at ".*?|")
  18290. (let ((pos (point))
  18291. (noalign (looking-at "[^|\n\r]* |"))
  18292. (c org-table-may-need-update))
  18293. (replace-match
  18294. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18295. (goto-char pos)
  18296. ;; noalign: if there were two spaces at the end, this field
  18297. ;; does not determine the width of the column.
  18298. (when noalign (setq org-table-may-need-update c)))
  18299. (delete-char N))
  18300. (delete-char N)
  18301. (org-fix-tags-on-the-fly))))
  18302. ;; Make `delete-selection-mode' work with Org mode and Orgtbl mode
  18303. (put 'org-self-insert-command 'delete-selection
  18304. (lambda ()
  18305. (not (run-hook-with-args-until-success
  18306. 'self-insert-uses-region-functions))))
  18307. (put 'orgtbl-self-insert-command 'delete-selection
  18308. (lambda ()
  18309. (not (run-hook-with-args-until-success
  18310. 'self-insert-uses-region-functions))))
  18311. (put 'org-delete-char 'delete-selection 'supersede)
  18312. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18313. (put 'org-yank 'delete-selection 'yank)
  18314. ;; Make `flyspell-mode' delay after some commands
  18315. (put 'org-self-insert-command 'flyspell-delayed t)
  18316. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18317. (put 'org-delete-char 'flyspell-delayed t)
  18318. (put 'org-delete-backward-char 'flyspell-delayed t)
  18319. ;; Make pabbrev-mode expand after Org mode commands
  18320. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18321. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18322. (defun org-remap (map &rest commands)
  18323. "In MAP, remap the functions given in COMMANDS.
  18324. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18325. (let (new old)
  18326. (while commands
  18327. (setq old (pop commands) new (pop commands))
  18328. (org-defkey map (vector 'remap old) new))))
  18329. (defun org-transpose-words ()
  18330. "Transpose words for Org.
  18331. This uses the `org-mode-transpose-word-syntax-table' syntax
  18332. table, which interprets characters in `org-emphasis-alist' as
  18333. word constituents."
  18334. (interactive)
  18335. (with-syntax-table org-mode-transpose-word-syntax-table
  18336. (call-interactively 'transpose-words)))
  18337. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18338. (when (eq org-enable-table-editor 'optimized)
  18339. ;; If the user wants maximum table support, we need to hijack
  18340. ;; some standard editing functions
  18341. (org-remap org-mode-map
  18342. 'self-insert-command 'org-self-insert-command
  18343. 'delete-char 'org-delete-char
  18344. 'delete-backward-char 'org-delete-backward-char)
  18345. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18346. (defvar org-ctrl-c-ctrl-c-hook nil
  18347. "Hook for functions attaching themselves to `C-c C-c'.
  18348. This can be used to add additional functionality to the C-c C-c
  18349. key which executes context-dependent commands. This hook is run
  18350. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18351. run after the last test.
  18352. Each function will be called with no arguments. The function
  18353. must check if the context is appropriate for it to act. If yes,
  18354. it should do its thing and then return a non-nil value. If the
  18355. context is wrong, just do nothing and return nil.")
  18356. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18357. "Hook for functions attaching themselves to `C-c C-c'.
  18358. This can be used to add additional functionality to the C-c C-c
  18359. key which executes context-dependent commands. This hook is run
  18360. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18361. before the first test.
  18362. Each function will be called with no arguments. The function
  18363. must check if the context is appropriate for it to act. If yes,
  18364. it should do its thing and then return a non-nil value. If the
  18365. context is wrong, just do nothing and return nil.")
  18366. (defvar org-tab-first-hook nil
  18367. "Hook for functions to attach themselves to TAB.
  18368. See `org-ctrl-c-ctrl-c-hook' for more information.
  18369. This hook runs as the first action when TAB is pressed, even before
  18370. `org-cycle' messes around with the `outline-regexp' to cater for
  18371. inline tasks and plain list item folding.
  18372. If any function in this hook returns t, any other actions that
  18373. would have been caused by TAB (such as table field motion or visibility
  18374. cycling) will not occur.")
  18375. (defvar org-tab-after-check-for-table-hook nil
  18376. "Hook for functions to attach themselves to TAB.
  18377. See `org-ctrl-c-ctrl-c-hook' for more information.
  18378. This hook runs after it has been established that the cursor is not in a
  18379. table, but before checking if the cursor is in a headline or if global cycling
  18380. should be done.
  18381. If any function in this hook returns t, not other actions like visibility
  18382. cycling will be done.")
  18383. (defvar org-tab-after-check-for-cycling-hook nil
  18384. "Hook for functions to attach themselves to TAB.
  18385. See `org-ctrl-c-ctrl-c-hook' for more information.
  18386. This hook runs after it has been established that not table field motion and
  18387. not visibility should be done because of current context. This is probably
  18388. the place where a package like yasnippets can hook in.")
  18389. (defvar org-tab-before-tab-emulation-hook nil
  18390. "Hook for functions to attach themselves to TAB.
  18391. See `org-ctrl-c-ctrl-c-hook' for more information.
  18392. This hook runs after every other options for TAB have been exhausted, but
  18393. before indentation and \t insertion takes place.")
  18394. (defvar org-metaleft-hook nil
  18395. "Hook for functions attaching themselves to `M-left'.
  18396. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18397. (defvar org-metaright-hook nil
  18398. "Hook for functions attaching themselves to `M-right'.
  18399. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18400. (defvar org-metaup-hook nil
  18401. "Hook for functions attaching themselves to `M-up'.
  18402. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18403. (defvar org-metadown-hook nil
  18404. "Hook for functions attaching themselves to `M-down'.
  18405. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18406. (defvar org-shiftmetaleft-hook nil
  18407. "Hook for functions attaching themselves to `M-S-left'.
  18408. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18409. (defvar org-shiftmetaright-hook nil
  18410. "Hook for functions attaching themselves to `M-S-right'.
  18411. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18412. (defvar org-shiftmetaup-hook nil
  18413. "Hook for functions attaching themselves to `M-S-up'.
  18414. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18415. (defvar org-shiftmetadown-hook nil
  18416. "Hook for functions attaching themselves to `M-S-down'.
  18417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18418. (defvar org-metareturn-hook nil
  18419. "Hook for functions attaching themselves to `M-RET'.
  18420. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18421. (defvar org-shiftup-hook nil
  18422. "Hook for functions attaching themselves to `S-up'.
  18423. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18424. (defvar org-shiftup-final-hook nil
  18425. "Hook for functions attaching themselves to `S-up'.
  18426. This one runs after all other options except shift-select have been excluded.
  18427. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18428. (defvar org-shiftdown-hook nil
  18429. "Hook for functions attaching themselves to `S-down'.
  18430. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18431. (defvar org-shiftdown-final-hook nil
  18432. "Hook for functions attaching themselves to `S-down'.
  18433. This one runs after all other options except shift-select have been excluded.
  18434. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18435. (defvar org-shiftleft-hook nil
  18436. "Hook for functions attaching themselves to `S-left'.
  18437. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18438. (defvar org-shiftleft-final-hook nil
  18439. "Hook for functions attaching themselves to `S-left'.
  18440. This one runs after all other options except shift-select have been excluded.
  18441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18442. (defvar org-shiftright-hook nil
  18443. "Hook for functions attaching themselves to `S-right'.
  18444. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18445. (defvar org-shiftright-final-hook nil
  18446. "Hook for functions attaching themselves to `S-right'.
  18447. This one runs after all other options except shift-select have been excluded.
  18448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18449. (defun org-modifier-cursor-error ()
  18450. "Throw an error, a modified cursor command was applied in wrong context."
  18451. (user-error "This command is active in special context like tables, headlines or items"))
  18452. (defun org-shiftselect-error ()
  18453. "Throw an error because Shift-Cursor command was applied in wrong context."
  18454. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18455. (user-error "To use shift-selection with Org mode, customize `org-support-shift-select'")
  18456. (user-error "This command works only in special context like headlines or timestamps")))
  18457. (defun org-call-for-shift-select (cmd)
  18458. (let ((this-command-keys-shift-translated t))
  18459. (call-interactively cmd)))
  18460. (defun org-shifttab (&optional arg)
  18461. "Global visibility cycling or move to previous table field.
  18462. Call `org-table-previous-field' within a table.
  18463. When ARG is nil, cycle globally through visibility states.
  18464. When ARG is a numeric prefix, show contents of this level."
  18465. (interactive "P")
  18466. (cond
  18467. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18468. ((integerp arg)
  18469. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18470. (message "Content view to level: %d" arg)
  18471. (org-content (prefix-numeric-value arg2))
  18472. (org-cycle-show-empty-lines t)
  18473. (setq org-cycle-global-status 'overview)))
  18474. (t (call-interactively 'org-global-cycle))))
  18475. (defun org-shiftmetaleft ()
  18476. "Promote subtree or delete table column.
  18477. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18478. `org-table-delete-column', depending on context. See the
  18479. individual commands for more information."
  18480. (interactive)
  18481. (cond
  18482. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18483. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18484. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18485. ((if (not (org-region-active-p)) (org-at-item-p)
  18486. (save-excursion (goto-char (region-beginning))
  18487. (org-at-item-p)))
  18488. (call-interactively 'org-outdent-item-tree))
  18489. (t (org-modifier-cursor-error))))
  18490. (defun org-shiftmetaright ()
  18491. "Demote subtree or insert table column.
  18492. Calls `org-demote-subtree', `org-indent-item-tree', or
  18493. `org-table-insert-column', depending on context. See the
  18494. individual commands for more information."
  18495. (interactive)
  18496. (cond
  18497. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18498. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18499. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18500. ((if (not (org-region-active-p)) (org-at-item-p)
  18501. (save-excursion (goto-char (region-beginning))
  18502. (org-at-item-p)))
  18503. (call-interactively 'org-indent-item-tree))
  18504. (t (org-modifier-cursor-error))))
  18505. (defun org-shiftmetaup (&optional _arg)
  18506. "Drag the line at point up.
  18507. In a table, kill the current row.
  18508. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18509. but also adjust the previous clocked item in the clock history.
  18510. Everywhere else, drag the line at point up."
  18511. (interactive "P")
  18512. (cond
  18513. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18514. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18515. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18516. (call-interactively 'org-timestamp-up)))
  18517. (t (call-interactively 'org-drag-line-backward))))
  18518. (defun org-shiftmetadown (&optional _arg)
  18519. "Drag the line at point down.
  18520. In a table, insert an empty row at the current line.
  18521. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18522. but also adjust the previous clocked item in the clock history.
  18523. Everywhere else, drag the line at point down."
  18524. (interactive "P")
  18525. (cond
  18526. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18527. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18528. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18529. (call-interactively 'org-timestamp-down)))
  18530. (t (call-interactively 'org-drag-line-forward))))
  18531. (defsubst org-hidden-tree-error ()
  18532. (user-error
  18533. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18534. (defun org-metaleft (&optional _arg)
  18535. "Promote heading, list item at point or move table column left.
  18536. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18537. depending on context. With no specific context, calls the Emacs
  18538. default `backward-word'. See the individual commands for more
  18539. information.
  18540. This function runs the hook `org-metaleft-hook' as a first step,
  18541. and returns at first non-nil value."
  18542. (interactive "P")
  18543. (cond
  18544. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18545. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18546. ((org-with-limited-levels
  18547. (or (org-at-heading-p)
  18548. (and (org-region-active-p)
  18549. (save-excursion
  18550. (goto-char (region-beginning))
  18551. (org-at-heading-p)))))
  18552. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18553. (call-interactively 'org-do-promote))
  18554. ;; At an inline task.
  18555. ((org-at-heading-p)
  18556. (call-interactively 'org-inlinetask-promote))
  18557. ((or (org-at-item-p)
  18558. (and (org-region-active-p)
  18559. (save-excursion
  18560. (goto-char (region-beginning))
  18561. (org-at-item-p))))
  18562. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18563. (call-interactively 'org-outdent-item))
  18564. (t (call-interactively 'backward-word))))
  18565. (defun org-metaright (&optional _arg)
  18566. "Demote heading, list item at point or move table column right.
  18567. In front of a drawer or a block keyword, indent it correctly.
  18568. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18569. `org-indent-drawer' or `org-indent-block' depending on context.
  18570. With no specific context, calls the Emacs default `forward-word'.
  18571. See the individual commands for more information.
  18572. This function runs the hook `org-metaright-hook' as a first step,
  18573. and returns at first non-nil value."
  18574. (interactive "P")
  18575. (cond
  18576. ((run-hook-with-args-until-success 'org-metaright-hook))
  18577. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18578. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18579. ((org-at-block-p) (call-interactively 'org-indent-block))
  18580. ((org-with-limited-levels
  18581. (or (org-at-heading-p)
  18582. (and (org-region-active-p)
  18583. (save-excursion
  18584. (goto-char (region-beginning))
  18585. (org-at-heading-p)))))
  18586. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18587. (call-interactively 'org-do-demote))
  18588. ;; At an inline task.
  18589. ((org-at-heading-p)
  18590. (call-interactively 'org-inlinetask-demote))
  18591. ((or (org-at-item-p)
  18592. (and (org-region-active-p)
  18593. (save-excursion
  18594. (goto-char (region-beginning))
  18595. (org-at-item-p))))
  18596. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18597. (call-interactively 'org-indent-item))
  18598. (t (call-interactively 'forward-word))))
  18599. (defun org-check-for-hidden (what)
  18600. "Check if there are hidden headlines/items in the current visual line.
  18601. WHAT can be either `headlines' or `items'. If the current line is
  18602. an outline or item heading and it has a folded subtree below it,
  18603. this function returns t, nil otherwise."
  18604. (let ((re (cond
  18605. ((eq what 'headlines) org-outline-regexp-bol)
  18606. ((eq what 'items) (org-item-beginning-re))
  18607. (t (error "This should not happen"))))
  18608. beg end)
  18609. (save-excursion
  18610. (catch 'exit
  18611. (unless (org-region-active-p)
  18612. (setq beg (point-at-bol))
  18613. (beginning-of-line 2)
  18614. (while (and (not (eobp)) ;; this is like `next-line'
  18615. (get-char-property (1- (point)) 'invisible))
  18616. (beginning-of-line 2))
  18617. (setq end (point))
  18618. (goto-char beg)
  18619. (goto-char (point-at-eol))
  18620. (setq end (max end (point)))
  18621. (while (re-search-forward re end t)
  18622. (when (get-char-property (match-beginning 0) 'invisible)
  18623. (throw 'exit t))))
  18624. nil))))
  18625. (defun org-metaup (&optional _arg)
  18626. "Move subtree up or move table row up.
  18627. Calls `org-move-subtree-up' or `org-table-move-row' or
  18628. `org-move-item-up', depending on context. See the individual commands
  18629. for more information."
  18630. (interactive "P")
  18631. (cond
  18632. ((run-hook-with-args-until-success 'org-metaup-hook))
  18633. ((org-region-active-p)
  18634. (let* ((a (min (region-beginning) (region-end)))
  18635. (b (1- (max (region-beginning) (region-end))))
  18636. (c (save-excursion (goto-char a)
  18637. (move-beginning-of-line 0)))
  18638. (d (save-excursion (goto-char a)
  18639. (move-end-of-line 0) (point))))
  18640. (transpose-regions a b c d)
  18641. (goto-char c)))
  18642. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18643. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18644. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18645. (t (org-drag-element-backward))))
  18646. (defun org-metadown (&optional _arg)
  18647. "Move subtree down or move table row down.
  18648. Calls `org-move-subtree-down' or `org-table-move-row' or
  18649. `org-move-item-down', depending on context. See the individual
  18650. commands for more information."
  18651. (interactive "P")
  18652. (cond
  18653. ((run-hook-with-args-until-success 'org-metadown-hook))
  18654. ((org-region-active-p)
  18655. (let* ((a (min (region-beginning) (region-end)))
  18656. (b (max (region-beginning) (region-end)))
  18657. (c (save-excursion (goto-char b)
  18658. (move-beginning-of-line 1)))
  18659. (d (save-excursion (goto-char b)
  18660. (move-end-of-line 1) (1+ (point)))))
  18661. (transpose-regions a b c d)
  18662. (goto-char d)))
  18663. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18664. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18665. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18666. (t (org-drag-element-forward))))
  18667. (defun org-shiftup (&optional arg)
  18668. "Increase item in timestamp or increase priority of current headline.
  18669. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18670. depending on context. See the individual commands for more information."
  18671. (interactive "P")
  18672. (cond
  18673. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18674. ((and org-support-shift-select (org-region-active-p))
  18675. (org-call-for-shift-select 'previous-line))
  18676. ((org-at-timestamp-p 'lax)
  18677. (call-interactively (if org-edit-timestamp-down-means-later
  18678. 'org-timestamp-down 'org-timestamp-up)))
  18679. ((and (not (eq org-support-shift-select 'always))
  18680. org-enable-priority-commands
  18681. (org-at-heading-p))
  18682. (call-interactively 'org-priority-up))
  18683. ((and (not org-support-shift-select) (org-at-item-p))
  18684. (call-interactively 'org-previous-item))
  18685. ((org-clocktable-try-shift 'up arg))
  18686. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18687. (org-support-shift-select
  18688. (org-call-for-shift-select 'previous-line))
  18689. (t (org-shiftselect-error))))
  18690. (defun org-shiftdown (&optional arg)
  18691. "Decrease item in timestamp or decrease priority of current headline.
  18692. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18693. depending on context. See the individual commands for more information."
  18694. (interactive "P")
  18695. (cond
  18696. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18697. ((and org-support-shift-select (org-region-active-p))
  18698. (org-call-for-shift-select 'next-line))
  18699. ((org-at-timestamp-p 'lax)
  18700. (call-interactively (if org-edit-timestamp-down-means-later
  18701. 'org-timestamp-up 'org-timestamp-down)))
  18702. ((and (not (eq org-support-shift-select 'always))
  18703. org-enable-priority-commands
  18704. (org-at-heading-p))
  18705. (call-interactively 'org-priority-down))
  18706. ((and (not org-support-shift-select) (org-at-item-p))
  18707. (call-interactively 'org-next-item))
  18708. ((org-clocktable-try-shift 'down arg))
  18709. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18710. (org-support-shift-select
  18711. (org-call-for-shift-select 'next-line))
  18712. (t (org-shiftselect-error))))
  18713. (defun org-shiftright (&optional arg)
  18714. "Cycle the thing at point or in the current line, depending on context.
  18715. Depending on context, this does one of the following:
  18716. - switch a timestamp at point one day into the future
  18717. - on a headline, switch to the next TODO keyword.
  18718. - on an item, switch entire list to the next bullet type
  18719. - on a property line, switch to the next allowed value
  18720. - on a clocktable definition line, move time block into the future"
  18721. (interactive "P")
  18722. (cond
  18723. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18724. ((and org-support-shift-select (org-region-active-p))
  18725. (org-call-for-shift-select 'forward-char))
  18726. ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-up-day))
  18727. ((and (not (eq org-support-shift-select 'always))
  18728. (org-at-heading-p))
  18729. (let ((org-inhibit-logging
  18730. (not org-treat-S-cursor-todo-selection-as-state-change))
  18731. (org-inhibit-blocking
  18732. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18733. (org-call-with-arg 'org-todo 'right)))
  18734. ((or (and org-support-shift-select
  18735. (not (eq org-support-shift-select 'always))
  18736. (org-at-item-bullet-p))
  18737. (and (not org-support-shift-select) (org-at-item-p)))
  18738. (org-call-with-arg 'org-cycle-list-bullet nil))
  18739. ((and (not (eq org-support-shift-select 'always))
  18740. (org-at-property-p))
  18741. (call-interactively 'org-property-next-allowed-value))
  18742. ((org-clocktable-try-shift 'right arg))
  18743. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18744. (org-support-shift-select
  18745. (org-call-for-shift-select 'forward-char))
  18746. (t (org-shiftselect-error))))
  18747. (defun org-shiftleft (&optional arg)
  18748. "Cycle the thing at point or in the current line, depending on context.
  18749. Depending on context, this does one of the following:
  18750. - switch a timestamp at point one day into the past
  18751. - on a headline, switch to the previous TODO keyword.
  18752. - on an item, switch entire list to the previous bullet type
  18753. - on a property line, switch to the previous allowed value
  18754. - on a clocktable definition line, move time block into the past"
  18755. (interactive "P")
  18756. (cond
  18757. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18758. ((and org-support-shift-select (org-region-active-p))
  18759. (org-call-for-shift-select 'backward-char))
  18760. ((org-at-timestamp-p 'lax) (call-interactively 'org-timestamp-down-day))
  18761. ((and (not (eq org-support-shift-select 'always))
  18762. (org-at-heading-p))
  18763. (let ((org-inhibit-logging
  18764. (not org-treat-S-cursor-todo-selection-as-state-change))
  18765. (org-inhibit-blocking
  18766. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18767. (org-call-with-arg 'org-todo 'left)))
  18768. ((or (and org-support-shift-select
  18769. (not (eq org-support-shift-select 'always))
  18770. (org-at-item-bullet-p))
  18771. (and (not org-support-shift-select) (org-at-item-p)))
  18772. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18773. ((and (not (eq org-support-shift-select 'always))
  18774. (org-at-property-p))
  18775. (call-interactively 'org-property-previous-allowed-value))
  18776. ((org-clocktable-try-shift 'left arg))
  18777. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18778. (org-support-shift-select
  18779. (org-call-for-shift-select 'backward-char))
  18780. (t (org-shiftselect-error))))
  18781. (defun org-shiftcontrolright ()
  18782. "Switch to next TODO set."
  18783. (interactive)
  18784. (cond
  18785. ((and org-support-shift-select (org-region-active-p))
  18786. (org-call-for-shift-select 'forward-word))
  18787. ((and (not (eq org-support-shift-select 'always))
  18788. (org-at-heading-p))
  18789. (org-call-with-arg 'org-todo 'nextset))
  18790. (org-support-shift-select
  18791. (org-call-for-shift-select 'forward-word))
  18792. (t (org-shiftselect-error))))
  18793. (defun org-shiftcontrolleft ()
  18794. "Switch to previous TODO set."
  18795. (interactive)
  18796. (cond
  18797. ((and org-support-shift-select (org-region-active-p))
  18798. (org-call-for-shift-select 'backward-word))
  18799. ((and (not (eq org-support-shift-select 'always))
  18800. (org-at-heading-p))
  18801. (org-call-with-arg 'org-todo 'previousset))
  18802. (org-support-shift-select
  18803. (org-call-for-shift-select 'backward-word))
  18804. (t (org-shiftselect-error))))
  18805. (defun org-shiftcontrolup (&optional n)
  18806. "Change timestamps synchronously up in CLOCK log lines.
  18807. Optional argument N tells to change by that many units."
  18808. (interactive "P")
  18809. (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
  18810. (let (org-support-shift-select)
  18811. (org-clock-timestamps-up n))
  18812. (user-error "Not at a clock log")))
  18813. (defun org-shiftcontroldown (&optional n)
  18814. "Change timestamps synchronously down in CLOCK log lines.
  18815. Optional argument N tells to change by that many units."
  18816. (interactive "P")
  18817. (if (and (org-at-clock-log-p) (org-at-timestamp-p 'lax))
  18818. (let (org-support-shift-select)
  18819. (org-clock-timestamps-down n))
  18820. (user-error "Not at a clock log")))
  18821. (defun org-increase-number-at-point (&optional inc)
  18822. "Increment the number at point.
  18823. With an optional prefix numeric argument INC, increment using
  18824. this numeric value."
  18825. (interactive "p")
  18826. (if (not (number-at-point))
  18827. (user-error "Not on a number")
  18828. (unless inc (setq inc 1))
  18829. (let ((pos (point))
  18830. (beg (skip-chars-backward "-+^/*0-9eE."))
  18831. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18832. (setq nap (buffer-substring-no-properties
  18833. (+ pos beg) (+ pos beg end)))
  18834. (delete-region (+ pos beg) (+ pos beg end))
  18835. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18836. (when (org-at-table-p)
  18837. (org-table-align)
  18838. (org-table-end-of-field 1))))
  18839. (defun org-decrease-number-at-point (&optional inc)
  18840. "Decrement the number at point.
  18841. With an optional prefix numeric argument INC, decrement using
  18842. this numeric value."
  18843. (interactive "p")
  18844. (org-increase-number-at-point (- (or inc 1))))
  18845. (defun org-ctrl-c-ret ()
  18846. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18847. (interactive)
  18848. (cond
  18849. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18850. (t (call-interactively 'org-insert-heading))))
  18851. (defun org-find-visible ()
  18852. (let ((s (point)))
  18853. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18854. (get-char-property s 'invisible)))
  18855. s))
  18856. (defun org-find-invisible ()
  18857. (let ((s (point)))
  18858. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18859. (not (get-char-property s 'invisible))))
  18860. s))
  18861. (defun org-copy-visible (beg end)
  18862. "Copy the visible parts of the region."
  18863. (interactive "r")
  18864. (let (snippets s)
  18865. (save-excursion
  18866. (save-restriction
  18867. (narrow-to-region beg end)
  18868. (setq s (goto-char (point-min)))
  18869. (while (not (= (point) (point-max)))
  18870. (goto-char (org-find-invisible))
  18871. (push (buffer-substring s (point)) snippets)
  18872. (setq s (goto-char (org-find-visible))))))
  18873. (kill-new (apply 'concat (nreverse snippets)))))
  18874. (defun org-copy-special ()
  18875. "Copy region in table or copy current subtree.
  18876. Calls `org-table-copy-region' or `org-copy-subtree', depending on
  18877. context. See the individual commands for more information."
  18878. (interactive)
  18879. (call-interactively
  18880. (if (org-at-table-p) #'org-table-copy-region #'org-copy-subtree)))
  18881. (defun org-cut-special ()
  18882. "Cut region in table or cut current subtree.
  18883. Calls `org-table-cut-region' or `org-cut-subtree', depending on
  18884. context. See the individual commands for more information."
  18885. (interactive)
  18886. (call-interactively
  18887. (if (org-at-table-p) #'org-table-cut-region #'org-cut-subtree)))
  18888. (defun org-paste-special (arg)
  18889. "Paste rectangular region into table, or past subtree relative to level.
  18890. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18891. See the individual commands for more information."
  18892. (interactive "P")
  18893. (if (org-at-table-p)
  18894. (org-table-paste-rectangle)
  18895. (org-paste-subtree arg)))
  18896. (defun org-edit-special (&optional arg)
  18897. "Call a special editor for the element at point.
  18898. When at a table, call the formula editor with `org-table-edit-formulas'.
  18899. When in a source code block, call `org-edit-src-code'.
  18900. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18901. When in an export block, call `org-edit-export-block'.
  18902. When in a LaTeX environment, call `org-edit-latex-environment'.
  18903. When at an #+INCLUDE keyword, visit the included file.
  18904. When at a footnote reference, call `org-edit-footnote-reference'
  18905. On a link, call `ffap' to visit the link at point.
  18906. Otherwise, return a user error."
  18907. (interactive "P")
  18908. (let ((element (org-element-at-point)))
  18909. (barf-if-buffer-read-only)
  18910. (pcase (org-element-type element)
  18911. (`src-block
  18912. (if (not arg) (org-edit-src-code)
  18913. (let* ((info (org-babel-get-src-block-info))
  18914. (lang (nth 0 info))
  18915. (params (nth 2 info))
  18916. (session (cdr (assq :session params))))
  18917. (if (not session) (org-edit-src-code)
  18918. ;; At a src-block with a session and function called with
  18919. ;; an ARG: switch to the buffer related to the inferior
  18920. ;; process.
  18921. (switch-to-buffer
  18922. (funcall (intern (concat "org-babel-prep-session:" lang))
  18923. session params))))))
  18924. (`keyword
  18925. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18926. (org-open-link-from-string
  18927. (format "[[%s]]"
  18928. (expand-file-name
  18929. (let ((value (org-element-property :value element)))
  18930. (cond ((org-file-url-p value)
  18931. (user-error "The file is specified as a URL, cannot be edited"))
  18932. ((not (org-string-nw-p value))
  18933. (user-error "No file to edit"))
  18934. ((string-match "\\`\"\\(.*?\\)\"" value)
  18935. (match-string 1 value))
  18936. ((string-match "\\`[^ \t\"]\\S-*" value)
  18937. (match-string 0 value))
  18938. (t (user-error "No valid file specified")))))))
  18939. (user-error "No special environment to edit here")))
  18940. (`table
  18941. (if (eq (org-element-property :type element) 'table.el)
  18942. (org-edit-table.el)
  18943. (call-interactively 'org-table-edit-formulas)))
  18944. ;; Only Org tables contain `table-row' type elements.
  18945. (`table-row (call-interactively 'org-table-edit-formulas))
  18946. (`example-block (org-edit-src-code))
  18947. (`export-block (org-edit-export-block))
  18948. (`fixed-width (org-edit-fixed-width-region))
  18949. (`latex-environment (org-edit-latex-environment))
  18950. (_
  18951. ;; No notable element at point. Though, we may be at a link or
  18952. ;; a footnote reference, which are objects. Thus, scan deeper.
  18953. (let ((context (org-element-context element)))
  18954. (pcase (org-element-type context)
  18955. (`footnote-reference (org-edit-footnote-reference))
  18956. (`inline-src-block (org-edit-inline-src-code))
  18957. (`link (call-interactively #'ffap))
  18958. (_ (user-error "No special environment to edit here"))))))))
  18959. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18960. (defun org-ctrl-c-ctrl-c (&optional arg)
  18961. "Set tags in headline, or update according to changed information at point.
  18962. This command does many different things, depending on context:
  18963. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18964. this is what we do.
  18965. - If the cursor is on a statistics cookie, update it.
  18966. - If the cursor is in a headline, prompt for tags and insert them
  18967. into the current line, aligned to `org-tags-column'. When called
  18968. with prefix arg, realign all tags in the current buffer.
  18969. - If the cursor is in one of the special #+KEYWORD lines, this
  18970. triggers scanning the buffer for these lines and updating the
  18971. information.
  18972. - If the cursor is inside a table, realign the table. This command
  18973. works even if the automatic table editor has been turned off.
  18974. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18975. the entire table.
  18976. - If the cursor is at a footnote reference or definition, jump to
  18977. the corresponding definition or references, respectively.
  18978. - If the cursor is a the beginning of a dynamic block, update it.
  18979. - If the current buffer is a capture buffer, close note and file it.
  18980. - If the cursor is on a <<<target>>>, update radio targets and
  18981. corresponding links in this buffer.
  18982. - If the cursor is on a numbered item in a plain list, renumber the
  18983. ordered list.
  18984. - If the cursor is on a checkbox, toggle it.
  18985. - If the cursor is on a code block, evaluate it. The variable
  18986. `org-confirm-babel-evaluate' can be used to control prompting
  18987. before code block evaluation, by default every code block
  18988. evaluation requires confirmation. Code block evaluation can be
  18989. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18990. (interactive "P")
  18991. (cond
  18992. ((or (bound-and-true-p org-clock-overlays) org-occur-highlights)
  18993. (when (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18994. (org-remove-occur-highlights)
  18995. (message "Temporary highlights/overlays removed from current buffer"))
  18996. ((and (local-variable-p 'org-finish-function)
  18997. (fboundp org-finish-function))
  18998. (funcall org-finish-function))
  18999. ((org-babel-hash-at-point))
  19000. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19001. (t
  19002. (let* ((context
  19003. (org-element-lineage
  19004. (org-element-context)
  19005. ;; Limit to supported contexts.
  19006. '(babel-call clock dynamic-block footnote-definition
  19007. footnote-reference inline-babel-call inline-src-block
  19008. inlinetask item keyword node-property paragraph
  19009. plain-list property-drawer radio-target src-block
  19010. statistics-cookie table table-cell table-row
  19011. timestamp)
  19012. t))
  19013. (type (org-element-type context)))
  19014. ;; For convenience: at the first line of a paragraph on the same
  19015. ;; line as an item, apply function on that item instead.
  19016. (when (eq type 'paragraph)
  19017. (let ((parent (org-element-property :parent context)))
  19018. (when (and (eq (org-element-type parent) 'item)
  19019. (= (line-beginning-position)
  19020. (org-element-property :begin parent)))
  19021. (setq context parent)
  19022. (setq type 'item))))
  19023. ;; Act according to type of element or object at point.
  19024. ;;
  19025. ;; Do nothing on a blank line, except if it is contained in
  19026. ;; a src block. Hence, we first check if point is in such
  19027. ;; a block and then if it is at a blank line.
  19028. (pcase type
  19029. ((or `inline-src-block `src-block)
  19030. (unless org-babel-no-eval-on-ctrl-c-ctrl-c
  19031. (org-babel-eval-wipe-error-buffer)
  19032. (org-babel-execute-src-block
  19033. current-prefix-arg (org-babel-get-src-block-info nil context))))
  19034. ((guard (org-match-line "[ \t]*$"))
  19035. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19036. (user-error
  19037. (substitute-command-keys
  19038. "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))
  19039. ((or `babel-call `inline-babel-call)
  19040. (let ((info (org-babel-lob-get-info context)))
  19041. (when info (org-babel-execute-src-block nil info))))
  19042. (`clock (org-clock-update-time-maybe))
  19043. (`dynamic-block
  19044. (save-excursion
  19045. (goto-char (org-element-property :post-affiliated context))
  19046. (org-update-dblock)))
  19047. (`footnote-definition
  19048. (goto-char (org-element-property :post-affiliated context))
  19049. (call-interactively 'org-footnote-action))
  19050. (`footnote-reference (call-interactively #'org-footnote-action))
  19051. ((or `headline `inlinetask)
  19052. (save-excursion (goto-char (org-element-property :begin context))
  19053. (call-interactively #'org-set-tags)))
  19054. (`item
  19055. ;; At an item: `C-u C-u' sets checkbox to "[-]"
  19056. ;; unconditionally, whereas `C-u' will toggle its presence.
  19057. ;; Without a universal argument, if the item has a checkbox,
  19058. ;; toggle it. Otherwise repair the list.
  19059. (let* ((box (org-element-property :checkbox context))
  19060. (struct (org-element-property :structure context))
  19061. (old-struct (copy-tree struct))
  19062. (parents (org-list-parents-alist struct))
  19063. (prevs (org-list-prevs-alist struct))
  19064. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19065. (org-list-set-checkbox
  19066. (org-element-property :begin context) struct
  19067. (cond ((equal arg '(16)) "[-]")
  19068. ((and (not box) (equal arg '(4))) "[ ]")
  19069. ((or (not box) (equal arg '(4))) nil)
  19070. ((eq box 'on) "[ ]")
  19071. (t "[X]")))
  19072. ;; Mimic `org-list-write-struct' but with grabbing a return
  19073. ;; value from `org-list-struct-fix-box'.
  19074. (org-list-struct-fix-ind struct parents 2)
  19075. (org-list-struct-fix-item-end struct)
  19076. (org-list-struct-fix-bul struct prevs)
  19077. (org-list-struct-fix-ind struct parents)
  19078. (let ((block-item
  19079. (org-list-struct-fix-box struct parents prevs orderedp)))
  19080. (if (and box (equal struct old-struct))
  19081. (if (equal arg '(16))
  19082. (message "Checkboxes already reset")
  19083. (user-error "Cannot toggle this checkbox: %s"
  19084. (if (eq box 'on)
  19085. "all subitems checked"
  19086. "unchecked subitems")))
  19087. (org-list-struct-apply-struct struct old-struct)
  19088. (org-update-checkbox-count-maybe))
  19089. (when block-item
  19090. (message "Checkboxes were removed due to empty box at line %d"
  19091. (org-current-line block-item))))))
  19092. (`keyword
  19093. (let ((org-inhibit-startup-visibility-stuff t)
  19094. (org-startup-align-all-tables nil))
  19095. (when (boundp 'org-table-coordinate-overlays)
  19096. (mapc #'delete-overlay org-table-coordinate-overlays)
  19097. (setq org-table-coordinate-overlays nil))
  19098. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19099. (message "Local setup has been refreshed"))
  19100. (`plain-list
  19101. ;; At a plain list, with a double C-u argument, set
  19102. ;; checkboxes of each item to "[-]", whereas a single one
  19103. ;; will toggle their presence according to the state of the
  19104. ;; first item in the list. Without an argument, repair the
  19105. ;; list.
  19106. (let* ((begin (org-element-property :contents-begin context))
  19107. (beginm (move-marker (make-marker) begin))
  19108. (struct (org-element-property :structure context))
  19109. (old-struct (copy-tree struct))
  19110. (first-box (save-excursion
  19111. (goto-char begin)
  19112. (looking-at org-list-full-item-re)
  19113. (match-string-no-properties 3)))
  19114. (new-box (cond ((equal arg '(16)) "[-]")
  19115. ((equal arg '(4)) (unless first-box "[ ]"))
  19116. ((equal first-box "[X]") "[ ]")
  19117. (t "[X]"))))
  19118. (cond
  19119. (arg
  19120. (dolist (pos
  19121. (org-list-get-all-items
  19122. begin struct (org-list-prevs-alist struct)))
  19123. (org-list-set-checkbox pos struct new-box)))
  19124. ((and first-box (eq (point) begin))
  19125. ;; For convenience, when point is at bol on the first
  19126. ;; item of the list and no argument is provided, simply
  19127. ;; toggle checkbox of that item, if any.
  19128. (org-list-set-checkbox begin struct new-box)))
  19129. (org-list-write-struct
  19130. struct (org-list-parents-alist struct) old-struct)
  19131. (org-update-checkbox-count-maybe)
  19132. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19133. ((or `property-drawer `node-property)
  19134. (call-interactively #'org-property-action))
  19135. (`radio-target
  19136. (call-interactively #'org-update-radio-target-regexp))
  19137. (`statistics-cookie
  19138. (call-interactively #'org-update-statistics-cookies))
  19139. ((or `table `table-cell `table-row)
  19140. ;; At a table, recalculate every field and align it. Also
  19141. ;; send the table if necessary. If the table has
  19142. ;; a `table.el' type, just give up. At a table row or cell,
  19143. ;; maybe recalculate line but always align table.
  19144. (if (eq (org-element-property :type context) 'table.el)
  19145. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19146. Use `\\[org-edit-special]' to edit table.el tables"))
  19147. (let ((org-enable-table-editor t))
  19148. (if (or (eq type 'table)
  19149. ;; Check if point is at a TBLFM line.
  19150. (and (eq type 'table-row)
  19151. (= (point) (org-element-property :end context))))
  19152. (save-excursion
  19153. (if (org-at-TBLFM-p)
  19154. (progn (require 'org-table)
  19155. (org-table-calc-current-TBLFM))
  19156. (goto-char (org-element-property :contents-begin context))
  19157. (org-call-with-arg 'org-table-recalculate (or arg t))
  19158. (orgtbl-send-table 'maybe)))
  19159. (org-table-maybe-eval-formula)
  19160. (cond (arg (call-interactively #'org-table-recalculate))
  19161. ((org-table-maybe-recalculate-line))
  19162. (t (org-table-align)))))))
  19163. (`timestamp (org-timestamp-change 0 'day))
  19164. ((and `nil (guard (org-at-heading-p)))
  19165. ;; When point is on an unsupported object type, we can miss
  19166. ;; the fact that it also is at a heading. Handle it here.
  19167. (call-interactively #'org-set-tags))
  19168. ((guard
  19169. (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)))
  19170. (_
  19171. (user-error
  19172. (substitute-command-keys
  19173. "`\\[org-ctrl-c-ctrl-c]' can do nothing useful here"))))))))
  19174. (defun org-mode-restart ()
  19175. (interactive)
  19176. (let ((indent-status (bound-and-true-p org-indent-mode)))
  19177. (funcall major-mode)
  19178. (hack-local-variables)
  19179. (when (and indent-status (not (bound-and-true-p org-indent-mode)))
  19180. (org-indent-mode -1))
  19181. (org-reset-file-cache))
  19182. (message "%s restarted" major-mode))
  19183. (defun org-kill-note-or-show-branches ()
  19184. "Abort storing current note, or call `outline-show-branches'."
  19185. (interactive)
  19186. (if (not org-finish-function)
  19187. (progn
  19188. (outline-hide-subtree)
  19189. (call-interactively 'outline-show-branches))
  19190. (let ((org-note-abort t))
  19191. (funcall org-finish-function))))
  19192. (defun org-delete-indentation (&optional arg)
  19193. "Join current line to previous and fix whitespace at join.
  19194. If previous line is a headline add to headline title. Otherwise
  19195. the function calls `delete-indentation'.
  19196. With a non-nil optional argument, join it to the following one."
  19197. (interactive "*P")
  19198. (if (save-excursion
  19199. (beginning-of-line (if arg 1 0))
  19200. (let ((case-fold-search nil))
  19201. (looking-at org-complex-heading-regexp)))
  19202. ;; At headline.
  19203. (let ((tags-column (when (match-beginning 5)
  19204. (save-excursion (goto-char (match-beginning 5))
  19205. (current-column))))
  19206. (string (concat " " (progn (when arg (forward-line 1))
  19207. (org-trim (delete-and-extract-region
  19208. (line-beginning-position)
  19209. (line-end-position)))))))
  19210. (unless (bobp) (delete-region (point) (1- (point))))
  19211. (goto-char (or (match-end 4)
  19212. (match-beginning 5)
  19213. (match-end 0)))
  19214. (skip-chars-backward " \t")
  19215. (save-excursion (insert string))
  19216. ;; Adjust alignment of tags.
  19217. (cond
  19218. ((not tags-column)) ;no tags
  19219. (org-auto-align-tags (org-set-tags nil t))
  19220. (t (org--align-tags-here tags-column)))) ;preserve tags column
  19221. (delete-indentation arg)))
  19222. (defun org-open-line (n)
  19223. "Insert a new row in tables, call `open-line' elsewhere.
  19224. If `org-special-ctrl-o' is nil, just call `open-line' everywhere.
  19225. As a special case, when a document starts with a table, allow to
  19226. call `open-line' on the very first character."
  19227. (interactive "*p")
  19228. (if (and org-special-ctrl-o (/= (point) 1) (org-at-table-p))
  19229. (org-table-insert-row)
  19230. (open-line n)))
  19231. (defun org-return (&optional indent)
  19232. "Goto next table row or insert a newline.
  19233. Calls `org-table-next-row' or `newline', depending on context.
  19234. When optional INDENT argument is non-nil, call
  19235. `newline-and-indent' instead of `newline'.
  19236. When `org-return-follows-link' is non-nil and point is on
  19237. a timestamp or a link, call `org-open-at-point'. However, it
  19238. will not happen if point is in a table or on a \"dead\"
  19239. object (e.g., within a comment). In these case, you need to use
  19240. `org-open-at-point' directly."
  19241. (interactive)
  19242. (let ((context (if org-return-follows-link (org-element-context)
  19243. (org-element-at-point))))
  19244. (cond
  19245. ;; In a table, call `org-table-next-row'.
  19246. ((or (and (eq (org-element-type context) 'table)
  19247. (>= (point) (org-element-property :contents-begin context))
  19248. (< (point) (org-element-property :contents-end context)))
  19249. (org-element-lineage context '(table-row table-cell) t))
  19250. (org-table-justify-field-maybe)
  19251. (call-interactively #'org-table-next-row))
  19252. ;; On a link or a timestamp, call `org-open-at-point' if
  19253. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19254. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19255. ((and org-return-follows-link
  19256. (or (org-in-regexp org-ts-regexp-both nil t)
  19257. (org-in-regexp org-tsr-regexp-both nil t)
  19258. (org-in-regexp org-any-link-re nil t)))
  19259. (call-interactively #'org-open-at-point))
  19260. ;; Insert newline in heading, but preserve tags.
  19261. ((and (not (bolp))
  19262. (save-excursion (beginning-of-line)
  19263. (let ((case-fold-search nil))
  19264. (looking-at org-complex-heading-regexp))))
  19265. ;; At headline. Split line. However, if point is on keyword,
  19266. ;; priority cookie or tags, do not break any of them: add
  19267. ;; a newline after the headline instead.
  19268. (let ((tags-column (and (match-beginning 5)
  19269. (save-excursion (goto-char (match-beginning 5))
  19270. (current-column))))
  19271. (string
  19272. (when (and (match-end 4) (org-point-in-group (point) 4))
  19273. (delete-and-extract-region (point) (match-end 4)))))
  19274. ;; Adjust tag alignment.
  19275. (cond
  19276. ((not (and tags-column string)))
  19277. (org-auto-align-tags (org-set-tags nil t))
  19278. (t (org--align-tags-here tags-column))) ;preserve tags column
  19279. (end-of-line)
  19280. (org-show-entry)
  19281. (if indent (newline-and-indent) (newline))
  19282. (when string (save-excursion (insert (org-trim string))))))
  19283. ;; In a list, make sure indenting keeps trailing text within.
  19284. ((and indent
  19285. (not (eolp))
  19286. (org-element-lineage context '(item)))
  19287. (let ((trailing-data
  19288. (delete-and-extract-region (point) (line-end-position))))
  19289. (newline-and-indent)
  19290. (save-excursion (insert trailing-data))))
  19291. (t (if indent (newline-and-indent) (newline))))))
  19292. (defun org-return-indent ()
  19293. "Goto next table row or insert a newline and indent.
  19294. Calls `org-table-next-row' or `newline-and-indent', depending on
  19295. context. See the individual commands for more information."
  19296. (interactive)
  19297. (org-return t))
  19298. (defun org-ctrl-c-star ()
  19299. "Compute table, or change heading status of lines.
  19300. Calls `org-table-recalculate' or `org-toggle-heading',
  19301. depending on context."
  19302. (interactive)
  19303. (cond
  19304. ((org-at-table-p)
  19305. (call-interactively 'org-table-recalculate))
  19306. (t
  19307. ;; Convert all lines in region to list items
  19308. (call-interactively 'org-toggle-heading))))
  19309. (defun org-ctrl-c-minus ()
  19310. "Insert separator line in table or modify bullet status of line.
  19311. Also turns a plain line or a region of lines into list items.
  19312. Calls `org-table-insert-hline', `org-toggle-item', or
  19313. `org-cycle-list-bullet', depending on context."
  19314. (interactive)
  19315. (cond
  19316. ((org-at-table-p)
  19317. (call-interactively 'org-table-insert-hline))
  19318. ((org-region-active-p)
  19319. (call-interactively 'org-toggle-item))
  19320. ((org-in-item-p)
  19321. (call-interactively 'org-cycle-list-bullet))
  19322. (t
  19323. (call-interactively 'org-toggle-item))))
  19324. (defun org-toggle-heading (&optional nstars)
  19325. "Convert headings to normal text, or items or text to headings.
  19326. If there is no active region, only convert the current line.
  19327. With a `\\[universal-argument]' prefix, convert the whole list at
  19328. point into heading.
  19329. In a region:
  19330. - If the first non blank line is a headline, remove the stars
  19331. from all headlines in the region.
  19332. - If it is a normal line, turn each and every normal line (i.e.,
  19333. not an heading or an item) in the region into headings. If you
  19334. want to convert only the first line of this region, use one
  19335. universal prefix argument.
  19336. - If it is a plain list item, turn all plain list items into headings.
  19337. When converting a line into a heading, the number of stars is chosen
  19338. such that the lines become children of the current entry. However,
  19339. when a numeric prefix argument is given, its value determines the
  19340. number of stars to add."
  19341. (interactive "P")
  19342. (let ((skip-blanks
  19343. (function
  19344. ;; Return beginning of first non-blank line, starting from
  19345. ;; line at POS.
  19346. (lambda (pos)
  19347. (save-excursion
  19348. (goto-char pos)
  19349. (while (org-at-comment-p) (forward-line))
  19350. (skip-chars-forward " \r\t\n")
  19351. (point-at-bol)))))
  19352. beg end toggled)
  19353. ;; Determine boundaries of changes. If a universal prefix has
  19354. ;; been given, put the list in a region. If region ends at a bol,
  19355. ;; do not consider the last line to be in the region.
  19356. (when (and current-prefix-arg (org-at-item-p))
  19357. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19358. (org-mark-element))
  19359. (if (org-region-active-p)
  19360. (setq beg (funcall skip-blanks (region-beginning))
  19361. end (copy-marker (save-excursion
  19362. (goto-char (region-end))
  19363. (if (bolp) (point) (point-at-eol)))))
  19364. (setq beg (funcall skip-blanks (point-at-bol))
  19365. end (copy-marker (point-at-eol))))
  19366. ;; Ensure inline tasks don't count as headings.
  19367. (org-with-limited-levels
  19368. (save-excursion
  19369. (goto-char beg)
  19370. (cond
  19371. ;; Case 1. Started at an heading: de-star headings.
  19372. ((org-at-heading-p)
  19373. (while (< (point) end)
  19374. (when (org-at-heading-p t)
  19375. (looking-at org-outline-regexp) (replace-match "")
  19376. (setq toggled t))
  19377. (forward-line)))
  19378. ;; Case 2. Started at an item: change items into headlines.
  19379. ;; One star will be added by `org-list-to-subtree'.
  19380. ((org-at-item-p)
  19381. (while (< (point) end)
  19382. (when (org-at-item-p)
  19383. ;; Pay attention to cases when region ends before list.
  19384. (let* ((struct (org-list-struct))
  19385. (list-end
  19386. (min (org-list-get-bottom-point struct) (1+ end))))
  19387. (save-restriction
  19388. (narrow-to-region (point) list-end)
  19389. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19390. (setq toggled t))
  19391. (forward-line)))
  19392. ;; Case 3. Started at normal text: make every line an heading,
  19393. ;; skipping headlines and items.
  19394. (t (let* ((stars
  19395. (make-string
  19396. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19397. (add-stars
  19398. (cond (nstars "") ; stars from prefix only
  19399. ((equal stars "") "*") ; before first heading
  19400. (org-odd-levels-only "**") ; inside heading, odd
  19401. (t "*"))) ; inside heading, oddeven
  19402. (rpl (concat stars add-stars " "))
  19403. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19404. (while (< (point) (if (equal nstars '(4)) lend end))
  19405. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19406. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19407. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19408. (forward-line)))))))
  19409. (unless toggled (message "Cannot toggle heading from here"))))
  19410. (defun org-meta-return (&optional arg)
  19411. "Insert a new heading or wrap a region in a table.
  19412. Calls `org-insert-heading', `org-insert-item' or
  19413. `org-table-wrap-region', depending on context. When called with
  19414. an argument, unconditionally call `org-insert-heading'."
  19415. (interactive "P")
  19416. (org-check-before-invisible-edit 'insert)
  19417. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19418. (call-interactively (cond (arg #'org-insert-heading)
  19419. ((org-at-table-p) #'org-table-wrap-region)
  19420. ((org-in-item-p) #'org-insert-item)
  19421. (t #'org-insert-heading)))))
  19422. ;;; Menu entries
  19423. (defsubst org-in-subtree-not-table-p ()
  19424. "Are we in a subtree and not in a table?"
  19425. (and (not (org-before-first-heading-p))
  19426. (not (org-at-table-p))))
  19427. ;; Define the Org mode menus
  19428. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19429. '("Tbl"
  19430. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19431. ["Next Field" org-cycle (org-at-table-p)]
  19432. ["Previous Field" org-shifttab (org-at-table-p)]
  19433. ["Next Row" org-return (org-at-table-p)]
  19434. "--"
  19435. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19436. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19437. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19438. "--"
  19439. ("Column"
  19440. ["Move Column Left" org-metaleft (org-at-table-p)]
  19441. ["Move Column Right" org-metaright (org-at-table-p)]
  19442. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19443. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19444. ("Row"
  19445. ["Move Row Up" org-metaup (org-at-table-p)]
  19446. ["Move Row Down" org-metadown (org-at-table-p)]
  19447. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19448. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19449. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19450. "--"
  19451. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19452. ("Rectangle"
  19453. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19454. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19455. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19456. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19457. "--"
  19458. ("Calculate"
  19459. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19460. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19461. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19462. "--"
  19463. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19464. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19465. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19466. "--"
  19467. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19468. "--"
  19469. ["Sum Column/Rectangle" org-table-sum
  19470. (or (org-at-table-p) (org-region-active-p))]
  19471. ["Which Column?" org-table-current-column (org-at-table-p)])
  19472. ["Debug Formulas"
  19473. org-table-toggle-formula-debugger
  19474. :style toggle :selected (bound-and-true-p org-table-formula-debug)]
  19475. ["Show Col/Row Numbers"
  19476. org-table-toggle-coordinate-overlays
  19477. :style toggle
  19478. :selected (bound-and-true-p org-table-overlay-coordinates)]
  19479. "--"
  19480. ["Create" org-table-create (and (not (org-at-table-p))
  19481. org-enable-table-editor)]
  19482. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19483. ["Import from File" org-table-import (not (org-at-table-p))]
  19484. ["Export to File" org-table-export (org-at-table-p)]
  19485. "--"
  19486. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19487. "--"
  19488. ("Plot"
  19489. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19490. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19491. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19492. '("Org"
  19493. ("Show/Hide"
  19494. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19495. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19496. ["Sparse Tree..." org-sparse-tree t]
  19497. ["Reveal Context" org-reveal t]
  19498. ["Show All" outline-show-all t]
  19499. "--"
  19500. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19501. "--"
  19502. ["New Heading" org-insert-heading t]
  19503. ("Navigate Headings"
  19504. ["Up" outline-up-heading t]
  19505. ["Next" outline-next-visible-heading t]
  19506. ["Previous" outline-previous-visible-heading t]
  19507. ["Next Same Level" outline-forward-same-level t]
  19508. ["Previous Same Level" outline-backward-same-level t]
  19509. "--"
  19510. ["Jump" org-goto t])
  19511. ("Edit Structure"
  19512. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19513. "--"
  19514. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19515. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19516. "--"
  19517. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19518. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19519. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19520. "--"
  19521. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19522. "--"
  19523. ["Copy visible text" org-copy-visible t]
  19524. "--"
  19525. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19526. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19527. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19528. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19529. "--"
  19530. ["Sort Region/Children" org-sort t]
  19531. "--"
  19532. ["Convert to odd levels" org-convert-to-odd-levels t]
  19533. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19534. ("Editing"
  19535. ["Emphasis..." org-emphasize t]
  19536. ["Edit Source Example" org-edit-special t]
  19537. "--"
  19538. ["Footnote new/jump" org-footnote-action t]
  19539. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19540. ("Archive"
  19541. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19542. "--"
  19543. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19544. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19545. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19546. )
  19547. "--"
  19548. ("Hyperlinks"
  19549. ["Store Link (Global)" org-store-link t]
  19550. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19551. ["Insert Link" org-insert-link t]
  19552. ["Follow Link" org-open-at-point t]
  19553. "--"
  19554. ["Next link" org-next-link t]
  19555. ["Previous link" org-previous-link t]
  19556. "--"
  19557. ["Descriptive Links"
  19558. org-toggle-link-display
  19559. :style radio
  19560. :selected org-descriptive-links
  19561. ]
  19562. ["Literal Links"
  19563. org-toggle-link-display
  19564. :style radio
  19565. :selected (not org-descriptive-links)])
  19566. "--"
  19567. ("TODO Lists"
  19568. ["TODO/DONE/-" org-todo t]
  19569. ("Select keyword"
  19570. ["Next keyword" org-shiftright (org-at-heading-p)]
  19571. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19572. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19573. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19574. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19575. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19576. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19577. "--"
  19578. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19579. :selected org-enforce-todo-dependencies :style toggle :active t]
  19580. "Settings for tree at point"
  19581. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19582. :selected (org-entry-get nil "ORDERED")
  19583. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19584. ["Do Children parallel" org-toggle-ordered-property :style radio
  19585. :selected (not (org-entry-get nil "ORDERED"))
  19586. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19587. "--"
  19588. ["Set Priority" org-priority t]
  19589. ["Priority Up" org-shiftup t]
  19590. ["Priority Down" org-shiftdown t]
  19591. "--"
  19592. ["Get news from all feeds" org-feed-update-all t]
  19593. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19594. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19595. ("TAGS and Properties"
  19596. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19597. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19598. "--"
  19599. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19600. ["Column view of properties" org-columns t]
  19601. ["Insert Column View DBlock" org-columns-insert-dblock t])
  19602. ("Dates and Scheduling"
  19603. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19604. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19605. ("Change Date"
  19606. ["1 Day Later" org-shiftright (org-at-timestamp-p 'lax)]
  19607. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p 'lax)]
  19608. ["1 ... Later" org-shiftup (org-at-timestamp-p 'lax)]
  19609. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p 'lax)])
  19610. ["Compute Time Range" org-evaluate-time-range t]
  19611. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19612. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19613. "--"
  19614. ["Custom time format" org-toggle-time-stamp-overlays
  19615. :style radio :selected org-display-custom-times]
  19616. "--"
  19617. ["Goto Calendar" org-goto-calendar t]
  19618. ["Date from Calendar" org-date-from-calendar t]
  19619. "--"
  19620. ["Start/Restart Timer" org-timer-start t]
  19621. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19622. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19623. ["Insert Timer String" org-timer t]
  19624. ["Insert Timer Item" org-timer-item t])
  19625. ("Logging work"
  19626. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19627. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19628. ["Clock out" org-clock-out t]
  19629. ["Clock cancel" org-clock-cancel t]
  19630. "--"
  19631. ["Mark as default task" org-clock-mark-default-task t]
  19632. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19633. ["Goto running clock" org-clock-goto t]
  19634. "--"
  19635. ["Display times" org-clock-display t]
  19636. ["Create clock table" org-clock-report t]
  19637. "--"
  19638. ["Record DONE time"
  19639. (progn (setq org-log-done (not org-log-done))
  19640. (message "Switching to %s will %s record a timestamp"
  19641. (car org-done-keywords)
  19642. (if org-log-done "automatically" "not")))
  19643. :style toggle :selected org-log-done])
  19644. "--"
  19645. ["Agenda Command..." org-agenda t]
  19646. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19647. ("File List for Agenda")
  19648. ("Special views current file"
  19649. ["TODO Tree" org-show-todo-tree t]
  19650. ["Check Deadlines" org-check-deadlines t]
  19651. ["Tags/Property tree" org-match-sparse-tree t])
  19652. "--"
  19653. ["Export/Publish..." org-export-dispatch t]
  19654. ("LaTeX"
  19655. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19656. :selected org-cdlatex-mode]
  19657. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19658. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19659. ["Modify math symbol" org-cdlatex-math-modify
  19660. (org-inside-LaTeX-fragment-p)]
  19661. ["Insert citation" org-reftex-citation t])
  19662. "--"
  19663. ("MobileOrg"
  19664. ["Push Files and Views" org-mobile-push t]
  19665. ["Get Captured and Flagged" org-mobile-pull t]
  19666. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19667. "--"
  19668. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19669. "--"
  19670. ("Documentation"
  19671. ["Show Version" org-version t]
  19672. ["Info Documentation" org-info t])
  19673. ("Customize"
  19674. ["Browse Org Group" org-customize t]
  19675. "--"
  19676. ["Expand This Menu" org-create-customize-menu
  19677. (fboundp 'customize-menu-create)])
  19678. ["Send bug report" org-submit-bug-report t]
  19679. "--"
  19680. ("Refresh/Reload"
  19681. ["Refresh setup current buffer" org-mode-restart t]
  19682. ["Reload Org (after update)" org-reload t]
  19683. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19684. ))
  19685. (defun org-info (&optional node)
  19686. "Read documentation for Org in the info system.
  19687. With optional NODE, go directly to that node."
  19688. (interactive)
  19689. (info (format "(org)%s" (or node ""))))
  19690. ;;;###autoload
  19691. (defun org-submit-bug-report ()
  19692. "Submit a bug report on Org via mail.
  19693. Don't hesitate to report any problems or inaccurate documentation.
  19694. If you don't have setup sending mail from (X)Emacs, please copy the
  19695. output buffer into your mail program, as it gives us important
  19696. information about your Org version and configuration."
  19697. (interactive)
  19698. (require 'reporter)
  19699. (defvar reporter-prompt-for-summary-p)
  19700. (org-load-modules-maybe)
  19701. (org-require-autoloaded-modules)
  19702. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19703. (reporter-submit-bug-report
  19704. "emacs-orgmode@gnu.org"
  19705. (org-version nil 'full)
  19706. (let (list)
  19707. (save-window-excursion
  19708. (pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19709. (delete-other-windows)
  19710. (erase-buffer)
  19711. (insert "You are about to submit a bug report to the Org mailing list.
  19712. We would like to add your full Org and Outline configuration to the
  19713. bug report. This greatly simplifies the work of the maintainer and
  19714. other experts on the mailing list.
  19715. HOWEVER, some variables you have customized may contain private
  19716. information. The names of customers, colleagues, or friends, might
  19717. appear in the form of file names, tags, todo states, or search strings.
  19718. If you answer yes to the prompt, you might want to check and remove
  19719. such private information before sending the email.")
  19720. (add-text-properties (point-min) (point-max) '(face org-warning))
  19721. (when (yes-or-no-p "Include your Org configuration ")
  19722. (mapatoms
  19723. (lambda (v)
  19724. (and (boundp v)
  19725. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19726. (or (and (symbol-value v)
  19727. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19728. (and
  19729. (get v 'custom-type) (get v 'standard-value)
  19730. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19731. (push v list)))))
  19732. (kill-buffer (get-buffer "*Warn about privacy*"))
  19733. list))
  19734. nil nil
  19735. "Remember to cover the basics, that is, what you expected to happen and
  19736. what in fact did happen. You don't know how to make a good report? See
  19737. http://orgmode.org/manual/Feedback.html#Feedback
  19738. Your bug report will be posted to the Org mailing list.
  19739. ------------------------------------------------------------------------")
  19740. (save-excursion
  19741. (when (re-search-backward "^\\(Subject: \\)Org mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19742. (replace-match "\\1Bug: \\3 [\\2]")))))
  19743. (defun org-install-agenda-files-menu ()
  19744. (let ((bl (buffer-list)))
  19745. (save-excursion
  19746. (while bl
  19747. (set-buffer (pop bl))
  19748. (when (derived-mode-p 'org-mode) (setq bl nil)))
  19749. (when (derived-mode-p 'org-mode)
  19750. (easy-menu-change
  19751. '("Org") "File List for Agenda"
  19752. (append
  19753. (list
  19754. ["Edit File List" (org-edit-agenda-file-list) t]
  19755. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19756. ["Remove Current File from List" org-remove-file t]
  19757. ["Cycle through agenda files" org-cycle-agenda-files t]
  19758. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19759. "--")
  19760. (mapcar 'org-file-menu-entry (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 (org-split-string string "[ \t\n]+"))
  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. (defvar org-element-paragraph-separate) ; org-element.el
  20849. (defun org-setup-filling ()
  20850. (require 'org-element)
  20851. ;; Prevent auto-fill from inserting unwanted new items.
  20852. (when (boundp 'fill-nobreak-predicate)
  20853. (setq-local
  20854. fill-nobreak-predicate
  20855. (org-uniquify
  20856. (append fill-nobreak-predicate
  20857. '(org-fill-line-break-nobreak-p
  20858. org-fill-n-macro-as-item-nobreak-p
  20859. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20860. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20861. (setq-local paragraph-start paragraph-ending)
  20862. (setq-local paragraph-separate paragraph-ending))
  20863. (setq-local fill-paragraph-function 'org-fill-paragraph)
  20864. (setq-local auto-fill-inhibit-regexp nil)
  20865. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  20866. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  20867. (setq-local comment-line-break-function 'org-comment-line-break-function))
  20868. (defun org-fill-line-break-nobreak-p ()
  20869. "Non-nil when a new line at point would create an Org line break."
  20870. (save-excursion
  20871. (skip-chars-backward "[ \t]")
  20872. (skip-chars-backward "\\\\")
  20873. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20874. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20875. "Non-nil when a new line at point would split a timestamp."
  20876. (and (org-at-timestamp-p 'lax)
  20877. (not (looking-at org-ts-regexp-both))))
  20878. (defun org-fill-n-macro-as-item-nobreak-p ()
  20879. "Non-nil when a new line at point would create a new list."
  20880. ;; During export, a "n" macro followed by a dot or a closing
  20881. ;; parenthesis can end up being parsed as a new list item.
  20882. (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)"))
  20883. (declare-function message-in-body-p "message" ())
  20884. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20885. (defun org-adaptive-fill-function ()
  20886. "Compute a fill prefix for the current line.
  20887. Return fill prefix, as a string, or nil if current line isn't
  20888. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20889. matches in paragraphs or comments, use it."
  20890. (catch 'exit
  20891. (when (derived-mode-p 'message-mode)
  20892. (save-excursion
  20893. (beginning-of-line)
  20894. (cond ((not (message-in-body-p)) (throw 'exit nil))
  20895. ((looking-at-p org-table-line-regexp) (throw 'exit nil))
  20896. ((looking-at message-cite-prefix-regexp)
  20897. (throw 'exit (match-string-no-properties 0)))
  20898. ((looking-at org-outline-regexp)
  20899. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20900. (org-with-wide-buffer
  20901. (unless (org-at-heading-p)
  20902. (let* ((p (line-beginning-position))
  20903. (element (save-excursion
  20904. (beginning-of-line)
  20905. (org-element-at-point)))
  20906. (type (org-element-type element))
  20907. (post-affiliated (org-element-property :post-affiliated element)))
  20908. (unless (< p post-affiliated)
  20909. (cl-case type
  20910. (comment
  20911. (save-excursion
  20912. (beginning-of-line)
  20913. (looking-at "[ \t]*")
  20914. (concat (match-string 0) "# ")))
  20915. (footnote-definition "")
  20916. ((item plain-list)
  20917. (make-string (org-list-item-body-column post-affiliated) ?\s))
  20918. (paragraph
  20919. ;; Fill prefix is usually the same as the current line,
  20920. ;; unless the paragraph is at the beginning of an item.
  20921. (let ((parent (org-element-property :parent element)))
  20922. (save-excursion
  20923. (beginning-of-line)
  20924. (cond ((eq (org-element-type parent) 'item)
  20925. (make-string (org-list-item-body-column
  20926. (org-element-property :begin parent))
  20927. ?\s))
  20928. ((and adaptive-fill-regexp
  20929. ;; Locally disable
  20930. ;; `adaptive-fill-function' to let
  20931. ;; `fill-context-prefix' handle
  20932. ;; `adaptive-fill-regexp' variable.
  20933. (let (adaptive-fill-function)
  20934. (fill-context-prefix
  20935. post-affiliated
  20936. (org-element-property :end element)))))
  20937. ((looking-at "[ \t]+") (match-string 0))
  20938. (t "")))))
  20939. (comment-block
  20940. ;; Only fill contents if P is within block boundaries.
  20941. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20942. (forward-line)
  20943. (point)))
  20944. (cend (save-excursion
  20945. (goto-char (org-element-property :end element))
  20946. (skip-chars-backward " \r\t\n")
  20947. (line-beginning-position))))
  20948. (when (and (>= p cbeg) (< p cend))
  20949. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20950. (match-string 0)
  20951. "")))))))))))
  20952. (declare-function message-goto-body "message" ())
  20953. (defvar message-cite-prefix-regexp) ; From message.el
  20954. (defun org-fill-element (&optional justify)
  20955. "Fill element at point, when applicable.
  20956. This function only applies to comment blocks, comments, example
  20957. blocks and paragraphs. Also, as a special case, re-align table
  20958. when point is at one.
  20959. If JUSTIFY is non-nil (interactively, with prefix argument),
  20960. justify as well. If `sentence-end-double-space' is non-nil, then
  20961. period followed by one space does not end a sentence, so don't
  20962. break a line there. The variable `fill-column' controls the
  20963. width for filling.
  20964. For convenience, when point is at a plain list, an item or
  20965. a footnote definition, try to fill the first paragraph within."
  20966. (with-syntax-table org-mode-transpose-word-syntax-table
  20967. ;; Move to end of line in order to get the first paragraph within
  20968. ;; a plain list or a footnote definition.
  20969. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20970. ;; First check if point is in a blank line at the beginning of
  20971. ;; the buffer. In that case, ignore filling.
  20972. (cl-case (org-element-type element)
  20973. ;; Use major mode filling function is src blocks.
  20974. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20975. ;; Align Org tables, leave table.el tables as-is.
  20976. (table-row (org-table-align) t)
  20977. (table
  20978. (when (eq (org-element-property :type element) 'org)
  20979. (save-excursion
  20980. (goto-char (org-element-property :post-affiliated element))
  20981. (org-table-align)))
  20982. t)
  20983. (paragraph
  20984. ;; Paragraphs may contain `line-break' type objects.
  20985. (let ((beg (max (point-min)
  20986. (org-element-property :contents-begin element)))
  20987. (end (min (point-max)
  20988. (org-element-property :contents-end element))))
  20989. ;; Do nothing if point is at an affiliated keyword.
  20990. (if (< (line-end-position) beg) t
  20991. (when (derived-mode-p 'message-mode)
  20992. ;; In `message-mode', do not fill following citation
  20993. ;; in current paragraph nor text before message body.
  20994. (let ((body-start (save-excursion (message-goto-body))))
  20995. (when body-start (setq beg (max body-start beg))))
  20996. (when (save-excursion
  20997. (re-search-forward
  20998. (concat "^" message-cite-prefix-regexp) end t))
  20999. (setq end (match-beginning 0))))
  21000. ;; Fill paragraph, taking line breaks into account.
  21001. (save-excursion
  21002. (goto-char beg)
  21003. (let ((cuts (list beg)))
  21004. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21005. (when (eq 'line-break
  21006. (org-element-type
  21007. (save-excursion (backward-char)
  21008. (org-element-context))))
  21009. (push (point) cuts)))
  21010. (dolist (c (delq end cuts))
  21011. (fill-region-as-paragraph c end justify)
  21012. (setq end c))))
  21013. t)))
  21014. ;; Contents of `comment-block' type elements should be
  21015. ;; filled as plain text, but only if point is within block
  21016. ;; markers.
  21017. (comment-block
  21018. (let* ((case-fold-search t)
  21019. (beg (save-excursion
  21020. (goto-char (org-element-property :begin element))
  21021. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21022. (forward-line)
  21023. (point)))
  21024. (end (save-excursion
  21025. (goto-char (org-element-property :end element))
  21026. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21027. (line-beginning-position))))
  21028. (if (or (< (point) beg) (> (point) end)) t
  21029. (fill-region-as-paragraph
  21030. (save-excursion (end-of-line)
  21031. (re-search-backward "^[ \t]*$" beg 'move)
  21032. (line-beginning-position))
  21033. (save-excursion (beginning-of-line)
  21034. (re-search-forward "^[ \t]*$" end 'move)
  21035. (line-beginning-position))
  21036. justify))))
  21037. ;; Fill comments.
  21038. (comment
  21039. (let ((begin (org-element-property :post-affiliated element))
  21040. (end (org-element-property :end element)))
  21041. (when (and (>= (point) begin) (<= (point) end))
  21042. (let ((begin (save-excursion
  21043. (end-of-line)
  21044. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21045. (progn (forward-line) (point))
  21046. begin)))
  21047. (end (save-excursion
  21048. (end-of-line)
  21049. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21050. (1- (line-beginning-position))
  21051. (skip-chars-backward " \r\t\n")
  21052. (line-end-position)))))
  21053. ;; Do not fill comments when at a blank line.
  21054. (when (> end begin)
  21055. (let ((fill-prefix
  21056. (save-excursion
  21057. (beginning-of-line)
  21058. (looking-at "[ \t]*#")
  21059. (let ((comment-prefix (match-string 0)))
  21060. (goto-char (match-end 0))
  21061. (if (looking-at adaptive-fill-regexp)
  21062. (concat comment-prefix (match-string 0))
  21063. (concat comment-prefix " "))))))
  21064. (save-excursion
  21065. (fill-region-as-paragraph begin end justify))))))
  21066. t))
  21067. ;; Ignore every other element.
  21068. (otherwise t)))))
  21069. (defun org-fill-paragraph (&optional justify region)
  21070. "Fill element at point, when applicable.
  21071. This function only applies to comment blocks, comments, example
  21072. blocks and paragraphs. Also, as a special case, re-align table
  21073. when point is at one.
  21074. For convenience, when point is at a plain list, an item or
  21075. a footnote definition, try to fill the first paragraph within.
  21076. If JUSTIFY is non-nil (interactively, with prefix argument),
  21077. justify as well. If `sentence-end-double-space' is non-nil, then
  21078. period followed by one space does not end a sentence, so don't
  21079. break a line there. The variable `fill-column' controls the
  21080. width for filling.
  21081. The REGION argument is non-nil if called interactively; in that
  21082. case, if Transient Mark mode is enabled and the mark is active,
  21083. fill each of the elements in the active region, instead of just
  21084. filling the current element."
  21085. (interactive (progn
  21086. (barf-if-buffer-read-only)
  21087. (list (if current-prefix-arg 'full) t)))
  21088. (cond
  21089. ((and (derived-mode-p 'message-mode)
  21090. (or (not (message-in-body-p))
  21091. (save-excursion (move-beginning-of-line 1)
  21092. (looking-at message-cite-prefix-regexp))))
  21093. ;; First ensure filling is correct in message-mode.
  21094. (let ((fill-paragraph-function
  21095. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21096. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21097. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21098. (paragraph-separate
  21099. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21100. (fill-paragraph nil)))
  21101. ((and region transient-mark-mode mark-active
  21102. (not (eq (region-beginning) (region-end))))
  21103. (let ((origin (point-marker))
  21104. (start (region-beginning)))
  21105. (unwind-protect
  21106. (progn
  21107. (goto-char (region-end))
  21108. (while (> (point) start)
  21109. (org-backward-paragraph)
  21110. (org-fill-element justify)))
  21111. (goto-char origin)
  21112. (set-marker origin nil))))
  21113. (t (org-fill-element justify))))
  21114. (org-remap org-mode-map 'fill-paragraph 'org-fill-paragraph)
  21115. (defun org-auto-fill-function ()
  21116. "Auto-fill function."
  21117. ;; Check if auto-filling is meaningful.
  21118. (let ((fc (current-fill-column)))
  21119. (when (and fc (> (current-column) fc))
  21120. (let* ((fill-prefix (org-adaptive-fill-function))
  21121. ;; Enforce empty fill prefix, if required. Otherwise, it
  21122. ;; will be computed again.
  21123. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21124. (when fill-prefix (do-auto-fill))))))
  21125. (defun org-comment-line-break-function (&optional soft)
  21126. "Break line at point and indent, continuing comment if within one.
  21127. The inserted newline is marked hard if variable
  21128. `use-hard-newlines' is true, unless optional argument SOFT is
  21129. non-nil."
  21130. (if soft (insert-and-inherit ?\n) (newline 1))
  21131. (save-excursion (forward-char -1) (delete-horizontal-space))
  21132. (delete-horizontal-space)
  21133. (indent-to-left-margin)
  21134. (insert-before-markers-and-inherit fill-prefix))
  21135. ;;; Fixed Width Areas
  21136. (defun org-toggle-fixed-width ()
  21137. "Toggle fixed-width markup.
  21138. Add or remove fixed-width markup on current line, whenever it
  21139. makes sense. Return an error otherwise.
  21140. If a region is active and if it contains only fixed-width areas
  21141. or blank lines, remove all fixed-width markup in it. If the
  21142. region contains anything else, convert all non-fixed-width lines
  21143. to fixed-width ones.
  21144. Blank lines at the end of the region are ignored unless the
  21145. region only contains such lines."
  21146. (interactive)
  21147. (if (not (org-region-active-p))
  21148. ;; No region:
  21149. ;;
  21150. ;; Remove fixed width marker only in a fixed-with element.
  21151. ;;
  21152. ;; Add fixed width maker in paragraphs, in blank lines after
  21153. ;; elements or at the beginning of a headline or an inlinetask,
  21154. ;; and before any one-line elements (e.g., a clock).
  21155. (progn
  21156. (beginning-of-line)
  21157. (let* ((element (org-element-at-point))
  21158. (type (org-element-type element)))
  21159. (cond
  21160. ((and (eq type 'fixed-width)
  21161. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21162. (replace-match
  21163. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21164. ((and (memq type '(babel-call clock comment diary-sexp headline
  21165. horizontal-rule keyword paragraph
  21166. planning))
  21167. (<= (org-element-property :post-affiliated element) (point)))
  21168. (skip-chars-forward " \t")
  21169. (insert ": "))
  21170. ((and (looking-at-p "[ \t]*$")
  21171. (or (eq type 'inlinetask)
  21172. (save-excursion
  21173. (skip-chars-forward " \r\t\n")
  21174. (<= (org-element-property :end element) (point)))))
  21175. (delete-region (point) (line-end-position))
  21176. (org-indent-line)
  21177. (insert ": "))
  21178. (t (user-error "Cannot insert a fixed-width line here")))))
  21179. ;; Region active.
  21180. (let* ((begin (save-excursion
  21181. (goto-char (region-beginning))
  21182. (line-beginning-position)))
  21183. (end (copy-marker
  21184. (save-excursion
  21185. (goto-char (region-end))
  21186. (unless (eolp) (beginning-of-line))
  21187. (if (save-excursion (re-search-backward "\\S-" begin t))
  21188. (progn (skip-chars-backward " \r\t\n") (point))
  21189. (point)))))
  21190. (all-fixed-width-p
  21191. (catch 'not-all-p
  21192. (save-excursion
  21193. (goto-char begin)
  21194. (skip-chars-forward " \r\t\n")
  21195. (when (eobp) (throw 'not-all-p nil))
  21196. (while (< (point) end)
  21197. (let ((element (org-element-at-point)))
  21198. (if (eq (org-element-type element) 'fixed-width)
  21199. (goto-char (org-element-property :end element))
  21200. (throw 'not-all-p nil))))
  21201. t))))
  21202. (if all-fixed-width-p
  21203. (save-excursion
  21204. (goto-char begin)
  21205. (while (< (point) end)
  21206. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21207. (replace-match
  21208. "" nil nil nil
  21209. (if (= (line-end-position) (match-end 0)) 0 1)))
  21210. (forward-line)))
  21211. (let ((min-ind (point-max)))
  21212. ;; Find minimum indentation across all lines.
  21213. (save-excursion
  21214. (goto-char begin)
  21215. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21216. (setq min-ind 0)
  21217. (catch 'zerop
  21218. (while (< (point) end)
  21219. (unless (looking-at-p "[ \t]*$")
  21220. (let ((ind (org-get-indentation)))
  21221. (setq min-ind (min min-ind ind))
  21222. (when (zerop ind) (throw 'zerop t))))
  21223. (forward-line)))))
  21224. ;; Loop over all lines and add fixed-width markup everywhere
  21225. ;; but in fixed-width lines.
  21226. (save-excursion
  21227. (goto-char begin)
  21228. (while (< (point) end)
  21229. (cond
  21230. ((org-at-heading-p)
  21231. (insert ": ")
  21232. (forward-line)
  21233. (while (and (< (point) end) (looking-at-p "[ \t]*$"))
  21234. (insert ":")
  21235. (forward-line)))
  21236. ((looking-at-p "[ \t]*:\\( \\|$\\)")
  21237. (let* ((element (org-element-at-point))
  21238. (element-end (org-element-property :end element)))
  21239. (if (eq (org-element-type element) 'fixed-width)
  21240. (progn (goto-char element-end)
  21241. (skip-chars-backward " \r\t\n")
  21242. (forward-line))
  21243. (let ((limit (min end element-end)))
  21244. (while (< (point) limit)
  21245. (org-move-to-column min-ind t)
  21246. (insert ": ")
  21247. (forward-line))))))
  21248. (t
  21249. (org-move-to-column min-ind t)
  21250. (insert ": ")
  21251. (forward-line)))))))
  21252. (set-marker end nil))))
  21253. ;;; Comments
  21254. ;; Org comments syntax is quite complex. It requires the entire line
  21255. ;; to be just a comment. Also, even with the right syntax at the
  21256. ;; beginning of line, some elements (e.g., verse-block or
  21257. ;; example-block) don't accept comments. Usual Emacs comment commands
  21258. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21259. ;; Org still relies on `comment-dwim', but cannot trust
  21260. ;; `comment-only-p'. So, `comment-region-function' and
  21261. ;; `uncomment-region-function' both point
  21262. ;; to`org-comment-or-uncomment-region'. Eventually,
  21263. ;; `org-insert-comment' takes care of insertion of comments at the
  21264. ;; beginning of line.
  21265. ;; `org-setup-comments-handling' install comments related variables
  21266. ;; during `org-mode' initialization.
  21267. (defun org-setup-comments-handling ()
  21268. (interactive)
  21269. (setq-local comment-use-syntax nil)
  21270. (setq-local comment-start "# ")
  21271. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21272. (setq-local comment-insert-comment-function 'org-insert-comment)
  21273. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21274. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21275. (defun org-insert-comment ()
  21276. "Insert an empty comment above current line.
  21277. If the line is empty, insert comment at its beginning. When
  21278. point is within a source block, comment according to the related
  21279. major mode."
  21280. (if (let ((element (org-element-at-point)))
  21281. (and (eq (org-element-type element) 'src-block)
  21282. (< (save-excursion
  21283. (goto-char (org-element-property :post-affiliated element))
  21284. (line-end-position))
  21285. (point))
  21286. (> (save-excursion
  21287. (goto-char (org-element-property :end element))
  21288. (skip-chars-backward " \r\t\n")
  21289. (line-beginning-position))
  21290. (point))))
  21291. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21292. (beginning-of-line)
  21293. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21294. (open-line 1))
  21295. (org-indent-line)
  21296. (insert "# ")))
  21297. (defvar comment-empty-lines) ; From newcomment.el.
  21298. (defun org-comment-or-uncomment-region (beg end &rest _)
  21299. "Comment or uncomment each non-blank line in the region.
  21300. Uncomment each non-blank line between BEG and END if it only
  21301. contains commented lines. Otherwise, comment them. If region is
  21302. strictly within a source block, use appropriate comment syntax."
  21303. (if (let ((element (org-element-at-point)))
  21304. (and (eq (org-element-type element) 'src-block)
  21305. (< (save-excursion
  21306. (goto-char (org-element-property :post-affiliated element))
  21307. (line-end-position))
  21308. beg)
  21309. (>= (save-excursion
  21310. (goto-char (org-element-property :end element))
  21311. (skip-chars-backward " \r\t\n")
  21312. (line-beginning-position))
  21313. end)))
  21314. ;; Translate region boundaries for the Org buffer to the source
  21315. ;; buffer.
  21316. (let ((offset (- end beg)))
  21317. (save-excursion
  21318. (goto-char beg)
  21319. (org-babel-do-in-edit-buffer
  21320. (comment-or-uncomment-region (point) (+ offset (point))))))
  21321. (save-restriction
  21322. ;; Restrict region
  21323. (narrow-to-region (save-excursion (goto-char beg)
  21324. (skip-chars-forward " \r\t\n" end)
  21325. (line-beginning-position))
  21326. (save-excursion (goto-char end)
  21327. (skip-chars-backward " \r\t\n" beg)
  21328. (line-end-position)))
  21329. (let ((uncommentp
  21330. ;; UNCOMMENTP is non-nil when every non blank line between
  21331. ;; BEG and END is a comment.
  21332. (save-excursion
  21333. (goto-char (point-min))
  21334. (while (and (not (eobp))
  21335. (let ((element (org-element-at-point)))
  21336. (and (eq (org-element-type element) 'comment)
  21337. (goto-char (min (point-max)
  21338. (org-element-property
  21339. :end element)))))))
  21340. (eobp))))
  21341. (if uncommentp
  21342. ;; Only blank lines and comments in region: uncomment it.
  21343. (save-excursion
  21344. (goto-char (point-min))
  21345. (while (not (eobp))
  21346. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21347. (replace-match "" nil nil nil 1))
  21348. (forward-line)))
  21349. ;; Comment each line in region.
  21350. (let ((min-indent (point-max)))
  21351. ;; First find the minimum indentation across all lines.
  21352. (save-excursion
  21353. (goto-char (point-min))
  21354. (while (and (not (eobp)) (not (zerop min-indent)))
  21355. (unless (looking-at "[ \t]*$")
  21356. (setq min-indent (min min-indent (current-indentation))))
  21357. (forward-line)))
  21358. ;; Then loop over all lines.
  21359. (save-excursion
  21360. (goto-char (point-min))
  21361. (while (not (eobp))
  21362. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21363. ;; Don't get fooled by invisible text (e.g. link path)
  21364. ;; when moving to column MIN-INDENT.
  21365. (let ((buffer-invisibility-spec nil))
  21366. (org-move-to-column min-indent t))
  21367. (insert comment-start))
  21368. (forward-line)))))))))
  21369. (defun org-comment-dwim (_arg)
  21370. "Call `comment-dwim' within a source edit buffer if needed."
  21371. (interactive "*P")
  21372. (if (org-in-src-block-p)
  21373. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21374. (call-interactively 'comment-dwim)))
  21375. ;;; Timestamps API
  21376. ;; This section contains tools to operate on timestamp objects, as
  21377. ;; returned by, e.g. `org-element-context'.
  21378. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21379. "Encode TIMESTAMP object into Emacs internal time.
  21380. Use end of date range or time range when END is non-nil."
  21381. (apply #'encode-time
  21382. (cons 0
  21383. (mapcar
  21384. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21385. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21386. '(:minute-start :hour-start :day-start :month-start
  21387. :year-start))))))
  21388. (defun org-timestamp-has-time-p (timestamp)
  21389. "Non-nil when TIMESTAMP has a time specified."
  21390. (org-element-property :hour-start timestamp))
  21391. (defun org-timestamp-format (timestamp format &optional end utc)
  21392. "Format a TIMESTAMP object into a string.
  21393. FORMAT is a format specifier to be passed to
  21394. `format-time-string'.
  21395. When optional argument END is non-nil, use end of date-range or
  21396. time-range, if possible.
  21397. When optional argument UTC is non-nil, time will be expressed as
  21398. Universal Time."
  21399. (format-time-string
  21400. format (org-timestamp--to-internal-time timestamp end)
  21401. (and utc t)))
  21402. (defun org-timestamp-split-range (timestamp &optional end)
  21403. "Extract a TIMESTAMP object from a date or time range.
  21404. END, when non-nil, means extract the end of the range.
  21405. Otherwise, extract its start.
  21406. Return a new timestamp object."
  21407. (let ((type (org-element-property :type timestamp)))
  21408. (if (memq type '(active inactive diary)) timestamp
  21409. (let ((split-ts (org-element-copy timestamp)))
  21410. ;; Set new type.
  21411. (org-element-put-property
  21412. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21413. ;; Copy start properties over end properties if END is
  21414. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21415. (let ((p-alist '((:minute-start . :minute-end)
  21416. (:hour-start . :hour-end)
  21417. (:day-start . :day-end)
  21418. (:month-start . :month-end)
  21419. (:year-start . :year-end))))
  21420. (dolist (p-cell p-alist)
  21421. (org-element-put-property
  21422. split-ts
  21423. (funcall (if end #'car #'cdr) p-cell)
  21424. (org-element-property
  21425. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21426. ;; Eventually refresh `:raw-value'.
  21427. (org-element-put-property split-ts :raw-value nil)
  21428. (org-element-put-property
  21429. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21430. (defun org-timestamp-translate (timestamp &optional boundary)
  21431. "Translate TIMESTAMP object to custom format.
  21432. Format string is defined in `org-time-stamp-custom-formats',
  21433. which see.
  21434. When optional argument BOUNDARY is non-nil, it is either the
  21435. symbol `start' or `end'. In this case, only translate the
  21436. starting or ending part of TIMESTAMP if it is a date or time
  21437. range. Otherwise, translate both parts.
  21438. Return timestamp as-is if `org-display-custom-times' is nil or if
  21439. it has a `diary' type."
  21440. (let ((type (org-element-property :type timestamp)))
  21441. (if (or (not org-display-custom-times) (eq type 'diary))
  21442. (org-element-interpret-data timestamp)
  21443. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21444. org-time-stamp-custom-formats)))
  21445. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21446. (concat (org-timestamp-format timestamp fmt)
  21447. "--"
  21448. (org-timestamp-format timestamp fmt t))
  21449. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21450. ;;; Other stuff.
  21451. (defvar reftex-docstruct-symbol)
  21452. (defvar org--rds)
  21453. (defun org-reftex-citation ()
  21454. "Use reftex-citation to insert a citation into the buffer.
  21455. This looks for a line like
  21456. #+BIBLIOGRAPHY: foo plain option:-d
  21457. and derives from it that foo.bib is the bibliography file relevant
  21458. for this document. It then installs the necessary environment for RefTeX
  21459. to work in this buffer and calls `reftex-citation' to insert a citation
  21460. into the buffer.
  21461. Export of such citations to both LaTeX and HTML is handled by the contributed
  21462. package ox-bibtex by Taru Karttunen."
  21463. (interactive)
  21464. (let ((reftex-docstruct-symbol 'org--rds)
  21465. org--rds bib)
  21466. (org-with-wide-buffer
  21467. (let ((case-fold-search t)
  21468. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21469. (if (not (save-excursion
  21470. (or (re-search-forward re nil t)
  21471. (re-search-backward re nil t))))
  21472. (user-error "No bibliography defined in file")
  21473. (setq bib (concat (match-string 1) ".bib")
  21474. org--rds (list (list 'bib bib))))))
  21475. (call-interactively 'reftex-citation)))
  21476. ;;;; Functions extending outline functionality
  21477. (defun org-beginning-of-line (&optional n)
  21478. "Go to the beginning of the current visible line.
  21479. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21480. tags on the first attempt, and only move to after the tags when
  21481. the cursor is already beyond the end of the headline.
  21482. With argument N not nil or 1, move forward N - 1 lines first."
  21483. (interactive "^p")
  21484. (let ((origin (point))
  21485. (special (pcase org-special-ctrl-a/e
  21486. (`(,C-a . ,_) C-a) (_ org-special-ctrl-a/e)))
  21487. deactivate-mark)
  21488. ;; First move to a visible line.
  21489. (if (bound-and-true-p visual-line-mode)
  21490. (beginning-of-visual-line n)
  21491. (move-beginning-of-line n)
  21492. ;; `move-beginning-of-line' may leave point after invisible
  21493. ;; characters if line starts with such of these (e.g., with
  21494. ;; a link at column 0). Really move to the beginning of the
  21495. ;; current visible line.
  21496. (beginning-of-line))
  21497. (cond
  21498. ;; No special behavior. Point is already at the beginning of
  21499. ;; a line, logical or visual.
  21500. ((not special))
  21501. ;; `beginning-of-visual-line' left point before logical beginning
  21502. ;; of line: point is at the beginning of a visual line. Bail
  21503. ;; out.
  21504. ((and (bound-and-true-p visual-line-mode) (not (bolp))))
  21505. ((let ((case-fold-search nil)) (looking-at org-complex-heading-regexp))
  21506. ;; At a headline, special position is before the title, but
  21507. ;; after any TODO keyword or priority cookie.
  21508. (let ((refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21509. (line-end-position)))
  21510. (bol (point)))
  21511. (if (eq special 'reversed)
  21512. (when (and (= origin bol) (eq last-command this-command))
  21513. (goto-char refpos))
  21514. (when (or (> origin refpos) (= origin bol))
  21515. (goto-char refpos)))))
  21516. ((and (looking-at org-list-full-item-re)
  21517. (memq (org-element-type (save-match-data (org-element-at-point)))
  21518. '(item plain-list)))
  21519. ;; Set special position at first white space character after
  21520. ;; bullet, and check-box, if any.
  21521. (let ((after-bullet
  21522. (let ((box (match-end 3)))
  21523. (cond ((not box) (match-end 1))
  21524. ((eq (char-after box) ?\s) (1+ box))
  21525. (t box)))))
  21526. (if (eq special 'reversed)
  21527. (when (and (= (point) origin) (eq last-command this-command))
  21528. (goto-char after-bullet))
  21529. (when (or (> origin after-bullet) (= (point) origin))
  21530. (goto-char after-bullet)))))
  21531. ;; No special context. Point is already at beginning of line.
  21532. (t nil))))
  21533. (defun org-end-of-line (&optional n)
  21534. "Go to the end of the line, but before ellipsis, if any.
  21535. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21536. tags on the first attempt, and only move to after the tags when
  21537. the cursor is already beyond the end of the headline.
  21538. With argument N not nil or 1, move forward N - 1 lines first."
  21539. (interactive "^p")
  21540. (let ((origin (point))
  21541. (special (pcase org-special-ctrl-a/e
  21542. (`(,_ . ,C-e) C-e) (_ org-special-ctrl-a/e)))
  21543. deactivate-mark)
  21544. ;; First move to a visible line.
  21545. (if (bound-and-true-p visual-line-mode)
  21546. (beginning-of-visual-line n)
  21547. (move-beginning-of-line n))
  21548. (cond
  21549. ;; At a headline, with tags.
  21550. ((and special
  21551. (save-excursion
  21552. (beginning-of-line)
  21553. (let ((case-fold-search nil))
  21554. (looking-at org-complex-heading-regexp)))
  21555. (match-end 5))
  21556. (let ((tags (save-excursion
  21557. (goto-char (match-beginning 5))
  21558. (skip-chars-backward " \t")
  21559. (point)))
  21560. (visual-end (and (bound-and-true-p visual-line-mode)
  21561. (save-excursion
  21562. (end-of-visual-line)
  21563. (point)))))
  21564. ;; If `end-of-visual-line' brings us before end of line or
  21565. ;; even tags, i.e., the headline spans over multiple visual
  21566. ;; lines, move there.
  21567. (cond ((and visual-end
  21568. (< visual-end tags)
  21569. (<= origin visual-end))
  21570. (goto-char visual-end))
  21571. ((eq special 'reversed)
  21572. (if (and (= origin (line-end-position))
  21573. (eq this-command last-command))
  21574. (goto-char tags)
  21575. (end-of-line)))
  21576. (t
  21577. (if (or (< origin tags) (= origin (line-end-position)))
  21578. (goto-char tags)
  21579. (end-of-line))))))
  21580. ((bound-and-true-p visual-line-mode)
  21581. (let ((bol (line-beginning-position)))
  21582. (end-of-visual-line)
  21583. ;; If `end-of-visual-line' gets us past the ellipsis at the
  21584. ;; end of a line, backtrack and use `end-of-line' instead.
  21585. (when (/= bol (line-beginning-position))
  21586. (goto-char bol)
  21587. (end-of-line))))
  21588. (t (end-of-line)))))
  21589. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21590. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21591. (defun org-backward-sentence (&optional _arg)
  21592. "Go to beginning of sentence, or beginning of table field.
  21593. This will call `backward-sentence' or `org-table-beginning-of-field',
  21594. depending on context."
  21595. (interactive)
  21596. (let* ((element (org-element-at-point))
  21597. (contents-begin (org-element-property :contents-begin element))
  21598. (table (org-element-lineage element '(table) t)))
  21599. (if (and table
  21600. (> (point) contents-begin)
  21601. (<= (point) (org-element-property :contents-end table)))
  21602. (call-interactively #'org-table-beginning-of-field)
  21603. (save-restriction
  21604. (when (and contents-begin
  21605. (< (point-min) contents-begin)
  21606. (> (point) contents-begin))
  21607. (narrow-to-region contents-begin
  21608. (org-element-property :contents-end element)))
  21609. (call-interactively #'backward-sentence)))))
  21610. (defun org-forward-sentence (&optional _arg)
  21611. "Go to end of sentence, or end of table field.
  21612. This will call `forward-sentence' or `org-table-end-of-field',
  21613. depending on context."
  21614. (interactive)
  21615. (let* ((element (org-element-at-point))
  21616. (contents-end (org-element-property :contents-end element))
  21617. (table (org-element-lineage element '(table) t)))
  21618. (if (and table
  21619. (>= (point) (org-element-property :contents-begin table))
  21620. (< (point) contents-end))
  21621. (call-interactively #'org-table-end-of-field)
  21622. (save-restriction
  21623. (when (and contents-end
  21624. (> (point-max) contents-end)
  21625. ;; Skip blank lines between elements.
  21626. (< (org-element-property :end element)
  21627. (save-excursion (goto-char contents-end)
  21628. (skip-chars-forward " \r\t\n"))))
  21629. (narrow-to-region (org-element-property :contents-begin element)
  21630. contents-end))
  21631. (call-interactively #'forward-sentence)))))
  21632. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21633. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21634. (defun org-kill-line (&optional _arg)
  21635. "Kill line, to tags or end of line."
  21636. (interactive)
  21637. (cond
  21638. ((or (not org-special-ctrl-k)
  21639. (bolp)
  21640. (not (org-at-heading-p)))
  21641. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21642. org-ctrl-k-protect-subtree
  21643. (or (eq org-ctrl-k-protect-subtree 'error)
  21644. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  21645. (user-error "C-k aborted as it would kill a hidden subtree"))
  21646. (call-interactively
  21647. (if (bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21648. ((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
  21649. (kill-region (point) (match-beginning 1))
  21650. (org-set-tags nil t))
  21651. (t (kill-region (point) (point-at-eol)))))
  21652. (define-key org-mode-map "\C-k" 'org-kill-line)
  21653. (defun org-yank (&optional arg)
  21654. "Yank. If the kill is a subtree, treat it specially.
  21655. This command will look at the current kill and check if is a single
  21656. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21657. cursor is at the beginning of a line or after the stars of a currently
  21658. empty headline, then the yank is handled specially. How exactly depends
  21659. on the value of the following variables.
  21660. `org-yank-folded-subtrees'
  21661. By default, this variable is non-nil, which results in
  21662. subtree(s) being folded after insertion, except if doing so
  21663. would swallow text after the yanked text.
  21664. `org-yank-adjusted-subtrees'
  21665. When non-nil (the default value is nil), the subtree will be
  21666. promoted or demoted in order to fit into the local outline tree
  21667. structure, which means that the level will be adjusted so that it
  21668. becomes the smaller one of the two *visible* surrounding headings.
  21669. Any prefix to this command will cause `yank' to be called directly with
  21670. no special treatment. In particular, a simple `\\[universal-argument]' prefix \
  21671. will just
  21672. plainly yank the text as it is.
  21673. \[1] The test checks if the first non-white line is a heading
  21674. and if there are no other headings with fewer stars."
  21675. (interactive "P")
  21676. (org-yank-generic 'yank arg))
  21677. (defun org-yank-generic (command arg)
  21678. "Perform some yank-like command.
  21679. This function implements the behavior described in the `org-yank'
  21680. documentation. However, it has been generalized to work for any
  21681. interactive command with similar behavior."
  21682. ;; pretend to be command COMMAND
  21683. (setq this-command command)
  21684. (if arg
  21685. (call-interactively command)
  21686. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21687. (and (org-kill-is-subtree-p)
  21688. (or (bolp)
  21689. (and (looking-at "[ \t]*$")
  21690. (string-match
  21691. "\\`\\*+\\'"
  21692. (buffer-substring (point-at-bol) (point)))))))
  21693. swallowp)
  21694. (cond
  21695. ((and subtreep org-yank-folded-subtrees)
  21696. (let ((beg (point))
  21697. end)
  21698. (if (and subtreep org-yank-adjusted-subtrees)
  21699. (org-paste-subtree nil nil 'for-yank)
  21700. (call-interactively command))
  21701. (setq end (point))
  21702. (goto-char beg)
  21703. (when (and (bolp) subtreep
  21704. (not (setq swallowp
  21705. (org-yank-folding-would-swallow-text beg end))))
  21706. (org-with-limited-levels
  21707. (or (looking-at org-outline-regexp)
  21708. (re-search-forward org-outline-regexp-bol end t))
  21709. (while (and (< (point) end) (looking-at org-outline-regexp))
  21710. (outline-hide-subtree)
  21711. (org-cycle-show-empty-lines 'folded)
  21712. (condition-case nil
  21713. (outline-forward-same-level 1)
  21714. (error (goto-char end))))))
  21715. (when swallowp
  21716. (message
  21717. "Inserted text not folded because that would swallow text"))
  21718. (goto-char end)
  21719. (skip-chars-forward " \t\n\r")
  21720. (beginning-of-line 1)
  21721. (push-mark beg 'nomsg)))
  21722. ((and subtreep org-yank-adjusted-subtrees)
  21723. (let ((beg (point-at-bol)))
  21724. (org-paste-subtree nil nil 'for-yank)
  21725. (push-mark beg 'nomsg)))
  21726. (t
  21727. (call-interactively command))))))
  21728. (defun org-yank-folding-would-swallow-text (beg end)
  21729. "Would hide-subtree at BEG swallow any text after END?"
  21730. (let (level)
  21731. (org-with-limited-levels
  21732. (save-excursion
  21733. (goto-char beg)
  21734. (when (or (looking-at org-outline-regexp)
  21735. (re-search-forward org-outline-regexp-bol end t))
  21736. (setq level (org-outline-level)))
  21737. (goto-char end)
  21738. (skip-chars-forward " \t\r\n\v\f")
  21739. (not (or (eobp)
  21740. (and (bolp) (looking-at-p org-outline-regexp)
  21741. (<= (org-outline-level) level))))))))
  21742. (define-key org-mode-map "\C-y" 'org-yank)
  21743. (defun org-truely-invisible-p ()
  21744. "Check if point is at a character currently not visible.
  21745. This version does not only check the character property, but also
  21746. `visible-mode'."
  21747. (unless (bound-and-true-p visible-mode)
  21748. (org-invisible-p)))
  21749. (defun org-invisible-p2 ()
  21750. "Check if point is at a character currently not visible.
  21751. If the point is at EOL (and not at the beginning of a buffer too),
  21752. move it back by one char before doing this check."
  21753. (save-excursion
  21754. (when (and (eolp) (not (bobp)))
  21755. (backward-char 1))
  21756. (org-invisible-p)))
  21757. (defun org-back-to-heading (&optional invisible-ok)
  21758. "Call `outline-back-to-heading', but provide a better error message."
  21759. (condition-case nil
  21760. (outline-back-to-heading invisible-ok)
  21761. (error (error "Before first headline at position %d in buffer %s"
  21762. (point) (current-buffer)))))
  21763. (defun org-before-first-heading-p ()
  21764. "Before first heading?"
  21765. (save-excursion
  21766. (end-of-line)
  21767. (null (re-search-backward org-outline-regexp-bol nil t))))
  21768. (defun org-at-heading-p (&optional ignored)
  21769. (outline-on-heading-p t))
  21770. (defun org-in-commented-heading-p (&optional no-inheritance)
  21771. "Non-nil if point is under a commented heading.
  21772. This function also checks ancestors of the current headline,
  21773. unless optional argument NO-INHERITANCE is non-nil."
  21774. (cond
  21775. ((org-before-first-heading-p) nil)
  21776. ((let ((headline (nth 4 (org-heading-components))))
  21777. (and headline
  21778. (let ((case-fold-search nil))
  21779. (string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21780. headline)))))
  21781. (no-inheritance nil)
  21782. (t
  21783. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21784. (defun org-at-comment-p nil
  21785. "Is cursor in a commented line?"
  21786. (save-excursion
  21787. (save-match-data
  21788. (beginning-of-line)
  21789. (looking-at "^[ \t]*# "))))
  21790. (defun org-at-drawer-p nil
  21791. "Is cursor at a drawer keyword?"
  21792. (save-excursion
  21793. (move-beginning-of-line 1)
  21794. (looking-at org-drawer-regexp)))
  21795. (defun org-at-block-p nil
  21796. "Is cursor at a block keyword?"
  21797. (save-excursion
  21798. (move-beginning-of-line 1)
  21799. (looking-at org-block-regexp)))
  21800. (defun org-point-at-end-of-empty-headline ()
  21801. "If point is at the end of an empty headline, return t, else nil.
  21802. If the heading only contains a TODO keyword, it is still still considered
  21803. empty."
  21804. (let ((case-fold-search nil))
  21805. (and (looking-at "[ \t]*$")
  21806. org-todo-line-regexp
  21807. (save-excursion
  21808. (beginning-of-line)
  21809. (looking-at org-todo-line-regexp)
  21810. (string= (match-string 3) "")))))
  21811. (defun org-at-heading-or-item-p ()
  21812. (or (org-at-heading-p) (org-at-item-p)))
  21813. (defun org-at-target-p ()
  21814. (or (org-in-regexp org-radio-target-regexp)
  21815. (org-in-regexp org-target-regexp)))
  21816. ;; Compatibility alias with Org versions < 7.8.03
  21817. (defalias 'org-on-target-p 'org-at-target-p)
  21818. (defun org-up-heading-all (arg)
  21819. "Move to the heading line of which the present line is a subheading.
  21820. This function considers both visible and invisible heading lines.
  21821. With argument, move up ARG levels."
  21822. (outline-up-heading arg t))
  21823. (defun org-up-heading-safe ()
  21824. "Move to the heading line of which the present line is a subheading.
  21825. This version will not throw an error. It will return the level of the
  21826. headline found, or nil if no higher level is found.
  21827. Also, this function will be a lot faster than `outline-up-heading',
  21828. because it relies on stars being the outline starters. This can really
  21829. make a significant difference in outlines with very many siblings."
  21830. (when (ignore-errors (org-back-to-heading t))
  21831. (let ((level-up (1- (funcall outline-level))))
  21832. (and (> level-up 0)
  21833. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21834. (funcall outline-level)))))
  21835. (defun org-first-sibling-p ()
  21836. "Is this heading the first child of its parents?"
  21837. (interactive)
  21838. (let ((re org-outline-regexp-bol)
  21839. level l)
  21840. (unless (org-at-heading-p t)
  21841. (user-error "Not at a heading"))
  21842. (setq level (funcall outline-level))
  21843. (save-excursion
  21844. (if (not (re-search-backward re nil t))
  21845. t
  21846. (setq l (funcall outline-level))
  21847. (< l level)))))
  21848. (defun org-goto-sibling (&optional previous)
  21849. "Goto the next sibling, even if it is invisible.
  21850. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21851. when a sibling was found. When none is found, return nil and don't
  21852. move point."
  21853. (let ((fun (if previous 're-search-backward 're-search-forward))
  21854. (pos (point))
  21855. (re org-outline-regexp-bol)
  21856. level l)
  21857. (when (ignore-errors (org-back-to-heading t))
  21858. (setq level (funcall outline-level))
  21859. (catch 'exit
  21860. (or previous (forward-char 1))
  21861. (while (funcall fun re nil t)
  21862. (setq l (funcall outline-level))
  21863. (when (< l level) (goto-char pos) (throw 'exit nil))
  21864. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21865. (goto-char pos)
  21866. nil))))
  21867. (defun org-show-siblings ()
  21868. "Show all siblings of the current headline."
  21869. (save-excursion
  21870. (while (org-goto-sibling) (org-flag-heading nil)))
  21871. (save-excursion
  21872. (while (org-goto-sibling 'previous)
  21873. (org-flag-heading nil))))
  21874. (defun org-goto-first-child ()
  21875. "Goto the first child, even if it is invisible.
  21876. Return t when a child was found. Otherwise don't move point and
  21877. return nil."
  21878. (let (level (pos (point)) (re org-outline-regexp-bol))
  21879. (when (ignore-errors (org-back-to-heading t))
  21880. (setq level (outline-level))
  21881. (forward-char 1)
  21882. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21883. (progn (goto-char (match-beginning 0)) t)
  21884. (goto-char pos) nil))))
  21885. (defun org-show-hidden-entry ()
  21886. "Show an entry where even the heading is hidden."
  21887. (save-excursion
  21888. (org-show-entry)))
  21889. (defun org-flag-heading (flag &optional entry)
  21890. "Flag the current heading. FLAG non-nil means make invisible.
  21891. When ENTRY is non-nil, show the entire entry."
  21892. (save-excursion
  21893. (org-back-to-heading t)
  21894. ;; Check if we should show the entire entry
  21895. (if entry
  21896. (progn
  21897. (org-show-entry)
  21898. (save-excursion
  21899. (and (outline-next-heading)
  21900. (org-flag-heading nil))))
  21901. (outline-flag-region (max (point-min) (1- (point)))
  21902. (save-excursion (outline-end-of-heading) (point))
  21903. flag))))
  21904. (defun org-get-next-sibling ()
  21905. "Move to next heading of the same level, and return point.
  21906. If there is no such heading, return nil.
  21907. This is like outline-next-sibling, but invisible headings are ok."
  21908. (let ((level (funcall outline-level)))
  21909. (outline-next-heading)
  21910. (while (and (not (eobp)) (> (funcall outline-level) level))
  21911. (outline-next-heading))
  21912. (unless (or (eobp) (< (funcall outline-level) level))
  21913. (point))))
  21914. (defun org-get-last-sibling ()
  21915. "Move to previous heading of the same level, and return point.
  21916. If there is no such heading, return nil."
  21917. (let ((opoint (point))
  21918. (level (funcall outline-level)))
  21919. (outline-previous-heading)
  21920. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21921. (while (and (> (funcall outline-level) level)
  21922. (not (bobp)))
  21923. (outline-previous-heading))
  21924. (unless (< (funcall outline-level) level)
  21925. (point)))))
  21926. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21927. "Goto to the end of a subtree."
  21928. ;; This contains an exact copy of the original function, but it uses
  21929. ;; `org-back-to-heading', to make it work also in invisible
  21930. ;; trees. And is uses an invisible-ok argument.
  21931. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21932. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21933. ;; with many children and grandchildren etc, this can be much faster
  21934. ;; than the outline version.
  21935. (org-back-to-heading invisible-ok)
  21936. (let ((first t)
  21937. (level (funcall outline-level)))
  21938. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21939. ;; A true heading (not a plain list item), in Org
  21940. ;; This means we can easily find the end by looking
  21941. ;; only for the right number of stars. Using a regexp to do
  21942. ;; this is so much faster than using a Lisp loop.
  21943. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21944. (forward-char 1)
  21945. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21946. ;; something else, do it the slow way
  21947. (while (and (not (eobp))
  21948. (or first (> (funcall outline-level) level)))
  21949. (setq first nil)
  21950. (outline-next-heading)))
  21951. (unless to-heading
  21952. (when (memq (preceding-char) '(?\n ?\^M))
  21953. ;; Go to end of line before heading
  21954. (forward-char -1)
  21955. (when (memq (preceding-char) '(?\n ?\^M))
  21956. ;; leave blank line before heading
  21957. (forward-char -1)))))
  21958. (point))
  21959. (defun org-end-of-meta-data (&optional full)
  21960. "Skip planning line and properties drawer in current entry.
  21961. When optional argument FULL is non-nil, also skip empty lines,
  21962. clocking lines and regular drawers at the beginning of the
  21963. entry."
  21964. (org-back-to-heading t)
  21965. (forward-line)
  21966. (when (looking-at-p org-planning-line-re) (forward-line))
  21967. (when (looking-at org-property-drawer-re)
  21968. (goto-char (match-end 0))
  21969. (forward-line))
  21970. (when (and full (not (org-at-heading-p)))
  21971. (catch 'exit
  21972. (let ((end (save-excursion (outline-next-heading) (point)))
  21973. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  21974. (while (not (eobp))
  21975. (cond ((looking-at-p org-drawer-regexp)
  21976. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  21977. (forward-line)
  21978. (throw 'exit t)))
  21979. ((looking-at-p re) (forward-line))
  21980. (t (throw 'exit t))))))))
  21981. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21982. "Move forward to the ARG'th subheading at same level as this one.
  21983. Stop at the first and last subheadings of a superior heading.
  21984. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21985. non-nil it will also look at invisible ones."
  21986. (interactive "p")
  21987. (let ((backward? (and arg (< arg 0))))
  21988. (if (org-before-first-heading-p)
  21989. (if backward? (goto-char (point-min)) (outline-next-heading))
  21990. (org-back-to-heading invisible-ok)
  21991. (unless backward? (end-of-line)) ;do not match current headline
  21992. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21993. (f (if backward? #'re-search-backward #'re-search-forward))
  21994. (count (if arg (abs arg) 1))
  21995. (result (point)))
  21996. (while (and (> count 0)
  21997. (funcall f org-outline-regexp-bol nil 'move))
  21998. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21999. (cond ((< l level) (setq count 0))
  22000. ((and (= l level)
  22001. (or invisible-ok
  22002. (not (org-invisible-p
  22003. (line-beginning-position)))))
  22004. (cl-decf count)
  22005. (when (= l level) (setq result (point)))))))
  22006. (goto-char result))
  22007. (beginning-of-line))))
  22008. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22009. "Move backward to the ARG'th subheading at same level as this one.
  22010. Stop at the first and last subheadings of a superior heading."
  22011. (interactive "p")
  22012. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22013. (defun org-next-visible-heading (arg)
  22014. "Move to the next visible heading.
  22015. This function wraps `outline-next-visible-heading' with
  22016. `org-with-limited-levels' in order to skip over inline tasks and
  22017. respect customization of `org-odd-levels-only'."
  22018. (interactive "p")
  22019. (org-with-limited-levels
  22020. (outline-next-visible-heading arg)))
  22021. (defun org-previous-visible-heading (arg)
  22022. "Move to the previous visible heading.
  22023. This function wraps `outline-previous-visible-heading' with
  22024. `org-with-limited-levels' in order to skip over inline tasks and
  22025. respect customization of `org-odd-levels-only'."
  22026. (interactive "p")
  22027. (org-with-limited-levels
  22028. (outline-previous-visible-heading arg)))
  22029. (defun org-next-block (arg &optional backward block-regexp)
  22030. "Jump to the next block.
  22031. With a prefix argument ARG, jump forward ARG many blocks.
  22032. When BACKWARD is non-nil, jump to the previous block.
  22033. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22034. Match data is set according to this regexp when the function
  22035. returns.
  22036. Return point at beginning of the opening line of found block.
  22037. Throw an error if no block is found."
  22038. (interactive "p")
  22039. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22040. (case-fold-search t)
  22041. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22042. (count (or arg 1))
  22043. (origin (point))
  22044. last-element)
  22045. (if backward (beginning-of-line) (end-of-line))
  22046. (while (and (> count 0) (funcall search-fn re nil t))
  22047. (let ((element (save-excursion
  22048. (goto-char (match-beginning 0))
  22049. (save-match-data (org-element-at-point)))))
  22050. (when (and (memq (org-element-type element)
  22051. '(center-block comment-block dynamic-block
  22052. example-block export-block quote-block
  22053. special-block src-block verse-block))
  22054. (<= (match-beginning 0)
  22055. (org-element-property :post-affiliated element)))
  22056. (setq last-element element)
  22057. (cl-decf count))))
  22058. (if (= count 0)
  22059. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22060. (save-match-data (org-show-context)))
  22061. (goto-char origin)
  22062. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22063. (defun org-previous-block (arg &optional block-regexp)
  22064. "Jump to the previous block.
  22065. With a prefix argument ARG, jump backward ARG many source blocks.
  22066. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22067. (interactive "p")
  22068. (org-next-block arg t block-regexp))
  22069. (defun org-forward-paragraph ()
  22070. "Move forward to beginning of next paragraph or equivalent.
  22071. The function moves point to the beginning of the next visible
  22072. structural element, which can be a paragraph, a table, a list
  22073. item, etc. It also provides some special moves for convenience:
  22074. - On an affiliated keyword, jump to the beginning of the
  22075. relative element.
  22076. - On an item or a footnote definition, move to the second
  22077. element inside, if any.
  22078. - On a table or a property drawer, jump after it.
  22079. - On a verse or source block, stop after blank lines."
  22080. (interactive)
  22081. (unless (eobp)
  22082. (let* ((deactivate-mark nil)
  22083. (element (org-element-at-point))
  22084. (type (org-element-type element))
  22085. (post-affiliated (org-element-property :post-affiliated element))
  22086. (contents-begin (org-element-property :contents-begin element))
  22087. (contents-end (org-element-property :contents-end element))
  22088. (end (let ((end (org-element-property :end element)) (parent element))
  22089. (while (and (setq parent (org-element-property :parent parent))
  22090. (= (org-element-property :contents-end parent) end))
  22091. (setq end (org-element-property :end parent)))
  22092. end)))
  22093. (cond ((not element)
  22094. (skip-chars-forward " \r\t\n")
  22095. (or (eobp) (beginning-of-line)))
  22096. ;; On affiliated keywords, move to element's beginning.
  22097. ((< (point) post-affiliated)
  22098. (goto-char post-affiliated))
  22099. ;; At a table row, move to the end of the table. Similarly,
  22100. ;; at a node property, move to the end of the property
  22101. ;; drawer.
  22102. ((memq type '(node-property table-row))
  22103. (goto-char (org-element-property
  22104. :end (org-element-property :parent element))))
  22105. ((memq type '(property-drawer table)) (goto-char end))
  22106. ;; Consider blank lines as separators in verse and source
  22107. ;; blocks to ease editing.
  22108. ((memq type '(src-block verse-block))
  22109. (when (eq type 'src-block)
  22110. (setq contents-end
  22111. (save-excursion (goto-char end)
  22112. (skip-chars-backward " \r\t\n")
  22113. (line-beginning-position))))
  22114. (beginning-of-line)
  22115. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22116. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22117. (goto-char end)
  22118. (skip-chars-forward " \r\t\n")
  22119. (if (= (point) contents-end) (goto-char end)
  22120. (beginning-of-line))))
  22121. ;; With no contents, just skip element.
  22122. ((not contents-begin) (goto-char end))
  22123. ;; If contents are invisible, skip the element altogether.
  22124. ((org-invisible-p (line-end-position))
  22125. (cl-case type
  22126. (headline
  22127. (org-with-limited-levels (outline-next-visible-heading 1)))
  22128. ;; At a plain list, make sure we move to the next item
  22129. ;; instead of skipping the whole list.
  22130. (plain-list (forward-char)
  22131. (org-forward-paragraph))
  22132. (otherwise (goto-char end))))
  22133. ((>= (point) contents-end) (goto-char end))
  22134. ((>= (point) contents-begin)
  22135. ;; This can only happen on paragraphs and plain lists.
  22136. (cl-case type
  22137. (paragraph (goto-char end))
  22138. ;; At a plain list, try to move to second element in
  22139. ;; first item, if possible.
  22140. (plain-list (end-of-line)
  22141. (org-forward-paragraph))))
  22142. ;; When contents start on the middle of a line (e.g. in
  22143. ;; items and footnote definitions), try to reach first
  22144. ;; element starting after current line.
  22145. ((> (line-end-position) contents-begin)
  22146. (end-of-line)
  22147. (org-forward-paragraph))
  22148. (t (goto-char contents-begin))))))
  22149. (defun org-backward-paragraph ()
  22150. "Move backward to start of previous paragraph or equivalent.
  22151. The function moves point to the beginning of the current
  22152. structural element, which can be a paragraph, a table, a list
  22153. item, etc., or to the beginning of the previous visible one if
  22154. point is already there. It also provides some special moves for
  22155. convenience:
  22156. - On an affiliated keyword, jump to the first one.
  22157. - On a table or a property drawer, move to its beginning.
  22158. - On a verse or source block, stop before blank lines."
  22159. (interactive)
  22160. (unless (bobp)
  22161. (let* ((deactivate-mark nil)
  22162. (element (org-element-at-point))
  22163. (type (org-element-type element))
  22164. (contents-begin (org-element-property :contents-begin element))
  22165. (contents-end (org-element-property :contents-end element))
  22166. (post-affiliated (org-element-property :post-affiliated element))
  22167. (begin (org-element-property :begin element)))
  22168. (cond
  22169. ((not element) (goto-char (point-min)))
  22170. ((= (point) begin)
  22171. (backward-char)
  22172. (org-backward-paragraph))
  22173. ((<= (point) post-affiliated) (goto-char begin))
  22174. ((memq type '(node-property table-row))
  22175. (goto-char (org-element-property
  22176. :post-affiliated (org-element-property :parent element))))
  22177. ((memq type '(property-drawer table)) (goto-char begin))
  22178. ((memq type '(src-block verse-block))
  22179. (when (eq type 'src-block)
  22180. (setq contents-begin
  22181. (save-excursion (goto-char begin) (forward-line) (point))))
  22182. (if (= (point) contents-begin) (goto-char post-affiliated)
  22183. ;; Inside a verse block, see blank lines as paragraph
  22184. ;; separators.
  22185. (let ((origin (point)))
  22186. (skip-chars-backward " \r\t\n" contents-begin)
  22187. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22188. (skip-chars-forward " \r\t\n" origin)
  22189. (if (= (point) origin) (goto-char contents-begin)
  22190. (beginning-of-line))))))
  22191. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22192. ((eq type 'paragraph)
  22193. (goto-char contents-begin)
  22194. ;; When at first paragraph in an item or a footnote definition,
  22195. ;; move directly to beginning of line.
  22196. (let ((parent-contents
  22197. (org-element-property
  22198. :contents-begin (org-element-property :parent element))))
  22199. (when (and parent-contents (= parent-contents contents-begin))
  22200. (beginning-of-line))))
  22201. ;; At the end of a greater element, move to the beginning of the
  22202. ;; last element within.
  22203. ((>= (point) contents-end)
  22204. (goto-char (1- contents-end))
  22205. (org-backward-paragraph))
  22206. (t (goto-char (or post-affiliated begin))))
  22207. ;; Ensure we never leave point invisible.
  22208. (when (org-invisible-p (point)) (beginning-of-visual-line)))))
  22209. (defun org-forward-element ()
  22210. "Move forward by one element.
  22211. Move to the next element at the same level, when possible."
  22212. (interactive)
  22213. (cond ((eobp) (user-error "Cannot move further down"))
  22214. ((org-with-limited-levels (org-at-heading-p))
  22215. (let ((origin (point)))
  22216. (goto-char (org-end-of-subtree nil t))
  22217. (unless (org-with-limited-levels (org-at-heading-p))
  22218. (goto-char origin)
  22219. (user-error "Cannot move further down"))))
  22220. (t
  22221. (let* ((elem (org-element-at-point))
  22222. (end (org-element-property :end elem))
  22223. (parent (org-element-property :parent elem)))
  22224. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22225. (goto-char (org-element-property :end parent)))
  22226. ((integer-or-marker-p end) (goto-char end))
  22227. (t (message "No element at point")))))))
  22228. (defun org-backward-element ()
  22229. "Move backward by one element.
  22230. Move to the previous element at the same level, when possible."
  22231. (interactive)
  22232. (cond ((bobp) (user-error "Cannot move further up"))
  22233. ((org-with-limited-levels (org-at-heading-p))
  22234. ;; At a headline, move to the previous one, if any, or stay
  22235. ;; here.
  22236. (let ((origin (point)))
  22237. (org-with-limited-levels (org-backward-heading-same-level 1))
  22238. ;; When current headline has no sibling above, move to its
  22239. ;; parent.
  22240. (when (= (point) origin)
  22241. (or (org-with-limited-levels (org-up-heading-safe))
  22242. (progn (goto-char origin)
  22243. (user-error "Cannot move further up"))))))
  22244. (t
  22245. (let* ((elem (org-element-at-point))
  22246. (beg (org-element-property :begin elem)))
  22247. (cond
  22248. ;; Move to beginning of current element if point isn't
  22249. ;; there already.
  22250. ((null beg) (message "No element at point"))
  22251. ((/= (point) beg) (goto-char beg))
  22252. (t (goto-char beg)
  22253. (skip-chars-backward " \r\t\n")
  22254. (unless (bobp)
  22255. (let ((prev (org-element-at-point)))
  22256. (goto-char (org-element-property :begin prev))
  22257. (while (and (setq prev (org-element-property :parent prev))
  22258. (<= (org-element-property :end prev) beg))
  22259. (goto-char (org-element-property :begin prev)))))))))))
  22260. (defun org-up-element ()
  22261. "Move to upper element."
  22262. (interactive)
  22263. (if (org-with-limited-levels (org-at-heading-p))
  22264. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22265. (let* ((elem (org-element-at-point))
  22266. (parent (org-element-property :parent elem)))
  22267. (if parent (goto-char (org-element-property :begin parent))
  22268. (if (org-with-limited-levels (org-before-first-heading-p))
  22269. (user-error "No surrounding element")
  22270. (org-with-limited-levels (org-back-to-heading)))))))
  22271. (defun org-down-element ()
  22272. "Move to inner element."
  22273. (interactive)
  22274. (let ((element (org-element-at-point)))
  22275. (cond
  22276. ((memq (org-element-type element) '(plain-list table))
  22277. (goto-char (org-element-property :contents-begin element))
  22278. (forward-char))
  22279. ((memq (org-element-type element) org-element-greater-elements)
  22280. ;; If contents are hidden, first disclose them.
  22281. (when (org-invisible-p (line-end-position)) (org-cycle))
  22282. (goto-char (or (org-element-property :contents-begin element)
  22283. (user-error "No content for this element"))))
  22284. (t (user-error "No inner element")))))
  22285. (defun org-drag-element-backward ()
  22286. "Move backward element at point."
  22287. (interactive)
  22288. (let ((elem (or (org-element-at-point)
  22289. (user-error "No element at point"))))
  22290. (if (eq (org-element-type elem) 'headline)
  22291. ;; Preserve point when moving a whole tree, even if point was
  22292. ;; on blank lines below the headline.
  22293. (let ((offset (skip-chars-backward " \t\n")))
  22294. (unwind-protect (org-move-subtree-up)
  22295. (forward-char (- offset))))
  22296. (let ((prev-elem
  22297. (save-excursion
  22298. (goto-char (org-element-property :begin elem))
  22299. (skip-chars-backward " \r\t\n")
  22300. (unless (bobp)
  22301. (let* ((beg (org-element-property :begin elem))
  22302. (prev (org-element-at-point))
  22303. (up prev))
  22304. (while (and (setq up (org-element-property :parent up))
  22305. (<= (org-element-property :end up) beg))
  22306. (setq prev up))
  22307. prev)))))
  22308. ;; Error out if no previous element or previous element is
  22309. ;; a parent of the current one.
  22310. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22311. (user-error "Cannot drag element backward")
  22312. (let ((pos (point)))
  22313. (org-element-swap-A-B prev-elem elem)
  22314. (goto-char (+ (org-element-property :begin prev-elem)
  22315. (- pos (org-element-property :begin elem))))))))))
  22316. (defun org-drag-element-forward ()
  22317. "Move forward element at point."
  22318. (interactive)
  22319. (let* ((pos (point))
  22320. (elem (or (org-element-at-point)
  22321. (user-error "No element at point"))))
  22322. (when (= (point-max) (org-element-property :end elem))
  22323. (user-error "Cannot drag element forward"))
  22324. (goto-char (org-element-property :end elem))
  22325. (let ((next-elem (org-element-at-point)))
  22326. (when (or (org-element-nested-p elem next-elem)
  22327. (and (eq (org-element-type next-elem) 'headline)
  22328. (not (eq (org-element-type elem) 'headline))))
  22329. (goto-char pos)
  22330. (user-error "Cannot drag element forward"))
  22331. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22332. ;; body's length (without final blanks) and by the length of
  22333. ;; blanks between ELEM and NEXT-ELEM.
  22334. (let ((size-next (- (save-excursion
  22335. (goto-char (org-element-property :end next-elem))
  22336. (skip-chars-backward " \r\t\n")
  22337. (forward-line)
  22338. ;; Small correction if buffer doesn't end
  22339. ;; with a newline character.
  22340. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22341. (org-element-property :begin next-elem)))
  22342. (size-blank (- (org-element-property :end elem)
  22343. (save-excursion
  22344. (goto-char (org-element-property :end elem))
  22345. (skip-chars-backward " \r\t\n")
  22346. (forward-line)
  22347. (point)))))
  22348. (org-element-swap-A-B elem next-elem)
  22349. (goto-char (+ pos size-next size-blank))))))
  22350. (defun org-drag-line-forward (arg)
  22351. "Drag the line at point ARG lines forward."
  22352. (interactive "p")
  22353. (dotimes (_ (abs arg))
  22354. (let ((c (current-column)))
  22355. (if (< 0 arg)
  22356. (progn
  22357. (beginning-of-line 2)
  22358. (transpose-lines 1)
  22359. (beginning-of-line 0))
  22360. (transpose-lines 1)
  22361. (beginning-of-line -1))
  22362. (org-move-to-column c))))
  22363. (defun org-drag-line-backward (arg)
  22364. "Drag the line at point ARG lines backward."
  22365. (interactive "p")
  22366. (org-drag-line-forward (- arg)))
  22367. (defun org-mark-element ()
  22368. "Put point at beginning of this element, mark at end.
  22369. Interactively, if this command is repeated or (in Transient Mark
  22370. mode) if the mark is active, it marks the next element after the
  22371. ones already marked."
  22372. (interactive)
  22373. (let (deactivate-mark)
  22374. (if (and (called-interactively-p 'any)
  22375. (or (and (eq last-command this-command) (mark t))
  22376. (and transient-mark-mode mark-active)))
  22377. (set-mark
  22378. (save-excursion
  22379. (goto-char (mark))
  22380. (goto-char (org-element-property :end (org-element-at-point)))))
  22381. (let ((element (org-element-at-point)))
  22382. (end-of-line)
  22383. (push-mark (org-element-property :end element) t t)
  22384. (goto-char (org-element-property :begin element))))))
  22385. (defun org-narrow-to-element ()
  22386. "Narrow buffer to current element."
  22387. (interactive)
  22388. (let ((elem (org-element-at-point)))
  22389. (cond
  22390. ((eq (car elem) 'headline)
  22391. (narrow-to-region
  22392. (org-element-property :begin elem)
  22393. (org-element-property :end elem)))
  22394. ((memq (car elem) org-element-greater-elements)
  22395. (narrow-to-region
  22396. (org-element-property :contents-begin elem)
  22397. (org-element-property :contents-end elem)))
  22398. (t
  22399. (narrow-to-region
  22400. (org-element-property :begin elem)
  22401. (org-element-property :end elem))))))
  22402. (defun org-transpose-element ()
  22403. "Transpose current and previous elements, keeping blank lines between.
  22404. Point is moved after both elements."
  22405. (interactive)
  22406. (org-skip-whitespace)
  22407. (let ((end (org-element-property :end (org-element-at-point))))
  22408. (org-drag-element-backward)
  22409. (goto-char end)))
  22410. (defun org-unindent-buffer ()
  22411. "Un-indent the visible part of the buffer.
  22412. Relative indentation (between items, inside blocks, etc.) isn't
  22413. modified."
  22414. (interactive)
  22415. (unless (eq major-mode 'org-mode)
  22416. (user-error "Cannot un-indent a buffer not in Org mode"))
  22417. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22418. (unindent-tree
  22419. (lambda (contents)
  22420. (dolist (element (reverse contents))
  22421. (if (memq (org-element-type element) '(headline section))
  22422. (funcall unindent-tree (org-element-contents element))
  22423. (save-excursion
  22424. (save-restriction
  22425. (narrow-to-region
  22426. (org-element-property :begin element)
  22427. (org-element-property :end element))
  22428. (org-do-remove-indentation))))))))
  22429. (funcall unindent-tree (org-element-contents parse-tree))))
  22430. (defun org-show-children (&optional level)
  22431. "Show all direct subheadings of this heading.
  22432. Prefix arg LEVEL is how many levels below the current level
  22433. should be shown. Default is enough to cause the following
  22434. heading to appear."
  22435. (interactive "p")
  22436. ;; If `orgstruct-mode' is active, use the slower version.
  22437. (if orgstruct-mode (call-interactively #'outline-show-children)
  22438. (save-excursion
  22439. (org-back-to-heading t)
  22440. (let* ((current-level (funcall outline-level))
  22441. (max-level (org-get-valid-level
  22442. current-level
  22443. (if level (prefix-numeric-value level) 1)))
  22444. (end (save-excursion (org-end-of-subtree t t)))
  22445. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22446. (past-first-child nil)
  22447. ;; Make sure to skip inlinetasks.
  22448. (re (format regexp-fmt
  22449. current-level
  22450. (cond
  22451. ((not (featurep 'org-inlinetask)) "")
  22452. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22453. 3))
  22454. (t (1- org-inlinetask-min-level))))))
  22455. ;; Display parent heading.
  22456. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22457. (forward-line)
  22458. ;; Display children. First child may be deeper than expected
  22459. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22460. ;; MAX-LEVEL accordingly.
  22461. (while (re-search-forward re end t)
  22462. (unless past-first-child
  22463. (setq re (format regexp-fmt
  22464. current-level
  22465. (max (funcall outline-level) max-level)))
  22466. (setq past-first-child t))
  22467. (outline-flag-region
  22468. (line-end-position 0) (line-end-position) nil))))))
  22469. (defun org-show-subtree ()
  22470. "Show everything after this heading at deeper levels."
  22471. (interactive)
  22472. (outline-flag-region
  22473. (point)
  22474. (save-excursion
  22475. (org-end-of-subtree t t))
  22476. nil))
  22477. (defun org-show-entry ()
  22478. "Show the body directly following this heading.
  22479. Show the heading too, if it is currently invisible."
  22480. (interactive)
  22481. (save-excursion
  22482. (ignore-errors
  22483. (org-back-to-heading t)
  22484. (outline-flag-region
  22485. (max (point-min) (1- (point)))
  22486. (save-excursion
  22487. (if (re-search-forward
  22488. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22489. (match-beginning 1)
  22490. (point-max)))
  22491. nil)
  22492. (org-cycle-hide-drawers 'children))))
  22493. (defun org-make-options-regexp (kwds &optional extra)
  22494. "Make a regular expression for keyword lines.
  22495. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22496. when non-nil, is a regexp matching keywords names."
  22497. (concat "^[ \t]*#\\+\\("
  22498. (regexp-opt kwds)
  22499. (and extra (concat (and kwds "\\|") extra))
  22500. "\\):[ \t]*\\(.*\\)"))
  22501. ;;;; Integration with and fixes for other packages
  22502. ;;; Imenu support
  22503. (defvar-local org-imenu-markers nil
  22504. "All markers currently used by Imenu.")
  22505. (defun org-imenu-new-marker (&optional pos)
  22506. "Return a new marker for use by Imenu, and remember the marker."
  22507. (let ((m (make-marker)))
  22508. (move-marker m (or pos (point)))
  22509. (push m org-imenu-markers)
  22510. m))
  22511. (defun org-imenu-get-tree ()
  22512. "Produce the index for Imenu."
  22513. (dolist (x org-imenu-markers) (move-marker x nil))
  22514. (setq org-imenu-markers nil)
  22515. (let* ((case-fold-search nil)
  22516. (n org-imenu-depth)
  22517. (re (concat "^" (org-get-limited-outline-regexp)))
  22518. (subs (make-vector (1+ n) nil))
  22519. (last-level 0)
  22520. m level head0 head)
  22521. (org-with-wide-buffer
  22522. (goto-char (point-max))
  22523. (while (re-search-backward re nil t)
  22524. (setq level (org-reduced-level (funcall outline-level)))
  22525. (when (and (<= level n)
  22526. (looking-at org-complex-heading-regexp)
  22527. (setq head0 (match-string-no-properties 4)))
  22528. (setq head (org-link-display-format head0)
  22529. m (org-imenu-new-marker))
  22530. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22531. (if (>= level last-level)
  22532. (push (cons head m) (aref subs level))
  22533. (push (cons head (aref subs (1+ level))) (aref subs level))
  22534. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22535. (setq last-level level))))
  22536. (aref subs 1)))
  22537. (eval-after-load "imenu"
  22538. '(progn
  22539. (add-hook 'imenu-after-jump-hook
  22540. (lambda ()
  22541. (when (derived-mode-p 'org-mode)
  22542. (org-show-context 'org-goto))))))
  22543. (defun org-link-display-format (s)
  22544. "Replace links in string S with their description.
  22545. If there is no description, use the link target."
  22546. (save-match-data
  22547. (replace-regexp-in-string
  22548. org-bracket-link-analytic-regexp
  22549. (lambda (m)
  22550. (if (match-end 5) (match-string 5 m)
  22551. (concat (match-string 1 m) (match-string 3 m))))
  22552. s nil t)))
  22553. (defun org-toggle-link-display ()
  22554. "Toggle the literal or descriptive display of links."
  22555. (interactive)
  22556. (if org-descriptive-links
  22557. (progn (org-remove-from-invisibility-spec '(org-link))
  22558. (org-restart-font-lock)
  22559. (setq org-descriptive-links nil))
  22560. (progn (add-to-invisibility-spec '(org-link))
  22561. (org-restart-font-lock)
  22562. (setq org-descriptive-links t))))
  22563. ;; Speedbar support
  22564. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22565. "Overlay marking the agenda restriction line in speedbar.")
  22566. (overlay-put org-speedbar-restriction-lock-overlay
  22567. 'face 'org-agenda-restriction-lock)
  22568. (overlay-put org-speedbar-restriction-lock-overlay
  22569. 'help-echo "Agendas are currently limited to this item.")
  22570. (delete-overlay org-speedbar-restriction-lock-overlay)
  22571. (defun org-speedbar-set-agenda-restriction ()
  22572. "Restrict future agenda commands to the location at point in speedbar.
  22573. To get rid of the restriction, use `\\[org-agenda-remove-restriction-lock]'."
  22574. (interactive)
  22575. (require 'org-agenda)
  22576. (let (p m tp np dir txt)
  22577. (cond
  22578. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22579. 'org-imenu t))
  22580. (setq m (get-text-property p 'org-imenu-marker))
  22581. (with-current-buffer (marker-buffer m)
  22582. (goto-char m)
  22583. (org-agenda-set-restriction-lock 'subtree)))
  22584. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22585. 'speedbar-function 'speedbar-find-file))
  22586. (setq tp (previous-single-property-change
  22587. (1+ p) 'speedbar-function)
  22588. np (next-single-property-change
  22589. tp 'speedbar-function)
  22590. dir (speedbar-line-directory)
  22591. txt (buffer-substring-no-properties (or tp (point-min))
  22592. (or np (point-max))))
  22593. (with-current-buffer (find-file-noselect
  22594. (let ((default-directory dir))
  22595. (expand-file-name txt)))
  22596. (unless (derived-mode-p 'org-mode)
  22597. (user-error "Cannot restrict to non-Org mode file"))
  22598. (org-agenda-set-restriction-lock 'file)))
  22599. (t (user-error "Don't know how to restrict Org mode agenda")))
  22600. (move-overlay org-speedbar-restriction-lock-overlay
  22601. (point-at-bol) (point-at-eol))
  22602. (setq current-prefix-arg nil)
  22603. (org-agenda-maybe-redo)))
  22604. (defvar speedbar-file-key-map)
  22605. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22606. (eval-after-load "speedbar"
  22607. '(progn
  22608. (speedbar-add-supported-extension ".org")
  22609. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22610. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22611. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22612. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22613. (add-hook 'speedbar-visiting-tag-hook
  22614. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22615. ;;; Fixes and Hacks for problems with other packages
  22616. (defun org--flyspell-object-check-p (element)
  22617. "Non-nil when Flyspell can check object at point.
  22618. ELEMENT is the element at point."
  22619. (let ((object (save-excursion
  22620. (when (looking-at-p "\\>") (backward-char))
  22621. (org-element-context element))))
  22622. (cl-case (org-element-type object)
  22623. ;; Prevent checks in links due to keybinding conflict with
  22624. ;; Flyspell.
  22625. ((code entity export-snippet inline-babel-call
  22626. inline-src-block line-break latex-fragment link macro
  22627. statistics-cookie target timestamp verbatim)
  22628. nil)
  22629. (footnote-reference
  22630. ;; Only in inline footnotes, within the definition.
  22631. (and (eq (org-element-property :type object) 'inline)
  22632. (< (save-excursion
  22633. (goto-char (org-element-property :begin object))
  22634. (search-forward ":" nil t 2))
  22635. (point))))
  22636. (otherwise t))))
  22637. (defun org-mode-flyspell-verify ()
  22638. "Function used for `flyspell-generic-check-word-predicate'."
  22639. (if (org-at-heading-p)
  22640. ;; At a headline or an inlinetask, check title only. This is
  22641. ;; faster than relying on `org-element-at-point'.
  22642. (and (save-excursion (beginning-of-line)
  22643. (and (let ((case-fold-search t))
  22644. (not (looking-at-p "\\*+ END[ \t]*$")))
  22645. (let ((case-fold-search nil))
  22646. (looking-at org-complex-heading-regexp))))
  22647. (match-beginning 4)
  22648. (>= (point) (match-beginning 4))
  22649. (or (not (match-beginning 5))
  22650. (< (point) (match-beginning 5))))
  22651. (let* ((element (org-element-at-point))
  22652. (post-affiliated (org-element-property :post-affiliated element)))
  22653. (cond
  22654. ;; Ignore checks in all affiliated keywords but captions.
  22655. ((< (point) post-affiliated)
  22656. (and (save-excursion
  22657. (beginning-of-line)
  22658. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22659. (> (point) (match-end 0))
  22660. (org--flyspell-object-check-p element)))
  22661. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22662. ((let ((log (org-log-into-drawer)))
  22663. (and log
  22664. (let ((drawer (org-element-lineage element '(drawer))))
  22665. (and drawer
  22666. (eq (compare-strings
  22667. log nil nil
  22668. (org-element-property :drawer-name drawer) nil nil t)
  22669. t)))))
  22670. nil)
  22671. (t
  22672. (cl-case (org-element-type element)
  22673. ((comment quote-section) t)
  22674. (comment-block
  22675. ;; Allow checks between block markers, not on them.
  22676. (and (> (line-beginning-position) post-affiliated)
  22677. (save-excursion
  22678. (end-of-line)
  22679. (skip-chars-forward " \r\t\n")
  22680. (< (point) (org-element-property :end element)))))
  22681. ;; Arbitrary list of keywords where checks are meaningful.
  22682. ;; Make sure point is on the value part of the element.
  22683. (keyword
  22684. (and (member (org-element-property :key element)
  22685. '("DESCRIPTION" "TITLE"))
  22686. (save-excursion
  22687. (search-backward ":" (line-beginning-position) t))))
  22688. ;; Check is globally allowed in paragraphs verse blocks and
  22689. ;; table rows (after affiliated keywords) but some objects
  22690. ;; must not be affected.
  22691. ((paragraph table-row verse-block)
  22692. (let ((cbeg (org-element-property :contents-begin element))
  22693. (cend (org-element-property :contents-end element)))
  22694. (and cbeg (>= (point) cbeg) (< (point) cend)
  22695. (org--flyspell-object-check-p element))))))))))
  22696. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22697. (defun org-remove-flyspell-overlays-in (beg end)
  22698. "Remove flyspell overlays in region."
  22699. (and (bound-and-true-p flyspell-mode)
  22700. (fboundp 'flyspell-delete-region-overlays)
  22701. (flyspell-delete-region-overlays beg end)))
  22702. (defvar flyspell-delayed-commands)
  22703. (eval-after-load "flyspell"
  22704. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22705. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22706. (eval-after-load "bookmark"
  22707. '(if (boundp 'bookmark-after-jump-hook)
  22708. ;; We can use the hook
  22709. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22710. ;; Hook not available, use advice
  22711. (defadvice bookmark-jump (after org-make-visible activate)
  22712. "Make the position visible."
  22713. (org-bookmark-jump-unhide))))
  22714. ;; Make sure saveplace shows the location if it was hidden
  22715. (eval-after-load "saveplace"
  22716. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22717. "Make the position visible."
  22718. (org-bookmark-jump-unhide)))
  22719. ;; Make sure ecb shows the location if it was hidden
  22720. (eval-after-load "ecb"
  22721. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22722. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22723. (when (derived-mode-p 'org-mode)
  22724. (org-show-context))))
  22725. (defun org-bookmark-jump-unhide ()
  22726. "Unhide the current position, to show the bookmark location."
  22727. (and (derived-mode-p 'org-mode)
  22728. (or (org-invisible-p)
  22729. (save-excursion (goto-char (max (point-min) (1- (point))))
  22730. (org-invisible-p)))
  22731. (org-show-context 'bookmark-jump)))
  22732. (defun org-mark-jump-unhide ()
  22733. "Make the point visible with `org-show-context' after jumping to the mark."
  22734. (when (and (derived-mode-p 'org-mode)
  22735. (org-invisible-p))
  22736. (org-show-context 'mark-goto)))
  22737. (eval-after-load "simple"
  22738. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22739. "Make the point visible with `org-show-context'."
  22740. (org-mark-jump-unhide)))
  22741. (eval-after-load "simple"
  22742. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22743. "Make the point visible with `org-show-context'."
  22744. (org-mark-jump-unhide)))
  22745. (eval-after-load "simple"
  22746. '(defadvice pop-global-mark (after org-make-visible activate)
  22747. "Make the point visible with `org-show-context'."
  22748. (org-mark-jump-unhide)))
  22749. ;; Make session.el ignore our circular variable
  22750. (defvar session-globals-exclude)
  22751. (eval-after-load "session"
  22752. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22753. ;;;; Finish up
  22754. (provide 'org)
  22755. (run-hooks 'org-load-hook)
  22756. ;;; org.el ends here