org.el 959 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306
  1. ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar-local org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. ;;;; Require other packages
  65. (require 'cl-lib)
  66. (eval-when-compile (require 'gnus-sum))
  67. (require 'calendar)
  68. (require 'find-func)
  69. (require 'format-spec)
  70. (or (equal this-command 'eval-buffer)
  71. (condition-case nil
  72. (load (concat (file-name-directory load-file-name)
  73. "org-loaddefs.el")
  74. nil t t t)
  75. (error
  76. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  77. (sit-for 3)
  78. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  79. (sit-for 3))))
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  83. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  84. ;;
  85. ;; In Org buffers, the value of `outline-regexp' is that of
  86. ;; `org-outline-regexp'. The only function still directly relying on
  87. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  88. ;; job when `orgstruct-mode' is active.
  89. (defvar org-outline-regexp "\\*+ "
  90. "Regexp to match Org headlines.")
  91. (defvar org-outline-regexp-bol "^\\*+ "
  92. "Regexp to match Org headlines.
  93. This is similar to `org-outline-regexp' but additionally makes
  94. sure that we are at the beginning of the line.")
  95. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  96. "Matches a headline, putting stars and text into groups.
  97. Stars are put in group 1 and the trimmed body in group 2.")
  98. (declare-function calendar-check-holidays "holidays" (date))
  99. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  100. (declare-function isearch-no-upper-case-p "isearch" (string regexp-flag))
  101. (declare-function org-add-archive-files "org-archive" (files))
  102. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  103. (declare-function org-agenda-list "org-agenda"
  104. (&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-clocktable-shift "org-clock" (dir n))
  118. (declare-function org-element-at-point "org-element" ())
  119. (declare-function org-element-cache-refresh "org-element" (pos))
  120. (declare-function org-element-cache-reset "org-element" (&optional all))
  121. (declare-function org-element-contents "org-element" (element))
  122. (declare-function org-element-context "org-element" (&optional element))
  123. (declare-function org-element-copy "org-element" (datum))
  124. (declare-function org-element-interpret-data "org-element" (data))
  125. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  126. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  127. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  128. (declare-function org-element-property "org-element" (property element))
  129. (declare-function org-element-put-property "org-element" (element property value))
  130. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  131. (declare-function org-element-type "org-element" (element))
  132. (declare-function org-element-update-syntax "org-element" ())
  133. (declare-function org-id-find-id-file "org-id" (id))
  134. (declare-function org-id-get-create "org-id" (&optional force))
  135. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  136. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  137. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  138. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  139. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  140. (declare-function org-table-align "org-table" ())
  141. (declare-function org-table-begin "org-table" (&optional table-type))
  142. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  143. (declare-function org-table-blank-field "org-table" ())
  144. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  145. (declare-function org-table-edit-field "org-table" (arg))
  146. (declare-function org-table-end "org-table" (&optional table-type))
  147. (declare-function org-table-end-of-field "org-table" (&optional n))
  148. (declare-function org-table-insert-row "org-table" (&optional arg))
  149. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  150. (declare-function org-table-maybe-eval-formula "org-table" ())
  151. (declare-function org-table-maybe-recalculate-line "org-table" ())
  152. (declare-function org-table-next-row "org-table" ())
  153. (declare-function org-table-paste-rectangle "org-table" ())
  154. (declare-function org-table-wrap-region "org-table" (arg))
  155. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  156. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  157. (declare-function orgtbl-mode "org-table" (&optional arg))
  158. (defsubst org-uniquify (list)
  159. "Non-destructively remove duplicate elements from LIST."
  160. (let ((res (copy-sequence list))) (delete-dups res)))
  161. (defsubst org-get-at-bol (property)
  162. "Get text property PROPERTY at the beginning of line."
  163. (get-text-property (point-at-bol) property))
  164. (defsubst org-trim (s)
  165. "Remove whitespace at the beginning and the end of string S."
  166. (replace-regexp-in-string
  167. "\\`[ \t\n\r]+" ""
  168. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  169. ;; load languages based on value of `org-babel-load-languages'
  170. (defvar org-babel-load-languages)
  171. ;;;###autoload
  172. (defun org-babel-do-load-languages (sym value)
  173. "Load the languages defined in `org-babel-load-languages'."
  174. (set-default sym value)
  175. (dolist (pair org-babel-load-languages)
  176. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  177. (if active
  178. (require (intern (concat "ob-" lang)))
  179. (funcall 'fmakunbound
  180. (intern (concat "org-babel-execute:" lang)))
  181. (funcall 'fmakunbound
  182. (intern (concat "org-babel-expand-body:" lang)))))))
  183. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  184. ;;;###autoload
  185. (defun org-babel-load-file (file &optional compile)
  186. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  187. This function exports the source code using `org-babel-tangle'
  188. and then loads the resulting file using `load-file'. With prefix
  189. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  190. file to byte-code before it is loaded."
  191. (interactive "fFile to load: \nP")
  192. (let* ((age (lambda (file)
  193. (float-time
  194. (time-subtract (current-time)
  195. (nth 5 (or (file-attributes (file-truename file))
  196. (file-attributes file)))))))
  197. (base-name (file-name-sans-extension file))
  198. (exported-file (concat base-name ".el")))
  199. ;; tangle if the org-mode file is newer than the elisp file
  200. (unless (and (file-exists-p exported-file)
  201. (> (funcall age file) (funcall age exported-file)))
  202. ;; Tangle-file traversal returns reversed list of tangled files
  203. ;; and we want to evaluate the first target.
  204. (setq exported-file
  205. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  206. (message "%s %s"
  207. (if compile
  208. (progn (byte-compile-file exported-file 'load)
  209. "Compiled and loaded")
  210. (progn (load-file exported-file) "Loaded"))
  211. exported-file)))
  212. (defcustom org-babel-load-languages '((emacs-lisp . t))
  213. "Languages which can be evaluated in Org-mode buffers.
  214. This list can be used to load support for any of the languages
  215. below, note that each language will depend on a different set of
  216. system executables and/or Emacs modes. When a language is
  217. \"loaded\", then code blocks in that language can be evaluated
  218. with `org-babel-execute-src-block' bound by default to C-c
  219. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  220. be set to remove code block evaluation from the C-c C-c
  221. keybinding. By default only Emacs Lisp (which has no
  222. requirements) is loaded."
  223. :group 'org-babel
  224. :set 'org-babel-do-load-languages
  225. :version "24.1"
  226. :type '(alist :tag "Babel Languages"
  227. :key-type
  228. (choice
  229. (const :tag "Awk" awk)
  230. (const :tag "C" C)
  231. (const :tag "R" R)
  232. (const :tag "Asymptote" asymptote)
  233. (const :tag "Calc" calc)
  234. (const :tag "Clojure" clojure)
  235. (const :tag "CSS" css)
  236. (const :tag "Ditaa" ditaa)
  237. (const :tag "Dot" dot)
  238. (const :tag "Emacs Lisp" emacs-lisp)
  239. (const :tag "Forth" forth)
  240. (const :tag "Fortran" fortran)
  241. (const :tag "Gnuplot" gnuplot)
  242. (const :tag "Haskell" haskell)
  243. (const :tag "IO" io)
  244. (const :tag "J" J)
  245. (const :tag "Java" java)
  246. (const :tag "Javascript" js)
  247. (const :tag "LaTeX" latex)
  248. (const :tag "Ledger" ledger)
  249. (const :tag "Lilypond" lilypond)
  250. (const :tag "Lisp" lisp)
  251. (const :tag "Makefile" makefile)
  252. (const :tag "Maxima" maxima)
  253. (const :tag "Matlab" matlab)
  254. (const :tag "Mscgen" mscgen)
  255. (const :tag "Ocaml" ocaml)
  256. (const :tag "Octave" octave)
  257. (const :tag "Org" org)
  258. (const :tag "Perl" perl)
  259. (const :tag "Pico Lisp" picolisp)
  260. (const :tag "PlantUML" plantuml)
  261. (const :tag "Python" python)
  262. (const :tag "Ruby" ruby)
  263. (const :tag "Sass" sass)
  264. (const :tag "Scala" scala)
  265. (const :tag "Scheme" scheme)
  266. (const :tag "Screen" screen)
  267. (const :tag "Shell Script" shell)
  268. (const :tag "Shen" shen)
  269. (const :tag "Sql" sql)
  270. (const :tag "Sqlite" sqlite)
  271. (const :tag "Stan" stan)
  272. (const :tag "ebnf2ps" ebnf2ps))
  273. :value-type (boolean :tag "Activate" :value t)))
  274. ;;;; Customization variables
  275. (defcustom org-clone-delete-id nil
  276. "Remove ID property of clones of a subtree.
  277. When non-nil, clones of a subtree don't inherit the ID property.
  278. Otherwise they inherit the ID property with a new unique
  279. identifier."
  280. :type 'boolean
  281. :version "24.1"
  282. :group 'org-id)
  283. ;;; Version
  284. (org-check-version)
  285. ;;;###autoload
  286. (defun org-version (&optional here full message)
  287. "Show the org-mode version.
  288. Interactively, or when MESSAGE is non-nil, show it in echo area.
  289. With prefix argument, or when HERE is non-nil, insert it at point.
  290. In non-interactive uses, a reduced version string is output unless
  291. FULL is given."
  292. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  293. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  294. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  295. (load-suffixes (list ".el"))
  296. (org-install-dir
  297. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  298. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  299. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  300. (let* ((load-suffixes save-load-suffixes)
  301. (release (org-release))
  302. (git-version (org-git-version))
  303. (version (format "Org-mode version %s (%s @ %s)"
  304. release
  305. git-version
  306. (if org-install-dir
  307. (if (string= org-dir org-install-dir)
  308. org-install-dir
  309. (concat "mixed installation! "
  310. org-install-dir
  311. " and "
  312. org-dir))
  313. "org-loaddefs.el can not be found!")))
  314. (version1 (if full version release)))
  315. (when here (insert version1))
  316. (when message (message "%s" version1))
  317. version1)))
  318. (defconst org-version (org-version))
  319. ;;; Syntax Constants
  320. ;;;; Block
  321. (defconst org-block-regexp
  322. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  323. "Regular expression for hiding blocks.")
  324. (defconst org-dblock-start-re
  325. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  326. "Matches the start line of a dynamic block, with parameters.")
  327. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  328. "Matches the end of a dynamic block.")
  329. ;;;; Clock and Planning
  330. (defconst org-clock-string "CLOCK:"
  331. "String used as prefix for timestamps clocking work hours on an item.")
  332. (defvar org-closed-string "CLOSED:"
  333. "String used as the prefix for timestamps logging closing a TODO entry.")
  334. (defvar org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].")
  339. (defvar org-scheduled-string "SCHEDULED:"
  340. "String to mark scheduled TODO entries.
  341. A schedule is this string, followed by a time stamp. Should be a word,
  342. terminated by a colon. You can insert a schedule keyword and
  343. a timestamp with \\[org-schedule].")
  344. (defconst org-ds-keyword-length
  345. (+ 2
  346. (apply #'max
  347. (mapcar #'length
  348. (list org-deadline-string org-scheduled-string
  349. org-clock-string org-closed-string))))
  350. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  351. (defconst org-planning-line-re
  352. (concat "^[ \t]*"
  353. (regexp-opt
  354. (list org-closed-string org-deadline-string org-scheduled-string)
  355. t))
  356. "Matches a line with planning info.
  357. Matched keyword is in group 1.")
  358. (defconst org-clock-line-re
  359. (concat "^[ \t]*" org-clock-string)
  360. "Matches a line with clock info.")
  361. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  362. "Matches the DEADLINE keyword.")
  363. (defconst org-deadline-time-regexp
  364. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  365. "Matches the DEADLINE keyword together with a time stamp.")
  366. (defconst org-deadline-time-hour-regexp
  367. (concat "\\<" org-deadline-string
  368. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  369. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  370. (defconst org-deadline-line-regexp
  371. (concat "\\<\\(" org-deadline-string "\\).*")
  372. "Matches the DEADLINE keyword and the rest of the line.")
  373. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  374. "Matches the SCHEDULED keyword.")
  375. (defconst org-scheduled-time-regexp
  376. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  377. "Matches the SCHEDULED keyword together with a time stamp.")
  378. (defconst org-scheduled-time-hour-regexp
  379. (concat "\\<" org-scheduled-string
  380. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  381. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  382. (defconst org-closed-time-regexp
  383. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  384. "Matches the CLOSED keyword together with a time stamp.")
  385. (defconst org-keyword-time-regexp
  386. (concat "\\<"
  387. (regexp-opt
  388. (list org-scheduled-string org-deadline-string org-closed-string
  389. org-clock-string)
  390. t)
  391. " *[[<]\\([^]>]+\\)[]>]")
  392. "Matches any of the 4 keywords, together with the time stamp.")
  393. (defconst org-keyword-time-not-clock-regexp
  394. (concat
  395. "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string) t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 3 keywords, together with the time stamp.")
  400. (defconst org-maybe-keyword-time-regexp
  401. (concat "\\(\\<"
  402. (regexp-opt
  403. (list org-scheduled-string org-deadline-string org-closed-string
  404. org-clock-string)
  405. t)
  406. "\\)?"
  407. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  408. "\\|"
  409. "<%%([^\r\n>]*>\\)")
  410. "Matches a timestamp, possibly preceded by a keyword.")
  411. (defconst org-all-time-keywords
  412. (mapcar (lambda (w) (substring w 0 -1))
  413. (list org-scheduled-string org-deadline-string
  414. org-clock-string org-closed-string))
  415. "List of time keywords.")
  416. ;;;; Drawer
  417. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  418. "Matches first or last line of a hidden block.
  419. Group 1 contains drawer's name or \"END\".")
  420. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  421. "Regular expression matching the first line of a property drawer.")
  422. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  423. "Regular expression matching the last line of a property drawer.")
  424. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  425. "Regular expression matching the first line of a clock drawer.")
  426. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  427. "Regular expression matching the last line of a clock drawer.")
  428. (defconst org-property-drawer-re
  429. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  430. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  431. "[ \t]*:END:[ \t]*$")
  432. "Matches an entire property drawer.")
  433. (defconst org-clock-drawer-re
  434. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  435. org-clock-drawer-end-re "\\)\n?")
  436. "Matches an entire clock drawer.")
  437. ;;;; Headline
  438. (defconst org-heading-keyword-regexp-format
  439. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  440. "Printf format for a regexp matching a headline with some keyword.
  441. This regexp will match the headline of any node which has the
  442. exact keyword that is put into the format. The keyword isn't in
  443. any group by default, but the stars and the body are.")
  444. (defconst org-heading-keyword-maybe-regexp-format
  445. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  446. "Printf format for a regexp matching a headline, possibly with some keyword.
  447. This regexp can match any headline with the specified keyword, or
  448. without a keyword. The keyword isn't in any group by default,
  449. but the stars and the body are.")
  450. (defconst org-archive-tag "ARCHIVE"
  451. "The tag that marks a subtree as archived.
  452. An archived subtree does not open during visibility cycling, and does
  453. not contribute to the agenda listings.")
  454. (defconst org-comment-string "COMMENT"
  455. "Entries starting with this keyword will never be exported.
  456. An entry can be toggled between COMMENT and normal with
  457. \\[org-toggle-comment].")
  458. ;;;; LaTeX Environments and Fragments
  459. (defconst org-latex-regexps
  460. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  461. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  462. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  463. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  464. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  465. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  466. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  467. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  468. "Regular expressions for matching embedded LaTeX.")
  469. ;;;; Node Property
  470. (defconst org-effort-property "Effort"
  471. "The property that is being used to keep track of effort estimates.
  472. Effort estimates given in this property need to have the format H:MM.")
  473. ;;;; Table
  474. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  475. "Detect an org-type or table-type table.")
  476. (defconst org-table-line-regexp "^[ \t]*|"
  477. "Detect an org-type table line.")
  478. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  479. "Detect an org-type table line.")
  480. (defconst org-table-hline-regexp "^[ \t]*|-"
  481. "Detect an org-type table hline.")
  482. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  483. "Detect a table-type table hline.")
  484. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  485. "Detect the first line outside a table when searching from within it.
  486. This works for both table types.")
  487. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  488. "Detect a #+TBLFM line.")
  489. ;;;; Timestamp
  490. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  491. "Regular expression for fast time stamp matching.")
  492. (defconst org-ts-regexp-inactive
  493. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  494. "Regular expression for fast inactive time stamp matching.")
  495. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  496. "Regular expression for fast time stamp matching.")
  497. (defconst org-ts-regexp0
  498. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  499. "Regular expression matching time strings for analysis.
  500. This one does not require the space after the date, so it can be used
  501. on a string that terminates immediately after the date.")
  502. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  503. "Regular expression matching time strings for analysis.")
  504. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  505. "Regular expression matching time stamps, with groups.")
  506. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  507. "Regular expression matching time stamps (also [..]), with groups.")
  508. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  509. "Regular expression matching a time stamp range.")
  510. (defconst org-tr-regexp-both
  511. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  512. "Regular expression matching a time stamp range.")
  513. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  514. org-ts-regexp "\\)?")
  515. "Regular expression matching a time stamp or time stamp range.")
  516. (defconst org-tsr-regexp-both
  517. (concat org-ts-regexp-both "\\(--?-?"
  518. org-ts-regexp-both "\\)?")
  519. "Regular expression matching a time stamp or time stamp range.
  520. The time stamps may be either active or inactive.")
  521. (defconst org-repeat-re
  522. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  523. "Regular expression for specifying repeated events.
  524. After a match, group 1 contains the repeat expression.")
  525. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  526. "Formats for `format-time-string' which are used for time stamps.")
  527. ;;; The custom variables
  528. (defgroup org nil
  529. "Outline-based notes management and organizer."
  530. :tag "Org"
  531. :group 'outlines
  532. :group 'calendar)
  533. (defcustom org-mode-hook nil
  534. "Mode hook for Org-mode, run after the mode was turned on."
  535. :group 'org
  536. :type 'hook)
  537. (defcustom org-load-hook nil
  538. "Hook that is run after org.el has been loaded."
  539. :group 'org
  540. :type 'hook)
  541. (defcustom org-log-buffer-setup-hook nil
  542. "Hook that is run after an Org log buffer is created."
  543. :group 'org
  544. :version "24.1"
  545. :type 'hook)
  546. (defvar org-modules) ; defined below
  547. (defvar org-modules-loaded nil
  548. "Have the modules been loaded already?")
  549. (defun org-load-modules-maybe (&optional force)
  550. "Load all extensions listed in `org-modules'."
  551. (when (or force (not org-modules-loaded))
  552. (dolist (ext org-modules)
  553. (condition-case nil (require ext)
  554. (error (message "Problems while trying to load feature `%s'" ext))))
  555. (setq org-modules-loaded t)))
  556. (defun org-set-modules (var value)
  557. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  558. (set var value)
  559. (when (featurep 'org)
  560. (org-load-modules-maybe 'force)
  561. (org-element-cache-reset 'all)))
  562. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  563. "Modules that should always be loaded together with org.el.
  564. If a description starts with <C>, the file is not part of Emacs
  565. and loading it will require that you have downloaded and properly
  566. installed the Org mode distribution.
  567. You can also use this system to load external packages (i.e. neither Org
  568. core modules, nor modules from the CONTRIB directory). Just add symbols
  569. to the end of the list. If the package is called org-xyz.el, then you need
  570. to add the symbol `xyz', and the package must have a call to:
  571. (provide \\='org-xyz)
  572. For export specific modules, see also `org-export-backends'."
  573. :group 'org
  574. :set 'org-set-modules
  575. :version "24.4"
  576. :package-version '(Org . "8.0")
  577. :type
  578. '(set :greedy t
  579. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  580. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  581. (const :tag " crypt: Encryption of subtrees" org-crypt)
  582. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  583. (const :tag " docview: Links to doc-view buffers" org-docview)
  584. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  585. (const :tag " habit: Track your consistency with habits" org-habit)
  586. (const :tag " id: Global IDs for identifying entries" org-id)
  587. (const :tag " info: Links to Info nodes" org-info)
  588. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  589. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  590. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  591. (const :tag " mouse: Additional mouse support" org-mouse)
  592. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  593. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  594. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  595. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  596. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  597. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  598. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  599. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  600. (const :tag "C collector: Collect properties into tables" org-collector)
  601. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  602. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  603. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  604. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  605. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  606. (const :tag "C eval: Include command output as text" org-eval)
  607. (const :tag "C eww: Store link to url of eww" org-eww)
  608. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  609. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  610. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  611. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  612. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  613. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  614. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  615. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  616. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  617. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  618. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  619. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  620. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  621. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  622. (const :tag "C registry: A registry for Org-mode links" org-registry)
  623. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  624. (const :tag "C secretary: Team management with org-mode" org-secretary)
  625. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  626. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  627. (const :tag "C track: Keep up with Org-mode development" org-track)
  628. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  629. (const :tag "C vm: Links to VM folders/messages" org-vm)
  630. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  631. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  632. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  633. (defvar org-export-registered-backends) ; From ox.el.
  634. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  635. (declare-function org-export-backend-name "ox" (backend) t)
  636. (defcustom org-export-backends '(ascii html icalendar latex odt)
  637. "List of export back-ends that should be always available.
  638. If a description starts with <C>, the file is not part of Emacs
  639. and loading it will require that you have downloaded and properly
  640. installed the Org mode distribution.
  641. Unlike to `org-modules', libraries in this list will not be
  642. loaded along with Org, but only once the export framework is
  643. needed.
  644. This variable needs to be set before org.el is loaded. If you
  645. need to make a change while Emacs is running, use the customize
  646. interface or run the following code, where VAL stands for the new
  647. value of the variable, after updating it:
  648. (progn
  649. (setq org-export-registered-backends
  650. (cl-remove-if-not
  651. (lambda (backend)
  652. (let ((name (org-export-backend-name backend)))
  653. (or (memq name val)
  654. (catch \\='parentp
  655. (dolist (b val)
  656. (and (org-export-derived-backend-p b name)
  657. (throw \\='parentp t)))))))
  658. org-export-registered-backends))
  659. (let ((new-list (mapcar #\\='org-export-backend-name
  660. org-export-registered-backends)))
  661. (dolist (backend val)
  662. (cond
  663. ((not (load (format \"ox-%s\" backend) t t))
  664. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  665. backend))
  666. ((not (memq backend new-list)) (push backend new-list))))
  667. (set-default \\='org-export-backends new-list)))
  668. Adding a back-end to this list will also pull the back-end it
  669. depends on, if any."
  670. :group 'org
  671. :group 'org-export
  672. :version "25.1"
  673. :package-version '(Org . "9.0")
  674. :initialize 'custom-initialize-set
  675. :set (lambda (var val)
  676. (if (not (featurep 'ox)) (set-default var val)
  677. ;; Any back-end not required anymore (not present in VAL and not
  678. ;; a parent of any back-end in the new value) is removed from the
  679. ;; list of registered back-ends.
  680. (setq org-export-registered-backends
  681. (cl-remove-if-not
  682. (lambda (backend)
  683. (let ((name (org-export-backend-name backend)))
  684. (or (memq name val)
  685. (catch 'parentp
  686. (dolist (b val)
  687. (and (org-export-derived-backend-p b name)
  688. (throw 'parentp t)))))))
  689. org-export-registered-backends))
  690. ;; Now build NEW-LIST of both new back-ends and required
  691. ;; parents.
  692. (let ((new-list (mapcar #'org-export-backend-name
  693. org-export-registered-backends)))
  694. (dolist (backend val)
  695. (cond
  696. ((not (load (format "ox-%s" backend) t t))
  697. (message "Problems while trying to load export back-end `%s'"
  698. backend))
  699. ((not (memq backend new-list)) (push backend new-list))))
  700. ;; Set VAR to that list with fixed dependencies.
  701. (set-default var new-list))))
  702. :type '(set :greedy t
  703. (const :tag " ascii Export buffer to ASCII format" ascii)
  704. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  705. (const :tag " html Export buffer to HTML format" html)
  706. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  707. (const :tag " latex Export buffer to LaTeX format" latex)
  708. (const :tag " man Export buffer to MAN format" man)
  709. (const :tag " md Export buffer to Markdown format" md)
  710. (const :tag " odt Export buffer to ODT format" odt)
  711. (const :tag " org Export buffer to Org format" org)
  712. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  713. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  714. (const :tag "C deck Export buffer to deck.js presentations" deck)
  715. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  716. (const :tag "C groff Export buffer to Groff format" groff)
  717. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  718. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  719. (const :tag "C s5 Export buffer to s5 presentations" s5)
  720. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  721. (eval-after-load 'ox
  722. '(dolist (backend org-export-backends)
  723. (condition-case nil (require (intern (format "ox-%s" backend)))
  724. (error (message "Problems while trying to load export back-end `%s'"
  725. backend)))))
  726. (defcustom org-support-shift-select nil
  727. "Non-nil means make shift-cursor commands select text when possible.
  728. \\<org-mode-map>\
  729. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  730. start selecting a region, or enlarge regions started in this way.
  731. In Org-mode, in special contexts, these same keys are used for
  732. other purposes, important enough to compete with shift selection.
  733. Org tries to balance these needs by supporting `shift-select-mode'
  734. outside these special contexts, under control of this variable.
  735. The default of this variable is nil, to avoid confusing behavior. Shifted
  736. cursor keys will then execute Org commands in the following contexts:
  737. - on a headline, changing TODO state (left/right) and priority (up/down)
  738. - on a time stamp, changing the time
  739. - in a plain list item, changing the bullet type
  740. - in a property definition line, switching between allowed values
  741. - in the BEGIN line of a clock table (changing the time block).
  742. Outside these contexts, the commands will throw an error.
  743. When this variable is t and the cursor is not in a special
  744. context, Org-mode will support shift-selection for making and
  745. enlarging regions. To make this more effective, the bullet
  746. cycling will no longer happen anywhere in an item line, but only
  747. if the cursor is exactly on the bullet.
  748. If you set this variable to the symbol `always', then the keys
  749. will not be special in headlines, property lines, and item lines,
  750. to make shift selection work there as well. If this is what you
  751. want, you can use the following alternative commands:
  752. `\\[org-todo]' and `\\[org-priority]' \
  753. to change TODO state and priority,
  754. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  755. can be used to switch TODO sets,
  756. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  757. and properties can be edited by hand or in column view.
  758. However, when the cursor is on a timestamp, shift-cursor commands
  759. will still edit the time stamp - this is just too good to give up."
  760. :group 'org
  761. :type '(choice
  762. (const :tag "Never" nil)
  763. (const :tag "When outside special context" t)
  764. (const :tag "Everywhere except timestamps" always)))
  765. (defcustom org-loop-over-headlines-in-active-region nil
  766. "Shall some commands act upon headlines in the active region?
  767. When set to t, some commands will be performed in all headlines
  768. within the active region.
  769. When set to `start-level', some commands will be performed in all
  770. headlines within the active region, provided that these headlines
  771. are of the same level than the first one.
  772. When set to a string, those commands will be performed on the
  773. matching headlines within the active region. Such string must be
  774. a tags/property/todo match as it is used in the agenda tags view.
  775. The list of commands is: `org-schedule', `org-deadline',
  776. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  777. `org-archive-to-archive-sibling'. The archiving commands skip
  778. already archived entries."
  779. :type '(choice (const :tag "Don't loop" nil)
  780. (const :tag "All headlines in active region" t)
  781. (const :tag "In active region, headlines at the same level than the first one" start-level)
  782. (string :tag "Tags/Property/Todo matcher"))
  783. :version "24.1"
  784. :group 'org-todo
  785. :group 'org-archive)
  786. (defgroup org-startup nil
  787. "Options concerning startup of Org-mode."
  788. :tag "Org Startup"
  789. :group 'org)
  790. (defcustom org-startup-folded t
  791. "Non-nil means entering Org-mode will switch to OVERVIEW.
  792. This can also be configured on a per-file basis by adding one of
  793. the following lines anywhere in the buffer:
  794. #+STARTUP: fold (or `overview', this is equivalent)
  795. #+STARTUP: nofold (or `showall', this is equivalent)
  796. #+STARTUP: content
  797. #+STARTUP: showeverything
  798. By default, this option is ignored when Org opens agenda files
  799. for the first time. If you want the agenda to honor the startup
  800. option, set `org-agenda-inhibit-startup' to nil."
  801. :group 'org-startup
  802. :type '(choice
  803. (const :tag "nofold: show all" nil)
  804. (const :tag "fold: overview" t)
  805. (const :tag "content: all headlines" content)
  806. (const :tag "show everything, even drawers" showeverything)))
  807. (defcustom org-startup-truncated t
  808. "Non-nil means entering Org-mode will set `truncate-lines'.
  809. This is useful since some lines containing links can be very long and
  810. uninteresting. Also tables look terrible when wrapped."
  811. :group 'org-startup
  812. :type 'boolean)
  813. (defcustom org-startup-indented nil
  814. "Non-nil means turn on `org-indent-mode' on startup.
  815. This can also be configured on a per-file basis by adding one of
  816. the following lines anywhere in the buffer:
  817. #+STARTUP: indent
  818. #+STARTUP: noindent"
  819. :group 'org-structure
  820. :type '(choice
  821. (const :tag "Not" nil)
  822. (const :tag "Globally (slow on startup in large files)" t)))
  823. (defcustom org-use-sub-superscripts t
  824. "Non-nil means interpret \"_\" and \"^\" for display.
  825. If you want to control how Org exports those characters, see
  826. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  827. used to be an alias for `org-export-with-sub-superscripts' in
  828. Org <8.0, it is not anymore.
  829. When this option is turned on, you can use TeX-like syntax for
  830. sub- and superscripts within the buffer. Several characters after
  831. \"_\" or \"^\" will be considered as a single item - so grouping
  832. with {} is normally not needed. For example, the following things
  833. will be parsed as single sub- or superscripts:
  834. 10^24 or 10^tau several digits will be considered 1 item.
  835. 10^-12 or 10^-tau a leading sign with digits or a word
  836. x^2-y^3 will be read as x^2 - y^3, because items are
  837. terminated by almost any nonword/nondigit char.
  838. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  839. Still, ambiguity is possible. So when in doubt, use {} to enclose
  840. the sub/superscript. If you set this variable to the symbol `{}',
  841. the braces are *required* in order to trigger interpretations as
  842. sub/superscript. This can be helpful in documents that need \"_\"
  843. frequently in plain text."
  844. :group 'org-startup
  845. :version "24.4"
  846. :package-version '(Org . "8.0")
  847. :type '(choice
  848. (const :tag "Always interpret" t)
  849. (const :tag "Only with braces" {})
  850. (const :tag "Never interpret" nil)))
  851. (defcustom org-startup-with-beamer-mode nil
  852. "Non-nil means turn on `org-beamer-mode' on startup.
  853. This can also be configured on a per-file basis by adding one of
  854. the following lines anywhere in the buffer:
  855. #+STARTUP: beamer"
  856. :group 'org-startup
  857. :version "24.1"
  858. :type 'boolean)
  859. (defcustom org-startup-align-all-tables nil
  860. "Non-nil means align all tables when visiting a file.
  861. This is useful when the column width in tables is forced with <N> cookies
  862. in table fields. Such tables will look correct only after the first re-align.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: align
  866. #+STARTUP: noalign"
  867. :group 'org-startup
  868. :type 'boolean)
  869. (defcustom org-startup-with-inline-images nil
  870. "Non-nil means show inline images when loading a new Org file.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: inlineimages
  874. #+STARTUP: noinlineimages"
  875. :group 'org-startup
  876. :version "24.1"
  877. :type 'boolean)
  878. (defcustom org-startup-with-latex-preview nil
  879. "Non-nil means preview LaTeX fragments when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: latexpreview
  883. #+STARTUP: nolatexpreview"
  884. :group 'org-startup
  885. :version "24.4"
  886. :package-version '(Org . "8.0")
  887. :type 'boolean)
  888. (defcustom org-insert-mode-line-in-empty-file nil
  889. "Non-nil means insert the first line setting Org-mode in empty files.
  890. When the function `org-mode' is called interactively in an empty file, this
  891. normally means that the file name does not automatically trigger Org-mode.
  892. To ensure that the file will always be in Org-mode in the future, a
  893. line enforcing Org-mode will be inserted into the buffer, if this option
  894. has been set."
  895. :group 'org-startup
  896. :type 'boolean)
  897. (defcustom org-replace-disputed-keys nil
  898. "Non-nil means use alternative key bindings for some keys.
  899. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  900. These keys are also used by other packages like shift-selection-mode'
  901. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  902. If you want to use Org-mode together with one of these other modes,
  903. or more generally if you would like to move some Org-mode commands to
  904. other keys, set this variable and configure the keys with the variable
  905. `org-disputed-keys'.
  906. This option is only relevant at load-time of Org-mode, and must be set
  907. *before* org.el is loaded. Changing it requires a restart of Emacs to
  908. become effective."
  909. :group 'org-startup
  910. :type 'boolean)
  911. (defcustom org-use-extra-keys nil
  912. "Non-nil means use extra key sequence definitions for certain commands.
  913. This happens automatically if `window-system' is nil. This
  914. variable lets you do the same manually. You must set it before
  915. loading org.
  916. Example: on Carbon Emacs 22 running graphically, with an external
  917. keyboard on a Powerbook, the default way of setting M-left might
  918. not work for either Alt or ESC. Setting this variable will make
  919. it work for ESC."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  923. (defcustom org-disputed-keys
  924. '(([(shift up)] . [(meta p)])
  925. ([(shift down)] . [(meta n)])
  926. ([(shift left)] . [(meta -)])
  927. ([(shift right)] . [(meta +)])
  928. ([(control shift right)] . [(meta shift +)])
  929. ([(control shift left)] . [(meta shift -)]))
  930. "Keys for which Org-mode and other modes compete.
  931. This is an alist, cars are the default keys, second element specifies
  932. the alternative to use when `org-replace-disputed-keys' is t.
  933. Keys can be specified in any syntax supported by `define-key'.
  934. The value of this option takes effect only at Org-mode's startup,
  935. therefore you'll have to restart Emacs to apply it after changing."
  936. :group 'org-startup
  937. :type 'alist)
  938. (defun org-key (key)
  939. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  940. Or return the original if not disputed."
  941. (when org-replace-disputed-keys
  942. (let* ((nkey (key-description key))
  943. (x (org-find-if (lambda (x)
  944. (equal (key-description (car x)) nkey))
  945. org-disputed-keys)))
  946. (setq key (if x (cdr x) key))))
  947. key)
  948. (defun org-find-if (predicate seq)
  949. (catch 'exit
  950. (while seq
  951. (if (funcall predicate (car seq))
  952. (throw 'exit (car seq))
  953. (pop seq)))))
  954. (defun org-defkey (keymap key def)
  955. "Define a key, possibly translated, as returned by `org-key'."
  956. (define-key keymap (org-key key) def))
  957. (defcustom org-ellipsis nil
  958. "The ellipsis to use in the Org-mode outline.
  959. When nil, just use the standard three dots.
  960. When a string, use that string instead.
  961. When a face, use the standard 3 dots, but with the specified face.
  962. The change affects only Org-mode (which will then use its own display table).
  963. Changing this requires executing \\[org-mode] in a buffer to become
  964. effective."
  965. :group 'org-startup
  966. :type '(choice (const :tag "Default" nil)
  967. (face :tag "Face" :value org-warning)
  968. (string :tag "String" :value "...#")))
  969. (defvar org-display-table nil
  970. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  971. (defgroup org-keywords nil
  972. "Keywords in Org-mode."
  973. :tag "Org Keywords"
  974. :group 'org)
  975. (defcustom org-closed-keep-when-no-todo nil
  976. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  977. :group 'org-todo
  978. :group 'org-keywords
  979. :version "24.4"
  980. :package-version '(Org . "8.0")
  981. :type 'boolean)
  982. (defgroup org-structure nil
  983. "Options concerning the general structure of Org-mode files."
  984. :tag "Org Structure"
  985. :group 'org)
  986. (defgroup org-reveal-location nil
  987. "Options about how to make context of a location visible."
  988. :tag "Org Reveal Location"
  989. :group 'org-structure)
  990. (defcustom org-show-context-detail '((agenda . local)
  991. (bookmark-jump . lineage)
  992. (isearch . lineage)
  993. (default . ancestors))
  994. "Alist between context and visibility span when revealing a location.
  995. \\<org-mode-map>Some actions may move point into invisible
  996. locations. As a consequence, Org always expose a neighborhood
  997. around point. How much is shown depends on the initial action,
  998. or context. Valid contexts are
  999. agenda when exposing an entry from the agenda
  1000. org-goto when using the command `org-goto' (\\[org-goto])
  1001. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1002. tags-tree when constructing a sparse tree based on tags matches
  1003. link-search when exposing search matches associated with a link
  1004. mark-goto when exposing the jump goal of a mark
  1005. bookmark-jump when exposing a bookmark location
  1006. isearch when exiting from an incremental search
  1007. default default for all contexts not set explicitly
  1008. Allowed visibility spans are
  1009. minimal show current headline; if point is not on headline,
  1010. also show entry
  1011. local show current headline, entry and next headline
  1012. ancestors show current headline and its direct ancestors; if
  1013. point is not on headline, also show entry
  1014. lineage show current headline, its direct ancestors and all
  1015. their children; if point is not on headline, also show
  1016. entry and first child
  1017. tree show current headline, its direct ancestors and all
  1018. their children; if point is not on headline, also show
  1019. entry and all children
  1020. canonical show current headline, its direct ancestors along with
  1021. their entries and children; if point is not located on
  1022. the headline, also show current entry and all children
  1023. As special cases, a nil or t value means show all contexts in
  1024. `minimal' or `canonical' view, respectively.
  1025. Some views can make displayed information very compact, but also
  1026. make it harder to edit the location of the match. In such
  1027. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1028. more context."
  1029. :group 'org-reveal-location
  1030. :version "25.2"
  1031. :package-version '(Org . "9.0")
  1032. :type '(choice
  1033. (const :tag "Canonical" t)
  1034. (const :tag "Minimal" nil)
  1035. (repeat :greedy t :tag "Individual contexts"
  1036. (cons
  1037. (choice :tag "Context"
  1038. (const agenda)
  1039. (const org-goto)
  1040. (const occur-tree)
  1041. (const tags-tree)
  1042. (const link-search)
  1043. (const mark-goto)
  1044. (const bookmark-jump)
  1045. (const isearch)
  1046. (const default))
  1047. (choice :tag "Detail level"
  1048. (const minimal)
  1049. (const local)
  1050. (const ancestors)
  1051. (const lineage)
  1052. (const tree)
  1053. (const canonical))))))
  1054. (defcustom org-indirect-buffer-display 'other-window
  1055. "How should indirect tree buffers be displayed?
  1056. This applies to indirect buffers created with the commands
  1057. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1058. Valid values are:
  1059. current-window Display in the current window
  1060. other-window Just display in another window.
  1061. dedicated-frame Create one new frame, and re-use it each time.
  1062. new-frame Make a new frame each time. Note that in this case
  1063. previously-made indirect buffers are kept, and you need to
  1064. kill these buffers yourself."
  1065. :group 'org-structure
  1066. :group 'org-agenda-windows
  1067. :type '(choice
  1068. (const :tag "In current window" current-window)
  1069. (const :tag "In current frame, other window" other-window)
  1070. (const :tag "Each time a new frame" new-frame)
  1071. (const :tag "One dedicated frame" dedicated-frame)))
  1072. (defcustom org-use-speed-commands nil
  1073. "Non-nil means activate single letter commands at beginning of a headline.
  1074. This may also be a function to test for appropriate locations where speed
  1075. commands should be active.
  1076. For example, to activate speed commands when the point is on any
  1077. star at the beginning of the headline, you can do this:
  1078. (setq org-use-speed-commands
  1079. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1080. :group 'org-structure
  1081. :type '(choice
  1082. (const :tag "Never" nil)
  1083. (const :tag "At beginning of headline stars" t)
  1084. (function)))
  1085. (defcustom org-speed-commands-user nil
  1086. "Alist of additional speed commands.
  1087. This list will be checked before `org-speed-commands-default'
  1088. when the variable `org-use-speed-commands' is non-nil
  1089. and when the cursor is at the beginning of a headline.
  1090. The car if each entry is a string with a single letter, which must
  1091. be assigned to `self-insert-command' in the global map.
  1092. The cdr is either a command to be called interactively, a function
  1093. to be called, or a form to be evaluated.
  1094. An entry that is just a list with a single string will be interpreted
  1095. as a descriptive headline that will be added when listing the speed
  1096. commands in the Help buffer using the `?' speed command."
  1097. :group 'org-structure
  1098. :type '(repeat :value ("k" . ignore)
  1099. (choice :value ("k" . ignore)
  1100. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1101. (cons :tag "Letter and Command"
  1102. (string :tag "Command letter")
  1103. (choice
  1104. (function)
  1105. (sexp))))))
  1106. (defcustom org-bookmark-names-plist
  1107. '(:last-capture "org-capture-last-stored"
  1108. :last-refile "org-refile-last-stored"
  1109. :last-capture-marker "org-capture-last-stored-marker")
  1110. "Names for bookmarks automatically set by some Org commands.
  1111. This can provide strings as names for a number of bookmarks Org sets
  1112. automatically. The following keys are currently implemented:
  1113. :last-capture
  1114. :last-capture-marker
  1115. :last-refile
  1116. When a key does not show up in the property list, the corresponding bookmark
  1117. is not set."
  1118. :group 'org-structure
  1119. :type 'plist)
  1120. (defgroup org-cycle nil
  1121. "Options concerning visibility cycling in Org-mode."
  1122. :tag "Org Cycle"
  1123. :group 'org-structure)
  1124. (defcustom org-cycle-skip-children-state-if-no-children t
  1125. "Non-nil means skip CHILDREN state in entries that don't have any."
  1126. :group 'org-cycle
  1127. :type 'boolean)
  1128. (defcustom org-cycle-max-level nil
  1129. "Maximum level which should still be subject to visibility cycling.
  1130. Levels higher than this will, for cycling, be treated as text, not a headline.
  1131. When `org-odd-levels-only' is set, a value of N in this variable actually
  1132. means 2N-1 stars as the limiting headline.
  1133. When nil, cycle all levels.
  1134. Note that the limiting level of cycling is also influenced by
  1135. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1136. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1137. than its value."
  1138. :group 'org-cycle
  1139. :type '(choice
  1140. (const :tag "No limit" nil)
  1141. (integer :tag "Maximum level")))
  1142. (defcustom org-hide-block-startup nil
  1143. "Non-nil means entering Org-mode will fold all blocks.
  1144. This can also be set in on a per-file basis with
  1145. #+STARTUP: hideblocks
  1146. #+STARTUP: showblocks"
  1147. :group 'org-startup
  1148. :group 'org-cycle
  1149. :type 'boolean)
  1150. (defcustom org-cycle-global-at-bob nil
  1151. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1152. This makes it possible to do global cycling without having to use S-TAB or
  1153. \\[universal-argument] TAB. For this special case to work, the first line
  1154. of the buffer must not be a headline -- it may be empty or some other text.
  1155. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1156. sure the cursor stays at the beginning of the buffer. When this option is
  1157. nil, don't do anything special at the beginning of the buffer."
  1158. :group 'org-cycle
  1159. :type 'boolean)
  1160. (defcustom org-cycle-level-after-item/entry-creation t
  1161. "Non-nil means cycle entry level or item indentation in new empty entries.
  1162. When the cursor is at the end of an empty headline, i.e., with only stars
  1163. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1164. and then all possible ancestor states, before returning to the original state.
  1165. This makes data entry extremely fast: M-RET to create a new headline,
  1166. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1167. When the cursor is at the end of an empty plain list item, one TAB will
  1168. make it a subitem, two or more tabs will back up to make this an item
  1169. higher up in the item hierarchy."
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-emulate-tab t
  1173. "Where should `org-cycle' emulate TAB.
  1174. nil Never
  1175. white Only in completely white lines
  1176. whitestart Only at the beginning of lines, before the first non-white char
  1177. t Everywhere except in headlines
  1178. exc-hl-bol Everywhere except at the start of a headline
  1179. If TAB is used in a place where it does not emulate TAB, the current subtree
  1180. visibility is cycled."
  1181. :group 'org-cycle
  1182. :type '(choice (const :tag "Never" nil)
  1183. (const :tag "Only in completely white lines" white)
  1184. (const :tag "Before first char in a line" whitestart)
  1185. (const :tag "Everywhere except in headlines" t)
  1186. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1187. (defcustom org-cycle-separator-lines 2
  1188. "Number of empty lines needed to keep an empty line between collapsed trees.
  1189. If you leave an empty line between the end of a subtree and the following
  1190. headline, this empty line is hidden when the subtree is folded.
  1191. Org-mode will leave (exactly) one empty line visible if the number of
  1192. empty lines is equal or larger to the number given in this variable.
  1193. So the default 2 means at least 2 empty lines after the end of a subtree
  1194. are needed to produce free space between a collapsed subtree and the
  1195. following headline.
  1196. If the number is negative, and the number of empty lines is at least -N,
  1197. all empty lines are shown.
  1198. Special case: when 0, never leave empty lines in collapsed view."
  1199. :group 'org-cycle
  1200. :type 'integer)
  1201. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1202. (defcustom org-pre-cycle-hook nil
  1203. "Hook that is run before visibility cycling is happening.
  1204. The function(s) in this hook must accept a single argument which indicates
  1205. the new state that will be set right after running this hook. The
  1206. argument is a symbol. Before a global state change, it can have the values
  1207. `overview', `content', or `all'. Before a local state change, it can have
  1208. the values `folded', `children', or `subtree'."
  1209. :group 'org-cycle
  1210. :type 'hook)
  1211. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1212. org-cycle-hide-drawers
  1213. org-cycle-show-empty-lines
  1214. org-optimize-window-after-visibility-change)
  1215. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1216. The function(s) in this hook must accept a single argument which indicates
  1217. the new state that was set by the most recent `org-cycle' command. The
  1218. argument is a symbol. After a global state change, it can have the values
  1219. `overview', `contents', or `all'. After a local state change, it can have
  1220. the values `folded', `children', or `subtree'."
  1221. :group 'org-cycle
  1222. :type 'hook
  1223. :version "25.1"
  1224. :package-version '(Org . "8.3"))
  1225. (defgroup org-edit-structure nil
  1226. "Options concerning structure editing in Org-mode."
  1227. :tag "Org Edit Structure"
  1228. :group 'org-structure)
  1229. (defcustom org-odd-levels-only nil
  1230. "Non-nil means skip even levels and only use odd levels for the outline.
  1231. This has the effect that two stars are being added/taken away in
  1232. promotion/demotion commands. It also influences how levels are
  1233. handled by the exporters.
  1234. Changing it requires restart of `font-lock-mode' to become effective
  1235. for fontification also in regions already fontified.
  1236. You may also set this on a per-file basis by adding one of the following
  1237. lines to the buffer:
  1238. #+STARTUP: odd
  1239. #+STARTUP: oddeven"
  1240. :group 'org-edit-structure
  1241. :group 'org-appearance
  1242. :type 'boolean)
  1243. (defcustom org-adapt-indentation t
  1244. "Non-nil means adapt indentation to outline node level.
  1245. When this variable is set, Org assumes that you write outlines by
  1246. indenting text in each node to align with the headline (after the
  1247. stars). The following issues are influenced by this variable:
  1248. - The indentation is increased by one space in a demotion
  1249. command, and decreased by one in a promotion command. However,
  1250. in the latter case, if shifting some line in the entry body
  1251. would alter document structure (e.g., insert a new headline),
  1252. indentation is not changed at all.
  1253. - Property drawers and planning information is inserted indented
  1254. when this variable is set. When nil, they will not be indented.
  1255. - TAB indents a line relative to current level. The lines below
  1256. a headline will be indented when this variable is set.
  1257. Note that this is all about true indentation, by adding and
  1258. removing space characters. See also `org-indent.el' which does
  1259. level-dependent indentation in a virtual way, i.e. at display
  1260. time in Emacs."
  1261. :group 'org-edit-structure
  1262. :type 'boolean)
  1263. (defcustom org-special-ctrl-a/e nil
  1264. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1265. When t, `C-a' will bring back the cursor to the beginning of the
  1266. headline text, i.e. after the stars and after a possible TODO
  1267. keyword. In an item, this will be the position after bullet and
  1268. check-box, if any. When the cursor is already at that position,
  1269. another `C-a' will bring it to the beginning of the line.
  1270. `C-e' will jump to the end of the headline, ignoring the presence
  1271. of tags in the headline. A second `C-e' will then jump to the
  1272. true end of the line, after any tags. This also means that, when
  1273. this variable is non-nil, `C-e' also will never jump beyond the
  1274. end of the heading of a folded section, i.e. not after the
  1275. ellipses.
  1276. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1277. normally, going to the true line boundary first. Only a directly
  1278. following, identical keypress will bring the cursor to the
  1279. special positions.
  1280. This may also be a cons cell where the behavior for `C-a' and
  1281. `C-e' is set separately."
  1282. :group 'org-edit-structure
  1283. :type '(choice
  1284. (const :tag "off" nil)
  1285. (const :tag "on: after stars/bullet and before tags first" t)
  1286. (const :tag "reversed: true line boundary first" reversed)
  1287. (cons :tag "Set C-a and C-e separately"
  1288. (choice :tag "Special C-a"
  1289. (const :tag "off" nil)
  1290. (const :tag "on: after stars/bullet first" t)
  1291. (const :tag "reversed: before stars/bullet first" reversed))
  1292. (choice :tag "Special C-e"
  1293. (const :tag "off" nil)
  1294. (const :tag "on: before tags first" t)
  1295. (const :tag "reversed: after tags first" reversed)))))
  1296. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1297. (defcustom org-special-ctrl-k nil
  1298. "Non-nil means `C-k' will behave specially in headlines.
  1299. When nil, `C-k' will call the default `kill-line' command.
  1300. When t, the following will happen while the cursor is in the headline:
  1301. - When the cursor is at the beginning of a headline, kill the entire
  1302. line and possible the folded subtree below the line.
  1303. - When in the middle of the headline text, kill the headline up to the tags.
  1304. - When after the headline text, kill the tags."
  1305. :group 'org-edit-structure
  1306. :type 'boolean)
  1307. (defcustom org-ctrl-k-protect-subtree nil
  1308. "Non-nil means, do not delete a hidden subtree with C-k.
  1309. When set to the symbol `error', simply throw an error when C-k is
  1310. used to kill (part-of) a headline that has hidden text behind it.
  1311. Any other non-nil value will result in a query to the user, if it is
  1312. OK to kill that hidden subtree. When nil, kill without remorse."
  1313. :group 'org-edit-structure
  1314. :version "24.1"
  1315. :type '(choice
  1316. (const :tag "Do not protect hidden subtrees" nil)
  1317. (const :tag "Protect hidden subtrees with a security query" t)
  1318. (const :tag "Never kill a hidden subtree with C-k" error)))
  1319. (defcustom org-special-ctrl-o t
  1320. "Non-nil means, make `C-o' insert a row in tables."
  1321. :group 'org-edit-structure
  1322. :type 'boolean)
  1323. (defcustom org-catch-invisible-edits nil
  1324. "Check if in invisible region before inserting or deleting a character.
  1325. Valid values are:
  1326. nil Do not check, so just do invisible edits.
  1327. error Throw an error and do nothing.
  1328. show Make point visible, and do the requested edit.
  1329. show-and-error Make point visible, then throw an error and abort the edit.
  1330. smart Make point visible, and do insertion/deletion if it is
  1331. adjacent to visible text and the change feels predictable.
  1332. Never delete a previously invisible character or add in the
  1333. middle or right after an invisible region. Basically, this
  1334. allows insertion and backward-delete right before ellipses.
  1335. FIXME: maybe in this case we should not even show?"
  1336. :group 'org-edit-structure
  1337. :version "24.1"
  1338. :type '(choice
  1339. (const :tag "Do not check" nil)
  1340. (const :tag "Throw error when trying to edit" error)
  1341. (const :tag "Unhide, but do not do the edit" show-and-error)
  1342. (const :tag "Show invisible part and do the edit" show)
  1343. (const :tag "Be smart and do the right thing" smart)))
  1344. (defcustom org-yank-folded-subtrees t
  1345. "Non-nil means when yanking subtrees, fold them.
  1346. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1347. it starts with a heading and all other headings in it are either children
  1348. or siblings, then fold all the subtrees. However, do this only if no
  1349. text after the yank would be swallowed into a folded tree by this action."
  1350. :group 'org-edit-structure
  1351. :type 'boolean)
  1352. (defcustom org-yank-adjusted-subtrees nil
  1353. "Non-nil means when yanking subtrees, adjust the level.
  1354. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1355. this function for details."
  1356. :group 'org-edit-structure
  1357. :type 'boolean)
  1358. (defcustom org-M-RET-may-split-line '((default . t))
  1359. "Non-nil means M-RET will split the line at the cursor position.
  1360. When nil, it will go to the end of the line before making a
  1361. new line.
  1362. You may also set this option in a different way for different
  1363. contexts. Valid contexts are:
  1364. headline when creating a new headline
  1365. item when creating a new item
  1366. table in a table field
  1367. default the value to be used for all contexts not explicitly
  1368. customized"
  1369. :group 'org-structure
  1370. :group 'org-table
  1371. :type '(choice
  1372. (const :tag "Always" t)
  1373. (const :tag "Never" nil)
  1374. (repeat :greedy t :tag "Individual contexts"
  1375. (cons
  1376. (choice :tag "Context"
  1377. (const headline)
  1378. (const item)
  1379. (const table)
  1380. (const default))
  1381. (boolean)))))
  1382. (defcustom org-insert-heading-respect-content nil
  1383. "Non-nil means insert new headings after the current subtree.
  1384. When nil, the new heading is created directly after the current line.
  1385. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1386. this variable on for the duration of the command."
  1387. :group 'org-structure
  1388. :type 'boolean)
  1389. (defcustom org-blank-before-new-entry '((heading . auto)
  1390. (plain-list-item . auto))
  1391. "Should `org-insert-heading' leave a blank line before new heading/item?
  1392. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1393. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1394. which case Org will look at the surrounding headings/items and try to
  1395. make an intelligent decision whether to insert a blank line or not."
  1396. :group 'org-edit-structure
  1397. :type '(list
  1398. (cons (const heading)
  1399. (choice (const :tag "Never" nil)
  1400. (const :tag "Always" t)
  1401. (const :tag "Auto" auto)))
  1402. (cons (const plain-list-item)
  1403. (choice (const :tag "Never" nil)
  1404. (const :tag "Always" t)
  1405. (const :tag "Auto" auto)))))
  1406. (defcustom org-insert-heading-hook nil
  1407. "Hook being run after inserting a new heading."
  1408. :group 'org-edit-structure
  1409. :type 'hook)
  1410. (defcustom org-enable-fixed-width-editor t
  1411. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1412. This currently only means they are never auto-wrapped.
  1413. When nil, such lines will be treated like ordinary lines."
  1414. :group 'org-edit-structure
  1415. :type 'boolean)
  1416. (defcustom org-goto-auto-isearch t
  1417. "Non-nil means typing characters in `org-goto' starts incremental search.
  1418. When nil, you can use these keybindings to navigate the buffer:
  1419. q Quit the org-goto interface
  1420. n Go to the next visible heading
  1421. p Go to the previous visible heading
  1422. f Go one heading forward on same level
  1423. b Go one heading backward on same level
  1424. u Go one heading up"
  1425. :group 'org-edit-structure
  1426. :type 'boolean)
  1427. (defgroup org-sparse-trees nil
  1428. "Options concerning sparse trees in Org-mode."
  1429. :tag "Org Sparse Trees"
  1430. :group 'org-structure)
  1431. (defcustom org-highlight-sparse-tree-matches t
  1432. "Non-nil means highlight all matches that define a sparse tree.
  1433. The highlights will automatically disappear the next time the buffer is
  1434. changed by an edit command."
  1435. :group 'org-sparse-trees
  1436. :type 'boolean)
  1437. (defcustom org-remove-highlights-with-change t
  1438. "Non-nil means any change to the buffer will remove temporary highlights.
  1439. \\<org-mode-map>\
  1440. Such highlights are created by `org-occur' and `org-clock-display'.
  1441. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1442. to get rid of the highlights.
  1443. The highlights created by `org-toggle-latex-fragment' always need
  1444. `\\[org-toggle-latex-fragment]' to be removed."
  1445. :group 'org-sparse-trees
  1446. :group 'org-time
  1447. :type 'boolean)
  1448. (defcustom org-occur-case-fold-search t
  1449. "Non-nil means `org-occur' should be case-insensitive.
  1450. If set to `smart' the search will be case-insensitive only if it
  1451. doesn't specify any upper case character."
  1452. :group 'org-sparse-trees
  1453. :version "25.1"
  1454. :type '(choice
  1455. (const :tag "Case-sensitive" nil)
  1456. (const :tag "Case-insensitive" t)
  1457. (const :tag "Case-insensitive for lower case searches only" 'smart)))
  1458. (defcustom org-occur-hook '(org-first-headline-recenter)
  1459. "Hook that is run after `org-occur' has constructed a sparse tree.
  1460. This can be used to recenter the window to show as much of the structure
  1461. as possible."
  1462. :group 'org-sparse-trees
  1463. :type 'hook)
  1464. (defgroup org-imenu-and-speedbar nil
  1465. "Options concerning imenu and speedbar in Org-mode."
  1466. :tag "Org Imenu and Speedbar"
  1467. :group 'org-structure)
  1468. (defcustom org-imenu-depth 2
  1469. "The maximum level for Imenu access to Org-mode headlines.
  1470. This also applied for speedbar access."
  1471. :group 'org-imenu-and-speedbar
  1472. :type 'integer)
  1473. (defgroup org-table nil
  1474. "Options concerning tables in Org-mode."
  1475. :tag "Org Table"
  1476. :group 'org)
  1477. (defcustom org-enable-table-editor 'optimized
  1478. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1479. When nil, such lines will be treated like ordinary lines.
  1480. When equal to the symbol `optimized', the table editor will be optimized to
  1481. do the following:
  1482. - Automatic overwrite mode in front of whitespace in table fields.
  1483. This makes the structure of the table stay in tact as long as the edited
  1484. field does not exceed the column width.
  1485. - Minimize the number of realigns. Normally, the table is aligned each time
  1486. TAB or RET are pressed to move to another field. With optimization this
  1487. happens only if changes to a field might have changed the column width.
  1488. Optimization requires replacing the functions `self-insert-command',
  1489. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1490. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1491. very good at guessing when a re-align will be necessary, but you can always
  1492. force one with \\[org-ctrl-c-ctrl-c].
  1493. If you would like to use the optimized version in Org-mode, but the
  1494. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1495. This variable can be used to turn on and off the table editor during a session,
  1496. but in order to toggle optimization, a restart is required.
  1497. See also the variable `org-table-auto-blank-field'."
  1498. :group 'org-table
  1499. :type '(choice
  1500. (const :tag "off" nil)
  1501. (const :tag "on" t)
  1502. (const :tag "on, optimized" optimized)))
  1503. (defcustom org-self-insert-cluster-for-undo nil
  1504. "Non-nil means cluster self-insert commands for undo when possible.
  1505. If this is set, then, like in the Emacs command loop, 20 consecutive
  1506. characters will be undone together.
  1507. This is configurable, because there is some impact on typing performance."
  1508. :group 'org-table
  1509. :type 'boolean)
  1510. (defcustom org-table-tab-recognizes-table.el t
  1511. "Non-nil means TAB will automatically notice a table.el table.
  1512. When it sees such a table, it moves point into it and - if necessary -
  1513. calls `table-recognize-table'."
  1514. :group 'org-table-editing
  1515. :type 'boolean)
  1516. (defgroup org-link nil
  1517. "Options concerning links in Org-mode."
  1518. :tag "Org Link"
  1519. :group 'org)
  1520. (defvar-local org-link-abbrev-alist-local nil
  1521. "Buffer-local version of `org-link-abbrev-alist', which see.
  1522. The value of this is taken from the #+LINK lines.")
  1523. (defcustom org-link-abbrev-alist nil
  1524. "Alist of link abbreviations.
  1525. The car of each element is a string, to be replaced at the start of a link.
  1526. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1527. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1528. [[linkkey:tag][description]]
  1529. The `linkkey' must be a word word, starting with a letter, followed
  1530. by letters, numbers, `-' or `_'.
  1531. If REPLACE is a string, the tag will simply be appended to create the link.
  1532. If the string contains \"%s\", the tag will be inserted there. If the string
  1533. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1534. at that point (see the function `url-hexify-string'). If the string contains
  1535. the specifier \"%(my-function)\", then the custom function `my-function' will
  1536. be invoked: this function takes the tag as its only argument and must return
  1537. a string.
  1538. REPLACE may also be a function that will be called with the tag as the
  1539. only argument to create the link, which should be returned as a string.
  1540. See the manual for examples."
  1541. :group 'org-link
  1542. :type '(repeat
  1543. (cons
  1544. (string :tag "Protocol")
  1545. (choice
  1546. (string :tag "Format")
  1547. (function)))))
  1548. (defcustom org-descriptive-links t
  1549. "Non-nil means Org will display descriptive links.
  1550. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1551. \"Org Website\", hiding the link itself and just displaying its
  1552. description. When set to nil, Org will display the full links
  1553. literally.
  1554. You can interactively set the value of this variable by calling
  1555. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1556. :group 'org-link
  1557. :type 'boolean)
  1558. (defcustom org-link-file-path-type 'adaptive
  1559. "How the path name in file links should be stored.
  1560. Valid values are:
  1561. relative Relative to the current directory, i.e. the directory of the file
  1562. into which the link is being inserted.
  1563. absolute Absolute path, if possible with ~ for home directory.
  1564. noabbrev Absolute path, no abbreviation of home directory.
  1565. adaptive Use relative path for files in the current directory and sub-
  1566. directories of it. For other files, use an absolute path."
  1567. :group 'org-link
  1568. :type '(choice
  1569. (const relative)
  1570. (const absolute)
  1571. (const noabbrev)
  1572. (const adaptive)))
  1573. (defvaralias 'org-activate-links 'org-highlight-links)
  1574. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1575. "Types of links that should be highlighted in Org-mode files.
  1576. This is a list of symbols, each one of them leading to the
  1577. highlighting of a certain link type.
  1578. You can still open links that are not highlighted.
  1579. In principle, it does not hurt to turn on highlighting for all
  1580. link types. There may be a small gain when turning off unused
  1581. link types. The types are:
  1582. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1583. angle Links in angular brackets that may contain whitespace like
  1584. <bbdb:Carsten Dominik>.
  1585. plain Plain links in normal text, no whitespace, like http://google.com.
  1586. radio Text that is matched by a radio target, see manual for details.
  1587. tag Tag settings in a headline (link to tag search).
  1588. date Time stamps (link to calendar).
  1589. footnote Footnote labels.
  1590. If you set this variable during an Emacs session, use `org-mode-restart'
  1591. in the Org buffer so that the change takes effect."
  1592. :group 'org-link
  1593. :group 'org-appearance
  1594. :type '(set :greedy t
  1595. (const :tag "Double bracket links" bracket)
  1596. (const :tag "Angular bracket links" angle)
  1597. (const :tag "Plain text links" plain)
  1598. (const :tag "Radio target matches" radio)
  1599. (const :tag "Tags" tag)
  1600. (const :tag "Timestamps" date)
  1601. (const :tag "Footnotes" footnote)))
  1602. (defcustom org-make-link-description-function nil
  1603. "Function to use for generating link descriptions from links.
  1604. When nil, the link location will be used. This function must take
  1605. two parameters: the first one is the link, the second one is the
  1606. description generated by `org-insert-link'. The function should
  1607. return the description to use."
  1608. :group 'org-link
  1609. :type '(choice (const nil) (function)))
  1610. (defgroup org-link-store nil
  1611. "Options concerning storing links in Org-mode."
  1612. :tag "Org Store Link"
  1613. :group 'org-link)
  1614. (defcustom org-url-hexify-p t
  1615. "When non-nil, hexify URL when creating a link."
  1616. :type 'boolean
  1617. :version "24.3"
  1618. :group 'org-link-store)
  1619. (defcustom org-email-link-description-format "Email %c: %.30s"
  1620. "Format of the description part of a link to an email or usenet message.
  1621. The following %-escapes will be replaced by corresponding information:
  1622. %F full \"From\" field
  1623. %f name, taken from \"From\" field, address if no name
  1624. %T full \"To\" field
  1625. %t first name in \"To\" field, address if no name
  1626. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1627. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1628. %s subject
  1629. %d date
  1630. %m message-id.
  1631. You may use normal field width specification between the % and the letter.
  1632. This is for example useful to limit the length of the subject.
  1633. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1634. :group 'org-link-store
  1635. :type 'string)
  1636. (defcustom org-from-is-user-regexp
  1637. (let (r1 r2)
  1638. (when (and user-mail-address (not (string= user-mail-address "")))
  1639. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1640. (when (and user-full-name (not (string= user-full-name "")))
  1641. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1642. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1643. "Regexp matched against the \"From:\" header of an email or usenet message.
  1644. It should match if the message is from the user him/herself."
  1645. :group 'org-link-store
  1646. :type 'regexp)
  1647. (defcustom org-context-in-file-links t
  1648. "Non-nil means file links from `org-store-link' contain context.
  1649. A search string will be added to the file name with :: as separator and
  1650. used to find the context when the link is activated by the command
  1651. `org-open-at-point'. When this option is t, the entire active region
  1652. will be placed in the search string of the file link. If set to a
  1653. positive integer, only the first n lines of context will be stored.
  1654. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1655. negates this setting for the duration of the command."
  1656. :group 'org-link-store
  1657. :type '(choice boolean integer))
  1658. (defcustom org-keep-stored-link-after-insertion nil
  1659. "Non-nil means keep link in list for entire session.
  1660. The command `org-store-link' adds a link pointing to the current
  1661. location to an internal list. These links accumulate during a session.
  1662. The command `org-insert-link' can be used to insert links into any
  1663. Org-mode file (offering completion for all stored links). When this
  1664. option is nil, every link which has been inserted once using \\[org-insert-link]
  1665. will be removed from the list, to make completing the unused links
  1666. more efficient."
  1667. :group 'org-link-store
  1668. :type 'boolean)
  1669. (defgroup org-link-follow nil
  1670. "Options concerning following links in Org-mode."
  1671. :tag "Org Follow Link"
  1672. :group 'org-link)
  1673. (defcustom org-link-translation-function nil
  1674. "Function to translate links with different syntax to Org syntax.
  1675. This can be used to translate links created for example by the Planner
  1676. or emacs-wiki packages to Org syntax.
  1677. The function must accept two parameters, a TYPE containing the link
  1678. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1679. which is everything after the link protocol. It should return a cons
  1680. with possibly modified values of type and path.
  1681. Org contains a function for this, so if you set this variable to
  1682. `org-translate-link-from-planner', you should be able follow many
  1683. links created by planner."
  1684. :group 'org-link-follow
  1685. :type '(choice (const nil) (function)))
  1686. (defcustom org-follow-link-hook nil
  1687. "Hook that is run after a link has been followed."
  1688. :group 'org-link-follow
  1689. :type 'hook)
  1690. (defcustom org-tab-follows-link nil
  1691. "Non-nil means on links TAB will follow the link.
  1692. Needs to be set before org.el is loaded.
  1693. This really should not be used, it does not make sense, and the
  1694. implementation is bad."
  1695. :group 'org-link-follow
  1696. :type 'boolean)
  1697. (defcustom org-return-follows-link nil
  1698. "Non-nil means on links RET will follow the link.
  1699. In tables, the special behavior of RET has precedence."
  1700. :group 'org-link-follow
  1701. :type 'boolean)
  1702. (defcustom org-mouse-1-follows-link
  1703. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1704. "Non-nil means mouse-1 on a link will follow the link.
  1705. A longer mouse click will still set point. Needs to be set
  1706. before org.el is loaded."
  1707. :group 'org-link-follow
  1708. :version "24.4"
  1709. :package-version '(Org . "8.3")
  1710. :type '(choice
  1711. (const :tag "A double click follows the link" double)
  1712. (const :tag "Unconditionally follow the link with mouse-1" t)
  1713. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1714. (defcustom org-mark-ring-length 4
  1715. "Number of different positions to be recorded in the ring.
  1716. Changing this requires a restart of Emacs to work correctly."
  1717. :group 'org-link-follow
  1718. :type 'integer)
  1719. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1720. "Non-nil means internal links in Org files must exactly match a headline.
  1721. When nil, the link search tries to match a phrase with all words
  1722. in the search text."
  1723. :group 'org-link-follow
  1724. :version "24.1"
  1725. :type '(choice
  1726. (const :tag "Use fuzzy text search" nil)
  1727. (const :tag "Match only exact headline" t)
  1728. (const :tag "Match exact headline or query to create it"
  1729. query-to-create)))
  1730. (defcustom org-link-frame-setup
  1731. '((vm . vm-visit-folder-other-frame)
  1732. (vm-imap . vm-visit-imap-folder-other-frame)
  1733. (gnus . org-gnus-no-new-news)
  1734. (file . find-file-other-window)
  1735. (wl . wl-other-frame))
  1736. "Setup the frame configuration for following links.
  1737. When following a link with Emacs, it may often be useful to display
  1738. this link in another window or frame. This variable can be used to
  1739. set this up for the different types of links.
  1740. For VM, use any of
  1741. `vm-visit-folder'
  1742. `vm-visit-folder-other-window'
  1743. `vm-visit-folder-other-frame'
  1744. For Gnus, use any of
  1745. `gnus'
  1746. `gnus-other-frame'
  1747. `org-gnus-no-new-news'
  1748. For FILE, use any of
  1749. `find-file'
  1750. `find-file-other-window'
  1751. `find-file-other-frame'
  1752. For Wanderlust use any of
  1753. `wl'
  1754. `wl-other-frame'
  1755. For the calendar, use the variable `calendar-setup'.
  1756. For BBDB, it is currently only possible to display the matches in
  1757. another window."
  1758. :group 'org-link-follow
  1759. :type '(list
  1760. (cons (const vm)
  1761. (choice
  1762. (const vm-visit-folder)
  1763. (const vm-visit-folder-other-window)
  1764. (const vm-visit-folder-other-frame)))
  1765. (cons (const vm-imap)
  1766. (choice
  1767. (const vm-visit-imap-folder)
  1768. (const vm-visit-imap-folder-other-window)
  1769. (const vm-visit-imap-folder-other-frame)))
  1770. (cons (const gnus)
  1771. (choice
  1772. (const gnus)
  1773. (const gnus-other-frame)
  1774. (const org-gnus-no-new-news)))
  1775. (cons (const file)
  1776. (choice
  1777. (const find-file)
  1778. (const find-file-other-window)
  1779. (const find-file-other-frame)))
  1780. (cons (const wl)
  1781. (choice
  1782. (const wl)
  1783. (const wl-other-frame)))))
  1784. (defcustom org-display-internal-link-with-indirect-buffer nil
  1785. "Non-nil means use indirect buffer to display infile links.
  1786. Activating internal links (from one location in a file to another location
  1787. in the same file) normally just jumps to the location. When the link is
  1788. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1789. is displayed in
  1790. another window. When this option is set, the other window actually displays
  1791. an indirect buffer clone of the current buffer, to avoid any visibility
  1792. changes to the current buffer."
  1793. :group 'org-link-follow
  1794. :type 'boolean)
  1795. (defcustom org-open-non-existing-files nil
  1796. "Non-nil means `org-open-file' will open non-existing files.
  1797. When nil, an error will be generated.
  1798. This variable applies only to external applications because they
  1799. might choke on non-existing files. If the link is to a file that
  1800. will be opened in Emacs, the variable is ignored."
  1801. :group 'org-link-follow
  1802. :type 'boolean)
  1803. (defcustom org-open-directory-means-index-dot-org nil
  1804. "Non-nil means a link to a directory really means to index.org.
  1805. When nil, following a directory link will run dired or open a finder/explorer
  1806. window on that directory."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1810. "Non-nil means ask for confirmation before executing shell links.
  1811. Shell links can be dangerous: just think about a link
  1812. [[shell:rm -rf ~/*][Google Search]]
  1813. This link would show up in your Org-mode document as \"Google Search\",
  1814. but really it would remove your entire home directory.
  1815. Therefore we advise against setting this variable to nil.
  1816. Just change it to `y-or-n-p' if you want to confirm with a
  1817. single keystroke rather than having to type \"yes\"."
  1818. :group 'org-link-follow
  1819. :type '(choice
  1820. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1821. (const :tag "with y-or-n (faster)" y-or-n-p)
  1822. (const :tag "no confirmation (dangerous)" nil)))
  1823. (put 'org-confirm-shell-link-function
  1824. 'safe-local-variable
  1825. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1826. (defcustom org-confirm-shell-link-not-regexp ""
  1827. "A regexp to skip confirmation for shell links."
  1828. :group 'org-link-follow
  1829. :version "24.1"
  1830. :type 'regexp)
  1831. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1832. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1833. Elisp links can be dangerous: just think about a link
  1834. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1835. This link would show up in your Org-mode document as \"Google Search\",
  1836. but really it would remove your entire home directory.
  1837. Therefore we advise against setting this variable to nil.
  1838. Just change it to `y-or-n-p' if you want to confirm with a
  1839. single keystroke rather than having to type \"yes\"."
  1840. :group 'org-link-follow
  1841. :type '(choice
  1842. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1843. (const :tag "with y-or-n (faster)" y-or-n-p)
  1844. (const :tag "no confirmation (dangerous)" nil)))
  1845. (put 'org-confirm-shell-link-function
  1846. 'safe-local-variable
  1847. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1848. (defcustom org-confirm-elisp-link-not-regexp ""
  1849. "A regexp to skip confirmation for Elisp links."
  1850. :group 'org-link-follow
  1851. :version "24.1"
  1852. :type 'regexp)
  1853. (defconst org-file-apps-defaults-gnu
  1854. '((remote . emacs)
  1855. (system . mailcap)
  1856. (t . mailcap))
  1857. "Default file applications on a UNIX or GNU/Linux system.
  1858. See `org-file-apps'.")
  1859. (defconst org-file-apps-defaults-macosx
  1860. '((remote . emacs)
  1861. (system . "open %s")
  1862. ("ps.gz" . "gv %s")
  1863. ("eps.gz" . "gv %s")
  1864. ("dvi" . "xdvi %s")
  1865. ("fig" . "xfig %s")
  1866. (t . "open %s"))
  1867. "Default file applications on a MacOS X system.
  1868. The system \"open\" is known as a default, but we use X11 applications
  1869. for some files for which the OS does not have a good default.
  1870. See `org-file-apps'.")
  1871. (defconst org-file-apps-defaults-windowsnt
  1872. (list '(remote . emacs)
  1873. (cons 'system (lambda (file _path)
  1874. (with-no-warnings (w32-shell-execute "open" file))))
  1875. (cons t (lambda (file _path)
  1876. (with-no-warnings (w32-shell-execute "open" file)))))
  1877. "Default file applications on a Windows NT system.
  1878. The system \"open\" is used for most files.
  1879. See `org-file-apps'.")
  1880. (defcustom org-file-apps
  1881. '((auto-mode . emacs)
  1882. ("\\.mm\\'" . default)
  1883. ("\\.x?html?\\'" . default)
  1884. ("\\.pdf\\'" . default))
  1885. "External applications for opening `file:path' items in a document.
  1886. \\<org-mode-map>\
  1887. Org mode uses system defaults for different file types, but
  1888. you can use this variable to set the application for a given file
  1889. extension. The entries in this list are cons cells where the car identifies
  1890. files and the cdr the corresponding command. Possible values for the
  1891. file identifier are
  1892. \"string\" A string as a file identifier can be interpreted in different
  1893. ways, depending on its contents:
  1894. - Alphanumeric characters only:
  1895. Match links with this file extension.
  1896. Example: (\"pdf\" . \"evince %s\")
  1897. to open PDFs with evince.
  1898. - Regular expression: Match links where the
  1899. filename matches the regexp. If you want to
  1900. use groups here, use shy groups.
  1901. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1902. \(\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1903. to open *.html and *.xhtml with firefox.
  1904. - Regular expression which contains (non-shy) groups:
  1905. Match links where the whole link, including \"::\", and
  1906. anything after that, matches the regexp.
  1907. In a custom command string, %1, %2, etc. are replaced with
  1908. the parts of the link that were matched by the groups.
  1909. For backwards compatibility, if a command string is given
  1910. that does not use any of the group matches, this case is
  1911. handled identically to the second one (i.e. match against
  1912. file name only).
  1913. In a custom function, you can access the group matches with
  1914. \(match-string n link).
  1915. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1916. to open [[file:document.pdf::5]] with evince at page 5.
  1917. `directory' Matches a directory
  1918. `remote' Matches a remote file, accessible through tramp or efs.
  1919. Remote files most likely should be visited through Emacs
  1920. because external applications cannot handle such paths.
  1921. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1922. so all files Emacs knows how to handle. Using this with
  1923. command `emacs' will open most files in Emacs. Beware that this
  1924. will also open html files inside Emacs, unless you add
  1925. \(\"html\" . default) to the list as well.
  1926. `system' The system command to open files, like `open' on Windows
  1927. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1928. that will be selected if you call \\[org-open-at-point] with a double
  1929. \\[universal-argument] \\[universal-argument] prefix.
  1930. t Default for files not matched by any of the other options.
  1931. Possible values for the command are:
  1932. `emacs' The file will be visited by the current Emacs process.
  1933. `default' Use the default application for this file type, which is the
  1934. association for t in the list, most likely in the system-specific
  1935. part. This can be used to overrule an unwanted setting in the
  1936. system-specific variable.
  1937. `system' Use the system command for opening files, like \"open\".
  1938. This command is specified by the entry whose car is `system'.
  1939. Most likely, the system-specific version of this variable
  1940. does define this command, but you can overrule/replace it
  1941. here.
  1942. `mailcap' Use command specified in the mailcaps.
  1943. string A command to be executed by a shell; %s will be replaced
  1944. by the path to the file.
  1945. function A Lisp function, which will be called with two arguments:
  1946. the file path and the original link string, without the
  1947. \"file:\" prefix.
  1948. For more examples, see the system specific constants
  1949. `org-file-apps-defaults-macosx'
  1950. `org-file-apps-defaults-windowsnt'
  1951. `org-file-apps-defaults-gnu'."
  1952. :group 'org-link-follow
  1953. :type '(repeat
  1954. (cons (choice :value ""
  1955. (string :tag "Extension")
  1956. (const :tag "System command to open files" system)
  1957. (const :tag "Default for unrecognized files" t)
  1958. (const :tag "Remote file" remote)
  1959. (const :tag "Links to a directory" directory)
  1960. (const :tag "Any files that have Emacs modes"
  1961. auto-mode))
  1962. (choice :value ""
  1963. (const :tag "Visit with Emacs" emacs)
  1964. (const :tag "Use default" default)
  1965. (const :tag "Use the system command" system)
  1966. (string :tag "Command")
  1967. (function :tag "Function")))))
  1968. (defcustom org-doi-server-url "http://dx.doi.org/"
  1969. "The URL of the DOI server."
  1970. :type 'string
  1971. :version "24.3"
  1972. :group 'org-link-follow)
  1973. (defgroup org-refile nil
  1974. "Options concerning refiling entries in Org-mode."
  1975. :tag "Org Refile"
  1976. :group 'org)
  1977. (defcustom org-directory "~/org"
  1978. "Directory with Org files.
  1979. This is just a default location to look for Org files. There is no need
  1980. at all to put your files into this directory. It is used in the
  1981. following situations:
  1982. 1. When a capture template specifies a target file that is not an
  1983. absolute path. The path will then be interpreted relative to
  1984. `org-directory'
  1985. 2. When the value of variable `org-agenda-files' is a single file, any
  1986. relative paths in this file will be taken as relative to
  1987. `org-directory'."
  1988. :group 'org-refile
  1989. :group 'org-capture
  1990. :type 'directory)
  1991. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1992. "Default target for storing notes.
  1993. Used as a fall back file for org-capture.el, for templates that
  1994. do not specify a target file."
  1995. :group 'org-refile
  1996. :group 'org-capture
  1997. :type 'file)
  1998. (defcustom org-goto-interface 'outline
  1999. "The default interface to be used for `org-goto'.
  2000. Allowed values are:
  2001. outline The interface shows an outline of the relevant file
  2002. and the correct heading is found by moving through
  2003. the outline or by searching with incremental search.
  2004. outline-path-completion Headlines in the current buffer are offered via
  2005. completion. This is the interface also used by
  2006. the refile command."
  2007. :group 'org-refile
  2008. :type '(choice
  2009. (const :tag "Outline" outline)
  2010. (const :tag "Outline-path-completion" outline-path-completion)))
  2011. (defcustom org-goto-max-level 5
  2012. "Maximum target level when running `org-goto' with refile interface."
  2013. :group 'org-refile
  2014. :type 'integer)
  2015. (defcustom org-reverse-note-order nil
  2016. "Non-nil means store new notes at the beginning of a file or entry.
  2017. When nil, new notes will be filed to the end of a file or entry.
  2018. This can also be a list with cons cells of regular expressions that
  2019. are matched against file names, and values."
  2020. :group 'org-capture
  2021. :group 'org-refile
  2022. :type '(choice
  2023. (const :tag "Reverse always" t)
  2024. (const :tag "Reverse never" nil)
  2025. (repeat :tag "By file name regexp"
  2026. (cons regexp boolean))))
  2027. (defcustom org-log-refile nil
  2028. "Information to record when a task is refiled.
  2029. Possible values are:
  2030. nil Don't add anything
  2031. time Add a time stamp to the task
  2032. note Prompt for a note and add it with template `org-log-note-headings'
  2033. This option can also be set with on a per-file-basis with
  2034. #+STARTUP: nologrefile
  2035. #+STARTUP: logrefile
  2036. #+STARTUP: lognoterefile
  2037. You can have local logging settings for a subtree by setting the LOGGING
  2038. property to one or more of these keywords.
  2039. When bulk-refiling from the agenda, the value `note' is forbidden and
  2040. will temporarily be changed to `time'."
  2041. :group 'org-refile
  2042. :group 'org-progress
  2043. :version "24.1"
  2044. :type '(choice
  2045. (const :tag "No logging" nil)
  2046. (const :tag "Record timestamp" time)
  2047. (const :tag "Record timestamp with note." note)))
  2048. (defcustom org-refile-targets nil
  2049. "Targets for refiling entries with \\[org-refile].
  2050. This is a list of cons cells. Each cell contains:
  2051. - a specification of the files to be considered, either a list of files,
  2052. or a symbol whose function or variable value will be used to retrieve
  2053. a file name or a list of file names. If you use `org-agenda-files' for
  2054. that, all agenda files will be scanned for targets. Nil means consider
  2055. headings in the current buffer.
  2056. - A specification of how to find candidate refile targets. This may be
  2057. any of:
  2058. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2059. This tag has to be present in all target headlines, inheritance will
  2060. not be considered.
  2061. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2062. todo keyword.
  2063. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2064. headlines that are refiling targets.
  2065. - a cons cell (:level . N). Any headline of level N is considered a target.
  2066. Note that, when `org-odd-levels-only' is set, level corresponds to
  2067. order in hierarchy, not to the number of stars.
  2068. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2069. Note that, when `org-odd-levels-only' is set, level corresponds to
  2070. order in hierarchy, not to the number of stars.
  2071. Each element of this list generates a set of possible targets.
  2072. The union of these sets is presented (with completion) to
  2073. the user by `org-refile'.
  2074. You can set the variable `org-refile-target-verify-function' to a function
  2075. to verify each headline found by the simple criteria above.
  2076. When this variable is nil, all top-level headlines in the current buffer
  2077. are used, equivalent to the value `((nil . (:level . 1))'."
  2078. :group 'org-refile
  2079. :type '(repeat
  2080. (cons
  2081. (choice :value org-agenda-files
  2082. (const :tag "All agenda files" org-agenda-files)
  2083. (const :tag "Current buffer" nil)
  2084. (function) (variable) (file))
  2085. (choice :tag "Identify target headline by"
  2086. (cons :tag "Specific tag" (const :value :tag) (string))
  2087. (cons :tag "TODO keyword" (const :value :todo) (string))
  2088. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2089. (cons :tag "Level number" (const :value :level) (integer))
  2090. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2091. (defcustom org-refile-target-verify-function nil
  2092. "Function to verify if the headline at point should be a refile target.
  2093. The function will be called without arguments, with point at the
  2094. beginning of the headline. It should return t and leave point
  2095. where it is if the headline is a valid target for refiling.
  2096. If the target should not be selected, the function must return nil.
  2097. In addition to this, it may move point to a place from where the search
  2098. should be continued. For example, the function may decide that the entire
  2099. subtree of the current entry should be excluded and move point to the end
  2100. of the subtree."
  2101. :group 'org-refile
  2102. :type '(choice
  2103. (const nil)
  2104. (function)))
  2105. (defcustom org-refile-use-cache nil
  2106. "Non-nil means cache refile targets to speed up the process.
  2107. \\<org-mode-map>\
  2108. The cache for a particular file will be updated automatically when
  2109. the buffer has been killed, or when any of the marker used for flagging
  2110. refile targets no longer points at a live buffer.
  2111. If you have added new entries to a buffer that might themselves be targets,
  2112. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2113. if you find that easier, \
  2114. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2115. \\[org-refile]'."
  2116. :group 'org-refile
  2117. :version "24.1"
  2118. :type 'boolean)
  2119. (defcustom org-refile-use-outline-path nil
  2120. "Non-nil means provide refile targets as paths.
  2121. So a level 3 headline will be available as level1/level2/level3.
  2122. When the value is `file', also include the file name (without directory)
  2123. into the path. In this case, you can also stop the completion after
  2124. the file name, to get entries inserted as top level in the file.
  2125. When `full-file-path', include the full file path."
  2126. :group 'org-refile
  2127. :type '(choice
  2128. (const :tag "Not" nil)
  2129. (const :tag "Yes" t)
  2130. (const :tag "Start with file name" file)
  2131. (const :tag "Start with full file path" full-file-path)))
  2132. (defcustom org-outline-path-complete-in-steps t
  2133. "Non-nil means complete the outline path in hierarchical steps.
  2134. When Org-mode uses the refile interface to select an outline path
  2135. \(see variable `org-refile-use-outline-path'), the completion of
  2136. the path can be done in a single go, or it can be done in steps down
  2137. the headline hierarchy. Going in steps is probably the best if you
  2138. do not use a special completion package like `ido' or `icicles'.
  2139. However, when using these packages, going in one step can be very
  2140. fast, while still showing the whole path to the entry."
  2141. :group 'org-refile
  2142. :type 'boolean)
  2143. (defcustom org-refile-allow-creating-parent-nodes nil
  2144. "Non-nil means allow the creation of new nodes as refile targets.
  2145. New nodes are then created by adding \"/new node name\" to the completion
  2146. of an existing node. When the value of this variable is `confirm',
  2147. new node creation must be confirmed by the user (recommended).
  2148. When nil, the completion must match an existing entry.
  2149. Note that, if the new heading is not seen by the criteria
  2150. listed in `org-refile-targets', multiple instances of the same
  2151. heading would be created by trying again to file under the new
  2152. heading."
  2153. :group 'org-refile
  2154. :type '(choice
  2155. (const :tag "Never" nil)
  2156. (const :tag "Always" t)
  2157. (const :tag "Prompt for confirmation" confirm)))
  2158. (defcustom org-refile-active-region-within-subtree nil
  2159. "Non-nil means also refile active region within a subtree.
  2160. By default `org-refile' doesn't allow refiling regions if they
  2161. don't contain a set of subtrees, but it might be convenient to
  2162. do so sometimes: in that case, the first line of the region is
  2163. converted to a headline before refiling."
  2164. :group 'org-refile
  2165. :version "24.1"
  2166. :type 'boolean)
  2167. (defgroup org-todo nil
  2168. "Options concerning TODO items in Org-mode."
  2169. :tag "Org TODO"
  2170. :group 'org)
  2171. (defgroup org-progress nil
  2172. "Options concerning Progress logging in Org-mode."
  2173. :tag "Org Progress"
  2174. :group 'org-time)
  2175. (defvar org-todo-interpretation-widgets
  2176. '((:tag "Sequence (cycling hits every state)" sequence)
  2177. (:tag "Type (cycling directly to DONE)" type))
  2178. "The available interpretation symbols for customizing `org-todo-keywords'.
  2179. Interested libraries should add to this list.")
  2180. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2181. "List of TODO entry keyword sequences and their interpretation.
  2182. \\<org-mode-map>This is a list of sequences.
  2183. Each sequence starts with a symbol, either `sequence' or `type',
  2184. indicating if the keywords should be interpreted as a sequence of
  2185. action steps, or as different types of TODO items. The first
  2186. keywords are states requiring action - these states will select a headline
  2187. for inclusion into the global TODO list Org-mode produces. If one of
  2188. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2189. signify that no further action is necessary. If \"|\" is not found,
  2190. the last keyword is treated as the only DONE state of the sequence.
  2191. The command \\[org-todo] cycles an entry through these states, and one
  2192. additional state where no keyword is present. For details about this
  2193. cycling, see the manual.
  2194. TODO keywords and interpretation can also be set on a per-file basis with
  2195. the special #+SEQ_TODO and #+TYP_TODO lines.
  2196. Each keyword can optionally specify a character for fast state selection
  2197. \(in combination with the variable `org-use-fast-todo-selection')
  2198. and specifiers for state change logging, using the same syntax that
  2199. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2200. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2201. indicates to record a time stamp each time this state is selected.
  2202. Each keyword may also specify if a timestamp or a note should be
  2203. recorded when entering or leaving the state, by adding additional
  2204. characters in the parenthesis after the keyword. This looks like this:
  2205. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2206. record only the time of the state change. With X and Y being either
  2207. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2208. Y when leaving the state if and only if the *target* state does not
  2209. define X. You may omit any of the fast-selection key or X or /Y,
  2210. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2211. For backward compatibility, this variable may also be just a list
  2212. of keywords. In this case the interpretation (sequence or type) will be
  2213. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2214. :group 'org-todo
  2215. :group 'org-keywords
  2216. :type '(choice
  2217. (repeat :tag "Old syntax, just keywords"
  2218. (string :tag "Keyword"))
  2219. (repeat :tag "New syntax"
  2220. (cons
  2221. (choice
  2222. :tag "Interpretation"
  2223. ;;Quick and dirty way to see
  2224. ;;`org-todo-interpretations'. This takes the
  2225. ;;place of item arguments
  2226. :convert-widget
  2227. (lambda (widget)
  2228. (widget-put widget
  2229. :args (mapcar
  2230. (lambda (x)
  2231. (widget-convert
  2232. (cons 'const x)))
  2233. org-todo-interpretation-widgets))
  2234. widget))
  2235. (repeat
  2236. (string :tag "Keyword"))))))
  2237. (defvar-local org-todo-keywords-1 nil
  2238. "All TODO and DONE keywords active in a buffer.")
  2239. (defvar org-todo-keywords-for-agenda nil)
  2240. (defvar org-done-keywords-for-agenda nil)
  2241. (defvar org-todo-keyword-alist-for-agenda nil)
  2242. (defvar org-tag-alist-for-agenda nil
  2243. "Alist of all tags from all agenda files.")
  2244. (defvar org-tag-groups-alist-for-agenda nil
  2245. "Alist of all groups tags from all current agenda files.")
  2246. (defvar-local org-tag-groups-alist nil)
  2247. (defvar org-agenda-contributing-files nil)
  2248. (defvar-local org-current-tag-alist nil
  2249. "Alist of all tag groups in current buffer.
  2250. This variable takes into consideration `org-tag-alist',
  2251. `org-tag-persistent-alist' and TAGS keywords in the buffer.")
  2252. (defvar-local org-not-done-keywords nil)
  2253. (defvar-local org-done-keywords nil)
  2254. (defvar-local org-todo-heads nil)
  2255. (defvar-local org-todo-sets nil)
  2256. (defvar-local org-todo-log-states nil)
  2257. (defvar-local org-todo-kwd-alist nil)
  2258. (defvar-local org-todo-key-alist nil)
  2259. (defvar-local org-todo-key-trigger nil)
  2260. (defcustom org-todo-interpretation 'sequence
  2261. "Controls how TODO keywords are interpreted.
  2262. This variable is in principle obsolete and is only used for
  2263. backward compatibility, if the interpretation of todo keywords is
  2264. not given already in `org-todo-keywords'. See that variable for
  2265. more information."
  2266. :group 'org-todo
  2267. :group 'org-keywords
  2268. :type '(choice (const sequence)
  2269. (const type)))
  2270. (defcustom org-use-fast-todo-selection t
  2271. "\\<org-mode-map>\
  2272. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2273. This variable describes if and under what circumstances the cycling
  2274. mechanism for TODO keywords will be replaced by a single-key, direct
  2275. selection scheme.
  2276. When nil, fast selection is never used.
  2277. When the symbol `prefix', it will be used when `org-todo' is called
  2278. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2279. in an Org-mode buffer, and
  2280. `\\[universal-argument] t' in an agenda buffer.
  2281. When t, fast selection is used by default. In this case, the prefix
  2282. argument forces cycling instead.
  2283. In all cases, the special interface is only used if access keys have
  2284. actually been assigned by the user, i.e. if keywords in the configuration
  2285. are followed by a letter in parenthesis, like TODO(t)."
  2286. :group 'org-todo
  2287. :type '(choice
  2288. (const :tag "Never" nil)
  2289. (const :tag "By default" t)
  2290. (const :tag "Only with C-u C-c C-t" prefix)))
  2291. (defcustom org-provide-todo-statistics t
  2292. "Non-nil means update todo statistics after insert and toggle.
  2293. ALL-HEADLINES means update todo statistics by including headlines
  2294. with no TODO keyword as well, counting them as not done.
  2295. A list of TODO keywords means the same, but skip keywords that are
  2296. not in this list.
  2297. When set to a list of two lists, the first list contains keywords
  2298. to consider as TODO keywords, the second list contains keywords
  2299. to consider as DONE keywords.
  2300. When this is set, todo statistics is updated in the parent of the
  2301. current entry each time a todo state is changed."
  2302. :group 'org-todo
  2303. :type '(choice
  2304. (const :tag "Yes, only for TODO entries" t)
  2305. (const :tag "Yes, including all entries" all-headlines)
  2306. (repeat :tag "Yes, for TODOs in this list"
  2307. (string :tag "TODO keyword"))
  2308. (list :tag "Yes, for TODOs and DONEs in these lists"
  2309. (repeat (string :tag "TODO keyword"))
  2310. (repeat (string :tag "DONE keyword")))
  2311. (other :tag "No TODO statistics" nil)))
  2312. (defcustom org-hierarchical-todo-statistics t
  2313. "Non-nil means TODO statistics covers just direct children.
  2314. When nil, all entries in the subtree are considered.
  2315. This has only an effect if `org-provide-todo-statistics' is set.
  2316. To set this to nil for only a single subtree, use a COOKIE_DATA
  2317. property and include the word \"recursive\" into the value."
  2318. :group 'org-todo
  2319. :type 'boolean)
  2320. (defcustom org-after-todo-state-change-hook nil
  2321. "Hook which is run after the state of a TODO item was changed.
  2322. The new state (a string with a TODO keyword, or nil) is available in the
  2323. Lisp variable `org-state'."
  2324. :group 'org-todo
  2325. :type 'hook)
  2326. (defvar org-blocker-hook nil
  2327. "Hook for functions that are allowed to block a state change.
  2328. Functions in this hook should not modify the buffer.
  2329. Each function gets as its single argument a property list,
  2330. see `org-trigger-hook' for more information about this list.
  2331. If any of the functions in this hook returns nil, the state change
  2332. is blocked.")
  2333. (defvar org-trigger-hook nil
  2334. "Hook for functions that are triggered by a state change.
  2335. Each function gets as its single argument a property list with at
  2336. least the following elements:
  2337. (:type type-of-change :position pos-at-entry-start
  2338. :from old-state :to new-state)
  2339. Depending on the type, more properties may be present.
  2340. This mechanism is currently implemented for:
  2341. TODO state changes
  2342. ------------------
  2343. :type todo-state-change
  2344. :from previous state (keyword as a string), or nil, or a symbol
  2345. `todo' or `done', to indicate the general type of state.
  2346. :to new state, like in :from")
  2347. (defcustom org-enforce-todo-dependencies nil
  2348. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2349. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2350. be blocked if any prior sibling is not yet done.
  2351. Finally, if the parent is blocked because of ordered siblings of its own,
  2352. the child will also be blocked."
  2353. :set (lambda (var val)
  2354. (set var val)
  2355. (if val
  2356. (add-hook 'org-blocker-hook
  2357. 'org-block-todo-from-children-or-siblings-or-parent)
  2358. (remove-hook 'org-blocker-hook
  2359. 'org-block-todo-from-children-or-siblings-or-parent)))
  2360. :group 'org-todo
  2361. :type 'boolean)
  2362. (defcustom org-enforce-todo-checkbox-dependencies nil
  2363. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2364. When this is nil, checkboxes have no influence on switching TODO states.
  2365. When non-nil, you first need to check off all check boxes before the TODO
  2366. entry can be switched to DONE.
  2367. This variable needs to be set before org.el is loaded, and you need to
  2368. restart Emacs after a change to make the change effective. The only way
  2369. to change is while Emacs is running is through the customize interface."
  2370. :set (lambda (var val)
  2371. (set var val)
  2372. (if val
  2373. (add-hook 'org-blocker-hook
  2374. 'org-block-todo-from-checkboxes)
  2375. (remove-hook 'org-blocker-hook
  2376. 'org-block-todo-from-checkboxes)))
  2377. :group 'org-todo
  2378. :type 'boolean)
  2379. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2380. "Non-nil means inserting a TODO heading is treated as state change.
  2381. So when the command \\[org-insert-todo-heading] is used, state change
  2382. logging will apply if appropriate. When nil, the new TODO item will
  2383. be inserted directly, and no logging will take place."
  2384. :group 'org-todo
  2385. :type 'boolean)
  2386. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2387. "Non-nil means switching TODO states with S-cursor counts as state change.
  2388. This is the default behavior. However, setting this to nil allows a
  2389. convenient way to select a TODO state and bypass any logging associated
  2390. with that."
  2391. :group 'org-todo
  2392. :type 'boolean)
  2393. (defcustom org-todo-state-tags-triggers nil
  2394. "Tag changes that should be triggered by TODO state changes.
  2395. This is a list. Each entry is
  2396. (state-change (tag . flag) .......)
  2397. State-change can be a string with a state, and empty string to indicate the
  2398. state that has no TODO keyword, or it can be one of the symbols `todo'
  2399. or `done', meaning any not-done or done state, respectively."
  2400. :group 'org-todo
  2401. :group 'org-tags
  2402. :type '(repeat
  2403. (cons (choice :tag "When changing to"
  2404. (const :tag "Not-done state" todo)
  2405. (const :tag "Done state" done)
  2406. (string :tag "State"))
  2407. (repeat
  2408. (cons :tag "Tag action"
  2409. (string :tag "Tag")
  2410. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2411. (defcustom org-log-done nil
  2412. "Information to record when a task moves to the DONE state.
  2413. Possible values are:
  2414. nil Don't add anything, just change the keyword
  2415. time Add a time stamp to the task
  2416. note Prompt for a note and add it with template `org-log-note-headings'
  2417. This option can also be set with on a per-file-basis with
  2418. #+STARTUP: nologdone
  2419. #+STARTUP: logdone
  2420. #+STARTUP: lognotedone
  2421. You can have local logging settings for a subtree by setting the LOGGING
  2422. property to one or more of these keywords."
  2423. :group 'org-todo
  2424. :group 'org-progress
  2425. :type '(choice
  2426. (const :tag "No logging" nil)
  2427. (const :tag "Record CLOSED timestamp" time)
  2428. (const :tag "Record CLOSED timestamp with note." note)))
  2429. ;; Normalize old uses of org-log-done.
  2430. (cond
  2431. ((eq org-log-done t) (setq org-log-done 'time))
  2432. ((and (listp org-log-done) (memq 'done org-log-done))
  2433. (setq org-log-done 'note)))
  2434. (defcustom org-log-reschedule nil
  2435. "Information to record when the scheduling date of a tasks is modified.
  2436. Possible values are:
  2437. nil Don't add anything, just change the date
  2438. time Add a time stamp to the task
  2439. note Prompt for a note and add it with template `org-log-note-headings'
  2440. This option can also be set with on a per-file-basis with
  2441. #+STARTUP: nologreschedule
  2442. #+STARTUP: logreschedule
  2443. #+STARTUP: lognotereschedule"
  2444. :group 'org-todo
  2445. :group 'org-progress
  2446. :type '(choice
  2447. (const :tag "No logging" nil)
  2448. (const :tag "Record timestamp" time)
  2449. (const :tag "Record timestamp with note." note)))
  2450. (defcustom org-log-redeadline nil
  2451. "Information to record when the deadline date of a tasks is modified.
  2452. Possible values are:
  2453. nil Don't add anything, just change the date
  2454. time Add a time stamp to the task
  2455. note Prompt for a note and add it with template `org-log-note-headings'
  2456. This option can also be set with on a per-file-basis with
  2457. #+STARTUP: nologredeadline
  2458. #+STARTUP: logredeadline
  2459. #+STARTUP: lognoteredeadline
  2460. You can have local logging settings for a subtree by setting the LOGGING
  2461. property to one or more of these keywords."
  2462. :group 'org-todo
  2463. :group 'org-progress
  2464. :type '(choice
  2465. (const :tag "No logging" nil)
  2466. (const :tag "Record timestamp" time)
  2467. (const :tag "Record timestamp with note." note)))
  2468. (defcustom org-log-note-clock-out nil
  2469. "Non-nil means record a note when clocking out of an item.
  2470. This can also be configured on a per-file basis by adding one of
  2471. the following lines anywhere in the buffer:
  2472. #+STARTUP: lognoteclock-out
  2473. #+STARTUP: nolognoteclock-out"
  2474. :group 'org-todo
  2475. :group 'org-progress
  2476. :type 'boolean)
  2477. (defcustom org-log-done-with-time t
  2478. "Non-nil means the CLOSED time stamp will contain date and time.
  2479. When nil, only the date will be recorded."
  2480. :group 'org-progress
  2481. :type 'boolean)
  2482. (defcustom org-log-note-headings
  2483. '((done . "CLOSING NOTE %t")
  2484. (state . "State %-12s from %-12S %t")
  2485. (note . "Note taken on %t")
  2486. (reschedule . "Rescheduled from %S on %t")
  2487. (delschedule . "Not scheduled, was %S on %t")
  2488. (redeadline . "New deadline from %S on %t")
  2489. (deldeadline . "Removed deadline, was %S on %t")
  2490. (refile . "Refiled on %t")
  2491. (clock-out . ""))
  2492. "Headings for notes added to entries.
  2493. The value is an alist, with the car being a symbol indicating the
  2494. note context, and the cdr is the heading to be used. The heading
  2495. may also be the empty string. The following placeholders can be
  2496. used:
  2497. %t a time stamp.
  2498. %T an active time stamp instead the default inactive one
  2499. %d a short-format time stamp.
  2500. %D an active short-format time stamp.
  2501. %s the new TODO state or time stamp (inactive), in double quotes.
  2502. %S the old TODO state or time stamp (inactive), in double quotes.
  2503. %u the user name.
  2504. %U full user name.
  2505. In fact, it is not a good idea to change the `state' entry,
  2506. because Agenda Log mode depends on the format of these entries."
  2507. :group 'org-todo
  2508. :group 'org-progress
  2509. :type '(list :greedy t
  2510. (cons (const :tag "Heading when closing an item" done) string)
  2511. (cons (const :tag
  2512. "Heading when changing todo state (todo sequence only)"
  2513. state) string)
  2514. (cons (const :tag "Heading when just taking a note" note) string)
  2515. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2516. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2517. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2518. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2519. (cons (const :tag "Heading when refiling" refile) string)
  2520. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2521. (unless (assq 'note org-log-note-headings)
  2522. (push '(note . "%t") org-log-note-headings))
  2523. (defcustom org-log-into-drawer nil
  2524. "Non-nil means insert state change notes and time stamps into a drawer.
  2525. When nil, state changes notes will be inserted after the headline and
  2526. any scheduling and clock lines, but not inside a drawer.
  2527. The value of this variable should be the name of the drawer to use.
  2528. LOGBOOK is proposed as the default drawer for this purpose, you can
  2529. also set this to a string to define the drawer of your choice.
  2530. A value of t is also allowed, representing \"LOGBOOK\".
  2531. A value of t or nil can also be set with on a per-file-basis with
  2532. #+STARTUP: logdrawer
  2533. #+STARTUP: nologdrawer
  2534. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2535. will be ignored.
  2536. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2537. a subtree.
  2538. Do not check directly this variable in a Lisp program. Call
  2539. function `org-log-into-drawer' instead."
  2540. :group 'org-todo
  2541. :group 'org-progress
  2542. :type '(choice
  2543. (const :tag "Not into a drawer" nil)
  2544. (const :tag "LOGBOOK" t)
  2545. (string :tag "Other")))
  2546. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2547. (defun org-log-into-drawer ()
  2548. "Name of the log drawer, as a string, or nil.
  2549. This is the value of `org-log-into-drawer'. However, if the
  2550. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2551. be used instead of the default value."
  2552. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2553. (cond ((equal p "nil") nil)
  2554. ((equal p "t") "LOGBOOK")
  2555. ((stringp p) p)
  2556. (p "LOGBOOK")
  2557. ((stringp org-log-into-drawer) org-log-into-drawer)
  2558. (org-log-into-drawer "LOGBOOK"))))
  2559. (defcustom org-log-state-notes-insert-after-drawers nil
  2560. "Non-nil means insert state change notes after any drawers in entry.
  2561. Only the drawers that *immediately* follow the headline and the
  2562. deadline/scheduled line are skipped.
  2563. When nil, insert notes right after the heading and perhaps the line
  2564. with deadline/scheduling if present.
  2565. This variable will have no effect if `org-log-into-drawer' is
  2566. set."
  2567. :group 'org-todo
  2568. :group 'org-progress
  2569. :type 'boolean)
  2570. (defcustom org-log-states-order-reversed t
  2571. "Non-nil means the latest state note will be directly after heading.
  2572. When nil, the state change notes will be ordered according to time.
  2573. This option can also be set with on a per-file-basis with
  2574. #+STARTUP: logstatesreversed
  2575. #+STARTUP: nologstatesreversed"
  2576. :group 'org-todo
  2577. :group 'org-progress
  2578. :type 'boolean)
  2579. (defcustom org-todo-repeat-to-state nil
  2580. "The TODO state to which a repeater should return the repeating task.
  2581. By default this is the first task in a TODO sequence, or the previous state
  2582. in a TODO_TYP set. But you can specify another task here.
  2583. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2584. :group 'org-todo
  2585. :version "24.1"
  2586. :type '(choice (const :tag "Head of sequence" nil)
  2587. (string :tag "Specific state")))
  2588. (defcustom org-log-repeat 'time
  2589. "Non-nil means record moving through the DONE state when triggering repeat.
  2590. An auto-repeating task is immediately switched back to TODO when
  2591. marked DONE. If you are not logging state changes (by adding \"@\"
  2592. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2593. record a closing note, there will be no record of the task moving
  2594. through DONE. This variable forces taking a note anyway.
  2595. nil Don't force a record
  2596. time Record a time stamp
  2597. note Prompt for a note and add it with template `org-log-note-headings'
  2598. This option can also be set with on a per-file-basis with
  2599. #+STARTUP: nologrepeat
  2600. #+STARTUP: logrepeat
  2601. #+STARTUP: lognoterepeat
  2602. You can have local logging settings for a subtree by setting the LOGGING
  2603. property to one or more of these keywords."
  2604. :group 'org-todo
  2605. :group 'org-progress
  2606. :type '(choice
  2607. (const :tag "Don't force a record" nil)
  2608. (const :tag "Force recording the DONE state" time)
  2609. (const :tag "Force recording a note with the DONE state" note)))
  2610. (defgroup org-priorities nil
  2611. "Priorities in Org-mode."
  2612. :tag "Org Priorities"
  2613. :group 'org-todo)
  2614. (defcustom org-enable-priority-commands t
  2615. "Non-nil means priority commands are active.
  2616. When nil, these commands will be disabled, so that you never accidentally
  2617. set a priority."
  2618. :group 'org-priorities
  2619. :type 'boolean)
  2620. (defcustom org-highest-priority ?A
  2621. "The highest priority of TODO items. A character like ?A, ?B etc.
  2622. Must have a smaller ASCII number than `org-lowest-priority'."
  2623. :group 'org-priorities
  2624. :type 'character)
  2625. (defcustom org-lowest-priority ?C
  2626. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2627. Must have a larger ASCII number than `org-highest-priority'."
  2628. :group 'org-priorities
  2629. :type 'character)
  2630. (defcustom org-default-priority ?B
  2631. "The default priority of TODO items.
  2632. This is the priority an item gets if no explicit priority is given.
  2633. When starting to cycle on an empty priority the first step in the cycle
  2634. depends on `org-priority-start-cycle-with-default'. The resulting first
  2635. step priority must not exceed the range from `org-highest-priority' to
  2636. `org-lowest-priority' which means that `org-default-priority' has to be
  2637. in this range exclusive or inclusive the range boundaries. Else the
  2638. first step refuses to set the default and the second will fall back
  2639. to (depending on the command used) the highest or lowest priority."
  2640. :group 'org-priorities
  2641. :type 'character)
  2642. (defcustom org-priority-start-cycle-with-default t
  2643. "Non-nil means start with default priority when starting to cycle.
  2644. When this is nil, the first step in the cycle will be (depending on the
  2645. command used) one higher or lower than the default priority.
  2646. See also `org-default-priority'."
  2647. :group 'org-priorities
  2648. :type 'boolean)
  2649. (defcustom org-get-priority-function nil
  2650. "Function to extract the priority from a string.
  2651. The string is normally the headline. If this is nil Org computes the
  2652. priority from the priority cookie like [#A] in the headline. It returns
  2653. an integer, increasing by 1000 for each priority level.
  2654. The user can set a different function here, which should take a string
  2655. as an argument and return the numeric priority."
  2656. :group 'org-priorities
  2657. :version "24.1"
  2658. :type '(choice
  2659. (const nil)
  2660. (function)))
  2661. (defgroup org-time nil
  2662. "Options concerning time stamps and deadlines in Org-mode."
  2663. :tag "Org Time"
  2664. :group 'org)
  2665. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2666. "Number of minutes to round time stamps to.
  2667. \\<org-mode-map>\
  2668. These are two values, the first applies when first creating a time stamp.
  2669. The second applies when changing it with the commands `S-up' and `S-down'.
  2670. When changing the time stamp, this means that it will change in steps
  2671. of N minutes, as given by the second value.
  2672. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2673. numbers should be factors of 60, so for example 5, 10, 15.
  2674. When this is larger than 1, you can still force an exact time stamp by using
  2675. a double prefix argument to a time stamp command like \
  2676. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2677. and by using a prefix arg to `S-up/down' to specify the exact number
  2678. of minutes to shift."
  2679. :group 'org-time
  2680. :get (lambda (var) ; Make sure both elements are there
  2681. (if (integerp (default-value var))
  2682. (list (default-value var) 5)
  2683. (default-value var)))
  2684. :type '(list
  2685. (integer :tag "when inserting times")
  2686. (integer :tag "when modifying times")))
  2687. ;; Normalize old customizations of this variable.
  2688. (when (integerp org-time-stamp-rounding-minutes)
  2689. (setq org-time-stamp-rounding-minutes
  2690. (list org-time-stamp-rounding-minutes
  2691. org-time-stamp-rounding-minutes)))
  2692. (defcustom org-display-custom-times nil
  2693. "Non-nil means overlay custom formats over all time stamps.
  2694. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2695. To turn this on on a per-file basis, insert anywhere in the file:
  2696. #+STARTUP: customtime"
  2697. :group 'org-time
  2698. :set 'set-default
  2699. :type 'sexp)
  2700. (make-variable-buffer-local 'org-display-custom-times)
  2701. (defcustom org-time-stamp-custom-formats
  2702. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2703. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2704. These are overlaid over the default ISO format if the variable
  2705. `org-display-custom-times' is set. Time like %H:%M should be at the
  2706. end of the second format. The custom formats are also honored by export
  2707. commands, if custom time display is turned on at the time of export."
  2708. :group 'org-time
  2709. :type 'sexp)
  2710. (defun org-time-stamp-format (&optional long inactive)
  2711. "Get the right format for a time string."
  2712. (let ((f (if long (cdr org-time-stamp-formats)
  2713. (car org-time-stamp-formats))))
  2714. (if inactive
  2715. (concat "[" (substring f 1 -1) "]")
  2716. f)))
  2717. (defcustom org-time-clocksum-format
  2718. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2719. "The format string used when creating CLOCKSUM lines.
  2720. This is also used when Org mode generates a time duration.
  2721. The value can be a single format string containing two
  2722. %-sequences, which will be filled with the number of hours and
  2723. minutes in that order.
  2724. Alternatively, the value can be a plist associating any of the
  2725. keys :years, :months, :weeks, :days, :hours or :minutes with
  2726. format strings. The time duration is formatted using only the
  2727. time components that are needed and concatenating the results.
  2728. If a time unit in absent, it falls back to the next smallest
  2729. unit.
  2730. The keys :require-years, :require-months, :require-days,
  2731. :require-weeks, :require-hours, :require-minutes are also
  2732. meaningful. A non-nil value for these keys indicates that the
  2733. corresponding time component should always be included, even if
  2734. its value is 0.
  2735. For example,
  2736. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2737. :require-minutes t)
  2738. means durations longer than a day will be expressed in days,
  2739. hours and minutes, and durations less than a day will always be
  2740. expressed in hours and minutes (even for durations less than an
  2741. hour).
  2742. The value
  2743. (:days \"%dd\" :minutes \"%dm\")
  2744. means durations longer than a day will be expressed in days and
  2745. minutes, and durations less than a day will be expressed entirely
  2746. in minutes (even for durations longer than an hour)."
  2747. :group 'org-time
  2748. :group 'org-clock
  2749. :version "24.4"
  2750. :package-version '(Org . "8.0")
  2751. :type '(choice (string :tag "Format string")
  2752. (set :tag "Plist"
  2753. (group :inline t (const :tag "Years" :years)
  2754. (string :tag "Format string"))
  2755. (group :inline t
  2756. (const :tag "Always show years" :require-years)
  2757. (const t))
  2758. (group :inline t (const :tag "Months" :months)
  2759. (string :tag "Format string"))
  2760. (group :inline t
  2761. (const :tag "Always show months" :require-months)
  2762. (const t))
  2763. (group :inline t (const :tag "Weeks" :weeks)
  2764. (string :tag "Format string"))
  2765. (group :inline t
  2766. (const :tag "Always show weeks" :require-weeks)
  2767. (const t))
  2768. (group :inline t (const :tag "Days" :days)
  2769. (string :tag "Format string"))
  2770. (group :inline t
  2771. (const :tag "Always show days" :require-days)
  2772. (const t))
  2773. (group :inline t (const :tag "Hours" :hours)
  2774. (string :tag "Format string"))
  2775. (group :inline t
  2776. (const :tag "Always show hours" :require-hours)
  2777. (const t))
  2778. (group :inline t (const :tag "Minutes" :minutes)
  2779. (string :tag "Format string"))
  2780. (group :inline t
  2781. (const :tag "Always show minutes" :require-minutes)
  2782. (const t)))))
  2783. (defcustom org-time-clocksum-use-fractional nil
  2784. "When non-nil, \\[org-clock-display] uses fractional times.
  2785. See `org-time-clocksum-format' for more on time clock formats."
  2786. :group 'org-time
  2787. :group 'org-clock
  2788. :version "24.3"
  2789. :type 'boolean)
  2790. (defcustom org-time-clocksum-use-effort-durations nil
  2791. "When non-nil, \\[org-clock-display] uses effort durations.
  2792. E.g. by default, one day is considered to be a 8 hours effort,
  2793. so a task that has been clocked for 16 hours will be displayed
  2794. as during 2 days in the clock display or in the clocktable.
  2795. See `org-effort-durations' on how to set effort durations
  2796. and `org-time-clocksum-format' for more on time clock formats."
  2797. :group 'org-time
  2798. :group 'org-clock
  2799. :version "24.4"
  2800. :package-version '(Org . "8.0")
  2801. :type 'boolean)
  2802. (defcustom org-time-clocksum-fractional-format "%.2f"
  2803. "The format string used when creating CLOCKSUM lines,
  2804. or when Org mode generates a time duration, if
  2805. `org-time-clocksum-use-fractional' is enabled.
  2806. The value can be a single format string containing one
  2807. %-sequence, which will be filled with the number of hours as
  2808. a float.
  2809. Alternatively, the value can be a plist associating any of the
  2810. keys :years, :months, :weeks, :days, :hours or :minutes with
  2811. a format string. The time duration is formatted using the
  2812. largest time unit which gives a non-zero integer part. If all
  2813. specified formats have zero integer part, the smallest time unit
  2814. is used."
  2815. :group 'org-time
  2816. :type '(choice (string :tag "Format string")
  2817. (set (group :inline t (const :tag "Years" :years)
  2818. (string :tag "Format string"))
  2819. (group :inline t (const :tag "Months" :months)
  2820. (string :tag "Format string"))
  2821. (group :inline t (const :tag "Weeks" :weeks)
  2822. (string :tag "Format string"))
  2823. (group :inline t (const :tag "Days" :days)
  2824. (string :tag "Format string"))
  2825. (group :inline t (const :tag "Hours" :hours)
  2826. (string :tag "Format string"))
  2827. (group :inline t (const :tag "Minutes" :minutes)
  2828. (string :tag "Format string")))))
  2829. (defcustom org-deadline-warning-days 14
  2830. "Number of days before expiration during which a deadline becomes active.
  2831. This variable governs the display in sparse trees and in the agenda.
  2832. When 0 or negative, it means use this number (the absolute value of it)
  2833. even if a deadline has a different individual lead time specified.
  2834. Custom commands can set this variable in the options section."
  2835. :group 'org-time
  2836. :group 'org-agenda-daily/weekly
  2837. :type 'integer)
  2838. (defcustom org-scheduled-delay-days 0
  2839. "Number of days before a scheduled item becomes active.
  2840. This variable governs the display in sparse trees and in the agenda.
  2841. The default value (i.e. 0) means: don't delay scheduled item.
  2842. When negative, it means use this number (the absolute value of it)
  2843. even if a scheduled item has a different individual delay time
  2844. specified.
  2845. Custom commands can set this variable in the options section."
  2846. :group 'org-time
  2847. :group 'org-agenda-daily/weekly
  2848. :version "24.4"
  2849. :package-version '(Org . "8.0")
  2850. :type 'integer)
  2851. (defcustom org-read-date-prefer-future t
  2852. "Non-nil means assume future for incomplete date input from user.
  2853. This affects the following situations:
  2854. 1. The user gives a month but not a year.
  2855. For example, if it is April and you enter \"feb 2\", this will be read
  2856. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2857. 2. The user gives a day, but no month.
  2858. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2859. read this as the third of *next* month. However, if you enter \"17\",
  2860. it will be considered as *this* month.
  2861. If you set this variable to the symbol `time', then also the following
  2862. will work:
  2863. 3. If the user gives a time.
  2864. If the time is before now, it will be interpreted as tomorrow.
  2865. Currently none of this works for ISO week specifications.
  2866. When this option is nil, the current day, month and year will always be
  2867. used as defaults.
  2868. See also `org-agenda-jump-prefer-future'."
  2869. :group 'org-time
  2870. :type '(choice
  2871. (const :tag "Never" nil)
  2872. (const :tag "Check month and day" t)
  2873. (const :tag "Check month, day, and time" time)))
  2874. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2875. "Should the agenda jump command prefer the future for incomplete dates?
  2876. The default is to do the same as configured in `org-read-date-prefer-future'.
  2877. But you can also set a deviating value here.
  2878. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2879. :group 'org-agenda
  2880. :group 'org-time
  2881. :version "24.1"
  2882. :type '(choice
  2883. (const :tag "Use org-read-date-prefer-future"
  2884. org-read-date-prefer-future)
  2885. (const :tag "Never" nil)
  2886. (const :tag "Always" t)))
  2887. (defcustom org-read-date-force-compatible-dates t
  2888. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2889. Depending on the system Emacs is running on, certain dates cannot
  2890. be represented with the type used internally to represent time.
  2891. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2892. correctly. Some systems allow for earlier dates, some for later,
  2893. some for both. One way to find out it to insert any date into an
  2894. Org buffer, putting the cursor on the year and hitting S-up and
  2895. S-down to test the range.
  2896. When this variable is set to t, the date/time prompt will not let
  2897. you specify dates outside the 1970-2037 range, so it is certain that
  2898. these dates will work in whatever version of Emacs you are
  2899. running, and also that you can move a file from one Emacs implementation
  2900. to another. WHenever Org is forcing the year for you, it will display
  2901. a message and beep.
  2902. When this variable is nil, Org will check if the date is
  2903. representable in the specific Emacs implementation you are using.
  2904. If not, it will force a year, usually the current year, and beep
  2905. to remind you. Currently this setting is not recommended because
  2906. the likelihood that you will open your Org files in an Emacs that
  2907. has limited date range is not negligible.
  2908. A workaround for this problem is to use diary sexp dates for time
  2909. stamps outside of this range."
  2910. :group 'org-time
  2911. :version "24.1"
  2912. :type 'boolean)
  2913. (defcustom org-read-date-display-live t
  2914. "Non-nil means display current interpretation of date prompt live.
  2915. This display will be in an overlay, in the minibuffer."
  2916. :group 'org-time
  2917. :type 'boolean)
  2918. (defcustom org-read-date-popup-calendar t
  2919. "Non-nil means pop up a calendar when prompting for a date.
  2920. In the calendar, the date can be selected with mouse-1. However, the
  2921. minibuffer will also be active, and you can simply enter the date as well.
  2922. When nil, only the minibuffer will be available."
  2923. :group 'org-time
  2924. :type 'boolean)
  2925. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2926. 'org-read-date-popup-calendar)
  2927. (make-obsolete-variable
  2928. 'org-read-date-minibuffer-setup-hook
  2929. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2930. (defcustom org-read-date-minibuffer-setup-hook nil
  2931. "Hook to be used to set up keys for the date/time interface.
  2932. Add key definitions to `minibuffer-local-map', which will be a
  2933. temporary copy.
  2934. WARNING: This option is obsolete, you should use
  2935. `org-read-date-minibuffer-local-map' to set up keys."
  2936. :group 'org-time
  2937. :type 'hook)
  2938. (defcustom org-extend-today-until 0
  2939. "The hour when your day really ends. Must be an integer.
  2940. This has influence for the following applications:
  2941. - When switching the agenda to \"today\". It it is still earlier than
  2942. the time given here, the day recognized as TODAY is actually yesterday.
  2943. - When a date is read from the user and it is still before the time given
  2944. here, the current date and time will be assumed to be yesterday, 23:59.
  2945. Also, timestamps inserted in capture templates follow this rule.
  2946. IMPORTANT: This is a feature whose implementation is and likely will
  2947. remain incomplete. Really, it is only here because past midnight seems to
  2948. be the favorite working time of John Wiegley :-)"
  2949. :group 'org-time
  2950. :type 'integer)
  2951. (defcustom org-use-effective-time nil
  2952. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2953. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2954. \"effective time\" of any timestamps between midnight and 8am will be
  2955. 23:59 of the previous day."
  2956. :group 'org-time
  2957. :version "24.1"
  2958. :type 'boolean)
  2959. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2960. "When non-nil, use the last clock out time for `org-todo'.
  2961. Note that this option has precedence over the combined use of
  2962. `org-use-effective-time' and `org-extend-today-until'."
  2963. :group 'org-time
  2964. :version "24.4"
  2965. :package-version '(Org . "8.0")
  2966. :type 'boolean)
  2967. (defcustom org-edit-timestamp-down-means-later nil
  2968. "Non-nil means S-down will increase the time in a time stamp.
  2969. When nil, S-up will increase."
  2970. :group 'org-time
  2971. :type 'boolean)
  2972. (defcustom org-calendar-follow-timestamp-change t
  2973. "Non-nil means make the calendar window follow timestamp changes.
  2974. When a timestamp is modified and the calendar window is visible, it will be
  2975. moved to the new date."
  2976. :group 'org-time
  2977. :type 'boolean)
  2978. (defgroup org-tags nil
  2979. "Options concerning tags in Org-mode."
  2980. :tag "Org Tags"
  2981. :group 'org)
  2982. (defcustom org-tag-alist nil
  2983. "Default tags available in Org files.
  2984. The value of this variable is an alist. Associations either:
  2985. (TAG)
  2986. (TAG . SELECT)
  2987. (SPECIAL)
  2988. where TAG is a tag as a string, SELECT is a character, used to
  2989. select that tag through the fast tag selection interface, and
  2990. SPECIAL is one of the following keywords: `:startgroup',
  2991. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2992. `:newline'. These keywords are used to define a hierarchy of
  2993. tags. See manual for details.
  2994. When this variable is nil, Org mode bases tag input on what is
  2995. already in the buffer. The value can be overridden locally by
  2996. using a TAGS keyword, e.g.,
  2997. #+TAGS: tag1 tag2
  2998. See also `org-tag-persistent-alist' to sidestep this behavior."
  2999. :group 'org-tags
  3000. :type '(repeat
  3001. (choice
  3002. (cons (string :tag "Tag name")
  3003. (character :tag "Access char"))
  3004. (const :tag "Start radio group" (:startgroup))
  3005. (const :tag "Start tag group, non distinct" (:startgrouptag))
  3006. (const :tag "Group tags delimiter" (:grouptags))
  3007. (const :tag "End radio group" (:endgroup))
  3008. (const :tag "End tag group, non distinct" (:endgrouptag))
  3009. (const :tag "New line" (:newline)))))
  3010. (defcustom org-tag-persistent-alist nil
  3011. "Tags always available in Org files.
  3012. The value of this variable is an alist. Associations either:
  3013. (TAG)
  3014. (TAG . SELECT)
  3015. (SPECIAL)
  3016. where TAG is a tag as a string, SELECT is a character, used to
  3017. select that tag through the fast tag selection interface, and
  3018. SPECIAL is one of the following keywords: `:startgroup',
  3019. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  3020. `:newline'. These keywords are used to define a hierarchy of
  3021. tags. See manual for details.
  3022. Unlike to `org-tag-alist', tags defined in this variable do not
  3023. depend on a local TAGS keyword. Instead, to disable these tags
  3024. on a per-file basis, insert anywhere in the file:
  3025. #+STARTUP: noptag"
  3026. :group 'org-tags
  3027. :type '(repeat
  3028. (choice
  3029. (cons (string :tag "Tag name")
  3030. (character :tag "Access char"))
  3031. (const :tag "Start radio group" (:startgroup))
  3032. (const :tag "Start tag group, non distinct" (:startgrouptag))
  3033. (const :tag "Group tags delimiter" (:grouptags))
  3034. (const :tag "End radio group" (:endgroup))
  3035. (const :tag "End tag group, non distinct" (:endgrouptag))
  3036. (const :tag "New line" (:newline)))))
  3037. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3038. "If non-nil, always offer completion for all tags of all agenda files.
  3039. Instead of customizing this variable directly, you might want to
  3040. set it locally for capture buffers, because there no list of
  3041. tags in that file can be created dynamically (there are none).
  3042. (add-hook \\='org-capture-mode-hook
  3043. (lambda ()
  3044. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3045. :group 'org-tags
  3046. :version "24.1"
  3047. :type 'boolean)
  3048. (defvar org-file-tags nil
  3049. "List of tags that can be inherited by all entries in the file.
  3050. The tags will be inherited if the variable `org-use-tag-inheritance'
  3051. says they should be.
  3052. This variable is populated from #+FILETAGS lines.")
  3053. (defcustom org-use-fast-tag-selection 'auto
  3054. "Non-nil means use fast tag selection scheme.
  3055. This is a special interface to select and deselect tags with single keys.
  3056. When nil, fast selection is never used.
  3057. When the symbol `auto', fast selection is used if and only if selection
  3058. characters for tags have been configured, either through the variable
  3059. `org-tag-alist' or through a #+TAGS line in the buffer.
  3060. When t, fast selection is always used and selection keys are assigned
  3061. automatically if necessary."
  3062. :group 'org-tags
  3063. :type '(choice
  3064. (const :tag "Always" t)
  3065. (const :tag "Never" nil)
  3066. (const :tag "When selection characters are configured" auto)))
  3067. (defcustom org-fast-tag-selection-single-key nil
  3068. "Non-nil means fast tag selection exits after first change.
  3069. When nil, you have to press RET to exit it.
  3070. During fast tag selection, you can toggle this flag with `C-c'.
  3071. This variable can also have the value `expert'. In this case, the window
  3072. displaying the tags menu is not even shown, until you press C-c again."
  3073. :group 'org-tags
  3074. :type '(choice
  3075. (const :tag "No" nil)
  3076. (const :tag "Yes" t)
  3077. (const :tag "Expert" expert)))
  3078. (defvar org-fast-tag-selection-include-todo nil
  3079. "Non-nil means fast tags selection interface will also offer TODO states.
  3080. This is an undocumented feature, you should not rely on it.")
  3081. (defcustom org-tags-column -77
  3082. "The column to which tags should be indented in a headline.
  3083. If this number is positive, it specifies the column. If it is negative,
  3084. it means that the tags should be flushright to that column. For example,
  3085. -80 works well for a normal 80 character screen.
  3086. When 0, place tags directly after headline text, with only one space in
  3087. between."
  3088. :group 'org-tags
  3089. :type 'integer)
  3090. (defcustom org-auto-align-tags t
  3091. "Non-nil keeps tags aligned when modifying headlines.
  3092. Some operations (i.e. demoting) change the length of a headline and
  3093. therefore shift the tags around. With this option turned on, after
  3094. each such operation the tags are again aligned to `org-tags-column'."
  3095. :group 'org-tags
  3096. :type 'boolean)
  3097. (defcustom org-use-tag-inheritance t
  3098. "Non-nil means tags in levels apply also for sublevels.
  3099. When nil, only the tags directly given in a specific line apply there.
  3100. This may also be a list of tags that should be inherited, or a regexp that
  3101. matches tags that should be inherited. Additional control is possible
  3102. with the variable `org-tags-exclude-from-inheritance' which gives an
  3103. explicit list of tags to be excluded from inheritance, even if the value of
  3104. `org-use-tag-inheritance' would select it for inheritance.
  3105. If this option is t, a match early-on in a tree can lead to a large
  3106. number of matches in the subtree when constructing the agenda or creating
  3107. a sparse tree. If you only want to see the first match in a tree during
  3108. a search, check out the variable `org-tags-match-list-sublevels'."
  3109. :group 'org-tags
  3110. :type '(choice
  3111. (const :tag "Not" nil)
  3112. (const :tag "Always" t)
  3113. (repeat :tag "Specific tags" (string :tag "Tag"))
  3114. (regexp :tag "Tags matched by regexp")))
  3115. (defcustom org-tags-exclude-from-inheritance nil
  3116. "List of tags that should never be inherited.
  3117. This is a way to exclude a few tags from inheritance. For way to do
  3118. the opposite, to actively allow inheritance for selected tags,
  3119. see the variable `org-use-tag-inheritance'."
  3120. :group 'org-tags
  3121. :type '(repeat (string :tag "Tag")))
  3122. (defun org-tag-inherit-p (tag)
  3123. "Check if TAG is one that should be inherited."
  3124. (cond
  3125. ((member tag org-tags-exclude-from-inheritance) nil)
  3126. ((eq org-use-tag-inheritance t) t)
  3127. ((not org-use-tag-inheritance) nil)
  3128. ((stringp org-use-tag-inheritance)
  3129. (string-match org-use-tag-inheritance tag))
  3130. ((listp org-use-tag-inheritance)
  3131. (member tag org-use-tag-inheritance))
  3132. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3133. (defcustom org-tags-match-list-sublevels t
  3134. "Non-nil means list also sublevels of headlines matching a search.
  3135. This variable applies to tags/property searches, and also to stuck
  3136. projects because this search is based on a tags match as well.
  3137. When set to the symbol `indented', sublevels are indented with
  3138. leading dots.
  3139. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3140. the sublevels of a headline matching a tag search often also match
  3141. the same search. Listing all of them can create very long lists.
  3142. Setting this variable to nil causes subtrees of a match to be skipped.
  3143. This variable is semi-obsolete and probably should always be true. It
  3144. is better to limit inheritance to certain tags using the variables
  3145. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3146. :group 'org-tags
  3147. :type '(choice
  3148. (const :tag "No, don't list them" nil)
  3149. (const :tag "Yes, do list them" t)
  3150. (const :tag "List them, indented with leading dots" indented)))
  3151. (defcustom org-tags-sort-function nil
  3152. "When set, tags are sorted using this function as a comparator."
  3153. :group 'org-tags
  3154. :type '(choice
  3155. (const :tag "No sorting" nil)
  3156. (const :tag "Alphabetical" string<)
  3157. (const :tag "Reverse alphabetical" string>)
  3158. (function :tag "Custom function" nil)))
  3159. (defvar org-tags-history nil
  3160. "History of minibuffer reads for tags.")
  3161. (defvar org-last-tags-completion-table nil
  3162. "The last used completion table for tags.")
  3163. (defvar org-after-tags-change-hook nil
  3164. "Hook that is run after the tags in a line have changed.")
  3165. (defgroup org-properties nil
  3166. "Options concerning properties in Org-mode."
  3167. :tag "Org Properties"
  3168. :group 'org)
  3169. (defcustom org-property-format "%-10s %s"
  3170. "How property key/value pairs should be formatted by `indent-line'.
  3171. When `indent-line' hits a property definition, it will format the line
  3172. according to this format, mainly to make sure that the values are
  3173. lined-up with respect to each other."
  3174. :group 'org-properties
  3175. :type 'string)
  3176. (defcustom org-properties-postprocess-alist nil
  3177. "Alist of properties and functions to adjust inserted values.
  3178. Elements of this alist must be of the form
  3179. ([string] [function])
  3180. where [string] must be a property name and [function] must be a
  3181. lambda expression: this lambda expression must take one argument,
  3182. the value to adjust, and return the new value as a string.
  3183. For example, this element will allow the property \"Remaining\"
  3184. to be updated wrt the relation between the \"Effort\" property
  3185. and the clock summary:
  3186. ((\"Remaining\" (lambda(value)
  3187. (let ((clocksum (org-clock-sum-current-item))
  3188. (effort (org-duration-string-to-minutes
  3189. (org-entry-get (point) \"Effort\"))))
  3190. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3191. :group 'org-properties
  3192. :version "24.1"
  3193. :type '(alist :key-type (string :tag "Property")
  3194. :value-type (function :tag "Function")))
  3195. (defcustom org-use-property-inheritance nil
  3196. "Non-nil means properties apply also for sublevels.
  3197. This setting is chiefly used during property searches. Turning it on can
  3198. cause significant overhead when doing a search, which is why it is not
  3199. on by default.
  3200. When nil, only the properties directly given in the current entry count.
  3201. When t, every property is inherited. The value may also be a list of
  3202. properties that should have inheritance, or a regular expression matching
  3203. properties that should be inherited.
  3204. However, note that some special properties use inheritance under special
  3205. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3206. and the properties ending in \"_ALL\" when they are used as descriptor
  3207. for valid values of a property.
  3208. Note for programmers:
  3209. When querying an entry with `org-entry-get', you can control if inheritance
  3210. should be used. By default, `org-entry-get' looks only at the local
  3211. properties. You can request inheritance by setting the inherit argument
  3212. to t (to force inheritance) or to `selective' (to respect the setting
  3213. in this variable)."
  3214. :group 'org-properties
  3215. :type '(choice
  3216. (const :tag "Not" nil)
  3217. (const :tag "Always" t)
  3218. (repeat :tag "Specific properties" (string :tag "Property"))
  3219. (regexp :tag "Properties matched by regexp")))
  3220. (defun org-property-inherit-p (property)
  3221. "Check if PROPERTY is one that should be inherited."
  3222. (cond
  3223. ((eq org-use-property-inheritance t) t)
  3224. ((not org-use-property-inheritance) nil)
  3225. ((stringp org-use-property-inheritance)
  3226. (string-match org-use-property-inheritance property))
  3227. ((listp org-use-property-inheritance)
  3228. (member property org-use-property-inheritance))
  3229. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3230. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3231. "The default column format, if no other format has been defined.
  3232. This variable can be set on the per-file basis by inserting a line
  3233. #+COLUMNS: %25ITEM ....."
  3234. :group 'org-properties
  3235. :type 'string)
  3236. (defcustom org-columns-ellipses ".."
  3237. "The ellipses to be used when a field in column view is truncated.
  3238. When this is the empty string, as many characters as possible are shown,
  3239. but then there will be no visual indication that the field has been truncated.
  3240. When this is a string of length N, the last N characters of a truncated
  3241. field are replaced by this string. If the column is narrower than the
  3242. ellipses string, only part of the ellipses string will be shown."
  3243. :group 'org-properties
  3244. :type 'string)
  3245. (defconst org-global-properties-fixed
  3246. '(("VISIBILITY_ALL" . "folded children content all")
  3247. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3248. "List of property/value pairs that can be inherited by any entry.
  3249. These are fixed values, for the preset properties. The user variable
  3250. that can be used to add to this list is `org-global-properties'.
  3251. The entries in this list are cons cells where the car is a property
  3252. name and cdr is a string with the value. If the value represents
  3253. multiple items like an \"_ALL\" property, separate the items by
  3254. spaces.")
  3255. (defcustom org-global-properties nil
  3256. "List of property/value pairs that can be inherited by any entry.
  3257. This list will be combined with the constant `org-global-properties-fixed'.
  3258. The entries in this list are cons cells where the car is a property
  3259. name and cdr is a string with the value.
  3260. You can set buffer-local values for the same purpose in the variable
  3261. `org-file-properties' this by adding lines like
  3262. #+PROPERTY: NAME VALUE"
  3263. :group 'org-properties
  3264. :type '(repeat
  3265. (cons (string :tag "Property")
  3266. (string :tag "Value"))))
  3267. (defvar-local org-file-properties nil
  3268. "List of property/value pairs that can be inherited by any entry.
  3269. Valid for the current buffer.
  3270. This variable is populated from #+PROPERTY lines.")
  3271. (defgroup org-agenda nil
  3272. "Options concerning agenda views in Org-mode."
  3273. :tag "Org Agenda"
  3274. :group 'org)
  3275. (defvar-local org-category nil
  3276. "Variable used by org files to set a category for agenda display.
  3277. Such files should use a file variable to set it, for example
  3278. # -*- mode: org; org-category: \"ELisp\"
  3279. or contain a special line
  3280. #+CATEGORY: ELisp
  3281. If the file does not specify a category, then file's base name
  3282. is used instead.")
  3283. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3284. (defcustom org-agenda-files nil
  3285. "The files to be used for agenda display.
  3286. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3287. \\[org-remove-file]. You can also use customize to edit the list.
  3288. If an entry is a directory, all files in that directory that are matched by
  3289. `org-agenda-file-regexp' will be part of the file list.
  3290. If the value of the variable is not a list but a single file name, then
  3291. the list of agenda files is actually stored and maintained in that file, one
  3292. agenda file per line. In this file paths can be given relative to
  3293. `org-directory'. Tilde expansion and environment variable substitution
  3294. are also made."
  3295. :group 'org-agenda
  3296. :type '(choice
  3297. (repeat :tag "List of files and directories" file)
  3298. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3299. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3300. "Regular expression to match files for `org-agenda-files'.
  3301. If any element in the list in that variable contains a directory instead
  3302. of a normal file, all files in that directory that are matched by this
  3303. regular expression will be included."
  3304. :group 'org-agenda
  3305. :type 'regexp)
  3306. (defcustom org-agenda-text-search-extra-files nil
  3307. "List of extra files to be searched by text search commands.
  3308. These files will be searched in addition to the agenda files by the
  3309. commands `org-search-view' (`\\[org-agenda] s') \
  3310. and `org-occur-in-agenda-files'.
  3311. Note that these files will only be searched for text search commands,
  3312. not for the other agenda views like todo lists, tag searches or the weekly
  3313. agenda. This variable is intended to list notes and possibly archive files
  3314. that should also be searched by these two commands.
  3315. In fact, if the first element in the list is the symbol `agenda-archives',
  3316. then all archive files of all agenda files will be added to the search
  3317. scope."
  3318. :group 'org-agenda
  3319. :type '(set :greedy t
  3320. (const :tag "Agenda Archives" agenda-archives)
  3321. (repeat :inline t (file))))
  3322. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3323. 'org-agenda-text-search-extra-files)
  3324. (defcustom org-agenda-skip-unavailable-files nil
  3325. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3326. A nil value means to remove them, after a query, from the list."
  3327. :group 'org-agenda
  3328. :type 'boolean)
  3329. (defcustom org-calendar-to-agenda-key [?c]
  3330. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3331. The command `org-calendar-goto-agenda' will be bound to this key. The
  3332. default is the character `c' because then `c' can be used to switch back and
  3333. forth between agenda and calendar."
  3334. :group 'org-agenda
  3335. :type 'sexp)
  3336. (defcustom org-calendar-insert-diary-entry-key [?i]
  3337. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3338. This option is irrelevant until `org-agenda-diary-file' has been configured
  3339. to point to an Org-mode file. When that is the case, the command
  3340. `org-agenda-diary-entry' will be bound to the key given here, by default
  3341. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3342. if you want to continue doing this, you need to change this to a different
  3343. key."
  3344. :group 'org-agenda
  3345. :type 'sexp)
  3346. (defcustom org-agenda-diary-file 'diary-file
  3347. "File to which to add new entries with the `i' key in agenda and calendar.
  3348. When this is the symbol `diary-file', the functionality in the Emacs
  3349. calendar will be used to add entries to the `diary-file'. But when this
  3350. points to a file, `org-agenda-diary-entry' will be used instead."
  3351. :group 'org-agenda
  3352. :type '(choice
  3353. (const :tag "The standard Emacs diary file" diary-file)
  3354. (file :tag "Special Org file diary entries")))
  3355. (eval-after-load "calendar"
  3356. '(progn
  3357. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3358. 'org-calendar-goto-agenda)
  3359. (add-hook 'calendar-mode-hook
  3360. (lambda ()
  3361. (unless (eq org-agenda-diary-file 'diary-file)
  3362. (define-key calendar-mode-map
  3363. org-calendar-insert-diary-entry-key
  3364. 'org-agenda-diary-entry))))))
  3365. (defgroup org-latex nil
  3366. "Options for embedding LaTeX code into Org-mode."
  3367. :tag "Org LaTeX"
  3368. :group 'org)
  3369. (defcustom org-format-latex-options
  3370. '(:foreground default :background default :scale 1.0
  3371. :html-foreground "Black" :html-background "Transparent"
  3372. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3373. "Options for creating images from LaTeX fragments.
  3374. This is a property list with the following properties:
  3375. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3376. `default' means use the foreground of the default face.
  3377. `auto' means use the foreground from the text face.
  3378. :background the background color, or \"Transparent\".
  3379. `default' means use the background of the default face.
  3380. `auto' means use the background from the text face.
  3381. :scale a scaling factor for the size of the images, to get more pixels
  3382. :html-foreground, :html-background, :html-scale
  3383. the same numbers for HTML export.
  3384. :matchers a list indicating which matchers should be used to
  3385. find LaTeX fragments. Valid members of this list are:
  3386. \"begin\" find environments
  3387. \"$1\" find single characters surrounded by $.$
  3388. \"$\" find math expressions surrounded by $...$
  3389. \"$$\" find math expressions surrounded by $$....$$
  3390. \"\\(\" find math expressions surrounded by \\(...\\)
  3391. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3392. :group 'org-latex
  3393. :type 'plist)
  3394. (defcustom org-format-latex-signal-error t
  3395. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3396. When nil, just push out a message."
  3397. :group 'org-latex
  3398. :version "24.1"
  3399. :type 'boolean)
  3400. (defcustom org-latex-to-mathml-jar-file nil
  3401. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3402. Use this to specify additional executable file say a jar file.
  3403. When using MathToWeb as the converter, specify the full-path to
  3404. your mathtoweb.jar file."
  3405. :group 'org-latex
  3406. :version "24.1"
  3407. :type '(choice
  3408. (const :tag "None" nil)
  3409. (file :tag "JAR file" :must-match t)))
  3410. (defcustom org-latex-to-mathml-convert-command nil
  3411. "Command to convert LaTeX fragments to MathML.
  3412. Replace format-specifiers in the command as noted below and use
  3413. `shell-command' to convert LaTeX to MathML.
  3414. %j: Executable file in fully expanded form as specified by
  3415. `org-latex-to-mathml-jar-file'.
  3416. %I: Input LaTeX file in fully expanded form.
  3417. %i: The latex fragment to be converted.
  3418. %o: Output MathML file.
  3419. This command is used by `org-create-math-formula'.
  3420. When using MathToWeb as the converter, set this option to
  3421. \"java -jar %j -unicode -force -df %o %I\".
  3422. When using LaTeXML set this option to
  3423. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3424. :group 'org-latex
  3425. :version "24.1"
  3426. :type '(choice
  3427. (const :tag "None" nil)
  3428. (string :tag "\nShell command")))
  3429. (define-obsolete-variable-alias
  3430. 'org-latex-create-formula-image-program
  3431. 'org-preview-latex-default-process "25.1")
  3432. (defcustom org-preview-latex-default-process 'dvipng
  3433. "The default process to convert LaTeX fragments to image files.
  3434. All available processes and theirs documents can be found in
  3435. `org-preview-latex-process-alist', which see."
  3436. :group 'org-latex
  3437. :version "25.2"
  3438. :package-version '(Org . "9.0")
  3439. :type 'symbol)
  3440. (defcustom org-preview-latex-process-alist
  3441. '((dvipng
  3442. :programs ("latex" "dvipng" "gs")
  3443. :description "dvi > png"
  3444. :message "you need to install the programs: latex, dvipng and ghostscript."
  3445. :image-input-type "dvi"
  3446. :image-output-type "png"
  3447. :image-size-adjust (1.0 . 1.0)
  3448. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3449. :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %b.png %f"))
  3450. (dvisvgm
  3451. :programs ("latex" "dvisvgm" "gs")
  3452. :description "dvi > svg"
  3453. :message "you need to install the programs: latex, dvisvgm and ghostscript."
  3454. :use-xcolor t
  3455. :image-input-type "dvi"
  3456. :image-output-type "svg"
  3457. :image-size-adjust (1.7 . 1.5)
  3458. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3459. :image-converter ("dvisvgm %f -n -b min -c %S -o %b.svg"))
  3460. (imagemagick
  3461. :programs ("latex" "convert" "gs")
  3462. :description "pdf > png"
  3463. :message
  3464. "you need to install the programs: latex, imagemagick and ghostscript."
  3465. :use-xcolor t
  3466. :image-input-type "pdf"
  3467. :image-output-type "png"
  3468. :image-size-adjust (1.0 . 1.0)
  3469. :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
  3470. :image-converter
  3471. ("convert -density %D -trim -antialias %f -quality 100 %b.png")))
  3472. "Definitions of external processes for LaTeX previewing.
  3473. Org mode can use some external commands to generate TeX snippet's images for
  3474. previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
  3475. `org-create-formula-image' how to call them.
  3476. The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
  3477. PROPERTIES accepts the following attributes:
  3478. :programs list of strings, required programs.
  3479. :description string, describe the process.
  3480. :message string, message it when required programs cannot be found.
  3481. :image-input-type string, input file type of image converter (e.g., \"dvi\").
  3482. :image-output-type string, output file type of image converter (e.g., \"png\").
  3483. :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
  3484. deal with background and foreground color of image.
  3485. Otherwise, dvipng style background and foregroud color
  3486. format are generated. You may then refer to them in
  3487. command options with \"%F\" and \"%B\".
  3488. :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
  3489. image size showed in buffer and the cdr element is for
  3490. HTML file. This option is only useful for process
  3491. developers, users should use variable
  3492. `org-format-latex-options' instead.
  3493. :post-clean list of strings, files matched are to be cleaned up once
  3494. the image is generated. When nil, the files with \".dvi\",
  3495. \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
  3496. \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
  3497. be cleaned up.
  3498. :latex-header list of strings, the LaTeX header of the snippet file.
  3499. When nil, the fallback value is used instead, which is
  3500. controlled by `org-format-latex-header',
  3501. `org-latex-default-packages-alist' and
  3502. `org-latex-packages-alist', which see.
  3503. :latex-compiler list of LaTeX commands, as strings. Each of them is given
  3504. to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
  3505. replaced with values defined below.
  3506. :image-converter list of image converter commands strings. Each of them is
  3507. given to the shell and supports any of the following
  3508. place-holders defined below.
  3509. Place-holders used by `:image-converter' and `:latex-compiler':
  3510. %f input file name.
  3511. %b base name of input file.
  3512. %o base directory of input file.
  3513. Place-holders only used by `:image-converter':
  3514. %F foreground of image
  3515. %B background of image
  3516. %D dpi, which is used to adjust image size by some processing commands.
  3517. %S the image size scale ratio, which is used to adjust image size by some
  3518. processing commands."
  3519. :group 'org-latex
  3520. :version "25.2"
  3521. :package-version '(Org . "9.0")
  3522. :type '(alist :tag "LaTeX to image backends"
  3523. :value-type (plist)))
  3524. (define-obsolete-variable-alias
  3525. 'org-latex-preview-ltxpng-directory
  3526. 'org-preview-latex-image-directory "25.1")
  3527. (defcustom org-preview-latex-image-directory "ltximg/"
  3528. "Path to store latex preview images.
  3529. A relative path here creates many directories relative to the
  3530. processed org files paths. An absolute path puts all preview
  3531. images at the same place."
  3532. :group 'org-latex
  3533. :version "25.1"
  3534. :package-version '(Org . "9.0")
  3535. :type 'string)
  3536. (defun org-format-latex-mathml-available-p ()
  3537. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3538. (save-match-data
  3539. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3540. org-latex-to-mathml-convert-command)
  3541. (let ((executable (car (split-string
  3542. org-latex-to-mathml-convert-command))))
  3543. (when (executable-find executable)
  3544. (if (string-match
  3545. "%j" org-latex-to-mathml-convert-command)
  3546. (file-readable-p org-latex-to-mathml-jar-file)
  3547. t))))))
  3548. (defcustom org-format-latex-header "\\documentclass{article}
  3549. \\usepackage[usenames]{color}
  3550. \[PACKAGES]
  3551. \[DEFAULT-PACKAGES]
  3552. \\pagestyle{empty} % do not remove
  3553. % The settings below are copied from fullpage.sty
  3554. \\setlength{\\textwidth}{\\paperwidth}
  3555. \\addtolength{\\textwidth}{-3cm}
  3556. \\setlength{\\oddsidemargin}{1.5cm}
  3557. \\addtolength{\\oddsidemargin}{-2.54cm}
  3558. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3559. \\setlength{\\textheight}{\\paperheight}
  3560. \\addtolength{\\textheight}{-\\headheight}
  3561. \\addtolength{\\textheight}{-\\headsep}
  3562. \\addtolength{\\textheight}{-\\footskip}
  3563. \\addtolength{\\textheight}{-3cm}
  3564. \\setlength{\\topmargin}{1.5cm}
  3565. \\addtolength{\\topmargin}{-2.54cm}"
  3566. "The document header used for processing LaTeX fragments.
  3567. It is imperative that this header make sure that no page number
  3568. appears on the page. The package defined in the variables
  3569. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3570. will either replace the placeholder \"[PACKAGES]\" in this
  3571. header, or they will be appended."
  3572. :group 'org-latex
  3573. :type 'string)
  3574. (defun org-set-packages-alist (var val)
  3575. "Set the packages alist and make sure it has 3 elements per entry."
  3576. (set var (mapcar (lambda (x)
  3577. (if (and (consp x) (= (length x) 2))
  3578. (list (car x) (nth 1 x) t)
  3579. x))
  3580. val)))
  3581. (defun org-get-packages-alist (var)
  3582. "Get the packages alist and make sure it has 3 elements per entry."
  3583. (mapcar (lambda (x)
  3584. (if (and (consp x) (= (length x) 2))
  3585. (list (car x) (nth 1 x) t)
  3586. x))
  3587. (default-value var)))
  3588. (defcustom org-latex-default-packages-alist
  3589. '(("AUTO" "inputenc" t ("pdflatex"))
  3590. ("T1" "fontenc" t ("pdflatex"))
  3591. ("" "graphicx" t)
  3592. ("" "grffile" t)
  3593. ("" "longtable" nil)
  3594. ("" "wrapfig" nil)
  3595. ("" "rotating" nil)
  3596. ("normalem" "ulem" t)
  3597. ("" "amsmath" t)
  3598. ("" "textcomp" t)
  3599. ("" "amssymb" t)
  3600. ("" "capt-of" nil)
  3601. ("" "hyperref" nil))
  3602. "Alist of default packages to be inserted in the header.
  3603. Change this only if one of the packages here causes an
  3604. incompatibility with another package you are using.
  3605. The packages in this list are needed by one part or another of
  3606. Org mode to function properly:
  3607. - inputenc, fontenc: for basic font and character selection
  3608. - graphicx: for including images
  3609. - grffile: allow periods and spaces in graphics file names
  3610. - longtable: For multipage tables
  3611. - wrapfig: for figure placement
  3612. - rotating: for sideways figures and tables
  3613. - ulem: for underline and strike-through
  3614. - amsmath: for subscript and superscript and math environments
  3615. - textcomp, amssymb: for various symbols used
  3616. for interpreting the entities in `org-entities'. You can skip
  3617. some of these packages if you don't use any of their symbols.
  3618. - capt-of: for captions outside of floats
  3619. - hyperref: for cross references
  3620. Therefore you should not modify this variable unless you know
  3621. what you are doing. The one reason to change it anyway is that
  3622. you might be loading some other package that conflicts with one
  3623. of the default packages. Each element is either a cell or
  3624. a string.
  3625. A cell is of the format
  3626. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3627. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3628. when compiling LaTeX snippets into images for inclusion into
  3629. non-LaTeX output. COMPILERS is a list of compilers that should
  3630. include the package, see `org-latex-compiler'. If the document
  3631. compiler is not in the list, and the list is non-nil, the package
  3632. will not be inserted in the final document.
  3633. A string will be inserted as-is in the header of the document."
  3634. :group 'org-latex
  3635. :group 'org-export-latex
  3636. :set 'org-set-packages-alist
  3637. :get 'org-get-packages-alist
  3638. :version "25.1"
  3639. :package-version '(Org . "8.3")
  3640. :type '(repeat
  3641. (choice
  3642. (list :tag "options/package pair"
  3643. (string :tag "options")
  3644. (string :tag "package")
  3645. (boolean :tag "Snippet"))
  3646. (string :tag "A line of LaTeX"))))
  3647. (defcustom org-latex-packages-alist nil
  3648. "Alist of packages to be inserted in every LaTeX header.
  3649. These will be inserted after `org-latex-default-packages-alist'.
  3650. Each element is either a cell or a string.
  3651. A cell is of the format:
  3652. (\"options\" \"package\" SNIPPET-FLAG)
  3653. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3654. needed when turning LaTeX snippets into images for inclusion into
  3655. non-LaTeX output.
  3656. A string will be inserted as-is in the header of the document.
  3657. Make sure that you only list packages here which:
  3658. - you want in every file;
  3659. - do not conflict with the setup in `org-format-latex-header';
  3660. - do not conflict with the default packages in
  3661. `org-latex-default-packages-alist'."
  3662. :group 'org-latex
  3663. :group 'org-export-latex
  3664. :set 'org-set-packages-alist
  3665. :get 'org-get-packages-alist
  3666. :type '(repeat
  3667. (choice
  3668. (list :tag "options/package pair"
  3669. (string :tag "options")
  3670. (string :tag "package")
  3671. (boolean :tag "Snippet"))
  3672. (string :tag "A line of LaTeX"))))
  3673. (defgroup org-appearance nil
  3674. "Settings for Org-mode appearance."
  3675. :tag "Org Appearance"
  3676. :group 'org)
  3677. (defcustom org-level-color-stars-only nil
  3678. "Non-nil means fontify only the stars in each headline.
  3679. When nil, the entire headline is fontified.
  3680. Changing it requires restart of `font-lock-mode' to become effective
  3681. also in regions already fontified."
  3682. :group 'org-appearance
  3683. :type 'boolean)
  3684. (defcustom org-hide-leading-stars nil
  3685. "Non-nil means hide the first N-1 stars in a headline.
  3686. This works by using the face `org-hide' for these stars. This
  3687. face is white for a light background, and black for a dark
  3688. background. You may have to customize the face `org-hide' to
  3689. make this work.
  3690. Changing it requires restart of `font-lock-mode' to become effective
  3691. also in regions already fontified.
  3692. You may also set this on a per-file basis by adding one of the following
  3693. lines to the buffer:
  3694. #+STARTUP: hidestars
  3695. #+STARTUP: showstars"
  3696. :group 'org-appearance
  3697. :type 'boolean)
  3698. (defcustom org-hidden-keywords nil
  3699. "List of symbols corresponding to keywords to be hidden the org buffer.
  3700. For example, a value \\='(title) for this list will make the document's title
  3701. appear in the buffer without the initial #+TITLE: keyword."
  3702. :group 'org-appearance
  3703. :version "24.1"
  3704. :type '(set (const :tag "#+AUTHOR" author)
  3705. (const :tag "#+DATE" date)
  3706. (const :tag "#+EMAIL" email)
  3707. (const :tag "#+TITLE" title)))
  3708. (defcustom org-custom-properties nil
  3709. "List of properties (as strings) with a special meaning.
  3710. The default use of these custom properties is to let the user
  3711. hide them with `org-toggle-custom-properties-visibility'."
  3712. :group 'org-properties
  3713. :group 'org-appearance
  3714. :version "24.3"
  3715. :type '(repeat (string :tag "Property Name")))
  3716. (defcustom org-fontify-done-headline nil
  3717. "Non-nil means change the face of a headline if it is marked DONE.
  3718. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3719. When this is non-nil, the headline after the keyword is set to the
  3720. `org-headline-done' as an additional indication."
  3721. :group 'org-appearance
  3722. :type 'boolean)
  3723. (defcustom org-fontify-emphasized-text t
  3724. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3725. Changing this variable requires a restart of Emacs to take effect."
  3726. :group 'org-appearance
  3727. :type 'boolean)
  3728. (defcustom org-fontify-whole-heading-line nil
  3729. "Non-nil means fontify the whole line for headings.
  3730. This is useful when setting a background color for the
  3731. org-level-* faces."
  3732. :group 'org-appearance
  3733. :type 'boolean)
  3734. (defcustom org-highlight-latex-and-related nil
  3735. "Non-nil means highlight LaTeX related syntax in the buffer.
  3736. When non nil, the value should be a list containing any of the
  3737. following symbols:
  3738. `latex' Highlight LaTeX snippets and environments.
  3739. `script' Highlight subscript and superscript.
  3740. `entities' Highlight entities."
  3741. :group 'org-appearance
  3742. :version "24.4"
  3743. :package-version '(Org . "8.0")
  3744. :type '(choice
  3745. (const :tag "No highlighting" nil)
  3746. (set :greedy t :tag "Highlight"
  3747. (const :tag "LaTeX snippets and environments" latex)
  3748. (const :tag "Subscript and superscript" script)
  3749. (const :tag "Entities" entities))))
  3750. (defcustom org-hide-emphasis-markers nil
  3751. "Non-nil mean font-lock should hide the emphasis marker characters."
  3752. :group 'org-appearance
  3753. :type 'boolean)
  3754. (defcustom org-hide-macro-markers nil
  3755. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3756. :group 'org-appearance
  3757. :type 'boolean)
  3758. (defcustom org-pretty-entities nil
  3759. "Non-nil means show entities as UTF8 characters.
  3760. When nil, the \\name form remains in the buffer."
  3761. :group 'org-appearance
  3762. :version "24.1"
  3763. :type 'boolean)
  3764. (defcustom org-pretty-entities-include-sub-superscripts t
  3765. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3766. :group 'org-appearance
  3767. :version "24.1"
  3768. :type 'boolean)
  3769. (defvar org-emph-re nil
  3770. "Regular expression for matching emphasis.
  3771. After a match, the match groups contain these elements:
  3772. 0 The match of the full regular expression, including the characters
  3773. before and after the proper match
  3774. 1 The character before the proper match, or empty at beginning of line
  3775. 2 The proper match, including the leading and trailing markers
  3776. 3 The leading marker like * or /, indicating the type of highlighting
  3777. 4 The text between the emphasis markers, not including the markers
  3778. 5 The character after the match, empty at the end of a line")
  3779. (defvar org-verbatim-re nil
  3780. "Regular expression for matching verbatim text.")
  3781. (defvar org-emphasis-regexp-components) ; defined just below
  3782. (defvar org-emphasis-alist) ; defined just below
  3783. (defun org-set-emph-re (var val)
  3784. "Set variable and compute the emphasis regular expression."
  3785. (set var val)
  3786. (when (and (boundp 'org-emphasis-alist)
  3787. (boundp 'org-emphasis-regexp-components)
  3788. org-emphasis-alist org-emphasis-regexp-components)
  3789. (let* ((e org-emphasis-regexp-components)
  3790. (pre (car e))
  3791. (post (nth 1 e))
  3792. (border (nth 2 e))
  3793. (body (nth 3 e))
  3794. (nl (nth 4 e))
  3795. (body1 (concat body "*?"))
  3796. (markers (mapconcat 'car org-emphasis-alist ""))
  3797. (vmarkers (mapconcat
  3798. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3799. org-emphasis-alist "")))
  3800. ;; make sure special characters appear at the right position in the class
  3801. (if (string-match "\\^" markers)
  3802. (setq markers (concat (replace-match "" t t markers) "^")))
  3803. (if (string-match "-" markers)
  3804. (setq markers (concat (replace-match "" t t markers) "-")))
  3805. (if (string-match "\\^" vmarkers)
  3806. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3807. (if (string-match "-" vmarkers)
  3808. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3809. (if (> nl 0)
  3810. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3811. (int-to-string nl) "\\}")))
  3812. ;; Make the regexp
  3813. (setq org-emph-re
  3814. (concat "\\([" pre "]\\|^\\)"
  3815. "\\("
  3816. "\\([" markers "]\\)"
  3817. "\\("
  3818. "[^" border "]\\|"
  3819. "[^" border "]"
  3820. body1
  3821. "[^" border "]"
  3822. "\\)"
  3823. "\\3\\)"
  3824. "\\([" post "]\\|$\\)"))
  3825. (setq org-verbatim-re
  3826. (concat "\\([" pre "]\\|^\\)"
  3827. "\\("
  3828. "\\([" vmarkers "]\\)"
  3829. "\\("
  3830. "[^" border "]\\|"
  3831. "[^" border "]"
  3832. body1
  3833. "[^" border "]"
  3834. "\\)"
  3835. "\\3\\)"
  3836. "\\([" post "]\\|$\\)")))))
  3837. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3838. ;; set this option proved cumbersome. See this message/thread:
  3839. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3840. (defvar org-emphasis-regexp-components
  3841. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3842. "Components used to build the regular expression for emphasis.
  3843. This is a list with five entries. Terminology: In an emphasis string
  3844. like \" *strong word* \", we call the initial space PREMATCH, the final
  3845. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3846. and \"trong wor\" is the body. The different components in this variable
  3847. specify what is allowed/forbidden in each part:
  3848. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3849. post Chars allowed as postmatch. End of line will be allowed too.
  3850. border The chars *forbidden* as border characters.
  3851. body-regexp A regexp like \".\" to match a body character. Don't use
  3852. non-shy groups here, and don't allow newline here.
  3853. newline The maximum number of newlines allowed in an emphasis exp.
  3854. You need to reload Org or to restart Emacs after customizing this.")
  3855. (defcustom org-emphasis-alist
  3856. '(("*" bold)
  3857. ("/" italic)
  3858. ("_" underline)
  3859. ("=" org-verbatim verbatim)
  3860. ("~" org-code verbatim)
  3861. ("+" (:strike-through t)))
  3862. "Alist of characters and faces to emphasize text.
  3863. Text starting and ending with a special character will be emphasized,
  3864. for example *bold*, _underlined_ and /italic/. This variable sets the
  3865. marker characters and the face to be used by font-lock for highlighting
  3866. in Org-mode Emacs buffers.
  3867. You need to reload Org or to restart Emacs after customizing this."
  3868. :group 'org-appearance
  3869. :set 'org-set-emph-re
  3870. :version "24.4"
  3871. :package-version '(Org . "8.0")
  3872. :type '(repeat
  3873. (list
  3874. (string :tag "Marker character")
  3875. (choice
  3876. (face :tag "Font-lock-face")
  3877. (plist :tag "Face property list"))
  3878. (option (const verbatim)))))
  3879. (defvar org-protecting-blocks '("src" "example" "export")
  3880. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3881. This is needed for font-lock setup.")
  3882. ;;; Functions and variables from their packages
  3883. ;; Declared here to avoid compiler warnings
  3884. (defvar mark-active)
  3885. ;; Various packages
  3886. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3887. (declare-function calendar-forward-day "cal-move" (arg))
  3888. (declare-function calendar-goto-date "cal-move" (date))
  3889. (declare-function calendar-goto-today "cal-move" ())
  3890. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3891. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3892. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3893. (declare-function cdlatex-tab "ext:cdlatex" ())
  3894. (declare-function dired-get-filename
  3895. "dired"
  3896. (&optional localp no-error-if-not-filep))
  3897. (declare-function iswitchb-read-buffer
  3898. "iswitchb"
  3899. (prompt &optional
  3900. default require-match _predicate start matches-set))
  3901. (declare-function org-agenda-change-all-lines
  3902. "org-agenda"
  3903. (newhead hdmarker &optional fixface just-this))
  3904. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3905. "org-agenda"
  3906. (&optional end))
  3907. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3908. (declare-function org-agenda-format-item
  3909. "org-agenda"
  3910. (extra txt &optional level category tags dotime
  3911. remove-re habitp))
  3912. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3913. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3914. (declare-function org-agenda-save-markers-for-cut-and-paste
  3915. "org-agenda"
  3916. (beg end))
  3917. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3918. (declare-function org-agenda-skip "org-agenda" ())
  3919. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3920. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3921. (declare-function org-indent-mode "org-indent" (&optional arg))
  3922. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3923. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3924. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3925. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3926. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3927. (declare-function parse-time-string "parse-time" (string))
  3928. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3929. (declare-function table--at-cell-p
  3930. "table"
  3931. (position &optional object at-column))
  3932. (defvar align-mode-rules-list)
  3933. (defvar calc-embedded-close-formula)
  3934. (defvar calc-embedded-open-formula)
  3935. (defvar calc-embedded-open-mode)
  3936. (defvar font-lock-unfontify-region-function)
  3937. (defvar iswitchb-temp-buflist)
  3938. (defvar org-agenda-tags-todo-honor-ignore-options)
  3939. (defvar remember-data-file)
  3940. (defvar texmathp-why)
  3941. ;;;###autoload
  3942. (defun turn-on-orgtbl ()
  3943. "Unconditionally turn on `orgtbl-mode'."
  3944. (require 'org-table)
  3945. (orgtbl-mode 1))
  3946. (defun org-at-table-p (&optional table-type)
  3947. "Non-nil if the cursor is inside an Org table.
  3948. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3949. If `org-enable-table-editor' is nil, return nil unconditionally."
  3950. (and
  3951. org-enable-table-editor
  3952. (save-excursion
  3953. (beginning-of-line)
  3954. (looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3955. (or (not (derived-mode-p 'org-mode))
  3956. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3957. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3958. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3959. (defun org-at-table.el-p ()
  3960. "Non-nil when point is at a table.el table."
  3961. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3962. (let ((element (org-element-at-point)))
  3963. (and (eq (org-element-type element) 'table)
  3964. (eq (org-element-property :type element) 'table.el)))))
  3965. (defun org-table-recognize-table.el ()
  3966. "If there is a table.el table nearby, recognize it and move into it."
  3967. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3968. (beginning-of-line)
  3969. (unless (or (looking-at org-table-dataline-regexp)
  3970. (not (looking-at org-table1-hline-regexp)))
  3971. (forward-line)
  3972. (when (looking-at org-table-any-border-regexp)
  3973. (forward-line -2)))
  3974. (if (re-search-forward "|" (org-table-end t) t)
  3975. (progn
  3976. (require 'table)
  3977. (if (table--at-cell-p (point)) t
  3978. (message "recognizing table.el table...")
  3979. (table-recognize-table)
  3980. (message "recognizing table.el table...done")))
  3981. (error "This should not happen"))))
  3982. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3983. (make-obsolete 'org-table-recognize-table.el
  3984. "please notify the org mailing list if you use this function."
  3985. "Org 9.0")
  3986. (defun org-at-table-hline-p ()
  3987. "Non-nil when point is inside a hline in a table.
  3988. Assume point is already in a table. If `org-enable-table-editor'
  3989. is nil, return nil unconditionally."
  3990. (and org-enable-table-editor
  3991. (save-excursion
  3992. (beginning-of-line)
  3993. (looking-at org-table-hline-regexp))))
  3994. (defun org-table-map-tables (function &optional quietly)
  3995. "Apply FUNCTION to the start of all tables in the buffer."
  3996. (save-excursion
  3997. (save-restriction
  3998. (widen)
  3999. (goto-char (point-min))
  4000. (while (re-search-forward org-table-any-line-regexp nil t)
  4001. (unless quietly
  4002. (message "Mapping tables: %d%%"
  4003. (floor (* 100.0 (point)) (buffer-size))))
  4004. (beginning-of-line 1)
  4005. (when (and (looking-at org-table-line-regexp)
  4006. ;; Exclude tables in src/example/verbatim/clocktable blocks
  4007. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  4008. (save-excursion (funcall function))
  4009. (or (looking-at org-table-line-regexp)
  4010. (forward-char 1)))
  4011. (re-search-forward org-table-any-border-regexp nil 1))))
  4012. (unless quietly (message "Mapping tables: done")))
  4013. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  4014. (declare-function org-clock-update-mode-line "org-clock" ())
  4015. (declare-function org-resolve-clocks "org-clock"
  4016. (&optional also-non-dangling-p prompt last-valid))
  4017. (defun org-at-TBLFM-p (&optional pos)
  4018. "Non-nil when point (or POS) is in #+TBLFM line."
  4019. (save-excursion
  4020. (goto-char (or pos (point)))
  4021. (beginning-of-line)
  4022. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  4023. (eq (org-element-type (org-element-at-point)) 'table))))
  4024. (defvar org-clock-start-time)
  4025. (defvar org-clock-marker (make-marker)
  4026. "Marker recording the last clock-in.")
  4027. (defvar org-clock-hd-marker (make-marker)
  4028. "Marker recording the last clock-in, but the headline position.")
  4029. (defvar org-clock-heading ""
  4030. "The heading of the current clock entry.")
  4031. (defun org-clock-is-active ()
  4032. "Return the buffer where the clock is currently running.
  4033. Return nil if no clock is running."
  4034. (marker-buffer org-clock-marker))
  4035. (defun org-check-running-clock ()
  4036. "Check if the current buffer contains the running clock.
  4037. If yes, offer to stop it and to save the buffer with the changes."
  4038. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  4039. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  4040. (buffer-name))))
  4041. (org-clock-out)
  4042. (when (y-or-n-p "Save changed buffer?")
  4043. (save-buffer))))
  4044. (defun org-clocktable-try-shift (dir n)
  4045. "Check if this line starts a clock table, if yes, shift the time block."
  4046. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  4047. (org-clocktable-shift dir n)))
  4048. ;;;###autoload
  4049. (defun org-clock-persistence-insinuate ()
  4050. "Set up hooks for clock persistence."
  4051. (require 'org-clock)
  4052. (add-hook 'org-mode-hook 'org-clock-load)
  4053. (add-hook 'kill-emacs-hook 'org-clock-save))
  4054. (defgroup org-archive nil
  4055. "Options concerning archiving in Org-mode."
  4056. :tag "Org Archive"
  4057. :group 'org-structure)
  4058. (defcustom org-archive-location "%s_archive::"
  4059. "The location where subtrees should be archived.
  4060. The value of this variable is a string, consisting of two parts,
  4061. separated by a double-colon. The first part is a filename and
  4062. the second part is a headline.
  4063. When the filename is omitted, archiving happens in the same file.
  4064. %s in the filename will be replaced by the current file
  4065. name (without the directory part). Archiving to a different file
  4066. is useful to keep archived entries from contributing to the
  4067. Org-mode Agenda.
  4068. The archived entries will be filed as subtrees of the specified
  4069. headline. When the headline is omitted, the subtrees are simply
  4070. filed away at the end of the file, as top-level entries. Also in
  4071. the heading you can use %s to represent the file name, this can be
  4072. useful when using the same archive for a number of different files.
  4073. Here are a few examples:
  4074. \"%s_archive::\"
  4075. If the current file is Projects.org, archive in file
  4076. Projects.org_archive, as top-level trees. This is the default.
  4077. \"::* Archived Tasks\"
  4078. Archive in the current file, under the top-level headline
  4079. \"* Archived Tasks\".
  4080. \"~/org/archive.org::\"
  4081. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4082. \"~/org/archive.org::* From %s\"
  4083. Archive in file ~/org/archive.org (absolute path), under headlines
  4084. \"From FILENAME\" where file name is the current file name.
  4085. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4086. The \"datetree/\" string is special, signifying to archive
  4087. items to the datetree. Items are placed in either the CLOSED
  4088. date of the item, or the current date if there is no CLOSED date.
  4089. The heading will be a subentry to the current date. There doesn't
  4090. need to be a heading, but there always needs to be a slash after
  4091. datetree. For example, to store archived items directly in the
  4092. datetree, use \"~/org/datetree.org::datetree/\".
  4093. \"basement::** Finished Tasks\"
  4094. Archive in file ./basement (relative path), as level 3 trees
  4095. below the level 2 heading \"** Finished Tasks\".
  4096. You may set this option on a per-file basis by adding to the buffer a
  4097. line like
  4098. #+ARCHIVE: basement::** Finished Tasks
  4099. You may also define it locally for a subtree by setting an ARCHIVE property
  4100. in the entry. If such a property is found in an entry, or anywhere up
  4101. the hierarchy, it will be used."
  4102. :group 'org-archive
  4103. :type 'string)
  4104. (defcustom org-agenda-skip-archived-trees t
  4105. "Non-nil means the agenda will skip any items located in archived trees.
  4106. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4107. variable is no longer recommended, you should leave it at the value t.
  4108. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4109. :group 'org-archive
  4110. :group 'org-agenda-skip
  4111. :type 'boolean)
  4112. (defcustom org-columns-skip-archived-trees t
  4113. "Non-nil means ignore archived trees when creating column view."
  4114. :group 'org-archive
  4115. :group 'org-properties
  4116. :type 'boolean)
  4117. (defcustom org-cycle-open-archived-trees nil
  4118. "Non-nil means `org-cycle' will open archived trees.
  4119. An archived tree is a tree marked with the tag ARCHIVE.
  4120. When nil, archived trees will stay folded. You can still open them with
  4121. normal outline commands like `show-all', but not with the cycling commands."
  4122. :group 'org-archive
  4123. :group 'org-cycle
  4124. :type 'boolean)
  4125. (defcustom org-sparse-tree-open-archived-trees nil
  4126. "Non-nil means sparse tree construction shows matches in archived trees.
  4127. When nil, matches in these trees are highlighted, but the trees are kept in
  4128. collapsed state."
  4129. :group 'org-archive
  4130. :group 'org-sparse-trees
  4131. :type 'boolean)
  4132. (defcustom org-sparse-tree-default-date-type nil
  4133. "The default date type when building a sparse tree.
  4134. When this is nil, a date is a scheduled or a deadline timestamp.
  4135. Otherwise, these types are allowed:
  4136. all: all timestamps
  4137. active: only active timestamps (<...>)
  4138. inactive: only inactive timestamps ([...])
  4139. scheduled: only scheduled timestamps
  4140. deadline: only deadline timestamps"
  4141. :type '(choice (const :tag "Scheduled or deadline" nil)
  4142. (const :tag "All timestamps" all)
  4143. (const :tag "Only active timestamps" active)
  4144. (const :tag "Only inactive timestamps" inactive)
  4145. (const :tag "Only scheduled timestamps" scheduled)
  4146. (const :tag "Only deadline timestamps" deadline)
  4147. (const :tag "Only closed timestamps" closed))
  4148. :version "25.1"
  4149. :package-version '(Org . "8.3")
  4150. :group 'org-sparse-trees)
  4151. (defun org-cycle-hide-archived-subtrees (state)
  4152. "Re-hide all archived subtrees after a visibility state change."
  4153. (when (and (not org-cycle-open-archived-trees)
  4154. (not (memq state '(overview folded))))
  4155. (save-excursion
  4156. (let* ((globalp (memq state '(contents all)))
  4157. (beg (if globalp (point-min) (point)))
  4158. (end (if globalp (point-max) (org-end-of-subtree t))))
  4159. (org-hide-archived-subtrees beg end)
  4160. (goto-char beg)
  4161. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4162. (message "%s" (substitute-command-keys
  4163. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4164. (defun org-force-cycle-archived ()
  4165. "Cycle subtree even if it is archived."
  4166. (interactive)
  4167. (setq this-command 'org-cycle)
  4168. (let ((org-cycle-open-archived-trees t))
  4169. (call-interactively 'org-cycle)))
  4170. (defun org-hide-archived-subtrees (beg end)
  4171. "Re-hide all archived subtrees after a visibility state change."
  4172. (org-with-wide-buffer
  4173. (let ((case-fold-search nil)
  4174. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4175. (goto-char beg)
  4176. ;; Include headline point is currently on.
  4177. (beginning-of-line)
  4178. (while (and (< (point) end) (re-search-forward re end t))
  4179. (when (member org-archive-tag (org-get-tags))
  4180. (org-flag-subtree t)
  4181. (org-end-of-subtree t))))))
  4182. (declare-function outline-end-of-heading "outline" ())
  4183. (declare-function outline-flag-region "outline" (from to flag))
  4184. (defun org-flag-subtree (flag)
  4185. (save-excursion
  4186. (org-back-to-heading t)
  4187. (outline-end-of-heading)
  4188. (outline-flag-region (point)
  4189. (progn (org-end-of-subtree t) (point))
  4190. flag)))
  4191. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4192. ;; Declare Column View Code
  4193. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4194. (declare-function org-columns-compute "org-colview" (property))
  4195. ;; Declare ID code
  4196. (declare-function org-id-store-link "org-id")
  4197. (declare-function org-id-locations-load "org-id")
  4198. (declare-function org-id-locations-save "org-id")
  4199. (defvar org-id-track-globally)
  4200. ;;; Variables for pre-computed regular expressions, all buffer local
  4201. (defvar-local org-todo-regexp nil
  4202. "Matches any of the TODO state keywords.")
  4203. (defvar-local org-not-done-regexp nil
  4204. "Matches any of the TODO state keywords except the last one.")
  4205. (defvar-local org-not-done-heading-regexp nil
  4206. "Matches a TODO headline that is not done.")
  4207. (defvar-local org-todo-line-regexp nil
  4208. "Matches a headline and puts TODO state into group 2 if present.")
  4209. (defvar-local org-complex-heading-regexp nil
  4210. "Matches a headline and puts everything into groups:
  4211. group 1: the stars
  4212. group 2: The todo keyword, maybe
  4213. group 3: Priority cookie
  4214. group 4: True headline
  4215. group 5: Tags")
  4216. (defvar-local org-complex-heading-regexp-format nil
  4217. "Printf format to make regexp to match an exact headline.
  4218. This regexp will match the headline of any node which has the
  4219. exact headline text that is put into the format, but may have any
  4220. TODO state, priority and tags.")
  4221. (defvar-local org-todo-line-tags-regexp nil
  4222. "Matches a headline and puts TODO state into group 2 if present.
  4223. Also put tags into group 4 if tags are present.")
  4224. (defconst org-plain-time-of-day-regexp
  4225. (concat
  4226. "\\(\\<[012]?[0-9]"
  4227. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4228. "\\(--?"
  4229. "\\(\\<[012]?[0-9]"
  4230. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4231. "\\)?")
  4232. "Regular expression to match a plain time or time range.
  4233. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4234. groups carry important information:
  4235. 0 the full match
  4236. 1 the first time, range or not
  4237. 8 the second time, if it is a range.")
  4238. (defconst org-plain-time-extension-regexp
  4239. (concat
  4240. "\\(\\<[012]?[0-9]"
  4241. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4242. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4243. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4244. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4245. groups carry important information:
  4246. 0 the full match
  4247. 7 hours of duration
  4248. 9 minutes of duration")
  4249. (defconst org-stamp-time-of-day-regexp
  4250. (concat
  4251. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4252. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4253. "\\(--?"
  4254. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4255. "Regular expression to match a timestamp time or time range.
  4256. After a match, the following groups carry important information:
  4257. 0 the full match
  4258. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4259. 2 the first time, range or not
  4260. 4 the second time, if it is a range.")
  4261. (defconst org-startup-options
  4262. '(("fold" org-startup-folded t)
  4263. ("overview" org-startup-folded t)
  4264. ("nofold" org-startup-folded nil)
  4265. ("showall" org-startup-folded nil)
  4266. ("showeverything" org-startup-folded showeverything)
  4267. ("content" org-startup-folded content)
  4268. ("indent" org-startup-indented t)
  4269. ("noindent" org-startup-indented nil)
  4270. ("hidestars" org-hide-leading-stars t)
  4271. ("showstars" org-hide-leading-stars nil)
  4272. ("odd" org-odd-levels-only t)
  4273. ("oddeven" org-odd-levels-only nil)
  4274. ("align" org-startup-align-all-tables t)
  4275. ("noalign" org-startup-align-all-tables nil)
  4276. ("inlineimages" org-startup-with-inline-images t)
  4277. ("noinlineimages" org-startup-with-inline-images nil)
  4278. ("latexpreview" org-startup-with-latex-preview t)
  4279. ("nolatexpreview" org-startup-with-latex-preview nil)
  4280. ("customtime" org-display-custom-times t)
  4281. ("logdone" org-log-done time)
  4282. ("lognotedone" org-log-done note)
  4283. ("nologdone" org-log-done nil)
  4284. ("lognoteclock-out" org-log-note-clock-out t)
  4285. ("nolognoteclock-out" org-log-note-clock-out nil)
  4286. ("logrepeat" org-log-repeat state)
  4287. ("lognoterepeat" org-log-repeat note)
  4288. ("logdrawer" org-log-into-drawer t)
  4289. ("nologdrawer" org-log-into-drawer nil)
  4290. ("logstatesreversed" org-log-states-order-reversed t)
  4291. ("nologstatesreversed" org-log-states-order-reversed nil)
  4292. ("nologrepeat" org-log-repeat nil)
  4293. ("logreschedule" org-log-reschedule time)
  4294. ("lognotereschedule" org-log-reschedule note)
  4295. ("nologreschedule" org-log-reschedule nil)
  4296. ("logredeadline" org-log-redeadline time)
  4297. ("lognoteredeadline" org-log-redeadline note)
  4298. ("nologredeadline" org-log-redeadline nil)
  4299. ("logrefile" org-log-refile time)
  4300. ("lognoterefile" org-log-refile note)
  4301. ("nologrefile" org-log-refile nil)
  4302. ("fninline" org-footnote-define-inline t)
  4303. ("nofninline" org-footnote-define-inline nil)
  4304. ("fnlocal" org-footnote-section nil)
  4305. ("fnauto" org-footnote-auto-label t)
  4306. ("fnprompt" org-footnote-auto-label nil)
  4307. ("fnconfirm" org-footnote-auto-label confirm)
  4308. ("fnplain" org-footnote-auto-label plain)
  4309. ("fnadjust" org-footnote-auto-adjust t)
  4310. ("nofnadjust" org-footnote-auto-adjust nil)
  4311. ("constcgs" constants-unit-system cgs)
  4312. ("constSI" constants-unit-system SI)
  4313. ("noptag" org-tag-persistent-alist nil)
  4314. ("hideblocks" org-hide-block-startup t)
  4315. ("nohideblocks" org-hide-block-startup nil)
  4316. ("beamer" org-startup-with-beamer-mode t)
  4317. ("entitiespretty" org-pretty-entities t)
  4318. ("entitiesplain" org-pretty-entities nil))
  4319. "Variable associated with STARTUP options for org-mode.
  4320. Each element is a list of three items: the startup options (as written
  4321. in the #+STARTUP line), the corresponding variable, and the value to set
  4322. this variable to if the option is found. An optional forth element PUSH
  4323. means to push this value onto the list in the variable.")
  4324. (defcustom org-group-tags t
  4325. "When non-nil (the default), use group tags.
  4326. This can be turned on/off through `org-toggle-tags-groups'."
  4327. :group 'org-tags
  4328. :group 'org-startup
  4329. :type 'boolean)
  4330. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4331. (defun org-toggle-tags-groups ()
  4332. "Toggle support for group tags.
  4333. Support for group tags is controlled by the option
  4334. `org-group-tags', which is non-nil by default."
  4335. (interactive)
  4336. (setq org-group-tags (not org-group-tags))
  4337. (cond ((and (derived-mode-p 'org-agenda-mode)
  4338. org-group-tags)
  4339. (org-agenda-redo))
  4340. ((derived-mode-p 'org-mode)
  4341. (let ((org-inhibit-startup t)) (org-mode))))
  4342. (message "Groups tags support has been turned %s"
  4343. (if org-group-tags "on" "off")))
  4344. (defun org-set-regexps-and-options (&optional tags-only)
  4345. "Precompute regular expressions used in the current buffer.
  4346. When optional argument TAGS-ONLY is non-nil, only compute tags
  4347. related expressions."
  4348. (when (derived-mode-p 'org-mode)
  4349. (let ((alist (org--setup-collect-keywords
  4350. (org-make-options-regexp
  4351. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4352. (and (not tags-only)
  4353. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4354. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4355. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4356. ;; Startup options. Get this early since it does change
  4357. ;; behavior for other options (e.g., tags).
  4358. (let ((startup (cdr (assq 'startup alist))))
  4359. (dolist (option startup)
  4360. (let ((entry (assoc-string option org-startup-options t)))
  4361. (when entry
  4362. (let ((var (nth 1 entry))
  4363. (val (nth 2 entry)))
  4364. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4365. (unless (listp (symbol-value var))
  4366. (set (make-local-variable var) nil))
  4367. (add-to-list var val)))))))
  4368. (setq-local org-file-tags
  4369. (mapcar #'org-add-prop-inherited
  4370. (cdr (assq 'filetags alist))))
  4371. (setq org-current-tag-alist
  4372. (append org-tag-persistent-alist
  4373. (let ((tags (cdr (assq 'tags alist))))
  4374. (if tags (org-tag-string-to-alist tags)
  4375. org-tag-alist))))
  4376. (setq org-tag-groups-alist
  4377. (org-tag-alist-to-groups org-current-tag-alist))
  4378. (unless tags-only
  4379. ;; File properties.
  4380. (setq-local org-file-properties (cdr (assq 'property alist)))
  4381. ;; Archive location.
  4382. (let ((archive (cdr (assq 'archive alist))))
  4383. (when archive (setq-local org-archive-location archive)))
  4384. ;; Category.
  4385. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4386. (when cat
  4387. (setq-local org-category (intern cat))
  4388. (setq-local org-file-properties
  4389. (org--update-property-plist
  4390. "CATEGORY" cat org-file-properties))))
  4391. ;; Columns.
  4392. (let ((column (cdr (assq 'columns alist))))
  4393. (when column (setq-local org-columns-default-format column)))
  4394. ;; Constants.
  4395. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4396. ;; Link abbreviations.
  4397. (let ((links (cdr (assq 'link alist))))
  4398. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4399. ;; Priorities.
  4400. (let ((priorities (cdr (assq 'priorities alist))))
  4401. (when priorities
  4402. (setq-local org-highest-priority (nth 0 priorities))
  4403. (setq-local org-lowest-priority (nth 1 priorities))
  4404. (setq-local org-default-priority (nth 2 priorities))))
  4405. ;; Scripts.
  4406. (let ((scripts (assq 'scripts alist)))
  4407. (when scripts
  4408. (setq-local org-use-sub-superscripts (cdr scripts))))
  4409. ;; TODO keywords.
  4410. (setq-local org-todo-kwd-alist nil)
  4411. (setq-local org-todo-key-alist nil)
  4412. (setq-local org-todo-key-trigger nil)
  4413. (setq-local org-todo-keywords-1 nil)
  4414. (setq-local org-done-keywords nil)
  4415. (setq-local org-todo-heads nil)
  4416. (setq-local org-todo-sets nil)
  4417. (setq-local org-todo-log-states nil)
  4418. (let ((todo-sequences
  4419. (or (nreverse (cdr (assq 'todo alist)))
  4420. (let ((d (default-value 'org-todo-keywords)))
  4421. (if (not (stringp (car d))) d
  4422. ;; XXX: Backward compatibility code.
  4423. (list (cons org-todo-interpretation d)))))))
  4424. (dolist (sequence todo-sequences)
  4425. (let* ((sequence (or (run-hook-with-args-until-success
  4426. 'org-todo-setup-filter-hook sequence)
  4427. sequence))
  4428. (sequence-type (car sequence))
  4429. (keywords (cdr sequence))
  4430. (sep (member "|" keywords))
  4431. names alist)
  4432. (dolist (k (remove "|" keywords))
  4433. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4434. k)
  4435. (error "Invalid TODO keyword %s" k))
  4436. (let ((name (match-string 1 k))
  4437. (key (match-string 2 k))
  4438. (log (org-extract-log-state-settings k)))
  4439. (push name names)
  4440. (push (cons name (and key (string-to-char key))) alist)
  4441. (when log (push log org-todo-log-states))))
  4442. (let* ((names (nreverse names))
  4443. (done (if sep (org-remove-keyword-keys (cdr sep))
  4444. (last names)))
  4445. (head (car names))
  4446. (tail (list sequence-type head (car done) (org-last done))))
  4447. (add-to-list 'org-todo-heads head 'append)
  4448. (push names org-todo-sets)
  4449. (setq org-done-keywords (append org-done-keywords done nil))
  4450. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4451. (setq org-todo-key-alist
  4452. (append org-todo-key-alist
  4453. (and alist
  4454. (append '((:startgroup))
  4455. (nreverse alist)
  4456. '((:endgroup))))))
  4457. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4458. (setq org-todo-sets (nreverse org-todo-sets)
  4459. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4460. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4461. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4462. ;; Compute the regular expressions and other local variables.
  4463. ;; Using `org-outline-regexp-bol' would complicate them much,
  4464. ;; because of the fixed white space at the end of that string.
  4465. (unless org-done-keywords
  4466. (setq org-done-keywords
  4467. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4468. (setq org-not-done-keywords
  4469. (org-delete-all org-done-keywords
  4470. (copy-sequence org-todo-keywords-1))
  4471. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4472. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4473. org-not-done-heading-regexp
  4474. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4475. org-todo-line-regexp
  4476. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4477. org-complex-heading-regexp
  4478. (concat "^\\(\\*+\\)"
  4479. "\\(?: +" org-todo-regexp "\\)?"
  4480. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4481. "\\(?: +\\(.*?\\)\\)??"
  4482. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
  4483. "[ \t]*$")
  4484. org-complex-heading-regexp-format
  4485. (concat "^\\(\\*+\\)"
  4486. "\\(?: +" org-todo-regexp "\\)?"
  4487. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4488. "\\(?: +"
  4489. ;; Stats cookies can be stuck to body.
  4490. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4491. "\\(%s\\)"
  4492. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4493. "\\)"
  4494. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
  4495. "[ \t]*$")
  4496. org-todo-line-tags-regexp
  4497. (concat "^\\(\\*+\\)"
  4498. "\\(?: +" org-todo-regexp "\\)?"
  4499. "\\(?: +\\(.*?\\)\\)??"
  4500. "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
  4501. "[ \t]*$"))
  4502. (org-compute-latex-and-related-regexp)))))
  4503. (defun org--setup-collect-keywords (regexp &optional files alist)
  4504. "Return setup keywords values as an alist.
  4505. REGEXP matches a subset of setup keywords. FILES is a list of
  4506. file names already visited. It is used to avoid circular setup
  4507. files. ALIST, when non-nil, is the alist computed so far.
  4508. Return value contains the following keys: `archive', `category',
  4509. `columns', `constants', `filetags', `link', `priorities',
  4510. `property', `scripts', `startup', `tags' and `todo'."
  4511. (org-with-wide-buffer
  4512. (goto-char (point-min))
  4513. (let ((case-fold-search t))
  4514. (while (re-search-forward regexp nil t)
  4515. (let ((element (org-element-at-point)))
  4516. (when (eq (org-element-type element) 'keyword)
  4517. (let ((key (org-element-property :key element))
  4518. (value (org-element-property :value element)))
  4519. (cond
  4520. ((equal key "ARCHIVE")
  4521. (when (org-string-nw-p value)
  4522. (push (cons 'archive value) alist)))
  4523. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4524. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4525. ((equal key "CONSTANTS")
  4526. (let* ((constants (assq 'constants alist))
  4527. (store (cdr constants)))
  4528. (dolist (pair (org-split-string value))
  4529. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4530. pair)
  4531. (let* ((name (match-string 1 pair))
  4532. (value (match-string 2 pair))
  4533. (old (assoc name store)))
  4534. (if old (setcdr old value)
  4535. (push (cons name value) store)))))
  4536. (if constants (setcdr constants store)
  4537. (push (cons 'constants store) alist))))
  4538. ((equal key "FILETAGS")
  4539. (when (org-string-nw-p value)
  4540. (let ((old (assq 'filetags alist))
  4541. (new (apply #'nconc
  4542. (mapcar (lambda (x) (org-split-string x ":"))
  4543. (org-split-string value)))))
  4544. (if old (setcdr old (append new (cdr old)))
  4545. (push (cons 'filetags new) alist)))))
  4546. ((equal key "LINK")
  4547. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4548. (let ((links (assq 'link alist))
  4549. (pair (cons (match-string-no-properties 1 value)
  4550. (match-string-no-properties 2 value))))
  4551. (if links (push pair (cdr links))
  4552. (push (list 'link pair) alist)))))
  4553. ((equal key "OPTIONS")
  4554. (when (and (org-string-nw-p value)
  4555. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4556. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4557. ((equal key "PRIORITIES")
  4558. (push (cons 'priorities
  4559. (let ((prio (org-split-string value)))
  4560. (if (< (length prio) 3) '(?A ?C ?B)
  4561. (mapcar #'string-to-char prio))))
  4562. alist))
  4563. ((equal key "PROPERTY")
  4564. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4565. (let* ((property (assq 'property alist))
  4566. (value (org--update-property-plist
  4567. (match-string-no-properties 1 value)
  4568. (match-string-no-properties 2 value)
  4569. (cdr property))))
  4570. (if property (setcdr property value)
  4571. (push (cons 'property value) alist)))))
  4572. ((equal key "STARTUP")
  4573. (let ((startup (assq 'startup alist)))
  4574. (if startup
  4575. (setcdr startup
  4576. (append (cdr startup) (org-split-string value)))
  4577. (push (cons 'startup (org-split-string value)) alist))))
  4578. ((equal key "TAGS")
  4579. (let ((tag-cell (assq 'tags alist)))
  4580. (if tag-cell
  4581. (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
  4582. (push (cons 'tags value) alist))))
  4583. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4584. (let ((todo (assq 'todo alist))
  4585. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4586. (org-split-string value))))
  4587. (if todo (push value (cdr todo))
  4588. (push (list 'todo value) alist))))
  4589. ((equal key "SETUPFILE")
  4590. (unless buffer-read-only ; Do not check in Gnus messages.
  4591. (let ((f (and (org-string-nw-p value)
  4592. (expand-file-name
  4593. (org-remove-double-quotes value)))))
  4594. (when (and f (file-readable-p f) (not (member f files)))
  4595. (with-temp-buffer
  4596. (setq default-directory (file-name-directory f))
  4597. (insert-file-contents f)
  4598. (setq alist
  4599. ;; Fake Org mode to benefit from cache
  4600. ;; without recurring needlessly.
  4601. (let ((major-mode 'org-mode))
  4602. (org--setup-collect-keywords
  4603. regexp (cons f files) alist)))))))))))))))
  4604. alist)
  4605. (defun org-tag-string-to-alist (s)
  4606. "Return tag alist associated to string S.
  4607. S is a value for TAGS keyword or produced with
  4608. `org-tag-alist-to-string'. Return value is an alist suitable for
  4609. `org-tag-alist' or `org-tag-persistent-alist'."
  4610. (let ((lines (mapcar #'split-string (split-string s "\n" t)))
  4611. (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
  4612. "\\|{.+?}\\)" ; regular expression
  4613. "\\(?:(\\(.\\))\\)?\\'"))
  4614. alist group-flag)
  4615. (dolist (tokens lines (cdr (nreverse alist)))
  4616. (push '(:newline) alist)
  4617. (while tokens
  4618. (let ((token (pop tokens)))
  4619. (pcase token
  4620. ("{"
  4621. (push '(:startgroup) alist)
  4622. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4623. ("}"
  4624. (push '(:endgroup) alist)
  4625. (setq group-flag nil))
  4626. ("["
  4627. (push '(:startgrouptag) alist)
  4628. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4629. ("]"
  4630. (push '(:endgrouptag) alist)
  4631. (setq group-flag nil))
  4632. (":"
  4633. (push '(:grouptags) alist))
  4634. ((guard (string-match tag-re token))
  4635. (let ((tag (match-string 1 token))
  4636. (key (and (match-beginning 2)
  4637. (string-to-char (match-string 2 token)))))
  4638. ;; Push all tags in groups, no matter if they already
  4639. ;; appear somewhere else in the list.
  4640. (when (or group-flag (not (assoc tag alist)))
  4641. (push (cons tag key) alist))))))))))
  4642. (defun org-tag-alist-to-string (alist &optional skip-key)
  4643. "Return tag string associated to ALIST.
  4644. ALIST is an alist, as defined in `org-tag-alist' or
  4645. `org-tag-persistent-alist', or produced with
  4646. `org-tag-string-to-alist'.
  4647. Return value is a string suitable as a value for \"TAGS\"
  4648. keyword.
  4649. When optional argument SKIP-KEY is non-nil, skip selection keys
  4650. next to tags."
  4651. (mapconcat (lambda (token)
  4652. (pcase token
  4653. (`(:startgroup) "{")
  4654. (`(:endgroup) "}")
  4655. (`(:startgrouptag) "[")
  4656. (`(:endgrouptag) "]")
  4657. (`(:grouptags) ":")
  4658. (`(:newline) "\\n")
  4659. ((and
  4660. (guard (not skip-key))
  4661. `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
  4662. (format "%s(%c)" tag key))
  4663. (`(,(and tag (pred stringp)) . ,_) tag)
  4664. (_ (user-error "Invalid tag token: %S" token))))
  4665. alist
  4666. " "))
  4667. (defun org-tag-alist-to-groups (alist)
  4668. "Return group alist from tag ALIST.
  4669. ALIST is an alist, as defined in `org-tag-alist' or
  4670. `org-tag-persistent-alist', or produced with
  4671. `org-tag-string-to-alist'. Return value is an alist following
  4672. the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
  4673. a string, summarizing TAGS, as a list of strings."
  4674. (let (groups group-status current-group)
  4675. (dolist (token alist (nreverse groups))
  4676. (pcase token
  4677. (`(,(or :startgroup :startgrouptag)) (setq group-status t))
  4678. (`(,(or :endgroup :endgrouptag))
  4679. (when (eq group-status 'append)
  4680. (push (nreverse current-group) groups))
  4681. (setq group-status nil))
  4682. (`(:grouptags) (setq group-status 'append))
  4683. ((and `(,tag . ,_) (guard group-status))
  4684. (if (eq group-status 'append) (push tag current-group)
  4685. (setq current-group (list tag))))
  4686. (_ nil)))))
  4687. (defun org-file-contents (file &optional noerror)
  4688. "Return the contents of FILE, as a string."
  4689. (if (and file (file-readable-p file))
  4690. (with-temp-buffer
  4691. (insert-file-contents file)
  4692. (buffer-string))
  4693. (funcall (if noerror 'message 'error)
  4694. "Cannot read file \"%s\"%s"
  4695. file
  4696. (let ((from (buffer-file-name (buffer-base-buffer))))
  4697. (if from (concat " (referenced in file \"" from "\")") "")))))
  4698. (defun org-extract-log-state-settings (x)
  4699. "Extract the log state setting from a TODO keyword string.
  4700. This will extract info from a string like \"WAIT(w@/!)\"."
  4701. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4702. (let ((kw (match-string 1 x))
  4703. (log1 (and (match-end 3) (match-string 3 x)))
  4704. (log2 (and (match-end 4) (match-string 4 x))))
  4705. (and (or log1 log2)
  4706. (list kw
  4707. (and log1 (if (equal log1 "!") 'time 'note))
  4708. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4709. (defun org-remove-keyword-keys (list)
  4710. "Remove a pair of parenthesis at the end of each string in LIST."
  4711. (mapcar (lambda (x)
  4712. (if (string-match "(.*)$" x)
  4713. (substring x 0 (match-beginning 0))
  4714. x))
  4715. list))
  4716. (defun org-assign-fast-keys (alist)
  4717. "Assign fast keys to a keyword-key alist.
  4718. Respect keys that are already there."
  4719. (let (new e (alt ?0))
  4720. (while (setq e (pop alist))
  4721. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4722. (cdr e)) ;; Key already assigned.
  4723. (push e new)
  4724. (let ((clist (string-to-list (downcase (car e))))
  4725. (used (append new alist)))
  4726. (when (= (car clist) ?@)
  4727. (pop clist))
  4728. (while (and clist (rassoc (car clist) used))
  4729. (pop clist))
  4730. (unless clist
  4731. (while (rassoc alt used)
  4732. (cl-incf alt)))
  4733. (push (cons (car e) (or (car clist) alt)) new))))
  4734. (nreverse new)))
  4735. ;;; Some variables used in various places
  4736. (defvar org-window-configuration nil
  4737. "Used in various places to store a window configuration.")
  4738. (defvar org-selected-window nil
  4739. "Used in various places to store a window configuration.")
  4740. (defvar org-finish-function nil
  4741. "Function to be called when `C-c C-c' is used.
  4742. This is for getting out of special buffers like capture.")
  4743. (defvar org-last-state)
  4744. ;; Defined somewhere in this file, but used before definition.
  4745. (defvar org-entities) ;; defined in org-entities.el
  4746. (defvar org-struct-menu)
  4747. (defvar org-org-menu)
  4748. (defvar org-tbl-menu)
  4749. ;;;; Define the Org-mode
  4750. ;; We use a before-change function to check if a table might need
  4751. ;; an update.
  4752. (defvar org-table-may-need-update t
  4753. "Indicates that a table might need an update.
  4754. This variable is set by `org-before-change-function'.
  4755. `org-table-align' sets it back to nil.")
  4756. (defun org-before-change-function (_beg _end)
  4757. "Every change indicates that a table might need an update."
  4758. (setq org-table-may-need-update t))
  4759. (defvar org-mode-map)
  4760. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4761. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4762. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4763. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4764. (defvar org-table-buffer-is-an nil)
  4765. (defvar bidi-paragraph-direction)
  4766. (defvar buffer-face-mode-face)
  4767. (require 'outline)
  4768. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4769. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
  4770. ;; Other stuff we need.
  4771. (require 'time-date)
  4772. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4773. (require 'easymenu)
  4774. (autoload 'easy-menu-add "easymenu")
  4775. (require 'overlay)
  4776. ;; (require 'org-macs) moved higher up in the file before it is first used
  4777. (require 'org-entities)
  4778. ;; (require 'org-compat) moved higher up in the file before it is first used
  4779. (require 'org-faces)
  4780. (require 'org-list)
  4781. (require 'org-pcomplete)
  4782. (require 'org-src)
  4783. (require 'org-footnote)
  4784. (require 'org-macro)
  4785. ;; babel
  4786. (require 'ob)
  4787. ;;;###autoload
  4788. (define-derived-mode org-mode outline-mode "Org"
  4789. "Outline-based notes management and organizer, alias
  4790. \"Carsten's outline-mode for keeping track of everything.\"
  4791. Org-mode develops organizational tasks around a NOTES file which
  4792. contains information about projects as plain text. Org-mode is
  4793. implemented on top of outline-mode, which is ideal to keep the content
  4794. of large files well structured. It supports ToDo items, deadlines and
  4795. time stamps, which magically appear in the diary listing of the Emacs
  4796. calendar. Tables are easily created with a built-in table editor.
  4797. Plain text URL-like links connect to websites, emails (VM), Usenet
  4798. messages (Gnus), BBDB entries, and any files related to the project.
  4799. For printing and sharing of notes, an Org-mode file (or a part of it)
  4800. can be exported as a structured ASCII or HTML file.
  4801. The following commands are available:
  4802. \\{org-mode-map}"
  4803. ;; Get rid of Outline menus, they are not needed
  4804. ;; Need to do this here because define-derived-mode sets up
  4805. ;; the keymap so late. Still, it is a waste to call this each time
  4806. ;; we switch another buffer into Org mode.
  4807. (define-key org-mode-map [menu-bar headings] 'undefined)
  4808. (define-key org-mode-map [menu-bar hide] 'undefined)
  4809. (define-key org-mode-map [menu-bar show] 'undefined)
  4810. (org-load-modules-maybe)
  4811. (org-install-agenda-files-menu)
  4812. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4813. (add-to-invisibility-spec '(org-cwidth))
  4814. (add-to-invisibility-spec '(org-hide-block . t))
  4815. (setq-local outline-regexp org-outline-regexp)
  4816. (setq-local outline-level 'org-outline-level)
  4817. (setq bidi-paragraph-direction 'left-to-right)
  4818. (when (and org-ellipsis
  4819. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4820. (fboundp 'make-glyph-code))
  4821. (unless org-display-table
  4822. (setq org-display-table (make-display-table)))
  4823. (set-display-table-slot
  4824. org-display-table 4
  4825. (vconcat (mapcar
  4826. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4827. org-ellipsis)))
  4828. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4829. (setq buffer-display-table org-display-table))
  4830. (org-set-regexps-and-options)
  4831. (org-set-font-lock-defaults)
  4832. (when (and org-tag-faces (not org-tags-special-faces-re))
  4833. ;; tag faces set outside customize.... force initialization.
  4834. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4835. ;; Calc embedded
  4836. (setq-local calc-embedded-open-mode "# ")
  4837. ;; Modify a few syntax entries
  4838. (modify-syntax-entry ?@ "w")
  4839. (modify-syntax-entry ?\" "\"")
  4840. (modify-syntax-entry ?\\ "_")
  4841. (modify-syntax-entry ?~ "_")
  4842. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4843. ;; Activate before-change-function
  4844. (setq-local org-table-may-need-update t)
  4845. (add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4846. ;; Check for running clock before killing a buffer
  4847. (add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4848. ;; Initialize macros templates.
  4849. (org-macro-initialize-templates)
  4850. ;; Initialize radio targets.
  4851. (org-update-radio-target-regexp)
  4852. ;; Indentation.
  4853. (setq-local indent-line-function 'org-indent-line)
  4854. (setq-local indent-region-function 'org-indent-region)
  4855. ;; Filling and auto-filling.
  4856. (org-setup-filling)
  4857. ;; Comments.
  4858. (org-setup-comments-handling)
  4859. ;; Initialize cache.
  4860. (org-element-cache-reset)
  4861. ;; Beginning/end of defun
  4862. (setq-local beginning-of-defun-function 'org-backward-element)
  4863. (setq-local end-of-defun-function
  4864. (lambda ()
  4865. (if (not (org-at-heading-p))
  4866. (org-forward-element)
  4867. (org-forward-element)
  4868. (forward-char -1))))
  4869. ;; Next error for sparse trees
  4870. (setq-local next-error-function 'org-occur-next-match)
  4871. ;; Make sure dependence stuff works reliably, even for users who set it
  4872. ;; too late :-(
  4873. (if org-enforce-todo-dependencies
  4874. (add-hook 'org-blocker-hook
  4875. 'org-block-todo-from-children-or-siblings-or-parent)
  4876. (remove-hook 'org-blocker-hook
  4877. 'org-block-todo-from-children-or-siblings-or-parent))
  4878. (if org-enforce-todo-checkbox-dependencies
  4879. (add-hook 'org-blocker-hook
  4880. 'org-block-todo-from-checkboxes)
  4881. (remove-hook 'org-blocker-hook
  4882. 'org-block-todo-from-checkboxes))
  4883. ;; Align options lines
  4884. (setq-local
  4885. align-mode-rules-list
  4886. '((org-in-buffer-settings
  4887. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4888. (modes . '(org-mode)))))
  4889. ;; Imenu
  4890. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4891. ;; Make isearch reveal context
  4892. (setq-local outline-isearch-open-invisible-function
  4893. (lambda (&rest _) (org-show-context 'isearch)))
  4894. ;; Setup the pcomplete hooks
  4895. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4896. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4897. (setq-local pcomplete-default-completion-function 'ignore)
  4898. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4899. (setq-local pcomplete-termination-string "")
  4900. (setq-local buffer-face-mode-face 'org-default)
  4901. ;; If empty file that did not turn on Org mode automatically, make
  4902. ;; it to.
  4903. (when (and org-insert-mode-line-in-empty-file
  4904. (org-called-interactively-p 'any)
  4905. (= (point-min) (point-max)))
  4906. (insert "# -*- mode: org -*-\n\n"))
  4907. (unless org-inhibit-startup
  4908. (org-unmodified
  4909. (when org-startup-with-beamer-mode (org-beamer-mode))
  4910. (when org-startup-align-all-tables
  4911. (org-table-map-tables #'org-table-align t))
  4912. (when org-startup-with-inline-images (org-display-inline-images))
  4913. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4914. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4915. (when org-startup-truncated (setq truncate-lines t))
  4916. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4917. (org-refresh-effort-properties)))
  4918. ;; Try to set `org-hide' face correctly.
  4919. (let ((foreground (org-find-invisible-foreground)))
  4920. (when foreground
  4921. (set-face-foreground 'org-hide foreground))))
  4922. ;; Update `customize-package-emacs-version-alist'
  4923. (add-to-list 'customize-package-emacs-version-alist
  4924. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4925. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4926. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4927. (defvar org-mode-transpose-word-syntax-table
  4928. (let ((st (make-syntax-table text-mode-syntax-table)))
  4929. (dolist (c org-emphasis-alist st)
  4930. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4931. (when (fboundp 'abbrev-table-put)
  4932. (abbrev-table-put org-mode-abbrev-table
  4933. :parents (list text-mode-abbrev-table)))
  4934. (defun org-find-invisible-foreground ()
  4935. (let ((candidates (remove
  4936. "unspecified-bg"
  4937. (nconc
  4938. (list (face-background 'default)
  4939. (face-background 'org-default))
  4940. (mapcar
  4941. (lambda (alist)
  4942. (when (boundp alist)
  4943. (cdr (assoc 'background-color (symbol-value alist)))))
  4944. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4945. (list (face-foreground 'org-hide))))))
  4946. (car (remove nil candidates))))
  4947. (defun org-current-time (&optional rounding-minutes past)
  4948. "Current time, possibly rounded to ROUNDING-MINUTES.
  4949. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4950. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4951. the rounding returns a past time."
  4952. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4953. (car org-time-stamp-rounding-minutes)))
  4954. (time (decode-time)) res)
  4955. (if (< r 1)
  4956. (current-time)
  4957. (setq res
  4958. (apply 'encode-time
  4959. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4960. (nthcdr 2 time))))
  4961. (if (and past (< (float-time (time-subtract (current-time) res)) 0))
  4962. (seconds-to-time (- (float-time res) (* r 60)))
  4963. res))))
  4964. (defun org-today ()
  4965. "Return today date, considering `org-extend-today-until'."
  4966. (time-to-days
  4967. (time-subtract (current-time)
  4968. (list 0 (* 3600 org-extend-today-until) 0))))
  4969. ;;;; Font-Lock stuff, including the activators
  4970. (defvar org-mouse-map (make-sparse-keymap))
  4971. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4972. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4973. (when org-mouse-1-follows-link
  4974. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4975. (when org-tab-follows-link
  4976. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4977. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4978. (require 'font-lock)
  4979. (defconst org-non-link-chars "]\t\n\r<>")
  4980. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4981. "file+sys" "news" "shell" "elisp" "doi" "message"
  4982. "help"))
  4983. (defvar org-link-types-re nil
  4984. "Matches a link that has a url-like prefix like \"http:\"")
  4985. (defvar org-link-re-with-space nil
  4986. "Matches a link with spaces, optional angular brackets around it.")
  4987. (defvar org-link-re-with-space2 nil
  4988. "Matches a link with spaces, optional angular brackets around it.")
  4989. (defvar org-link-re-with-space3 nil
  4990. "Matches a link with spaces, only for internal part in bracket links.")
  4991. (defvar org-angle-link-re nil
  4992. "Matches link with angular brackets, spaces are allowed.")
  4993. (defvar org-plain-link-re nil
  4994. "Matches plain link, without spaces.")
  4995. (defvar org-bracket-link-regexp nil
  4996. "Matches a link in double brackets.")
  4997. (defvar org-bracket-link-analytic-regexp nil
  4998. "Regular expression used to analyze links.
  4999. Here is what the match groups contain after a match:
  5000. 1: http:
  5001. 2: http
  5002. 3: path
  5003. 4: [desc]
  5004. 5: desc")
  5005. (defvar org-bracket-link-analytic-regexp++ nil
  5006. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  5007. (defvar org-any-link-re nil
  5008. "Regular expression matching any link.")
  5009. (defconst org-match-sexp-depth 3
  5010. "Number of stacked braces for sub/superscript matching.")
  5011. (defun org-create-multibrace-regexp (left right n)
  5012. "Create a regular expression which will match a balanced sexp.
  5013. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  5014. as single character strings.
  5015. The regexp returned will match the entire expression including the
  5016. delimiters. It will also define a single group which contains the
  5017. match except for the outermost delimiters. The maximum depth of
  5018. stacked delimiters is N. Escaping delimiters is not possible."
  5019. (let* ((nothing (concat "[^" left right "]*?"))
  5020. (or "\\|")
  5021. (re nothing)
  5022. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  5023. (while (> n 1)
  5024. (setq n (1- n)
  5025. re (concat re or next)
  5026. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  5027. (concat left "\\(" re "\\)" right)))
  5028. (defconst org-match-substring-regexp
  5029. (concat
  5030. "\\(\\S-\\)\\([_^]\\)\\("
  5031. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5032. "\\|"
  5033. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5034. "\\|"
  5035. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5036. "The regular expression matching a sub- or superscript.")
  5037. (defconst org-match-substring-with-braces-regexp
  5038. (concat
  5039. "\\(\\S-\\)\\([_^]\\)"
  5040. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5041. "The regular expression matching a sub- or superscript, forcing braces.")
  5042. (defun org-make-link-regexps ()
  5043. "Update the link regular expressions.
  5044. This should be called after the variable `org-link-types' has changed."
  5045. (let ((types-re (regexp-opt org-link-types t)))
  5046. (setq org-link-types-re
  5047. (concat "\\`" types-re ":")
  5048. org-link-re-with-space
  5049. (concat "<?" types-re ":"
  5050. "\\([^" org-non-link-chars " ]"
  5051. "[^" org-non-link-chars "]*"
  5052. "[^" org-non-link-chars " ]\\)>?")
  5053. org-link-re-with-space2
  5054. (concat "<?" types-re ":"
  5055. "\\([^" org-non-link-chars " ]"
  5056. "[^\t\n\r]*"
  5057. "[^" org-non-link-chars " ]\\)>?")
  5058. org-link-re-with-space3
  5059. (concat "<?" types-re ":"
  5060. "\\([^" org-non-link-chars " ]"
  5061. "[^\t\n\r]*\\)")
  5062. org-angle-link-re
  5063. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5064. types-re)
  5065. org-plain-link-re
  5066. (concat
  5067. "\\<" types-re ":"
  5068. "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)")
  5069. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5070. org-bracket-link-regexp
  5071. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5072. org-bracket-link-analytic-regexp
  5073. (concat
  5074. "\\[\\["
  5075. "\\(" types-re ":\\)?"
  5076. "\\([^]]+\\)"
  5077. "\\]"
  5078. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5079. "\\]")
  5080. org-bracket-link-analytic-regexp++
  5081. (concat
  5082. "\\[\\["
  5083. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5084. "\\([^]]+\\)"
  5085. "\\]"
  5086. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5087. "\\]")
  5088. org-any-link-re
  5089. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5090. org-angle-link-re "\\)\\|\\("
  5091. org-plain-link-re "\\)"))))
  5092. (org-make-link-regexps)
  5093. (defvar org-emph-face nil)
  5094. (defun org-do-emphasis-faces (limit)
  5095. "Run through the buffer and emphasize strings."
  5096. (let (rtn a)
  5097. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5098. (let* ((border (char-after (match-beginning 3)))
  5099. (bre (regexp-quote (char-to-string border))))
  5100. (when (and (not (= border (char-after (match-beginning 4))))
  5101. (not (string-match-p (concat bre ".*" bre)
  5102. (replace-regexp-in-string
  5103. "\n" " "
  5104. (substring (match-string 2) 1 -1)))))
  5105. (setq rtn t)
  5106. (setq a (assoc (match-string 3) org-emphasis-alist))
  5107. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5108. 'face
  5109. (nth 1 a))
  5110. (and (nth 2 a)
  5111. (org-remove-flyspell-overlays-in
  5112. (match-beginning 0) (match-end 0)))
  5113. (add-text-properties (match-beginning 2) (match-end 2)
  5114. '(font-lock-multiline t org-emphasis t))
  5115. (when org-hide-emphasis-markers
  5116. (add-text-properties (match-end 4) (match-beginning 5)
  5117. '(invisible org-link))
  5118. (add-text-properties (match-beginning 3) (match-end 3)
  5119. '(invisible org-link)))))
  5120. (goto-char (1+ (match-beginning 0))))
  5121. rtn))
  5122. (defun org-emphasize (&optional char)
  5123. "Insert or change an emphasis, i.e. a font like bold or italic.
  5124. If there is an active region, change that region to a new emphasis.
  5125. If there is no region, just insert the marker characters and position
  5126. the cursor between them.
  5127. CHAR should be the marker character. If it is a space, it means to
  5128. remove the emphasis of the selected region.
  5129. If CHAR is not given (for example in an interactive call) it will be
  5130. prompted for."
  5131. (interactive)
  5132. (let ((erc org-emphasis-regexp-components)
  5133. (string "") beg end move s)
  5134. (if (org-region-active-p)
  5135. (setq beg (region-beginning)
  5136. end (region-end)
  5137. string (buffer-substring beg end))
  5138. (setq move t))
  5139. (unless char
  5140. (message "Emphasis marker or tag: [%s]"
  5141. (mapconcat #'car org-emphasis-alist ""))
  5142. (setq char (read-char-exclusive)))
  5143. (if (equal char ?\s)
  5144. (setq s ""
  5145. move nil)
  5146. (unless (assoc (char-to-string char) org-emphasis-alist)
  5147. (user-error "No such emphasis marker: \"%c\"" char))
  5148. (setq s (char-to-string char)))
  5149. (while (and (> (length string) 1)
  5150. (equal (substring string 0 1) (substring string -1))
  5151. (assoc (substring string 0 1) org-emphasis-alist))
  5152. (setq string (substring string 1 -1)))
  5153. (setq string (concat s string s))
  5154. (when beg (delete-region beg end))
  5155. (unless (or (bolp)
  5156. (string-match (concat "[" (nth 0 erc) "\n]")
  5157. (char-to-string (char-before (point)))))
  5158. (insert " "))
  5159. (unless (or (eobp)
  5160. (string-match (concat "[" (nth 1 erc) "\n]")
  5161. (char-to-string (char-after (point)))))
  5162. (insert " ") (backward-char 1))
  5163. (insert string)
  5164. (and move (backward-char 1))))
  5165. (defconst org-nonsticky-props
  5166. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5167. (defsubst org-rear-nonsticky-at (pos)
  5168. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5169. (defun org-activate-plain-links (limit)
  5170. "Add link properties for plain links."
  5171. (when (and (re-search-forward org-plain-link-re limit t)
  5172. (not (org-in-src-block-p)))
  5173. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5174. 'face))
  5175. (link (match-string-no-properties 0)))
  5176. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5177. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5178. (add-text-properties (match-beginning 0) (match-end 0)
  5179. (list 'mouse-face 'highlight
  5180. 'face 'org-link
  5181. 'htmlize-link `(:uri ,link)
  5182. 'keymap org-mouse-map))
  5183. (org-rear-nonsticky-at (match-end 0))
  5184. t))))
  5185. (defun org-activate-code (limit)
  5186. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5187. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5188. (remove-text-properties (match-beginning 0) (match-end 0)
  5189. '(display t invisible t intangible t))
  5190. t))
  5191. (defcustom org-src-fontify-natively t
  5192. "When non-nil, fontify code in code blocks.
  5193. See also the `org-block' face."
  5194. :type 'boolean
  5195. :version "24.4"
  5196. :package-version '(Org . "8.3")
  5197. :group 'org-appearance
  5198. :group 'org-babel)
  5199. (defcustom org-allow-promoting-top-level-subtree nil
  5200. "When non-nil, allow promoting a top level subtree.
  5201. The leading star of the top level headline will be replaced
  5202. by a #."
  5203. :type 'boolean
  5204. :version "24.1"
  5205. :group 'org-appearance)
  5206. (defun org-fontify-meta-lines-and-blocks (limit)
  5207. (condition-case nil
  5208. (org-fontify-meta-lines-and-blocks-1 limit)
  5209. (error (message "org-mode fontification error in %S at %d"
  5210. (current-buffer)
  5211. (line-number-at-pos)))))
  5212. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5213. "Fontify #+ lines and blocks."
  5214. (let ((case-fold-search t))
  5215. (when (re-search-forward
  5216. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5217. limit t)
  5218. (let ((beg (match-beginning 0))
  5219. (block-start (match-end 0))
  5220. (block-end nil)
  5221. (lang (match-string 7))
  5222. (beg1 (line-beginning-position 2))
  5223. (dc1 (downcase (match-string 2)))
  5224. (dc3 (downcase (match-string 3)))
  5225. end end1 quoting block-type)
  5226. (cond
  5227. ((and (match-end 4) (equal dc3 "+begin"))
  5228. ;; Truly a block
  5229. (setq block-type (downcase (match-string 5))
  5230. quoting (member block-type org-protecting-blocks))
  5231. (when (re-search-forward
  5232. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5233. nil t) ;; on purpose, we look further than LIMIT
  5234. (setq end (min (point-max) (match-end 0))
  5235. end1 (min (point-max) (1- (match-beginning 0))))
  5236. (setq block-end (match-beginning 0))
  5237. (when quoting
  5238. (org-remove-flyspell-overlays-in beg1 end1)
  5239. (remove-text-properties beg end
  5240. '(display t invisible t intangible t)))
  5241. (add-text-properties
  5242. beg end '(font-lock-fontified t font-lock-multiline t))
  5243. (add-text-properties beg beg1 '(face org-meta-line))
  5244. (org-remove-flyspell-overlays-in beg beg1)
  5245. (add-text-properties ; For end_src
  5246. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5247. (org-remove-flyspell-overlays-in end1 end)
  5248. (cond
  5249. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5250. (org-src-font-lock-fontify-block lang block-start block-end)
  5251. (add-text-properties beg1 block-end '(src-block t)))
  5252. (quoting
  5253. (add-text-properties beg1 (min (point-max) (1+ end1))
  5254. (list 'face
  5255. (list :inherit
  5256. (let ((face-name
  5257. (intern (format "org-block-%s" lang))))
  5258. (append (and (facep face-name) (list face-name))
  5259. '(org-block))))))) ; end of source block
  5260. ((not org-fontify-quote-and-verse-blocks))
  5261. ((string= block-type "quote")
  5262. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5263. ((string= block-type "verse")
  5264. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5265. (add-text-properties beg beg1 '(face org-block-begin-line))
  5266. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5267. '(face org-block-end-line))
  5268. t))
  5269. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5270. (org-remove-flyspell-overlays-in
  5271. (match-beginning 0)
  5272. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5273. (add-text-properties
  5274. beg (match-end 3)
  5275. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5276. '(font-lock-fontified t invisible t)
  5277. '(font-lock-fontified t face org-document-info-keyword)))
  5278. (add-text-properties
  5279. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5280. (if (string-equal dc1 "+title:")
  5281. '(font-lock-fontified t face org-document-title)
  5282. '(font-lock-fontified t face org-document-info))))
  5283. ((equal dc1 "+caption:")
  5284. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5285. (remove-text-properties (match-beginning 0) (match-end 0)
  5286. '(display t invisible t intangible t))
  5287. (add-text-properties (match-beginning 1) (match-end 3)
  5288. '(font-lock-fontified t face org-meta-line))
  5289. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5290. '(font-lock-fontified t face org-block))
  5291. t)
  5292. ((member dc3 '(" " ""))
  5293. (org-remove-flyspell-overlays-in beg (match-end 0))
  5294. (add-text-properties
  5295. beg (match-end 0)
  5296. '(font-lock-fontified t face font-lock-comment-face)))
  5297. (t ;; just any other in-buffer setting, but not indented
  5298. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5299. (remove-text-properties (match-beginning 0) (match-end 0)
  5300. '(display t invisible t intangible t))
  5301. (add-text-properties beg (match-end 0)
  5302. '(font-lock-fontified t face org-meta-line))
  5303. t))))))
  5304. (defun org-fontify-drawers (limit)
  5305. "Fontify drawers."
  5306. (when (re-search-forward org-drawer-regexp limit t)
  5307. (add-text-properties
  5308. (match-beginning 0) (match-end 0)
  5309. '(font-lock-fontified t face org-special-keyword))
  5310. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5311. t))
  5312. (defun org-fontify-macros (limit)
  5313. "Fontify macros."
  5314. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5315. (add-text-properties
  5316. (match-beginning 0) (match-end 0)
  5317. '(font-lock-fontified t face org-macro))
  5318. (when org-hide-macro-markers
  5319. (add-text-properties (match-end 2) (match-beginning 2)
  5320. '(invisible t))
  5321. (add-text-properties (match-beginning 1) (match-end 1)
  5322. '(invisible t)))
  5323. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5324. t))
  5325. (defun org-activate-angle-links (limit)
  5326. "Add text properties for angle links."
  5327. (when (and (re-search-forward org-angle-link-re limit t)
  5328. (not (org-in-src-block-p)))
  5329. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5330. (add-text-properties (match-beginning 0) (match-end 0)
  5331. (list 'mouse-face 'highlight
  5332. 'keymap org-mouse-map
  5333. 'font-lock-multiline t))
  5334. (org-rear-nonsticky-at (match-end 0))
  5335. t))
  5336. (defun org-activate-footnote-links (limit)
  5337. "Add text properties for footnotes."
  5338. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5339. (when fn
  5340. (let* ((beg (nth 1 fn))
  5341. (end (nth 2 fn))
  5342. (label (car fn))
  5343. (referencep (/= (line-beginning-position) beg)))
  5344. (when (and referencep (nth 3 fn))
  5345. (save-excursion
  5346. (goto-char beg)
  5347. (search-forward (or label "fn:"))
  5348. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5349. (add-text-properties beg end
  5350. (list 'mouse-face 'highlight
  5351. 'keymap org-mouse-map
  5352. 'help-echo
  5353. (if referencep "Footnote reference"
  5354. "Footnote definition")
  5355. 'font-lock-fontified t
  5356. 'font-lock-multiline t
  5357. 'face 'org-footnote))))))
  5358. (defun org-activate-bracket-links (limit)
  5359. "Add text properties for bracketed links."
  5360. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5361. (not (org-in-src-block-p)))
  5362. (let* ((hl (match-string-no-properties 1))
  5363. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5364. (ip (org-maybe-intangible
  5365. (list 'invisible 'org-link
  5366. 'keymap org-mouse-map 'mouse-face 'highlight
  5367. 'font-lock-multiline t 'help-echo help
  5368. 'htmlize-link `(:uri ,hl))))
  5369. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5370. 'font-lock-multiline t 'help-echo help
  5371. 'htmlize-link `(:uri ,hl))))
  5372. ;; We need to remove the invisible property here. Table narrowing
  5373. ;; may have made some of this invisible.
  5374. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5375. (remove-text-properties (match-beginning 0) (match-end 0)
  5376. '(invisible nil))
  5377. (if (match-end 3)
  5378. (progn
  5379. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5380. (org-rear-nonsticky-at (match-beginning 3))
  5381. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5382. (org-rear-nonsticky-at (match-end 3))
  5383. (add-text-properties (match-end 3) (match-end 0) ip)
  5384. (org-rear-nonsticky-at (match-end 0)))
  5385. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5386. (org-rear-nonsticky-at (match-beginning 1))
  5387. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5388. (org-rear-nonsticky-at (match-end 1))
  5389. (add-text-properties (match-end 1) (match-end 0) ip)
  5390. (org-rear-nonsticky-at (match-end 0)))
  5391. t)))
  5392. (defun org-activate-dates (limit)
  5393. "Add text properties for dates."
  5394. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5395. (not (equal (char-before (match-beginning 0)) 91)))
  5396. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5397. (add-text-properties (match-beginning 0) (match-end 0)
  5398. (list 'mouse-face 'highlight
  5399. 'keymap org-mouse-map))
  5400. (org-rear-nonsticky-at (match-end 0))
  5401. (when org-display-custom-times
  5402. (if (match-end 3)
  5403. (org-display-custom-time (match-beginning 3) (match-end 3))
  5404. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5405. t))
  5406. (defvar-local org-target-link-regexp nil
  5407. "Regular expression matching radio targets in plain text.")
  5408. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5409. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5410. border border border))
  5411. "Regular expression matching a link target.")
  5412. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5413. "Regular expression matching a radio target.")
  5414. (defconst org-any-target-regexp
  5415. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5416. "Regular expression matching any target.")
  5417. (defun org-activate-target-links (limit)
  5418. "Add text properties for target matches."
  5419. (when org-target-link-regexp
  5420. (let ((case-fold-search t))
  5421. (when (re-search-forward org-target-link-regexp limit t)
  5422. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5423. (add-text-properties (match-beginning 1) (match-end 1)
  5424. (list 'mouse-face 'highlight
  5425. 'keymap org-mouse-map
  5426. 'help-echo "Radio target link"
  5427. 'org-linked-text t))
  5428. (org-rear-nonsticky-at (match-end 1))
  5429. t))))
  5430. (defun org-update-radio-target-regexp ()
  5431. "Find all radio targets in this file and update the regular expression.
  5432. Also refresh fontification if needed."
  5433. (interactive)
  5434. (let ((old-regexp org-target-link-regexp)
  5435. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5436. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5437. (targets
  5438. (org-with-wide-buffer
  5439. (goto-char (point-min))
  5440. (let (rtn)
  5441. (while (re-search-forward org-radio-target-regexp nil t)
  5442. ;; Make sure point is really within the object.
  5443. (backward-char)
  5444. (let ((obj (org-element-context)))
  5445. (when (eq (org-element-type obj) 'radio-target)
  5446. (cl-pushnew (org-element-property :value obj) rtn
  5447. :test #'equal))))
  5448. rtn))))
  5449. (setq org-target-link-regexp
  5450. (and targets
  5451. (concat before-re
  5452. (mapconcat
  5453. (lambda (x)
  5454. (replace-regexp-in-string
  5455. " +" "\\s-+" (regexp-quote x) t t))
  5456. targets
  5457. "\\|")
  5458. after-re)))
  5459. (unless (equal old-regexp org-target-link-regexp)
  5460. ;; Clean-up cache.
  5461. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5462. ((not org-target-link-regexp) old-regexp)
  5463. (t
  5464. (concat before-re
  5465. (mapconcat
  5466. (lambda (re)
  5467. (substring re (length before-re)
  5468. (- (length after-re))))
  5469. (list old-regexp org-target-link-regexp)
  5470. "\\|")
  5471. after-re)))))
  5472. (org-with-wide-buffer
  5473. (goto-char (point-min))
  5474. (while (re-search-forward regexp nil t)
  5475. (org-element-cache-refresh (match-beginning 1)))))
  5476. ;; Re fontify buffer.
  5477. (when (memq 'radio org-highlight-links)
  5478. (org-restart-font-lock)))))
  5479. (defun org-hide-wide-columns (limit)
  5480. (let (s e)
  5481. (setq s (text-property-any (point) (or limit (point-max))
  5482. 'org-cwidth t))
  5483. (when s
  5484. (setq e (next-single-property-change s 'org-cwidth))
  5485. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5486. (goto-char e)
  5487. t)))
  5488. (defvar org-latex-and-related-regexp nil
  5489. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5490. (defun org-compute-latex-and-related-regexp ()
  5491. "Compute regular expression for LaTeX, entities and sub/superscript.
  5492. Result depends on variable `org-highlight-latex-and-related'."
  5493. (setq-local
  5494. org-latex-and-related-regexp
  5495. (let* ((re-sub
  5496. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5497. ((eq org-use-sub-superscripts '{})
  5498. (list org-match-substring-with-braces-regexp))
  5499. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5500. (re-latex
  5501. (when (memq 'latex org-highlight-latex-and-related)
  5502. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5503. (delq nil
  5504. (mapcar (lambda (x)
  5505. (and (member (car x) matchers) (nth 1 x)))
  5506. org-latex-regexps)))))
  5507. (re-entities
  5508. (when (memq 'entities org-highlight-latex-and-related)
  5509. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5510. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5511. (defun org-do-latex-and-related (limit)
  5512. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5513. LIMIT bounds the search for syntax to highlight. Stop at first
  5514. highlighted object, if any. Return t if some highlighting was
  5515. done, nil otherwise."
  5516. (when (org-string-nw-p org-latex-and-related-regexp)
  5517. (catch 'found
  5518. (while (re-search-forward org-latex-and-related-regexp limit t)
  5519. (unless
  5520. (cl-some
  5521. (lambda (f)
  5522. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5523. (save-excursion
  5524. (goto-char (1+ (match-beginning 0)))
  5525. (face-at-point nil t)))
  5526. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5527. '(?_ ?^))
  5528. 1
  5529. 0)))
  5530. (font-lock-prepend-text-property
  5531. (+ offset (match-beginning 0)) (match-end 0)
  5532. 'face 'org-latex-and-related)
  5533. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5534. '(font-lock-multiline t)))
  5535. (throw 'found t)))
  5536. nil)))
  5537. (defun org-restart-font-lock ()
  5538. "Restart `font-lock-mode', to force refontification."
  5539. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5540. (font-lock-mode -1)
  5541. (font-lock-mode 1)))
  5542. (defun org-activate-tags (limit)
  5543. (when (re-search-forward
  5544. "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$" limit t)
  5545. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5546. (add-text-properties (match-beginning 1) (match-end 1)
  5547. (list 'mouse-face 'highlight
  5548. 'keymap org-mouse-map))
  5549. (org-rear-nonsticky-at (match-end 1))
  5550. t))
  5551. (defun org-outline-level ()
  5552. "Compute the outline level of the heading at point.
  5553. If this is called at a normal headline, the level is the number
  5554. of stars. Use `org-reduced-level' to remove the effect of
  5555. `org-odd-levels'. Unlike to `org-current-level', this function
  5556. takes into consideration inlinetasks."
  5557. (org-with-wide-buffer
  5558. (end-of-line)
  5559. (if (re-search-backward org-outline-regexp-bol nil t)
  5560. (1- (- (match-end 0) (match-beginning 0)))
  5561. 0)))
  5562. (defvar org-font-lock-keywords nil)
  5563. (defsubst org-re-property (property &optional literal allow-null value)
  5564. "Return a regexp matching a PROPERTY line.
  5565. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5566. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5567. non-nil, match properties even without a value.
  5568. Match group 3 is set to the value when it exists. If there is no
  5569. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5570. With optional argument VALUE, match only property lines with
  5571. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5572. unless LITERAL is non-nil."
  5573. (concat
  5574. "^\\(?4:[ \t]*\\)"
  5575. (format "\\(?1::\\(?2:%s\\):\\)"
  5576. (if literal property (regexp-quote property)))
  5577. (cond (value
  5578. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5579. (if literal value (regexp-quote value))))
  5580. (allow-null
  5581. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5582. (t
  5583. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5584. (defconst org-property-re
  5585. (org-re-property "\\S-+" 'literal t)
  5586. "Regular expression matching a property line.
  5587. There are four matching groups:
  5588. 1: :PROPKEY: including the leading and trailing colon,
  5589. 2: PROPKEY without the leading and trailing colon,
  5590. 3: PROPVAL without leading or trailing spaces,
  5591. 4: the indentation of the current line,
  5592. 5: trailing whitespace.")
  5593. (defvar org-font-lock-hook nil
  5594. "Functions to be called for special font lock stuff.")
  5595. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5596. (defvar org-font-lock-set-keywords-hook nil
  5597. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5598. This is called after `org-font-lock-extra-keywords' is defined, but before
  5599. it is installed to be used by font lock. This can be useful if something
  5600. needs to be inserted at a specific position in the font-lock sequence.")
  5601. (defun org-font-lock-hook (limit)
  5602. "Run `org-font-lock-hook' within LIMIT."
  5603. (run-hook-with-args 'org-font-lock-hook limit))
  5604. (defun org-set-font-lock-defaults ()
  5605. "Set font lock defaults for the current buffer."
  5606. (let* ((em org-fontify-emphasized-text)
  5607. (lk org-highlight-links)
  5608. (org-font-lock-extra-keywords
  5609. (list
  5610. ;; Call the hook
  5611. '(org-font-lock-hook)
  5612. ;; Headlines
  5613. `(,(if org-fontify-whole-heading-line
  5614. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5615. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5616. (1 (org-get-level-face 1))
  5617. (2 (org-get-level-face 2))
  5618. (3 (org-get-level-face 3)))
  5619. ;; Table lines
  5620. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5621. (1 'org-table t))
  5622. ;; Table internals
  5623. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5624. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5625. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5626. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5627. ;; Drawers
  5628. '(org-fontify-drawers)
  5629. ;; Properties
  5630. (list org-property-re
  5631. '(1 'org-special-keyword t)
  5632. '(3 'org-property-value t))
  5633. ;; Links
  5634. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5635. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5636. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5637. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5638. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5639. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5640. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5641. ;; Targets.
  5642. (list org-any-target-regexp '(0 'org-target t))
  5643. ;; Diary sexps.
  5644. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5645. ;; Macro
  5646. '(org-fontify-macros)
  5647. '(org-hide-wide-columns (0 nil append))
  5648. ;; TODO keyword
  5649. (list (format org-heading-keyword-regexp-format
  5650. org-todo-regexp)
  5651. '(2 (org-get-todo-face 2) t))
  5652. ;; DONE
  5653. (if org-fontify-done-headline
  5654. (list (format org-heading-keyword-regexp-format
  5655. (concat
  5656. "\\(?:"
  5657. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5658. "\\)"))
  5659. '(2 'org-headline-done t))
  5660. nil)
  5661. ;; Priorities
  5662. '(org-font-lock-add-priority-faces)
  5663. ;; Tags
  5664. '(org-font-lock-add-tag-faces)
  5665. ;; Tags groups
  5666. (when (and org-group-tags org-tag-groups-alist)
  5667. (list (concat org-outline-regexp-bol ".+\\(:"
  5668. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5669. ":\\).*$")
  5670. '(1 'org-tag-group prepend)))
  5671. ;; Special keywords
  5672. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5673. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5674. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5675. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5676. ;; Emphasis
  5677. (when em '(org-do-emphasis-faces))
  5678. ;; Checkboxes
  5679. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5680. 1 'org-checkbox prepend)
  5681. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5682. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5683. (0 (org-get-checkbox-statistics-face) t)))
  5684. ;; Description list items
  5685. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5686. 1 'org-list-dt prepend)
  5687. ;; ARCHIVEd headings
  5688. (list (concat
  5689. org-outline-regexp-bol
  5690. "\\(.*:" org-archive-tag ":.*\\)")
  5691. '(1 'org-archived prepend))
  5692. ;; Specials
  5693. '(org-do-latex-and-related)
  5694. '(org-fontify-entities)
  5695. '(org-raise-scripts)
  5696. ;; Code
  5697. '(org-activate-code (1 'org-code t))
  5698. ;; COMMENT
  5699. (list (format
  5700. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5701. org-todo-regexp
  5702. org-comment-string)
  5703. '(9 'org-special-keyword t))
  5704. ;; Blocks and meta lines
  5705. '(org-fontify-meta-lines-and-blocks))))
  5706. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5707. (run-hooks 'org-font-lock-set-keywords-hook)
  5708. ;; Now set the full font-lock-keywords
  5709. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5710. (setq-local font-lock-defaults
  5711. '(org-font-lock-keywords t nil nil backward-paragraph))
  5712. (kill-local-variable 'font-lock-keywords) nil))
  5713. (defun org-toggle-pretty-entities ()
  5714. "Toggle the composition display of entities as UTF8 characters."
  5715. (interactive)
  5716. (setq-local org-pretty-entities (not org-pretty-entities))
  5717. (org-restart-font-lock)
  5718. (if org-pretty-entities
  5719. (message "Entities are now displayed as UTF8 characters")
  5720. (save-restriction
  5721. (widen)
  5722. (decompose-region (point-min) (point-max))
  5723. (message "Entities are now displayed as plain text"))))
  5724. (defvar-local org-custom-properties-overlays nil
  5725. "List of overlays used for custom properties.")
  5726. (defun org-toggle-custom-properties-visibility ()
  5727. "Display or hide properties in `org-custom-properties'."
  5728. (interactive)
  5729. (if org-custom-properties-overlays
  5730. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5731. (setq org-custom-properties-overlays nil))
  5732. (when org-custom-properties
  5733. (org-with-wide-buffer
  5734. (goto-char (point-min))
  5735. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5736. (while (re-search-forward regexp nil t)
  5737. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5738. (when (and end (< (point) end))
  5739. ;; Hide first custom property in current drawer.
  5740. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5741. (overlay-put o 'invisible t)
  5742. (overlay-put o 'org-custom-property t)
  5743. (push o org-custom-properties-overlays))
  5744. ;; Hide additional custom properties in the same drawer.
  5745. (while (re-search-forward regexp end t)
  5746. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5747. (overlay-put o 'invisible t)
  5748. (overlay-put o 'org-custom-property t)
  5749. (push o org-custom-properties-overlays)))))
  5750. ;; Each entry is limited to a single property drawer.
  5751. (outline-next-heading)))))))
  5752. (defun org-fontify-entities (limit)
  5753. "Find an entity to fontify."
  5754. (let (ee)
  5755. (when org-pretty-entities
  5756. (catch 'match
  5757. ;; "\_ "-family is left out on purpose. Only the first one,
  5758. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5759. ;; confusing when adding a second white space character.
  5760. (while (re-search-forward
  5761. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5762. limit t)
  5763. (when (and (not (org-at-comment-p))
  5764. (setq ee (org-entity-get (match-string 1)))
  5765. (= (length (nth 6 ee)) 1))
  5766. (let* ((end (if (equal (match-string 2) "{}")
  5767. (match-end 2)
  5768. (match-end 1))))
  5769. (add-text-properties
  5770. (match-beginning 0) end
  5771. (list 'font-lock-fontified t))
  5772. (compose-region (match-beginning 0) end
  5773. (nth 6 ee) nil)
  5774. (backward-char 1)
  5775. (throw 'match t))))
  5776. nil))))
  5777. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5778. "Fontify string S like in Org-mode."
  5779. (with-temp-buffer
  5780. (insert s)
  5781. (let ((org-odd-levels-only odd-levels))
  5782. (org-mode)
  5783. (org-font-lock-ensure)
  5784. (buffer-string))))
  5785. (defvar org-m nil)
  5786. (defvar org-l nil)
  5787. (defvar org-f nil)
  5788. (defun org-get-level-face (n)
  5789. "Get the right face for match N in font-lock matching of headlines."
  5790. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5791. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5792. (if org-cycle-level-faces
  5793. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5794. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5795. (cond
  5796. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5797. ((eq n 2) org-f)
  5798. (t (unless org-level-color-stars-only org-f))))
  5799. (defun org-face-from-face-or-color (context inherit face-or-color)
  5800. "Create a face list that inherits INHERIT, but sets the foreground color.
  5801. When FACE-OR-COLOR is not a string, just return it."
  5802. (if (stringp face-or-color)
  5803. (list :inherit inherit
  5804. (cdr (assoc context org-faces-easy-properties))
  5805. face-or-color)
  5806. face-or-color))
  5807. (defun org-get-todo-face (kwd)
  5808. "Get the right face for a TODO keyword KWD.
  5809. If KWD is a number, get the corresponding match group."
  5810. (when (numberp kwd) (setq kwd (match-string kwd)))
  5811. (or (org-face-from-face-or-color
  5812. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5813. (and (member kwd org-done-keywords) 'org-done)
  5814. 'org-todo))
  5815. (defun org-get-priority-face (priority)
  5816. "Get the right face for PRIORITY.
  5817. PRIORITY is a character."
  5818. (or (org-face-from-face-or-color
  5819. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5820. 'org-priority))
  5821. (defun org-get-tag-face (tag)
  5822. "Get the right face for TAG.
  5823. If TAG is a number, get the corresponding match group."
  5824. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5825. (or (org-face-from-face-or-color
  5826. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5827. 'org-tag)))
  5828. (defun org-font-lock-add-priority-faces (limit)
  5829. "Add the special priority faces."
  5830. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5831. (add-text-properties
  5832. (match-beginning 1) (match-end 1)
  5833. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5834. 'font-lock-fontified t))))
  5835. (defun org-font-lock-add-tag-faces (limit)
  5836. "Add the special tag faces."
  5837. (when (and org-tag-faces org-tags-special-faces-re)
  5838. (while (re-search-forward org-tags-special-faces-re limit t)
  5839. (add-text-properties (match-beginning 1) (match-end 1)
  5840. (list 'face (org-get-tag-face 1)
  5841. 'font-lock-fontified t))
  5842. (backward-char 1))))
  5843. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5844. "Remove fontification and activation overlays from links."
  5845. (font-lock-default-unfontify-region beg end)
  5846. (let* ((buffer-undo-list t)
  5847. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5848. (inhibit-modification-hooks t)
  5849. deactivate-mark buffer-file-name buffer-file-truename)
  5850. (decompose-region beg end)
  5851. (remove-text-properties beg end
  5852. '(mouse-face t keymap t org-linked-text t
  5853. invisible t intangible t
  5854. org-emphasis t))
  5855. (org-remove-font-lock-display-properties beg end)))
  5856. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5857. ((raise 0.3) (height 0.7))
  5858. ((raise -0.5))
  5859. ((raise 0.5)))
  5860. "Display properties for showing superscripts and subscripts.")
  5861. (defun org-remove-font-lock-display-properties (beg end)
  5862. "Remove specific display properties that have been added by font lock.
  5863. The will remove the raise properties that are used to show superscripts
  5864. and subscripts."
  5865. (let (next prop)
  5866. (while (< beg end)
  5867. (setq next (next-single-property-change beg 'display nil end)
  5868. prop (get-text-property beg 'display))
  5869. (when (member prop org-script-display)
  5870. (put-text-property beg next 'display nil))
  5871. (setq beg next))))
  5872. (defun org-raise-scripts (limit)
  5873. "Add raise properties to sub/superscripts."
  5874. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5875. (re-search-forward
  5876. (if (eq org-use-sub-superscripts t)
  5877. org-match-substring-regexp
  5878. org-match-substring-with-braces-regexp)
  5879. limit t))
  5880. (let* ((pos (point)) table-p comment-p
  5881. (mpos (match-beginning 3))
  5882. (emph-p (get-text-property mpos 'org-emphasis))
  5883. (link-p (get-text-property mpos 'mouse-face))
  5884. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5885. (goto-char (point-at-bol))
  5886. (setq table-p (looking-at-p org-table-dataline-regexp)
  5887. comment-p (looking-at-p "^[ \t]*#[ +]"))
  5888. (goto-char pos)
  5889. ;; Handle a_b^c
  5890. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5891. (unless (or comment-p emph-p link-p keyw-p)
  5892. (put-text-property (match-beginning 3) (match-end 0)
  5893. 'display
  5894. (if (equal (char-after (match-beginning 2)) ?^)
  5895. (nth (if table-p 3 1) org-script-display)
  5896. (nth (if table-p 2 0) org-script-display)))
  5897. (add-text-properties (match-beginning 2) (match-end 2)
  5898. (list 'invisible t
  5899. 'org-dwidth t 'org-dwidth-n 1))
  5900. (if (and (eq (char-after (match-beginning 3)) ?{)
  5901. (eq (char-before (match-end 3)) ?}))
  5902. (progn
  5903. (add-text-properties
  5904. (match-beginning 3) (1+ (match-beginning 3))
  5905. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5906. (add-text-properties
  5907. (1- (match-end 3)) (match-end 3)
  5908. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5909. t)))
  5910. ;;;; Visibility cycling, including org-goto and indirect buffer
  5911. ;;; Cycling
  5912. (defvar-local org-cycle-global-status nil)
  5913. (put 'org-cycle-global-status 'org-state t)
  5914. (defvar-local org-cycle-subtree-status nil)
  5915. (put 'org-cycle-subtree-status 'org-state t)
  5916. (defvar org-inlinetask-min-level)
  5917. (defun org-unlogged-message (&rest args)
  5918. "Display a message, but avoid logging it in the *Messages* buffer."
  5919. (let ((message-log-max nil))
  5920. (apply 'message args)))
  5921. ;;;###autoload
  5922. (defun org-cycle (&optional arg)
  5923. "TAB-action and visibility cycling for Org-mode.
  5924. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5925. is outline visibility cycling, but it also invokes other actions
  5926. in special contexts.
  5927. - When this function is called with a prefix argument, rotate the entire
  5928. buffer through 3 states (global cycling)
  5929. 1. OVERVIEW: Show only top-level headlines.
  5930. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5931. 3. SHOW ALL: Show everything.
  5932. With a double \\[universal-argument] prefix argument, \
  5933. switch to the startup visibility,
  5934. determined by the variable `org-startup-folded', and by any VISIBILITY
  5935. properties in the buffer.
  5936. With a triple \\[universal-argument] prefix argument, \
  5937. show the entire buffer, including any drawers.
  5938. - When inside a table, re-align the table and move to the next field.
  5939. - When point is at the beginning of a headline, rotate the subtree started
  5940. by this line through 3 different states (local cycling)
  5941. 1. FOLDED: Only the main headline is shown.
  5942. 2. CHILDREN: The main headline and the direct children are shown.
  5943. From this state, you can move to one of the children
  5944. and zoom in further.
  5945. 3. SUBTREE: Show the entire subtree, including body text.
  5946. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5947. - When point is at the beginning of an empty headline and the variable
  5948. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5949. of the headline by demoting and promoting it to likely levels. This
  5950. speeds up creation document structure by pressing TAB once or several
  5951. times right after creating a new headline.
  5952. - When there is a numeric prefix, go up to a heading with level ARG, do
  5953. a `show-subtree' and return to the previous cursor position. If ARG
  5954. is negative, go up that many levels.
  5955. - When point is not at the beginning of a headline, execute the global
  5956. binding for TAB, which is re-indenting the line. See the option
  5957. `org-cycle-emulate-tab' for details.
  5958. - Special case: if point is at the beginning of the buffer and there is
  5959. no headline in line 1, this function will act as if called with prefix arg
  5960. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5961. But only if also the variable `org-cycle-global-at-bob' is t."
  5962. (interactive "P")
  5963. (org-load-modules-maybe)
  5964. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5965. (and org-cycle-level-after-item/entry-creation
  5966. (or (org-cycle-level)
  5967. (org-cycle-item-indentation))))
  5968. (let* ((limit-level
  5969. (or org-cycle-max-level
  5970. (and (boundp 'org-inlinetask-min-level)
  5971. org-inlinetask-min-level
  5972. (1- org-inlinetask-min-level))))
  5973. (nstars (and limit-level
  5974. (if org-odd-levels-only
  5975. (and limit-level (1- (* limit-level 2)))
  5976. limit-level)))
  5977. (org-outline-regexp
  5978. (if (not (derived-mode-p 'org-mode))
  5979. outline-regexp
  5980. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5981. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5982. (not (looking-at org-outline-regexp))))
  5983. (org-cycle-hook
  5984. (if bob-special
  5985. (delq 'org-optimize-window-after-visibility-change
  5986. (copy-sequence org-cycle-hook))
  5987. org-cycle-hook))
  5988. (pos (point)))
  5989. (when (or bob-special (equal arg '(4)))
  5990. ;; special case: use global cycling
  5991. (setq arg t))
  5992. (cond
  5993. ((equal arg '(16))
  5994. (setq last-command 'dummy)
  5995. (org-set-startup-visibility)
  5996. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5997. ((equal arg '(64))
  5998. (outline-show-all)
  5999. (org-unlogged-message "Entire buffer visible, including drawers"))
  6000. ;; Try cdlatex TAB completion
  6001. ((org-try-cdlatex-tab))
  6002. ;; Table: enter it or move to the next field.
  6003. ((org-at-table-p 'any)
  6004. (if (org-at-table.el-p)
  6005. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  6006. Use \\[org-edit-special] to edit table.el tables"))
  6007. (if arg (org-table-edit-field t)
  6008. (org-table-justify-field-maybe)
  6009. (call-interactively 'org-table-next-field))))
  6010. ((run-hook-with-args-until-success
  6011. 'org-tab-after-check-for-table-hook))
  6012. ;; Global cycling: delegate to `org-cycle-internal-global'.
  6013. ((eq arg t) (org-cycle-internal-global))
  6014. ;; Drawers: delegate to `org-flag-drawer'.
  6015. ((save-excursion
  6016. (beginning-of-line 1)
  6017. (looking-at org-drawer-regexp))
  6018. (org-flag-drawer ; toggle block visibility
  6019. (not (get-char-property (match-end 0) 'invisible))))
  6020. ;; Show-subtree, ARG levels up from here.
  6021. ((integerp arg)
  6022. (save-excursion
  6023. (org-back-to-heading)
  6024. (outline-up-heading (if (< arg 0) (- arg)
  6025. (- (funcall outline-level) arg)))
  6026. (org-show-subtree)))
  6027. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  6028. ((and (featurep 'org-inlinetask)
  6029. (org-inlinetask-at-task-p)
  6030. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6031. (org-inlinetask-toggle-visibility))
  6032. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  6033. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  6034. (save-excursion (move-beginning-of-line 1)
  6035. (looking-at org-outline-regexp)))
  6036. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6037. (org-cycle-internal-local))
  6038. ;; From there: TAB emulation and template completion.
  6039. (buffer-read-only (org-back-to-heading))
  6040. ((run-hook-with-args-until-success
  6041. 'org-tab-after-check-for-cycling-hook))
  6042. ((org-try-structure-completion))
  6043. ((run-hook-with-args-until-success
  6044. 'org-tab-before-tab-emulation-hook))
  6045. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6046. (or (not (bolp))
  6047. (not (looking-at org-outline-regexp))))
  6048. (call-interactively (global-key-binding "\t")))
  6049. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6050. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6051. (or (and (eq org-cycle-emulate-tab 'white)
  6052. (= (match-end 0) (point-at-eol)))
  6053. (and (eq org-cycle-emulate-tab 'whitestart)
  6054. (>= (match-end 0) pos))))
  6055. t
  6056. (eq org-cycle-emulate-tab t))
  6057. (call-interactively (global-key-binding "\t")))
  6058. (t (save-excursion
  6059. (org-back-to-heading)
  6060. (org-cycle)))))))
  6061. (defun org-cycle-internal-global ()
  6062. "Do the global cycling action."
  6063. ;; Hack to avoid display of messages for .org attachments in Gnus
  6064. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6065. (cond
  6066. ((and (eq last-command this-command)
  6067. (eq org-cycle-global-status 'overview))
  6068. ;; We just created the overview - now do table of contents
  6069. ;; This can be slow in very large buffers, so indicate action
  6070. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6071. (unless ga (org-unlogged-message "CONTENTS..."))
  6072. (org-content)
  6073. (unless ga (org-unlogged-message "CONTENTS...done"))
  6074. (setq org-cycle-global-status 'contents)
  6075. (run-hook-with-args 'org-cycle-hook 'contents))
  6076. ((and (eq last-command this-command)
  6077. (eq org-cycle-global-status 'contents))
  6078. ;; We just showed the table of contents - now show everything
  6079. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6080. (outline-show-all)
  6081. (unless ga (org-unlogged-message "SHOW ALL"))
  6082. (setq org-cycle-global-status 'all)
  6083. (run-hook-with-args 'org-cycle-hook 'all))
  6084. (t
  6085. ;; Default action: go to overview
  6086. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6087. (org-overview)
  6088. (unless ga (org-unlogged-message "OVERVIEW"))
  6089. (setq org-cycle-global-status 'overview)
  6090. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6091. (defvar org-called-with-limited-levels nil
  6092. "Non-nil when `org-with-limited-levels' is currently active.")
  6093. (defun org-cycle-internal-local ()
  6094. "Do the local cycling action."
  6095. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6096. ;; First, determine end of headline (EOH), end of subtree or item
  6097. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6098. (save-excursion
  6099. (if (org-at-item-p)
  6100. (progn
  6101. (beginning-of-line)
  6102. (setq struct (org-list-struct))
  6103. (setq eoh (point-at-eol))
  6104. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6105. (setq has-children (org-list-has-child-p (point) struct)))
  6106. (org-back-to-heading)
  6107. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6108. (setq eos (save-excursion (org-end-of-subtree t t)
  6109. (when (bolp) (backward-char)) (point)))
  6110. (setq has-children
  6111. (or (save-excursion
  6112. (let ((level (funcall outline-level)))
  6113. (outline-next-heading)
  6114. (and (org-at-heading-p t)
  6115. (> (funcall outline-level) level))))
  6116. (save-excursion
  6117. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6118. ;; Determine end invisible part of buffer (EOL)
  6119. (beginning-of-line 2)
  6120. (while (and (not (eobp)) ;This is like `next-line'.
  6121. (get-char-property (1- (point)) 'invisible))
  6122. (goto-char (next-single-char-property-change (point) 'invisible))
  6123. (and (eolp) (beginning-of-line 2)))
  6124. (setq eol (point)))
  6125. ;; Find out what to do next and set `this-command'
  6126. (cond
  6127. ((= eos eoh)
  6128. ;; Nothing is hidden behind this heading
  6129. (unless (org-before-first-heading-p)
  6130. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6131. (org-unlogged-message "EMPTY ENTRY")
  6132. (setq org-cycle-subtree-status nil)
  6133. (save-excursion
  6134. (goto-char eos)
  6135. (outline-next-heading)
  6136. (when (outline-invisible-p) (org-flag-heading nil))))
  6137. ((and (or (>= eol eos)
  6138. (not (string-match "\\S-" (buffer-substring eol eos))))
  6139. (or has-children
  6140. (not (setq children-skipped
  6141. org-cycle-skip-children-state-if-no-children))))
  6142. ;; Entire subtree is hidden in one line: children view
  6143. (unless (org-before-first-heading-p)
  6144. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6145. (if (org-at-item-p)
  6146. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6147. (org-show-entry)
  6148. (org-with-limited-levels (org-show-children))
  6149. ;; FIXME: This slows down the func way too much.
  6150. ;; How keep drawers hidden in subtree anyway?
  6151. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6152. ;; (org-cycle-hide-drawers 'subtree))
  6153. ;; Fold every list in subtree to top-level items.
  6154. (when (eq org-cycle-include-plain-lists 'integrate)
  6155. (save-excursion
  6156. (org-back-to-heading)
  6157. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6158. (beginning-of-line 1)
  6159. (let* ((struct (org-list-struct))
  6160. (prevs (org-list-prevs-alist struct))
  6161. (end (org-list-get-bottom-point struct)))
  6162. (dolist (e (org-list-get-all-items (point) struct prevs))
  6163. (org-list-set-item-visibility e struct 'folded))
  6164. (goto-char (if (< end eos) end eos)))))))
  6165. (org-unlogged-message "CHILDREN")
  6166. (save-excursion
  6167. (goto-char eos)
  6168. (outline-next-heading)
  6169. (when (outline-invisible-p) (org-flag-heading nil)))
  6170. (setq org-cycle-subtree-status 'children)
  6171. (unless (org-before-first-heading-p)
  6172. (run-hook-with-args 'org-cycle-hook 'children)))
  6173. ((or children-skipped
  6174. (and (eq last-command this-command)
  6175. (eq org-cycle-subtree-status 'children)))
  6176. ;; We just showed the children, or no children are there,
  6177. ;; now show everything.
  6178. (unless (org-before-first-heading-p)
  6179. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6180. (outline-flag-region eoh eos nil)
  6181. (org-unlogged-message
  6182. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6183. (setq org-cycle-subtree-status 'subtree)
  6184. (unless (org-before-first-heading-p)
  6185. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6186. (t
  6187. ;; Default action: hide the subtree.
  6188. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6189. (outline-flag-region eoh eos t)
  6190. (org-unlogged-message "FOLDED")
  6191. (setq org-cycle-subtree-status 'folded)
  6192. (unless (org-before-first-heading-p)
  6193. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6194. ;;;###autoload
  6195. (defun org-global-cycle (&optional arg)
  6196. "Cycle the global visibility. For details see `org-cycle'.
  6197. With \\[universal-argument] prefix arg, switch to startup visibility.
  6198. With a numeric prefix, show all headlines up to that level."
  6199. (interactive "P")
  6200. (let ((org-cycle-include-plain-lists
  6201. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6202. (cond
  6203. ((integerp arg)
  6204. (outline-show-all)
  6205. (outline-hide-sublevels arg)
  6206. (setq org-cycle-global-status 'contents))
  6207. ((equal arg '(4))
  6208. (org-set-startup-visibility)
  6209. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6210. (t
  6211. (org-cycle '(4))))))
  6212. (defun org-set-startup-visibility ()
  6213. "Set the visibility required by startup options and properties."
  6214. (cond
  6215. ((eq org-startup-folded t)
  6216. (org-overview))
  6217. ((eq org-startup-folded 'content)
  6218. (org-content))
  6219. ((or (eq org-startup-folded 'showeverything)
  6220. (eq org-startup-folded nil))
  6221. (outline-show-all)))
  6222. (unless (eq org-startup-folded 'showeverything)
  6223. (when org-hide-block-startup (org-hide-block-all))
  6224. (org-set-visibility-according-to-property 'no-cleanup)
  6225. (org-cycle-hide-archived-subtrees 'all)
  6226. (org-cycle-hide-drawers 'all)
  6227. (org-cycle-show-empty-lines t)))
  6228. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6229. "Switch subtree visibilities according to :VISIBILITY: property."
  6230. (interactive)
  6231. (org-with-wide-buffer
  6232. (goto-char (point-min))
  6233. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6234. (if (not (org-at-property-p)) (outline-next-heading)
  6235. (let ((state (match-string 3)))
  6236. (save-excursion
  6237. (org-back-to-heading t)
  6238. (outline-hide-subtree)
  6239. (org-reveal)
  6240. (cond
  6241. ((equal state "folded")
  6242. (outline-hide-subtree))
  6243. ((equal state "children")
  6244. (org-show-hidden-entry)
  6245. (org-show-children))
  6246. ((equal state "content")
  6247. (save-excursion
  6248. (save-restriction
  6249. (org-narrow-to-subtree)
  6250. (org-content))))
  6251. ((member state '("all" "showall"))
  6252. (outline-show-subtree)))))))
  6253. (unless no-cleanup
  6254. (org-cycle-hide-archived-subtrees 'all)
  6255. (org-cycle-hide-drawers 'all)
  6256. (org-cycle-show-empty-lines 'all))))
  6257. ;; This function uses outline-regexp instead of the more fundamental
  6258. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6259. ;; buffers, where outline-regexp is needed.
  6260. (defun org-overview ()
  6261. "Switch to overview mode, showing only top-level headlines.
  6262. This shows all headlines with a level equal or greater than the level
  6263. of the first headline in the buffer. This is important, because if the
  6264. first headline is not level one, then (hide-sublevels 1) gives confusing
  6265. results."
  6266. (interactive)
  6267. (save-excursion
  6268. (let ((level
  6269. (save-excursion
  6270. (goto-char (point-min))
  6271. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6272. (goto-char (match-beginning 0))
  6273. (funcall outline-level)))))
  6274. (and level (outline-hide-sublevels level)))))
  6275. (defun org-content (&optional arg)
  6276. "Show all headlines in the buffer, like a table of contents.
  6277. With numerical argument N, show content up to level N."
  6278. (interactive "P")
  6279. (org-overview)
  6280. (save-excursion
  6281. ;; Visit all headings and show their offspring
  6282. (and (integerp arg) (org-overview))
  6283. (goto-char (point-max))
  6284. (catch 'exit
  6285. (while (and (progn (condition-case nil
  6286. (outline-previous-visible-heading 1)
  6287. (error (goto-char (point-min))))
  6288. t)
  6289. (looking-at org-outline-regexp))
  6290. (if (integerp arg)
  6291. (org-show-children (1- arg))
  6292. (outline-show-branches))
  6293. (when (bobp) (throw 'exit nil))))))
  6294. (defun org-optimize-window-after-visibility-change (state)
  6295. "Adjust the window after a change in outline visibility.
  6296. This function is the default value of the hook `org-cycle-hook'."
  6297. (when (get-buffer-window (current-buffer))
  6298. (cond
  6299. ((eq state 'content) nil)
  6300. ((eq state 'all) nil)
  6301. ((eq state 'folded) nil)
  6302. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6303. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6304. (defun org-remove-empty-overlays-at (pos)
  6305. "Remove outline overlays that do not contain non-white stuff."
  6306. (dolist (o (overlays-at pos))
  6307. (and (eq 'outline (overlay-get o 'invisible))
  6308. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6309. (overlay-end o))))
  6310. (delete-overlay o))))
  6311. (defun org-clean-visibility-after-subtree-move ()
  6312. "Fix visibility issues after moving a subtree."
  6313. ;; First, find a reasonable region to look at:
  6314. ;; Start two siblings above, end three below
  6315. (let* ((beg (save-excursion
  6316. (and (org-get-last-sibling)
  6317. (org-get-last-sibling))
  6318. (point)))
  6319. (end (save-excursion
  6320. (and (org-get-next-sibling)
  6321. (org-get-next-sibling)
  6322. (org-get-next-sibling))
  6323. (if (org-at-heading-p)
  6324. (point-at-eol)
  6325. (point))))
  6326. (level (looking-at "\\*+"))
  6327. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6328. (save-excursion
  6329. (save-restriction
  6330. (narrow-to-region beg end)
  6331. (when re
  6332. ;; Properly fold already folded siblings
  6333. (goto-char (point-min))
  6334. (while (re-search-forward re nil t)
  6335. (when (and (not (outline-invisible-p))
  6336. (save-excursion
  6337. (goto-char (point-at-eol)) (outline-invisible-p)))
  6338. (outline-hide-entry))))
  6339. (org-cycle-show-empty-lines 'overview)
  6340. (org-cycle-hide-drawers 'overview)))))
  6341. (defun org-cycle-show-empty-lines (state)
  6342. "Show empty lines above all visible headlines.
  6343. The region to be covered depends on STATE when called through
  6344. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6345. entire buffer covered. Note that an empty line is only shown if there
  6346. are at least `org-cycle-separator-lines' empty lines before the headline."
  6347. (when (/= org-cycle-separator-lines 0)
  6348. (save-excursion
  6349. (let* ((n (abs org-cycle-separator-lines))
  6350. (re (cond
  6351. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6352. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6353. (t (let ((ns (number-to-string (- n 2))))
  6354. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6355. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6356. beg end)
  6357. (cond
  6358. ((memq state '(overview contents t))
  6359. (setq beg (point-min) end (point-max)))
  6360. ((memq state '(children folded))
  6361. (setq beg (point)
  6362. end (progn (org-end-of-subtree t t)
  6363. (line-beginning-position 2)))))
  6364. (when beg
  6365. (goto-char beg)
  6366. (while (re-search-forward re end t)
  6367. (unless (get-char-property (match-end 1) 'invisible)
  6368. (let ((e (match-end 1))
  6369. (b (if (>= org-cycle-separator-lines 0)
  6370. (match-beginning 1)
  6371. (save-excursion
  6372. (goto-char (match-beginning 0))
  6373. (skip-chars-backward " \t\n")
  6374. (line-end-position)))))
  6375. (outline-flag-region b e nil))))))))
  6376. ;; Never hide empty lines at the end of the file.
  6377. (save-excursion
  6378. (goto-char (point-max))
  6379. (outline-previous-heading)
  6380. (outline-end-of-heading)
  6381. (when (and (looking-at "[ \t\n]+")
  6382. (= (match-end 0) (point-max)))
  6383. (outline-flag-region (point) (match-end 0) nil))))
  6384. (defun org-show-empty-lines-in-parent ()
  6385. "Move to the parent and re-show empty lines before visible headlines."
  6386. (save-excursion
  6387. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6388. (org-cycle-show-empty-lines context))))
  6389. (defun org-files-list ()
  6390. "Return `org-agenda-files' list, plus all open org-mode files.
  6391. This is useful for operations that need to scan all of a user's
  6392. open and agenda-wise Org files."
  6393. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6394. (dolist (buf (buffer-list))
  6395. (with-current-buffer buf
  6396. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6397. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6398. files))
  6399. (defsubst org-entry-beginning-position ()
  6400. "Return the beginning position of the current entry."
  6401. (save-excursion (org-back-to-heading t) (point)))
  6402. (defsubst org-entry-end-position ()
  6403. "Return the end position of the current entry."
  6404. (save-excursion (outline-next-heading) (point)))
  6405. (defun org-cycle-hide-drawers (state &optional exceptions)
  6406. "Re-hide all drawers after a visibility state change.
  6407. When non-nil, optional argument EXCEPTIONS is a list of strings
  6408. specifying which drawers should not be hidden."
  6409. (when (and (derived-mode-p 'org-mode)
  6410. (not (memq state '(overview folded contents))))
  6411. (save-excursion
  6412. (let* ((globalp (memq state '(contents all)))
  6413. (beg (if globalp (point-min) (point)))
  6414. (end (if globalp (point-max)
  6415. (if (eq state 'children)
  6416. (save-excursion (outline-next-heading) (point))
  6417. (org-end-of-subtree t)))))
  6418. (goto-char beg)
  6419. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6420. (unless (member-ignore-case (match-string 1) exceptions)
  6421. (let ((drawer (org-element-at-point)))
  6422. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6423. (org-flag-drawer t drawer)
  6424. ;; Make sure to skip drawer entirely or we might flag
  6425. ;; it another time when matching its ending line with
  6426. ;; `org-drawer-regexp'.
  6427. (goto-char (org-element-property :end drawer))))))))))
  6428. (defun org-flag-drawer (flag &optional element)
  6429. "When FLAG is non-nil, hide the drawer we are at.
  6430. Otherwise make it visible. When optional argument ELEMENT is
  6431. a parsed drawer, as returned by `org-element-at-point', hide or
  6432. show that drawer instead."
  6433. (let ((drawer (or element
  6434. (and (save-excursion
  6435. (beginning-of-line)
  6436. (looking-at-p org-drawer-regexp))
  6437. (org-element-at-point)))))
  6438. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6439. (let ((post (org-element-property :post-affiliated drawer)))
  6440. (save-excursion
  6441. (outline-flag-region
  6442. (progn (goto-char post) (line-end-position))
  6443. (progn (goto-char (org-element-property :end drawer))
  6444. (skip-chars-backward " \r\t\n")
  6445. (line-end-position))
  6446. flag))
  6447. ;; When the drawer is hidden away, make sure point lies in
  6448. ;; a visible part of the buffer.
  6449. (when (and flag (> (line-beginning-position) post))
  6450. (goto-char post))))))
  6451. (defun org-subtree-end-visible-p ()
  6452. "Is the end of the current subtree visible?"
  6453. (pos-visible-in-window-p
  6454. (save-excursion (org-end-of-subtree t) (point))))
  6455. (defun org-first-headline-recenter ()
  6456. "Move cursor to the first headline and recenter the headline."
  6457. (let ((window (get-buffer-window)))
  6458. (when window
  6459. (goto-char (point-min))
  6460. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6461. (set-window-start window (line-beginning-position))))))
  6462. ;;; Saving and restoring visibility
  6463. (defun org-outline-overlay-data (&optional use-markers)
  6464. "Return a list of the locations of all outline overlays.
  6465. These are overlays with the `invisible' property value `outline'.
  6466. The return value is a list of cons cells, with start and stop
  6467. positions for each overlay.
  6468. If USE-MARKERS is set, return the positions as markers."
  6469. (let (beg end)
  6470. (save-excursion
  6471. (save-restriction
  6472. (widen)
  6473. (delq nil
  6474. (mapcar (lambda (o)
  6475. (when (eq (overlay-get o 'invisible) 'outline)
  6476. (setq beg (overlay-start o)
  6477. end (overlay-end o))
  6478. (and beg end (> end beg)
  6479. (if use-markers
  6480. (cons (copy-marker beg)
  6481. (copy-marker end t))
  6482. (cons beg end)))))
  6483. (overlays-in (point-min) (point-max))))))))
  6484. (defun org-set-outline-overlay-data (data)
  6485. "Create visibility overlays for all positions in DATA.
  6486. DATA should have been made by `org-outline-overlay-data'."
  6487. (org-with-wide-buffer
  6488. (outline-show-all)
  6489. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6490. ;;; Folding of blocks
  6491. (defvar-local org-hide-block-overlays nil
  6492. "Overlays hiding blocks.")
  6493. (defun org-block-map (function &optional start end)
  6494. "Call FUNCTION at the head of all source blocks in the current buffer.
  6495. Optional arguments START and END can be used to limit the range."
  6496. (let ((start (or start (point-min)))
  6497. (end (or end (point-max))))
  6498. (save-excursion
  6499. (goto-char start)
  6500. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6501. (save-excursion
  6502. (save-match-data
  6503. (goto-char (match-beginning 0))
  6504. (funcall function)))))))
  6505. (defun org-hide-block-toggle-all ()
  6506. "Toggle the visibility of all blocks in the current buffer."
  6507. (org-block-map 'org-hide-block-toggle))
  6508. (defun org-hide-block-all ()
  6509. "Fold all blocks in the current buffer."
  6510. (interactive)
  6511. (org-show-block-all)
  6512. (org-block-map 'org-hide-block-toggle-maybe))
  6513. (defun org-show-block-all ()
  6514. "Unfold all blocks in the current buffer."
  6515. (interactive)
  6516. (mapc #'delete-overlay org-hide-block-overlays)
  6517. (setq org-hide-block-overlays nil))
  6518. (defun org-hide-block-toggle-maybe ()
  6519. "Toggle visibility of block at point.
  6520. Unlike to `org-hide-block-toggle', this function does not throw
  6521. an error. Return a non-nil value when toggling is successful."
  6522. (interactive)
  6523. (ignore-errors (org-hide-block-toggle)))
  6524. (defun org-hide-block-toggle (&optional force)
  6525. "Toggle the visibility of the current block.
  6526. When optional argument FORCE is `off', make block visible. If it
  6527. is non-nil, hide it unconditionally. Throw an error when not at
  6528. a block. Return a non-nil value when toggling is successful."
  6529. (interactive)
  6530. (let ((element (org-element-at-point)))
  6531. (unless (memq (org-element-type element)
  6532. '(center-block comment-block dynamic-block example-block
  6533. export-block quote-block special-block
  6534. src-block verse-block))
  6535. (user-error "Not at a block"))
  6536. (let* ((start (save-excursion
  6537. (goto-char (org-element-property :post-affiliated element))
  6538. (line-end-position)))
  6539. (end (save-excursion
  6540. (goto-char (org-element-property :end element))
  6541. (skip-chars-backward " \r\t\n")
  6542. (line-end-position)))
  6543. (overlays (overlays-at start)))
  6544. (cond
  6545. ;; Do nothing when not before or at the block opening line or
  6546. ;; at the block closing line.
  6547. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6548. ((and (not (eq force 'off))
  6549. (not (memq t (mapcar
  6550. (lambda (o)
  6551. (eq (overlay-get o 'invisible) 'org-hide-block))
  6552. overlays))))
  6553. (let ((ov (make-overlay start end)))
  6554. (overlay-put ov 'invisible 'org-hide-block)
  6555. ;; Make the block accessible to `isearch'.
  6556. (overlay-put
  6557. ov 'isearch-open-invisible
  6558. (lambda (ov)
  6559. (when (memq ov org-hide-block-overlays)
  6560. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6561. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6562. (delete-overlay ov))))
  6563. (push ov org-hide-block-overlays)
  6564. ;; When the block is hidden away, make sure point is left in
  6565. ;; a visible part of the buffer.
  6566. (when (> (line-beginning-position) start)
  6567. (goto-char start)
  6568. (beginning-of-line))
  6569. ;; Signal successful toggling.
  6570. t))
  6571. ((or (not force) (eq force 'off))
  6572. (dolist (ov overlays t)
  6573. (when (memq ov org-hide-block-overlays)
  6574. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6575. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6576. (delete-overlay ov))))))))
  6577. ;; org-tab-after-check-for-cycling-hook
  6578. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6579. ;; Remove overlays when changing major mode
  6580. (add-hook 'org-mode-hook
  6581. (lambda () (add-hook 'change-major-mode-hook
  6582. 'org-show-block-all 'append 'local)))
  6583. ;;; Org-goto
  6584. (defvar org-goto-window-configuration nil)
  6585. (defvar org-goto-marker nil)
  6586. (defvar org-goto-map)
  6587. (defun org-goto-map ()
  6588. "Set the keymap `org-goto'."
  6589. (setq org-goto-map
  6590. (let ((map (make-sparse-keymap)))
  6591. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6592. mouse-drag-region universal-argument org-occur)))
  6593. (dolist (cmd cmds)
  6594. (substitute-key-definition cmd cmd map global-map)))
  6595. (suppress-keymap map)
  6596. (org-defkey map "\C-m" 'org-goto-ret)
  6597. (org-defkey map [(return)] 'org-goto-ret)
  6598. (org-defkey map [(left)] 'org-goto-left)
  6599. (org-defkey map [(right)] 'org-goto-right)
  6600. (org-defkey map [(control ?g)] 'org-goto-quit)
  6601. (org-defkey map "\C-i" 'org-cycle)
  6602. (org-defkey map [(tab)] 'org-cycle)
  6603. (org-defkey map [(down)] 'outline-next-visible-heading)
  6604. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6605. (if org-goto-auto-isearch
  6606. (if (fboundp 'define-key-after)
  6607. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6608. nil)
  6609. (org-defkey map "q" 'org-goto-quit)
  6610. (org-defkey map "n" 'outline-next-visible-heading)
  6611. (org-defkey map "p" 'outline-previous-visible-heading)
  6612. (org-defkey map "f" 'outline-forward-same-level)
  6613. (org-defkey map "b" 'outline-backward-same-level)
  6614. (org-defkey map "u" 'outline-up-heading))
  6615. (org-defkey map "/" 'org-occur)
  6616. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6617. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6618. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6619. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6620. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6621. map)))
  6622. (defconst org-goto-help
  6623. "Browse buffer copy, to find location or copy text.%s
  6624. RET=jump to location C-g=quit and return to previous location
  6625. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6626. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6627. (defun org-goto (&optional alternative-interface)
  6628. "Look up a different location in the current file, keeping current visibility.
  6629. When you want look-up or go to a different location in a
  6630. document, the fastest way is often to fold the entire buffer and
  6631. then dive into the tree. This method has the disadvantage, that
  6632. the previous location will be folded, which may not be what you
  6633. want.
  6634. This command works around this by showing a copy of the current
  6635. buffer in an indirect buffer, in overview mode. You can dive
  6636. into the tree in that copy, use org-occur and incremental search
  6637. to find a location. When pressing RET or `Q', the command
  6638. returns to the original buffer in which the visibility is still
  6639. unchanged. After RET it will also jump to the location selected
  6640. in the indirect buffer and expose the headline hierarchy above.
  6641. With a prefix argument, use the alternative interface: e.g., if
  6642. `org-goto-interface' is `outline' use `outline-path-completion'."
  6643. (interactive "P")
  6644. (org-goto-map)
  6645. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6646. (org-refile-use-outline-path t)
  6647. (org-refile-target-verify-function nil)
  6648. (interface
  6649. (if (not alternative-interface)
  6650. org-goto-interface
  6651. (if (eq org-goto-interface 'outline)
  6652. 'outline-path-completion
  6653. 'outline)))
  6654. (org-goto-start-pos (point))
  6655. (selected-point
  6656. (if (eq interface 'outline)
  6657. (car (org-get-location (current-buffer) org-goto-help))
  6658. (let ((pa (org-refile-get-location "Goto")))
  6659. (org-refile-check-position pa)
  6660. (nth 3 pa)))))
  6661. (if selected-point
  6662. (progn
  6663. (org-mark-ring-push org-goto-start-pos)
  6664. (goto-char selected-point)
  6665. (when (or (outline-invisible-p) (org-invisible-p2))
  6666. (org-show-context 'org-goto)))
  6667. (message "Quit"))))
  6668. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6669. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6670. (defvar org-goto-local-auto-isearch-map) ; defined below
  6671. (defun org-get-location (_buf help)
  6672. "Let the user select a location in current buffer.
  6673. This function uses a recursive edit. It returns the selected position
  6674. or nil."
  6675. (org-no-popups
  6676. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6677. (isearch-hide-immediately nil)
  6678. (isearch-search-fun-function
  6679. (lambda () 'org-goto-local-search-headings))
  6680. (org-goto-selected-point org-goto-exit-command))
  6681. (save-excursion
  6682. (save-window-excursion
  6683. (delete-other-windows)
  6684. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6685. (org-pop-to-buffer-same-window
  6686. (condition-case nil
  6687. (make-indirect-buffer (current-buffer) "*org-goto*")
  6688. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6689. (with-output-to-temp-buffer "*Org Help*"
  6690. (princ (format help (if org-goto-auto-isearch
  6691. " Just type for auto-isearch."
  6692. " n/p/f/b/u to navigate, q to quit."))))
  6693. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6694. (setq buffer-read-only nil)
  6695. (let ((org-startup-truncated t)
  6696. (org-startup-folded nil)
  6697. (org-startup-align-all-tables nil))
  6698. (org-mode)
  6699. (org-overview))
  6700. (setq buffer-read-only t)
  6701. (if (and (boundp 'org-goto-start-pos)
  6702. (integer-or-marker-p org-goto-start-pos))
  6703. (progn (goto-char org-goto-start-pos)
  6704. (when (outline-invisible-p)
  6705. (org-show-set-visibility 'lineage)))
  6706. (goto-char (point-min)))
  6707. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6708. (message "Select location and press RET")
  6709. (use-local-map org-goto-map)
  6710. (recursive-edit)))
  6711. (kill-buffer "*org-goto*")
  6712. (cons org-goto-selected-point org-goto-exit-command))))
  6713. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6714. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6715. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6716. (if (fboundp 'isearch-other-control-char)
  6717. (progn
  6718. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6719. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6720. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6721. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6722. (define-key org-goto-local-auto-isearch-map [return] nil))
  6723. (defun org-goto-local-search-headings (string bound noerror)
  6724. "Search and make sure that any matches are in headlines."
  6725. (catch 'return
  6726. (while (if isearch-forward
  6727. (search-forward string bound noerror)
  6728. (search-backward string bound noerror))
  6729. (when (save-match-data
  6730. (and (save-excursion
  6731. (beginning-of-line)
  6732. (looking-at org-complex-heading-regexp))
  6733. (or (not (match-beginning 5))
  6734. (< (point) (match-beginning 5)))))
  6735. (throw 'return (point))))))
  6736. (defun org-goto-local-auto-isearch ()
  6737. "Start isearch."
  6738. (interactive)
  6739. (goto-char (point-min))
  6740. (let ((keys (this-command-keys)))
  6741. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6742. (isearch-mode t)
  6743. (isearch-process-search-char (string-to-char keys)))))
  6744. (defun org-goto-ret (&optional _arg)
  6745. "Finish `org-goto' by going to the new location."
  6746. (interactive "P")
  6747. (setq org-goto-selected-point (point))
  6748. (setq org-goto-exit-command 'return)
  6749. (throw 'exit nil))
  6750. (defun org-goto-left ()
  6751. "Finish `org-goto' by going to the new location."
  6752. (interactive)
  6753. (if (org-at-heading-p)
  6754. (progn
  6755. (beginning-of-line 1)
  6756. (setq org-goto-selected-point (point)
  6757. org-goto-exit-command 'left)
  6758. (throw 'exit nil))
  6759. (user-error "Not on a heading")))
  6760. (defun org-goto-right ()
  6761. "Finish `org-goto' by going to the new location."
  6762. (interactive)
  6763. (if (org-at-heading-p)
  6764. (progn
  6765. (setq org-goto-selected-point (point)
  6766. org-goto-exit-command 'right)
  6767. (throw 'exit nil))
  6768. (user-error "Not on a heading")))
  6769. (defun org-goto-quit ()
  6770. "Finish `org-goto' without cursor motion."
  6771. (interactive)
  6772. (setq org-goto-selected-point nil)
  6773. (setq org-goto-exit-command 'quit)
  6774. (throw 'exit nil))
  6775. ;;; Indirect buffer display of subtrees
  6776. (defvar org-indirect-dedicated-frame nil
  6777. "This is the frame being used for indirect tree display.")
  6778. (defvar org-last-indirect-buffer nil)
  6779. (defun org-tree-to-indirect-buffer (&optional arg)
  6780. "Create indirect buffer and narrow it to current subtree.
  6781. With a numerical prefix ARG, go up to this level and then take that tree.
  6782. If ARG is negative, go up that many levels.
  6783. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6784. indirect buffer previously made with this command, to avoid proliferation of
  6785. indirect buffers. However, when you call the command with a \
  6786. \\[universal-argument] prefix, or
  6787. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6788. is kept so that you can work with several indirect buffers at the same time.
  6789. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6790. \\[universal-argument] prefix also
  6791. requests that a new frame be made for the new buffer, so that the dedicated
  6792. frame is not changed."
  6793. (interactive "P")
  6794. (let ((cbuf (current-buffer))
  6795. (cwin (selected-window))
  6796. (pos (point))
  6797. beg end level heading ibuf)
  6798. (save-excursion
  6799. (org-back-to-heading t)
  6800. (when (numberp arg)
  6801. (setq level (org-outline-level))
  6802. (when (< arg 0) (setq arg (+ level arg)))
  6803. (while (> (setq level (org-outline-level)) arg)
  6804. (org-up-heading-safe)))
  6805. (setq beg (point)
  6806. heading (org-get-heading 'no-tags))
  6807. (org-end-of-subtree t t)
  6808. (when (org-at-heading-p) (backward-char 1))
  6809. (setq end (point)))
  6810. (when (and (buffer-live-p org-last-indirect-buffer)
  6811. (not (eq org-indirect-buffer-display 'new-frame))
  6812. (not arg))
  6813. (kill-buffer org-last-indirect-buffer))
  6814. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6815. org-last-indirect-buffer ibuf)
  6816. (cond
  6817. ((or (eq org-indirect-buffer-display 'new-frame)
  6818. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6819. (select-frame (make-frame))
  6820. (delete-other-windows)
  6821. (org-pop-to-buffer-same-window ibuf)
  6822. (org-set-frame-title heading))
  6823. ((eq org-indirect-buffer-display 'dedicated-frame)
  6824. (raise-frame
  6825. (select-frame (or (and org-indirect-dedicated-frame
  6826. (frame-live-p org-indirect-dedicated-frame)
  6827. org-indirect-dedicated-frame)
  6828. (setq org-indirect-dedicated-frame (make-frame)))))
  6829. (delete-other-windows)
  6830. (org-pop-to-buffer-same-window ibuf)
  6831. (org-set-frame-title (concat "Indirect: " heading)))
  6832. ((eq org-indirect-buffer-display 'current-window)
  6833. (org-pop-to-buffer-same-window ibuf))
  6834. ((eq org-indirect-buffer-display 'other-window)
  6835. (pop-to-buffer ibuf))
  6836. (t (error "Invalid value")))
  6837. (narrow-to-region beg end)
  6838. (outline-show-all)
  6839. (goto-char pos)
  6840. (run-hook-with-args 'org-cycle-hook 'all)
  6841. (and (window-live-p cwin) (select-window cwin))))
  6842. (defun org-get-indirect-buffer (&optional buffer heading)
  6843. (setq buffer (or buffer (current-buffer)))
  6844. (let ((n 1) (base (buffer-name buffer)) bname)
  6845. (while (buffer-live-p
  6846. (get-buffer
  6847. (setq bname
  6848. (concat base "-"
  6849. (if heading (concat heading "-" (number-to-string n))
  6850. (number-to-string n))))))
  6851. (setq n (1+ n)))
  6852. (condition-case nil
  6853. (make-indirect-buffer buffer bname 'clone)
  6854. (error (make-indirect-buffer buffer bname)))))
  6855. (defun org-set-frame-title (title)
  6856. "Set the title of the current frame to the string TITLE."
  6857. (modify-frame-parameters (selected-frame) (list (cons 'name title))))
  6858. ;;;; Structure editing
  6859. ;;; Inserting headlines
  6860. (defun org--line-empty-p (n)
  6861. "Is the Nth next line empty?
  6862. Counts the current line as N = 1 and the previous line as N = 0;
  6863. see `beginning-of-line'."
  6864. (save-excursion
  6865. (and (not (bobp))
  6866. (or (beginning-of-line n) t)
  6867. (save-match-data
  6868. (looking-at "[ \t]*$")))))
  6869. (defun org-previous-line-empty-p ()
  6870. "Is the previous line a blank line?
  6871. When NEXT is non-nil, check the next line instead."
  6872. (org--line-empty-p 0))
  6873. (defun org-next-line-empty-p ()
  6874. "Is the previous line a blank line?
  6875. When NEXT is non-nil, check the next line instead."
  6876. (org--line-empty-p 2))
  6877. (defun org-insert-heading (&optional arg invisible-ok top)
  6878. "Insert a new heading or an item with the same depth at point.
  6879. If point is at the beginning of a heading or a list item, insert
  6880. a new heading or a new item above the current one. If point is
  6881. at the beginning of a normal line, turn the line into a heading.
  6882. If point is in the middle of a headline or a list item, split the
  6883. headline or the item and create a new headline/item with the text
  6884. in the current line after point \(see `org-M-RET-may-split-line'
  6885. on how to modify this behavior).
  6886. With one universal prefix argument, set the user option
  6887. `org-insert-heading-respect-content' to t for the duration of
  6888. the command. This modifies the behavior described above in this
  6889. ways: on list items and at the beginning of normal lines, force
  6890. the insertion of a heading after the current subtree.
  6891. With two universal prefix arguments, insert the heading at the
  6892. end of the grandparent subtree. For example, if point is within
  6893. a 2nd-level heading, then it will insert a 2nd-level heading at
  6894. the end of the 1st-level parent heading.
  6895. If point is at the beginning of a headline, insert a sibling
  6896. before the current headline. If point is not at the beginning,
  6897. split the line and create a new headline with the text in the
  6898. current line after point \(see `org-M-RET-may-split-line' on how
  6899. to modify this behavior).
  6900. If point is at the beginning of a normal line, turn this line
  6901. into a heading.
  6902. When INVISIBLE-OK is set, stop at invisible headlines when going
  6903. back. This is important for non-interactive uses of the
  6904. command.
  6905. When optional argument TOP is non-nil, insert a level 1 heading,
  6906. unconditionally."
  6907. (interactive "P")
  6908. (let ((itemp (and (not top) (org-in-item-p)))
  6909. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6910. (respect-content (or org-insert-heading-respect-content
  6911. (equal arg '(4))))
  6912. (initial-content ""))
  6913. (cond
  6914. ((or (= (buffer-size) 0)
  6915. (and (not (save-excursion
  6916. (and (ignore-errors (org-back-to-heading invisible-ok))
  6917. (org-at-heading-p))))
  6918. (or arg (not itemp))))
  6919. ;; At beginning of buffer or so high up that only a heading
  6920. ;; makes sense.
  6921. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6922. ((not respect-content)
  6923. (unless may-split (end-of-line))
  6924. (insert "\n* "))
  6925. ((re-search-forward org-outline-regexp-bol nil t)
  6926. (beginning-of-line)
  6927. (insert "* \n")
  6928. (backward-char))
  6929. (t (goto-char (point-max))
  6930. (insert "\n* ")))
  6931. (run-hooks 'org-insert-heading-hook))
  6932. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6933. (t
  6934. ;; Maybe move at the end of the subtree
  6935. (when (equal arg '(16))
  6936. (org-up-heading-safe)
  6937. (org-end-of-subtree t))
  6938. ;; Insert a heading
  6939. (save-restriction
  6940. (widen)
  6941. (let* ((level nil)
  6942. (on-heading (org-at-heading-p))
  6943. (empty-line-p (if on-heading
  6944. (org-previous-line-empty-p)
  6945. ;; We will decide later
  6946. nil))
  6947. ;; Get a level string to fall back on.
  6948. (fix-level
  6949. (if (org-before-first-heading-p) "*"
  6950. (save-excursion
  6951. (org-back-to-heading t)
  6952. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6953. (looking-at org-outline-regexp)
  6954. (make-string (1- (length (match-string 0))) ?*))))
  6955. (stars
  6956. (save-excursion
  6957. (condition-case nil
  6958. (if top "* "
  6959. (org-back-to-heading invisible-ok)
  6960. (when (and (not on-heading)
  6961. (featurep 'org-inlinetask)
  6962. (integerp org-inlinetask-min-level)
  6963. (>= (length (match-string 0))
  6964. org-inlinetask-min-level))
  6965. ;; Find a heading level before the inline
  6966. ;; task.
  6967. (while (and (setq level (org-up-heading-safe))
  6968. (>= level org-inlinetask-min-level)))
  6969. (if (org-at-heading-p)
  6970. (org-back-to-heading invisible-ok)
  6971. (error "This should not happen")))
  6972. (unless (and (save-excursion
  6973. (save-match-data
  6974. (org-backward-heading-same-level
  6975. 1 invisible-ok))
  6976. (= (point) (match-beginning 0)))
  6977. (not (org-next-line-empty-p)))
  6978. (setq empty-line-p (or empty-line-p
  6979. (org-previous-line-empty-p))))
  6980. (match-string 0))
  6981. (error (or fix-level "* ")))))
  6982. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6983. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  6984. ;; If we insert after content, move there and clean up
  6985. ;; whitespace.
  6986. (when (and respect-content
  6987. (not (looking-at-p org-outline-regexp-bol)))
  6988. (if (not (org-before-first-heading-p))
  6989. (org-end-of-subtree nil t)
  6990. (re-search-forward org-outline-regexp-bol)
  6991. (beginning-of-line 0))
  6992. (skip-chars-backward " \r\t\n")
  6993. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6994. (looking-at "[ \t]+") (replace-match ""))
  6995. (unless (eobp) (forward-char 1))
  6996. (when (looking-at "^\\*")
  6997. (unless (bobp) (backward-char 1))
  6998. (insert "\n")))
  6999. ;; If we are splitting, grab the text that should be moved
  7000. ;; to the new headline.
  7001. (when may-split
  7002. (if (org-at-heading-p)
  7003. ;; This is a heading: split intelligently (keeping
  7004. ;; tags).
  7005. (let ((pos (point)))
  7006. (beginning-of-line)
  7007. (unless (looking-at org-complex-heading-regexp)
  7008. (error "This should not happen"))
  7009. (when (and (match-beginning 4)
  7010. (> pos (match-beginning 4))
  7011. (< pos (match-end 4)))
  7012. (setq initial-content (buffer-substring pos (match-end 4)))
  7013. (goto-char pos)
  7014. (delete-region (point) (match-end 4))
  7015. (if (looking-at "[ \t]*$")
  7016. (replace-match "")
  7017. (insert (make-string (length initial-content) ?\s)))
  7018. (setq initial-content (org-trim initial-content)))
  7019. (goto-char pos))
  7020. ;; A normal line.
  7021. (setq initial-content
  7022. (org-trim
  7023. (delete-and-extract-region (point) (line-end-position))))))
  7024. ;; If we are at the beginning of the line, insert before it.
  7025. ;; Otherwise, after it.
  7026. (cond
  7027. ((and (bolp) (looking-at "[ \t]*$")))
  7028. ((bolp) (save-excursion (insert "\n")))
  7029. (t (end-of-line)
  7030. (insert "\n")))
  7031. ;; Insert the new heading
  7032. (insert stars)
  7033. (just-one-space)
  7034. (insert initial-content)
  7035. (unless (and blank (org-previous-line-empty-p))
  7036. (org-N-empty-lines-before-current (if blank 1 0)))
  7037. ;; Adjust visibility, which may be messed up if we removed
  7038. ;; blank lines while previous entry was hidden.
  7039. (let ((bol (line-beginning-position)))
  7040. (dolist (o (overlays-at (1- bol)))
  7041. (when (and (eq (overlay-get o 'invisible) 'outline)
  7042. (eq (overlay-end o) bol))
  7043. (move-overlay o (overlay-start o) (1- bol)))))
  7044. (run-hooks 'org-insert-heading-hook)))))))
  7045. (defun org-N-empty-lines-before-current (N)
  7046. "Make the number of empty lines before current exactly N.
  7047. So this will delete or add empty lines."
  7048. (save-excursion
  7049. (beginning-of-line)
  7050. (let ((p (point)))
  7051. (skip-chars-backward " \r\t\n")
  7052. (unless (bolp) (forward-line))
  7053. (delete-region (point) p))
  7054. (when (> N 0) (insert (make-string N ?\n)))))
  7055. (defun org-get-heading (&optional no-tags no-todo)
  7056. "Return the heading of the current entry, without the stars.
  7057. When NO-TAGS is non-nil, don't include tags.
  7058. When NO-TODO is non-nil, don't include TODO keywords."
  7059. (save-excursion
  7060. (org-back-to-heading t)
  7061. (let ((case-fold-search nil))
  7062. (cond
  7063. ((and no-tags no-todo)
  7064. (looking-at org-complex-heading-regexp)
  7065. (match-string 4))
  7066. (no-tags
  7067. (looking-at (concat org-outline-regexp
  7068. "\\(.*?\\)"
  7069. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7070. (match-string 1))
  7071. (no-todo
  7072. (looking-at org-todo-line-regexp)
  7073. (match-string 3))
  7074. (t (looking-at org-heading-regexp)
  7075. (match-string 2))))))
  7076. (defvar orgstruct-mode) ; defined below
  7077. (defun org-heading-components ()
  7078. "Return the components of the current heading.
  7079. This is a list with the following elements:
  7080. - the level as an integer
  7081. - the reduced level, different if `org-odd-levels-only' is set.
  7082. - the TODO keyword, or nil
  7083. - the priority character, like ?A, or nil if no priority is given
  7084. - the headline text itself, or the tags string if no headline text
  7085. - the tags string, or nil."
  7086. (save-excursion
  7087. (org-back-to-heading t)
  7088. (when (let (case-fold-search)
  7089. (looking-at
  7090. (if orgstruct-mode
  7091. org-heading-regexp
  7092. org-complex-heading-regexp)))
  7093. (if orgstruct-mode
  7094. (list (length (match-string 1))
  7095. (org-reduced-level (length (match-string 1)))
  7096. nil
  7097. nil
  7098. (match-string 2)
  7099. nil)
  7100. (list (length (match-string 1))
  7101. (org-reduced-level (length (match-string 1)))
  7102. (match-string-no-properties 2)
  7103. (and (match-end 3) (aref (match-string 3) 2))
  7104. (match-string-no-properties 4)
  7105. (match-string-no-properties 5))))))
  7106. (defun org-get-entry ()
  7107. "Get the entry text, after heading, entire subtree."
  7108. (save-excursion
  7109. (org-back-to-heading t)
  7110. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7111. (defun org-edit-headline (&optional heading)
  7112. "Edit the current headline.
  7113. Set it to HEADING when provided."
  7114. (interactive)
  7115. (org-with-wide-buffer
  7116. (org-back-to-heading t)
  7117. (when (looking-at org-complex-heading-regexp)
  7118. (let* ((old (match-string-no-properties 4))
  7119. (new (save-match-data
  7120. (org-trim (or heading (read-string "Edit: " old))))))
  7121. (unless (equal old new)
  7122. (if old (replace-match new t t nil 4)
  7123. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7124. (insert " " new))
  7125. (org-set-tags nil t)
  7126. (when (looking-at "[ \t]*$") (replace-match "")))))))
  7127. (defun org-insert-heading-after-current ()
  7128. "Insert a new heading with same level as current, after current subtree."
  7129. (interactive)
  7130. (org-back-to-heading)
  7131. (org-insert-heading)
  7132. (org-move-subtree-down)
  7133. (end-of-line 1))
  7134. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7135. "Insert heading with `org-insert-heading-respect-content' set to t."
  7136. (interactive)
  7137. (org-insert-heading '(4) invisible-ok))
  7138. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7139. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7140. (interactive)
  7141. (org-insert-todo-heading force-state '(4)))
  7142. (defun org-insert-todo-heading (arg &optional force-heading)
  7143. "Insert a new heading with the same level and TODO state as current heading.
  7144. If the heading has no TODO state, or if the state is DONE, use the first
  7145. state (TODO by default). Also with one prefix arg, force first state. With
  7146. two prefix args, force inserting at the end of the parent subtree."
  7147. (interactive "P")
  7148. (when (or force-heading (not (org-insert-item 'checkbox)))
  7149. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7150. force-heading))
  7151. (save-excursion
  7152. (org-back-to-heading)
  7153. (outline-previous-heading)
  7154. (looking-at org-todo-line-regexp))
  7155. (let*
  7156. ((new-mark-x
  7157. (if (or (equal arg '(4))
  7158. (not (match-beginning 2))
  7159. (member (match-string 2) org-done-keywords))
  7160. (car org-todo-keywords-1)
  7161. (match-string 2)))
  7162. (new-mark
  7163. (or
  7164. (run-hook-with-args-until-success
  7165. 'org-todo-get-default-hook new-mark-x nil)
  7166. new-mark-x)))
  7167. (beginning-of-line 1)
  7168. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7169. (if org-treat-insert-todo-heading-as-state-change
  7170. (org-todo new-mark)
  7171. (insert new-mark " "))))
  7172. (when org-provide-todo-statistics
  7173. (org-update-parent-todo-statistics))))
  7174. (defun org-insert-subheading (arg)
  7175. "Insert a new subheading and demote it.
  7176. Works for outline headings and for plain lists alike."
  7177. (interactive "P")
  7178. (org-insert-heading arg)
  7179. (cond
  7180. ((org-at-heading-p) (org-do-demote))
  7181. ((org-at-item-p) (org-indent-item))))
  7182. (defun org-insert-todo-subheading (arg)
  7183. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7184. Works for outline headings and for plain lists alike."
  7185. (interactive "P")
  7186. (org-insert-todo-heading arg)
  7187. (cond
  7188. ((org-at-heading-p) (org-do-demote))
  7189. ((org-at-item-p) (org-indent-item))))
  7190. ;;; Promotion and Demotion
  7191. (defvar org-after-demote-entry-hook nil
  7192. "Hook run after an entry has been demoted.
  7193. The cursor will be at the beginning of the entry.
  7194. When a subtree is being demoted, the hook will be called for each node.")
  7195. (defvar org-after-promote-entry-hook nil
  7196. "Hook run after an entry has been promoted.
  7197. The cursor will be at the beginning of the entry.
  7198. When a subtree is being promoted, the hook will be called for each node.")
  7199. (defun org-promote-subtree ()
  7200. "Promote the entire subtree.
  7201. See also `org-promote'."
  7202. (interactive)
  7203. (save-excursion
  7204. (org-with-limited-levels (org-map-tree 'org-promote)))
  7205. (org-fix-position-after-promote))
  7206. (defun org-demote-subtree ()
  7207. "Demote the entire subtree.
  7208. See `org-demote' and `org-promote'."
  7209. (interactive)
  7210. (save-excursion
  7211. (org-with-limited-levels (org-map-tree 'org-demote)))
  7212. (org-fix-position-after-promote))
  7213. (defun org-do-promote ()
  7214. "Promote the current heading higher up the tree.
  7215. If the region is active in `transient-mark-mode', promote all
  7216. headings in the region."
  7217. (interactive)
  7218. (save-excursion
  7219. (if (org-region-active-p)
  7220. (org-map-region 'org-promote (region-beginning) (region-end))
  7221. (org-promote)))
  7222. (org-fix-position-after-promote))
  7223. (defun org-do-demote ()
  7224. "Demote the current heading lower down the tree.
  7225. If the region is active in `transient-mark-mode', demote all
  7226. headings in the region."
  7227. (interactive)
  7228. (save-excursion
  7229. (if (org-region-active-p)
  7230. (org-map-region 'org-demote (region-beginning) (region-end))
  7231. (org-demote)))
  7232. (org-fix-position-after-promote))
  7233. (defun org-fix-position-after-promote ()
  7234. "Fix cursor position and indentation after demoting/promoting."
  7235. (let ((pos (point)))
  7236. (when (save-excursion
  7237. (beginning-of-line 1)
  7238. (looking-at org-todo-line-regexp)
  7239. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7240. (cond ((eobp) (insert " "))
  7241. ((eolp) (insert " "))
  7242. ((equal (char-after) ?\ ) (forward-char 1))))))
  7243. (defun org-current-level ()
  7244. "Return the level of the current entry, or nil if before the first headline.
  7245. The level is the number of stars at the beginning of the
  7246. headline. Use `org-reduced-level' to remove the effect of
  7247. `org-odd-levels'. Unlike to `org-outline-level', this function
  7248. ignores inlinetasks."
  7249. (let ((level (org-with-limited-levels (org-outline-level))))
  7250. (and (> level 0) level)))
  7251. (defun org-get-previous-line-level ()
  7252. "Return the outline depth of the last headline before the current line.
  7253. Returns 0 for the first headline in the buffer, and nil if before the
  7254. first headline."
  7255. (and (org-current-level)
  7256. (or (and (/= (line-beginning-position) (point-min))
  7257. (save-excursion (beginning-of-line 0) (org-current-level)))
  7258. 0)))
  7259. (defun org-reduced-level (l)
  7260. "Compute the effective level of a heading.
  7261. This takes into account the setting of `org-odd-levels-only'."
  7262. (cond
  7263. ((zerop l) 0)
  7264. (org-odd-levels-only (1+ (floor (/ l 2))))
  7265. (t l)))
  7266. (defun org-level-increment ()
  7267. "Return the number of stars that will be added or removed at a
  7268. time to headlines when structure editing, based on the value of
  7269. `org-odd-levels-only'."
  7270. (if org-odd-levels-only 2 1))
  7271. (defun org-get-valid-level (level &optional change)
  7272. "Rectify a level change under the influence of `org-odd-levels-only'
  7273. LEVEL is a current level, CHANGE is by how much the level should be
  7274. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7275. even level numbers will become the next higher odd number."
  7276. (if org-odd-levels-only
  7277. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7278. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7279. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7280. (max 1 (+ level (or change 0)))))
  7281. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7282. (defun org-promote ()
  7283. "Promote the current heading higher up the tree."
  7284. (org-with-wide-buffer
  7285. (org-back-to-heading t)
  7286. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7287. after-change-functions))
  7288. (level (save-match-data (funcall outline-level)))
  7289. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7290. (diff (abs (- level (length up-head) -1))))
  7291. (cond
  7292. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7293. (replace-match "# " nil t))
  7294. ((= level 1)
  7295. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7296. (t (replace-match up-head nil t)))
  7297. (unless (= level 1)
  7298. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7299. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7300. (run-hooks 'org-after-promote-entry-hook))))
  7301. (defun org-demote ()
  7302. "Demote the current heading lower down the tree."
  7303. (org-with-wide-buffer
  7304. (org-back-to-heading t)
  7305. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7306. after-change-functions))
  7307. (level (save-match-data (funcall outline-level)))
  7308. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7309. (diff (abs (- level (length down-head) -1))))
  7310. (replace-match down-head nil t)
  7311. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7312. (when org-adapt-indentation (org-fixup-indentation diff))
  7313. (run-hooks 'org-after-demote-entry-hook))))
  7314. (defun org-cycle-level ()
  7315. "Cycle the level of an empty headline through possible states.
  7316. This goes first to child, then to parent, level, then up the hierarchy.
  7317. After top level, it switches back to sibling level."
  7318. (interactive)
  7319. (let ((org-adapt-indentation nil))
  7320. (when (org-point-at-end-of-empty-headline)
  7321. (setq this-command 'org-cycle-level) ; Only needed for caching
  7322. (let ((cur-level (org-current-level))
  7323. (prev-level (org-get-previous-line-level)))
  7324. (cond
  7325. ;; If first headline in file, promote to top-level.
  7326. ((= prev-level 0)
  7327. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7328. do (org-do-promote)))
  7329. ;; If same level as prev, demote one.
  7330. ((= prev-level cur-level)
  7331. (org-do-demote))
  7332. ;; If parent is top-level, promote to top level if not already.
  7333. ((= prev-level 1)
  7334. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7335. do (org-do-promote)))
  7336. ;; If top-level, return to prev-level.
  7337. ((= cur-level 1)
  7338. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7339. do (org-do-demote)))
  7340. ;; If less than prev-level, promote one.
  7341. ((< cur-level prev-level)
  7342. (org-do-promote))
  7343. ;; If deeper than prev-level, promote until higher than
  7344. ;; prev-level.
  7345. ((> cur-level prev-level)
  7346. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7347. do (org-do-promote))))
  7348. t))))
  7349. (defun org-map-tree (fun)
  7350. "Call FUN for every heading underneath the current one."
  7351. (org-back-to-heading)
  7352. (let ((level (funcall outline-level)))
  7353. (save-excursion
  7354. (funcall fun)
  7355. (while (and (progn
  7356. (outline-next-heading)
  7357. (> (funcall outline-level) level))
  7358. (not (eobp)))
  7359. (funcall fun)))))
  7360. (defun org-map-region (fun beg end)
  7361. "Call FUN for every heading between BEG and END."
  7362. (let ((org-ignore-region t))
  7363. (save-excursion
  7364. (setq end (copy-marker end))
  7365. (goto-char beg)
  7366. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7367. (< (point) end))
  7368. (funcall fun))
  7369. (while (and (progn
  7370. (outline-next-heading)
  7371. (< (point) end))
  7372. (not (eobp)))
  7373. (funcall fun)))))
  7374. (defun org-fixup-indentation (diff)
  7375. "Change the indentation in the current entry by DIFF.
  7376. DIFF is an integer. Indentation is done according to the
  7377. following rules:
  7378. - Planning information and property drawers are always indented
  7379. according to the new level of the headline;
  7380. - Footnote definitions and their contents are ignored;
  7381. - Inlinetasks' boundaries are not shifted;
  7382. - Empty lines are ignored;
  7383. - Other lines' indentation are shifted by DIFF columns, unless
  7384. it would introduce a structural change in the document, in
  7385. which case no shifting is done at all.
  7386. Assume point is at a heading or an inlinetask beginning."
  7387. (org-with-wide-buffer
  7388. (narrow-to-region (line-beginning-position)
  7389. (save-excursion
  7390. (if (org-with-limited-levels (org-at-heading-p))
  7391. (org-with-limited-levels (outline-next-heading))
  7392. (org-inlinetask-goto-end))
  7393. (point)))
  7394. (forward-line)
  7395. ;; Indent properly planning info and property drawer.
  7396. (when (looking-at-p org-planning-line-re)
  7397. (org-indent-line)
  7398. (forward-line))
  7399. (when (looking-at org-property-drawer-re)
  7400. (goto-char (match-end 0))
  7401. (forward-line)
  7402. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7403. (catch 'no-shift
  7404. (when (zerop diff) (throw 'no-shift nil))
  7405. ;; If DIFF is negative, first check if a shift is possible at all
  7406. ;; (e.g., it doesn't break structure). This can only happen if
  7407. ;; some contents are not properly indented.
  7408. (let ((case-fold-search t))
  7409. (when (< diff 0)
  7410. (let ((diff (- diff))
  7411. (forbidden-re (concat org-outline-regexp
  7412. "\\|"
  7413. (substring org-footnote-definition-re 1))))
  7414. (save-excursion
  7415. (while (not (eobp))
  7416. (cond
  7417. ((looking-at-p "[ \t]*$") (forward-line))
  7418. ((and (looking-at-p org-footnote-definition-re)
  7419. (let ((e (org-element-at-point)))
  7420. (and (eq (org-element-type e) 'footnote-definition)
  7421. (goto-char (org-element-property :end e))))))
  7422. ((looking-at-p org-outline-regexp) (forward-line))
  7423. ;; Give up if shifting would move before column 0 or
  7424. ;; if it would introduce a headline or a footnote
  7425. ;; definition.
  7426. (t
  7427. (skip-chars-forward " \t")
  7428. (let ((ind (current-column)))
  7429. (when (or (< ind diff)
  7430. (and (= ind diff) (looking-at-p forbidden-re)))
  7431. (throw 'no-shift nil)))
  7432. ;; Ignore contents of example blocks and source
  7433. ;; blocks if their indentation is meant to be
  7434. ;; preserved. Jump to block's closing line.
  7435. (beginning-of-line)
  7436. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7437. (let ((e (org-element-at-point)))
  7438. (and (memq (org-element-type e)
  7439. '(example-block src-block))
  7440. (or org-src-preserve-indentation
  7441. (org-element-property :preserve-indent e))
  7442. (goto-char (org-element-property :end e))
  7443. (progn (skip-chars-backward " \r\t\n")
  7444. (beginning-of-line)
  7445. t))))
  7446. (forward-line))))))))
  7447. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7448. ;; by DIFF. Also skip contents of source or example blocks
  7449. ;; when indentation is meant to be preserved.
  7450. (while (not (eobp))
  7451. (cond
  7452. ((and (looking-at-p org-footnote-definition-re)
  7453. (let ((e (org-element-at-point)))
  7454. (and (eq (org-element-type e) 'footnote-definition)
  7455. (goto-char (org-element-property :end e))))))
  7456. ((looking-at-p org-outline-regexp) (forward-line))
  7457. ((looking-at-p "[ \t]*$") (forward-line))
  7458. (t
  7459. (indent-line-to (+ (org-get-indentation) diff))
  7460. (beginning-of-line)
  7461. (or (and (looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7462. (let ((e (org-element-at-point)))
  7463. (and (memq (org-element-type e)
  7464. '(example-block src-block))
  7465. (or org-src-preserve-indentation
  7466. (org-element-property :preserve-indent e))
  7467. (goto-char (org-element-property :end e))
  7468. (progn (skip-chars-backward " \r\t\n")
  7469. (beginning-of-line)
  7470. t))))
  7471. (forward-line)))))))))
  7472. (defun org-convert-to-odd-levels ()
  7473. "Convert an org-mode file with all levels allowed to one with odd levels.
  7474. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7475. level 5 etc."
  7476. (interactive)
  7477. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7478. (let ((outline-level 'org-outline-level)
  7479. (org-odd-levels-only nil) n)
  7480. (save-excursion
  7481. (goto-char (point-min))
  7482. (while (re-search-forward "^\\*\\*+ " nil t)
  7483. (setq n (- (length (match-string 0)) 2))
  7484. (while (>= (setq n (1- n)) 0)
  7485. (org-demote))
  7486. (end-of-line 1))))))
  7487. (defun org-convert-to-oddeven-levels ()
  7488. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7489. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7490. file contains a section with an even level, conversion would
  7491. destroy the structure of the file. An error is signaled in this
  7492. case."
  7493. (interactive)
  7494. (goto-char (point-min))
  7495. ;; First check if there are no even levels
  7496. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7497. (org-show-set-visibility 'canonical)
  7498. (error "Not all levels are odd in this file. Conversion not possible"))
  7499. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7500. (let ((outline-regexp org-outline-regexp)
  7501. (outline-level 'org-outline-level)
  7502. (org-odd-levels-only nil) n)
  7503. (save-excursion
  7504. (goto-char (point-min))
  7505. (while (re-search-forward "^\\*\\*+ " nil t)
  7506. (setq n (/ (1- (length (match-string 0))) 2))
  7507. (while (>= (setq n (1- n)) 0)
  7508. (org-promote))
  7509. (end-of-line 1))))))
  7510. (defun org-tr-level (n)
  7511. "Make N odd if required."
  7512. (if org-odd-levels-only (1+ (/ n 2)) n))
  7513. ;;; Vertical tree motion, cutting and pasting of subtrees
  7514. (defun org-move-subtree-up (&optional arg)
  7515. "Move the current subtree up past ARG headlines of the same level."
  7516. (interactive "p")
  7517. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7518. (defun org-move-subtree-down (&optional arg)
  7519. "Move the current subtree down past ARG headlines of the same level."
  7520. (interactive "p")
  7521. (setq arg (prefix-numeric-value arg))
  7522. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7523. 'org-get-last-sibling))
  7524. (ins-point (make-marker))
  7525. (cnt (abs arg))
  7526. (col (current-column))
  7527. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7528. ;; Select the tree
  7529. (org-back-to-heading)
  7530. (setq beg0 (point))
  7531. (save-excursion
  7532. (setq ne-beg (org-back-over-empty-lines))
  7533. (setq beg (point)))
  7534. (save-match-data
  7535. (save-excursion (outline-end-of-heading)
  7536. (setq folded (outline-invisible-p)))
  7537. (progn (org-end-of-subtree nil t)
  7538. (unless (eobp) (backward-char))))
  7539. (outline-next-heading)
  7540. (setq ne-end (org-back-over-empty-lines))
  7541. (setq end (point))
  7542. (goto-char beg0)
  7543. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7544. ;; include less whitespace
  7545. (save-excursion
  7546. (goto-char beg)
  7547. (forward-line (- ne-beg ne-end))
  7548. (setq beg (point))))
  7549. ;; Find insertion point, with error handling
  7550. (while (> cnt 0)
  7551. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7552. (progn (goto-char beg0)
  7553. (user-error "Cannot move past superior level or buffer limit")))
  7554. (setq cnt (1- cnt)))
  7555. (when (> arg 0)
  7556. ;; Moving forward - still need to move over subtree
  7557. (org-end-of-subtree t t)
  7558. (save-excursion
  7559. (org-back-over-empty-lines)
  7560. (or (bolp) (newline))))
  7561. (setq ne-ins (org-back-over-empty-lines))
  7562. (move-marker ins-point (point))
  7563. (setq txt (buffer-substring beg end))
  7564. (org-save-markers-in-region beg end)
  7565. (delete-region beg end)
  7566. (org-remove-empty-overlays-at beg)
  7567. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7568. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7569. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7570. (let ((bbb (point)))
  7571. (insert-before-markers txt)
  7572. (org-reinstall-markers-in-region bbb)
  7573. (move-marker ins-point bbb))
  7574. (or (bolp) (insert "\n"))
  7575. (setq ins-end (point))
  7576. (goto-char ins-point)
  7577. (org-skip-whitespace)
  7578. (when (and (< arg 0)
  7579. (org-first-sibling-p)
  7580. (> ne-ins ne-beg))
  7581. ;; Move whitespace back to beginning
  7582. (save-excursion
  7583. (goto-char ins-end)
  7584. (let ((kill-whole-line t))
  7585. (kill-line (- ne-ins ne-beg)) (point)))
  7586. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7587. (move-marker ins-point nil)
  7588. (if folded
  7589. (outline-hide-subtree)
  7590. (org-show-entry)
  7591. (org-show-children)
  7592. (org-cycle-hide-drawers 'children))
  7593. (org-clean-visibility-after-subtree-move)
  7594. ;; move back to the initial column we were at
  7595. (move-to-column col)))
  7596. (defvar org-subtree-clip ""
  7597. "Clipboard for cut and paste of subtrees.
  7598. This is actually only a copy of the kill, because we use the normal kill
  7599. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7600. (defvar org-subtree-clip-folded nil
  7601. "Was the last copied subtree folded?
  7602. This is used to fold the tree back after pasting.")
  7603. (defun org-cut-subtree (&optional n)
  7604. "Cut the current subtree into the clipboard.
  7605. With prefix arg N, cut this many sequential subtrees.
  7606. This is a short-hand for marking the subtree and then cutting it."
  7607. (interactive "p")
  7608. (org-copy-subtree n 'cut))
  7609. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7610. "Copy the current subtree it in the clipboard.
  7611. With prefix arg N, copy this many sequential subtrees.
  7612. This is a short-hand for marking the subtree and then copying it.
  7613. If CUT is non-nil, actually cut the subtree.
  7614. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7615. of some markers in the region, even if CUT is non-nil. This is
  7616. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7617. (interactive "p")
  7618. (let (beg end folded (beg0 (point)))
  7619. (if (org-called-interactively-p 'any)
  7620. (org-back-to-heading nil) ; take what looks like a subtree
  7621. (org-back-to-heading t)) ; take what is really there
  7622. (setq beg (point))
  7623. (skip-chars-forward " \t\r\n")
  7624. (save-match-data
  7625. (if nosubtrees
  7626. (outline-next-heading)
  7627. (save-excursion (outline-end-of-heading)
  7628. (setq folded (outline-invisible-p)))
  7629. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7630. (org-end-of-subtree t t)))
  7631. ;; Include the end of an inlinetask
  7632. (when (and (featurep 'org-inlinetask)
  7633. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7634. "END[ \t]*$")))
  7635. (end-of-line))
  7636. (setq end (point))
  7637. (goto-char beg0)
  7638. (when (> end beg)
  7639. (setq org-subtree-clip-folded folded)
  7640. (when (or cut force-store-markers)
  7641. (org-save-markers-in-region beg end))
  7642. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7643. (setq org-subtree-clip (current-kill 0))
  7644. (message "%s: Subtree(s) with %d characters"
  7645. (if cut "Cut" "Copied")
  7646. (length org-subtree-clip)))))
  7647. (defun org-paste-subtree (&optional level tree for-yank remove)
  7648. "Paste the clipboard as a subtree, with modification of headline level.
  7649. The entire subtree is promoted or demoted in order to match a new headline
  7650. level.
  7651. If the cursor is at the beginning of a headline, the same level as
  7652. that headline is used to paste the tree.
  7653. If not, the new level is derived from the *visible* headings
  7654. before and after the insertion point, and taken to be the inferior headline
  7655. level of the two. So if the previous visible heading is level 3 and the
  7656. next is level 4 (or vice versa), level 4 will be used for insertion.
  7657. This makes sure that the subtree remains an independent subtree and does
  7658. not swallow low level entries.
  7659. You can also force a different level, either by using a numeric prefix
  7660. argument, or by inserting the heading marker by hand. For example, if the
  7661. cursor is after \"*****\", then the tree will be shifted to level 5.
  7662. If optional TREE is given, use this text instead of the kill ring.
  7663. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7664. move back over whitespace before inserting, and move point to the end of
  7665. the inserted text when done.
  7666. When REMOVE is non-nil, remove the subtree from the clipboard."
  7667. (interactive "P")
  7668. (setq tree (or tree (and kill-ring (current-kill 0))))
  7669. (unless (org-kill-is-subtree-p tree)
  7670. (user-error "%s"
  7671. (substitute-command-keys
  7672. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7673. (org-with-limited-levels
  7674. (let* ((visp (not (outline-invisible-p)))
  7675. (txt tree)
  7676. (^re_ "\\(\\*+\\)[ \t]*")
  7677. (old-level (if (string-match org-outline-regexp-bol txt)
  7678. (- (match-end 0) (match-beginning 0) 1)
  7679. -1))
  7680. (force-level (cond (level (prefix-numeric-value level))
  7681. ((and (looking-at "[ \t]*$")
  7682. (string-match
  7683. "^\\*+$" (buffer-substring
  7684. (point-at-bol) (point))))
  7685. (- (match-end 0) (match-beginning 0)))
  7686. ((and (bolp)
  7687. (looking-at org-outline-regexp))
  7688. (- (match-end 0) (point) 1))))
  7689. (previous-level (save-excursion
  7690. (condition-case nil
  7691. (progn
  7692. (outline-previous-visible-heading 1)
  7693. (if (looking-at ^re_)
  7694. (- (match-end 0) (match-beginning 0) 1)
  7695. 1))
  7696. (error 1))))
  7697. (next-level (save-excursion
  7698. (condition-case nil
  7699. (progn
  7700. (or (looking-at org-outline-regexp)
  7701. (outline-next-visible-heading 1))
  7702. (if (looking-at ^re_)
  7703. (- (match-end 0) (match-beginning 0) 1)
  7704. 1))
  7705. (error 1))))
  7706. (new-level (or force-level (max previous-level next-level)))
  7707. (shift (if (or (= old-level -1)
  7708. (= new-level -1)
  7709. (= old-level new-level))
  7710. 0
  7711. (- new-level old-level)))
  7712. (delta (if (> shift 0) -1 1))
  7713. (func (if (> shift 0) 'org-demote 'org-promote))
  7714. (org-odd-levels-only nil)
  7715. beg end newend)
  7716. ;; Remove the forced level indicator
  7717. (when force-level
  7718. (delete-region (point-at-bol) (point)))
  7719. ;; Paste
  7720. (beginning-of-line (if (bolp) 1 2))
  7721. (setq beg (point))
  7722. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7723. (insert-before-markers txt)
  7724. (unless (string-match "\n\\'" txt) (insert "\n"))
  7725. (setq newend (point))
  7726. (org-reinstall-markers-in-region beg)
  7727. (setq end (point))
  7728. (goto-char beg)
  7729. (skip-chars-forward " \t\n\r")
  7730. (setq beg (point))
  7731. (when (and (outline-invisible-p) visp)
  7732. (save-excursion (outline-show-heading)))
  7733. ;; Shift if necessary
  7734. (unless (= shift 0)
  7735. (save-restriction
  7736. (narrow-to-region beg end)
  7737. (while (not (= shift 0))
  7738. (org-map-region func (point-min) (point-max))
  7739. (setq shift (+ delta shift)))
  7740. (goto-char (point-min))
  7741. (setq newend (point-max))))
  7742. (when (or (org-called-interactively-p 'interactive) for-yank)
  7743. (message "Clipboard pasted as level %d subtree" new-level))
  7744. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7745. kill-ring
  7746. (eq org-subtree-clip (current-kill 0))
  7747. org-subtree-clip-folded)
  7748. ;; The tree was folded before it was killed/copied
  7749. (outline-hide-subtree))
  7750. (and for-yank (goto-char newend))
  7751. (and remove (setq kill-ring (cdr kill-ring))))))
  7752. (defun org-kill-is-subtree-p (&optional txt)
  7753. "Check if the current kill is an outline subtree, or a set of trees.
  7754. Returns nil if kill does not start with a headline, or if the first
  7755. headline level is not the largest headline level in the tree.
  7756. So this will actually accept several entries of equal levels as well,
  7757. which is OK for `org-paste-subtree'.
  7758. If optional TXT is given, check this string instead of the current kill."
  7759. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7760. (re (org-get-limited-outline-regexp))
  7761. (^re (concat "^" re))
  7762. (start-level (and kill
  7763. (string-match
  7764. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7765. kill)
  7766. (- (match-end 2) (match-beginning 2) 1)))
  7767. (start (1+ (or (match-beginning 2) -1))))
  7768. (if (not start-level)
  7769. (progn
  7770. nil) ;; does not even start with a heading
  7771. (catch 'exit
  7772. (while (setq start (string-match ^re kill (1+ start)))
  7773. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7774. (throw 'exit nil)))
  7775. t))))
  7776. (defvar org-markers-to-move nil
  7777. "Markers that should be moved with a cut-and-paste operation.
  7778. Those markers are stored together with their positions relative to
  7779. the start of the region.")
  7780. (defun org-save-markers-in-region (beg end)
  7781. "Check markers in region.
  7782. If these markers are between BEG and END, record their position relative
  7783. to BEG, so that after moving the block of text, we can put the markers back
  7784. into place.
  7785. This function gets called just before an entry or tree gets cut from the
  7786. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7787. called immediately, to move the markers with the entries."
  7788. (setq org-markers-to-move nil)
  7789. (when (featurep 'org-clock)
  7790. (org-clock-save-markers-for-cut-and-paste beg end))
  7791. (when (featurep 'org-agenda)
  7792. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7793. (defun org-check-and-save-marker (marker beg end)
  7794. "Check if MARKER is between BEG and END.
  7795. If yes, remember the marker and the distance to BEG."
  7796. (when (and (marker-buffer marker)
  7797. (equal (marker-buffer marker) (current-buffer))
  7798. (>= marker beg) (< marker end))
  7799. (push (cons marker (- marker beg)) org-markers-to-move)))
  7800. (defun org-reinstall-markers-in-region (beg)
  7801. "Move all remembered markers to their position relative to BEG."
  7802. (dolist (x org-markers-to-move)
  7803. (move-marker (car x) (+ beg (cdr x))))
  7804. (setq org-markers-to-move nil))
  7805. (defun org-narrow-to-subtree ()
  7806. "Narrow buffer to the current subtree."
  7807. (interactive)
  7808. (save-excursion
  7809. (save-match-data
  7810. (org-with-limited-levels
  7811. (narrow-to-region
  7812. (progn (org-back-to-heading t) (point))
  7813. (progn (org-end-of-subtree t t)
  7814. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7815. (point)))))))
  7816. (defun org-narrow-to-block ()
  7817. "Narrow buffer to the current block."
  7818. (interactive)
  7819. (let* ((case-fold-search t)
  7820. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7821. "^[ \t]*#\\+end_.*")))
  7822. (if blockp
  7823. (narrow-to-region (car blockp) (cdr blockp))
  7824. (user-error "Not in a block"))))
  7825. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7826. "Clone the task (subtree) at point N times.
  7827. The clones will be inserted as siblings.
  7828. In interactive use, the user will be prompted for the number of
  7829. clones to be produced. If the entry has a timestamp, the user
  7830. will also be prompted for a time shift, which may be a repeater
  7831. as used in time stamps, for example `+3d'. To disable this,
  7832. you can call the function with a universal prefix argument.
  7833. When a valid repeater is given and the entry contains any time
  7834. stamps, the clones will become a sequence in time, with time
  7835. stamps in the subtree shifted for each clone produced. If SHIFT
  7836. is nil or the empty string, time stamps will be left alone. The
  7837. ID property of the original subtree is removed.
  7838. If the original subtree did contain time stamps with a repeater,
  7839. the following will happen:
  7840. - the repeater will be removed in each clone
  7841. - an additional clone will be produced, with the current, unshifted
  7842. date(s) in the entry.
  7843. - the original entry will be placed *after* all the clones, with
  7844. repeater intact.
  7845. - the start days in the repeater in the original entry will be shifted
  7846. to past the last clone.
  7847. In this way you can spell out a number of instances of a repeating task,
  7848. and still retain the repeater to cover future instances of the task.
  7849. As described above, N+1 clones are produced when the original
  7850. subtree has a repeater. Setting N to 0, then, can be used to
  7851. remove the repeater from a subtree and create a shifted clone
  7852. with the original repeater."
  7853. (interactive "nNumber of clones to produce: ")
  7854. (let ((shift
  7855. (or shift
  7856. (if (and (not (equal current-prefix-arg '(4)))
  7857. (save-excursion
  7858. (re-search-forward org-ts-regexp-both
  7859. (save-excursion
  7860. (org-end-of-subtree t)
  7861. (point)) t)))
  7862. (read-from-minibuffer
  7863. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7864. ""))) ;; No time shift
  7865. (n-no-remove -1)
  7866. (drawer-re org-drawer-regexp)
  7867. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7868. beg end template task idprop
  7869. shift-n shift-what doshift nmin nmax)
  7870. (unless (wholenump n)
  7871. (user-error "Invalid number of replications %s" n))
  7872. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7873. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7874. shift)))
  7875. (user-error "Invalid shift specification %s" shift))
  7876. (when doshift
  7877. (setq shift-n (string-to-number (match-string 1 shift))
  7878. shift-what (cdr (assoc (match-string 2 shift)
  7879. '(("d" . day) ("w" . week)
  7880. ("m" . month) ("y" . year))))))
  7881. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7882. (setq nmin 1 nmax n)
  7883. (org-back-to-heading t)
  7884. (setq beg (point))
  7885. (setq idprop (org-entry-get nil "ID"))
  7886. (org-end-of-subtree t t)
  7887. (or (bolp) (insert "\n"))
  7888. (setq end (point))
  7889. (setq template (buffer-substring beg end))
  7890. (when (and doshift
  7891. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7892. (delete-region beg end)
  7893. (setq end beg)
  7894. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7895. (goto-char end)
  7896. (cl-loop for n from nmin to nmax do
  7897. ;; prepare clone
  7898. (with-temp-buffer
  7899. (insert template)
  7900. (org-mode)
  7901. (goto-char (point-min))
  7902. (org-show-subtree)
  7903. (and idprop (if org-clone-delete-id
  7904. (org-entry-delete nil "ID")
  7905. (org-id-get-create t)))
  7906. (unless (= n 0)
  7907. (while (re-search-forward org-clock-re nil t)
  7908. (kill-whole-line))
  7909. (goto-char (point-min))
  7910. (while (re-search-forward drawer-re nil t)
  7911. (org-remove-empty-drawer-at (point))))
  7912. (goto-char (point-min))
  7913. (when doshift
  7914. (while (re-search-forward org-ts-regexp-both nil t)
  7915. (org-timestamp-change (* n shift-n) shift-what))
  7916. (unless (= n n-no-remove)
  7917. (goto-char (point-min))
  7918. (while (re-search-forward org-ts-regexp nil t)
  7919. (save-excursion
  7920. (goto-char (match-beginning 0))
  7921. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7922. (delete-region (match-beginning 1) (match-end 1)))))))
  7923. (setq task (buffer-string)))
  7924. (insert task))
  7925. (goto-char beg)))
  7926. ;;; Outline Sorting
  7927. (defun org-sort (with-case)
  7928. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7929. Optional argument WITH-CASE means sort case-sensitively."
  7930. (interactive "P")
  7931. (cond
  7932. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7933. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7934. (t
  7935. (org-call-with-arg 'org-sort-entries with-case))))
  7936. (defun org-sort-remove-invisible (s)
  7937. "Remove invisible links from string S."
  7938. (remove-text-properties 0 (length s) org-rm-props s)
  7939. (while (string-match org-bracket-link-regexp s)
  7940. (setq s (replace-match (if (match-end 2)
  7941. (match-string 3 s)
  7942. (match-string 1 s))
  7943. t t s)))
  7944. (let ((st (format " %s " s)))
  7945. (while (string-match org-emph-re st)
  7946. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7947. (setq s (substring st 1 -1)))
  7948. s)
  7949. (defvar org-priority-regexp) ; defined later in the file
  7950. (defvar org-after-sorting-entries-or-items-hook nil
  7951. "Hook that is run after a bunch of entries or items have been sorted.
  7952. When children are sorted, the cursor is in the parent line when this
  7953. hook gets called. When a region or a plain list is sorted, the cursor
  7954. will be in the first entry of the sorted region/list.")
  7955. (defun org-sort-entries
  7956. (&optional with-case sorting-type getkey-func compare-func property)
  7957. "Sort entries on a certain level of an outline tree.
  7958. If there is an active region, the entries in the region are sorted.
  7959. Else, if the cursor is before the first entry, sort the top-level items.
  7960. Else, the children of the entry at point are sorted.
  7961. Sorting can be alphabetically, numerically, by date/time as given by
  7962. a time stamp, by a property, by priority order, or by a custom function.
  7963. The command prompts for the sorting type unless it has been given to the
  7964. function through the SORTING-TYPE argument, which needs to be a character,
  7965. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7966. the precise meaning of each character:
  7967. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7968. c By creation time, which is assumed to be the first inactive time stamp
  7969. at the beginning of a line.
  7970. d By deadline date/time.
  7971. k By clocking time.
  7972. n Numerically, by converting the beginning of the entry/item to a number.
  7973. o By order of TODO keywords.
  7974. p By priority according to the cookie.
  7975. r By the value of a property.
  7976. s By scheduled date/time.
  7977. t By date/time, either the first active time stamp in the entry, or, if
  7978. none exist, by the first inactive one.
  7979. Capital letters will reverse the sort order.
  7980. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7981. called with point at the beginning of the record. It must return either
  7982. a string or a number that should serve as the sorting key for that record.
  7983. Comparing entries ignores case by default. However, with an optional argument
  7984. WITH-CASE, the sorting considers case as well.
  7985. Sorting is done against the visible part of the headlines, it ignores hidden
  7986. links.
  7987. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7988. (interactive "P")
  7989. (let ((case-func (if with-case 'identity 'downcase))
  7990. (cmstr
  7991. ;; The clock marker is lost when using `sort-subr', let's
  7992. ;; store the clocking string.
  7993. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7994. (save-excursion
  7995. (goto-char org-clock-marker)
  7996. (buffer-substring-no-properties (line-beginning-position)
  7997. (point)))))
  7998. start beg end stars re re2
  7999. txt what tmp)
  8000. ;; Find beginning and end of region to sort
  8001. (cond
  8002. ((org-region-active-p)
  8003. ;; we will sort the region
  8004. (setq end (region-end)
  8005. what "region")
  8006. (goto-char (region-beginning))
  8007. (unless (org-at-heading-p) (outline-next-heading))
  8008. (setq start (point)))
  8009. ((or (org-at-heading-p)
  8010. (ignore-errors (progn (org-back-to-heading) t)))
  8011. ;; we will sort the children of the current headline
  8012. (org-back-to-heading)
  8013. (setq start (point)
  8014. end (progn (org-end-of-subtree t t)
  8015. (or (bolp) (insert "\n"))
  8016. (when (>= (org-back-over-empty-lines) 1)
  8017. (forward-line 1))
  8018. (point))
  8019. what "children")
  8020. (goto-char start)
  8021. (outline-show-subtree)
  8022. (outline-next-heading))
  8023. (t
  8024. ;; we will sort the top-level entries in this file
  8025. (goto-char (point-min))
  8026. (or (org-at-heading-p) (outline-next-heading))
  8027. (setq start (point))
  8028. (goto-char (point-max))
  8029. (beginning-of-line 1)
  8030. (when (looking-at ".*?\\S-")
  8031. ;; File ends in a non-white line
  8032. (end-of-line 1)
  8033. (insert "\n"))
  8034. (setq end (point-max))
  8035. (setq what "top-level")
  8036. (goto-char start)
  8037. (outline-show-all)))
  8038. (setq beg (point))
  8039. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  8040. (looking-at "\\(\\*+\\)")
  8041. (setq stars (match-string 1)
  8042. re (concat "^" (regexp-quote stars) " +")
  8043. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8044. txt (buffer-substring beg end))
  8045. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  8046. (when (and (not (equal stars "*")) (string-match re2 txt))
  8047. (user-error "Region to sort contains a level above the first entry"))
  8048. (unless sorting-type
  8049. (message
  8050. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8051. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8052. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8053. what)
  8054. (setq sorting-type (read-char-exclusive))
  8055. (unless getkey-func
  8056. (and (= (downcase sorting-type) ?f)
  8057. (setq getkey-func
  8058. (completing-read "Sort using function: "
  8059. obarray 'fboundp t nil nil))
  8060. (setq getkey-func (intern getkey-func))))
  8061. (and (= (downcase sorting-type) ?r)
  8062. (not property)
  8063. (setq property
  8064. (completing-read "Property: "
  8065. (mapcar #'list (org-buffer-property-keys t))
  8066. nil t))))
  8067. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8068. (message "Sorting entries...")
  8069. (save-restriction
  8070. (narrow-to-region start end)
  8071. (let ((dcst (downcase sorting-type))
  8072. (case-fold-search nil)
  8073. (now (current-time)))
  8074. (sort-subr
  8075. (/= dcst sorting-type)
  8076. ;; This function moves to the beginning character of the "record" to
  8077. ;; be sorted.
  8078. (lambda nil
  8079. (if (re-search-forward re nil t)
  8080. (goto-char (match-beginning 0))
  8081. (goto-char (point-max))))
  8082. ;; This function moves to the last character of the "record" being
  8083. ;; sorted.
  8084. (lambda nil
  8085. (save-match-data
  8086. (condition-case nil
  8087. (outline-forward-same-level 1)
  8088. (error
  8089. (goto-char (point-max))))))
  8090. ;; This function returns the value that gets sorted against.
  8091. (lambda nil
  8092. (cond
  8093. ((= dcst ?n)
  8094. (if (looking-at org-complex-heading-regexp)
  8095. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8096. nil))
  8097. ((= dcst ?a)
  8098. (if (looking-at org-complex-heading-regexp)
  8099. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8100. nil))
  8101. ((= dcst ?k)
  8102. (or (get-text-property (point) :org-clock-minutes) 0))
  8103. ((= dcst ?t)
  8104. (let ((end (save-excursion (outline-next-heading) (point))))
  8105. (if (or (re-search-forward org-ts-regexp end t)
  8106. (re-search-forward org-ts-regexp-both end t))
  8107. (org-time-string-to-seconds (match-string 0))
  8108. (float-time now))))
  8109. ((= dcst ?c)
  8110. (let ((end (save-excursion (outline-next-heading) (point))))
  8111. (if (re-search-forward
  8112. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8113. end t)
  8114. (org-time-string-to-seconds (match-string 0))
  8115. (float-time now))))
  8116. ((= dcst ?s)
  8117. (let ((end (save-excursion (outline-next-heading) (point))))
  8118. (if (re-search-forward org-scheduled-time-regexp end t)
  8119. (org-time-string-to-seconds (match-string 1))
  8120. (float-time now))))
  8121. ((= dcst ?d)
  8122. (let ((end (save-excursion (outline-next-heading) (point))))
  8123. (if (re-search-forward org-deadline-time-regexp end t)
  8124. (org-time-string-to-seconds (match-string 1))
  8125. (float-time now))))
  8126. ((= dcst ?p)
  8127. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8128. (string-to-char (match-string 2))
  8129. org-default-priority))
  8130. ((= dcst ?r)
  8131. (or (org-entry-get nil property) ""))
  8132. ((= dcst ?o)
  8133. (when (looking-at org-complex-heading-regexp)
  8134. (let* ((m (match-string 2))
  8135. (s (if (member m org-done-keywords) '- '+)))
  8136. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8137. ((= dcst ?f)
  8138. (if getkey-func
  8139. (progn
  8140. (setq tmp (funcall getkey-func))
  8141. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8142. tmp)
  8143. (error "Invalid key function `%s'" getkey-func)))
  8144. (t (error "Invalid sorting type `%c'" sorting-type))))
  8145. nil
  8146. (cond
  8147. ((= dcst ?a) 'string<)
  8148. ((= dcst ?f) compare-func)
  8149. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8150. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8151. ;; Reset the clock marker if needed
  8152. (when cmstr
  8153. (save-excursion
  8154. (goto-char start)
  8155. (search-forward cmstr nil t)
  8156. (move-marker org-clock-marker (point))))
  8157. (message "Sorting entries...done")))
  8158. ;;; The orgstruct minor mode
  8159. ;; Define a minor mode which can be used in other modes in order to
  8160. ;; integrate the org-mode structure editing commands.
  8161. ;; This is really a hack, because the org-mode structure commands use
  8162. ;; keys which normally belong to the major mode. Here is how it
  8163. ;; works: The minor mode defines all the keys necessary to operate the
  8164. ;; structure commands, but wraps the commands into a function which
  8165. ;; tests if the cursor is currently at a headline or a plain list
  8166. ;; item. If that is the case, the structure command is used,
  8167. ;; temporarily setting many Org-mode variables like regular
  8168. ;; expressions for filling etc. However, when any of those keys is
  8169. ;; used at a different location, function uses `key-binding' to look
  8170. ;; up if the key has an associated command in another currently active
  8171. ;; keymap (minor modes, major mode, global), and executes that
  8172. ;; command. There might be problems if any of the keys is otherwise
  8173. ;; used as a prefix key.
  8174. (defcustom orgstruct-heading-prefix-regexp ""
  8175. "Regexp that matches the custom prefix of Org headlines in
  8176. orgstruct(++)-mode."
  8177. :group 'org
  8178. :version "24.4"
  8179. :package-version '(Org . "8.3")
  8180. :type 'regexp)
  8181. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8182. (defcustom orgstruct-setup-hook nil
  8183. "Hook run after orgstruct-mode-map is filled."
  8184. :group 'org
  8185. :version "24.4"
  8186. :package-version '(Org . "8.0")
  8187. :type 'hook)
  8188. (defvar orgstruct-initialized nil)
  8189. (defvar org-local-vars nil
  8190. "List of local variables, for use by `orgstruct-mode'.")
  8191. ;;;###autoload
  8192. (define-minor-mode orgstruct-mode
  8193. "Toggle the minor mode `orgstruct-mode'.
  8194. This mode is for using Org-mode structure commands in other
  8195. modes. The following keys behave as if Org-mode were active, if
  8196. the cursor is on a headline, or on a plain list item (both as
  8197. defined by Org-mode)."
  8198. nil " OrgStruct" (make-sparse-keymap)
  8199. (funcall (if orgstruct-mode
  8200. 'add-to-invisibility-spec
  8201. 'remove-from-invisibility-spec)
  8202. '(outline . t))
  8203. (when orgstruct-mode
  8204. (org-load-modules-maybe)
  8205. (unless orgstruct-initialized
  8206. (orgstruct-setup)
  8207. (setq orgstruct-initialized t))))
  8208. ;;;###autoload
  8209. (defun turn-on-orgstruct ()
  8210. "Unconditionally turn on `orgstruct-mode'."
  8211. (orgstruct-mode 1))
  8212. (defvar-local orgstruct-is-++ nil
  8213. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8214. (defvar-local org-fb-vars nil)
  8215. (defun orgstruct++-mode (&optional arg)
  8216. "Toggle `orgstruct-mode', the enhanced version of it.
  8217. In addition to setting orgstruct-mode, this also exports all
  8218. indentation and autofilling variables from org-mode into the
  8219. buffer. It will also recognize item context in multiline items."
  8220. (interactive "P")
  8221. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8222. (if (< arg 1)
  8223. (progn (orgstruct-mode -1)
  8224. (dolist (v org-fb-vars)
  8225. (set (make-local-variable (car v))
  8226. (if (eq (car-safe (cadr v)) 'quote)
  8227. (cl-cadadr v)
  8228. (nth 1 v)))))
  8229. (orgstruct-mode 1)
  8230. (setq org-fb-vars nil)
  8231. (unless org-local-vars
  8232. (setq org-local-vars (org-get-local-variables)))
  8233. (let (var val)
  8234. (dolist (x org-local-vars)
  8235. (when (string-match
  8236. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8237. \\|fill-prefix\\|indent-\\)"
  8238. (symbol-name (car x)))
  8239. (setq var (car x) val (nth 1 x))
  8240. (push (list var `(quote ,(eval var))) org-fb-vars)
  8241. (set (make-local-variable var)
  8242. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8243. (setq-local orgstruct-is-++ t))))
  8244. ;;;###autoload
  8245. (defun turn-on-orgstruct++ ()
  8246. "Unconditionally turn on `orgstruct++-mode'."
  8247. (orgstruct++-mode 1))
  8248. (defun orgstruct-error ()
  8249. "Error when there is no default binding for a structure key."
  8250. (interactive)
  8251. (funcall (if (fboundp 'user-error)
  8252. 'user-error
  8253. 'error)
  8254. "This key has no function outside structure elements"))
  8255. (defun orgstruct-setup ()
  8256. "Setup orgstruct keymap."
  8257. (dolist (cell '((org-demote . t)
  8258. (org-metaleft . t)
  8259. (org-metaright . t)
  8260. (org-promote . t)
  8261. (org-shiftmetaleft . t)
  8262. (org-shiftmetaright . t)
  8263. org-backward-element
  8264. org-backward-heading-same-level
  8265. org-ctrl-c-ret
  8266. org-ctrl-c-minus
  8267. org-ctrl-c-star
  8268. org-cycle
  8269. org-force-cycle-archived
  8270. org-forward-heading-same-level
  8271. org-insert-heading
  8272. org-insert-heading-respect-content
  8273. org-kill-note-or-show-branches
  8274. org-mark-subtree
  8275. org-meta-return
  8276. org-metadown
  8277. org-metaup
  8278. org-narrow-to-subtree
  8279. org-promote-subtree
  8280. org-reveal
  8281. org-shiftdown
  8282. org-shiftleft
  8283. org-shiftmetadown
  8284. org-shiftmetaup
  8285. org-shiftright
  8286. org-shifttab
  8287. org-shifttab
  8288. org-shiftup
  8289. org-show-children
  8290. org-show-subtree
  8291. org-sort
  8292. org-up-element
  8293. outline-demote
  8294. outline-next-visible-heading
  8295. outline-previous-visible-heading
  8296. outline-promote
  8297. outline-up-heading))
  8298. (let ((f (or (car-safe cell) cell))
  8299. (disable-when-heading-prefix (cdr-safe cell)))
  8300. (when (fboundp f)
  8301. (let ((new-bindings))
  8302. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8303. (where-is-internal f outline-mode-map)))
  8304. (push binding new-bindings)
  8305. ;; TODO use local-function-key-map
  8306. (dolist (rep '(("<tab>" . "TAB")
  8307. ("<return>" . "RET")
  8308. ("<escape>" . "ESC")
  8309. ("<delete>" . "DEL")))
  8310. (setq binding (read-kbd-macro
  8311. (let ((case-fold-search))
  8312. (replace-regexp-in-string
  8313. (regexp-quote (cdr rep))
  8314. (car rep)
  8315. (key-description binding)))))
  8316. (cl-pushnew binding new-bindings :test 'equal)))
  8317. (dolist (binding new-bindings)
  8318. (let ((key (lookup-key orgstruct-mode-map binding)))
  8319. (when (or (not key) (numberp key))
  8320. (ignore-errors
  8321. (org-defkey orgstruct-mode-map
  8322. binding
  8323. (orgstruct-make-binding
  8324. f binding disable-when-heading-prefix))))))))))
  8325. (run-hooks 'orgstruct-setup-hook))
  8326. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8327. "Create a function for binding in the structure minor mode.
  8328. FUN is the command to call inside a table. KEY is the key that
  8329. should be checked in for a command to execute outside of tables.
  8330. Non-nil `disable-when-heading-prefix' means to disable the command
  8331. if `orgstruct-heading-prefix-regexp' is not empty."
  8332. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8333. (let ((nname name)
  8334. (i 0))
  8335. (while (fboundp (intern nname))
  8336. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8337. (setq name (intern nname)))
  8338. (eval
  8339. (let ((bindings '((org-heading-regexp
  8340. (concat "^"
  8341. orgstruct-heading-prefix-regexp
  8342. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8343. (org-outline-regexp
  8344. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8345. (org-outline-regexp-bol
  8346. (concat "^" org-outline-regexp))
  8347. (outline-regexp org-outline-regexp)
  8348. (outline-heading-end-regexp "\n")
  8349. (outline-level 'org-outline-level)
  8350. (outline-heading-alist))))
  8351. `(defun ,name (arg)
  8352. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8353. "Outside of structure, run the binding of `"
  8354. (key-description key) "'."
  8355. (when disable-when-heading-prefix
  8356. (concat
  8357. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8358. "back to the default binding due to limitations of Org's implementation of\n"
  8359. "`" (symbol-name fun) "'.")))
  8360. (interactive "p")
  8361. (let* ((disable
  8362. ,(and disable-when-heading-prefix
  8363. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8364. (fallback
  8365. (or disable
  8366. (not
  8367. (let* ,bindings
  8368. (org-context-p 'headline 'item
  8369. ,(when (memq fun
  8370. '(org-insert-heading
  8371. org-insert-heading-respect-content
  8372. org-meta-return))
  8373. '(when orgstruct-is-++
  8374. 'item-body))))))))
  8375. (if fallback
  8376. (let* ((orgstruct-mode)
  8377. (binding
  8378. (let ((key ,key))
  8379. (catch 'exit
  8380. (dolist
  8381. (rep
  8382. '(nil
  8383. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8384. ("<\\([^>]*\\)return>" . "\\1RET")
  8385. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8386. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8387. nil)
  8388. (when rep
  8389. (setq key (read-kbd-macro
  8390. (let ((case-fold-search))
  8391. (replace-regexp-in-string
  8392. (car rep)
  8393. (cdr rep)
  8394. (key-description key))))))
  8395. (when (key-binding key)
  8396. (throw 'exit (key-binding key))))))))
  8397. (if (keymapp binding)
  8398. (org-set-transient-map binding)
  8399. (let ((func (or binding
  8400. (unless disable
  8401. 'orgstruct-error))))
  8402. (when func
  8403. (call-interactively func)))))
  8404. (org-run-like-in-org-mode
  8405. (lambda ()
  8406. (interactive)
  8407. (let* ,bindings
  8408. (call-interactively ',fun)))))))))
  8409. name))
  8410. (defun org-contextualize-keys (alist contexts)
  8411. "Return valid elements in ALIST depending on CONTEXTS.
  8412. `org-agenda-custom-commands' or `org-capture-templates' are the
  8413. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8414. or `org-capture-templates-contexts' are the associated contexts
  8415. definitions."
  8416. (let ((contexts
  8417. ;; normalize contexts
  8418. (mapcar
  8419. (lambda(c) (cond ((listp (cadr c))
  8420. (list (car c) (car c) (nth 1 c)))
  8421. ((string= "" (cadr c))
  8422. (list (car c) (car c) (nth 2 c)))
  8423. (t c)))
  8424. contexts))
  8425. (a alist) r s)
  8426. ;; loop over all commands or templates
  8427. (dolist (c a)
  8428. (let (vrules repl)
  8429. (cond
  8430. ((not (assoc (car c) contexts))
  8431. (push c r))
  8432. ((and (assoc (car c) contexts)
  8433. (setq vrules (org-contextualize-validate-key
  8434. (car c) contexts)))
  8435. (mapc (lambda (vr)
  8436. (unless (equal (car vr) (cadr vr))
  8437. (setq repl vr)))
  8438. vrules)
  8439. (if (not repl) (push c r)
  8440. (push (cadr repl) s)
  8441. (push
  8442. (cons (car c)
  8443. (cdr (or (assoc (cadr repl) alist)
  8444. (error "Undefined key `%s' as contextual replacement for `%s'"
  8445. (cadr repl) (car c)))))
  8446. r))))))
  8447. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8448. (delq
  8449. nil
  8450. (delete-dups
  8451. (mapcar (lambda (x)
  8452. (let ((tpl (car x)))
  8453. (unless (delq
  8454. nil
  8455. (mapcar (lambda (y)
  8456. (equal y tpl))
  8457. s))
  8458. x)))
  8459. (reverse r))))))
  8460. (defun org-contextualize-validate-key (key contexts)
  8461. "Check CONTEXTS for agenda or capture KEY."
  8462. (let (res)
  8463. (dolist (r contexts)
  8464. (dolist (rr (car (last r)))
  8465. (when
  8466. (and (equal key (car r))
  8467. (if (functionp rr) (funcall rr)
  8468. (or (and (eq (car rr) 'in-file)
  8469. (buffer-file-name)
  8470. (string-match (cdr rr) (buffer-file-name)))
  8471. (and (eq (car rr) 'in-mode)
  8472. (string-match (cdr rr) (symbol-name major-mode)))
  8473. (and (eq (car rr) 'in-buffer)
  8474. (string-match (cdr rr) (buffer-name)))
  8475. (when (and (eq (car rr) 'not-in-file)
  8476. (buffer-file-name))
  8477. (not (string-match (cdr rr) (buffer-file-name))))
  8478. (when (eq (car rr) 'not-in-mode)
  8479. (not (string-match (cdr rr) (symbol-name major-mode))))
  8480. (when (eq (car rr) 'not-in-buffer)
  8481. (not (string-match (cdr rr) (buffer-name)))))))
  8482. (push r res))))
  8483. (delete-dups (delq nil res))))
  8484. (defun org-context-p (&rest contexts)
  8485. "Check if local context is any of CONTEXTS.
  8486. Possible values in the list of contexts are `table', `headline', and `item'."
  8487. (let ((pos (point)))
  8488. (goto-char (point-at-bol))
  8489. (prog1 (or (and (memq 'table contexts)
  8490. (looking-at "[ \t]*|"))
  8491. (and (memq 'headline contexts)
  8492. (looking-at org-outline-regexp))
  8493. (and (memq 'item contexts)
  8494. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8495. (and (memq 'item-body contexts)
  8496. (org-in-item-p)))
  8497. (goto-char pos))))
  8498. (defconst org-unique-local-variables
  8499. '(org-element--cache
  8500. org-element--cache-objects
  8501. org-element--cache-sync-keys
  8502. org-element--cache-sync-requests
  8503. org-element--cache-sync-timer)
  8504. "List of local variables that cannot be transferred to another buffer.")
  8505. (defun org-get-local-variables ()
  8506. "Return a list of all local variables in an Org mode buffer."
  8507. (delq nil
  8508. (mapcar
  8509. (lambda (x)
  8510. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8511. (name (car binding)))
  8512. (and (not (get name 'org-state))
  8513. (not (memq name org-unique-local-variables))
  8514. (string-match-p
  8515. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8516. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8517. (symbol-name name))
  8518. binding)))
  8519. (with-temp-buffer
  8520. (org-mode)
  8521. (buffer-local-variables)))))
  8522. (defun org-clone-local-variables (from-buffer &optional regexp)
  8523. "Clone local variables from FROM-BUFFER.
  8524. Optional argument REGEXP selects variables to clone."
  8525. (dolist (pair (buffer-local-variables from-buffer))
  8526. (let ((name (car pair)))
  8527. (when (and (symbolp name)
  8528. (not (memq name org-unique-local-variables))
  8529. (or (null regexp) (string-match regexp (symbol-name name))))
  8530. (set (make-local-variable name) (cdr pair))))))
  8531. ;;;###autoload
  8532. (defun org-run-like-in-org-mode (cmd)
  8533. "Run a command, pretending that the current buffer is in Org-mode.
  8534. This will temporarily bind local variables that are typically bound in
  8535. Org-mode to the values they have in Org-mode, and then interactively
  8536. call CMD."
  8537. (org-load-modules-maybe)
  8538. (unless org-local-vars
  8539. (setq org-local-vars (org-get-local-variables)))
  8540. (let (binds)
  8541. (dolist (var org-local-vars)
  8542. (when (or (not (boundp (car var)))
  8543. (eq (symbol-value (car var))
  8544. (default-value (car var))))
  8545. (push (list (car var) `(quote ,(cadr var))) binds)))
  8546. (eval `(let ,binds
  8547. (call-interactively (quote ,cmd))))))
  8548. (defun org-get-category (&optional pos force-refresh)
  8549. "Get the category applying to position POS."
  8550. (save-match-data
  8551. (when force-refresh (org-refresh-category-properties))
  8552. (let ((pos (or pos (point))))
  8553. (or (get-text-property pos 'org-category)
  8554. (progn (org-refresh-category-properties)
  8555. (get-text-property pos 'org-category))))))
  8556. ;;; Refresh properties
  8557. (defun org-refresh-properties (dprop tprop)
  8558. "Refresh buffer text properties.
  8559. DPROP is the drawer property and TPROP is either the
  8560. corresponding text property to set, or an alist with each element
  8561. being a text property (as a symbol) and a function to apply to
  8562. the value of the drawer property."
  8563. (let ((case-fold-search t)
  8564. (inhibit-read-only t))
  8565. (org-with-silent-modifications
  8566. (save-excursion
  8567. (save-restriction
  8568. (widen)
  8569. (goto-char (point-min))
  8570. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8571. (org-refresh-property tprop (match-string-no-properties 1))))))))
  8572. (defun org-refresh-property (tprop p)
  8573. "Refresh the buffer text property TPROP from the drawer property P.
  8574. The refresh happens only for the current tree (not subtree)."
  8575. (unless (org-before-first-heading-p)
  8576. (save-excursion
  8577. (org-back-to-heading t)
  8578. (if (symbolp tprop)
  8579. ;; TPROP is a text property symbol
  8580. (put-text-property
  8581. (point) (or (outline-next-heading) (point-max)) tprop p)
  8582. ;; TPROP is an alist with (properties . function) elements
  8583. (dolist (al tprop)
  8584. (save-excursion
  8585. (put-text-property
  8586. (line-beginning-position) (or (outline-next-heading) (point-max))
  8587. (car al)
  8588. (funcall (cdr al) p))))))))
  8589. (defun org-refresh-category-properties ()
  8590. "Refresh category text properties in the buffer."
  8591. (let ((case-fold-search t)
  8592. (inhibit-read-only t)
  8593. (default-category
  8594. (cond ((null org-category)
  8595. (if buffer-file-name
  8596. (file-name-sans-extension
  8597. (file-name-nondirectory buffer-file-name))
  8598. "???"))
  8599. ((symbolp org-category) (symbol-name org-category))
  8600. (t org-category))))
  8601. (org-with-silent-modifications
  8602. (org-with-wide-buffer
  8603. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8604. ;; This is the default category for the buffer. If none is
  8605. ;; found, fall-back to `org-category' or buffer file name.
  8606. (put-text-property
  8607. (point-min) (point-max)
  8608. 'org-category
  8609. (catch 'buffer-category
  8610. (goto-char (point-max))
  8611. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8612. (let ((element (org-element-at-point)))
  8613. (when (eq (org-element-type element) 'keyword)
  8614. (throw 'buffer-category
  8615. (org-element-property :value element)))))
  8616. default-category))
  8617. ;; Set sub-tree specific categories.
  8618. (goto-char (point-min))
  8619. (let ((regexp (org-re-property "CATEGORY")))
  8620. (while (re-search-forward regexp nil t)
  8621. (let ((value (match-string-no-properties 3)))
  8622. (when (org-at-property-p)
  8623. (put-text-property
  8624. (save-excursion (org-back-to-heading t) (point))
  8625. (save-excursion (org-end-of-subtree t t) (point))
  8626. 'org-category
  8627. value)))))))))
  8628. (defun org-refresh-stats-properties ()
  8629. "Refresh stats text properties in the buffer."
  8630. (let (stats)
  8631. (org-with-silent-modifications
  8632. (save-excursion
  8633. (save-restriction
  8634. (widen)
  8635. (goto-char (point-min))
  8636. (while (re-search-forward
  8637. (concat org-outline-regexp-bol ".*"
  8638. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8639. nil t)
  8640. (setq stats (cond ((equal (match-string 3) "0") 0)
  8641. ((match-string 2)
  8642. (/ (* (string-to-number (match-string 2)) 100)
  8643. (string-to-number (match-string 3))))
  8644. (t (string-to-number (match-string 1)))))
  8645. (org-back-to-heading t)
  8646. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8647. 'org-stats stats)))))))
  8648. (defun org-refresh-effort-properties ()
  8649. "Refresh effort properties"
  8650. (org-refresh-properties
  8651. org-effort-property
  8652. '((effort . identity)
  8653. (effort-minutes . org-duration-string-to-minutes))))
  8654. ;;;; Link Stuff
  8655. ;;; Link abbreviations
  8656. (defun org-link-expand-abbrev (link)
  8657. "Apply replacements as defined in `org-link-abbrev-alist'."
  8658. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8659. (let* ((key (match-string 1 link))
  8660. (as (or (assoc key org-link-abbrev-alist-local)
  8661. (assoc key org-link-abbrev-alist)))
  8662. (tag (and (match-end 2) (match-string 3 link)))
  8663. rpl)
  8664. (if (not as)
  8665. link
  8666. (setq rpl (cdr as))
  8667. (cond
  8668. ((symbolp rpl) (funcall rpl tag))
  8669. ((string-match "%(\\([^)]+\\))" rpl)
  8670. (replace-match
  8671. (save-match-data
  8672. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8673. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8674. ((string-match "%h" rpl)
  8675. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8676. (t (concat rpl tag)))))
  8677. link))
  8678. ;;; Storing and inserting links
  8679. (defvar org-insert-link-history nil
  8680. "Minibuffer history for links inserted with `org-insert-link'.")
  8681. (defvar org-stored-links nil
  8682. "Contains the links stored with `org-store-link'.")
  8683. (defvar org-store-link-plist nil
  8684. "Plist with info about the most recently link created with `org-store-link'.")
  8685. (defvar org-link-protocols nil
  8686. "Link protocols added to Org-mode using `org-add-link-type'.")
  8687. (defvar org-store-link-functions nil
  8688. "List of functions that are called to create and store a link.
  8689. Each function will be called in turn until one returns a non-nil
  8690. value. Each function should check if it is responsible for creating
  8691. this link (for example by looking at the major mode).
  8692. If not, it must exit and return nil.
  8693. If yes, it should return a non-nil value after a calling
  8694. `org-store-link-props' with a list of properties and values.
  8695. Special properties are:
  8696. :type The link prefix, like \"http\". This must be given.
  8697. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8698. This is obligatory as well.
  8699. :description Optional default description for the second pair
  8700. of brackets in an Org-mode link. The user can still change
  8701. this when inserting this link into an Org-mode buffer.
  8702. In addition to these, any additional properties can be specified
  8703. and then used in capture templates.")
  8704. (defun org-add-link-type (type &optional follow export)
  8705. "Add TYPE to the list of `org-link-types'.
  8706. Re-compute all regular expressions depending on `org-link-types'
  8707. FOLLOW and EXPORT are two functions.
  8708. FOLLOW should take the link path as the single argument and do whatever
  8709. is necessary to follow the link, for example find a file or display
  8710. a mail message.
  8711. EXPORT should format the link path for export to one of the export formats.
  8712. It should be a function accepting three arguments:
  8713. path the path of the link, the text after the prefix (like \"http:\")
  8714. desc the description of the link, if any
  8715. format the export format, a symbol like `html' or `latex' or `ascii'.
  8716. The function may use the FORMAT information to return different values
  8717. depending on the format. The return value will be put literally into
  8718. the exported file. If the return value is nil, this means Org should
  8719. do what it normally does with links which do not have EXPORT defined.
  8720. Org mode has a built-in default for exporting links. If you are happy with
  8721. this default, there is no need to define an export function for the link
  8722. type. For a simple example of an export function, see `org-bbdb.el'."
  8723. (add-to-list 'org-link-types type t)
  8724. (org-make-link-regexps)
  8725. (org-element-update-syntax)
  8726. (if (assoc type org-link-protocols)
  8727. (setcdr (assoc type org-link-protocols) (list follow export))
  8728. (push (list type follow export) org-link-protocols)))
  8729. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8730. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8731. ;;;###autoload
  8732. (defun org-store-link (arg)
  8733. "\\<org-mode-map>Store an org-link to the current location.
  8734. This link is added to `org-stored-links' and can later be inserted
  8735. into an Org buffer with \\[org-insert-link].
  8736. For some link types, a prefix ARG is interpreted.
  8737. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8738. For file links, ARG negates `org-context-in-file-links'.
  8739. A double prefix ARG force skipping storing functions that are not
  8740. part of Org's core.
  8741. A triple prefix ARG force storing a link for each line in the
  8742. active region."
  8743. (interactive "P")
  8744. (org-load-modules-maybe)
  8745. (if (and (equal arg '(64)) (org-region-active-p))
  8746. (save-excursion
  8747. (let ((end (region-end)))
  8748. (goto-char (region-beginning))
  8749. (set-mark (point))
  8750. (while (< (point-at-eol) end)
  8751. (move-end-of-line 1) (activate-mark)
  8752. (let (current-prefix-arg)
  8753. (call-interactively 'org-store-link))
  8754. (move-beginning-of-line 2)
  8755. (set-mark (point)))))
  8756. (org-with-limited-levels
  8757. (setq org-store-link-plist nil)
  8758. (let (link cpltxt desc description search
  8759. txt custom-id agenda-link sfuns sfunsn)
  8760. (cond
  8761. ;; Store a link using an external link type
  8762. ((and (not (equal arg '(16)))
  8763. (setq sfuns
  8764. (delq
  8765. nil (mapcar (lambda (f)
  8766. (let (fs) (if (funcall f) (push f fs))))
  8767. org-store-link-functions))
  8768. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8769. (or (and (cdr sfuns)
  8770. (funcall (intern
  8771. (completing-read
  8772. "Which function for creating the link? "
  8773. sfunsn nil t (car sfunsn)))))
  8774. (funcall (caar sfuns)))
  8775. (setq link (plist-get org-store-link-plist :link)
  8776. desc (or (plist-get org-store-link-plist
  8777. :description)
  8778. link))))
  8779. ;; Store a link from a source code buffer.
  8780. ((org-src-edit-buffer-p)
  8781. (cond
  8782. ((save-excursion
  8783. (beginning-of-line)
  8784. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8785. "[ \t]*$")))
  8786. (setq link (format "(%s)" (match-string-no-properties 1))))
  8787. ((org-called-interactively-p 'any)
  8788. (let (label)
  8789. (while (or (not label)
  8790. (org-with-wide-buffer
  8791. (goto-char (point-min))
  8792. (re-search-forward
  8793. (regexp-quote (format org-coderef-label-format label))
  8794. nil t)))
  8795. (when label (message "Label exists already") (sit-for 2))
  8796. (setq label (read-string "Code line label: " label)))
  8797. (end-of-line)
  8798. (setq link (format org-coderef-label-format label))
  8799. (let ((gc (- 79 (length link))))
  8800. (if (< (current-column) gc) (org-move-to-column gc t)
  8801. (insert " ")))
  8802. (insert link)
  8803. (setq link (concat "(" label ")") desc nil)))
  8804. (t (setq link nil))))
  8805. ;; We are in the agenda, link to referenced location
  8806. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8807. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8808. (get-text-property (point) 'org-marker))))
  8809. (when m
  8810. (org-with-point-at m
  8811. (setq agenda-link
  8812. (if (org-called-interactively-p 'any)
  8813. (call-interactively 'org-store-link)
  8814. (org-store-link nil)))))))
  8815. ((eq major-mode 'calendar-mode)
  8816. (let ((cd (calendar-cursor-to-date)))
  8817. (setq link
  8818. (format-time-string
  8819. (car org-time-stamp-formats)
  8820. (apply 'encode-time
  8821. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8822. nil nil nil))))
  8823. (org-store-link-props :type "calendar" :date cd)))
  8824. ((eq major-mode 'help-mode)
  8825. (setq link (concat "help:" (save-excursion
  8826. (goto-char (point-min))
  8827. (looking-at "^[^ ]+")
  8828. (match-string 0))))
  8829. (org-store-link-props :type "help"))
  8830. ((eq major-mode 'w3-mode)
  8831. (setq cpltxt (if (and (buffer-name)
  8832. (not (string-match "Untitled" (buffer-name))))
  8833. (buffer-name)
  8834. (url-view-url t))
  8835. link (url-view-url t))
  8836. (org-store-link-props :type "w3" :url (url-view-url t)))
  8837. ((eq major-mode 'image-mode)
  8838. (setq cpltxt (concat "file:"
  8839. (abbreviate-file-name buffer-file-name))
  8840. link cpltxt)
  8841. (org-store-link-props :type "image" :file buffer-file-name))
  8842. ;; In dired, store a link to the file of the current line
  8843. ((derived-mode-p 'dired-mode)
  8844. (let ((file (dired-get-filename nil t)))
  8845. (setq file (if file
  8846. (abbreviate-file-name
  8847. (expand-file-name (dired-get-filename nil t)))
  8848. ;; otherwise, no file so use current directory.
  8849. default-directory))
  8850. (setq cpltxt (concat "file:" file)
  8851. link cpltxt)))
  8852. ((setq search (run-hook-with-args-until-success
  8853. 'org-create-file-search-functions))
  8854. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8855. "::" search))
  8856. (setq cpltxt (or description link)))
  8857. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8858. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8859. (cond
  8860. ;; Store a link using the target at point
  8861. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8862. (setq cpltxt
  8863. (concat "file:"
  8864. (abbreviate-file-name
  8865. (buffer-file-name (buffer-base-buffer)))
  8866. "::" (match-string 1))
  8867. link cpltxt))
  8868. ((and (featurep 'org-id)
  8869. (or (eq org-id-link-to-org-use-id t)
  8870. (and (org-called-interactively-p 'any)
  8871. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8872. (and (eq org-id-link-to-org-use-id
  8873. 'create-if-interactive-and-no-custom-id)
  8874. (not custom-id))))
  8875. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8876. ;; Store a link using the ID at point
  8877. (setq link (condition-case nil
  8878. (prog1 (org-id-store-link)
  8879. (setq desc (or (plist-get org-store-link-plist
  8880. :description)
  8881. "")))
  8882. (error
  8883. ;; Probably before first headline, link only to file
  8884. (concat "file:"
  8885. (abbreviate-file-name
  8886. (buffer-file-name (buffer-base-buffer))))))))
  8887. (t
  8888. ;; Just link to current headline
  8889. (setq cpltxt (concat "file:"
  8890. (abbreviate-file-name
  8891. (buffer-file-name (buffer-base-buffer)))))
  8892. ;; Add a context search string
  8893. (when (org-xor org-context-in-file-links arg)
  8894. (let* ((element (org-element-at-point))
  8895. (name (org-element-property :name element)))
  8896. (setq txt (cond
  8897. ((org-at-heading-p) nil)
  8898. (name)
  8899. ((org-region-active-p)
  8900. (buffer-substring (region-beginning) (region-end)))))
  8901. (when (or (null txt) (string-match "\\S-" txt))
  8902. (setq cpltxt
  8903. (concat cpltxt "::"
  8904. (condition-case nil
  8905. (org-make-org-heading-search-string txt)
  8906. (error "")))
  8907. desc (or name
  8908. (nth 4 (ignore-errors (org-heading-components)))
  8909. "NONE")))))
  8910. (when (string-match "::\\'" cpltxt)
  8911. (setq cpltxt (substring cpltxt 0 -2)))
  8912. (setq link cpltxt))))
  8913. ((buffer-file-name (buffer-base-buffer))
  8914. ;; Just link to this file here.
  8915. (setq cpltxt (concat "file:"
  8916. (abbreviate-file-name
  8917. (buffer-file-name (buffer-base-buffer)))))
  8918. ;; Add a context string.
  8919. (when (org-xor org-context-in-file-links arg)
  8920. (setq txt (if (org-region-active-p)
  8921. (buffer-substring (region-beginning) (region-end))
  8922. (buffer-substring (point-at-bol) (point-at-eol))))
  8923. ;; Only use search option if there is some text.
  8924. (when (string-match "\\S-" txt)
  8925. (setq cpltxt
  8926. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8927. desc "NONE")))
  8928. (setq link cpltxt))
  8929. ((org-called-interactively-p 'interactive)
  8930. (user-error "No method for storing a link from this buffer"))
  8931. (t (setq link nil)))
  8932. ;; We're done setting link and desc, clean up
  8933. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8934. (setq link (or link cpltxt)
  8935. desc (or desc cpltxt))
  8936. (cond ((not desc))
  8937. ((equal desc "NONE") (setq desc nil))
  8938. (t (setq desc
  8939. (replace-regexp-in-string
  8940. org-bracket-link-analytic-regexp
  8941. (lambda (m) (or (match-string 5 m) (match-string 3 m)))
  8942. desc))))
  8943. ;; Return the link
  8944. (if (not (and (or (org-called-interactively-p 'any)
  8945. executing-kbd-macro)
  8946. link))
  8947. (or agenda-link (and link (org-make-link-string link desc)))
  8948. (push (list link desc) org-stored-links)
  8949. (message "Stored: %s" (or desc link))
  8950. (when custom-id
  8951. (setq link (concat "file:" (abbreviate-file-name
  8952. (buffer-file-name)) "::#" custom-id))
  8953. (push (list link desc) org-stored-links))
  8954. (car org-stored-links))))))
  8955. (defun org-store-link-props (&rest plist)
  8956. "Store link properties, extract names, addresses and dates."
  8957. (let ((x (plist-get plist :from)))
  8958. (when x
  8959. (let ((adr (mail-extract-address-components x)))
  8960. (setq plist (plist-put plist :fromname (car adr)))
  8961. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8962. (let ((x (plist-get plist :to)))
  8963. (when x
  8964. (let ((adr (mail-extract-address-components x)))
  8965. (setq plist (plist-put plist :toname (car adr)))
  8966. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8967. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8968. (when x
  8969. (setq plist (plist-put plist :date-timestamp
  8970. (format-time-string
  8971. (org-time-stamp-format t) x)))
  8972. (setq plist (plist-put plist :date-timestamp-inactive
  8973. (format-time-string
  8974. (org-time-stamp-format t t) x)))))
  8975. (let ((from (plist-get plist :from))
  8976. (to (plist-get plist :to)))
  8977. (when (and from to org-from-is-user-regexp)
  8978. (setq plist
  8979. (plist-put plist :fromto
  8980. (if (string-match org-from-is-user-regexp from)
  8981. (concat "to %t")
  8982. (concat "from %f"))))))
  8983. (setq org-store-link-plist plist))
  8984. (defun org-add-link-props (&rest plist)
  8985. "Add these properties to the link property list."
  8986. (let (key value)
  8987. (while plist
  8988. (setq key (pop plist) value (pop plist))
  8989. (setq org-store-link-plist
  8990. (plist-put org-store-link-plist key value)))))
  8991. (defun org-email-link-description (&optional fmt)
  8992. "Return the description part of an email link.
  8993. This takes information from `org-store-link-plist' and formats it
  8994. according to FMT (default from `org-email-link-description-format')."
  8995. (setq fmt (or fmt org-email-link-description-format))
  8996. (let* ((p org-store-link-plist)
  8997. (to (plist-get p :toaddress))
  8998. (from (plist-get p :fromaddress))
  8999. (table
  9000. (list
  9001. (cons "%c" (plist-get p :fromto))
  9002. (cons "%F" (plist-get p :from))
  9003. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9004. (cons "%T" (plist-get p :to))
  9005. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9006. (cons "%s" (plist-get p :subject))
  9007. (cons "%d" (plist-get p :date))
  9008. (cons "%m" (plist-get p :message-id)))))
  9009. (when (string-match "%c" fmt)
  9010. ;; Check if the user wrote this message
  9011. (if (and org-from-is-user-regexp from to
  9012. (save-match-data (string-match org-from-is-user-regexp from)))
  9013. (setq fmt (replace-match "to %t" t t fmt))
  9014. (setq fmt (replace-match "from %f" t t fmt))))
  9015. (org-replace-escapes fmt table)))
  9016. (defun org-make-org-heading-search-string (&optional string)
  9017. "Make search string for the current headline or STRING."
  9018. (let ((s (or string
  9019. (and (derived-mode-p 'org-mode)
  9020. (save-excursion
  9021. (org-back-to-heading t)
  9022. (org-element-property :raw-value (org-element-at-point))))))
  9023. (lines org-context-in-file-links))
  9024. (or string (setq s (concat "*" s))) ; Add * for headlines
  9025. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  9026. (when (and string (integerp lines) (> lines 0))
  9027. (let ((slines (org-split-string s "\n")))
  9028. (when (< lines (length slines))
  9029. (setq s (mapconcat
  9030. 'identity
  9031. (reverse (nthcdr (- (length slines) lines)
  9032. (reverse slines))) "\n")))))
  9033. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9034. (defun org-make-link-string (link &optional description)
  9035. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9036. (unless (org-string-nw-p link) (error "Empty link"))
  9037. (let ((uri (cond ((string-match org-link-types-re link)
  9038. (concat (match-string 1 link)
  9039. (org-link-escape (substring link (match-end 1)))))
  9040. ;; For readability, url-encode internal links only
  9041. ;; when absolutely needed (i.e, when they contain
  9042. ;; square brackets). File links however, are
  9043. ;; encoded since, e.g., spaces are significant.
  9044. ((or (file-name-absolute-p link)
  9045. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9046. (org-link-escape link))
  9047. (t link)))
  9048. (description
  9049. (and (org-string-nw-p description)
  9050. ;; Remove brackets from description, as they are fatal.
  9051. (replace-regexp-in-string
  9052. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9053. (org-trim description)))))
  9054. (format "[[%s]%s]"
  9055. uri
  9056. (if description (format "[%s]" description) ""))))
  9057. (defconst org-link-escape-chars
  9058. ;;%20 %5B %5D %25
  9059. '(?\s ?\[ ?\] ?%)
  9060. "List of characters that should be escaped in a link when stored to Org.
  9061. This is the list that is used for internal purposes.")
  9062. (defconst org-link-escape-chars-browser
  9063. ;;%20 %22
  9064. '(?\s ?\")
  9065. "List of characters to be escaped before handing over to the browser.
  9066. If you consider using this constant then you probably want to use
  9067. the function `org-link-escape-browser' instead. See there why
  9068. this constant is a candidate to be removed once Org drops support
  9069. for Emacs 24.1 and 24.2.")
  9070. (defun org-link-escape (text &optional table merge)
  9071. "Return percent escaped representation of TEXT.
  9072. TEXT is a string with the text to escape.
  9073. Optional argument TABLE is a list with characters that should be
  9074. escaped. When nil, `org-link-escape-chars' is used.
  9075. If optional argument MERGE is set, merge TABLE into
  9076. `org-link-escape-chars'."
  9077. (let ((characters-to-encode
  9078. (cond ((null table) org-link-escape-chars)
  9079. (merge (append org-link-escape-chars table))
  9080. (t table))))
  9081. (mapconcat
  9082. (lambda (c)
  9083. (if (or (memq c characters-to-encode)
  9084. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9085. (mapconcat (lambda (e) (format "%%%.2X" e))
  9086. (or (encode-coding-char c 'utf-8)
  9087. (error "Unable to percent escape character: %c" c))
  9088. "")
  9089. (char-to-string c)))
  9090. text "")))
  9091. (defun org-link-escape-browser (text)
  9092. "Escape some characters before handing over to the browser.
  9093. This function is a candidate to be removed together with the
  9094. constant `org-link-escape-chars-browser' once Org drops support
  9095. for Emacs 24.1 and 24.2. All calls to this function will have to
  9096. be replaced with `url-encode-url' which is available since Emacs
  9097. 24.3.1."
  9098. ;; Example with the Org link
  9099. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9100. ;; to open the browser with +subject:"Release 8.2" filled into the
  9101. ;; query field: In this case the variable TEXT contains the
  9102. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9103. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9104. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9105. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9106. (if (fboundp 'url-encode-url)
  9107. (url-encode-url text)
  9108. (if (org-string-match-p
  9109. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9110. text)
  9111. (org-link-escape text org-link-escape-chars-browser)
  9112. text)))
  9113. (defun org-link-unescape (str)
  9114. "Unhex hexified Unicode parts in string STR.
  9115. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9116. reciprocal of `org-link-escape', which see."
  9117. (if (org-string-nw-p str)
  9118. (replace-regexp-in-string
  9119. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9120. str))
  9121. (defun org-link-unescape-compound (hex)
  9122. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9123. Note: this function also decodes single byte encodings like
  9124. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9125. (save-match-data
  9126. (let* ((bytes (cdr (split-string hex "%")))
  9127. (ret "")
  9128. (eat 0)
  9129. (sum 0))
  9130. (while bytes
  9131. (let* ((val (string-to-number (pop bytes) 16))
  9132. (shift-xor
  9133. (if (= 0 eat)
  9134. (cond
  9135. ((>= val 252) (cons 6 252))
  9136. ((>= val 248) (cons 5 248))
  9137. ((>= val 240) (cons 4 240))
  9138. ((>= val 224) (cons 3 224))
  9139. ((>= val 192) (cons 2 192))
  9140. (t (cons 0 0)))
  9141. (cons 6 128))))
  9142. (when (>= val 192) (setq eat (car shift-xor)))
  9143. (setq val (logxor val (cdr shift-xor)))
  9144. (setq sum (+ (lsh sum (car shift-xor)) val))
  9145. (when (> eat 0) (setq eat (- eat 1)))
  9146. (cond
  9147. ((= 0 eat) ;multi byte
  9148. (setq ret (concat ret (org-char-to-string sum)))
  9149. (setq sum 0))
  9150. ((not bytes) ; single byte(s)
  9151. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9152. ret)))
  9153. (defun org-link-unescape-single-byte-sequence (hex)
  9154. "Unhexify hex-encoded single byte character sequences."
  9155. (mapconcat (lambda (byte)
  9156. (char-to-string (string-to-number byte 16)))
  9157. (cdr (split-string hex "%")) ""))
  9158. (defun org-xor (a b)
  9159. "Exclusive or."
  9160. (if a (not b) b))
  9161. (defun org-fixup-message-id-for-http (s)
  9162. "Replace special characters in a message id, so it can be used in an http query."
  9163. (when (string-match "%" s)
  9164. (setq s (mapconcat (lambda (c)
  9165. (if (eq c ?%)
  9166. "%25"
  9167. (char-to-string c)))
  9168. s "")))
  9169. (while (string-match "<" s)
  9170. (setq s (replace-match "%3C" t t s)))
  9171. (while (string-match ">" s)
  9172. (setq s (replace-match "%3E" t t s)))
  9173. (while (string-match "@" s)
  9174. (setq s (replace-match "%40" t t s)))
  9175. s)
  9176. (defun org-link-prettify (link)
  9177. "Return a human-readable representation of LINK.
  9178. The car of LINK must be a raw link.
  9179. The cdr of LINK must be either a link description or nil."
  9180. (let ((desc (or (cadr link) "<no description>")))
  9181. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9182. "<" (car link) ">")))
  9183. ;;;###autoload
  9184. (defun org-insert-link-global ()
  9185. "Insert a link like Org-mode does.
  9186. This command can be called in any mode to insert a link in Org-mode syntax."
  9187. (interactive)
  9188. (org-load-modules-maybe)
  9189. (org-run-like-in-org-mode 'org-insert-link))
  9190. (defun org-insert-all-links (arg &optional pre post)
  9191. "Insert all links in `org-stored-links'.
  9192. When a universal prefix, do not delete the links from `org-stored-links'.
  9193. When `ARG' is a number, insert the last N link(s).
  9194. `PRE' and `POST' are optional arguments to define a string to
  9195. prepend or to append."
  9196. (interactive "P")
  9197. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9198. (links (copy-sequence org-stored-links))
  9199. (pr (or pre "- "))
  9200. (po (or post "\n"))
  9201. (cnt 1) l)
  9202. (if (null org-stored-links)
  9203. (message "No link to insert")
  9204. (while (and (or (listp arg) (>= arg cnt))
  9205. (setq l (if (listp arg)
  9206. (pop links)
  9207. (pop org-stored-links))))
  9208. (setq cnt (1+ cnt))
  9209. (insert pr)
  9210. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9211. (insert po)))))
  9212. (defun org-insert-last-stored-link (arg)
  9213. "Insert the last link stored in `org-stored-links'."
  9214. (interactive "p")
  9215. (org-insert-all-links arg "" "\n"))
  9216. (defun org-link-fontify-links-to-this-file ()
  9217. "Fontify links to the current file in `org-stored-links'."
  9218. (let ((f (buffer-file-name)) a b)
  9219. (setq a (mapcar (lambda(l)
  9220. (let ((ll (car l)))
  9221. (when (and (string-match "^file:\\(.+\\)::" ll)
  9222. (equal f (expand-file-name (match-string 1 ll))))
  9223. ll)))
  9224. org-stored-links))
  9225. (when (featurep 'org-id)
  9226. (setq b (mapcar (lambda(l)
  9227. (let ((ll (car l)))
  9228. (when (and (string-match "^id:\\(.+\\)$" ll)
  9229. (equal f (expand-file-name
  9230. (or (org-id-find-id-file
  9231. (match-string 1 ll)) ""))))
  9232. ll)))
  9233. org-stored-links)))
  9234. (mapcar (lambda(l)
  9235. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9236. (delq nil (append a b)))))
  9237. (defvar org--links-history nil)
  9238. (defun org-insert-link (&optional complete-file link-location default-description)
  9239. "Insert a link. At the prompt, enter the link.
  9240. Completion can be used to insert any of the link protocol prefixes like
  9241. http or ftp in use.
  9242. The history can be used to select a link previously stored with
  9243. `org-store-link'. When the empty string is entered (i.e. if you just
  9244. press RET at the prompt), the link defaults to the most recently
  9245. stored link. As SPC triggers completion in the minibuffer, you need to
  9246. use M-SPC or C-q SPC to force the insertion of a space character.
  9247. You will also be prompted for a description, and if one is given, it will
  9248. be displayed in the buffer instead of the link.
  9249. If there is already a link at point, this command will allow you to edit link
  9250. and description parts.
  9251. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9252. be selected using completion. The path to the file will be relative to the
  9253. current directory if the file is in the current directory or a subdirectory.
  9254. Otherwise, the link will be the absolute path as completed in the minibuffer
  9255. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9256. option `org-link-file-path-type'.
  9257. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9258. the current directory or below.
  9259. With three \\[universal-argument] prefixes, negate the meaning of
  9260. `org-keep-stored-link-after-insertion'.
  9261. If `org-make-link-description-function' is non-nil, this function will be
  9262. called with the link target, and the result will be the default
  9263. link description.
  9264. If the LINK-LOCATION parameter is non-nil, this value will be
  9265. used as the link location instead of reading one interactively.
  9266. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9267. be used as the default description."
  9268. (interactive "P")
  9269. (let* ((wcf (current-window-configuration))
  9270. (origbuf (current-buffer))
  9271. (region (when (org-region-active-p)
  9272. (buffer-substring (region-beginning) (region-end))))
  9273. (remove (and region (list (region-beginning) (region-end))))
  9274. (desc region)
  9275. (link link-location)
  9276. (abbrevs org-link-abbrev-alist-local)
  9277. entry all-prefixes auto-desc)
  9278. (cond
  9279. (link-location) ; specified by arg, just use it.
  9280. ((org-in-regexp org-bracket-link-regexp 1)
  9281. ;; We do have a link at point, and we are going to edit it.
  9282. (setq remove (list (match-beginning 0) (match-end 0)))
  9283. (setq desc (when (match-end 3) (match-string-no-properties 3)))
  9284. (setq link (read-string "Link: "
  9285. (org-link-unescape
  9286. (match-string-no-properties 1)))))
  9287. ((or (org-in-regexp org-angle-link-re)
  9288. (org-in-regexp org-plain-link-re))
  9289. ;; Convert to bracket link
  9290. (setq remove (list (match-beginning 0) (match-end 0))
  9291. link (read-string "Link: "
  9292. (org-remove-angle-brackets (match-string 0)))))
  9293. ((member complete-file '((4) (16)))
  9294. ;; Completing read for file names.
  9295. (setq link (org-file-complete-link complete-file)))
  9296. (t
  9297. ;; Read link, with completion for stored links.
  9298. (org-link-fontify-links-to-this-file)
  9299. (org-switch-to-buffer-other-window "*Org Links*")
  9300. (with-current-buffer "*Org Links*"
  9301. (erase-buffer)
  9302. (insert "Insert a link.
  9303. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9304. (when org-stored-links
  9305. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9306. (insert (mapconcat 'org-link-prettify
  9307. (reverse org-stored-links) "\n")))
  9308. (goto-char (point-min)))
  9309. (let ((cw (selected-window)))
  9310. (select-window (get-buffer-window "*Org Links*" 'visible))
  9311. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9312. (unless (pos-visible-in-window-p (point-max))
  9313. (org-fit-window-to-buffer))
  9314. (and (window-live-p cw) (select-window cw)))
  9315. (setq all-prefixes (append (mapcar 'car abbrevs)
  9316. (mapcar 'car org-link-abbrev-alist)
  9317. org-link-types))
  9318. (unwind-protect
  9319. ;; Fake a link history, containing the stored links.
  9320. (let ((org--links-history
  9321. (append (mapcar #'car org-stored-links)
  9322. org-insert-link-history)))
  9323. (setq link
  9324. (org-completing-read
  9325. "Link: "
  9326. (append
  9327. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9328. (mapcar #'car org-stored-links))
  9329. nil nil nil
  9330. 'org--links-history
  9331. (caar org-stored-links)))
  9332. (unless (org-string-nw-p link) (user-error "No link selected"))
  9333. (dolist (l org-stored-links)
  9334. (when (equal link (cadr l))
  9335. (setq link (car l))
  9336. (setq auto-desc t)))
  9337. (when (or (member link all-prefixes)
  9338. (and (equal ":" (substring link -1))
  9339. (member (substring link 0 -1) all-prefixes)
  9340. (setq link (substring link 0 -1))))
  9341. (setq link (with-current-buffer origbuf
  9342. (org-link-try-special-completion link)))))
  9343. (set-window-configuration wcf)
  9344. (kill-buffer "*Org Links*"))
  9345. (setq entry (assoc link org-stored-links))
  9346. (or entry (push link org-insert-link-history))
  9347. (setq desc (or desc (nth 1 entry)))))
  9348. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9349. (not org-keep-stored-link-after-insertion))
  9350. (setq org-stored-links (delq (assoc link org-stored-links)
  9351. org-stored-links)))
  9352. (when (and (string-match org-plain-link-re link)
  9353. (not (string-match org-ts-regexp link)))
  9354. ;; URL-like link, normalize the use of angular brackets.
  9355. (setq link (org-remove-angle-brackets link)))
  9356. ;; Check if we are linking to the current file with a search
  9357. ;; option If yes, simplify the link by using only the search
  9358. ;; option.
  9359. (when (and buffer-file-name
  9360. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9361. (let* ((path (match-string 1 link))
  9362. (case-fold-search nil)
  9363. (search (match-string 2 link)))
  9364. (save-match-data
  9365. (when (equal (file-truename buffer-file-name) (file-truename path))
  9366. ;; We are linking to this same file, with a search option
  9367. (setq link search)))))
  9368. ;; Check if we can/should use a relative path. If yes, simplify the link
  9369. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9370. (let* ((type (match-string 1 link))
  9371. (path (match-string 2 link))
  9372. (origpath path)
  9373. (case-fold-search nil))
  9374. (cond
  9375. ((or (eq org-link-file-path-type 'absolute)
  9376. (equal complete-file '(16)))
  9377. (setq path (abbreviate-file-name (expand-file-name path))))
  9378. ((eq org-link-file-path-type 'noabbrev)
  9379. (setq path (expand-file-name path)))
  9380. ((eq org-link-file-path-type 'relative)
  9381. (setq path (file-relative-name path)))
  9382. (t
  9383. (save-match-data
  9384. (if (string-match (concat "^" (regexp-quote
  9385. (expand-file-name
  9386. (file-name-as-directory
  9387. default-directory))))
  9388. (expand-file-name path))
  9389. ;; We are linking a file with relative path name.
  9390. (setq path (substring (expand-file-name path)
  9391. (match-end 0)))
  9392. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9393. (setq link (concat type path))
  9394. (when (equal desc origpath)
  9395. (setq desc path))))
  9396. (if org-make-link-description-function
  9397. (setq desc
  9398. (or (condition-case nil
  9399. (funcall org-make-link-description-function link desc)
  9400. (error (progn (message "Can't get link description from `%s'"
  9401. (symbol-name org-make-link-description-function))
  9402. (sit-for 2) nil)))
  9403. (read-string "Description: " default-description)))
  9404. (if default-description (setq desc default-description)
  9405. (setq desc (or (and auto-desc desc)
  9406. (read-string "Description: " desc)))))
  9407. (unless (string-match "\\S-" desc) (setq desc nil))
  9408. (when remove (apply 'delete-region remove))
  9409. (insert (org-make-link-string link desc))
  9410. ;; Redisplay so as the new link has proper invisible characters.
  9411. (sit-for 0)))
  9412. (defun org-link-try-special-completion (type)
  9413. "If there is completion support for link type TYPE, offer it."
  9414. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9415. (if (functionp fun)
  9416. (funcall fun)
  9417. (read-string "Link (no completion support): " (concat type ":")))))
  9418. (defun org-file-complete-link (&optional arg)
  9419. "Create a file link using completion."
  9420. (let ((file (read-file-name "File: "))
  9421. (pwd (file-name-as-directory (expand-file-name ".")))
  9422. (pwd1 (file-name-as-directory (abbreviate-file-name
  9423. (expand-file-name ".")))))
  9424. (cond ((equal arg '(16))
  9425. (concat "file:"
  9426. (abbreviate-file-name (expand-file-name file))))
  9427. ((string-match
  9428. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9429. (concat "file:" (match-string 1 file)))
  9430. ((string-match
  9431. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9432. (expand-file-name file))
  9433. (concat "file:"
  9434. (match-string 1 (expand-file-name file))))
  9435. (t (concat "file:" file)))))
  9436. (defun org-completing-read (&rest args)
  9437. "Completing-read with SPACE being a normal character."
  9438. (let ((enable-recursive-minibuffers t)
  9439. (minibuffer-local-completion-map
  9440. (copy-keymap minibuffer-local-completion-map)))
  9441. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9442. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9443. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9444. 'org-time-stamp-inactive)
  9445. (apply #'completing-read args)))
  9446. (define-obsolete-function-alias
  9447. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9448. (define-obsolete-function-alias
  9449. 'org-icompleting-read 'completing-read "Org 9.0")
  9450. (define-obsolete-function-alias
  9451. 'org-iread-file-name 'read-file-name "Org 9.0")
  9452. ;;; Opening/following a link
  9453. (defvar org-link-search-failed nil)
  9454. (defvar org-open-link-functions nil
  9455. "Hook for functions finding a plain text link.
  9456. These functions must take a single argument, the link content.
  9457. They will be called for links that look like [[link text][description]]
  9458. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9459. like a filename (e.g. \"./blue.png\").
  9460. These functions will be called *before* Org attempts to resolve the
  9461. link by doing text searches in the current buffer - so if you want a
  9462. link \"[[target]]\" to still find \"<<target>>\", your function should
  9463. handle this as a special case.
  9464. When the function does handle the link, it must return a non-nil value.
  9465. If it decides that it is not responsible for this link, it must return
  9466. nil to indicate that that Org-mode can continue with other options
  9467. like exact and fuzzy text search.")
  9468. (defun org-next-link (&optional search-backward)
  9469. "Move forward to the next link.
  9470. If the link is in hidden text, expose it."
  9471. (interactive "P")
  9472. (when (and org-link-search-failed (eq this-command last-command))
  9473. (goto-char (point-min))
  9474. (message "Link search wrapped back to beginning of buffer"))
  9475. (setq org-link-search-failed nil)
  9476. (let* ((pos (point))
  9477. (ct (org-context))
  9478. (a (assoc :link ct))
  9479. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9480. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9481. ((looking-at org-any-link-re)
  9482. ;; Don't stay stuck at link without an org-link face
  9483. (forward-char (if search-backward -1 1))))
  9484. (if (funcall srch-fun org-any-link-re nil t)
  9485. (progn
  9486. (goto-char (match-beginning 0))
  9487. (when (outline-invisible-p) (org-show-context)))
  9488. (goto-char pos)
  9489. (setq org-link-search-failed t)
  9490. (message "No further link found"))))
  9491. (defun org-previous-link ()
  9492. "Move backward to the previous link.
  9493. If the link is in hidden text, expose it."
  9494. (interactive)
  9495. (funcall 'org-next-link t))
  9496. (defun org-translate-link (s)
  9497. "Translate a link string if a translation function has been defined."
  9498. (with-temp-buffer
  9499. (insert (org-trim s))
  9500. (org-trim (org-element-interpret-data (org-element-context)))))
  9501. (defun org-translate-link-from-planner (type path)
  9502. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9503. This is still an experimental function, your mileage may vary."
  9504. (cond
  9505. ((member type '("http" "https" "news" "ftp"))
  9506. ;; standard Internet links are the same.
  9507. nil)
  9508. ((and (equal type "irc") (string-match "^//" path))
  9509. ;; Planner has two / at the beginning of an irc link, we have 1.
  9510. ;; We should have zero, actually....
  9511. (setq path (substring path 1)))
  9512. ((and (equal type "lisp") (string-match "^/" path))
  9513. ;; Planner has a slash, we do not.
  9514. (setq type "elisp" path (substring path 1)))
  9515. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9516. ;; A typical message link. Planner has the id after the final slash,
  9517. ;; we separate it with a hash mark
  9518. (setq path (concat (match-string 1 path) "#"
  9519. (org-remove-angle-brackets (match-string 2 path))))))
  9520. (cons type path))
  9521. (defun org-find-file-at-mouse (ev)
  9522. "Open file link or URL at mouse."
  9523. (interactive "e")
  9524. (mouse-set-point ev)
  9525. (org-open-at-point 'in-emacs))
  9526. (defun org-open-at-mouse (ev)
  9527. "Open file link or URL at mouse.
  9528. See the docstring of `org-open-file' for details."
  9529. (interactive "e")
  9530. (mouse-set-point ev)
  9531. (when (eq major-mode 'org-agenda-mode)
  9532. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9533. (org-open-at-point))
  9534. (defvar org-window-config-before-follow-link nil
  9535. "The window configuration before following a link.
  9536. This is saved in case the need arises to restore it.")
  9537. ;;;###autoload
  9538. (defun org-open-at-point-global ()
  9539. "Follow a link or time-stamp like Org mode does.
  9540. This command can be called in any mode to follow an external link
  9541. or a time-stamp that has Org mode syntax. Its behavior is
  9542. undefined when called on internal links (e.g., fuzzy links).
  9543. Raise an error when there is nothing to follow. "
  9544. (interactive)
  9545. (cond ((org-in-regexp org-any-link-re)
  9546. (org-open-link-from-string (match-string-no-properties 0)))
  9547. ((or (org-in-regexp org-ts-regexp-both nil t)
  9548. (org-in-regexp org-tsr-regexp-both nil t))
  9549. (org-follow-timestamp-link))
  9550. (t (user-error "No link found"))))
  9551. ;;;###autoload
  9552. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9553. "Open a link in the string S, as if it was in Org-mode."
  9554. (interactive "sLink: \nP")
  9555. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9556. (with-temp-buffer
  9557. (let ((org-inhibit-startup (not reference-buffer)))
  9558. (org-mode)
  9559. (insert s)
  9560. (goto-char (point-min))
  9561. (when reference-buffer
  9562. (setq org-link-abbrev-alist-local
  9563. (with-current-buffer reference-buffer
  9564. org-link-abbrev-alist-local)))
  9565. (org-open-at-point arg reference-buffer)))))
  9566. (defvar org-open-at-point-functions nil
  9567. "Hook that is run when following a link at point.
  9568. Functions in this hook must return t if they identify and follow
  9569. a link at point. If they don't find anything interesting at point,
  9570. they must return nil.")
  9571. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9572. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9573. (defun org-open-at-point (&optional arg reference-buffer)
  9574. "Open link, timestamp, footnote or tags at point.
  9575. When point is on a link, follow it. Normally, files will be
  9576. opened by an appropriate application. If the optional prefix
  9577. argument ARG is non-nil, Emacs will visit the file. With
  9578. a double prefix argument, try to open outside of Emacs, in the
  9579. application the system uses for this file type.
  9580. When point is on a timestamp, open the agenda at the day
  9581. specified.
  9582. When point is a footnote definition, move to the first reference
  9583. found. If it is on a reference, move to the associated
  9584. definition.
  9585. When point is on a headline, display a list of every link in the
  9586. entry, so it is possible to pick one, or all, of them. If point
  9587. is on a tag, call `org-tags-view' instead.
  9588. When optional argument REFERENCE-BUFFER is non-nil, it should
  9589. specify a buffer from where the link search should happen. This
  9590. is used internally by `org-open-link-from-string'.
  9591. On top of syntactically correct links, this function will open
  9592. the link at point in comments or comment blocks and the first
  9593. link in a property drawer line."
  9594. (interactive "P")
  9595. ;; On a code block, open block's results.
  9596. (unless (call-interactively 'org-babel-open-src-block-result)
  9597. (org-load-modules-maybe)
  9598. (setq org-window-config-before-follow-link (current-window-configuration))
  9599. (org-remove-occur-highlights nil nil t)
  9600. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9601. (let* ((context
  9602. ;; Only consider supported types, even if they are not
  9603. ;; the closest one.
  9604. (org-element-lineage
  9605. (org-element-context)
  9606. '(clock comment comment-block footnote-definition
  9607. footnote-reference headline inlinetask keyword link
  9608. node-property timestamp)
  9609. t))
  9610. (type (org-element-type context))
  9611. (value (org-element-property :value context)))
  9612. (cond
  9613. ((not context) (user-error "No link found"))
  9614. ;; Exception: open timestamps and links in properties
  9615. ;; drawers, keywords and comments.
  9616. ((memq type '(comment comment-block keyword node-property))
  9617. (call-interactively #'org-open-at-point-global))
  9618. ;; On a headline or an inlinetask, but not on a timestamp,
  9619. ;; a link, a footnote reference or on tags.
  9620. ((and (memq type '(headline inlinetask))
  9621. ;; Not on tags.
  9622. (progn (save-excursion (beginning-of-line)
  9623. (looking-at org-complex-heading-regexp))
  9624. (or (not (match-beginning 5))
  9625. (< (point) (match-beginning 5)))))
  9626. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9627. (links (car data))
  9628. (links-end (cdr data)))
  9629. (if links
  9630. (dolist (link (if (stringp links) (list links) links))
  9631. (search-forward link nil links-end)
  9632. (goto-char (match-beginning 0))
  9633. (org-open-at-point))
  9634. (require 'org-attach)
  9635. (org-attach-reveal 'if-exists))))
  9636. ;; On a clock line, make sure point is on the timestamp
  9637. ;; before opening it.
  9638. ((and (eq type 'clock)
  9639. value
  9640. (>= (point) (org-element-property :begin value))
  9641. (<= (point) (org-element-property :end value)))
  9642. (org-follow-timestamp-link))
  9643. ;; Do nothing on white spaces after an object.
  9644. ((>= (point)
  9645. (save-excursion
  9646. (goto-char (org-element-property :end context))
  9647. (skip-chars-backward " \t")
  9648. (point)))
  9649. (user-error "No link found"))
  9650. ((eq type 'timestamp) (org-follow-timestamp-link))
  9651. ;; On tags within a headline or an inlinetask.
  9652. ((and (memq type '(headline inlinetask))
  9653. (progn (save-excursion (beginning-of-line)
  9654. (looking-at org-complex-heading-regexp))
  9655. (and (match-beginning 5)
  9656. (>= (point) (match-beginning 5)))))
  9657. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9658. ((eq type 'link)
  9659. ;; When link is located within the description of another
  9660. ;; link (e.g., an inline image), always open the parent
  9661. ;; link.
  9662. (let*((link (let ((up (org-element-property :parent context)))
  9663. (if (eq (org-element-type up) 'link) up context)))
  9664. (type (org-element-property :type link))
  9665. (path (org-link-unescape (org-element-property :path link))))
  9666. ;; Switch back to REFERENCE-BUFFER needed when called in
  9667. ;; a temporary buffer through `org-open-link-from-string'.
  9668. (with-current-buffer (or reference-buffer (current-buffer))
  9669. (cond
  9670. ((equal type "file")
  9671. (if (string-match "[*?{]" (file-name-nondirectory path))
  9672. (dired path)
  9673. ;; Look into `org-link-protocols' in order to find
  9674. ;; a DEDICATED-FUNCTION to open file. The function
  9675. ;; will be applied on raw link instead of parsed
  9676. ;; link due to the limitation in `org-add-link-type'
  9677. ;; ("open" function called with a single argument).
  9678. ;; If no such function is found, fallback to
  9679. ;; `org-open-file'.
  9680. ;;
  9681. ;; Note : "file+emacs" and "file+sys" types are
  9682. ;; hard-coded in order to escape the previous
  9683. ;; limitation.
  9684. (let* ((option (org-element-property :search-option link))
  9685. (app (org-element-property :application link))
  9686. (dedicated-function
  9687. (nth 1 (assoc app org-link-protocols))))
  9688. (if dedicated-function
  9689. (funcall dedicated-function
  9690. (concat path
  9691. (and option (concat "::" option))))
  9692. (apply #'org-open-file
  9693. path
  9694. (cond (arg)
  9695. ((equal app "emacs") 'emacs)
  9696. ((equal app "sys") 'system))
  9697. (cond ((not option) nil)
  9698. ((org-string-match-p "\\`[0-9]+\\'" option)
  9699. (list (string-to-number option)))
  9700. (t (list nil
  9701. (org-link-unescape option)))))))))
  9702. ((assoc type org-link-protocols)
  9703. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9704. ((equal type "help")
  9705. (let ((f-or-v (intern path)))
  9706. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9707. ((boundp f-or-v) (describe-variable f-or-v))
  9708. (t (error "Not a known function or variable")))))
  9709. ((member type '("http" "https" "ftp" "mailto" "news"))
  9710. (browse-url (org-link-escape-browser (concat type ":" path))))
  9711. ((equal type "doi")
  9712. (browse-url
  9713. (org-link-escape-browser (concat org-doi-server-url path))))
  9714. ((equal type "message") (browse-url (concat type ":" path)))
  9715. ((equal type "shell")
  9716. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9717. (cmd path))
  9718. (if (or (and (org-string-nw-p
  9719. org-confirm-shell-link-not-regexp)
  9720. (string-match
  9721. org-confirm-shell-link-not-regexp cmd))
  9722. (not org-confirm-shell-link-function)
  9723. (funcall org-confirm-shell-link-function
  9724. (format "Execute \"%s\" in shell? "
  9725. (org-add-props cmd nil
  9726. 'face 'org-warning))))
  9727. (progn
  9728. (message "Executing %s" cmd)
  9729. (shell-command cmd buf)
  9730. (when (featurep 'midnight)
  9731. (setq clean-buffer-list-kill-buffer-names
  9732. (cons (buffer-name buf)
  9733. clean-buffer-list-kill-buffer-names))))
  9734. (user-error "Abort"))))
  9735. ((equal type "elisp")
  9736. (let ((cmd path))
  9737. (if (or (and (org-string-nw-p
  9738. org-confirm-elisp-link-not-regexp)
  9739. (org-string-match-p
  9740. org-confirm-elisp-link-not-regexp cmd))
  9741. (not org-confirm-elisp-link-function)
  9742. (funcall org-confirm-elisp-link-function
  9743. (format "Execute \"%s\" as elisp? "
  9744. (org-add-props cmd nil
  9745. 'face 'org-warning))))
  9746. (message "%s => %s" cmd
  9747. (if (eq (string-to-char cmd) ?\()
  9748. (eval (read cmd))
  9749. (call-interactively (read cmd))))
  9750. (user-error "Abort"))))
  9751. ((equal type "id")
  9752. (require 'org-id)
  9753. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9754. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9755. (unless (run-hook-with-args-until-success
  9756. 'org-open-link-functions path)
  9757. (if (not arg) (org-mark-ring-push)
  9758. (switch-to-buffer-other-window
  9759. (org-get-buffer-for-internal-link (current-buffer))))
  9760. (let ((destination
  9761. (org-with-wide-buffer
  9762. (if (equal type "radio")
  9763. (org-search-radio-target
  9764. (org-element-property :path link))
  9765. (org-link-search
  9766. (if (member type '("custom-id" "coderef"))
  9767. (org-element-property :raw-link link)
  9768. path)
  9769. ;; Prevent fuzzy links from matching
  9770. ;; themselves.
  9771. (and (equal type "fuzzy")
  9772. (+ 2 (org-element-property :begin link)))))
  9773. (point))))
  9774. (unless (and (<= (point-min) destination)
  9775. (>= (point-max) destination))
  9776. (widen))
  9777. (goto-char destination))))
  9778. (t (browse-url-at-point))))))
  9779. ;; On a footnote reference or at a footnote definition's label.
  9780. ((or (eq type 'footnote-reference)
  9781. (and (eq type 'footnote-definition)
  9782. (save-excursion
  9783. ;; Do not validate action when point is on the
  9784. ;; spaces right after the footnote label, in
  9785. ;; order to be on par with behaviour on links.
  9786. (skip-chars-forward " \t")
  9787. (let ((begin
  9788. (org-element-property :contents-begin context)))
  9789. (if begin (< (point) begin)
  9790. (= (org-element-property :post-affiliated context)
  9791. (line-beginning-position)))))))
  9792. (org-footnote-action))
  9793. (t (user-error "No link found")))))
  9794. (run-hook-with-args 'org-follow-link-hook)))
  9795. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9796. "Offer links in the current entry and return the selected link.
  9797. If there is only one link, return it.
  9798. If NTH is an integer, return the NTH link found.
  9799. If ZERO is a string, check also this string for a link, and if
  9800. there is one, return it."
  9801. (with-current-buffer buffer
  9802. (save-excursion
  9803. (save-restriction
  9804. (widen)
  9805. (goto-char marker)
  9806. (let ((cnt ?0)
  9807. have-zero end links link c)
  9808. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9809. (push (match-string 0 zero) links)
  9810. (setq cnt (1- cnt) have-zero t))
  9811. (save-excursion
  9812. (org-back-to-heading t)
  9813. (setq end (save-excursion (outline-next-heading) (point)))
  9814. (while (re-search-forward org-any-link-re end t)
  9815. (push (match-string 0) links))
  9816. (setq links (org-uniquify (reverse links))))
  9817. (cond
  9818. ((null links)
  9819. (message "No links"))
  9820. ((equal (length links) 1)
  9821. (setq link (car links)))
  9822. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9823. (setq link (nth (if have-zero nth (1- nth)) links)))
  9824. (t ; we have to select a link
  9825. (save-excursion
  9826. (save-window-excursion
  9827. (delete-other-windows)
  9828. (with-output-to-temp-buffer "*Select Link*"
  9829. (dolist (l links)
  9830. (cond
  9831. ((not (string-match org-bracket-link-regexp l))
  9832. (princ (format "[%c] %s\n" (cl-incf cnt)
  9833. (org-remove-angle-brackets l))))
  9834. ((match-end 3)
  9835. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9836. (match-string 3 l) (match-string 1 l))))
  9837. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9838. (match-string 1 l)))))))
  9839. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9840. (message "Select link to open, RET to open all:")
  9841. (setq c (read-char-exclusive))
  9842. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9843. (when (equal c ?q) (user-error "Abort"))
  9844. (if (equal c ?\C-m)
  9845. (setq link links)
  9846. (setq nth (- c ?0))
  9847. (when have-zero (setq nth (1+ nth)))
  9848. (unless (and (integerp nth) (>= (length links) nth))
  9849. (user-error "Invalid link selection"))
  9850. (setq link (nth (1- nth) links)))))
  9851. (cons link end))))))
  9852. ;; TODO: These functions are deprecated since `org-open-at-point'
  9853. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9854. (defun org-open-file-with-system (path)
  9855. "Open file at PATH using the system way of opening it."
  9856. (org-open-file path 'system))
  9857. (defun org-open-file-with-emacs (path)
  9858. "Open file at PATH in Emacs."
  9859. (org-open-file path 'emacs))
  9860. ;;; File search
  9861. (defvar org-create-file-search-functions nil
  9862. "List of functions to construct the right search string for a file link.
  9863. These functions are called in turn with point at the location to
  9864. which the link should point.
  9865. A function in the hook should first test if it would like to
  9866. handle this file type, for example by checking the `major-mode'
  9867. or the file extension. If it decides not to handle this file, it
  9868. should just return nil to give other functions a chance. If it
  9869. does handle the file, it must return the search string to be used
  9870. when following the link. The search string will be part of the
  9871. file link, given after a double colon, and `org-open-at-point'
  9872. will automatically search for it. If special measures must be
  9873. taken to make the search successful, another function should be
  9874. added to the companion hook `org-execute-file-search-functions',
  9875. which see.
  9876. A function in this hook may also use `setq' to set the variable
  9877. `description' to provide a suggestion for the descriptive text to
  9878. be used for this link when it gets inserted into an Org-mode
  9879. buffer with \\[org-insert-link].")
  9880. (defvar org-execute-file-search-functions nil
  9881. "List of functions to execute a file search triggered by a link.
  9882. Functions added to this hook must accept a single argument, the
  9883. search string that was part of the file link, the part after the
  9884. double colon. The function must first check if it would like to
  9885. handle this search, for example by checking the `major-mode' or
  9886. the file extension. If it decides not to handle this search, it
  9887. should just return nil to give other functions a chance. If it
  9888. does handle the search, it must return a non-nil value to keep
  9889. other functions from trying.
  9890. Each function can access the current prefix argument through the
  9891. variable `current-prefix-arg'. Note that a single prefix is used
  9892. to force opening a link in Emacs, so it may be good to only use a
  9893. numeric or double prefix to guide the search function.
  9894. In case this is needed, a function in this hook can also restore
  9895. the window configuration before `org-open-at-point' was called using:
  9896. \(set-window-configuration org-window-config-before-follow-link)")
  9897. (defun org-search-radio-target (target)
  9898. "Search a radio target matching TARGET in current buffer.
  9899. White spaces are not significant."
  9900. (let ((re (format "<<<%s>>>"
  9901. (mapconcat #'regexp-quote
  9902. (org-split-string target "[ \t\n]+")
  9903. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9904. (origin (point)))
  9905. (goto-char (point-min))
  9906. (catch :radio-match
  9907. (while (re-search-forward re nil t)
  9908. (backward-char)
  9909. (let ((object (org-element-context)))
  9910. (when (eq (org-element-type object) 'radio-target)
  9911. (goto-char (org-element-property :begin object))
  9912. (org-show-context 'link-search)
  9913. (throw :radio-match nil))))
  9914. (goto-char origin)
  9915. (user-error "No match for radio target: %s" target))))
  9916. (defun org-link-search (s &optional avoid-pos stealth)
  9917. "Search for a search string S.
  9918. If S starts with \"#\", it triggers a custom ID search.
  9919. If S is enclosed within parenthesis, it initiates a coderef
  9920. search.
  9921. If S is surrounded by forward slashes, it is interpreted as
  9922. a regular expression. In Org mode files, this will create an
  9923. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9924. to list matches. If the current buffer is in `dired-mode', grep
  9925. will be used to search in all files.
  9926. When AVOID-POS is given, ignore matches near that position.
  9927. When optional argument STEALTH is non-nil, do not modify
  9928. visibility around point, thus ignoring `org-show-context-detail'
  9929. variable.
  9930. Search is case-insensitive and ignores white spaces. Return type
  9931. of matched result, with is either `dedicated' or `fuzzy'."
  9932. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9933. (let* ((case-fold-search t)
  9934. (origin (point))
  9935. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9936. (words (org-split-string s "[ \t\n]+"))
  9937. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9938. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9939. type)
  9940. (cond
  9941. ;; Check if there are any special search functions.
  9942. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9943. ((eq (string-to-char s) ?#)
  9944. ;; Look for a custom ID S if S starts with "#".
  9945. (let* ((id (substring normalized 1))
  9946. (match (org-find-property "CUSTOM_ID" id)))
  9947. (if match (progn (goto-char match) (setf type 'dedicated))
  9948. (error "No match for custom ID: %s" id))))
  9949. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9950. ;; Look for coderef targets if S is enclosed within parenthesis.
  9951. (let ((coderef (match-string-no-properties 1 normalized))
  9952. (re (substring s-single-re 1 -1)))
  9953. (goto-char (point-min))
  9954. (catch :coderef-match
  9955. (while (re-search-forward re nil t)
  9956. (let ((element (org-element-at-point)))
  9957. (when (and (memq (org-element-type element)
  9958. '(example-block src-block))
  9959. ;; Build proper regexp according to current
  9960. ;; block's label format.
  9961. (let ((label-fmt
  9962. (regexp-quote
  9963. (or (org-element-property :label-fmt element)
  9964. org-coderef-label-format))))
  9965. (save-excursion
  9966. (beginning-of-line)
  9967. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9968. (format label-fmt coderef))))))
  9969. (setq type 'dedicated)
  9970. (goto-char (match-beginning 1))
  9971. (throw :coderef-match nil))))
  9972. (goto-char origin)
  9973. (error "No match for coderef: %s" coderef))))
  9974. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9975. ;; Look for a regular expression.
  9976. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9977. (match-string 1 s)))
  9978. ;; Fuzzy links.
  9979. (t
  9980. (let ((starred (eq (string-to-char normalized) ?*)))
  9981. (cond
  9982. ;; Look for targets, only if not in a headline search.
  9983. ((and (not starred)
  9984. (let ((target (format "<<%s>>" s-multi-re)))
  9985. (catch :target-match
  9986. (goto-char (point-min))
  9987. (while (re-search-forward target nil t)
  9988. (backward-char)
  9989. (let ((context (org-element-context)))
  9990. (when (eq (org-element-type context) 'target)
  9991. (setq type 'dedicated)
  9992. (goto-char (org-element-property :begin context))
  9993. (throw :target-match t))))
  9994. nil))))
  9995. ;; Look for elements named after S, only if not in a headline
  9996. ;; search.
  9997. ((and (not starred)
  9998. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9999. (catch :name-match
  10000. (goto-char (point-min))
  10001. (while (re-search-forward name nil t)
  10002. (let ((element (org-element-at-point)))
  10003. (when (equal (org-split-string
  10004. (org-element-property :name element)
  10005. "[ \t]+")
  10006. words)
  10007. (setq type 'dedicated)
  10008. (beginning-of-line)
  10009. (throw :name-match t))))
  10010. nil))))
  10011. ;; Regular text search. Prefer headlines in Org mode
  10012. ;; buffers.
  10013. ((and (derived-mode-p 'org-mode)
  10014. (let* ((wspace "[ \t]")
  10015. (wspaceopt (concat wspace "*"))
  10016. (cookie (concat "\\(?:"
  10017. wspaceopt
  10018. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10019. wspaceopt
  10020. "\\)"))
  10021. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10022. (re (concat
  10023. org-outline-regexp-bol
  10024. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10025. "\\(?:\\[#.\\][ \t]+\\)?"
  10026. "\\(?:" org-comment-string "[ \t]+\\)?"
  10027. sep "?"
  10028. (let ((title (mapconcat #'regexp-quote
  10029. words
  10030. sep)))
  10031. (if starred (substring title 1) title))
  10032. sep "?"
  10033. "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?"
  10034. "[ \t]*$")))
  10035. (goto-char (point-min))
  10036. (re-search-forward re nil t)))
  10037. (goto-char (match-beginning 0))
  10038. (setq type 'dedicated))
  10039. ;; Offer to create non-existent headline depending on
  10040. ;; `org-link-search-must-match-exact-headline'.
  10041. ((and (derived-mode-p 'org-mode)
  10042. (not org-link-search-inhibit-query)
  10043. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10044. (yes-or-no-p "No match - create this as a new heading? "))
  10045. (goto-char (point-max))
  10046. (unless (bolp) (newline))
  10047. (org-insert-heading nil t t)
  10048. (insert s "\n")
  10049. (beginning-of-line 0))
  10050. ;; Only headlines are looked after. No need to process
  10051. ;; further: throw an error.
  10052. ((and (derived-mode-p 'org-mode)
  10053. (or starred org-link-search-must-match-exact-headline))
  10054. (goto-char origin)
  10055. (error "No match for fuzzy expression: %s" normalized))
  10056. ;; Regular text search.
  10057. ((catch :fuzzy-match
  10058. (goto-char (point-min))
  10059. (while (re-search-forward s-multi-re nil t)
  10060. ;; Skip match if it contains AVOID-POS or it is included
  10061. ;; in a link with a description but outside the
  10062. ;; description.
  10063. (unless (or (and avoid-pos
  10064. (<= (match-beginning 0) avoid-pos)
  10065. (> (match-end 0) avoid-pos))
  10066. (and (save-match-data
  10067. (org-in-regexp org-bracket-link-regexp))
  10068. (match-beginning 3)
  10069. (or (> (match-beginning 3) (point))
  10070. (<= (match-end 3) (point)))
  10071. (org-element-lineage
  10072. (save-match-data (org-element-context))
  10073. '(link) t)))
  10074. (goto-char (match-beginning 0))
  10075. (setq type 'fuzzy)
  10076. (throw :fuzzy-match t)))
  10077. nil))
  10078. ;; All failed. Throw an error.
  10079. (t (goto-char origin)
  10080. (error "No match for fuzzy expression: %s" normalized))))))
  10081. ;; Disclose surroundings of match, if appropriate.
  10082. (when (and (derived-mode-p 'org-mode) (not stealth))
  10083. (org-show-context 'link-search))
  10084. type))
  10085. (defun org-get-buffer-for-internal-link (buffer)
  10086. "Return a buffer to be used for displaying the link target of internal links."
  10087. (cond
  10088. ((not org-display-internal-link-with-indirect-buffer)
  10089. buffer)
  10090. ((string-match "(Clone)$" (buffer-name buffer))
  10091. (message "Buffer is already a clone, not making another one")
  10092. ;; we also do not modify visibility in this case
  10093. buffer)
  10094. (t ; make a new indirect buffer for displaying the link
  10095. (let* ((bn (buffer-name buffer))
  10096. (ibn (concat bn "(Clone)"))
  10097. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10098. (with-current-buffer ib (org-overview))
  10099. ib))))
  10100. (defun org-do-occur (regexp &optional cleanup)
  10101. "Call the Emacs command `occur'.
  10102. If CLEANUP is non-nil, remove the printout of the regular expression
  10103. in the *Occur* buffer. This is useful if the regex is long and not useful
  10104. to read."
  10105. (occur regexp)
  10106. (when cleanup
  10107. (let ((cwin (selected-window)) win beg end)
  10108. (when (setq win (get-buffer-window "*Occur*"))
  10109. (select-window win))
  10110. (goto-char (point-min))
  10111. (when (re-search-forward "match[a-z]+" nil t)
  10112. (setq beg (match-end 0))
  10113. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10114. (setq end (1- (match-beginning 0)))))
  10115. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10116. (goto-char (point-min))
  10117. (select-window cwin))))
  10118. ;;; The mark ring for links jumps
  10119. (defvar org-mark-ring nil
  10120. "Mark ring for positions before jumps in Org-mode.")
  10121. (defvar org-mark-ring-last-goto nil
  10122. "Last position in the mark ring used to go back.")
  10123. ;; Fill and close the ring
  10124. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10125. (dotimes (_ org-mark-ring-length)
  10126. (push (make-marker) org-mark-ring))
  10127. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10128. org-mark-ring)
  10129. (defun org-mark-ring-push (&optional pos buffer)
  10130. "Put the current position or POS into the mark ring and rotate it."
  10131. (interactive)
  10132. (setq pos (or pos (point)))
  10133. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10134. (move-marker (car org-mark-ring)
  10135. (or pos (point))
  10136. (or buffer (current-buffer)))
  10137. (message "%s"
  10138. (substitute-command-keys
  10139. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10140. (defun org-mark-ring-goto (&optional n)
  10141. "Jump to the previous position in the mark ring.
  10142. With prefix arg N, jump back that many stored positions. When
  10143. called several times in succession, walk through the entire ring.
  10144. Org-mode commands jumping to a different position in the current file,
  10145. or to another Org-mode file, automatically push the old position
  10146. onto the ring."
  10147. (interactive "p")
  10148. (let (p m)
  10149. (if (eq last-command this-command)
  10150. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10151. (setq p org-mark-ring))
  10152. (setq org-mark-ring-last-goto p)
  10153. (setq m (car p))
  10154. (org-pop-to-buffer-same-window (marker-buffer m))
  10155. (goto-char m)
  10156. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10157. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10158. ;;; Emacs 24.4+
  10159. (defun org-remove-angle-brackets (s)
  10160. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10161. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10162. s)
  10163. (defun org-add-angle-brackets (s)
  10164. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10165. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10166. s)
  10167. (defun org-remove-double-quotes (s)
  10168. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10169. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10170. s)
  10171. ;;; Following specific links
  10172. (defvar org-agenda-buffer-tmp-name)
  10173. (defvar org-agenda-start-on-weekday)
  10174. (defun org-follow-timestamp-link ()
  10175. "Open an agenda view for the time-stamp date/range at point."
  10176. (cond
  10177. ((org-at-date-range-p t)
  10178. (let ((org-agenda-start-on-weekday)
  10179. (t1 (match-string 1))
  10180. (t2 (match-string 2)) tt1 tt2)
  10181. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10182. tt2 (time-to-days (org-time-string-to-time t2)))
  10183. (let ((org-agenda-buffer-tmp-name
  10184. (format "*Org Agenda(a:%s)"
  10185. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10186. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10187. ((org-at-timestamp-p t)
  10188. (let ((org-agenda-buffer-tmp-name
  10189. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10190. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10191. (substring (match-string 1) 0 10)))
  10192. 1)))
  10193. (t (error "This should not happen"))))
  10194. ;;; Following file links
  10195. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10196. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10197. (declare-function mailcap-mime-info
  10198. "mailcap" (string &optional request no-decode))
  10199. (defvar org-wait nil)
  10200. (defun org-open-file (path &optional in-emacs line search)
  10201. "Open the file at PATH.
  10202. First, this expands any special file name abbreviations. Then the
  10203. configuration variable `org-file-apps' is checked if it contains an
  10204. entry for this file type, and if yes, the corresponding command is launched.
  10205. If no application is found, Emacs simply visits the file.
  10206. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10207. With a double \\[universal-argument] \\[universal-argument] \
  10208. prefix arg, Org tries to avoid opening in Emacs
  10209. and to use an external application to visit the file.
  10210. Optional LINE specifies a line to go to, optional SEARCH a string
  10211. to search for. If LINE or SEARCH is given, the file will be
  10212. opened in Emacs, unless an entry from org-file-apps that makes
  10213. use of groups in a regexp matches.
  10214. If you want to change the way frames are used when following a
  10215. link, please customize `org-link-frame-setup'.
  10216. If the file does not exist, an error is thrown."
  10217. (let* ((file (if (equal path "")
  10218. buffer-file-name
  10219. (substitute-in-file-name (expand-file-name path))))
  10220. (file-apps (append org-file-apps (org-default-apps)))
  10221. (apps (cl-remove-if
  10222. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10223. (apps-dlink (cl-remove-if-not
  10224. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10225. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10226. (dirp (unless remp (file-directory-p file)))
  10227. (file (if (and dirp org-open-directory-means-index-dot-org)
  10228. (concat (file-name-as-directory file) "index.org")
  10229. file))
  10230. (a-m-a-p (assq 'auto-mode apps))
  10231. (dfile (downcase file))
  10232. ;; Reconstruct the original link from the PATH, LINE and
  10233. ;; SEARCH args.
  10234. (link (cond (line (concat file "::" (number-to-string line)))
  10235. (search (concat file "::" search))
  10236. (t file)))
  10237. (dlink (downcase link))
  10238. (old-buffer (current-buffer))
  10239. (old-pos (point))
  10240. (old-mode major-mode)
  10241. (ext
  10242. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10243. (match-string 1 dfile)))
  10244. cmd link-match-data)
  10245. (cond
  10246. ((member in-emacs '((16) system))
  10247. (setq cmd (cdr (assq 'system apps))))
  10248. (in-emacs (setq cmd 'emacs))
  10249. (t
  10250. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10251. (and dirp (cdr (assq 'directory apps)))
  10252. ;; First, try matching against apps-dlink if we
  10253. ;; get a match here, store the match data for
  10254. ;; later.
  10255. (let ((match (assoc-default dlink apps-dlink
  10256. 'string-match)))
  10257. (if match
  10258. (progn (setq link-match-data (match-data))
  10259. match)
  10260. (progn (setq in-emacs (or in-emacs line search))
  10261. nil))) ; if we have no match in apps-dlink,
  10262. ; always open the file in emacs if line or search
  10263. ; is given (for backwards compatibility)
  10264. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10265. 'string-match)
  10266. (cdr (assoc ext apps))
  10267. (cdr (assq t apps))))))
  10268. (when (eq cmd 'system)
  10269. (setq cmd (cdr (assoc 'system apps))))
  10270. (when (eq cmd 'default)
  10271. (setq cmd (cdr (assoc t apps))))
  10272. (when (eq cmd 'mailcap)
  10273. (require 'mailcap)
  10274. (mailcap-parse-mailcaps)
  10275. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10276. (command (mailcap-mime-info mime-type)))
  10277. (if (stringp command)
  10278. (setq cmd command)
  10279. (setq cmd 'emacs))))
  10280. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10281. (not (file-exists-p file))
  10282. (not org-open-non-existing-files))
  10283. (user-error "No such file: %s" file))
  10284. (cond
  10285. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10286. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10287. (while (string-match "['\"]%s['\"]" cmd)
  10288. (setq cmd (replace-match "%s" t t cmd)))
  10289. (while (string-match "%s" cmd)
  10290. (setq cmd (replace-match
  10291. (save-match-data
  10292. (shell-quote-argument
  10293. (convert-standard-filename file)))
  10294. t t cmd)))
  10295. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10296. (save-match-data
  10297. (let ((match-index 1)
  10298. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10299. (set-match-data link-match-data)
  10300. (while (<= match-index number-of-groups)
  10301. (let ((regex (concat "%" (number-to-string match-index)))
  10302. (replace-with (match-string match-index dlink)))
  10303. (while (string-match regex cmd)
  10304. (setq cmd (replace-match replace-with t t cmd))))
  10305. (setq match-index (+ match-index 1)))))
  10306. (save-window-excursion
  10307. (message "Running %s...done" cmd)
  10308. (start-process-shell-command cmd nil cmd)
  10309. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10310. ((or (stringp cmd)
  10311. (eq cmd 'emacs))
  10312. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10313. (widen)
  10314. (cond (line (org-goto-line line)
  10315. (when (derived-mode-p 'org-mode) (org-reveal)))
  10316. (search (org-link-search search))))
  10317. ((functionp cmd)
  10318. (save-match-data
  10319. (set-match-data link-match-data)
  10320. (condition-case nil
  10321. (funcall cmd file link)
  10322. ;; FIXME: Remove this check when most default installations
  10323. ;; of Emacs have at least Org 9.0.
  10324. ((debug wrong-number-of-arguments wrong-type-argument
  10325. invalid-function)
  10326. (user-error "Please see Org News for version 9.0 about \
  10327. `org-file-apps'--Lisp error: %S" cmd)))))
  10328. ((consp cmd)
  10329. ;; FIXME: Remove this check when most default installations of
  10330. ;; Emacs have at least Org 9.0.
  10331. ;; Heads-up instead of silently fall back to
  10332. ;; `org-link-frame-setup' for an old usage of `org-file-apps'
  10333. ;; with sexp instead of a function for `cmd'.
  10334. (user-error "Please see Org News for version 9.0 about \
  10335. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10336. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10337. (and (derived-mode-p 'org-mode)
  10338. (eq old-mode 'org-mode)
  10339. (or (not (eq old-buffer (current-buffer)))
  10340. (not (eq old-pos (point))))
  10341. (org-mark-ring-push old-pos old-buffer))))
  10342. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10343. "This function returns non-nil if `entry' uses a regular
  10344. expression which should be matched against the whole link by
  10345. org-open-file.
  10346. It assumes that is the case when the entry uses a regular
  10347. expression which has at least one grouping construct and the
  10348. action is either a lisp form or a command string containing
  10349. `%1', i.e. using at least one subexpression match as a
  10350. parameter."
  10351. (let ((selector (car entry))
  10352. (action (cdr entry)))
  10353. (if (stringp selector)
  10354. (and (> (regexp-opt-depth selector) 0)
  10355. (or (and (stringp action)
  10356. (string-match "%[0-9]" action))
  10357. (consp action)))
  10358. nil)))
  10359. (defun org-default-apps ()
  10360. "Return the default applications for this operating system."
  10361. (cond
  10362. ((eq system-type 'darwin)
  10363. org-file-apps-defaults-macosx)
  10364. ((eq system-type 'windows-nt)
  10365. org-file-apps-defaults-windowsnt)
  10366. (t org-file-apps-defaults-gnu)))
  10367. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10368. "Convert extensions to regular expressions in the cars of LIST.
  10369. Also, weed out any non-string entries, because the return value is used
  10370. only for regexp matching.
  10371. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10372. point to the symbol `emacs', indicating that the file should
  10373. be opened in Emacs."
  10374. (append
  10375. (delq nil
  10376. (mapcar (lambda (x)
  10377. (unless (not (stringp (car x)))
  10378. (if (string-match "\\W" (car x))
  10379. x
  10380. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10381. list))
  10382. (when add-auto-mode
  10383. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10384. (defvar ange-ftp-name-format)
  10385. (defun org-file-remote-p (file)
  10386. "Test whether FILE specifies a location on a remote system.
  10387. Return non-nil if the location is indeed remote.
  10388. For example, the filename \"/user@host:/foo\" specifies a location
  10389. on the system \"/user@host:\"."
  10390. (cond ((fboundp 'file-remote-p)
  10391. (file-remote-p file))
  10392. ((fboundp 'tramp-handle-file-remote-p)
  10393. (tramp-handle-file-remote-p file))
  10394. ((and (boundp 'ange-ftp-name-format)
  10395. (string-match (car ange-ftp-name-format) file))
  10396. t)))
  10397. ;;;; Refiling
  10398. (defun org-get-org-file ()
  10399. "Read a filename, with default directory `org-directory'."
  10400. (let ((default (or org-default-notes-file remember-data-file)))
  10401. (read-file-name (format "File name [%s]: " default)
  10402. (file-name-as-directory org-directory)
  10403. default)))
  10404. (defun org-notes-order-reversed-p ()
  10405. "Check if the current file should receive notes in reversed order."
  10406. (cond
  10407. ((not org-reverse-note-order) nil)
  10408. ((eq t org-reverse-note-order) t)
  10409. ((not (listp org-reverse-note-order)) nil)
  10410. (t (catch 'exit
  10411. (dolist (entry org-reverse-note-order)
  10412. (when (string-match (car entry) buffer-file-name)
  10413. (throw 'exit (cdr entry))))))))
  10414. (defvar org-refile-target-table nil
  10415. "The list of refile targets, created by `org-refile'.")
  10416. (defvar org-agenda-new-buffers nil
  10417. "Buffers created to visit agenda files.")
  10418. (defvar org-refile-cache nil
  10419. "Cache for refile targets.")
  10420. (defvar org-refile-markers nil
  10421. "All the markers used for caching refile locations.")
  10422. (defun org-refile-marker (pos)
  10423. "Get a new refile marker, but only if caching is in use."
  10424. (if (not org-refile-use-cache)
  10425. pos
  10426. (let ((m (make-marker)))
  10427. (move-marker m pos)
  10428. (push m org-refile-markers)
  10429. m)))
  10430. (defun org-refile-cache-clear ()
  10431. "Clear the refile cache and disable all the markers."
  10432. (dolist (m org-refile-markers) (move-marker m nil))
  10433. (setq org-refile-markers nil)
  10434. (setq org-refile-cache nil)
  10435. (message "Refile cache has been cleared"))
  10436. (defun org-refile-cache-check-set (set)
  10437. "Check if all the markers in the cache still have live buffers."
  10438. (let (marker)
  10439. (catch 'exit
  10440. (while (and set (setq marker (nth 3 (pop set))))
  10441. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10442. (when (and marker (null (marker-buffer marker)))
  10443. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10444. (sit-for 3)
  10445. (throw 'exit nil)))
  10446. t)))
  10447. (defun org-refile-cache-put (set &rest identifiers)
  10448. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10449. (let* ((key (sha1 (prin1-to-string identifiers)))
  10450. (entry (assoc key org-refile-cache)))
  10451. (if entry
  10452. (setcdr entry set)
  10453. (push (cons key set) org-refile-cache))))
  10454. (defun org-refile-cache-get (&rest identifiers)
  10455. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10456. (cond
  10457. ((not org-refile-cache) nil)
  10458. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10459. (t
  10460. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10461. org-refile-cache))))
  10462. (and set (org-refile-cache-check-set set) set)))))
  10463. (defvar org-outline-path-cache nil
  10464. "Alist between buffer positions and outline paths.
  10465. It value is an alist (POSITION . PATH) where POSITION is the
  10466. buffer position at the beginning of an entry and PATH is a list
  10467. of strings describing the outline path for that entry, in reverse
  10468. order.")
  10469. (defun org-refile-get-targets (&optional default-buffer)
  10470. "Produce a table with refile targets."
  10471. (let ((case-fold-search nil)
  10472. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10473. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10474. targets tgs files desc descre)
  10475. (message "Getting targets...")
  10476. (with-current-buffer (or default-buffer (current-buffer))
  10477. (dolist (entry entries)
  10478. (setq files (car entry) desc (cdr entry))
  10479. (cond
  10480. ((null files) (setq files (list (current-buffer))))
  10481. ((eq files 'org-agenda-files)
  10482. (setq files (org-agenda-files 'unrestricted)))
  10483. ((and (symbolp files) (fboundp files))
  10484. (setq files (funcall files)))
  10485. ((and (symbolp files) (boundp files))
  10486. (setq files (symbol-value files))))
  10487. (when (stringp files) (setq files (list files)))
  10488. (cond
  10489. ((eq (car desc) :tag)
  10490. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10491. ((eq (car desc) :todo)
  10492. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10493. ((eq (car desc) :regexp)
  10494. (setq descre (cdr desc)))
  10495. ((eq (car desc) :level)
  10496. (setq descre (concat "^\\*\\{" (number-to-string
  10497. (if org-odd-levels-only
  10498. (1- (* 2 (cdr desc)))
  10499. (cdr desc)))
  10500. "\\}[ \t]")))
  10501. ((eq (car desc) :maxlevel)
  10502. (setq descre (concat "^\\*\\{1," (number-to-string
  10503. (if org-odd-levels-only
  10504. (1- (* 2 (cdr desc)))
  10505. (cdr desc)))
  10506. "\\}[ \t]")))
  10507. (t (error "Bad refiling target description %s" desc)))
  10508. (dolist (f files)
  10509. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10510. (or
  10511. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10512. (progn
  10513. (when (bufferp f)
  10514. (setq f (buffer-file-name (buffer-base-buffer f))))
  10515. (setq f (and f (expand-file-name f)))
  10516. (when (eq org-refile-use-outline-path 'file)
  10517. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10518. (org-with-wide-buffer
  10519. (goto-char (point-min))
  10520. (setq org-outline-path-cache nil)
  10521. (while (re-search-forward descre nil t)
  10522. (beginning-of-line)
  10523. (looking-at org-complex-heading-regexp)
  10524. (let ((begin (point))
  10525. (heading (match-string-no-properties 4)))
  10526. (unless (or (and
  10527. org-refile-target-verify-function
  10528. (not
  10529. (funcall org-refile-target-verify-function)))
  10530. (not heading))
  10531. (let ((re (format org-complex-heading-regexp-format
  10532. (regexp-quote heading)))
  10533. (target
  10534. (if (not org-refile-use-outline-path) heading
  10535. (mapconcat
  10536. #'org-protect-slash
  10537. (append
  10538. (pcase org-refile-use-outline-path
  10539. (`file (list (file-name-nondirectory
  10540. (buffer-file-name
  10541. (buffer-base-buffer)))))
  10542. (`full-file-path
  10543. (list (buffer-file-name
  10544. (buffer-base-buffer))))
  10545. (_ nil))
  10546. (org-get-outline-path t t))
  10547. "/"))))
  10548. (push (list target f re (org-refile-marker (point)))
  10549. tgs)))
  10550. (when (= (point) begin)
  10551. ;; Verification function has not moved point.
  10552. (end-of-line)))))))
  10553. (when org-refile-use-cache
  10554. (org-refile-cache-put tgs (buffer-file-name) descre))
  10555. (setq targets (append tgs targets))))))
  10556. (message "Getting targets...done")
  10557. (nreverse targets)))
  10558. (defun org-protect-slash (s)
  10559. (while (string-match "/" s)
  10560. (setq s (replace-match "\\" t t s)))
  10561. s)
  10562. (defun org--get-outline-path-1 (&optional use-cache)
  10563. "Return outline path to current headline.
  10564. Outline path is a list of strings, in reverse order. When
  10565. optional argument USE-CACHE is non-nil, make use of a cache. See
  10566. `org-get-outline-path' for details.
  10567. Assume buffer is widened and point is on a headline."
  10568. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10569. (let ((p (point))
  10570. (heading (progn
  10571. (looking-at org-complex-heading-regexp)
  10572. (if (not (match-end 4)) ""
  10573. ;; Remove statistics cookies.
  10574. (org-trim
  10575. (org-link-display-format
  10576. (replace-regexp-in-string
  10577. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10578. (match-string-no-properties 4))))))))
  10579. (if (org-up-heading-safe)
  10580. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10581. (when use-cache
  10582. (push (cons p path) org-outline-path-cache))
  10583. path)
  10584. ;; This is a new root node. Since we assume we are moving
  10585. ;; forward, we can drop previous cache so as to limit number
  10586. ;; of associations there.
  10587. (let ((path (list heading)))
  10588. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10589. path)))))
  10590. (defun org-get-outline-path (&optional with-self use-cache)
  10591. "Return the outline path to the current entry.
  10592. An outline path is a list of ancestors for current headline, as
  10593. a list of strings. Statistics cookies are removed and links are
  10594. replaced with their description, if any, or their path otherwise.
  10595. When optional argument WITH-SELF is non-nil, the path also
  10596. includes the current headline.
  10597. When optional argument USE-CACHE is non-nil, cache outline paths
  10598. between calls to this function so as to avoid backtracking. This
  10599. argument is useful when planning to find more than one outline
  10600. path in the same document. In that case, there are two
  10601. conditions to satisfy:
  10602. - `org-outline-path-cache' is set to nil before starting the
  10603. process;
  10604. - outline paths are computed by increasing buffer positions."
  10605. (org-with-wide-buffer
  10606. (and (or (and with-self (org-back-to-heading t))
  10607. (org-up-heading-safe))
  10608. (reverse (org--get-outline-path-1 use-cache)))))
  10609. (defun org-format-outline-path (path &optional width prefix separator)
  10610. "Format the outline path PATH for display.
  10611. WIDTH is the maximum number of characters that is available.
  10612. PREFIX is a prefix to be included in the returned string,
  10613. such as the file name.
  10614. SEPARATOR is inserted between the different parts of the path,
  10615. the default is \"/\"."
  10616. (setq width (or width 79))
  10617. (setq path (delq nil path))
  10618. (unless (> width 0)
  10619. (user-error "Argument `width' must be positive"))
  10620. (setq separator (or separator "/"))
  10621. (let* ((org-odd-levels-only nil)
  10622. (fpath (concat
  10623. prefix (and prefix path separator)
  10624. (mapconcat
  10625. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10626. (cl-loop for head in path
  10627. for n from 0
  10628. collect (org-add-props
  10629. head nil 'face
  10630. (nth (% n org-n-level-faces) org-level-faces)))
  10631. separator))))
  10632. (when (> (length fpath) width)
  10633. (if (< width 7)
  10634. ;; It's unlikely that `width' will be this small, but don't
  10635. ;; waste characters by adding ".." if it is.
  10636. (setq fpath (substring fpath 0 width))
  10637. (setf (substring fpath (- width 2)) "..")))
  10638. fpath))
  10639. (defun org-display-outline-path (&optional file current separator just-return-string)
  10640. "Display the current outline path in the echo area.
  10641. If FILE is non-nil, prepend the output with the file name.
  10642. If CURRENT is non-nil, append the current heading to the output.
  10643. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10644. the different parts of the path and defaults to \"/\".
  10645. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10646. (interactive "P")
  10647. (let* (case-fold-search
  10648. (bfn (buffer-file-name (buffer-base-buffer)))
  10649. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10650. res)
  10651. (when current (setq path (append path
  10652. (save-excursion
  10653. (org-back-to-heading t)
  10654. (when (looking-at org-complex-heading-regexp)
  10655. (list (match-string 4)))))))
  10656. (setq res
  10657. (org-format-outline-path
  10658. path
  10659. (1- (frame-width))
  10660. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10661. separator))
  10662. (if just-return-string
  10663. (org-no-properties res)
  10664. (org-unlogged-message "%s" res))))
  10665. (defvar org-refile-history nil
  10666. "History for refiling operations.")
  10667. (defvar org-after-refile-insert-hook nil
  10668. "Hook run after `org-refile' has inserted its stuff at the new location.
  10669. Note that this is still *before* the stuff will be removed from
  10670. the *old* location.")
  10671. (defvar org-capture-last-stored-marker)
  10672. (defvar org-refile-keep nil
  10673. "Non-nil means `org-refile' will copy instead of refile.")
  10674. (defun org-copy ()
  10675. "Like `org-refile', but copy."
  10676. (interactive)
  10677. (let ((org-refile-keep t))
  10678. (funcall 'org-refile nil nil nil "Copy")))
  10679. (defun org-refile (&optional arg default-buffer rfloc msg)
  10680. "Move the entry or entries at point to another heading.
  10681. The list of target headings is compiled using the information in
  10682. `org-refile-targets', which see.
  10683. At the target location, the entry is filed as a subitem of the
  10684. target heading. Depending on `org-reverse-note-order', the new
  10685. subitem will either be the first or the last subitem.
  10686. If there is an active region, all entries in that region will be
  10687. refiled. However, the region must fulfill the requirement that
  10688. the first heading sets the top-level of the moved text.
  10689. With prefix arg ARG, the command will only visit the target
  10690. location and not actually move anything.
  10691. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10692. refiling operation has put the subtree.
  10693. With a numeric prefix argument of `2', refile to the running clock.
  10694. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10695. being set to t and copy to the target location, don't move it.
  10696. Beware that keeping refiled entries may result in duplicated ID
  10697. properties.
  10698. RFLOC can be a refile location obtained in a different way.
  10699. MSG is a string to replace \"Refile\" in the default prompt with
  10700. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10701. See also `org-refile-use-outline-path'.
  10702. If you are using target caching (see `org-refile-use-cache'), you
  10703. have to clear the target cache in order to find new targets.
  10704. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10705. prefix argument (`C-u C-u C-u C-c C-w')."
  10706. (interactive "P")
  10707. (if (member arg '(0 (64)))
  10708. (org-refile-cache-clear)
  10709. (let* ((actionmsg (cond (msg msg)
  10710. ((equal arg 3) "Refile (and keep)")
  10711. (t "Refile")))
  10712. (regionp (org-region-active-p))
  10713. (region-start (and regionp (region-beginning)))
  10714. (region-end (and regionp (region-end)))
  10715. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10716. pos it nbuf file level reversed)
  10717. (setq last-command nil)
  10718. (when regionp
  10719. (goto-char region-start)
  10720. (or (bolp) (goto-char (point-at-bol)))
  10721. (setq region-start (point))
  10722. (unless (or (org-kill-is-subtree-p
  10723. (buffer-substring region-start region-end))
  10724. (prog1 org-refile-active-region-within-subtree
  10725. (let ((s (point-at-eol)))
  10726. (org-toggle-heading)
  10727. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10728. (user-error "The region is not a (sequence of) subtree(s)")))
  10729. (if (equal arg '(16))
  10730. (org-refile-goto-last-stored)
  10731. (when (or
  10732. (and (equal arg 2)
  10733. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10734. (prog1
  10735. (setq it (list (or org-clock-heading "running clock")
  10736. (buffer-file-name
  10737. (marker-buffer org-clock-hd-marker))
  10738. ""
  10739. (marker-position org-clock-hd-marker)))
  10740. (setq arg nil)))
  10741. (setq it
  10742. (or rfloc
  10743. (let (heading-text)
  10744. (save-excursion
  10745. (unless (and arg (listp arg))
  10746. (org-back-to-heading t)
  10747. (setq heading-text
  10748. (replace-regexp-in-string
  10749. org-bracket-link-regexp
  10750. "\\3"
  10751. (nth 4 (org-heading-components)))))
  10752. (org-refile-get-location
  10753. (cond ((and arg (listp arg)) "Goto")
  10754. (regionp (concat actionmsg " region to"))
  10755. (t (concat actionmsg " subtree \""
  10756. heading-text "\" to")))
  10757. default-buffer
  10758. (and (not (equal '(4) arg))
  10759. org-refile-allow-creating-parent-nodes)))))))
  10760. (setq file (nth 1 it)
  10761. pos (nth 3 it))
  10762. (when (and (not arg)
  10763. pos
  10764. (equal (buffer-file-name) file)
  10765. (if regionp
  10766. (and (>= pos region-start)
  10767. (<= pos region-end))
  10768. (and (>= pos (point))
  10769. (< pos (save-excursion
  10770. (org-end-of-subtree t t))))))
  10771. (error "Cannot refile to position inside the tree or region"))
  10772. (setq nbuf (or (find-buffer-visiting file)
  10773. (find-file-noselect file)))
  10774. (if (and arg (not (equal arg 3)))
  10775. (progn
  10776. (org-pop-to-buffer-same-window nbuf)
  10777. (goto-char pos)
  10778. (org-show-context 'org-goto))
  10779. (if regionp
  10780. (progn
  10781. (org-kill-new (buffer-substring region-start region-end))
  10782. (org-save-markers-in-region region-start region-end))
  10783. (org-copy-subtree 1 nil t))
  10784. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10785. (find-file-noselect file)))
  10786. (setq reversed (org-notes-order-reversed-p))
  10787. (save-excursion
  10788. (save-restriction
  10789. (widen)
  10790. (if pos
  10791. (progn
  10792. (goto-char pos)
  10793. (looking-at org-outline-regexp)
  10794. (setq level (org-get-valid-level (funcall outline-level) 1))
  10795. (goto-char
  10796. (if reversed
  10797. (or (outline-next-heading) (point-max))
  10798. (or (save-excursion (org-get-next-sibling))
  10799. (org-end-of-subtree t t)
  10800. (point-max)))))
  10801. (setq level 1)
  10802. (if (not reversed)
  10803. (goto-char (point-max))
  10804. (goto-char (point-min))
  10805. (or (outline-next-heading) (goto-char (point-max)))))
  10806. (unless (bolp) (newline))
  10807. (org-paste-subtree level nil nil t)
  10808. (when org-log-refile
  10809. (org-add-log-setup 'refile nil nil org-log-refile)
  10810. (unless (eq org-log-refile 'note)
  10811. (save-excursion (org-add-log-note))))
  10812. (and org-auto-align-tags
  10813. (let ((org-loop-over-headlines-in-active-region nil))
  10814. (org-set-tags nil t)))
  10815. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10816. :last-refile)))
  10817. (when bookmark-name
  10818. (with-demoted-errors
  10819. (bookmark-set bookmark-name))))
  10820. ;; If we are refiling for capture, make sure that the
  10821. ;; last-capture pointers point here
  10822. (when (org-bound-and-true-p org-capture-is-refiling)
  10823. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10824. :last-capture-marker)))
  10825. (when bookmark-name
  10826. (with-demoted-errors
  10827. (bookmark-set bookmark-name))))
  10828. (move-marker org-capture-last-stored-marker (point)))
  10829. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10830. (run-hooks 'org-after-refile-insert-hook))))
  10831. (unless org-refile-keep
  10832. (if regionp
  10833. (delete-region (point) (+ (point) (- region-end region-start)))
  10834. (delete-region
  10835. (and (org-back-to-heading t) (point))
  10836. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10837. (when (featurep 'org-inlinetask)
  10838. (org-inlinetask-remove-END-maybe))
  10839. (setq org-markers-to-move nil)
  10840. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10841. (defun org-refile-goto-last-stored ()
  10842. "Go to the location where the last refile was stored."
  10843. (interactive)
  10844. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10845. (message "This is the location of the last refile"))
  10846. (defun org-refile--get-location (refloc tbl)
  10847. "When user refile to REFLOC, find the associated target in TBL.
  10848. Also check `org-refile-target-table'."
  10849. (car (delq
  10850. nil
  10851. (mapcar
  10852. (lambda (r) (or (assoc r tbl)
  10853. (assoc r org-refile-target-table)))
  10854. (list (replace-regexp-in-string "/$" "" refloc)
  10855. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10856. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  10857. "Prompt the user for a refile location, using PROMPT.
  10858. PROMPT should not be suffixed with a colon and a space, because
  10859. this function appends the default value from
  10860. `org-refile-history' automatically, if that is not empty."
  10861. (let ((org-refile-targets org-refile-targets)
  10862. (org-refile-use-outline-path org-refile-use-outline-path))
  10863. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  10864. (unless org-refile-target-table
  10865. (user-error "No refile targets"))
  10866. (let* ((cbuf (current-buffer))
  10867. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10868. (cfunc (if (and org-refile-use-outline-path
  10869. org-outline-path-complete-in-steps)
  10870. #'org-olpath-completing-read
  10871. #'completing-read))
  10872. (extra (if org-refile-use-outline-path "/" ""))
  10873. (cbnex (concat (buffer-name) extra))
  10874. (filename (and cfn (expand-file-name cfn)))
  10875. (tbl (mapcar
  10876. (lambda (x)
  10877. (if (and (not (member org-refile-use-outline-path
  10878. '(file full-file-path)))
  10879. (not (equal filename (nth 1 x))))
  10880. (cons (concat (car x) extra " ("
  10881. (file-name-nondirectory (nth 1 x)) ")")
  10882. (cdr x))
  10883. (cons (concat (car x) extra) (cdr x))))
  10884. org-refile-target-table))
  10885. (completion-ignore-case t)
  10886. cdef
  10887. (prompt (concat prompt
  10888. (or (and (car org-refile-history)
  10889. (concat " (default " (car org-refile-history) ")"))
  10890. (and (assoc cbnex tbl) (setq cdef cbnex)
  10891. (concat " (default " cbnex ")"))) ": "))
  10892. pa answ parent-target child parent old-hist)
  10893. (setq old-hist org-refile-history)
  10894. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10895. nil 'org-refile-history (or cdef (car org-refile-history))))
  10896. (if (setq pa (org-refile--get-location answ tbl))
  10897. (progn
  10898. (org-refile-check-position pa)
  10899. (when (or (not org-refile-history)
  10900. (not (eq old-hist org-refile-history))
  10901. (not (equal (car pa) (car org-refile-history))))
  10902. (setq org-refile-history
  10903. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10904. org-refile-history
  10905. (cdr org-refile-history))))
  10906. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10907. (pop org-refile-history)))
  10908. pa)
  10909. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10910. (progn
  10911. (setq parent (match-string 1 answ)
  10912. child (match-string 2 answ))
  10913. (setq parent-target (org-refile--get-location parent tbl))
  10914. (when (and parent-target
  10915. (or (eq new-nodes t)
  10916. (and (eq new-nodes 'confirm)
  10917. (y-or-n-p (format "Create new node \"%s\"? "
  10918. child)))))
  10919. (org-refile-new-child parent-target child)))
  10920. (user-error "Invalid target location")))))
  10921. (declare-function org-string-nw-p "org-macs" (s))
  10922. (defun org-refile-check-position (refile-pointer)
  10923. "Check if the refile pointer matches the headline to which it points."
  10924. (let* ((file (nth 1 refile-pointer))
  10925. (re (nth 2 refile-pointer))
  10926. (pos (nth 3 refile-pointer))
  10927. buffer)
  10928. (if (and (not (markerp pos)) (not file))
  10929. (user-error "Please indicate a target file in the refile path")
  10930. (when (org-string-nw-p re)
  10931. (setq buffer (if (markerp pos)
  10932. (marker-buffer pos)
  10933. (or (find-buffer-visiting file)
  10934. (find-file-noselect file))))
  10935. (with-current-buffer buffer
  10936. (save-excursion
  10937. (save-restriction
  10938. (widen)
  10939. (goto-char pos)
  10940. (beginning-of-line 1)
  10941. (unless (looking-at-p re)
  10942. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10943. (defun org-refile-new-child (parent-target child)
  10944. "Use refile target PARENT-TARGET to add new CHILD below it."
  10945. (unless parent-target
  10946. (error "Cannot find parent for new node"))
  10947. (let ((file (nth 1 parent-target))
  10948. (pos (nth 3 parent-target))
  10949. level)
  10950. (with-current-buffer (or (find-buffer-visiting file)
  10951. (find-file-noselect file))
  10952. (save-excursion
  10953. (save-restriction
  10954. (widen)
  10955. (if pos
  10956. (goto-char pos)
  10957. (goto-char (point-max))
  10958. (unless (bolp) (newline)))
  10959. (when (looking-at org-outline-regexp)
  10960. (setq level (funcall outline-level))
  10961. (org-end-of-subtree t t))
  10962. (org-back-over-empty-lines)
  10963. (insert "\n" (make-string
  10964. (if pos (org-get-valid-level level 1) 1) ?*)
  10965. " " child "\n")
  10966. (beginning-of-line 0)
  10967. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10968. (defun org-olpath-completing-read (prompt collection &rest args)
  10969. "Read an outline path like a file name."
  10970. (let ((thetable collection))
  10971. (apply #'completing-read
  10972. prompt
  10973. (lambda (string predicate &optional flag)
  10974. (cond
  10975. ((eq flag nil) (try-completion string thetable))
  10976. ((eq flag t)
  10977. (let ((l (length string)))
  10978. (mapcar (lambda (x)
  10979. (let ((r (substring x l))
  10980. (f (if (string-match " ([^)]*)$" x)
  10981. (match-string 0 x)
  10982. "")))
  10983. (if (string-match "/" r)
  10984. (concat string (substring r 0 (match-end 0)) f)
  10985. x)))
  10986. (all-completions string thetable predicate))))
  10987. ;; Exact match?
  10988. ((eq flag 'lambda) (assoc string thetable))))
  10989. args)))
  10990. ;;;; Dynamic blocks
  10991. (defun org-find-dblock (name)
  10992. "Find the first dynamic block with name NAME in the buffer.
  10993. If not found, stay at current position and return nil."
  10994. (let ((case-fold-search t) pos)
  10995. (save-excursion
  10996. (goto-char (point-min))
  10997. (setq pos (and (re-search-forward
  10998. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10999. (match-beginning 0))))
  11000. (when pos (goto-char pos))
  11001. pos))
  11002. (defun org-create-dblock (plist)
  11003. "Create a dynamic block section, with parameters taken from PLIST.
  11004. PLIST must contain a :name entry which is used as the name of the block."
  11005. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11006. (end-of-line 1)
  11007. (newline))
  11008. (let ((col (current-column))
  11009. (name (plist-get plist :name)))
  11010. (insert "#+BEGIN: " name)
  11011. (while plist
  11012. (if (eq (car plist) :name)
  11013. (setq plist (cddr plist))
  11014. (insert " " (prin1-to-string (pop plist)))))
  11015. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11016. (beginning-of-line -2)))
  11017. (defun org-prepare-dblock ()
  11018. "Prepare dynamic block for refresh.
  11019. This empties the block, puts the cursor at the insert position and returns
  11020. the property list including an extra property :name with the block name."
  11021. (unless (looking-at org-dblock-start-re)
  11022. (user-error "Not at a dynamic block"))
  11023. (let* ((begdel (1+ (match-end 0)))
  11024. (name (org-no-properties (match-string 1)))
  11025. (params (append (list :name name)
  11026. (read (concat "(" (match-string 3) ")")))))
  11027. (save-excursion
  11028. (beginning-of-line 1)
  11029. (skip-chars-forward " \t")
  11030. (setq params (plist-put params :indentation-column (current-column))))
  11031. (unless (re-search-forward org-dblock-end-re nil t)
  11032. (error "Dynamic block not terminated"))
  11033. (setq params
  11034. (append params
  11035. (list :content (buffer-substring
  11036. begdel (match-beginning 0)))))
  11037. (delete-region begdel (match-beginning 0))
  11038. (goto-char begdel)
  11039. (open-line 1)
  11040. params))
  11041. (defun org-map-dblocks (&optional command)
  11042. "Apply COMMAND to all dynamic blocks in the current buffer.
  11043. If COMMAND is not given, use `org-update-dblock'."
  11044. (let ((cmd (or command 'org-update-dblock)))
  11045. (save-excursion
  11046. (goto-char (point-min))
  11047. (while (re-search-forward org-dblock-start-re nil t)
  11048. (goto-char (match-beginning 0))
  11049. (save-excursion
  11050. (condition-case nil
  11051. (funcall cmd)
  11052. (error (message "Error during update of dynamic block"))))
  11053. (unless (re-search-forward org-dblock-end-re nil t)
  11054. (error "Dynamic block not terminated"))))))
  11055. (defun org-dblock-update (&optional arg)
  11056. "User command for updating dynamic blocks.
  11057. Update the dynamic block at point. With prefix ARG, update all dynamic
  11058. blocks in the buffer."
  11059. (interactive "P")
  11060. (if arg
  11061. (org-update-all-dblocks)
  11062. (or (looking-at org-dblock-start-re)
  11063. (org-beginning-of-dblock))
  11064. (org-update-dblock)))
  11065. (defun org-update-dblock ()
  11066. "Update the dynamic block at point.
  11067. This means to empty the block, parse for parameters and then call
  11068. the correct writing function."
  11069. (interactive)
  11070. (save-excursion
  11071. (let* ((win (selected-window))
  11072. (pos (point))
  11073. (line (org-current-line))
  11074. (params (org-prepare-dblock))
  11075. (name (plist-get params :name))
  11076. (indent (plist-get params :indentation-column))
  11077. (cmd (intern (concat "org-dblock-write:" name))))
  11078. (message "Updating dynamic block `%s' at line %d..." name line)
  11079. (funcall cmd params)
  11080. (message "Updating dynamic block `%s' at line %d...done" name line)
  11081. (goto-char pos)
  11082. (when (and indent (> indent 0))
  11083. (setq indent (make-string indent ?\ ))
  11084. (save-excursion
  11085. (select-window win)
  11086. (org-beginning-of-dblock)
  11087. (forward-line 1)
  11088. (while (not (looking-at org-dblock-end-re))
  11089. (insert indent)
  11090. (beginning-of-line 2))
  11091. (when (looking-at org-dblock-end-re)
  11092. (and (looking-at "[ \t]+")
  11093. (replace-match ""))
  11094. (insert indent)))))))
  11095. (defun org-beginning-of-dblock ()
  11096. "Find the beginning of the dynamic block at point.
  11097. Error if there is no such block at point."
  11098. (let ((pos (point))
  11099. beg)
  11100. (end-of-line 1)
  11101. (if (and (re-search-backward org-dblock-start-re nil t)
  11102. (setq beg (match-beginning 0))
  11103. (re-search-forward org-dblock-end-re nil t)
  11104. (> (match-end 0) pos))
  11105. (goto-char beg)
  11106. (goto-char pos)
  11107. (error "Not in a dynamic block"))))
  11108. (defun org-update-all-dblocks ()
  11109. "Update all dynamic blocks in the buffer.
  11110. This function can be used in a hook."
  11111. (interactive)
  11112. (when (derived-mode-p 'org-mode)
  11113. (org-map-dblocks 'org-update-dblock)))
  11114. ;;;; Completion
  11115. (declare-function org-export-backend-options "ox" (cl-x) t)
  11116. (defun org-get-export-keywords ()
  11117. "Return a list of all currently understood export keywords.
  11118. Export keywords include options, block names, attributes and
  11119. keywords relative to each registered export back-end."
  11120. (let (keywords)
  11121. (dolist (backend
  11122. (org-bound-and-true-p org-export-registered-backends)
  11123. (delq nil keywords))
  11124. ;; Back-end name (for keywords, like #+LATEX:)
  11125. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11126. (dolist (option-entry (org-export-backend-options backend))
  11127. ;; Back-end options.
  11128. (push (nth 1 option-entry) keywords)))))
  11129. (defconst org-options-keywords
  11130. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11131. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11132. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11133. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11134. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11135. (defcustom org-structure-template-alist
  11136. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11137. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11138. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11139. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11140. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11141. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11142. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11143. ("L" "#+LaTeX: ")
  11144. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11145. ("H" "#+HTML: ")
  11146. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11147. ("A" "#+ASCII: ")
  11148. ("i" "#+INDEX: ?")
  11149. ("I" "#+INCLUDE: %file ?"))
  11150. "Structure completion elements.
  11151. This is a list of abbreviation keys and values. The value gets inserted
  11152. if you type `<' followed by the key and then press the completion key,
  11153. usually `TAB'. %file will be replaced by a file name after prompting
  11154. for the file using completion. The cursor will be placed at the position
  11155. of the `?' in the template.
  11156. There are two templates for each key, the first uses the original Org syntax,
  11157. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11158. the default when the /org-mtags.el/ module has been loaded. See also the
  11159. variable `org-mtags-prefer-muse-templates'."
  11160. :group 'org-completion
  11161. :type '(repeat
  11162. (list
  11163. (string :tag "Key")
  11164. (string :tag "Template")))
  11165. :version "25.1"
  11166. :package-version '(Org . "8.3"))
  11167. (defun org-try-structure-completion ()
  11168. "Try to complete a structure template before point.
  11169. This looks for strings like \"<e\" on an otherwise empty line and
  11170. expands them."
  11171. (let ((l (buffer-substring (point-at-bol) (point)))
  11172. a)
  11173. (when (and (looking-at "[ \t]*$")
  11174. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11175. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11176. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11177. (match-beginning 1)) a)
  11178. t)))
  11179. (defun org-complete-expand-structure-template (start cell)
  11180. "Expand a structure template."
  11181. (let ((rpl (nth 1 cell))
  11182. (ind ""))
  11183. (delete-region start (point))
  11184. (when (string-match "\\`[ \t]*#\\+" rpl)
  11185. (cond
  11186. ((bolp))
  11187. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11188. (setq ind (buffer-substring (point-at-bol) (point))))
  11189. (t (newline))))
  11190. (setq start (point))
  11191. (when (string-match "%file" rpl)
  11192. (setq rpl (replace-match
  11193. (concat
  11194. "\""
  11195. (save-match-data
  11196. (abbreviate-file-name (read-file-name "Include file: ")))
  11197. "\"")
  11198. t t rpl)))
  11199. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11200. (concat "\n" ind)))
  11201. (insert rpl)
  11202. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11203. ;;;; TODO, DEADLINE, Comments
  11204. (defun org-toggle-comment ()
  11205. "Change the COMMENT state of an entry."
  11206. (interactive)
  11207. (save-excursion
  11208. (org-back-to-heading)
  11209. (looking-at org-complex-heading-regexp)
  11210. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11211. (skip-chars-forward " \t")
  11212. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11213. (if (org-in-commented-heading-p t)
  11214. (delete-region (point)
  11215. (progn (search-forward " " (line-end-position) 'move)
  11216. (skip-chars-forward " \t")
  11217. (point)))
  11218. (insert org-comment-string)
  11219. (unless (eolp) (insert " ")))))
  11220. (defvar org-last-todo-state-is-todo nil
  11221. "This is non-nil when the last TODO state change led to a TODO state.
  11222. If the last change removed the TODO tag or switched to DONE, then
  11223. this is nil.")
  11224. (defvar org-setting-tags nil) ; dynamically skipped
  11225. (defvar org-todo-setup-filter-hook nil
  11226. "Hook for functions that pre-filter todo specs.
  11227. Each function takes a todo spec and returns either nil or the spec
  11228. transformed into canonical form." )
  11229. (defvar org-todo-get-default-hook nil
  11230. "Hook for functions that get a default item for todo.
  11231. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11232. nil or a string to be used for the todo mark." )
  11233. (defvar org-agenda-headline-snapshot-before-repeat)
  11234. (defun org-current-effective-time ()
  11235. "Return current time adjusted for `org-extend-today-until' variable."
  11236. (let* ((ct (org-current-time))
  11237. (dct (decode-time ct))
  11238. (ct1
  11239. (cond
  11240. (org-use-last-clock-out-time-as-effective-time
  11241. (or (org-clock-get-last-clock-out-time) ct))
  11242. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11243. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11244. (t ct))))
  11245. ct1))
  11246. (defun org-todo-yesterday (&optional arg)
  11247. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11248. (interactive "P")
  11249. (if (eq major-mode 'org-agenda-mode)
  11250. (apply 'org-agenda-todo-yesterday arg)
  11251. (let* ((org-use-effective-time t)
  11252. (hour (nth 2 (decode-time (org-current-time))))
  11253. (org-extend-today-until (1+ hour)))
  11254. (org-todo arg))))
  11255. (defvar org-block-entry-blocking ""
  11256. "First entry preventing the TODO state change.")
  11257. (defun org-cancel-repeater ()
  11258. "Cancel a repeater by setting its numeric value to zero."
  11259. (interactive)
  11260. (save-excursion
  11261. (org-back-to-heading t)
  11262. (let ((bound1 (point))
  11263. (bound0 (save-excursion (outline-next-heading) (point))))
  11264. (when (and (re-search-forward
  11265. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11266. org-deadline-time-regexp "\\)\\|\\("
  11267. org-ts-regexp "\\)")
  11268. bound0 t)
  11269. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11270. bound1 t))
  11271. (replace-match "0" t nil nil 1)))))
  11272. (defvar org-state) ;; dynamically scoped into this function
  11273. (defun org-todo (&optional arg)
  11274. "Change the TODO state of an item.
  11275. The state of an item is given by a keyword at the start of the heading,
  11276. like
  11277. *** TODO Write paper
  11278. *** DONE Call mom
  11279. The different keywords are specified in the variable `org-todo-keywords'.
  11280. By default the available states are \"TODO\" and \"DONE\".
  11281. So for this example: when the item starts with TODO, it is changed to DONE.
  11282. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11283. DONE are present, add TODO at the beginning of the heading.
  11284. With \\[universal-argument] prefix arg, use completion to determine the new \
  11285. state.
  11286. With numeric prefix arg, switch to that state.
  11287. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11288. keywords (nextset).
  11289. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11290. With a numeric prefix arg of 0, inhibit note taking for the change.
  11291. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11292. When called through ELisp, arg is also interpreted in the following way:
  11293. `none' -> empty state
  11294. \"\"(empty string) -> switch to empty state
  11295. `done' -> switch to DONE
  11296. `nextset' -> switch to the next set of keywords
  11297. `previousset' -> switch to the previous set of keywords
  11298. \"WAITING\" -> switch to the specified keyword, but only if it
  11299. really is a member of `org-todo-keywords'."
  11300. (interactive "P")
  11301. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11302. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11303. 'region-start-level 'region))
  11304. org-loop-over-headlines-in-active-region)
  11305. (org-map-entries
  11306. `(org-todo ,arg)
  11307. org-loop-over-headlines-in-active-region
  11308. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11309. (when (equal arg '(16)) (setq arg 'nextset))
  11310. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11311. (let ((org-blocker-hook org-blocker-hook)
  11312. commentp
  11313. case-fold-search)
  11314. (when (equal arg '(64))
  11315. (setq arg nil org-blocker-hook nil))
  11316. (when (and org-blocker-hook
  11317. (or org-inhibit-blocking
  11318. (org-entry-get nil "NOBLOCKING")))
  11319. (setq org-blocker-hook nil))
  11320. (save-excursion
  11321. (catch 'exit
  11322. (org-back-to-heading t)
  11323. (when (org-in-commented-heading-p t)
  11324. (org-toggle-comment)
  11325. (setq commentp t))
  11326. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11327. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11328. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11329. (let* ((match-data (match-data))
  11330. (startpos (point-at-bol))
  11331. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11332. (org-log-done org-log-done)
  11333. (org-log-repeat org-log-repeat)
  11334. (org-todo-log-states org-todo-log-states)
  11335. (org-inhibit-logging
  11336. (if (equal arg 0)
  11337. (progn (setq arg nil) 'note) org-inhibit-logging))
  11338. (this (match-string 1))
  11339. (hl-pos (match-beginning 0))
  11340. (head (org-get-todo-sequence-head this))
  11341. (ass (assoc head org-todo-kwd-alist))
  11342. (interpret (nth 1 ass))
  11343. (done-word (nth 3 ass))
  11344. (final-done-word (nth 4 ass))
  11345. (org-last-state (or this ""))
  11346. (completion-ignore-case t)
  11347. (member (member this org-todo-keywords-1))
  11348. (tail (cdr member))
  11349. (org-state (cond
  11350. ((and org-todo-key-trigger
  11351. (or (and (equal arg '(4))
  11352. (eq org-use-fast-todo-selection 'prefix))
  11353. (and (not arg) org-use-fast-todo-selection
  11354. (not (eq org-use-fast-todo-selection
  11355. 'prefix)))))
  11356. ;; Use fast selection
  11357. (org-fast-todo-selection))
  11358. ((and (equal arg '(4))
  11359. (or (not org-use-fast-todo-selection)
  11360. (not org-todo-key-trigger)))
  11361. ;; Read a state with completion
  11362. (completing-read
  11363. "State: " (mapcar #'list org-todo-keywords-1)
  11364. nil t))
  11365. ((eq arg 'right)
  11366. (if this
  11367. (if tail (car tail) nil)
  11368. (car org-todo-keywords-1)))
  11369. ((eq arg 'left)
  11370. (unless (equal member org-todo-keywords-1)
  11371. (if this
  11372. (nth (- (length org-todo-keywords-1)
  11373. (length tail) 2)
  11374. org-todo-keywords-1)
  11375. (org-last org-todo-keywords-1))))
  11376. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11377. (setq arg nil))) ; hack to fall back to cycling
  11378. (arg
  11379. ;; user or caller requests a specific state
  11380. (cond
  11381. ((equal arg "") nil)
  11382. ((eq arg 'none) nil)
  11383. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11384. ((eq arg 'nextset)
  11385. (or (car (cdr (member head org-todo-heads)))
  11386. (car org-todo-heads)))
  11387. ((eq arg 'previousset)
  11388. (let ((org-todo-heads (reverse org-todo-heads)))
  11389. (or (car (cdr (member head org-todo-heads)))
  11390. (car org-todo-heads))))
  11391. ((car (member arg org-todo-keywords-1)))
  11392. ((stringp arg)
  11393. (user-error "State `%s' not valid in this file" arg))
  11394. ((nth (1- (prefix-numeric-value arg))
  11395. org-todo-keywords-1))))
  11396. ((null member) (or head (car org-todo-keywords-1)))
  11397. ((equal this final-done-word) nil) ;; -> make empty
  11398. ((null tail) nil) ;; -> first entry
  11399. ((memq interpret '(type priority))
  11400. (if (eq this-command last-command)
  11401. (car tail)
  11402. (if (> (length tail) 0)
  11403. (or done-word (car org-done-keywords))
  11404. nil)))
  11405. (t
  11406. (car tail))))
  11407. (org-state (or
  11408. (run-hook-with-args-until-success
  11409. 'org-todo-get-default-hook org-state org-last-state)
  11410. org-state))
  11411. (next (if org-state (concat " " org-state " ") " "))
  11412. (change-plist (list :type 'todo-state-change :from this :to org-state
  11413. :position startpos))
  11414. dolog now-done-p)
  11415. (when org-blocker-hook
  11416. (setq org-last-todo-state-is-todo
  11417. (not (member this org-done-keywords)))
  11418. (unless (save-excursion
  11419. (save-match-data
  11420. (org-with-wide-buffer
  11421. (run-hook-with-args-until-failure
  11422. 'org-blocker-hook change-plist))))
  11423. (if (org-called-interactively-p 'interactive)
  11424. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11425. this org-state org-block-entry-blocking)
  11426. ;; fail silently
  11427. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11428. this org-state org-block-entry-blocking)
  11429. (throw 'exit nil))))
  11430. (store-match-data match-data)
  11431. (replace-match next t t)
  11432. (cond ((equal this org-state)
  11433. (message "TODO state was already %s" (org-trim next)))
  11434. ((pos-visible-in-window-p hl-pos)
  11435. (message "TODO state changed to %s" (org-trim next))))
  11436. (unless head
  11437. (setq head (org-get-todo-sequence-head org-state)
  11438. ass (assoc head org-todo-kwd-alist)
  11439. interpret (nth 1 ass)
  11440. done-word (nth 3 ass)
  11441. final-done-word (nth 4 ass)))
  11442. (when (memq arg '(nextset previousset))
  11443. (message "Keyword-Set %d/%d: %s"
  11444. (- (length org-todo-sets) -1
  11445. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11446. (length org-todo-sets)
  11447. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11448. (setq org-last-todo-state-is-todo
  11449. (not (member org-state org-done-keywords)))
  11450. (setq now-done-p (and (member org-state org-done-keywords)
  11451. (not (member this org-done-keywords))))
  11452. (and logging (org-local-logging logging))
  11453. (when (and (or org-todo-log-states org-log-done)
  11454. (not (eq org-inhibit-logging t))
  11455. (not (memq arg '(nextset previousset))))
  11456. ;; we need to look at recording a time and note
  11457. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11458. (nth 2 (assoc this org-todo-log-states))))
  11459. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11460. (setq dolog 'time))
  11461. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11462. (and org-state
  11463. (member org-state org-not-done-keywords)
  11464. (not (member this org-not-done-keywords))))
  11465. ;; This is now a todo state and was not one before
  11466. ;; If there was a CLOSED time stamp, get rid of it.
  11467. (org-add-planning-info nil nil 'closed))
  11468. (when (and now-done-p org-log-done)
  11469. ;; It is now done, and it was not done before
  11470. (org-add-planning-info 'closed (org-current-effective-time))
  11471. (when (and (not dolog) (eq 'note org-log-done))
  11472. (org-add-log-setup 'done org-state this 'note)))
  11473. (when (and org-state dolog)
  11474. ;; This is a non-nil state, and we need to log it
  11475. (org-add-log-setup 'state org-state this dolog)))
  11476. ;; Fixup tag positioning
  11477. (org-todo-trigger-tag-changes org-state)
  11478. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11479. (when org-provide-todo-statistics
  11480. (org-update-parent-todo-statistics))
  11481. (run-hooks 'org-after-todo-state-change-hook)
  11482. (when (and arg (not (member org-state org-done-keywords)))
  11483. (setq head (org-get-todo-sequence-head org-state)))
  11484. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11485. ;; Do we need to trigger a repeat?
  11486. (when now-done-p
  11487. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11488. ;; This is for the agenda, take a snapshot of the headline.
  11489. (save-match-data
  11490. (setq org-agenda-headline-snapshot-before-repeat
  11491. (org-get-heading))))
  11492. (org-auto-repeat-maybe org-state))
  11493. ;; Fixup cursor location if close to the keyword
  11494. (when (and (outline-on-heading-p)
  11495. (not (bolp))
  11496. (save-excursion (beginning-of-line 1)
  11497. (looking-at org-todo-line-regexp))
  11498. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11499. (goto-char (or (match-end 2) (match-end 1)))
  11500. (and (looking-at " ") (just-one-space)))
  11501. (when org-trigger-hook
  11502. (save-excursion
  11503. (run-hook-with-args 'org-trigger-hook change-plist)))
  11504. (when commentp (org-toggle-comment))))))))
  11505. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11506. "Block turning an entry into a TODO, using the hierarchy.
  11507. This checks whether the current task should be blocked from state
  11508. changes. Such blocking occurs when:
  11509. 1. The task has children which are not all in a completed state.
  11510. 2. A task has a parent with the property :ORDERED:, and there
  11511. are siblings prior to the current task with incomplete
  11512. status.
  11513. 3. The parent of the task is blocked because it has siblings that should
  11514. be done first, or is child of a block grandparent TODO entry."
  11515. (if (not org-enforce-todo-dependencies)
  11516. t ; if locally turned off don't block
  11517. (catch 'dont-block
  11518. ;; If this is not a todo state change, or if this entry is already DONE,
  11519. ;; do not block
  11520. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11521. (member (plist-get change-plist :from)
  11522. (cons 'done org-done-keywords))
  11523. (member (plist-get change-plist :to)
  11524. (cons 'todo org-not-done-keywords))
  11525. (not (plist-get change-plist :to)))
  11526. (throw 'dont-block t))
  11527. ;; If this task has children, and any are undone, it's blocked
  11528. (save-excursion
  11529. (org-back-to-heading t)
  11530. (let ((this-level (funcall outline-level)))
  11531. (outline-next-heading)
  11532. (let ((child-level (funcall outline-level)))
  11533. (while (and (not (eobp))
  11534. (> child-level this-level))
  11535. ;; this todo has children, check whether they are all
  11536. ;; completed
  11537. (when (and (not (org-entry-is-done-p))
  11538. (org-entry-is-todo-p))
  11539. (setq org-block-entry-blocking (org-get-heading))
  11540. (throw 'dont-block nil))
  11541. (outline-next-heading)
  11542. (setq child-level (funcall outline-level))))))
  11543. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11544. ;; any previous siblings are undone, it's blocked
  11545. (save-excursion
  11546. (org-back-to-heading t)
  11547. (let* ((pos (point))
  11548. (parent-pos (and (org-up-heading-safe) (point))))
  11549. (unless parent-pos (throw 'dont-block t)) ; no parent
  11550. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11551. (forward-line 1)
  11552. (re-search-forward org-not-done-heading-regexp pos t))
  11553. (setq org-block-entry-blocking (match-string 0))
  11554. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11555. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11556. (while t
  11557. (goto-char parent-pos)
  11558. (unless (looking-at org-not-done-heading-regexp)
  11559. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11560. (setq pos (point))
  11561. (setq parent-pos (and (org-up-heading-safe) (point)))
  11562. (unless parent-pos (throw 'dont-block t)) ; no parent
  11563. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11564. (forward-line 1)
  11565. (re-search-forward org-not-done-heading-regexp pos t)
  11566. (setq org-block-entry-blocking (org-get-heading)))
  11567. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11568. (defcustom org-track-ordered-property-with-tag nil
  11569. "Should the ORDERED property also be shown as a tag?
  11570. The ORDERED property decides if an entry should require subtasks to be
  11571. completed in sequence. Since a property is not very visible, setting
  11572. this option means that toggling the ORDERED property with the command
  11573. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11574. not relevant for the behavior, but it makes things more visible.
  11575. Note that toggling the tag with tags commands will not change the property
  11576. and therefore not influence behavior!
  11577. This can be t, meaning the tag ORDERED should be used, It can also be a
  11578. string to select a different tag for this task."
  11579. :group 'org-todo
  11580. :type '(choice
  11581. (const :tag "No tracking" nil)
  11582. (const :tag "Track with ORDERED tag" t)
  11583. (string :tag "Use other tag")))
  11584. (defun org-toggle-ordered-property ()
  11585. "Toggle the ORDERED property of the current entry.
  11586. For better visibility, you can track the value of this property with a tag.
  11587. See variable `org-track-ordered-property-with-tag'."
  11588. (interactive)
  11589. (let* ((t1 org-track-ordered-property-with-tag)
  11590. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11591. (save-excursion
  11592. (org-back-to-heading)
  11593. (if (org-entry-get nil "ORDERED")
  11594. (progn
  11595. (org-delete-property "ORDERED")
  11596. (and tag (org-toggle-tag tag 'off))
  11597. (message "Subtasks can be completed in arbitrary order"))
  11598. (org-entry-put nil "ORDERED" "t")
  11599. (and tag (org-toggle-tag tag 'on))
  11600. (message "Subtasks must be completed in sequence")))))
  11601. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11602. (defun org-block-todo-from-checkboxes (change-plist)
  11603. "Block turning an entry into a TODO, using checkboxes.
  11604. This checks whether the current task should be blocked from state
  11605. changes because there are unchecked boxes in this entry."
  11606. (if (not org-enforce-todo-checkbox-dependencies)
  11607. t ; if locally turned off don't block
  11608. (catch 'dont-block
  11609. ;; If this is not a todo state change, or if this entry is already DONE,
  11610. ;; do not block
  11611. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11612. (member (plist-get change-plist :from)
  11613. (cons 'done org-done-keywords))
  11614. (member (plist-get change-plist :to)
  11615. (cons 'todo org-not-done-keywords))
  11616. (not (plist-get change-plist :to)))
  11617. (throw 'dont-block t))
  11618. ;; If this task has checkboxes that are not checked, it's blocked
  11619. (save-excursion
  11620. (org-back-to-heading t)
  11621. (let ((beg (point)) end)
  11622. (outline-next-heading)
  11623. (setq end (point))
  11624. (goto-char beg)
  11625. (when (org-list-search-forward
  11626. (concat (org-item-beginning-re)
  11627. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11628. "\\[[- ]\\]")
  11629. end t)
  11630. (when (boundp 'org-blocked-by-checkboxes)
  11631. (setq org-blocked-by-checkboxes t))
  11632. (throw 'dont-block nil))))
  11633. t))) ; do not block
  11634. (defun org-entry-blocked-p ()
  11635. "Non-nil if entry at point is blocked."
  11636. (and (not (org-entry-get nil "NOBLOCKING"))
  11637. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11638. (not (run-hook-with-args-until-failure
  11639. 'org-blocker-hook
  11640. (list :type 'todo-state-change
  11641. :position (point)
  11642. :from 'todo
  11643. :to 'done)))))
  11644. (defun org-update-statistics-cookies (all)
  11645. "Update the statistics cookie, either from TODO or from checkboxes.
  11646. This should be called with the cursor in a line with a statistics cookie."
  11647. (interactive "P")
  11648. (if all
  11649. (progn
  11650. (org-update-checkbox-count 'all)
  11651. (org-map-entries 'org-update-parent-todo-statistics))
  11652. (if (not (org-at-heading-p))
  11653. (org-update-checkbox-count)
  11654. (let ((pos (point-marker))
  11655. end l1 l2)
  11656. (ignore-errors (org-back-to-heading t))
  11657. (if (not (org-at-heading-p))
  11658. (org-update-checkbox-count)
  11659. (setq l1 (org-outline-level))
  11660. (setq end (save-excursion
  11661. (outline-next-heading)
  11662. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11663. (point)))
  11664. (if (and (save-excursion
  11665. (re-search-forward
  11666. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11667. (not (save-excursion (re-search-forward
  11668. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11669. (org-update-checkbox-count)
  11670. (if (and l2 (> l2 l1))
  11671. (progn
  11672. (goto-char end)
  11673. (org-update-parent-todo-statistics))
  11674. (goto-char pos)
  11675. (beginning-of-line 1)
  11676. (while (re-search-forward
  11677. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11678. (point-at-eol) t)
  11679. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11680. (goto-char pos)
  11681. (move-marker pos nil)))))
  11682. (defvar org-entry-property-inherited-from) ;; defined below
  11683. (defun org-update-parent-todo-statistics ()
  11684. "Update any statistics cookie in the parent of the current headline.
  11685. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11686. the entire subtree and this will travel up the hierarchy and update
  11687. statistics everywhere."
  11688. (let* ((prop (save-excursion (org-up-heading-safe)
  11689. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11690. (recursive (or (not org-hierarchical-todo-statistics)
  11691. (and prop (string-match "\\<recursive\\>" prop))))
  11692. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11693. 0))
  11694. (first t)
  11695. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11696. level ltoggle l1 new ndel
  11697. (cnt-all 0) (cnt-done 0) is-percent kwd
  11698. checkbox-beg ov ovs ove cookie-present)
  11699. (catch 'exit
  11700. (save-excursion
  11701. (beginning-of-line 1)
  11702. (setq ltoggle (funcall outline-level))
  11703. ;; Three situations are to consider:
  11704. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11705. ;; to the top-level ancestor on the headline;
  11706. ;; 2. If parent has "recursive" property, repeat up to the
  11707. ;; headline setting that property, taking inheritance into
  11708. ;; account;
  11709. ;; 3. Else, move up to direct parent and proceed only once.
  11710. (while (and (setq level (org-up-heading-safe))
  11711. (or recursive first)
  11712. (>= (point) lim))
  11713. (setq first nil cookie-present nil)
  11714. (unless (and level
  11715. (not (string-match
  11716. "\\<checkbox\\>"
  11717. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11718. "")))))
  11719. (throw 'exit nil))
  11720. (while (re-search-forward box-re (point-at-eol) t)
  11721. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11722. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11723. (save-match-data
  11724. (unless (outline-next-heading) (throw 'exit nil))
  11725. (while (and (looking-at org-complex-heading-regexp)
  11726. (> (setq l1 (length (match-string 1))) level))
  11727. (setq kwd (and (or recursive (= l1 ltoggle))
  11728. (match-string 2)))
  11729. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11730. (and (eq org-provide-todo-statistics t)
  11731. (or (member kwd org-done-keywords)))
  11732. (and (listp org-provide-todo-statistics)
  11733. (stringp (car org-provide-todo-statistics))
  11734. (or (member kwd org-provide-todo-statistics)
  11735. (member kwd org-done-keywords)))
  11736. (and (listp org-provide-todo-statistics)
  11737. (listp (car org-provide-todo-statistics))
  11738. (or (member kwd (car org-provide-todo-statistics))
  11739. (and (member kwd org-done-keywords)
  11740. (member kwd (cadr org-provide-todo-statistics))))))
  11741. (setq cnt-all (1+ cnt-all))
  11742. (and (eq org-provide-todo-statistics t)
  11743. kwd
  11744. (setq cnt-all (1+ cnt-all))))
  11745. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11746. (member kwd org-done-keywords))
  11747. (and (listp org-provide-todo-statistics)
  11748. (listp (car org-provide-todo-statistics))
  11749. (member kwd org-done-keywords)
  11750. (member kwd (cadr org-provide-todo-statistics)))
  11751. (and (listp org-provide-todo-statistics)
  11752. (stringp (car org-provide-todo-statistics))
  11753. (member kwd org-done-keywords)))
  11754. (setq cnt-done (1+ cnt-done)))
  11755. (outline-next-heading)))
  11756. (setq new
  11757. (if is-percent
  11758. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11759. (max 1 cnt-all)))
  11760. (format "[%d/%d]" cnt-done cnt-all))
  11761. ndel (- (match-end 0) checkbox-beg))
  11762. ;; handle overlays when updating cookie from column view
  11763. (when (setq ov (car (overlays-at checkbox-beg)))
  11764. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11765. (delete-overlay ov))
  11766. (goto-char checkbox-beg)
  11767. (insert new)
  11768. (delete-region (point) (+ (point) ndel))
  11769. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11770. (when ov (move-overlay ov ovs ove)))
  11771. (when cookie-present
  11772. (run-hook-with-args 'org-after-todo-statistics-hook
  11773. cnt-done (- cnt-all cnt-done))))))
  11774. (run-hooks 'org-todo-statistics-hook)))
  11775. (defvar org-after-todo-statistics-hook nil
  11776. "Hook that is called after a TODO statistics cookie has been updated.
  11777. Each function is called with two arguments: the number of not-done entries
  11778. and the number of done entries.
  11779. For example, the following function, when added to this hook, will switch
  11780. an entry to DONE when all children are done, and back to TODO when new
  11781. entries are set to a TODO status. Note that this hook is only called
  11782. when there is a statistics cookie in the headline!
  11783. \(defun org-summary-todo (n-done n-not-done)
  11784. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11785. \(let (org-log-done org-log-states) ; turn off logging
  11786. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11787. ")
  11788. (defvar org-todo-statistics-hook nil
  11789. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11790. This hook runs even if there is no statistics cookie present, in which case
  11791. `org-after-todo-statistics-hook' would not run.")
  11792. (defun org-todo-trigger-tag-changes (state)
  11793. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11794. (let ((l org-todo-state-tags-triggers)
  11795. changes)
  11796. (when (or (not state) (equal state ""))
  11797. (setq changes (append changes (cdr (assoc "" l)))))
  11798. (when (and (stringp state) (> (length state) 0))
  11799. (setq changes (append changes (cdr (assoc state l)))))
  11800. (when (member state org-not-done-keywords)
  11801. (setq changes (append changes (cdr (assoc 'todo l)))))
  11802. (when (member state org-done-keywords)
  11803. (setq changes (append changes (cdr (assoc 'done l)))))
  11804. (dolist (c changes)
  11805. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11806. (defun org-local-logging (value)
  11807. "Get logging settings from a property VALUE."
  11808. ;; Directly set the variables, they are already local.
  11809. (setq org-log-done nil
  11810. org-log-repeat nil
  11811. org-todo-log-states nil)
  11812. (dolist (w (org-split-string value))
  11813. (let (a)
  11814. (cond
  11815. ((setq a (assoc w org-startup-options))
  11816. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11817. (set (nth 1 a) (nth 2 a))))
  11818. ((setq a (org-extract-log-state-settings w))
  11819. (and (member (car a) org-todo-keywords-1)
  11820. (push a org-todo-log-states)))))))
  11821. (defun org-get-todo-sequence-head (kwd)
  11822. "Return the head of the TODO sequence to which KWD belongs.
  11823. If KWD is not set, check if there is a text property remembering the
  11824. right sequence."
  11825. (let (p)
  11826. (cond
  11827. ((not kwd)
  11828. (or (get-text-property (point-at-bol) 'org-todo-head)
  11829. (progn
  11830. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11831. nil (point-at-eol)))
  11832. (get-text-property p 'org-todo-head))))
  11833. ((not (member kwd org-todo-keywords-1))
  11834. (car org-todo-keywords-1))
  11835. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11836. (defun org-fast-todo-selection ()
  11837. "Fast TODO keyword selection with single keys.
  11838. Returns the new TODO keyword, or nil if no state change should occur."
  11839. (let* ((fulltable org-todo-key-alist)
  11840. (done-keywords org-done-keywords) ;; needed for the faces.
  11841. (maxlen (apply 'max (mapcar
  11842. (lambda (x)
  11843. (if (stringp (car x)) (string-width (car x)) 0))
  11844. fulltable)))
  11845. (expert nil)
  11846. (fwidth (+ maxlen 3 1 3))
  11847. (ncol (/ (- (window-width) 4) fwidth))
  11848. tg cnt e c tbl
  11849. groups ingroup)
  11850. (save-excursion
  11851. (save-window-excursion
  11852. (if expert
  11853. (set-buffer (get-buffer-create " *Org todo*"))
  11854. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11855. (erase-buffer)
  11856. (setq-local org-done-keywords done-keywords)
  11857. (setq tbl fulltable cnt 0)
  11858. (while (setq e (pop tbl))
  11859. (cond
  11860. ((equal e '(:startgroup))
  11861. (push '() groups) (setq ingroup t)
  11862. (unless (= cnt 0)
  11863. (setq cnt 0)
  11864. (insert "\n"))
  11865. (insert "{ "))
  11866. ((equal e '(:endgroup))
  11867. (setq ingroup nil cnt 0)
  11868. (insert "}\n"))
  11869. ((equal e '(:newline))
  11870. (unless (= cnt 0)
  11871. (setq cnt 0)
  11872. (insert "\n")
  11873. (setq e (car tbl))
  11874. (while (equal (car tbl) '(:newline))
  11875. (insert "\n")
  11876. (setq tbl (cdr tbl)))))
  11877. (t
  11878. (setq tg (car e) c (cdr e))
  11879. (when ingroup (push tg (car groups)))
  11880. (setq tg (org-add-props tg nil 'face
  11881. (org-get-todo-face tg)))
  11882. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11883. (insert "[" c "] " tg (make-string
  11884. (- fwidth 4 (length tg)) ?\ ))
  11885. (when (= (setq cnt (1+ cnt)) ncol)
  11886. (insert "\n")
  11887. (when ingroup (insert " "))
  11888. (setq cnt 0)))))
  11889. (insert "\n")
  11890. (goto-char (point-min))
  11891. (unless expert (org-fit-window-to-buffer))
  11892. (message "[a-z..]:Set [SPC]:clear")
  11893. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11894. (cond
  11895. ((or (= c ?\C-g)
  11896. (and (= c ?q) (not (rassoc c fulltable))))
  11897. (setq quit-flag t))
  11898. ((= c ?\ ) nil)
  11899. ((setq e (rassoc c fulltable) tg (car e))
  11900. tg)
  11901. (t (setq quit-flag t)))))))
  11902. (defun org-entry-is-todo-p ()
  11903. (member (org-get-todo-state) org-not-done-keywords))
  11904. (defun org-entry-is-done-p ()
  11905. (member (org-get-todo-state) org-done-keywords))
  11906. (defun org-get-todo-state ()
  11907. "Return the TODO keyword of the current subtree."
  11908. (save-excursion
  11909. (org-back-to-heading t)
  11910. (and (looking-at org-todo-line-regexp)
  11911. (match-end 2)
  11912. (match-string 2))))
  11913. (defun org-at-date-range-p (&optional inactive-ok)
  11914. "Non-nil if point is inside a date range.
  11915. When optional argument INACTIVE-OK is non-nil, also consider
  11916. inactive time ranges.
  11917. When this function returns a non-nil value, match data is set
  11918. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11919. on INACTIVE-OK."
  11920. (interactive)
  11921. (save-excursion
  11922. (catch 'exit
  11923. (let ((pos (point)))
  11924. (skip-chars-backward "^[<\r\n")
  11925. (skip-chars-backward "<[")
  11926. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11927. (>= (match-end 0) pos)
  11928. (throw 'exit t))
  11929. (skip-chars-backward "^<[\r\n")
  11930. (skip-chars-backward "<[")
  11931. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11932. (>= (match-end 0) pos)
  11933. (throw 'exit t)))
  11934. nil)))
  11935. (defun org-get-repeat (&optional tagline)
  11936. "Check if there is a deadline/schedule with repeater in this entry."
  11937. (save-match-data
  11938. (save-excursion
  11939. (org-back-to-heading t)
  11940. (and (re-search-forward (if tagline
  11941. (concat tagline "\\s-*" org-repeat-re)
  11942. org-repeat-re)
  11943. (org-entry-end-position) t)
  11944. (match-string-no-properties 1)))))
  11945. (defvar org-last-changed-timestamp)
  11946. (defvar org-last-inserted-timestamp)
  11947. (defvar org-log-post-message)
  11948. (defvar org-log-note-purpose)
  11949. (defvar org-log-note-how nil)
  11950. (defvar org-log-note-extra)
  11951. (defun org-auto-repeat-maybe (done-word)
  11952. "Check if the current headline contains a repeated time-stamp.
  11953. If yes, set TODO state back to what it was and change the base date
  11954. of repeating deadline/scheduled time stamps to new date.
  11955. This function is run automatically after each state change to a DONE state."
  11956. (let* ((repeat (org-get-repeat))
  11957. (aa (assoc org-last-state org-todo-kwd-alist))
  11958. (interpret (nth 1 aa))
  11959. (head (nth 2 aa))
  11960. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11961. (msg "Entry repeats: ")
  11962. (org-log-done nil)
  11963. (org-todo-log-states nil))
  11964. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11965. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11966. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11967. org-todo-repeat-to-state)))
  11968. (unless (and to-state (member to-state org-todo-keywords-1))
  11969. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11970. (org-todo to-state))
  11971. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11972. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11973. (org-time-stamp-format t t))))
  11974. (when org-log-repeat
  11975. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11976. (memq 'org-add-log-note post-command-hook))
  11977. ;; We are already setup for some record.
  11978. (when (eq org-log-repeat 'note)
  11979. ;; Make sure we take a note, not only a time stamp.
  11980. (setq org-log-note-how 'note))
  11981. ;; Set up for taking a record.
  11982. (org-add-log-setup 'state
  11983. (or done-word (car org-done-keywords))
  11984. org-last-state
  11985. org-log-repeat)))
  11986. (org-back-to-heading t)
  11987. (org-add-planning-info nil nil 'closed)
  11988. (let ((end (save-excursion (outline-next-heading) (point))))
  11989. (while (re-search-forward org-ts-regexp end t)
  11990. (when (save-match-data
  11991. (or (org-at-planning-p)
  11992. (org-at-property-p)
  11993. (eq (org-element-type (save-excursion
  11994. (backward-char)
  11995. (org-element-context)))
  11996. 'timestamp)))
  11997. (let ((type (cond ((match-end 1) org-scheduled-string)
  11998. ((match-end 3) org-deadline-string)
  11999. (t "Plain:")))
  12000. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  12001. (cond
  12002. ((not
  12003. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  12004. ;; Time-stamps without a repeater are usually skipped.
  12005. ;; However, a SCHEDULED time-stamp without one is
  12006. ;; removed, as it is considered as no longer relevant.
  12007. (when (equal type org-scheduled-string)
  12008. (org-remove-timestamp-with-keyword type)))
  12009. (t
  12010. (let ((n (string-to-number (match-string 2 ts)))
  12011. (what (match-string 3 ts)))
  12012. (when (equal what "w") (setq n (* n 7) what "d"))
  12013. (when (and (equal what "h")
  12014. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  12015. ts)))
  12016. (user-error
  12017. "Cannot repeat in Repeat in %d hour(s) because no hour \
  12018. has been set"
  12019. n))
  12020. ;; Preparation, see if we need to modify the start
  12021. ;; date for the change.
  12022. (when (match-end 1)
  12023. (let ((time (save-match-data (org-time-string-to-time ts))))
  12024. (cond
  12025. ((equal (match-string 1 ts) ".")
  12026. ;; Shift starting date to today
  12027. (org-timestamp-change
  12028. (- (org-today) (time-to-days time))
  12029. 'day))
  12030. ((equal (match-string 1 ts) "+")
  12031. (let ((nshiftmax 10)
  12032. (nshift 0))
  12033. (while (or (= nshift 0)
  12034. (<= (time-to-days time)
  12035. (time-to-days (current-time))))
  12036. (when (= (cl-incf nshift) nshiftmax)
  12037. (or (y-or-n-p
  12038. (format "%d repeater intervals were not \
  12039. enough to shift date past today. Continue? "
  12040. nshift))
  12041. (user-error "Abort")))
  12042. (org-timestamp-change n (cdr (assoc what whata)))
  12043. (org-at-timestamp-p t)
  12044. (setq ts (match-string 1))
  12045. (setq time
  12046. (save-match-data
  12047. (org-time-string-to-time ts)))))
  12048. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12049. ;; Rematch, so that we have everything in
  12050. ;; place for the real shift.
  12051. (org-at-timestamp-p t)
  12052. (setq ts (match-string 1))
  12053. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  12054. ts)))))
  12055. (save-excursion
  12056. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12057. (setq msg
  12058. (concat
  12059. msg type " " org-last-changed-timestamp " ")))))))))
  12060. (setq org-log-post-message msg)
  12061. (message "%s" msg))))
  12062. (defun org-show-todo-tree (arg)
  12063. "Make a compact tree which shows all headlines marked with TODO.
  12064. The tree will show the lines where the regexp matches, and all higher
  12065. headlines above the match.
  12066. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12067. With a numeric prefix N, construct a sparse tree for the Nth element
  12068. of `org-todo-keywords-1'."
  12069. (interactive "P")
  12070. (let ((case-fold-search nil)
  12071. (kwd-re
  12072. (cond ((null arg) org-not-done-regexp)
  12073. ((equal arg '(4))
  12074. (let ((kwd
  12075. (completing-read "Keyword (or KWD1|KWD2|...): "
  12076. (mapcar #'list org-todo-keywords-1))))
  12077. (concat "\\("
  12078. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12079. "\\)\\>")))
  12080. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12081. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12082. org-todo-keywords-1)))
  12083. (t (user-error "Invalid prefix argument: %s" arg)))))
  12084. (message "%d TODO entries found"
  12085. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12086. (defun org-deadline (arg &optional time)
  12087. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12088. With one universal prefix argument, remove any deadline from the item.
  12089. With two universal prefix arguments, prompt for a warning delay.
  12090. With argument TIME, set the deadline at the corresponding date. TIME
  12091. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12092. (interactive "P")
  12093. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12094. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12095. 'region-start-level 'region))
  12096. org-loop-over-headlines-in-active-region)
  12097. (org-map-entries
  12098. `(org-deadline ',arg ,time)
  12099. org-loop-over-headlines-in-active-region
  12100. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12101. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12102. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12103. (repeater (and old-date
  12104. (string-match
  12105. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12106. old-date)
  12107. (match-string 1 old-date))))
  12108. (cond
  12109. ((equal arg '(4))
  12110. (when (and old-date org-log-redeadline)
  12111. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  12112. (org-remove-timestamp-with-keyword org-deadline-string)
  12113. (message "Item no longer has a deadline."))
  12114. ((equal arg '(16))
  12115. (save-excursion
  12116. (org-back-to-heading t)
  12117. (if (re-search-forward
  12118. org-deadline-time-regexp
  12119. (save-excursion (outline-next-heading) (point)) t)
  12120. (let* ((rpl0 (match-string 1))
  12121. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12122. (replace-match
  12123. (concat org-deadline-string
  12124. " <" rpl
  12125. (format " -%dd"
  12126. (abs
  12127. (- (time-to-days
  12128. (save-match-data
  12129. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12130. (time-to-days old-date-time))))
  12131. ">") t t))
  12132. (user-error "No deadline information to update"))))
  12133. (t
  12134. (org-add-planning-info 'deadline time 'closed)
  12135. (when (and old-date
  12136. org-log-redeadline
  12137. (not (equal old-date org-last-inserted-timestamp)))
  12138. (org-add-log-setup
  12139. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12140. (when repeater
  12141. (save-excursion
  12142. (org-back-to-heading t)
  12143. (when (re-search-forward (concat org-deadline-string " "
  12144. org-last-inserted-timestamp)
  12145. (save-excursion
  12146. (outline-next-heading) (point)) t)
  12147. (goto-char (1- (match-end 0)))
  12148. (insert " " repeater)
  12149. (setq org-last-inserted-timestamp
  12150. (concat (substring org-last-inserted-timestamp 0 -1)
  12151. " " repeater
  12152. (substring org-last-inserted-timestamp -1))))))
  12153. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12154. (defun org-schedule (arg &optional time)
  12155. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12156. With one universal prefix argument, remove any scheduling date from the item.
  12157. With two universal prefix arguments, prompt for a delay cookie.
  12158. With argument TIME, scheduled at the corresponding date. TIME can
  12159. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12160. (interactive "P")
  12161. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12162. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12163. 'region-start-level 'region))
  12164. org-loop-over-headlines-in-active-region)
  12165. (org-map-entries
  12166. `(org-schedule ',arg ,time)
  12167. org-loop-over-headlines-in-active-region
  12168. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12169. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12170. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12171. (repeater (and old-date
  12172. (string-match
  12173. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12174. old-date)
  12175. (match-string 1 old-date))))
  12176. (cond
  12177. ((equal arg '(4))
  12178. (progn
  12179. (when (and old-date org-log-reschedule)
  12180. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12181. (org-remove-timestamp-with-keyword org-scheduled-string)
  12182. (message "Item is no longer scheduled.")))
  12183. ((equal arg '(16))
  12184. (save-excursion
  12185. (org-back-to-heading t)
  12186. (if (re-search-forward
  12187. org-scheduled-time-regexp
  12188. (save-excursion (outline-next-heading) (point)) t)
  12189. (let* ((rpl0 (match-string 1))
  12190. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12191. (replace-match
  12192. (concat org-scheduled-string
  12193. " <" rpl
  12194. (format " -%dd"
  12195. (abs
  12196. (- (time-to-days
  12197. (save-match-data
  12198. (org-read-date nil t nil "Delay until" old-date-time)))
  12199. (time-to-days old-date-time))))
  12200. ">") t t))
  12201. (user-error "No scheduled information to update"))))
  12202. (t
  12203. (org-add-planning-info 'scheduled time 'closed)
  12204. (when (and old-date
  12205. org-log-reschedule
  12206. (not (equal old-date org-last-inserted-timestamp)))
  12207. (org-add-log-setup
  12208. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12209. (when repeater
  12210. (save-excursion
  12211. (org-back-to-heading t)
  12212. (when (re-search-forward (concat org-scheduled-string " "
  12213. org-last-inserted-timestamp)
  12214. (save-excursion
  12215. (outline-next-heading) (point)) t)
  12216. (goto-char (1- (match-end 0)))
  12217. (insert " " repeater)
  12218. (setq org-last-inserted-timestamp
  12219. (concat (substring org-last-inserted-timestamp 0 -1)
  12220. " " repeater
  12221. (substring org-last-inserted-timestamp -1))))))
  12222. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12223. (defun org-get-scheduled-time (pom &optional inherit)
  12224. "Get the scheduled time as a time tuple, of a format suitable
  12225. for calling org-schedule with, or if there is no scheduling,
  12226. returns nil."
  12227. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12228. (when time
  12229. (apply 'encode-time (org-parse-time-string time)))))
  12230. (defun org-get-deadline-time (pom &optional inherit)
  12231. "Get the deadline as a time tuple, of a format suitable for
  12232. calling org-deadline with, or if there is no scheduling, returns
  12233. nil."
  12234. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12235. (when time
  12236. (apply 'encode-time (org-parse-time-string time)))))
  12237. (defun org-remove-timestamp-with-keyword (keyword)
  12238. "Remove all time stamps with KEYWORD in the current entry."
  12239. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12240. beg)
  12241. (save-excursion
  12242. (org-back-to-heading t)
  12243. (setq beg (point))
  12244. (outline-next-heading)
  12245. (while (re-search-backward re beg t)
  12246. (replace-match "")
  12247. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12248. (equal (char-before) ?\ ))
  12249. (backward-delete-char 1)
  12250. (when (string-match "^[ \t]*$" (buffer-substring
  12251. (point-at-bol) (point-at-eol)))
  12252. (delete-region (point-at-bol)
  12253. (min (point-max) (1+ (point-at-eol))))))))))
  12254. (defvar org-time-was-given) ; dynamically scoped parameter
  12255. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12256. (defun org-at-planning-p ()
  12257. "Non-nil when point is on a planning info line."
  12258. ;; This is as accurate and faster than `org-element-at-point' since
  12259. ;; planning info location is fixed in the section.
  12260. (org-with-wide-buffer
  12261. (beginning-of-line)
  12262. (and (looking-at-p org-planning-line-re)
  12263. (eq (point)
  12264. (ignore-errors
  12265. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12266. (org-back-to-heading t)
  12267. (org-with-limited-levels (org-back-to-heading t)))
  12268. (line-beginning-position 2))))))
  12269. (defun org-add-planning-info (what &optional time &rest remove)
  12270. "Insert new timestamp with keyword in the planning line.
  12271. WHAT indicates what kind of time stamp to add. It is a symbol
  12272. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12273. the time to use. If none is given, the user is prompted for
  12274. a date. REMOVE indicates what kind of entries to remove. An old
  12275. WHAT entry will also be removed."
  12276. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12277. (catch 'exit
  12278. (when (and (memq what '(scheduled deadline))
  12279. (or (not time)
  12280. (and (stringp time)
  12281. (string-match "^[-+]+[0-9]" time))))
  12282. ;; Try to get a default date/time from existing timestamp
  12283. (save-excursion
  12284. (org-back-to-heading t)
  12285. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12286. (when (re-search-forward (if (eq what 'scheduled)
  12287. org-scheduled-time-regexp
  12288. org-deadline-time-regexp)
  12289. end t)
  12290. (setq ts (match-string 1)
  12291. default-time (apply 'encode-time (org-parse-time-string ts))
  12292. default-input (and ts (org-get-compact-tod ts)))))))
  12293. (when what
  12294. (setq time
  12295. (if (stringp time)
  12296. ;; This is a string (relative or absolute), set
  12297. ;; proper date.
  12298. (apply #'encode-time
  12299. (org-read-date-analyze
  12300. time default-time (decode-time default-time)))
  12301. ;; If necessary, get the time from the user
  12302. (or time (org-read-date nil 'to-time nil nil
  12303. default-time default-input)))))
  12304. (org-with-wide-buffer
  12305. (org-back-to-heading t)
  12306. (forward-line)
  12307. (unless (bolp) (insert "\n"))
  12308. (cond ((looking-at-p org-planning-line-re)
  12309. ;; Move to current indentation.
  12310. (skip-chars-forward " \t")
  12311. ;; Check if we have to remove something.
  12312. (dolist (type (if what (cons what remove) remove))
  12313. (save-excursion
  12314. (when (re-search-forward
  12315. (cl-case type
  12316. (closed org-closed-time-regexp)
  12317. (deadline org-deadline-time-regexp)
  12318. (scheduled org-scheduled-time-regexp)
  12319. (otherwise
  12320. (error "Invalid planning type: %s" type)))
  12321. (line-end-position) t)
  12322. ;; Delete until next keyword or end of line.
  12323. (delete-region
  12324. (match-beginning 0)
  12325. (if (re-search-forward org-keyword-time-not-clock-regexp
  12326. (line-end-position)
  12327. t)
  12328. (match-beginning 0)
  12329. (line-end-position))))))
  12330. ;; If there is nothing more to add and no more keyword
  12331. ;; is left, remove the line completely.
  12332. (if (and (looking-at-p "[ \t]*$") (not what))
  12333. (delete-region (line-beginning-position)
  12334. (line-beginning-position 2))
  12335. ;; If we removed last keyword, do not leave trailing
  12336. ;; white space at the end of line.
  12337. (let ((p (point)))
  12338. (save-excursion
  12339. (end-of-line)
  12340. (unless (= (skip-chars-backward " \t" p) 0)
  12341. (delete-region (point) (line-end-position)))))))
  12342. ((not what) (throw 'exit nil)) ; Nothing to do.
  12343. (t (insert-before-markers "\n")
  12344. (backward-char 1)
  12345. (when org-adapt-indentation
  12346. (indent-to-column (1+ (org-outline-level))))))
  12347. (when what
  12348. ;; Insert planning keyword.
  12349. (insert (cl-case what
  12350. (closed org-closed-string)
  12351. (deadline org-deadline-string)
  12352. (scheduled org-scheduled-string)
  12353. (otherwise (error "Invalid planning type: %s" what)))
  12354. " ")
  12355. ;; Insert associated timestamp.
  12356. (let ((ts (org-insert-time-stamp
  12357. time
  12358. (or org-time-was-given
  12359. (and (eq what 'closed) org-log-done-with-time))
  12360. (eq what 'closed)
  12361. nil nil (list org-end-time-was-given))))
  12362. (unless (eolp) (insert " "))
  12363. ts))))))
  12364. (defvar org-log-note-marker (make-marker)
  12365. "Marker pointing at the entry where the note is to be inserted.")
  12366. (defvar org-log-note-purpose nil)
  12367. (defvar org-log-note-state nil)
  12368. (defvar org-log-note-previous-state nil)
  12369. (defvar org-log-note-extra nil)
  12370. (defvar org-log-note-window-configuration nil)
  12371. (defvar org-log-note-return-to (make-marker))
  12372. (defvar org-log-note-effective-time nil
  12373. "Remembered current time so that dynamically scoped
  12374. `org-extend-today-until' affects timestamps in state change log")
  12375. (defvar org-log-post-message nil
  12376. "Message to be displayed after a log note has been stored.
  12377. The auto-repeater uses this.")
  12378. (defun org-add-note ()
  12379. "Add a note to the current entry.
  12380. This is done in the same way as adding a state change note."
  12381. (interactive)
  12382. (org-add-log-setup 'note))
  12383. (defun org-log-beginning (&optional create)
  12384. "Return expected start of log notes in current entry.
  12385. When optional argument CREATE is non-nil, the function creates
  12386. a drawer to store notes, if necessary. Returned position ignores
  12387. narrowing."
  12388. (org-with-wide-buffer
  12389. (let ((drawer (org-log-into-drawer)))
  12390. (cond
  12391. (drawer
  12392. (org-end-of-meta-data)
  12393. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12394. (end (if (org-at-heading-p) (point)
  12395. (save-excursion (outline-next-heading) (point))))
  12396. (case-fold-search t))
  12397. (catch 'exit
  12398. ;; Try to find existing drawer.
  12399. (while (re-search-forward regexp end t)
  12400. (let ((element (org-element-at-point)))
  12401. (when (eq (org-element-type element) 'drawer)
  12402. (let ((cend (org-element-property :contents-end element)))
  12403. (when (and (not org-log-states-order-reversed) cend)
  12404. (goto-char cend)))
  12405. (throw 'exit nil))))
  12406. ;; No drawer found. Create one, if permitted.
  12407. (when create
  12408. (unless (bolp) (insert "\n"))
  12409. (let ((beg (point)))
  12410. (insert ":" drawer ":\n:END:\n")
  12411. (org-indent-region beg (point)))
  12412. (end-of-line -1)))))
  12413. (t
  12414. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12415. (skip-chars-forward " \t\n")
  12416. (beginning-of-line)
  12417. (unless org-log-states-order-reversed
  12418. (org-skip-over-state-notes)
  12419. (skip-chars-backward " \t\n")
  12420. (forward-line)))))
  12421. (if (bolp) (point) (line-beginning-position 2))))
  12422. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12423. "Set up the post command hook to take a note.
  12424. If this is about to TODO state change, the new state is expected in STATE.
  12425. HOW is an indicator what kind of note should be created.
  12426. EXTRA is additional text that will be inserted into the notes buffer."
  12427. (move-marker org-log-note-marker (point))
  12428. (setq org-log-note-purpose purpose
  12429. org-log-note-state state
  12430. org-log-note-previous-state prev-state
  12431. org-log-note-how how
  12432. org-log-note-extra extra
  12433. org-log-note-effective-time (org-current-effective-time))
  12434. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12435. (defun org-skip-over-state-notes ()
  12436. "Skip past the list of State notes in an entry."
  12437. (when (ignore-errors (goto-char (org-in-item-p)))
  12438. (let* ((struct (org-list-struct))
  12439. (prevs (org-list-prevs-alist struct))
  12440. (regexp
  12441. (concat "[ \t]*- +"
  12442. (replace-regexp-in-string
  12443. " +" " +"
  12444. (org-replace-escapes
  12445. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12446. `(("%d" . ,org-ts-regexp-inactive)
  12447. ("%D" . ,org-ts-regexp)
  12448. ("%s" . "\"\\S-+\"")
  12449. ("%S" . "\"\\S-+\"")
  12450. ("%t" . ,org-ts-regexp-inactive)
  12451. ("%T" . ,org-ts-regexp)
  12452. ("%u" . ".*?")
  12453. ("%U" . ".*?")))))))
  12454. (while (looking-at-p regexp)
  12455. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12456. (org-list-get-item-end (point) struct)))))))
  12457. (defun org-add-log-note (&optional _purpose)
  12458. "Pop up a window for taking a note, and add this note later."
  12459. (remove-hook 'post-command-hook 'org-add-log-note)
  12460. (setq org-log-note-window-configuration (current-window-configuration))
  12461. (delete-other-windows)
  12462. (move-marker org-log-note-return-to (point))
  12463. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12464. (goto-char org-log-note-marker)
  12465. (org-switch-to-buffer-other-window "*Org Note*")
  12466. (erase-buffer)
  12467. (if (memq org-log-note-how '(time state))
  12468. (let (current-prefix-arg) (org-store-log-note))
  12469. (let ((org-inhibit-startup t)) (org-mode))
  12470. (insert (format "# Insert note for %s.
  12471. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12472. (cond
  12473. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12474. ((eq org-log-note-purpose 'done) "closed todo item")
  12475. ((eq org-log-note-purpose 'state)
  12476. (format "state change from \"%s\" to \"%s\""
  12477. (or org-log-note-previous-state "")
  12478. (or org-log-note-state "")))
  12479. ((eq org-log-note-purpose 'reschedule)
  12480. "rescheduling")
  12481. ((eq org-log-note-purpose 'delschedule)
  12482. "no longer scheduled")
  12483. ((eq org-log-note-purpose 'redeadline)
  12484. "changing deadline")
  12485. ((eq org-log-note-purpose 'deldeadline)
  12486. "removing deadline")
  12487. ((eq org-log-note-purpose 'refile)
  12488. "refiling")
  12489. ((eq org-log-note-purpose 'note)
  12490. "this entry")
  12491. (t (error "This should not happen")))))
  12492. (when org-log-note-extra (insert org-log-note-extra))
  12493. (setq-local org-finish-function 'org-store-log-note)
  12494. (run-hooks 'org-log-buffer-setup-hook)))
  12495. (defvar org-note-abort nil) ; dynamically scoped
  12496. (defun org-store-log-note ()
  12497. "Finish taking a log note, and insert it to where it belongs."
  12498. (let ((txt (prog1 (buffer-string)
  12499. (kill-buffer)))
  12500. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12501. lines)
  12502. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12503. (setq txt (replace-match "" t t txt)))
  12504. (when (string-match "\\s-+\\'" txt)
  12505. (setq txt (replace-match "" t t txt)))
  12506. (setq lines (org-split-string txt "\n"))
  12507. (when (org-string-nw-p note)
  12508. (setq note
  12509. (org-replace-escapes
  12510. note
  12511. (list (cons "%u" (user-login-name))
  12512. (cons "%U" user-full-name)
  12513. (cons "%t" (format-time-string
  12514. (org-time-stamp-format 'long 'inactive)
  12515. org-log-note-effective-time))
  12516. (cons "%T" (format-time-string
  12517. (org-time-stamp-format 'long nil)
  12518. org-log-note-effective-time))
  12519. (cons "%d" (format-time-string
  12520. (org-time-stamp-format nil 'inactive)
  12521. org-log-note-effective-time))
  12522. (cons "%D" (format-time-string
  12523. (org-time-stamp-format nil nil)
  12524. org-log-note-effective-time))
  12525. (cons "%s" (cond
  12526. ((not org-log-note-state) "")
  12527. ((org-string-match-p org-ts-regexp
  12528. org-log-note-state)
  12529. (format "\"[%s]\""
  12530. (substring org-log-note-state 1 -1)))
  12531. (t (format "\"%s\"" org-log-note-state))))
  12532. (cons "%S"
  12533. (cond
  12534. ((not org-log-note-previous-state) "")
  12535. ((org-string-match-p org-ts-regexp
  12536. org-log-note-previous-state)
  12537. (format "\"[%s]\""
  12538. (substring
  12539. org-log-note-previous-state 1 -1)))
  12540. (t (format "\"%s\""
  12541. org-log-note-previous-state)))))))
  12542. (when lines (setq note (concat note " \\\\")))
  12543. (push note lines))
  12544. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12545. (with-current-buffer (marker-buffer org-log-note-marker)
  12546. (org-with-wide-buffer
  12547. ;; Find location for the new note.
  12548. (goto-char org-log-note-marker)
  12549. (set-marker org-log-note-marker nil)
  12550. (goto-char (org-log-beginning t))
  12551. ;; Make sure point is at the beginning of an empty line.
  12552. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12553. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12554. ;; In an existing list, add a new item at the top level.
  12555. ;; Otherwise, indent line like a regular one.
  12556. (let ((itemp (org-in-item-p)))
  12557. (if itemp
  12558. (indent-line-to
  12559. (let ((struct (save-excursion
  12560. (goto-char itemp) (org-list-struct))))
  12561. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12562. (org-indent-line)))
  12563. (insert (org-list-bullet-string "-") (pop lines))
  12564. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12565. (dolist (line lines)
  12566. (insert "\n")
  12567. (indent-line-to ind)
  12568. (insert line)))
  12569. (message "Note stored")
  12570. (org-back-to-heading t)
  12571. (org-cycle-hide-drawers 'children))
  12572. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12573. ;; from within `org-add-log-note' because `buffer-undo-list'
  12574. ;; is then modified outside of `org-with-remote-undo'.
  12575. (when (eq this-command 'org-agenda-todo)
  12576. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12577. ;; Don't add undo information when called from `org-agenda-todo'.
  12578. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12579. (set-window-configuration org-log-note-window-configuration)
  12580. (with-current-buffer (marker-buffer org-log-note-return-to)
  12581. (goto-char org-log-note-return-to))
  12582. (move-marker org-log-note-return-to nil)
  12583. (when org-log-post-message (message "%s" org-log-post-message))))
  12584. (defun org-remove-empty-drawer-at (pos)
  12585. "Remove an empty drawer at position POS.
  12586. POS may also be a marker."
  12587. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12588. (org-with-wide-buffer
  12589. (goto-char pos)
  12590. (let ((drawer (org-element-at-point)))
  12591. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12592. (not (org-element-property :contents-begin drawer)))
  12593. (delete-region (org-element-property :begin drawer)
  12594. (progn (goto-char (org-element-property :end drawer))
  12595. (skip-chars-backward " \r\t\n")
  12596. (forward-line)
  12597. (point))))))))
  12598. (defvar org-ts-type nil)
  12599. (defun org-sparse-tree (&optional arg type)
  12600. "Create a sparse tree, prompt for the details.
  12601. This command can create sparse trees. You first need to select the type
  12602. of match used to create the tree:
  12603. t Show all TODO entries.
  12604. T Show entries with a specific TODO keyword.
  12605. m Show entries selected by a tags/property match.
  12606. p Enter a property name and its value (both with completion on existing
  12607. names/values) and show entries with that property.
  12608. r Show entries matching a regular expression (`/' can be used as well).
  12609. b Show deadlines and scheduled items before a date.
  12610. a Show deadlines and scheduled items after a date.
  12611. d Show deadlines due within `org-deadline-warning-days'.
  12612. D Show deadlines and scheduled items between a date range."
  12613. (interactive "P")
  12614. (setq type (or type org-sparse-tree-default-date-type))
  12615. (setq org-ts-type type)
  12616. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12617. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12618. \[c]ycle through date types: %s"
  12619. (cl-case type
  12620. (all "all timestamps")
  12621. (scheduled "only scheduled")
  12622. (deadline "only deadline")
  12623. (active "only active timestamps")
  12624. (inactive "only inactive timestamps")
  12625. (closed "with a closed time-stamp")
  12626. (otherwise "scheduled/deadline")))
  12627. (let ((answer (read-char-exclusive)))
  12628. (cl-case answer
  12629. (?c
  12630. (org-sparse-tree
  12631. arg
  12632. (cadr
  12633. (memq type '(nil all scheduled deadline active inactive closed)))))
  12634. (?d (call-interactively 'org-check-deadlines))
  12635. (?b (call-interactively 'org-check-before-date))
  12636. (?a (call-interactively 'org-check-after-date))
  12637. (?D (call-interactively 'org-check-dates-range))
  12638. (?t (call-interactively 'org-show-todo-tree))
  12639. (?T (org-show-todo-tree '(4)))
  12640. (?m (call-interactively 'org-match-sparse-tree))
  12641. ((?p ?P)
  12642. (let* ((kwd (completing-read
  12643. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12644. (value (completing-read
  12645. "Value: " (mapcar #'list (org-property-values kwd)))))
  12646. (unless (string-match "\\`{.*}\\'" value)
  12647. (setq value (concat "\"" value "\"")))
  12648. (org-match-sparse-tree arg (concat kwd "=" value))))
  12649. ((?r ?R ?/) (call-interactively 'org-occur))
  12650. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12651. (defvar-local org-occur-highlights nil
  12652. "List of overlays used for occur matches.")
  12653. (defvar-local org-occur-parameters nil
  12654. "Parameters of the active org-occur calls.
  12655. This is a list, each call to org-occur pushes as cons cell,
  12656. containing the regular expression and the callback, onto the list.
  12657. The list can contain several entries if `org-occur' has been called
  12658. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12659. will only contain one set of parameters. When the highlights are
  12660. removed (for example with `C-c C-c', or with the next edit (depending
  12661. on `org-remove-highlights-with-change'), this variable is emptied
  12662. as well.")
  12663. (defun org-occur (regexp &optional keep-previous callback)
  12664. "Make a compact tree which shows all matches of REGEXP.
  12665. The tree will show the lines where the regexp matches, and any other context
  12666. defined in `org-show-context-detail', which see.
  12667. When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
  12668. done by a previous call to `org-occur' will be kept, to allow stacking of
  12669. calls to this command.
  12670. Optional argument CALLBACK can be a function of no argument. In this case,
  12671. it is called with point at the end of the match, match data being set
  12672. accordingly. Current match is shown only if the return value is non-nil.
  12673. The function must neither move point nor alter narrowing."
  12674. (interactive "sRegexp: \nP")
  12675. (when (equal regexp "")
  12676. (user-error "Regexp cannot be empty"))
  12677. (unless keep-previous
  12678. (org-remove-occur-highlights nil nil t))
  12679. (push (cons regexp callback) org-occur-parameters)
  12680. (let ((cnt 0))
  12681. (save-excursion
  12682. (goto-char (point-min))
  12683. (when (or (not keep-previous) ; do not want to keep
  12684. (not org-occur-highlights)) ; no previous matches
  12685. ;; hide everything
  12686. (org-overview))
  12687. (let ((case-fold-search (if (eq org-occur-case-fold-search 'smart)
  12688. (isearch-no-upper-case-p regexp t)
  12689. org-occur-case-fold-search)))
  12690. (while (re-search-forward regexp nil t)
  12691. (when (or (not callback)
  12692. (save-match-data (funcall callback)))
  12693. (setq cnt (1+ cnt))
  12694. (when org-highlight-sparse-tree-matches
  12695. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12696. (org-show-context 'occur-tree)))))
  12697. (when org-remove-highlights-with-change
  12698. (add-hook 'before-change-functions 'org-remove-occur-highlights
  12699. nil 'local))
  12700. (unless org-sparse-tree-open-archived-trees
  12701. (org-hide-archived-subtrees (point-min) (point-max)))
  12702. (run-hooks 'org-occur-hook)
  12703. (when (org-called-interactively-p 'interactive)
  12704. (message "%d match(es) for regexp %s" cnt regexp))
  12705. cnt))
  12706. (defun org-occur-next-match (&optional n _reset)
  12707. "Function for `next-error-function' to find sparse tree matches.
  12708. N is the number of matches to move, when negative move backwards.
  12709. This function always goes back to the starting point when no
  12710. match is found."
  12711. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12712. (search-func (if (< n 0)
  12713. 'previous-single-char-property-change
  12714. 'next-single-char-property-change))
  12715. (n (abs n))
  12716. (pos (point))
  12717. p1)
  12718. (catch 'exit
  12719. (while (setq p1 (funcall search-func (point) 'org-type))
  12720. (when (equal p1 limit)
  12721. (goto-char pos)
  12722. (user-error "No more matches"))
  12723. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12724. (setq n (1- n))
  12725. (when (= n 0)
  12726. (goto-char p1)
  12727. (throw 'exit (point))))
  12728. (goto-char p1))
  12729. (goto-char p1)
  12730. (user-error "No more matches"))))
  12731. (defun org-show-context (&optional key)
  12732. "Make sure point and context are visible.
  12733. Optional argument KEY, when non-nil, is a symbol. See
  12734. `org-show-context-detail' for allowed values and how much is to
  12735. be shown."
  12736. (org-show-set-visibility
  12737. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12738. ((cdr (assq key org-show-context-detail)))
  12739. (t (cdr (assq 'default org-show-context-detail))))))
  12740. (defun org-show-set-visibility (detail)
  12741. "Set visibility around point according to DETAIL.
  12742. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12743. `tree', `canonical' or t. See `org-show-context-detail' for more
  12744. information."
  12745. ;; Show current heading and possibly its entry, following headline
  12746. ;; or all children.
  12747. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12748. (org-flag-heading nil)
  12749. (org-show-entry)
  12750. ;; If point is hidden within a drawer or a block, make sure to
  12751. ;; expose it.
  12752. (dolist (o (overlays-at (point)))
  12753. (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
  12754. (delete-overlay o)))
  12755. (unless (org-before-first-heading-p)
  12756. (org-with-limited-levels
  12757. (cl-case detail
  12758. ((tree canonical t) (org-show-children))
  12759. ((nil minimal ancestors))
  12760. (t (save-excursion
  12761. (outline-next-heading)
  12762. (org-flag-heading nil)))))))
  12763. ;; Show all siblings.
  12764. (when (eq detail 'lineage) (org-show-siblings))
  12765. ;; Show ancestors, possibly with their children.
  12766. (when (memq detail '(ancestors lineage tree canonical t))
  12767. (save-excursion
  12768. (while (org-up-heading-safe)
  12769. (org-flag-heading nil)
  12770. (when (memq detail '(canonical t)) (org-show-entry))
  12771. (when (memq detail '(tree canonical t)) (org-show-children))))))
  12772. (defvar org-reveal-start-hook nil
  12773. "Hook run before revealing a location.")
  12774. (defun org-reveal (&optional siblings)
  12775. "Show current entry, hierarchy above it, and the following headline.
  12776. This can be used to show a consistent set of context around
  12777. locations exposed with `org-show-context'.
  12778. With optional argument SIBLINGS, on each level of the hierarchy all
  12779. siblings are shown. This repairs the tree structure to what it would
  12780. look like when opened with hierarchical calls to `org-cycle'.
  12781. With double optional argument \\[universal-argument] \\[universal-argument], \
  12782. go to the parent and show the
  12783. entire tree."
  12784. (interactive "P")
  12785. (run-hooks 'org-reveal-start-hook)
  12786. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12787. ((equal siblings '(16))
  12788. (save-excursion
  12789. (when (org-up-heading-safe)
  12790. (org-show-subtree)
  12791. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12792. (t (org-show-set-visibility 'lineage))))
  12793. (defun org-highlight-new-match (beg end)
  12794. "Highlight from BEG to END and mark the highlight is an occur headline."
  12795. (let ((ov (make-overlay beg end)))
  12796. (overlay-put ov 'face 'secondary-selection)
  12797. (overlay-put ov 'org-type 'org-occur)
  12798. (push ov org-occur-highlights)))
  12799. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12800. "Remove the occur highlights from the buffer.
  12801. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12802. from the `before-change-functions' in the current buffer."
  12803. (interactive)
  12804. (unless org-inhibit-highlight-removal
  12805. (mapc #'delete-overlay org-occur-highlights)
  12806. (setq org-occur-highlights nil)
  12807. (setq org-occur-parameters nil)
  12808. (unless noremove
  12809. (remove-hook 'before-change-functions
  12810. 'org-remove-occur-highlights 'local))))
  12811. ;;;; Priorities
  12812. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12813. "Regular expression matching the priority indicator.")
  12814. (defvar org-remove-priority-next-time nil)
  12815. (defun org-priority-up ()
  12816. "Increase the priority of the current item."
  12817. (interactive)
  12818. (org-priority 'up))
  12819. (defun org-priority-down ()
  12820. "Decrease the priority of the current item."
  12821. (interactive)
  12822. (org-priority 'down))
  12823. (defun org-priority (&optional action _show)
  12824. "Change the priority of an item.
  12825. ACTION can be `set', `up', `down', or a character."
  12826. (interactive "P")
  12827. (if (equal action '(4))
  12828. (org-show-priority)
  12829. (unless org-enable-priority-commands
  12830. (user-error "Priority commands are disabled"))
  12831. (setq action (or action 'set))
  12832. (let (current new news have remove)
  12833. (save-excursion
  12834. (org-back-to-heading t)
  12835. (when (looking-at org-priority-regexp)
  12836. (setq current (string-to-char (match-string 2))
  12837. have t))
  12838. (cond
  12839. ((eq action 'remove)
  12840. (setq remove t new ?\ ))
  12841. ((or (eq action 'set)
  12842. (integerp action))
  12843. (if (not (eq action 'set))
  12844. (setq new action)
  12845. (message "Priority %c-%c, SPC to remove: "
  12846. org-highest-priority org-lowest-priority)
  12847. (save-match-data
  12848. (setq new (read-char-exclusive))))
  12849. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12850. (= (upcase org-lowest-priority) org-lowest-priority))
  12851. (setq new (upcase new)))
  12852. (cond ((equal new ?\ ) (setq remove t))
  12853. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12854. (user-error "Priority must be between `%c' and `%c'"
  12855. org-highest-priority org-lowest-priority))))
  12856. ((eq action 'up)
  12857. (setq new (if have
  12858. (1- current) ; normal cycling
  12859. ;; last priority was empty
  12860. (if (eq last-command this-command)
  12861. org-lowest-priority ; wrap around empty to lowest
  12862. ;; default
  12863. (if org-priority-start-cycle-with-default
  12864. org-default-priority
  12865. (1- org-default-priority))))))
  12866. ((eq action 'down)
  12867. (setq new (if have
  12868. (1+ current) ; normal cycling
  12869. ;; last priority was empty
  12870. (if (eq last-command this-command)
  12871. org-highest-priority ; wrap around empty to highest
  12872. ;; default
  12873. (if org-priority-start-cycle-with-default
  12874. org-default-priority
  12875. (1+ org-default-priority))))))
  12876. (t (user-error "Invalid action")))
  12877. (when (or (< (upcase new) org-highest-priority)
  12878. (> (upcase new) org-lowest-priority))
  12879. (if (and (memq action '(up down))
  12880. (not have) (not (eq last-command this-command)))
  12881. ;; `new' is from default priority
  12882. (error
  12883. "The default can not be set, see `org-default-priority' why")
  12884. ;; normal cycling: `new' is beyond highest/lowest priority
  12885. ;; and is wrapped around to the empty priority
  12886. (setq remove t)))
  12887. (setq news (format "%c" new))
  12888. (if have
  12889. (if remove
  12890. (replace-match "" t t nil 1)
  12891. (replace-match news t t nil 2))
  12892. (if remove
  12893. (user-error "No priority cookie found in line")
  12894. (let ((case-fold-search nil))
  12895. (looking-at org-todo-line-regexp))
  12896. (if (match-end 2)
  12897. (progn
  12898. (goto-char (match-end 2))
  12899. (insert " [#" news "]"))
  12900. (goto-char (match-beginning 3))
  12901. (insert "[#" news "] "))))
  12902. (org-set-tags nil 'align))
  12903. (if remove
  12904. (message "Priority removed")
  12905. (message "Priority of current item set to %s" news)))))
  12906. (defun org-show-priority ()
  12907. "Show the priority of the current item.
  12908. This priority is composed of the main priority given with the [#A] cookies,
  12909. and by additional input from the age of a schedules or deadline entry."
  12910. (interactive)
  12911. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12912. (org-get-at-bol 'priority)
  12913. (save-excursion
  12914. (save-match-data
  12915. (beginning-of-line)
  12916. (and (looking-at org-heading-regexp)
  12917. (org-get-priority (match-string 0))))))))
  12918. (message "Priority is %d" (if pri pri -1000))))
  12919. (defun org-get-priority (s)
  12920. "Find priority cookie and return priority."
  12921. (save-match-data
  12922. (if (functionp org-get-priority-function)
  12923. (funcall org-get-priority-function)
  12924. (if (not (string-match org-priority-regexp s))
  12925. (* 1000 (- org-lowest-priority org-default-priority))
  12926. (* 1000 (- org-lowest-priority
  12927. (string-to-char (match-string 2 s))))))))
  12928. ;;;; Tags
  12929. (defvar org-agenda-archives-mode)
  12930. (defvar org-map-continue-from nil
  12931. "Position from where mapping should continue.
  12932. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12933. (defvar org-scanner-tags nil
  12934. "The current tag list while the tags scanner is running.")
  12935. (defvar org-trust-scanner-tags nil
  12936. "Should `org-get-tags-at' use the tags for the scanner.
  12937. This is for internal dynamical scoping only.
  12938. When this is non-nil, the function `org-get-tags-at' will return the value
  12939. of `org-scanner-tags' instead of building the list by itself. This
  12940. can lead to large speed-ups when the tags scanner is used in a file with
  12941. many entries, and when the list of tags is retrieved, for example to
  12942. obtain a list of properties. Building the tags list for each entry in such
  12943. a file becomes an N^2 operation - but with this variable set, it scales
  12944. as N.")
  12945. (defvar org--matcher-tags-todo-only nil)
  12946. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12947. "Scan headline tags with inheritance and produce output ACTION.
  12948. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12949. or `agenda' to produce an entry list for an agenda view. It can also be
  12950. a Lisp form or a function that should be called at each matched headline, in
  12951. this case the return value is a list of all return values from these calls.
  12952. MATCHER is a function accepting three arguments, returning
  12953. a non-nil value whenever a given set of tags qualifies a headline
  12954. for inclusion. See `org-make-tags-matcher' for more information.
  12955. As a special case, it can also be set to t (respectively nil) in
  12956. order to match all (respectively none) headline.
  12957. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12958. keyword are included in the output.
  12959. START-LEVEL can be a string with asterisks, reducing the scope to
  12960. headlines matching this string."
  12961. (require 'org-agenda)
  12962. (let* ((re (concat "^"
  12963. (if start-level
  12964. ;; Get the correct level to match
  12965. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12966. org-outline-regexp)
  12967. " *\\(\\<\\("
  12968. (mapconcat #'regexp-quote org-todo-keywords-1 "\\|")
  12969. "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$"))
  12970. (props (list 'face 'default
  12971. 'done-face 'org-agenda-done
  12972. 'undone-face 'default
  12973. 'mouse-face 'highlight
  12974. 'org-not-done-regexp org-not-done-regexp
  12975. 'org-todo-regexp org-todo-regexp
  12976. 'org-complex-heading-regexp org-complex-heading-regexp
  12977. 'help-echo
  12978. (format "mouse-2 or RET jump to org file %s"
  12979. (abbreviate-file-name
  12980. (or (buffer-file-name (buffer-base-buffer))
  12981. (buffer-name (buffer-base-buffer)))))))
  12982. (org-map-continue-from nil)
  12983. lspos tags tags-list
  12984. (tags-alist (list (cons 0 org-file-tags)))
  12985. (llast 0) rtn rtn1 level category i txt
  12986. todo marker entry priority
  12987. ts-date ts-date-type ts-date-pair)
  12988. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12989. (setq action (list 'lambda nil action)))
  12990. (save-excursion
  12991. (goto-char (point-min))
  12992. (when (eq action 'sparse-tree)
  12993. (org-overview)
  12994. (org-remove-occur-highlights))
  12995. (while (let (case-fold-search)
  12996. (re-search-forward re nil t))
  12997. (setq org-map-continue-from nil)
  12998. (catch :skip
  12999. (setq todo
  13000. ;; TODO: is the 1-2 difference a bug?
  13001. (when (match-end 1) (match-string-no-properties 2))
  13002. tags (when (match-end 4) (match-string-no-properties 4)))
  13003. (goto-char (setq lspos (match-beginning 0)))
  13004. (setq level (org-reduced-level (org-outline-level))
  13005. category (org-get-category))
  13006. (when (eq action 'agenda)
  13007. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  13008. ts-date (car ts-date-pair)
  13009. ts-date-type (cdr ts-date-pair)))
  13010. (setq i llast llast level)
  13011. ;; remove tag lists from same and sublevels
  13012. (while (>= i level)
  13013. (when (setq entry (assoc i tags-alist))
  13014. (setq tags-alist (delete entry tags-alist)))
  13015. (setq i (1- i)))
  13016. ;; add the next tags
  13017. (when tags
  13018. (setq tags (org-split-string tags ":")
  13019. tags-alist
  13020. (cons (cons level tags) tags-alist)))
  13021. ;; compile tags for current headline
  13022. (setq tags-list
  13023. (if org-use-tag-inheritance
  13024. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  13025. tags)
  13026. org-scanner-tags tags-list)
  13027. (when org-use-tag-inheritance
  13028. (setcdr (car tags-alist)
  13029. (mapcar (lambda (x)
  13030. (setq x (copy-sequence x))
  13031. (org-add-prop-inherited x))
  13032. (cdar tags-alist))))
  13033. (when (and tags org-use-tag-inheritance
  13034. (or (not (eq t org-use-tag-inheritance))
  13035. org-tags-exclude-from-inheritance))
  13036. ;; Selective inheritance, remove uninherited ones.
  13037. (setcdr (car tags-alist)
  13038. (org-remove-uninherited-tags (cdar tags-alist))))
  13039. (when (and
  13040. ;; eval matcher only when the todo condition is OK
  13041. (and (or (not todo-only) (member todo org-not-done-keywords))
  13042. (if (functionp matcher)
  13043. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13044. (funcall matcher todo tags-list level))
  13045. matcher))
  13046. ;; Call the skipper, but return t if it does not
  13047. ;; skip, so that the `and' form continues evaluating.
  13048. (progn
  13049. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13050. t)
  13051. ;; Check if timestamps are deselecting this entry
  13052. (or (not todo-only)
  13053. (and (member todo org-not-done-keywords)
  13054. (or (not org-agenda-tags-todo-honor-ignore-options)
  13055. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13056. ;; select this headline
  13057. (cond
  13058. ((eq action 'sparse-tree)
  13059. (and org-highlight-sparse-tree-matches
  13060. (org-get-heading) (match-end 0)
  13061. (org-highlight-new-match
  13062. (match-beginning 1) (match-end 1)))
  13063. (org-show-context 'tags-tree))
  13064. ((eq action 'agenda)
  13065. (setq txt (org-agenda-format-item
  13066. ""
  13067. (concat
  13068. (if (eq org-tags-match-list-sublevels 'indented)
  13069. (make-string (1- level) ?.) "")
  13070. (org-get-heading))
  13071. (make-string level ?\s)
  13072. category
  13073. tags-list)
  13074. priority (org-get-priority txt))
  13075. (goto-char lspos)
  13076. (setq marker (org-agenda-new-marker))
  13077. (org-add-props txt props
  13078. 'org-marker marker 'org-hd-marker marker 'org-category category
  13079. 'todo-state todo
  13080. 'ts-date ts-date
  13081. 'priority priority
  13082. 'type (concat "tagsmatch" ts-date-type))
  13083. (push txt rtn))
  13084. ((functionp action)
  13085. (setq org-map-continue-from nil)
  13086. (save-excursion
  13087. (setq rtn1 (funcall action))
  13088. (push rtn1 rtn)))
  13089. (t (user-error "Invalid action")))
  13090. ;; if we are to skip sublevels, jump to end of subtree
  13091. (unless org-tags-match-list-sublevels
  13092. (org-end-of-subtree t)
  13093. (backward-char 1))))
  13094. ;; Get the correct position from where to continue
  13095. (if org-map-continue-from
  13096. (goto-char org-map-continue-from)
  13097. (and (= (point) lspos) (end-of-line 1)))))
  13098. (when (and (eq action 'sparse-tree)
  13099. (not org-sparse-tree-open-archived-trees))
  13100. (org-hide-archived-subtrees (point-min) (point-max)))
  13101. (nreverse rtn)))
  13102. (defun org-remove-uninherited-tags (tags)
  13103. "Remove all tags that are not inherited from the list TAGS."
  13104. (cond
  13105. ((eq org-use-tag-inheritance t)
  13106. (if org-tags-exclude-from-inheritance
  13107. (org-delete-all org-tags-exclude-from-inheritance tags)
  13108. tags))
  13109. ((not org-use-tag-inheritance) nil)
  13110. ((stringp org-use-tag-inheritance)
  13111. (delq nil (mapcar
  13112. (lambda (x)
  13113. (if (and (string-match org-use-tag-inheritance x)
  13114. (not (member x org-tags-exclude-from-inheritance)))
  13115. x nil))
  13116. tags)))
  13117. ((listp org-use-tag-inheritance)
  13118. (delq nil (mapcar
  13119. (lambda (x)
  13120. (if (member x org-use-tag-inheritance) x nil))
  13121. tags)))))
  13122. (defun org-match-sparse-tree (&optional todo-only match)
  13123. "Create a sparse tree according to tags string MATCH.
  13124. MATCH can contain positive and negative selection of tags, like
  13125. \"+WORK+URGENT-WITHBOSS\".
  13126. If optional argument TODO-ONLY is non-nil, only select lines that are
  13127. also TODO lines."
  13128. (interactive "P")
  13129. (org-agenda-prepare-buffers (list (current-buffer)))
  13130. (let ((org--matcher-tags-todo-only todo-only))
  13131. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  13132. org--matcher-tags-todo-only)))
  13133. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13134. (defvar org-cached-props nil)
  13135. (defun org-cached-entry-get (pom property)
  13136. (if (or (eq t org-use-property-inheritance)
  13137. (and (stringp org-use-property-inheritance)
  13138. (let ((case-fold-search t))
  13139. (org-string-match-p org-use-property-inheritance property)))
  13140. (and (listp org-use-property-inheritance)
  13141. (member-ignore-case property org-use-property-inheritance)))
  13142. ;; Caching is not possible, check it directly.
  13143. (org-entry-get pom property 'inherit)
  13144. ;; Get all properties, so we can do complicated checks easily.
  13145. (cdr (assoc-string property
  13146. (or org-cached-props
  13147. (setq org-cached-props (org-entry-properties pom)))
  13148. t))))
  13149. (defun org-global-tags-completion-table (&optional files)
  13150. "Return the list of all tags in all agenda buffer/files.
  13151. Optional FILES argument is a list of files which can be used
  13152. instead of the agenda files."
  13153. (save-excursion
  13154. (org-uniquify
  13155. (delq nil
  13156. (apply #'append
  13157. (mapcar
  13158. (lambda (file)
  13159. (set-buffer (find-file-noselect file))
  13160. (mapcar (lambda (x)
  13161. (and (stringp (car-safe x))
  13162. (list (car-safe x))))
  13163. (or org-current-tag-alist (org-get-buffer-tags))))
  13164. (if (car-safe files) files
  13165. (org-agenda-files))))))))
  13166. (defun org-make-tags-matcher (match)
  13167. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13168. Returns a cons of the selection string MATCH and a function
  13169. implementing the matcher.
  13170. The matcher is to be called at an Org entry, with point on the
  13171. headline, and returns non-nil if the entry matches the selection
  13172. string MATCH. It must be called with three arguments: the TODO
  13173. keyword at the entry (or nil if none), the list of all tags at
  13174. the entry including inherited ones and the reduced level of the
  13175. headline. Additionally, the category of the entry, if any, must
  13176. be specified as the text property `org-category' on the headline.
  13177. This function sets the variable `org--matcher-tags-todo-only' to
  13178. a non-nil value if the matcher restricts matching to TODO
  13179. entries, otherwise it is not touched.
  13180. See also `org-scan-tags'."
  13181. (unless match
  13182. ;; Get a new match request, with completion against the global
  13183. ;; tags table and the local tags in current buffer.
  13184. (let ((org-last-tags-completion-table
  13185. (org-uniquify
  13186. (delq nil (append (org-get-buffer-tags)
  13187. (org-global-tags-completion-table))))))
  13188. (setq match
  13189. (completing-read
  13190. "Match: "
  13191. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13192. (let ((match0 match)
  13193. (re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)")
  13194. (start 0)
  13195. tagsmatch todomatch tagsmatcher todomatcher)
  13196. ;; Expand group tags.
  13197. (setq match (org-tags-expand match))
  13198. ;; Check if there is a TODO part of this match, which would be the
  13199. ;; part after a "/". To make sure that this slash is not part of
  13200. ;; a property value to be matched against, we also check that
  13201. ;; there is no / after that slash. First, find the last slash.
  13202. (let ((s 0))
  13203. (while (string-match "/+" match s)
  13204. (setq start (match-beginning 0))
  13205. (setq s (match-end 0))))
  13206. (if (and (string-match "/+" match start)
  13207. (not (string-match-p "\"" match start)))
  13208. ;; Match contains also a TODO-matching request.
  13209. (progn
  13210. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13211. (setq todomatch (substring match (match-end 0)))
  13212. (when (string-match "\\`!" todomatch)
  13213. (setq org--matcher-tags-todo-only t)
  13214. (setq todomatch (substring todomatch 1)))
  13215. (when (string-match "\\`\\s-*\\'" todomatch)
  13216. (setq todomatch nil)))
  13217. ;; Only matching tags.
  13218. (setq tagsmatch match)
  13219. (setq todomatch nil))
  13220. ;; Make the tags matcher.
  13221. (when (org-string-nw-p tagsmatch)
  13222. (let ((orlist nil)
  13223. (orterms (org-split-string tagsmatch "|"))
  13224. term)
  13225. (while (setq term (pop orterms))
  13226. (while (and (equal (substring term -1) "\\") orterms)
  13227. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13228. (while (string-match re term)
  13229. (let* ((rest (substring term (match-end 0)))
  13230. (minus (and (match-end 1)
  13231. (equal (match-string 1 term) "-")))
  13232. (tag (save-match-data
  13233. (replace-regexp-in-string
  13234. "\\\\-" "-" (match-string 2 term))))
  13235. (regexp (eq (string-to-char tag) ?{))
  13236. (levelp (match-end 4))
  13237. (propp (match-end 5))
  13238. (mm
  13239. (cond
  13240. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13241. (levelp
  13242. `(,(org-op-to-function (match-string 3 term))
  13243. level
  13244. ,(string-to-number (match-string 4 term))))
  13245. (propp
  13246. (let* ((gv (pcase (upcase (match-string 5 term))
  13247. ("CATEGORY"
  13248. '(get-text-property (point) 'org-category))
  13249. ("TODO" 'todo)
  13250. (p `(org-cached-entry-get nil ,p))))
  13251. (pv (match-string 7 term))
  13252. (regexp (eq (string-to-char pv) ?{))
  13253. (strp (eq (string-to-char pv) ?\"))
  13254. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13255. (po (org-op-to-function (match-string 6 term)
  13256. (if timep 'time strp))))
  13257. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13258. (when timep (setq pv (org-matcher-time pv)))
  13259. (cond ((and regexp (eq po 'org<>))
  13260. `(not (string-match ,pv (or ,gv ""))))
  13261. (regexp `(string-match ,pv (or ,gv "")))
  13262. (strp `(,po (or ,gv "") ,pv))
  13263. (t
  13264. `(,po
  13265. (string-to-number (or ,gv ""))
  13266. ,(string-to-number pv))))))
  13267. (t `(member ,tag tags-list)))))
  13268. (push (if minus `(not ,mm) mm) tagsmatcher)
  13269. (setq term rest)))
  13270. (push `(and ,@tagsmatcher) orlist)
  13271. (setq tagsmatcher nil))
  13272. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13273. ;; Make the TODO matcher.
  13274. (when (org-string-nw-p todomatch)
  13275. (let ((orlist nil))
  13276. (dolist (term (org-split-string todomatch "|"))
  13277. (while (string-match re term)
  13278. (let* ((minus (and (match-end 1)
  13279. (equal (match-string 1 term) "-")))
  13280. (kwd (match-string 2 term))
  13281. (regexp (eq (string-to-char kwd) ?{))
  13282. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13283. `(equal todo ,kwd))))
  13284. (push (if minus `(not ,mm) mm) todomatcher))
  13285. (setq term (substring term (match-end 0))))
  13286. (push (if (> (length todomatcher) 1)
  13287. (cons 'and todomatcher)
  13288. (car todomatcher))
  13289. orlist)
  13290. (setq todomatcher nil))
  13291. (setq todomatcher (cons 'or orlist))))
  13292. ;; Return the string and function of the matcher. If no
  13293. ;; tags-specific or todo-specific matcher exists, match
  13294. ;; everything.
  13295. (let ((matcher (if (and tagsmatcher todomatcher)
  13296. `(and ,tagsmatcher ,todomatcher)
  13297. (or tagsmatcher todomatcher t))))
  13298. (when org--matcher-tags-todo-only
  13299. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13300. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13301. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13302. "Expand group tags in MATCH.
  13303. This replaces every group tag in MATCH with a regexp tag search.
  13304. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13305. will be replaced like this:
  13306. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13307. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13308. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13309. Replacing by a regexp preserves the structure of the match.
  13310. E.g., this expansion
  13311. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13312. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13313. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13314. A group tag in MATCH can contain regular expressions of its own.
  13315. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13316. will be replaced like this:
  13317. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13318. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13319. assumed to be a single group tag, and the function will return
  13320. the list of tags in this group.
  13321. When DOWNCASE is non-nil, expand downcased TAGS."
  13322. (if org-group-tags
  13323. (let* ((case-fold-search t)
  13324. (stable org-mode-syntax-table)
  13325. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13326. (taggroups (if downcased
  13327. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13328. taggroups)
  13329. taggroups))
  13330. (taggroups-keys (mapcar #'car taggroups))
  13331. (return-match (if downcased (downcase match) match))
  13332. (count 0)
  13333. (work-already-expanded tags-already-expanded)
  13334. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13335. ;; @ and _ are allowed as word-components in tags.
  13336. (modify-syntax-entry ?@ "w" stable)
  13337. (modify-syntax-entry ?_ "w" stable)
  13338. ;; Temporarily replace regexp-expressions in the match-expression.
  13339. (while (string-match "{.+?}" return-match)
  13340. (cl-incf count)
  13341. (push (match-string 0 return-match) regexps-in-match)
  13342. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13343. (while (and taggroups-keys
  13344. (with-syntax-table stable
  13345. (string-match
  13346. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13347. (regexp-opt taggroups-keys) "\\>\\)")
  13348. return-match)))
  13349. (let* ((dir (match-string 1 return-match))
  13350. (tag (match-string 2 return-match))
  13351. (tag (if downcased (downcase tag) tag)))
  13352. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13353. (member tag work-already-expanded))
  13354. (setq tags-in-group (assoc tag taggroups))
  13355. (push tag work-already-expanded)
  13356. ;; Recursively expand each tag in the group, if the tag hasn't
  13357. ;; already been expanded. Restore the match-data after all recursive calls.
  13358. (save-match-data
  13359. (let (tags-expanded)
  13360. (dolist (x (cdr tags-in-group))
  13361. (if (and (member x taggroups-keys)
  13362. (not (member x work-already-expanded)))
  13363. (setq tags-expanded
  13364. (delete-dups
  13365. (append
  13366. (org-tags-expand x t downcased
  13367. work-already-expanded)
  13368. tags-expanded)))
  13369. (setq tags-expanded
  13370. (append (list x) tags-expanded)))
  13371. (setq work-already-expanded
  13372. (delete-dups
  13373. (append tags-expanded
  13374. work-already-expanded))))
  13375. (setq tags-in-group
  13376. (delete-dups (cons (car tags-in-group)
  13377. tags-expanded)))))
  13378. ;; Filter tag-regexps from tags.
  13379. (setq regexp-in-group-escaped
  13380. (delq nil (mapcar (lambda (x)
  13381. (if (stringp x)
  13382. (and (equal "{" (substring x 0 1))
  13383. (equal "}" (substring x -1))
  13384. x)
  13385. x))
  13386. tags-in-group))
  13387. regexp-in-group
  13388. (mapcar (lambda (x)
  13389. (substring x 1 -1))
  13390. regexp-in-group-escaped)
  13391. tags-in-group
  13392. (delq nil (mapcar (lambda (x)
  13393. (if (stringp x)
  13394. (and (not (equal "{" (substring x 0 1)))
  13395. (not (equal "}" (substring x -1)))
  13396. x)
  13397. x))
  13398. tags-in-group)))
  13399. ;; If single-as-list, do no more in the while-loop.
  13400. (if (not single-as-list)
  13401. (progn
  13402. (when regexp-in-group
  13403. (setq regexp-in-group
  13404. (concat "\\|"
  13405. (mapconcat 'identity regexp-in-group
  13406. "\\|"))))
  13407. (setq tags-in-group
  13408. (concat dir
  13409. "{\\<"
  13410. (regexp-opt tags-in-group)
  13411. "\\>"
  13412. regexp-in-group
  13413. "}"))
  13414. (when (stringp tags-in-group)
  13415. (org-add-props tags-in-group '(grouptag t)))
  13416. (setq return-match
  13417. (replace-match tags-in-group t t return-match)))
  13418. (setq tags-in-group
  13419. (append regexp-in-group-escaped tags-in-group))))
  13420. (setq taggroups-keys (delete tag taggroups-keys))))
  13421. ;; Add the regular expressions back into the match-expression again.
  13422. (while regexps-in-match
  13423. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13424. (pop regexps-in-match)
  13425. return-match t t))
  13426. (cl-decf count))
  13427. (if single-as-list
  13428. (if tags-in-group tags-in-group (list return-match))
  13429. return-match))
  13430. (if single-as-list
  13431. (list (if downcased (downcase match) match))
  13432. match)))
  13433. (defun org-op-to-function (op &optional stringp)
  13434. "Turn an operator into the appropriate function."
  13435. (setq op
  13436. (cond
  13437. ((equal op "<" ) '(< string< org-time<))
  13438. ((equal op ">" ) '(> org-string> org-time>))
  13439. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13440. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13441. ((member op '("=" "==")) '(= string= org-time=))
  13442. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13443. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13444. (defun org<> (a b) (not (= a b)))
  13445. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13446. (defun org-string>= (a b) (not (string< a b)))
  13447. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13448. (defun org-string<> (a b) (not (string= a b)))
  13449. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13450. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13451. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13452. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13453. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13454. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13455. (defun org-2ft (s)
  13456. "Convert S to a floating point time.
  13457. If S is already a number, just return it. If it is a string, parse
  13458. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13459. (cond
  13460. ((numberp s) s)
  13461. ((stringp s)
  13462. (condition-case nil
  13463. (float-time (apply 'encode-time (org-parse-time-string s)))
  13464. (error 0.)))
  13465. (t 0.)))
  13466. (defun org-time-today ()
  13467. "Time in seconds today at 0:00.
  13468. Returns the float number of seconds since the beginning of the
  13469. epoch to the beginning of today (00:00)."
  13470. (float-time (apply 'encode-time
  13471. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13472. (defun org-matcher-time (s)
  13473. "Interpret a time comparison value."
  13474. (save-match-data
  13475. (cond
  13476. ((string= s "<now>") (float-time))
  13477. ((string= s "<today>") (org-time-today))
  13478. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13479. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13480. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13481. (+ (org-time-today)
  13482. (* (string-to-number (match-string 1 s))
  13483. (cdr (assoc (match-string 2 s)
  13484. '(("d" . 86400.0) ("w" . 604800.0)
  13485. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13486. (t (org-2ft s)))))
  13487. (defun org-match-any-p (re list)
  13488. "Does re match any element of list?"
  13489. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13490. (delq nil list))
  13491. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13492. (defvar org-tags-overlay (make-overlay 1 1))
  13493. (delete-overlay org-tags-overlay)
  13494. (defun org-get-local-tags-at (&optional pos)
  13495. "Get a list of tags defined in the current headline."
  13496. (org-get-tags-at pos 'local))
  13497. (defun org-get-local-tags ()
  13498. "Get a list of tags defined in the current headline."
  13499. (org-get-tags-at nil 'local))
  13500. (defun org-get-tags-at (&optional pos local)
  13501. "Get a list of all headline tags applicable at POS.
  13502. POS defaults to point. If tags are inherited, the list contains
  13503. the targets in the same sequence as the headlines appear, i.e.
  13504. the tags of the current headline come last.
  13505. When LOCAL is non-nil, only return tags from the current headline,
  13506. ignore inherited ones."
  13507. (interactive)
  13508. (if (and org-trust-scanner-tags
  13509. (or (not pos) (equal pos (point)))
  13510. (not local))
  13511. org-scanner-tags
  13512. (let (tags ltags lastpos parent)
  13513. (save-excursion
  13514. (save-restriction
  13515. (widen)
  13516. (goto-char (or pos (point)))
  13517. (save-match-data
  13518. (catch 'done
  13519. (condition-case nil
  13520. (progn
  13521. (org-back-to-heading t)
  13522. (while (not (equal lastpos (point)))
  13523. (setq lastpos (point))
  13524. (when (looking-at ".+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13525. (setq ltags (org-split-string
  13526. (match-string-no-properties 1) ":"))
  13527. (when parent
  13528. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13529. (setq tags (append
  13530. (if parent
  13531. (org-remove-uninherited-tags ltags)
  13532. ltags)
  13533. tags)))
  13534. (or org-use-tag-inheritance (throw 'done t))
  13535. (when local (throw 'done t))
  13536. (or (org-up-heading-safe) (error nil))
  13537. (setq parent t)))
  13538. (error nil)))))
  13539. (if local
  13540. tags
  13541. (reverse (delete-dups
  13542. (reverse (append
  13543. (org-remove-uninherited-tags
  13544. org-file-tags)
  13545. tags)))))))))
  13546. (defun org-add-prop-inherited (s)
  13547. (add-text-properties 0 (length s) '(inherited t) s)
  13548. s)
  13549. (defun org-toggle-tag (tag &optional onoff)
  13550. "Toggle the tag TAG for the current line.
  13551. If ONOFF is `on' or `off', don't toggle but set to this state."
  13552. (let (res current)
  13553. (save-excursion
  13554. (org-back-to-heading t)
  13555. (if (re-search-forward "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$"
  13556. (point-at-eol) t)
  13557. (progn
  13558. (setq current (match-string 1))
  13559. (replace-match ""))
  13560. (setq current ""))
  13561. (setq current (nreverse (org-split-string current ":")))
  13562. (cond
  13563. ((eq onoff 'on)
  13564. (setq res t)
  13565. (or (member tag current) (push tag current)))
  13566. ((eq onoff 'off)
  13567. (or (not (member tag current)) (setq current (delete tag current))))
  13568. (t (if (member tag current)
  13569. (setq current (delete tag current))
  13570. (setq res t)
  13571. (push tag current))))
  13572. (end-of-line 1)
  13573. (if current
  13574. (progn
  13575. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13576. (org-set-tags nil t))
  13577. (delete-horizontal-space))
  13578. (run-hooks 'org-after-tags-change-hook))
  13579. res))
  13580. (defun org-align-tags-here (to-col)
  13581. ;; Assumes that this is a headline
  13582. "Align tags on the current headline to TO-COL."
  13583. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13584. (beginning-of-line 1)
  13585. (if (and (looking-at ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13586. (< pos (match-beginning 2)))
  13587. (progn
  13588. (setq tags-l (string-width (match-string 2)))
  13589. (goto-char (match-beginning 1))
  13590. (insert " ")
  13591. (delete-region (point) (1+ (match-beginning 2)))
  13592. (setq ncol (max (current-column)
  13593. (1+ col)
  13594. (if (> to-col 0)
  13595. to-col
  13596. (- (abs to-col) tags-l))))
  13597. (setq p (point))
  13598. (insert (make-string (- ncol (current-column)) ?\ ))
  13599. (setq ncol (current-column))
  13600. (when indent-tabs-mode (tabify p (point-at-eol)))
  13601. (org-move-to-column (min ncol col)))
  13602. (goto-char pos))))
  13603. (defun org-set-tags-command (&optional arg just-align)
  13604. "Call the set-tags command for the current entry."
  13605. (interactive "P")
  13606. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13607. (org-set-tags arg just-align)
  13608. (save-excursion
  13609. (unless (and (org-region-active-p)
  13610. org-loop-over-headlines-in-active-region)
  13611. (org-back-to-heading t))
  13612. (org-set-tags arg just-align))))
  13613. (defun org-set-tags-to (data)
  13614. "Set the tags of the current entry to DATA, replacing the current tags.
  13615. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13616. If DATA is nil or the empty string, any tags will be removed."
  13617. (interactive "sTags: ")
  13618. (setq data
  13619. (cond
  13620. ((eq data nil) "")
  13621. ((equal data "") "")
  13622. ((stringp data)
  13623. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13624. ":"))
  13625. ((listp data)
  13626. (concat ":" (mapconcat 'identity data ":") ":"))))
  13627. (when data
  13628. (save-excursion
  13629. (org-back-to-heading t)
  13630. (when (looking-at org-complex-heading-regexp)
  13631. (if (match-end 5)
  13632. (progn
  13633. (goto-char (match-beginning 5))
  13634. (insert data)
  13635. (delete-region (point) (point-at-eol))
  13636. (org-set-tags nil 'align))
  13637. (goto-char (point-at-eol))
  13638. (insert " " data)
  13639. (org-set-tags nil 'align)))
  13640. (beginning-of-line 1)
  13641. (when (looking-at ".*?\\([ \t]+\\)$")
  13642. (delete-region (match-beginning 1) (match-end 1))))))
  13643. (defun org-align-all-tags ()
  13644. "Align the tags in all headings."
  13645. (interactive)
  13646. (save-excursion
  13647. (or (ignore-errors (org-back-to-heading t))
  13648. (outline-next-heading))
  13649. (if (org-at-heading-p)
  13650. (org-set-tags t)
  13651. (message "No headings"))))
  13652. (defvar org-indent-indentation-per-level)
  13653. (defun org-set-tags (&optional arg just-align)
  13654. "Set the tags for the current headline.
  13655. With prefix ARG, realign all tags in headings in the current buffer.
  13656. When JUST-ALIGN is non-nil, only align tags."
  13657. (interactive "P")
  13658. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13659. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13660. 'region-start-level
  13661. 'region))
  13662. org-loop-over-headlines-in-active-region)
  13663. (org-map-entries
  13664. ;; We don't use ARG and JUST-ALIGN here because these args
  13665. ;; are not useful when looping over headlines.
  13666. #'org-set-tags
  13667. org-loop-over-headlines-in-active-region
  13668. cl
  13669. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13670. (let ((org-setting-tags t))
  13671. (if arg
  13672. (save-excursion
  13673. (goto-char (point-min))
  13674. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13675. (while (re-search-forward org-outline-regexp-bol nil t)
  13676. (org-set-tags nil t)
  13677. (end-of-line)))
  13678. (message "All tags realigned to column %d" org-tags-column))
  13679. (let* ((current (org-get-tags-string))
  13680. (col (current-column))
  13681. (tags
  13682. (if just-align current
  13683. ;; Get a new set of tags from the user.
  13684. (save-excursion
  13685. (let* ((seen)
  13686. (table
  13687. (setq
  13688. org-last-tags-completion-table
  13689. ;; Uniquify tags in alists, yet preserve
  13690. ;; structure (i.e., keywords).
  13691. (delq nil
  13692. (mapcar
  13693. (lambda (pair)
  13694. (let ((head (car pair)))
  13695. (cond ((symbolp head) pair)
  13696. ((member head seen) nil)
  13697. (t (push head seen)
  13698. pair))))
  13699. (append
  13700. (or org-current-tag-alist
  13701. (org-get-buffer-tags))
  13702. (and
  13703. org-complete-tags-always-offer-all-agenda-tags
  13704. (org-global-tags-completion-table
  13705. (org-agenda-files))))))))
  13706. (current-tags (org-split-string current ":"))
  13707. (inherited-tags
  13708. (nreverse (nthcdr (length current-tags)
  13709. (nreverse (org-get-tags-at))))))
  13710. (replace-regexp-in-string
  13711. "\\([-+&]+\\|,\\)"
  13712. ":"
  13713. (if (or (eq t org-use-fast-tag-selection)
  13714. (and org-use-fast-tag-selection
  13715. (delq nil (mapcar #'cdr table))))
  13716. (org-fast-tag-selection
  13717. current-tags inherited-tags table
  13718. (and org-fast-tag-selection-include-todo
  13719. org-todo-key-alist))
  13720. (let ((org-add-colon-after-tag-completion
  13721. (< 1 (length table))))
  13722. (org-trim
  13723. (completing-read
  13724. "Tags: "
  13725. #'org-tags-completion-function
  13726. nil nil current 'org-tags-history))))))))))
  13727. (when org-tags-sort-function
  13728. (setq tags
  13729. (mapconcat
  13730. #'identity
  13731. (sort (org-split-string tags "[^[:alnum:]_@#%]+")
  13732. org-tags-sort-function)
  13733. ":")))
  13734. (if (not (org-string-nw-p tags)) (setq tags "")
  13735. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13736. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13737. ;; Insert new tags at the correct column
  13738. (beginning-of-line)
  13739. (let ((level (if (looking-at org-outline-regexp)
  13740. (- (match-end 0) (point) 1)
  13741. 1)))
  13742. (cond
  13743. ((and (equal current "") (equal tags "")))
  13744. ((re-search-forward
  13745. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13746. (line-end-position)
  13747. t)
  13748. (if (equal tags "") (replace-match "" t t)
  13749. (goto-char (match-beginning 0))
  13750. (let* ((c0 (current-column))
  13751. ;; Compute offset for the case of org-indent-mode
  13752. ;; active.
  13753. (di (if (org-bound-and-true-p org-indent-mode)
  13754. (* (1- org-indent-indentation-per-level)
  13755. (1- level))
  13756. 0))
  13757. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13758. (tc (+ org-tags-column
  13759. (if (> org-tags-column 0) (- di) di)))
  13760. (c1 (max (1+ c0)
  13761. (if (> tc 0) tc
  13762. (- (- tc) (string-width tags)))))
  13763. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13764. (replace-match rpl t t)
  13765. (when indent-tabs-mode (tabify p0 (point))))))
  13766. (t (error "Tags alignment failed"))))
  13767. (org-move-to-column col))
  13768. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13769. (defun org-change-tag-in-region (beg end tag off)
  13770. "Add or remove TAG for each entry in the region.
  13771. This works in the agenda, and also in an org-mode buffer."
  13772. (interactive
  13773. (list (region-beginning) (region-end)
  13774. (let ((org-last-tags-completion-table
  13775. (if (derived-mode-p 'org-mode)
  13776. (org-uniquify
  13777. (delq nil (append (org-get-buffer-tags)
  13778. (org-global-tags-completion-table))))
  13779. (org-global-tags-completion-table))))
  13780. (completing-read
  13781. "Tag: " 'org-tags-completion-function nil nil nil
  13782. 'org-tags-history))
  13783. (progn
  13784. (message "[s]et or [r]emove? ")
  13785. (equal (read-char-exclusive) ?r))))
  13786. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13787. (let ((agendap (equal major-mode 'org-agenda-mode))
  13788. l1 l2 m buf pos newhead (cnt 0))
  13789. (goto-char end)
  13790. (setq l2 (1- (org-current-line)))
  13791. (goto-char beg)
  13792. (setq l1 (org-current-line))
  13793. (cl-loop for l from l1 to l2 do
  13794. (org-goto-line l)
  13795. (setq m (get-text-property (point) 'org-hd-marker))
  13796. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13797. (and agendap m))
  13798. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13799. pos (if agendap m (point)))
  13800. (with-current-buffer buf
  13801. (save-excursion
  13802. (save-restriction
  13803. (goto-char pos)
  13804. (setq cnt (1+ cnt))
  13805. (org-toggle-tag tag (if off 'off 'on))
  13806. (setq newhead (org-get-heading)))))
  13807. (and agendap (org-agenda-change-all-lines newhead m))))
  13808. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13809. (defun org-tags-completion-function (string _predicate &optional flag)
  13810. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13811. (confirm (lambda (x) (stringp (car x)))))
  13812. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13813. (setq s1 (match-string 1 string)
  13814. s2 (match-string 2 string))
  13815. (setq s1 "" s2 string))
  13816. (cond
  13817. ((eq flag nil)
  13818. ;; try completion
  13819. (setq rtn (try-completion s2 ctable confirm))
  13820. (when (stringp rtn)
  13821. (setq rtn
  13822. (concat s1 s2 (substring rtn (length s2))
  13823. (if (and org-add-colon-after-tag-completion
  13824. (assoc rtn ctable))
  13825. ":" ""))))
  13826. rtn)
  13827. ((eq flag t)
  13828. ;; all-completions
  13829. (all-completions s2 ctable confirm))
  13830. ((eq flag 'lambda)
  13831. ;; exact match?
  13832. (assoc s2 ctable)))))
  13833. (defun org-fast-tag-insert (kwd tags face &optional end)
  13834. "Insert KDW, and the TAGS, the latter with face FACE.
  13835. Also insert END."
  13836. (insert (format "%-12s" (concat kwd ":"))
  13837. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13838. (or end "")))
  13839. (defun org-fast-tag-show-exit (flag)
  13840. (save-excursion
  13841. (org-goto-line 3)
  13842. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13843. (replace-match ""))
  13844. (when flag
  13845. (end-of-line 1)
  13846. (org-move-to-column (- (window-width) 19) t)
  13847. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13848. (defun org-set-current-tags-overlay (current prefix)
  13849. "Add an overlay to CURRENT tag with PREFIX."
  13850. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13851. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13852. (org-overlay-display org-tags-overlay (concat prefix s))))
  13853. (defvar org-last-tag-selection-key nil)
  13854. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13855. "Fast tag selection with single keys.
  13856. CURRENT is the current list of tags in the headline, INHERITED is the
  13857. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13858. possibly with grouping information. TODO-TABLE is a similar table with
  13859. TODO keywords, should these have keys assigned to them.
  13860. If the keys are nil, a-z are automatically assigned.
  13861. Returns the new tags string, or nil to not change the current settings."
  13862. (let* ((fulltable (append table todo-table))
  13863. (maxlen (apply 'max (mapcar
  13864. (lambda (x)
  13865. (if (stringp (car x)) (string-width (car x)) 0))
  13866. fulltable)))
  13867. (buf (current-buffer))
  13868. (expert (eq org-fast-tag-selection-single-key 'expert))
  13869. (buffer-tags nil)
  13870. (fwidth (+ maxlen 3 1 3))
  13871. (ncol (/ (- (window-width) 4) fwidth))
  13872. (i-face 'org-done)
  13873. (c-face 'org-todo)
  13874. tg cnt e c char c1 c2 ntable tbl rtn
  13875. ov-start ov-end ov-prefix
  13876. (exit-after-next org-fast-tag-selection-single-key)
  13877. (done-keywords org-done-keywords)
  13878. groups ingroup intaggroup)
  13879. (save-excursion
  13880. (beginning-of-line 1)
  13881. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  13882. (setq ov-start (match-beginning 1)
  13883. ov-end (match-end 1)
  13884. ov-prefix "")
  13885. (setq ov-start (1- (point-at-eol))
  13886. ov-end (1+ ov-start))
  13887. (skip-chars-forward "^\n\r")
  13888. (setq ov-prefix
  13889. (concat
  13890. (buffer-substring (1- (point)) (point))
  13891. (if (> (current-column) org-tags-column)
  13892. " "
  13893. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13894. (move-overlay org-tags-overlay ov-start ov-end)
  13895. (save-window-excursion
  13896. (if expert
  13897. (set-buffer (get-buffer-create " *Org tags*"))
  13898. (delete-other-windows)
  13899. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13900. (org-switch-to-buffer-other-window " *Org tags*"))
  13901. (erase-buffer)
  13902. (setq-local org-done-keywords done-keywords)
  13903. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13904. (org-fast-tag-insert "Current" current c-face "\n\n")
  13905. (org-fast-tag-show-exit exit-after-next)
  13906. (org-set-current-tags-overlay current ov-prefix)
  13907. (setq tbl fulltable char ?a cnt 0)
  13908. (while (setq e (pop tbl))
  13909. (cond
  13910. ((eq (car e) :startgroup)
  13911. (push '() groups) (setq ingroup t)
  13912. (unless (zerop cnt)
  13913. (setq cnt 0)
  13914. (insert "\n"))
  13915. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13916. ((eq (car e) :endgroup)
  13917. (setq ingroup nil cnt 0)
  13918. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13919. ((eq (car e) :startgrouptag)
  13920. (setq intaggroup t)
  13921. (unless (zerop cnt)
  13922. (setq cnt 0)
  13923. (insert "\n"))
  13924. (insert "[ "))
  13925. ((eq (car e) :endgrouptag)
  13926. (setq intaggroup nil cnt 0)
  13927. (insert "]\n"))
  13928. ((equal e '(:newline))
  13929. (unless (zerop cnt)
  13930. (setq cnt 0)
  13931. (insert "\n")
  13932. (setq e (car tbl))
  13933. (while (equal (car tbl) '(:newline))
  13934. (insert "\n")
  13935. (setq tbl (cdr tbl)))))
  13936. ((equal e '(:grouptags)) (insert " : "))
  13937. (t
  13938. (setq tg (copy-sequence (car e)) c2 nil)
  13939. (if (cdr e)
  13940. (setq c (cdr e))
  13941. ;; automatically assign a character.
  13942. (setq c1 (string-to-char
  13943. (downcase (substring
  13944. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13945. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13946. (while (or (rassoc char ntable) (rassoc char table))
  13947. (setq char (1+ char)))
  13948. (setq c2 c1))
  13949. (setq c (or c2 char)))
  13950. (when ingroup (push tg (car groups)))
  13951. (setq tg (org-add-props tg nil 'face
  13952. (cond
  13953. ((not (assoc tg table))
  13954. (org-get-todo-face tg))
  13955. ((member tg current) c-face)
  13956. ((member tg inherited) i-face))))
  13957. (when (equal (caar tbl) :grouptags)
  13958. (org-add-props tg nil 'face 'org-tag-group))
  13959. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13960. (insert "[" c "] " tg (make-string
  13961. (- fwidth 4 (length tg)) ?\ ))
  13962. (push (cons tg c) ntable)
  13963. (when (= (cl-incf cnt) ncol)
  13964. (insert "\n")
  13965. (when (or ingroup intaggroup) (insert " "))
  13966. (setq cnt 0)))))
  13967. (setq ntable (nreverse ntable))
  13968. (insert "\n")
  13969. (goto-char (point-min))
  13970. (unless expert (org-fit-window-to-buffer))
  13971. (setq rtn
  13972. (catch 'exit
  13973. (while t
  13974. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13975. (if (not groups) "no " "")
  13976. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13977. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13978. (setq org-last-tag-selection-key c)
  13979. (cond
  13980. ((= c ?\r) (throw 'exit t))
  13981. ((= c ?!)
  13982. (setq groups (not groups))
  13983. (goto-char (point-min))
  13984. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13985. ((= c ?\C-c)
  13986. (if (not expert)
  13987. (org-fast-tag-show-exit
  13988. (setq exit-after-next (not exit-after-next)))
  13989. (setq expert nil)
  13990. (delete-other-windows)
  13991. (set-window-buffer (split-window-vertically) " *Org tags*")
  13992. (org-switch-to-buffer-other-window " *Org tags*")
  13993. (org-fit-window-to-buffer)))
  13994. ((or (= c ?\C-g)
  13995. (and (= c ?q) (not (rassoc c ntable))))
  13996. (delete-overlay org-tags-overlay)
  13997. (setq quit-flag t))
  13998. ((= c ?\ )
  13999. (setq current nil)
  14000. (when exit-after-next (setq exit-after-next 'now)))
  14001. ((= c ?\t)
  14002. (condition-case nil
  14003. (setq tg (completing-read
  14004. "Tag: "
  14005. (or buffer-tags
  14006. (with-current-buffer buf
  14007. (setq buffer-tags
  14008. (org-get-buffer-tags))))))
  14009. (quit (setq tg "")))
  14010. (when (string-match "\\S-" tg)
  14011. (cl-pushnew (list tg) buffer-tags :test #'equal)
  14012. (if (member tg current)
  14013. (setq current (delete tg current))
  14014. (push tg current)))
  14015. (when exit-after-next (setq exit-after-next 'now)))
  14016. ((setq e (rassoc c todo-table) tg (car e))
  14017. (with-current-buffer buf
  14018. (save-excursion (org-todo tg)))
  14019. (when exit-after-next (setq exit-after-next 'now)))
  14020. ((setq e (rassoc c ntable) tg (car e))
  14021. (if (member tg current)
  14022. (setq current (delete tg current))
  14023. (cl-loop for g in groups do
  14024. (when (member tg g)
  14025. (dolist (x g) (setq current (delete x current)))))
  14026. (push tg current))
  14027. (when exit-after-next (setq exit-after-next 'now))))
  14028. ;; Create a sorted list
  14029. (setq current
  14030. (sort current
  14031. (lambda (a b)
  14032. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14033. (when (eq exit-after-next 'now) (throw 'exit t))
  14034. (goto-char (point-min))
  14035. (beginning-of-line 2)
  14036. (delete-region (point) (point-at-eol))
  14037. (org-fast-tag-insert "Current" current c-face)
  14038. (org-set-current-tags-overlay current ov-prefix)
  14039. (while (re-search-forward "\\[.\\] \\([[:alnum:]_@#%]+\\)" nil t)
  14040. (setq tg (match-string 1))
  14041. (add-text-properties
  14042. (match-beginning 1) (match-end 1)
  14043. (list 'face
  14044. (cond
  14045. ((member tg current) c-face)
  14046. ((member tg inherited) i-face)
  14047. (t (get-text-property (match-beginning 1) 'face))))))
  14048. (goto-char (point-min)))))
  14049. (delete-overlay org-tags-overlay)
  14050. (if rtn
  14051. (mapconcat 'identity current ":")
  14052. nil))))
  14053. (defun org-get-tags-string ()
  14054. "Get the TAGS string in the current headline."
  14055. (unless (org-at-heading-p t)
  14056. (user-error "Not on a heading"))
  14057. (save-excursion
  14058. (beginning-of-line 1)
  14059. (if (looking-at ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
  14060. (match-string-no-properties 1)
  14061. "")))
  14062. (defun org-get-tags ()
  14063. "Get the list of tags specified in the current headline."
  14064. (org-split-string (org-get-tags-string) ":"))
  14065. (defun org-get-buffer-tags ()
  14066. "Get a table of all tags used in the buffer, for completion."
  14067. (org-with-wide-buffer
  14068. (goto-char (point-min))
  14069. (let ((tag-re (concat org-outline-regexp-bol
  14070. "\\(?:.*?[ \t]\\)?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  14071. tags)
  14072. (while (re-search-forward tag-re nil t)
  14073. (dolist (tag (org-split-string (match-string-no-properties 1) ":"))
  14074. (push tag tags)))
  14075. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14076. ;;;; The mapping API
  14077. (defvar org-agenda-skip-comment-trees)
  14078. (defvar org-agenda-skip-function)
  14079. (defun org-map-entries (func &optional match scope &rest skip)
  14080. "Call FUNC at each headline selected by MATCH in SCOPE.
  14081. FUNC is a function or a lisp form. The function will be called without
  14082. arguments, with the cursor positioned at the beginning of the headline.
  14083. The return values of all calls to the function will be collected and
  14084. returned as a list.
  14085. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14086. does not need to preserve point. After evaluation, the cursor will be
  14087. moved to the end of the line (presumably of the headline of the
  14088. processed entry) and search continues from there. Under some
  14089. circumstances, this may not produce the wanted results. For example,
  14090. if you have removed (e.g. archived) the current (sub)tree it could
  14091. mean that the next entry will be skipped entirely. In such cases, you
  14092. can specify the position from where search should continue by making
  14093. FUNC set the variable `org-map-continue-from' to the desired buffer
  14094. position.
  14095. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14096. Only headlines that are matched by this query will be considered during
  14097. the iteration. When MATCH is nil or t, all headlines will be
  14098. visited by the iteration.
  14099. SCOPE determines the scope of this command. It can be any of:
  14100. nil The current buffer, respecting the restriction if any
  14101. tree The subtree started with the entry at point
  14102. region The entries within the active region, if any
  14103. region-start-level
  14104. The entries within the active region, but only those at
  14105. the same level than the first one.
  14106. file The current buffer, without restriction
  14107. file-with-archives
  14108. The current buffer, and any archives associated with it
  14109. agenda All agenda files
  14110. agenda-with-archives
  14111. All agenda files with any archive files associated with them
  14112. \(file1 file2 ...)
  14113. If this is a list, all files in the list will be scanned
  14114. The remaining args are treated as settings for the skipping facilities of
  14115. the scanner. The following items can be given here:
  14116. archive skip trees with the archive tag
  14117. comment skip trees with the COMMENT keyword
  14118. function or Emacs Lisp form:
  14119. will be used as value for `org-agenda-skip-function', so
  14120. whenever the function returns a position, FUNC will not be
  14121. called for that entry and search will continue from the
  14122. position returned
  14123. If your function needs to retrieve the tags including inherited tags
  14124. at the *current* entry, you can use the value of the variable
  14125. `org-scanner-tags' which will be much faster than getting the value
  14126. with `org-get-tags-at'. If your function gets properties with
  14127. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14128. to t around the call to `org-entry-properties' to get the same speedup.
  14129. Note that if your function moves around to retrieve tags and properties at
  14130. a *different* entry, you cannot use these techniques."
  14131. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14132. (not (org-region-active-p)))
  14133. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14134. (org-agenda-skip-archived-trees (memq 'archive skip))
  14135. (org-agenda-skip-comment-trees (memq 'comment skip))
  14136. (org-agenda-skip-function
  14137. (car (org-delete-all '(comment archive) skip)))
  14138. (org-tags-match-list-sublevels t)
  14139. (start-level (eq scope 'region-start-level))
  14140. matcher res
  14141. org-todo-keywords-for-agenda
  14142. org-done-keywords-for-agenda
  14143. org-todo-keyword-alist-for-agenda
  14144. org-tag-alist-for-agenda
  14145. org--matcher-tags-todo-only)
  14146. (cond
  14147. ((eq match t) (setq matcher t))
  14148. ((eq match nil) (setq matcher t))
  14149. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14150. (save-excursion
  14151. (save-restriction
  14152. (cond ((eq scope 'tree)
  14153. (org-back-to-heading t)
  14154. (org-narrow-to-subtree)
  14155. (setq scope nil))
  14156. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14157. (org-region-active-p))
  14158. ;; If needed, set start-level to a string like "2"
  14159. (when start-level
  14160. (save-excursion
  14161. (goto-char (region-beginning))
  14162. (unless (org-at-heading-p) (outline-next-heading))
  14163. (setq start-level (org-current-level))))
  14164. (narrow-to-region (region-beginning)
  14165. (save-excursion
  14166. (goto-char (region-end))
  14167. (unless (and (bolp) (org-at-heading-p))
  14168. (outline-next-heading))
  14169. (point)))
  14170. (setq scope nil)))
  14171. (if (not scope)
  14172. (progn
  14173. (org-agenda-prepare-buffers
  14174. (and buffer-file-name (list buffer-file-name)))
  14175. (setq res
  14176. (org-scan-tags
  14177. func matcher org--matcher-tags-todo-only start-level)))
  14178. ;; Get the right scope
  14179. (cond
  14180. ((and scope (listp scope) (symbolp (car scope)))
  14181. (setq scope (eval scope)))
  14182. ((eq scope 'agenda)
  14183. (setq scope (org-agenda-files t)))
  14184. ((eq scope 'agenda-with-archives)
  14185. (setq scope (org-agenda-files t))
  14186. (setq scope (org-add-archive-files scope)))
  14187. ((eq scope 'file)
  14188. (setq scope (and buffer-file-name (list buffer-file-name))))
  14189. ((eq scope 'file-with-archives)
  14190. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14191. (org-agenda-prepare-buffers scope)
  14192. (dolist (file scope)
  14193. (with-current-buffer (org-find-base-buffer-visiting file)
  14194. (save-excursion
  14195. (save-restriction
  14196. (widen)
  14197. (goto-char (point-min))
  14198. (setq res
  14199. (append
  14200. res
  14201. (org-scan-tags
  14202. func matcher org--matcher-tags-todo-only))))))))))
  14203. res)))
  14204. ;;; Properties API
  14205. (defconst org-special-properties
  14206. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14207. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14208. "The special properties valid in Org mode.
  14209. These are properties that are not defined in the property drawer,
  14210. but in some other way.")
  14211. (defconst org-default-properties
  14212. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14213. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14214. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14215. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14216. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14217. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14218. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14219. "Some properties that are used by Org mode for various purposes.
  14220. Being in this list makes sure that they are offered for completion.")
  14221. (defun org--valid-property-p (property)
  14222. "Non nil when string PROPERTY is a valid property name."
  14223. (not
  14224. (or (equal property "")
  14225. (org-string-match-p "\\s-" property))))
  14226. (defun org--update-property-plist (key val props)
  14227. "Associate KEY to VAL in alist PROPS.
  14228. Modifications are made by side-effect. Return new alist."
  14229. (let* ((appending (string= (substring key -1) "+"))
  14230. (key (if appending (substring key 0 -1) key))
  14231. (old (assoc-string key props t)))
  14232. (if (not old) (cons (cons key val) props)
  14233. (setcdr old (if appending (concat (cdr old) " " val) val))
  14234. props)))
  14235. (defun org-get-property-block (&optional beg force)
  14236. "Return the (beg . end) range of the body of the property drawer.
  14237. BEG is the beginning of the current subtree, or of the part
  14238. before the first headline. If it is not given, it will be found.
  14239. If the drawer does not exist, create it if FORCE is non-nil, or
  14240. return nil."
  14241. (org-with-wide-buffer
  14242. (when beg (goto-char beg))
  14243. (unless (org-before-first-heading-p)
  14244. (let ((beg (cond (beg)
  14245. ((or (not (featurep 'org-inlinetask))
  14246. (org-inlinetask-in-task-p))
  14247. (org-back-to-heading t))
  14248. (t (org-with-limited-levels (org-back-to-heading t))))))
  14249. (forward-line)
  14250. (when (looking-at-p org-planning-line-re) (forward-line))
  14251. (cond ((looking-at org-property-drawer-re)
  14252. (forward-line)
  14253. (cons (point) (progn (goto-char (match-end 0))
  14254. (line-beginning-position))))
  14255. (force
  14256. (goto-char beg)
  14257. (org-insert-property-drawer)
  14258. (let ((pos (save-excursion (search-forward ":END:")
  14259. (line-beginning-position))))
  14260. (cons pos pos))))))))
  14261. (defun org-at-property-p ()
  14262. "Non-nil when point is inside a property drawer.
  14263. See `org-property-re' for match data, if applicable."
  14264. (save-excursion
  14265. (beginning-of-line)
  14266. (and (looking-at org-property-re)
  14267. (let ((property-drawer (save-match-data (org-get-property-block))))
  14268. (and property-drawer
  14269. (>= (point) (car property-drawer))
  14270. (< (point) (cdr property-drawer)))))))
  14271. (defun org-property-action ()
  14272. "Do an action on properties."
  14273. (interactive)
  14274. (unless (org-at-property-p) (user-error "Not at a property"))
  14275. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14276. (let ((c (read-char-exclusive)))
  14277. (cl-case c
  14278. (?s (call-interactively #'org-set-property))
  14279. (?d (call-interactively #'org-delete-property))
  14280. (?D (call-interactively #'org-delete-property-globally))
  14281. (?c (call-interactively #'org-compute-property-at-point))
  14282. (otherwise (user-error "No such property action %c" c)))))
  14283. (defun org-inc-effort ()
  14284. "Increment the value of the effort property in the current entry."
  14285. (interactive)
  14286. (org-set-effort nil t))
  14287. (defvar org-clock-effort) ; Defined in org-clock.el.
  14288. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14289. (defun org-set-effort (&optional value increment)
  14290. "Set the effort property of the current entry.
  14291. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14292. 10th allowed value.
  14293. When INCREMENT is non-nil, set the property to the next allowed value."
  14294. (interactive "P")
  14295. (when (equal value 0) (setq value 10))
  14296. (let* ((completion-ignore-case t)
  14297. (prop org-effort-property)
  14298. (cur (org-entry-get nil prop))
  14299. (allowed (org-property-get-allowed-values nil prop 'table))
  14300. (existing (mapcar 'list (org-property-values prop)))
  14301. (heading (nth 4 (org-heading-components)))
  14302. rpl
  14303. (val (cond
  14304. ((stringp value) value)
  14305. ((and allowed (integerp value))
  14306. (or (car (nth (1- value) allowed))
  14307. (car (org-last allowed))))
  14308. ((and allowed increment)
  14309. (or (cl-caadr (member (list cur) allowed))
  14310. (user-error "Allowed effort values are not set")))
  14311. (allowed
  14312. (message "Select 1-9,0, [RET%s]: %s"
  14313. (if cur (concat "=" cur) "")
  14314. (mapconcat 'car allowed " "))
  14315. (setq rpl (read-char-exclusive))
  14316. (if (equal rpl ?\r)
  14317. cur
  14318. (setq rpl (- rpl ?0))
  14319. (when (equal rpl 0) (setq rpl 10))
  14320. (if (and (> rpl 0) (<= rpl (length allowed)))
  14321. (car (nth (1- rpl) allowed))
  14322. (org-completing-read "Effort: " allowed nil))))
  14323. (t
  14324. (org-completing-read
  14325. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14326. (concat "[" cur "]") "")
  14327. ": ")
  14328. existing nil nil "" nil cur)))))
  14329. (unless (equal (org-entry-get nil prop) val)
  14330. (org-entry-put nil prop val))
  14331. (org-refresh-property
  14332. '((effort . identity)
  14333. (effort-minutes . org-duration-string-to-minutes))
  14334. val)
  14335. (when (string= heading org-clock-current-task)
  14336. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14337. (org-clock-update-mode-line))
  14338. (message "%s is now %s" prop val)))
  14339. (defun org-entry-properties (&optional pom which)
  14340. "Get all properties of the current entry.
  14341. When POM is a buffer position, get all properties from the entry
  14342. there instead.
  14343. This includes the TODO keyword, the tags, time strings for
  14344. deadline, scheduled, and clocking, and any additional properties
  14345. defined in the entry.
  14346. If WHICH is nil or `all', get all properties. If WHICH is
  14347. `special' or `standard', only get that subclass. If WHICH is
  14348. a string, only get that property.
  14349. Return value is an alist. Keys are properties, as upcased
  14350. strings."
  14351. (org-with-point-at pom
  14352. (when (and (derived-mode-p 'org-mode)
  14353. (ignore-errors (org-back-to-heading t)))
  14354. (catch 'exit
  14355. (let* ((beg (point))
  14356. (specific (and (stringp which) (upcase which)))
  14357. (which (cond ((not specific) which)
  14358. ((member specific org-special-properties) 'special)
  14359. (t 'standard)))
  14360. props)
  14361. ;; Get the special properties, like TODO and TAGS.
  14362. (when (memq which '(nil all special))
  14363. (when (or (not specific) (string= specific "CLOCKSUM"))
  14364. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14365. (when clocksum
  14366. (push (cons "CLOCKSUM"
  14367. (org-minutes-to-clocksum-string clocksum))
  14368. props)))
  14369. (when specific (throw 'exit props)))
  14370. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14371. (let ((clocksumt (get-text-property (point)
  14372. :org-clock-minutes-today)))
  14373. (when clocksumt
  14374. (push (cons "CLOCKSUM_T"
  14375. (org-minutes-to-clocksum-string clocksumt))
  14376. props)))
  14377. (when specific (throw 'exit props)))
  14378. (when (or (not specific) (string= specific "ITEM"))
  14379. (when (looking-at org-complex-heading-regexp)
  14380. (push (cons "ITEM"
  14381. (let ((title (match-string-no-properties 4)))
  14382. (if (org-string-nw-p title)
  14383. (org-remove-tabs title)
  14384. "")))
  14385. props))
  14386. (when specific (throw 'exit props)))
  14387. (when (or (not specific) (string= specific "TODO"))
  14388. (let ((case-fold-search nil))
  14389. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14390. (push (cons "TODO" (match-string-no-properties 2)) props)))
  14391. (when specific (throw 'exit props)))
  14392. (when (or (not specific) (string= specific "PRIORITY"))
  14393. (push (cons "PRIORITY"
  14394. (if (looking-at org-priority-regexp)
  14395. (match-string-no-properties 2)
  14396. (char-to-string org-default-priority)))
  14397. props)
  14398. (when specific (throw 'exit props)))
  14399. (when (or (not specific) (string= specific "FILE"))
  14400. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14401. props)
  14402. (when specific (throw 'exit props)))
  14403. (when (or (not specific) (string= specific "TAGS"))
  14404. (let ((value (org-string-nw-p (org-get-tags-string))))
  14405. (when value (push (cons "TAGS" value) props)))
  14406. (when specific (throw 'exit props)))
  14407. (when (or (not specific) (string= specific "ALLTAGS"))
  14408. (let ((value (org-get-tags-at)))
  14409. (when value
  14410. (push (cons "ALLTAGS"
  14411. (format ":%s:" (mapconcat #'identity value ":")))
  14412. props)))
  14413. (when specific (throw 'exit props)))
  14414. (when (or (not specific) (string= specific "BLOCKED"))
  14415. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14416. (when specific (throw 'exit props)))
  14417. (when (or (not specific)
  14418. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14419. (forward-line)
  14420. (when (looking-at-p org-planning-line-re)
  14421. (end-of-line)
  14422. (let ((bol (line-beginning-position))
  14423. ;; Backward compatibility: time keywords used to
  14424. ;; be configurable (before 8.3). Make sure we
  14425. ;; get the correct keyword.
  14426. (key-assoc `(("CLOSED" . ,org-closed-string)
  14427. ("DEADLINE" . ,org-deadline-string)
  14428. ("SCHEDULED" . ,org-scheduled-string))))
  14429. (dolist (pair (if specific (list (assoc specific key-assoc))
  14430. key-assoc))
  14431. (save-excursion
  14432. (when (search-backward (cdr pair) bol t)
  14433. (goto-char (match-end 0))
  14434. (skip-chars-forward " \t")
  14435. (and (looking-at org-ts-regexp-both)
  14436. (push (cons (car pair)
  14437. (match-string-no-properties 0))
  14438. props)))))))
  14439. (when specific (throw 'exit props)))
  14440. (when (or (not specific)
  14441. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14442. (let ((find-ts
  14443. (lambda (end ts)
  14444. ;; Fix next time-stamp before END. TS is the
  14445. ;; list of time-stamps found so far.
  14446. (let ((ts ts)
  14447. (regexp (cond
  14448. ((string= specific "TIMESTAMP")
  14449. org-ts-regexp)
  14450. ((string= specific "TIMESTAMP_IA")
  14451. org-ts-regexp-inactive)
  14452. ((assoc "TIMESTAMP_IA" ts)
  14453. org-ts-regexp)
  14454. ((assoc "TIMESTAMP" ts)
  14455. org-ts-regexp-inactive)
  14456. (t org-ts-regexp-both))))
  14457. (catch 'next
  14458. (while (re-search-forward regexp end t)
  14459. (backward-char)
  14460. (let ((object (org-element-context)))
  14461. ;; Accept to match timestamps in node
  14462. ;; properties, too.
  14463. (when (memq (org-element-type object)
  14464. '(node-property timestamp))
  14465. (let ((type
  14466. (org-element-property :type object)))
  14467. (cond
  14468. ((and (memq type '(active active-range))
  14469. (not (equal specific "TIMESTAMP_IA")))
  14470. (unless (assoc "TIMESTAMP" ts)
  14471. (push (cons "TIMESTAMP"
  14472. (org-element-property
  14473. :raw-value object))
  14474. ts)
  14475. (when specific (throw 'exit ts))))
  14476. ((and (memq type '(inactive inactive-range))
  14477. (not (string= specific "TIMESTAMP")))
  14478. (unless (assoc "TIMESTAMP_IA" ts)
  14479. (push (cons "TIMESTAMP_IA"
  14480. (org-element-property
  14481. :raw-value object))
  14482. ts)
  14483. (when specific (throw 'exit ts))))))
  14484. ;; Both timestamp types are found,
  14485. ;; move to next part.
  14486. (when (= (length ts) 2) (throw 'next ts)))))
  14487. ts)))))
  14488. (goto-char beg)
  14489. ;; First look for timestamps within headline.
  14490. (let ((ts (funcall find-ts (line-end-position) nil)))
  14491. (if (= (length ts) 2) (setq props (nconc ts props))
  14492. (forward-line)
  14493. ;; Then find timestamps in the section, skipping
  14494. ;; planning line.
  14495. (when (looking-at-p org-planning-line-re)
  14496. (forward-line))
  14497. (let ((end (save-excursion (outline-next-heading))))
  14498. (setq props (nconc (funcall find-ts end ts) props))))))))
  14499. ;; Get the standard properties, like :PROP:.
  14500. (when (memq which '(nil all standard))
  14501. ;; If we are looking after a specific property, delegate
  14502. ;; to `org-entry-get', which is faster. However, make an
  14503. ;; exception for "CATEGORY", since it can be also set
  14504. ;; through keywords (i.e. #+CATEGORY).
  14505. (if (and specific (not (equal specific "CATEGORY")))
  14506. (let ((value (org-entry-get beg specific nil t)))
  14507. (throw 'exit (and value (list (cons specific value)))))
  14508. (let ((range (org-get-property-block beg)))
  14509. (when range
  14510. (let ((end (cdr range)) seen-base)
  14511. (goto-char (car range))
  14512. ;; Unlike to `org--update-property-plist', we
  14513. ;; handle the case where base values is found
  14514. ;; after its extension. We also forbid standard
  14515. ;; properties to be named as special properties.
  14516. (while (re-search-forward org-property-re end t)
  14517. (let* ((key (upcase (match-string-no-properties 2)))
  14518. (extendp (org-string-match-p "\\+\\'" key))
  14519. (key-base (if extendp (substring key 0 -1) key))
  14520. (value (match-string-no-properties 3)))
  14521. (cond
  14522. ((member-ignore-case key-base org-special-properties))
  14523. (extendp
  14524. (setq props
  14525. (org--update-property-plist key value props)))
  14526. ((member key seen-base))
  14527. (t (push key seen-base)
  14528. (let ((p (assoc-string key props t)))
  14529. (if p (setcdr p (concat value " " (cdr p)))
  14530. (push (cons key value) props))))))))))))
  14531. (unless (assoc "CATEGORY" props)
  14532. (push (cons "CATEGORY" (org-get-category beg)) props)
  14533. (when (string= specific "CATEGORY") (throw 'exit props)))
  14534. ;; Return value.
  14535. props)))))
  14536. (defun org-property--local-values (property literal-nil)
  14537. "Return value for PROPERTY in current entry.
  14538. Value is a list whose car is the base value for PROPERTY and cdr
  14539. a list of accumulated values. Return nil if neither is found in
  14540. the entry. Also return nil when PROPERTY is set to \"nil\",
  14541. unless LITERAL-NIL is non-nil."
  14542. (let ((range (org-get-property-block)))
  14543. (when range
  14544. (goto-char (car range))
  14545. (let* ((case-fold-search t)
  14546. (end (cdr range))
  14547. (value
  14548. ;; Base value.
  14549. (save-excursion
  14550. (let ((v (and (re-search-forward
  14551. (org-re-property property nil t) end t)
  14552. (match-string-no-properties 3))))
  14553. (list (if literal-nil v (org-not-nil v)))))))
  14554. ;; Find additional values.
  14555. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14556. (while (re-search-forward property+ end t)
  14557. (push (match-string-no-properties 3) value)))
  14558. ;; Return final values.
  14559. (and (not (equal value '(nil))) (nreverse value))))))
  14560. (defun org-entry-get (pom property &optional inherit literal-nil)
  14561. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14562. If INHERIT is non-nil and the entry does not have the property,
  14563. then also check higher levels of the hierarchy. If INHERIT is
  14564. the symbol `selective', use inheritance only if the setting in
  14565. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14566. If the property is present but empty, the return value is the
  14567. empty string. If the property is not present at all, nil is
  14568. returned. In any other case, return the value as a string.
  14569. Search is case-insensitive.
  14570. If LITERAL-NIL is set, return the string value \"nil\" as
  14571. a string, do not interpret it as the list atom nil. This is used
  14572. for inheritance when a \"nil\" value can supersede a non-nil
  14573. value higher up the hierarchy."
  14574. (org-with-point-at pom
  14575. (cond
  14576. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14577. ;; We need a special property. Use `org-entry-properties' to
  14578. ;; retrieve it, but specify the wanted property.
  14579. (cdr (assoc-string property (org-entry-properties nil property))))
  14580. ((and inherit
  14581. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14582. (org-entry-get-with-inheritance property literal-nil))
  14583. (t
  14584. (let* ((local (org-property--local-values property literal-nil))
  14585. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14586. (if literal-nil value (org-not-nil value)))))))
  14587. (defun org-property-or-variable-value (var &optional inherit)
  14588. "Check if there is a property fixing the value of VAR.
  14589. If yes, return this value. If not, return the current value of the variable."
  14590. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14591. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14592. (read prop)
  14593. (symbol-value var))))
  14594. (defun org-entry-delete (pom property)
  14595. "Delete PROPERTY from entry at point-or-marker POM.
  14596. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14597. non-nil when a property was removed."
  14598. (unless (member property org-special-properties)
  14599. (org-with-point-at pom
  14600. (let ((range (org-get-property-block)))
  14601. (when range
  14602. (let* ((begin (car range))
  14603. (origin (cdr range))
  14604. (end (copy-marker origin))
  14605. (re (org-re-property
  14606. (concat (regexp-quote property) "\\+?") t t)))
  14607. (goto-char begin)
  14608. (while (re-search-forward re end t)
  14609. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14610. ;; If drawer is empty, remove it altogether.
  14611. (when (= begin end)
  14612. (delete-region (line-beginning-position 0)
  14613. (line-beginning-position 2)))
  14614. ;; Return non-nil if some property was removed.
  14615. (prog1 (/= end origin) (set-marker end nil))))))))
  14616. ;; Multi-values properties are properties that contain multiple values
  14617. ;; These values are assumed to be single words, separated by whitespace.
  14618. (defun org-entry-add-to-multivalued-property (pom property value)
  14619. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14620. (let* ((old (org-entry-get pom property))
  14621. (values (and old (org-split-string old "[ \t]"))))
  14622. (setq value (org-entry-protect-space value))
  14623. (unless (member value values)
  14624. (setq values (append values (list value)))
  14625. (org-entry-put pom property
  14626. (mapconcat 'identity values " ")))))
  14627. (defun org-entry-remove-from-multivalued-property (pom property value)
  14628. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14629. (let* ((old (org-entry-get pom property))
  14630. (values (and old (org-split-string old "[ \t]"))))
  14631. (setq value (org-entry-protect-space value))
  14632. (when (member value values)
  14633. (setq values (delete value values))
  14634. (org-entry-put pom property
  14635. (mapconcat 'identity values " ")))))
  14636. (defun org-entry-member-in-multivalued-property (pom property value)
  14637. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14638. (let* ((old (org-entry-get pom property))
  14639. (values (and old (org-split-string old "[ \t]"))))
  14640. (setq value (org-entry-protect-space value))
  14641. (member value values)))
  14642. (defun org-entry-get-multivalued-property (pom property)
  14643. "Return a list of values in a multivalued property."
  14644. (let* ((value (org-entry-get pom property))
  14645. (values (and value (org-split-string value "[ \t]"))))
  14646. (mapcar 'org-entry-restore-space values)))
  14647. (defun org-entry-put-multivalued-property (pom property &rest values)
  14648. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14649. VALUES should be a list of strings. Spaces will be protected."
  14650. (org-entry-put pom property
  14651. (mapconcat 'org-entry-protect-space values " "))
  14652. (let* ((value (org-entry-get pom property))
  14653. (values (and value (org-split-string value "[ \t]"))))
  14654. (mapcar 'org-entry-restore-space values)))
  14655. (defun org-entry-protect-space (s)
  14656. "Protect spaces and newline in string S."
  14657. (while (string-match " " s)
  14658. (setq s (replace-match "%20" t t s)))
  14659. (while (string-match "\n" s)
  14660. (setq s (replace-match "%0A" t t s)))
  14661. s)
  14662. (defun org-entry-restore-space (s)
  14663. "Restore spaces and newline in string S."
  14664. (while (string-match "%20" s)
  14665. (setq s (replace-match " " t t s)))
  14666. (while (string-match "%0A" s)
  14667. (setq s (replace-match "\n" t t s)))
  14668. s)
  14669. (defvar org-entry-property-inherited-from (make-marker)
  14670. "Marker pointing to the entry from where a property was inherited.
  14671. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14672. location of the entry where the inheritance search matched. If there was
  14673. no match, the marker will point nowhere.
  14674. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14675. is set.")
  14676. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14677. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14678. The search will stop at the first ancestor which has the property defined.
  14679. If the value found is \"nil\", return nil to show that the property
  14680. should be considered as undefined (this is the meaning of nil here).
  14681. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14682. (move-marker org-entry-property-inherited-from nil)
  14683. (org-with-wide-buffer
  14684. (let (value)
  14685. (catch 'exit
  14686. (while t
  14687. (let ((v (org-property--local-values property literal-nil)))
  14688. (when v
  14689. (setq value
  14690. (concat (mapconcat #'identity (delq nil v) " ")
  14691. (and value " ")
  14692. value)))
  14693. (cond
  14694. ((car v)
  14695. (org-back-to-heading t)
  14696. (move-marker org-entry-property-inherited-from (point))
  14697. (throw 'exit nil))
  14698. ((org-up-heading-safe))
  14699. (t
  14700. (let ((global
  14701. (cdr (or (assoc-string property org-file-properties t)
  14702. (assoc-string property org-global-properties t)
  14703. (assoc-string property org-global-properties-fixed t)))))
  14704. (cond ((not global))
  14705. (value (setq value (concat global " " value)))
  14706. (t (setq value global))))
  14707. (throw 'exit nil))))))
  14708. (if literal-nil value (org-not-nil value)))))
  14709. (defvar org-property-changed-functions nil
  14710. "Hook called when the value of a property has changed.
  14711. Each hook function should accept two arguments, the name of the property
  14712. and the new value.")
  14713. (defun org-entry-put (pom property value)
  14714. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14715. If the value is nil, it is converted to the empty string. If it
  14716. is not a string, an error is raised. Also raise an error on
  14717. invalid property names.
  14718. PROPERTY can be any regular property (see
  14719. `org-special-properties'). It can also be \"TODO\",
  14720. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14721. For the last two properties, VALUE may have any of the special
  14722. values \"earlier\" and \"later\". The function then increases or
  14723. decreases scheduled or deadline date by one day."
  14724. (cond ((null value) (setq value ""))
  14725. ((not (stringp value)) (error "Properties values should be strings"))
  14726. ((not (org--valid-property-p property))
  14727. (user-error "Invalid property name: \"%s\"" property)))
  14728. (org-with-point-at pom
  14729. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14730. (org-back-to-heading t)
  14731. (org-with-limited-levels (org-back-to-heading t)))
  14732. (let ((beg (point)))
  14733. (cond
  14734. ((equal property "TODO")
  14735. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14736. ((not (member value org-todo-keywords-1))
  14737. (user-error "\"%s\" is not a valid TODO state" value)))
  14738. (org-todo value)
  14739. (org-set-tags nil 'align))
  14740. ((equal property "PRIORITY")
  14741. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14742. (org-set-tags nil 'align))
  14743. ((equal property "SCHEDULED")
  14744. (forward-line)
  14745. (if (and (looking-at-p org-planning-line-re)
  14746. (re-search-forward
  14747. org-scheduled-time-regexp (line-end-position) t))
  14748. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14749. ((string= value "later") (org-timestamp-change 1 'day))
  14750. ((string= value "") (org-schedule '(4)))
  14751. (t (org-schedule nil value)))
  14752. (if (member value '("earlier" "later" ""))
  14753. (call-interactively #'org-schedule)
  14754. (org-schedule nil value))))
  14755. ((equal property "DEADLINE")
  14756. (forward-line)
  14757. (if (and (looking-at-p org-planning-line-re)
  14758. (re-search-forward
  14759. org-deadline-time-regexp (line-end-position) t))
  14760. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14761. ((string= value "later") (org-timestamp-change 1 'day))
  14762. ((string= value "") (org-deadline '(4)))
  14763. (t (org-deadline nil value)))
  14764. (if (member value '("earlier" "later" ""))
  14765. (call-interactively #'org-deadline)
  14766. (org-deadline nil value))))
  14767. ((member property org-special-properties)
  14768. (error "The %s property cannot be set with `org-entry-put'" property))
  14769. (t
  14770. (let* ((range (org-get-property-block beg 'force))
  14771. (end (cdr range))
  14772. (case-fold-search t))
  14773. (goto-char (car range))
  14774. (if (re-search-forward (org-re-property property nil t) end t)
  14775. (progn (delete-region (match-beginning 0) (match-end 0))
  14776. (goto-char (match-beginning 0)))
  14777. (goto-char end)
  14778. (insert "\n")
  14779. (backward-char))
  14780. (insert ":" property ":")
  14781. (when value (insert " " value))
  14782. (org-indent-line)))))
  14783. (run-hook-with-args 'org-property-changed-functions property value)))
  14784. (defun org-buffer-property-keys
  14785. (&optional specials defaults columns ignore-malformed)
  14786. "Get all property keys in the current buffer.
  14787. When SPECIALS is non-nil, also list the special properties that
  14788. reflect things like tags and TODO state.
  14789. When DEFAULTS is non-nil, also include properties that has
  14790. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14791. DESCRIPTION, LOCATION, and LOGGING and others.
  14792. When COLUMNS in non-nil, also include property names given in
  14793. COLUMN formats in the current buffer.
  14794. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14795. automatically performed, such drawers will be silently ignored."
  14796. (let ((case-fold-search t)
  14797. (props (append
  14798. (and specials org-special-properties)
  14799. (and defaults (cons org-effort-property org-default-properties))
  14800. nil)))
  14801. (org-with-wide-buffer
  14802. (goto-char (point-min))
  14803. (while (re-search-forward org-property-start-re nil t)
  14804. (let ((range (org-get-property-block)))
  14805. (catch 'skip
  14806. (unless range
  14807. (when (and (not ignore-malformed)
  14808. (not (org-before-first-heading-p))
  14809. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14810. (line-beginning-position))))
  14811. (org-get-property-block nil t))
  14812. (throw 'skip nil))
  14813. (goto-char (car range))
  14814. (let ((begin (car range))
  14815. (end (cdr range)))
  14816. ;; Make sure that found property block is not located
  14817. ;; before current point, as it would generate an infloop.
  14818. ;; It can happen, for example, in the following
  14819. ;; situation:
  14820. ;;
  14821. ;; * Headline
  14822. ;; :PROPERTIES:
  14823. ;; ...
  14824. ;; :END:
  14825. ;; *************** Inlinetask
  14826. ;; #+BEGIN_EXAMPLE
  14827. ;; :PROPERTIES:
  14828. ;; #+END_EXAMPLE
  14829. ;;
  14830. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14831. (while (< (point) end)
  14832. (let ((p (progn (looking-at org-property-re)
  14833. (match-string-no-properties 2))))
  14834. ;; Only add true property name, not extension symbol.
  14835. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14836. (substring p 0 -1))
  14837. props))
  14838. (forward-line))))
  14839. (outline-next-heading)))
  14840. (when columns
  14841. (goto-char (point-min))
  14842. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14843. (let ((element (org-element-at-point)))
  14844. (when (memq (org-element-type element) '(keyword node-property))
  14845. (let ((value (org-element-property :value element))
  14846. (start 0))
  14847. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14848. (setq start (match-end 0))
  14849. (let ((p (match-string-no-properties 1 value)))
  14850. (unless (member-ignore-case p org-special-properties)
  14851. (push p props))))))))))
  14852. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14853. (defun org-property-values (key)
  14854. "List all non-nil values of property KEY in current buffer."
  14855. (org-with-wide-buffer
  14856. (goto-char (point-min))
  14857. (let ((case-fold-search t)
  14858. (re (org-re-property key))
  14859. values)
  14860. (while (re-search-forward re nil t)
  14861. (push (org-entry-get (point) key) values))
  14862. (delete-dups values))))
  14863. (defun org-insert-property-drawer ()
  14864. "Insert a property drawer into the current entry."
  14865. (org-with-wide-buffer
  14866. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14867. (org-back-to-heading t)
  14868. (org-with-limited-levels (org-back-to-heading t)))
  14869. (forward-line)
  14870. (when (looking-at-p org-planning-line-re) (forward-line))
  14871. (unless (looking-at-p org-property-drawer-re)
  14872. ;; Make sure we start editing a line from current entry, not from
  14873. ;; next one. It prevents extending text properties or overlays
  14874. ;; belonging to the latter.
  14875. (when (bolp) (backward-char))
  14876. (let ((begin (1+ (point)))
  14877. (inhibit-read-only t))
  14878. (insert "\n:PROPERTIES:\n:END:")
  14879. (when (eobp) (insert "\n"))
  14880. (org-indent-region begin (point))))))
  14881. (defun org-insert-drawer (&optional arg drawer)
  14882. "Insert a drawer at point.
  14883. When optional argument ARG is non-nil, insert a property drawer.
  14884. Optional argument DRAWER, when non-nil, is a string representing
  14885. drawer's name. Otherwise, the user is prompted for a name.
  14886. If a region is active, insert the drawer around that region
  14887. instead.
  14888. Point is left between drawer's boundaries."
  14889. (interactive "P")
  14890. (let* ((drawer (if arg "PROPERTIES"
  14891. (or drawer (read-from-minibuffer "Drawer: ")))))
  14892. (cond
  14893. ;; With C-u, fall back on `org-insert-property-drawer'
  14894. (arg (org-insert-property-drawer))
  14895. ;; Check validity of suggested drawer's name.
  14896. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14897. (user-error "Invalid drawer name"))
  14898. ;; With an active region, insert a drawer at point.
  14899. ((not (org-region-active-p))
  14900. (progn
  14901. (unless (bolp) (insert "\n"))
  14902. (insert (format ":%s:\n\n:END:\n" drawer))
  14903. (forward-line -2)))
  14904. ;; Otherwise, insert the drawer at point
  14905. (t
  14906. (let ((rbeg (region-beginning))
  14907. (rend (copy-marker (region-end))))
  14908. (unwind-protect
  14909. (progn
  14910. (goto-char rbeg)
  14911. (beginning-of-line)
  14912. (when (save-excursion
  14913. (re-search-forward org-outline-regexp-bol rend t))
  14914. (user-error "Drawers cannot contain headlines"))
  14915. ;; Position point at the beginning of the first
  14916. ;; non-blank line in region. Insert drawer's opening
  14917. ;; there, then indent it.
  14918. (org-skip-whitespace)
  14919. (beginning-of-line)
  14920. (insert ":" drawer ":\n")
  14921. (forward-line -1)
  14922. (indent-for-tab-command)
  14923. ;; Move point to the beginning of the first blank line
  14924. ;; after the last non-blank line in region. Insert
  14925. ;; drawer's closing, then indent it.
  14926. (goto-char rend)
  14927. (skip-chars-backward " \r\t\n")
  14928. (insert "\n:END:")
  14929. (deactivate-mark t)
  14930. (indent-for-tab-command)
  14931. (unless (eolp) (insert "\n")))
  14932. ;; Clear marker, whatever the outcome of insertion is.
  14933. (set-marker rend nil)))))))
  14934. (defvar org-property-set-functions-alist nil
  14935. "Property set function alist.
  14936. Each entry should have the following format:
  14937. (PROPERTY . READ-FUNCTION)
  14938. The read function will be called with the same argument as
  14939. `org-completing-read'.")
  14940. (defun org-set-property-function (property)
  14941. "Get the function that should be used to set PROPERTY.
  14942. This is computed according to `org-property-set-functions-alist'."
  14943. (or (cdr (assoc property org-property-set-functions-alist))
  14944. 'org-completing-read))
  14945. (defun org-read-property-value (property)
  14946. "Read PROPERTY value from user."
  14947. (let* ((completion-ignore-case t)
  14948. (allowed (org-property-get-allowed-values nil property 'table))
  14949. (cur (org-entry-get nil property))
  14950. (prompt (concat property " value"
  14951. (if (and cur (string-match "\\S-" cur))
  14952. (concat " [" cur "]") "") ": "))
  14953. (set-function (org-set-property-function property))
  14954. (val (if allowed
  14955. (funcall set-function prompt allowed nil
  14956. (not (get-text-property 0 'org-unrestricted
  14957. (caar allowed))))
  14958. (funcall set-function prompt
  14959. (mapcar 'list (org-property-values property))
  14960. nil nil "" nil cur))))
  14961. (org-trim val)))
  14962. (defvar org-last-set-property nil)
  14963. (defvar org-last-set-property-value nil)
  14964. (defun org-read-property-name ()
  14965. "Read a property name."
  14966. (let ((completion-ignore-case t)
  14967. (default-prop (or (and (org-at-property-p)
  14968. (match-string-no-properties 2))
  14969. org-last-set-property)))
  14970. (org-completing-read
  14971. (concat "Property"
  14972. (if default-prop (concat " [" default-prop "]") "")
  14973. ": ")
  14974. (mapcar #'list (org-buffer-property-keys nil t t))
  14975. nil nil nil nil default-prop)))
  14976. (defun org-set-property-and-value (use-last)
  14977. "Allow to set [PROPERTY]: [value] direction from prompt.
  14978. When use-default, don't even ask, just use the last
  14979. \"[PROPERTY]: [value]\" string from the history."
  14980. (interactive "P")
  14981. (let* ((completion-ignore-case t)
  14982. (pv (or (and use-last org-last-set-property-value)
  14983. (org-completing-read
  14984. "Enter a \"[Property]: [value]\" pair: "
  14985. nil nil nil nil nil
  14986. org-last-set-property-value)))
  14987. prop val)
  14988. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14989. (setq prop (match-string 1 pv)
  14990. val (match-string 2 pv))
  14991. (org-set-property prop val))))
  14992. (defun org-set-property (property value)
  14993. "In the current entry, set PROPERTY to VALUE.
  14994. When called interactively, this will prompt for a property name, offering
  14995. completion on existing and default properties. And then it will prompt
  14996. for a value, offering completion either on allowed values (via an inherited
  14997. xxx_ALL property) or on existing values in other instances of this property
  14998. in the current file.
  14999. Throw an error when trying to set a property with an invalid name."
  15000. (interactive (list nil nil))
  15001. (let ((property (or property (org-read-property-name))))
  15002. ;; `org-entry-put' also makes the following check, but this one
  15003. ;; avoids polluting `org-last-set-property' and
  15004. ;; `org-last-set-property-value' needlessly.
  15005. (unless (org--valid-property-p property)
  15006. (user-error "Invalid property name: \"%s\"" property))
  15007. (let ((value (or value (org-read-property-value property)))
  15008. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  15009. (setq org-last-set-property property)
  15010. (setq org-last-set-property-value (concat property ": " value))
  15011. ;; Possibly postprocess the inserted value:
  15012. (when fn (setq value (funcall fn value)))
  15013. (unless (equal (org-entry-get nil property) value)
  15014. (org-entry-put nil property value)))))
  15015. (defun org-find-property (property &optional value)
  15016. "Find first entry in buffer that sets PROPERTY.
  15017. When optional argument VALUE is non-nil, only consider an entry
  15018. if it contains PROPERTY set to this value. If PROPERTY should be
  15019. explicitly set to nil, use string \"nil\" for VALUE.
  15020. Return position where the entry begins, or nil if there is no
  15021. such entry. If narrowing is in effect, only search the visible
  15022. part of the buffer."
  15023. (save-excursion
  15024. (goto-char (point-min))
  15025. (let ((case-fold-search t)
  15026. (re (org-re-property property nil (not value) value)))
  15027. (catch 'exit
  15028. (while (re-search-forward re nil t)
  15029. (when (if value (org-at-property-p)
  15030. (org-entry-get (point) property nil t))
  15031. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  15032. (defun org-delete-property (property)
  15033. "In the current entry, delete PROPERTY."
  15034. (interactive
  15035. (let* ((completion-ignore-case t)
  15036. (cat (org-entry-get (point) "CATEGORY"))
  15037. (props0 (org-entry-properties nil 'standard))
  15038. (props (if cat props0
  15039. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  15040. (prop (if (< 1 (length props))
  15041. (completing-read "Property: " props nil t)
  15042. (caar props))))
  15043. (list prop)))
  15044. (if (not property)
  15045. (message "No property to delete in this entry")
  15046. (org-entry-delete nil property)
  15047. (message "Property \"%s\" deleted" property)))
  15048. (defun org-delete-property-globally (property)
  15049. "Remove PROPERTY globally, from all entries.
  15050. This function ignores narrowing, if any."
  15051. (interactive
  15052. (let* ((completion-ignore-case t)
  15053. (prop (completing-read
  15054. "Globally remove property: "
  15055. (mapcar #'list (org-buffer-property-keys)))))
  15056. (list prop)))
  15057. (org-with-wide-buffer
  15058. (goto-char (point-min))
  15059. (let ((count 0)
  15060. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15061. (while (re-search-forward re nil t)
  15062. (when (org-entry-delete (point) property) (cl-incf count)))
  15063. (message "Property \"%s\" removed from %d entries" property count))))
  15064. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15065. (defun org-compute-property-at-point ()
  15066. "Compute the property at point.
  15067. This looks for an enclosing column format, extracts the operator and
  15068. then applies it to the property in the column format's scope."
  15069. (interactive)
  15070. (unless (org-at-property-p)
  15071. (user-error "Not at a property"))
  15072. (let ((prop (match-string-no-properties 2)))
  15073. (org-columns-get-format-and-top-level)
  15074. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  15075. (user-error "No operator defined for property %s" prop))
  15076. (org-columns-compute prop)))
  15077. (defvar org-property-allowed-value-functions nil
  15078. "Hook for functions supplying allowed values for a specific property.
  15079. The functions must take a single argument, the name of the property, and
  15080. return a flat list of allowed values. If \":ETC\" is one of
  15081. the values, this means that these values are intended as defaults for
  15082. completion, but that other values should be allowed too.
  15083. The functions must return nil if they are not responsible for this
  15084. property.")
  15085. (defun org-property-get-allowed-values (pom property &optional table)
  15086. "Get allowed values for the property PROPERTY.
  15087. When TABLE is non-nil, return an alist that can directly be used for
  15088. completion."
  15089. (let (vals)
  15090. (cond
  15091. ((equal property "TODO")
  15092. (setq vals (org-with-point-at pom
  15093. (append org-todo-keywords-1 '("")))))
  15094. ((equal property "PRIORITY")
  15095. (let ((n org-lowest-priority))
  15096. (while (>= n org-highest-priority)
  15097. (push (char-to-string n) vals)
  15098. (setq n (1- n)))))
  15099. ((equal property "CATEGORY"))
  15100. ((member property org-special-properties))
  15101. ((setq vals (run-hook-with-args-until-success
  15102. 'org-property-allowed-value-functions property)))
  15103. (t
  15104. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15105. (when (and vals (string-match "\\S-" vals))
  15106. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15107. (setq vals (mapcar (lambda (x)
  15108. (cond ((stringp x) x)
  15109. ((numberp x) (number-to-string x))
  15110. ((symbolp x) (symbol-name x))
  15111. (t "???")))
  15112. vals)))))
  15113. (when (member ":ETC" vals)
  15114. (setq vals (remove ":ETC" vals))
  15115. (org-add-props (car vals) '(org-unrestricted t)))
  15116. (if table (mapcar 'list vals) vals)))
  15117. (defun org-property-previous-allowed-value (&optional _previous)
  15118. "Switch to the next allowed value for this property."
  15119. (interactive)
  15120. (org-property-next-allowed-value t))
  15121. (defun org-property-next-allowed-value (&optional previous)
  15122. "Switch to the next allowed value for this property."
  15123. (interactive)
  15124. (unless (org-at-property-p)
  15125. (user-error "Not at a property"))
  15126. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15127. (key (match-string 2))
  15128. (value (match-string 3))
  15129. (allowed (or (org-property-get-allowed-values (point) key)
  15130. (and (member value '("[ ]" "[-]" "[X]"))
  15131. '("[ ]" "[X]"))))
  15132. (heading (save-match-data (nth 4 (org-heading-components))))
  15133. nval)
  15134. (unless allowed
  15135. (user-error "Allowed values for this property have not been defined"))
  15136. (when previous (setq allowed (reverse allowed)))
  15137. (when (member value allowed)
  15138. (setq nval (car (cdr (member value allowed)))))
  15139. (setq nval (or nval (car allowed)))
  15140. (when (equal nval value)
  15141. (user-error "Only one allowed value for this property"))
  15142. (org-at-property-p)
  15143. (replace-match (concat " :" key ": " nval) t t)
  15144. (org-indent-line)
  15145. (beginning-of-line 1)
  15146. (skip-chars-forward " \t")
  15147. (when (equal prop org-effort-property)
  15148. (org-refresh-property
  15149. '((effort . identity)
  15150. (effort-minutes . org-duration-string-to-minutes))
  15151. nval)
  15152. (when (string= org-clock-current-task heading)
  15153. (setq org-clock-effort nval)
  15154. (org-clock-update-mode-line)))
  15155. (run-hook-with-args 'org-property-changed-functions key nval)))
  15156. (defun org-find-olp (path &optional this-buffer)
  15157. "Return a marker pointing to the entry at outline path OLP.
  15158. If anything goes wrong, throw an error.
  15159. You can wrap this call to catch the error like this:
  15160. \(condition-case msg
  15161. \(org-mobile-locate-entry (match-string 4))
  15162. \(error (nth 1 msg)))
  15163. The return value will then be either a string with the error message,
  15164. or a marker if everything is OK.
  15165. If THIS-BUFFER is set, the outline path does not contain a file,
  15166. only headings."
  15167. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15168. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15169. (level 1)
  15170. (lmin 1)
  15171. (lmax 1)
  15172. end found flevel)
  15173. (unless buffer (error "File not found :%s" file))
  15174. (with-current-buffer buffer
  15175. (org-with-wide-buffer
  15176. (goto-char (point-min))
  15177. (dolist (heading path)
  15178. (let ((re (format org-complex-heading-regexp-format
  15179. (regexp-quote heading)))
  15180. (cnt 0))
  15181. (while (re-search-forward re end t)
  15182. (setq level (- (match-end 1) (match-beginning 1)))
  15183. (when (and (>= level lmin) (<= level lmax))
  15184. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15185. (when (= cnt 0)
  15186. (error "Heading not found on level %d: %s" lmax heading))
  15187. (when (> cnt 1)
  15188. (error "Heading not unique on level %d: %s" lmax heading))
  15189. (goto-char found)
  15190. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15191. (setq end (save-excursion (org-end-of-subtree t t)))))
  15192. (when (org-at-heading-p)
  15193. (point-marker))))))
  15194. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15195. "Find node HEADING in BUFFER.
  15196. Return a marker to the heading if it was found, or nil if not.
  15197. If POS-ONLY is set, return just the position instead of a marker.
  15198. The heading text must match exact, but it may have a TODO keyword,
  15199. a priority cookie and tags in the standard locations."
  15200. (with-current-buffer (or buffer (current-buffer))
  15201. (save-excursion
  15202. (save-restriction
  15203. (widen)
  15204. (goto-char (point-min))
  15205. (let (case-fold-search)
  15206. (when (re-search-forward
  15207. (format org-complex-heading-regexp-format
  15208. (regexp-quote heading)) nil t)
  15209. (if pos-only
  15210. (match-beginning 0)
  15211. (move-marker (make-marker) (match-beginning 0)))))))))
  15212. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15213. "Find Org node headline HEADING in all .org files in directory DIR.
  15214. When the target headline is found, return a marker to this location."
  15215. (let ((files (directory-files (or dir default-directory)
  15216. t "\\`[^.#].*\\.org\\'"))
  15217. visiting m buffer)
  15218. (catch 'found
  15219. (dolist (file files)
  15220. (message "trying %s" file)
  15221. (setq visiting (org-find-base-buffer-visiting file))
  15222. (setq buffer (or visiting (find-file-noselect file)))
  15223. (setq m (org-find-exact-headline-in-buffer
  15224. heading buffer))
  15225. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15226. (and m (throw 'found m))))))
  15227. (defun org-find-entry-with-id (ident)
  15228. "Locate the entry that contains the ID property with exact value IDENT.
  15229. IDENT can be a string, a symbol or a number, this function will search for
  15230. the string representation of it.
  15231. Return the position where this entry starts, or nil if there is no such entry."
  15232. (interactive "sID: ")
  15233. (let ((id (cond
  15234. ((stringp ident) ident)
  15235. ((symbolp ident) (symbol-name ident))
  15236. ((numberp ident) (number-to-string ident))
  15237. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15238. (org-with-wide-buffer (org-find-property "ID" id))))
  15239. ;;;; Timestamps
  15240. (defvar org-last-changed-timestamp nil)
  15241. (defvar org-last-inserted-timestamp nil
  15242. "The last time stamp inserted with `org-insert-time-stamp'.")
  15243. (defvar org-ts-what) ; dynamically scoped parameter
  15244. (defun org-time-stamp (arg &optional inactive)
  15245. "Prompt for a date/time and insert a time stamp.
  15246. If the user specifies a time like HH:MM or if this command is
  15247. called with at least one prefix argument, the time stamp contains
  15248. the date and the time. Otherwise, only the date is included.
  15249. All parts of a date not specified by the user are filled in from
  15250. the timestamp at point, if any, or the current date/time
  15251. otherwise.
  15252. If there is already a timestamp at the cursor, it is replaced.
  15253. With two universal prefix arguments, insert an active timestamp
  15254. with the current time without prompting the user.
  15255. When called from lisp, the timestamp is inactive if INACTIVE is
  15256. non-nil."
  15257. (interactive "P")
  15258. (let* ((ts (cond
  15259. ((org-at-date-range-p t)
  15260. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15261. ((org-at-timestamp-p t) (match-string 0))))
  15262. ;; Default time is either the timestamp at point or today.
  15263. ;; When entering a range, only the range start is considered.
  15264. (default-time (if (not ts) (current-time)
  15265. (apply #'encode-time (org-parse-time-string ts))))
  15266. (default-input (and ts (org-get-compact-tod ts)))
  15267. (repeater (and ts
  15268. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15269. (match-string 0 ts)))
  15270. org-time-was-given
  15271. org-end-time-was-given
  15272. (time
  15273. (and (if (equal arg '(16)) (current-time)
  15274. ;; Preserve `this-command' and `last-command'.
  15275. (let ((this-command this-command)
  15276. (last-command last-command))
  15277. (org-read-date
  15278. arg 'totime nil nil default-time default-input
  15279. inactive))))))
  15280. (cond
  15281. ((and ts
  15282. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15283. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15284. (insert "--")
  15285. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15286. (ts
  15287. ;; Make sure we're on a timestamp. When in the middle of a date
  15288. ;; range, move arbitrarily to range end.
  15289. (unless (org-at-timestamp-p t)
  15290. (skip-chars-forward "-")
  15291. (org-at-timestamp-p t))
  15292. (replace-match "")
  15293. (setq org-last-changed-timestamp
  15294. (org-insert-time-stamp
  15295. time (or org-time-was-given arg)
  15296. inactive nil nil (list org-end-time-was-given)))
  15297. (when repeater
  15298. (backward-char)
  15299. (insert " " repeater)
  15300. (setq org-last-changed-timestamp
  15301. (concat (substring org-last-inserted-timestamp 0 -1)
  15302. " " repeater ">")))
  15303. (message "Timestamp updated"))
  15304. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15305. (t (org-insert-time-stamp
  15306. time (or org-time-was-given arg) inactive nil nil
  15307. (list org-end-time-was-given))))))
  15308. ;; FIXME: can we use this for something else, like computing time differences?
  15309. (defun org-get-compact-tod (s)
  15310. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15311. (let* ((t1 (match-string 1 s))
  15312. (h1 (string-to-number (match-string 2 s)))
  15313. (m1 (string-to-number (match-string 3 s)))
  15314. (t2 (and (match-end 4) (match-string 5 s)))
  15315. (h2 (and t2 (string-to-number (match-string 6 s))))
  15316. (m2 (and t2 (string-to-number (match-string 7 s))))
  15317. dh dm)
  15318. (if (not t2)
  15319. t1
  15320. (setq dh (- h2 h1) dm (- m2 m1))
  15321. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15322. (concat t1 "+" (number-to-string dh)
  15323. (and (/= 0 dm) (format ":%02d" dm)))))))
  15324. (defun org-time-stamp-inactive (&optional arg)
  15325. "Insert an inactive time stamp.
  15326. An inactive time stamp is enclosed in square brackets instead of angle
  15327. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15328. does not link to the calendar and cannot be changed with the S-cursor keys.
  15329. So these are more for recording a certain time/date."
  15330. (interactive "P")
  15331. (org-time-stamp arg 'inactive))
  15332. (defvar org-date-ovl (make-overlay 1 1))
  15333. (overlay-put org-date-ovl 'face 'org-date-selected)
  15334. (delete-overlay org-date-ovl)
  15335. (defvar org-ans1) ; dynamically scoped parameter
  15336. (defvar org-ans2) ; dynamically scoped parameter
  15337. (defvar org-plain-time-of-day-regexp) ; defined below
  15338. (defvar org-overriding-default-time nil) ; dynamically scoped
  15339. (defvar org-read-date-overlay nil)
  15340. (defvar org-dcst nil) ; dynamically scoped
  15341. (defvar org-read-date-history nil)
  15342. (defvar org-read-date-final-answer nil)
  15343. (defvar org-read-date-analyze-futurep nil)
  15344. (defvar org-read-date-analyze-forced-year nil)
  15345. (defvar org-read-date-inactive)
  15346. (defvar org-read-date-minibuffer-local-map
  15347. (let* ((map (make-sparse-keymap)))
  15348. (set-keymap-parent map minibuffer-local-map)
  15349. (org-defkey map (kbd ".")
  15350. (lambda () (interactive)
  15351. ;; Are we at the beginning of the prompt?
  15352. (if (looking-back "^[^:]+: "
  15353. (let ((inhibit-field-text-motion t))
  15354. (line-beginning-position)))
  15355. (org-eval-in-calendar '(calendar-goto-today))
  15356. (insert "."))))
  15357. (org-defkey map (kbd "C-.")
  15358. (lambda () (interactive)
  15359. (org-eval-in-calendar '(calendar-goto-today))))
  15360. (org-defkey map [(meta shift left)]
  15361. (lambda () (interactive)
  15362. (org-eval-in-calendar '(calendar-backward-month 1))))
  15363. (org-defkey map [(meta shift right)]
  15364. (lambda () (interactive)
  15365. (org-eval-in-calendar '(calendar-forward-month 1))))
  15366. (org-defkey map [(meta shift up)]
  15367. (lambda () (interactive)
  15368. (org-eval-in-calendar '(calendar-backward-year 1))))
  15369. (org-defkey map [(meta shift down)]
  15370. (lambda () (interactive)
  15371. (org-eval-in-calendar '(calendar-forward-year 1))))
  15372. (org-defkey map [?\e (shift left)]
  15373. (lambda () (interactive)
  15374. (org-eval-in-calendar '(calendar-backward-month 1))))
  15375. (org-defkey map [?\e (shift right)]
  15376. (lambda () (interactive)
  15377. (org-eval-in-calendar '(calendar-forward-month 1))))
  15378. (org-defkey map [?\e (shift up)]
  15379. (lambda () (interactive)
  15380. (org-eval-in-calendar '(calendar-backward-year 1))))
  15381. (org-defkey map [?\e (shift down)]
  15382. (lambda () (interactive)
  15383. (org-eval-in-calendar '(calendar-forward-year 1))))
  15384. (org-defkey map [(shift up)]
  15385. (lambda () (interactive)
  15386. (org-eval-in-calendar '(calendar-backward-week 1))))
  15387. (org-defkey map [(shift down)]
  15388. (lambda () (interactive)
  15389. (org-eval-in-calendar '(calendar-forward-week 1))))
  15390. (org-defkey map [(shift left)]
  15391. (lambda () (interactive)
  15392. (org-eval-in-calendar '(calendar-backward-day 1))))
  15393. (org-defkey map [(shift right)]
  15394. (lambda () (interactive)
  15395. (org-eval-in-calendar '(calendar-forward-day 1))))
  15396. (org-defkey map "!"
  15397. (lambda () (interactive)
  15398. (org-eval-in-calendar '(diary-view-entries))
  15399. (message "")))
  15400. (org-defkey map ">"
  15401. (lambda () (interactive)
  15402. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15403. (org-defkey map "<"
  15404. (lambda () (interactive)
  15405. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15406. (org-defkey map "\C-v"
  15407. (lambda () (interactive)
  15408. (org-eval-in-calendar
  15409. '(calendar-scroll-left-three-months 1))))
  15410. (org-defkey map "\M-v"
  15411. (lambda () (interactive)
  15412. (org-eval-in-calendar
  15413. '(calendar-scroll-right-three-months 1))))
  15414. map)
  15415. "Keymap for minibuffer commands when using `org-read-date'.")
  15416. (defvar org-def)
  15417. (defvar org-defdecode)
  15418. (defvar org-with-time)
  15419. (defvar calendar-setup) ; Dynamically scoped.
  15420. (defun org-read-date (&optional with-time to-time from-string prompt
  15421. default-time default-input inactive)
  15422. "Read a date, possibly a time, and make things smooth for the user.
  15423. The prompt will suggest to enter an ISO date, but you can also enter anything
  15424. which will at least partially be understood by `parse-time-string'.
  15425. Unrecognized parts of the date will default to the current day, month, year,
  15426. hour and minute. If this command is called to replace a timestamp at point,
  15427. or to enter the second timestamp of a range, the default time is taken
  15428. from the existing stamp. Furthermore, the command prefers the future,
  15429. so if you are giving a date where the year is not given, and the day-month
  15430. combination is already past in the current year, it will assume you
  15431. mean next year. For details, see the manual. A few examples:
  15432. 3-2-5 --> 2003-02-05
  15433. feb 15 --> currentyear-02-15
  15434. 2/15 --> currentyear-02-15
  15435. sep 12 9 --> 2009-09-12
  15436. 12:45 --> today 12:45
  15437. 22 sept 0:34 --> currentyear-09-22 0:34
  15438. 12 --> currentyear-currentmonth-12
  15439. Fri --> nearest Friday after today
  15440. -Tue --> last Tuesday
  15441. etc.
  15442. Furthermore you can specify a relative date by giving, as the *first* thing
  15443. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15444. change in days weeks, months, years.
  15445. With a single plus or minus, the date is relative to today. With a double
  15446. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15447. +4d --> four days from today
  15448. +4 --> same as above
  15449. +2w --> two weeks from today
  15450. ++5 --> five days from default date
  15451. The function understands only English month and weekday abbreviations.
  15452. While prompting, a calendar is popped up - you can also select the
  15453. date with the mouse (button 1). The calendar shows a period of three
  15454. months. To scroll it to other months, use the keys `>' and `<'.
  15455. If you don't like the calendar, turn it off with
  15456. (setq org-read-date-popup-calendar nil)
  15457. With optional argument TO-TIME, the date will immediately be converted
  15458. to an internal time.
  15459. With an optional argument WITH-TIME, the prompt will suggest to
  15460. also insert a time. Note that when WITH-TIME is not set, you can
  15461. still enter a time, and this function will inform the calling routine
  15462. about this change. The calling routine may then choose to change the
  15463. format used to insert the time stamp into the buffer to include the time.
  15464. With optional argument FROM-STRING, read from this string instead from
  15465. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15466. the time/date that is used for everything that is not specified by the
  15467. user."
  15468. (require 'parse-time)
  15469. (let* ((org-with-time with-time)
  15470. (org-time-stamp-rounding-minutes
  15471. (if (equal org-with-time '(16))
  15472. '(0 0)
  15473. org-time-stamp-rounding-minutes))
  15474. (org-dcst org-display-custom-times)
  15475. (ct (org-current-time))
  15476. (org-def (or org-overriding-default-time default-time ct))
  15477. (org-defdecode (decode-time org-def))
  15478. (cur-frame (selected-frame))
  15479. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15480. (calendar-setup
  15481. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15482. (calendar-move-hook nil)
  15483. (calendar-view-diary-initially-flag nil)
  15484. (calendar-view-holidays-initially-flag nil)
  15485. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15486. ;; Rationalize `org-def' and `org-defdecode', if required.
  15487. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15488. (setf (nth 2 org-defdecode) -1)
  15489. (setf (nth 1 org-defdecode) 59)
  15490. (setq org-def (apply #'encode-time org-defdecode))
  15491. (setq org-defdecode (decode-time org-def)))
  15492. (let* ((timestr (format-time-string
  15493. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15494. org-def))
  15495. (prompt (concat (if prompt (concat prompt " ") "")
  15496. (format "Date+time [%s]: " timestr))))
  15497. (cond
  15498. (from-string (setq ans from-string))
  15499. (org-read-date-popup-calendar
  15500. (save-excursion
  15501. (save-window-excursion
  15502. (calendar)
  15503. (when (eq calendar-setup 'calendar-only)
  15504. (setq cal-frame
  15505. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15506. (select-frame cal-frame))
  15507. (org-eval-in-calendar '(setq cursor-type nil) t)
  15508. (unwind-protect
  15509. (progn
  15510. (calendar-forward-day (- (time-to-days org-def)
  15511. (calendar-absolute-from-gregorian
  15512. (calendar-current-date))))
  15513. (org-eval-in-calendar nil t)
  15514. (let* ((old-map (current-local-map))
  15515. (map (copy-keymap calendar-mode-map))
  15516. (minibuffer-local-map
  15517. (copy-keymap org-read-date-minibuffer-local-map)))
  15518. (org-defkey map (kbd "RET") 'org-calendar-select)
  15519. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15520. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15521. (unwind-protect
  15522. (progn
  15523. (use-local-map map)
  15524. (setq org-read-date-inactive inactive)
  15525. (add-hook 'post-command-hook 'org-read-date-display)
  15526. (setq org-ans0
  15527. (read-string prompt
  15528. default-input
  15529. 'org-read-date-history
  15530. nil))
  15531. ;; org-ans0: from prompt
  15532. ;; org-ans1: from mouse click
  15533. ;; org-ans2: from calendar motion
  15534. (setq ans
  15535. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15536. (remove-hook 'post-command-hook 'org-read-date-display)
  15537. (use-local-map old-map)
  15538. (when org-read-date-overlay
  15539. (delete-overlay org-read-date-overlay)
  15540. (setq org-read-date-overlay nil)))))
  15541. (bury-buffer "*Calendar*")
  15542. (when cal-frame
  15543. (delete-frame cal-frame)
  15544. (select-frame-set-input-focus cur-frame))))))
  15545. (t ; Naked prompt only
  15546. (unwind-protect
  15547. (setq ans (read-string prompt default-input
  15548. 'org-read-date-history timestr))
  15549. (when org-read-date-overlay
  15550. (delete-overlay org-read-date-overlay)
  15551. (setq org-read-date-overlay nil))))))
  15552. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15553. (when org-read-date-analyze-forced-year
  15554. (message "Year was forced into %s"
  15555. (if org-read-date-force-compatible-dates
  15556. "compatible range (1970-2037)"
  15557. "range representable on this machine"))
  15558. (ding))
  15559. ;; One round trip to get rid of 34th of August and stuff like that....
  15560. (setq final (decode-time (apply 'encode-time final)))
  15561. (setq org-read-date-final-answer ans)
  15562. (if to-time
  15563. (apply 'encode-time final)
  15564. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15565. (format "%04d-%02d-%02d %02d:%02d"
  15566. (nth 5 final) (nth 4 final) (nth 3 final)
  15567. (nth 2 final) (nth 1 final))
  15568. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15569. (defun org-read-date-display ()
  15570. "Display the current date prompt interpretation in the minibuffer."
  15571. (when org-read-date-display-live
  15572. (when org-read-date-overlay
  15573. (delete-overlay org-read-date-overlay))
  15574. (when (minibufferp (current-buffer))
  15575. (save-excursion
  15576. (end-of-line 1)
  15577. (while (not (equal (buffer-substring
  15578. (max (point-min) (- (point) 4)) (point))
  15579. " "))
  15580. (insert " ")))
  15581. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15582. " " (or org-ans1 org-ans2)))
  15583. (org-end-time-was-given nil)
  15584. (f (org-read-date-analyze ans org-def org-defdecode))
  15585. (fmts (if org-dcst
  15586. org-time-stamp-custom-formats
  15587. org-time-stamp-formats))
  15588. (fmt (if (or org-with-time
  15589. (and (boundp 'org-time-was-given) org-time-was-given))
  15590. (cdr fmts)
  15591. (car fmts)))
  15592. (txt (format-time-string fmt (apply 'encode-time f)))
  15593. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15594. (txt (concat "=> " txt)))
  15595. (when (and org-end-time-was-given
  15596. (string-match org-plain-time-of-day-regexp txt))
  15597. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15598. org-end-time-was-given
  15599. (substring txt (match-end 0)))))
  15600. (when org-read-date-analyze-futurep
  15601. (setq txt (concat txt " (=>F)")))
  15602. (setq org-read-date-overlay
  15603. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15604. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15605. (defun org-read-date-analyze (ans def defdecode)
  15606. "Analyze the combined answer of the date prompt."
  15607. ;; FIXME: cleanup and comment
  15608. ;; Pass `current-time' result to `decode-time' (instead of calling
  15609. ;; without arguments) so that only `current-time' has to be
  15610. ;; overriden in tests.
  15611. (let ((org-def def)
  15612. (org-defdecode defdecode)
  15613. (nowdecode (decode-time (current-time)))
  15614. delta deltan deltaw deltadef year month day
  15615. hour minute second wday pm h2 m2 tl wday1
  15616. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15617. (setq org-read-date-analyze-futurep nil
  15618. org-read-date-analyze-forced-year nil)
  15619. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15620. (setq ans "+0"))
  15621. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15622. (setq ans (replace-match "" t t ans)
  15623. deltan (car delta)
  15624. deltaw (nth 1 delta)
  15625. deltadef (nth 2 delta)))
  15626. ;; Check if there is an iso week date in there. If yes, store the
  15627. ;; info and postpone interpreting it until the rest of the parsing
  15628. ;; is done.
  15629. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15630. (setq iso-year (when (match-end 1)
  15631. (org-small-year-to-year
  15632. (string-to-number (match-string 1 ans))))
  15633. iso-weekday (when (match-end 3)
  15634. (string-to-number (match-string 3 ans)))
  15635. iso-week (string-to-number (match-string 2 ans)))
  15636. (setq ans (replace-match "" t t ans)))
  15637. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15638. (when (string-match
  15639. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15640. (setq year (if (match-end 2)
  15641. (string-to-number (match-string 2 ans))
  15642. (progn (setq kill-year t)
  15643. (string-to-number (format-time-string "%Y"))))
  15644. month (string-to-number (match-string 3 ans))
  15645. day (string-to-number (match-string 4 ans)))
  15646. (setq year (org-small-year-to-year year))
  15647. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15648. t nil ans)))
  15649. ;; Help matching dotted european dates
  15650. (when (string-match
  15651. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15652. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15653. (setq kill-year t)
  15654. (string-to-number (format-time-string "%Y")))
  15655. day (string-to-number (match-string 1 ans))
  15656. month (string-to-number (match-string 2 ans))
  15657. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15658. t nil ans)))
  15659. ;; Help matching american dates, like 5/30 or 5/30/7
  15660. (when (string-match
  15661. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15662. (setq year (if (match-end 4)
  15663. (string-to-number (match-string 4 ans))
  15664. (progn (setq kill-year t)
  15665. (string-to-number (format-time-string "%Y"))))
  15666. month (string-to-number (match-string 1 ans))
  15667. day (string-to-number (match-string 2 ans)))
  15668. (setq year (org-small-year-to-year year))
  15669. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15670. t nil ans)))
  15671. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15672. ;; If there is a time with am/pm, and *no* time without it, we convert
  15673. ;; so that matching will be successful.
  15674. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15675. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15676. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15677. (setq hour (string-to-number (match-string 1 ans))
  15678. minute (if (match-end 3)
  15679. (string-to-number (match-string 3 ans))
  15680. 0)
  15681. pm (equal ?p
  15682. (string-to-char (downcase (match-string 4 ans)))))
  15683. (if (and (= hour 12) (not pm))
  15684. (setq hour 0)
  15685. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15686. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15687. t t ans))))
  15688. ;; Check if a time range is given as a duration
  15689. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15690. (setq hour (string-to-number (match-string 1 ans))
  15691. h2 (+ hour (string-to-number (match-string 3 ans)))
  15692. minute (string-to-number (match-string 2 ans))
  15693. m2 (+ minute (if (match-end 5) (string-to-number
  15694. (match-string 5 ans))0)))
  15695. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15696. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15697. t t ans)))
  15698. ;; Check if there is a time range
  15699. (when (boundp 'org-end-time-was-given)
  15700. (setq org-time-was-given nil)
  15701. (when (and (string-match org-plain-time-of-day-regexp ans)
  15702. (match-end 8))
  15703. (setq org-end-time-was-given (match-string 8 ans))
  15704. (setq ans (concat (substring ans 0 (match-beginning 7))
  15705. (substring ans (match-end 7))))))
  15706. (setq tl (parse-time-string ans)
  15707. day (or (nth 3 tl) (nth 3 org-defdecode))
  15708. month
  15709. (cond ((nth 4 tl))
  15710. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15711. ;; Day was specified. Make sure DAY+MONTH
  15712. ;; combination happens in the future.
  15713. ((nth 3 tl)
  15714. (setq futurep t)
  15715. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15716. (nth 4 nowdecode)))
  15717. (t (nth 4 org-defdecode)))
  15718. year
  15719. (cond ((and (not kill-year) (nth 5 tl)))
  15720. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15721. ;; Month was guessed in the future and is at least
  15722. ;; equal to NOWDECODE's. Fix year accordingly.
  15723. (futurep
  15724. (if (or (> month (nth 4 nowdecode))
  15725. (>= day (nth 3 nowdecode)))
  15726. (nth 5 nowdecode)
  15727. (1+ (nth 5 nowdecode))))
  15728. ;; Month was specified. Make sure MONTH+YEAR
  15729. ;; combination happens in the future.
  15730. ((nth 4 tl)
  15731. (setq futurep t)
  15732. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15733. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15734. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15735. (t (nth 5 nowdecode))))
  15736. (t (nth 5 org-defdecode)))
  15737. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15738. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15739. second (or (nth 0 tl) 0)
  15740. wday (nth 6 tl))
  15741. (when (and (eq org-read-date-prefer-future 'time)
  15742. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15743. (equal day (nth 3 nowdecode))
  15744. (equal month (nth 4 nowdecode))
  15745. (equal year (nth 5 nowdecode))
  15746. (nth 2 tl)
  15747. (or (< (nth 2 tl) (nth 2 nowdecode))
  15748. (and (= (nth 2 tl) (nth 2 nowdecode))
  15749. (nth 1 tl)
  15750. (< (nth 1 tl) (nth 1 nowdecode)))))
  15751. (setq day (1+ day)
  15752. futurep t))
  15753. ;; Special date definitions below
  15754. (cond
  15755. (iso-week
  15756. ;; There was an iso week
  15757. (require 'cal-iso)
  15758. (setq futurep nil)
  15759. (setq year (or iso-year year)
  15760. day (or iso-weekday wday 1)
  15761. wday nil ; to make sure that the trigger below does not match
  15762. iso-date (calendar-gregorian-from-absolute
  15763. (calendar-iso-to-absolute
  15764. (list iso-week day year))))
  15765. ; FIXME: Should we also push ISO weeks into the future?
  15766. ; (when (and org-read-date-prefer-future
  15767. ; (not iso-year)
  15768. ; (< (calendar-absolute-from-gregorian iso-date)
  15769. ; (time-to-days (current-time))))
  15770. ; (setq year (1+ year)
  15771. ; iso-date (calendar-gregorian-from-absolute
  15772. ; (calendar-iso-to-absolute
  15773. ; (list iso-week day year)))))
  15774. (setq month (car iso-date)
  15775. year (nth 2 iso-date)
  15776. day (nth 1 iso-date)))
  15777. (deltan
  15778. (setq futurep nil)
  15779. (unless deltadef
  15780. ;; Pass `current-time' result to `decode-time' (instead of
  15781. ;; calling without arguments) so that only `current-time' has
  15782. ;; to be overriden in tests.
  15783. (let ((now (decode-time (current-time))))
  15784. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15785. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15786. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15787. ((equal deltaw "m") (setq month (+ month deltan)))
  15788. ((equal deltaw "y") (setq year (+ year deltan)))))
  15789. ((and wday (not (nth 3 tl)))
  15790. ;; Weekday was given, but no day, so pick that day in the week
  15791. ;; on or after the derived date.
  15792. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15793. (unless (equal wday wday1)
  15794. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15795. (when (and (boundp 'org-time-was-given)
  15796. (nth 2 tl))
  15797. (setq org-time-was-given t))
  15798. (when (< year 100) (setq year (+ 2000 year)))
  15799. ;; Check of the date is representable
  15800. (if org-read-date-force-compatible-dates
  15801. (progn
  15802. (when (< year 1970)
  15803. (setq year 1970 org-read-date-analyze-forced-year t))
  15804. (when (> year 2037)
  15805. (setq year 2037 org-read-date-analyze-forced-year t)))
  15806. (condition-case nil
  15807. (ignore (encode-time second minute hour day month year))
  15808. (error
  15809. (setq year (nth 5 org-defdecode))
  15810. (setq org-read-date-analyze-forced-year t))))
  15811. (setq org-read-date-analyze-futurep futurep)
  15812. (list second minute hour day month year)))
  15813. (defvar parse-time-weekdays)
  15814. (defun org-read-date-get-relative (s today default)
  15815. "Check string S for special relative date string.
  15816. TODAY and DEFAULT are internal times, for today and for a default.
  15817. Return shift list (N what def-flag)
  15818. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15819. N is the number of WHATs to shift.
  15820. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15821. the DEFAULT date rather than TODAY."
  15822. (require 'parse-time)
  15823. (when (and
  15824. (string-match
  15825. (concat
  15826. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15827. "\\([0-9]+\\)?"
  15828. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15829. "\\([ \t]\\|$\\)") s)
  15830. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15831. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15832. (string-to-char (substring (match-string 1 s) -1))
  15833. ?+))
  15834. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15835. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15836. (what (if (match-end 3) (match-string 3 s) "d"))
  15837. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15838. (date (if rel default today))
  15839. (wday (nth 6 (decode-time date)))
  15840. delta)
  15841. (if wday1
  15842. (progn
  15843. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15844. (when (= delta 0) (setq delta 7))
  15845. (when (= dir ?-)
  15846. (setq delta (- delta 7))
  15847. (when (= delta 0) (setq delta -7)))
  15848. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15849. (list delta "d" rel))
  15850. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15851. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15852. "Turn a user-specified date into the internal representation.
  15853. The internal representation needed by the calendar is (month day year).
  15854. This is a wrapper to handle the brain-dead convention in calendar that
  15855. user function argument order change dependent on argument order."
  15856. (if (boundp 'calendar-date-style)
  15857. (cond
  15858. ((eq calendar-date-style 'american)
  15859. (list arg1 arg2 arg3))
  15860. ((eq calendar-date-style 'european)
  15861. (list arg2 arg1 arg3))
  15862. ((eq calendar-date-style 'iso)
  15863. (list arg2 arg3 arg1)))
  15864. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15865. (if (org-bound-and-true-p european-calendar-style)
  15866. (list arg2 arg1 arg3)
  15867. (list arg1 arg2 arg3)))))
  15868. (defun org-eval-in-calendar (form &optional keepdate)
  15869. "Eval FORM in the calendar window and return to current window.
  15870. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15871. (let ((sf (selected-frame))
  15872. (sw (selected-window)))
  15873. (select-window (get-buffer-window "*Calendar*" t))
  15874. (eval form)
  15875. (when (and (not keepdate) (calendar-cursor-to-date))
  15876. (let* ((date (calendar-cursor-to-date))
  15877. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15878. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15879. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15880. (select-window sw)
  15881. (select-frame-set-input-focus sf)))
  15882. (defun org-calendar-select ()
  15883. "Return to `org-read-date' with the date currently selected.
  15884. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15885. (interactive)
  15886. (when (calendar-cursor-to-date)
  15887. (let* ((date (calendar-cursor-to-date))
  15888. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15889. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15890. (when (active-minibuffer-window) (exit-minibuffer))))
  15891. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15892. "Insert a date stamp for the date given by the internal TIME.
  15893. See `format-time-string' for the format of TIME.
  15894. WITH-HM means use the stamp format that includes the time of the day.
  15895. INACTIVE means use square brackets instead of angular ones, so that the
  15896. stamp will not contribute to the agenda.
  15897. PRE and POST are optional strings to be inserted before and after the
  15898. stamp.
  15899. The command returns the inserted time stamp."
  15900. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15901. stamp)
  15902. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15903. (insert-before-markers (or pre ""))
  15904. (when (listp extra)
  15905. (setq extra (car extra))
  15906. (if (and (stringp extra)
  15907. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15908. (setq extra (format "-%02d:%02d"
  15909. (string-to-number (match-string 1 extra))
  15910. (string-to-number (match-string 2 extra))))
  15911. (setq extra nil)))
  15912. (when extra
  15913. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15914. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15915. (insert-before-markers (or post ""))
  15916. (setq org-last-inserted-timestamp stamp)))
  15917. (defun org-toggle-time-stamp-overlays ()
  15918. "Toggle the use of custom time stamp formats."
  15919. (interactive)
  15920. (setq org-display-custom-times (not org-display-custom-times))
  15921. (unless org-display-custom-times
  15922. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15923. (while (setq p (next-single-property-change p 'display))
  15924. (when (and (get-text-property p 'display)
  15925. (eq (get-text-property p 'face) 'org-date))
  15926. (remove-text-properties
  15927. p (setq p (next-single-property-change p 'display))
  15928. '(display t))))
  15929. (set-buffer-modified-p bmp)))
  15930. (org-restart-font-lock)
  15931. (setq org-table-may-need-update t)
  15932. (if org-display-custom-times
  15933. (message "Time stamps are overlaid with custom format")
  15934. (message "Time stamp overlays removed")))
  15935. (defun org-display-custom-time (beg end)
  15936. "Overlay modified time stamp format over timestamp between BEG and END."
  15937. (let* ((ts (buffer-substring beg end))
  15938. t1 w1 with-hm tf time str w2 (off 0))
  15939. (save-match-data
  15940. (setq t1 (org-parse-time-string ts t))
  15941. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15942. (setq off (- (match-end 0) (match-beginning 0)))))
  15943. (setq end (- end off))
  15944. (setq w1 (- end beg)
  15945. with-hm (and (nth 1 t1) (nth 2 t1))
  15946. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15947. time (org-fix-decoded-time t1)
  15948. str (org-add-props
  15949. (format-time-string
  15950. (substring tf 1 -1) (apply 'encode-time time))
  15951. nil 'mouse-face 'highlight)
  15952. w2 (length str))
  15953. (unless (= w2 w1)
  15954. (add-text-properties (1+ beg) (+ 2 beg)
  15955. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15956. (put-text-property beg end 'display str)))
  15957. (defun org-fix-decoded-time (time)
  15958. "Set 0 instead of nil for the first 6 elements of time.
  15959. Don't touch the rest."
  15960. (let ((n 0))
  15961. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15962. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15963. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15964. "Difference between TIMESTAMP-STRING and now in days.
  15965. If SECONDS is non-nil, return the difference in seconds."
  15966. (let ((fdiff (if seconds #'float-time #'time-to-days)))
  15967. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15968. (funcall fdiff (current-time)))))
  15969. (defun org-deadline-close-p (timestamp-string &optional ndays)
  15970. "Is the time in TIMESTAMP-STRING close to the current date?"
  15971. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15972. (and (<= (org-time-stamp-to-now timestamp-string) ndays)
  15973. (not (org-entry-is-done-p))))
  15974. (defun org-get-wdays (ts &optional delay zero-delay)
  15975. "Get the deadline lead time appropriate for timestring TS.
  15976. When DELAY is non-nil, get the delay time for scheduled items
  15977. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15978. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15979. don't try to find the delay cookie in the scheduled timestamp."
  15980. (let ((tv (if delay org-scheduled-delay-days
  15981. org-deadline-warning-days)))
  15982. (cond
  15983. ((or (and delay (< tv 0))
  15984. (and delay zero-delay (<= tv 0))
  15985. (and (not delay) (<= tv 0)))
  15986. ;; Enforce this value no matter what
  15987. (- tv))
  15988. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15989. ;; lead time is specified.
  15990. (floor (* (string-to-number (match-string 1 ts))
  15991. (cdr (assoc (match-string 2 ts)
  15992. '(("d" . 1) ("w" . 7)
  15993. ("m" . 30.4) ("y" . 365.25)
  15994. ("h" . 0.041667)))))))
  15995. ;; go for the default.
  15996. (t tv))))
  15997. (defun org-calendar-select-mouse (ev)
  15998. "Return to `org-read-date' with the date currently selected.
  15999. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16000. (interactive "e")
  16001. (mouse-set-point ev)
  16002. (when (calendar-cursor-to-date)
  16003. (let* ((date (calendar-cursor-to-date))
  16004. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16005. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16006. (when (active-minibuffer-window) (exit-minibuffer))))
  16007. (defun org-check-deadlines (ndays)
  16008. "Check if there are any deadlines due or past due.
  16009. A deadline is considered due if it happens within `org-deadline-warning-days'
  16010. days from today's date. If the deadline appears in an entry marked DONE,
  16011. it is not shown. The prefix arg NDAYS can be used to test that many
  16012. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16013. (interactive "P")
  16014. (let* ((org-warn-days
  16015. (cond
  16016. ((equal ndays '(4)) 100000)
  16017. (ndays (prefix-numeric-value ndays))
  16018. (t (abs org-deadline-warning-days))))
  16019. (case-fold-search nil)
  16020. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16021. (callback
  16022. (lambda () (org-deadline-close-p (match-string 1) org-warn-days))))
  16023. (message "%d deadlines past-due or due within %d days"
  16024. (org-occur regexp nil callback)
  16025. org-warn-days)))
  16026. (defsubst org-re-timestamp (type)
  16027. "Return a regexp for timestamp TYPE.
  16028. Allowed values for TYPE are:
  16029. all: all timestamps
  16030. active: only active timestamps (<...>)
  16031. inactive: only inactive timestamps ([...])
  16032. scheduled: only scheduled timestamps
  16033. deadline: only deadline timestamps
  16034. closed: only closed time-stamps
  16035. When TYPE is nil, fall back on returning a regexp that matches
  16036. both scheduled and deadline timestamps."
  16037. (cl-case type
  16038. (all org-ts-regexp-both)
  16039. (active org-ts-regexp)
  16040. (inactive org-ts-regexp-inactive)
  16041. (scheduled org-scheduled-time-regexp)
  16042. (deadline org-deadline-time-regexp)
  16043. (closed org-closed-time-regexp)
  16044. (otherwise
  16045. (concat "\\<"
  16046. (regexp-opt (list org-deadline-string org-scheduled-string))
  16047. " *<\\([^>]+\\)>"))))
  16048. (defun org-check-before-date (d)
  16049. "Check if there are deadlines or scheduled entries before date D."
  16050. (interactive (list (org-read-date)))
  16051. (let* ((case-fold-search nil)
  16052. (regexp (org-re-timestamp org-ts-type))
  16053. (ts-type org-ts-type)
  16054. (callback
  16055. (lambda ()
  16056. (let ((match (match-string 1)))
  16057. (and (if (memq ts-type '(active inactive all))
  16058. (eq (org-element-type (save-excursion
  16059. (backward-char)
  16060. (org-element-context)))
  16061. 'timestamp)
  16062. (org-at-planning-p))
  16063. (time-less-p
  16064. (org-time-string-to-time match)
  16065. (org-time-string-to-time d)))))))
  16066. (message "%d entries before %s"
  16067. (org-occur regexp nil callback)
  16068. d)))
  16069. (defun org-check-after-date (d)
  16070. "Check if there are deadlines or scheduled entries after date D."
  16071. (interactive (list (org-read-date)))
  16072. (let* ((case-fold-search nil)
  16073. (regexp (org-re-timestamp org-ts-type))
  16074. (ts-type org-ts-type)
  16075. (callback
  16076. (lambda ()
  16077. (let ((match (match-string 1)))
  16078. (and (if (memq ts-type '(active inactive all))
  16079. (eq (org-element-type (save-excursion
  16080. (backward-char)
  16081. (org-element-context)))
  16082. 'timestamp)
  16083. (org-at-planning-p))
  16084. (not (time-less-p
  16085. (org-time-string-to-time match)
  16086. (org-time-string-to-time d))))))))
  16087. (message "%d entries after %s"
  16088. (org-occur regexp nil callback)
  16089. d)))
  16090. (defun org-check-dates-range (start-date end-date)
  16091. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16092. (interactive (list (org-read-date nil nil nil "Range starts")
  16093. (org-read-date nil nil nil "Range end")))
  16094. (let ((case-fold-search nil)
  16095. (regexp (org-re-timestamp org-ts-type))
  16096. (callback
  16097. (let ((type org-ts-type))
  16098. (lambda ()
  16099. (let ((match (match-string 1)))
  16100. (and
  16101. (if (memq type '(active inactive all))
  16102. (eq (org-element-type (save-excursion
  16103. (backward-char)
  16104. (org-element-context)))
  16105. 'timestamp)
  16106. (org-at-planning-p))
  16107. (not (time-less-p
  16108. (org-time-string-to-time match)
  16109. (org-time-string-to-time start-date)))
  16110. (time-less-p
  16111. (org-time-string-to-time match)
  16112. (org-time-string-to-time end-date))))))))
  16113. (message "%d entries between %s and %s"
  16114. (org-occur regexp nil callback) start-date end-date)))
  16115. (defun org-evaluate-time-range (&optional to-buffer)
  16116. "Evaluate a time range by computing the difference between start and end.
  16117. Normally the result is just printed in the echo area, but with prefix arg
  16118. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16119. If the time range is actually in a table, the result is inserted into the
  16120. next column.
  16121. For time difference computation, a year is assumed to be exactly 365
  16122. days in order to avoid rounding problems."
  16123. (interactive "P")
  16124. (or
  16125. (org-clock-update-time-maybe)
  16126. (save-excursion
  16127. (unless (org-at-date-range-p t)
  16128. (goto-char (point-at-bol))
  16129. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16130. (unless (org-at-date-range-p t)
  16131. (user-error "Not at a time-stamp range, and none found in current line")))
  16132. (let* ((ts1 (match-string 1))
  16133. (ts2 (match-string 2))
  16134. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16135. (match-end (match-end 0))
  16136. (time1 (org-time-string-to-time ts1))
  16137. (time2 (org-time-string-to-time ts2))
  16138. (t1 (float-time time1))
  16139. (t2 (float-time time2))
  16140. (diff (abs (- t2 t1)))
  16141. (negative (< (- t2 t1) 0))
  16142. ;; (ys (floor (* 365 24 60 60)))
  16143. (ds (* 24 60 60))
  16144. (hs (* 60 60))
  16145. (fy "%dy %dd %02d:%02d")
  16146. (fy1 "%dy %dd")
  16147. (fd "%dd %02d:%02d")
  16148. (fd1 "%dd")
  16149. (fh "%02d:%02d")
  16150. y d h m align)
  16151. (if havetime
  16152. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16153. y 0
  16154. d (floor (/ diff ds)) diff (mod diff ds)
  16155. h (floor (/ diff hs)) diff (mod diff hs)
  16156. m (floor (/ diff 60)))
  16157. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16158. y 0
  16159. d (floor (+ (/ diff ds) 0.5))
  16160. h 0 m 0))
  16161. (if (not to-buffer)
  16162. (message "%s" (org-make-tdiff-string y d h m))
  16163. (if (org-at-table-p)
  16164. (progn
  16165. (goto-char match-end)
  16166. (setq align t)
  16167. (and (looking-at " *|") (goto-char (match-end 0))))
  16168. (goto-char match-end))
  16169. (when (looking-at
  16170. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16171. (replace-match ""))
  16172. (when negative (insert " -"))
  16173. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16174. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16175. (insert " " (format fh h m))))
  16176. (when align (org-table-align))
  16177. (message "Time difference inserted")))))
  16178. (defun org-make-tdiff-string (y d h m)
  16179. (let ((fmt "")
  16180. (l nil))
  16181. (when (> y 0)
  16182. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16183. (push y l))
  16184. (when (> d 0)
  16185. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16186. (push d l))
  16187. (when (> h 0)
  16188. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16189. (push h l))
  16190. (when (> m 0)
  16191. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16192. (push m l))
  16193. (apply 'format fmt (nreverse l))))
  16194. (defun org-time-string-to-time (s &optional buffer pos)
  16195. "Convert a timestamp string into internal time."
  16196. (condition-case errdata
  16197. (apply 'encode-time (org-parse-time-string s))
  16198. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16199. s (if (not (and buffer pos))
  16200. ""
  16201. (format-message " at %d in buffer `%s'" pos buffer))
  16202. (cdr errdata)))))
  16203. (defun org-time-string-to-seconds (s)
  16204. "Convert a timestamp string to a number of seconds."
  16205. (float-time (org-time-string-to-time s)))
  16206. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16207. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16208. "Convert time stamp S to an absolute day number.
  16209. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16210. stamp, get the closest date to DAYNR. If PREFER is
  16211. `past' (respectively `future') return a date past (respectively
  16212. after) or equal to DAYNR.
  16213. POS is the location of time stamp S, as a buffer position in
  16214. BUFFER.
  16215. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16216. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16217. signalled."
  16218. (cond
  16219. ((string-match "\\`%%\\((.*)\\)" s)
  16220. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16221. ;; only able to match individual dates. If it fails, raise an
  16222. ;; error.
  16223. (if (and daynr
  16224. (org-diary-sexp-entry
  16225. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16226. daynr
  16227. (signal 'org-diary-sexp-no-match (list s))))
  16228. ((and daynr show-all) (org-closest-date s daynr prefer))
  16229. (t (time-to-days
  16230. (condition-case errdata
  16231. (apply #'encode-time (org-parse-time-string s))
  16232. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16233. s
  16234. (if (not (and buffer pos)) ""
  16235. (format-message " at %d in buffer `%s'" pos buffer))
  16236. (cdr errdata))))))))
  16237. (defun org-days-to-iso-week (days)
  16238. "Return the iso week number."
  16239. (require 'cal-iso)
  16240. (car (calendar-iso-from-absolute days)))
  16241. (defun org-small-year-to-year (year)
  16242. "Convert 2-digit years into 4-digit years.
  16243. YEAR is expanded into one of the 30 next years, if possible, or
  16244. into a past one. Any year larger than 99 is returned unchanged."
  16245. (if (>= year 100) year
  16246. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16247. (century (/ current 100))
  16248. (offset (- year (% current 100))))
  16249. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16250. ((> offset -70) (+ (* century 100) year))
  16251. (t (+ (* (1+ century) 100) year))))))
  16252. (defun org-time-from-absolute (d)
  16253. "Return the time corresponding to date D.
  16254. D may be an absolute day number, or a calendar-type list (month day year)."
  16255. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16256. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16257. (defvar org-agenda-current-date)
  16258. (defun org-calendar-holiday ()
  16259. "List of holidays, for Diary display in Org mode."
  16260. (require 'holidays)
  16261. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16262. (and hl (mapconcat #'identity hl "; "))))
  16263. (defun org-diary-sexp-entry (sexp entry d)
  16264. "Process a SEXP diary ENTRY for date D."
  16265. (require 'diary-lib)
  16266. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16267. ;; dynamically.
  16268. (let* ((sexp `(let ((entry ,entry)
  16269. (date ',d))
  16270. ,(car (read-from-string sexp))))
  16271. (result (if calendar-debug-sexp (eval sexp)
  16272. (condition-case nil
  16273. (eval sexp)
  16274. (error
  16275. (beep)
  16276. (message "Bad sexp at line %d in %s: %s"
  16277. (org-current-line)
  16278. (buffer-file-name) sexp)
  16279. (sleep-for 2))))))
  16280. (cond ((stringp result) (split-string result "; "))
  16281. ((and (consp result)
  16282. (not (consp (cdr result)))
  16283. (stringp (cdr result))) (cdr result))
  16284. ((and (consp result)
  16285. (stringp (car result))) result)
  16286. (result entry))))
  16287. (defun org-diary-to-ical-string (frombuf)
  16288. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16289. This uses the icalendar.el library."
  16290. (let* ((tmpdir temporary-file-directory)
  16291. (tmpfile (make-temp-name
  16292. (expand-file-name "orgics" tmpdir)))
  16293. buf rtn b e)
  16294. (with-current-buffer frombuf
  16295. (icalendar-export-region (point-min) (point-max) tmpfile)
  16296. (setq buf (find-buffer-visiting tmpfile))
  16297. (set-buffer buf)
  16298. (goto-char (point-min))
  16299. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16300. (setq b (match-beginning 0)))
  16301. (goto-char (point-max))
  16302. (when (re-search-backward "^END:VEVENT" nil t)
  16303. (setq e (match-end 0)))
  16304. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16305. (kill-buffer buf)
  16306. (delete-file tmpfile)
  16307. rtn))
  16308. (defun org-closest-date (start current prefer)
  16309. "Return closest date to CURRENT starting from START.
  16310. CURRENT and START are both time stamps.
  16311. When PREFER is `past', return a date that is either CURRENT or
  16312. past. When PREFER is `future', return a date that is either
  16313. CURRENT or future.
  16314. Only time stamps with a repeater are modified. Any other time
  16315. stamp stay unchanged. In any case, return value is an absolute
  16316. day number."
  16317. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16318. ;; No repeater. Do not shift time stamp.
  16319. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16320. (let ((value (string-to-number (match-string 1 start)))
  16321. (type (match-string 2 start)))
  16322. (if (= 0 value)
  16323. ;; Repeater with a 0-value is considered as void.
  16324. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16325. (let* ((base (org-date-to-gregorian start))
  16326. (target (org-date-to-gregorian current))
  16327. (sday (calendar-absolute-from-gregorian base))
  16328. (cday (calendar-absolute-from-gregorian target))
  16329. n1 n2)
  16330. ;; If START is already past CURRENT, just return START.
  16331. (if (<= cday sday) sday
  16332. ;; Compute closest date before (N1) and closest date past
  16333. ;; (N2) CURRENT.
  16334. (pcase type
  16335. ("h"
  16336. (let ((missing-hours
  16337. (mod (+ (- (* 24 (- cday sday))
  16338. (nth 2 (org-parse-time-string start)))
  16339. org-extend-today-until)
  16340. value)))
  16341. (setf n1 (if (= missing-hours 0) cday
  16342. (- cday (1+ (/ missing-hours 24)))))
  16343. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16344. ((or "d" "w")
  16345. (let ((value (if (equal type "w") (* 7 value) value)))
  16346. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16347. (setf n2 (+ n1 value))))
  16348. ("m"
  16349. (let* ((add-months
  16350. (lambda (d n)
  16351. ;; Add N months to gregorian date D, i.e.,
  16352. ;; a list (MONTH DAY YEAR). Return a valid
  16353. ;; gregorian date.
  16354. (let ((m (+ (nth 0 d) n)))
  16355. (list (mod m 12)
  16356. (nth 1 d)
  16357. (+ (/ m 12) (nth 2 d))))))
  16358. (months ; Complete months to TARGET.
  16359. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16360. (- (nth 0 target) (nth 0 base))
  16361. ;; If START's day is greater than
  16362. ;; TARGET's, remove incomplete month.
  16363. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16364. value)
  16365. value))
  16366. (before (funcall add-months base months)))
  16367. (setf n1 (calendar-absolute-from-gregorian before))
  16368. (setf n2
  16369. (calendar-absolute-from-gregorian
  16370. (funcall add-months before value)))))
  16371. (_
  16372. (let* ((d (nth 1 base))
  16373. (m (nth 0 base))
  16374. (y (nth 2 base))
  16375. (years ; Complete years to TARGET.
  16376. (* (/ (- (nth 2 target)
  16377. y
  16378. ;; If START's month and day are
  16379. ;; greater than TARGET's, remove
  16380. ;; incomplete year.
  16381. (if (or (> (nth 0 target) m)
  16382. (and (= (nth 0 target) m)
  16383. (> (nth 1 target) d)))
  16384. 0
  16385. 1))
  16386. value)
  16387. value))
  16388. (before (list m d (+ y years))))
  16389. (setf n1 (calendar-absolute-from-gregorian before))
  16390. (setf n2 (calendar-absolute-from-gregorian
  16391. (list m d (+ (nth 2 before) value)))))))
  16392. ;; Handle PREFER parameter, if any.
  16393. (cond
  16394. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16395. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16396. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16397. (defun org-date-to-gregorian (d)
  16398. "Turn any specification of date D into a Gregorian date for the calendar."
  16399. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16400. ((and (listp d) (= (length d) 3)) d)
  16401. ((stringp d)
  16402. (let ((d (org-parse-time-string d)))
  16403. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16404. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16405. (defun org-parse-time-string (s &optional nodefault)
  16406. "Parse the standard Org-mode time string.
  16407. This should be a lot faster than the normal `parse-time-string'.
  16408. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16409. hour and minute fields will be nil if not given."
  16410. (cond ((string-match org-ts-regexp0 s)
  16411. (list 0
  16412. (when (or (match-beginning 8) (not nodefault))
  16413. (string-to-number (or (match-string 8 s) "0")))
  16414. (when (or (match-beginning 7) (not nodefault))
  16415. (string-to-number (or (match-string 7 s) "0")))
  16416. (string-to-number (match-string 4 s))
  16417. (string-to-number (match-string 3 s))
  16418. (string-to-number (match-string 2 s))
  16419. nil nil nil))
  16420. ((string-match "^<[^>]+>$" s)
  16421. (decode-time (seconds-to-time (org-matcher-time s))))
  16422. (t (error "Not a standard Org-mode time string: %s" s))))
  16423. (defun org-timestamp-up (&optional arg)
  16424. "Increase the date item at the cursor by one.
  16425. If the cursor is on the year, change the year. If it is on the month,
  16426. the day or the time, change that.
  16427. With prefix ARG, change by that many units."
  16428. (interactive "p")
  16429. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16430. (defun org-timestamp-down (&optional arg)
  16431. "Decrease the date item at the cursor by one.
  16432. If the cursor is on the year, change the year. If it is on the month,
  16433. the day or the time, change that.
  16434. With prefix ARG, change by that many units."
  16435. (interactive "p")
  16436. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16437. (defun org-timestamp-up-day (&optional arg)
  16438. "Increase the date in the time stamp by one day.
  16439. With prefix ARG, change that many days."
  16440. (interactive "p")
  16441. (if (and (not (org-at-timestamp-p t))
  16442. (org-at-heading-p))
  16443. (org-todo 'up)
  16444. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16445. (defun org-timestamp-down-day (&optional arg)
  16446. "Decrease the date in the time stamp by one day.
  16447. With prefix ARG, change that many days."
  16448. (interactive "p")
  16449. (if (and (not (org-at-timestamp-p t))
  16450. (org-at-heading-p))
  16451. (org-todo 'down)
  16452. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16453. (defun org-at-timestamp-p (&optional inactive-ok)
  16454. "Non-nil if point is inside a timestamp.
  16455. When optional argument INACTIVE-OK is non-nil, also consider
  16456. inactive timestamps.
  16457. When this function returns a non-nil value, match data is set
  16458. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16459. INACTIVE-OK."
  16460. (interactive)
  16461. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16462. (pos (point))
  16463. (ans (or (looking-at tsr)
  16464. (save-excursion
  16465. (skip-chars-backward "^[<\n\r\t")
  16466. (when (> (point) (point-min)) (backward-char 1))
  16467. (and (looking-at tsr)
  16468. (> (- (match-end 0) pos) -1))))))
  16469. (and ans
  16470. (boundp 'org-ts-what)
  16471. (setq org-ts-what
  16472. (cond
  16473. ((= pos (match-beginning 0)) 'bracket)
  16474. ;; Point is considered to be "on the bracket" whether
  16475. ;; it's really on it or right after it.
  16476. ((= pos (1- (match-end 0))) 'bracket)
  16477. ((= pos (match-end 0)) 'after)
  16478. ((org-pos-in-match-range pos 2) 'year)
  16479. ((org-pos-in-match-range pos 3) 'month)
  16480. ((org-pos-in-match-range pos 7) 'hour)
  16481. ((org-pos-in-match-range pos 8) 'minute)
  16482. ((or (org-pos-in-match-range pos 4)
  16483. (org-pos-in-match-range pos 5)) 'day)
  16484. ((and (> pos (or (match-end 8) (match-end 5)))
  16485. (< pos (match-end 0)))
  16486. (- pos (or (match-end 8) (match-end 5))))
  16487. (t 'day))))
  16488. ans))
  16489. (defun org-toggle-timestamp-type ()
  16490. "Toggle the type (<active> or [inactive]) of a time stamp."
  16491. (interactive)
  16492. (when (org-at-timestamp-p t)
  16493. (let ((beg (match-beginning 0)) (end (match-end 0))
  16494. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16495. (save-excursion
  16496. (goto-char beg)
  16497. (while (re-search-forward "[][<>]" end t)
  16498. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16499. t t)))
  16500. (message "Timestamp is now %sactive"
  16501. (if (equal (char-after beg) ?<) "" "in")))))
  16502. (defun org-at-clock-log-p nil
  16503. "Is the cursor on the clock log line?"
  16504. (save-excursion
  16505. (move-beginning-of-line 1)
  16506. (looking-at org-clock-line-re)))
  16507. (defvar org-clock-history) ; defined in org-clock.el
  16508. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16509. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16510. "Change the date in the time stamp at point.
  16511. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16512. `year', `minute', `second'. If WHAT is not given, the cursor position
  16513. in the timestamp determines what will be changed.
  16514. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16515. (let ((origin (point)) origin-cat
  16516. with-hm inactive
  16517. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16518. org-ts-what
  16519. extra rem
  16520. ts time time0 fixnext clrgx)
  16521. (unless (org-at-timestamp-p t)
  16522. (user-error "Not at a timestamp"))
  16523. (if (and (not what) (eq org-ts-what 'bracket))
  16524. (org-toggle-timestamp-type)
  16525. ;; Point isn't on brackets. Remember the part of the time-stamp
  16526. ;; the point was in. Indeed, size of time-stamps may change,
  16527. ;; but point must be kept in the same category nonetheless.
  16528. (setq origin-cat org-ts-what)
  16529. (when (and (not what) (not (eq org-ts-what 'day))
  16530. org-display-custom-times
  16531. (get-text-property (point) 'display)
  16532. (not (get-text-property (1- (point)) 'display)))
  16533. (setq org-ts-what 'day))
  16534. (setq org-ts-what (or what org-ts-what)
  16535. inactive (= (char-after (match-beginning 0)) ?\[)
  16536. ts (match-string 0))
  16537. (replace-match "")
  16538. (when (string-match
  16539. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16540. ts)
  16541. (setq extra (match-string 1 ts))
  16542. (when suppress-tmp-delay
  16543. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16544. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16545. (setq with-hm t))
  16546. (setq time0 (org-parse-time-string ts))
  16547. (when (and updown
  16548. (eq org-ts-what 'minute)
  16549. (not current-prefix-arg))
  16550. ;; This looks like s-up and s-down. Change by one rounding step.
  16551. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16552. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16553. (setcar (cdr time0) (+ (nth 1 time0)
  16554. (if (> n 0) (- rem) (- dm rem))))))
  16555. (setq time
  16556. (apply #'encode-time
  16557. (or (car time0) 0)
  16558. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16559. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16560. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16561. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16562. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16563. (nthcdr 6 time0)))
  16564. (when (and (member org-ts-what '(hour minute))
  16565. extra
  16566. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16567. (setq extra (org-modify-ts-extra
  16568. extra
  16569. (if (eq org-ts-what 'hour) 2 5)
  16570. n dm)))
  16571. (when (integerp org-ts-what)
  16572. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16573. (when (eq what 'calendar)
  16574. (let ((cal-date (org-get-date-from-calendar)))
  16575. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16576. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16577. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16578. (setcar time0 (or (car time0) 0))
  16579. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16580. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16581. (setq time (apply 'encode-time time0))))
  16582. ;; Insert the new time-stamp, and ensure point stays in the same
  16583. ;; category as before (i.e. not after the last position in that
  16584. ;; category).
  16585. (let ((pos (point)))
  16586. ;; Stay before inserted string. `save-excursion' is of no use.
  16587. (setq org-last-changed-timestamp
  16588. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16589. (goto-char pos))
  16590. (save-match-data
  16591. (looking-at org-ts-regexp3)
  16592. (goto-char (cond
  16593. ;; `day' category ends before `hour' if any, or at
  16594. ;; the end of the day name.
  16595. ((eq origin-cat 'day)
  16596. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16597. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16598. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16599. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16600. ;; `year' and `month' have both fixed size: point
  16601. ;; couldn't have moved into another part.
  16602. (t origin))))
  16603. ;; Update clock if on a CLOCK line.
  16604. (org-clock-update-time-maybe)
  16605. ;; Maybe adjust the closest clock in `org-clock-history'
  16606. (when org-clock-adjust-closest
  16607. (if (not (and (org-at-clock-log-p)
  16608. (< 1 (length (delq nil (mapcar 'marker-position
  16609. org-clock-history))))))
  16610. (message "No clock to adjust")
  16611. (cond ((save-excursion ; fix previous clock?
  16612. (re-search-backward org-ts-regexp0 nil t)
  16613. (looking-back (concat org-clock-string " \\[")
  16614. (line-beginning-position)))
  16615. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16616. ((save-excursion ; fix next clock?
  16617. (re-search-backward org-ts-regexp0 nil t)
  16618. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16619. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16620. (save-window-excursion
  16621. ;; Find closest clock to point, adjust the previous/next one in history
  16622. (let* ((p (save-excursion (org-back-to-heading t)))
  16623. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16624. (clfixnth
  16625. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16626. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16627. (if (not clfixpos)
  16628. (message "No clock to adjust")
  16629. (save-excursion
  16630. (org-goto-marker-or-bmk clfixpos)
  16631. (org-show-subtree)
  16632. (when (re-search-forward clrgx nil t)
  16633. (goto-char (match-beginning 1))
  16634. (let (org-clock-adjust-closest)
  16635. (org-timestamp-change n org-ts-what updown))
  16636. (message "Clock adjusted in %s for heading: %s"
  16637. (file-name-nondirectory (buffer-file-name))
  16638. (org-get-heading t t)))))))))
  16639. ;; Try to recenter the calendar window, if any.
  16640. (when (and org-calendar-follow-timestamp-change
  16641. (get-buffer-window "*Calendar*" t)
  16642. (memq org-ts-what '(day month year)))
  16643. (org-recenter-calendar (time-to-days time))))))
  16644. (defun org-modify-ts-extra (s pos n dm)
  16645. "Change the different parts of the lead-time and repeat fields in timestamp."
  16646. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16647. ng h m new rem)
  16648. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16649. (cond
  16650. ((or (org-pos-in-match-range pos 2)
  16651. (org-pos-in-match-range pos 3))
  16652. (setq m (string-to-number (match-string 3 s))
  16653. h (string-to-number (match-string 2 s)))
  16654. (if (org-pos-in-match-range pos 2)
  16655. (setq h (+ h n))
  16656. (setq n (* dm (org-no-warnings (signum n))))
  16657. (unless (= 0 (setq rem (% m dm)))
  16658. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16659. (setq m (+ m n)))
  16660. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16661. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16662. (setq h (mod h 24))
  16663. (setq ng 1 new (format "-%02d:%02d" h m)))
  16664. ((org-pos-in-match-range pos 6)
  16665. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16666. ((org-pos-in-match-range pos 5)
  16667. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16668. ((org-pos-in-match-range pos 9)
  16669. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16670. ((org-pos-in-match-range pos 8)
  16671. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16672. (when ng
  16673. (setq s (concat
  16674. (substring s 0 (match-beginning ng))
  16675. new
  16676. (substring s (match-end ng))))))
  16677. s))
  16678. (defun org-recenter-calendar (d)
  16679. "If the calendar is visible, recenter it to date D."
  16680. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16681. (when cwin
  16682. (let ((calendar-move-hook nil))
  16683. (with-selected-window cwin
  16684. (calendar-goto-date
  16685. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16686. (defun org-goto-calendar (&optional arg)
  16687. "Go to the Emacs calendar at the current date.
  16688. If there is a time stamp in the current line, go to that date.
  16689. A prefix ARG can be used to force the current date."
  16690. (interactive "P")
  16691. (let ((tsr org-ts-regexp) diff
  16692. (calendar-move-hook nil)
  16693. (calendar-view-holidays-initially-flag nil)
  16694. (calendar-view-diary-initially-flag nil))
  16695. (when (or (org-at-timestamp-p)
  16696. (save-excursion
  16697. (beginning-of-line 1)
  16698. (looking-at (concat ".*" tsr))))
  16699. (let ((d1 (time-to-days (current-time)))
  16700. (d2 (time-to-days
  16701. (org-time-string-to-time (match-string 1)))))
  16702. (setq diff (- d2 d1))))
  16703. (calendar)
  16704. (calendar-goto-today)
  16705. (when (and diff (not arg)) (calendar-forward-day diff))))
  16706. (defun org-get-date-from-calendar ()
  16707. "Return a list (month day year) of date at point in calendar."
  16708. (with-current-buffer "*Calendar*"
  16709. (save-match-data
  16710. (calendar-cursor-to-date))))
  16711. (defun org-date-from-calendar ()
  16712. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16713. If there is already a time stamp at the cursor position, update it."
  16714. (interactive)
  16715. (if (org-at-timestamp-p t)
  16716. (org-timestamp-change 0 'calendar)
  16717. (let ((cal-date (org-get-date-from-calendar)))
  16718. (org-insert-time-stamp
  16719. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16720. (defcustom org-effort-durations
  16721. `(("min" . 1)
  16722. ("h" . 60)
  16723. ("d" . ,(* 60 8))
  16724. ("w" . ,(* 60 8 5))
  16725. ("m" . ,(* 60 8 5 4))
  16726. ("y" . ,(* 60 8 5 40)))
  16727. "Conversion factor to minutes for an effort modifier.
  16728. Each entry has the form (MODIFIER . MINUTES).
  16729. In an effort string, a number followed by MODIFIER is multiplied
  16730. by the specified number of MINUTES to obtain an effort in
  16731. minutes.
  16732. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16733. effort string \"2hours\" is equivalent to 120 minutes."
  16734. :group 'org-agenda
  16735. :version "25.1"
  16736. :package-version '(Org . "8.3")
  16737. :type '(alist :key-type (string :tag "Modifier")
  16738. :value-type (number :tag "Minutes")))
  16739. (defun org-minutes-to-clocksum-string (m)
  16740. "Format number of minutes as a clocksum string.
  16741. The format is determined by `org-time-clocksum-format',
  16742. `org-time-clocksum-use-fractional' and
  16743. `org-time-clocksum-fractional-format' and
  16744. `org-time-clocksum-use-effort-durations'."
  16745. (let ((clocksum "")
  16746. (m (round m)) ; Don't allow fractions of minutes
  16747. h d w mo y fmt n)
  16748. (setq h (if org-time-clocksum-use-effort-durations
  16749. (cdr (assoc "h" org-effort-durations)) 60)
  16750. d (if org-time-clocksum-use-effort-durations
  16751. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16752. w (if org-time-clocksum-use-effort-durations
  16753. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16754. mo (if org-time-clocksum-use-effort-durations
  16755. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16756. y (if org-time-clocksum-use-effort-durations
  16757. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16758. ;; fractional format
  16759. (if org-time-clocksum-use-fractional
  16760. (cond
  16761. ;; single format string
  16762. ((stringp org-time-clocksum-fractional-format)
  16763. (format org-time-clocksum-fractional-format (/ m (float h))))
  16764. ;; choice of fractional formats for different time units
  16765. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16766. (> (/ (truncate m) (* y d h)) 0))
  16767. (format fmt (/ m (* y d (float h)))))
  16768. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16769. (> (/ (truncate m) (* mo d h)) 0))
  16770. (format fmt (/ m (* mo d (float h)))))
  16771. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16772. (> (/ (truncate m) (* w d h)) 0))
  16773. (format fmt (/ m (* w d (float h)))))
  16774. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16775. (> (/ (truncate m) (* d h)) 0))
  16776. (format fmt (/ m (* d (float h)))))
  16777. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16778. (> (/ (truncate m) h) 0))
  16779. (format fmt (/ m (float h))))
  16780. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16781. (format fmt m))
  16782. ;; fall back to smallest time unit with a format
  16783. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16784. (format fmt (/ m (float h))))
  16785. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16786. (format fmt (/ m (* d (float h)))))
  16787. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16788. (format fmt (/ m (* w d (float h)))))
  16789. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16790. (format fmt (/ m (* mo d (float h)))))
  16791. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16792. (format fmt (/ m (* y d (float h))))))
  16793. ;; standard (non-fractional) format, with single format string
  16794. (if (stringp org-time-clocksum-format)
  16795. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16796. ;; separate formats components
  16797. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16798. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16799. (plist-get org-time-clocksum-format :require-years))
  16800. (setq clocksum (concat clocksum (format fmt n))
  16801. m (- m (* n y d h))))
  16802. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16803. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16804. (plist-get org-time-clocksum-format :require-months))
  16805. (setq clocksum (concat clocksum (format fmt n))
  16806. m (- m (* n mo d h))))
  16807. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16808. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16809. (plist-get org-time-clocksum-format :require-weeks))
  16810. (setq clocksum (concat clocksum (format fmt n))
  16811. m (- m (* n w d h))))
  16812. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16813. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16814. (plist-get org-time-clocksum-format :require-days))
  16815. (setq clocksum (concat clocksum (format fmt n))
  16816. m (- m (* n d h))))
  16817. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16818. (or (> (setq n (/ (truncate m) h)) 0)
  16819. (plist-get org-time-clocksum-format :require-hours))
  16820. (setq clocksum (concat clocksum (format fmt n))
  16821. m (- m (* n h))))
  16822. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16823. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16824. (setq clocksum (concat clocksum (format fmt m))))
  16825. ;; return formatted time duration
  16826. clocksum))))
  16827. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16828. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16829. "Org mode version 8.0")
  16830. (defun org-hours-to-clocksum-string (n)
  16831. (org-minutes-to-clocksum-string (* n 60)))
  16832. (defun org-hh:mm-string-to-minutes (s)
  16833. "Convert a string H:MM to a number of minutes.
  16834. If the string is just a number, interpret it as minutes.
  16835. In fact, the first hh:mm or number in the string will be taken,
  16836. there can be extra stuff in the string.
  16837. If no number is found, the return value is 0."
  16838. (cond
  16839. ((integerp s) s)
  16840. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16841. (+ (* (string-to-number (match-string 1 s)) 60)
  16842. (string-to-number (match-string 2 s))))
  16843. ((string-match "\\([0-9]+\\)" s)
  16844. (string-to-number (match-string 1 s)))
  16845. (t 0)))
  16846. (defcustom org-image-actual-width t
  16847. "Should we use the actual width of images when inlining them?
  16848. When set to t, always use the image width.
  16849. When set to a number, use imagemagick (when available) to set
  16850. the image's width to this value.
  16851. When set to a number in a list, try to get the width from any
  16852. #+ATTR.* keyword if it matches a width specification like
  16853. #+ATTR_HTML: :width 300px
  16854. and fall back on that number if none is found.
  16855. When set to nil, try to get the width from an #+ATTR.* keyword
  16856. and fall back on the original width if none is found.
  16857. This requires Emacs >= 24.1, build with imagemagick support."
  16858. :group 'org-appearance
  16859. :version "24.4"
  16860. :package-version '(Org . "8.0")
  16861. :type '(choice
  16862. (const :tag "Use the image width" t)
  16863. (integer :tag "Use a number of pixels")
  16864. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16865. (const :tag "Use #+ATTR* or don't resize" nil)))
  16866. (defcustom org-agenda-inhibit-startup nil
  16867. "Inhibit startup when preparing agenda buffers.
  16868. When this variable is t, the initialization of the Org agenda
  16869. buffers is inhibited: e.g. the visibility state is not set, the
  16870. tables are not re-aligned, etc."
  16871. :type 'boolean
  16872. :version "24.3"
  16873. :group 'org-agenda)
  16874. (define-obsolete-variable-alias
  16875. 'org-agenda-ignore-drawer-properties
  16876. 'org-agenda-ignore-properties "25.1")
  16877. (defcustom org-agenda-ignore-properties nil
  16878. "Avoid updating text properties when building the agenda.
  16879. Properties are used to prepare buffers for effort estimates,
  16880. appointments, statistics and subtree-local categories.
  16881. If you don't use these in the agenda, you can add them to this
  16882. list and agenda building will be a bit faster.
  16883. The value is a list, with zero or more of the symbols `effort', `appt',
  16884. `stats' or `category'."
  16885. :type '(set :greedy t
  16886. (const effort)
  16887. (const appt)
  16888. (const stats)
  16889. (const category))
  16890. :version "25.1"
  16891. :package-version '(Org . "8.3")
  16892. :group 'org-agenda)
  16893. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16894. "Convert a duration string S to minutes.
  16895. A bare number is interpreted as minutes, modifiers can be set by
  16896. customizing `org-effort-durations' (which see).
  16897. Entries containing a colon are interpreted as H:MM by
  16898. `org-hh:mm-string-to-minutes'."
  16899. (let ((result 0)
  16900. (re (concat "\\([0-9.]+\\) *\\("
  16901. (regexp-opt (mapcar 'car org-effort-durations))
  16902. "\\)")))
  16903. (while (string-match re s)
  16904. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16905. (string-to-number (match-string 1 s))))
  16906. (setq s (replace-match "" nil t s)))
  16907. (setq result (floor result))
  16908. (cl-incf result (org-hh:mm-string-to-minutes s))
  16909. (if output-to-string (number-to-string result) result)))
  16910. ;;;; Files
  16911. (defun org-save-all-org-buffers ()
  16912. "Save all Org-mode buffers without user confirmation."
  16913. (interactive)
  16914. (message "Saving all Org-mode buffers...")
  16915. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16916. (when (featurep 'org-id) (org-id-locations-save))
  16917. (message "Saving all Org-mode buffers... done"))
  16918. (defun org-revert-all-org-buffers ()
  16919. "Revert all Org-mode buffers.
  16920. Prompt for confirmation when there are unsaved changes.
  16921. Be sure you know what you are doing before letting this function
  16922. overwrite your changes.
  16923. This function is useful in a setup where one tracks org files
  16924. with a version control system, to revert on one machine after pulling
  16925. changes from another. I believe the procedure must be like this:
  16926. 1. M-x org-save-all-org-buffers
  16927. 2. Pull changes from the other machine, resolve conflicts
  16928. 3. M-x org-revert-all-org-buffers"
  16929. (interactive)
  16930. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16931. (user-error "Abort"))
  16932. (save-excursion
  16933. (save-window-excursion
  16934. (dolist (b (buffer-list))
  16935. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16936. (with-current-buffer b buffer-file-name))
  16937. (org-pop-to-buffer-same-window b)
  16938. (revert-buffer t 'no-confirm)))
  16939. (when (and (featurep 'org-id) org-id-track-globally)
  16940. (org-id-locations-load)))))
  16941. ;;;; Agenda files
  16942. ;;;###autoload
  16943. (defun org-switchb (&optional arg)
  16944. "Switch between Org buffers.
  16945. With \\[universal-argument] prefix, restrict available buffers to files.
  16946. With \\[universal-argument] \\[universal-argument] \
  16947. prefix, restrict available buffers to agenda files."
  16948. (interactive "P")
  16949. (let ((blist (org-buffer-list
  16950. (cond ((equal arg '(4)) 'files)
  16951. ((equal arg '(16)) 'agenda)))))
  16952. (org-pop-to-buffer-same-window
  16953. (completing-read "Org buffer: "
  16954. (mapcar #'list (mapcar #'buffer-name blist))
  16955. nil t))))
  16956. (defun org-buffer-list (&optional predicate exclude-tmp)
  16957. "Return a list of Org buffers.
  16958. PREDICATE can be `export', `files' or `agenda'.
  16959. export restrict the list to Export buffers.
  16960. files restrict the list to buffers visiting Org files.
  16961. agenda restrict the list to buffers visiting agenda files.
  16962. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16963. (let* ((bfn nil)
  16964. (agenda-files (and (eq predicate 'agenda)
  16965. (mapcar 'file-truename (org-agenda-files t))))
  16966. (filter
  16967. (cond
  16968. ((eq predicate 'files)
  16969. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16970. ((eq predicate 'export)
  16971. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16972. ((eq predicate 'agenda)
  16973. (lambda (b)
  16974. (with-current-buffer b
  16975. (and (derived-mode-p 'org-mode)
  16976. (setq bfn (buffer-file-name b))
  16977. (member (file-truename bfn) agenda-files)))))
  16978. (t (lambda (b) (with-current-buffer b
  16979. (or (derived-mode-p 'org-mode)
  16980. (string-match "\\*Org .*Export"
  16981. (buffer-name b)))))))))
  16982. (delq nil
  16983. (mapcar
  16984. (lambda(b)
  16985. (if (and (funcall filter b)
  16986. (or (not exclude-tmp)
  16987. (not (string-match "tmp" (buffer-name b)))))
  16988. b
  16989. nil))
  16990. (buffer-list)))))
  16991. (defun org-agenda-files (&optional unrestricted archives)
  16992. "Get the list of agenda files.
  16993. Optional UNRESTRICTED means return the full list even if a restriction
  16994. is currently in place.
  16995. When ARCHIVES is t, include all archive files that are really being
  16996. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16997. `org-agenda-archives-mode' is t."
  16998. (let ((files
  16999. (cond
  17000. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  17001. ((stringp org-agenda-files) (org-read-agenda-file-list))
  17002. ((listp org-agenda-files) org-agenda-files)
  17003. (t (error "Invalid value of `org-agenda-files'")))))
  17004. (setq files (apply 'append
  17005. (mapcar (lambda (f)
  17006. (if (file-directory-p f)
  17007. (directory-files
  17008. f t org-agenda-file-regexp)
  17009. (list f)))
  17010. files)))
  17011. (when org-agenda-skip-unavailable-files
  17012. (setq files (delq nil
  17013. (mapcar (function
  17014. (lambda (file)
  17015. (and (file-readable-p file) file)))
  17016. files))))
  17017. (when (or (eq archives t)
  17018. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  17019. (setq files (org-add-archive-files files)))
  17020. files))
  17021. (defun org-agenda-file-p (&optional file)
  17022. "Return non-nil, if FILE is an agenda file.
  17023. If FILE is omitted, use the file associated with the current
  17024. buffer."
  17025. (let ((fname (or file (buffer-file-name))))
  17026. (and fname
  17027. (member (file-truename fname)
  17028. (mapcar #'file-truename (org-agenda-files t))))))
  17029. (defun org-edit-agenda-file-list ()
  17030. "Edit the list of agenda files.
  17031. Depending on setup, this either uses customize to edit the variable
  17032. `org-agenda-files', or it visits the file that is holding the list. In the
  17033. latter case, the buffer is set up in a way that saving it automatically kills
  17034. the buffer and restores the previous window configuration."
  17035. (interactive)
  17036. (if (stringp org-agenda-files)
  17037. (let ((cw (current-window-configuration)))
  17038. (find-file org-agenda-files)
  17039. (setq-local org-window-configuration cw)
  17040. (add-hook 'after-save-hook
  17041. (lambda ()
  17042. (set-window-configuration
  17043. (prog1 org-window-configuration
  17044. (kill-buffer (current-buffer))))
  17045. (org-install-agenda-files-menu)
  17046. (message "New agenda file list installed"))
  17047. nil 'local)
  17048. (message "%s" (substitute-command-keys
  17049. "Edit list and finish with \\[save-buffer]")))
  17050. (customize-variable 'org-agenda-files)))
  17051. (defun org-store-new-agenda-file-list (list)
  17052. "Set new value for the agenda file list and save it correctly."
  17053. (if (stringp org-agenda-files)
  17054. (let ((fe (org-read-agenda-file-list t)) b u)
  17055. (while (setq b (find-buffer-visiting org-agenda-files))
  17056. (kill-buffer b))
  17057. (with-temp-file org-agenda-files
  17058. (insert
  17059. (mapconcat
  17060. (lambda (f) ;; Keep un-expanded entries.
  17061. (if (setq u (assoc f fe))
  17062. (cdr u)
  17063. f))
  17064. list "\n")
  17065. "\n")))
  17066. (let ((org-mode-hook nil) (org-inhibit-startup t)
  17067. (org-insert-mode-line-in-empty-file nil))
  17068. (setq org-agenda-files list)
  17069. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17070. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  17071. "Read the list of agenda files from a file.
  17072. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  17073. filenames, used by `org-store-new-agenda-file-list' to write back
  17074. un-expanded file names."
  17075. (when (file-directory-p org-agenda-files)
  17076. (error "`org-agenda-files' cannot be a single directory"))
  17077. (when (stringp org-agenda-files)
  17078. (with-temp-buffer
  17079. (insert-file-contents org-agenda-files)
  17080. (mapcar
  17081. (lambda (f)
  17082. (let ((e (expand-file-name (substitute-in-file-name f)
  17083. org-directory)))
  17084. (if pair-with-expansion
  17085. (cons e f)
  17086. e)))
  17087. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17088. ;;;###autoload
  17089. (defun org-cycle-agenda-files ()
  17090. "Cycle through the files in `org-agenda-files'.
  17091. If the current buffer visits an agenda file, find the next one in the list.
  17092. If the current buffer does not, find the first agenda file."
  17093. (interactive)
  17094. (let* ((fs (or (org-agenda-files t)
  17095. (user-error "No agenda files")))
  17096. (files (copy-sequence fs))
  17097. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17098. file)
  17099. (when tcf
  17100. (while (and (setq file (pop files))
  17101. (not (equal (file-truename file) tcf)))))
  17102. (find-file (car (or files fs)))
  17103. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17104. (defun org-agenda-file-to-front (&optional to-end)
  17105. "Move/add the current file to the top of the agenda file list.
  17106. If the file is not present in the list, it is added to the front. If it is
  17107. present, it is moved there. With optional argument TO-END, add/move to the
  17108. end of the list."
  17109. (interactive "P")
  17110. (let ((org-agenda-skip-unavailable-files nil)
  17111. (file-alist (mapcar (lambda (x)
  17112. (cons (file-truename x) x))
  17113. (org-agenda-files t)))
  17114. (ctf (file-truename
  17115. (or buffer-file-name
  17116. (user-error "Please save the current buffer to a file"))))
  17117. x had)
  17118. (setq x (assoc ctf file-alist) had x)
  17119. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17120. (if to-end
  17121. (setq file-alist (append (delq x file-alist) (list x)))
  17122. (setq file-alist (cons x (delq x file-alist))))
  17123. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17124. (org-install-agenda-files-menu)
  17125. (message "File %s to %s of agenda file list"
  17126. (if had "moved" "added") (if to-end "end" "front"))))
  17127. (defun org-remove-file (&optional file)
  17128. "Remove current file from the list of files in variable `org-agenda-files'.
  17129. These are the files which are being checked for agenda entries.
  17130. Optional argument FILE means use this file instead of the current."
  17131. (interactive)
  17132. (let* ((org-agenda-skip-unavailable-files nil)
  17133. (file (or file buffer-file-name
  17134. (user-error "Current buffer does not visit a file")))
  17135. (true-file (file-truename file))
  17136. (afile (abbreviate-file-name file))
  17137. (files (delq nil (mapcar
  17138. (lambda (x)
  17139. (unless (equal true-file
  17140. (file-truename x))
  17141. x))
  17142. (org-agenda-files t)))))
  17143. (if (not (= (length files) (length (org-agenda-files t))))
  17144. (progn
  17145. (org-store-new-agenda-file-list files)
  17146. (org-install-agenda-files-menu)
  17147. (message "Removed from Org Agenda list: %s" afile))
  17148. (message "File was not in list: %s (not removed)" afile))))
  17149. (defun org-file-menu-entry (file)
  17150. (vector file (list 'find-file file) t))
  17151. (defun org-check-agenda-file (file)
  17152. "Make sure FILE exists. If not, ask user what to do."
  17153. (unless (file-exists-p file)
  17154. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17155. (abbreviate-file-name file))
  17156. (let ((r (downcase (read-char-exclusive))))
  17157. (cond
  17158. ((equal r ?r)
  17159. (org-remove-file file)
  17160. (throw 'nextfile t))
  17161. (t (user-error "Abort"))))))
  17162. (defun org-get-agenda-file-buffer (file)
  17163. "Get an agenda buffer visiting FILE.
  17164. If the buffer needs to be created, add it to the list of buffers
  17165. which might be released later."
  17166. (let ((buf (org-find-base-buffer-visiting file)))
  17167. (if buf
  17168. buf ; just return it
  17169. ;; Make a new buffer and remember it
  17170. (setq buf (find-file-noselect file))
  17171. (when buf (push buf org-agenda-new-buffers))
  17172. buf)))
  17173. (defun org-release-buffers (blist)
  17174. "Release all buffers in list, asking the user for confirmation when needed.
  17175. When a buffer is unmodified, it is just killed. When modified, it is saved
  17176. \(if the user agrees) and then killed."
  17177. (let (file)
  17178. (dolist (buf blist)
  17179. (setq file (buffer-file-name buf))
  17180. (when (and (buffer-modified-p buf)
  17181. file
  17182. (y-or-n-p (format "Save file %s? " file)))
  17183. (with-current-buffer buf (save-buffer)))
  17184. (kill-buffer buf))))
  17185. (defun org-agenda-prepare-buffers (files)
  17186. "Create buffers for all agenda files, protect archived trees and comments."
  17187. (interactive)
  17188. (let ((pa '(:org-archived t))
  17189. (pc '(:org-comment t))
  17190. (pall '(:org-archived t :org-comment t))
  17191. (inhibit-read-only t)
  17192. (org-inhibit-startup org-agenda-inhibit-startup)
  17193. (rea (concat ":" org-archive-tag ":"))
  17194. re pos)
  17195. (setq org-tag-alist-for-agenda nil
  17196. org-tag-groups-alist-for-agenda nil)
  17197. (save-excursion
  17198. (save-restriction
  17199. (dolist (file files)
  17200. (catch 'nextfile
  17201. (if (bufferp file)
  17202. (set-buffer file)
  17203. (org-check-agenda-file file)
  17204. (set-buffer (org-get-agenda-file-buffer file)))
  17205. (widen)
  17206. (org-set-regexps-and-options 'tags-only)
  17207. (setq pos (point))
  17208. (or (memq 'category org-agenda-ignore-properties)
  17209. (org-refresh-category-properties))
  17210. (or (memq 'stats org-agenda-ignore-properties)
  17211. (org-refresh-stats-properties))
  17212. (or (memq 'effort org-agenda-ignore-properties)
  17213. (org-refresh-effort-properties))
  17214. (or (memq 'appt org-agenda-ignore-properties)
  17215. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17216. (setq org-todo-keywords-for-agenda
  17217. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17218. (setq org-done-keywords-for-agenda
  17219. (append org-done-keywords-for-agenda org-done-keywords))
  17220. (setq org-todo-keyword-alist-for-agenda
  17221. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17222. (setq org-tag-alist-for-agenda
  17223. (org-uniquify
  17224. (append org-tag-alist-for-agenda
  17225. org-current-tag-alist)))
  17226. ;; Merge current file's tag groups into global
  17227. ;; `org-tag-groups-alist-for-agenda'.
  17228. (when org-group-tags
  17229. (dolist (alist org-tag-groups-alist)
  17230. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17231. (if old
  17232. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17233. (push alist org-tag-groups-alist-for-agenda)))))
  17234. (org-with-silent-modifications
  17235. (save-excursion
  17236. (remove-text-properties (point-min) (point-max) pall)
  17237. (when org-agenda-skip-archived-trees
  17238. (goto-char (point-min))
  17239. (while (re-search-forward rea nil t)
  17240. (when (org-at-heading-p t)
  17241. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17242. (goto-char (point-min))
  17243. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17244. (while (re-search-forward re nil t)
  17245. (when (save-match-data (org-in-commented-heading-p t))
  17246. (add-text-properties
  17247. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17248. (goto-char pos)))))
  17249. (setq org-todo-keywords-for-agenda
  17250. (org-uniquify org-todo-keywords-for-agenda))
  17251. (setq org-todo-keyword-alist-for-agenda
  17252. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17253. ;;;; CDLaTeX minor mode
  17254. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17255. "Keymap for the minor `org-cdlatex-mode'.")
  17256. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17257. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17258. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17259. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17260. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17261. (defvar org-cdlatex-texmathp-advice-is-done nil
  17262. "Flag remembering if we have applied the advice to texmathp already.")
  17263. (define-minor-mode org-cdlatex-mode
  17264. "Toggle the minor `org-cdlatex-mode'.
  17265. This mode supports entering LaTeX environment and math in LaTeX fragments
  17266. in Org-mode.
  17267. \\{org-cdlatex-mode-map}"
  17268. nil " OCDL" nil
  17269. (when org-cdlatex-mode
  17270. (require 'cdlatex)
  17271. (run-hooks 'cdlatex-mode-hook)
  17272. (cdlatex-compute-tables))
  17273. (unless org-cdlatex-texmathp-advice-is-done
  17274. (setq org-cdlatex-texmathp-advice-is-done t)
  17275. (defadvice texmathp (around org-math-always-on activate)
  17276. "Always return t in org-mode buffers.
  17277. This is because we want to insert math symbols without dollars even outside
  17278. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17279. an embedded LaTeX fragment, let texmathp do its job.
  17280. \\[org-cdlatex-mode-map]"
  17281. (interactive)
  17282. (let (p)
  17283. (cond
  17284. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17285. ((eq this-command 'cdlatex-math-symbol)
  17286. (setq ad-return-value t
  17287. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17288. (t
  17289. (let ((p (org-inside-LaTeX-fragment-p)))
  17290. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17291. (setq ad-return-value t
  17292. texmathp-why '("Org-mode embedded math" . 0))
  17293. (when p ad-do-it)))))))))
  17294. (defun turn-on-org-cdlatex ()
  17295. "Unconditionally turn on `org-cdlatex-mode'."
  17296. (org-cdlatex-mode 1))
  17297. (defun org-try-cdlatex-tab ()
  17298. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17299. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17300. - inside a LaTeX fragment, or
  17301. - after the first word in a line, where an abbreviation expansion could
  17302. insert a LaTeX environment."
  17303. (when org-cdlatex-mode
  17304. (cond
  17305. ;; Before any word on the line: No expansion possible.
  17306. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17307. ;; Just after first word on the line: Expand it. Make sure it
  17308. ;; cannot happen on headlines, though.
  17309. ((save-excursion
  17310. (skip-chars-backward "a-zA-Z0-9*")
  17311. (skip-chars-backward " \t")
  17312. (and (bolp) (not (org-at-heading-p))))
  17313. (cdlatex-tab) t)
  17314. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17315. (defun org-cdlatex-underscore-caret (&optional _arg)
  17316. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17317. Revert to the normal definition outside of these fragments."
  17318. (interactive "P")
  17319. (if (org-inside-LaTeX-fragment-p)
  17320. (call-interactively 'cdlatex-sub-superscript)
  17321. (let (org-cdlatex-mode)
  17322. (call-interactively (key-binding (vector last-input-event))))))
  17323. (defun org-cdlatex-math-modify (&optional _arg)
  17324. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17325. Revert to the normal definition outside of these fragments."
  17326. (interactive "P")
  17327. (if (org-inside-LaTeX-fragment-p)
  17328. (call-interactively 'cdlatex-math-modify)
  17329. (let (org-cdlatex-mode)
  17330. (call-interactively (key-binding (vector last-input-event))))))
  17331. (defun org-cdlatex-environment-indent (&optional environment item)
  17332. "Execute `cdlatex-environment' and indent the inserted environment.
  17333. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17334. The inserted environment is indented to current indentation
  17335. unless point is at the beginning of the line, in which the
  17336. environment remains unintended."
  17337. (interactive)
  17338. ;; cdlatex-environment always return nil. Therefore, capture output
  17339. ;; first and determine if an environment was selected.
  17340. (let* ((beg (point-marker))
  17341. (end (copy-marker (point) t))
  17342. (inserted (progn
  17343. (ignore-errors (cdlatex-environment environment item))
  17344. (< beg end)))
  17345. ;; Figure out how many lines to move forward after the
  17346. ;; environment has been inserted.
  17347. (lines (when inserted
  17348. (save-excursion
  17349. (- (cl-loop while (< beg (point))
  17350. with x = 0
  17351. do (forward-line -1)
  17352. (cl-incf x)
  17353. finally return x)
  17354. (if (progn (goto-char beg)
  17355. (and (progn (skip-chars-forward " \t") (eolp))
  17356. (progn (skip-chars-backward " \t") (bolp))))
  17357. 1 0)))))
  17358. (env (org-trim (delete-and-extract-region beg end))))
  17359. (when inserted
  17360. ;; Get indentation of next line unless at column 0.
  17361. (let ((ind (if (bolp) 0
  17362. (save-excursion
  17363. (org-return-indent)
  17364. (prog1 (org-get-indentation)
  17365. (when (progn (skip-chars-forward " \t") (eolp))
  17366. (delete-region beg (point)))))))
  17367. (bol (progn (skip-chars-backward " \t") (bolp))))
  17368. ;; Insert a newline before environment unless at column zero
  17369. ;; to "escape" the current line. Insert a newline if
  17370. ;; something is one the same line as \end{ENVIRONMENT}.
  17371. (insert
  17372. (concat (unless bol "\n") env
  17373. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17374. (unless (zerop ind)
  17375. (save-excursion
  17376. (goto-char beg)
  17377. (while (< (point) end)
  17378. (unless (eolp) (indent-line-to ind))
  17379. (forward-line))))
  17380. (goto-char beg)
  17381. (forward-line lines)
  17382. (indent-line-to ind)))
  17383. (set-marker beg nil)
  17384. (set-marker end nil)))
  17385. ;;;; LaTeX fragments
  17386. (defun org-inside-LaTeX-fragment-p ()
  17387. "Test if point is inside a LaTeX fragment.
  17388. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17389. sequence appearing also before point.
  17390. Even though the matchers for math are configurable, this function assumes
  17391. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17392. delimiters are skipped when they have been removed by customization.
  17393. The return value is nil, or a cons cell with the delimiter and the
  17394. position of this delimiter.
  17395. This function does a reasonably good job, but can locally be fooled by
  17396. for example currency specifications. For example it will assume being in
  17397. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17398. fragments that are properly closed, but during editing, we have to live
  17399. with the uncertainty caused by missing closing delimiters. This function
  17400. looks only before point, not after."
  17401. (catch 'exit
  17402. (let ((pos (point))
  17403. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17404. (lim (progn
  17405. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17406. (point)))
  17407. dd-on str (start 0) m re)
  17408. (goto-char pos)
  17409. (when dodollar
  17410. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17411. re (nth 1 (assoc "$" org-latex-regexps)))
  17412. (while (string-match re str start)
  17413. (cond
  17414. ((= (match-end 0) (length str))
  17415. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17416. ((= (match-end 0) (- (length str) 5))
  17417. (throw 'exit nil))
  17418. (t (setq start (match-end 0))))))
  17419. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17420. (goto-char pos)
  17421. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17422. (and (match-beginning 2) (throw 'exit nil))
  17423. ;; count $$
  17424. (while (re-search-backward "\\$\\$" lim t)
  17425. (setq dd-on (not dd-on)))
  17426. (goto-char pos)
  17427. (when dd-on (cons "$$" m))))))
  17428. (defun org-inside-latex-macro-p ()
  17429. "Is point inside a LaTeX macro or its arguments?"
  17430. (save-match-data
  17431. (org-in-regexp
  17432. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17433. (defun org--format-latex-make-overlay (beg end image &optional imagetype)
  17434. "Build an overlay between BEG and END using IMAGE file.
  17435. Argument IMAGETYPE is the extension of the displayed image,
  17436. as a string. It defaults to \"png\"."
  17437. (let ((ov (make-overlay beg end))
  17438. (imagetype (or (intern imagetype) 'png)))
  17439. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17440. (overlay-put ov 'evaporate t)
  17441. (overlay-put ov
  17442. 'modification-hooks
  17443. (list (lambda (o _flag _beg _end &optional _l)
  17444. (delete-overlay o))))
  17445. (overlay-put ov
  17446. 'display
  17447. (list 'image :type imagetype :file image :ascent 'center))))
  17448. (defun org--list-latex-overlays (&optional beg end)
  17449. "List all Org LaTeX overlays in current buffer.
  17450. Limit to overlays between BEG and END when those are provided."
  17451. (cl-remove-if-not
  17452. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17453. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17454. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17455. "Remove all overlays with LaTeX fragment images in current buffer.
  17456. When optional arguments BEG and END are non-nil, remove all
  17457. overlays between them instead. Return a non-nil value when some
  17458. overlays were removed, nil otherwise."
  17459. (let ((overlays (org--list-latex-overlays beg end)))
  17460. (mapc #'delete-overlay overlays)
  17461. overlays))
  17462. (define-obsolete-function-alias
  17463. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17464. (defun org-toggle-latex-fragment (&optional arg)
  17465. "Preview the LaTeX fragment at point, or all locally or globally.
  17466. If the cursor is on a LaTeX fragment, create the image and overlay
  17467. it over the source code, if there is none. Remove it otherwise.
  17468. If there is no fragment at point, display all fragments in the
  17469. current section.
  17470. With prefix ARG, preview or clear image for all fragments in the
  17471. current subtree or in the whole buffer when used before the first
  17472. headline. With a double prefix ARG \\[universal-argument] \
  17473. \\[universal-argument] preview or clear images
  17474. for all fragments in the buffer."
  17475. (interactive "P")
  17476. (when (display-graphic-p)
  17477. (catch 'exit
  17478. (save-excursion
  17479. (let ((window-start (window-start)) msg)
  17480. (save-restriction
  17481. (cond
  17482. ((or (equal arg '(16))
  17483. (and (equal arg '(4))
  17484. (org-with-limited-levels (org-before-first-heading-p))))
  17485. (if (org-remove-latex-fragment-image-overlays)
  17486. (progn (message "LaTeX fragments images removed from buffer")
  17487. (throw 'exit nil))
  17488. (setq msg "Creating images for buffer...")))
  17489. ((equal arg '(4))
  17490. (org-with-limited-levels (org-back-to-heading t))
  17491. (let ((beg (point))
  17492. (end (progn (org-end-of-subtree t) (point))))
  17493. (if (org-remove-latex-fragment-image-overlays beg end)
  17494. (progn
  17495. (message "LaTeX fragment images removed from subtree")
  17496. (throw 'exit nil))
  17497. (setq msg "Creating images for subtree...")
  17498. (narrow-to-region beg end))))
  17499. ((let ((datum (org-element-context)))
  17500. (when (memq (org-element-type datum)
  17501. '(latex-environment latex-fragment))
  17502. (let* ((beg (org-element-property :begin datum))
  17503. (end (org-element-property :end datum)))
  17504. (if (org-remove-latex-fragment-image-overlays beg end)
  17505. (progn (message "LaTeX fragment image removed")
  17506. (throw 'exit nil))
  17507. (narrow-to-region beg end)
  17508. (setq msg "Creating image..."))))))
  17509. (t
  17510. (org-with-limited-levels
  17511. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17512. (outline-previous-heading)
  17513. (point)))
  17514. (end (progn (outline-next-heading) (point))))
  17515. (if (org-remove-latex-fragment-image-overlays beg end)
  17516. (progn
  17517. (message "LaTeX fragment images removed from section")
  17518. (throw 'exit nil))
  17519. (setq msg "Creating images for section...")
  17520. (narrow-to-region beg end))))))
  17521. (let ((file (buffer-file-name (buffer-base-buffer))))
  17522. (org-format-latex
  17523. (concat org-preview-latex-image-directory "org-ltximg")
  17524. ;; Emacs cannot overlay images from remote hosts.
  17525. ;; Create it in `temporary-file-directory' instead.
  17526. (if (or (not file) (file-remote-p file))
  17527. temporary-file-directory
  17528. default-directory)
  17529. 'overlays msg 'forbuffer org-preview-latex-default-process)))
  17530. ;; Work around a bug that doesn't restore window's start
  17531. ;; when widening back the buffer.
  17532. (set-window-start nil window-start)
  17533. (message (concat msg "done")))))))
  17534. (defun org-format-latex
  17535. (prefix &optional dir overlays msg forbuffer processing-type)
  17536. "Replace LaTeX fragments with links to an image, and produce images.
  17537. When optional argument OVERLAYS is non-nil, display the image on
  17538. top of the fragment instead of replacing it.
  17539. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17540. Some of the options can be changed using the variable
  17541. `org-format-latex-options', which see."
  17542. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17543. (unless (eq processing-type 'verbatim)
  17544. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17545. (cnt 0)
  17546. checkdir-flag)
  17547. (goto-char (point-min))
  17548. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17549. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17550. (overlay-recenter (point-max)))
  17551. (while (re-search-forward math-regexp nil t)
  17552. (unless (and overlays
  17553. (eq (get-char-property (point) 'org-overlay-type)
  17554. 'org-latex-overlay))
  17555. (let* ((context (org-element-context))
  17556. (type (org-element-type context)))
  17557. (when (memq type '(latex-environment latex-fragment))
  17558. (let ((block-type (eq type 'latex-environment))
  17559. (value (org-element-property :value context))
  17560. (beg (org-element-property :begin context))
  17561. (end (save-excursion
  17562. (goto-char (org-element-property :end context))
  17563. (skip-chars-backward " \r\t\n")
  17564. (point))))
  17565. (cond
  17566. ((eq processing-type 'mathjax)
  17567. ;; Prepare for MathJax processing.
  17568. (if (not (string-match "\\`\\$\\$?" value))
  17569. (goto-char end)
  17570. (delete-region beg end)
  17571. (if (string= (match-string 0 value) "$$")
  17572. (insert "\\[" (substring value 2 -2) "\\]")
  17573. (insert "\\(" (substring value 1 -1) "\\)"))))
  17574. ((assq processing-type org-preview-latex-process-alist)
  17575. ;; Process to an image.
  17576. (cl-incf cnt)
  17577. (goto-char beg)
  17578. (let* ((processing-info
  17579. (cdr (assq processing-type org-preview-latex-process-alist)))
  17580. (face (face-at-point))
  17581. ;; Get the colors from the face at point.
  17582. (fg
  17583. (let ((color (plist-get org-format-latex-options
  17584. :foreground)))
  17585. (if (and forbuffer (eq color 'auto))
  17586. (face-attribute face :foreground nil 'default)
  17587. color)))
  17588. (bg
  17589. (let ((color (plist-get org-format-latex-options
  17590. :background)))
  17591. (if (and forbuffer (eq color 'auto))
  17592. (face-attribute face :background nil 'default)
  17593. color)))
  17594. (hash (sha1 (prin1-to-string
  17595. (list org-format-latex-header
  17596. org-latex-default-packages-alist
  17597. org-latex-packages-alist
  17598. org-format-latex-options
  17599. forbuffer value fg bg))))
  17600. (imagetype (or (plist-get processing-info :image-output-type) "png"))
  17601. (absprefix (expand-file-name prefix dir))
  17602. (linkfile (format "%s_%s.%s" prefix hash imagetype))
  17603. (movefile (format "%s_%s.%s" absprefix hash imagetype))
  17604. (sep (and block-type "\n\n"))
  17605. (link (concat sep "[[file:" linkfile "]]" sep))
  17606. (options
  17607. (org-combine-plists
  17608. org-format-latex-options
  17609. `(:foreground ,fg :background ,bg))))
  17610. (when msg (message msg cnt))
  17611. (unless checkdir-flag ; Ensure the directory exists.
  17612. (setq checkdir-flag t)
  17613. (let ((todir (file-name-directory absprefix)))
  17614. (unless (file-directory-p todir)
  17615. (make-directory todir t))))
  17616. (unless (file-exists-p movefile)
  17617. (org-create-formula-image
  17618. value movefile options forbuffer processing-type))
  17619. (if overlays
  17620. (progn
  17621. (dolist (o (overlays-in beg end))
  17622. (when (eq (overlay-get o 'org-overlay-type)
  17623. 'org-latex-overlay)
  17624. (delete-overlay o)))
  17625. (org--format-latex-make-overlay beg end movefile imagetype)
  17626. (goto-char end))
  17627. (delete-region beg end)
  17628. (insert
  17629. (org-add-props link
  17630. (list 'org-latex-src
  17631. (replace-regexp-in-string "\"" "" value)
  17632. 'org-latex-src-embed-type
  17633. (if block-type 'paragraph 'character)))))))
  17634. ((eq processing-type 'mathml)
  17635. ;; Process to MathML.
  17636. (unless (org-format-latex-mathml-available-p)
  17637. (user-error "LaTeX to MathML converter not configured"))
  17638. (cl-incf cnt)
  17639. (when msg (message msg cnt))
  17640. (goto-char beg)
  17641. (delete-region beg end)
  17642. (insert (org-format-latex-as-mathml
  17643. value block-type prefix dir)))
  17644. (t
  17645. (error "Unknown conversion process %s for LaTeX fragments"
  17646. processing-type)))))))))))
  17647. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17648. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17649. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17650. sucessful, return the portion between \"<math...> </math>\"
  17651. elements otherwise return nil. When MATHML-FILE is specified,
  17652. write the results in to that file. When invoked as an
  17653. interactive command, prompt for LATEX-FRAG, with initial value
  17654. set to the current active region and echo the results for user
  17655. inspection."
  17656. (interactive (list (let ((frag (when (org-region-active-p)
  17657. (buffer-substring-no-properties
  17658. (region-beginning) (region-end)))))
  17659. (read-string "LaTeX Fragment: " frag nil frag))))
  17660. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17661. (let* ((tmp-in-file
  17662. (let ((file (file-relative-name
  17663. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17664. (write-region latex-frag nil file)
  17665. file))
  17666. (tmp-out-file (file-relative-name
  17667. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17668. (cmd (format-spec
  17669. org-latex-to-mathml-convert-command
  17670. `((?j . ,(and org-latex-to-mathml-jar-file
  17671. (shell-quote-argument
  17672. (expand-file-name
  17673. org-latex-to-mathml-jar-file))))
  17674. (?I . ,(shell-quote-argument tmp-in-file))
  17675. (?i . ,latex-frag)
  17676. (?o . ,(shell-quote-argument tmp-out-file)))))
  17677. mathml shell-command-output)
  17678. (when (org-called-interactively-p 'any)
  17679. (unless (org-format-latex-mathml-available-p)
  17680. (user-error "LaTeX to MathML converter not configured")))
  17681. (message "Running %s" cmd)
  17682. (setq shell-command-output (shell-command-to-string cmd))
  17683. (setq mathml
  17684. (when (file-readable-p tmp-out-file)
  17685. (with-current-buffer (find-file-noselect tmp-out-file t)
  17686. (goto-char (point-min))
  17687. (when (re-search-forward
  17688. (concat
  17689. (regexp-quote
  17690. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17691. "[^>]*?>"
  17692. "\\(.\\|\n\\)*"
  17693. "</math>")
  17694. nil t)
  17695. (prog1 (match-string 0) (kill-buffer))))))
  17696. (cond
  17697. (mathml
  17698. (setq mathml
  17699. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17700. (when mathml-file
  17701. (write-region mathml nil mathml-file))
  17702. (when (org-called-interactively-p 'any)
  17703. (message mathml)))
  17704. ((message "LaTeX to MathML conversion failed")
  17705. (message shell-command-output)))
  17706. (delete-file tmp-in-file)
  17707. (when (file-exists-p tmp-out-file)
  17708. (delete-file tmp-out-file))
  17709. mathml))
  17710. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17711. prefix &optional dir)
  17712. "Use `org-create-math-formula' but check local cache first."
  17713. (let* ((absprefix (expand-file-name prefix dir))
  17714. (print-length nil) (print-level nil)
  17715. (formula-id (concat
  17716. "formula-"
  17717. (sha1
  17718. (prin1-to-string
  17719. (list latex-frag
  17720. org-latex-to-mathml-convert-command)))))
  17721. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17722. (formula-cache-dir (file-name-directory formula-cache)))
  17723. (unless (file-directory-p formula-cache-dir)
  17724. (make-directory formula-cache-dir t))
  17725. (unless (file-exists-p formula-cache)
  17726. (org-create-math-formula latex-frag formula-cache))
  17727. (if (file-exists-p formula-cache)
  17728. ;; Successful conversion. Return the link to MathML file.
  17729. (org-add-props
  17730. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17731. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17732. 'org-latex-src-embed-type (if latex-frag-type
  17733. 'paragraph 'character)))
  17734. ;; Failed conversion. Return the LaTeX fragment verbatim
  17735. latex-frag)))
  17736. (declare-function org-export-get-backend "ox" (name))
  17737. (declare-function org-export--get-global-options "ox" (&optional backend))
  17738. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17739. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17740. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17741. (defun org-create-formula--latex-header ()
  17742. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17743. (let ((info (org-combine-plists (org-export--get-global-options
  17744. (org-export-get-backend 'latex))
  17745. (org-export--get-inbuffer-options
  17746. (org-export-get-backend 'latex)))))
  17747. (org-latex-guess-babel-language
  17748. (org-latex-guess-inputenc
  17749. (org-splice-latex-header
  17750. org-format-latex-header
  17751. org-latex-default-packages-alist
  17752. org-latex-packages-alist t
  17753. (plist-get info :latex-header)))
  17754. info)))
  17755. (defun org--get-display-dpi ()
  17756. "Get the DPI of the display.
  17757. Assumes that the display has the same pixel width in the
  17758. horizontal and vertical directions."
  17759. (if (display-graphic-p)
  17760. (round (/ (display-pixel-height)
  17761. (/ (display-mm-height) 25.4)))
  17762. (error "Attempt to calculate the dpi of a non-graphic display")))
  17763. (defun org-create-formula-image
  17764. (string tofile options buffer &optional processing-type)
  17765. "Create an image from LaTeX source using external processes.
  17766. The LaTeX STRING is saved to a temporary LaTeX file, then
  17767. converted to an image file by process PROCESSING-TYPE defined in
  17768. `org-preview-latex-process-alist'. A nil value defaults to
  17769. `org-preview-latex-default-process'.
  17770. The generated image file is eventually moved to TOFILE.
  17771. The OPTIONS argument controls the size, foreground color and
  17772. background color of the generated image.
  17773. When BUFFER non-nil, this function is used for LaTeX previewing.
  17774. Otherwise, it is used to deal with LaTeX snippets showed in
  17775. a HTML file."
  17776. (let* ((processing-type (or processing-type
  17777. org-preview-latex-default-process))
  17778. (processing-info
  17779. (cdr (assq processing-type org-preview-latex-process-alist)))
  17780. (programs (plist-get processing-info :programs))
  17781. (error-message (or (plist-get processing-info :message) ""))
  17782. (use-xcolor (plist-get processing-info :use-xcolor))
  17783. (image-input-type (plist-get processing-info :image-input-type))
  17784. (image-output-type (plist-get processing-info :image-output-type))
  17785. (post-clean (or (plist-get processing-info :post-clean)
  17786. '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
  17787. ".svg" ".png" ".jpg" ".jpeg" ".out")))
  17788. (latex-header (or (plist-get processing-info :latex-header)
  17789. (org-create-formula--latex-header)))
  17790. (latex-compiler (plist-get processing-info :latex-compiler))
  17791. (image-converter (plist-get processing-info :image-converter))
  17792. (tmpdir temporary-file-directory)
  17793. (texfilebase (make-temp-name
  17794. (expand-file-name "orgtex" tmpdir)))
  17795. (texfile (concat texfilebase ".tex"))
  17796. (font-height (face-attribute 'default :height nil))
  17797. (image-size-adjust (or (plist-get processing-info :image-size-adjust)
  17798. '(1.0 . 1.0)))
  17799. (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
  17800. (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
  17801. (dpi (* scale (floor (if buffer font-height 140.0))))
  17802. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17803. "Black"))
  17804. (bg (or (plist-get options (if buffer :background :html-background))
  17805. "Transparent"))
  17806. (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
  17807. (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
  17808. (dolist (program programs)
  17809. (org-check-external-command program error-message))
  17810. (if use-xcolor
  17811. (progn (if (eq fg 'default)
  17812. (setq fg (org-latex-color :foreground))
  17813. (setq fg (org-latex-color-format fg)))
  17814. (if (eq bg 'default)
  17815. (setq bg (org-latex-color :background))
  17816. (setq bg (org-latex-color-format
  17817. (if (string= bg "Transparent") "white" bg))))
  17818. (with-temp-file texfile
  17819. (insert latex-header)
  17820. (insert "\n\\begin{document}\n"
  17821. "\\definecolor{fg}{rgb}{" fg "}\n"
  17822. "\\definecolor{bg}{rgb}{" bg "}\n"
  17823. "\n\\pagecolor{bg}\n"
  17824. "\n{\\color{fg}\n"
  17825. string
  17826. "\n}\n"
  17827. "\n\\end{document}\n")))
  17828. (if (eq fg 'default)
  17829. (setq fg (org-dvipng-color :foreground))
  17830. (unless (string= fg "Transparent")
  17831. (setq fg (org-dvipng-color-format fg))))
  17832. (if (eq bg 'default)
  17833. (setq bg (org-dvipng-color :background))
  17834. (unless (string= bg "Transparent")
  17835. (setq bg (org-dvipng-color-format bg))))
  17836. (with-temp-file texfile
  17837. (insert latex-header)
  17838. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17839. (let* ((err-msg (format "Please adjust '%s' part of \
  17840. `org-preview-latex-process-alist'."
  17841. processing-type))
  17842. (image-input-file
  17843. (org-compile-file
  17844. texfile latex-compiler image-input-type err-msg log-buf))
  17845. (image-output-file
  17846. (org-compile-file
  17847. image-input-file image-converter image-output-type err-msg log-buf
  17848. `((?F . ,(shell-quote-argument fg))
  17849. (?B . ,(shell-quote-argument bg))
  17850. (?D . ,(shell-quote-argument (format "%s" dpi)))
  17851. (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
  17852. (copy-file image-output-file tofile 'replace)
  17853. (dolist (e post-clean)
  17854. (when (file-exists-p (concat texfilebase e))
  17855. (delete-file (concat texfilebase e))))
  17856. image-output-file)))
  17857. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17858. "Fill a LaTeX header template TPL.
  17859. In the template, the following place holders will be recognized:
  17860. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17861. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17862. [PACKAGES] \\usepackage statements for PKG
  17863. [NO-PACKAGES] do not include PKG
  17864. [EXTRA] the string EXTRA
  17865. [NO-EXTRA] do not include EXTRA
  17866. For backward compatibility, if both the positive and the negative place
  17867. holder is missing, the positive one (without the \"NO-\") will be
  17868. assumed to be present at the end of the template.
  17869. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17870. EXTRA is a string.
  17871. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17872. (let (rpl (end ""))
  17873. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17874. (setq rpl (if (or (match-end 1) (not def-pkg))
  17875. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17876. tpl (replace-match rpl t t tpl))
  17877. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17878. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17879. (setq rpl (if (or (match-end 1) (not pkg))
  17880. "" (org-latex-packages-to-string pkg snippets-p t))
  17881. tpl (replace-match rpl t t tpl))
  17882. (when pkg (setq end
  17883. (concat end "\n"
  17884. (org-latex-packages-to-string pkg snippets-p)))))
  17885. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17886. (setq rpl (if (or (match-end 1) (not extra))
  17887. "" (concat extra "\n"))
  17888. tpl (replace-match rpl t t tpl))
  17889. (when (and extra (string-match "\\S-" extra))
  17890. (setq end (concat end "\n" extra))))
  17891. (if (string-match "\\S-" end)
  17892. (concat tpl "\n" end)
  17893. tpl)))
  17894. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17895. "Turn an alist of packages into a string with the \\usepackage macros."
  17896. (setq pkg (mapconcat (lambda(p)
  17897. (cond
  17898. ((stringp p) p)
  17899. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17900. (format "%% Package %s omitted" (cadr p)))
  17901. ((equal "" (car p))
  17902. (format "\\usepackage{%s}" (cadr p)))
  17903. (t
  17904. (format "\\usepackage[%s]{%s}"
  17905. (car p) (cadr p)))))
  17906. pkg
  17907. "\n"))
  17908. (if newline (concat pkg "\n") pkg))
  17909. (defun org-dvipng-color (attr)
  17910. "Return a RGB color specification for dvipng."
  17911. (apply #'format "rgb %s %s %s"
  17912. (mapcar #'org-normalize-color
  17913. (color-values (face-attribute 'default attr nil)))))
  17914. (defun org-dvipng-color-format (color-name)
  17915. "Convert COLOR-NAME to a RGB color value for dvipng."
  17916. (apply 'format "rgb %s %s %s"
  17917. (mapcar 'org-normalize-color
  17918. (color-values color-name))))
  17919. (defun org-latex-color (attr)
  17920. "Return a RGB color for the LaTeX color package."
  17921. (apply #'format "%s,%s,%s"
  17922. (mapcar #'org-normalize-color
  17923. (color-values (face-attribute 'default attr nil)))))
  17924. (defun org-latex-color-format (color-name)
  17925. "Convert COLOR-NAME to a RGB color value."
  17926. (apply 'format "%s,%s,%s"
  17927. (mapcar 'org-normalize-color
  17928. (color-values color-name))))
  17929. (defun org-normalize-color (value)
  17930. "Return string to be used as color value for an RGB component."
  17931. (format "%g" (/ value 65535.0)))
  17932. ;; Image display
  17933. (defvar-local org-inline-image-overlays nil)
  17934. (defun org-toggle-inline-images (&optional include-linked)
  17935. "Toggle the display of inline images.
  17936. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17937. (interactive "P")
  17938. (if org-inline-image-overlays
  17939. (progn
  17940. (org-remove-inline-images)
  17941. (when (org-called-interactively-p 'interactive)
  17942. (message "Inline image display turned off")))
  17943. (org-display-inline-images include-linked)
  17944. (when (org-called-interactively-p 'interactive)
  17945. (message (if org-inline-image-overlays
  17946. (format "%d images displayed inline"
  17947. (length org-inline-image-overlays))
  17948. "No images to display inline")))))
  17949. (defun org-redisplay-inline-images ()
  17950. "Refresh the display of inline images."
  17951. (interactive)
  17952. (if (not org-inline-image-overlays)
  17953. (org-toggle-inline-images)
  17954. (org-toggle-inline-images)
  17955. (org-toggle-inline-images)))
  17956. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17957. "Display inline images.
  17958. An inline image is a link which follows either of these
  17959. conventions:
  17960. 1. Its path is a file with an extension matching return value
  17961. from `image-file-name-regexp' and it has no contents.
  17962. 2. Its description consists in a single link of the previous
  17963. type.
  17964. When optional argument INCLUDE-LINKED is non-nil, also links with
  17965. a text description part will be inlined. This can be nice for
  17966. a quick look at those images, but it does not reflect what
  17967. exported files will look like.
  17968. When optional argument REFRESH is non-nil, refresh existing
  17969. images between BEG and END. This will create new image displays
  17970. only if necessary. BEG and END default to the buffer
  17971. boundaries."
  17972. (interactive "P")
  17973. (when (display-graphic-p)
  17974. (unless refresh
  17975. (org-remove-inline-images)
  17976. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17977. (org-with-wide-buffer
  17978. (goto-char (or beg (point-min)))
  17979. (let ((case-fold-search t)
  17980. (file-extension-re (image-file-name-regexp)))
  17981. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17982. (let ((link (save-match-data (org-element-context))))
  17983. ;; Check if we're at an inline image.
  17984. (when (and (equal (org-element-property :type link) "file")
  17985. (or include-linked
  17986. (not (org-element-property :contents-begin link)))
  17987. (let ((parent (org-element-property :parent link)))
  17988. (or (not (eq (org-element-type parent) 'link))
  17989. (not (cdr (org-element-contents parent)))))
  17990. (org-string-match-p file-extension-re
  17991. (org-element-property :path link)))
  17992. (let ((file (expand-file-name
  17993. (org-link-unescape
  17994. (org-element-property :path link)))))
  17995. (when (file-exists-p file)
  17996. (let ((width
  17997. ;; Apply `org-image-actual-width' specifications.
  17998. (cond
  17999. ((not (image-type-available-p 'imagemagick)) nil)
  18000. ((eq org-image-actual-width t) nil)
  18001. ((listp org-image-actual-width)
  18002. (or
  18003. ;; First try to find a width among
  18004. ;; attributes associated to the paragraph
  18005. ;; containing link.
  18006. (let ((paragraph
  18007. (let ((e link))
  18008. (while (and (setq e (org-element-property
  18009. :parent e))
  18010. (not (eq (org-element-type e)
  18011. 'paragraph))))
  18012. e)))
  18013. (when paragraph
  18014. (save-excursion
  18015. (goto-char (org-element-property :begin paragraph))
  18016. (when
  18017. (re-search-forward
  18018. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18019. (org-element-property
  18020. :post-affiliated paragraph)
  18021. t)
  18022. (string-to-number (match-string 1))))))
  18023. ;; Otherwise, fall-back to provided number.
  18024. (car org-image-actual-width)))
  18025. ((numberp org-image-actual-width)
  18026. org-image-actual-width)))
  18027. (old (get-char-property-and-overlay
  18028. (org-element-property :begin link)
  18029. 'org-image-overlay)))
  18030. (if (and (car-safe old) refresh)
  18031. (image-refresh (overlay-get (cdr old) 'display))
  18032. (let ((image (create-image file
  18033. (and width 'imagemagick)
  18034. nil
  18035. :width width)))
  18036. (when image
  18037. (let* ((link
  18038. ;; If inline image is the description
  18039. ;; of another link, be sure to
  18040. ;; consider the latter as the one to
  18041. ;; apply the overlay on.
  18042. (let ((parent
  18043. (org-element-property :parent link)))
  18044. (if (eq (org-element-type parent) 'link)
  18045. parent
  18046. link)))
  18047. (ov (make-overlay
  18048. (org-element-property :begin link)
  18049. (progn
  18050. (goto-char
  18051. (org-element-property :end link))
  18052. (skip-chars-backward " \t")
  18053. (point)))))
  18054. (overlay-put ov 'display image)
  18055. (overlay-put ov 'face 'default)
  18056. (overlay-put ov 'org-image-overlay t)
  18057. (overlay-put
  18058. ov 'modification-hooks
  18059. (list 'org-display-inline-remove-overlay))
  18060. (push ov org-inline-image-overlays)))))))))))))))
  18061. (define-obsolete-function-alias
  18062. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18063. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  18064. "Remove inline-display overlay if a corresponding region is modified."
  18065. (let ((inhibit-modification-hooks t))
  18066. (when (and ov after)
  18067. (delete ov org-inline-image-overlays)
  18068. (delete-overlay ov))))
  18069. (defun org-remove-inline-images ()
  18070. "Remove inline display of images."
  18071. (interactive)
  18072. (mapc #'delete-overlay org-inline-image-overlays)
  18073. (setq org-inline-image-overlays nil))
  18074. ;;;; Key bindings
  18075. ;; Outline functions from `outline-mode-prefix-map'
  18076. ;; that can be remapped in Org:
  18077. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18078. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18079. (define-key org-mode-map [remap outline-forward-same-level]
  18080. 'org-forward-heading-same-level)
  18081. (define-key org-mode-map [remap outline-backward-same-level]
  18082. 'org-backward-heading-same-level)
  18083. (define-key org-mode-map [remap outline-show-branches]
  18084. 'org-kill-note-or-show-branches)
  18085. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18086. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18087. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18088. (define-key org-mode-map [remap outline-next-visible-heading]
  18089. 'org-next-visible-heading)
  18090. (define-key org-mode-map [remap outline-previous-visible-heading]
  18091. 'org-previous-visible-heading)
  18092. (define-key org-mode-map [remap show-children] 'org-show-children)
  18093. ;; Outline functions from `outline-mode-prefix-map' that can not
  18094. ;; be remapped in Org:
  18095. ;; - the column "key binding" shows whether the Outline function is still
  18096. ;; available in Org mode on the same key that it has been bound to in
  18097. ;; Outline mode:
  18098. ;; - "overridden": key used for a different functionality in Org mode
  18099. ;; - else: key still bound to the same Outline function in Org mode
  18100. ;; | Outline function | key binding | Org replacement |
  18101. ;; |------------------------------------+-------------+--------------------------|
  18102. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18103. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18104. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18105. ;; | `show-entry' | overridden | no replacement |
  18106. ;; | `show-branches' | `C-c C-k' | still same function |
  18107. ;; | `show-subtree' | overridden | visibility cycling |
  18108. ;; | `show-all' | overridden | no replacement |
  18109. ;; | `hide-subtree' | overridden | visibility cycling |
  18110. ;; | `hide-body' | overridden | no replacement |
  18111. ;; | `hide-entry' | overridden | visibility cycling |
  18112. ;; | `hide-leaves' | overridden | no replacement |
  18113. ;; | `hide-sublevels' | overridden | no replacement |
  18114. ;; | `hide-other' | overridden | no replacement |
  18115. ;; Make `C-c C-x' a prefix key
  18116. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18117. ;; TAB key with modifiers
  18118. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18119. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18120. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18121. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18122. ;; The following line is necessary under Suse GNU/Linux
  18123. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab)
  18124. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18125. (define-key org-mode-map [backtab] 'org-shifttab)
  18126. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18127. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18128. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18129. ;; Cursor keys with modifiers
  18130. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18131. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18132. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18133. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18134. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18135. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18136. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18137. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18138. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18139. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18140. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18141. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18142. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18143. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18144. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18145. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18146. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18147. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18148. ;; Babel keys
  18149. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18150. (dolist (pair org-babel-key-bindings)
  18151. (define-key org-babel-map (car pair) (cdr pair)))
  18152. ;;; Extra keys for tty access.
  18153. ;; We only set them when really needed because otherwise the
  18154. ;; menus don't show the simple keys
  18155. (when (or org-use-extra-keys (not window-system))
  18156. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18157. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18158. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18159. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18160. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18161. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18162. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18163. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18164. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18165. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18166. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18167. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18168. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18169. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18170. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18171. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18172. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18173. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18174. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18175. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18176. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18177. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18178. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18179. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18180. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18181. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18182. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18183. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18184. ;; All the other keys
  18185. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18186. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18187. (if (boundp 'narrow-map)
  18188. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18189. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18190. (if (boundp 'narrow-map)
  18191. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18192. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18193. (if (boundp 'narrow-map)
  18194. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18195. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18196. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18197. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18198. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18199. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18200. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18201. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18202. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18203. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18204. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18205. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18206. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18207. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18208. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18209. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18210. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18211. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18212. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18213. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18214. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18215. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18216. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18217. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18218. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18219. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18220. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18221. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18222. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18223. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18224. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18225. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18226. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18227. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18228. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18229. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18230. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18231. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18232. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18233. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18234. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18235. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18236. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18237. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18238. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18239. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18240. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18241. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18242. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18243. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18244. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18245. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18246. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18247. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18248. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18249. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18250. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18251. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18252. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18253. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18254. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18255. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18256. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18257. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18258. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18259. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18260. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18261. (org-defkey org-mode-map "\C-m" 'org-return)
  18262. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18263. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18264. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18265. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18266. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18267. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18268. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18269. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18270. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18271. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18272. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18273. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18274. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18275. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18276. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18277. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18278. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18280. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18283. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18284. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18285. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18286. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18288. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18293. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18294. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18295. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18296. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18297. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18298. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18299. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18300. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18301. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18302. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18303. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18304. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18305. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18306. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18307. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18308. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18309. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18310. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  18311. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18312. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18313. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18314. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18315. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18316. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18317. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18318. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18319. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18320. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18321. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18322. (defconst org-speed-commands-default
  18323. '(
  18324. ("Outline Navigation")
  18325. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18326. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18327. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18328. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18329. ("F" . org-next-block)
  18330. ("B" . org-previous-block)
  18331. ("u" . (org-speed-move-safe 'outline-up-heading))
  18332. ("j" . org-goto)
  18333. ("g" . (org-refile t))
  18334. ("Outline Visibility")
  18335. ("c" . org-cycle)
  18336. ("C" . org-shifttab)
  18337. (" " . org-display-outline-path)
  18338. ("s" . org-narrow-to-subtree)
  18339. ("=" . org-columns)
  18340. ("Outline Structure Editing")
  18341. ("U" . org-metaup)
  18342. ("D" . org-metadown)
  18343. ("r" . org-metaright)
  18344. ("l" . org-metaleft)
  18345. ("R" . org-shiftmetaright)
  18346. ("L" . org-shiftmetaleft)
  18347. ("i" . (progn (forward-char 1) (call-interactively
  18348. 'org-insert-heading-respect-content)))
  18349. ("^" . org-sort)
  18350. ("w" . org-refile)
  18351. ("a" . org-archive-subtree-default-with-confirmation)
  18352. ("@" . org-mark-subtree)
  18353. ("#" . org-toggle-comment)
  18354. ("Clock Commands")
  18355. ("I" . org-clock-in)
  18356. ("O" . org-clock-out)
  18357. ("Meta Data Editing")
  18358. ("t" . org-todo)
  18359. ("," . (org-priority))
  18360. ("0" . (org-priority ?\ ))
  18361. ("1" . (org-priority ?A))
  18362. ("2" . (org-priority ?B))
  18363. ("3" . (org-priority ?C))
  18364. (":" . org-set-tags-command)
  18365. ("e" . org-set-effort)
  18366. ("E" . org-inc-effort)
  18367. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18368. (org-entry-put (point) "APPT_WARNTIME" m)))
  18369. ("Agenda Views etc")
  18370. ("v" . org-agenda)
  18371. ("/" . org-sparse-tree)
  18372. ("Misc")
  18373. ("o" . org-open-at-point)
  18374. ("?" . org-speed-command-help)
  18375. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18376. (">" . (org-agenda-remove-restriction-lock))
  18377. )
  18378. "The default speed commands.")
  18379. (defun org-print-speed-command (e)
  18380. (if (> (length (car e)) 1)
  18381. (progn
  18382. (princ "\n")
  18383. (princ (car e))
  18384. (princ "\n")
  18385. (princ (make-string (length (car e)) ?-))
  18386. (princ "\n"))
  18387. (princ (car e))
  18388. (princ " ")
  18389. (if (symbolp (cdr e))
  18390. (princ (symbol-name (cdr e)))
  18391. (prin1 (cdr e)))
  18392. (princ "\n")))
  18393. (defun org-speed-command-help ()
  18394. "Show the available speed commands."
  18395. (interactive)
  18396. (if (not org-use-speed-commands)
  18397. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18398. (with-output-to-temp-buffer "*Help*"
  18399. (princ "User-defined Speed commands\n===========================\n")
  18400. (mapc #'org-print-speed-command org-speed-commands-user)
  18401. (princ "\n")
  18402. (princ "Built-in Speed commands\n=======================\n")
  18403. (mapc #'org-print-speed-command org-speed-commands-default))
  18404. (with-current-buffer "*Help*"
  18405. (setq truncate-lines t))))
  18406. (defun org-speed-move-safe (cmd)
  18407. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18408. If not, return to the original position and throw an error."
  18409. (interactive)
  18410. (let ((pos (point)))
  18411. (call-interactively cmd)
  18412. (unless (and (bolp) (org-at-heading-p))
  18413. (goto-char pos)
  18414. (error "Boundary reached while executing %s" cmd))))
  18415. (defvar org-self-insert-command-undo-counter 0)
  18416. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18417. (defvar org-speed-command nil)
  18418. (define-obsolete-function-alias
  18419. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18420. (defun org-speed-command-activate (keys)
  18421. "Hook for activating single-letter speed commands.
  18422. `org-speed-commands-default' specifies a minimal command set.
  18423. Use `org-speed-commands-user' for further customization."
  18424. (when (or (and (bolp) (looking-at org-outline-regexp))
  18425. (and (functionp org-use-speed-commands)
  18426. (funcall org-use-speed-commands)))
  18427. (cdr (assoc keys (append org-speed-commands-user
  18428. org-speed-commands-default)))))
  18429. (define-obsolete-function-alias
  18430. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18431. (defun org-babel-speed-command-activate (keys)
  18432. "Hook for activating single-letter code block commands."
  18433. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18434. (cdr (assoc keys org-babel-key-bindings))))
  18435. (defcustom org-speed-command-hook
  18436. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18437. "Hook for activating speed commands at strategic locations.
  18438. Hook functions are called in sequence until a valid handler is
  18439. found.
  18440. Each hook takes a single argument, a user-pressed command key
  18441. which is also a `self-insert-command' from the global map.
  18442. Within the hook, examine the cursor position and the command key
  18443. and return nil or a valid handler as appropriate. Handler could
  18444. be one of an interactive command, a function, or a form.
  18445. Set `org-use-speed-commands' to non-nil value to enable this
  18446. hook. The default setting is `org-speed-command-activate'."
  18447. :group 'org-structure
  18448. :version "24.1"
  18449. :type 'hook)
  18450. (defun org-self-insert-command (N)
  18451. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18452. If the cursor is in a table looking at whitespace, the whitespace is
  18453. overwritten, and the table is not marked as requiring realignment."
  18454. (interactive "p")
  18455. (org-check-before-invisible-edit 'insert)
  18456. (cond
  18457. ((and org-use-speed-commands
  18458. (let ((kv (this-command-keys-vector)))
  18459. (setq org-speed-command
  18460. (run-hook-with-args-until-success
  18461. 'org-speed-command-hook
  18462. (make-string 1 (aref kv (1- (length kv))))))))
  18463. (cond
  18464. ((commandp org-speed-command)
  18465. (setq this-command org-speed-command)
  18466. (call-interactively org-speed-command))
  18467. ((functionp org-speed-command)
  18468. (funcall org-speed-command))
  18469. ((and org-speed-command (listp org-speed-command))
  18470. (eval org-speed-command))
  18471. (t (let (org-use-speed-commands)
  18472. (call-interactively 'org-self-insert-command)))))
  18473. ((and
  18474. (org-at-table-p)
  18475. (progn
  18476. ;; Check if we blank the field, and if that triggers align.
  18477. (and (featurep 'org-table) org-table-auto-blank-field
  18478. (memq last-command
  18479. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18480. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18481. ;; Got extra space, this field does not determine
  18482. ;; column width.
  18483. (let (org-table-may-need-update) (org-table-blank-field))
  18484. ;; No extra space, this field may determine column
  18485. ;; width.
  18486. (org-table-blank-field)))
  18487. t)
  18488. (eq N 1)
  18489. (looking-at "[^|\n]* \\( \\)|"))
  18490. ;; There is room for insertion without re-aligning the table.
  18491. (delete-region (match-beginning 1) (match-end 1))
  18492. (self-insert-command N))
  18493. (t
  18494. (setq org-table-may-need-update t)
  18495. (self-insert-command N)
  18496. (org-fix-tags-on-the-fly)
  18497. (when org-self-insert-cluster-for-undo
  18498. (if (not (eq last-command 'org-self-insert-command))
  18499. (setq org-self-insert-command-undo-counter 1)
  18500. (if (>= org-self-insert-command-undo-counter 20)
  18501. (setq org-self-insert-command-undo-counter 1)
  18502. (and (> org-self-insert-command-undo-counter 0)
  18503. buffer-undo-list (listp buffer-undo-list)
  18504. (not (cadr buffer-undo-list)) ; remove nil entry
  18505. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18506. (setq org-self-insert-command-undo-counter
  18507. (1+ org-self-insert-command-undo-counter))))))))
  18508. (defun org-check-before-invisible-edit (kind)
  18509. "Check is editing if kind KIND would be dangerous with invisible text around.
  18510. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18511. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18512. (when (and org-catch-invisible-edits
  18513. (or (not (boundp 'visible-mode)) (not visible-mode))
  18514. (or (get-char-property (point) 'invisible)
  18515. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18516. ;; OK, we need to take a closer look
  18517. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18518. (invisible-before-point (unless (bobp) (get-char-property
  18519. (1- (point)) 'invisible)))
  18520. (border-and-ok-direction
  18521. (or
  18522. ;; Check if we are acting predictably before invisible text
  18523. (and invisible-at-point (not invisible-before-point)
  18524. (memq kind '(insert delete-backward)))
  18525. ;; Check if we are acting predictably after invisible text
  18526. ;; This works not well, and I have turned it off. It seems
  18527. ;; better to always show and stop after invisible text.
  18528. ;; (and (not invisible-at-point) invisible-before-point
  18529. ;; (memq kind '(insert delete)))
  18530. )))
  18531. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18532. (memq invisible-before-point '(outline org-hide-block t)))
  18533. (when (eq org-catch-invisible-edits 'error)
  18534. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18535. (if (and org-custom-properties-overlays
  18536. (y-or-n-p "Display invisible properties in this buffer? "))
  18537. (org-toggle-custom-properties-visibility)
  18538. ;; Make the area visible
  18539. (save-excursion
  18540. (when invisible-before-point
  18541. (goto-char (previous-single-char-property-change
  18542. (point) 'invisible)))
  18543. (outline-show-subtree))
  18544. (cond
  18545. ((eq org-catch-invisible-edits 'show)
  18546. ;; That's it, we do the edit after showing
  18547. (message
  18548. "Unfolding invisible region around point before editing")
  18549. (sit-for 1))
  18550. ((and (eq org-catch-invisible-edits 'smart)
  18551. border-and-ok-direction)
  18552. (message "Unfolding invisible region around point before editing"))
  18553. (t
  18554. ;; Don't do the edit, make the user repeat it in full visibility
  18555. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18556. (defun org-fix-tags-on-the-fly ()
  18557. (when (and (equal (char-after (point-at-bol)) ?*)
  18558. (org-at-heading-p))
  18559. (org-align-tags-here org-tags-column)))
  18560. (defun org-delete-backward-char (N)
  18561. "Like `delete-backward-char', insert whitespace at field end in tables.
  18562. When deleting backwards, in tables this function will insert whitespace in
  18563. front of the next \"|\" separator, to keep the table aligned. The table will
  18564. still be marked for re-alignment if the field did fill the entire column,
  18565. because, in this case the deletion might narrow the column."
  18566. (interactive "p")
  18567. (save-match-data
  18568. (org-check-before-invisible-edit 'delete-backward)
  18569. (if (and (org-at-table-p)
  18570. (eq N 1)
  18571. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18572. (looking-at ".*?|"))
  18573. (let ((pos (point))
  18574. (noalign (looking-at "[^|\n\r]* |"))
  18575. (c org-table-may-need-update))
  18576. (backward-delete-char N)
  18577. (unless overwrite-mode
  18578. (skip-chars-forward "^|")
  18579. (insert " ")
  18580. (goto-char (1- pos)))
  18581. ;; noalign: if there were two spaces at the end, this field
  18582. ;; does not determine the width of the column.
  18583. (when noalign (setq org-table-may-need-update c)))
  18584. (backward-delete-char N)
  18585. (org-fix-tags-on-the-fly))))
  18586. (defun org-delete-char (N)
  18587. "Like `delete-char', but insert whitespace at field end in tables.
  18588. When deleting characters, in tables this function will insert whitespace in
  18589. front of the next \"|\" separator, to keep the table aligned. The table will
  18590. still be marked for re-alignment if the field did fill the entire column,
  18591. because, in this case the deletion might narrow the column."
  18592. (interactive "p")
  18593. (save-match-data
  18594. (org-check-before-invisible-edit 'delete)
  18595. (if (and (org-at-table-p)
  18596. (not (bolp))
  18597. (not (= (char-after) ?|))
  18598. (eq N 1))
  18599. (if (looking-at ".*?|")
  18600. (let ((pos (point))
  18601. (noalign (looking-at "[^|\n\r]* |"))
  18602. (c org-table-may-need-update))
  18603. (replace-match
  18604. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18605. (goto-char pos)
  18606. ;; noalign: if there were two spaces at the end, this field
  18607. ;; does not determine the width of the column.
  18608. (when noalign (setq org-table-may-need-update c)))
  18609. (delete-char N))
  18610. (delete-char N)
  18611. (org-fix-tags-on-the-fly))))
  18612. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18613. (put 'org-self-insert-command 'delete-selection
  18614. (lambda ()
  18615. (not (run-hook-with-args-until-success
  18616. 'self-insert-uses-region-functions))))
  18617. (put 'orgtbl-self-insert-command 'delete-selection
  18618. (lambda ()
  18619. (not (run-hook-with-args-until-success
  18620. 'self-insert-uses-region-functions))))
  18621. (put 'org-delete-char 'delete-selection 'supersede)
  18622. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18623. (put 'org-yank 'delete-selection 'yank)
  18624. ;; Make `flyspell-mode' delay after some commands
  18625. (put 'org-self-insert-command 'flyspell-delayed t)
  18626. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18627. (put 'org-delete-char 'flyspell-delayed t)
  18628. (put 'org-delete-backward-char 'flyspell-delayed t)
  18629. ;; Make pabbrev-mode expand after org-mode commands
  18630. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18631. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18632. (defun org-remap (map &rest commands)
  18633. "In MAP, remap the functions given in COMMANDS.
  18634. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18635. (let (new old)
  18636. (while commands
  18637. (setq old (pop commands) new (pop commands))
  18638. (if (fboundp 'command-remapping)
  18639. (org-defkey map (vector 'remap old) new)
  18640. (substitute-key-definition old new map global-map)))))
  18641. (defun org-transpose-words ()
  18642. "Transpose words for Org.
  18643. This uses the `org-mode-transpose-word-syntax-table' syntax
  18644. table, which interprets characters in `org-emphasis-alist' as
  18645. word constituents."
  18646. (interactive)
  18647. (with-syntax-table org-mode-transpose-word-syntax-table
  18648. (call-interactively 'transpose-words)))
  18649. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18650. (when (eq org-enable-table-editor 'optimized)
  18651. ;; If the user wants maximum table support, we need to hijack
  18652. ;; some standard editing functions
  18653. (org-remap org-mode-map
  18654. 'self-insert-command 'org-self-insert-command
  18655. 'delete-char 'org-delete-char
  18656. 'delete-backward-char 'org-delete-backward-char)
  18657. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18658. (defvar org-ctrl-c-ctrl-c-hook nil
  18659. "Hook for functions attaching themselves to `C-c C-c'.
  18660. This can be used to add additional functionality to the C-c C-c
  18661. key which executes context-dependent commands. This hook is run
  18662. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18663. run after the last test.
  18664. Each function will be called with no arguments. The function
  18665. must check if the context is appropriate for it to act. If yes,
  18666. it should do its thing and then return a non-nil value. If the
  18667. context is wrong, just do nothing and return nil.")
  18668. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18669. "Hook for functions attaching themselves to `C-c C-c'.
  18670. This can be used to add additional functionality to the C-c C-c
  18671. key which executes context-dependent commands. This hook is run
  18672. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18673. before the first test.
  18674. Each function will be called with no arguments. The function
  18675. must check if the context is appropriate for it to act. If yes,
  18676. it should do its thing and then return a non-nil value. If the
  18677. context is wrong, just do nothing and return nil.")
  18678. (defvar org-tab-first-hook nil
  18679. "Hook for functions to attach themselves to TAB.
  18680. See `org-ctrl-c-ctrl-c-hook' for more information.
  18681. This hook runs as the first action when TAB is pressed, even before
  18682. `org-cycle' messes around with the `outline-regexp' to cater for
  18683. inline tasks and plain list item folding.
  18684. If any function in this hook returns t, any other actions that
  18685. would have been caused by TAB (such as table field motion or visibility
  18686. cycling) will not occur.")
  18687. (defvar org-tab-after-check-for-table-hook nil
  18688. "Hook for functions to attach themselves to TAB.
  18689. See `org-ctrl-c-ctrl-c-hook' for more information.
  18690. This hook runs after it has been established that the cursor is not in a
  18691. table, but before checking if the cursor is in a headline or if global cycling
  18692. should be done.
  18693. If any function in this hook returns t, not other actions like visibility
  18694. cycling will be done.")
  18695. (defvar org-tab-after-check-for-cycling-hook nil
  18696. "Hook for functions to attach themselves to TAB.
  18697. See `org-ctrl-c-ctrl-c-hook' for more information.
  18698. This hook runs after it has been established that not table field motion and
  18699. not visibility should be done because of current context. This is probably
  18700. the place where a package like yasnippets can hook in.")
  18701. (defvar org-tab-before-tab-emulation-hook nil
  18702. "Hook for functions to attach themselves to TAB.
  18703. See `org-ctrl-c-ctrl-c-hook' for more information.
  18704. This hook runs after every other options for TAB have been exhausted, but
  18705. before indentation and \t insertion takes place.")
  18706. (defvar org-metaleft-hook nil
  18707. "Hook for functions attaching themselves to `M-left'.
  18708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18709. (defvar org-metaright-hook nil
  18710. "Hook for functions attaching themselves to `M-right'.
  18711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18712. (defvar org-metaup-hook nil
  18713. "Hook for functions attaching themselves to `M-up'.
  18714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18715. (defvar org-metadown-hook nil
  18716. "Hook for functions attaching themselves to `M-down'.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-shiftmetaleft-hook nil
  18719. "Hook for functions attaching themselves to `M-S-left'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-shiftmetaright-hook nil
  18722. "Hook for functions attaching themselves to `M-S-right'.
  18723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18724. (defvar org-shiftmetaup-hook nil
  18725. "Hook for functions attaching themselves to `M-S-up'.
  18726. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18727. (defvar org-shiftmetadown-hook nil
  18728. "Hook for functions attaching themselves to `M-S-down'.
  18729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18730. (defvar org-metareturn-hook nil
  18731. "Hook for functions attaching themselves to `M-RET'.
  18732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18733. (defvar org-shiftup-hook nil
  18734. "Hook for functions attaching themselves to `S-up'.
  18735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18736. (defvar org-shiftup-final-hook nil
  18737. "Hook for functions attaching themselves to `S-up'.
  18738. This one runs after all other options except shift-select have been excluded.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-shiftdown-hook nil
  18741. "Hook for functions attaching themselves to `S-down'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftdown-final-hook nil
  18744. "Hook for functions attaching themselves to `S-down'.
  18745. This one runs after all other options except shift-select have been excluded.
  18746. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18747. (defvar org-shiftleft-hook nil
  18748. "Hook for functions attaching themselves to `S-left'.
  18749. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18750. (defvar org-shiftleft-final-hook nil
  18751. "Hook for functions attaching themselves to `S-left'.
  18752. This one runs after all other options except shift-select have been excluded.
  18753. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18754. (defvar org-shiftright-hook nil
  18755. "Hook for functions attaching themselves to `S-right'.
  18756. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18757. (defvar org-shiftright-final-hook nil
  18758. "Hook for functions attaching themselves to `S-right'.
  18759. This one runs after all other options except shift-select have been excluded.
  18760. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18761. (defun org-modifier-cursor-error ()
  18762. "Throw an error, a modified cursor command was applied in wrong context."
  18763. (user-error "This command is active in special context like tables, headlines or items"))
  18764. (defun org-shiftselect-error ()
  18765. "Throw an error because Shift-Cursor command was applied in wrong context."
  18766. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18767. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18768. (user-error "This command works only in special context like headlines or timestamps")))
  18769. (defun org-call-for-shift-select (cmd)
  18770. (let ((this-command-keys-shift-translated t))
  18771. (call-interactively cmd)))
  18772. (defun org-shifttab (&optional arg)
  18773. "Global visibility cycling or move to previous table field.
  18774. Call `org-table-previous-field' within a table.
  18775. When ARG is nil, cycle globally through visibility states.
  18776. When ARG is a numeric prefix, show contents of this level."
  18777. (interactive "P")
  18778. (cond
  18779. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18780. ((integerp arg)
  18781. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18782. (message "Content view to level: %d" arg)
  18783. (org-content (prefix-numeric-value arg2))
  18784. (org-cycle-show-empty-lines t)
  18785. (setq org-cycle-global-status 'overview)))
  18786. (t (call-interactively 'org-global-cycle))))
  18787. (defun org-shiftmetaleft ()
  18788. "Promote subtree or delete table column.
  18789. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18790. `org-table-delete-column', depending on context. See the
  18791. individual commands for more information."
  18792. (interactive)
  18793. (cond
  18794. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18795. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18796. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18797. ((if (not (org-region-active-p)) (org-at-item-p)
  18798. (save-excursion (goto-char (region-beginning))
  18799. (org-at-item-p)))
  18800. (call-interactively 'org-outdent-item-tree))
  18801. (t (org-modifier-cursor-error))))
  18802. (defun org-shiftmetaright ()
  18803. "Demote subtree or insert table column.
  18804. Calls `org-demote-subtree', `org-indent-item-tree', or
  18805. `org-table-insert-column', depending on context. See the
  18806. individual commands for more information."
  18807. (interactive)
  18808. (cond
  18809. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18810. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18811. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18812. ((if (not (org-region-active-p)) (org-at-item-p)
  18813. (save-excursion (goto-char (region-beginning))
  18814. (org-at-item-p)))
  18815. (call-interactively 'org-indent-item-tree))
  18816. (t (org-modifier-cursor-error))))
  18817. (defun org-shiftmetaup (&optional _arg)
  18818. "Drag the line at point up.
  18819. In a table, kill the current row.
  18820. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18821. but also adjust the previous clocked item in the clock history.
  18822. Everywhere else, drag the line at point up."
  18823. (interactive "P")
  18824. (cond
  18825. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18826. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18827. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18828. (call-interactively 'org-timestamp-up)))
  18829. (t (call-interactively 'org-drag-line-backward))))
  18830. (defun org-shiftmetadown (&optional _arg)
  18831. "Drag the line at point down.
  18832. In a table, insert an empty row at the current line.
  18833. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18834. but also adjust the previous clocked item in the clock history.
  18835. Everywhere else, drag the line at point down."
  18836. (interactive "P")
  18837. (cond
  18838. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18839. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18840. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18841. (call-interactively 'org-timestamp-down)))
  18842. (t (call-interactively 'org-drag-line-forward))))
  18843. (defsubst org-hidden-tree-error ()
  18844. (user-error
  18845. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18846. (defun org-metaleft (&optional _arg)
  18847. "Promote heading, list item at point or move table column left.
  18848. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18849. depending on context. With no specific context, calls the Emacs
  18850. default `backward-word'. See the individual commands for more
  18851. information.
  18852. This function runs the hook `org-metaleft-hook' as a first step,
  18853. and returns at first non-nil value."
  18854. (interactive "P")
  18855. (cond
  18856. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18857. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18858. ((org-with-limited-levels
  18859. (or (org-at-heading-p)
  18860. (and (org-region-active-p)
  18861. (save-excursion
  18862. (goto-char (region-beginning))
  18863. (org-at-heading-p)))))
  18864. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18865. (call-interactively 'org-do-promote))
  18866. ;; At an inline task.
  18867. ((org-at-heading-p)
  18868. (call-interactively 'org-inlinetask-promote))
  18869. ((or (org-at-item-p)
  18870. (and (org-region-active-p)
  18871. (save-excursion
  18872. (goto-char (region-beginning))
  18873. (org-at-item-p))))
  18874. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18875. (call-interactively 'org-outdent-item))
  18876. (t (call-interactively 'backward-word))))
  18877. (defun org-metaright (&optional _arg)
  18878. "Demote heading, list item at point or move table column right.
  18879. In front of a drawer or a block keyword, indent it correctly.
  18880. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18881. `org-indnet-drawer' or `org-indent-block' depending on context.
  18882. With no specific context, calls the Emacs default `forward-word'.
  18883. See the individual commands for more information.
  18884. This function runs the hook `org-metaright-hook' as a first step,
  18885. and returns at first non-nil value."
  18886. (interactive "P")
  18887. (cond
  18888. ((run-hook-with-args-until-success 'org-metaright-hook))
  18889. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18890. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18891. ((org-at-block-p) (call-interactively 'org-indent-block))
  18892. ((org-with-limited-levels
  18893. (or (org-at-heading-p)
  18894. (and (org-region-active-p)
  18895. (save-excursion
  18896. (goto-char (region-beginning))
  18897. (org-at-heading-p)))))
  18898. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18899. (call-interactively 'org-do-demote))
  18900. ;; At an inline task.
  18901. ((org-at-heading-p)
  18902. (call-interactively 'org-inlinetask-demote))
  18903. ((or (org-at-item-p)
  18904. (and (org-region-active-p)
  18905. (save-excursion
  18906. (goto-char (region-beginning))
  18907. (org-at-item-p))))
  18908. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18909. (call-interactively 'org-indent-item))
  18910. (t (call-interactively 'forward-word))))
  18911. (defun org-check-for-hidden (what)
  18912. "Check if there are hidden headlines/items in the current visual line.
  18913. WHAT can be either `headlines' or `items'. If the current line is
  18914. an outline or item heading and it has a folded subtree below it,
  18915. this function returns t, nil otherwise."
  18916. (let ((re (cond
  18917. ((eq what 'headlines) org-outline-regexp-bol)
  18918. ((eq what 'items) (org-item-beginning-re))
  18919. (t (error "This should not happen"))))
  18920. beg end)
  18921. (save-excursion
  18922. (catch 'exit
  18923. (unless (org-region-active-p)
  18924. (setq beg (point-at-bol))
  18925. (beginning-of-line 2)
  18926. (while (and (not (eobp)) ;; this is like `next-line'
  18927. (get-char-property (1- (point)) 'invisible))
  18928. (beginning-of-line 2))
  18929. (setq end (point))
  18930. (goto-char beg)
  18931. (goto-char (point-at-eol))
  18932. (setq end (max end (point)))
  18933. (while (re-search-forward re end t)
  18934. (when (get-char-property (match-beginning 0) 'invisible)
  18935. (throw 'exit t))))
  18936. nil))))
  18937. (defun org-metaup (&optional _arg)
  18938. "Move subtree up or move table row up.
  18939. Calls `org-move-subtree-up' or `org-table-move-row' or
  18940. `org-move-item-up', depending on context. See the individual commands
  18941. for more information."
  18942. (interactive "P")
  18943. (cond
  18944. ((run-hook-with-args-until-success 'org-metaup-hook))
  18945. ((org-region-active-p)
  18946. (let* ((a (min (region-beginning) (region-end)))
  18947. (b (1- (max (region-beginning) (region-end))))
  18948. (c (save-excursion (goto-char a)
  18949. (move-beginning-of-line 0)))
  18950. (d (save-excursion (goto-char a)
  18951. (move-end-of-line 0) (point))))
  18952. (transpose-regions a b c d)
  18953. (goto-char c)))
  18954. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18955. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18956. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18957. (t (org-drag-element-backward))))
  18958. (defun org-metadown (&optional _arg)
  18959. "Move subtree down or move table row down.
  18960. Calls `org-move-subtree-down' or `org-table-move-row' or
  18961. `org-move-item-down', depending on context. See the individual
  18962. commands for more information."
  18963. (interactive "P")
  18964. (cond
  18965. ((run-hook-with-args-until-success 'org-metadown-hook))
  18966. ((org-region-active-p)
  18967. (let* ((a (min (region-beginning) (region-end)))
  18968. (b (max (region-beginning) (region-end)))
  18969. (c (save-excursion (goto-char b)
  18970. (move-beginning-of-line 1)))
  18971. (d (save-excursion (goto-char b)
  18972. (move-end-of-line 1) (1+ (point)))))
  18973. (transpose-regions a b c d)
  18974. (goto-char d)))
  18975. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18976. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18977. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18978. (t (org-drag-element-forward))))
  18979. (defun org-shiftup (&optional arg)
  18980. "Increase item in timestamp or increase priority of current headline.
  18981. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18982. depending on context. See the individual commands for more information."
  18983. (interactive "P")
  18984. (cond
  18985. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18986. ((and org-support-shift-select (org-region-active-p))
  18987. (org-call-for-shift-select 'previous-line))
  18988. ((org-at-timestamp-p t)
  18989. (call-interactively (if org-edit-timestamp-down-means-later
  18990. 'org-timestamp-down 'org-timestamp-up)))
  18991. ((and (not (eq org-support-shift-select 'always))
  18992. org-enable-priority-commands
  18993. (org-at-heading-p))
  18994. (call-interactively 'org-priority-up))
  18995. ((and (not org-support-shift-select) (org-at-item-p))
  18996. (call-interactively 'org-previous-item))
  18997. ((org-clocktable-try-shift 'up arg))
  18998. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18999. (org-support-shift-select
  19000. (org-call-for-shift-select 'previous-line))
  19001. (t (org-shiftselect-error))))
  19002. (defun org-shiftdown (&optional arg)
  19003. "Decrease item in timestamp or decrease priority of current headline.
  19004. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19005. depending on context. See the individual commands for more information."
  19006. (interactive "P")
  19007. (cond
  19008. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19009. ((and org-support-shift-select (org-region-active-p))
  19010. (org-call-for-shift-select 'next-line))
  19011. ((org-at-timestamp-p t)
  19012. (call-interactively (if org-edit-timestamp-down-means-later
  19013. 'org-timestamp-up 'org-timestamp-down)))
  19014. ((and (not (eq org-support-shift-select 'always))
  19015. org-enable-priority-commands
  19016. (org-at-heading-p))
  19017. (call-interactively 'org-priority-down))
  19018. ((and (not org-support-shift-select) (org-at-item-p))
  19019. (call-interactively 'org-next-item))
  19020. ((org-clocktable-try-shift 'down arg))
  19021. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19022. (org-support-shift-select
  19023. (org-call-for-shift-select 'next-line))
  19024. (t (org-shiftselect-error))))
  19025. (defun org-shiftright (&optional arg)
  19026. "Cycle the thing at point or in the current line, depending on context.
  19027. Depending on context, this does one of the following:
  19028. - switch a timestamp at point one day into the future
  19029. - on a headline, switch to the next TODO keyword.
  19030. - on an item, switch entire list to the next bullet type
  19031. - on a property line, switch to the next allowed value
  19032. - on a clocktable definition line, move time block into the future"
  19033. (interactive "P")
  19034. (cond
  19035. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19036. ((and org-support-shift-select (org-region-active-p))
  19037. (org-call-for-shift-select 'forward-char))
  19038. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19039. ((and (not (eq org-support-shift-select 'always))
  19040. (org-at-heading-p))
  19041. (let ((org-inhibit-logging
  19042. (not org-treat-S-cursor-todo-selection-as-state-change))
  19043. (org-inhibit-blocking
  19044. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19045. (org-call-with-arg 'org-todo 'right)))
  19046. ((or (and org-support-shift-select
  19047. (not (eq org-support-shift-select 'always))
  19048. (org-at-item-bullet-p))
  19049. (and (not org-support-shift-select) (org-at-item-p)))
  19050. (org-call-with-arg 'org-cycle-list-bullet nil))
  19051. ((and (not (eq org-support-shift-select 'always))
  19052. (org-at-property-p))
  19053. (call-interactively 'org-property-next-allowed-value))
  19054. ((org-clocktable-try-shift 'right arg))
  19055. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19056. (org-support-shift-select
  19057. (org-call-for-shift-select 'forward-char))
  19058. (t (org-shiftselect-error))))
  19059. (defun org-shiftleft (&optional arg)
  19060. "Cycle the thing at point or in the current line, depending on context.
  19061. Depending on context, this does one of the following:
  19062. - switch a timestamp at point one day into the past
  19063. - on a headline, switch to the previous TODO keyword.
  19064. - on an item, switch entire list to the previous bullet type
  19065. - on a property line, switch to the previous allowed value
  19066. - on a clocktable definition line, move time block into the past"
  19067. (interactive "P")
  19068. (cond
  19069. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19070. ((and org-support-shift-select (org-region-active-p))
  19071. (org-call-for-shift-select 'backward-char))
  19072. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19073. ((and (not (eq org-support-shift-select 'always))
  19074. (org-at-heading-p))
  19075. (let ((org-inhibit-logging
  19076. (not org-treat-S-cursor-todo-selection-as-state-change))
  19077. (org-inhibit-blocking
  19078. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19079. (org-call-with-arg 'org-todo 'left)))
  19080. ((or (and org-support-shift-select
  19081. (not (eq org-support-shift-select 'always))
  19082. (org-at-item-bullet-p))
  19083. (and (not org-support-shift-select) (org-at-item-p)))
  19084. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19085. ((and (not (eq org-support-shift-select 'always))
  19086. (org-at-property-p))
  19087. (call-interactively 'org-property-previous-allowed-value))
  19088. ((org-clocktable-try-shift 'left arg))
  19089. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19090. (org-support-shift-select
  19091. (org-call-for-shift-select 'backward-char))
  19092. (t (org-shiftselect-error))))
  19093. (defun org-shiftcontrolright ()
  19094. "Switch to next TODO set."
  19095. (interactive)
  19096. (cond
  19097. ((and org-support-shift-select (org-region-active-p))
  19098. (org-call-for-shift-select 'forward-word))
  19099. ((and (not (eq org-support-shift-select 'always))
  19100. (org-at-heading-p))
  19101. (org-call-with-arg 'org-todo 'nextset))
  19102. (org-support-shift-select
  19103. (org-call-for-shift-select 'forward-word))
  19104. (t (org-shiftselect-error))))
  19105. (defun org-shiftcontrolleft ()
  19106. "Switch to previous TODO set."
  19107. (interactive)
  19108. (cond
  19109. ((and org-support-shift-select (org-region-active-p))
  19110. (org-call-for-shift-select 'backward-word))
  19111. ((and (not (eq org-support-shift-select 'always))
  19112. (org-at-heading-p))
  19113. (org-call-with-arg 'org-todo 'previousset))
  19114. (org-support-shift-select
  19115. (org-call-for-shift-select 'backward-word))
  19116. (t (org-shiftselect-error))))
  19117. (defun org-shiftcontrolup (&optional n)
  19118. "Change timestamps synchronously up in CLOCK log lines.
  19119. Optional argument N tells to change by that many units."
  19120. (interactive "P")
  19121. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19122. (let (org-support-shift-select)
  19123. (org-clock-timestamps-up n))
  19124. (user-error "Not at a clock log")))
  19125. (defun org-shiftcontroldown (&optional n)
  19126. "Change timestamps synchronously down in CLOCK log lines.
  19127. Optional argument N tells to change by that many units."
  19128. (interactive "P")
  19129. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19130. (let (org-support-shift-select)
  19131. (org-clock-timestamps-down n))
  19132. (user-error "Not at a clock log")))
  19133. (defun org-increase-number-at-point (&optional inc)
  19134. "Increment the number at point.
  19135. With an optional prefix numeric argument INC, increment using
  19136. this numeric value."
  19137. (interactive "p")
  19138. (if (not (number-at-point))
  19139. (user-error "Not on a number")
  19140. (unless inc (setq inc 1))
  19141. (let ((pos (point))
  19142. (beg (skip-chars-backward "-+^/*0-9eE."))
  19143. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19144. (setq nap (buffer-substring-no-properties
  19145. (+ pos beg) (+ pos beg end)))
  19146. (delete-region (+ pos beg) (+ pos beg end))
  19147. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19148. (when (org-at-table-p)
  19149. (org-table-align)
  19150. (org-table-end-of-field 1))))
  19151. (defun org-decrease-number-at-point (&optional inc)
  19152. "Decrement the number at point.
  19153. With an optional prefix numeric argument INC, decrement using
  19154. this numeric value."
  19155. (interactive "p")
  19156. (org-increase-number-at-point (- (or inc 1))))
  19157. (defun org-ctrl-c-ret ()
  19158. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19159. (interactive)
  19160. (cond
  19161. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19162. (t (call-interactively 'org-insert-heading))))
  19163. (defun org-find-visible ()
  19164. (let ((s (point)))
  19165. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19166. (get-char-property s 'invisible)))
  19167. s))
  19168. (defun org-find-invisible ()
  19169. (let ((s (point)))
  19170. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19171. (not (get-char-property s 'invisible))))
  19172. s))
  19173. (defun org-copy-visible (beg end)
  19174. "Copy the visible parts of the region."
  19175. (interactive "r")
  19176. (let (snippets s)
  19177. (save-excursion
  19178. (save-restriction
  19179. (narrow-to-region beg end)
  19180. (setq s (goto-char (point-min)))
  19181. (while (not (= (point) (point-max)))
  19182. (goto-char (org-find-invisible))
  19183. (push (buffer-substring s (point)) snippets)
  19184. (setq s (goto-char (org-find-visible))))))
  19185. (kill-new (apply 'concat (nreverse snippets)))))
  19186. (defun org-copy-special ()
  19187. "Copy region in table or copy current subtree.
  19188. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19189. See the individual commands for more information."
  19190. (interactive)
  19191. (call-interactively
  19192. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19193. (defun org-cut-special ()
  19194. "Cut region in table or cut current subtree.
  19195. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19196. See the individual commands for more information."
  19197. (interactive)
  19198. (call-interactively
  19199. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19200. (defun org-paste-special (arg)
  19201. "Paste rectangular region into table, or past subtree relative to level.
  19202. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19203. See the individual commands for more information."
  19204. (interactive "P")
  19205. (if (org-at-table-p)
  19206. (org-table-paste-rectangle)
  19207. (org-paste-subtree arg)))
  19208. (defsubst org-in-fixed-width-region-p ()
  19209. "Is point in a fixed-width region?"
  19210. (save-match-data
  19211. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19212. (defun org-edit-special (&optional arg)
  19213. "Call a special editor for the element at point.
  19214. When at a table, call the formula editor with `org-table-edit-formulas'.
  19215. When in a source code block, call `org-edit-src-code'.
  19216. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19217. When in an export block, call `org-edit-export-block'.
  19218. When at an #+INCLUDE keyword, visit the included file.
  19219. When at a footnote reference, call `org-edit-footnote-reference'
  19220. On a link, call `ffap' to visit the link at point.
  19221. Otherwise, return a user error."
  19222. (interactive "P")
  19223. (let ((element (org-element-at-point)))
  19224. (cl-assert (not buffer-read-only) nil
  19225. "Buffer is read-only: %s" (buffer-name))
  19226. (pcase (org-element-type element)
  19227. (`src-block
  19228. (if (not arg) (org-edit-src-code)
  19229. (let* ((info (org-babel-get-src-block-info))
  19230. (lang (nth 0 info))
  19231. (params (nth 2 info))
  19232. (session (cdr (assq :session params))))
  19233. (if (not session) (org-edit-src-code)
  19234. ;; At a src-block with a session and function called with
  19235. ;; an ARG: switch to the buffer related to the inferior
  19236. ;; process.
  19237. (switch-to-buffer
  19238. (funcall (intern (concat "org-babel-prep-session:" lang))
  19239. session params))))))
  19240. (`keyword
  19241. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19242. (org-open-link-from-string
  19243. (format "[[%s]]"
  19244. (expand-file-name
  19245. (let ((value (org-element-property :value element)))
  19246. (cond ((not (org-string-nw-p value))
  19247. (user-error "No file to edit"))
  19248. ((string-match "\\`\"\\(.*?\\)\"" value)
  19249. (match-string 1 value))
  19250. ((string-match "\\`[^ \t\"]\\S-*" value)
  19251. (match-string 0 value))
  19252. (t (user-error "No valid file specified")))))))
  19253. (user-error "No special environment to edit here")))
  19254. (`table
  19255. (if (eq (org-element-property :type element) 'table.el)
  19256. (org-edit-table.el)
  19257. (call-interactively 'org-table-edit-formulas)))
  19258. ;; Only Org tables contain `table-row' type elements.
  19259. (`table-row (call-interactively 'org-table-edit-formulas))
  19260. (`example-block (org-edit-src-code))
  19261. (`export-block (org-edit-export-block))
  19262. (`fixed-width (org-edit-fixed-width-region))
  19263. (_
  19264. ;; No notable element at point. Though, we may be at a link or
  19265. ;; a footnote reference, which are objects. Thus, scan deeper.
  19266. (let ((context (org-element-context element)))
  19267. (pcase (org-element-type context)
  19268. (`footnote-reference (org-edit-footnote-reference))
  19269. (`inline-src-block (org-edit-inline-src-code))
  19270. (`link (call-interactively #'ffap))
  19271. (_ (user-error "No special environment to edit here"))))))))
  19272. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19273. (defun org-ctrl-c-ctrl-c (&optional arg)
  19274. "Set tags in headline, or update according to changed information at point.
  19275. This command does many different things, depending on context:
  19276. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19277. this is what we do.
  19278. - If the cursor is on a statistics cookie, update it.
  19279. - If the cursor is in a headline, prompt for tags and insert them
  19280. into the current line, aligned to `org-tags-column'. When called
  19281. with prefix arg, realign all tags in the current buffer.
  19282. - If the cursor is in one of the special #+KEYWORD lines, this
  19283. triggers scanning the buffer for these lines and updating the
  19284. information.
  19285. - If the cursor is inside a table, realign the table. This command
  19286. works even if the automatic table editor has been turned off.
  19287. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19288. the entire table.
  19289. - If the cursor is at a footnote reference or definition, jump to
  19290. the corresponding definition or references, respectively.
  19291. - If the cursor is a the beginning of a dynamic block, update it.
  19292. - If the current buffer is a capture buffer, close note and file it.
  19293. - If the cursor is on a <<<target>>>, update radio targets and
  19294. corresponding links in this buffer.
  19295. - If the cursor is on a numbered item in a plain list, renumber the
  19296. ordered list.
  19297. - If the cursor is on a checkbox, toggle it.
  19298. - If the cursor is on a code block, evaluate it. The variable
  19299. `org-confirm-babel-evaluate' can be used to control prompting
  19300. before code block evaluation, by default every code block
  19301. evaluation requires confirmation. Code block evaluation can be
  19302. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19303. (interactive "P")
  19304. (cond
  19305. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19306. org-occur-highlights)
  19307. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19308. (org-remove-occur-highlights)
  19309. (message "Temporary highlights/overlays removed from current buffer"))
  19310. ((and (local-variable-p 'org-finish-function (current-buffer))
  19311. (fboundp org-finish-function))
  19312. (funcall org-finish-function))
  19313. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19314. (t
  19315. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19316. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19317. (user-error "C-c C-c can do nothing useful at this location"))
  19318. (let* ((context (org-element-context))
  19319. (type (org-element-type context)))
  19320. (cl-case type
  19321. ;; When at a link, act according to the parent instead.
  19322. (link (setq context (org-element-property :parent context))
  19323. (setq type (org-element-type context)))
  19324. ;; Unsupported object types: refer to the first supported
  19325. ;; element or object containing it.
  19326. ((bold code entity export-snippet inline-babel-call inline-src-block
  19327. italic latex-fragment line-break macro strike-through subscript
  19328. superscript underline verbatim)
  19329. (setq context
  19330. (org-element-lineage
  19331. context '(radio-target paragraph verse-block table-cell)))))
  19332. ;; For convenience: at the first line of a paragraph on the
  19333. ;; same line as an item, apply function on that item instead.
  19334. (when (eq type 'paragraph)
  19335. (let ((parent (org-element-property :parent context)))
  19336. (when (and (eq (org-element-type parent) 'item)
  19337. (= (line-beginning-position)
  19338. (org-element-property :begin parent)))
  19339. (setq context parent type 'item))))
  19340. ;; Act according to type of element or object at point.
  19341. (cl-case type
  19342. (clock (org-clock-update-time-maybe))
  19343. (dynamic-block
  19344. (save-excursion
  19345. (goto-char (org-element-property :post-affiliated context))
  19346. (org-update-dblock)))
  19347. (footnote-definition
  19348. (goto-char (org-element-property :post-affiliated context))
  19349. (call-interactively 'org-footnote-action))
  19350. (footnote-reference (call-interactively 'org-footnote-action))
  19351. ((headline inlinetask)
  19352. (save-excursion (goto-char (org-element-property :begin context))
  19353. (call-interactively 'org-set-tags)))
  19354. (item
  19355. ;; At an item: a double C-u set checkbox to "[-]"
  19356. ;; unconditionally, whereas a single one will toggle its
  19357. ;; presence. Without a universal argument, if the item
  19358. ;; has a checkbox, toggle it. Otherwise repair the list.
  19359. (let* ((box (org-element-property :checkbox context))
  19360. (struct (org-element-property :structure context))
  19361. (old-struct (copy-tree struct))
  19362. (parents (org-list-parents-alist struct))
  19363. (prevs (org-list-prevs-alist struct))
  19364. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19365. (org-list-set-checkbox
  19366. (org-element-property :begin context) struct
  19367. (cond ((equal arg '(16)) "[-]")
  19368. ((and (not box) (equal arg '(4))) "[ ]")
  19369. ((or (not box) (equal arg '(4))) nil)
  19370. ((eq box 'on) "[ ]")
  19371. (t "[X]")))
  19372. ;; Mimic `org-list-write-struct' but with grabbing
  19373. ;; a return value from `org-list-struct-fix-box'.
  19374. (org-list-struct-fix-ind struct parents 2)
  19375. (org-list-struct-fix-item-end struct)
  19376. (org-list-struct-fix-bul struct prevs)
  19377. (org-list-struct-fix-ind struct parents)
  19378. (let ((block-item
  19379. (org-list-struct-fix-box struct parents prevs orderedp)))
  19380. (if (and box (equal struct old-struct))
  19381. (if (equal arg '(16))
  19382. (message "Checkboxes already reset")
  19383. (user-error "Cannot toggle this checkbox: %s"
  19384. (if (eq box 'on)
  19385. "all subitems checked"
  19386. "unchecked subitems")))
  19387. (org-list-struct-apply-struct struct old-struct)
  19388. (org-update-checkbox-count-maybe))
  19389. (when block-item
  19390. (message "Checkboxes were removed due to empty box at line %d"
  19391. (org-current-line block-item))))))
  19392. (keyword
  19393. (let ((org-inhibit-startup-visibility-stuff t)
  19394. (org-startup-align-all-tables nil))
  19395. (when (boundp 'org-table-coordinate-overlays)
  19396. (mapc #'delete-overlay org-table-coordinate-overlays)
  19397. (setq org-table-coordinate-overlays nil))
  19398. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19399. (message "Local setup has been refreshed"))
  19400. (plain-list
  19401. ;; At a plain list, with a double C-u argument, set
  19402. ;; checkboxes of each item to "[-]", whereas a single one
  19403. ;; will toggle their presence according to the state of the
  19404. ;; first item in the list. Without an argument, repair the
  19405. ;; list.
  19406. (let* ((begin (org-element-property :contents-begin context))
  19407. (beginm (move-marker (make-marker) begin))
  19408. (struct (org-element-property :structure context))
  19409. (old-struct (copy-tree struct))
  19410. (first-box (save-excursion
  19411. (goto-char begin)
  19412. (looking-at org-list-full-item-re)
  19413. (match-string-no-properties 3)))
  19414. (new-box (cond ((equal arg '(16)) "[-]")
  19415. ((equal arg '(4)) (unless first-box "[ ]"))
  19416. ((equal first-box "[X]") "[ ]")
  19417. (t "[X]"))))
  19418. (cond
  19419. (arg
  19420. (dolist (pos
  19421. (org-list-get-all-items
  19422. begin struct (org-list-prevs-alist struct)))
  19423. (org-list-set-checkbox pos struct new-box)))
  19424. ((and first-box (eq (point) begin))
  19425. ;; For convenience, when point is at bol on the first
  19426. ;; item of the list and no argument is provided, simply
  19427. ;; toggle checkbox of that item, if any.
  19428. (org-list-set-checkbox begin struct new-box)))
  19429. (org-list-write-struct
  19430. struct (org-list-parents-alist struct) old-struct)
  19431. (org-update-checkbox-count-maybe)
  19432. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19433. ((property-drawer node-property)
  19434. (call-interactively 'org-property-action))
  19435. ((radio-target target)
  19436. (call-interactively 'org-update-radio-target-regexp))
  19437. (statistics-cookie
  19438. (call-interactively 'org-update-statistics-cookies))
  19439. ((table table-cell table-row)
  19440. ;; At a table, recalculate every field and align it. Also
  19441. ;; send the table if necessary. If the table has
  19442. ;; a `table.el' type, just give up. At a table row or
  19443. ;; cell, maybe recalculate line but always align table.
  19444. (if (eq (org-element-property :type context) 'table.el)
  19445. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19446. Use \\[org-edit-special] to edit table.el tables"))
  19447. (let ((org-enable-table-editor t))
  19448. (if (or (eq type 'table)
  19449. ;; Check if point is at a TBLFM line.
  19450. (and (eq type 'table-row)
  19451. (= (point) (org-element-property :end context))))
  19452. (save-excursion
  19453. (if (org-at-TBLFM-p)
  19454. (progn (require 'org-table)
  19455. (org-table-calc-current-TBLFM))
  19456. (goto-char (org-element-property :contents-begin context))
  19457. (org-call-with-arg 'org-table-recalculate (or arg t))
  19458. (orgtbl-send-table 'maybe)))
  19459. (org-table-maybe-eval-formula)
  19460. (cond (arg (call-interactively 'org-table-recalculate))
  19461. ((org-table-maybe-recalculate-line))
  19462. (t (org-table-align)))))))
  19463. (timestamp (org-timestamp-change 0 'day))
  19464. (otherwise
  19465. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19466. (user-error
  19467. "C-c C-c can do nothing useful at this location")))))))))
  19468. (defun org-mode-restart ()
  19469. (interactive)
  19470. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19471. (funcall major-mode)
  19472. (hack-local-variables)
  19473. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19474. (org-indent-mode -1)))
  19475. (message "%s restarted" major-mode))
  19476. (defun org-kill-note-or-show-branches ()
  19477. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19478. (interactive)
  19479. (if (not org-finish-function)
  19480. (progn
  19481. (outline-hide-subtree)
  19482. (call-interactively 'outline-show-branches))
  19483. (let ((org-note-abort t))
  19484. (funcall org-finish-function))))
  19485. (defun org-delete-indentation (&optional ARG)
  19486. "Join current line to previous and fix whitespace at join.
  19487. If previous line is a headline add to headline title. Otherwise
  19488. the function calls `delete-indentation'.
  19489. With argument, join this line to following line."
  19490. (interactive "*P")
  19491. (if (save-excursion
  19492. (if ARG (beginning-of-line)
  19493. (forward-line -1))
  19494. (looking-at org-complex-heading-regexp))
  19495. ;; At headline.
  19496. (let ((tags-column (when (match-beginning 5)
  19497. (save-excursion (goto-char (match-beginning 5))
  19498. (current-column))))
  19499. (string (concat " " (progn (when ARG (forward-line 1))
  19500. (org-trim (delete-and-extract-region
  19501. (line-beginning-position)
  19502. (line-end-position)))))))
  19503. (unless (bobp) (delete-region (point) (1- (point))))
  19504. (goto-char (or (match-end 4)
  19505. (match-beginning 5)
  19506. (match-end 0)))
  19507. (skip-chars-backward " \t")
  19508. (save-excursion (insert string))
  19509. ;; Adjust alignment of tags.
  19510. (when tags-column
  19511. (org-align-tags-here (if org-auto-align-tags
  19512. org-tags-column
  19513. tags-column))))
  19514. (delete-indentation ARG)))
  19515. (defun org-open-line (n)
  19516. "Insert a new row in tables, call `open-line' elsewhere.
  19517. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19518. (interactive "*p")
  19519. (cond
  19520. ((not org-special-ctrl-o)
  19521. (open-line n))
  19522. ((org-at-table-p)
  19523. (org-table-insert-row))
  19524. (t
  19525. (open-line n))))
  19526. (defun org-return (&optional indent)
  19527. "Goto next table row or insert a newline.
  19528. Calls `org-table-next-row' or `newline', depending on context.
  19529. When optional INDENT argument is non-nil, call
  19530. `newline-and-indent' instead of `newline'.
  19531. When `org-return-follows-link' is non-nil and point is on
  19532. a timestamp or a link, call `org-open-at-point'. However, it
  19533. will not happen if point is in a table or on a \"dead\"
  19534. object (e.g., within a comment). In these case, you need to use
  19535. `org-open-at-point' directly."
  19536. (interactive)
  19537. (let ((context (if org-return-follows-link (org-element-context)
  19538. (org-element-at-point))))
  19539. (cond
  19540. ;; In a table, call `org-table-next-row'.
  19541. ((or (and (eq (org-element-type context) 'table)
  19542. (>= (point) (org-element-property :contents-begin context))
  19543. (< (point) (org-element-property :contents-end context)))
  19544. (org-element-lineage context '(table-row table-cell) t))
  19545. (org-table-justify-field-maybe)
  19546. (call-interactively #'org-table-next-row))
  19547. ;; On a link or a timestamp, call `org-open-at-point' if
  19548. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19549. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19550. ((and org-return-follows-link
  19551. (or (org-in-regexp org-ts-regexp-both nil t)
  19552. (org-in-regexp org-tsr-regexp-both nil t)
  19553. (org-in-regexp org-any-link-re nil t)))
  19554. (call-interactively #'org-open-at-point))
  19555. ;; Insert newline in heading, but preserve tags.
  19556. ((and (not (bolp))
  19557. (save-excursion (beginning-of-line)
  19558. (looking-at org-complex-heading-regexp)))
  19559. ;; At headline. Split line. However, if point is on keyword,
  19560. ;; priority cookie or tags, do not break any of them: add
  19561. ;; a newline after the headline instead.
  19562. (let ((tags-column (and (match-beginning 5)
  19563. (save-excursion (goto-char (match-beginning 5))
  19564. (current-column))))
  19565. (string
  19566. (when (and (match-end 4)
  19567. (>= (point)
  19568. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19569. (<= (point) (match-end 4)))
  19570. (delete-and-extract-region (point) (match-end 4)))))
  19571. (when (and tags-column string) ; Adjust tag alignment.
  19572. (org-align-tags-here
  19573. (if org-auto-align-tags org-tags-column tags-column)))
  19574. (end-of-line)
  19575. (org-show-entry)
  19576. (if indent (newline-and-indent) (newline))
  19577. (when string (save-excursion (insert (org-trim string))))))
  19578. ;; In a list, make sure indenting keeps trailing text within.
  19579. ((and indent
  19580. (not (eolp))
  19581. (org-element-lineage context '(item)))
  19582. (let ((trailing-data
  19583. (delete-and-extract-region (point) (line-end-position))))
  19584. (newline-and-indent)
  19585. (save-excursion (insert trailing-data))))
  19586. (t (if indent (newline-and-indent) (newline))))))
  19587. (defun org-return-indent ()
  19588. "Goto next table row or insert a newline and indent.
  19589. Calls `org-table-next-row' or `newline-and-indent', depending on
  19590. context. See the individual commands for more information."
  19591. (interactive)
  19592. (org-return t))
  19593. (defun org-ctrl-c-star ()
  19594. "Compute table, or change heading status of lines.
  19595. Calls `org-table-recalculate' or `org-toggle-heading',
  19596. depending on context."
  19597. (interactive)
  19598. (cond
  19599. ((org-at-table-p)
  19600. (call-interactively 'org-table-recalculate))
  19601. (t
  19602. ;; Convert all lines in region to list items
  19603. (call-interactively 'org-toggle-heading))))
  19604. (defun org-ctrl-c-minus ()
  19605. "Insert separator line in table or modify bullet status of line.
  19606. Also turns a plain line or a region of lines into list items.
  19607. Calls `org-table-insert-hline', `org-toggle-item', or
  19608. `org-cycle-list-bullet', depending on context."
  19609. (interactive)
  19610. (cond
  19611. ((org-at-table-p)
  19612. (call-interactively 'org-table-insert-hline))
  19613. ((org-region-active-p)
  19614. (call-interactively 'org-toggle-item))
  19615. ((org-in-item-p)
  19616. (call-interactively 'org-cycle-list-bullet))
  19617. (t
  19618. (call-interactively 'org-toggle-item))))
  19619. (defun org-toggle-item (arg)
  19620. "Convert headings or normal lines to items, items to normal lines.
  19621. If there is no active region, only the current line is considered.
  19622. If the first non blank line in the region is a headline, convert
  19623. all headlines to items, shifting text accordingly.
  19624. If it is an item, convert all items to normal lines.
  19625. If it is normal text, change region into a list of items.
  19626. With a prefix argument ARG, change the region in a single item."
  19627. (interactive "P")
  19628. (let ((shift-text
  19629. (function
  19630. ;; Shift text in current section to IND, from point to END.
  19631. ;; The function leaves point to END line.
  19632. (lambda (ind end)
  19633. (let ((min-i 1000) (end (copy-marker end)))
  19634. ;; First determine the minimum indentation (MIN-I) of
  19635. ;; the text.
  19636. (save-excursion
  19637. (catch 'exit
  19638. (while (< (point) end)
  19639. (let ((i (org-get-indentation)))
  19640. (cond
  19641. ;; Skip blank lines and inline tasks.
  19642. ((looking-at "^[ \t]*$"))
  19643. ((looking-at org-outline-regexp-bol))
  19644. ;; We can't find less than 0 indentation.
  19645. ((zerop i) (throw 'exit (setq min-i 0)))
  19646. ((< i min-i) (setq min-i i))))
  19647. (forward-line))))
  19648. ;; Then indent each line so that a line indented to
  19649. ;; MIN-I becomes indented to IND. Ignore blank lines
  19650. ;; and inline tasks in the process.
  19651. (let ((delta (- ind min-i)))
  19652. (while (< (point) end)
  19653. (unless (or (looking-at "^[ \t]*$")
  19654. (looking-at org-outline-regexp-bol))
  19655. (indent-line-to (+ (org-get-indentation) delta)))
  19656. (forward-line)))))))
  19657. (skip-blanks
  19658. (function
  19659. ;; Return beginning of first non-blank line, starting from
  19660. ;; line at POS.
  19661. (lambda (pos)
  19662. (save-excursion
  19663. (goto-char pos)
  19664. (skip-chars-forward " \r\t\n")
  19665. (point-at-bol)))))
  19666. beg end)
  19667. ;; Determine boundaries of changes.
  19668. (if (org-region-active-p)
  19669. (setq beg (funcall skip-blanks (region-beginning))
  19670. end (copy-marker (region-end)))
  19671. (setq beg (funcall skip-blanks (point-at-bol))
  19672. end (copy-marker (point-at-eol))))
  19673. ;; Depending on the starting line, choose an action on the text
  19674. ;; between BEG and END.
  19675. (org-with-limited-levels
  19676. (save-excursion
  19677. (goto-char beg)
  19678. (cond
  19679. ;; Case 1. Start at an item: de-itemize. Note that it only
  19680. ;; happens when a region is active: `org-ctrl-c-minus'
  19681. ;; would call `org-cycle-list-bullet' otherwise.
  19682. ((org-at-item-p)
  19683. (while (< (point) end)
  19684. (when (org-at-item-p)
  19685. (skip-chars-forward " \t")
  19686. (delete-region (point) (match-end 0)))
  19687. (forward-line)))
  19688. ;; Case 2. Start at an heading: convert to items.
  19689. ((org-at-heading-p)
  19690. (let* ((bul (org-list-bullet-string "-"))
  19691. (bul-len (length bul))
  19692. (done (org-entry-is-done-p))
  19693. (todo (org-entry-is-todo-p))
  19694. ;; Indentation of the first heading. It should be
  19695. ;; relative to the indentation of its parent, if any.
  19696. (start-ind (save-excursion
  19697. (cond
  19698. ((not org-adapt-indentation) 0)
  19699. ((not (outline-previous-heading)) 0)
  19700. (t (length (match-string 0))))))
  19701. ;; Level of first heading. Further headings will be
  19702. ;; compared to it to determine hierarchy in the list.
  19703. (ref-level (org-reduced-level (org-outline-level))))
  19704. (when (or done todo) (org-todo ""))
  19705. (while (< (point) end)
  19706. (let* ((level (org-reduced-level (org-outline-level)))
  19707. (delta (max 0 (- level ref-level))))
  19708. ;; If current headline is less indented than the first
  19709. ;; one, set it as reference, in order to preserve
  19710. ;; subtrees.
  19711. (when (< level ref-level) (setq ref-level level))
  19712. (replace-match bul t t)
  19713. (indent-line-to (+ start-ind (* delta bul-len)))
  19714. (when (or done todo)
  19715. (let* ((struct (org-list-struct))
  19716. (old (copy-tree struct)))
  19717. (org-list-set-checkbox (line-beginning-position)
  19718. struct
  19719. (if done "[X]" "[ ]"))
  19720. (org-list-write-struct struct
  19721. (org-list-parents-alist struct)
  19722. old)))
  19723. ;; Ensure all text down to END (or SECTION-END) belongs
  19724. ;; to the newly created item.
  19725. (let ((section-end (save-excursion
  19726. (or (outline-next-heading) (point)))))
  19727. (forward-line)
  19728. (funcall shift-text
  19729. (+ start-ind (* (1+ delta) bul-len))
  19730. (min end section-end)))))))
  19731. ;; Case 3. Normal line with ARG: make the first line of region
  19732. ;; an item, and shift indentation of others lines to
  19733. ;; set them as item's body.
  19734. (arg (let* ((bul (org-list-bullet-string "-"))
  19735. (bul-len (length bul))
  19736. (ref-ind (org-get-indentation)))
  19737. (skip-chars-forward " \t")
  19738. (insert bul)
  19739. (forward-line)
  19740. (while (< (point) end)
  19741. ;; Ensure that lines less indented than first one
  19742. ;; still get included in item body.
  19743. (funcall shift-text
  19744. (+ ref-ind bul-len)
  19745. (min end (save-excursion (or (outline-next-heading)
  19746. (point)))))
  19747. (forward-line))))
  19748. ;; Case 4. Normal line without ARG: turn each non-item line
  19749. ;; into an item.
  19750. (t
  19751. (while (< (point) end)
  19752. (unless (or (org-at-heading-p) (org-at-item-p))
  19753. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19754. (replace-match
  19755. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19756. (forward-line))))))))
  19757. (defun org-toggle-heading (&optional nstars)
  19758. "Convert headings to normal text, or items or text to headings.
  19759. If there is no active region, only convert the current line.
  19760. With a \\[universal-argument] prefix, convert the whole list at
  19761. point into heading.
  19762. In a region:
  19763. - If the first non blank line is a headline, remove the stars
  19764. from all headlines in the region.
  19765. - If it is a normal line, turn each and every normal line (i.e.,
  19766. not an heading or an item) in the region into headings. If you
  19767. want to convert only the first line of this region, use one
  19768. universal prefix argument.
  19769. - If it is a plain list item, turn all plain list items into headings.
  19770. When converting a line into a heading, the number of stars is chosen
  19771. such that the lines become children of the current entry. However,
  19772. when a numeric prefix argument is given, its value determines the
  19773. number of stars to add."
  19774. (interactive "P")
  19775. (let ((skip-blanks
  19776. (function
  19777. ;; Return beginning of first non-blank line, starting from
  19778. ;; line at POS.
  19779. (lambda (pos)
  19780. (save-excursion
  19781. (goto-char pos)
  19782. (while (org-at-comment-p) (forward-line))
  19783. (skip-chars-forward " \r\t\n")
  19784. (point-at-bol)))))
  19785. beg end toggled)
  19786. ;; Determine boundaries of changes. If a universal prefix has
  19787. ;; been given, put the list in a region. If region ends at a bol,
  19788. ;; do not consider the last line to be in the region.
  19789. (when (and current-prefix-arg (org-at-item-p))
  19790. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19791. (org-mark-element))
  19792. (if (org-region-active-p)
  19793. (setq beg (funcall skip-blanks (region-beginning))
  19794. end (copy-marker (save-excursion
  19795. (goto-char (region-end))
  19796. (if (bolp) (point) (point-at-eol)))))
  19797. (setq beg (funcall skip-blanks (point-at-bol))
  19798. end (copy-marker (point-at-eol))))
  19799. ;; Ensure inline tasks don't count as headings.
  19800. (org-with-limited-levels
  19801. (save-excursion
  19802. (goto-char beg)
  19803. (cond
  19804. ;; Case 1. Started at an heading: de-star headings.
  19805. ((org-at-heading-p)
  19806. (while (< (point) end)
  19807. (when (org-at-heading-p t)
  19808. (looking-at org-outline-regexp) (replace-match "")
  19809. (setq toggled t))
  19810. (forward-line)))
  19811. ;; Case 2. Started at an item: change items into headlines.
  19812. ;; One star will be added by `org-list-to-subtree'.
  19813. ((org-at-item-p)
  19814. (while (< (point) end)
  19815. (when (org-at-item-p)
  19816. ;; Pay attention to cases when region ends before list.
  19817. (let* ((struct (org-list-struct))
  19818. (list-end
  19819. (min (org-list-get-bottom-point struct) (1+ end))))
  19820. (save-restriction
  19821. (narrow-to-region (point) list-end)
  19822. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19823. (setq toggled t))
  19824. (forward-line)))
  19825. ;; Case 3. Started at normal text: make every line an heading,
  19826. ;; skipping headlines and items.
  19827. (t (let* ((stars
  19828. (make-string
  19829. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19830. (add-stars
  19831. (cond (nstars "") ; stars from prefix only
  19832. ((equal stars "") "*") ; before first heading
  19833. (org-odd-levels-only "**") ; inside heading, odd
  19834. (t "*"))) ; inside heading, oddeven
  19835. (rpl (concat stars add-stars " "))
  19836. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19837. (while (< (point) (if (equal nstars '(4)) lend end))
  19838. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19839. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19840. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19841. (forward-line)))))))
  19842. (unless toggled (message "Cannot toggle heading from here"))))
  19843. (defun org-meta-return (&optional _arg)
  19844. "Insert a new heading or wrap a region in a table.
  19845. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19846. on context. See the individual commands for more information."
  19847. (interactive)
  19848. (org-check-before-invisible-edit 'insert)
  19849. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19850. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19851. #'org-insert-heading))))
  19852. ;;; Menu entries
  19853. (defsubst org-in-subtree-not-table-p ()
  19854. "Are we in a subtree and not in a table?"
  19855. (and (not (org-before-first-heading-p))
  19856. (not (org-at-table-p))))
  19857. ;; Define the Org-mode menus
  19858. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19859. '("Tbl"
  19860. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19861. ["Next Field" org-cycle (org-at-table-p)]
  19862. ["Previous Field" org-shifttab (org-at-table-p)]
  19863. ["Next Row" org-return (org-at-table-p)]
  19864. "--"
  19865. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19866. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19867. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19868. "--"
  19869. ("Column"
  19870. ["Move Column Left" org-metaleft (org-at-table-p)]
  19871. ["Move Column Right" org-metaright (org-at-table-p)]
  19872. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19873. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19874. ("Row"
  19875. ["Move Row Up" org-metaup (org-at-table-p)]
  19876. ["Move Row Down" org-metadown (org-at-table-p)]
  19877. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19878. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19879. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19880. "--"
  19881. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19882. ("Rectangle"
  19883. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19884. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19885. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19886. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19887. "--"
  19888. ("Calculate"
  19889. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19890. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19891. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19892. "--"
  19893. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19894. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19895. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19896. "--"
  19897. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19898. "--"
  19899. ["Sum Column/Rectangle" org-table-sum
  19900. (or (org-at-table-p) (org-region-active-p))]
  19901. ["Which Column?" org-table-current-column (org-at-table-p)])
  19902. ["Debug Formulas"
  19903. org-table-toggle-formula-debugger
  19904. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19905. ["Show Col/Row Numbers"
  19906. org-table-toggle-coordinate-overlays
  19907. :style toggle
  19908. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19909. "--"
  19910. ["Create" org-table-create (and (not (org-at-table-p))
  19911. org-enable-table-editor)]
  19912. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19913. ["Import from File" org-table-import (not (org-at-table-p))]
  19914. ["Export to File" org-table-export (org-at-table-p)]
  19915. "--"
  19916. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19917. "--"
  19918. ("Plot"
  19919. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19920. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19921. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19922. '("Org"
  19923. ("Show/Hide"
  19924. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19925. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19926. ["Sparse Tree..." org-sparse-tree t]
  19927. ["Reveal Context" org-reveal t]
  19928. ["Show All" outline-show-all t]
  19929. "--"
  19930. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19931. "--"
  19932. ["New Heading" org-insert-heading t]
  19933. ("Navigate Headings"
  19934. ["Up" outline-up-heading t]
  19935. ["Next" outline-next-visible-heading t]
  19936. ["Previous" outline-previous-visible-heading t]
  19937. ["Next Same Level" outline-forward-same-level t]
  19938. ["Previous Same Level" outline-backward-same-level t]
  19939. "--"
  19940. ["Jump" org-goto t])
  19941. ("Edit Structure"
  19942. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19943. "--"
  19944. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19945. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19946. "--"
  19947. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19948. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19949. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19950. "--"
  19951. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19952. "--"
  19953. ["Copy visible text" org-copy-visible t]
  19954. "--"
  19955. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19956. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19957. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19958. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19959. "--"
  19960. ["Sort Region/Children" org-sort t]
  19961. "--"
  19962. ["Convert to odd levels" org-convert-to-odd-levels t]
  19963. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19964. ("Editing"
  19965. ["Emphasis..." org-emphasize t]
  19966. ["Edit Source Example" org-edit-special t]
  19967. "--"
  19968. ["Footnote new/jump" org-footnote-action t]
  19969. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19970. ("Archive"
  19971. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19972. "--"
  19973. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19974. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19975. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19976. )
  19977. "--"
  19978. ("Hyperlinks"
  19979. ["Store Link (Global)" org-store-link t]
  19980. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19981. ["Insert Link" org-insert-link t]
  19982. ["Follow Link" org-open-at-point t]
  19983. "--"
  19984. ["Next link" org-next-link t]
  19985. ["Previous link" org-previous-link t]
  19986. "--"
  19987. ["Descriptive Links"
  19988. org-toggle-link-display
  19989. :style radio
  19990. :selected org-descriptive-links
  19991. ]
  19992. ["Literal Links"
  19993. org-toggle-link-display
  19994. :style radio
  19995. :selected (not org-descriptive-links)])
  19996. "--"
  19997. ("TODO Lists"
  19998. ["TODO/DONE/-" org-todo t]
  19999. ("Select keyword"
  20000. ["Next keyword" org-shiftright (org-at-heading-p)]
  20001. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20002. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20003. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20004. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20005. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20006. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20007. "--"
  20008. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20009. :selected org-enforce-todo-dependencies :style toggle :active t]
  20010. "Settings for tree at point"
  20011. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20012. :selected (org-entry-get nil "ORDERED")
  20013. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20014. ["Do Children parallel" org-toggle-ordered-property :style radio
  20015. :selected (not (org-entry-get nil "ORDERED"))
  20016. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20017. "--"
  20018. ["Set Priority" org-priority t]
  20019. ["Priority Up" org-shiftup t]
  20020. ["Priority Down" org-shiftdown t]
  20021. "--"
  20022. ["Get news from all feeds" org-feed-update-all t]
  20023. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20024. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20025. ("TAGS and Properties"
  20026. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20027. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20028. "--"
  20029. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20030. ["Column view of properties" org-columns t]
  20031. ["Insert Column View DBlock" org-columns-insert-dblock t])
  20032. ("Dates and Scheduling"
  20033. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20034. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20035. ("Change Date"
  20036. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20037. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20038. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20039. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20040. ["Compute Time Range" org-evaluate-time-range t]
  20041. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20042. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20043. "--"
  20044. ["Custom time format" org-toggle-time-stamp-overlays
  20045. :style radio :selected org-display-custom-times]
  20046. "--"
  20047. ["Goto Calendar" org-goto-calendar t]
  20048. ["Date from Calendar" org-date-from-calendar t]
  20049. "--"
  20050. ["Start/Restart Timer" org-timer-start t]
  20051. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20052. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20053. ["Insert Timer String" org-timer t]
  20054. ["Insert Timer Item" org-timer-item t])
  20055. ("Logging work"
  20056. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20057. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20058. ["Clock out" org-clock-out t]
  20059. ["Clock cancel" org-clock-cancel t]
  20060. "--"
  20061. ["Mark as default task" org-clock-mark-default-task t]
  20062. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20063. ["Goto running clock" org-clock-goto t]
  20064. "--"
  20065. ["Display times" org-clock-display t]
  20066. ["Create clock table" org-clock-report t]
  20067. "--"
  20068. ["Record DONE time"
  20069. (progn (setq org-log-done (not org-log-done))
  20070. (message "Switching to %s will %s record a timestamp"
  20071. (car org-done-keywords)
  20072. (if org-log-done "automatically" "not")))
  20073. :style toggle :selected org-log-done])
  20074. "--"
  20075. ["Agenda Command..." org-agenda t]
  20076. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20077. ("File List for Agenda")
  20078. ("Special views current file"
  20079. ["TODO Tree" org-show-todo-tree t]
  20080. ["Check Deadlines" org-check-deadlines t]
  20081. ["Timeline" org-timeline t]
  20082. ["Tags/Property tree" org-match-sparse-tree t])
  20083. "--"
  20084. ["Export/Publish..." org-export-dispatch t]
  20085. ("LaTeX"
  20086. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20087. :selected org-cdlatex-mode]
  20088. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20089. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20090. ["Modify math symbol" org-cdlatex-math-modify
  20091. (org-inside-LaTeX-fragment-p)]
  20092. ["Insert citation" org-reftex-citation t])
  20093. "--"
  20094. ("MobileOrg"
  20095. ["Push Files and Views" org-mobile-push t]
  20096. ["Get Captured and Flagged" org-mobile-pull t]
  20097. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20098. "--"
  20099. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20100. "--"
  20101. ("Documentation"
  20102. ["Show Version" org-version t]
  20103. ["Info Documentation" org-info t])
  20104. ("Customize"
  20105. ["Browse Org Group" org-customize t]
  20106. "--"
  20107. ["Expand This Menu" org-create-customize-menu
  20108. (fboundp 'customize-menu-create)])
  20109. ["Send bug report" org-submit-bug-report t]
  20110. "--"
  20111. ("Refresh/Reload"
  20112. ["Refresh setup current buffer" org-mode-restart t]
  20113. ["Reload Org (after update)" org-reload t]
  20114. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20115. ))
  20116. (defun org-info (&optional node)
  20117. "Read documentation for Org-mode in the info system.
  20118. With optional NODE, go directly to that node."
  20119. (interactive)
  20120. (info (format "(org)%s" (or node ""))))
  20121. ;;;###autoload
  20122. (defun org-submit-bug-report ()
  20123. "Submit a bug report on Org-mode via mail.
  20124. Don't hesitate to report any problems or inaccurate documentation.
  20125. If you don't have setup sending mail from (X)Emacs, please copy the
  20126. output buffer into your mail program, as it gives us important
  20127. information about your Org-mode version and configuration."
  20128. (interactive)
  20129. (require 'reporter)
  20130. (defvar reporter-prompt-for-summary-p)
  20131. (org-load-modules-maybe)
  20132. (org-require-autoloaded-modules)
  20133. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20134. (reporter-submit-bug-report
  20135. "emacs-orgmode@gnu.org"
  20136. (org-version nil 'full)
  20137. (let (list)
  20138. (save-window-excursion
  20139. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20140. (delete-other-windows)
  20141. (erase-buffer)
  20142. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20143. We would like to add your full Org-mode and Outline configuration to the
  20144. bug report. This greatly simplifies the work of the maintainer and
  20145. other experts on the mailing list.
  20146. HOWEVER, some variables you have customized may contain private
  20147. information. The names of customers, colleagues, or friends, might
  20148. appear in the form of file names, tags, todo states, or search strings.
  20149. If you answer yes to the prompt, you might want to check and remove
  20150. such private information before sending the email.")
  20151. (add-text-properties (point-min) (point-max) '(face org-warning))
  20152. (when (yes-or-no-p "Include your Org-mode configuration ")
  20153. (mapatoms
  20154. (lambda (v)
  20155. (and (boundp v)
  20156. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20157. (or (and (symbol-value v)
  20158. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20159. (and
  20160. (get v 'custom-type) (get v 'standard-value)
  20161. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20162. (push v list)))))
  20163. (kill-buffer (get-buffer "*Warn about privacy*"))
  20164. list))
  20165. nil nil
  20166. "Remember to cover the basics, that is, what you expected to happen and
  20167. what in fact did happen. You don't know how to make a good report? See
  20168. http://orgmode.org/manual/Feedback.html#Feedback
  20169. Your bug report will be posted to the Org-mode mailing list.
  20170. ------------------------------------------------------------------------")
  20171. (save-excursion
  20172. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20173. (replace-match "\\1Bug: \\3 [\\2]")))))
  20174. (defun org-install-agenda-files-menu ()
  20175. (let ((bl (buffer-list)))
  20176. (save-excursion
  20177. (while bl
  20178. (set-buffer (pop bl))
  20179. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20180. (when (derived-mode-p 'org-mode)
  20181. (easy-menu-change
  20182. '("Org") "File List for Agenda"
  20183. (append
  20184. (list
  20185. ["Edit File List" (org-edit-agenda-file-list) t]
  20186. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20187. ["Remove Current File from List" org-remove-file t]
  20188. ["Cycle through agenda files" org-cycle-agenda-files t]
  20189. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20190. "--")
  20191. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20192. ;;;; Documentation
  20193. (defun org-require-autoloaded-modules ()
  20194. (interactive)
  20195. (mapc #'require
  20196. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20197. org-table org-timer)))
  20198. ;;;###autoload
  20199. (defun org-reload (&optional uncompiled)
  20200. "Reload all org lisp files.
  20201. With prefix arg UNCOMPILED, load the uncompiled versions."
  20202. (interactive "P")
  20203. (require 'loadhist)
  20204. (let* ((org-dir (org-find-library-dir "org"))
  20205. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20206. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20207. (remove-re (format "\\`%s\\'"
  20208. (regexp-opt '("org" "org-loaddefs" "org-version"))))
  20209. (feats (delete-dups
  20210. (mapcar 'file-name-sans-extension
  20211. (mapcar 'file-name-nondirectory
  20212. (delq nil
  20213. (mapcar 'feature-file
  20214. features))))))
  20215. (lfeat (append
  20216. (sort
  20217. (setq feats
  20218. (delq nil (mapcar
  20219. (lambda (f)
  20220. (if (and (string-match feature-re f)
  20221. (not (string-match remove-re f)))
  20222. f nil))
  20223. feats)))
  20224. 'string-lessp)
  20225. (list "org-version" "org")))
  20226. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20227. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20228. load-uncore load-misses)
  20229. (setq load-misses
  20230. (delq 't
  20231. (mapcar (lambda (f)
  20232. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20233. (and (string= org-dir contrib-dir)
  20234. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20235. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20236. (add-to-list 'load-uncore f 'append)
  20237. 't)
  20238. f))
  20239. lfeat)))
  20240. (when load-uncore
  20241. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20242. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20243. (if load-misses
  20244. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20245. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20246. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20247. ;;;###autoload
  20248. (defun org-customize ()
  20249. "Call the customize function with org as argument."
  20250. (interactive)
  20251. (org-load-modules-maybe)
  20252. (org-require-autoloaded-modules)
  20253. (customize-browse 'org))
  20254. (defun org-create-customize-menu ()
  20255. "Create a full customization menu for Org-mode, insert it into the menu."
  20256. (interactive)
  20257. (org-load-modules-maybe)
  20258. (org-require-autoloaded-modules)
  20259. (if (fboundp 'customize-menu-create)
  20260. (progn
  20261. (easy-menu-change
  20262. '("Org") "Customize"
  20263. `(["Browse Org group" org-customize t]
  20264. "--"
  20265. ,(customize-menu-create 'org)
  20266. ["Set" Custom-set t]
  20267. ["Save" Custom-save t]
  20268. ["Reset to Current" Custom-reset-current t]
  20269. ["Reset to Saved" Custom-reset-saved t]
  20270. ["Reset to Standard Settings" Custom-reset-standard t]))
  20271. (message "\"Org\"-menu now contains full customization menu"))
  20272. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20273. ;;;; Miscellaneous stuff
  20274. ;;; Generally useful functions
  20275. (defun org-get-at-eol (property n)
  20276. "Get text property PROPERTY at the end of line less N characters."
  20277. (get-text-property (- (point-at-eol) n) property))
  20278. (defun org-find-text-property-in-string (prop s)
  20279. "Return the first non-nil value of property PROP in string S."
  20280. (or (get-text-property 0 prop s)
  20281. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20282. prop s)))
  20283. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20284. "Display the given MESSAGE as a warning."
  20285. (if (fboundp 'display-warning)
  20286. (display-warning 'org message :warning)
  20287. (let ((buf (get-buffer-create "*Org warnings*")))
  20288. (with-current-buffer buf
  20289. (goto-char (point-max))
  20290. (insert "Warning (Org): " message)
  20291. (unless (bolp)
  20292. (newline)))
  20293. (display-buffer buf)
  20294. (sit-for 0))))
  20295. (defun org-eval (form)
  20296. "Eval FORM and return result."
  20297. (condition-case error
  20298. (eval form)
  20299. (error (format "%%![Error: %s]" error))))
  20300. (defun org-in-clocktable-p ()
  20301. "Check if the cursor is in a clocktable."
  20302. (let ((pos (point)) start)
  20303. (save-excursion
  20304. (end-of-line 1)
  20305. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20306. (setq start (match-beginning 0))
  20307. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20308. (>= (match-end 0) pos)
  20309. start))))
  20310. (defun org-in-verbatim-emphasis ()
  20311. (save-match-data
  20312. (and (org-in-regexp org-emph-re 2)
  20313. (>= (point) (match-beginning 3))
  20314. (<= (point) (match-end 4))
  20315. (member (match-string 3) '("=" "~")))))
  20316. (defun org-overlay-display (ovl text &optional face evap)
  20317. "Make overlay OVL display TEXT with face FACE."
  20318. (overlay-put ovl 'display text)
  20319. (if face (overlay-put ovl 'face face))
  20320. (if evap (overlay-put ovl 'evaporate t)))
  20321. (defun org-overlay-before-string (ovl text &optional face evap)
  20322. "Make overlay OVL display TEXT with face FACE."
  20323. (if face (org-add-props text nil 'face face))
  20324. (overlay-put ovl 'before-string text)
  20325. (if evap (overlay-put ovl 'evaporate t)))
  20326. (defun org-find-overlays (prop &optional pos delete)
  20327. "Find all overlays specifying PROP at POS or point.
  20328. If DELETE is non-nil, delete all those overlays."
  20329. (let (found)
  20330. (dolist (ov (overlays-at (or pos (point))) found)
  20331. (cond ((not (overlay-get ov prop)))
  20332. (delete (delete-overlay ov))
  20333. (t (push ov found))))))
  20334. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20335. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20336. (if (and marker (marker-buffer marker)
  20337. (buffer-live-p (marker-buffer marker)))
  20338. (progn
  20339. (org-pop-to-buffer-same-window (marker-buffer marker))
  20340. (when (or (> marker (point-max)) (< marker (point-min)))
  20341. (widen))
  20342. (goto-char marker)
  20343. (org-show-context 'org-goto))
  20344. (if bookmark
  20345. (bookmark-jump bookmark)
  20346. (error "Cannot find location"))))
  20347. (defun org-quote-csv-field (s)
  20348. "Quote field for inclusion in CSV material."
  20349. (if (string-match "[\",]" s)
  20350. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20351. s))
  20352. (defun org-force-self-insert (N)
  20353. "Needed to enforce self-insert under remapping."
  20354. (interactive "p")
  20355. (self-insert-command N))
  20356. (defun org-string-width (s)
  20357. "Compute width of string, ignoring invisible characters.
  20358. This ignores character with invisibility property `org-link', and also
  20359. characters with property `org-cwidth', because these will become invisible
  20360. upon the next fontification round."
  20361. (let (b l)
  20362. (when (or (eq t buffer-invisibility-spec)
  20363. (assq 'org-link buffer-invisibility-spec))
  20364. (while (setq b (text-property-any 0 (length s)
  20365. 'invisible 'org-link s))
  20366. (setq s (concat (substring s 0 b)
  20367. (substring s (or (next-single-property-change
  20368. b 'invisible s)
  20369. (length s)))))))
  20370. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20371. (setq s (concat (substring s 0 b)
  20372. (substring s (or (next-single-property-change
  20373. b 'org-cwidth s)
  20374. (length s))))))
  20375. (setq l (string-width s) b -1)
  20376. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20377. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20378. l))
  20379. (defun org-shorten-string (s maxlength)
  20380. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20381. If the string is shorter or has length MAXLENGTH, just return the
  20382. original string. If it is longer, the functions finds a space in the
  20383. string, breaks this string off at that locations and adds three dots
  20384. as ellipsis. Including the ellipsis, the string will not be longer
  20385. than MAXLENGTH. If finding a good breaking point in the string does
  20386. not work, the string is just chopped off in the middle of a word
  20387. if necessary."
  20388. (if (<= (length s) maxlength)
  20389. s
  20390. (let* ((n (max (- maxlength 4) 1))
  20391. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20392. (if (string-match re s)
  20393. (concat (match-string 1 s) "...")
  20394. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20395. (defun org-get-indentation (&optional line)
  20396. "Get the indentation of the current line, interpreting tabs.
  20397. When LINE is given, assume it represents a line and compute its indentation."
  20398. (if line
  20399. (when (string-match "^ *" (org-remove-tabs line))
  20400. (match-end 0))
  20401. (save-excursion
  20402. (beginning-of-line 1)
  20403. (skip-chars-forward " \t")
  20404. (current-column))))
  20405. (defun org-get-string-indentation (s)
  20406. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20407. (let ((n -1) (i 0) (w tab-width) c)
  20408. (catch 'exit
  20409. (while (< (setq n (1+ n)) (length s))
  20410. (setq c (aref s n))
  20411. (cond ((= c ?\ ) (setq i (1+ i)))
  20412. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20413. (t (throw 'exit t)))))
  20414. i))
  20415. (defun org-remove-tabs (s &optional width)
  20416. "Replace tabulators in S with spaces.
  20417. Assumes that s is a single line, starting in column 0."
  20418. (setq width (or width tab-width))
  20419. (while (string-match "\t" s)
  20420. (setq s (replace-match
  20421. (make-string
  20422. (- (* width (/ (+ (match-beginning 0) width) width))
  20423. (match-beginning 0)) ?\ )
  20424. t t s)))
  20425. s)
  20426. (defun org-fix-indentation (line ind)
  20427. "Fix indentation in LINE.
  20428. IND is a cons cell with target and minimum indentation.
  20429. If the current indentation in LINE is smaller than the minimum,
  20430. leave it alone. If it is larger than ind, set it to the target."
  20431. (let* ((l (org-remove-tabs line))
  20432. (i (org-get-indentation l))
  20433. (i1 (car ind)) (i2 (cdr ind)))
  20434. (when (>= i i2) (setq l (substring line i2)))
  20435. (if (> i1 0)
  20436. (concat (make-string i1 ?\ ) l)
  20437. l)))
  20438. (defun org-remove-indentation (code &optional n)
  20439. "Remove maximum common indentation in string CODE and return it.
  20440. N may optionally be the number of columns to remove. Return CODE
  20441. as-is if removal failed."
  20442. (with-temp-buffer
  20443. (insert code)
  20444. (if (org-do-remove-indentation n) (buffer-string) code)))
  20445. (defun org-do-remove-indentation (&optional n)
  20446. "Remove the maximum common indentation from the buffer.
  20447. When optional argument N is a positive integer, remove exactly
  20448. that much characters from indentation, if possible. Return nil
  20449. if it fails."
  20450. (catch :exit
  20451. (goto-char (point-min))
  20452. ;; Find maximum common indentation, if not specified.
  20453. (let ((n (or n
  20454. (let ((min-ind (point-max)))
  20455. (save-excursion
  20456. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20457. (let ((ind (1- (current-column))))
  20458. (if (zerop ind) (throw :exit nil)
  20459. (setq min-ind (min min-ind ind))))))
  20460. min-ind))))
  20461. (if (zerop n) (throw :exit nil)
  20462. ;; Remove exactly N indentation, but give up if not possible.
  20463. (while (not (eobp))
  20464. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20465. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20466. ((< ind n) (throw :exit nil))
  20467. (t (indent-line-to (- ind n))))
  20468. (forward-line)))
  20469. ;; Signal success.
  20470. t))))
  20471. (defun org-fill-template (template alist)
  20472. "Find each %key of ALIST in TEMPLATE and replace it."
  20473. (let ((case-fold-search nil))
  20474. (dolist (entry (sort (copy-sequence alist)
  20475. (lambda (a b) (< (length (car a)) (length (car b))))))
  20476. (setq template
  20477. (replace-regexp-in-string
  20478. (concat "%" (regexp-quote (car entry)))
  20479. (or (cdr entry) "") template t t)))
  20480. template))
  20481. (defun org-base-buffer (buffer)
  20482. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20483. (if (not buffer)
  20484. buffer
  20485. (or (buffer-base-buffer buffer)
  20486. buffer)))
  20487. (defun org-wrap (string &optional width lines)
  20488. "Wrap string to either a number of lines, or a width in characters.
  20489. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20490. that costs. If there is a word longer than WIDTH, the text is actually
  20491. wrapped to the length of that word.
  20492. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20493. many lines, whatever width that takes.
  20494. The return value is a list of lines, without newlines at the end."
  20495. (let* ((words (org-split-string string "[ \t\n]+"))
  20496. (maxword (apply 'max (mapcar 'org-string-width words)))
  20497. w ll)
  20498. (cond (width
  20499. (org-do-wrap words (max maxword width)))
  20500. (lines
  20501. (setq w maxword)
  20502. (setq ll (org-do-wrap words maxword))
  20503. (if (<= (length ll) lines)
  20504. ll
  20505. (setq ll words)
  20506. (while (> (length ll) lines)
  20507. (setq w (1+ w))
  20508. (setq ll (org-do-wrap words w)))
  20509. ll))
  20510. (t (error "Cannot wrap this")))))
  20511. (defun org-do-wrap (words width)
  20512. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20513. (let (lines line)
  20514. (while words
  20515. (setq line (pop words))
  20516. (while (and words (< (+ (length line) (length (car words))) width))
  20517. (setq line (concat line " " (pop words))))
  20518. (setq lines (push line lines)))
  20519. (nreverse lines)))
  20520. (defun org-split-string (string &optional separators)
  20521. "Splits STRING into substrings at SEPARATORS.
  20522. SEPARATORS is a regular expression.
  20523. No empty strings are returned if there are matches at the beginning
  20524. and end of string."
  20525. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20526. (let ((start 0) notfirst list)
  20527. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20528. (if (and notfirst
  20529. (= start (match-beginning 0))
  20530. (< start (length string)))
  20531. (1+ start) start))
  20532. (< (match-beginning 0) (length string)))
  20533. (setq notfirst t)
  20534. (or (eq (match-beginning 0) 0)
  20535. (and (eq (match-beginning 0) (match-end 0))
  20536. (eq (match-beginning 0) start))
  20537. (push (substring string start (match-beginning 0)) list))
  20538. (setq start (match-end 0)))
  20539. (or (eq start (length string))
  20540. (push (substring string start) list))
  20541. (nreverse list)))
  20542. (defun org-quote-vert (s)
  20543. "Replace \"|\" with \"\\vert\"."
  20544. (while (string-match "|" s)
  20545. (setq s (replace-match "\\vert" t t s)))
  20546. s)
  20547. (defun org-uuidgen-p (s)
  20548. "Is S an ID created by UUIDGEN?"
  20549. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20550. (defun org-in-src-block-p (&optional inside)
  20551. "Whether point is in a code source block.
  20552. When INSIDE is non-nil, don't consider we are within a src block
  20553. when point is at #+BEGIN_SRC or #+END_SRC."
  20554. (let ((case-fold-search t))
  20555. (or (and (eq (get-char-property (point) 'src-block) t))
  20556. (and (not inside)
  20557. (save-match-data
  20558. (save-excursion
  20559. (beginning-of-line)
  20560. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20561. (defun org-context ()
  20562. "Return a list of contexts of the current cursor position.
  20563. If several contexts apply, all are returned.
  20564. Each context entry is a list with a symbol naming the context, and
  20565. two positions indicating start and end of the context. Possible
  20566. contexts are:
  20567. :headline anywhere in a headline
  20568. :headline-stars on the leading stars in a headline
  20569. :todo-keyword on a TODO keyword (including DONE) in a headline
  20570. :tags on the TAGS in a headline
  20571. :priority on the priority cookie in a headline
  20572. :item on the first line of a plain list item
  20573. :item-bullet on the bullet/number of a plain list item
  20574. :checkbox on the checkbox in a plain list item
  20575. :table in an org-mode table
  20576. :table-special on a special filed in a table
  20577. :table-table in a table.el table
  20578. :clocktable in a clocktable
  20579. :src-block in a source block
  20580. :link on a hyperlink
  20581. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20582. :target on a <<target>>
  20583. :radio-target on a <<<radio-target>>>
  20584. :latex-fragment on a LaTeX fragment
  20585. :latex-preview on a LaTeX fragment with overlaid preview image
  20586. This function expects the position to be visible because it uses font-lock
  20587. faces as a help to recognize the following contexts: :table-special, :link,
  20588. and :keyword."
  20589. (let* ((f (get-text-property (point) 'face))
  20590. (faces (if (listp f) f (list f)))
  20591. (case-fold-search t)
  20592. (p (point)) clist o)
  20593. ;; First the large context
  20594. (cond
  20595. ((org-at-heading-p t)
  20596. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20597. (when (progn
  20598. (beginning-of-line 1)
  20599. (looking-at org-todo-line-tags-regexp))
  20600. (push (org-point-in-group p 1 :headline-stars) clist)
  20601. (push (org-point-in-group p 2 :todo-keyword) clist)
  20602. (push (org-point-in-group p 4 :tags) clist))
  20603. (goto-char p)
  20604. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20605. (when (looking-at "\\[#[A-Z0-9]\\]")
  20606. (push (org-point-in-group p 0 :priority) clist)))
  20607. ((org-at-item-p)
  20608. (push (org-point-in-group p 2 :item-bullet) clist)
  20609. (push (list :item (point-at-bol)
  20610. (save-excursion (org-end-of-item) (point)))
  20611. clist)
  20612. (and (org-at-item-checkbox-p)
  20613. (push (org-point-in-group p 0 :checkbox) clist)))
  20614. ((org-at-table-p)
  20615. (push (list :table (org-table-begin) (org-table-end)) clist)
  20616. (when (memq 'org-formula faces)
  20617. (push (list :table-special
  20618. (previous-single-property-change p 'face)
  20619. (next-single-property-change p 'face)) clist)))
  20620. ((org-at-table-p 'any)
  20621. (push (list :table-table) clist)))
  20622. (goto-char p)
  20623. (let ((case-fold-search t))
  20624. ;; New the "medium" contexts: clocktables, source blocks
  20625. (cond ((org-in-clocktable-p)
  20626. (push (list :clocktable
  20627. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20628. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20629. (match-beginning 1))
  20630. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20631. (match-end 0))) clist))
  20632. ((org-in-src-block-p)
  20633. (push (list :src-block
  20634. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20635. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20636. (match-beginning 1))
  20637. (and (search-forward "#+END_SRC" nil t)
  20638. (match-beginning 0))) clist))))
  20639. (goto-char p)
  20640. ;; Now the small context
  20641. (cond
  20642. ((org-at-timestamp-p)
  20643. (push (org-point-in-group p 0 :timestamp) clist))
  20644. ((memq 'org-link faces)
  20645. (push (list :link
  20646. (previous-single-property-change p 'face)
  20647. (next-single-property-change p 'face)) clist))
  20648. ((memq 'org-special-keyword faces)
  20649. (push (list :keyword
  20650. (previous-single-property-change p 'face)
  20651. (next-single-property-change p 'face)) clist))
  20652. ((org-at-target-p)
  20653. (push (org-point-in-group p 0 :target) clist)
  20654. (goto-char (1- (match-beginning 0)))
  20655. (when (looking-at org-radio-target-regexp)
  20656. (push (org-point-in-group p 0 :radio-target) clist))
  20657. (goto-char p))
  20658. ((setq o (cl-some
  20659. (lambda (o)
  20660. (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
  20661. o))
  20662. (overlays-at (point))))
  20663. (push (list :latex-fragment
  20664. (overlay-start o) (overlay-end o)) clist)
  20665. (push (list :latex-preview
  20666. (overlay-start o) (overlay-end o)) clist))
  20667. ((org-inside-LaTeX-fragment-p)
  20668. ;; FIXME: positions wrong.
  20669. (push (list :latex-fragment (point) (point)) clist)))
  20670. (setq clist (nreverse (delq nil clist)))
  20671. clist))
  20672. (defun org-in-regexp (regexp &optional nlines visually)
  20673. "Check if point is inside a match of REGEXP.
  20674. Normally only the current line is checked, but you can include
  20675. NLINES extra lines around point into the search. If VISUALLY is
  20676. set, require that the cursor is not after the match but really
  20677. on, so that the block visually is on the match.
  20678. Return nil or a cons cell (BEG . END) where BEG and END are,
  20679. respectively, the positions at the beginning and the end of the
  20680. match."
  20681. (catch :exit
  20682. (let ((pos (point))
  20683. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20684. (save-excursion
  20685. (beginning-of-line (- 1 (or nlines 0)))
  20686. (while (and (re-search-forward regexp eol t)
  20687. (<= (match-beginning 0) pos))
  20688. (let ((end (match-end 0)))
  20689. (when (or (> end pos) (and (= end pos) (not visually)))
  20690. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20691. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20692. "Org mode 8.3")
  20693. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20694. "Non-nil when point is between matches of START-RE and END-RE.
  20695. Also return a non-nil value when point is on one of the matches.
  20696. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20697. buffer positions. Default values are the positions of headlines
  20698. surrounding the point.
  20699. The functions returns a cons cell whose car (resp. cdr) is the
  20700. position before START-RE (resp. after END-RE)."
  20701. (save-match-data
  20702. (let ((pos (point))
  20703. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20704. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20705. beg end)
  20706. (save-excursion
  20707. ;; Point is on a block when on START-RE or if START-RE can be
  20708. ;; found before it...
  20709. (and (or (org-in-regexp start-re)
  20710. (re-search-backward start-re limit-up t))
  20711. (setq beg (match-beginning 0))
  20712. ;; ... and END-RE after it...
  20713. (goto-char (match-end 0))
  20714. (re-search-forward end-re limit-down t)
  20715. (> (setq end (match-end 0)) pos)
  20716. ;; ... without another START-RE in-between.
  20717. (goto-char (match-beginning 0))
  20718. (not (re-search-backward start-re (1+ beg) t))
  20719. ;; Return value.
  20720. (cons beg end))))))
  20721. (defun org-in-block-p (names)
  20722. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20723. NAMES is a list of strings containing names of blocks.
  20724. Return first block name matched, or nil. Beware that in case of
  20725. nested blocks, the returned name may not belong to the closest
  20726. block from point."
  20727. (save-match-data
  20728. (catch 'exit
  20729. (let ((case-fold-search t)
  20730. (lim-up (save-excursion (outline-previous-heading)))
  20731. (lim-down (save-excursion (outline-next-heading))))
  20732. (dolist (name names)
  20733. (let ((n (regexp-quote name)))
  20734. (when (org-between-regexps-p
  20735. (concat "^[ \t]*#\\+begin_" n)
  20736. (concat "^[ \t]*#\\+end_" n)
  20737. lim-up lim-down)
  20738. (throw 'exit n)))))
  20739. nil)))
  20740. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20741. "Call `multi-occur' with buffers for all agenda files."
  20742. (interactive "sOrg-files matching: ")
  20743. (let* ((files (org-agenda-files))
  20744. (tnames (mapcar #'file-truename files))
  20745. (extra org-agenda-text-search-extra-files))
  20746. (when (eq (car extra) 'agenda-archives)
  20747. (setq extra (cdr extra))
  20748. (setq files (org-add-archive-files files)))
  20749. (dolist (f extra)
  20750. (unless (member (file-truename f) tnames)
  20751. (unless (member f files) (setq files (append files (list f))))
  20752. (setq tnames (append tnames (list (file-truename f))))))
  20753. (multi-occur
  20754. (mapcar (lambda (x)
  20755. (with-current-buffer
  20756. ;; FIXME: Why not just (find-file-noselect x)?
  20757. ;; Is it to avoid the "revert buffer" prompt?
  20758. (or (get-file-buffer x) (find-file-noselect x))
  20759. (widen)
  20760. (current-buffer)))
  20761. files)
  20762. regexp)))
  20763. (if (boundp 'occur-mode-find-occurrence-hook)
  20764. ;; Emacs 23
  20765. (add-hook 'occur-mode-find-occurrence-hook
  20766. (lambda ()
  20767. (when (derived-mode-p 'org-mode)
  20768. (org-reveal))))
  20769. ;; Emacs 22
  20770. (defadvice occur-mode-goto-occurrence
  20771. (after org-occur-reveal activate)
  20772. (and (derived-mode-p 'org-mode) (org-reveal)))
  20773. (defadvice occur-mode-goto-occurrence-other-window
  20774. (after org-occur-reveal activate)
  20775. (and (derived-mode-p 'org-mode) (org-reveal)))
  20776. (defadvice occur-mode-display-occurrence
  20777. (after org-occur-reveal activate)
  20778. (when (derived-mode-p 'org-mode)
  20779. (let ((pos (occur-mode-find-occurrence)))
  20780. (with-current-buffer (marker-buffer pos)
  20781. (save-excursion
  20782. (goto-char pos)
  20783. (org-reveal)))))))
  20784. (defun org-occur-link-in-agenda-files ()
  20785. "Create a link and search for it in the agendas.
  20786. The link is not stored in `org-stored-links', it is just created
  20787. for the search purpose."
  20788. (interactive)
  20789. (let ((link (condition-case nil
  20790. (org-store-link nil)
  20791. (error "Unable to create a link to here"))))
  20792. (org-occur-in-agenda-files (regexp-quote link))))
  20793. (defun org-reverse-string (string)
  20794. "Return the reverse of STRING."
  20795. (apply 'string (reverse (string-to-list string))))
  20796. ;; defsubst org-uniquify must be defined before first use
  20797. (defun org-uniquify-alist (alist)
  20798. "Merge elements of ALIST with the same key.
  20799. For example, in this alist:
  20800. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20801. => \\='((a 1 3) (b 2))
  20802. merge (a 1) and (a 3) into (a 1 3).
  20803. The function returns the new ALIST."
  20804. (let (rtn)
  20805. (dolist (e alist rtn)
  20806. (let (n)
  20807. (if (not (assoc (car e) rtn))
  20808. (push e rtn)
  20809. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20810. (setq rtn (assq-delete-all (car e) rtn))
  20811. (push n rtn))))))
  20812. (defun org-delete-all (elts list)
  20813. "Remove all elements in ELTS from LIST."
  20814. (while elts
  20815. (setq list (delete (pop elts) list)))
  20816. list)
  20817. (defun org-back-over-empty-lines ()
  20818. "Move backwards over whitespace, to the beginning of the first empty line.
  20819. Returns the number of empty lines passed."
  20820. (let ((pos (point)))
  20821. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20822. (skip-chars-backward " \t\n\r")
  20823. (unless (eobp)
  20824. (forward-line -1)))
  20825. (beginning-of-line 2)
  20826. (goto-char (min (point) pos))
  20827. (count-lines (point) pos)))
  20828. (defun org-skip-whitespace ()
  20829. (skip-chars-forward " \t\n\r"))
  20830. (defun org-point-in-group (point group &optional context)
  20831. "Check if POINT is in match-group GROUP.
  20832. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20833. match. If the match group does not exist or point is not inside it,
  20834. return nil."
  20835. (and (match-beginning group)
  20836. (>= point (match-beginning group))
  20837. (<= point (match-end group))
  20838. (if context
  20839. (list context (match-beginning group) (match-end group))
  20840. t)))
  20841. (defun org-switch-to-buffer-other-window (&rest args)
  20842. "Switch to buffer in a second window on the current frame.
  20843. In particular, do not allow pop-up frames.
  20844. Returns the newly created buffer."
  20845. (org-no-popups
  20846. (apply 'switch-to-buffer-other-window args)))
  20847. (defun org-combine-plists (&rest plists)
  20848. "Create a single property list from all plists in PLISTS.
  20849. The process starts by copying the first list, and then setting properties
  20850. from the other lists. Settings in the last list are the most significant
  20851. ones and overrule settings in the other lists."
  20852. (let ((rtn (copy-sequence (pop plists)))
  20853. p v ls)
  20854. (while plists
  20855. (setq ls (pop plists))
  20856. (while ls
  20857. (setq p (pop ls) v (pop ls))
  20858. (setq rtn (plist-put rtn p v))))
  20859. rtn))
  20860. (defun org-replace-escapes (string table)
  20861. "Replace %-escapes in STRING with values in TABLE.
  20862. TABLE is an association list with keys like \"%a\" and string values.
  20863. The sequences in STRING may contain normal field width and padding information,
  20864. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20865. so values can contain further %-escapes if they are define later in TABLE."
  20866. (let ((tbl (copy-alist table))
  20867. (case-fold-search nil)
  20868. (pchg 0)
  20869. re rpl)
  20870. (dolist (e tbl)
  20871. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20872. (when (and (cdr e) (string-match re (cdr e)))
  20873. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20874. (safe "SREF"))
  20875. (add-text-properties 0 3 (list 'sref sref) safe)
  20876. (setcdr e (replace-match safe t t (cdr e)))))
  20877. (while (string-match re string)
  20878. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20879. (cdr e)))
  20880. (setq string (replace-match rpl t t string))))
  20881. (while (setq pchg (next-property-change pchg string))
  20882. (let ((sref (get-text-property pchg 'sref string)))
  20883. (when (and sref (string-match "SREF" string pchg))
  20884. (setq string (replace-match sref t t string)))))
  20885. string))
  20886. (defun org-sublist (list start end)
  20887. "Return a section of LIST, from START to END.
  20888. Counting starts at 1."
  20889. (cl-subseq list (1- start) end))
  20890. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20891. (defun org-find-base-buffer-visiting (file)
  20892. "Like `find-buffer-visiting' but always return the base buffer and
  20893. not an indirect buffer."
  20894. (let ((buf (or (get-file-buffer file)
  20895. (find-buffer-visiting file))))
  20896. (if buf
  20897. (or (buffer-base-buffer buf) buf)
  20898. nil)))
  20899. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20900. "Org 9.0")
  20901. ;;; TODO: Only called once, from ox-odt which should probably use
  20902. ;;; org-export-inline-image-p or something.
  20903. (defun org-file-image-p (file)
  20904. "Return non-nil if FILE is an image."
  20905. (save-match-data
  20906. (string-match (image-file-name-regexp) file)))
  20907. (defun org-get-cursor-date (&optional with-time)
  20908. "Return the date at cursor in as a time.
  20909. This works in the calendar and in the agenda, anywhere else it just
  20910. returns the current time.
  20911. If WITH-TIME is non-nil, returns the time of the event at point (in
  20912. the agenda) or the current time of the day."
  20913. (let (date day defd tp hod mod)
  20914. (when with-time
  20915. (setq tp (get-text-property (point) 'time))
  20916. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20917. (setq hod (string-to-number (match-string 1 tp))
  20918. mod (string-to-number (match-string 2 tp))))
  20919. (or tp (let ((now (decode-time)))
  20920. (setq hod (nth 2 now)
  20921. mod (nth 1 now)))))
  20922. (cond
  20923. ((eq major-mode 'calendar-mode)
  20924. (setq date (calendar-cursor-to-date)
  20925. defd (encode-time 0 (or mod 0) (or hod 0)
  20926. (nth 1 date) (nth 0 date) (nth 2 date))))
  20927. ((eq major-mode 'org-agenda-mode)
  20928. (setq day (get-text-property (point) 'day))
  20929. (when day
  20930. (setq date (calendar-gregorian-from-absolute day)
  20931. defd (encode-time 0 (or mod 0) (or hod 0)
  20932. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20933. (or defd (current-time))))
  20934. (defun org-mark-subtree (&optional up)
  20935. "Mark the current subtree.
  20936. This puts point at the start of the current subtree, and mark at
  20937. the end. If a numeric prefix UP is given, move up into the
  20938. hierarchy of headlines by UP levels before marking the subtree."
  20939. (interactive "P")
  20940. (org-with-limited-levels
  20941. (cond ((org-at-heading-p) (beginning-of-line))
  20942. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20943. (t (outline-previous-visible-heading 1))))
  20944. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20945. (if (org-called-interactively-p 'any)
  20946. (call-interactively 'org-mark-element)
  20947. (org-mark-element)))
  20948. (defun org-file-newer-than-p (file time)
  20949. "Non-nil if FILE is newer than TIME.
  20950. FILE is a filename, as a string, TIME is a list of integers, as
  20951. returned by, e.g., `current-time'."
  20952. (and (file-exists-p file)
  20953. ;; Only compare times up to whole seconds as some file-systems
  20954. ;; (e.g. HFS+) do not retain any finer granularity. As
  20955. ;; a consequence, make sure we return non-nil when the two
  20956. ;; times are equal.
  20957. (not (time-less-p (cl-subseq (nth 5 (file-attributes file)) 0 2)
  20958. (cl-subseq time 0 2)))))
  20959. (defun org-compile-file (source process ext &optional err-msg log-buf spec)
  20960. "Compile a SOURCE file using PROCESS.
  20961. PROCESS is either a function or a list of shell commands, as
  20962. strings. EXT is a file extension, without the leading dot, as
  20963. a string. It is used to check if the process actually succeeded.
  20964. PROCESS must create a file with the same base name and directory
  20965. as SOURCE, but ending with EXT. The function then returns its
  20966. filename. Otherwise, it raises an error. The error message can
  20967. then be refined by providing string ERR-MSG, which is appended to
  20968. the standard message.
  20969. If PROCESS is a function, it is called with a single argument:
  20970. the SOURCE file.
  20971. If it is a list of commands, each of them is called using
  20972. `shell-command'. By default, in each command, %b, %f, %F and %o
  20973. are replaced with, respectively, SOURCE base name, name, full
  20974. name and directory. It is possible, however, to use more
  20975. place-holders by specifying them in optional argument SPEC, as an
  20976. alist following the pattern (CHARACTER . REPLACEMENT-STRING).
  20977. When PROCESS is a list of commands, optional argument LOG-BUF can
  20978. be set to a buffer or a buffer name. `shell-command' then uses
  20979. it for output.
  20980. `default-directory' is set to SOURCE directory during the whole
  20981. process."
  20982. (let* ((source-name (file-name-nondirectory source))
  20983. (base-name (file-name-sans-extension source-name))
  20984. (full-name (file-truename source))
  20985. (out-dir (file-name-directory source))
  20986. ;; Properly set working directory for compilation.
  20987. (default-directory (file-name-directory full-name))
  20988. (time (current-time))
  20989. (err-msg (if (stringp err-msg) (concat ". " err-msg) "")))
  20990. (save-window-excursion
  20991. (pcase process
  20992. ((pred functionp) (funcall process (shell-quote-argument source)))
  20993. ((pred consp)
  20994. (let ((log-buf (and log-buf (get-buffer-create log-buf)))
  20995. (spec (append spec
  20996. `((?b . ,(shell-quote-argument base-name))
  20997. (?f . ,(shell-quote-argument source-name))
  20998. (?F . ,(shell-quote-argument full-name))
  20999. (?o . ,(shell-quote-argument out-dir))))))
  21000. (dolist (command process)
  21001. (shell-command (format-spec command spec) log-buf))))
  21002. (_ (error "No valid command to process %S%s" source err-msg)))
  21003. ;; Check for process failure.
  21004. (let ((output (concat out-dir base-name "." ext)))
  21005. (unless (org-file-newer-than-p output time)
  21006. (error (format "File %S wasn't produced%s" output err-msg)))
  21007. output))))
  21008. ;;; Indentation
  21009. (defun org--get-expected-indentation (element contentsp)
  21010. "Expected indentation column for current line, according to ELEMENT.
  21011. ELEMENT is an element containing point. CONTENTSP is non-nil
  21012. when indentation is to be computed according to contents of
  21013. ELEMENT."
  21014. (let ((type (org-element-type element))
  21015. (start (org-element-property :begin element))
  21016. (post-affiliated (org-element-property :post-affiliated element)))
  21017. (org-with-wide-buffer
  21018. (cond
  21019. (contentsp
  21020. (cl-case type
  21021. ((diary-sexp footnote-definition) 0)
  21022. ((headline inlinetask nil)
  21023. (if (not org-adapt-indentation) 0
  21024. (let ((level (org-current-level)))
  21025. (if level (1+ level) 0))))
  21026. ((item plain-list) (org-list-item-body-column post-affiliated))
  21027. (t
  21028. (goto-char start)
  21029. (org-get-indentation))))
  21030. ((memq type '(headline inlinetask nil))
  21031. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21032. (org--get-expected-indentation element t)
  21033. 0))
  21034. ((memq type '(diary-sexp footnote-definition)) 0)
  21035. ;; First paragraph of a footnote definition or an item.
  21036. ;; Indent like parent.
  21037. ((< (line-beginning-position) start)
  21038. (org--get-expected-indentation
  21039. (org-element-property :parent element) t))
  21040. ;; At first line: indent according to previous sibling, if any,
  21041. ;; ignoring footnote definitions and inline tasks, or parent's
  21042. ;; contents.
  21043. ((= (line-beginning-position) start)
  21044. (catch 'exit
  21045. (while t
  21046. (if (= (point-min) start) (throw 'exit 0)
  21047. (goto-char (1- start))
  21048. (let* ((previous (org-element-at-point))
  21049. (parent previous))
  21050. (while (and parent (<= (org-element-property :end parent) start))
  21051. (setq previous parent
  21052. parent (org-element-property :parent parent)))
  21053. (cond
  21054. ((not previous) (throw 'exit 0))
  21055. ((> (org-element-property :end previous) start)
  21056. (throw 'exit (org--get-expected-indentation previous t)))
  21057. ((memq (org-element-type previous)
  21058. '(footnote-definition inlinetask))
  21059. (setq start (org-element-property :begin previous)))
  21060. (t (goto-char (org-element-property :begin previous))
  21061. (throw 'exit
  21062. (if (bolp) (org-get-indentation)
  21063. ;; At first paragraph in an item or
  21064. ;; a footnote definition.
  21065. (org--get-expected-indentation
  21066. (org-element-property :parent previous) t))))))))))
  21067. ;; Otherwise, move to the first non-blank line above.
  21068. (t
  21069. (beginning-of-line)
  21070. (let ((pos (point)))
  21071. (skip-chars-backward " \r\t\n")
  21072. (cond
  21073. ;; Two blank lines end a footnote definition or a plain
  21074. ;; list. When we indent an empty line after them, the
  21075. ;; containing list or footnote definition is over, so it
  21076. ;; qualifies as a previous sibling. Therefore, we indent
  21077. ;; like its first line.
  21078. ((and (memq type '(footnote-definition plain-list))
  21079. (> (count-lines (point) pos) 2))
  21080. (goto-char start)
  21081. (org-get-indentation))
  21082. ;; Line above is the first one of a paragraph at the
  21083. ;; beginning of an item or a footnote definition. Indent
  21084. ;; like parent.
  21085. ((< (line-beginning-position) start)
  21086. (org--get-expected-indentation
  21087. (org-element-property :parent element) t))
  21088. ;; Line above is the beginning of an element, i.e., point
  21089. ;; was originally on the blank lines between element's start
  21090. ;; and contents.
  21091. ((= (line-beginning-position) post-affiliated)
  21092. (org--get-expected-indentation element t))
  21093. ;; POS is after contents in a greater element. Indent like
  21094. ;; the beginning of the element.
  21095. ;;
  21096. ;; As a special case, if point is at the end of a footnote
  21097. ;; definition or an item, indent like the very last element
  21098. ;; within. If that last element is an item, indent like its
  21099. ;; contents.
  21100. ((and (not (eq type 'paragraph))
  21101. (let ((cend (org-element-property :contents-end element)))
  21102. (and cend (<= cend pos))))
  21103. (if (memq type '(footnote-definition item plain-list))
  21104. (let ((last (org-element-at-point)))
  21105. (org--get-expected-indentation
  21106. last (eq (org-element-type last) 'item)))
  21107. (goto-char start)
  21108. (org-get-indentation)))
  21109. ;; In any other case, indent like the current line.
  21110. (t (org-get-indentation)))))))))
  21111. (defun org--align-node-property ()
  21112. "Align node property at point.
  21113. Alignment is done according to `org-property-format', which see."
  21114. (when (save-excursion
  21115. (beginning-of-line)
  21116. (looking-at org-property-re))
  21117. (replace-match
  21118. (concat (match-string 4)
  21119. (org-trim
  21120. (format org-property-format (match-string 1) (match-string 3))))
  21121. t t)))
  21122. (defun org-indent-line ()
  21123. "Indent line depending on context.
  21124. Indentation is done according to the following rules:
  21125. - Footnote definitions, diary sexps, headlines and inline tasks
  21126. have to start at column 0.
  21127. - On the very first line of an element, consider, in order, the
  21128. next rules until one matches:
  21129. 1. If there's a sibling element before, ignoring footnote
  21130. definitions and inline tasks, indent like its first line.
  21131. 2. If element has a parent, indent like its contents. More
  21132. precisely, if parent is an item, indent after the
  21133. description part, if any, or the bullet (see
  21134. `org-list-description-max-indent'). Else, indent like
  21135. parent's first line.
  21136. 3. Otherwise, indent relatively to current level, if
  21137. `org-adapt-indentation' is non-nil, or to left margin.
  21138. - On a blank line at the end of an element, indent according to
  21139. the type of the element. More precisely
  21140. 1. If element is a plain list, an item, or a footnote
  21141. definition, indent like the very last element within.
  21142. 2. If element is a paragraph, indent like its last non blank
  21143. line.
  21144. 3. Otherwise, indent like its very first line.
  21145. - In the code part of a source block, use language major mode
  21146. to indent current line if `org-src-tab-acts-natively' is
  21147. non-nil. If it is nil, do nothing.
  21148. - Otherwise, indent like the first non-blank line above.
  21149. The function doesn't indent an item as it could break the whole
  21150. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21151. \\[org-shiftmetaright].
  21152. Also align node properties according to `org-property-format'."
  21153. (interactive)
  21154. (cond
  21155. (orgstruct-is-++
  21156. (let ((indent-line-function
  21157. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21158. (indent-according-to-mode)))
  21159. ((org-at-heading-p) 'noindent)
  21160. (t
  21161. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21162. (type (org-element-type element)))
  21163. (cond ((and (memq type '(plain-list item))
  21164. (= (line-beginning-position)
  21165. (org-element-property :post-affiliated element)))
  21166. 'noindent)
  21167. ((and (eq type 'src-block)
  21168. org-src-tab-acts-natively
  21169. (> (line-beginning-position)
  21170. (org-element-property :post-affiliated element))
  21171. (< (line-beginning-position)
  21172. (org-with-wide-buffer
  21173. (goto-char (org-element-property :end element))
  21174. (skip-chars-backward " \r\t\n")
  21175. (line-beginning-position))))
  21176. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21177. (t
  21178. (let ((column (org--get-expected-indentation element nil)))
  21179. ;; Preserve current column.
  21180. (if (<= (current-column) (current-indentation))
  21181. (indent-line-to column)
  21182. (save-excursion (indent-line-to column))))
  21183. ;; Align node property. Also preserve current column.
  21184. (when (eq type 'node-property)
  21185. (let ((column (current-column)))
  21186. (org--align-node-property)
  21187. (org-move-to-column column)))))))))
  21188. (defun org-indent-region (start end)
  21189. "Indent each non-blank line in the region.
  21190. Called from a program, START and END specify the region to
  21191. indent. The function will not indent contents of example blocks,
  21192. verse blocks and export blocks as leading white spaces are
  21193. assumed to be significant there."
  21194. (interactive "r")
  21195. (save-excursion
  21196. (goto-char start)
  21197. (skip-chars-forward " \r\t\n")
  21198. (unless (eobp) (beginning-of-line))
  21199. (let ((indent-to
  21200. (lambda (ind pos)
  21201. ;; Set IND as indentation for all lines between point and
  21202. ;; POS. Blank lines are ignored. Leave point after POS
  21203. ;; once done.
  21204. (let ((limit (copy-marker pos)))
  21205. (while (< (point) limit)
  21206. (unless (looking-at-p "[ \t]*$") (indent-line-to ind))
  21207. (forward-line))
  21208. (set-marker limit nil))))
  21209. (end (copy-marker end)))
  21210. (while (< (point) end)
  21211. (if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21212. (let* ((element (org-element-at-point))
  21213. (type (org-element-type element))
  21214. (element-end (copy-marker (org-element-property :end element)))
  21215. (ind (org--get-expected-indentation element nil)))
  21216. (cond
  21217. ((or (memq type '(paragraph table table-row))
  21218. (not (or (org-element-property :contents-begin element)
  21219. (memq type
  21220. '(example-block export-block src-block)))))
  21221. ;; Elements here are indented as a single block. Also
  21222. ;; align node properties.
  21223. (when (eq type 'node-property)
  21224. (org--align-node-property)
  21225. (beginning-of-line))
  21226. (funcall indent-to ind (min element-end end)))
  21227. (t
  21228. ;; Elements in this category consist of three parts:
  21229. ;; before the contents, the contents, and after the
  21230. ;; contents. The contents are treated specially,
  21231. ;; according to the element type, or not indented at
  21232. ;; all. Other parts are indented as a single block.
  21233. (let* ((post (copy-marker
  21234. (org-element-property :post-affiliated element)))
  21235. (cbeg
  21236. (copy-marker
  21237. (cond
  21238. ((not (org-element-property :contents-begin element))
  21239. ;; Fake contents for source blocks.
  21240. (org-with-wide-buffer
  21241. (goto-char post)
  21242. (forward-line)
  21243. (point)))
  21244. ((memq type '(footnote-definition item plain-list))
  21245. ;; Contents in these elements could start on
  21246. ;; the same line as the beginning of the
  21247. ;; element. Make sure we start indenting
  21248. ;; from the second line.
  21249. (org-with-wide-buffer
  21250. (goto-char post)
  21251. (end-of-line)
  21252. (skip-chars-forward " \r\t\n")
  21253. (if (eobp) (point) (line-beginning-position))))
  21254. (t (org-element-property :contents-begin element)))))
  21255. (cend (copy-marker
  21256. (or (org-element-property :contents-end element)
  21257. ;; Fake contents for source blocks.
  21258. (org-with-wide-buffer
  21259. (goto-char element-end)
  21260. (skip-chars-backward " \r\t\n")
  21261. (line-beginning-position)))
  21262. t)))
  21263. ;; Do not change items indentation individually as it
  21264. ;; might break the list as a whole. On the other
  21265. ;; hand, when at a plain list, indent it as a whole.
  21266. (cond ((eq type 'plain-list)
  21267. (let ((offset (- ind (org-get-indentation))))
  21268. (unless (zerop offset)
  21269. (indent-rigidly (org-element-property :begin element)
  21270. (org-element-property :end element)
  21271. offset))
  21272. (goto-char cbeg)))
  21273. ((eq type 'item) (goto-char cbeg))
  21274. (t (funcall indent-to ind (min cbeg end))))
  21275. (when (< (point) end)
  21276. (cl-case type
  21277. ((example-block export-block verse-block))
  21278. (src-block
  21279. ;; In a source block, indent source code
  21280. ;; according to language major mode, but only if
  21281. ;; `org-src-tab-acts-natively' is non-nil.
  21282. (when (and (< (point) end) org-src-tab-acts-natively)
  21283. (ignore-errors
  21284. (org-babel-do-in-edit-buffer
  21285. (indent-region (point-min) (point-max))))))
  21286. (t (org-indent-region (point) (min cend end))))
  21287. (goto-char (min cend end))
  21288. (when (< (point) end)
  21289. (funcall indent-to ind (min element-end end))))
  21290. (set-marker post nil)
  21291. (set-marker cbeg nil)
  21292. (set-marker cend nil))))
  21293. (set-marker element-end nil))))
  21294. (set-marker end nil))))
  21295. (defun org-indent-drawer ()
  21296. "Indent the drawer at point."
  21297. (interactive)
  21298. (unless (save-excursion
  21299. (beginning-of-line)
  21300. (looking-at-p org-drawer-regexp))
  21301. (user-error "Not at a drawer"))
  21302. (let ((element (org-element-at-point)))
  21303. (unless (memq (org-element-type element) '(drawer property-drawer))
  21304. (user-error "Not at a drawer"))
  21305. (org-with-wide-buffer
  21306. (org-indent-region (org-element-property :begin element)
  21307. (org-element-property :end element))))
  21308. (message "Drawer at point indented"))
  21309. (defun org-indent-block ()
  21310. "Indent the block at point."
  21311. (interactive)
  21312. (unless (save-excursion
  21313. (beginning-of-line)
  21314. (let ((case-fold-search t))
  21315. (looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21316. (user-error "Not at a block"))
  21317. (let ((element (org-element-at-point)))
  21318. (unless (memq (org-element-type element)
  21319. '(comment-block center-block dynamic-block example-block
  21320. export-block quote-block special-block
  21321. src-block verse-block))
  21322. (user-error "Not at a block"))
  21323. (org-with-wide-buffer
  21324. (org-indent-region (org-element-property :begin element)
  21325. (org-element-property :end element))))
  21326. (message "Block at point indented"))
  21327. ;;; Filling
  21328. ;; We use our own fill-paragraph and auto-fill functions.
  21329. ;; `org-fill-paragraph' relies on adaptive filling and context
  21330. ;; checking. Appropriate `fill-prefix' is computed with
  21331. ;; `org-adaptive-fill-function'.
  21332. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21333. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21334. ;; `org-adaptive-fill-function' value. Internally,
  21335. ;; `org-comment-line-break-function' breaks the line.
  21336. ;; `org-setup-filling' installs filling and auto-filling related
  21337. ;; variables during `org-mode' initialization.
  21338. (defvar org-element-paragraph-separate) ; org-element.el
  21339. (defun org-setup-filling ()
  21340. (require 'org-element)
  21341. ;; Prevent auto-fill from inserting unwanted new items.
  21342. (when (boundp 'fill-nobreak-predicate)
  21343. (setq-local
  21344. fill-nobreak-predicate
  21345. (org-uniquify
  21346. (append fill-nobreak-predicate
  21347. '(org-fill-line-break-nobreak-p
  21348. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21349. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21350. (setq-local paragraph-start paragraph-ending)
  21351. (setq-local paragraph-separate paragraph-ending))
  21352. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21353. (setq-local auto-fill-inhibit-regexp nil)
  21354. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21355. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21356. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21357. (defun org-fill-line-break-nobreak-p ()
  21358. "Non-nil when a new line at point would create an Org line break."
  21359. (save-excursion
  21360. (skip-chars-backward "[ \t]")
  21361. (skip-chars-backward "\\\\")
  21362. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21363. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21364. "Non-nil when a new line at point would split a timestamp."
  21365. (and (org-at-timestamp-p t)
  21366. (not (looking-at org-ts-regexp-both))))
  21367. (declare-function message-in-body-p "message" ())
  21368. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21369. (defun org-adaptive-fill-function ()
  21370. "Compute a fill prefix for the current line.
  21371. Return fill prefix, as a string, or nil if current line isn't
  21372. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21373. matches in paragraphs or comments, use it."
  21374. (catch 'exit
  21375. (when (derived-mode-p 'message-mode)
  21376. (save-excursion
  21377. (beginning-of-line)
  21378. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21379. ((looking-at-p org-table-line-regexp) (throw 'exit nil))
  21380. ((looking-at message-cite-prefix-regexp)
  21381. (throw 'exit (match-string-no-properties 0)))
  21382. ((looking-at org-outline-regexp)
  21383. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21384. (org-with-wide-buffer
  21385. (unless (org-at-heading-p)
  21386. (let* ((p (line-beginning-position))
  21387. (element (save-excursion
  21388. (beginning-of-line)
  21389. (org-element-at-point)))
  21390. (type (org-element-type element))
  21391. (post-affiliated (org-element-property :post-affiliated element)))
  21392. (unless (< p post-affiliated)
  21393. (cl-case type
  21394. (comment
  21395. (save-excursion
  21396. (beginning-of-line)
  21397. (looking-at "[ \t]*")
  21398. (concat (match-string 0) "# ")))
  21399. (footnote-definition "")
  21400. ((item plain-list)
  21401. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21402. (paragraph
  21403. ;; Fill prefix is usually the same as the current line,
  21404. ;; unless the paragraph is at the beginning of an item.
  21405. (let ((parent (org-element-property :parent element)))
  21406. (save-excursion
  21407. (beginning-of-line)
  21408. (cond ((eq (org-element-type parent) 'item)
  21409. (make-string (org-list-item-body-column
  21410. (org-element-property :begin parent))
  21411. ?\s))
  21412. ((and adaptive-fill-regexp
  21413. ;; Locally disable
  21414. ;; `adaptive-fill-function' to let
  21415. ;; `fill-context-prefix' handle
  21416. ;; `adaptive-fill-regexp' variable.
  21417. (let (adaptive-fill-function)
  21418. (fill-context-prefix
  21419. post-affiliated
  21420. (org-element-property :end element)))))
  21421. ((looking-at "[ \t]+") (match-string 0))
  21422. (t "")))))
  21423. (comment-block
  21424. ;; Only fill contents if P is within block boundaries.
  21425. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21426. (forward-line)
  21427. (point)))
  21428. (cend (save-excursion
  21429. (goto-char (org-element-property :end element))
  21430. (skip-chars-backward " \r\t\n")
  21431. (line-beginning-position))))
  21432. (when (and (>= p cbeg) (< p cend))
  21433. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21434. (match-string 0)
  21435. "")))))))))))
  21436. (declare-function message-goto-body "message" ())
  21437. (defvar message-cite-prefix-regexp) ; From message.el
  21438. (defun org-fill-paragraph (&optional justify)
  21439. "Fill element at point, when applicable.
  21440. This function only applies to comment blocks, comments, example
  21441. blocks and paragraphs. Also, as a special case, re-align table
  21442. when point is at one.
  21443. If JUSTIFY is non-nil (interactively, with prefix argument),
  21444. justify as well. If `sentence-end-double-space' is non-nil, then
  21445. period followed by one space does not end a sentence, so don't
  21446. break a line there. The variable `fill-column' controls the
  21447. width for filling.
  21448. For convenience, when point is at a plain list, an item or
  21449. a footnote definition, try to fill the first paragraph within."
  21450. (interactive)
  21451. (if (and (derived-mode-p 'message-mode)
  21452. (or (not (message-in-body-p))
  21453. (save-excursion (move-beginning-of-line 1)
  21454. (looking-at message-cite-prefix-regexp))))
  21455. ;; First ensure filling is correct in message-mode.
  21456. (let ((fill-paragraph-function
  21457. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21458. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21459. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21460. (paragraph-separate
  21461. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21462. (fill-paragraph nil))
  21463. (with-syntax-table org-mode-transpose-word-syntax-table
  21464. ;; Move to end of line in order to get the first paragraph
  21465. ;; within a plain list or a footnote definition.
  21466. (let ((element (save-excursion
  21467. (end-of-line)
  21468. (or (ignore-errors (org-element-at-point))
  21469. (user-error "An element cannot be parsed line %d"
  21470. (line-number-at-pos (point)))))))
  21471. ;; First check if point is in a blank line at the beginning of
  21472. ;; the buffer. In that case, ignore filling.
  21473. (cl-case (org-element-type element)
  21474. ;; Use major mode filling function is src blocks.
  21475. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21476. ;; Align Org tables, leave table.el tables as-is.
  21477. (table-row (org-table-align) t)
  21478. (table
  21479. (when (eq (org-element-property :type element) 'org)
  21480. (save-excursion
  21481. (goto-char (org-element-property :post-affiliated element))
  21482. (org-table-align)))
  21483. t)
  21484. (paragraph
  21485. ;; Paragraphs may contain `line-break' type objects.
  21486. (let ((beg (max (point-min)
  21487. (org-element-property :contents-begin element)))
  21488. (end (min (point-max)
  21489. (org-element-property :contents-end element))))
  21490. ;; Do nothing if point is at an affiliated keyword.
  21491. (if (< (line-end-position) beg) t
  21492. (when (derived-mode-p 'message-mode)
  21493. ;; In `message-mode', do not fill following citation
  21494. ;; in current paragraph nor text before message body.
  21495. (let ((body-start (save-excursion (message-goto-body))))
  21496. (when body-start (setq beg (max body-start beg))))
  21497. (when (save-excursion
  21498. (re-search-forward
  21499. (concat "^" message-cite-prefix-regexp) end t))
  21500. (setq end (match-beginning 0))))
  21501. ;; Fill paragraph, taking line breaks into account.
  21502. (save-excursion
  21503. (goto-char beg)
  21504. (let ((cuts (list beg)))
  21505. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21506. (when (eq 'line-break
  21507. (org-element-type
  21508. (save-excursion (backward-char)
  21509. (org-element-context))))
  21510. (push (point) cuts)))
  21511. (dolist (c (delq end cuts))
  21512. (fill-region-as-paragraph c end justify)
  21513. (setq end c))))
  21514. t)))
  21515. ;; Contents of `comment-block' type elements should be
  21516. ;; filled as plain text, but only if point is within block
  21517. ;; markers.
  21518. (comment-block
  21519. (let* ((case-fold-search t)
  21520. (beg (save-excursion
  21521. (goto-char (org-element-property :begin element))
  21522. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21523. (forward-line)
  21524. (point)))
  21525. (end (save-excursion
  21526. (goto-char (org-element-property :end element))
  21527. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21528. (line-beginning-position))))
  21529. (if (or (< (point) beg) (> (point) end)) t
  21530. (fill-region-as-paragraph
  21531. (save-excursion (end-of-line)
  21532. (re-search-backward "^[ \t]*$" beg 'move)
  21533. (line-beginning-position))
  21534. (save-excursion (beginning-of-line)
  21535. (re-search-forward "^[ \t]*$" end 'move)
  21536. (line-beginning-position))
  21537. justify))))
  21538. ;; Fill comments.
  21539. (comment
  21540. (let ((begin (org-element-property :post-affiliated element))
  21541. (end (org-element-property :end element)))
  21542. (when (and (>= (point) begin) (<= (point) end))
  21543. (let ((begin (save-excursion
  21544. (end-of-line)
  21545. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21546. (progn (forward-line) (point))
  21547. begin)))
  21548. (end (save-excursion
  21549. (end-of-line)
  21550. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21551. (1- (line-beginning-position))
  21552. (skip-chars-backward " \r\t\n")
  21553. (line-end-position)))))
  21554. ;; Do not fill comments when at a blank line.
  21555. (when (> end begin)
  21556. (let ((fill-prefix
  21557. (save-excursion
  21558. (beginning-of-line)
  21559. (looking-at "[ \t]*#")
  21560. (let ((comment-prefix (match-string 0)))
  21561. (goto-char (match-end 0))
  21562. (if (looking-at adaptive-fill-regexp)
  21563. (concat comment-prefix (match-string 0))
  21564. (concat comment-prefix " "))))))
  21565. (save-excursion
  21566. (fill-region-as-paragraph begin end justify))))))
  21567. t))
  21568. ;; Ignore every other element.
  21569. (otherwise t))))))
  21570. (defun org-auto-fill-function ()
  21571. "Auto-fill function."
  21572. ;; Check if auto-filling is meaningful.
  21573. (let ((fc (current-fill-column)))
  21574. (when (and fc (> (current-column) fc))
  21575. (let* ((fill-prefix (org-adaptive-fill-function))
  21576. ;; Enforce empty fill prefix, if required. Otherwise, it
  21577. ;; will be computed again.
  21578. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21579. (when fill-prefix (do-auto-fill))))))
  21580. (defun org-comment-line-break-function (&optional soft)
  21581. "Break line at point and indent, continuing comment if within one.
  21582. The inserted newline is marked hard if variable
  21583. `use-hard-newlines' is true, unless optional argument SOFT is
  21584. non-nil."
  21585. (if soft (insert-and-inherit ?\n) (newline 1))
  21586. (save-excursion (forward-char -1) (delete-horizontal-space))
  21587. (delete-horizontal-space)
  21588. (indent-to-left-margin)
  21589. (insert-before-markers-and-inherit fill-prefix))
  21590. ;;; Fixed Width Areas
  21591. (defun org-toggle-fixed-width ()
  21592. "Toggle fixed-width markup.
  21593. Add or remove fixed-width markup on current line, whenever it
  21594. makes sense. Return an error otherwise.
  21595. If a region is active and if it contains only fixed-width areas
  21596. or blank lines, remove all fixed-width markup in it. If the
  21597. region contains anything else, convert all non-fixed-width lines
  21598. to fixed-width ones.
  21599. Blank lines at the end of the region are ignored unless the
  21600. region only contains such lines."
  21601. (interactive)
  21602. (if (not (org-region-active-p))
  21603. ;; No region:
  21604. ;;
  21605. ;; Remove fixed width marker only in a fixed-with element.
  21606. ;;
  21607. ;; Add fixed width maker in paragraphs, in blank lines after
  21608. ;; elements or at the beginning of a headline or an inlinetask,
  21609. ;; and before any one-line elements (e.g., a clock).
  21610. (progn
  21611. (beginning-of-line)
  21612. (let* ((element (org-element-at-point))
  21613. (type (org-element-type element)))
  21614. (cond
  21615. ((and (eq type 'fixed-width)
  21616. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21617. (replace-match
  21618. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21619. ((and (memq type '(babel-call clock comment diary-sexp headline
  21620. horizontal-rule keyword paragraph
  21621. planning))
  21622. (<= (org-element-property :post-affiliated element) (point)))
  21623. (skip-chars-forward " \t")
  21624. (insert ": "))
  21625. ((and (looking-at-p "[ \t]*$")
  21626. (or (eq type 'inlinetask)
  21627. (save-excursion
  21628. (skip-chars-forward " \r\t\n")
  21629. (<= (org-element-property :end element) (point)))))
  21630. (delete-region (point) (line-end-position))
  21631. (org-indent-line)
  21632. (insert ": "))
  21633. (t (user-error "Cannot insert a fixed-width line here")))))
  21634. ;; Region active.
  21635. (let* ((begin (save-excursion
  21636. (goto-char (region-beginning))
  21637. (line-beginning-position)))
  21638. (end (copy-marker
  21639. (save-excursion
  21640. (goto-char (region-end))
  21641. (unless (eolp) (beginning-of-line))
  21642. (if (save-excursion (re-search-backward "\\S-" begin t))
  21643. (progn (skip-chars-backward " \r\t\n") (point))
  21644. (point)))))
  21645. (all-fixed-width-p
  21646. (catch 'not-all-p
  21647. (save-excursion
  21648. (goto-char begin)
  21649. (skip-chars-forward " \r\t\n")
  21650. (when (eobp) (throw 'not-all-p nil))
  21651. (while (< (point) end)
  21652. (let ((element (org-element-at-point)))
  21653. (if (eq (org-element-type element) 'fixed-width)
  21654. (goto-char (org-element-property :end element))
  21655. (throw 'not-all-p nil))))
  21656. t))))
  21657. (if all-fixed-width-p
  21658. (save-excursion
  21659. (goto-char begin)
  21660. (while (< (point) end)
  21661. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21662. (replace-match
  21663. "" nil nil nil
  21664. (if (= (line-end-position) (match-end 0)) 0 1)))
  21665. (forward-line)))
  21666. (let ((min-ind (point-max)))
  21667. ;; Find minimum indentation across all lines.
  21668. (save-excursion
  21669. (goto-char begin)
  21670. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21671. (setq min-ind 0)
  21672. (catch 'zerop
  21673. (while (< (point) end)
  21674. (unless (looking-at-p "[ \t]*$")
  21675. (let ((ind (org-get-indentation)))
  21676. (setq min-ind (min min-ind ind))
  21677. (when (zerop ind) (throw 'zerop t))))
  21678. (forward-line)))))
  21679. ;; Loop over all lines and add fixed-width markup everywhere
  21680. ;; but in fixed-width lines.
  21681. (save-excursion
  21682. (goto-char begin)
  21683. (while (< (point) end)
  21684. (cond
  21685. ((org-at-heading-p)
  21686. (insert ": ")
  21687. (forward-line)
  21688. (while (and (< (point) end) (looking-at-p "[ \t]*$"))
  21689. (insert ":")
  21690. (forward-line)))
  21691. ((looking-at-p "[ \t]*:\\( \\|$\\)")
  21692. (let* ((element (org-element-at-point))
  21693. (element-end (org-element-property :end element)))
  21694. (if (eq (org-element-type element) 'fixed-width)
  21695. (progn (goto-char element-end)
  21696. (skip-chars-backward " \r\t\n")
  21697. (forward-line))
  21698. (let ((limit (min end element-end)))
  21699. (while (< (point) limit)
  21700. (org-move-to-column min-ind t)
  21701. (insert ": ")
  21702. (forward-line))))))
  21703. (t
  21704. (org-move-to-column min-ind t)
  21705. (insert ": ")
  21706. (forward-line)))))))
  21707. (set-marker end nil))))
  21708. ;;; Comments
  21709. ;; Org comments syntax is quite complex. It requires the entire line
  21710. ;; to be just a comment. Also, even with the right syntax at the
  21711. ;; beginning of line, some some elements (i.e. verse-block or
  21712. ;; example-block) don't accept comments. Usual Emacs comment commands
  21713. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21714. ;; Org still relies on `comment-dwim', but cannot trust
  21715. ;; `comment-only-p'. So, `comment-region-function' and
  21716. ;; `uncomment-region-function' both point
  21717. ;; to`org-comment-or-uncomment-region'. Eventually,
  21718. ;; `org-insert-comment' takes care of insertion of comments at the
  21719. ;; beginning of line.
  21720. ;; `org-setup-comments-handling' install comments related variables
  21721. ;; during `org-mode' initialization.
  21722. (defun org-setup-comments-handling ()
  21723. (interactive)
  21724. (setq-local comment-use-syntax nil)
  21725. (setq-local comment-start "# ")
  21726. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21727. (setq-local comment-insert-comment-function 'org-insert-comment)
  21728. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21729. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21730. (defun org-insert-comment ()
  21731. "Insert an empty comment above current line.
  21732. If the line is empty, insert comment at its beginning. When
  21733. point is within a source block, comment according to the related
  21734. major mode."
  21735. (if (let ((element (org-element-at-point)))
  21736. (and (eq (org-element-type element) 'src-block)
  21737. (< (save-excursion
  21738. (goto-char (org-element-property :post-affiliated element))
  21739. (line-end-position))
  21740. (point))
  21741. (> (save-excursion
  21742. (goto-char (org-element-property :end element))
  21743. (skip-chars-backward " \r\t\n")
  21744. (line-beginning-position))
  21745. (point))))
  21746. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21747. (beginning-of-line)
  21748. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21749. (open-line 1))
  21750. (org-indent-line)
  21751. (insert "# ")))
  21752. (defvar comment-empty-lines) ; From newcomment.el.
  21753. (defun org-comment-or-uncomment-region (beg end &rest _)
  21754. "Comment or uncomment each non-blank line in the region.
  21755. Uncomment each non-blank line between BEG and END if it only
  21756. contains commented lines. Otherwise, comment them. If region is
  21757. strictly within a source block, use appropriate comment syntax."
  21758. (if (let ((element (org-element-at-point)))
  21759. (and (eq (org-element-type element) 'src-block)
  21760. (< (save-excursion
  21761. (goto-char (org-element-property :post-affiliated element))
  21762. (line-end-position))
  21763. beg)
  21764. (>= (save-excursion
  21765. (goto-char (org-element-property :end element))
  21766. (skip-chars-backward " \r\t\n")
  21767. (line-beginning-position))
  21768. end)))
  21769. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21770. (save-restriction
  21771. ;; Restrict region
  21772. (narrow-to-region (save-excursion (goto-char beg)
  21773. (skip-chars-forward " \r\t\n" end)
  21774. (line-beginning-position))
  21775. (save-excursion (goto-char end)
  21776. (skip-chars-backward " \r\t\n" beg)
  21777. (line-end-position)))
  21778. (let ((uncommentp
  21779. ;; UNCOMMENTP is non-nil when every non blank line between
  21780. ;; BEG and END is a comment.
  21781. (save-excursion
  21782. (goto-char (point-min))
  21783. (while (and (not (eobp))
  21784. (let ((element (org-element-at-point)))
  21785. (and (eq (org-element-type element) 'comment)
  21786. (goto-char (min (point-max)
  21787. (org-element-property
  21788. :end element)))))))
  21789. (eobp))))
  21790. (if uncommentp
  21791. ;; Only blank lines and comments in region: uncomment it.
  21792. (save-excursion
  21793. (goto-char (point-min))
  21794. (while (not (eobp))
  21795. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21796. (replace-match "" nil nil nil 1))
  21797. (forward-line)))
  21798. ;; Comment each line in region.
  21799. (let ((min-indent (point-max)))
  21800. ;; First find the minimum indentation across all lines.
  21801. (save-excursion
  21802. (goto-char (point-min))
  21803. (while (and (not (eobp)) (not (zerop min-indent)))
  21804. (unless (looking-at "[ \t]*$")
  21805. (setq min-indent (min min-indent (current-indentation))))
  21806. (forward-line)))
  21807. ;; Then loop over all lines.
  21808. (save-excursion
  21809. (goto-char (point-min))
  21810. (while (not (eobp))
  21811. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21812. ;; Don't get fooled by invisible text (e.g. link path)
  21813. ;; when moving to column MIN-INDENT.
  21814. (let ((buffer-invisibility-spec nil))
  21815. (org-move-to-column min-indent t))
  21816. (insert comment-start))
  21817. (forward-line)))))))))
  21818. (defun org-comment-dwim (_arg)
  21819. "Call `comment-dwim' within a source edit buffer if needed."
  21820. (interactive "*P")
  21821. (if (org-in-src-block-p)
  21822. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21823. (call-interactively 'comment-dwim)))
  21824. ;;; Timestamps API
  21825. ;; This section contains tools to operate on timestamp objects, as
  21826. ;; returned by, e.g. `org-element-context'.
  21827. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21828. "Encode TIMESTAMP object into Emacs internal time.
  21829. Use end of date range or time range when END is non-nil."
  21830. (apply #'encode-time
  21831. (cons 0
  21832. (mapcar
  21833. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21834. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21835. '(:minute-start :hour-start :day-start :month-start
  21836. :year-start))))))
  21837. (defun org-timestamp-has-time-p (timestamp)
  21838. "Non-nil when TIMESTAMP has a time specified."
  21839. (org-element-property :hour-start timestamp))
  21840. (defun org-timestamp-format (timestamp format &optional end utc)
  21841. "Format a TIMESTAMP object into a string.
  21842. FORMAT is a format specifier to be passed to
  21843. `format-time-string'.
  21844. When optional argument END is non-nil, use end of date-range or
  21845. time-range, if possible.
  21846. When optional argument UTC is non-nil, time will be expressed as
  21847. Universal Time."
  21848. (format-time-string
  21849. format (org-timestamp--to-internal-time timestamp end)
  21850. (and utc t)))
  21851. (defun org-timestamp-split-range (timestamp &optional end)
  21852. "Extract a TIMESTAMP object from a date or time range.
  21853. END, when non-nil, means extract the end of the range.
  21854. Otherwise, extract its start.
  21855. Return a new timestamp object."
  21856. (let ((type (org-element-property :type timestamp)))
  21857. (if (memq type '(active inactive diary)) timestamp
  21858. (let ((split-ts (org-element-copy timestamp)))
  21859. ;; Set new type.
  21860. (org-element-put-property
  21861. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21862. ;; Copy start properties over end properties if END is
  21863. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21864. (let ((p-alist '((:minute-start . :minute-end)
  21865. (:hour-start . :hour-end)
  21866. (:day-start . :day-end)
  21867. (:month-start . :month-end)
  21868. (:year-start . :year-end))))
  21869. (dolist (p-cell p-alist)
  21870. (org-element-put-property
  21871. split-ts
  21872. (funcall (if end #'car #'cdr) p-cell)
  21873. (org-element-property
  21874. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21875. ;; Eventually refresh `:raw-value'.
  21876. (org-element-put-property split-ts :raw-value nil)
  21877. (org-element-put-property
  21878. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21879. (defun org-timestamp-translate (timestamp &optional boundary)
  21880. "Translate TIMESTAMP object to custom format.
  21881. Format string is defined in `org-time-stamp-custom-formats',
  21882. which see.
  21883. When optional argument BOUNDARY is non-nil, it is either the
  21884. symbol `start' or `end'. In this case, only translate the
  21885. starting or ending part of TIMESTAMP if it is a date or time
  21886. range. Otherwise, translate both parts.
  21887. Return timestamp as-is if `org-display-custom-times' is nil or if
  21888. it has a `diary' type."
  21889. (let ((type (org-element-property :type timestamp)))
  21890. (if (or (not org-display-custom-times) (eq type 'diary))
  21891. (org-element-interpret-data timestamp)
  21892. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21893. org-time-stamp-custom-formats)))
  21894. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21895. (concat (org-timestamp-format timestamp fmt)
  21896. "--"
  21897. (org-timestamp-format timestamp fmt t))
  21898. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21899. ;;; Other stuff.
  21900. (defvar reftex-docstruct-symbol)
  21901. (defvar reftex-cite-format)
  21902. (defvar org--rds)
  21903. (defun org-reftex-citation ()
  21904. "Use reftex-citation to insert a citation into the buffer.
  21905. This looks for a line like
  21906. #+BIBLIOGRAPHY: foo plain option:-d
  21907. and derives from it that foo.bib is the bibliography file relevant
  21908. for this document. It then installs the necessary environment for RefTeX
  21909. to work in this buffer and calls `reftex-citation' to insert a citation
  21910. into the buffer.
  21911. Export of such citations to both LaTeX and HTML is handled by the contributed
  21912. package ox-bibtex by Taru Karttunen."
  21913. (interactive)
  21914. (let ((reftex-docstruct-symbol 'org--rds)
  21915. (reftex-cite-format "\\cite{%l}")
  21916. org--rds bib)
  21917. (save-excursion
  21918. (save-restriction
  21919. (widen)
  21920. (let ((case-fold-search t)
  21921. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21922. (if (not (save-excursion
  21923. (or (re-search-forward re nil t)
  21924. (re-search-backward re nil t))))
  21925. (user-error "No bibliography defined in file")
  21926. (setq bib (concat (match-string 1) ".bib")
  21927. org--rds (list (list 'bib bib)))))))
  21928. (call-interactively 'reftex-citation)))
  21929. ;;;; Functions extending outline functionality
  21930. (defun org-beginning-of-line (&optional arg)
  21931. "Go to the beginning of the current line. If that is invisible, continue
  21932. to a visible line beginning. This makes the function of C-a more intuitive.
  21933. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21934. first attempt, and only move to after the tags when the cursor is already
  21935. beyond the end of the headline."
  21936. (interactive "P")
  21937. (let ((pos (point))
  21938. (special (if (consp org-special-ctrl-a/e)
  21939. (car org-special-ctrl-a/e)
  21940. org-special-ctrl-a/e))
  21941. deactivate-mark refpos)
  21942. (call-interactively (if (org-bound-and-true-p visual-line-mode)
  21943. #'beginning-of-visual-line
  21944. #'move-beginning-of-line))
  21945. (cond
  21946. ((or arg (not special)))
  21947. ((and (looking-at org-complex-heading-regexp)
  21948. (eq (char-after (match-end 1)) ?\s))
  21949. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21950. (point-at-eol)))
  21951. (goto-char
  21952. (if (eq special t)
  21953. (cond ((> pos refpos) refpos)
  21954. ((= pos (point)) refpos)
  21955. (t (point)))
  21956. (cond ((> pos (point)) (point))
  21957. ((not (eq last-command this-command)) (point))
  21958. (t refpos)))))
  21959. ((org-at-item-p)
  21960. ;; Being at an item and not looking at an the item means point
  21961. ;; was previously moved to beginning of a visual line, which
  21962. ;; doesn't contain the item. Therefore, do nothing special,
  21963. ;; just stay here.
  21964. (when (looking-at org-list-full-item-re)
  21965. ;; Set special position at first white space character after
  21966. ;; bullet, and check-box, if any.
  21967. (let ((after-bullet
  21968. (let ((box (match-end 3)))
  21969. (if (not box) (match-end 1)
  21970. (let ((after (char-after box)))
  21971. (if (and after (= after ? )) (1+ box) box))))))
  21972. ;; Special case: Move point to special position when
  21973. ;; currently after it or at beginning of line.
  21974. (if (eq special t)
  21975. (when (or (> pos after-bullet) (= (point) pos))
  21976. (goto-char after-bullet))
  21977. ;; Reversed case: Move point to special position when
  21978. ;; point was already at beginning of line and command is
  21979. ;; repeated.
  21980. (when (and (= (point) pos) (eq last-command this-command))
  21981. (goto-char after-bullet))))))))
  21982. (setq disable-point-adjustment
  21983. (or (not (invisible-p (point)))
  21984. (not (invisible-p (max (point-min) (1- (point))))))))
  21985. (defun org-end-of-line (&optional arg)
  21986. "Go to the end of the line.
  21987. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21988. tags on the first attempt, and only move to after the tags when
  21989. the cursor is already beyond the end of the headline."
  21990. (interactive "P")
  21991. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21992. org-special-ctrl-a/e))
  21993. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21994. 'end-of-visual-line)
  21995. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21996. (t 'end-of-line)))
  21997. deactivate-mark)
  21998. (if (or (not special) arg) (call-interactively move-fun)
  21999. (let* ((element (save-excursion (beginning-of-line)
  22000. (org-element-at-point)))
  22001. (type (org-element-type element)))
  22002. (cond
  22003. ((memq type '(headline inlinetask))
  22004. (let ((pos (point)))
  22005. (beginning-of-line 1)
  22006. (if (looking-at ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$")
  22007. (if (eq special t)
  22008. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  22009. (goto-char (match-beginning 1))
  22010. (goto-char (match-end 0)))
  22011. (if (or (< pos (match-end 0))
  22012. (not (eq this-command last-command)))
  22013. (goto-char (match-end 0))
  22014. (goto-char (match-beginning 1))))
  22015. (call-interactively move-fun))))
  22016. ((outline-invisible-p (line-end-position))
  22017. ;; If element is hidden, `move-end-of-line' would put point
  22018. ;; after it. Use `end-of-line' to stay on current line.
  22019. (call-interactively 'end-of-line))
  22020. (t (call-interactively move-fun))))))
  22021. (setq disable-point-adjustment
  22022. (or (not (invisible-p (point)))
  22023. (not (invisible-p (max (point-min) (1- (point))))))))
  22024. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22025. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22026. (defun org-backward-sentence (&optional _arg)
  22027. "Go to beginning of sentence, or beginning of table field.
  22028. This will call `backward-sentence' or `org-table-beginning-of-field',
  22029. depending on context."
  22030. (interactive)
  22031. (let* ((element (org-element-at-point))
  22032. (contents-begin (org-element-property :contents-begin element))
  22033. (table (org-element-lineage element '(table) t)))
  22034. (if (and table
  22035. (> (point) contents-begin)
  22036. (<= (point) (org-element-property :contents-end table)))
  22037. (call-interactively #'org-table-beginning-of-field)
  22038. (save-restriction
  22039. (when (and contents-begin
  22040. (< (point-min) contents-begin)
  22041. (> (point) contents-begin))
  22042. (narrow-to-region contents-begin
  22043. (org-element-property :contents-end element)))
  22044. (call-interactively #'backward-sentence)))))
  22045. (defun org-forward-sentence (&optional _arg)
  22046. "Go to end of sentence, or end of table field.
  22047. This will call `forward-sentence' or `org-table-end-of-field',
  22048. depending on context."
  22049. (interactive)
  22050. (let* ((element (org-element-at-point))
  22051. (contents-end (org-element-property :contents-end element))
  22052. (table (org-element-lineage element '(table) t)))
  22053. (if (and table
  22054. (>= (point) (org-element-property :contents-begin table))
  22055. (< (point) contents-end))
  22056. (call-interactively #'org-table-end-of-field)
  22057. (save-restriction
  22058. (when (and contents-end
  22059. (> (point-max) contents-end)
  22060. ;; Skip blank lines between elements.
  22061. (< (org-element-property :end element)
  22062. (save-excursion (goto-char contents-end)
  22063. (skip-chars-forward " \r\t\n"))))
  22064. (narrow-to-region (org-element-property :contents-begin element)
  22065. contents-end))
  22066. (call-interactively #'forward-sentence)))))
  22067. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22068. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22069. (defun org-kill-line (&optional _arg)
  22070. "Kill line, to tags or end of line."
  22071. (interactive)
  22072. (cond
  22073. ((or (not org-special-ctrl-k)
  22074. (bolp)
  22075. (not (org-at-heading-p)))
  22076. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22077. org-ctrl-k-protect-subtree
  22078. (or (eq org-ctrl-k-protect-subtree 'error)
  22079. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22080. (user-error "C-k aborted as it would kill a hidden subtree"))
  22081. (call-interactively
  22082. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22083. ((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
  22084. (kill-region (point) (match-beginning 1))
  22085. (org-set-tags nil t))
  22086. (t (kill-region (point) (point-at-eol)))))
  22087. (define-key org-mode-map "\C-k" 'org-kill-line)
  22088. (defun org-yank (&optional arg)
  22089. "Yank. If the kill is a subtree, treat it specially.
  22090. This command will look at the current kill and check if is a single
  22091. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22092. cursor is at the beginning of a line or after the stars of a currently
  22093. empty headline, then the yank is handled specially. How exactly depends
  22094. on the value of the following variables.
  22095. `org-yank-folded-subtrees'
  22096. By default, this variable is non-nil, which results in subtree(s)
  22097. being folded after insertion, but only if doing so would now
  22098. swallow text after the yanked text.
  22099. `org-yank-adjusted-subtrees'
  22100. When non-nil (the default value is nil), the subtree will be
  22101. promoted or demoted in order to fit into the local outline tree
  22102. structure, which means that the level will be adjusted so that it
  22103. becomes the smaller one of the two *visible* surrounding headings.
  22104. Any prefix to this command will cause `yank' to be called directly with
  22105. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22106. will just
  22107. plainly yank the text as it is.
  22108. \[1] The test checks if the first non-white line is a heading
  22109. and if there are no other headings with fewer stars."
  22110. (interactive "P")
  22111. (org-yank-generic 'yank arg))
  22112. (defun org-yank-generic (command arg)
  22113. "Perform some yank-like command.
  22114. This function implements the behavior described in the `org-yank'
  22115. documentation. However, it has been generalized to work for any
  22116. interactive command with similar behavior."
  22117. ;; pretend to be command COMMAND
  22118. (setq this-command command)
  22119. (if arg
  22120. (call-interactively command)
  22121. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22122. (and (org-kill-is-subtree-p)
  22123. (or (bolp)
  22124. (and (looking-at "[ \t]*$")
  22125. (string-match
  22126. "\\`\\*+\\'"
  22127. (buffer-substring (point-at-bol) (point)))))))
  22128. swallowp)
  22129. (cond
  22130. ((and subtreep org-yank-folded-subtrees)
  22131. (let ((beg (point))
  22132. end)
  22133. (if (and subtreep org-yank-adjusted-subtrees)
  22134. (org-paste-subtree nil nil 'for-yank)
  22135. (call-interactively command))
  22136. (setq end (point))
  22137. (goto-char beg)
  22138. (when (and (bolp) subtreep
  22139. (not (setq swallowp
  22140. (org-yank-folding-would-swallow-text beg end))))
  22141. (org-with-limited-levels
  22142. (or (looking-at org-outline-regexp)
  22143. (re-search-forward org-outline-regexp-bol end t))
  22144. (while (and (< (point) end) (looking-at org-outline-regexp))
  22145. (outline-hide-subtree)
  22146. (org-cycle-show-empty-lines 'folded)
  22147. (condition-case nil
  22148. (outline-forward-same-level 1)
  22149. (error (goto-char end))))))
  22150. (when swallowp
  22151. (message
  22152. "Inserted text not folded because that would swallow text"))
  22153. (goto-char end)
  22154. (skip-chars-forward " \t\n\r")
  22155. (beginning-of-line 1)
  22156. (push-mark beg 'nomsg)))
  22157. ((and subtreep org-yank-adjusted-subtrees)
  22158. (let ((beg (point-at-bol)))
  22159. (org-paste-subtree nil nil 'for-yank)
  22160. (push-mark beg 'nomsg)))
  22161. (t
  22162. (call-interactively command))))))
  22163. (defun org-yank-folding-would-swallow-text (beg end)
  22164. "Would hide-subtree at BEG swallow any text after END?"
  22165. (let (level)
  22166. (org-with-limited-levels
  22167. (save-excursion
  22168. (goto-char beg)
  22169. (when (or (looking-at org-outline-regexp)
  22170. (re-search-forward org-outline-regexp-bol end t))
  22171. (setq level (org-outline-level)))
  22172. (goto-char end)
  22173. (skip-chars-forward " \t\r\n\v\f")
  22174. (not (or (eobp)
  22175. (and (bolp) (looking-at-p org-outline-regexp)
  22176. (<= (org-outline-level) level))))))))
  22177. (define-key org-mode-map "\C-y" 'org-yank)
  22178. (defun org-truely-invisible-p ()
  22179. "Check if point is at a character currently not visible.
  22180. This version does not only check the character property, but also
  22181. `visible-mode'."
  22182. ;; Early versions of noutline don't have `outline-invisible-p'.
  22183. (unless (org-bound-and-true-p visible-mode)
  22184. (outline-invisible-p)))
  22185. (defun org-invisible-p2 ()
  22186. "Check if point is at a character currently not visible."
  22187. (save-excursion
  22188. (when (and (eolp) (not (bobp))) (backward-char 1))
  22189. ;; Early versions of noutline don't have `outline-invisible-p'.
  22190. (outline-invisible-p)))
  22191. (defun org-back-to-heading (&optional invisible-ok)
  22192. "Call `outline-back-to-heading', but provide a better error message."
  22193. (condition-case nil
  22194. (outline-back-to-heading invisible-ok)
  22195. (error (error "Before first headline at position %d in buffer %s"
  22196. (point) (current-buffer)))))
  22197. (defun org-before-first-heading-p ()
  22198. "Before first heading?"
  22199. (save-excursion
  22200. (end-of-line)
  22201. (null (re-search-backward org-outline-regexp-bol nil t))))
  22202. (defun org-at-heading-p (&optional ignored)
  22203. (outline-on-heading-p t))
  22204. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22205. ;;; warning was only added in Org 9.0, which should be taken into
  22206. ;;; account when deciding when to remove the alias.
  22207. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22208. (defun org-in-commented-heading-p (&optional no-inheritance)
  22209. "Non-nil if point is under a commented heading.
  22210. This function also checks ancestors of the current headline,
  22211. unless optional argument NO-INHERITANCE is non-nil."
  22212. (cond
  22213. ((org-before-first-heading-p) nil)
  22214. ((let ((headline (nth 4 (org-heading-components))))
  22215. (and headline
  22216. (let ((case-fold-search nil))
  22217. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22218. headline)))))
  22219. (no-inheritance nil)
  22220. (t
  22221. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22222. (defun org-at-comment-p nil
  22223. "Is cursor in a commented line?"
  22224. (save-excursion
  22225. (save-match-data
  22226. (beginning-of-line)
  22227. (looking-at "^[ \t]*# "))))
  22228. (defun org-at-drawer-p nil
  22229. "Is cursor at a drawer keyword?"
  22230. (save-excursion
  22231. (move-beginning-of-line 1)
  22232. (looking-at org-drawer-regexp)))
  22233. (defun org-at-block-p nil
  22234. "Is cursor at a block keyword?"
  22235. (save-excursion
  22236. (move-beginning-of-line 1)
  22237. (looking-at org-block-regexp)))
  22238. (defun org-point-at-end-of-empty-headline ()
  22239. "If point is at the end of an empty headline, return t, else nil.
  22240. If the heading only contains a TODO keyword, it is still still considered
  22241. empty."
  22242. (and (looking-at "[ \t]*$")
  22243. (when org-todo-line-regexp
  22244. (save-excursion
  22245. (beginning-of-line 1)
  22246. (let ((case-fold-search nil))
  22247. (looking-at org-todo-line-regexp)
  22248. (string= (match-string 3) ""))))))
  22249. (defun org-at-heading-or-item-p ()
  22250. (or (org-at-heading-p) (org-at-item-p)))
  22251. (defun org-at-target-p ()
  22252. (or (org-in-regexp org-radio-target-regexp)
  22253. (org-in-regexp org-target-regexp)))
  22254. ;; Compatibility alias with Org versions < 7.8.03
  22255. (defalias 'org-on-target-p 'org-at-target-p)
  22256. (defun org-up-heading-all (arg)
  22257. "Move to the heading line of which the present line is a subheading.
  22258. This function considers both visible and invisible heading lines.
  22259. With argument, move up ARG levels."
  22260. (if (fboundp 'outline-up-heading-all)
  22261. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22262. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22263. (defun org-up-heading-safe ()
  22264. "Move to the heading line of which the present line is a subheading.
  22265. This version will not throw an error. It will return the level of the
  22266. headline found, or nil if no higher level is found.
  22267. Also, this function will be a lot faster than `outline-up-heading',
  22268. because it relies on stars being the outline starters. This can really
  22269. make a significant difference in outlines with very many siblings."
  22270. (when (ignore-errors (org-back-to-heading t))
  22271. (let ((level-up (1- (funcall outline-level))))
  22272. (and (> level-up 0)
  22273. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22274. (funcall outline-level)))))
  22275. (defun org-first-sibling-p ()
  22276. "Is this heading the first child of its parents?"
  22277. (interactive)
  22278. (let ((re org-outline-regexp-bol)
  22279. level l)
  22280. (unless (org-at-heading-p t)
  22281. (user-error "Not at a heading"))
  22282. (setq level (funcall outline-level))
  22283. (save-excursion
  22284. (if (not (re-search-backward re nil t))
  22285. t
  22286. (setq l (funcall outline-level))
  22287. (< l level)))))
  22288. (defun org-goto-sibling (&optional previous)
  22289. "Goto the next sibling, even if it is invisible.
  22290. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22291. when a sibling was found. When none is found, return nil and don't
  22292. move point."
  22293. (let ((fun (if previous 're-search-backward 're-search-forward))
  22294. (pos (point))
  22295. (re org-outline-regexp-bol)
  22296. level l)
  22297. (when (ignore-errors (org-back-to-heading t))
  22298. (setq level (funcall outline-level))
  22299. (catch 'exit
  22300. (or previous (forward-char 1))
  22301. (while (funcall fun re nil t)
  22302. (setq l (funcall outline-level))
  22303. (when (< l level) (goto-char pos) (throw 'exit nil))
  22304. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22305. (goto-char pos)
  22306. nil))))
  22307. (defun org-show-siblings ()
  22308. "Show all siblings of the current headline."
  22309. (save-excursion
  22310. (while (org-goto-sibling) (org-flag-heading nil)))
  22311. (save-excursion
  22312. (while (org-goto-sibling 'previous)
  22313. (org-flag-heading nil))))
  22314. (defun org-goto-first-child ()
  22315. "Goto the first child, even if it is invisible.
  22316. Return t when a child was found. Otherwise don't move point and
  22317. return nil."
  22318. (let (level (pos (point)) (re org-outline-regexp-bol))
  22319. (when (ignore-errors (org-back-to-heading t))
  22320. (setq level (outline-level))
  22321. (forward-char 1)
  22322. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22323. (progn (goto-char (match-beginning 0)) t)
  22324. (goto-char pos) nil))))
  22325. (defun org-show-hidden-entry ()
  22326. "Show an entry where even the heading is hidden."
  22327. (save-excursion
  22328. (org-show-entry)))
  22329. (defun org-flag-heading (flag &optional entry)
  22330. "Flag the current heading. FLAG non-nil means make invisible.
  22331. When ENTRY is non-nil, show the entire entry."
  22332. (save-excursion
  22333. (org-back-to-heading t)
  22334. ;; Check if we should show the entire entry
  22335. (if entry
  22336. (progn
  22337. (org-show-entry)
  22338. (save-excursion
  22339. (and (outline-next-heading)
  22340. (org-flag-heading nil))))
  22341. (outline-flag-region (max (point-min) (1- (point)))
  22342. (save-excursion (outline-end-of-heading) (point))
  22343. flag))))
  22344. (defun org-get-next-sibling ()
  22345. "Move to next heading of the same level, and return point.
  22346. If there is no such heading, return nil.
  22347. This is like outline-next-sibling, but invisible headings are ok."
  22348. (let ((level (funcall outline-level)))
  22349. (outline-next-heading)
  22350. (while (and (not (eobp)) (> (funcall outline-level) level))
  22351. (outline-next-heading))
  22352. (unless (or (eobp) (< (funcall outline-level) level))
  22353. (point))))
  22354. (defun org-get-last-sibling ()
  22355. "Move to previous heading of the same level, and return point.
  22356. If there is no such heading, return nil."
  22357. (let ((opoint (point))
  22358. (level (funcall outline-level)))
  22359. (outline-previous-heading)
  22360. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22361. (while (and (> (funcall outline-level) level)
  22362. (not (bobp)))
  22363. (outline-previous-heading))
  22364. (unless (< (funcall outline-level) level)
  22365. (point)))))
  22366. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22367. "Goto to the end of a subtree."
  22368. ;; This contains an exact copy of the original function, but it uses
  22369. ;; `org-back-to-heading', to make it work also in invisible
  22370. ;; trees. And is uses an invisible-ok argument.
  22371. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22372. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22373. ;; with many children and grandchildren etc, this can be much faster
  22374. ;; than the outline version.
  22375. (org-back-to-heading invisible-ok)
  22376. (let ((first t)
  22377. (level (funcall outline-level)))
  22378. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22379. ;; A true heading (not a plain list item), in Org-mode
  22380. ;; This means we can easily find the end by looking
  22381. ;; only for the right number of stars. Using a regexp to do
  22382. ;; this is so much faster than using a Lisp loop.
  22383. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22384. (forward-char 1)
  22385. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22386. ;; something else, do it the slow way
  22387. (while (and (not (eobp))
  22388. (or first (> (funcall outline-level) level)))
  22389. (setq first nil)
  22390. (outline-next-heading)))
  22391. (unless to-heading
  22392. (when (memq (preceding-char) '(?\n ?\^M))
  22393. ;; Go to end of line before heading
  22394. (forward-char -1)
  22395. (when (memq (preceding-char) '(?\n ?\^M))
  22396. ;; leave blank line before heading
  22397. (forward-char -1)))))
  22398. (point))
  22399. (defun org-end-of-meta-data (&optional full)
  22400. "Skip planning line and properties drawer in current entry.
  22401. When optional argument FULL is non-nil, also skip empty lines,
  22402. clocking lines and regular drawers at the beginning of the
  22403. entry."
  22404. (org-back-to-heading t)
  22405. (forward-line)
  22406. (when (looking-at-p org-planning-line-re) (forward-line))
  22407. (when (looking-at org-property-drawer-re)
  22408. (goto-char (match-end 0))
  22409. (forward-line))
  22410. (when (and full (not (org-at-heading-p)))
  22411. (catch 'exit
  22412. (let ((end (save-excursion (outline-next-heading) (point)))
  22413. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22414. (while (not (eobp))
  22415. (cond ((looking-at-p org-drawer-regexp)
  22416. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22417. (forward-line)
  22418. (throw 'exit t)))
  22419. ((looking-at-p re) (forward-line))
  22420. (t (throw 'exit t))))))))
  22421. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22422. "Move forward to the ARG'th subheading at same level as this one.
  22423. Stop at the first and last subheadings of a superior heading.
  22424. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22425. non-nil it will also look at invisible ones."
  22426. (interactive "p")
  22427. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22428. (if (and arg (< arg 0))
  22429. (goto-char (point-min))
  22430. (outline-next-heading))
  22431. (org-at-heading-p)
  22432. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22433. (f (if (and arg (< arg 0))
  22434. 're-search-backward
  22435. 're-search-forward))
  22436. (count (if arg (abs arg) 1))
  22437. (result (point)))
  22438. (while (and (prog1 (> count 0)
  22439. (forward-char (if (and arg (< arg 0)) -1 1)))
  22440. (funcall f org-outline-regexp-bol nil 'move))
  22441. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22442. (cond ((< l level) (setq count 0))
  22443. ((and (= l level)
  22444. (or invisible-ok
  22445. (progn
  22446. (goto-char (line-beginning-position))
  22447. (not (outline-invisible-p)))))
  22448. (setq count (1- count))
  22449. (when (eq l level)
  22450. (setq result (point)))))))
  22451. (goto-char result))
  22452. (beginning-of-line 1)))
  22453. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22454. "Move backward to the ARG'th subheading at same level as this one.
  22455. Stop at the first and last subheadings of a superior heading."
  22456. (interactive "p")
  22457. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22458. (defun org-next-visible-heading (arg)
  22459. "Move to the next visible heading.
  22460. This function wraps `outline-next-visible-heading' with
  22461. `org-with-limited-levels' in order to skip over inline tasks and
  22462. respect customization of `org-odd-levels-only'."
  22463. (interactive "p")
  22464. (org-with-limited-levels
  22465. (outline-next-visible-heading arg)))
  22466. (defun org-previous-visible-heading (arg)
  22467. "Move to the previous visible heading.
  22468. This function wraps `outline-previous-visible-heading' with
  22469. `org-with-limited-levels' in order to skip over inline tasks and
  22470. respect customization of `org-odd-levels-only'."
  22471. (interactive "p")
  22472. (org-with-limited-levels
  22473. (outline-previous-visible-heading arg)))
  22474. (defun org-next-block (arg &optional backward block-regexp)
  22475. "Jump to the next block.
  22476. With a prefix argument ARG, jump forward ARG many blocks.
  22477. When BACKWARD is non-nil, jump to the previous block.
  22478. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22479. Match data is set according to this regexp when the function
  22480. returns.
  22481. Return point at beginning of the opening line of found block.
  22482. Throw an error if no block is found."
  22483. (interactive "p")
  22484. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22485. (case-fold-search t)
  22486. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22487. (count (or arg 1))
  22488. (origin (point))
  22489. last-element)
  22490. (if backward (beginning-of-line) (end-of-line))
  22491. (while (and (> count 0) (funcall search-fn re nil t))
  22492. (let ((element (save-excursion
  22493. (goto-char (match-beginning 0))
  22494. (save-match-data (org-element-at-point)))))
  22495. (when (and (memq (org-element-type element)
  22496. '(center-block comment-block dynamic-block
  22497. example-block export-block quote-block
  22498. special-block src-block verse-block))
  22499. (<= (match-beginning 0)
  22500. (org-element-property :post-affiliated element)))
  22501. (setq last-element element)
  22502. (cl-decf count))))
  22503. (if (= count 0)
  22504. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22505. (save-match-data (org-show-context)))
  22506. (goto-char origin)
  22507. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22508. (defun org-previous-block (arg &optional block-regexp)
  22509. "Jump to the previous block.
  22510. With a prefix argument ARG, jump backward ARG many source blocks.
  22511. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22512. (interactive "p")
  22513. (org-next-block arg t block-regexp))
  22514. (defun org-forward-paragraph ()
  22515. "Move forward to beginning of next paragraph or equivalent.
  22516. The function moves point to the beginning of the next visible
  22517. structural element, which can be a paragraph, a table, a list
  22518. item, etc. It also provides some special moves for convenience:
  22519. - On an affiliated keyword, jump to the beginning of the
  22520. relative element.
  22521. - On an item or a footnote definition, move to the second
  22522. element inside, if any.
  22523. - On a table or a property drawer, jump after it.
  22524. - On a verse or source block, stop after blank lines."
  22525. (interactive)
  22526. (when (eobp) (user-error "Cannot move further down"))
  22527. (let* ((deactivate-mark nil)
  22528. (element (org-element-at-point))
  22529. (type (org-element-type element))
  22530. (post-affiliated (org-element-property :post-affiliated element))
  22531. (contents-begin (org-element-property :contents-begin element))
  22532. (contents-end (org-element-property :contents-end element))
  22533. (end (let ((end (org-element-property :end element)) (parent element))
  22534. (while (and (setq parent (org-element-property :parent parent))
  22535. (= (org-element-property :contents-end parent) end))
  22536. (setq end (org-element-property :end parent)))
  22537. end)))
  22538. (cond ((not element)
  22539. (skip-chars-forward " \r\t\n")
  22540. (or (eobp) (beginning-of-line)))
  22541. ;; On affiliated keywords, move to element's beginning.
  22542. ((< (point) post-affiliated)
  22543. (goto-char post-affiliated))
  22544. ;; At a table row, move to the end of the table. Similarly,
  22545. ;; at a node property, move to the end of the property
  22546. ;; drawer.
  22547. ((memq type '(node-property table-row))
  22548. (goto-char (org-element-property
  22549. :end (org-element-property :parent element))))
  22550. ((memq type '(property-drawer table)) (goto-char end))
  22551. ;; Consider blank lines as separators in verse and source
  22552. ;; blocks to ease editing.
  22553. ((memq type '(src-block verse-block))
  22554. (when (eq type 'src-block)
  22555. (setq contents-end
  22556. (save-excursion (goto-char end)
  22557. (skip-chars-backward " \r\t\n")
  22558. (line-beginning-position))))
  22559. (beginning-of-line)
  22560. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22561. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22562. (goto-char end)
  22563. (skip-chars-forward " \r\t\n")
  22564. (if (= (point) contents-end) (goto-char end)
  22565. (beginning-of-line))))
  22566. ;; With no contents, just skip element.
  22567. ((not contents-begin) (goto-char end))
  22568. ;; If contents are invisible, skip the element altogether.
  22569. ((outline-invisible-p (line-end-position))
  22570. (cl-case type
  22571. (headline
  22572. (org-with-limited-levels (outline-next-visible-heading 1)))
  22573. ;; At a plain list, make sure we move to the next item
  22574. ;; instead of skipping the whole list.
  22575. (plain-list (forward-char)
  22576. (org-forward-paragraph))
  22577. (otherwise (goto-char end))))
  22578. ((>= (point) contents-end) (goto-char end))
  22579. ((>= (point) contents-begin)
  22580. ;; This can only happen on paragraphs and plain lists.
  22581. (cl-case type
  22582. (paragraph (goto-char end))
  22583. ;; At a plain list, try to move to second element in
  22584. ;; first item, if possible.
  22585. (plain-list (end-of-line)
  22586. (org-forward-paragraph))))
  22587. ;; When contents start on the middle of a line (e.g. in
  22588. ;; items and footnote definitions), try to reach first
  22589. ;; element starting after current line.
  22590. ((> (line-end-position) contents-begin)
  22591. (end-of-line)
  22592. (org-forward-paragraph))
  22593. (t (goto-char contents-begin)))))
  22594. (defun org-backward-paragraph ()
  22595. "Move backward to start of previous paragraph or equivalent.
  22596. The function moves point to the beginning of the current
  22597. structural element, which can be a paragraph, a table, a list
  22598. item, etc., or to the beginning of the previous visible one if
  22599. point is already there. It also provides some special moves for
  22600. convenience:
  22601. - On an affiliated keyword, jump to the first one.
  22602. - On a table or a property drawer, move to its beginning.
  22603. - On a verse or source block, stop before blank lines."
  22604. (interactive)
  22605. (when (bobp) (user-error "Cannot move further up"))
  22606. (let* ((deactivate-mark nil)
  22607. (element (org-element-at-point))
  22608. (type (org-element-type element))
  22609. (contents-begin (org-element-property :contents-begin element))
  22610. (contents-end (org-element-property :contents-end element))
  22611. (post-affiliated (org-element-property :post-affiliated element))
  22612. (begin (org-element-property :begin element)))
  22613. (cond
  22614. ((not element) (goto-char (point-min)))
  22615. ((= (point) begin)
  22616. (backward-char)
  22617. (org-backward-paragraph))
  22618. ((<= (point) post-affiliated) (goto-char begin))
  22619. ((memq type '(node-property table-row))
  22620. (goto-char (org-element-property
  22621. :post-affiliated (org-element-property :parent element))))
  22622. ((memq type '(property-drawer table)) (goto-char begin))
  22623. ((memq type '(src-block verse-block))
  22624. (when (eq type 'src-block)
  22625. (setq contents-begin
  22626. (save-excursion (goto-char begin) (forward-line) (point))))
  22627. (if (= (point) contents-begin) (goto-char post-affiliated)
  22628. ;; Inside a verse block, see blank lines as paragraph
  22629. ;; separators.
  22630. (let ((origin (point)))
  22631. (skip-chars-backward " \r\t\n" contents-begin)
  22632. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22633. (skip-chars-forward " \r\t\n" origin)
  22634. (if (= (point) origin) (goto-char contents-begin)
  22635. (beginning-of-line))))))
  22636. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22637. ((eq type 'paragraph)
  22638. (goto-char contents-begin)
  22639. ;; When at first paragraph in an item or a footnote definition,
  22640. ;; move directly to beginning of line.
  22641. (let ((parent-contents
  22642. (org-element-property
  22643. :contents-begin (org-element-property :parent element))))
  22644. (when (and parent-contents (= parent-contents contents-begin))
  22645. (beginning-of-line))))
  22646. ;; At the end of a greater element, move to the beginning of the
  22647. ;; last element within.
  22648. ((>= (point) contents-end)
  22649. (goto-char (1- contents-end))
  22650. (org-backward-paragraph))
  22651. (t (goto-char (or post-affiliated begin))))
  22652. ;; Ensure we never leave point invisible.
  22653. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22654. (defun org-forward-element ()
  22655. "Move forward by one element.
  22656. Move to the next element at the same level, when possible."
  22657. (interactive)
  22658. (cond ((eobp) (user-error "Cannot move further down"))
  22659. ((org-with-limited-levels (org-at-heading-p))
  22660. (let ((origin (point)))
  22661. (goto-char (org-end-of-subtree nil t))
  22662. (unless (org-with-limited-levels (org-at-heading-p))
  22663. (goto-char origin)
  22664. (user-error "Cannot move further down"))))
  22665. (t
  22666. (let* ((elem (org-element-at-point))
  22667. (end (org-element-property :end elem))
  22668. (parent (org-element-property :parent elem)))
  22669. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22670. (goto-char (org-element-property :end parent)))
  22671. ((integer-or-marker-p end) (goto-char end))
  22672. (t (message "No element at point")))))))
  22673. (defun org-backward-element ()
  22674. "Move backward by one element.
  22675. Move to the previous element at the same level, when possible."
  22676. (interactive)
  22677. (cond ((bobp) (user-error "Cannot move further up"))
  22678. ((org-with-limited-levels (org-at-heading-p))
  22679. ;; At a headline, move to the previous one, if any, or stay
  22680. ;; here.
  22681. (let ((origin (point)))
  22682. (org-with-limited-levels (org-backward-heading-same-level 1))
  22683. ;; When current headline has no sibling above, move to its
  22684. ;; parent.
  22685. (when (= (point) origin)
  22686. (or (org-with-limited-levels (org-up-heading-safe))
  22687. (progn (goto-char origin)
  22688. (user-error "Cannot move further up"))))))
  22689. (t
  22690. (let* ((elem (org-element-at-point))
  22691. (beg (org-element-property :begin elem)))
  22692. (cond
  22693. ;; Move to beginning of current element if point isn't
  22694. ;; there already.
  22695. ((null beg) (message "No element at point"))
  22696. ((/= (point) beg) (goto-char beg))
  22697. (t (goto-char beg)
  22698. (skip-chars-backward " \r\t\n")
  22699. (unless (bobp)
  22700. (let ((prev (org-element-at-point)))
  22701. (goto-char (org-element-property :begin prev))
  22702. (while (and (setq prev (org-element-property :parent prev))
  22703. (<= (org-element-property :end prev) beg))
  22704. (goto-char (org-element-property :begin prev)))))))))))
  22705. (defun org-up-element ()
  22706. "Move to upper element."
  22707. (interactive)
  22708. (if (org-with-limited-levels (org-at-heading-p))
  22709. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22710. (let* ((elem (org-element-at-point))
  22711. (parent (org-element-property :parent elem)))
  22712. (if parent (goto-char (org-element-property :begin parent))
  22713. (if (org-with-limited-levels (org-before-first-heading-p))
  22714. (user-error "No surrounding element")
  22715. (org-with-limited-levels (org-back-to-heading)))))))
  22716. (defvar org-element-greater-elements)
  22717. (defun org-down-element ()
  22718. "Move to inner element."
  22719. (interactive)
  22720. (let ((element (org-element-at-point)))
  22721. (cond
  22722. ((memq (org-element-type element) '(plain-list table))
  22723. (goto-char (org-element-property :contents-begin element))
  22724. (forward-char))
  22725. ((memq (org-element-type element) org-element-greater-elements)
  22726. ;; If contents are hidden, first disclose them.
  22727. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22728. (goto-char (or (org-element-property :contents-begin element)
  22729. (user-error "No content for this element"))))
  22730. (t (user-error "No inner element")))))
  22731. (defun org-drag-element-backward ()
  22732. "Move backward element at point."
  22733. (interactive)
  22734. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22735. (let* ((elem (org-element-at-point))
  22736. (prev-elem
  22737. (save-excursion
  22738. (goto-char (org-element-property :begin elem))
  22739. (skip-chars-backward " \r\t\n")
  22740. (unless (bobp)
  22741. (let* ((beg (org-element-property :begin elem))
  22742. (prev (org-element-at-point))
  22743. (up prev))
  22744. (while (and (setq up (org-element-property :parent up))
  22745. (<= (org-element-property :end up) beg))
  22746. (setq prev up))
  22747. prev)))))
  22748. ;; Error out if no previous element or previous element is
  22749. ;; a parent of the current one.
  22750. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22751. (user-error "Cannot drag element backward")
  22752. (let ((pos (point)))
  22753. (org-element-swap-A-B prev-elem elem)
  22754. (goto-char (+ (org-element-property :begin prev-elem)
  22755. (- pos (org-element-property :begin elem)))))))))
  22756. (defun org-drag-element-forward ()
  22757. "Move forward element at point."
  22758. (interactive)
  22759. (let* ((pos (point))
  22760. (elem (org-element-at-point)))
  22761. (when (= (point-max) (org-element-property :end elem))
  22762. (user-error "Cannot drag element forward"))
  22763. (goto-char (org-element-property :end elem))
  22764. (let ((next-elem (org-element-at-point)))
  22765. (when (or (org-element-nested-p elem next-elem)
  22766. (and (eq (org-element-type next-elem) 'headline)
  22767. (not (eq (org-element-type elem) 'headline))))
  22768. (goto-char pos)
  22769. (user-error "Cannot drag element forward"))
  22770. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22771. ;; body's length (without final blanks) and by the length of
  22772. ;; blanks between ELEM and NEXT-ELEM.
  22773. (let ((size-next (- (save-excursion
  22774. (goto-char (org-element-property :end next-elem))
  22775. (skip-chars-backward " \r\t\n")
  22776. (forward-line)
  22777. ;; Small correction if buffer doesn't end
  22778. ;; with a newline character.
  22779. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22780. (org-element-property :begin next-elem)))
  22781. (size-blank (- (org-element-property :end elem)
  22782. (save-excursion
  22783. (goto-char (org-element-property :end elem))
  22784. (skip-chars-backward " \r\t\n")
  22785. (forward-line)
  22786. (point)))))
  22787. (org-element-swap-A-B elem next-elem)
  22788. (goto-char (+ pos size-next size-blank))))))
  22789. (defun org-drag-line-forward (arg)
  22790. "Drag the line at point ARG lines forward."
  22791. (interactive "p")
  22792. (dotimes (_ (abs arg))
  22793. (let ((c (current-column)))
  22794. (if (< 0 arg)
  22795. (progn
  22796. (beginning-of-line 2)
  22797. (transpose-lines 1)
  22798. (beginning-of-line 0))
  22799. (transpose-lines 1)
  22800. (beginning-of-line -1))
  22801. (org-move-to-column c))))
  22802. (defun org-drag-line-backward (arg)
  22803. "Drag the line at point ARG lines backward."
  22804. (interactive "p")
  22805. (org-drag-line-forward (- arg)))
  22806. (defun org-mark-element ()
  22807. "Put point at beginning of this element, mark at end.
  22808. Interactively, if this command is repeated or (in Transient Mark
  22809. mode) if the mark is active, it marks the next element after the
  22810. ones already marked."
  22811. (interactive)
  22812. (let (deactivate-mark)
  22813. (if (and (org-called-interactively-p 'any)
  22814. (or (and (eq last-command this-command) (mark t))
  22815. (and transient-mark-mode mark-active)))
  22816. (set-mark
  22817. (save-excursion
  22818. (goto-char (mark))
  22819. (goto-char (org-element-property :end (org-element-at-point)))))
  22820. (let ((element (org-element-at-point)))
  22821. (end-of-line)
  22822. (push-mark (org-element-property :end element) t t)
  22823. (goto-char (org-element-property :begin element))))))
  22824. (defun org-narrow-to-element ()
  22825. "Narrow buffer to current element."
  22826. (interactive)
  22827. (let ((elem (org-element-at-point)))
  22828. (cond
  22829. ((eq (car elem) 'headline)
  22830. (narrow-to-region
  22831. (org-element-property :begin elem)
  22832. (org-element-property :end elem)))
  22833. ((memq (car elem) org-element-greater-elements)
  22834. (narrow-to-region
  22835. (org-element-property :contents-begin elem)
  22836. (org-element-property :contents-end elem)))
  22837. (t
  22838. (narrow-to-region
  22839. (org-element-property :begin elem)
  22840. (org-element-property :end elem))))))
  22841. (defun org-transpose-element ()
  22842. "Transpose current and previous elements, keeping blank lines between.
  22843. Point is moved after both elements."
  22844. (interactive)
  22845. (org-skip-whitespace)
  22846. (let ((end (org-element-property :end (org-element-at-point))))
  22847. (org-drag-element-backward)
  22848. (goto-char end)))
  22849. (defun org-unindent-buffer ()
  22850. "Un-indent the visible part of the buffer.
  22851. Relative indentation (between items, inside blocks, etc.) isn't
  22852. modified."
  22853. (interactive)
  22854. (unless (eq major-mode 'org-mode)
  22855. (user-error "Cannot un-indent a buffer not in Org mode"))
  22856. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22857. (unindent-tree
  22858. (lambda (contents)
  22859. (dolist (element (reverse contents))
  22860. (if (memq (org-element-type element) '(headline section))
  22861. (funcall unindent-tree (org-element-contents element))
  22862. (save-excursion
  22863. (save-restriction
  22864. (narrow-to-region
  22865. (org-element-property :begin element)
  22866. (org-element-property :end element))
  22867. (org-do-remove-indentation))))))))
  22868. (funcall unindent-tree (org-element-contents parse-tree))))
  22869. (defun org-show-children (&optional level)
  22870. "Show all direct subheadings of this heading.
  22871. Prefix arg LEVEL is how many levels below the current level
  22872. should be shown. Default is enough to cause the following
  22873. heading to appear."
  22874. (interactive "p")
  22875. ;; If `orgstruct-mode' is active, use the slower version.
  22876. (if orgstruct-mode (call-interactively #'outline-show-children)
  22877. (save-excursion
  22878. (org-back-to-heading t)
  22879. (let* ((current-level (funcall outline-level))
  22880. (max-level (org-get-valid-level
  22881. current-level
  22882. (if level (prefix-numeric-value level) 1)))
  22883. (end (save-excursion (org-end-of-subtree t t)))
  22884. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22885. (past-first-child nil)
  22886. ;; Make sure to skip inlinetasks.
  22887. (re (format regexp-fmt
  22888. current-level
  22889. (cond
  22890. ((not (featurep 'org-inlinetask)) "")
  22891. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22892. 3))
  22893. (t (1- org-inlinetask-min-level))))))
  22894. ;; Display parent heading.
  22895. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22896. (forward-line)
  22897. ;; Display children. First child may be deeper than expected
  22898. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22899. ;; MAX-LEVEL accordingly.
  22900. (while (re-search-forward re end t)
  22901. (unless past-first-child
  22902. (setq re (format regexp-fmt
  22903. current-level
  22904. (max (funcall outline-level) max-level)))
  22905. (setq past-first-child t))
  22906. (outline-flag-region
  22907. (line-end-position 0) (line-end-position) nil))))))
  22908. (defun org-show-subtree ()
  22909. "Show everything after this heading at deeper levels."
  22910. (interactive)
  22911. (outline-flag-region
  22912. (point)
  22913. (save-excursion
  22914. (org-end-of-subtree t t))
  22915. nil))
  22916. (defun org-show-entry ()
  22917. "Show the body directly following this heading.
  22918. Show the heading too, if it is currently invisible."
  22919. (interactive)
  22920. (save-excursion
  22921. (ignore-errors
  22922. (org-back-to-heading t)
  22923. (outline-flag-region
  22924. (max (point-min) (1- (point)))
  22925. (save-excursion
  22926. (if (re-search-forward
  22927. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22928. (match-beginning 1)
  22929. (point-max)))
  22930. nil)
  22931. (org-cycle-hide-drawers 'children))))
  22932. (defun org-make-options-regexp (kwds &optional extra)
  22933. "Make a regular expression for keyword lines.
  22934. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22935. when non-nil, is a regexp matching keywords names."
  22936. (concat "^[ \t]*#\\+\\("
  22937. (regexp-opt kwds)
  22938. (and extra (concat (and kwds "\\|") extra))
  22939. "\\):[ \t]*\\(.*\\)"))
  22940. ;;;; Integration with and fixes for other packages
  22941. ;;; Imenu support
  22942. (defvar-local org-imenu-markers nil
  22943. "All markers currently used by Imenu.")
  22944. (defun org-imenu-new-marker (&optional pos)
  22945. "Return a new marker for use by Imenu, and remember the marker."
  22946. (let ((m (make-marker)))
  22947. (move-marker m (or pos (point)))
  22948. (push m org-imenu-markers)
  22949. m))
  22950. (defun org-imenu-get-tree ()
  22951. "Produce the index for Imenu."
  22952. (dolist (x org-imenu-markers) (move-marker x nil))
  22953. (setq org-imenu-markers nil)
  22954. (let* ((case-fold-search nil)
  22955. (n org-imenu-depth)
  22956. (re (concat "^" (org-get-limited-outline-regexp)))
  22957. (subs (make-vector (1+ n) nil))
  22958. (last-level 0)
  22959. m level head0 head)
  22960. (save-excursion
  22961. (save-restriction
  22962. (widen)
  22963. (goto-char (point-max))
  22964. (while (re-search-backward re nil t)
  22965. (setq level (org-reduced-level (funcall outline-level)))
  22966. (when (and (<= level n)
  22967. (looking-at org-complex-heading-regexp)
  22968. (setq head0 (match-string-no-properties 4)))
  22969. (setq head (org-link-display-format head0)
  22970. m (org-imenu-new-marker))
  22971. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22972. (if (>= level last-level)
  22973. (push (cons head m) (aref subs level))
  22974. (push (cons head (aref subs (1+ level))) (aref subs level))
  22975. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22976. (setq last-level level)))))
  22977. (aref subs 1)))
  22978. (eval-after-load "imenu"
  22979. '(progn
  22980. (add-hook 'imenu-after-jump-hook
  22981. (lambda ()
  22982. (when (derived-mode-p 'org-mode)
  22983. (org-show-context 'org-goto))))))
  22984. (defun org-link-display-format (s)
  22985. "Replace links in string S with their description.
  22986. If there is no description, use the link target."
  22987. (save-match-data
  22988. (replace-regexp-in-string
  22989. org-bracket-link-analytic-regexp
  22990. (lambda (m)
  22991. (if (match-end 5) (match-string 5 m)
  22992. (concat (match-string 1 m) (match-string 3 m))))
  22993. s nil t)))
  22994. (defun org-toggle-link-display ()
  22995. "Toggle the literal or descriptive display of links."
  22996. (interactive)
  22997. (if org-descriptive-links
  22998. (progn (org-remove-from-invisibility-spec '(org-link))
  22999. (org-restart-font-lock)
  23000. (setq org-descriptive-links nil))
  23001. (progn (add-to-invisibility-spec '(org-link))
  23002. (org-restart-font-lock)
  23003. (setq org-descriptive-links t))))
  23004. ;; Speedbar support
  23005. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23006. "Overlay marking the agenda restriction line in speedbar.")
  23007. (overlay-put org-speedbar-restriction-lock-overlay
  23008. 'face 'org-agenda-restriction-lock)
  23009. (overlay-put org-speedbar-restriction-lock-overlay
  23010. 'help-echo "Agendas are currently limited to this item.")
  23011. (delete-overlay org-speedbar-restriction-lock-overlay)
  23012. (defun org-speedbar-set-agenda-restriction ()
  23013. "Restrict future agenda commands to the location at point in speedbar.
  23014. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23015. (interactive)
  23016. (require 'org-agenda)
  23017. (let (p m tp np dir txt)
  23018. (cond
  23019. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23020. 'org-imenu t))
  23021. (setq m (get-text-property p 'org-imenu-marker))
  23022. (with-current-buffer (marker-buffer m)
  23023. (goto-char m)
  23024. (org-agenda-set-restriction-lock 'subtree)))
  23025. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23026. 'speedbar-function 'speedbar-find-file))
  23027. (setq tp (previous-single-property-change
  23028. (1+ p) 'speedbar-function)
  23029. np (next-single-property-change
  23030. tp 'speedbar-function)
  23031. dir (speedbar-line-directory)
  23032. txt (buffer-substring-no-properties (or tp (point-min))
  23033. (or np (point-max))))
  23034. (with-current-buffer (find-file-noselect
  23035. (let ((default-directory dir))
  23036. (expand-file-name txt)))
  23037. (unless (derived-mode-p 'org-mode)
  23038. (user-error "Cannot restrict to non-Org-mode file"))
  23039. (org-agenda-set-restriction-lock 'file)))
  23040. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23041. (move-overlay org-speedbar-restriction-lock-overlay
  23042. (point-at-bol) (point-at-eol))
  23043. (setq current-prefix-arg nil)
  23044. (org-agenda-maybe-redo)))
  23045. (defvar speedbar-file-key-map)
  23046. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23047. (eval-after-load "speedbar"
  23048. '(progn
  23049. (speedbar-add-supported-extension ".org")
  23050. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23051. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23052. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23053. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23054. (add-hook 'speedbar-visiting-tag-hook
  23055. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23056. ;;; Fixes and Hacks for problems with other packages
  23057. (defun org--flyspell-object-check-p (element)
  23058. "Non-nil when Flyspell can check object at point.
  23059. ELEMENT is the element at point."
  23060. (let ((object (save-excursion
  23061. (when (looking-at-p "\\>") (backward-char))
  23062. (org-element-context element))))
  23063. (cl-case (org-element-type object)
  23064. ;; Prevent checks in links due to keybinding conflict with
  23065. ;; Flyspell.
  23066. ((code entity export-snippet inline-babel-call
  23067. inline-src-block line-break latex-fragment link macro
  23068. statistics-cookie target timestamp verbatim)
  23069. nil)
  23070. (footnote-reference
  23071. ;; Only in inline footnotes, within the definition.
  23072. (and (eq (org-element-property :type object) 'inline)
  23073. (< (save-excursion
  23074. (goto-char (org-element-property :begin object))
  23075. (search-forward ":" nil t 2))
  23076. (point))))
  23077. (otherwise t))))
  23078. (defun org-mode-flyspell-verify ()
  23079. "Function used for `flyspell-generic-check-word-predicate'."
  23080. (if (org-at-heading-p)
  23081. ;; At a headline or an inlinetask, check title only. This is
  23082. ;; faster than relying on `org-element-at-point'.
  23083. (and (save-excursion (beginning-of-line)
  23084. (and (let ((case-fold-search t))
  23085. (not (looking-at "\\*+ END[ \t]*$")))
  23086. (looking-at org-complex-heading-regexp)))
  23087. (match-beginning 4)
  23088. (>= (point) (match-beginning 4))
  23089. (or (not (match-beginning 5))
  23090. (< (point) (match-beginning 5))))
  23091. (let* ((element (org-element-at-point))
  23092. (post-affiliated (org-element-property :post-affiliated element)))
  23093. (cond
  23094. ;; Ignore checks in all affiliated keywords but captions.
  23095. ((< (point) post-affiliated)
  23096. (and (save-excursion
  23097. (beginning-of-line)
  23098. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23099. (> (point) (match-end 0))
  23100. (org--flyspell-object-check-p element)))
  23101. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23102. ((let ((log (org-log-into-drawer)))
  23103. (and log
  23104. (let ((drawer (org-element-lineage element '(drawer))))
  23105. (and drawer
  23106. (eq (compare-strings
  23107. log nil nil
  23108. (org-element-property :drawer-name drawer) nil nil t)
  23109. t)))))
  23110. nil)
  23111. (t
  23112. (cl-case (org-element-type element)
  23113. ((comment quote-section) t)
  23114. (comment-block
  23115. ;; Allow checks between block markers, not on them.
  23116. (and (> (line-beginning-position) post-affiliated)
  23117. (save-excursion
  23118. (end-of-line)
  23119. (skip-chars-forward " \r\t\n")
  23120. (< (point) (org-element-property :end element)))))
  23121. ;; Arbitrary list of keywords where checks are meaningful.
  23122. ;; Make sure point is on the value part of the element.
  23123. (keyword
  23124. (and (member (org-element-property :key element)
  23125. '("DESCRIPTION" "TITLE"))
  23126. (save-excursion
  23127. (search-backward ":" (line-beginning-position) t))))
  23128. ;; Check is globally allowed in paragraphs verse blocks and
  23129. ;; table rows (after affiliated keywords) but some objects
  23130. ;; must not be affected.
  23131. ((paragraph table-row verse-block)
  23132. (let ((cbeg (org-element-property :contents-begin element))
  23133. (cend (org-element-property :contents-end element)))
  23134. (and cbeg (>= (point) cbeg) (< (point) cend)
  23135. (org--flyspell-object-check-p element))))))))))
  23136. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23137. (defun org-remove-flyspell-overlays-in (beg end)
  23138. "Remove flyspell overlays in region."
  23139. (and (org-bound-and-true-p flyspell-mode)
  23140. (fboundp 'flyspell-delete-region-overlays)
  23141. (flyspell-delete-region-overlays beg end)))
  23142. (defvar flyspell-delayed-commands)
  23143. (eval-after-load "flyspell"
  23144. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23145. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23146. (eval-after-load "bookmark"
  23147. '(if (boundp 'bookmark-after-jump-hook)
  23148. ;; We can use the hook
  23149. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23150. ;; Hook not available, use advice
  23151. (defadvice bookmark-jump (after org-make-visible activate)
  23152. "Make the position visible."
  23153. (org-bookmark-jump-unhide))))
  23154. ;; Make sure saveplace shows the location if it was hidden
  23155. (eval-after-load "saveplace"
  23156. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23157. "Make the position visible."
  23158. (org-bookmark-jump-unhide)))
  23159. ;; Make sure ecb shows the location if it was hidden
  23160. (eval-after-load "ecb"
  23161. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23162. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23163. (when (derived-mode-p 'org-mode)
  23164. (org-show-context))))
  23165. (defun org-bookmark-jump-unhide ()
  23166. "Unhide the current position, to show the bookmark location."
  23167. (and (derived-mode-p 'org-mode)
  23168. (or (outline-invisible-p)
  23169. (save-excursion (goto-char (max (point-min) (1- (point))))
  23170. (outline-invisible-p)))
  23171. (org-show-context 'bookmark-jump)))
  23172. (defun org-mark-jump-unhide ()
  23173. "Make the point visible with `org-show-context' after jumping to the mark."
  23174. (when (and (derived-mode-p 'org-mode)
  23175. (outline-invisible-p))
  23176. (org-show-context 'mark-goto)))
  23177. (eval-after-load "simple"
  23178. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23179. "Make the point visible with `org-show-context'."
  23180. (org-mark-jump-unhide)))
  23181. (eval-after-load "simple"
  23182. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23183. "Make the point visible with `org-show-context'."
  23184. (org-mark-jump-unhide)))
  23185. (eval-after-load "simple"
  23186. '(defadvice pop-global-mark (after org-make-visible activate)
  23187. "Make the point visible with `org-show-context'."
  23188. (org-mark-jump-unhide)))
  23189. ;; Make session.el ignore our circular variable
  23190. (defvar session-globals-exclude)
  23191. (eval-after-load "session"
  23192. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23193. ;;;; Finish up
  23194. (provide 'org)
  23195. (run-hooks 'org-load-hook)
  23196. ;;; org.el ends here